blob: 9ef7bf686a9f7937c66d75d445c1907b307b7e73 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Ingo Molnar105ab3d2017-02-01 16:36:40 +01002#ifndef _LINUX_SCHED_TOPOLOGY_H
3#define _LINUX_SCHED_TOPOLOGY_H
4
Ingo Molnaree6a3d12017-02-06 10:01:09 +01005#include <linux/topology.h>
6
Ingo Molnar4c822692017-02-01 16:36:40 +01007#include <linux/sched/idle.h>
8
Ingo Molnara60b9ed2017-02-01 16:36:40 +01009/*
10 * sched-domains (multiprocessor balancing) declarations:
11 */
12#ifdef CONFIG_SMP
13
Valentin Schneiderd54a9652020-08-17 12:29:49 +010014/* Generate SD flag indexes */
Valentin Schneiderb6e862f2020-08-17 12:29:50 +010015#define SD_FLAG(name, mflags) __##name,
Valentin Schneiderd54a9652020-08-17 12:29:49 +010016enum {
17 #include <linux/sched/sd_flags.h>
18 __SD_FLAG_CNT,
19};
20#undef SD_FLAG
21/* Generate SD flag bits */
Valentin Schneiderb6e862f2020-08-17 12:29:50 +010022#define SD_FLAG(name, mflags) name = 1 << __##name,
Valentin Schneiderd54a9652020-08-17 12:29:49 +010023enum {
24 #include <linux/sched/sd_flags.h>
25};
26#undef SD_FLAG
Ingo Molnara60b9ed2017-02-01 16:36:40 +010027
Valentin Schneiderb6e862f2020-08-17 12:29:50 +010028#ifdef CONFIG_SCHED_DEBUG
Valentin Schneider8fca9492020-08-25 14:32:15 +010029
30struct sd_flag_debug {
Valentin Schneiderb6e862f2020-08-17 12:29:50 +010031 unsigned int meta_flags;
32 char *name;
Valentin Schneiderb6e862f2020-08-17 12:29:50 +010033};
Valentin Schneider8fca9492020-08-25 14:32:15 +010034extern const struct sd_flag_debug sd_flag_debug[];
35
Valentin Schneiderb6e862f2020-08-17 12:29:50 +010036#endif
37
Ingo Molnara60b9ed2017-02-01 16:36:40 +010038#ifdef CONFIG_SCHED_SMT
39static inline int cpu_smt_flags(void)
40{
41 return SD_SHARE_CPUCAPACITY | SD_SHARE_PKG_RESOURCES;
42}
43#endif
44
45#ifdef CONFIG_SCHED_MC
46static inline int cpu_core_flags(void)
47{
48 return SD_SHARE_PKG_RESOURCES;
49}
50#endif
51
52#ifdef CONFIG_NUMA
53static inline int cpu_numa_flags(void)
54{
55 return SD_NUMA;
56}
57#endif
58
59extern int arch_asym_cpu_priority(int cpu);
60
61struct sched_domain_attr {
62 int relax_domain_level;
63};
64
65#define SD_ATTR_INIT (struct sched_domain_attr) { \
66 .relax_domain_level = -1, \
67}
68
69extern int sched_domain_level_max;
70
71struct sched_group;
72
73struct sched_domain_shared {
74 atomic_t ref;
75 atomic_t nr_busy_cpus;
76 int has_idle_cores;
77};
78
79struct sched_domain {
80 /* These fields must be setup */
Joel Fernandes (Google)994aeb72019-03-20 20:34:24 -040081 struct sched_domain __rcu *parent; /* top domain must be null terminated */
82 struct sched_domain __rcu *child; /* bottom domain must be null terminated */
Ingo Molnara60b9ed2017-02-01 16:36:40 +010083 struct sched_group *groups; /* the balancing groups of the domain */
84 unsigned long min_interval; /* Minimum balance interval ms */
85 unsigned long max_interval; /* Maximum balance interval ms */
86 unsigned int busy_factor; /* less balancing by factor if busy */
87 unsigned int imbalance_pct; /* No balance until over watermark */
88 unsigned int cache_nice_tries; /* Leave cache hot tasks for # tries */
Ingo Molnara60b9ed2017-02-01 16:36:40 +010089
90 int nohz_idle; /* NOHZ IDLE status */
91 int flags; /* See SD_* */
92 int level;
93
94 /* Runtime fields. */
95 unsigned long last_balance; /* init to jiffies. units in jiffies */
96 unsigned int balance_interval; /* initialise to 1. units in ms. */
97 unsigned int nr_balance_failed; /* initialise to 0 */
98
99 /* idle_balance() stats */
100 u64 max_newidle_lb_cost;
101 unsigned long next_decay_max_lb_cost;
102
103 u64 avg_scan_cost; /* select_idle_sibling */
104
105#ifdef CONFIG_SCHEDSTATS
106 /* load_balance() stats */
107 unsigned int lb_count[CPU_MAX_IDLE_TYPES];
108 unsigned int lb_failed[CPU_MAX_IDLE_TYPES];
109 unsigned int lb_balanced[CPU_MAX_IDLE_TYPES];
110 unsigned int lb_imbalance[CPU_MAX_IDLE_TYPES];
111 unsigned int lb_gained[CPU_MAX_IDLE_TYPES];
112 unsigned int lb_hot_gained[CPU_MAX_IDLE_TYPES];
113 unsigned int lb_nobusyg[CPU_MAX_IDLE_TYPES];
114 unsigned int lb_nobusyq[CPU_MAX_IDLE_TYPES];
115
116 /* Active load balancing */
117 unsigned int alb_count;
118 unsigned int alb_failed;
119 unsigned int alb_pushed;
120
121 /* SD_BALANCE_EXEC stats */
122 unsigned int sbe_count;
123 unsigned int sbe_balanced;
124 unsigned int sbe_pushed;
125
126 /* SD_BALANCE_FORK stats */
127 unsigned int sbf_count;
128 unsigned int sbf_balanced;
129 unsigned int sbf_pushed;
130
131 /* try_to_wake_up() stats */
132 unsigned int ttwu_wake_remote;
133 unsigned int ttwu_move_affine;
134 unsigned int ttwu_move_balance;
135#endif
136#ifdef CONFIG_SCHED_DEBUG
137 char *name;
138#endif
139 union {
140 void *private; /* used during construction */
141 struct rcu_head rcu; /* used during destruction */
142 };
143 struct sched_domain_shared *shared;
144
145 unsigned int span_weight;
146 /*
147 * Span of all CPUs in this domain.
148 *
149 * NOTE: this field is variable length. (Allocated dynamically
150 * by attaching extra space to the end of the structure,
151 * depending on how many CPUs the kernel has booted up with)
152 */
Gustavo A. R. Silvafe946db2020-03-23 19:14:37 -0500153 unsigned long span[];
Ingo Molnara60b9ed2017-02-01 16:36:40 +0100154};
155
156static inline struct cpumask *sched_domain_span(struct sched_domain *sd)
157{
158 return to_cpumask(sd->span);
159}
160
Mathieu Poirierc22645f2019-07-19 15:59:53 +0200161extern void partition_sched_domains_locked(int ndoms_new,
162 cpumask_var_t doms_new[],
163 struct sched_domain_attr *dattr_new);
164
Ingo Molnara60b9ed2017-02-01 16:36:40 +0100165extern void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
166 struct sched_domain_attr *dattr_new);
167
168/* Allocate an array of sched domains, for partition_sched_domains(). */
169cpumask_var_t *alloc_sched_domains(unsigned int ndoms);
170void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms);
171
172bool cpus_share_cache(int this_cpu, int that_cpu);
173
174typedef const struct cpumask *(*sched_domain_mask_f)(int cpu);
175typedef int (*sched_domain_flags_f)(void);
176
177#define SDTL_OVERLAP 0x01
178
179struct sd_data {
Luc Van Oostenryck99687cd2019-01-18 15:49:36 +0100180 struct sched_domain *__percpu *sd;
181 struct sched_domain_shared *__percpu *sds;
182 struct sched_group *__percpu *sg;
183 struct sched_group_capacity *__percpu *sgc;
Ingo Molnara60b9ed2017-02-01 16:36:40 +0100184};
185
186struct sched_domain_topology_level {
187 sched_domain_mask_f mask;
188 sched_domain_flags_f sd_flags;
189 int flags;
190 int numa_level;
191 struct sd_data data;
192#ifdef CONFIG_SCHED_DEBUG
193 char *name;
194#endif
195};
196
197extern void set_sched_topology(struct sched_domain_topology_level *tl);
198
199#ifdef CONFIG_SCHED_DEBUG
200# define SD_INIT_NAME(type) .name = #type
201#else
202# define SD_INIT_NAME(type)
203#endif
204
205#else /* CONFIG_SMP */
206
207struct sched_domain_attr;
208
209static inline void
Mathieu Poirierc22645f2019-07-19 15:59:53 +0200210partition_sched_domains_locked(int ndoms_new, cpumask_var_t doms_new[],
211 struct sched_domain_attr *dattr_new)
212{
213}
214
215static inline void
Ingo Molnara60b9ed2017-02-01 16:36:40 +0100216partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
217 struct sched_domain_attr *dattr_new)
218{
219}
220
221static inline bool cpus_share_cache(int this_cpu, int that_cpu)
222{
223 return true;
224}
225
Vincent Guittot8ec59c02019-06-17 17:00:17 +0200226#endif /* !CONFIG_SMP */
227
Quentin Perret5bd09882018-12-03 09:56:14 +0000228#ifndef arch_scale_cpu_capacity
Valentin Schneiderf4470cd2020-07-31 20:20:14 +0100229/**
230 * arch_scale_cpu_capacity - get the capacity scale factor of a given CPU.
231 * @cpu: the CPU in question.
232 *
233 * Return: the CPU scale factor normalized against SCHED_CAPACITY_SCALE, i.e.
234 *
235 * max_perf(cpu)
236 * ----------------------------- * SCHED_CAPACITY_SCALE
237 * max(max_perf(c) : c \in CPUs)
238 */
Quentin Perret5bd09882018-12-03 09:56:14 +0000239static __always_inline
Vincent Guittot8ec59c02019-06-17 17:00:17 +0200240unsigned long arch_scale_cpu_capacity(int cpu)
Quentin Perret5bd09882018-12-03 09:56:14 +0000241{
242 return SCHED_CAPACITY_SCALE;
243}
244#endif
245
Thara Gopinath36a0df82020-02-21 19:52:06 -0500246#ifndef arch_scale_thermal_pressure
247static __always_inline
248unsigned long arch_scale_thermal_pressure(int cpu)
249{
250 return 0;
251}
252#endif
253
Valentin Schneider25980c72020-07-12 17:59:15 +0100254#ifndef arch_set_thermal_pressure
255static __always_inline
256void arch_set_thermal_pressure(const struct cpumask *cpus,
257 unsigned long th_pressure)
258{ }
259#endif
260
Ingo Molnaree6a3d12017-02-06 10:01:09 +0100261static inline int task_node(const struct task_struct *p)
262{
263 return cpu_to_node(task_cpu(p));
264}
265
Ingo Molnar105ab3d2017-02-01 16:36:40 +0100266#endif /* _LINUX_SCHED_TOPOLOGY_H */