cpuset.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. static inline bool cpusets_enabled(void)
  35. {
  36. return static_branch_unlikely(&cpusets_enabled_key);
  37. }
  38. static inline void cpuset_inc(void)
  39. {
  40. static_branch_inc_cpuslocked(&cpusets_pre_enable_key);
  41. static_branch_inc_cpuslocked(&cpusets_enabled_key);
  42. }
  43. static inline void cpuset_dec(void)
  44. {
  45. static_branch_dec_cpuslocked(&cpusets_enabled_key);
  46. static_branch_dec_cpuslocked(&cpusets_pre_enable_key);
  47. }
  48. extern int cpuset_init(void);
  49. extern void cpuset_init_smp(void);
  50. extern void cpuset_force_rebuild(void);
  51. extern void cpuset_update_active_cpus(void);
  52. extern void cpuset_update_active_cpus_affine(int cpu);
  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 bool 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. extern void cpuset_hotplug_workfn(struct work_struct *work);
  140. #else /* !CONFIG_CPUSETS */
  141. static inline bool cpusets_enabled(void) { return false; }
  142. static inline int cpuset_init(void) { return 0; }
  143. static inline void cpuset_init_smp(void) {}
  144. static inline void cpuset_force_rebuild(void) { }
  145. static inline void cpuset_update_active_cpus_affine(int cpu) {}
  146. static inline void cpuset_update_active_cpus(void)
  147. {
  148. partition_sched_domains(1, NULL, NULL);
  149. }
  150. static inline void cpuset_wait_for_hotplug(void) { }
  151. static inline void cpuset_cpus_allowed(struct task_struct *p,
  152. struct cpumask *mask)
  153. {
  154. cpumask_copy(mask, task_cpu_possible_mask(p));
  155. }
  156. static inline void cpuset_cpus_allowed_fallback(struct task_struct *p)
  157. {
  158. }
  159. static inline nodemask_t cpuset_mems_allowed(struct task_struct *p)
  160. {
  161. return node_possible_map;
  162. }
  163. #define cpuset_current_mems_allowed (node_states[N_MEMORY])
  164. static inline void cpuset_init_current_mems_allowed(void) {}
  165. static inline int cpuset_nodemask_valid_mems_allowed(nodemask_t *nodemask)
  166. {
  167. return 1;
  168. }
  169. static inline bool cpuset_node_allowed(int node, gfp_t gfp_mask)
  170. {
  171. return true;
  172. }
  173. static inline bool __cpuset_zone_allowed(struct zone *z, gfp_t gfp_mask)
  174. {
  175. return true;
  176. }
  177. static inline bool cpuset_zone_allowed(struct zone *z, gfp_t gfp_mask)
  178. {
  179. return true;
  180. }
  181. static inline int cpuset_mems_allowed_intersects(const struct task_struct *tsk1,
  182. const struct task_struct *tsk2)
  183. {
  184. return 1;
  185. }
  186. static inline void cpuset_memory_pressure_bump(void) {}
  187. static inline void cpuset_task_status_allowed(struct seq_file *m,
  188. struct task_struct *task)
  189. {
  190. }
  191. static inline int cpuset_mem_spread_node(void)
  192. {
  193. return 0;
  194. }
  195. static inline int cpuset_slab_spread_node(void)
  196. {
  197. return 0;
  198. }
  199. static inline int cpuset_do_page_mem_spread(void)
  200. {
  201. return 0;
  202. }
  203. static inline int cpuset_do_slab_mem_spread(void)
  204. {
  205. return 0;
  206. }
  207. static inline bool current_cpuset_is_being_rebound(void)
  208. {
  209. return false;
  210. }
  211. static inline void rebuild_sched_domains(void)
  212. {
  213. partition_sched_domains(1, NULL, NULL);
  214. }
  215. static inline void cpuset_print_current_mems_allowed(void)
  216. {
  217. }
  218. static inline void set_mems_allowed(nodemask_t nodemask)
  219. {
  220. }
  221. static inline unsigned int read_mems_allowed_begin(void)
  222. {
  223. return 0;
  224. }
  225. static inline bool read_mems_allowed_retry(unsigned int seq)
  226. {
  227. return false;
  228. }
  229. static inline void cpuset_hotplug_workfn(struct work_struct *work) {}
  230. #endif /* !CONFIG_CPUSETS */
  231. #endif /* _LINUX_CPUSET_H */