1 /* SPDX-License-Identifier: GPL-2.0 */
3 * NUMA memory policies for Linux.
4 * Copyright 2003,2004 Andi Kleen SuSE Labs
6 #ifndef _LINUX_MEMPOLICY_H
7 #define _LINUX_MEMPOLICY_H 1
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/nodemask.h>
15 #include <linux/pagemap.h>
16 #include <uapi/linux/mempolicy.h>
20 #define NO_INTERLEAVE_INDEX (-1UL) /* use task il_prev for interleaving */
25 * Describe a memory policy.
27 * A mempolicy can be either associated with a process or with a VMA.
28 * For VMA related allocations the VMA policy is preferred, otherwise
29 * the process policy is used. Interrupts ignore the memory policy
30 * of the current process.
32 * Locking policy for interleave:
33 * In process context there is no locking because only the process accesses
34 * its own state. All vma manipulation is somewhat protected by a down_read on
38 * Mempolicy objects are reference counted. A mempolicy will be freed when
39 * mpol_put() decrements the reference count to zero.
41 * Duplicating policy objects:
42 * mpol_dup() allocates a new mempolicy and copies the specified mempolicy
43 * to the new storage. The reference count of the new object is initialized
44 * to 1, representing the caller of mpol_dup().
48 unsigned short mode; /* See MPOL_* above */
49 unsigned short flags; /* See set_mempolicy() MPOL_F_* above */
50 nodemask_t nodes; /* interleave/bind/perfer */
51 int home_node; /* Home node to use for MPOL_BIND and MPOL_PREFERRED_MANY */
54 nodemask_t cpuset_mems_allowed; /* relative to these nodes */
55 nodemask_t user_nodemask; /* nodemask passed by user */
60 * Support for managing mempolicy data objects (clone, copy, destroy)
61 * The default fast path of a NULL MPOL_DEFAULT policy is always inlined.
64 extern void __mpol_put(struct mempolicy *pol);
65 static inline void mpol_put(struct mempolicy *pol)
72 * Does mempolicy pol need explicit unref after use?
73 * Currently only needed for shared policies.
75 static inline int mpol_needs_cond_ref(struct mempolicy *pol)
77 return (pol && (pol->flags & MPOL_F_SHARED));
80 static inline void mpol_cond_put(struct mempolicy *pol)
82 if (mpol_needs_cond_ref(pol))
86 extern struct mempolicy *__mpol_dup(struct mempolicy *pol);
87 static inline struct mempolicy *mpol_dup(struct mempolicy *pol)
90 pol = __mpol_dup(pol);
94 static inline void mpol_get(struct mempolicy *pol)
97 atomic_inc(&pol->refcnt);
100 extern bool __mpol_equal(struct mempolicy *a, struct mempolicy *b);
101 static inline bool mpol_equal(struct mempolicy *a, struct mempolicy *b)
105 return __mpol_equal(a, b);
109 * Tree of shared policies for a shared memory region.
111 struct shared_policy {
118 struct mempolicy *policy;
121 int vma_dup_policy(struct vm_area_struct *src, struct vm_area_struct *dst);
122 void mpol_shared_policy_init(struct shared_policy *sp, struct mempolicy *mpol);
123 int mpol_set_shared_policy(struct shared_policy *sp,
124 struct vm_area_struct *vma, struct mempolicy *mpol);
125 void mpol_free_shared_policy(struct shared_policy *sp);
126 struct mempolicy *mpol_shared_policy_lookup(struct shared_policy *sp,
129 struct mempolicy *get_task_policy(struct task_struct *p);
130 struct mempolicy *__get_vma_policy(struct vm_area_struct *vma,
131 unsigned long addr, pgoff_t *ilx);
132 struct mempolicy *get_vma_policy(struct vm_area_struct *vma,
133 unsigned long addr, int order, pgoff_t *ilx);
134 bool vma_policy_mof(struct vm_area_struct *vma);
136 extern void numa_default_policy(void);
137 extern void numa_policy_init(void);
138 extern void mpol_rebind_task(struct task_struct *tsk, const nodemask_t *new);
139 extern void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new);
141 extern int huge_node(struct vm_area_struct *vma,
142 unsigned long addr, gfp_t gfp_flags,
143 struct mempolicy **mpol, nodemask_t **nodemask);
144 extern bool init_nodemask_of_mempolicy(nodemask_t *mask);
145 extern bool mempolicy_in_oom_domain(struct task_struct *tsk,
146 const nodemask_t *mask);
147 extern unsigned int mempolicy_slab_node(void);
149 extern enum zone_type policy_zone;
151 static inline void check_highest_zone(enum zone_type k)
153 if (k > policy_zone && k != ZONE_MOVABLE)
157 int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from,
158 const nodemask_t *to, int flags);
162 extern int mpol_parse_str(char *str, struct mempolicy **mpol);
165 extern void mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol);
167 /* Check if a vma is migratable */
168 extern bool vma_migratable(struct vm_area_struct *vma);
170 int mpol_misplaced(struct folio *, struct vm_area_struct *, unsigned long);
171 extern void mpol_put_task_policy(struct task_struct *);
173 static inline bool mpol_is_preferred_many(struct mempolicy *pol)
175 return (pol->mode == MPOL_PREFERRED_MANY);
178 extern bool apply_policy_zone(struct mempolicy *policy, enum zone_type zone);
184 static inline struct mempolicy *get_task_policy(struct task_struct *p)
189 static inline bool mpol_equal(struct mempolicy *a, struct mempolicy *b)
194 static inline void mpol_put(struct mempolicy *pol)
198 static inline void mpol_cond_put(struct mempolicy *pol)
202 static inline void mpol_get(struct mempolicy *pol)
206 struct shared_policy {};
208 static inline void mpol_shared_policy_init(struct shared_policy *sp,
209 struct mempolicy *mpol)
213 static inline void mpol_free_shared_policy(struct shared_policy *sp)
217 static inline struct mempolicy *
218 mpol_shared_policy_lookup(struct shared_policy *sp, pgoff_t idx)
223 static inline struct mempolicy *get_vma_policy(struct vm_area_struct *vma,
224 unsigned long addr, int order, pgoff_t *ilx)
231 vma_dup_policy(struct vm_area_struct *src, struct vm_area_struct *dst)
236 static inline void numa_policy_init(void)
240 static inline void numa_default_policy(void)
244 static inline void mpol_rebind_task(struct task_struct *tsk,
245 const nodemask_t *new)
249 static inline void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new)
253 static inline int huge_node(struct vm_area_struct *vma,
254 unsigned long addr, gfp_t gfp_flags,
255 struct mempolicy **mpol, nodemask_t **nodemask)
262 static inline bool init_nodemask_of_mempolicy(nodemask_t *m)
267 static inline int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from,
268 const nodemask_t *to, int flags)
273 static inline void check_highest_zone(int k)
278 static inline int mpol_parse_str(char *str, struct mempolicy **mpol)
280 return 1; /* error */
284 static inline int mpol_misplaced(struct folio *folio,
285 struct vm_area_struct *vma,
286 unsigned long address)
288 return -1; /* no node preference */
291 static inline void mpol_put_task_policy(struct task_struct *task)
295 static inline bool mpol_is_preferred_many(struct mempolicy *pol)
300 #endif /* CONFIG_NUMA */