mempolicy.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. enum mpol_rebind_step {
  22. MPOL_REBIND_ONCE, /* do rebind work at once(not by two step) */
  23. MPOL_REBIND_STEP1, /* first step(set all the newly nodes) */
  24. MPOL_REBIND_STEP2, /* second step(clean all the disallowed nodes)*/
  25. MPOL_REBIND_NSTEP,
  26. };
  27. /* Flags for set_mempolicy */
  28. #define MPOL_F_STATIC_NODES (1 << 15)
  29. #define MPOL_F_RELATIVE_NODES (1 << 14)
  30. /*
  31. * MPOL_MODE_FLAGS is the union of all possible optional mode flags passed to
  32. * either set_mempolicy() or mbind().
  33. */
  34. #define MPOL_MODE_FLAGS (MPOL_F_STATIC_NODES | MPOL_F_RELATIVE_NODES)
  35. /* Flags for get_mempolicy */
  36. #define MPOL_F_NODE (1<<0) /* return next IL mode instead of node mask */
  37. #define MPOL_F_ADDR (1<<1) /* look up vma using address */
  38. #define MPOL_F_MEMS_ALLOWED (1<<2) /* return allowed memories */
  39. /* Flags for mbind */
  40. #define MPOL_MF_STRICT (1<<0) /* Verify existing pages in the mapping */
  41. #define MPOL_MF_MOVE (1<<1) /* Move pages owned by this process to conform to mapping */
  42. #define MPOL_MF_MOVE_ALL (1<<2) /* Move every page to conform to mapping */
  43. #define MPOL_MF_INTERNAL (1<<3) /* Internal flags start here */
  44. /*
  45. * Internal flags that share the struct mempolicy flags word with
  46. * "mode flags". These flags are allocated from bit 0 up, as they
  47. * are never OR'ed into the mode in mempolicy API arguments.
  48. */
  49. #define MPOL_F_SHARED (1 << 0) /* identify shared policies */
  50. #define MPOL_F_LOCAL (1 << 1) /* preferred local allocation */
  51. #define MPOL_F_REBINDING (1 << 2) /* identify policies in rebinding */
  52. #ifdef __KERNEL__
  53. #include <linux/mmzone.h>
  54. #include <linux/slab.h>
  55. #include <linux/rbtree.h>
  56. #include <linux/spinlock.h>
  57. #include <linux/nodemask.h>
  58. #include <linux/pagemap.h>
  59. struct mm_struct;
  60. #ifdef CONFIG_NUMA
  61. /*
  62. * Describe a memory policy.
  63. *
  64. * A mempolicy can be either associated with a process or with a VMA.
  65. * For VMA related allocations the VMA policy is preferred, otherwise
  66. * the process policy is used. Interrupts ignore the memory policy
  67. * of the current process.
  68. *
  69. * Locking policy for interlave:
  70. * In process context there is no locking because only the process accesses
  71. * its own state. All vma manipulation is somewhat protected by a down_read on
  72. * mmap_sem.
  73. *
  74. * Freeing policy:
  75. * Mempolicy objects are reference counted. A mempolicy will be freed when
  76. * mpol_put() decrements the reference count to zero.
  77. *
  78. * Duplicating policy objects:
  79. * mpol_dup() allocates a new mempolicy and copies the specified mempolicy
  80. * to the new storage. The reference count of the new object is initialized
  81. * to 1, representing the caller of mpol_dup().
  82. */
  83. struct mempolicy {
  84. atomic_t refcnt;
  85. unsigned short mode; /* See MPOL_* above */
  86. unsigned short flags; /* See set_mempolicy() MPOL_F_* above */
  87. union {
  88. short preferred_node; /* preferred */
  89. nodemask_t nodes; /* interleave/bind */
  90. /* undefined for default */
  91. } v;
  92. union {
  93. nodemask_t cpuset_mems_allowed; /* relative to these nodes */
  94. nodemask_t user_nodemask; /* nodemask passed by user */
  95. } w;
  96. };
  97. /*
  98. * Support for managing mempolicy data objects (clone, copy, destroy)
  99. * The default fast path of a NULL MPOL_DEFAULT policy is always inlined.
  100. */
  101. extern void __mpol_put(struct mempolicy *pol);
  102. static inline void mpol_put(struct mempolicy *pol)
  103. {
  104. if (pol)
  105. __mpol_put(pol);
  106. }
  107. /*
  108. * Does mempolicy pol need explicit unref after use?
  109. * Currently only needed for shared policies.
  110. */
  111. static inline int mpol_needs_cond_ref(struct mempolicy *pol)
  112. {
  113. return (pol && (pol->flags & MPOL_F_SHARED));
  114. }
  115. static inline void mpol_cond_put(struct mempolicy *pol)
  116. {
  117. if (mpol_needs_cond_ref(pol))
  118. __mpol_put(pol);
  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. static inline void mpol_get(struct mempolicy *pol)
  129. {
  130. if (pol)
  131. atomic_inc(&pol->refcnt);
  132. }
  133. extern bool __mpol_equal(struct mempolicy *a, struct mempolicy *b);
  134. static inline bool mpol_equal(struct mempolicy *a, struct mempolicy *b)
  135. {
  136. if (a == b)
  137. return true;
  138. return __mpol_equal(a, b);
  139. }
  140. /*
  141. * Tree of shared policies for a shared memory region.
  142. * Maintain the policies in a pseudo mm that contains vmas. The vmas
  143. * carry the policy. As a special twist the pseudo mm is indexed in pages, not
  144. * bytes, so that we can work with shared memory segments bigger than
  145. * unsigned long.
  146. */
  147. struct sp_node {
  148. struct rb_node nd;
  149. unsigned long start, end;
  150. struct mempolicy *policy;
  151. };
  152. struct shared_policy {
  153. struct rb_root root;
  154. struct mutex mutex;
  155. };
  156. int vma_dup_policy(struct vm_area_struct *src, struct vm_area_struct *dst);
  157. void mpol_shared_policy_init(struct shared_policy *sp, struct mempolicy *mpol);
  158. int mpol_set_shared_policy(struct shared_policy *info,
  159. struct vm_area_struct *vma,
  160. struct mempolicy *new);
  161. void mpol_free_shared_policy(struct shared_policy *p);
  162. struct mempolicy *mpol_shared_policy_lookup(struct shared_policy *sp,
  163. unsigned long idx);
  164. struct mempolicy *get_vma_policy(struct task_struct *tsk,
  165. struct vm_area_struct *vma, unsigned long addr);
  166. extern void numa_default_policy(void);
  167. extern void numa_policy_init(void);
  168. extern void mpol_rebind_task(struct task_struct *tsk, const nodemask_t *new,
  169. enum mpol_rebind_step step);
  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 bool init_nodemask_of_mempolicy(nodemask_t *mask);
  176. extern bool mempolicy_nodemask_intersects(struct task_struct *tsk,
  177. const nodemask_t *mask);
  178. extern unsigned slab_node(void);
  179. extern enum zone_type policy_zone;
  180. static inline void check_highest_zone(enum zone_type k)
  181. {
  182. if (k > policy_zone && k != ZONE_MOVABLE)
  183. policy_zone = k;
  184. }
  185. int do_migrate_pages(struct mm_struct *mm,
  186. const nodemask_t *from_nodes, const nodemask_t *to_nodes, int flags);
  187. #ifdef CONFIG_TMPFS
  188. extern int mpol_parse_str(char *str, struct mempolicy **mpol, int no_context);
  189. #endif
  190. extern int mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol,
  191. int no_context);
  192. /* Check if a vma is migratable */
  193. static inline int vma_migratable(struct vm_area_struct *vma)
  194. {
  195. if (vma->vm_flags & (VM_IO|VM_HUGETLB|VM_PFNMAP|VM_RESERVED))
  196. return 0;
  197. /*
  198. * Migration allocates pages in the highest zone. If we cannot
  199. * do so then migration (at least from node to node) is not
  200. * possible.
  201. */
  202. if (vma->vm_file &&
  203. gfp_zone(mapping_gfp_mask(vma->vm_file->f_mapping))
  204. < policy_zone)
  205. return 0;
  206. return 1;
  207. }
  208. #else
  209. struct mempolicy {};
  210. static inline bool mpol_equal(struct mempolicy *a, struct mempolicy *b)
  211. {
  212. return true;
  213. }
  214. static inline void mpol_put(struct mempolicy *p)
  215. {
  216. }
  217. static inline void mpol_cond_put(struct mempolicy *pol)
  218. {
  219. }
  220. static inline void mpol_get(struct mempolicy *pol)
  221. {
  222. }
  223. static inline struct mempolicy *mpol_dup(struct mempolicy *old)
  224. {
  225. return NULL;
  226. }
  227. struct shared_policy {};
  228. static inline int mpol_set_shared_policy(struct shared_policy *info,
  229. struct vm_area_struct *vma,
  230. struct mempolicy *new)
  231. {
  232. return -EINVAL;
  233. }
  234. static inline void mpol_shared_policy_init(struct shared_policy *sp,
  235. struct mempolicy *mpol)
  236. {
  237. }
  238. static inline void mpol_free_shared_policy(struct shared_policy *p)
  239. {
  240. }
  241. static inline struct mempolicy *
  242. mpol_shared_policy_lookup(struct shared_policy *sp, unsigned long idx)
  243. {
  244. return NULL;
  245. }
  246. #define vma_policy(vma) NULL
  247. static inline int
  248. vma_dup_policy(struct vm_area_struct *src, struct vm_area_struct *dst)
  249. {
  250. return 0;
  251. }
  252. static inline void numa_policy_init(void)
  253. {
  254. }
  255. static inline void numa_default_policy(void)
  256. {
  257. }
  258. static inline void mpol_rebind_task(struct task_struct *tsk,
  259. const nodemask_t *new,
  260. enum mpol_rebind_step step)
  261. {
  262. }
  263. static inline void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new)
  264. {
  265. }
  266. static inline void mpol_fix_fork_child_flag(struct task_struct *p)
  267. {
  268. }
  269. static inline struct zonelist *huge_zonelist(struct vm_area_struct *vma,
  270. unsigned long addr, gfp_t gfp_flags,
  271. struct mempolicy **mpol, nodemask_t **nodemask)
  272. {
  273. *mpol = NULL;
  274. *nodemask = NULL;
  275. return node_zonelist(0, gfp_flags);
  276. }
  277. static inline bool init_nodemask_of_mempolicy(nodemask_t *m)
  278. {
  279. return false;
  280. }
  281. static inline bool mempolicy_nodemask_intersects(struct task_struct *tsk,
  282. const nodemask_t *mask)
  283. {
  284. return false;
  285. }
  286. static inline int do_migrate_pages(struct mm_struct *mm,
  287. const nodemask_t *from_nodes,
  288. const nodemask_t *to_nodes, int flags)
  289. {
  290. return 0;
  291. }
  292. static inline void check_highest_zone(int k)
  293. {
  294. }
  295. #ifdef CONFIG_TMPFS
  296. static inline int mpol_parse_str(char *str, struct mempolicy **mpol,
  297. int no_context)
  298. {
  299. return 1; /* error */
  300. }
  301. #endif
  302. static inline int mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol,
  303. int no_context)
  304. {
  305. return 0;
  306. }
  307. #endif /* CONFIG_NUMA */
  308. #endif /* __KERNEL__ */
  309. #endif