blob: f36ed96f3197500568c08ee83abda91ecde26e3e [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Ingo Molnarf2cb1362017-02-01 13:10:18 +01002/*
3 * Scheduler topology setup/handling methods
4 */
Ingo Molnarf2cb1362017-02-01 13:10:18 +01005#include "sched.h"
6
7DEFINE_MUTEX(sched_domains_mutex);
8
9/* Protected by sched_domains_mutex: */
zhong jiangace80312018-08-03 20:37:32 +080010static cpumask_var_t sched_domains_tmpmask;
11static cpumask_var_t sched_domains_tmpmask2;
Ingo Molnarf2cb1362017-02-01 13:10:18 +010012
13#ifdef CONFIG_SCHED_DEBUG
14
Ingo Molnarf2cb1362017-02-01 13:10:18 +010015static int __init sched_debug_setup(char *str)
16{
Peter Zijlstra9469eb02017-09-07 17:03:53 +020017 sched_debug_enabled = true;
Ingo Molnarf2cb1362017-02-01 13:10:18 +010018
19 return 0;
20}
21early_param("sched_debug", sched_debug_setup);
22
23static inline bool sched_debug(void)
24{
25 return sched_debug_enabled;
26}
27
28static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
29 struct cpumask *groupmask)
30{
31 struct sched_group *group = sd->groups;
Valentin Schneider65c5e252020-08-17 12:29:51 +010032 unsigned long flags = sd->flags;
33 unsigned int idx;
Ingo Molnarf2cb1362017-02-01 13:10:18 +010034
35 cpumask_clear(groupmask);
36
Peter Zijlstra005f8742017-04-26 17:35:35 +020037 printk(KERN_DEBUG "%*s domain-%d: ", level, "", level);
Peter Zijlstra005f8742017-04-26 17:35:35 +020038 printk(KERN_CONT "span=%*pbl level=%s\n",
Ingo Molnarf2cb1362017-02-01 13:10:18 +010039 cpumask_pr_args(sched_domain_span(sd)), sd->name);
40
41 if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) {
Ingo Molnar97fb7a02018-03-03 14:01:12 +010042 printk(KERN_ERR "ERROR: domain->span does not contain CPU%d\n", cpu);
Ingo Molnarf2cb1362017-02-01 13:10:18 +010043 }
Yi Wang6cd0c582018-07-23 12:19:07 +080044 if (group && !cpumask_test_cpu(cpu, sched_group_span(group))) {
Ingo Molnar97fb7a02018-03-03 14:01:12 +010045 printk(KERN_ERR "ERROR: domain->groups does not contain CPU%d\n", cpu);
Ingo Molnarf2cb1362017-02-01 13:10:18 +010046 }
47
Valentin Schneider65c5e252020-08-17 12:29:51 +010048 for_each_set_bit(idx, &flags, __SD_FLAG_CNT) {
49 unsigned int flag = BIT(idx);
50 unsigned int meta_flags = sd_flag_debug[idx].meta_flags;
51
52 if ((meta_flags & SDF_SHARED_CHILD) && sd->child &&
53 !(sd->child->flags & flag))
54 printk(KERN_ERR "ERROR: flag %s set here but not in child\n",
55 sd_flag_debug[idx].name);
56
57 if ((meta_flags & SDF_SHARED_PARENT) && sd->parent &&
58 !(sd->parent->flags & flag))
59 printk(KERN_ERR "ERROR: flag %s set here but not in parent\n",
60 sd_flag_debug[idx].name);
61 }
62
Ingo Molnarf2cb1362017-02-01 13:10:18 +010063 printk(KERN_DEBUG "%*s groups:", level + 1, "");
64 do {
65 if (!group) {
66 printk("\n");
67 printk(KERN_ERR "ERROR: group is NULL\n");
68 break;
69 }
70
Peter Zijlstraae4df9d2017-05-01 11:03:12 +020071 if (!cpumask_weight(sched_group_span(group))) {
Ingo Molnarf2cb1362017-02-01 13:10:18 +010072 printk(KERN_CONT "\n");
73 printk(KERN_ERR "ERROR: empty group\n");
74 break;
75 }
76
77 if (!(sd->flags & SD_OVERLAP) &&
Peter Zijlstraae4df9d2017-05-01 11:03:12 +020078 cpumask_intersects(groupmask, sched_group_span(group))) {
Ingo Molnarf2cb1362017-02-01 13:10:18 +010079 printk(KERN_CONT "\n");
80 printk(KERN_ERR "ERROR: repeated CPUs\n");
81 break;
82 }
83
Peter Zijlstraae4df9d2017-05-01 11:03:12 +020084 cpumask_or(groupmask, groupmask, sched_group_span(group));
Ingo Molnarf2cb1362017-02-01 13:10:18 +010085
Peter Zijlstra005f8742017-04-26 17:35:35 +020086 printk(KERN_CONT " %d:{ span=%*pbl",
87 group->sgc->id,
Peter Zijlstraae4df9d2017-05-01 11:03:12 +020088 cpumask_pr_args(sched_group_span(group)));
Peter Zijlstrab0151c22017-04-14 17:29:16 +020089
Peter Zijlstraaf218122017-05-01 08:51:05 +020090 if ((sd->flags & SD_OVERLAP) &&
Peter Zijlstraae4df9d2017-05-01 11:03:12 +020091 !cpumask_equal(group_balance_mask(group), sched_group_span(group))) {
Peter Zijlstra005f8742017-04-26 17:35:35 +020092 printk(KERN_CONT " mask=%*pbl",
Peter Zijlstrae5c14b12017-05-01 10:47:02 +020093 cpumask_pr_args(group_balance_mask(group)));
Peter Zijlstrab0151c22017-04-14 17:29:16 +020094 }
95
Peter Zijlstra005f8742017-04-26 17:35:35 +020096 if (group->sgc->capacity != SCHED_CAPACITY_SCALE)
97 printk(KERN_CONT " cap=%lu", group->sgc->capacity);
Ingo Molnarf2cb1362017-02-01 13:10:18 +010098
Peter Zijlstraa420b062017-04-14 18:20:48 +020099 if (group == sd->groups && sd->child &&
100 !cpumask_equal(sched_domain_span(sd->child),
Peter Zijlstraae4df9d2017-05-01 11:03:12 +0200101 sched_group_span(group))) {
Peter Zijlstraa420b062017-04-14 18:20:48 +0200102 printk(KERN_ERR "ERROR: domain->groups does not match domain->child\n");
103 }
104
Peter Zijlstra005f8742017-04-26 17:35:35 +0200105 printk(KERN_CONT " }");
106
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100107 group = group->next;
Peter Zijlstrab0151c22017-04-14 17:29:16 +0200108
109 if (group != sd->groups)
110 printk(KERN_CONT ",");
111
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100112 } while (group != sd->groups);
113 printk(KERN_CONT "\n");
114
115 if (!cpumask_equal(sched_domain_span(sd), groupmask))
116 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
117
118 if (sd->parent &&
119 !cpumask_subset(groupmask, sched_domain_span(sd->parent)))
Ingo Molnar97fb7a02018-03-03 14:01:12 +0100120 printk(KERN_ERR "ERROR: parent span is not a superset of domain->span\n");
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100121 return 0;
122}
123
124static void sched_domain_debug(struct sched_domain *sd, int cpu)
125{
126 int level = 0;
127
128 if (!sched_debug_enabled)
129 return;
130
131 if (!sd) {
132 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
133 return;
134 }
135
Peter Zijlstra005f8742017-04-26 17:35:35 +0200136 printk(KERN_DEBUG "CPU%d attaching sched-domain(s):\n", cpu);
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100137
138 for (;;) {
139 if (sched_domain_debug_one(sd, cpu, level, sched_domains_tmpmask))
140 break;
141 level++;
142 sd = sd->parent;
143 if (!sd)
144 break;
145 }
146}
147#else /* !CONFIG_SCHED_DEBUG */
148
149# define sched_debug_enabled 0
150# define sched_domain_debug(sd, cpu) do { } while (0)
151static inline bool sched_debug(void)
152{
153 return false;
154}
155#endif /* CONFIG_SCHED_DEBUG */
156
157static int sd_degenerate(struct sched_domain *sd)
158{
159 if (cpumask_weight(sched_domain_span(sd)) == 1)
160 return 1;
161
162 /* Following flags need at least 2 groups */
Valentin Schneider6f349812020-08-17 12:29:54 +0100163 if ((sd->flags & SD_DEGENERATE_GROUPS_MASK) &&
164 (sd->groups != sd->groups->next))
165 return 0;
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100166
167 /* Following flags don't use groups */
168 if (sd->flags & (SD_WAKE_AFFINE))
169 return 0;
170
171 return 1;
172}
173
174static int
175sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
176{
177 unsigned long cflags = sd->flags, pflags = parent->flags;
178
179 if (sd_degenerate(parent))
180 return 1;
181
182 if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent)))
183 return 0;
184
185 /* Flags needing groups don't count if only 1 group in parent */
Valentin Schneiderab65afb2020-08-17 12:29:55 +0100186 if (parent->groups == parent->groups->next)
Valentin Schneider6f349812020-08-17 12:29:54 +0100187 pflags &= ~(SD_DEGENERATE_GROUPS_MASK | SD_PREFER_SIBLING);
Valentin Schneiderab65afb2020-08-17 12:29:55 +0100188
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100189 if (~cflags & pflags)
190 return 0;
191
192 return 1;
193}
194
Quentin Perret531b5c92018-12-03 09:56:21 +0000195#if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
Peter Zijlstraf8a696f2018-12-05 11:23:56 +0100196DEFINE_STATIC_KEY_FALSE(sched_energy_present);
Quentin Perret8d5d0cf2018-12-03 09:56:23 +0000197unsigned int sysctl_sched_energy_aware = 1;
Quentin Perret531b5c92018-12-03 09:56:21 +0000198DEFINE_MUTEX(sched_energy_mutex);
199bool sched_energy_update;
200
Quentin Perret8d5d0cf2018-12-03 09:56:23 +0000201#ifdef CONFIG_PROC_SYSCTL
202int sched_energy_aware_handler(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +0200203 void *buffer, size_t *lenp, loff_t *ppos)
Quentin Perret8d5d0cf2018-12-03 09:56:23 +0000204{
205 int ret, state;
206
207 if (write && !capable(CAP_SYS_ADMIN))
208 return -EPERM;
209
210 ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
211 if (!ret && write) {
212 state = static_branch_unlikely(&sched_energy_present);
213 if (state != sysctl_sched_energy_aware) {
214 mutex_lock(&sched_energy_mutex);
215 sched_energy_update = 1;
216 rebuild_sched_domains();
217 sched_energy_update = 0;
218 mutex_unlock(&sched_energy_mutex);
219 }
220 }
221
222 return ret;
223}
224#endif
225
Quentin Perret6aa140f2018-12-03 09:56:18 +0000226static void free_pd(struct perf_domain *pd)
227{
228 struct perf_domain *tmp;
229
230 while (pd) {
231 tmp = pd->next;
232 kfree(pd);
233 pd = tmp;
234 }
235}
236
237static struct perf_domain *find_pd(struct perf_domain *pd, int cpu)
238{
239 while (pd) {
240 if (cpumask_test_cpu(cpu, perf_domain_span(pd)))
241 return pd;
242 pd = pd->next;
243 }
244
245 return NULL;
246}
247
248static struct perf_domain *pd_init(int cpu)
249{
250 struct em_perf_domain *obj = em_cpu_get(cpu);
251 struct perf_domain *pd;
252
253 if (!obj) {
254 if (sched_debug())
255 pr_info("%s: no EM found for CPU%d\n", __func__, cpu);
256 return NULL;
257 }
258
259 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
260 if (!pd)
261 return NULL;
262 pd->em_pd = obj;
263
264 return pd;
265}
266
267static void perf_domain_debug(const struct cpumask *cpu_map,
268 struct perf_domain *pd)
269{
270 if (!sched_debug() || !pd)
271 return;
272
273 printk(KERN_DEBUG "root_domain %*pbl:", cpumask_pr_args(cpu_map));
274
275 while (pd) {
Lukasz Luba521b5122020-05-27 10:58:47 +0100276 printk(KERN_CONT " pd%d:{ cpus=%*pbl nr_pstate=%d }",
Quentin Perret6aa140f2018-12-03 09:56:18 +0000277 cpumask_first(perf_domain_span(pd)),
278 cpumask_pr_args(perf_domain_span(pd)),
Lukasz Luba521b5122020-05-27 10:58:47 +0100279 em_pd_nr_perf_states(pd->em_pd));
Quentin Perret6aa140f2018-12-03 09:56:18 +0000280 pd = pd->next;
281 }
282
283 printk(KERN_CONT "\n");
284}
285
286static void destroy_perf_domain_rcu(struct rcu_head *rp)
287{
288 struct perf_domain *pd;
289
290 pd = container_of(rp, struct perf_domain, rcu);
291 free_pd(pd);
292}
293
Quentin Perret1f74de82018-12-03 09:56:22 +0000294static void sched_energy_set(bool has_eas)
295{
296 if (!has_eas && static_branch_unlikely(&sched_energy_present)) {
297 if (sched_debug())
298 pr_info("%s: stopping EAS\n", __func__);
299 static_branch_disable_cpuslocked(&sched_energy_present);
300 } else if (has_eas && !static_branch_unlikely(&sched_energy_present)) {
301 if (sched_debug())
302 pr_info("%s: starting EAS\n", __func__);
303 static_branch_enable_cpuslocked(&sched_energy_present);
304 }
305}
306
Quentin Perretb68a4c02018-12-03 09:56:20 +0000307/*
308 * EAS can be used on a root domain if it meets all the following conditions:
309 * 1. an Energy Model (EM) is available;
310 * 2. the SD_ASYM_CPUCAPACITY flag is set in the sched_domain hierarchy.
Valentin Schneider38502ab2020-02-27 19:14:32 +0000311 * 3. no SMT is detected.
312 * 4. the EM complexity is low enough to keep scheduling overheads low;
313 * 5. schedutil is driving the frequency of all CPUs of the rd;
Quentin Perretb68a4c02018-12-03 09:56:20 +0000314 *
315 * The complexity of the Energy Model is defined as:
316 *
Lukasz Luba521b5122020-05-27 10:58:47 +0100317 * C = nr_pd * (nr_cpus + nr_ps)
Quentin Perretb68a4c02018-12-03 09:56:20 +0000318 *
319 * with parameters defined as:
320 * - nr_pd: the number of performance domains
321 * - nr_cpus: the number of CPUs
Lukasz Luba521b5122020-05-27 10:58:47 +0100322 * - nr_ps: the sum of the number of performance states of all performance
Quentin Perretb68a4c02018-12-03 09:56:20 +0000323 * domains (for example, on a system with 2 performance domains,
Lukasz Luba521b5122020-05-27 10:58:47 +0100324 * with 10 performance states each, nr_ps = 2 * 10 = 20).
Quentin Perretb68a4c02018-12-03 09:56:20 +0000325 *
326 * It is generally not a good idea to use such a model in the wake-up path on
327 * very complex platforms because of the associated scheduling overheads. The
328 * arbitrary constraint below prevents that. It makes EAS usable up to 16 CPUs
Lukasz Luba521b5122020-05-27 10:58:47 +0100329 * with per-CPU DVFS and less than 8 performance states each, for example.
Quentin Perretb68a4c02018-12-03 09:56:20 +0000330 */
331#define EM_MAX_COMPLEXITY 2048
332
Quentin Perret531b5c92018-12-03 09:56:21 +0000333extern struct cpufreq_governor schedutil_gov;
Quentin Perret1f74de82018-12-03 09:56:22 +0000334static bool build_perf_domains(const struct cpumask *cpu_map)
Quentin Perret6aa140f2018-12-03 09:56:18 +0000335{
Lukasz Luba521b5122020-05-27 10:58:47 +0100336 int i, nr_pd = 0, nr_ps = 0, nr_cpus = cpumask_weight(cpu_map);
Quentin Perret6aa140f2018-12-03 09:56:18 +0000337 struct perf_domain *pd = NULL, *tmp;
338 int cpu = cpumask_first(cpu_map);
339 struct root_domain *rd = cpu_rq(cpu)->rd;
Quentin Perret531b5c92018-12-03 09:56:21 +0000340 struct cpufreq_policy *policy;
341 struct cpufreq_governor *gov;
Quentin Perretb68a4c02018-12-03 09:56:20 +0000342
Quentin Perret8d5d0cf2018-12-03 09:56:23 +0000343 if (!sysctl_sched_energy_aware)
344 goto free;
345
Quentin Perretb68a4c02018-12-03 09:56:20 +0000346 /* EAS is enabled for asymmetric CPU capacity topologies. */
347 if (!per_cpu(sd_asym_cpucapacity, cpu)) {
348 if (sched_debug()) {
349 pr_info("rd %*pbl: CPUs do not have asymmetric capacities\n",
350 cpumask_pr_args(cpu_map));
351 }
352 goto free;
353 }
Quentin Perret6aa140f2018-12-03 09:56:18 +0000354
Valentin Schneider38502ab2020-02-27 19:14:32 +0000355 /* EAS definitely does *not* handle SMT */
356 if (sched_smt_active()) {
357 pr_warn("rd %*pbl: Disabling EAS, SMT is not supported\n",
358 cpumask_pr_args(cpu_map));
359 goto free;
360 }
361
Quentin Perret6aa140f2018-12-03 09:56:18 +0000362 for_each_cpu(i, cpu_map) {
363 /* Skip already covered CPUs. */
364 if (find_pd(pd, i))
365 continue;
366
Quentin Perret531b5c92018-12-03 09:56:21 +0000367 /* Do not attempt EAS if schedutil is not being used. */
368 policy = cpufreq_cpu_get(i);
369 if (!policy)
370 goto free;
371 gov = policy->governor;
372 cpufreq_cpu_put(policy);
373 if (gov != &schedutil_gov) {
374 if (rd->pd)
375 pr_warn("rd %*pbl: Disabling EAS, schedutil is mandatory\n",
376 cpumask_pr_args(cpu_map));
377 goto free;
378 }
379
Quentin Perret6aa140f2018-12-03 09:56:18 +0000380 /* Create the new pd and add it to the local list. */
381 tmp = pd_init(i);
382 if (!tmp)
383 goto free;
384 tmp->next = pd;
385 pd = tmp;
Quentin Perretb68a4c02018-12-03 09:56:20 +0000386
387 /*
Lukasz Luba521b5122020-05-27 10:58:47 +0100388 * Count performance domains and performance states for the
Quentin Perretb68a4c02018-12-03 09:56:20 +0000389 * complexity check.
390 */
391 nr_pd++;
Lukasz Luba521b5122020-05-27 10:58:47 +0100392 nr_ps += em_pd_nr_perf_states(pd->em_pd);
Quentin Perretb68a4c02018-12-03 09:56:20 +0000393 }
394
395 /* Bail out if the Energy Model complexity is too high. */
Lukasz Luba521b5122020-05-27 10:58:47 +0100396 if (nr_pd * (nr_ps + nr_cpus) > EM_MAX_COMPLEXITY) {
Quentin Perretb68a4c02018-12-03 09:56:20 +0000397 WARN(1, "rd %*pbl: Failed to start EAS, EM complexity is too high\n",
398 cpumask_pr_args(cpu_map));
399 goto free;
Quentin Perret6aa140f2018-12-03 09:56:18 +0000400 }
401
402 perf_domain_debug(cpu_map, pd);
403
404 /* Attach the new list of performance domains to the root domain. */
405 tmp = rd->pd;
406 rcu_assign_pointer(rd->pd, pd);
407 if (tmp)
408 call_rcu(&tmp->rcu, destroy_perf_domain_rcu);
409
Quentin Perret1f74de82018-12-03 09:56:22 +0000410 return !!pd;
Quentin Perret6aa140f2018-12-03 09:56:18 +0000411
412free:
413 free_pd(pd);
414 tmp = rd->pd;
415 rcu_assign_pointer(rd->pd, NULL);
416 if (tmp)
417 call_rcu(&tmp->rcu, destroy_perf_domain_rcu);
Quentin Perret1f74de82018-12-03 09:56:22 +0000418
419 return false;
Quentin Perret6aa140f2018-12-03 09:56:18 +0000420}
421#else
422static void free_pd(struct perf_domain *pd) { }
Quentin Perret531b5c92018-12-03 09:56:21 +0000423#endif /* CONFIG_ENERGY_MODEL && CONFIG_CPU_FREQ_GOV_SCHEDUTIL*/
Quentin Perret6aa140f2018-12-03 09:56:18 +0000424
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100425static void free_rootdomain(struct rcu_head *rcu)
426{
427 struct root_domain *rd = container_of(rcu, struct root_domain, rcu);
428
429 cpupri_cleanup(&rd->cpupri);
430 cpudl_cleanup(&rd->cpudl);
431 free_cpumask_var(rd->dlo_mask);
432 free_cpumask_var(rd->rto_mask);
433 free_cpumask_var(rd->online);
434 free_cpumask_var(rd->span);
Quentin Perret6aa140f2018-12-03 09:56:18 +0000435 free_pd(rd->pd);
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100436 kfree(rd);
437}
438
439void rq_attach_root(struct rq *rq, struct root_domain *rd)
440{
441 struct root_domain *old_rd = NULL;
442 unsigned long flags;
443
444 raw_spin_lock_irqsave(&rq->lock, flags);
445
446 if (rq->rd) {
447 old_rd = rq->rd;
448
449 if (cpumask_test_cpu(rq->cpu, old_rd->online))
450 set_rq_offline(rq);
451
452 cpumask_clear_cpu(rq->cpu, old_rd->span);
453
454 /*
455 * If we dont want to free the old_rd yet then
456 * set old_rd to NULL to skip the freeing later
457 * in this function:
458 */
459 if (!atomic_dec_and_test(&old_rd->refcount))
460 old_rd = NULL;
461 }
462
463 atomic_inc(&rd->refcount);
464 rq->rd = rd;
465
466 cpumask_set_cpu(rq->cpu, rd->span);
467 if (cpumask_test_cpu(rq->cpu, cpu_active_mask))
468 set_rq_online(rq);
469
470 raw_spin_unlock_irqrestore(&rq->lock, flags);
471
472 if (old_rd)
Paul E. McKenney337e9b02018-11-06 19:10:53 -0800473 call_rcu(&old_rd->rcu, free_rootdomain);
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100474}
475
Steven Rostedt (VMware)364f5662018-01-23 20:45:38 -0500476void sched_get_rd(struct root_domain *rd)
477{
478 atomic_inc(&rd->refcount);
479}
480
481void sched_put_rd(struct root_domain *rd)
482{
483 if (!atomic_dec_and_test(&rd->refcount))
484 return;
485
Paul E. McKenney337e9b02018-11-06 19:10:53 -0800486 call_rcu(&rd->rcu, free_rootdomain);
Steven Rostedt (VMware)364f5662018-01-23 20:45:38 -0500487}
488
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100489static int init_rootdomain(struct root_domain *rd)
490{
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100491 if (!zalloc_cpumask_var(&rd->span, GFP_KERNEL))
492 goto out;
493 if (!zalloc_cpumask_var(&rd->online, GFP_KERNEL))
494 goto free_span;
495 if (!zalloc_cpumask_var(&rd->dlo_mask, GFP_KERNEL))
496 goto free_online;
497 if (!zalloc_cpumask_var(&rd->rto_mask, GFP_KERNEL))
498 goto free_dlo_mask;
499
Steven Rostedt (Red Hat)4bdced52017-10-06 14:05:04 -0400500#ifdef HAVE_RT_PUSH_IPI
501 rd->rto_cpu = -1;
502 raw_spin_lock_init(&rd->rto_lock);
503 init_irq_work(&rd->rto_push_work, rto_push_irq_work_func);
504#endif
505
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100506 init_dl_bw(&rd->dl_bw);
507 if (cpudl_init(&rd->cpudl) != 0)
508 goto free_rto_mask;
509
510 if (cpupri_init(&rd->cpupri) != 0)
511 goto free_cpudl;
512 return 0;
513
514free_cpudl:
515 cpudl_cleanup(&rd->cpudl);
516free_rto_mask:
517 free_cpumask_var(rd->rto_mask);
518free_dlo_mask:
519 free_cpumask_var(rd->dlo_mask);
520free_online:
521 free_cpumask_var(rd->online);
522free_span:
523 free_cpumask_var(rd->span);
524out:
525 return -ENOMEM;
526}
527
528/*
529 * By default the system creates a single root-domain with all CPUs as
530 * members (mimicking the global state we have today).
531 */
532struct root_domain def_root_domain;
533
534void init_defrootdomain(void)
535{
536 init_rootdomain(&def_root_domain);
537
538 atomic_set(&def_root_domain.refcount, 1);
539}
540
541static struct root_domain *alloc_rootdomain(void)
542{
543 struct root_domain *rd;
544
Viresh Kumar4d13a062017-04-13 14:45:48 +0530545 rd = kzalloc(sizeof(*rd), GFP_KERNEL);
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100546 if (!rd)
547 return NULL;
548
549 if (init_rootdomain(rd) != 0) {
550 kfree(rd);
551 return NULL;
552 }
553
554 return rd;
555}
556
557static void free_sched_groups(struct sched_group *sg, int free_sgc)
558{
559 struct sched_group *tmp, *first;
560
561 if (!sg)
562 return;
563
564 first = sg;
565 do {
566 tmp = sg->next;
567
568 if (free_sgc && atomic_dec_and_test(&sg->sgc->ref))
569 kfree(sg->sgc);
570
Shu Wang213c5a42017-08-10 15:52:16 +0800571 if (atomic_dec_and_test(&sg->ref))
572 kfree(sg);
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100573 sg = tmp;
574 } while (sg != first);
575}
576
577static void destroy_sched_domain(struct sched_domain *sd)
578{
579 /*
Peter Zijlstraa090c4f2017-08-21 15:42:52 +0200580 * A normal sched domain may have multiple group references, an
581 * overlapping domain, having private groups, only one. Iterate,
582 * dropping group/capacity references, freeing where none remain.
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100583 */
Shu Wang213c5a42017-08-10 15:52:16 +0800584 free_sched_groups(sd->groups, 1);
585
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100586 if (sd->shared && atomic_dec_and_test(&sd->shared->ref))
587 kfree(sd->shared);
588 kfree(sd);
589}
590
591static void destroy_sched_domains_rcu(struct rcu_head *rcu)
592{
593 struct sched_domain *sd = container_of(rcu, struct sched_domain, rcu);
594
595 while (sd) {
596 struct sched_domain *parent = sd->parent;
597 destroy_sched_domain(sd);
598 sd = parent;
599 }
600}
601
602static void destroy_sched_domains(struct sched_domain *sd)
603{
604 if (sd)
605 call_rcu(&sd->rcu, destroy_sched_domains_rcu);
606}
607
608/*
609 * Keep a special pointer to the highest sched_domain that has
610 * SD_SHARE_PKG_RESOURCE set (Last Level Cache Domain) for this
611 * allows us to avoid some pointer chasing select_idle_sibling().
612 *
613 * Also keep a unique ID per domain (we use the first CPU number in
614 * the cpumask of the domain), this allows us to quickly tell if
615 * two CPUs are in the same cache domain, see cpus_share_cache().
616 */
Joel Fernandes (Google)994aeb72019-03-20 20:34:24 -0400617DEFINE_PER_CPU(struct sched_domain __rcu *, sd_llc);
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100618DEFINE_PER_CPU(int, sd_llc_size);
619DEFINE_PER_CPU(int, sd_llc_id);
Joel Fernandes (Google)994aeb72019-03-20 20:34:24 -0400620DEFINE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared);
621DEFINE_PER_CPU(struct sched_domain __rcu *, sd_numa);
622DEFINE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing);
623DEFINE_PER_CPU(struct sched_domain __rcu *, sd_asym_cpucapacity);
Morten Rasmussendf054e82018-07-04 11:17:39 +0100624DEFINE_STATIC_KEY_FALSE(sched_asym_cpucapacity);
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100625
626static void update_top_cache_domain(int cpu)
627{
628 struct sched_domain_shared *sds = NULL;
629 struct sched_domain *sd;
630 int id = cpu;
631 int size = 1;
632
633 sd = highest_flag_domain(cpu, SD_SHARE_PKG_RESOURCES);
634 if (sd) {
635 id = cpumask_first(sched_domain_span(sd));
636 size = cpumask_weight(sched_domain_span(sd));
637 sds = sd->shared;
638 }
639
640 rcu_assign_pointer(per_cpu(sd_llc, cpu), sd);
641 per_cpu(sd_llc_size, cpu) = size;
642 per_cpu(sd_llc_id, cpu) = id;
643 rcu_assign_pointer(per_cpu(sd_llc_shared, cpu), sds);
644
645 sd = lowest_flag_domain(cpu, SD_NUMA);
646 rcu_assign_pointer(per_cpu(sd_numa, cpu), sd);
647
648 sd = highest_flag_domain(cpu, SD_ASYM_PACKING);
Quentin Perret011b27b2018-12-03 09:56:19 +0000649 rcu_assign_pointer(per_cpu(sd_asym_packing, cpu), sd);
650
651 sd = lowest_flag_domain(cpu, SD_ASYM_CPUCAPACITY);
652 rcu_assign_pointer(per_cpu(sd_asym_cpucapacity, cpu), sd);
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100653}
654
655/*
656 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
657 * hold the hotplug lock.
658 */
659static void
660cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
661{
662 struct rq *rq = cpu_rq(cpu);
663 struct sched_domain *tmp;
664
665 /* Remove the sched domains which do not contribute to scheduling. */
666 for (tmp = sd; tmp; ) {
667 struct sched_domain *parent = tmp->parent;
668 if (!parent)
669 break;
670
671 if (sd_parent_degenerate(tmp, parent)) {
672 tmp->parent = parent->parent;
673 if (parent->parent)
674 parent->parent->child = tmp;
675 /*
676 * Transfer SD_PREFER_SIBLING down in case of a
677 * degenerate parent; the spans match for this
678 * so the property transfers.
679 */
680 if (parent->flags & SD_PREFER_SIBLING)
681 tmp->flags |= SD_PREFER_SIBLING;
682 destroy_sched_domain(parent);
683 } else
684 tmp = tmp->parent;
685 }
686
687 if (sd && sd_degenerate(sd)) {
688 tmp = sd;
689 sd = sd->parent;
690 destroy_sched_domain(tmp);
691 if (sd)
692 sd->child = NULL;
693 }
694
695 sched_domain_debug(sd, cpu);
696
697 rq_attach_root(rq, rd);
698 tmp = rq->sd;
699 rcu_assign_pointer(rq->sd, sd);
Peter Zijlstrabbdacdf2017-08-10 17:10:26 +0200700 dirty_sched_domain_sysctl(cpu);
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100701 destroy_sched_domains(tmp);
702
703 update_top_cache_domain(cpu);
704}
705
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100706struct s_data {
Luc Van Oostenryck99687cd2019-01-18 15:49:36 +0100707 struct sched_domain * __percpu *sd;
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100708 struct root_domain *rd;
709};
710
711enum s_alloc {
712 sa_rootdomain,
713 sa_sd,
714 sa_sd_storage,
715 sa_none,
716};
717
718/*
Peter Zijlstra35a566e2017-04-28 10:54:26 +0200719 * Return the canonical balance CPU for this group, this is the first CPU
Peter Zijlstrae5c14b12017-05-01 10:47:02 +0200720 * of this group that's also in the balance mask.
Peter Zijlstra35a566e2017-04-28 10:54:26 +0200721 *
Peter Zijlstrae5c14b12017-05-01 10:47:02 +0200722 * The balance mask are all those CPUs that could actually end up at this
723 * group. See build_balance_mask().
Peter Zijlstra35a566e2017-04-28 10:54:26 +0200724 *
725 * Also see should_we_balance().
726 */
727int group_balance_cpu(struct sched_group *sg)
728{
Peter Zijlstrae5c14b12017-05-01 10:47:02 +0200729 return cpumask_first(group_balance_mask(sg));
Peter Zijlstra35a566e2017-04-28 10:54:26 +0200730}
731
732
733/*
734 * NUMA topology (first read the regular topology blurb below)
735 *
736 * Given a node-distance table, for example:
737 *
738 * node 0 1 2 3
739 * 0: 10 20 30 20
740 * 1: 20 10 20 30
741 * 2: 30 20 10 20
742 * 3: 20 30 20 10
743 *
744 * which represents a 4 node ring topology like:
745 *
746 * 0 ----- 1
747 * | |
748 * | |
749 * | |
750 * 3 ----- 2
751 *
752 * We want to construct domains and groups to represent this. The way we go
753 * about doing this is to build the domains on 'hops'. For each NUMA level we
754 * construct the mask of all nodes reachable in @level hops.
755 *
756 * For the above NUMA topology that gives 3 levels:
757 *
758 * NUMA-2 0-3 0-3 0-3 0-3
759 * groups: {0-1,3},{1-3} {0-2},{0,2-3} {1-3},{0-1,3} {0,2-3},{0-2}
760 *
761 * NUMA-1 0-1,3 0-2 1-3 0,2-3
762 * groups: {0},{1},{3} {0},{1},{2} {1},{2},{3} {0},{2},{3}
763 *
764 * NUMA-0 0 1 2 3
765 *
766 *
767 * As can be seen; things don't nicely line up as with the regular topology.
768 * When we iterate a domain in child domain chunks some nodes can be
769 * represented multiple times -- hence the "overlap" naming for this part of
770 * the topology.
771 *
772 * In order to minimize this overlap, we only build enough groups to cover the
773 * domain. For instance Node-0 NUMA-2 would only get groups: 0-1,3 and 1-3.
774 *
775 * Because:
776 *
777 * - the first group of each domain is its child domain; this
778 * gets us the first 0-1,3
779 * - the only uncovered node is 2, who's child domain is 1-3.
780 *
781 * However, because of the overlap, computing a unique CPU for each group is
782 * more complicated. Consider for instance the groups of NODE-1 NUMA-2, both
783 * groups include the CPUs of Node-0, while those CPUs would not in fact ever
784 * end up at those groups (they would end up in group: 0-1,3).
785 *
Peter Zijlstrae5c14b12017-05-01 10:47:02 +0200786 * To correct this we have to introduce the group balance mask. This mask
Peter Zijlstra35a566e2017-04-28 10:54:26 +0200787 * will contain those CPUs in the group that can reach this group given the
788 * (child) domain tree.
789 *
790 * With this we can once again compute balance_cpu and sched_group_capacity
791 * relations.
792 *
793 * XXX include words on how balance_cpu is unique and therefore can be
794 * used for sched_group_capacity links.
795 *
796 *
797 * Another 'interesting' topology is:
798 *
799 * node 0 1 2 3
800 * 0: 10 20 20 30
801 * 1: 20 10 20 20
802 * 2: 20 20 10 20
803 * 3: 30 20 20 10
804 *
805 * Which looks a little like:
806 *
807 * 0 ----- 1
808 * | / |
809 * | / |
810 * | / |
811 * 2 ----- 3
812 *
813 * This topology is asymmetric, nodes 1,2 are fully connected, but nodes 0,3
814 * are not.
815 *
816 * This leads to a few particularly weird cases where the sched_domain's are
Ingo Molnar97fb7a02018-03-03 14:01:12 +0100817 * not of the same number for each CPU. Consider:
Peter Zijlstra35a566e2017-04-28 10:54:26 +0200818 *
819 * NUMA-2 0-3 0-3
820 * groups: {0-2},{1-3} {1-3},{0-2}
821 *
822 * NUMA-1 0-2 0-3 0-3 1-3
823 *
824 * NUMA-0 0 1 2 3
825 *
826 */
827
828
829/*
Peter Zijlstrae5c14b12017-05-01 10:47:02 +0200830 * Build the balance mask; it contains only those CPUs that can arrive at this
831 * group and should be considered to continue balancing.
Peter Zijlstra35a566e2017-04-28 10:54:26 +0200832 *
833 * We do this during the group creation pass, therefore the group information
834 * isn't complete yet, however since each group represents a (child) domain we
835 * can fully construct this using the sched_domain bits (which are already
836 * complete).
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100837 */
Peter Zijlstra16763302017-04-25 14:31:11 +0200838static void
Peter Zijlstrae5c14b12017-05-01 10:47:02 +0200839build_balance_mask(struct sched_domain *sd, struct sched_group *sg, struct cpumask *mask)
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100840{
Peter Zijlstraae4df9d2017-05-01 11:03:12 +0200841 const struct cpumask *sg_span = sched_group_span(sg);
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100842 struct sd_data *sdd = sd->private;
843 struct sched_domain *sibling;
844 int i;
845
Peter Zijlstra16763302017-04-25 14:31:11 +0200846 cpumask_clear(mask);
847
Lauro Ramos Venanciof32d7822017-04-20 16:51:40 -0300848 for_each_cpu(i, sg_span) {
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100849 sibling = *per_cpu_ptr(sdd->sd, i);
Peter Zijlstra73bb0592017-04-25 14:00:49 +0200850
851 /*
852 * Can happen in the asymmetric case, where these siblings are
853 * unused. The mask will not be empty because those CPUs that
854 * do have the top domain _should_ span the domain.
855 */
856 if (!sibling->child)
857 continue;
858
859 /* If we would not end up here, we can't continue from here */
860 if (!cpumask_equal(sg_span, sched_domain_span(sibling->child)))
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100861 continue;
862
Peter Zijlstra16763302017-04-25 14:31:11 +0200863 cpumask_set_cpu(i, mask);
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100864 }
Peter Zijlstra73bb0592017-04-25 14:00:49 +0200865
866 /* We must not have empty masks here */
Peter Zijlstra16763302017-04-25 14:31:11 +0200867 WARN_ON_ONCE(cpumask_empty(mask));
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100868}
869
870/*
Peter Zijlstra35a566e2017-04-28 10:54:26 +0200871 * XXX: This creates per-node group entries; since the load-balancer will
872 * immediately access remote memory to construct this group's load-balance
873 * statistics having the groups node local is of dubious benefit.
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100874 */
Lauro Ramos Venancio8c033462017-04-13 10:56:07 -0300875static struct sched_group *
876build_group_from_child_sched_domain(struct sched_domain *sd, int cpu)
877{
878 struct sched_group *sg;
879 struct cpumask *sg_span;
880
881 sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
882 GFP_KERNEL, cpu_to_node(cpu));
883
884 if (!sg)
885 return NULL;
886
Peter Zijlstraae4df9d2017-05-01 11:03:12 +0200887 sg_span = sched_group_span(sg);
Lauro Ramos Venancio8c033462017-04-13 10:56:07 -0300888 if (sd->child)
889 cpumask_copy(sg_span, sched_domain_span(sd->child));
890 else
891 cpumask_copy(sg_span, sched_domain_span(sd));
892
Shu Wang213c5a42017-08-10 15:52:16 +0800893 atomic_inc(&sg->ref);
Lauro Ramos Venancio8c033462017-04-13 10:56:07 -0300894 return sg;
895}
896
897static void init_overlap_sched_group(struct sched_domain *sd,
Peter Zijlstra16763302017-04-25 14:31:11 +0200898 struct sched_group *sg)
Lauro Ramos Venancio8c033462017-04-13 10:56:07 -0300899{
Peter Zijlstra16763302017-04-25 14:31:11 +0200900 struct cpumask *mask = sched_domains_tmpmask2;
Lauro Ramos Venancio8c033462017-04-13 10:56:07 -0300901 struct sd_data *sdd = sd->private;
902 struct cpumask *sg_span;
Peter Zijlstra16763302017-04-25 14:31:11 +0200903 int cpu;
904
Peter Zijlstrae5c14b12017-05-01 10:47:02 +0200905 build_balance_mask(sd, sg, mask);
Peter Zijlstraae4df9d2017-05-01 11:03:12 +0200906 cpu = cpumask_first_and(sched_group_span(sg), mask);
Lauro Ramos Venancio8c033462017-04-13 10:56:07 -0300907
908 sg->sgc = *per_cpu_ptr(sdd->sgc, cpu);
909 if (atomic_inc_return(&sg->sgc->ref) == 1)
Peter Zijlstrae5c14b12017-05-01 10:47:02 +0200910 cpumask_copy(group_balance_mask(sg), mask);
Peter Zijlstra35a566e2017-04-28 10:54:26 +0200911 else
Peter Zijlstrae5c14b12017-05-01 10:47:02 +0200912 WARN_ON_ONCE(!cpumask_equal(group_balance_mask(sg), mask));
Lauro Ramos Venancio8c033462017-04-13 10:56:07 -0300913
914 /*
915 * Initialize sgc->capacity such that even if we mess up the
916 * domains and no possible iteration will get us here, we won't
917 * die on a /0 trap.
918 */
Peter Zijlstraae4df9d2017-05-01 11:03:12 +0200919 sg_span = sched_group_span(sg);
Lauro Ramos Venancio8c033462017-04-13 10:56:07 -0300920 sg->sgc->capacity = SCHED_CAPACITY_SCALE * cpumask_weight(sg_span);
921 sg->sgc->min_capacity = SCHED_CAPACITY_SCALE;
Morten Rasmussene3d6d0c2018-07-04 11:17:41 +0100922 sg->sgc->max_capacity = SCHED_CAPACITY_SCALE;
Lauro Ramos Venancio8c033462017-04-13 10:56:07 -0300923}
924
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100925static int
926build_overlap_sched_groups(struct sched_domain *sd, int cpu)
927{
Peter Zijlstra91eaed02017-04-14 17:32:07 +0200928 struct sched_group *first = NULL, *last = NULL, *sg;
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100929 const struct cpumask *span = sched_domain_span(sd);
930 struct cpumask *covered = sched_domains_tmpmask;
931 struct sd_data *sdd = sd->private;
932 struct sched_domain *sibling;
933 int i;
934
935 cpumask_clear(covered);
936
Peter Zijlstra0372dd22017-04-14 17:24:02 +0200937 for_each_cpu_wrap(i, span, cpu) {
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100938 struct cpumask *sg_span;
939
940 if (cpumask_test_cpu(i, covered))
941 continue;
942
943 sibling = *per_cpu_ptr(sdd->sd, i);
944
Lauro Ramos Venancioc20e1ea2017-04-20 16:51:42 -0300945 /*
946 * Asymmetric node setups can result in situations where the
947 * domain tree is of unequal depth, make sure to skip domains
948 * that already cover the entire range.
949 *
950 * In that case build_sched_domains() will have terminated the
951 * iteration early and our sibling sd spans will be empty.
952 * Domains should always include the CPU they're built on, so
953 * check that.
954 */
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100955 if (!cpumask_test_cpu(i, sched_domain_span(sibling)))
956 continue;
957
Lauro Ramos Venancio8c033462017-04-13 10:56:07 -0300958 sg = build_group_from_child_sched_domain(sibling, cpu);
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100959 if (!sg)
960 goto fail;
961
Peter Zijlstraae4df9d2017-05-01 11:03:12 +0200962 sg_span = sched_group_span(sg);
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100963 cpumask_or(covered, covered, sg_span);
964
Peter Zijlstra16763302017-04-25 14:31:11 +0200965 init_overlap_sched_group(sd, sg);
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100966
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100967 if (!first)
968 first = sg;
969 if (last)
970 last->next = sg;
971 last = sg;
972 last->next = first;
973 }
Peter Zijlstra91eaed02017-04-14 17:32:07 +0200974 sd->groups = first;
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100975
976 return 0;
977
978fail:
979 free_sched_groups(first, 0);
980
981 return -ENOMEM;
982}
983
Peter Zijlstra35a566e2017-04-28 10:54:26 +0200984
985/*
986 * Package topology (also see the load-balance blurb in fair.c)
987 *
988 * The scheduler builds a tree structure to represent a number of important
989 * topology features. By default (default_topology[]) these include:
990 *
991 * - Simultaneous multithreading (SMT)
992 * - Multi-Core Cache (MC)
993 * - Package (DIE)
994 *
995 * Where the last one more or less denotes everything up to a NUMA node.
996 *
997 * The tree consists of 3 primary data structures:
998 *
999 * sched_domain -> sched_group -> sched_group_capacity
1000 * ^ ^ ^ ^
1001 * `-' `-'
1002 *
Ingo Molnar97fb7a02018-03-03 14:01:12 +01001003 * The sched_domains are per-CPU and have a two way link (parent & child) and
Peter Zijlstra35a566e2017-04-28 10:54:26 +02001004 * denote the ever growing mask of CPUs belonging to that level of topology.
1005 *
1006 * Each sched_domain has a circular (double) linked list of sched_group's, each
1007 * denoting the domains of the level below (or individual CPUs in case of the
1008 * first domain level). The sched_group linked by a sched_domain includes the
1009 * CPU of that sched_domain [*].
1010 *
1011 * Take for instance a 2 threaded, 2 core, 2 cache cluster part:
1012 *
1013 * CPU 0 1 2 3 4 5 6 7
1014 *
1015 * DIE [ ]
1016 * MC [ ] [ ]
1017 * SMT [ ] [ ] [ ] [ ]
1018 *
1019 * - or -
1020 *
1021 * DIE 0-7 0-7 0-7 0-7 0-7 0-7 0-7 0-7
1022 * MC 0-3 0-3 0-3 0-3 4-7 4-7 4-7 4-7
1023 * SMT 0-1 0-1 2-3 2-3 4-5 4-5 6-7 6-7
1024 *
1025 * CPU 0 1 2 3 4 5 6 7
1026 *
1027 * One way to think about it is: sched_domain moves you up and down among these
1028 * topology levels, while sched_group moves you sideways through it, at child
1029 * domain granularity.
1030 *
1031 * sched_group_capacity ensures each unique sched_group has shared storage.
1032 *
1033 * There are two related construction problems, both require a CPU that
1034 * uniquely identify each group (for a given domain):
1035 *
1036 * - The first is the balance_cpu (see should_we_balance() and the
1037 * load-balance blub in fair.c); for each group we only want 1 CPU to
1038 * continue balancing at a higher domain.
1039 *
1040 * - The second is the sched_group_capacity; we want all identical groups
1041 * to share a single sched_group_capacity.
1042 *
1043 * Since these topologies are exclusive by construction. That is, its
1044 * impossible for an SMT thread to belong to multiple cores, and cores to
1045 * be part of multiple caches. There is a very clear and unique location
1046 * for each CPU in the hierarchy.
1047 *
1048 * Therefore computing a unique CPU for each group is trivial (the iteration
1049 * mask is redundant and set all 1s; all CPUs in a group will end up at _that_
1050 * group), we can simply pick the first CPU in each group.
1051 *
1052 *
1053 * [*] in other words, the first group of each domain is its child domain.
1054 */
1055
Peter Zijlstra0c0e7762017-05-03 14:18:06 +02001056static struct sched_group *get_group(int cpu, struct sd_data *sdd)
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001057{
1058 struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu);
1059 struct sched_domain *child = sd->child;
Peter Zijlstra0c0e7762017-05-03 14:18:06 +02001060 struct sched_group *sg;
Valentin Schneider67d4f6f2019-04-09 18:35:45 +01001061 bool already_visited;
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001062
1063 if (child)
1064 cpu = cpumask_first(sched_domain_span(child));
1065
Peter Zijlstra0c0e7762017-05-03 14:18:06 +02001066 sg = *per_cpu_ptr(sdd->sg, cpu);
1067 sg->sgc = *per_cpu_ptr(sdd->sgc, cpu);
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001068
Valentin Schneider67d4f6f2019-04-09 18:35:45 +01001069 /* Increase refcounts for claim_allocations: */
1070 already_visited = atomic_inc_return(&sg->ref) > 1;
1071 /* sgc visits should follow a similar trend as sg */
1072 WARN_ON(already_visited != (atomic_inc_return(&sg->sgc->ref) > 1));
1073
1074 /* If we have already visited that group, it's already initialized. */
1075 if (already_visited)
1076 return sg;
Peter Zijlstra0c0e7762017-05-03 14:18:06 +02001077
1078 if (child) {
Peter Zijlstraae4df9d2017-05-01 11:03:12 +02001079 cpumask_copy(sched_group_span(sg), sched_domain_span(child));
1080 cpumask_copy(group_balance_mask(sg), sched_group_span(sg));
Peter Zijlstra0c0e7762017-05-03 14:18:06 +02001081 } else {
Peter Zijlstraae4df9d2017-05-01 11:03:12 +02001082 cpumask_set_cpu(cpu, sched_group_span(sg));
Peter Zijlstrae5c14b12017-05-01 10:47:02 +02001083 cpumask_set_cpu(cpu, group_balance_mask(sg));
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001084 }
1085
Peter Zijlstraae4df9d2017-05-01 11:03:12 +02001086 sg->sgc->capacity = SCHED_CAPACITY_SCALE * cpumask_weight(sched_group_span(sg));
Peter Zijlstra0c0e7762017-05-03 14:18:06 +02001087 sg->sgc->min_capacity = SCHED_CAPACITY_SCALE;
Morten Rasmussene3d6d0c2018-07-04 11:17:41 +01001088 sg->sgc->max_capacity = SCHED_CAPACITY_SCALE;
Peter Zijlstra0c0e7762017-05-03 14:18:06 +02001089
1090 return sg;
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001091}
1092
1093/*
1094 * build_sched_groups will build a circular linked list of the groups
Valentin Schneiderd8743232019-04-09 18:35:46 +01001095 * covered by the given span, will set each group's ->cpumask correctly,
1096 * and will initialize their ->sgc.
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001097 *
1098 * Assumes the sched_domain tree is fully constructed
1099 */
1100static int
1101build_sched_groups(struct sched_domain *sd, int cpu)
1102{
1103 struct sched_group *first = NULL, *last = NULL;
1104 struct sd_data *sdd = sd->private;
1105 const struct cpumask *span = sched_domain_span(sd);
1106 struct cpumask *covered;
1107 int i;
1108
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001109 lockdep_assert_held(&sched_domains_mutex);
1110 covered = sched_domains_tmpmask;
1111
1112 cpumask_clear(covered);
1113
Peter Zijlstra0c0e7762017-05-03 14:18:06 +02001114 for_each_cpu_wrap(i, span, cpu) {
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001115 struct sched_group *sg;
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001116
1117 if (cpumask_test_cpu(i, covered))
1118 continue;
1119
Peter Zijlstra0c0e7762017-05-03 14:18:06 +02001120 sg = get_group(i, sdd);
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001121
Peter Zijlstraae4df9d2017-05-01 11:03:12 +02001122 cpumask_or(covered, covered, sched_group_span(sg));
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001123
1124 if (!first)
1125 first = sg;
1126 if (last)
1127 last->next = sg;
1128 last = sg;
1129 }
1130 last->next = first;
Peter Zijlstra0c0e7762017-05-03 14:18:06 +02001131 sd->groups = first;
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001132
1133 return 0;
1134}
1135
1136/*
1137 * Initialize sched groups cpu_capacity.
1138 *
1139 * cpu_capacity indicates the capacity of sched group, which is used while
1140 * distributing the load between different sched groups in a sched domain.
1141 * Typically cpu_capacity for all the groups in a sched domain will be same
1142 * unless there are asymmetries in the topology. If there are asymmetries,
1143 * group having more cpu_capacity will pickup more load compared to the
1144 * group having less cpu_capacity.
1145 */
1146static void init_sched_groups_capacity(int cpu, struct sched_domain *sd)
1147{
1148 struct sched_group *sg = sd->groups;
1149
1150 WARN_ON(!sg);
1151
1152 do {
1153 int cpu, max_cpu = -1;
1154
Peter Zijlstraae4df9d2017-05-01 11:03:12 +02001155 sg->group_weight = cpumask_weight(sched_group_span(sg));
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001156
1157 if (!(sd->flags & SD_ASYM_PACKING))
1158 goto next;
1159
Peter Zijlstraae4df9d2017-05-01 11:03:12 +02001160 for_each_cpu(cpu, sched_group_span(sg)) {
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001161 if (max_cpu < 0)
1162 max_cpu = cpu;
1163 else if (sched_asym_prefer(cpu, max_cpu))
1164 max_cpu = cpu;
1165 }
1166 sg->asym_prefer_cpu = max_cpu;
1167
1168next:
1169 sg = sg->next;
1170 } while (sg != sd->groups);
1171
1172 if (cpu != group_balance_cpu(sg))
1173 return;
1174
1175 update_group_capacity(sd, cpu);
1176}
1177
1178/*
1179 * Initializers for schedule domains
1180 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
1181 */
1182
1183static int default_relax_domain_level = -1;
1184int sched_domain_level_max;
1185
1186static int __init setup_relax_domain_level(char *str)
1187{
1188 if (kstrtoint(str, 0, &default_relax_domain_level))
1189 pr_warn("Unable to set relax_domain_level\n");
1190
1191 return 1;
1192}
1193__setup("relax_domain_level=", setup_relax_domain_level);
1194
1195static void set_domain_attribute(struct sched_domain *sd,
1196 struct sched_domain_attr *attr)
1197{
1198 int request;
1199
1200 if (!attr || attr->relax_domain_level < 0) {
1201 if (default_relax_domain_level < 0)
1202 return;
Valentin Schneider9ae7ab22019-10-14 17:44:08 +01001203 request = default_relax_domain_level;
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001204 } else
1205 request = attr->relax_domain_level;
Valentin Schneider9ae7ab22019-10-14 17:44:08 +01001206
1207 if (sd->level > request) {
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001208 /* Turn off idle balance on this domain: */
1209 sd->flags &= ~(SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001210 }
1211}
1212
1213static void __sdt_free(const struct cpumask *cpu_map);
1214static int __sdt_alloc(const struct cpumask *cpu_map);
1215
1216static void __free_domain_allocs(struct s_data *d, enum s_alloc what,
1217 const struct cpumask *cpu_map)
1218{
1219 switch (what) {
1220 case sa_rootdomain:
1221 if (!atomic_read(&d->rd->refcount))
1222 free_rootdomain(&d->rd->rcu);
1223 /* Fall through */
1224 case sa_sd:
1225 free_percpu(d->sd);
1226 /* Fall through */
1227 case sa_sd_storage:
1228 __sdt_free(cpu_map);
1229 /* Fall through */
1230 case sa_none:
1231 break;
1232 }
1233}
1234
1235static enum s_alloc
1236__visit_domain_allocation_hell(struct s_data *d, const struct cpumask *cpu_map)
1237{
1238 memset(d, 0, sizeof(*d));
1239
1240 if (__sdt_alloc(cpu_map))
1241 return sa_sd_storage;
1242 d->sd = alloc_percpu(struct sched_domain *);
1243 if (!d->sd)
1244 return sa_sd_storage;
1245 d->rd = alloc_rootdomain();
1246 if (!d->rd)
1247 return sa_sd;
Ingo Molnar97fb7a02018-03-03 14:01:12 +01001248
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001249 return sa_rootdomain;
1250}
1251
1252/*
1253 * NULL the sd_data elements we've used to build the sched_domain and
1254 * sched_group structure so that the subsequent __free_domain_allocs()
1255 * will not free the data we're using.
1256 */
1257static void claim_allocations(int cpu, struct sched_domain *sd)
1258{
1259 struct sd_data *sdd = sd->private;
1260
1261 WARN_ON_ONCE(*per_cpu_ptr(sdd->sd, cpu) != sd);
1262 *per_cpu_ptr(sdd->sd, cpu) = NULL;
1263
1264 if (atomic_read(&(*per_cpu_ptr(sdd->sds, cpu))->ref))
1265 *per_cpu_ptr(sdd->sds, cpu) = NULL;
1266
1267 if (atomic_read(&(*per_cpu_ptr(sdd->sg, cpu))->ref))
1268 *per_cpu_ptr(sdd->sg, cpu) = NULL;
1269
1270 if (atomic_read(&(*per_cpu_ptr(sdd->sgc, cpu))->ref))
1271 *per_cpu_ptr(sdd->sgc, cpu) = NULL;
1272}
1273
1274#ifdef CONFIG_NUMA
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001275enum numa_topology_type sched_numa_topology_type;
Ingo Molnar97fb7a02018-03-03 14:01:12 +01001276
1277static int sched_domains_numa_levels;
1278static int sched_domains_curr_level;
1279
1280int sched_max_numa_distance;
1281static int *sched_domains_numa_distance;
1282static struct cpumask ***sched_domains_numa_masks;
Matt Fleminga55c7452019-08-08 20:53:01 +01001283int __read_mostly node_reclaim_distance = RECLAIM_DISTANCE;
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001284#endif
1285
1286/*
1287 * SD_flags allowed in topology descriptions.
1288 *
1289 * These flags are purely descriptive of the topology and do not prescribe
1290 * behaviour. Behaviour is artificial and mapped in the below sd_init()
1291 * function:
1292 *
1293 * SD_SHARE_CPUCAPACITY - describes SMT topologies
1294 * SD_SHARE_PKG_RESOURCES - describes shared caches
1295 * SD_NUMA - describes NUMA topologies
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001296 *
1297 * Odd one out, which beside describing the topology has a quirk also
1298 * prescribes the desired behaviour that goes along with it:
1299 *
1300 * SD_ASYM_PACKING - describes SMT quirks
1301 */
1302#define TOPOLOGY_SD_FLAGS \
Ingo Molnar97fb7a02018-03-03 14:01:12 +01001303 (SD_SHARE_CPUCAPACITY | \
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001304 SD_SHARE_PKG_RESOURCES | \
Ingo Molnar97fb7a02018-03-03 14:01:12 +01001305 SD_NUMA | \
Valentin Schneidercfe7ddc2020-08-17 12:29:47 +01001306 SD_ASYM_PACKING)
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001307
1308static struct sched_domain *
1309sd_init(struct sched_domain_topology_level *tl,
1310 const struct cpumask *cpu_map,
Morten Rasmussen05484e02018-07-20 14:32:31 +01001311 struct sched_domain *child, int dflags, int cpu)
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001312{
1313 struct sd_data *sdd = &tl->data;
1314 struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu);
1315 int sd_id, sd_weight, sd_flags = 0;
1316
1317#ifdef CONFIG_NUMA
1318 /*
1319 * Ugly hack to pass state to sd_numa_mask()...
1320 */
1321 sched_domains_curr_level = tl->numa_level;
1322#endif
1323
1324 sd_weight = cpumask_weight(tl->mask(cpu));
1325
1326 if (tl->sd_flags)
1327 sd_flags = (*tl->sd_flags)();
1328 if (WARN_ONCE(sd_flags & ~TOPOLOGY_SD_FLAGS,
1329 "wrong sd_flags in topology description\n"))
Peng Liu9b1b2342020-06-09 23:09:36 +08001330 sd_flags &= TOPOLOGY_SD_FLAGS;
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001331
Morten Rasmussen05484e02018-07-20 14:32:31 +01001332 /* Apply detected topology flags */
1333 sd_flags |= dflags;
1334
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001335 *sd = (struct sched_domain){
1336 .min_interval = sd_weight,
1337 .max_interval = 2*sd_weight,
1338 .busy_factor = 32,
1339 .imbalance_pct = 125,
1340
1341 .cache_nice_tries = 0,
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001342
Valentin Schneider36c5bdc2020-04-15 22:05:07 +01001343 .flags = 1*SD_BALANCE_NEWIDLE
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001344 | 1*SD_BALANCE_EXEC
1345 | 1*SD_BALANCE_FORK
1346 | 0*SD_BALANCE_WAKE
1347 | 1*SD_WAKE_AFFINE
1348 | 0*SD_SHARE_CPUCAPACITY
1349 | 0*SD_SHARE_PKG_RESOURCES
1350 | 0*SD_SERIALIZE
Morten Rasmussen9c63e842018-07-04 11:17:50 +01001351 | 1*SD_PREFER_SIBLING
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001352 | 0*SD_NUMA
1353 | sd_flags
1354 ,
1355
1356 .last_balance = jiffies,
1357 .balance_interval = sd_weight,
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001358 .max_newidle_lb_cost = 0,
1359 .next_decay_max_lb_cost = jiffies,
1360 .child = child,
1361#ifdef CONFIG_SCHED_DEBUG
1362 .name = tl->name,
1363#endif
1364 };
1365
1366 cpumask_and(sched_domain_span(sd), cpu_map, tl->mask(cpu));
1367 sd_id = cpumask_first(sched_domain_span(sd));
1368
1369 /*
1370 * Convert topological properties into behaviour.
1371 */
1372
Morten Rasmussena526d462020-02-06 19:19:55 +00001373 /* Don't attempt to spread across CPUs of different capacities. */
1374 if ((sd->flags & SD_ASYM_CPUCAPACITY) && sd->child)
1375 sd->child->flags &= ~SD_PREFER_SIBLING;
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001376
1377 if (sd->flags & SD_SHARE_CPUCAPACITY) {
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001378 sd->imbalance_pct = 110;
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001379
1380 } else if (sd->flags & SD_SHARE_PKG_RESOURCES) {
1381 sd->imbalance_pct = 117;
1382 sd->cache_nice_tries = 1;
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001383
1384#ifdef CONFIG_NUMA
1385 } else if (sd->flags & SD_NUMA) {
1386 sd->cache_nice_tries = 2;
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001387
Morten Rasmussen9c63e842018-07-04 11:17:50 +01001388 sd->flags &= ~SD_PREFER_SIBLING;
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001389 sd->flags |= SD_SERIALIZE;
Matt Fleminga55c7452019-08-08 20:53:01 +01001390 if (sched_domains_numa_distance[tl->numa_level] > node_reclaim_distance) {
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001391 sd->flags &= ~(SD_BALANCE_EXEC |
1392 SD_BALANCE_FORK |
1393 SD_WAKE_AFFINE);
1394 }
1395
1396#endif
1397 } else {
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001398 sd->cache_nice_tries = 1;
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001399 }
1400
1401 /*
1402 * For all levels sharing cache; connect a sched_domain_shared
1403 * instance.
1404 */
1405 if (sd->flags & SD_SHARE_PKG_RESOURCES) {
1406 sd->shared = *per_cpu_ptr(sdd->sds, sd_id);
1407 atomic_inc(&sd->shared->ref);
1408 atomic_set(&sd->shared->nr_busy_cpus, sd_weight);
1409 }
1410
1411 sd->private = sdd;
1412
1413 return sd;
1414}
1415
1416/*
1417 * Topology list, bottom-up.
1418 */
1419static struct sched_domain_topology_level default_topology[] = {
1420#ifdef CONFIG_SCHED_SMT
1421 { cpu_smt_mask, cpu_smt_flags, SD_INIT_NAME(SMT) },
1422#endif
1423#ifdef CONFIG_SCHED_MC
1424 { cpu_coregroup_mask, cpu_core_flags, SD_INIT_NAME(MC) },
1425#endif
1426 { cpu_cpu_mask, SD_INIT_NAME(DIE) },
1427 { NULL, },
1428};
1429
1430static struct sched_domain_topology_level *sched_domain_topology =
1431 default_topology;
1432
1433#define for_each_sd_topology(tl) \
1434 for (tl = sched_domain_topology; tl->mask; tl++)
1435
1436void set_sched_topology(struct sched_domain_topology_level *tl)
1437{
1438 if (WARN_ON_ONCE(sched_smp_initialized))
1439 return;
1440
1441 sched_domain_topology = tl;
1442}
1443
1444#ifdef CONFIG_NUMA
1445
1446static const struct cpumask *sd_numa_mask(int cpu)
1447{
1448 return sched_domains_numa_masks[sched_domains_curr_level][cpu_to_node(cpu)];
1449}
1450
1451static void sched_numa_warn(const char *str)
1452{
1453 static int done = false;
1454 int i,j;
1455
1456 if (done)
1457 return;
1458
1459 done = true;
1460
1461 printk(KERN_WARNING "ERROR: %s\n\n", str);
1462
1463 for (i = 0; i < nr_node_ids; i++) {
1464 printk(KERN_WARNING " ");
1465 for (j = 0; j < nr_node_ids; j++)
1466 printk(KERN_CONT "%02d ", node_distance(i,j));
1467 printk(KERN_CONT "\n");
1468 }
1469 printk(KERN_WARNING "\n");
1470}
1471
1472bool find_numa_distance(int distance)
1473{
1474 int i;
1475
1476 if (distance == node_distance(0, 0))
1477 return true;
1478
1479 for (i = 0; i < sched_domains_numa_levels; i++) {
1480 if (sched_domains_numa_distance[i] == distance)
1481 return true;
1482 }
1483
1484 return false;
1485}
1486
1487/*
1488 * A system can have three types of NUMA topology:
1489 * NUMA_DIRECT: all nodes are directly connected, or not a NUMA system
1490 * NUMA_GLUELESS_MESH: some nodes reachable through intermediary nodes
1491 * NUMA_BACKPLANE: nodes can reach other nodes through a backplane
1492 *
1493 * The difference between a glueless mesh topology and a backplane
1494 * topology lies in whether communication between not directly
1495 * connected nodes goes through intermediary nodes (where programs
1496 * could run), or through backplane controllers. This affects
1497 * placement of programs.
1498 *
1499 * The type of topology can be discerned with the following tests:
1500 * - If the maximum distance between any nodes is 1 hop, the system
1501 * is directly connected.
1502 * - If for two nodes A and B, located N > 1 hops away from each other,
1503 * there is an intermediary node C, which is < N hops away from both
1504 * nodes A and B, the system is a glueless mesh.
1505 */
1506static void init_numa_topology_type(void)
1507{
1508 int a, b, c, n;
1509
1510 n = sched_max_numa_distance;
1511
Srikar Dronamrajue5e96fa2018-08-10 22:30:18 +05301512 if (sched_domains_numa_levels <= 2) {
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001513 sched_numa_topology_type = NUMA_DIRECT;
1514 return;
1515 }
1516
1517 for_each_online_node(a) {
1518 for_each_online_node(b) {
1519 /* Find two nodes furthest removed from each other. */
1520 if (node_distance(a, b) < n)
1521 continue;
1522
1523 /* Is there an intermediary node between a and b? */
1524 for_each_online_node(c) {
1525 if (node_distance(a, c) < n &&
1526 node_distance(b, c) < n) {
1527 sched_numa_topology_type =
1528 NUMA_GLUELESS_MESH;
1529 return;
1530 }
1531 }
1532
1533 sched_numa_topology_type = NUMA_BACKPLANE;
1534 return;
1535 }
1536 }
1537}
1538
1539void sched_init_numa(void)
1540{
1541 int next_distance, curr_distance = node_distance(0, 0);
1542 struct sched_domain_topology_level *tl;
1543 int level = 0;
1544 int i, j, k;
1545
Peter Zijlstra993f0b02018-11-02 14:22:25 +01001546 sched_domains_numa_distance = kzalloc(sizeof(int) * (nr_node_ids + 1), GFP_KERNEL);
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001547 if (!sched_domains_numa_distance)
1548 return;
1549
Suravee Suthikulpanit051f3ca2017-09-07 02:20:05 -05001550 /* Includes NUMA identity node at level 0. */
1551 sched_domains_numa_distance[level++] = curr_distance;
1552 sched_domains_numa_levels = level;
1553
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001554 /*
1555 * O(nr_nodes^2) deduplicating selection sort -- in order to find the
1556 * unique distances in the node_distance() table.
1557 *
1558 * Assumes node_distance(0,j) includes all distances in
1559 * node_distance(i,j) in order to avoid cubic time.
1560 */
1561 next_distance = curr_distance;
1562 for (i = 0; i < nr_node_ids; i++) {
1563 for (j = 0; j < nr_node_ids; j++) {
1564 for (k = 0; k < nr_node_ids; k++) {
1565 int distance = node_distance(i, k);
1566
1567 if (distance > curr_distance &&
1568 (distance < next_distance ||
1569 next_distance == curr_distance))
1570 next_distance = distance;
1571
1572 /*
1573 * While not a strong assumption it would be nice to know
1574 * about cases where if node A is connected to B, B is not
1575 * equally connected to A.
1576 */
1577 if (sched_debug() && node_distance(k, i) != distance)
1578 sched_numa_warn("Node-distance not symmetric");
1579
1580 if (sched_debug() && i && !find_numa_distance(distance))
1581 sched_numa_warn("Node-0 not representative");
1582 }
1583 if (next_distance != curr_distance) {
1584 sched_domains_numa_distance[level++] = next_distance;
1585 sched_domains_numa_levels = level;
1586 curr_distance = next_distance;
1587 } else break;
1588 }
1589
1590 /*
1591 * In case of sched_debug() we verify the above assumption.
1592 */
1593 if (!sched_debug())
1594 break;
1595 }
1596
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001597 /*
Suravee Suthikulpanit051f3ca2017-09-07 02:20:05 -05001598 * 'level' contains the number of unique distances
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001599 *
1600 * The sched_domains_numa_distance[] array includes the actual distance
1601 * numbers.
1602 */
1603
1604 /*
1605 * Here, we should temporarily reset sched_domains_numa_levels to 0.
1606 * If it fails to allocate memory for array sched_domains_numa_masks[][],
1607 * the array will contain less then 'level' members. This could be
1608 * dangerous when we use it to iterate array sched_domains_numa_masks[][]
1609 * in other functions.
1610 *
1611 * We reset it to 'level' at the end of this function.
1612 */
1613 sched_domains_numa_levels = 0;
1614
1615 sched_domains_numa_masks = kzalloc(sizeof(void *) * level, GFP_KERNEL);
1616 if (!sched_domains_numa_masks)
1617 return;
1618
1619 /*
1620 * Now for each level, construct a mask per node which contains all
1621 * CPUs of nodes that are that many hops away from us.
1622 */
1623 for (i = 0; i < level; i++) {
1624 sched_domains_numa_masks[i] =
1625 kzalloc(nr_node_ids * sizeof(void *), GFP_KERNEL);
1626 if (!sched_domains_numa_masks[i])
1627 return;
1628
1629 for (j = 0; j < nr_node_ids; j++) {
1630 struct cpumask *mask = kzalloc(cpumask_size(), GFP_KERNEL);
1631 if (!mask)
1632 return;
1633
1634 sched_domains_numa_masks[i][j] = mask;
1635
1636 for_each_node(k) {
1637 if (node_distance(j, k) > sched_domains_numa_distance[i])
1638 continue;
1639
1640 cpumask_or(mask, mask, cpumask_of_node(k));
1641 }
1642 }
1643 }
1644
1645 /* Compute default topology size */
1646 for (i = 0; sched_domain_topology[i].mask; i++);
1647
1648 tl = kzalloc((i + level + 1) *
1649 sizeof(struct sched_domain_topology_level), GFP_KERNEL);
1650 if (!tl)
1651 return;
1652
1653 /*
1654 * Copy the default topology bits..
1655 */
1656 for (i = 0; sched_domain_topology[i].mask; i++)
1657 tl[i] = sched_domain_topology[i];
1658
1659 /*
Suravee Suthikulpanit051f3ca2017-09-07 02:20:05 -05001660 * Add the NUMA identity distance, aka single NODE.
1661 */
1662 tl[i++] = (struct sched_domain_topology_level){
1663 .mask = sd_numa_mask,
1664 .numa_level = 0,
1665 SD_INIT_NAME(NODE)
1666 };
1667
1668 /*
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001669 * .. and append 'j' levels of NUMA goodness.
1670 */
Suravee Suthikulpanit051f3ca2017-09-07 02:20:05 -05001671 for (j = 1; j < level; i++, j++) {
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001672 tl[i] = (struct sched_domain_topology_level){
1673 .mask = sd_numa_mask,
1674 .sd_flags = cpu_numa_flags,
1675 .flags = SDTL_OVERLAP,
1676 .numa_level = j,
1677 SD_INIT_NAME(NUMA)
1678 };
1679 }
1680
1681 sched_domain_topology = tl;
1682
1683 sched_domains_numa_levels = level;
1684 sched_max_numa_distance = sched_domains_numa_distance[level - 1];
1685
1686 init_numa_topology_type();
1687}
1688
1689void sched_domains_numa_masks_set(unsigned int cpu)
1690{
1691 int node = cpu_to_node(cpu);
1692 int i, j;
1693
1694 for (i = 0; i < sched_domains_numa_levels; i++) {
1695 for (j = 0; j < nr_node_ids; j++) {
1696 if (node_distance(j, node) <= sched_domains_numa_distance[i])
1697 cpumask_set_cpu(cpu, sched_domains_numa_masks[i][j]);
1698 }
1699 }
1700}
1701
1702void sched_domains_numa_masks_clear(unsigned int cpu)
1703{
1704 int i, j;
1705
1706 for (i = 0; i < sched_domains_numa_levels; i++) {
1707 for (j = 0; j < nr_node_ids; j++)
1708 cpumask_clear_cpu(cpu, sched_domains_numa_masks[i][j]);
1709 }
1710}
1711
Wanpeng Lie0e8d492019-06-28 16:51:41 +08001712/*
1713 * sched_numa_find_closest() - given the NUMA topology, find the cpu
1714 * closest to @cpu from @cpumask.
1715 * cpumask: cpumask to find a cpu from
1716 * cpu: cpu to be close to
1717 *
1718 * returns: cpu, or nr_cpu_ids when nothing found.
1719 */
1720int sched_numa_find_closest(const struct cpumask *cpus, int cpu)
1721{
1722 int i, j = cpu_to_node(cpu);
1723
1724 for (i = 0; i < sched_domains_numa_levels; i++) {
1725 cpu = cpumask_any_and(cpus, sched_domains_numa_masks[i][j]);
1726 if (cpu < nr_cpu_ids)
1727 return cpu;
1728 }
1729 return nr_cpu_ids;
1730}
1731
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001732#endif /* CONFIG_NUMA */
1733
1734static int __sdt_alloc(const struct cpumask *cpu_map)
1735{
1736 struct sched_domain_topology_level *tl;
1737 int j;
1738
1739 for_each_sd_topology(tl) {
1740 struct sd_data *sdd = &tl->data;
1741
1742 sdd->sd = alloc_percpu(struct sched_domain *);
1743 if (!sdd->sd)
1744 return -ENOMEM;
1745
1746 sdd->sds = alloc_percpu(struct sched_domain_shared *);
1747 if (!sdd->sds)
1748 return -ENOMEM;
1749
1750 sdd->sg = alloc_percpu(struct sched_group *);
1751 if (!sdd->sg)
1752 return -ENOMEM;
1753
1754 sdd->sgc = alloc_percpu(struct sched_group_capacity *);
1755 if (!sdd->sgc)
1756 return -ENOMEM;
1757
1758 for_each_cpu(j, cpu_map) {
1759 struct sched_domain *sd;
1760 struct sched_domain_shared *sds;
1761 struct sched_group *sg;
1762 struct sched_group_capacity *sgc;
1763
1764 sd = kzalloc_node(sizeof(struct sched_domain) + cpumask_size(),
1765 GFP_KERNEL, cpu_to_node(j));
1766 if (!sd)
1767 return -ENOMEM;
1768
1769 *per_cpu_ptr(sdd->sd, j) = sd;
1770
1771 sds = kzalloc_node(sizeof(struct sched_domain_shared),
1772 GFP_KERNEL, cpu_to_node(j));
1773 if (!sds)
1774 return -ENOMEM;
1775
1776 *per_cpu_ptr(sdd->sds, j) = sds;
1777
1778 sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
1779 GFP_KERNEL, cpu_to_node(j));
1780 if (!sg)
1781 return -ENOMEM;
1782
1783 sg->next = sg;
1784
1785 *per_cpu_ptr(sdd->sg, j) = sg;
1786
1787 sgc = kzalloc_node(sizeof(struct sched_group_capacity) + cpumask_size(),
1788 GFP_KERNEL, cpu_to_node(j));
1789 if (!sgc)
1790 return -ENOMEM;
1791
Peter Zijlstra005f8742017-04-26 17:35:35 +02001792#ifdef CONFIG_SCHED_DEBUG
1793 sgc->id = j;
1794#endif
1795
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001796 *per_cpu_ptr(sdd->sgc, j) = sgc;
1797 }
1798 }
1799
1800 return 0;
1801}
1802
1803static void __sdt_free(const struct cpumask *cpu_map)
1804{
1805 struct sched_domain_topology_level *tl;
1806 int j;
1807
1808 for_each_sd_topology(tl) {
1809 struct sd_data *sdd = &tl->data;
1810
1811 for_each_cpu(j, cpu_map) {
1812 struct sched_domain *sd;
1813
1814 if (sdd->sd) {
1815 sd = *per_cpu_ptr(sdd->sd, j);
1816 if (sd && (sd->flags & SD_OVERLAP))
1817 free_sched_groups(sd->groups, 0);
1818 kfree(*per_cpu_ptr(sdd->sd, j));
1819 }
1820
1821 if (sdd->sds)
1822 kfree(*per_cpu_ptr(sdd->sds, j));
1823 if (sdd->sg)
1824 kfree(*per_cpu_ptr(sdd->sg, j));
1825 if (sdd->sgc)
1826 kfree(*per_cpu_ptr(sdd->sgc, j));
1827 }
1828 free_percpu(sdd->sd);
1829 sdd->sd = NULL;
1830 free_percpu(sdd->sds);
1831 sdd->sds = NULL;
1832 free_percpu(sdd->sg);
1833 sdd->sg = NULL;
1834 free_percpu(sdd->sgc);
1835 sdd->sgc = NULL;
1836 }
1837}
1838
Viresh Kumar181a80d12017-04-27 13:58:59 +05301839static struct sched_domain *build_sched_domain(struct sched_domain_topology_level *tl,
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001840 const struct cpumask *cpu_map, struct sched_domain_attr *attr,
Morten Rasmussen05484e02018-07-20 14:32:31 +01001841 struct sched_domain *child, int dflags, int cpu)
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001842{
Morten Rasmussen05484e02018-07-20 14:32:31 +01001843 struct sched_domain *sd = sd_init(tl, cpu_map, child, dflags, cpu);
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001844
1845 if (child) {
1846 sd->level = child->level + 1;
1847 sched_domain_level_max = max(sched_domain_level_max, sd->level);
1848 child->parent = sd;
1849
1850 if (!cpumask_subset(sched_domain_span(child),
1851 sched_domain_span(sd))) {
1852 pr_err("BUG: arch topology borken\n");
1853#ifdef CONFIG_SCHED_DEBUG
1854 pr_err(" the %s domain not a subset of the %s domain\n",
1855 child->name, sd->name);
1856#endif
Ingo Molnar97fb7a02018-03-03 14:01:12 +01001857 /* Fixup, ensure @sd has at least @child CPUs. */
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001858 cpumask_or(sched_domain_span(sd),
1859 sched_domain_span(sd),
1860 sched_domain_span(child));
1861 }
1862
1863 }
1864 set_domain_attribute(sd, attr);
1865
1866 return sd;
1867}
1868
1869/*
Valentin Schneiderccf74122020-01-15 16:09:15 +00001870 * Ensure topology masks are sane, i.e. there are no conflicts (overlaps) for
1871 * any two given CPUs at this (non-NUMA) topology level.
1872 */
1873static bool topology_span_sane(struct sched_domain_topology_level *tl,
1874 const struct cpumask *cpu_map, int cpu)
1875{
1876 int i;
1877
1878 /* NUMA levels are allowed to overlap */
1879 if (tl->flags & SDTL_OVERLAP)
1880 return true;
1881
1882 /*
1883 * Non-NUMA levels cannot partially overlap - they must be either
1884 * completely equal or completely disjoint. Otherwise we can end up
1885 * breaking the sched_group lists - i.e. a later get_group() pass
1886 * breaks the linking done for an earlier span.
1887 */
1888 for_each_cpu(i, cpu_map) {
1889 if (i == cpu)
1890 continue;
1891 /*
1892 * We should 'and' all those masks with 'cpu_map' to exactly
1893 * match the topology we're about to build, but that can only
1894 * remove CPUs, which only lessens our ability to detect
1895 * overlaps
1896 */
1897 if (!cpumask_equal(tl->mask(cpu), tl->mask(i)) &&
1898 cpumask_intersects(tl->mask(cpu), tl->mask(i)))
1899 return false;
1900 }
1901
1902 return true;
1903}
1904
1905/*
Morten Rasmussen05484e02018-07-20 14:32:31 +01001906 * Find the sched_domain_topology_level where all CPU capacities are visible
1907 * for all CPUs.
1908 */
1909static struct sched_domain_topology_level
1910*asym_cpu_capacity_level(const struct cpumask *cpu_map)
1911{
1912 int i, j, asym_level = 0;
1913 bool asym = false;
1914 struct sched_domain_topology_level *tl, *asym_tl = NULL;
1915 unsigned long cap;
1916
1917 /* Is there any asymmetry? */
Vincent Guittot8ec59c02019-06-17 17:00:17 +02001918 cap = arch_scale_cpu_capacity(cpumask_first(cpu_map));
Morten Rasmussen05484e02018-07-20 14:32:31 +01001919
1920 for_each_cpu(i, cpu_map) {
Vincent Guittot8ec59c02019-06-17 17:00:17 +02001921 if (arch_scale_cpu_capacity(i) != cap) {
Morten Rasmussen05484e02018-07-20 14:32:31 +01001922 asym = true;
1923 break;
1924 }
1925 }
1926
1927 if (!asym)
1928 return NULL;
1929
1930 /*
1931 * Examine topology from all CPU's point of views to detect the lowest
1932 * sched_domain_topology_level where a highest capacity CPU is visible
1933 * to everyone.
1934 */
1935 for_each_cpu(i, cpu_map) {
Vincent Guittot8ec59c02019-06-17 17:00:17 +02001936 unsigned long max_capacity = arch_scale_cpu_capacity(i);
Morten Rasmussen05484e02018-07-20 14:32:31 +01001937 int tl_id = 0;
1938
1939 for_each_sd_topology(tl) {
1940 if (tl_id < asym_level)
1941 goto next_level;
1942
1943 for_each_cpu_and(j, tl->mask(i), cpu_map) {
1944 unsigned long capacity;
1945
Vincent Guittot8ec59c02019-06-17 17:00:17 +02001946 capacity = arch_scale_cpu_capacity(j);
Morten Rasmussen05484e02018-07-20 14:32:31 +01001947
1948 if (capacity <= max_capacity)
1949 continue;
1950
1951 max_capacity = capacity;
1952 asym_level = tl_id;
1953 asym_tl = tl;
1954 }
1955next_level:
1956 tl_id++;
1957 }
1958 }
1959
1960 return asym_tl;
1961}
1962
1963
1964/*
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001965 * Build sched domains for a given set of CPUs and attach the sched domains
1966 * to the individual CPUs
1967 */
1968static int
1969build_sched_domains(const struct cpumask *cpu_map, struct sched_domain_attr *attr)
1970{
Valentin Schneidercd1cb332019-10-23 16:37:44 +01001971 enum s_alloc alloc_state = sa_none;
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001972 struct sched_domain *sd;
1973 struct s_data d;
1974 struct rq *rq = NULL;
1975 int i, ret = -ENOMEM;
Morten Rasmussen05484e02018-07-20 14:32:31 +01001976 struct sched_domain_topology_level *tl_asym;
Morten Rasmussendf054e82018-07-04 11:17:39 +01001977 bool has_asym = false;
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001978
Valentin Schneidercd1cb332019-10-23 16:37:44 +01001979 if (WARN_ON(cpumask_empty(cpu_map)))
1980 goto error;
1981
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001982 alloc_state = __visit_domain_allocation_hell(&d, cpu_map);
1983 if (alloc_state != sa_rootdomain)
1984 goto error;
1985
Morten Rasmussen05484e02018-07-20 14:32:31 +01001986 tl_asym = asym_cpu_capacity_level(cpu_map);
1987
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001988 /* Set up domains for CPUs specified by the cpu_map: */
1989 for_each_cpu(i, cpu_map) {
1990 struct sched_domain_topology_level *tl;
Valentin Schneiderc2001912020-08-17 12:29:56 +01001991 int dflags = 0;
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001992
1993 sd = NULL;
1994 for_each_sd_topology(tl) {
Morten Rasmussendf054e82018-07-04 11:17:39 +01001995 if (tl == tl_asym) {
Morten Rasmussen05484e02018-07-20 14:32:31 +01001996 dflags |= SD_ASYM_CPUCAPACITY;
Morten Rasmussendf054e82018-07-04 11:17:39 +01001997 has_asym = true;
1998 }
Morten Rasmussen05484e02018-07-20 14:32:31 +01001999
Valentin Schneiderccf74122020-01-15 16:09:15 +00002000 if (WARN_ON(!topology_span_sane(tl, cpu_map, i)))
2001 goto error;
2002
Morten Rasmussen05484e02018-07-20 14:32:31 +01002003 sd = build_sched_domain(tl, cpu_map, attr, sd, dflags, i);
2004
Ingo Molnarf2cb1362017-02-01 13:10:18 +01002005 if (tl == sched_domain_topology)
2006 *per_cpu_ptr(d.sd, i) = sd;
Peter Zijlstraaf855962017-04-26 17:36:41 +02002007 if (tl->flags & SDTL_OVERLAP)
Ingo Molnarf2cb1362017-02-01 13:10:18 +01002008 sd->flags |= SD_OVERLAP;
2009 if (cpumask_equal(cpu_map, sched_domain_span(sd)))
2010 break;
2011 }
2012 }
2013
2014 /* Build the groups for the domains */
2015 for_each_cpu(i, cpu_map) {
2016 for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
2017 sd->span_weight = cpumask_weight(sched_domain_span(sd));
2018 if (sd->flags & SD_OVERLAP) {
2019 if (build_overlap_sched_groups(sd, i))
2020 goto error;
2021 } else {
2022 if (build_sched_groups(sd, i))
2023 goto error;
2024 }
2025 }
2026 }
2027
2028 /* Calculate CPU capacity for physical packages and nodes */
2029 for (i = nr_cpumask_bits-1; i >= 0; i--) {
2030 if (!cpumask_test_cpu(i, cpu_map))
2031 continue;
2032
2033 for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
2034 claim_allocations(i, sd);
2035 init_sched_groups_capacity(i, sd);
2036 }
2037 }
2038
2039 /* Attach the domains */
2040 rcu_read_lock();
2041 for_each_cpu(i, cpu_map) {
2042 rq = cpu_rq(i);
2043 sd = *per_cpu_ptr(d.sd, i);
2044
2045 /* Use READ_ONCE()/WRITE_ONCE() to avoid load/store tearing: */
2046 if (rq->cpu_capacity_orig > READ_ONCE(d.rd->max_cpu_capacity))
2047 WRITE_ONCE(d.rd->max_cpu_capacity, rq->cpu_capacity_orig);
2048
2049 cpu_attach_domain(sd, d.rd, i);
2050 }
2051 rcu_read_unlock();
2052
Morten Rasmussendf054e82018-07-04 11:17:39 +01002053 if (has_asym)
Valentin Schneidere284df72019-10-23 16:37:45 +01002054 static_branch_inc_cpuslocked(&sched_asym_cpucapacity);
Morten Rasmussendf054e82018-07-04 11:17:39 +01002055
Ingo Molnarf2cb1362017-02-01 13:10:18 +01002056 if (rq && sched_debug_enabled) {
Juri Lellibf5015a2018-05-24 17:29:36 +02002057 pr_info("root domain span: %*pbl (max cpu_capacity = %lu)\n",
Ingo Molnarf2cb1362017-02-01 13:10:18 +01002058 cpumask_pr_args(cpu_map), rq->rd->max_cpu_capacity);
2059 }
2060
2061 ret = 0;
2062error:
2063 __free_domain_allocs(&d, alloc_state, cpu_map);
Ingo Molnar97fb7a02018-03-03 14:01:12 +01002064
Ingo Molnarf2cb1362017-02-01 13:10:18 +01002065 return ret;
2066}
2067
2068/* Current sched domains: */
2069static cpumask_var_t *doms_cur;
2070
2071/* Number of sched domains in 'doms_cur': */
2072static int ndoms_cur;
2073
2074/* Attribues of custom domains in 'doms_cur' */
2075static struct sched_domain_attr *dattr_cur;
2076
2077/*
2078 * Special case: If a kmalloc() of a doms_cur partition (array of
2079 * cpumask) fails, then fallback to a single sched domain,
2080 * as determined by the single cpumask fallback_doms.
2081 */
Peter Zijlstra8d5dc512017-04-25 15:29:40 +02002082static cpumask_var_t fallback_doms;
Ingo Molnarf2cb1362017-02-01 13:10:18 +01002083
2084/*
2085 * arch_update_cpu_topology lets virtualized architectures update the
2086 * CPU core maps. It is supposed to return 1 if the topology changed
2087 * or 0 if it stayed the same.
2088 */
2089int __weak arch_update_cpu_topology(void)
2090{
2091 return 0;
2092}
2093
2094cpumask_var_t *alloc_sched_domains(unsigned int ndoms)
2095{
2096 int i;
2097 cpumask_var_t *doms;
2098
Kees Cook6da2ec52018-06-12 13:55:00 -07002099 doms = kmalloc_array(ndoms, sizeof(*doms), GFP_KERNEL);
Ingo Molnarf2cb1362017-02-01 13:10:18 +01002100 if (!doms)
2101 return NULL;
2102 for (i = 0; i < ndoms; i++) {
2103 if (!alloc_cpumask_var(&doms[i], GFP_KERNEL)) {
2104 free_sched_domains(doms, i);
2105 return NULL;
2106 }
2107 }
2108 return doms;
2109}
2110
2111void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms)
2112{
2113 unsigned int i;
2114 for (i = 0; i < ndoms; i++)
2115 free_cpumask_var(doms[i]);
2116 kfree(doms);
2117}
2118
2119/*
Juri Lellicb0c0412018-12-19 14:34:45 +01002120 * Set up scheduler domains and groups. For now this just excludes isolated
2121 * CPUs, but could be used to exclude other special cases in the future.
Ingo Molnarf2cb1362017-02-01 13:10:18 +01002122 */
Peter Zijlstra8d5dc512017-04-25 15:29:40 +02002123int sched_init_domains(const struct cpumask *cpu_map)
Ingo Molnarf2cb1362017-02-01 13:10:18 +01002124{
2125 int err;
2126
Peter Zijlstra8d5dc512017-04-25 15:29:40 +02002127 zalloc_cpumask_var(&sched_domains_tmpmask, GFP_KERNEL);
Peter Zijlstra16763302017-04-25 14:31:11 +02002128 zalloc_cpumask_var(&sched_domains_tmpmask2, GFP_KERNEL);
Peter Zijlstra8d5dc512017-04-25 15:29:40 +02002129 zalloc_cpumask_var(&fallback_doms, GFP_KERNEL);
2130
Ingo Molnarf2cb1362017-02-01 13:10:18 +01002131 arch_update_cpu_topology();
2132 ndoms_cur = 1;
2133 doms_cur = alloc_sched_domains(ndoms_cur);
2134 if (!doms_cur)
2135 doms_cur = &fallback_doms;
Frederic Weisbeckeredb93822017-10-27 04:42:37 +02002136 cpumask_and(doms_cur[0], cpu_map, housekeeping_cpumask(HK_FLAG_DOMAIN));
Ingo Molnarf2cb1362017-02-01 13:10:18 +01002137 err = build_sched_domains(doms_cur[0], NULL);
2138 register_sched_domain_sysctl();
2139
2140 return err;
2141}
2142
2143/*
2144 * Detach sched domains from a group of CPUs specified in cpu_map
2145 * These CPUs will now be attached to the NULL domain
2146 */
2147static void detach_destroy_domains(const struct cpumask *cpu_map)
2148{
Valentin Schneidere284df72019-10-23 16:37:45 +01002149 unsigned int cpu = cpumask_any(cpu_map);
Ingo Molnarf2cb1362017-02-01 13:10:18 +01002150 int i;
2151
Valentin Schneidere284df72019-10-23 16:37:45 +01002152 if (rcu_access_pointer(per_cpu(sd_asym_cpucapacity, cpu)))
2153 static_branch_dec_cpuslocked(&sched_asym_cpucapacity);
2154
Ingo Molnarf2cb1362017-02-01 13:10:18 +01002155 rcu_read_lock();
2156 for_each_cpu(i, cpu_map)
2157 cpu_attach_domain(NULL, &def_root_domain, i);
2158 rcu_read_unlock();
2159}
2160
2161/* handle null as "default" */
2162static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
2163 struct sched_domain_attr *new, int idx_new)
2164{
2165 struct sched_domain_attr tmp;
2166
2167 /* Fast path: */
2168 if (!new && !cur)
2169 return 1;
2170
2171 tmp = SD_ATTR_INIT;
Ingo Molnar97fb7a02018-03-03 14:01:12 +01002172
Ingo Molnarf2cb1362017-02-01 13:10:18 +01002173 return !memcmp(cur ? (cur + idx_cur) : &tmp,
2174 new ? (new + idx_new) : &tmp,
2175 sizeof(struct sched_domain_attr));
2176}
2177
2178/*
2179 * Partition sched domains as specified by the 'ndoms_new'
2180 * cpumasks in the array doms_new[] of cpumasks. This compares
2181 * doms_new[] to the current sched domain partitioning, doms_cur[].
2182 * It destroys each deleted domain and builds each new domain.
2183 *
2184 * 'doms_new' is an array of cpumask_var_t's of length 'ndoms_new'.
2185 * The masks don't intersect (don't overlap.) We should setup one
2186 * sched domain for each mask. CPUs not in any of the cpumasks will
2187 * not be load balanced. If the same cpumask appears both in the
2188 * current 'doms_cur' domains and in the new 'doms_new', we can leave
2189 * it as it is.
2190 *
2191 * The passed in 'doms_new' should be allocated using
2192 * alloc_sched_domains. This routine takes ownership of it and will
2193 * free_sched_domains it when done with it. If the caller failed the
2194 * alloc call, then it can pass in doms_new == NULL && ndoms_new == 1,
2195 * and partition_sched_domains() will fallback to the single partition
2196 * 'fallback_doms', it also forces the domains to be rebuilt.
2197 *
2198 * If doms_new == NULL it will be replaced with cpu_online_mask.
2199 * ndoms_new == 0 is a special case for destroying existing domains,
2200 * and it will not create the default domain.
2201 *
Mathieu Poirierc22645f2019-07-19 15:59:53 +02002202 * Call with hotplug lock and sched_domains_mutex held
Ingo Molnarf2cb1362017-02-01 13:10:18 +01002203 */
Mathieu Poirierc22645f2019-07-19 15:59:53 +02002204void partition_sched_domains_locked(int ndoms_new, cpumask_var_t doms_new[],
2205 struct sched_domain_attr *dattr_new)
Ingo Molnarf2cb1362017-02-01 13:10:18 +01002206{
Quentin Perret1f74de82018-12-03 09:56:22 +00002207 bool __maybe_unused has_eas = false;
Ingo Molnarf2cb1362017-02-01 13:10:18 +01002208 int i, j, n;
2209 int new_topology;
2210
Mathieu Poirierc22645f2019-07-19 15:59:53 +02002211 lockdep_assert_held(&sched_domains_mutex);
Ingo Molnarf2cb1362017-02-01 13:10:18 +01002212
2213 /* Always unregister in case we don't destroy any domains: */
2214 unregister_sched_domain_sysctl();
2215
2216 /* Let the architecture update CPU core mappings: */
2217 new_topology = arch_update_cpu_topology();
2218
Peter Zijlstra09e0dd82017-08-08 12:16:24 +02002219 if (!doms_new) {
2220 WARN_ON_ONCE(dattr_new);
2221 n = 0;
2222 doms_new = alloc_sched_domains(1);
2223 if (doms_new) {
2224 n = 1;
Frederic Weisbeckeredb93822017-10-27 04:42:37 +02002225 cpumask_and(doms_new[0], cpu_active_mask,
2226 housekeeping_cpumask(HK_FLAG_DOMAIN));
Peter Zijlstra09e0dd82017-08-08 12:16:24 +02002227 }
2228 } else {
2229 n = ndoms_new;
2230 }
Ingo Molnarf2cb1362017-02-01 13:10:18 +01002231
2232 /* Destroy deleted domains: */
2233 for (i = 0; i < ndoms_cur; i++) {
2234 for (j = 0; j < n && !new_topology; j++) {
Quentin Perret6aa140f2018-12-03 09:56:18 +00002235 if (cpumask_equal(doms_cur[i], doms_new[j]) &&
Mathieu Poirierf9a25f72019-07-19 15:59:55 +02002236 dattrs_equal(dattr_cur, i, dattr_new, j)) {
2237 struct root_domain *rd;
2238
2239 /*
2240 * This domain won't be destroyed and as such
2241 * its dl_bw->total_bw needs to be cleared. It
2242 * will be recomputed in function
2243 * update_tasks_root_domain().
2244 */
2245 rd = cpu_rq(cpumask_any(doms_cur[i]))->rd;
2246 dl_clear_root_domain(rd);
Ingo Molnarf2cb1362017-02-01 13:10:18 +01002247 goto match1;
Mathieu Poirierf9a25f72019-07-19 15:59:55 +02002248 }
Ingo Molnarf2cb1362017-02-01 13:10:18 +01002249 }
2250 /* No match - a current sched domain not in new doms_new[] */
2251 detach_destroy_domains(doms_cur[i]);
2252match1:
2253 ;
2254 }
2255
2256 n = ndoms_cur;
Peter Zijlstra09e0dd82017-08-08 12:16:24 +02002257 if (!doms_new) {
Ingo Molnarf2cb1362017-02-01 13:10:18 +01002258 n = 0;
2259 doms_new = &fallback_doms;
Frederic Weisbeckeredb93822017-10-27 04:42:37 +02002260 cpumask_and(doms_new[0], cpu_active_mask,
2261 housekeeping_cpumask(HK_FLAG_DOMAIN));
Ingo Molnarf2cb1362017-02-01 13:10:18 +01002262 }
2263
2264 /* Build new domains: */
2265 for (i = 0; i < ndoms_new; i++) {
2266 for (j = 0; j < n && !new_topology; j++) {
Quentin Perret6aa140f2018-12-03 09:56:18 +00002267 if (cpumask_equal(doms_new[i], doms_cur[j]) &&
2268 dattrs_equal(dattr_new, i, dattr_cur, j))
Ingo Molnarf2cb1362017-02-01 13:10:18 +01002269 goto match2;
2270 }
2271 /* No match - add a new doms_new */
2272 build_sched_domains(doms_new[i], dattr_new ? dattr_new + i : NULL);
2273match2:
2274 ;
2275 }
2276
Quentin Perret531b5c92018-12-03 09:56:21 +00002277#if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
Quentin Perret6aa140f2018-12-03 09:56:18 +00002278 /* Build perf. domains: */
2279 for (i = 0; i < ndoms_new; i++) {
Quentin Perret531b5c92018-12-03 09:56:21 +00002280 for (j = 0; j < n && !sched_energy_update; j++) {
Quentin Perret6aa140f2018-12-03 09:56:18 +00002281 if (cpumask_equal(doms_new[i], doms_cur[j]) &&
Quentin Perret1f74de82018-12-03 09:56:22 +00002282 cpu_rq(cpumask_first(doms_cur[j]))->rd->pd) {
2283 has_eas = true;
Quentin Perret6aa140f2018-12-03 09:56:18 +00002284 goto match3;
Quentin Perret1f74de82018-12-03 09:56:22 +00002285 }
Quentin Perret6aa140f2018-12-03 09:56:18 +00002286 }
2287 /* No match - add perf. domains for a new rd */
Quentin Perret1f74de82018-12-03 09:56:22 +00002288 has_eas |= build_perf_domains(doms_new[i]);
Quentin Perret6aa140f2018-12-03 09:56:18 +00002289match3:
2290 ;
2291 }
Quentin Perret1f74de82018-12-03 09:56:22 +00002292 sched_energy_set(has_eas);
Quentin Perret6aa140f2018-12-03 09:56:18 +00002293#endif
2294
Ingo Molnarf2cb1362017-02-01 13:10:18 +01002295 /* Remember the new sched domains: */
2296 if (doms_cur != &fallback_doms)
2297 free_sched_domains(doms_cur, ndoms_cur);
2298
2299 kfree(dattr_cur);
2300 doms_cur = doms_new;
2301 dattr_cur = dattr_new;
2302 ndoms_cur = ndoms_new;
2303
2304 register_sched_domain_sysctl();
Mathieu Poirierc22645f2019-07-19 15:59:53 +02002305}
Ingo Molnarf2cb1362017-02-01 13:10:18 +01002306
Mathieu Poirierc22645f2019-07-19 15:59:53 +02002307/*
2308 * Call with hotplug lock held
2309 */
2310void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
2311 struct sched_domain_attr *dattr_new)
2312{
2313 mutex_lock(&sched_domains_mutex);
2314 partition_sched_domains_locked(ndoms_new, doms_new, dattr_new);
Ingo Molnarf2cb1362017-02-01 13:10:18 +01002315 mutex_unlock(&sched_domains_mutex);
2316}