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 #ifdef CONFIG_SMP
13
14 #define SD_LOAD_BALANCE 0x0001 /* Do load balancing on this domain. */
15 #define SD_BALANCE_NEWIDLE 0x0002 /* Balance when about to become idle */
16 #define SD_BALANCE_EXEC 0x0004 /* Balance on exec */
17 #define SD_BALANCE_FORK 0x0008 /* Balance on fork, clone */
18 #define SD_BALANCE_WAKE 0x0010 /* Balance on wakeup */
19 #define SD_WAKE_AFFINE 0x0020 /* Wake task to waking CPU */
20 #define SD_ASYM_CPUCAPACITY 0x0040 /* Groups have different max cpu capacities */
21 #define SD_SHARE_CPUCAPACITY 0x0080 /* Domain members share cpu capacity */
22 #define SD_SHARE_POWERDOMAIN 0x0100 /* Domain members share power domain */
23 #define SD_SHARE_PKG_RESOURCES 0x0200 /* Domain members share cpu pkg resources */
24 #define SD_SERIALIZE 0x0400 /* Only a single load balancing instance */
25 #define SD_ASYM_PACKING 0x0800 /* Place busy groups earlier in the domain */
26 #define SD_PREFER_SIBLING 0x1000 /* Prefer to place tasks in a sibling domain */
27 #define SD_OVERLAP 0x2000 /* sched_domains of this level overlap */
28 #define SD_NUMA 0x4000 /* cross-node balancing */
29 #define SD_SHARE_CAP_STATES 0x8000 /* Domain members share capacity state */
30
31 /*
32 * Increase resolution of cpu_capacity calculations
33 */
34 #define SCHED_CAPACITY_SHIFT SCHED_FIXEDPOINT_SHIFT
35 #define SCHED_CAPACITY_SCALE (1L << SCHED_CAPACITY_SHIFT)
36
37 #ifdef CONFIG_SCHED_SMT
cpu_smt_flags(void)38 static inline int cpu_smt_flags(void)
39 {
40 return SD_SHARE_CPUCAPACITY | SD_SHARE_PKG_RESOURCES;
41 }
42 #endif
43
44 #ifdef CONFIG_SCHED_MC
cpu_core_flags(void)45 static inline int cpu_core_flags(void)
46 {
47 return SD_SHARE_PKG_RESOURCES;
48 }
49 #endif
50
51 #ifdef CONFIG_NUMA
cpu_numa_flags(void)52 static inline int cpu_numa_flags(void)
53 {
54 return SD_NUMA;
55 }
56 #endif
57
58 extern int arch_asym_cpu_priority(int cpu);
59
60 struct sched_domain_attr {
61 int relax_domain_level;
62 };
63
64 #define SD_ATTR_INIT (struct sched_domain_attr) { \
65 .relax_domain_level = -1, \
66 }
67
68 extern int sched_domain_level_max;
69
70 struct capacity_state {
71 unsigned long cap; /* compute capacity */
72 unsigned long frequency;/* frequency */
73 unsigned long power; /* power consumption at this compute capacity */
74 };
75
76 struct idle_state {
77 unsigned long power; /* power consumption in this idle state */
78 };
79
80 struct sched_group_energy {
81 unsigned int nr_idle_states; /* number of idle states */
82 struct idle_state *idle_states; /* ptr to idle state array */
83 unsigned int nr_cap_states; /* number of capacity states */
84 struct capacity_state *cap_states; /* ptr to capacity state array */
85 };
86
87 struct sched_group;
88
89 struct sched_domain_shared {
90 atomic_t ref;
91 atomic_t nr_busy_cpus;
92 int has_idle_cores;
93
94 bool overutilized;
95 };
96
97 struct sched_domain {
98 /* These fields must be setup */
99 struct sched_domain *parent; /* top domain must be null terminated */
100 struct sched_domain *child; /* bottom domain must be null terminated */
101 struct sched_group *groups; /* the balancing groups of the domain */
102 unsigned long min_interval; /* Minimum balance interval ms */
103 unsigned long max_interval; /* Maximum balance interval ms */
104 unsigned int busy_factor; /* less balancing by factor if busy */
105 unsigned int imbalance_pct; /* No balance until over watermark */
106 unsigned int cache_nice_tries; /* Leave cache hot tasks for # tries */
107 unsigned int busy_idx;
108 unsigned int idle_idx;
109 unsigned int newidle_idx;
110 unsigned int wake_idx;
111 unsigned int forkexec_idx;
112 unsigned int smt_gain;
113
114 int nohz_idle; /* NOHZ IDLE status */
115 int flags; /* See SD_* */
116 int level;
117
118 /* Runtime fields. */
119 unsigned long last_balance; /* init to jiffies. units in jiffies */
120 unsigned int balance_interval; /* initialise to 1. units in ms. */
121 unsigned int nr_balance_failed; /* initialise to 0 */
122
123 /* idle_balance() stats */
124 u64 max_newidle_lb_cost;
125 unsigned long next_decay_max_lb_cost;
126
127 u64 avg_scan_cost; /* select_idle_sibling */
128
129 #ifdef CONFIG_SCHEDSTATS
130 /* load_balance() stats */
131 unsigned int lb_count[CPU_MAX_IDLE_TYPES];
132 unsigned int lb_failed[CPU_MAX_IDLE_TYPES];
133 unsigned int lb_balanced[CPU_MAX_IDLE_TYPES];
134 unsigned int lb_imbalance[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 #ifdef CONFIG_SCHED_DEBUG
161 char *name;
162 #endif
163 union {
164 void *private; /* used during construction */
165 struct rcu_head rcu; /* used during destruction */
166 };
167 struct sched_domain_shared *shared;
168
169 unsigned int span_weight;
170 /*
171 * Span of all CPUs in this domain.
172 *
173 * NOTE: this field is variable length. (Allocated dynamically
174 * by attaching extra space to the end of the structure,
175 * depending on how many CPUs the kernel has booted up with)
176 */
177 unsigned long span[0];
178 };
179
sched_domain_span(struct sched_domain * sd)180 static inline struct cpumask *sched_domain_span(struct sched_domain *sd)
181 {
182 return to_cpumask(sd->span);
183 }
184
185 extern void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
186 struct sched_domain_attr *dattr_new);
187
188 /* Allocate an array of sched domains, for partition_sched_domains(). */
189 cpumask_var_t *alloc_sched_domains(unsigned int ndoms);
190 void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms);
191
192 bool cpus_share_cache(int this_cpu, int that_cpu);
193
194 typedef const struct cpumask *(*sched_domain_mask_f)(int cpu);
195 typedef int (*sched_domain_flags_f)(void);
196 typedef
197 const struct sched_group_energy * const(*sched_domain_energy_f)(int cpu);
198 extern bool energy_aware(void);
199
200 #define SDTL_OVERLAP 0x01
201
202 struct sd_data {
203 struct sched_domain *__percpu *sd;
204 struct sched_domain_shared *__percpu *sds;
205 struct sched_group *__percpu *sg;
206 struct sched_group_capacity *__percpu *sgc;
207 };
208
209 struct sched_domain_topology_level {
210 sched_domain_mask_f mask;
211 sched_domain_flags_f sd_flags;
212 sched_domain_energy_f energy;
213 int flags;
214 int numa_level;
215 struct sd_data data;
216 #ifdef CONFIG_SCHED_DEBUG
217 char *name;
218 #endif
219 };
220
221 extern void set_sched_topology(struct sched_domain_topology_level *tl);
222
223 #ifdef CONFIG_SCHED_DEBUG
224 # define SD_INIT_NAME(type) .name = #type
225 #else
226 # define SD_INIT_NAME(type)
227 #endif
228
229 #else /* CONFIG_SMP */
230
231 struct sched_domain_attr;
232
233 static inline void
partition_sched_domains(int ndoms_new,cpumask_var_t doms_new[],struct sched_domain_attr * dattr_new)234 partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
235 struct sched_domain_attr *dattr_new)
236 {
237 }
238
cpus_share_cache(int this_cpu,int that_cpu)239 static inline bool cpus_share_cache(int this_cpu, int that_cpu)
240 {
241 return true;
242 }
243
244 #endif /* !CONFIG_SMP */
245
task_node(const struct task_struct * p)246 static inline int task_node(const struct task_struct *p)
247 {
248 return cpu_to_node(task_cpu(p));
249 }
250
251 #endif /* _LINUX_SCHED_TOPOLOGY_H */
252