mempolicy.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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_vma_policy(struct task_struct *tsk,
  120. struct vm_area_struct *vma, unsigned long addr);
  121. bool vma_policy_mof(struct task_struct *task, 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. enum mpol_rebind_step step);
  126. extern void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new);
  127. extern void mpol_fix_fork_child_flag(struct task_struct *p);
  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 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 int 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. /*
  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. static inline int
  204. vma_dup_policy(struct vm_area_struct *src, struct vm_area_struct *dst)
  205. {
  206. return 0;
  207. }
  208. static inline void numa_policy_init(void)
  209. {
  210. }
  211. static inline void numa_default_policy(void)
  212. {
  213. }
  214. static inline void mpol_rebind_task(struct task_struct *tsk,
  215. const nodemask_t *new,
  216. enum mpol_rebind_step step)
  217. {
  218. }
  219. static inline void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new)
  220. {
  221. }
  222. static inline void mpol_fix_fork_child_flag(struct task_struct *p)
  223. {
  224. }
  225. static inline struct zonelist *huge_zonelist(struct vm_area_struct *vma,
  226. unsigned long addr, gfp_t gfp_flags,
  227. struct mempolicy **mpol, nodemask_t **nodemask)
  228. {
  229. *mpol = NULL;
  230. *nodemask = NULL;
  231. return node_zonelist(0, gfp_flags);
  232. }
  233. static inline bool init_nodemask_of_mempolicy(nodemask_t *m)
  234. {
  235. return false;
  236. }
  237. static inline bool mempolicy_nodemask_intersects(struct task_struct *tsk,
  238. const nodemask_t *mask)
  239. {
  240. return false;
  241. }
  242. static inline int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from,
  243. const nodemask_t *to, int flags)
  244. {
  245. return 0;
  246. }
  247. static inline void check_highest_zone(int k)
  248. {
  249. }
  250. #ifdef CONFIG_TMPFS
  251. static inline int mpol_parse_str(char *str, struct mempolicy **mpol)
  252. {
  253. return 1; /* error */
  254. }
  255. #endif
  256. static inline int mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol)
  257. {
  258. return 0;
  259. }
  260. static inline int mpol_misplaced(struct page *page, struct vm_area_struct *vma,
  261. unsigned long address)
  262. {
  263. return -1; /* no node preference */
  264. }
  265. #endif /* CONFIG_NUMA */
  266. #endif