VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / include / linux / mempolicy.h
1 #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 */
22
23 /* Flags for mbind */
24 #define MPOL_MF_STRICT  (1<<0)  /* Verify existing pages in the mapping */
25
26 #ifdef __KERNEL__
27
28 #include <linux/config.h>
29 #include <linux/mmzone.h>
30 #include <linux/bitmap.h>
31 #include <linux/slab.h>
32 #include <linux/rbtree.h>
33 #include <asm/semaphore.h>
34
35 struct vm_area_struct;
36
37 #ifdef CONFIG_NUMA
38
39 /*
40  * Describe a memory policy.
41  *
42  * A mempolicy can be either associated with a process or with a VMA.
43  * For VMA related allocations the VMA policy is preferred, otherwise
44  * the process policy is used. Interrupts ignore the memory policy
45  * of the current process.
46  *
47  * Locking policy for interlave:
48  * In process context there is no locking because only the process accesses
49  * its own state. All vma manipulation is somewhat protected by a down_read on
50  * mmap_sem. For allocating in the interleave policy the page_table_lock
51  * must be also aquired to protect il_next.
52  *
53  * Freeing policy:
54  * When policy is MPOL_BIND v.zonelist is kmalloc'ed and must be kfree'd.
55  * All other policies don't have any external state. mpol_free() handles this.
56  *
57  * Copying policy objects:
58  * For MPOL_BIND the zonelist must be always duplicated. mpol_clone() does this.
59  */
60 struct mempolicy {
61         atomic_t refcnt;
62         short policy;   /* See MPOL_* above */
63         union {
64                 struct zonelist  *zonelist;     /* bind */
65                 short            preferred_node; /* preferred */
66                 DECLARE_BITMAP(nodes, MAX_NUMNODES); /* interleave */
67                 /* undefined for default */
68         } v;
69 };
70
71 /* An NULL mempolicy pointer is a synonym of &default_policy. */
72 extern struct mempolicy default_policy;
73
74 /*
75  * Support for managing mempolicy data objects (clone, copy, destroy)
76  * The default fast path of a NULL MPOL_DEFAULT policy is always inlined.
77  */
78
79 extern void __mpol_free(struct mempolicy *pol);
80 static inline void mpol_free(struct mempolicy *pol)
81 {
82         if (pol)
83                 __mpol_free(pol);
84 }
85
86 extern struct mempolicy *__mpol_copy(struct mempolicy *pol);
87 static inline struct mempolicy *mpol_copy(struct mempolicy *pol)
88 {
89         if (pol)
90                 pol = __mpol_copy(pol);
91         return pol;
92 }
93
94 #define vma_policy(vma) ((vma)->vm_policy)
95 #define vma_set_policy(vma, pol) ((vma)->vm_policy = (pol))
96
97 static inline void mpol_get(struct mempolicy *pol)
98 {
99         if (pol)
100                 atomic_inc(&pol->refcnt);
101 }
102
103 extern int __mpol_equal(struct mempolicy *a, struct mempolicy *b);
104 static inline int mpol_equal(struct mempolicy *a, struct mempolicy *b)
105 {
106         if (a == b)
107                 return 1;
108         return __mpol_equal(a, b);
109 }
110 #define vma_mpol_equal(a,b) mpol_equal(vma_policy(a), vma_policy(b))
111
112 /* Could later add inheritance of the process policy here. */
113
114 #define mpol_set_vma_default(vma) ((vma)->vm_policy = NULL)
115
116 /*
117  * Hugetlb policy. i386 hugetlb so far works with node numbers
118  * instead of zone lists, so give it special interfaces for now.
119  */
120 extern int mpol_first_node(struct vm_area_struct *vma, unsigned long addr);
121 extern int mpol_node_valid(int nid, struct vm_area_struct *vma,
122                         unsigned long addr);
123
124 /*
125  * Tree of shared policies for a shared memory region.
126  * Maintain the policies in a pseudo mm that contains vmas. The vmas
127  * carry the policy. As a special twist the pseudo mm is indexed in pages, not
128  * bytes, so that we can work with shared memory segments bigger than
129  * unsigned long.
130  */
131
132 struct sp_node {
133         struct rb_node nd;
134         unsigned long start, end;
135         struct mempolicy *policy;
136 };
137
138 struct shared_policy {
139         struct rb_root root;
140         struct semaphore sem;
141 };
142
143 static inline void mpol_shared_policy_init(struct shared_policy *info)
144 {
145         info->root = RB_ROOT;
146         init_MUTEX(&info->sem);
147 }
148
149 int mpol_set_shared_policy(struct shared_policy *info,
150                                 struct vm_area_struct *vma,
151                                 struct mempolicy *new);
152 void mpol_free_shared_policy(struct shared_policy *p);
153 struct mempolicy *mpol_shared_policy_lookup(struct shared_policy *sp,
154                                             unsigned long idx);
155
156 extern void numa_default_policy(void);
157 extern void numa_policy_init(void);
158
159 #else
160
161 struct mempolicy {};
162
163 static inline int mpol_equal(struct mempolicy *a, struct mempolicy *b)
164 {
165         return 1;
166 }
167 #define vma_mpol_equal(a,b) 1
168
169 #define mpol_set_vma_default(vma) do {} while(0)
170
171 static inline void mpol_free(struct mempolicy *p)
172 {
173 }
174
175 static inline void mpol_get(struct mempolicy *pol)
176 {
177 }
178
179 static inline struct mempolicy *mpol_copy(struct mempolicy *old)
180 {
181         return NULL;
182 }
183
184 static inline int mpol_first_node(struct vm_area_struct *vma, unsigned long a)
185 {
186         return numa_node_id();
187 }
188
189 static inline int
190 mpol_node_valid(int nid, struct vm_area_struct *vma, unsigned long a)
191 {
192         return 1;
193 }
194
195 struct shared_policy {};
196
197 static inline int mpol_set_shared_policy(struct shared_policy *info,
198                                         struct vm_area_struct *vma,
199                                         struct mempolicy *new)
200 {
201         return -EINVAL;
202 }
203
204 static inline void mpol_shared_policy_init(struct shared_policy *info)
205 {
206 }
207
208 static inline void mpol_free_shared_policy(struct shared_policy *p)
209 {
210 }
211
212 static inline struct mempolicy *
213 mpol_shared_policy_lookup(struct shared_policy *sp, unsigned long idx)
214 {
215         return NULL;
216 }
217
218 #define vma_policy(vma) NULL
219 #define vma_set_policy(vma, pol) do {} while(0)
220
221 static inline void numa_policy_init(void)
222 {
223 }
224
225 static inline void numa_default_policy(void)
226 {
227 }
228
229 #endif /* CONFIG_NUMA */
230 #endif /* __KERNEL__ */
231
232 #endif