blob: 4aa42732e47f34ca29392180fbee0fc73a086cd2 [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
Miao Xie708c1bb2010-05-24 14:32:07 -070026enum mpol_rebind_step {
27 MPOL_REBIND_ONCE, /* do rebind work at once(not by two step) */
28 MPOL_REBIND_STEP1, /* first step(set all the newly nodes) */
29 MPOL_REBIND_STEP2, /* second step(clean all the disallowed nodes)*/
30 MPOL_REBIND_NSTEP,
31};
32
David Rientjes028fec42008-04-28 02:12:25 -070033/* Flags for set_mempolicy */
David Rientjesf5b087b2008-04-28 02:12:27 -070034#define MPOL_F_STATIC_NODES (1 << 15)
David Rientjes4c50bc02008-04-28 02:12:30 -070035#define MPOL_F_RELATIVE_NODES (1 << 14)
David Rientjesf5b087b2008-04-28 02:12:27 -070036
David Rientjes028fec42008-04-28 02:12:25 -070037/*
38 * MPOL_MODE_FLAGS is the union of all possible optional mode flags passed to
39 * either set_mempolicy() or mbind().
40 */
David Rientjes4c50bc02008-04-28 02:12:30 -070041#define MPOL_MODE_FLAGS (MPOL_F_STATIC_NODES | MPOL_F_RELATIVE_NODES)
David Rientjes028fec42008-04-28 02:12:25 -070042
43/* Flags for get_mempolicy */
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define MPOL_F_NODE (1<<0) /* return next IL mode instead of node mask */
45#define MPOL_F_ADDR (1<<1) /* look up vma using address */
Lee Schermerhorn754af6f2007-10-16 01:24:51 -070046#define MPOL_F_MEMS_ALLOWED (1<<2) /* return allowed memories */
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48/* Flags for mbind */
49#define MPOL_MF_STRICT (1<<0) /* Verify existing pages in the mapping */
Christoph Lameterdc9aa5b2006-01-08 01:00:50 -080050#define MPOL_MF_MOVE (1<<1) /* Move pages owned by this process to conform to mapping */
51#define MPOL_MF_MOVE_ALL (1<<2) /* Move every page to conform to mapping */
52#define MPOL_MF_INTERNAL (1<<3) /* Internal flags start here */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Lee Schermerhornaab0b102008-04-28 02:13:13 -070054/*
55 * Internal flags that share the struct mempolicy flags word with
56 * "mode flags". These flags are allocated from bit 0 up, as they
57 * are never OR'ed into the mode in mempolicy API arguments.
58 */
59#define MPOL_F_SHARED (1 << 0) /* identify shared policies */
Lee Schermerhornfc36b8d2008-04-28 02:13:21 -070060#define MPOL_F_LOCAL (1 << 1) /* preferred local allocation */
Miao Xie708c1bb2010-05-24 14:32:07 -070061#define MPOL_F_REBINDING (1 << 2) /* identify policies in rebinding */
Lee Schermerhornaab0b102008-04-28 02:13:13 -070062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#ifdef __KERNEL__
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/mmzone.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/slab.h>
67#include <linux/rbtree.h>
68#include <linux/spinlock.h>
Andi Kleendfcd3c02005-10-29 18:15:48 -070069#include <linux/nodemask.h>
Gerald Schaefer83d16742008-07-23 21:28:22 -070070#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Ralf Baechle45b35a52006-06-08 00:43:41 -070072struct mm_struct;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74#ifdef CONFIG_NUMA
75
76/*
77 * Describe a memory policy.
78 *
79 * A mempolicy can be either associated with a process or with a VMA.
80 * For VMA related allocations the VMA policy is preferred, otherwise
81 * the process policy is used. Interrupts ignore the memory policy
82 * of the current process.
83 *
84 * Locking policy for interlave:
85 * In process context there is no locking because only the process accesses
86 * its own state. All vma manipulation is somewhat protected by a down_read on
Hugh Dickinsb8072f02005-10-29 18:16:41 -070087 * mmap_sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 *
89 * Freeing policy:
Mel Gorman19770b32008-04-28 02:12:18 -070090 * Mempolicy objects are reference counted. A mempolicy will be freed when
Lee Schermerhornf0be3d32008-04-28 02:13:08 -070091 * mpol_put() decrements the reference count to zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 *
Lee Schermerhorn846a16b2008-04-28 02:13:09 -070093 * Duplicating policy objects:
94 * mpol_dup() allocates a new mempolicy and copies the specified mempolicy
Mel Gorman19770b32008-04-28 02:12:18 -070095 * to the new storage. The reference count of the new object is initialized
Lee Schermerhorn846a16b2008-04-28 02:13:09 -070096 * to 1, representing the caller of mpol_dup().
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 */
98struct mempolicy {
99 atomic_t refcnt;
Lee Schermerhorn45c47452008-04-28 02:13:12 -0700100 unsigned short mode; /* See MPOL_* above */
David Rientjes028fec42008-04-28 02:12:25 -0700101 unsigned short flags; /* See set_mempolicy() MPOL_F_* above */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 union {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 short preferred_node; /* preferred */
Mel Gorman19770b32008-04-28 02:12:18 -0700104 nodemask_t nodes; /* interleave/bind */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 /* undefined for default */
106 } v;
David Rientjesf5b087b2008-04-28 02:12:27 -0700107 union {
108 nodemask_t cpuset_mems_allowed; /* relative to these nodes */
109 nodemask_t user_nodemask; /* nodemask passed by user */
110 } w;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111};
112
113/*
114 * Support for managing mempolicy data objects (clone, copy, destroy)
115 * The default fast path of a NULL MPOL_DEFAULT policy is always inlined.
116 */
117
Lee Schermerhornf0be3d32008-04-28 02:13:08 -0700118extern void __mpol_put(struct mempolicy *pol);
119static inline void mpol_put(struct mempolicy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
121 if (pol)
Lee Schermerhornf0be3d32008-04-28 02:13:08 -0700122 __mpol_put(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700125/*
126 * Does mempolicy pol need explicit unref after use?
127 * Currently only needed for shared policies.
128 */
129static inline int mpol_needs_cond_ref(struct mempolicy *pol)
130{
131 return (pol && (pol->flags & MPOL_F_SHARED));
132}
133
134static inline void mpol_cond_put(struct mempolicy *pol)
135{
136 if (mpol_needs_cond_ref(pol))
137 __mpol_put(pol);
138}
139
140extern struct mempolicy *__mpol_cond_copy(struct mempolicy *tompol,
141 struct mempolicy *frompol);
142static inline struct mempolicy *mpol_cond_copy(struct mempolicy *tompol,
143 struct mempolicy *frompol)
144{
145 if (!frompol)
146 return frompol;
147 return __mpol_cond_copy(tompol, frompol);
148}
149
Lee Schermerhorn846a16b2008-04-28 02:13:09 -0700150extern struct mempolicy *__mpol_dup(struct mempolicy *pol);
151static inline struct mempolicy *mpol_dup(struct mempolicy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 if (pol)
Lee Schermerhorn846a16b2008-04-28 02:13:09 -0700154 pol = __mpol_dup(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return pol;
156}
157
158#define vma_policy(vma) ((vma)->vm_policy)
159#define vma_set_policy(vma, pol) ((vma)->vm_policy = (pol))
160
161static inline void mpol_get(struct mempolicy *pol)
162{
163 if (pol)
164 atomic_inc(&pol->refcnt);
165}
166
KOSAKI Motohirofcfb4dc2012-01-10 15:08:21 -0800167extern bool __mpol_equal(struct mempolicy *a, struct mempolicy *b);
168static inline bool mpol_equal(struct mempolicy *a, struct mempolicy *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
170 if (a == b)
KOSAKI Motohirofcfb4dc2012-01-10 15:08:21 -0800171 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return __mpol_equal(a, b);
173}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 * Tree of shared policies for a shared memory region.
177 * Maintain the policies in a pseudo mm that contains vmas. The vmas
178 * carry the policy. As a special twist the pseudo mm is indexed in pages, not
179 * bytes, so that we can work with shared memory segments bigger than
180 * unsigned long.
181 */
182
183struct sp_node {
184 struct rb_node nd;
185 unsigned long start, end;
186 struct mempolicy *policy;
187};
188
189struct shared_policy {
190 struct rb_root root;
191 spinlock_t lock;
192};
193
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700194void mpol_shared_policy_init(struct shared_policy *sp, struct mempolicy *mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195int mpol_set_shared_policy(struct shared_policy *info,
196 struct vm_area_struct *vma,
197 struct mempolicy *new);
198void mpol_free_shared_policy(struct shared_policy *p);
199struct mempolicy *mpol_shared_policy_lookup(struct shared_policy *sp,
200 unsigned long idx);
201
Stephen Wilsond98f6cb2011-05-24 17:12:41 -0700202struct mempolicy *get_vma_policy(struct task_struct *tsk,
203 struct vm_area_struct *vma, unsigned long addr);
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205extern void numa_default_policy(void);
206extern void numa_policy_init(void);
Miao Xie708c1bb2010-05-24 14:32:07 -0700207extern void mpol_rebind_task(struct task_struct *tsk, const nodemask_t *new,
208 enum mpol_rebind_step step);
Paul Jackson42253992006-01-08 01:01:59 -0800209extern void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new);
Paul Jacksonc61afb12006-03-24 03:16:08 -0800210extern void mpol_fix_fork_child_flag(struct task_struct *p);
Paul Jackson42253992006-01-08 01:01:59 -0800211
Christoph Lameter5da7ca82006-01-06 00:10:46 -0800212extern struct zonelist *huge_zonelist(struct vm_area_struct *vma,
Mel Gorman19770b32008-04-28 02:12:18 -0700213 unsigned long addr, gfp_t gfp_flags,
214 struct mempolicy **mpol, nodemask_t **nodemask);
Lee Schermerhorn06808b02009-12-14 17:58:21 -0800215extern bool init_nodemask_of_mempolicy(nodemask_t *mask);
David Rientjes6f48d0eb2010-08-09 17:18:52 -0700216extern bool mempolicy_nodemask_intersects(struct task_struct *tsk,
217 const nodemask_t *mask);
Christoph Lameterdc85da12006-01-18 17:42:36 -0800218extern unsigned slab_node(struct mempolicy *policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Christoph Lameter2f6726e2006-09-25 23:31:18 -0700220extern enum zone_type policy_zone;
Christoph Lameter4be38e32006-01-06 00:11:17 -0800221
Christoph Lameter2f6726e2006-09-25 23:31:18 -0700222static inline void check_highest_zone(enum zone_type k)
Christoph Lameter4be38e32006-01-06 00:11:17 -0800223{
Mel Gormanb377fd32007-08-22 14:02:05 -0700224 if (k > policy_zone && k != ZONE_MOVABLE)
Christoph Lameter4be38e32006-01-06 00:11:17 -0800225 policy_zone = k;
226}
227
Andrew Morton0ce72d42012-05-29 15:06:24 -0700228int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from,
229 const nodemask_t *to, int flags);
Christoph Lameter39743882006-01-08 01:00:51 -0800230
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700231
232#ifdef CONFIG_TMPFS
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700233extern int mpol_parse_str(char *str, struct mempolicy **mpol, int no_context);
Stephen Wilson13057ef2011-05-24 17:12:46 -0700234#endif
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700235
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700236extern int mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol,
237 int no_context);
Gerald Schaefer83d16742008-07-23 21:28:22 -0700238
239/* Check if a vma is migratable */
240static inline int vma_migratable(struct vm_area_struct *vma)
241{
242 if (vma->vm_flags & (VM_IO|VM_HUGETLB|VM_PFNMAP|VM_RESERVED))
243 return 0;
244 /*
245 * Migration allocates pages in the highest zone. If we cannot
246 * do so then migration (at least from node to node) is not
247 * possible.
248 */
249 if (vma->vm_file &&
250 gfp_zone(mapping_gfp_mask(vma->vm_file->f_mapping))
251 < policy_zone)
252 return 0;
253 return 1;
254}
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256#else
257
258struct mempolicy {};
259
KOSAKI Motohirofcfb4dc2012-01-10 15:08:21 -0800260static inline bool mpol_equal(struct mempolicy *a, struct mempolicy *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
KOSAKI Motohirofcfb4dc2012-01-10 15:08:21 -0800262 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Lee Schermerhornf0be3d32008-04-28 02:13:08 -0700265static inline void mpol_put(struct mempolicy *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267}
268
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700269static inline void mpol_cond_put(struct mempolicy *pol)
270{
271}
272
273static inline struct mempolicy *mpol_cond_copy(struct mempolicy *to,
274 struct mempolicy *from)
275{
276 return from;
277}
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279static inline void mpol_get(struct mempolicy *pol)
280{
281}
282
Lee Schermerhorn846a16b2008-04-28 02:13:09 -0700283static inline struct mempolicy *mpol_dup(struct mempolicy *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 return NULL;
286}
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288struct shared_policy {};
289
290static inline int mpol_set_shared_policy(struct shared_policy *info,
291 struct vm_area_struct *vma,
292 struct mempolicy *new)
293{
294 return -EINVAL;
295}
296
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700297static inline void mpol_shared_policy_init(struct shared_policy *sp,
298 struct mempolicy *mpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300}
301
302static inline void mpol_free_shared_policy(struct shared_policy *p)
303{
304}
305
306static inline struct mempolicy *
307mpol_shared_policy_lookup(struct shared_policy *sp, unsigned long idx)
308{
309 return NULL;
310}
311
312#define vma_policy(vma) NULL
313#define vma_set_policy(vma, pol) do {} while(0)
314
315static inline void numa_policy_init(void)
316{
317}
318
319static inline void numa_default_policy(void)
320{
321}
322
Paul Jackson74cb2152006-01-08 01:01:56 -0800323static inline void mpol_rebind_task(struct task_struct *tsk,
Miao Xie708c1bb2010-05-24 14:32:07 -0700324 const nodemask_t *new,
325 enum mpol_rebind_step step)
Paul Jackson68860ec2005-10-30 15:02:36 -0800326{
327}
328
Paul Jackson42253992006-01-08 01:01:59 -0800329static inline void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new)
330{
331}
332
Paul Jacksonc61afb12006-03-24 03:16:08 -0800333static inline void mpol_fix_fork_child_flag(struct task_struct *p)
334{
335}
336
Christoph Lameter5da7ca82006-01-06 00:10:46 -0800337static inline struct zonelist *huge_zonelist(struct vm_area_struct *vma,
Mel Gorman19770b32008-04-28 02:12:18 -0700338 unsigned long addr, gfp_t gfp_flags,
339 struct mempolicy **mpol, nodemask_t **nodemask)
Christoph Lameter5da7ca82006-01-06 00:10:46 -0800340{
Mel Gorman19770b32008-04-28 02:12:18 -0700341 *mpol = NULL;
342 *nodemask = NULL;
Mel Gorman0e884602008-04-28 02:12:14 -0700343 return node_zonelist(0, gfp_flags);
Christoph Lameter5da7ca82006-01-06 00:10:46 -0800344}
345
David Rientjes6f48d0eb2010-08-09 17:18:52 -0700346static inline bool init_nodemask_of_mempolicy(nodemask_t *m)
347{
348 return false;
349}
350
351static inline bool mempolicy_nodemask_intersects(struct task_struct *tsk,
352 const nodemask_t *mask)
353{
354 return false;
355}
Lee Schermerhorn06808b02009-12-14 17:58:21 -0800356
Andrew Morton0ce72d42012-05-29 15:06:24 -0700357static inline int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from,
358 const nodemask_t *to, int flags)
Paul Jackson45b07ef2006-01-08 01:00:56 -0800359{
360 return 0;
361}
362
Christoph Lameter4be38e32006-01-06 00:11:17 -0800363static inline void check_highest_zone(int k)
364{
365}
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700366
367#ifdef CONFIG_TMPFS
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700368static inline int mpol_parse_str(char *str, struct mempolicy **mpol,
369 int no_context)
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700370{
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700371 return 1; /* error */
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700372}
Stephen Wilson13057ef2011-05-24 17:12:46 -0700373#endif
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700374
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700375static inline int mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol,
376 int no_context)
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700377{
378 return 0;
379}
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381#endif /* CONFIG_NUMA */
382#endif /* __KERNEL__ */
383
384#endif