xref: /linux/include/linux/sched/topology.h (revision ab93e0dd72c37d378dd936f031ffb83ff2bd87ce)
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 #ifdef CONFIG_SCHED_SMT
cpu_smt_flags(void)34 static inline int cpu_smt_flags(void)
35 {
36 	return SD_SHARE_CPUCAPACITY | SD_SHARE_LLC;
37 }
38 #endif
39 
40 #ifdef CONFIG_SCHED_CLUSTER
cpu_cluster_flags(void)41 static inline int cpu_cluster_flags(void)
42 {
43 	return SD_CLUSTER | SD_SHARE_LLC;
44 }
45 #endif
46 
47 #ifdef CONFIG_SCHED_MC
cpu_core_flags(void)48 static inline int cpu_core_flags(void)
49 {
50 	return SD_SHARE_LLC;
51 }
52 #endif
53 
54 #ifdef CONFIG_NUMA
cpu_numa_flags(void)55 static inline int cpu_numa_flags(void)
56 {
57 	return SD_NUMA;
58 }
59 #endif
60 
61 extern int arch_asym_cpu_priority(int cpu);
62 
63 struct sched_domain_attr {
64 	int relax_domain_level;
65 };
66 
67 #define SD_ATTR_INIT	(struct sched_domain_attr) {	\
68 	.relax_domain_level = -1,			\
69 }
70 
71 extern int sched_domain_level_max;
72 
73 struct sched_group;
74 
75 struct sched_domain_shared {
76 	atomic_t	ref;
77 	atomic_t	nr_busy_cpus;
78 	int		has_idle_cores;
79 	int		nr_idle_scan;
80 };
81 
82 struct sched_domain {
83 	/* These fields must be setup */
84 	struct sched_domain __rcu *parent;	/* top domain must be null terminated */
85 	struct sched_domain __rcu *child;	/* bottom domain must be null terminated */
86 	struct sched_group *groups;	/* the balancing groups of the domain */
87 	unsigned long min_interval;	/* Minimum balance interval ms */
88 	unsigned long max_interval;	/* Maximum balance interval ms */
89 	unsigned int busy_factor;	/* less balancing by factor if busy */
90 	unsigned int imbalance_pct;	/* No balance until over watermark */
91 	unsigned int cache_nice_tries;	/* Leave cache hot tasks for # tries */
92 	unsigned int imb_numa_nr;	/* Nr running tasks that allows a NUMA imbalance */
93 
94 	int nohz_idle;			/* NOHZ IDLE status */
95 	int flags;			/* See SD_* */
96 	int level;
97 
98 	/* Runtime fields. */
99 	unsigned long last_balance;	/* init to jiffies. units in jiffies */
100 	unsigned int balance_interval;	/* initialise to 1. units in ms. */
101 	unsigned int nr_balance_failed; /* initialise to 0 */
102 
103 	/* idle_balance() stats */
104 	u64 max_newidle_lb_cost;
105 	unsigned long last_decay_max_lb_cost;
106 
107 #ifdef CONFIG_SCHEDSTATS
108 	/* sched_balance_rq() stats */
109 	unsigned int lb_count[CPU_MAX_IDLE_TYPES];
110 	unsigned int lb_failed[CPU_MAX_IDLE_TYPES];
111 	unsigned int lb_balanced[CPU_MAX_IDLE_TYPES];
112 	unsigned int lb_imbalance_load[CPU_MAX_IDLE_TYPES];
113 	unsigned int lb_imbalance_util[CPU_MAX_IDLE_TYPES];
114 	unsigned int lb_imbalance_task[CPU_MAX_IDLE_TYPES];
115 	unsigned int lb_imbalance_misfit[CPU_MAX_IDLE_TYPES];
116 	unsigned int lb_gained[CPU_MAX_IDLE_TYPES];
117 	unsigned int lb_hot_gained[CPU_MAX_IDLE_TYPES];
118 	unsigned int lb_nobusyg[CPU_MAX_IDLE_TYPES];
119 	unsigned int lb_nobusyq[CPU_MAX_IDLE_TYPES];
120 
121 	/* Active load balancing */
122 	unsigned int alb_count;
123 	unsigned int alb_failed;
124 	unsigned int alb_pushed;
125 
126 	/* SD_BALANCE_EXEC stats */
127 	unsigned int sbe_count;
128 	unsigned int sbe_balanced;
129 	unsigned int sbe_pushed;
130 
131 	/* SD_BALANCE_FORK stats */
132 	unsigned int sbf_count;
133 	unsigned int sbf_balanced;
134 	unsigned int sbf_pushed;
135 
136 	/* try_to_wake_up() stats */
137 	unsigned int ttwu_wake_remote;
138 	unsigned int ttwu_move_affine;
139 	unsigned int ttwu_move_balance;
140 #endif
141 	char *name;
142 	union {
143 		void *private;		/* used during construction */
144 		struct rcu_head rcu;	/* used during destruction */
145 	};
146 	struct sched_domain_shared *shared;
147 
148 	unsigned int span_weight;
149 	/*
150 	 * Span of all CPUs in this domain.
151 	 *
152 	 * NOTE: this field is variable length. (Allocated dynamically
153 	 * by attaching extra space to the end of the structure,
154 	 * depending on how many CPUs the kernel has booted up with)
155 	 */
156 	unsigned long span[];
157 };
158 
sched_domain_span(struct sched_domain * sd)159 static inline struct cpumask *sched_domain_span(struct sched_domain *sd)
160 {
161 	return to_cpumask(sd->span);
162 }
163 
164 extern void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
165 				    struct sched_domain_attr *dattr_new);
166 
167 /* Allocate an array of sched domains, for partition_sched_domains(). */
168 cpumask_var_t *alloc_sched_domains(unsigned int ndoms);
169 void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms);
170 
171 bool cpus_equal_capacity(int this_cpu, int that_cpu);
172 bool cpus_share_cache(int this_cpu, int that_cpu);
173 bool cpus_share_resources(int this_cpu, int that_cpu);
174 
175 typedef const struct cpumask *(*sched_domain_mask_f)(int cpu);
176 typedef int (*sched_domain_flags_f)(void);
177 
178 struct sd_data {
179 	struct sched_domain *__percpu *sd;
180 	struct sched_domain_shared *__percpu *sds;
181 	struct sched_group *__percpu *sg;
182 	struct sched_group_capacity *__percpu *sgc;
183 };
184 
185 struct sched_domain_topology_level {
186 	sched_domain_mask_f mask;
187 	sched_domain_flags_f sd_flags;
188 	int		    numa_level;
189 	struct sd_data      data;
190 	char                *name;
191 };
192 
193 extern void __init set_sched_topology(struct sched_domain_topology_level *tl);
194 extern void sched_update_asym_prefer_cpu(int cpu, int old_prio, int new_prio);
195 
196 #define SDTL_INIT(maskfn, flagsfn, dname) ((struct sched_domain_topology_level) \
197 	    { .mask = maskfn, .sd_flags = flagsfn, .name = #dname })
198 
199 #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
200 extern void rebuild_sched_domains_energy(void);
201 #else
rebuild_sched_domains_energy(void)202 static inline void rebuild_sched_domains_energy(void)
203 {
204 }
205 #endif
206 
207 #ifndef arch_scale_cpu_capacity
208 /**
209  * arch_scale_cpu_capacity - get the capacity scale factor of a given CPU.
210  * @cpu: the CPU in question.
211  *
212  * Return: the CPU scale factor normalized against SCHED_CAPACITY_SCALE, i.e.
213  *
214  *             max_perf(cpu)
215  *      ----------------------------- * SCHED_CAPACITY_SCALE
216  *      max(max_perf(c) : c \in CPUs)
217  */
218 static __always_inline
arch_scale_cpu_capacity(int cpu)219 unsigned long arch_scale_cpu_capacity(int cpu)
220 {
221 	return SCHED_CAPACITY_SCALE;
222 }
223 #endif
224 
225 #ifndef arch_scale_hw_pressure
226 static __always_inline
arch_scale_hw_pressure(int cpu)227 unsigned long arch_scale_hw_pressure(int cpu)
228 {
229 	return 0;
230 }
231 #endif
232 
233 #ifndef arch_update_hw_pressure
234 static __always_inline
arch_update_hw_pressure(const struct cpumask * cpus,unsigned long capped_frequency)235 void arch_update_hw_pressure(const struct cpumask *cpus,
236 				  unsigned long capped_frequency)
237 { }
238 #endif
239 
240 #ifndef arch_scale_freq_ref
241 static __always_inline
arch_scale_freq_ref(int cpu)242 unsigned int arch_scale_freq_ref(int cpu)
243 {
244 	return 0;
245 }
246 #endif
247 
task_node(const struct task_struct * p)248 static inline int task_node(const struct task_struct *p)
249 {
250 	return cpu_to_node(task_cpu(p));
251 }
252 
253 #endif /* _LINUX_SCHED_TOPOLOGY_H */
254