xref: /linux/include/linux/mempolicy.h (revision f087b0bad454a91c7d1615f82954a4752843560d)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * NUMA memory policies for Linux.
4  * Copyright 2003,2004 Andi Kleen SuSE Labs
5  */
6 #ifndef _LINUX_MEMPOLICY_H
7 #define _LINUX_MEMPOLICY_H 1
8 
9 #include <linux/sched.h>
10 #include <linux/mmzone.h>
11 #include <linux/slab.h>
12 #include <linux/rbtree.h>
13 #include <linux/spinlock.h>
14 #include <linux/node.h>
15 #include <linux/nodemask.h>
16 #include <linux/pagemap.h>
17 #include <uapi/linux/mempolicy.h>
18 
19 struct mm_struct;
20 
21 #define NO_INTERLEAVE_INDEX (-1UL)	/* use task il_prev for interleaving */
22 
23 #ifdef CONFIG_NUMA
24 
25 /*
26  * Describe a memory policy.
27  *
28  * A mempolicy can be either associated with a process or with a VMA.
29  * For VMA related allocations the VMA policy is preferred, otherwise
30  * the process policy is used. Interrupts ignore the memory policy
31  * of the current process.
32  *
33  * Locking policy for interleave:
34  * In process context there is no locking because only the process accesses
35  * its own state. All vma manipulation is somewhat protected by a down_read on
36  * mmap_lock.
37  *
38  * Freeing policy:
39  * Mempolicy objects are reference counted.  A mempolicy will be freed when
40  * mpol_put() decrements the reference count to zero.
41  *
42  * Duplicating policy objects:
43  * mpol_dup() allocates a new mempolicy and copies the specified mempolicy
44  * to the new storage.  The reference count of the new object is initialized
45  * to 1, representing the caller of mpol_dup().
46  */
47 struct mempolicy {
48 	atomic_t refcnt;
49 	unsigned short mode; 	/* See MPOL_* above */
50 	unsigned short flags;	/* See set_mempolicy() MPOL_F_* above */
51 	nodemask_t nodes;	/* interleave/bind/preferred/etc */
52 	int home_node;		/* Home node to use for MPOL_BIND and MPOL_PREFERRED_MANY */
53 
54 	union {
55 		nodemask_t cpuset_mems_allowed;	/* relative to these nodes */
56 		nodemask_t user_nodemask;	/* nodemask passed by user */
57 	} w;
58 	struct rcu_head rcu;
59 };
60 
61 /*
62  * Support for managing mempolicy data objects (clone, copy, destroy)
63  * The default fast path of a NULL MPOL_DEFAULT policy is always inlined.
64  */
65 
66 extern void __mpol_put(struct mempolicy *pol);
mpol_put(struct mempolicy * pol)67 static inline void mpol_put(struct mempolicy *pol)
68 {
69 	if (pol)
70 		__mpol_put(pol);
71 }
72 
73 /*
74  * Does mempolicy pol need explicit unref after use?
75  * Currently only needed for shared policies.
76  */
mpol_needs_cond_ref(struct mempolicy * pol)77 static inline int mpol_needs_cond_ref(struct mempolicy *pol)
78 {
79 	return (pol && (pol->flags & MPOL_F_SHARED));
80 }
81 
mpol_cond_put(struct mempolicy * pol)82 static inline void mpol_cond_put(struct mempolicy *pol)
83 {
84 	if (mpol_needs_cond_ref(pol))
85 		__mpol_put(pol);
86 }
87 
88 extern struct mempolicy *__mpol_dup(struct mempolicy *pol);
mpol_dup(struct mempolicy * pol)89 static inline struct mempolicy *mpol_dup(struct mempolicy *pol)
90 {
91 	if (pol)
92 		pol = __mpol_dup(pol);
93 	return pol;
94 }
95 
mpol_get(struct mempolicy * pol)96 static inline void mpol_get(struct mempolicy *pol)
97 {
98 	if (pol)
99 		atomic_inc(&pol->refcnt);
100 }
101 
102 extern bool __mpol_equal(struct mempolicy *a, struct mempolicy *b);
mpol_equal(struct mempolicy * a,struct mempolicy * b)103 static inline bool mpol_equal(struct mempolicy *a, struct mempolicy *b)
104 {
105 	if (a == b)
106 		return true;
107 	return __mpol_equal(a, b);
108 }
109 
110 /*
111  * Tree of shared policies for a shared memory region.
112  */
113 struct shared_policy {
114 	struct rb_root root;
115 	rwlock_t lock;
116 };
117 struct sp_node {
118 	struct rb_node nd;
119 	pgoff_t start, end;
120 	struct mempolicy *policy;
121 };
122 
123 int vma_dup_policy(struct vm_area_struct *src, struct vm_area_struct *dst);
124 void mpol_shared_policy_init(struct shared_policy *sp, struct mempolicy *mpol);
125 int mpol_set_shared_policy(struct shared_policy *sp,
126 			   struct vm_area_struct *vma, struct mempolicy *mpol);
127 void mpol_free_shared_policy(struct shared_policy *sp);
128 struct mempolicy *mpol_shared_policy_lookup(struct shared_policy *sp,
129 					    pgoff_t idx);
130 
131 struct mempolicy *get_task_policy(struct task_struct *p);
132 struct mempolicy *__get_vma_policy(struct vm_area_struct *vma,
133 		unsigned long addr, pgoff_t *ilx);
134 struct mempolicy *get_vma_policy(struct vm_area_struct *vma,
135 		unsigned long addr, int order, pgoff_t *ilx);
136 bool vma_policy_mof(struct vm_area_struct *vma);
137 
138 extern void numa_default_policy(void);
139 extern void numa_policy_init(void);
140 extern void mpol_rebind_task(struct task_struct *tsk, const nodemask_t *new);
141 extern void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new);
142 
143 extern int huge_node(struct vm_area_struct *vma,
144 				unsigned long addr, gfp_t gfp_flags,
145 				struct mempolicy **mpol, nodemask_t **nodemask);
146 extern bool init_nodemask_of_mempolicy(nodemask_t *mask);
147 extern bool mempolicy_in_oom_domain(struct task_struct *tsk,
148 				const nodemask_t *mask);
149 extern unsigned int mempolicy_slab_node(void);
150 
151 extern enum zone_type policy_zone;
152 
check_highest_zone(enum zone_type k)153 static inline void check_highest_zone(enum zone_type k)
154 {
155 	if (k > policy_zone && k != ZONE_MOVABLE)
156 		policy_zone = k;
157 }
158 
159 int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from,
160 		     const nodemask_t *to, int flags);
161 
162 
163 #ifdef CONFIG_TMPFS
164 extern int mpol_parse_str(char *str, struct mempolicy **mpol);
165 #endif
166 
167 extern void mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol);
168 
169 /* Check if a vma is migratable */
170 extern bool vma_migratable(struct vm_area_struct *vma);
171 
172 int mpol_misplaced(struct folio *folio, struct vm_fault *vmf,
173 					unsigned long addr);
174 extern void mpol_put_task_policy(struct task_struct *);
175 
mpol_is_preferred_many(struct mempolicy * pol)176 static inline bool mpol_is_preferred_many(struct mempolicy *pol)
177 {
178 	return  (pol->mode == MPOL_PREFERRED_MANY);
179 }
180 
181 extern bool apply_policy_zone(struct mempolicy *policy, enum zone_type zone);
182 
183 extern int mempolicy_set_node_perf(unsigned int node,
184 				   struct access_coordinate *coords);
185 
186 #else
187 
188 struct mempolicy {};
189 
get_task_policy(struct task_struct * p)190 static inline struct mempolicy *get_task_policy(struct task_struct *p)
191 {
192 	return NULL;
193 }
194 
mpol_equal(struct mempolicy * a,struct mempolicy * b)195 static inline bool mpol_equal(struct mempolicy *a, struct mempolicy *b)
196 {
197 	return true;
198 }
199 
mpol_put(struct mempolicy * pol)200 static inline void mpol_put(struct mempolicy *pol)
201 {
202 }
203 
mpol_cond_put(struct mempolicy * pol)204 static inline void mpol_cond_put(struct mempolicy *pol)
205 {
206 }
207 
mpol_get(struct mempolicy * pol)208 static inline void mpol_get(struct mempolicy *pol)
209 {
210 }
211 
212 struct shared_policy {};
213 
mpol_shared_policy_init(struct shared_policy * sp,struct mempolicy * mpol)214 static inline void mpol_shared_policy_init(struct shared_policy *sp,
215 						struct mempolicy *mpol)
216 {
217 }
218 
mpol_free_shared_policy(struct shared_policy * sp)219 static inline void mpol_free_shared_policy(struct shared_policy *sp)
220 {
221 }
222 
223 static inline struct mempolicy *
mpol_shared_policy_lookup(struct shared_policy * sp,pgoff_t idx)224 mpol_shared_policy_lookup(struct shared_policy *sp, pgoff_t idx)
225 {
226 	return NULL;
227 }
228 
get_vma_policy(struct vm_area_struct * vma,unsigned long addr,int order,pgoff_t * ilx)229 static inline struct mempolicy *get_vma_policy(struct vm_area_struct *vma,
230 				unsigned long addr, int order, pgoff_t *ilx)
231 {
232 	*ilx = 0;
233 	return NULL;
234 }
235 
236 static inline int
vma_dup_policy(struct vm_area_struct * src,struct vm_area_struct * dst)237 vma_dup_policy(struct vm_area_struct *src, struct vm_area_struct *dst)
238 {
239 	return 0;
240 }
241 
numa_policy_init(void)242 static inline void numa_policy_init(void)
243 {
244 }
245 
numa_default_policy(void)246 static inline void numa_default_policy(void)
247 {
248 }
249 
mpol_rebind_task(struct task_struct * tsk,const nodemask_t * new)250 static inline void mpol_rebind_task(struct task_struct *tsk,
251 				const nodemask_t *new)
252 {
253 }
254 
mpol_rebind_mm(struct mm_struct * mm,nodemask_t * new)255 static inline void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new)
256 {
257 }
258 
huge_node(struct vm_area_struct * vma,unsigned long addr,gfp_t gfp_flags,struct mempolicy ** mpol,nodemask_t ** nodemask)259 static inline int huge_node(struct vm_area_struct *vma,
260 				unsigned long addr, gfp_t gfp_flags,
261 				struct mempolicy **mpol, nodemask_t **nodemask)
262 {
263 	*mpol = NULL;
264 	*nodemask = NULL;
265 	return 0;
266 }
267 
init_nodemask_of_mempolicy(nodemask_t * m)268 static inline bool init_nodemask_of_mempolicy(nodemask_t *m)
269 {
270 	return false;
271 }
272 
do_migrate_pages(struct mm_struct * mm,const nodemask_t * from,const nodemask_t * to,int flags)273 static inline int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from,
274 				   const nodemask_t *to, int flags)
275 {
276 	return 0;
277 }
278 
check_highest_zone(int k)279 static inline void check_highest_zone(int k)
280 {
281 }
282 
283 #ifdef CONFIG_TMPFS
mpol_parse_str(char * str,struct mempolicy ** mpol)284 static inline int mpol_parse_str(char *str, struct mempolicy **mpol)
285 {
286 	return 1;	/* error */
287 }
288 #endif
289 
mpol_misplaced(struct folio * folio,struct vm_fault * vmf,unsigned long address)290 static inline int mpol_misplaced(struct folio *folio,
291 				 struct vm_fault *vmf,
292 				 unsigned long address)
293 {
294 	return -1; /* no node preference */
295 }
296 
mpol_put_task_policy(struct task_struct * task)297 static inline void mpol_put_task_policy(struct task_struct *task)
298 {
299 }
300 
mpol_is_preferred_many(struct mempolicy * pol)301 static inline bool mpol_is_preferred_many(struct mempolicy *pol)
302 {
303 	return  false;
304 }
305 
306 #endif /* CONFIG_NUMA */
307 #endif
308