cpuset.h 8.2 KB

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