Loading...
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/dax.h>
12#include <linux/slab.h>
13#include <linux/rbtree.h>
14#include <linux/spinlock.h>
15#include <linux/nodemask.h>
16#include <linux/pagemap.h>
17#include <uapi/linux/mempolicy.h>
18
19struct mm_struct;
20
21#ifdef CONFIG_NUMA
22
23/*
24 * Describe a memory policy.
25 *
26 * A mempolicy can be either associated with a process or with a VMA.
27 * For VMA related allocations the VMA policy is preferred, otherwise
28 * the process policy is used. Interrupts ignore the memory policy
29 * of the current process.
30 *
31 * Locking policy for interleave:
32 * In process context there is no locking because only the process accesses
33 * its own state. All vma manipulation is somewhat protected by a down_read on
34 * mmap_lock.
35 *
36 * Freeing policy:
37 * Mempolicy objects are reference counted. A mempolicy will be freed when
38 * mpol_put() decrements the reference count to zero.
39 *
40 * Duplicating policy objects:
41 * mpol_dup() allocates a new mempolicy and copies the specified mempolicy
42 * to the new storage. The reference count of the new object is initialized
43 * to 1, representing the caller of mpol_dup().
44 */
45struct mempolicy {
46 atomic_t refcnt;
47 unsigned short mode; /* See MPOL_* above */
48 unsigned short flags; /* See set_mempolicy() MPOL_F_* above */
49 nodemask_t nodes; /* interleave/bind/perfer */
50
51 union {
52 nodemask_t cpuset_mems_allowed; /* relative to these nodes */
53 nodemask_t user_nodemask; /* nodemask passed by user */
54 } w;
55};
56
57/*
58 * Support for managing mempolicy data objects (clone, copy, destroy)
59 * The default fast path of a NULL MPOL_DEFAULT policy is always inlined.
60 */
61
62extern void __mpol_put(struct mempolicy *pol);
63static inline void mpol_put(struct mempolicy *pol)
64{
65 if (pol)
66 __mpol_put(pol);
67}
68
69/*
70 * Does mempolicy pol need explicit unref after use?
71 * Currently only needed for shared policies.
72 */
73static inline int mpol_needs_cond_ref(struct mempolicy *pol)
74{
75 return (pol && (pol->flags & MPOL_F_SHARED));
76}
77
78static inline void mpol_cond_put(struct mempolicy *pol)
79{
80 if (mpol_needs_cond_ref(pol))
81 __mpol_put(pol);
82}
83
84extern struct mempolicy *__mpol_dup(struct mempolicy *pol);
85static inline struct mempolicy *mpol_dup(struct mempolicy *pol)
86{
87 if (pol)
88 pol = __mpol_dup(pol);
89 return pol;
90}
91
92#define vma_policy(vma) ((vma)->vm_policy)
93
94static inline void mpol_get(struct mempolicy *pol)
95{
96 if (pol)
97 atomic_inc(&pol->refcnt);
98}
99
100extern bool __mpol_equal(struct mempolicy *a, struct mempolicy *b);
101static inline bool mpol_equal(struct mempolicy *a, struct mempolicy *b)
102{
103 if (a == b)
104 return true;
105 return __mpol_equal(a, b);
106}
107
108/*
109 * Tree of shared policies for a shared memory region.
110 * Maintain the policies in a pseudo mm that contains vmas. The vmas
111 * carry the policy. As a special twist the pseudo mm is indexed in pages, not
112 * bytes, so that we can work with shared memory segments bigger than
113 * unsigned long.
114 */
115
116struct sp_node {
117 struct rb_node nd;
118 unsigned long start, end;
119 struct mempolicy *policy;
120};
121
122struct shared_policy {
123 struct rb_root root;
124 rwlock_t lock;
125};
126
127int vma_dup_policy(struct vm_area_struct *src, struct vm_area_struct *dst);
128void mpol_shared_policy_init(struct shared_policy *sp, struct mempolicy *mpol);
129int mpol_set_shared_policy(struct shared_policy *info,
130 struct vm_area_struct *vma,
131 struct mempolicy *new);
132void mpol_free_shared_policy(struct shared_policy *p);
133struct mempolicy *mpol_shared_policy_lookup(struct shared_policy *sp,
134 unsigned long idx);
135
136struct mempolicy *get_task_policy(struct task_struct *p);
137struct mempolicy *__get_vma_policy(struct vm_area_struct *vma,
138 unsigned long addr);
139bool vma_policy_mof(struct vm_area_struct *vma);
140
141extern void numa_default_policy(void);
142extern void numa_policy_init(void);
143extern void mpol_rebind_task(struct task_struct *tsk, const nodemask_t *new);
144extern void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new);
145
146extern int huge_node(struct vm_area_struct *vma,
147 unsigned long addr, gfp_t gfp_flags,
148 struct mempolicy **mpol, nodemask_t **nodemask);
149extern bool init_nodemask_of_mempolicy(nodemask_t *mask);
150extern bool mempolicy_in_oom_domain(struct task_struct *tsk,
151 const nodemask_t *mask);
152extern nodemask_t *policy_nodemask(gfp_t gfp, struct mempolicy *policy);
153
154static inline nodemask_t *policy_nodemask_current(gfp_t gfp)
155{
156 struct mempolicy *mpol = get_task_policy(current);
157
158 return policy_nodemask(gfp, mpol);
159}
160
161extern unsigned int mempolicy_slab_node(void);
162
163extern enum zone_type policy_zone;
164
165static inline void check_highest_zone(enum zone_type k)
166{
167 if (k > policy_zone && k != ZONE_MOVABLE)
168 policy_zone = k;
169}
170
171int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from,
172 const nodemask_t *to, int flags);
173
174
175#ifdef CONFIG_TMPFS
176extern int mpol_parse_str(char *str, struct mempolicy **mpol);
177#endif
178
179extern void mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol);
180
181/* Check if a vma is migratable */
182extern bool vma_migratable(struct vm_area_struct *vma);
183
184extern int mpol_misplaced(struct page *, struct vm_area_struct *, unsigned long);
185extern void mpol_put_task_policy(struct task_struct *);
186
187#else
188
189struct mempolicy {};
190
191static inline bool mpol_equal(struct mempolicy *a, struct mempolicy *b)
192{
193 return true;
194}
195
196static inline void mpol_put(struct mempolicy *p)
197{
198}
199
200static inline void mpol_cond_put(struct mempolicy *pol)
201{
202}
203
204static inline void mpol_get(struct mempolicy *pol)
205{
206}
207
208struct shared_policy {};
209
210static inline void mpol_shared_policy_init(struct shared_policy *sp,
211 struct mempolicy *mpol)
212{
213}
214
215static inline void mpol_free_shared_policy(struct shared_policy *p)
216{
217}
218
219static inline struct mempolicy *
220mpol_shared_policy_lookup(struct shared_policy *sp, unsigned long idx)
221{
222 return NULL;
223}
224
225#define vma_policy(vma) NULL
226
227static inline int
228vma_dup_policy(struct vm_area_struct *src, struct vm_area_struct *dst)
229{
230 return 0;
231}
232
233static inline void numa_policy_init(void)
234{
235}
236
237static inline void numa_default_policy(void)
238{
239}
240
241static inline void mpol_rebind_task(struct task_struct *tsk,
242 const nodemask_t *new)
243{
244}
245
246static inline void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new)
247{
248}
249
250static inline int huge_node(struct vm_area_struct *vma,
251 unsigned long addr, gfp_t gfp_flags,
252 struct mempolicy **mpol, nodemask_t **nodemask)
253{
254 *mpol = NULL;
255 *nodemask = NULL;
256 return 0;
257}
258
259static inline bool init_nodemask_of_mempolicy(nodemask_t *m)
260{
261 return false;
262}
263
264static inline int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from,
265 const nodemask_t *to, int flags)
266{
267 return 0;
268}
269
270static inline void check_highest_zone(int k)
271{
272}
273
274#ifdef CONFIG_TMPFS
275static inline int mpol_parse_str(char *str, struct mempolicy **mpol)
276{
277 return 1; /* error */
278}
279#endif
280
281static inline int mpol_misplaced(struct page *page, struct vm_area_struct *vma,
282 unsigned long address)
283{
284 return -1; /* no node preference */
285}
286
287static inline void mpol_put_task_policy(struct task_struct *task)
288{
289}
290
291static inline nodemask_t *policy_nodemask_current(gfp_t gfp)
292{
293 return NULL;
294}
295#endif /* CONFIG_NUMA */
296#endif
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/*
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
17/* Policies */
18enum {
19 MPOL_DEFAULT,
20 MPOL_PREFERRED,
21 MPOL_BIND,
22 MPOL_INTERLEAVE,
23 MPOL_MAX, /* always last member of enum */
24};
25
26enum 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
33/* Flags for set_mempolicy */
34#define MPOL_F_STATIC_NODES (1 << 15)
35#define MPOL_F_RELATIVE_NODES (1 << 14)
36
37/*
38 * MPOL_MODE_FLAGS is the union of all possible optional mode flags passed to
39 * either set_mempolicy() or mbind().
40 */
41#define MPOL_MODE_FLAGS (MPOL_F_STATIC_NODES | MPOL_F_RELATIVE_NODES)
42
43/* Flags for get_mempolicy */
44#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 */
46#define MPOL_F_MEMS_ALLOWED (1<<2) /* return allowed memories */
47
48/* Flags for mbind */
49#define MPOL_MF_STRICT (1<<0) /* Verify existing pages in the mapping */
50#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 */
53
54/*
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 */
60#define MPOL_F_LOCAL (1 << 1) /* preferred local allocation */
61#define MPOL_F_REBINDING (1 << 2) /* identify policies in rebinding */
62
63#ifdef __KERNEL__
64
65#include <linux/mmzone.h>
66#include <linux/slab.h>
67#include <linux/rbtree.h>
68#include <linux/spinlock.h>
69#include <linux/nodemask.h>
70#include <linux/pagemap.h>
71
72struct mm_struct;
73
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
87 * mmap_sem.
88 *
89 * Freeing policy:
90 * Mempolicy objects are reference counted. A mempolicy will be freed when
91 * mpol_put() decrements the reference count to zero.
92 *
93 * Duplicating policy objects:
94 * mpol_dup() allocates a new mempolicy and copies the specified mempolicy
95 * to the new storage. The reference count of the new object is initialized
96 * to 1, representing the caller of mpol_dup().
97 */
98struct mempolicy {
99 atomic_t refcnt;
100 unsigned short mode; /* See MPOL_* above */
101 unsigned short flags; /* See set_mempolicy() MPOL_F_* above */
102 union {
103 short preferred_node; /* preferred */
104 nodemask_t nodes; /* interleave/bind */
105 /* undefined for default */
106 } v;
107 union {
108 nodemask_t cpuset_mems_allowed; /* relative to these nodes */
109 nodemask_t user_nodemask; /* nodemask passed by user */
110 } w;
111};
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
118extern void __mpol_put(struct mempolicy *pol);
119static inline void mpol_put(struct mempolicy *pol)
120{
121 if (pol)
122 __mpol_put(pol);
123}
124
125/*
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
150extern struct mempolicy *__mpol_dup(struct mempolicy *pol);
151static inline struct mempolicy *mpol_dup(struct mempolicy *pol)
152{
153 if (pol)
154 pol = __mpol_dup(pol);
155 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
167extern int __mpol_equal(struct mempolicy *a, struct mempolicy *b);
168static inline int mpol_equal(struct mempolicy *a, struct mempolicy *b)
169{
170 if (a == b)
171 return 1;
172 return __mpol_equal(a, b);
173}
174
175/*
176 * 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
194void mpol_shared_policy_init(struct shared_policy *sp, struct mempolicy *mpol);
195int 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
202struct mempolicy *get_vma_policy(struct task_struct *tsk,
203 struct vm_area_struct *vma, unsigned long addr);
204
205extern void numa_default_policy(void);
206extern void numa_policy_init(void);
207extern void mpol_rebind_task(struct task_struct *tsk, const nodemask_t *new,
208 enum mpol_rebind_step step);
209extern void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new);
210extern void mpol_fix_fork_child_flag(struct task_struct *p);
211
212extern struct zonelist *huge_zonelist(struct vm_area_struct *vma,
213 unsigned long addr, gfp_t gfp_flags,
214 struct mempolicy **mpol, nodemask_t **nodemask);
215extern bool init_nodemask_of_mempolicy(nodemask_t *mask);
216extern bool mempolicy_nodemask_intersects(struct task_struct *tsk,
217 const nodemask_t *mask);
218extern unsigned slab_node(struct mempolicy *policy);
219
220extern enum zone_type policy_zone;
221
222static inline void check_highest_zone(enum zone_type k)
223{
224 if (k > policy_zone && k != ZONE_MOVABLE)
225 policy_zone = k;
226}
227
228int do_migrate_pages(struct mm_struct *mm,
229 const nodemask_t *from_nodes, const nodemask_t *to_nodes, int flags);
230
231
232#ifdef CONFIG_TMPFS
233extern int mpol_parse_str(char *str, struct mempolicy **mpol, int no_context);
234#endif
235
236extern int mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol,
237 int no_context);
238
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
256#else
257
258struct mempolicy {};
259
260static inline int mpol_equal(struct mempolicy *a, struct mempolicy *b)
261{
262 return 1;
263}
264
265static inline void mpol_put(struct mempolicy *p)
266{
267}
268
269static 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
279static inline void mpol_get(struct mempolicy *pol)
280{
281}
282
283static inline struct mempolicy *mpol_dup(struct mempolicy *old)
284{
285 return NULL;
286}
287
288struct 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
297static inline void mpol_shared_policy_init(struct shared_policy *sp,
298 struct mempolicy *mpol)
299{
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
323static inline void mpol_rebind_task(struct task_struct *tsk,
324 const nodemask_t *new,
325 enum mpol_rebind_step step)
326{
327}
328
329static inline void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new)
330{
331}
332
333static inline void mpol_fix_fork_child_flag(struct task_struct *p)
334{
335}
336
337static inline struct zonelist *huge_zonelist(struct vm_area_struct *vma,
338 unsigned long addr, gfp_t gfp_flags,
339 struct mempolicy **mpol, nodemask_t **nodemask)
340{
341 *mpol = NULL;
342 *nodemask = NULL;
343 return node_zonelist(0, gfp_flags);
344}
345
346static 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}
356
357static inline int do_migrate_pages(struct mm_struct *mm,
358 const nodemask_t *from_nodes,
359 const nodemask_t *to_nodes, int flags)
360{
361 return 0;
362}
363
364static inline void check_highest_zone(int k)
365{
366}
367
368#ifdef CONFIG_TMPFS
369static inline int mpol_parse_str(char *str, struct mempolicy **mpol,
370 int no_context)
371{
372 return 1; /* error */
373}
374#endif
375
376static inline int mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol,
377 int no_context)
378{
379 return 0;
380}
381
382#endif /* CONFIG_NUMA */
383#endif /* __KERNEL__ */
384
385#endif