blob: 02b11efd70661776b40df6cfcef8093f1b5a8a42 [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
55struct vm_area_struct;
Ralf Baechle45b35a52006-06-08 00:43:41 -070056struct mm_struct;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58#ifdef CONFIG_NUMA
59
60/*
61 * Describe a memory policy.
62 *
63 * A mempolicy can be either associated with a process or with a VMA.
64 * For VMA related allocations the VMA policy is preferred, otherwise
65 * the process policy is used. Interrupts ignore the memory policy
66 * of the current process.
67 *
68 * Locking policy for interlave:
69 * In process context there is no locking because only the process accesses
70 * its own state. All vma manipulation is somewhat protected by a down_read on
Hugh Dickinsb8072f02005-10-29 18:16:41 -070071 * mmap_sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 *
73 * Freeing policy:
Mel Gorman19770b32008-04-28 02:12:18 -070074 * Mempolicy objects are reference counted. A mempolicy will be freed when
75 * mpol_free() decrements the reference count to zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 *
77 * Copying policy objects:
Mel Gorman19770b32008-04-28 02:12:18 -070078 * mpol_copy() allocates a new mempolicy and copies the specified mempolicy
79 * to the new storage. The reference count of the new object is initialized
80 * to 1, representing the caller of mpol_copy().
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 */
82struct mempolicy {
83 atomic_t refcnt;
David Rientjesa3b51e02008-04-28 02:12:23 -070084 unsigned short policy; /* See MPOL_* above */
David Rientjes028fec42008-04-28 02:12:25 -070085 unsigned short flags; /* See set_mempolicy() MPOL_F_* above */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 union {
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 short preferred_node; /* preferred */
Mel Gorman19770b32008-04-28 02:12:18 -070088 nodemask_t nodes; /* interleave/bind */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 /* undefined for default */
90 } v;
David Rientjesf5b087b2008-04-28 02:12:27 -070091 union {
92 nodemask_t cpuset_mems_allowed; /* relative to these nodes */
93 nodemask_t user_nodemask; /* nodemask passed by user */
94 } w;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095};
96
97/*
98 * Support for managing mempolicy data objects (clone, copy, destroy)
99 * The default fast path of a NULL MPOL_DEFAULT policy is always inlined.
100 */
101
102extern void __mpol_free(struct mempolicy *pol);
103static inline void mpol_free(struct mempolicy *pol)
104{
105 if (pol)
106 __mpol_free(pol);
107}
108
109extern struct mempolicy *__mpol_copy(struct mempolicy *pol);
110static inline struct mempolicy *mpol_copy(struct mempolicy *pol)
111{
112 if (pol)
113 pol = __mpol_copy(pol);
114 return pol;
115}
116
117#define vma_policy(vma) ((vma)->vm_policy)
118#define vma_set_policy(vma, pol) ((vma)->vm_policy = (pol))
119
120static inline void mpol_get(struct mempolicy *pol)
121{
122 if (pol)
123 atomic_inc(&pol->refcnt);
124}
125
126extern int __mpol_equal(struct mempolicy *a, struct mempolicy *b);
127static inline int mpol_equal(struct mempolicy *a, struct mempolicy *b)
128{
129 if (a == b)
130 return 1;
131 return __mpol_equal(a, b);
132}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134/* Could later add inheritance of the process policy here. */
135
136#define mpol_set_vma_default(vma) ((vma)->vm_policy = NULL)
137
138/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 * Tree of shared policies for a shared memory region.
140 * Maintain the policies in a pseudo mm that contains vmas. The vmas
141 * carry the policy. As a special twist the pseudo mm is indexed in pages, not
142 * bytes, so that we can work with shared memory segments bigger than
143 * unsigned long.
144 */
145
146struct sp_node {
147 struct rb_node nd;
148 unsigned long start, end;
149 struct mempolicy *policy;
150};
151
152struct shared_policy {
153 struct rb_root root;
154 spinlock_t lock;
155};
156
David Rientjesa3b51e02008-04-28 02:12:23 -0700157void mpol_shared_policy_init(struct shared_policy *info, unsigned short policy,
David Rientjes028fec42008-04-28 02:12:25 -0700158 unsigned short flags, nodemask_t *nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159int mpol_set_shared_policy(struct shared_policy *info,
160 struct vm_area_struct *vma,
161 struct mempolicy *new);
162void mpol_free_shared_policy(struct shared_policy *p);
163struct mempolicy *mpol_shared_policy_lookup(struct shared_policy *sp,
164 unsigned long idx);
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166extern void numa_default_policy(void);
167extern void numa_policy_init(void);
Paul Jackson74cb2152006-01-08 01:01:56 -0800168extern void mpol_rebind_task(struct task_struct *tsk,
169 const nodemask_t *new);
Paul Jackson42253992006-01-08 01:01:59 -0800170extern void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new);
Paul Jacksonc61afb12006-03-24 03:16:08 -0800171extern void mpol_fix_fork_child_flag(struct task_struct *p);
Paul Jackson42253992006-01-08 01:01:59 -0800172
Andi Kleend42c6992005-07-06 19:56:03 +0200173extern struct mempolicy default_policy;
Christoph Lameter5da7ca82006-01-06 00:10:46 -0800174extern struct zonelist *huge_zonelist(struct vm_area_struct *vma,
Mel Gorman19770b32008-04-28 02:12:18 -0700175 unsigned long addr, gfp_t gfp_flags,
176 struct mempolicy **mpol, nodemask_t **nodemask);
Christoph Lameterdc85da12006-01-18 17:42:36 -0800177extern unsigned slab_node(struct mempolicy *policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Christoph Lameter2f6726e2006-09-25 23:31:18 -0700179extern enum zone_type policy_zone;
Christoph Lameter4be38e32006-01-06 00:11:17 -0800180
Christoph Lameter2f6726e2006-09-25 23:31:18 -0700181static inline void check_highest_zone(enum zone_type k)
Christoph Lameter4be38e32006-01-06 00:11:17 -0800182{
Mel Gormanb377fd32007-08-22 14:02:05 -0700183 if (k > policy_zone && k != ZONE_MOVABLE)
Christoph Lameter4be38e32006-01-06 00:11:17 -0800184 policy_zone = k;
185}
186
Christoph Lameter39743882006-01-08 01:00:51 -0800187int do_migrate_pages(struct mm_struct *mm,
188 const nodemask_t *from_nodes, const nodemask_t *to_nodes, int flags);
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190#else
191
192struct mempolicy {};
193
194static inline int mpol_equal(struct mempolicy *a, struct mempolicy *b)
195{
196 return 1;
197}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199#define mpol_set_vma_default(vma) do {} while(0)
200
201static inline void mpol_free(struct mempolicy *p)
202{
203}
204
205static inline void mpol_get(struct mempolicy *pol)
206{
207}
208
209static inline struct mempolicy *mpol_copy(struct mempolicy *old)
210{
211 return NULL;
212}
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214struct shared_policy {};
215
216static inline int mpol_set_shared_policy(struct shared_policy *info,
217 struct vm_area_struct *vma,
218 struct mempolicy *new)
219{
220 return -EINVAL;
221}
222
Robin Holt7339ff82006-01-14 13:20:48 -0800223static inline void mpol_shared_policy_init(struct shared_policy *info,
David Rientjes028fec42008-04-28 02:12:25 -0700224 unsigned short policy, unsigned short flags, nodemask_t *nodes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226}
227
228static inline void mpol_free_shared_policy(struct shared_policy *p)
229{
230}
231
232static inline struct mempolicy *
233mpol_shared_policy_lookup(struct shared_policy *sp, unsigned long idx)
234{
235 return NULL;
236}
237
238#define vma_policy(vma) NULL
239#define vma_set_policy(vma, pol) do {} while(0)
240
241static inline void numa_policy_init(void)
242{
243}
244
245static inline void numa_default_policy(void)
246{
247}
248
Paul Jackson74cb2152006-01-08 01:01:56 -0800249static inline void mpol_rebind_task(struct task_struct *tsk,
Paul Jackson68860ec2005-10-30 15:02:36 -0800250 const nodemask_t *new)
251{
252}
253
Paul Jackson42253992006-01-08 01:01:59 -0800254static inline void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new)
255{
256}
257
Paul Jacksonc61afb12006-03-24 03:16:08 -0800258static inline void mpol_fix_fork_child_flag(struct task_struct *p)
259{
260}
261
Christoph Lameter5da7ca82006-01-06 00:10:46 -0800262static inline struct zonelist *huge_zonelist(struct vm_area_struct *vma,
Mel Gorman19770b32008-04-28 02:12:18 -0700263 unsigned long addr, gfp_t gfp_flags,
264 struct mempolicy **mpol, nodemask_t **nodemask)
Christoph Lameter5da7ca82006-01-06 00:10:46 -0800265{
Mel Gorman19770b32008-04-28 02:12:18 -0700266 *mpol = NULL;
267 *nodemask = NULL;
Mel Gorman0e884602008-04-28 02:12:14 -0700268 return node_zonelist(0, gfp_flags);
Christoph Lameter5da7ca82006-01-06 00:10:46 -0800269}
270
Paul Jackson45b07ef2006-01-08 01:00:56 -0800271static inline int do_migrate_pages(struct mm_struct *mm,
272 const nodemask_t *from_nodes,
273 const nodemask_t *to_nodes, int flags)
274{
275 return 0;
276}
277
Christoph Lameter4be38e32006-01-06 00:11:17 -0800278static inline void check_highest_zone(int k)
279{
280}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281#endif /* CONFIG_NUMA */
282#endif /* __KERNEL__ */
283
284#endif