mempolicy.h 8.9 KB

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