mempolicy.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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/bitmap.h>
  23. #include <linux/slab.h>
  24. #include <linux/rbtree.h>
  25. #include <linux/spinlock.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. For allocating in the interleave policy the page_table_lock
  40. * must be also aquired to protect il_next.
  41. *
  42. * Freeing policy:
  43. * When policy is MPOL_BIND v.zonelist is kmalloc'ed and must be kfree'd.
  44. * All other policies don't have any external state. mpol_free() handles this.
  45. *
  46. * Copying policy objects:
  47. * For MPOL_BIND the zonelist must be always duplicated. mpol_clone() does this.
  48. */
  49. struct mempolicy {
  50. atomic_t refcnt;
  51. short policy; /* See MPOL_* above */
  52. union {
  53. struct zonelist *zonelist; /* bind */
  54. short preferred_node; /* preferred */
  55. DECLARE_BITMAP(nodes, MAX_NUMNODES); /* interleave */
  56. /* undefined for default */
  57. } v;
  58. };
  59. /*
  60. * Support for managing mempolicy data objects (clone, copy, destroy)
  61. * The default fast path of a NULL MPOL_DEFAULT policy is always inlined.
  62. */
  63. extern void __mpol_free(struct mempolicy *pol);
  64. static inline void mpol_free(struct mempolicy *pol)
  65. {
  66. if (pol)
  67. __mpol_free(pol);
  68. }
  69. extern struct mempolicy *__mpol_copy(struct mempolicy *pol);
  70. static inline struct mempolicy *mpol_copy(struct mempolicy *pol)
  71. {
  72. if (pol)
  73. pol = __mpol_copy(pol);
  74. return pol;
  75. }
  76. #define vma_policy(vma) ((vma)->vm_policy)
  77. #define vma_set_policy(vma, pol) ((vma)->vm_policy = (pol))
  78. static inline void mpol_get(struct mempolicy *pol)
  79. {
  80. if (pol)
  81. atomic_inc(&pol->refcnt);
  82. }
  83. extern int __mpol_equal(struct mempolicy *a, struct mempolicy *b);
  84. static inline int mpol_equal(struct mempolicy *a, struct mempolicy *b)
  85. {
  86. if (a == b)
  87. return 1;
  88. return __mpol_equal(a, b);
  89. }
  90. #define vma_mpol_equal(a,b) mpol_equal(vma_policy(a), vma_policy(b))
  91. /* Could later add inheritance of the process policy here. */
  92. #define mpol_set_vma_default(vma) ((vma)->vm_policy = NULL)
  93. /*
  94. * Hugetlb policy. i386 hugetlb so far works with node numbers
  95. * instead of zone lists, so give it special interfaces for now.
  96. */
  97. extern int mpol_first_node(struct vm_area_struct *vma, unsigned long addr);
  98. extern int mpol_node_valid(int nid, struct vm_area_struct *vma,
  99. unsigned long addr);
  100. /*
  101. * Tree of shared policies for a shared memory region.
  102. * Maintain the policies in a pseudo mm that contains vmas. The vmas
  103. * carry the policy. As a special twist the pseudo mm is indexed in pages, not
  104. * bytes, so that we can work with shared memory segments bigger than
  105. * unsigned long.
  106. */
  107. struct sp_node {
  108. struct rb_node nd;
  109. unsigned long start, end;
  110. struct mempolicy *policy;
  111. };
  112. struct shared_policy {
  113. struct rb_root root;
  114. spinlock_t lock;
  115. };
  116. static inline void mpol_shared_policy_init(struct shared_policy *info)
  117. {
  118. info->root = RB_ROOT;
  119. spin_lock_init(&info->lock);
  120. }
  121. int mpol_set_shared_policy(struct shared_policy *info,
  122. struct vm_area_struct *vma,
  123. struct mempolicy *new);
  124. void mpol_free_shared_policy(struct shared_policy *p);
  125. struct mempolicy *mpol_shared_policy_lookup(struct shared_policy *sp,
  126. unsigned long idx);
  127. extern void numa_default_policy(void);
  128. extern void numa_policy_init(void);
  129. #else
  130. struct mempolicy {};
  131. static inline int mpol_equal(struct mempolicy *a, struct mempolicy *b)
  132. {
  133. return 1;
  134. }
  135. #define vma_mpol_equal(a,b) 1
  136. #define mpol_set_vma_default(vma) do {} while(0)
  137. static inline void mpol_free(struct mempolicy *p)
  138. {
  139. }
  140. static inline void mpol_get(struct mempolicy *pol)
  141. {
  142. }
  143. static inline struct mempolicy *mpol_copy(struct mempolicy *old)
  144. {
  145. return NULL;
  146. }
  147. static inline int mpol_first_node(struct vm_area_struct *vma, unsigned long a)
  148. {
  149. return numa_node_id();
  150. }
  151. static inline int
  152. mpol_node_valid(int nid, struct vm_area_struct *vma, unsigned long a)
  153. {
  154. return 1;
  155. }
  156. struct shared_policy {};
  157. static inline int mpol_set_shared_policy(struct shared_policy *info,
  158. struct vm_area_struct *vma,
  159. struct mempolicy *new)
  160. {
  161. return -EINVAL;
  162. }
  163. static inline void mpol_shared_policy_init(struct shared_policy *info)
  164. {
  165. }
  166. static inline void mpol_free_shared_policy(struct shared_policy *p)
  167. {
  168. }
  169. static inline struct mempolicy *
  170. mpol_shared_policy_lookup(struct shared_policy *sp, unsigned long idx)
  171. {
  172. return NULL;
  173. }
  174. #define vma_policy(vma) NULL
  175. #define vma_set_policy(vma, pol) do {} while(0)
  176. static inline void numa_policy_init(void)
  177. {
  178. }
  179. static inline void numa_default_policy(void)
  180. {
  181. }
  182. #endif /* CONFIG_NUMA */
  183. #endif /* __KERNEL__ */
  184. #endif