mempolicy.h 7.4 KB

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