mempolicy.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. /* Policies */
  9. #define MPOL_DEFAULT 0
  10. #define MPOL_PREFERRED 1
  11. #define MPOL_BIND 2
  12. #define MPOL_INTERLEAVE 3
  13. #define MPOL_MAX MPOL_INTERLEAVE
  14. /* Flags for get_mem_policy */
  15. #define MPOL_F_NODE (1<<0) /* return next IL mode instead of node mask */
  16. #define MPOL_F_ADDR (1<<1) /* look up vma using address */
  17. /* Flags for mbind */
  18. #define MPOL_MF_STRICT (1<<0) /* Verify existing pages in the mapping */
  19. #ifdef __KERNEL__
  20. #include <linux/config.h>
  21. #include <linux/mmzone.h>
  22. #include <linux/slab.h>
  23. #include <linux/rbtree.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/nodemask.h>
  26. struct vm_area_struct;
  27. #ifdef CONFIG_NUMA
  28. /*
  29. * Describe a memory policy.
  30. *
  31. * A mempolicy can be either associated with a process or with a VMA.
  32. * For VMA related allocations the VMA policy is preferred, otherwise
  33. * the process policy is used. Interrupts ignore the memory policy
  34. * of the current process.
  35. *
  36. * Locking policy for interlave:
  37. * In process context there is no locking because only the process accesses
  38. * its own state. All vma manipulation is somewhat protected by a down_read on
  39. * mmap_sem.
  40. *
  41. * Freeing policy:
  42. * When policy is MPOL_BIND v.zonelist is kmalloc'ed and must be kfree'd.
  43. * All other policies don't have any external state. mpol_free() handles this.
  44. *
  45. * Copying policy objects:
  46. * For MPOL_BIND the zonelist must be always duplicated. mpol_clone() does this.
  47. */
  48. struct mempolicy {
  49. atomic_t refcnt;
  50. short policy; /* See MPOL_* above */
  51. union {
  52. struct zonelist *zonelist; /* bind */
  53. short preferred_node; /* preferred */
  54. nodemask_t nodes; /* interleave */
  55. /* undefined for default */
  56. } v;
  57. };
  58. /*
  59. * Support for managing mempolicy data objects (clone, copy, destroy)
  60. * The default fast path of a NULL MPOL_DEFAULT policy is always inlined.
  61. */
  62. extern void __mpol_free(struct mempolicy *pol);
  63. static inline void mpol_free(struct mempolicy *pol)
  64. {
  65. if (pol)
  66. __mpol_free(pol);
  67. }
  68. extern struct mempolicy *__mpol_copy(struct mempolicy *pol);
  69. static inline struct mempolicy *mpol_copy(struct mempolicy *pol)
  70. {
  71. if (pol)
  72. pol = __mpol_copy(pol);
  73. return pol;
  74. }
  75. #define vma_policy(vma) ((vma)->vm_policy)
  76. #define vma_set_policy(vma, pol) ((vma)->vm_policy = (pol))
  77. static inline void mpol_get(struct mempolicy *pol)
  78. {
  79. if (pol)
  80. atomic_inc(&pol->refcnt);
  81. }
  82. extern int __mpol_equal(struct mempolicy *a, struct mempolicy *b);
  83. static inline int mpol_equal(struct mempolicy *a, struct mempolicy *b)
  84. {
  85. if (a == b)
  86. return 1;
  87. return __mpol_equal(a, b);
  88. }
  89. #define vma_mpol_equal(a,b) mpol_equal(vma_policy(a), vma_policy(b))
  90. /* Could later add inheritance of the process policy here. */
  91. #define mpol_set_vma_default(vma) ((vma)->vm_policy = NULL)
  92. /*
  93. * Hugetlb policy. i386 hugetlb so far works with node numbers
  94. * instead of zone lists, so give it special interfaces for now.
  95. */
  96. extern int mpol_first_node(struct vm_area_struct *vma, unsigned long addr);
  97. extern int mpol_node_valid(int nid, struct vm_area_struct *vma,
  98. unsigned long addr);
  99. /*
  100. * Tree of shared policies for a shared memory region.
  101. * Maintain the policies in a pseudo mm that contains vmas. The vmas
  102. * carry the policy. As a special twist the pseudo mm is indexed in pages, not
  103. * bytes, so that we can work with shared memory segments bigger than
  104. * unsigned long.
  105. */
  106. struct sp_node {
  107. struct rb_node nd;
  108. unsigned long start, end;
  109. struct mempolicy *policy;
  110. };
  111. struct shared_policy {
  112. struct rb_root root;
  113. spinlock_t lock;
  114. };
  115. static inline void mpol_shared_policy_init(struct shared_policy *info)
  116. {
  117. info->root = RB_ROOT;
  118. spin_lock_init(&info->lock);
  119. }
  120. int mpol_set_shared_policy(struct shared_policy *info,
  121. struct vm_area_struct *vma,
  122. struct mempolicy *new);
  123. void mpol_free_shared_policy(struct shared_policy *p);
  124. struct mempolicy *mpol_shared_policy_lookup(struct shared_policy *sp,
  125. unsigned long idx);
  126. struct mempolicy *get_vma_policy(struct task_struct *task,
  127. struct vm_area_struct *vma, unsigned long addr);
  128. extern void numa_default_policy(void);
  129. extern void numa_policy_init(void);
  130. extern void numa_policy_rebind(const nodemask_t *old, const nodemask_t *new);
  131. extern struct mempolicy default_policy;
  132. #else
  133. struct mempolicy {};
  134. static inline int mpol_equal(struct mempolicy *a, struct mempolicy *b)
  135. {
  136. return 1;
  137. }
  138. #define vma_mpol_equal(a,b) 1
  139. #define mpol_set_vma_default(vma) do {} while(0)
  140. static inline void mpol_free(struct mempolicy *p)
  141. {
  142. }
  143. static inline void mpol_get(struct mempolicy *pol)
  144. {
  145. }
  146. static inline struct mempolicy *mpol_copy(struct mempolicy *old)
  147. {
  148. return NULL;
  149. }
  150. static inline int mpol_first_node(struct vm_area_struct *vma, unsigned long a)
  151. {
  152. return numa_node_id();
  153. }
  154. static inline int
  155. mpol_node_valid(int nid, struct vm_area_struct *vma, unsigned long a)
  156. {
  157. return 1;
  158. }
  159. struct shared_policy {};
  160. static inline int mpol_set_shared_policy(struct shared_policy *info,
  161. struct vm_area_struct *vma,
  162. struct mempolicy *new)
  163. {
  164. return -EINVAL;
  165. }
  166. static inline void mpol_shared_policy_init(struct shared_policy *info)
  167. {
  168. }
  169. static inline void mpol_free_shared_policy(struct shared_policy *p)
  170. {
  171. }
  172. static inline struct mempolicy *
  173. mpol_shared_policy_lookup(struct shared_policy *sp, unsigned long idx)
  174. {
  175. return NULL;
  176. }
  177. #define vma_policy(vma) NULL
  178. #define vma_set_policy(vma, pol) do {} while(0)
  179. static inline void numa_policy_init(void)
  180. {
  181. }
  182. static inline void numa_default_policy(void)
  183. {
  184. }
  185. static inline void numa_policy_rebind(const nodemask_t *old,
  186. const nodemask_t *new)
  187. {
  188. }
  189. #endif /* CONFIG_NUMA */
  190. #endif /* __KERNEL__ */
  191. #endif