mempolicy.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. #include <linux/sched.h>
  9. #include <linux/mmzone.h>
  10. #include <linux/slab.h>
  11. #include <linux/rbtree.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/nodemask.h>
  14. #include <linux/pagemap.h>
  15. #include <uapi/linux/mempolicy.h>
  16. struct mm_struct;
  17. #ifdef CONFIG_NUMA
  18. /*
  19. * Describe a memory policy.
  20. *
  21. * A mempolicy can be either associated with a process or with a VMA.
  22. * For VMA related allocations the VMA policy is preferred, otherwise
  23. * the process policy is used. Interrupts ignore the memory policy
  24. * of the current process.
  25. *
  26. * Locking policy for interleave:
  27. * In process context there is no locking because only the process accesses
  28. * its own state. All vma manipulation is somewhat protected by a down_read on
  29. * mmap_lock.
  30. *
  31. * Freeing policy:
  32. * Mempolicy objects are reference counted. A mempolicy will be freed when
  33. * mpol_put() decrements the reference count to zero.
  34. *
  35. * Duplicating policy objects:
  36. * mpol_dup() allocates a new mempolicy and copies the specified mempolicy
  37. * to the new storage. The reference count of the new object is initialized
  38. * to 1, representing the caller of mpol_dup().
  39. */
  40. struct mempolicy {
  41. atomic_t refcnt;
  42. unsigned short mode; /* See MPOL_* above */
  43. unsigned short flags; /* See set_mempolicy() MPOL_F_* above */
  44. nodemask_t nodes; /* interleave/bind/perfer */
  45. int home_node; /* Home node to use for MPOL_BIND and MPOL_PREFERRED_MANY */
  46. union {
  47. nodemask_t cpuset_mems_allowed; /* relative to these nodes */
  48. nodemask_t user_nodemask; /* nodemask passed by user */
  49. } w;
  50. };
  51. /*
  52. * Support for managing mempolicy data objects (clone, copy, destroy)
  53. * The default fast path of a NULL MPOL_DEFAULT policy is always inlined.
  54. */
  55. extern void __mpol_put(struct mempolicy *pol);
  56. static inline void mpol_put(struct mempolicy *pol)
  57. {
  58. if (pol)
  59. __mpol_put(pol);
  60. }
  61. /*
  62. * Does mempolicy pol need explicit unref after use?
  63. * Currently only needed for shared policies.
  64. */
  65. static inline int mpol_needs_cond_ref(struct mempolicy *pol)
  66. {
  67. return (pol && (pol->flags & MPOL_F_SHARED));
  68. }
  69. static inline void mpol_cond_put(struct mempolicy *pol)
  70. {
  71. if (mpol_needs_cond_ref(pol))
  72. __mpol_put(pol);
  73. }
  74. extern struct mempolicy *__mpol_dup(struct mempolicy *pol);
  75. static inline struct mempolicy *mpol_dup(struct mempolicy *pol)
  76. {
  77. if (pol)
  78. pol = __mpol_dup(pol);
  79. return pol;
  80. }
  81. #define vma_policy(vma) ((vma)->vm_policy)
  82. static inline void mpol_get(struct mempolicy *pol)
  83. {
  84. if (pol)
  85. atomic_inc(&pol->refcnt);
  86. }
  87. extern bool __mpol_equal(struct mempolicy *a, struct mempolicy *b);
  88. static inline bool mpol_equal(struct mempolicy *a, struct mempolicy *b)
  89. {
  90. if (a == b)
  91. return true;
  92. return __mpol_equal(a, b);
  93. }
  94. /*
  95. * Tree of shared policies for a shared memory region.
  96. * Maintain the policies in a pseudo mm that contains vmas. The vmas
  97. * carry the policy. As a special twist the pseudo mm is indexed in pages, not
  98. * bytes, so that we can work with shared memory segments bigger than
  99. * unsigned long.
  100. */
  101. struct sp_node {
  102. struct rb_node nd;
  103. unsigned long start, end;
  104. struct mempolicy *policy;
  105. };
  106. struct shared_policy {
  107. struct rb_root root;
  108. rwlock_t lock;
  109. };
  110. int vma_dup_policy(struct vm_area_struct *src, struct vm_area_struct *dst);
  111. void mpol_shared_policy_init(struct shared_policy *sp, struct mempolicy *mpol);
  112. int mpol_set_shared_policy(struct shared_policy *info,
  113. struct vm_area_struct *vma,
  114. struct mempolicy *new);
  115. void mpol_free_shared_policy(struct shared_policy *p);
  116. struct mempolicy *mpol_shared_policy_lookup(struct shared_policy *sp,
  117. unsigned long idx);
  118. struct mempolicy *get_task_policy(struct task_struct *p);
  119. struct mempolicy *__get_vma_policy(struct vm_area_struct *vma,
  120. unsigned long addr);
  121. bool vma_policy_mof(struct vm_area_struct *vma);
  122. extern void numa_default_policy(void);
  123. extern void numa_policy_init(void);
  124. extern void mpol_rebind_task(struct task_struct *tsk, const nodemask_t *new);
  125. extern void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new);
  126. extern int huge_node(struct vm_area_struct *vma,
  127. unsigned long addr, gfp_t gfp_flags,
  128. struct mempolicy **mpol, nodemask_t **nodemask);
  129. extern bool init_nodemask_of_mempolicy(nodemask_t *mask);
  130. extern bool mempolicy_in_oom_domain(struct task_struct *tsk,
  131. const nodemask_t *mask);
  132. extern nodemask_t *policy_nodemask(gfp_t gfp, struct mempolicy *policy);
  133. extern unsigned int mempolicy_slab_node(void);
  134. extern enum zone_type policy_zone;
  135. static inline void check_highest_zone(enum zone_type k)
  136. {
  137. if (k > policy_zone && k != ZONE_MOVABLE)
  138. policy_zone = k;
  139. }
  140. int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from,
  141. const nodemask_t *to, int flags);
  142. #ifdef CONFIG_TMPFS
  143. extern int mpol_parse_str(char *str, struct mempolicy **mpol);
  144. #endif
  145. extern void mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol);
  146. /* Check if a vma is migratable */
  147. extern bool vma_migratable(struct vm_area_struct *vma);
  148. extern int mpol_misplaced(struct page *, struct vm_area_struct *, unsigned long);
  149. extern void mpol_put_task_policy(struct task_struct *);
  150. static inline bool mpol_is_preferred_many(struct mempolicy *pol)
  151. {
  152. return (pol->mode == MPOL_PREFERRED_MANY);
  153. }
  154. extern bool apply_policy_zone(struct mempolicy *policy, enum zone_type zone);
  155. #else
  156. struct mempolicy {};
  157. static inline bool mpol_equal(struct mempolicy *a, struct mempolicy *b)
  158. {
  159. return true;
  160. }
  161. static inline void mpol_put(struct mempolicy *p)
  162. {
  163. }
  164. static inline void mpol_cond_put(struct mempolicy *pol)
  165. {
  166. }
  167. static inline void mpol_get(struct mempolicy *pol)
  168. {
  169. }
  170. struct shared_policy {};
  171. static inline void mpol_shared_policy_init(struct shared_policy *sp,
  172. struct mempolicy *mpol)
  173. {
  174. }
  175. static inline void mpol_free_shared_policy(struct shared_policy *p)
  176. {
  177. }
  178. static inline struct mempolicy *
  179. mpol_shared_policy_lookup(struct shared_policy *sp, unsigned long idx)
  180. {
  181. return NULL;
  182. }
  183. #define vma_policy(vma) NULL
  184. static inline int
  185. vma_dup_policy(struct vm_area_struct *src, struct vm_area_struct *dst)
  186. {
  187. return 0;
  188. }
  189. static inline void numa_policy_init(void)
  190. {
  191. }
  192. static inline void numa_default_policy(void)
  193. {
  194. }
  195. static inline void mpol_rebind_task(struct task_struct *tsk,
  196. const nodemask_t *new)
  197. {
  198. }
  199. static inline void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new)
  200. {
  201. }
  202. static inline int huge_node(struct vm_area_struct *vma,
  203. unsigned long addr, gfp_t gfp_flags,
  204. struct mempolicy **mpol, nodemask_t **nodemask)
  205. {
  206. *mpol = NULL;
  207. *nodemask = NULL;
  208. return 0;
  209. }
  210. static inline bool init_nodemask_of_mempolicy(nodemask_t *m)
  211. {
  212. return false;
  213. }
  214. static inline int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from,
  215. const nodemask_t *to, int flags)
  216. {
  217. return 0;
  218. }
  219. static inline void check_highest_zone(int k)
  220. {
  221. }
  222. #ifdef CONFIG_TMPFS
  223. static inline int mpol_parse_str(char *str, struct mempolicy **mpol)
  224. {
  225. return 1; /* error */
  226. }
  227. #endif
  228. static inline int mpol_misplaced(struct page *page, struct vm_area_struct *vma,
  229. unsigned long address)
  230. {
  231. return -1; /* no node preference */
  232. }
  233. static inline void mpol_put_task_policy(struct task_struct *task)
  234. {
  235. }
  236. static inline bool mpol_is_preferred_many(struct mempolicy *pol)
  237. {
  238. return false;
  239. }
  240. #endif /* CONFIG_NUMA */
  241. #endif