1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_SCHED_TOPOLOGY_H
3 #define _LINUX_SCHED_TOPOLOGY_H
4
5 #include <linux/topology.h>
6
7 #include <linux/sched/idle.h>
8
9 /*
10 * sched-domains (multiprocessor balancing) declarations:
11 */
12
13 /* Generate SD flag indexes */
14 #define SD_FLAG(name, mflags) __##name,
15 enum {
16 #include <linux/sched/sd_flags.h>
17 __SD_FLAG_CNT,
18 };
19 #undef SD_FLAG
20 /* Generate SD flag bits */
21 #define SD_FLAG(name, mflags) name = 1 << __##name,
22 enum {
23 #include <linux/sched/sd_flags.h>
24 };
25 #undef SD_FLAG
26
27 struct sd_flag_debug {
28 unsigned int meta_flags;
29 char *name;
30 };
31 extern const struct sd_flag_debug sd_flag_debug[];
32
33 struct sched_domain_topology_level;
34
35 #ifdef CONFIG_SCHED_SMT
36 extern int cpu_smt_flags(void);
37 extern const struct cpumask *tl_smt_mask(struct sched_domain_topology_level *tl, int cpu);
38 #endif
39
40 #ifdef CONFIG_SCHED_CLUSTER
41 extern int cpu_cluster_flags(void);
42 extern const struct cpumask *tl_cls_mask(struct sched_domain_topology_level *tl, int cpu);
43 #endif
44
45 #ifdef CONFIG_SCHED_MC
46 extern int cpu_core_flags(void);
47 extern const struct cpumask *tl_mc_mask(struct sched_domain_topology_level *tl, int cpu);
48 #endif
49
50 extern const struct cpumask *tl_pkg_mask(struct sched_domain_topology_level *tl, int cpu);
51
52 extern int arch_asym_cpu_priority(int cpu);
53
54 struct sched_domain_attr {
55 int relax_domain_level;
56 };
57
58 #define SD_ATTR_INIT (struct sched_domain_attr) { \
59 .relax_domain_level = -1, \
60 }
61
62 extern int sched_domain_level_max;
63
64 struct sched_group;
65
66 struct sched_domain_shared {
67 atomic_t ref;
68 atomic_t nr_busy_cpus;
69 int has_idle_cores;
70 union {
71 int nr_idle_scan;
72 /*
73 * Used during allocation to claim the sched_domain_shared
74 * object at multiple levels.
75 *
76 * Note: between build and the first periodic LB tick, which
77 * rewrites the union via update_idle_cpu_scan(), readers of
78 * nr_idle_scan may observe the transient SD_* flag value as
79 * the scan bound. The flag bits are small positive integers,
80 * so the effect is just a slightly relaxed scan bound for one
81 * window and self-heals on the first tick.
82 */
83 int alloc_flags;
84 };
85 #ifdef CONFIG_SCHED_CACHE
86 unsigned long util_avg;
87 unsigned long capacity;
88 #endif
89 };
90
91 struct sched_domain {
92 /* These fields must be setup */
93 struct sched_domain __rcu *parent; /* top domain must be null terminated */
94 struct sched_domain __rcu *child; /* bottom domain must be null terminated */
95 struct sched_group *groups; /* the balancing groups of the domain */
96 unsigned long min_interval; /* Minimum balance interval ms */
97 unsigned long max_interval; /* Maximum balance interval ms */
98 unsigned int busy_factor; /* less balancing by factor if busy */
99 unsigned int imbalance_pct; /* No balance until over watermark */
100 unsigned int cache_nice_tries; /* Leave cache hot tasks for # tries */
101 unsigned int imb_numa_nr; /* Nr running tasks that allows a NUMA imbalance */
102
103 int nohz_idle; /* NOHZ IDLE status */
104 int flags; /* See SD_* */
105 int level;
106
107 /* Runtime fields. */
108 unsigned long last_balance; /* init to jiffies. units in jiffies */
109 unsigned int balance_interval; /* initialise to 1. units in ms. */
110 unsigned int nr_balance_failed; /* initialise to 0 */
111
112 /* idle_balance() stats */
113 unsigned int newidle_call;
114 unsigned int newidle_success;
115 unsigned int newidle_ratio;
116 u64 newidle_stamp;
117 u64 max_newidle_lb_cost;
118 unsigned long last_decay_max_lb_cost;
119
120 #ifdef CONFIG_SCHED_CACHE
121 unsigned int llc_max;
122 unsigned int *llc_counts __counted_by_ptr(llc_max);
123 unsigned long llc_bytes;
124 #endif
125
126 #ifdef CONFIG_SCHEDSTATS
127 /* sched_balance_rq() stats */
128 unsigned int lb_count[CPU_MAX_IDLE_TYPES];
129 unsigned int lb_failed[CPU_MAX_IDLE_TYPES];
130 unsigned int lb_balanced[CPU_MAX_IDLE_TYPES];
131 unsigned int lb_imbalance_load[CPU_MAX_IDLE_TYPES];
132 unsigned int lb_imbalance_util[CPU_MAX_IDLE_TYPES];
133 unsigned int lb_imbalance_task[CPU_MAX_IDLE_TYPES];
134 unsigned int lb_imbalance_misfit[CPU_MAX_IDLE_TYPES];
135 unsigned int lb_gained[CPU_MAX_IDLE_TYPES];
136 unsigned int lb_hot_gained[CPU_MAX_IDLE_TYPES];
137 unsigned int lb_nobusyg[CPU_MAX_IDLE_TYPES];
138 unsigned int lb_nobusyq[CPU_MAX_IDLE_TYPES];
139
140 /* Active load balancing */
141 unsigned int alb_count;
142 unsigned int alb_failed;
143 unsigned int alb_pushed;
144
145 /* SD_BALANCE_EXEC stats */
146 unsigned int sbe_count;
147 unsigned int sbe_balanced;
148 unsigned int sbe_pushed;
149
150 /* SD_BALANCE_FORK stats */
151 unsigned int sbf_count;
152 unsigned int sbf_balanced;
153 unsigned int sbf_pushed;
154
155 /* try_to_wake_up() stats */
156 unsigned int ttwu_wake_remote;
157 unsigned int ttwu_move_affine;
158 unsigned int ttwu_move_balance;
159 #endif
160 char *name;
161 union {
162 void *private; /* used during construction */
163 struct rcu_head rcu; /* used during destruction */
164 };
165 struct sched_domain_shared *shared;
166
167 unsigned int span_weight;
168 /*
169 * See sched_domain_span(), on why flex arrays are broken.
170 *
171 unsigned long span[];
172 */
173 };
174
sched_domain_span(struct sched_domain * sd)175 static inline struct cpumask *sched_domain_span(struct sched_domain *sd)
176 {
177 /*
178 * Turns out that C flexible arrays are fundamentally broken since it
179 * is allowed for offsetof(*sd, span) < sizeof(*sd), this means that
180 * structure initialzation *sd = { ... }; which writes every byte
181 * inside sizeof(*type), will over-write the start of the flexible
182 * array.
183 *
184 * Luckily, the way we allocate sched_domain is by:
185 *
186 * sizeof(*sd) + cpumask_size()
187 *
188 * this means that we have sufficient space for the whole flex array
189 * *outside* of sizeof(*sd). So use that, and avoid using sd->span.
190 */
191 unsigned long *bitmap = (void *)sd + sizeof(*sd);
192 return to_cpumask(bitmap);
193 }
194
195 extern void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
196 struct sched_domain_attr *dattr_new);
197
198 /* Allocate an array of sched domains, for partition_sched_domains(). */
199 cpumask_var_t *alloc_sched_domains(unsigned int ndoms);
200 void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms);
201
202 bool cpus_equal_capacity(int this_cpu, int that_cpu);
203 bool cpus_share_cache(int this_cpu, int that_cpu);
204 bool cpus_share_resources(int this_cpu, int that_cpu);
205
206 typedef const struct cpumask *(*sched_domain_mask_f)(struct sched_domain_topology_level *tl, int cpu);
207 typedef int (*sched_domain_flags_f)(void);
208
209 struct sd_data {
210 struct sched_domain *__percpu *sd;
211 struct sched_group *__percpu *sg;
212 struct sched_group_capacity *__percpu *sgc;
213 };
214
215 struct sched_domain_topology_level {
216 sched_domain_mask_f mask;
217 sched_domain_flags_f sd_flags;
218 int numa_level;
219 struct sd_data data;
220 char *name;
221 };
222
223 extern void __init set_sched_topology(struct sched_domain_topology_level *tl);
224 extern void sched_update_asym_prefer_cpu(int cpu, int old_prio, int new_prio);
225
226 #define SDTL_INIT(maskfn, flagsfn, dname) ((struct sched_domain_topology_level) \
227 { .mask = maskfn, .sd_flags = flagsfn, .name = #dname })
228
229 #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
230 extern void rebuild_sched_domains_energy(void);
231 #else
rebuild_sched_domains_energy(void)232 static inline void rebuild_sched_domains_energy(void)
233 {
234 }
235 #endif
236
237 #ifndef arch_scale_cpu_capacity
238 /**
239 * arch_scale_cpu_capacity - get the capacity scale factor of a given CPU.
240 * @cpu: the CPU in question.
241 *
242 * Return: the CPU scale factor normalized against SCHED_CAPACITY_SCALE, i.e.
243 *
244 * max_perf(cpu)
245 * ----------------------------- * SCHED_CAPACITY_SCALE
246 * max(max_perf(c) : c \in CPUs)
247 */
248 static __always_inline
arch_scale_cpu_capacity(int cpu)249 unsigned long arch_scale_cpu_capacity(int cpu)
250 {
251 return SCHED_CAPACITY_SCALE;
252 }
253 #endif
254
255 #ifndef arch_scale_hw_pressure
256 static __always_inline
arch_scale_hw_pressure(int cpu)257 unsigned long arch_scale_hw_pressure(int cpu)
258 {
259 return 0;
260 }
261 #endif
262
263 #ifndef arch_update_hw_pressure
264 static __always_inline
arch_update_hw_pressure(const struct cpumask * cpus,unsigned long capped_frequency)265 void arch_update_hw_pressure(const struct cpumask *cpus,
266 unsigned long capped_frequency)
267 { }
268 #endif
269
270 #ifndef arch_scale_freq_ref
271 static __always_inline
arch_scale_freq_ref(int cpu)272 unsigned int arch_scale_freq_ref(int cpu)
273 {
274 return 0;
275 }
276 #endif
277
task_node(const struct task_struct * p)278 static inline int task_node(const struct task_struct *p)
279 {
280 return cpu_to_node(task_cpu(p));
281 }
282
283 #ifdef CONFIG_SCHED_CACHE
284 extern void sched_update_llc_bytes(const struct cpumask *cpus);
285 #else
sched_update_llc_bytes(const struct cpumask * cpus)286 static inline void sched_update_llc_bytes(const struct cpumask *cpus) { }
287 #endif
288
289 #endif /* _LINUX_SCHED_TOPOLOGY_H */
290