mempolicy.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. #define MPOL_F_LOCAL (1 << 1) /* preferred local allocation */
  45. #ifdef __KERNEL__
  46. #include <linux/mmzone.h>
  47. #include <linux/slab.h>
  48. #include <linux/rbtree.h>
  49. #include <linux/spinlock.h>
  50. #include <linux/nodemask.h>
  51. struct mm_struct;
  52. #ifdef CONFIG_NUMA
  53. /*
  54. * Describe a memory policy.
  55. *
  56. * A mempolicy can be either associated with a process or with a VMA.
  57. * For VMA related allocations the VMA policy is preferred, otherwise
  58. * the process policy is used. Interrupts ignore the memory policy
  59. * of the current process.
  60. *
  61. * Locking policy for interlave:
  62. * In process context there is no locking because only the process accesses
  63. * its own state. All vma manipulation is somewhat protected by a down_read on
  64. * mmap_sem.
  65. *
  66. * Freeing policy:
  67. * Mempolicy objects are reference counted. A mempolicy will be freed when
  68. * mpol_put() decrements the reference count to zero.
  69. *
  70. * Duplicating policy objects:
  71. * mpol_dup() allocates a new mempolicy and copies the specified mempolicy
  72. * to the new storage. The reference count of the new object is initialized
  73. * to 1, representing the caller of mpol_dup().
  74. */
  75. struct mempolicy {
  76. atomic_t refcnt;
  77. unsigned short mode; /* See MPOL_* above */
  78. unsigned short flags; /* See set_mempolicy() MPOL_F_* above */
  79. union {
  80. short preferred_node; /* preferred */
  81. nodemask_t nodes; /* interleave/bind */
  82. /* undefined for default */
  83. } v;
  84. union {
  85. nodemask_t cpuset_mems_allowed; /* relative to these nodes */
  86. nodemask_t user_nodemask; /* nodemask passed by user */
  87. } w;
  88. };
  89. /*
  90. * Support for managing mempolicy data objects (clone, copy, destroy)
  91. * The default fast path of a NULL MPOL_DEFAULT policy is always inlined.
  92. */
  93. extern void __mpol_put(struct mempolicy *pol);
  94. static inline void mpol_put(struct mempolicy *pol)
  95. {
  96. if (pol)
  97. __mpol_put(pol);
  98. }
  99. /*
  100. * Does mempolicy pol need explicit unref after use?
  101. * Currently only needed for shared policies.
  102. */
  103. static inline int mpol_needs_cond_ref(struct mempolicy *pol)
  104. {
  105. return (pol && (pol->flags & MPOL_F_SHARED));
  106. }
  107. static inline void mpol_cond_put(struct mempolicy *pol)
  108. {
  109. if (mpol_needs_cond_ref(pol))
  110. __mpol_put(pol);
  111. }
  112. extern struct mempolicy *__mpol_cond_copy(struct mempolicy *tompol,
  113. struct mempolicy *frompol);
  114. static inline struct mempolicy *mpol_cond_copy(struct mempolicy *tompol,
  115. struct mempolicy *frompol)
  116. {
  117. if (!frompol)
  118. return frompol;
  119. return __mpol_cond_copy(tompol, frompol);
  120. }
  121. extern struct mempolicy *__mpol_dup(struct mempolicy *pol);
  122. static inline struct mempolicy *mpol_dup(struct mempolicy *pol)
  123. {
  124. if (pol)
  125. pol = __mpol_dup(pol);
  126. return pol;
  127. }
  128. #define vma_policy(vma) ((vma)->vm_policy)
  129. #define vma_set_policy(vma, pol) ((vma)->vm_policy = (pol))
  130. static inline void mpol_get(struct mempolicy *pol)
  131. {
  132. if (pol)
  133. atomic_inc(&pol->refcnt);
  134. }
  135. extern int __mpol_equal(struct mempolicy *a, struct mempolicy *b);
  136. static inline int mpol_equal(struct mempolicy *a, struct mempolicy *b)
  137. {
  138. if (a == b)
  139. return 1;
  140. return __mpol_equal(a, b);
  141. }
  142. /*
  143. * Tree of shared policies for a shared memory region.
  144. * Maintain the policies in a pseudo mm that contains vmas. The vmas
  145. * carry the policy. As a special twist the pseudo mm is indexed in pages, not
  146. * bytes, so that we can work with shared memory segments bigger than
  147. * unsigned long.
  148. */
  149. struct sp_node {
  150. struct rb_node nd;
  151. unsigned long start, end;
  152. struct mempolicy *policy;
  153. };
  154. struct shared_policy {
  155. struct rb_root root;
  156. spinlock_t lock;
  157. };
  158. void mpol_shared_policy_init(struct shared_policy *info, unsigned short mode,
  159. unsigned short flags, nodemask_t *nodes);
  160. int mpol_set_shared_policy(struct shared_policy *info,
  161. struct vm_area_struct *vma,
  162. struct mempolicy *new);
  163. void mpol_free_shared_policy(struct shared_policy *p);
  164. struct mempolicy *mpol_shared_policy_lookup(struct shared_policy *sp,
  165. unsigned long idx);
  166. extern void numa_default_policy(void);
  167. extern void numa_policy_init(void);
  168. extern void mpol_rebind_task(struct task_struct *tsk,
  169. const nodemask_t *new);
  170. extern void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new);
  171. extern void mpol_fix_fork_child_flag(struct task_struct *p);
  172. extern struct zonelist *huge_zonelist(struct vm_area_struct *vma,
  173. unsigned long addr, gfp_t gfp_flags,
  174. struct mempolicy **mpol, nodemask_t **nodemask);
  175. extern unsigned slab_node(struct mempolicy *policy);
  176. extern enum zone_type policy_zone;
  177. static inline void check_highest_zone(enum zone_type k)
  178. {
  179. if (k > policy_zone && k != ZONE_MOVABLE)
  180. policy_zone = k;
  181. }
  182. int do_migrate_pages(struct mm_struct *mm,
  183. const nodemask_t *from_nodes, const nodemask_t *to_nodes, int flags);
  184. #ifdef CONFIG_TMPFS
  185. extern int mpol_parse_str(char *str, unsigned short *mode,
  186. unsigned short *mode_flags, nodemask_t *policy_nodes);
  187. extern int mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol);
  188. #endif
  189. #else
  190. struct mempolicy {};
  191. static inline int mpol_equal(struct mempolicy *a, struct mempolicy *b)
  192. {
  193. return 1;
  194. }
  195. static inline void mpol_put(struct mempolicy *p)
  196. {
  197. }
  198. static inline void mpol_cond_put(struct mempolicy *pol)
  199. {
  200. }
  201. static inline struct mempolicy *mpol_cond_copy(struct mempolicy *to,
  202. struct mempolicy *from)
  203. {
  204. return from;
  205. }
  206. static inline void mpol_get(struct mempolicy *pol)
  207. {
  208. }
  209. static inline struct mempolicy *mpol_dup(struct mempolicy *old)
  210. {
  211. return NULL;
  212. }
  213. struct shared_policy {};
  214. static inline int mpol_set_shared_policy(struct shared_policy *info,
  215. struct vm_area_struct *vma,
  216. struct mempolicy *new)
  217. {
  218. return -EINVAL;
  219. }
  220. static inline void mpol_shared_policy_init(struct shared_policy *info,
  221. unsigned short mode, unsigned short flags, nodemask_t *nodes)
  222. {
  223. }
  224. static inline void mpol_free_shared_policy(struct shared_policy *p)
  225. {
  226. }
  227. static inline struct mempolicy *
  228. mpol_shared_policy_lookup(struct shared_policy *sp, unsigned long idx)
  229. {
  230. return NULL;
  231. }
  232. #define vma_policy(vma) NULL
  233. #define vma_set_policy(vma, pol) do {} while(0)
  234. static inline void numa_policy_init(void)
  235. {
  236. }
  237. static inline void numa_default_policy(void)
  238. {
  239. }
  240. static inline void mpol_rebind_task(struct task_struct *tsk,
  241. const nodemask_t *new)
  242. {
  243. }
  244. static inline void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new)
  245. {
  246. }
  247. static inline void mpol_fix_fork_child_flag(struct task_struct *p)
  248. {
  249. }
  250. static inline struct zonelist *huge_zonelist(struct vm_area_struct *vma,
  251. unsigned long addr, gfp_t gfp_flags,
  252. struct mempolicy **mpol, nodemask_t **nodemask)
  253. {
  254. *mpol = NULL;
  255. *nodemask = NULL;
  256. return node_zonelist(0, gfp_flags);
  257. }
  258. static inline int do_migrate_pages(struct mm_struct *mm,
  259. const nodemask_t *from_nodes,
  260. const nodemask_t *to_nodes, int flags)
  261. {
  262. return 0;
  263. }
  264. static inline void check_highest_zone(int k)
  265. {
  266. }
  267. #ifdef CONFIG_TMPFS
  268. static inline int mpol_parse_str(char *value, unsigned short *policy,
  269. unsigned short flags, nodemask_t *policy_nodes)
  270. {
  271. return 1;
  272. }
  273. static inline int mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol)
  274. {
  275. return 0;
  276. }
  277. #endif
  278. #endif /* CONFIG_NUMA */
  279. #endif /* __KERNEL__ */
  280. #endif