cpuset-internal.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. #ifndef __CPUSET_INTERNAL_H
  3. #define __CPUSET_INTERNAL_H
  4. #include <linux/cgroup.h>
  5. #include <linux/cpu.h>
  6. #include <linux/cpumask.h>
  7. #include <linux/cpuset.h>
  8. #include <linux/spinlock.h>
  9. #include <linux/union_find.h>
  10. /* See "Frequency meter" comments, below. */
  11. struct fmeter {
  12. int cnt; /* unprocessed events count */
  13. int val; /* most recent output value */
  14. time64_t time; /* clock (secs) when val computed */
  15. spinlock_t lock; /* guards read or write of above */
  16. };
  17. /*
  18. * Invalid partition error code
  19. */
  20. enum prs_errcode {
  21. PERR_NONE = 0,
  22. PERR_INVCPUS,
  23. PERR_INVPARENT,
  24. PERR_NOTPART,
  25. PERR_NOTEXCL,
  26. PERR_NOCPUS,
  27. PERR_HOTPLUG,
  28. PERR_CPUSEMPTY,
  29. PERR_HKEEPING,
  30. PERR_ACCESS,
  31. };
  32. /* bits in struct cpuset flags field */
  33. typedef enum {
  34. CS_ONLINE,
  35. CS_CPU_EXCLUSIVE,
  36. CS_MEM_EXCLUSIVE,
  37. CS_MEM_HARDWALL,
  38. CS_MEMORY_MIGRATE,
  39. CS_SCHED_LOAD_BALANCE,
  40. CS_SPREAD_PAGE,
  41. CS_SPREAD_SLAB,
  42. } cpuset_flagbits_t;
  43. /* The various types of files and directories in a cpuset file system */
  44. typedef enum {
  45. FILE_MEMORY_MIGRATE,
  46. FILE_CPULIST,
  47. FILE_MEMLIST,
  48. FILE_EFFECTIVE_CPULIST,
  49. FILE_EFFECTIVE_MEMLIST,
  50. FILE_SUBPARTS_CPULIST,
  51. FILE_EXCLUSIVE_CPULIST,
  52. FILE_EFFECTIVE_XCPULIST,
  53. FILE_ISOLATED_CPULIST,
  54. FILE_CPU_EXCLUSIVE,
  55. FILE_MEM_EXCLUSIVE,
  56. FILE_MEM_HARDWALL,
  57. FILE_SCHED_LOAD_BALANCE,
  58. FILE_PARTITION_ROOT,
  59. FILE_SCHED_RELAX_DOMAIN_LEVEL,
  60. FILE_MEMORY_PRESSURE_ENABLED,
  61. FILE_MEMORY_PRESSURE,
  62. FILE_SPREAD_PAGE,
  63. FILE_SPREAD_SLAB,
  64. } cpuset_filetype_t;
  65. struct cpuset {
  66. struct cgroup_subsys_state css;
  67. unsigned long flags; /* "unsigned long" so bitops work */
  68. /*
  69. * On default hierarchy:
  70. *
  71. * The user-configured masks can only be changed by writing to
  72. * cpuset.cpus and cpuset.mems, and won't be limited by the
  73. * parent masks.
  74. *
  75. * The effective masks is the real masks that apply to the tasks
  76. * in the cpuset. They may be changed if the configured masks are
  77. * changed or hotplug happens.
  78. *
  79. * effective_mask == configured_mask & parent's effective_mask,
  80. * and if it ends up empty, it will inherit the parent's mask.
  81. *
  82. *
  83. * On legacy hierarchy:
  84. *
  85. * The user-configured masks are always the same with effective masks.
  86. */
  87. /* user-configured CPUs and Memory Nodes allow to tasks */
  88. cpumask_var_t cpus_allowed;
  89. nodemask_t mems_allowed;
  90. /* effective CPUs and Memory Nodes allow to tasks */
  91. cpumask_var_t effective_cpus;
  92. nodemask_t effective_mems;
  93. /*
  94. * Exclusive CPUs dedicated to current cgroup (default hierarchy only)
  95. *
  96. * The effective_cpus of a valid partition root comes solely from its
  97. * effective_xcpus and some of the effective_xcpus may be distributed
  98. * to sub-partitions below & hence excluded from its effective_cpus.
  99. * For a valid partition root, its effective_cpus have no relationship
  100. * with cpus_allowed unless its exclusive_cpus isn't set.
  101. *
  102. * This value will only be set if either exclusive_cpus is set or
  103. * when this cpuset becomes a local partition root.
  104. */
  105. cpumask_var_t effective_xcpus;
  106. /*
  107. * Exclusive CPUs as requested by the user (default hierarchy only)
  108. *
  109. * Its value is independent of cpus_allowed and designates the set of
  110. * CPUs that can be granted to the current cpuset or its children when
  111. * it becomes a valid partition root. The effective set of exclusive
  112. * CPUs granted (effective_xcpus) depends on whether those exclusive
  113. * CPUs are passed down by its ancestors and not yet taken up by
  114. * another sibling partition root along the way.
  115. *
  116. * If its value isn't set, it defaults to cpus_allowed.
  117. */
  118. cpumask_var_t exclusive_cpus;
  119. /*
  120. * This is old Memory Nodes tasks took on.
  121. *
  122. * - top_cpuset.old_mems_allowed is initialized to mems_allowed.
  123. * - A new cpuset's old_mems_allowed is initialized when some
  124. * task is moved into it.
  125. * - old_mems_allowed is used in cpuset_migrate_mm() when we change
  126. * cpuset.mems_allowed and have tasks' nodemask updated, and
  127. * then old_mems_allowed is updated to mems_allowed.
  128. */
  129. nodemask_t old_mems_allowed;
  130. struct fmeter fmeter; /* memory_pressure filter */
  131. /*
  132. * Tasks are being attached to this cpuset. Used to prevent
  133. * zeroing cpus/mems_allowed between ->can_attach() and ->attach().
  134. */
  135. int attach_in_progress;
  136. /* for custom sched domain */
  137. int relax_domain_level;
  138. /* number of valid local child partitions */
  139. int nr_subparts;
  140. /* partition root state */
  141. int partition_root_state;
  142. /*
  143. * number of SCHED_DEADLINE tasks attached to this cpuset, so that we
  144. * know when to rebuild associated root domain bandwidth information.
  145. */
  146. int nr_deadline_tasks;
  147. int nr_migrate_dl_tasks;
  148. u64 sum_migrate_dl_bw;
  149. /* Invalid partition error code, not lock protected */
  150. enum prs_errcode prs_err;
  151. /* Handle for cpuset.cpus.partition */
  152. struct cgroup_file partition_file;
  153. /* Remote partition silbling list anchored at remote_children */
  154. struct list_head remote_sibling;
  155. /* Used to merge intersecting subsets for generate_sched_domains */
  156. struct uf_node node;
  157. };
  158. static inline struct cpuset *css_cs(struct cgroup_subsys_state *css)
  159. {
  160. return css ? container_of(css, struct cpuset, css) : NULL;
  161. }
  162. /* Retrieve the cpuset for a task */
  163. static inline struct cpuset *task_cs(struct task_struct *task)
  164. {
  165. return css_cs(task_css(task, cpuset_cgrp_id));
  166. }
  167. static inline struct cpuset *parent_cs(struct cpuset *cs)
  168. {
  169. return css_cs(cs->css.parent);
  170. }
  171. /* convenient tests for these bits */
  172. static inline bool is_cpuset_online(struct cpuset *cs)
  173. {
  174. return test_bit(CS_ONLINE, &cs->flags) && !css_is_dying(&cs->css);
  175. }
  176. static inline int is_cpu_exclusive(const struct cpuset *cs)
  177. {
  178. return test_bit(CS_CPU_EXCLUSIVE, &cs->flags);
  179. }
  180. static inline int is_mem_exclusive(const struct cpuset *cs)
  181. {
  182. return test_bit(CS_MEM_EXCLUSIVE, &cs->flags);
  183. }
  184. static inline int is_mem_hardwall(const struct cpuset *cs)
  185. {
  186. return test_bit(CS_MEM_HARDWALL, &cs->flags);
  187. }
  188. static inline int is_sched_load_balance(const struct cpuset *cs)
  189. {
  190. return test_bit(CS_SCHED_LOAD_BALANCE, &cs->flags);
  191. }
  192. static inline int is_memory_migrate(const struct cpuset *cs)
  193. {
  194. return test_bit(CS_MEMORY_MIGRATE, &cs->flags);
  195. }
  196. static inline int is_spread_page(const struct cpuset *cs)
  197. {
  198. return test_bit(CS_SPREAD_PAGE, &cs->flags);
  199. }
  200. static inline int is_spread_slab(const struct cpuset *cs)
  201. {
  202. return test_bit(CS_SPREAD_SLAB, &cs->flags);
  203. }
  204. /**
  205. * cpuset_for_each_child - traverse online children of a cpuset
  206. * @child_cs: loop cursor pointing to the current child
  207. * @pos_css: used for iteration
  208. * @parent_cs: target cpuset to walk children of
  209. *
  210. * Walk @child_cs through the online children of @parent_cs. Must be used
  211. * with RCU read locked.
  212. */
  213. #define cpuset_for_each_child(child_cs, pos_css, parent_cs) \
  214. css_for_each_child((pos_css), &(parent_cs)->css) \
  215. if (is_cpuset_online(((child_cs) = css_cs((pos_css)))))
  216. /**
  217. * cpuset_for_each_descendant_pre - pre-order walk of a cpuset's descendants
  218. * @des_cs: loop cursor pointing to the current descendant
  219. * @pos_css: used for iteration
  220. * @root_cs: target cpuset to walk ancestor of
  221. *
  222. * Walk @des_cs through the online descendants of @root_cs. Must be used
  223. * with RCU read locked. The caller may modify @pos_css by calling
  224. * css_rightmost_descendant() to skip subtree. @root_cs is included in the
  225. * iteration and the first node to be visited.
  226. */
  227. #define cpuset_for_each_descendant_pre(des_cs, pos_css, root_cs) \
  228. css_for_each_descendant_pre((pos_css), &(root_cs)->css) \
  229. if (is_cpuset_online(((des_cs) = css_cs((pos_css)))))
  230. void rebuild_sched_domains_locked(void);
  231. void cpuset_callback_lock_irq(void);
  232. void cpuset_callback_unlock_irq(void);
  233. void cpuset_update_tasks_cpumask(struct cpuset *cs, struct cpumask *new_cpus);
  234. void cpuset_update_tasks_nodemask(struct cpuset *cs);
  235. int cpuset_update_flag(cpuset_flagbits_t bit, struct cpuset *cs, int turning_on);
  236. ssize_t cpuset_write_resmask(struct kernfs_open_file *of,
  237. char *buf, size_t nbytes, loff_t off);
  238. int cpuset_common_seq_show(struct seq_file *sf, void *v);
  239. /*
  240. * cpuset-v1.c
  241. */
  242. #ifdef CONFIG_CPUSETS_V1
  243. extern struct cftype cpuset1_files[];
  244. void fmeter_init(struct fmeter *fmp);
  245. void cpuset1_update_task_spread_flags(struct cpuset *cs,
  246. struct task_struct *tsk);
  247. void cpuset1_update_tasks_flags(struct cpuset *cs);
  248. void cpuset1_hotplug_update_tasks(struct cpuset *cs,
  249. struct cpumask *new_cpus, nodemask_t *new_mems,
  250. bool cpus_updated, bool mems_updated);
  251. int cpuset1_validate_change(struct cpuset *cur, struct cpuset *trial);
  252. #else
  253. static inline void fmeter_init(struct fmeter *fmp) {}
  254. static inline void cpuset1_update_task_spread_flags(struct cpuset *cs,
  255. struct task_struct *tsk) {}
  256. static inline void cpuset1_update_tasks_flags(struct cpuset *cs) {}
  257. static inline void cpuset1_hotplug_update_tasks(struct cpuset *cs,
  258. struct cpumask *new_cpus, nodemask_t *new_mems,
  259. bool cpus_updated, bool mems_updated) {}
  260. static inline int cpuset1_validate_change(struct cpuset *cur,
  261. struct cpuset *trial) { return 0; }
  262. #endif /* CONFIG_CPUSETS_V1 */
  263. #endif /* __CPUSET_INTERNAL_H */