blob: 59c4865bc85f13fbe36beefd959ab499f1d5aaaf [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
11/* Policies */
12#define MPOL_DEFAULT 0
13#define MPOL_PREFERRED 1
14#define MPOL_BIND 2
15#define MPOL_INTERLEAVE 3
16
17#define MPOL_MAX MPOL_INTERLEAVE
18
19/* Flags for get_mem_policy */
20#define MPOL_F_NODE (1<<0) /* return next IL mode instead of node mask */
21#define MPOL_F_ADDR (1<<1) /* look up vma using address */
Lee Schermerhorn754af6f2007-10-16 01:24:51 -070022#define MPOL_F_MEMS_ALLOWED (1<<2) /* return allowed memories */
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24/* Flags for mbind */
25#define MPOL_MF_STRICT (1<<0) /* Verify existing pages in the mapping */
Christoph Lameterdc9aa5b2006-01-08 01:00:50 -080026#define MPOL_MF_MOVE (1<<1) /* Move pages owned by this process to conform to mapping */
27#define MPOL_MF_MOVE_ALL (1<<2) /* Move every page to conform to mapping */
28#define MPOL_MF_INTERNAL (1<<3) /* Internal flags start here */
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#ifdef __KERNEL__
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/mmzone.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/slab.h>
34#include <linux/rbtree.h>
35#include <linux/spinlock.h>
Andi Kleendfcd3c02005-10-29 18:15:48 -070036#include <linux/nodemask.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38struct vm_area_struct;
Ralf Baechle45b35a52006-06-08 00:43:41 -070039struct mm_struct;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#ifdef CONFIG_NUMA
42
43/*
44 * Describe a memory policy.
45 *
46 * A mempolicy can be either associated with a process or with a VMA.
47 * For VMA related allocations the VMA policy is preferred, otherwise
48 * the process policy is used. Interrupts ignore the memory policy
49 * of the current process.
50 *
51 * Locking policy for interlave:
52 * In process context there is no locking because only the process accesses
53 * its own state. All vma manipulation is somewhat protected by a down_read on
Hugh Dickinsb8072f02005-10-29 18:16:41 -070054 * mmap_sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 *
56 * Freeing policy:
57 * When policy is MPOL_BIND v.zonelist is kmalloc'ed and must be kfree'd.
58 * All other policies don't have any external state. mpol_free() handles this.
59 *
60 * Copying policy objects:
61 * For MPOL_BIND the zonelist must be always duplicated. mpol_clone() does this.
62 */
63struct mempolicy {
64 atomic_t refcnt;
65 short policy; /* See MPOL_* above */
66 union {
67 struct zonelist *zonelist; /* bind */
68 short preferred_node; /* preferred */
Andi Kleendfcd3c02005-10-29 18:15:48 -070069 nodemask_t nodes; /* interleave */
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 /* undefined for default */
71 } v;
Paul Jackson74cb2152006-01-08 01:01:56 -080072 nodemask_t cpuset_mems_allowed; /* mempolicy relative to these nodes */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073};
74
75/*
76 * Support for managing mempolicy data objects (clone, copy, destroy)
77 * The default fast path of a NULL MPOL_DEFAULT policy is always inlined.
78 */
79
80extern void __mpol_free(struct mempolicy *pol);
81static inline void mpol_free(struct mempolicy *pol)
82{
83 if (pol)
84 __mpol_free(pol);
85}
86
87extern struct mempolicy *__mpol_copy(struct mempolicy *pol);
88static inline struct mempolicy *mpol_copy(struct mempolicy *pol)
89{
90 if (pol)
91 pol = __mpol_copy(pol);
92 return pol;
93}
94
95#define vma_policy(vma) ((vma)->vm_policy)
96#define vma_set_policy(vma, pol) ((vma)->vm_policy = (pol))
97
98static inline void mpol_get(struct mempolicy *pol)
99{
100 if (pol)
101 atomic_inc(&pol->refcnt);
102}
103
104extern int __mpol_equal(struct mempolicy *a, struct mempolicy *b);
105static inline int mpol_equal(struct mempolicy *a, struct mempolicy *b)
106{
107 if (a == b)
108 return 1;
109 return __mpol_equal(a, b);
110}
111#define vma_mpol_equal(a,b) mpol_equal(vma_policy(a), vma_policy(b))
112
113/* Could later add inheritance of the process policy here. */
114
115#define mpol_set_vma_default(vma) ((vma)->vm_policy = NULL)
116
117/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 * Tree of shared policies for a shared memory region.
119 * Maintain the policies in a pseudo mm that contains vmas. The vmas
120 * carry the policy. As a special twist the pseudo mm is indexed in pages, not
121 * bytes, so that we can work with shared memory segments bigger than
122 * unsigned long.
123 */
124
125struct sp_node {
126 struct rb_node nd;
127 unsigned long start, end;
128 struct mempolicy *policy;
129};
130
131struct shared_policy {
132 struct rb_root root;
133 spinlock_t lock;
134};
135
Robin Holt7339ff82006-01-14 13:20:48 -0800136void mpol_shared_policy_init(struct shared_policy *info, int policy,
137 nodemask_t *nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138int mpol_set_shared_policy(struct shared_policy *info,
139 struct vm_area_struct *vma,
140 struct mempolicy *new);
141void mpol_free_shared_policy(struct shared_policy *p);
142struct mempolicy *mpol_shared_policy_lookup(struct shared_policy *sp,
143 unsigned long idx);
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145extern void numa_default_policy(void);
146extern void numa_policy_init(void);
Paul Jackson74cb2152006-01-08 01:01:56 -0800147extern void mpol_rebind_task(struct task_struct *tsk,
148 const nodemask_t *new);
Paul Jackson42253992006-01-08 01:01:59 -0800149extern void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new);
Paul Jacksonc61afb12006-03-24 03:16:08 -0800150extern void mpol_fix_fork_child_flag(struct task_struct *p);
Paul Jackson42253992006-01-08 01:01:59 -0800151
Andi Kleend42c6992005-07-06 19:56:03 +0200152extern struct mempolicy default_policy;
Christoph Lameter5da7ca82006-01-06 00:10:46 -0800153extern struct zonelist *huge_zonelist(struct vm_area_struct *vma,
Lee Schermerhorn480eccf2007-09-18 22:46:47 -0700154 unsigned long addr, gfp_t gfp_flags, struct mempolicy **mpol);
Christoph Lameterdc85da12006-01-18 17:42:36 -0800155extern unsigned slab_node(struct mempolicy *policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Christoph Lameter2f6726e2006-09-25 23:31:18 -0700157extern enum zone_type policy_zone;
Christoph Lameter4be38e32006-01-06 00:11:17 -0800158
Christoph Lameter2f6726e2006-09-25 23:31:18 -0700159static inline void check_highest_zone(enum zone_type k)
Christoph Lameter4be38e32006-01-06 00:11:17 -0800160{
Mel Gormanb377fd32007-08-22 14:02:05 -0700161 if (k > policy_zone && k != ZONE_MOVABLE)
Christoph Lameter4be38e32006-01-06 00:11:17 -0800162 policy_zone = k;
163}
164
Christoph Lameter39743882006-01-08 01:00:51 -0800165int do_migrate_pages(struct mm_struct *mm,
166 const nodemask_t *from_nodes, const nodemask_t *to_nodes, int flags);
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168#else
169
170struct mempolicy {};
171
172static inline int mpol_equal(struct mempolicy *a, struct mempolicy *b)
173{
174 return 1;
175}
176#define vma_mpol_equal(a,b) 1
177
178#define mpol_set_vma_default(vma) do {} while(0)
179
180static inline void mpol_free(struct mempolicy *p)
181{
182}
183
184static inline void mpol_get(struct mempolicy *pol)
185{
186}
187
188static inline struct mempolicy *mpol_copy(struct mempolicy *old)
189{
190 return NULL;
191}
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193struct shared_policy {};
194
195static inline int mpol_set_shared_policy(struct shared_policy *info,
196 struct vm_area_struct *vma,
197 struct mempolicy *new)
198{
199 return -EINVAL;
200}
201
Robin Holt7339ff82006-01-14 13:20:48 -0800202static inline void mpol_shared_policy_init(struct shared_policy *info,
203 int policy, nodemask_t *nodes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205}
206
207static inline void mpol_free_shared_policy(struct shared_policy *p)
208{
209}
210
211static inline struct mempolicy *
212mpol_shared_policy_lookup(struct shared_policy *sp, unsigned long idx)
213{
214 return NULL;
215}
216
217#define vma_policy(vma) NULL
218#define vma_set_policy(vma, pol) do {} while(0)
219
220static inline void numa_policy_init(void)
221{
222}
223
224static inline void numa_default_policy(void)
225{
226}
227
Paul Jackson74cb2152006-01-08 01:01:56 -0800228static inline void mpol_rebind_task(struct task_struct *tsk,
Paul Jackson68860ec2005-10-30 15:02:36 -0800229 const nodemask_t *new)
230{
231}
232
Paul Jackson42253992006-01-08 01:01:59 -0800233static inline void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new)
234{
235}
236
Paul Jacksonc61afb12006-03-24 03:16:08 -0800237static inline void mpol_fix_fork_child_flag(struct task_struct *p)
238{
239}
240
Christoph Lameter5da7ca82006-01-06 00:10:46 -0800241static inline struct zonelist *huge_zonelist(struct vm_area_struct *vma,
Lee Schermerhorn480eccf2007-09-18 22:46:47 -0700242 unsigned long addr, gfp_t gfp_flags, struct mempolicy **mpol)
Christoph Lameter5da7ca82006-01-06 00:10:46 -0800243{
Mel Gorman396faf02007-07-17 04:03:13 -0700244 return NODE_DATA(0)->node_zonelists + gfp_zone(gfp_flags);
Christoph Lameter5da7ca82006-01-06 00:10:46 -0800245}
246
Paul Jackson45b07ef2006-01-08 01:00:56 -0800247static inline int do_migrate_pages(struct mm_struct *mm,
248 const nodemask_t *from_nodes,
249 const nodemask_t *to_nodes, int flags)
250{
251 return 0;
252}
253
Christoph Lameter4be38e32006-01-06 00:11:17 -0800254static inline void check_highest_zone(int k)
255{
256}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257#endif /* CONFIG_NUMA */
258#endif /* __KERNEL__ */
259
260#endif