mempolicy.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. #ifndef _LINUX_MEMPOLICY_H
  2. #define _LINUX_MEMPOLICY_H 1
  3. #include <linux/errno.h>
  4. /*
  5. * NUMA memory policies for Linux.
  6. * Copyright 2003,2004 Andi Kleen SuSE Labs
  7. */
  8. /*
  9. * Both the MPOL_* mempolicy mode and the MPOL_F_* optional mode flags are
  10. * passed by the user to either set_mempolicy() or mbind() in an 'int' actual.
  11. * The MPOL_MODE_FLAGS macro determines the legal set of optional mode flags.
  12. */
  13. /* Policies */
  14. enum {
  15. MPOL_DEFAULT,
  16. MPOL_PREFERRED,
  17. MPOL_BIND,
  18. MPOL_INTERLEAVE,
  19. MPOL_MAX, /* always last member of enum */
  20. };
  21. /* Flags for set_mempolicy */
  22. #define MPOL_F_STATIC_NODES (1 << 15)
  23. #define MPOL_F_RELATIVE_NODES (1 << 14)
  24. /*
  25. * MPOL_MODE_FLAGS is the union of all possible optional mode flags passed to
  26. * either set_mempolicy() or mbind().
  27. */
  28. #define MPOL_MODE_FLAGS (MPOL_F_STATIC_NODES | MPOL_F_RELATIVE_NODES)
  29. /* Flags for get_mempolicy */
  30. #define MPOL_F_NODE (1<<0) /* return next IL mode instead of node mask */
  31. #define MPOL_F_ADDR (1<<1) /* look up vma using address */
  32. #define MPOL_F_MEMS_ALLOWED (1<<2) /* return allowed memories */
  33. /* Flags for mbind */
  34. #define MPOL_MF_STRICT (1<<0) /* Verify existing pages in the mapping */
  35. #define MPOL_MF_MOVE (1<<1) /* Move pages owned by this process to conform to mapping */
  36. #define MPOL_MF_MOVE_ALL (1<<2) /* Move every page to conform to mapping */
  37. #define MPOL_MF_INTERNAL (1<<3) /* Internal flags start here */
  38. /*
  39. * Internal flags that share the struct mempolicy flags word with
  40. * "mode flags". These flags are allocated from bit 0 up, as they
  41. * are never OR'ed into the mode in mempolicy API arguments.
  42. */
  43. #define MPOL_F_SHARED (1 << 0) /* identify shared policies */
  44. #ifdef __KERNEL__
  45. #include <linux/mmzone.h>
  46. #include <linux/slab.h>
  47. #include <linux/rbtree.h>
  48. #include <linux/spinlock.h>
  49. #include <linux/nodemask.h>
  50. struct mm_struct;
  51. #ifdef CONFIG_NUMA
  52. /*
  53. * Describe a memory policy.
  54. *
  55. * A mempolicy can be either associated with a process or with a VMA.
  56. * For VMA related allocations the VMA policy is preferred, otherwise
  57. * the process policy is used. Interrupts ignore the memory policy
  58. * of the current process.
  59. *
  60. * Locking policy for interlave:
  61. * In process context there is no locking because only the process accesses
  62. * its own state. All vma manipulation is somewhat protected by a down_read on
  63. * mmap_sem.
  64. *
  65. * Freeing policy:
  66. * Mempolicy objects are reference counted. A mempolicy will be freed when
  67. * mpol_put() decrements the reference count to zero.
  68. *
  69. * Duplicating policy objects:
  70. * mpol_dup() allocates a new mempolicy and copies the specified mempolicy
  71. * to the new storage. The reference count of the new object is initialized
  72. * to 1, representing the caller of mpol_dup().
  73. */
  74. struct mempolicy {
  75. atomic_t refcnt;
  76. unsigned short mode; /* See MPOL_* above */
  77. unsigned short flags; /* See set_mempolicy() MPOL_F_* above */
  78. union {
  79. short preferred_node; /* preferred */
  80. nodemask_t nodes; /* interleave/bind */
  81. /* undefined for default */
  82. } v;
  83. union {
  84. nodemask_t cpuset_mems_allowed; /* relative to these nodes */
  85. nodemask_t user_nodemask; /* nodemask passed by user */
  86. } w;
  87. };
  88. /*
  89. * Support for managing mempolicy data objects (clone, copy, destroy)
  90. * The default fast path of a NULL MPOL_DEFAULT policy is always inlined.
  91. */
  92. extern void __mpol_put(struct mempolicy *pol);
  93. static inline void mpol_put(struct mempolicy *pol)
  94. {
  95. if (pol)
  96. __mpol_put(pol);
  97. }
  98. /*
  99. * Does mempolicy pol need explicit unref after use?
  100. * Currently only needed for shared policies.
  101. */
  102. static inline int mpol_needs_cond_ref(struct mempolicy *pol)
  103. {
  104. return (pol && (pol->flags & MPOL_F_SHARED));
  105. }
  106. static inline void mpol_cond_put(struct mempolicy *pol)
  107. {
  108. if (mpol_needs_cond_ref(pol))
  109. __mpol_put(pol);
  110. }
  111. extern struct mempolicy *__mpol_cond_copy(struct mempolicy *tompol,
  112. struct mempolicy *frompol);
  113. static inline struct mempolicy *mpol_cond_copy(struct mempolicy *tompol,
  114. struct mempolicy *frompol)
  115. {
  116. if (!frompol)
  117. return frompol;
  118. return __mpol_cond_copy(tompol, frompol);
  119. }
  120. extern struct mempolicy *__mpol_dup(struct mempolicy *pol);
  121. static inline struct mempolicy *mpol_dup(struct mempolicy *pol)
  122. {
  123. if (pol)
  124. pol = __mpol_dup(pol);
  125. return pol;
  126. }
  127. #define vma_policy(vma) ((vma)->vm_policy)
  128. #define vma_set_policy(vma, pol) ((vma)->vm_policy = (pol))
  129. static inline void mpol_get(struct mempolicy *pol)
  130. {
  131. if (pol)
  132. atomic_inc(&pol->refcnt);
  133. }
  134. extern int __mpol_equal(struct mempolicy *a, struct mempolicy *b);
  135. static inline int mpol_equal(struct mempolicy *a, struct mempolicy *b)
  136. {
  137. if (a == b)
  138. return 1;
  139. return __mpol_equal(a, b);
  140. }
  141. /*
  142. * Tree of shared policies for a shared memory region.
  143. * Maintain the policies in a pseudo mm that contains vmas. The vmas
  144. * carry the policy. As a special twist the pseudo mm is indexed in pages, not
  145. * bytes, so that we can work with shared memory segments bigger than
  146. * unsigned long.
  147. */
  148. struct sp_node {
  149. struct rb_node nd;
  150. unsigned long start, end;
  151. struct mempolicy *policy;
  152. };
  153. struct shared_policy {
  154. struct rb_root root;
  155. spinlock_t lock;
  156. };
  157. void mpol_shared_policy_init(struct shared_policy *info, unsigned short mode,
  158. unsigned short flags, nodemask_t *nodes);
  159. int mpol_set_shared_policy(struct shared_policy *info,
  160. struct vm_area_struct *vma,
  161. struct mempolicy *new);
  162. void mpol_free_shared_policy(struct shared_policy *p);
  163. struct mempolicy *mpol_shared_policy_lookup(struct shared_policy *sp,
  164. unsigned long idx);
  165. extern void numa_default_policy(void);
  166. extern void numa_policy_init(void);
  167. extern void mpol_rebind_task(struct task_struct *tsk,
  168. const nodemask_t *new);
  169. extern void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new);
  170. extern void mpol_fix_fork_child_flag(struct task_struct *p);
  171. extern struct zonelist *huge_zonelist(struct vm_area_struct *vma,
  172. unsigned long addr, gfp_t gfp_flags,
  173. struct mempolicy **mpol, nodemask_t **nodemask);
  174. extern unsigned slab_node(struct mempolicy *policy);
  175. extern enum zone_type policy_zone;
  176. static inline void check_highest_zone(enum zone_type k)
  177. {
  178. if (k > policy_zone && k != ZONE_MOVABLE)
  179. policy_zone = k;
  180. }
  181. int do_migrate_pages(struct mm_struct *mm,
  182. const nodemask_t *from_nodes, const nodemask_t *to_nodes, int flags);
  183. #else
  184. struct mempolicy {};
  185. static inline int mpol_equal(struct mempolicy *a, struct mempolicy *b)
  186. {
  187. return 1;
  188. }
  189. static inline void mpol_put(struct mempolicy *p)
  190. {
  191. }
  192. static inline void mpol_cond_put(struct mempolicy *pol)
  193. {
  194. }
  195. static inline struct mempolicy *mpol_cond_copy(struct mempolicy *to,
  196. struct mempolicy *from)
  197. {
  198. return from;
  199. }
  200. static inline void mpol_get(struct mempolicy *pol)
  201. {
  202. }
  203. static inline struct mempolicy *mpol_dup(struct mempolicy *old)
  204. {
  205. return NULL;
  206. }
  207. struct shared_policy {};
  208. static inline int mpol_set_shared_policy(struct shared_policy *info,
  209. struct vm_area_struct *vma,
  210. struct mempolicy *new)
  211. {
  212. return -EINVAL;
  213. }
  214. static inline void mpol_shared_policy_init(struct shared_policy *info,
  215. unsigned short mode, unsigned short flags, nodemask_t *nodes)
  216. {
  217. }
  218. static inline void mpol_free_shared_policy(struct shared_policy *p)
  219. {
  220. }
  221. static inline struct mempolicy *
  222. mpol_shared_policy_lookup(struct shared_policy *sp, unsigned long idx)
  223. {
  224. return NULL;
  225. }
  226. #define vma_policy(vma) NULL
  227. #define vma_set_policy(vma, pol) do {} while(0)
  228. static inline void numa_policy_init(void)
  229. {
  230. }
  231. static inline void numa_default_policy(void)
  232. {
  233. }
  234. static inline void mpol_rebind_task(struct task_struct *tsk,
  235. const nodemask_t *new)
  236. {
  237. }
  238. static inline void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new)
  239. {
  240. }
  241. static inline void mpol_fix_fork_child_flag(struct task_struct *p)
  242. {
  243. }
  244. static inline struct zonelist *huge_zonelist(struct vm_area_struct *vma,
  245. unsigned long addr, gfp_t gfp_flags,
  246. struct mempolicy **mpol, nodemask_t **nodemask)
  247. {
  248. *mpol = NULL;
  249. *nodemask = NULL;
  250. return node_zonelist(0, gfp_flags);
  251. }
  252. static inline int do_migrate_pages(struct mm_struct *mm,
  253. const nodemask_t *from_nodes,
  254. const nodemask_t *to_nodes, int flags)
  255. {
  256. return 0;
  257. }
  258. static inline void check_highest_zone(int k)
  259. {
  260. }
  261. #endif /* CONFIG_NUMA */
  262. #endif /* __KERNEL__ */
  263. #endif