cpuset.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #ifndef _LINUX_CPUSET_H
  2. #define _LINUX_CPUSET_H
  3. /*
  4. * cpuset interface
  5. *
  6. * Copyright (C) 2003 BULL SA
  7. * Copyright (C) 2004-2006 Silicon Graphics, Inc.
  8. *
  9. */
  10. #include <linux/sched.h>
  11. #include <linux/cpumask.h>
  12. #include <linux/nodemask.h>
  13. #include <linux/mm.h>
  14. #include <linux/jump_label.h>
  15. #ifdef CONFIG_CPUSETS
  16. /*
  17. * Static branch rewrites can happen in an arbitrary order for a given
  18. * key. In code paths where we need to loop with read_mems_allowed_begin() and
  19. * read_mems_allowed_retry() to get a consistent view of mems_allowed, we need
  20. * to ensure that begin() always gets rewritten before retry() in the
  21. * disabled -> enabled transition. If not, then if local irqs are disabled
  22. * around the loop, we can deadlock since retry() would always be
  23. * comparing the latest value of the mems_allowed seqcount against 0 as
  24. * begin() still would see cpusets_enabled() as false. The enabled -> disabled
  25. * transition should happen in reverse order for the same reasons (want to stop
  26. * looking at real value of mems_allowed.sequence in retry() first).
  27. */
  28. extern struct static_key_false cpusets_pre_enable_key;
  29. extern struct static_key_false cpusets_enabled_key;
  30. static inline bool cpusets_enabled(void)
  31. {
  32. return static_branch_unlikely(&cpusets_enabled_key);
  33. }
  34. static inline int nr_cpusets(void)
  35. {
  36. /* jump label reference count + the top-level cpuset */
  37. return static_key_count(&cpusets_enabled_key.key) + 1;
  38. }
  39. static inline void cpuset_inc(void)
  40. {
  41. static_branch_inc(&cpusets_pre_enable_key);
  42. static_branch_inc(&cpusets_enabled_key);
  43. }
  44. static inline void cpuset_dec(void)
  45. {
  46. static_branch_dec(&cpusets_enabled_key);
  47. static_branch_dec(&cpusets_pre_enable_key);
  48. }
  49. extern int cpuset_init(void);
  50. extern void cpuset_init_smp(void);
  51. extern void cpuset_force_rebuild(void);
  52. extern void cpuset_update_active_cpus(bool cpu_online);
  53. extern void cpuset_wait_for_hotplug(void);
  54. extern void cpuset_cpus_allowed(struct task_struct *p, struct cpumask *mask);
  55. extern void cpuset_cpus_allowed_fallback(struct task_struct *p);
  56. extern nodemask_t cpuset_mems_allowed(struct task_struct *p);
  57. #define cpuset_current_mems_allowed (current->mems_allowed)
  58. void cpuset_init_current_mems_allowed(void);
  59. int cpuset_nodemask_valid_mems_allowed(nodemask_t *nodemask);
  60. extern bool __cpuset_node_allowed(int node, gfp_t gfp_mask);
  61. static inline bool cpuset_node_allowed(int node, gfp_t gfp_mask)
  62. {
  63. if (cpusets_enabled())
  64. return __cpuset_node_allowed(node, gfp_mask);
  65. return true;
  66. }
  67. static inline bool __cpuset_zone_allowed(struct zone *z, gfp_t gfp_mask)
  68. {
  69. return __cpuset_node_allowed(zone_to_nid(z), gfp_mask);
  70. }
  71. static inline bool cpuset_zone_allowed(struct zone *z, gfp_t gfp_mask)
  72. {
  73. if (cpusets_enabled())
  74. return __cpuset_zone_allowed(z, gfp_mask);
  75. return true;
  76. }
  77. extern int cpuset_mems_allowed_intersects(const struct task_struct *tsk1,
  78. const struct task_struct *tsk2);
  79. #define cpuset_memory_pressure_bump() \
  80. do { \
  81. if (cpuset_memory_pressure_enabled) \
  82. __cpuset_memory_pressure_bump(); \
  83. } while (0)
  84. extern int cpuset_memory_pressure_enabled;
  85. extern void __cpuset_memory_pressure_bump(void);
  86. extern void cpuset_task_status_allowed(struct seq_file *m,
  87. struct task_struct *task);
  88. extern int proc_cpuset_show(struct seq_file *m, struct pid_namespace *ns,
  89. struct pid *pid, struct task_struct *tsk);
  90. extern int cpuset_mem_spread_node(void);
  91. extern int cpuset_slab_spread_node(void);
  92. static inline int cpuset_do_page_mem_spread(void)
  93. {
  94. return task_spread_page(current);
  95. }
  96. static inline int cpuset_do_slab_mem_spread(void)
  97. {
  98. return task_spread_slab(current);
  99. }
  100. extern int current_cpuset_is_being_rebound(void);
  101. extern void rebuild_sched_domains(void);
  102. extern void cpuset_print_current_mems_allowed(void);
  103. /*
  104. * read_mems_allowed_begin is required when making decisions involving
  105. * mems_allowed such as during page allocation. mems_allowed can be updated in
  106. * parallel and depending on the new value an operation can fail potentially
  107. * causing process failure. A retry loop with read_mems_allowed_begin and
  108. * read_mems_allowed_retry prevents these artificial failures.
  109. */
  110. static inline unsigned int read_mems_allowed_begin(void)
  111. {
  112. if (!static_branch_unlikely(&cpusets_pre_enable_key))
  113. return 0;
  114. return read_seqcount_begin(&current->mems_allowed_seq);
  115. }
  116. /*
  117. * If this returns true, the operation that took place after
  118. * read_mems_allowed_begin may have failed artificially due to a concurrent
  119. * update of mems_allowed. It is up to the caller to retry the operation if
  120. * appropriate.
  121. */
  122. static inline bool read_mems_allowed_retry(unsigned int seq)
  123. {
  124. if (!static_branch_unlikely(&cpusets_enabled_key))
  125. return false;
  126. return read_seqcount_retry(&current->mems_allowed_seq, seq);
  127. }
  128. static inline void set_mems_allowed(nodemask_t nodemask)
  129. {
  130. unsigned long flags;
  131. task_lock(current);
  132. local_irq_save(flags);
  133. write_seqcount_begin(&current->mems_allowed_seq);
  134. current->mems_allowed = nodemask;
  135. write_seqcount_end(&current->mems_allowed_seq);
  136. local_irq_restore(flags);
  137. task_unlock(current);
  138. }
  139. #else /* !CONFIG_CPUSETS */
  140. static inline bool cpusets_enabled(void) { return false; }
  141. static inline int cpuset_init(void) { return 0; }
  142. static inline void cpuset_init_smp(void) {}
  143. static inline void cpuset_force_rebuild(void) { }
  144. static inline void cpuset_update_active_cpus(bool cpu_online)
  145. {
  146. partition_sched_domains(1, NULL, NULL);
  147. }
  148. static inline void cpuset_wait_for_hotplug(void) { }
  149. static inline void cpuset_cpus_allowed(struct task_struct *p,
  150. struct cpumask *mask)
  151. {
  152. cpumask_copy(mask, cpu_possible_mask);
  153. }
  154. static inline void cpuset_cpus_allowed_fallback(struct task_struct *p)
  155. {
  156. }
  157. static inline nodemask_t cpuset_mems_allowed(struct task_struct *p)
  158. {
  159. return node_possible_map;
  160. }
  161. #define cpuset_current_mems_allowed (node_states[N_MEMORY])
  162. static inline void cpuset_init_current_mems_allowed(void) {}
  163. static inline int cpuset_nodemask_valid_mems_allowed(nodemask_t *nodemask)
  164. {
  165. return 1;
  166. }
  167. static inline bool cpuset_node_allowed(int node, gfp_t gfp_mask)
  168. {
  169. return true;
  170. }
  171. static inline bool __cpuset_zone_allowed(struct zone *z, gfp_t gfp_mask)
  172. {
  173. return true;
  174. }
  175. static inline bool cpuset_zone_allowed(struct zone *z, gfp_t gfp_mask)
  176. {
  177. return true;
  178. }
  179. static inline int cpuset_mems_allowed_intersects(const struct task_struct *tsk1,
  180. const struct task_struct *tsk2)
  181. {
  182. return 1;
  183. }
  184. static inline void cpuset_memory_pressure_bump(void) {}
  185. static inline void cpuset_task_status_allowed(struct seq_file *m,
  186. struct task_struct *task)
  187. {
  188. }
  189. static inline int cpuset_mem_spread_node(void)
  190. {
  191. return 0;
  192. }
  193. static inline int cpuset_slab_spread_node(void)
  194. {
  195. return 0;
  196. }
  197. static inline int cpuset_do_page_mem_spread(void)
  198. {
  199. return 0;
  200. }
  201. static inline int cpuset_do_slab_mem_spread(void)
  202. {
  203. return 0;
  204. }
  205. static inline int current_cpuset_is_being_rebound(void)
  206. {
  207. return 0;
  208. }
  209. static inline void rebuild_sched_domains(void)
  210. {
  211. partition_sched_domains(1, NULL, NULL);
  212. }
  213. static inline void cpuset_print_current_mems_allowed(void)
  214. {
  215. }
  216. static inline void set_mems_allowed(nodemask_t nodemask)
  217. {
  218. }
  219. static inline unsigned int read_mems_allowed_begin(void)
  220. {
  221. return 0;
  222. }
  223. static inline bool read_mems_allowed_retry(unsigned int seq)
  224. {
  225. return false;
  226. }
  227. #endif /* !CONFIG_CPUSETS */
  228. #endif /* _LINUX_CPUSET_H */