mempolicy.h 7.1 KB

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