mempolicy.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. * Tree of shared policies for a shared memory region.
  94. * Maintain the policies in a pseudo mm that contains vmas. The vmas
  95. * carry the policy. As a special twist the pseudo mm is indexed in pages, not
  96. * bytes, so that we can work with shared memory segments bigger than
  97. * unsigned long.
  98. */
  99. struct sp_node {
  100. struct rb_node nd;
  101. unsigned long start, end;
  102. struct mempolicy *policy;
  103. };
  104. struct shared_policy {
  105. struct rb_root root;
  106. spinlock_t lock;
  107. };
  108. static inline void mpol_shared_policy_init(struct shared_policy *info)
  109. {
  110. info->root = RB_ROOT;
  111. spin_lock_init(&info->lock);
  112. }
  113. int mpol_set_shared_policy(struct shared_policy *info,
  114. struct vm_area_struct *vma,
  115. struct mempolicy *new);
  116. void mpol_free_shared_policy(struct shared_policy *p);
  117. struct mempolicy *mpol_shared_policy_lookup(struct shared_policy *sp,
  118. unsigned long idx);
  119. struct mempolicy *get_vma_policy(struct task_struct *task,
  120. struct vm_area_struct *vma, unsigned long addr);
  121. extern void numa_default_policy(void);
  122. extern void numa_policy_init(void);
  123. extern void numa_policy_rebind(const nodemask_t *old, const nodemask_t *new);
  124. extern struct mempolicy default_policy;
  125. extern struct zonelist *huge_zonelist(struct vm_area_struct *vma,
  126. unsigned long addr);
  127. #else
  128. struct mempolicy {};
  129. static inline int mpol_equal(struct mempolicy *a, struct mempolicy *b)
  130. {
  131. return 1;
  132. }
  133. #define vma_mpol_equal(a,b) 1
  134. #define mpol_set_vma_default(vma) do {} while(0)
  135. static inline void mpol_free(struct mempolicy *p)
  136. {
  137. }
  138. static inline void mpol_get(struct mempolicy *pol)
  139. {
  140. }
  141. static inline struct mempolicy *mpol_copy(struct mempolicy *old)
  142. {
  143. return NULL;
  144. }
  145. struct shared_policy {};
  146. static inline int mpol_set_shared_policy(struct shared_policy *info,
  147. struct vm_area_struct *vma,
  148. struct mempolicy *new)
  149. {
  150. return -EINVAL;
  151. }
  152. static inline void mpol_shared_policy_init(struct shared_policy *info)
  153. {
  154. }
  155. static inline void mpol_free_shared_policy(struct shared_policy *p)
  156. {
  157. }
  158. static inline struct mempolicy *
  159. mpol_shared_policy_lookup(struct shared_policy *sp, unsigned long idx)
  160. {
  161. return NULL;
  162. }
  163. #define vma_policy(vma) NULL
  164. #define vma_set_policy(vma, pol) do {} while(0)
  165. static inline void numa_policy_init(void)
  166. {
  167. }
  168. static inline void numa_default_policy(void)
  169. {
  170. }
  171. static inline void numa_policy_rebind(const nodemask_t *old,
  172. const nodemask_t *new)
  173. {
  174. }
  175. static inline struct zonelist *huge_zonelist(struct vm_area_struct *vma,
  176. unsigned long addr)
  177. {
  178. return NODE_DATA(0)->node_zonelists + gfp_zone(GFP_HIGHUSER);
  179. }
  180. #endif /* CONFIG_NUMA */
  181. #endif /* __KERNEL__ */
  182. #endif