blob: 5e19c2275a6fc0dc7f7203832f6b46aabd872d31 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _LINUX_MEMPOLICY_H
2#define _LINUX_MEMPOLICY_H 1
3
4#include <linux/errno.h>
5
6/*
7 * NUMA memory policies for Linux.
8 * Copyright 2003,2004 Andi Kleen SuSE Labs
9 */
10
David Rientjes028fec42008-04-28 02:12:25 -070011/*
12 * Both the MPOL_* mempolicy mode and the MPOL_F_* optional mode flags are
13 * passed by the user to either set_mempolicy() or mbind() in an 'int' actual.
14 * The MPOL_MODE_FLAGS macro determines the legal set of optional mode flags.
15 */
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017/* Policies */
David Rientjesa3b51e02008-04-28 02:12:23 -070018enum {
19 MPOL_DEFAULT,
20 MPOL_PREFERRED,
21 MPOL_BIND,
22 MPOL_INTERLEAVE,
23 MPOL_MAX, /* always last member of enum */
24};
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
David Rientjes028fec42008-04-28 02:12:25 -070026/* Flags for set_mempolicy */
David Rientjesf5b087b2008-04-28 02:12:27 -070027#define MPOL_F_STATIC_NODES (1 << 15)
David Rientjes4c50bc02008-04-28 02:12:30 -070028#define MPOL_F_RELATIVE_NODES (1 << 14)
David Rientjesf5b087b2008-04-28 02:12:27 -070029
David Rientjes028fec42008-04-28 02:12:25 -070030/*
31 * MPOL_MODE_FLAGS is the union of all possible optional mode flags passed to
32 * either set_mempolicy() or mbind().
33 */
David Rientjes4c50bc02008-04-28 02:12:30 -070034#define MPOL_MODE_FLAGS (MPOL_F_STATIC_NODES | MPOL_F_RELATIVE_NODES)
David Rientjes028fec42008-04-28 02:12:25 -070035
36/* Flags for get_mempolicy */
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define MPOL_F_NODE (1<<0) /* return next IL mode instead of node mask */
38#define MPOL_F_ADDR (1<<1) /* look up vma using address */
Lee Schermerhorn754af6f2007-10-16 01:24:51 -070039#define MPOL_F_MEMS_ALLOWED (1<<2) /* return allowed memories */
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/* Flags for mbind */
42#define MPOL_MF_STRICT (1<<0) /* Verify existing pages in the mapping */
Christoph Lameterdc9aa5b2006-01-08 01:00:50 -080043#define MPOL_MF_MOVE (1<<1) /* Move pages owned by this process to conform to mapping */
44#define MPOL_MF_MOVE_ALL (1<<2) /* Move every page to conform to mapping */
45#define MPOL_MF_INTERNAL (1<<3) /* Internal flags start here */
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#ifdef __KERNEL__
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/mmzone.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/slab.h>
51#include <linux/rbtree.h>
52#include <linux/spinlock.h>
Andi Kleendfcd3c02005-10-29 18:15:48 -070053#include <linux/nodemask.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Ralf Baechle45b35a52006-06-08 00:43:41 -070055struct mm_struct;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57#ifdef CONFIG_NUMA
58
59/*
60 * Describe a memory policy.
61 *
62 * A mempolicy can be either associated with a process or with a VMA.
63 * For VMA related allocations the VMA policy is preferred, otherwise
64 * the process policy is used. Interrupts ignore the memory policy
65 * of the current process.
66 *
67 * Locking policy for interlave:
68 * In process context there is no locking because only the process accesses
69 * its own state. All vma manipulation is somewhat protected by a down_read on
Hugh Dickinsb8072f02005-10-29 18:16:41 -070070 * mmap_sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 *
72 * Freeing policy:
Mel Gorman19770b32008-04-28 02:12:18 -070073 * Mempolicy objects are reference counted. A mempolicy will be freed when
Lee Schermerhornf0be3d32008-04-28 02:13:08 -070074 * mpol_put() decrements the reference count to zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 *
Lee Schermerhorn846a16b2008-04-28 02:13:09 -070076 * Duplicating policy objects:
77 * mpol_dup() allocates a new mempolicy and copies the specified mempolicy
Mel Gorman19770b32008-04-28 02:12:18 -070078 * to the new storage. The reference count of the new object is initialized
Lee Schermerhorn846a16b2008-04-28 02:13:09 -070079 * to 1, representing the caller of mpol_dup().
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 */
81struct mempolicy {
82 atomic_t refcnt;
David Rientjesa3b51e02008-04-28 02:12:23 -070083 unsigned short policy; /* See MPOL_* above */
David Rientjes028fec42008-04-28 02:12:25 -070084 unsigned short flags; /* See set_mempolicy() MPOL_F_* above */
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 union {
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 short preferred_node; /* preferred */
Mel Gorman19770b32008-04-28 02:12:18 -070087 nodemask_t nodes; /* interleave/bind */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 /* undefined for default */
89 } v;
David Rientjesf5b087b2008-04-28 02:12:27 -070090 union {
91 nodemask_t cpuset_mems_allowed; /* relative to these nodes */
92 nodemask_t user_nodemask; /* nodemask passed by user */
93 } w;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094};
95
96/*
97 * Support for managing mempolicy data objects (clone, copy, destroy)
98 * The default fast path of a NULL MPOL_DEFAULT policy is always inlined.
99 */
100
Lee Schermerhornf0be3d32008-04-28 02:13:08 -0700101extern void __mpol_put(struct mempolicy *pol);
102static inline void mpol_put(struct mempolicy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
104 if (pol)
Lee Schermerhornf0be3d32008-04-28 02:13:08 -0700105 __mpol_put(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Lee Schermerhorn846a16b2008-04-28 02:13:09 -0700108extern struct mempolicy *__mpol_dup(struct mempolicy *pol);
109static inline struct mempolicy *mpol_dup(struct mempolicy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 if (pol)
Lee Schermerhorn846a16b2008-04-28 02:13:09 -0700112 pol = __mpol_dup(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return pol;
114}
115
116#define vma_policy(vma) ((vma)->vm_policy)
117#define vma_set_policy(vma, pol) ((vma)->vm_policy = (pol))
118
119static inline void mpol_get(struct mempolicy *pol)
120{
121 if (pol)
122 atomic_inc(&pol->refcnt);
123}
124
125extern int __mpol_equal(struct mempolicy *a, struct mempolicy *b);
126static inline int mpol_equal(struct mempolicy *a, struct mempolicy *b)
127{
128 if (a == b)
129 return 1;
130 return __mpol_equal(a, b);
131}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 * Tree of shared policies for a shared memory region.
135 * Maintain the policies in a pseudo mm that contains vmas. The vmas
136 * carry the policy. As a special twist the pseudo mm is indexed in pages, not
137 * bytes, so that we can work with shared memory segments bigger than
138 * unsigned long.
139 */
140
141struct sp_node {
142 struct rb_node nd;
143 unsigned long start, end;
144 struct mempolicy *policy;
145};
146
147struct shared_policy {
148 struct rb_root root;
149 spinlock_t lock;
150};
151
David Rientjesa3b51e02008-04-28 02:12:23 -0700152void mpol_shared_policy_init(struct shared_policy *info, unsigned short policy,
David Rientjes028fec42008-04-28 02:12:25 -0700153 unsigned short flags, nodemask_t *nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154int mpol_set_shared_policy(struct shared_policy *info,
155 struct vm_area_struct *vma,
156 struct mempolicy *new);
157void mpol_free_shared_policy(struct shared_policy *p);
158struct mempolicy *mpol_shared_policy_lookup(struct shared_policy *sp,
159 unsigned long idx);
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161extern void numa_default_policy(void);
162extern void numa_policy_init(void);
Paul Jackson74cb2152006-01-08 01:01:56 -0800163extern void mpol_rebind_task(struct task_struct *tsk,
164 const nodemask_t *new);
Paul Jackson42253992006-01-08 01:01:59 -0800165extern void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new);
Paul Jacksonc61afb12006-03-24 03:16:08 -0800166extern void mpol_fix_fork_child_flag(struct task_struct *p);
Paul Jackson42253992006-01-08 01:01:59 -0800167
Christoph Lameter5da7ca82006-01-06 00:10:46 -0800168extern struct zonelist *huge_zonelist(struct vm_area_struct *vma,
Mel Gorman19770b32008-04-28 02:12:18 -0700169 unsigned long addr, gfp_t gfp_flags,
170 struct mempolicy **mpol, nodemask_t **nodemask);
Christoph Lameterdc85da12006-01-18 17:42:36 -0800171extern unsigned slab_node(struct mempolicy *policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Christoph Lameter2f6726e2006-09-25 23:31:18 -0700173extern enum zone_type policy_zone;
Christoph Lameter4be38e32006-01-06 00:11:17 -0800174
Christoph Lameter2f6726e2006-09-25 23:31:18 -0700175static inline void check_highest_zone(enum zone_type k)
Christoph Lameter4be38e32006-01-06 00:11:17 -0800176{
Mel Gormanb377fd32007-08-22 14:02:05 -0700177 if (k > policy_zone && k != ZONE_MOVABLE)
Christoph Lameter4be38e32006-01-06 00:11:17 -0800178 policy_zone = k;
179}
180
Christoph Lameter39743882006-01-08 01:00:51 -0800181int do_migrate_pages(struct mm_struct *mm,
182 const nodemask_t *from_nodes, const nodemask_t *to_nodes, int flags);
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184#else
185
186struct mempolicy {};
187
188static inline int mpol_equal(struct mempolicy *a, struct mempolicy *b)
189{
190 return 1;
191}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Lee Schermerhornf0be3d32008-04-28 02:13:08 -0700193static inline void mpol_put(struct mempolicy *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195}
196
197static inline void mpol_get(struct mempolicy *pol)
198{
199}
200
Lee Schermerhorn846a16b2008-04-28 02:13:09 -0700201static inline struct mempolicy *mpol_dup(struct mempolicy *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 return NULL;
204}
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206struct shared_policy {};
207
208static inline int mpol_set_shared_policy(struct shared_policy *info,
209 struct vm_area_struct *vma,
210 struct mempolicy *new)
211{
212 return -EINVAL;
213}
214
Robin Holt7339ff82006-01-14 13:20:48 -0800215static inline void mpol_shared_policy_init(struct shared_policy *info,
David Rientjes028fec42008-04-28 02:12:25 -0700216 unsigned short policy, unsigned short flags, nodemask_t *nodes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218}
219
220static inline void mpol_free_shared_policy(struct shared_policy *p)
221{
222}
223
224static inline struct mempolicy *
225mpol_shared_policy_lookup(struct shared_policy *sp, unsigned long idx)
226{
227 return NULL;
228}
229
230#define vma_policy(vma) NULL
231#define vma_set_policy(vma, pol) do {} while(0)
232
233static inline void numa_policy_init(void)
234{
235}
236
237static inline void numa_default_policy(void)
238{
239}
240
Paul Jackson74cb2152006-01-08 01:01:56 -0800241static inline void mpol_rebind_task(struct task_struct *tsk,
Paul Jackson68860ec2005-10-30 15:02:36 -0800242 const nodemask_t *new)
243{
244}
245
Paul Jackson42253992006-01-08 01:01:59 -0800246static inline void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new)
247{
248}
249
Paul Jacksonc61afb12006-03-24 03:16:08 -0800250static inline void mpol_fix_fork_child_flag(struct task_struct *p)
251{
252}
253
Christoph Lameter5da7ca82006-01-06 00:10:46 -0800254static inline struct zonelist *huge_zonelist(struct vm_area_struct *vma,
Mel Gorman19770b32008-04-28 02:12:18 -0700255 unsigned long addr, gfp_t gfp_flags,
256 struct mempolicy **mpol, nodemask_t **nodemask)
Christoph Lameter5da7ca82006-01-06 00:10:46 -0800257{
Mel Gorman19770b32008-04-28 02:12:18 -0700258 *mpol = NULL;
259 *nodemask = NULL;
Mel Gorman0e884602008-04-28 02:12:14 -0700260 return node_zonelist(0, gfp_flags);
Christoph Lameter5da7ca82006-01-06 00:10:46 -0800261}
262
Paul Jackson45b07ef2006-01-08 01:00:56 -0800263static inline int do_migrate_pages(struct mm_struct *mm,
264 const nodemask_t *from_nodes,
265 const nodemask_t *to_nodes, int flags)
266{
267 return 0;
268}
269
Christoph Lameter4be38e32006-01-06 00:11:17 -0800270static inline void check_highest_zone(int k)
271{
272}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273#endif /* CONFIG_NUMA */
274#endif /* __KERNEL__ */
275
276#endif