mempolicy.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. #define vma_set_policy(vma, pol) ((vma)->vm_policy = (pol))
  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. struct mutex mutex;
  111. };
  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_vma_policy(struct task_struct *tsk,
  120. struct vm_area_struct *vma, unsigned long addr);
  121. extern void numa_default_policy(void);
  122. extern void numa_policy_init(void);
  123. extern void mpol_rebind_task(struct task_struct *tsk, const nodemask_t *new,
  124. enum mpol_rebind_step step);
  125. extern void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new);
  126. extern void mpol_fix_fork_child_flag(struct task_struct *p);
  127. extern struct zonelist *huge_zonelist(struct vm_area_struct *vma,
  128. unsigned long addr, gfp_t gfp_flags,
  129. struct mempolicy **mpol, nodemask_t **nodemask);
  130. extern bool init_nodemask_of_mempolicy(nodemask_t *mask);
  131. extern bool mempolicy_nodemask_intersects(struct task_struct *tsk,
  132. const nodemask_t *mask);
  133. extern unsigned 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, int no_context);
  144. #endif
  145. extern int mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol,
  146. int no_context);
  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_HUGETLB | VM_PFNMAP))
  151. return 0;
  152. /*
  153. * Migration allocates pages in the highest zone. If we cannot
  154. * do so then migration (at least from node to node) is not
  155. * possible.
  156. */
  157. if (vma->vm_file &&
  158. gfp_zone(mapping_gfp_mask(vma->vm_file->f_mapping))
  159. < policy_zone)
  160. return 0;
  161. return 1;
  162. }
  163. extern int mpol_misplaced(struct page *, struct vm_area_struct *, unsigned long);
  164. #else
  165. struct mempolicy {};
  166. static inline bool mpol_equal(struct mempolicy *a, struct mempolicy *b)
  167. {
  168. return true;
  169. }
  170. static inline void mpol_put(struct mempolicy *p)
  171. {
  172. }
  173. static inline void mpol_cond_put(struct mempolicy *pol)
  174. {
  175. }
  176. static inline void mpol_get(struct mempolicy *pol)
  177. {
  178. }
  179. static inline struct mempolicy *mpol_dup(struct mempolicy *old)
  180. {
  181. return NULL;
  182. }
  183. struct shared_policy {};
  184. static inline int mpol_set_shared_policy(struct shared_policy *info,
  185. struct vm_area_struct *vma,
  186. struct mempolicy *new)
  187. {
  188. return -EINVAL;
  189. }
  190. static inline void mpol_shared_policy_init(struct shared_policy *sp,
  191. struct mempolicy *mpol)
  192. {
  193. }
  194. static inline void mpol_free_shared_policy(struct shared_policy *p)
  195. {
  196. }
  197. static inline struct mempolicy *
  198. mpol_shared_policy_lookup(struct shared_policy *sp, unsigned long idx)
  199. {
  200. return NULL;
  201. }
  202. #define vma_policy(vma) NULL
  203. #define vma_set_policy(vma, pol) do {} while(0)
  204. static inline void numa_policy_init(void)
  205. {
  206. }
  207. static inline void numa_default_policy(void)
  208. {
  209. }
  210. static inline void mpol_rebind_task(struct task_struct *tsk,
  211. const nodemask_t *new,
  212. enum mpol_rebind_step step)
  213. {
  214. }
  215. static inline void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new)
  216. {
  217. }
  218. static inline void mpol_fix_fork_child_flag(struct task_struct *p)
  219. {
  220. }
  221. static inline struct zonelist *huge_zonelist(struct vm_area_struct *vma,
  222. unsigned long addr, gfp_t gfp_flags,
  223. struct mempolicy **mpol, nodemask_t **nodemask)
  224. {
  225. *mpol = NULL;
  226. *nodemask = NULL;
  227. return node_zonelist(0, gfp_flags);
  228. }
  229. static inline bool init_nodemask_of_mempolicy(nodemask_t *m)
  230. {
  231. return false;
  232. }
  233. static inline bool mempolicy_nodemask_intersects(struct task_struct *tsk,
  234. const nodemask_t *mask)
  235. {
  236. return false;
  237. }
  238. static inline int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from,
  239. const nodemask_t *to, int flags)
  240. {
  241. return 0;
  242. }
  243. static inline void check_highest_zone(int k)
  244. {
  245. }
  246. #ifdef CONFIG_TMPFS
  247. static inline int mpol_parse_str(char *str, struct mempolicy **mpol,
  248. int no_context)
  249. {
  250. return 1; /* error */
  251. }
  252. #endif
  253. static inline int mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol,
  254. int no_context)
  255. {
  256. return 0;
  257. }
  258. static inline int mpol_misplaced(struct page *page, struct vm_area_struct *vma,
  259. unsigned long address)
  260. {
  261. return -1; /* no node preference */
  262. }
  263. #endif /* CONFIG_NUMA */
  264. #endif