1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Scheduler topology setup/handling methods
4 */
5 #include "sched.h"
6
7 DEFINE_MUTEX(sched_domains_mutex);
8
9 /* Protected by sched_domains_mutex: */
10 static cpumask_var_t sched_domains_tmpmask;
11 static cpumask_var_t sched_domains_tmpmask2;
12
13 #ifdef CONFIG_SCHED_DEBUG
14
sched_debug_setup(char * str)15 static int __init sched_debug_setup(char *str)
16 {
17 sched_debug_enabled = true;
18
19 return 0;
20 }
21 early_param("sched_debug", sched_debug_setup);
22
sched_debug(void)23 static inline bool sched_debug(void)
24 {
25 return sched_debug_enabled;
26 }
27
28 #define SD_FLAG(_name, mflags) [__##_name] = { .meta_flags = mflags, .name = #_name },
29 const struct sd_flag_debug sd_flag_debug[] = {
30 #include <linux/sched/sd_flags.h>
31 };
32 #undef SD_FLAG
33
sched_domain_debug_one(struct sched_domain * sd,int cpu,int level,struct cpumask * groupmask)34 static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
35 struct cpumask *groupmask)
36 {
37 struct sched_group *group = sd->groups;
38 unsigned long flags = sd->flags;
39 unsigned int idx;
40
41 cpumask_clear(groupmask);
42
43 printk(KERN_DEBUG "%*s domain-%d: ", level, "", level);
44 printk(KERN_CONT "span=%*pbl level=%s\n",
45 cpumask_pr_args(sched_domain_span(sd)), sd->name);
46
47 if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) {
48 printk(KERN_ERR "ERROR: domain->span does not contain CPU%d\n", cpu);
49 }
50 if (group && !cpumask_test_cpu(cpu, sched_group_span(group))) {
51 printk(KERN_ERR "ERROR: domain->groups does not contain CPU%d\n", cpu);
52 }
53
54 for_each_set_bit(idx, &flags, __SD_FLAG_CNT) {
55 unsigned int flag = BIT(idx);
56 unsigned int meta_flags = sd_flag_debug[idx].meta_flags;
57
58 if ((meta_flags & SDF_SHARED_CHILD) && sd->child &&
59 !(sd->child->flags & flag))
60 printk(KERN_ERR "ERROR: flag %s set here but not in child\n",
61 sd_flag_debug[idx].name);
62
63 if ((meta_flags & SDF_SHARED_PARENT) && sd->parent &&
64 !(sd->parent->flags & flag))
65 printk(KERN_ERR "ERROR: flag %s set here but not in parent\n",
66 sd_flag_debug[idx].name);
67 }
68
69 printk(KERN_DEBUG "%*s groups:", level + 1, "");
70 do {
71 if (!group) {
72 printk("\n");
73 printk(KERN_ERR "ERROR: group is NULL\n");
74 break;
75 }
76
77 if (!cpumask_weight(sched_group_span(group))) {
78 printk(KERN_CONT "\n");
79 printk(KERN_ERR "ERROR: empty group\n");
80 break;
81 }
82
83 if (!(sd->flags & SD_OVERLAP) &&
84 cpumask_intersects(groupmask, sched_group_span(group))) {
85 printk(KERN_CONT "\n");
86 printk(KERN_ERR "ERROR: repeated CPUs\n");
87 break;
88 }
89
90 cpumask_or(groupmask, groupmask, sched_group_span(group));
91
92 printk(KERN_CONT " %d:{ span=%*pbl",
93 group->sgc->id,
94 cpumask_pr_args(sched_group_span(group)));
95
96 if ((sd->flags & SD_OVERLAP) &&
97 !cpumask_equal(group_balance_mask(group), sched_group_span(group))) {
98 printk(KERN_CONT " mask=%*pbl",
99 cpumask_pr_args(group_balance_mask(group)));
100 }
101
102 if (group->sgc->capacity != SCHED_CAPACITY_SCALE)
103 printk(KERN_CONT " cap=%lu", group->sgc->capacity);
104
105 if (group == sd->groups && sd->child &&
106 !cpumask_equal(sched_domain_span(sd->child),
107 sched_group_span(group))) {
108 printk(KERN_ERR "ERROR: domain->groups does not match domain->child\n");
109 }
110
111 printk(KERN_CONT " }");
112
113 group = group->next;
114
115 if (group != sd->groups)
116 printk(KERN_CONT ",");
117
118 } while (group != sd->groups);
119 printk(KERN_CONT "\n");
120
121 if (!cpumask_equal(sched_domain_span(sd), groupmask))
122 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
123
124 if (sd->parent &&
125 !cpumask_subset(groupmask, sched_domain_span(sd->parent)))
126 printk(KERN_ERR "ERROR: parent span is not a superset of domain->span\n");
127 return 0;
128 }
129
sched_domain_debug(struct sched_domain * sd,int cpu)130 static void sched_domain_debug(struct sched_domain *sd, int cpu)
131 {
132 int level = 0;
133
134 if (!sched_debug_enabled)
135 return;
136
137 if (!sd) {
138 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
139 return;
140 }
141
142 printk(KERN_DEBUG "CPU%d attaching sched-domain(s):\n", cpu);
143
144 for (;;) {
145 if (sched_domain_debug_one(sd, cpu, level, sched_domains_tmpmask))
146 break;
147 level++;
148 sd = sd->parent;
149 if (!sd)
150 break;
151 }
152 }
153 #else /* !CONFIG_SCHED_DEBUG */
154
155 # define sched_debug_enabled 0
156 # define sched_domain_debug(sd, cpu) do { } while (0)
sched_debug(void)157 static inline bool sched_debug(void)
158 {
159 return false;
160 }
161 #endif /* CONFIG_SCHED_DEBUG */
162
163 /* Generate a mask of SD flags with the SDF_NEEDS_GROUPS metaflag */
164 #define SD_FLAG(name, mflags) (name * !!((mflags) & SDF_NEEDS_GROUPS)) |
165 static const unsigned int SD_DEGENERATE_GROUPS_MASK =
166 #include <linux/sched/sd_flags.h>
167 0;
168 #undef SD_FLAG
169
sd_degenerate(struct sched_domain * sd)170 static int sd_degenerate(struct sched_domain *sd)
171 {
172 if (cpumask_weight(sched_domain_span(sd)) == 1)
173 return 1;
174
175 /* Following flags need at least 2 groups */
176 if ((sd->flags & SD_DEGENERATE_GROUPS_MASK) &&
177 (sd->groups != sd->groups->next))
178 return 0;
179
180 /* Following flags don't use groups */
181 if (sd->flags & (SD_WAKE_AFFINE))
182 return 0;
183
184 return 1;
185 }
186
187 static int
sd_parent_degenerate(struct sched_domain * sd,struct sched_domain * parent)188 sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
189 {
190 unsigned long cflags = sd->flags, pflags = parent->flags;
191
192 if (sd_degenerate(parent))
193 return 1;
194
195 if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent)))
196 return 0;
197
198 /* Flags needing groups don't count if only 1 group in parent */
199 if (parent->groups == parent->groups->next)
200 pflags &= ~SD_DEGENERATE_GROUPS_MASK;
201
202 if (~cflags & pflags)
203 return 0;
204
205 return 1;
206 }
207
208 #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
209 DEFINE_STATIC_KEY_FALSE(sched_energy_present);
210 unsigned int sysctl_sched_energy_aware = 1;
211 DEFINE_MUTEX(sched_energy_mutex);
212 bool sched_energy_update;
213
214 #ifdef CONFIG_PROC_SYSCTL
sched_energy_aware_handler(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)215 int sched_energy_aware_handler(struct ctl_table *table, int write,
216 void *buffer, size_t *lenp, loff_t *ppos)
217 {
218 int ret, state;
219
220 if (write && !capable(CAP_SYS_ADMIN))
221 return -EPERM;
222
223 ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
224 if (!ret && write) {
225 state = static_branch_unlikely(&sched_energy_present);
226 if (state != sysctl_sched_energy_aware) {
227 mutex_lock(&sched_energy_mutex);
228 sched_energy_update = 1;
229 rebuild_sched_domains();
230 sched_energy_update = 0;
231 mutex_unlock(&sched_energy_mutex);
232 }
233 }
234
235 return ret;
236 }
237 #endif
238
free_pd(struct perf_domain * pd)239 static void free_pd(struct perf_domain *pd)
240 {
241 struct perf_domain *tmp;
242
243 while (pd) {
244 tmp = pd->next;
245 kfree(pd);
246 pd = tmp;
247 }
248 }
249
find_pd(struct perf_domain * pd,int cpu)250 static struct perf_domain *find_pd(struct perf_domain *pd, int cpu)
251 {
252 while (pd) {
253 if (cpumask_test_cpu(cpu, perf_domain_span(pd)))
254 return pd;
255 pd = pd->next;
256 }
257
258 return NULL;
259 }
260
pd_init(int cpu)261 static struct perf_domain *pd_init(int cpu)
262 {
263 struct em_perf_domain *obj = em_cpu_get(cpu);
264 struct perf_domain *pd;
265
266 if (!obj) {
267 if (sched_debug())
268 pr_info("%s: no EM found for CPU%d\n", __func__, cpu);
269 return NULL;
270 }
271
272 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
273 if (!pd)
274 return NULL;
275 pd->em_pd = obj;
276
277 return pd;
278 }
279
perf_domain_debug(const struct cpumask * cpu_map,struct perf_domain * pd)280 static void perf_domain_debug(const struct cpumask *cpu_map,
281 struct perf_domain *pd)
282 {
283 if (!sched_debug() || !pd)
284 return;
285
286 printk(KERN_DEBUG "root_domain %*pbl:", cpumask_pr_args(cpu_map));
287
288 while (pd) {
289 printk(KERN_CONT " pd%d:{ cpus=%*pbl nr_pstate=%d }",
290 cpumask_first(perf_domain_span(pd)),
291 cpumask_pr_args(perf_domain_span(pd)),
292 em_pd_nr_perf_states(pd->em_pd));
293 pd = pd->next;
294 }
295
296 printk(KERN_CONT "\n");
297 }
298
destroy_perf_domain_rcu(struct rcu_head * rp)299 static void destroy_perf_domain_rcu(struct rcu_head *rp)
300 {
301 struct perf_domain *pd;
302
303 pd = container_of(rp, struct perf_domain, rcu);
304 free_pd(pd);
305 }
306
sched_energy_set(bool has_eas)307 static void sched_energy_set(bool has_eas)
308 {
309 if (!has_eas && static_branch_unlikely(&sched_energy_present)) {
310 if (sched_debug())
311 pr_info("%s: stopping EAS\n", __func__);
312 static_branch_disable_cpuslocked(&sched_energy_present);
313 } else if (has_eas && !static_branch_unlikely(&sched_energy_present)) {
314 if (sched_debug())
315 pr_info("%s: starting EAS\n", __func__);
316 static_branch_enable_cpuslocked(&sched_energy_present);
317 }
318 }
319
320 /*
321 * EAS can be used on a root domain if it meets all the following conditions:
322 * 1. an Energy Model (EM) is available;
323 * 2. the SD_ASYM_CPUCAPACITY flag is set in the sched_domain hierarchy.
324 * 3. no SMT is detected.
325 * 4. the EM complexity is low enough to keep scheduling overheads low;
326 * 5. schedutil is driving the frequency of all CPUs of the rd;
327 *
328 * The complexity of the Energy Model is defined as:
329 *
330 * C = nr_pd * (nr_cpus + nr_ps)
331 *
332 * with parameters defined as:
333 * - nr_pd: the number of performance domains
334 * - nr_cpus: the number of CPUs
335 * - nr_ps: the sum of the number of performance states of all performance
336 * domains (for example, on a system with 2 performance domains,
337 * with 10 performance states each, nr_ps = 2 * 10 = 20).
338 *
339 * It is generally not a good idea to use such a model in the wake-up path on
340 * very complex platforms because of the associated scheduling overheads. The
341 * arbitrary constraint below prevents that. It makes EAS usable up to 16 CPUs
342 * with per-CPU DVFS and less than 8 performance states each, for example.
343 */
344 #define EM_MAX_COMPLEXITY 2048
345
346 extern struct cpufreq_governor schedutil_gov;
build_perf_domains(const struct cpumask * cpu_map)347 static bool build_perf_domains(const struct cpumask *cpu_map)
348 {
349 int i, nr_pd = 0, nr_ps = 0, nr_cpus = cpumask_weight(cpu_map);
350 struct perf_domain *pd = NULL, *tmp;
351 int cpu = cpumask_first(cpu_map);
352 struct root_domain *rd = cpu_rq(cpu)->rd;
353 struct cpufreq_policy *policy;
354 struct cpufreq_governor *gov;
355
356 if (!sysctl_sched_energy_aware)
357 goto free;
358
359 /* EAS is enabled for asymmetric CPU capacity topologies. */
360 if (!per_cpu(sd_asym_cpucapacity, cpu)) {
361 if (sched_debug()) {
362 pr_info("rd %*pbl: CPUs do not have asymmetric capacities\n",
363 cpumask_pr_args(cpu_map));
364 }
365 goto free;
366 }
367
368 /* EAS definitely does *not* handle SMT */
369 if (sched_smt_active()) {
370 pr_warn("rd %*pbl: Disabling EAS, SMT is not supported\n",
371 cpumask_pr_args(cpu_map));
372 goto free;
373 }
374
375 for_each_cpu(i, cpu_map) {
376 /* Skip already covered CPUs. */
377 if (find_pd(pd, i))
378 continue;
379
380 /* Do not attempt EAS if schedutil is not being used. */
381 policy = cpufreq_cpu_get(i);
382 if (!policy)
383 goto free;
384 gov = policy->governor;
385 cpufreq_cpu_put(policy);
386 if (gov != &schedutil_gov) {
387 if (rd->pd)
388 pr_warn("rd %*pbl: Disabling EAS, schedutil is mandatory\n",
389 cpumask_pr_args(cpu_map));
390 goto free;
391 }
392
393 /* Create the new pd and add it to the local list. */
394 tmp = pd_init(i);
395 if (!tmp)
396 goto free;
397 tmp->next = pd;
398 pd = tmp;
399
400 /*
401 * Count performance domains and performance states for the
402 * complexity check.
403 */
404 nr_pd++;
405 nr_ps += em_pd_nr_perf_states(pd->em_pd);
406 }
407
408 /* Bail out if the Energy Model complexity is too high. */
409 if (nr_pd * (nr_ps + nr_cpus) > EM_MAX_COMPLEXITY) {
410 WARN(1, "rd %*pbl: Failed to start EAS, EM complexity is too high\n",
411 cpumask_pr_args(cpu_map));
412 goto free;
413 }
414
415 perf_domain_debug(cpu_map, pd);
416
417 /* Attach the new list of performance domains to the root domain. */
418 tmp = rd->pd;
419 rcu_assign_pointer(rd->pd, pd);
420 if (tmp)
421 call_rcu(&tmp->rcu, destroy_perf_domain_rcu);
422
423 return !!pd;
424
425 free:
426 free_pd(pd);
427 tmp = rd->pd;
428 rcu_assign_pointer(rd->pd, NULL);
429 if (tmp)
430 call_rcu(&tmp->rcu, destroy_perf_domain_rcu);
431
432 return false;
433 }
434 #else
free_pd(struct perf_domain * pd)435 static void free_pd(struct perf_domain *pd) { }
436 #endif /* CONFIG_ENERGY_MODEL && CONFIG_CPU_FREQ_GOV_SCHEDUTIL*/
437
free_rootdomain(struct rcu_head * rcu)438 static void free_rootdomain(struct rcu_head *rcu)
439 {
440 struct root_domain *rd = container_of(rcu, struct root_domain, rcu);
441
442 cpupri_cleanup(&rd->cpupri);
443 cpudl_cleanup(&rd->cpudl);
444 free_cpumask_var(rd->dlo_mask);
445 free_cpumask_var(rd->rto_mask);
446 free_cpumask_var(rd->online);
447 free_cpumask_var(rd->span);
448 free_pd(rd->pd);
449 kfree(rd);
450 }
451
rq_attach_root(struct rq * rq,struct root_domain * rd)452 void rq_attach_root(struct rq *rq, struct root_domain *rd)
453 {
454 struct root_domain *old_rd = NULL;
455 unsigned long flags;
456
457 raw_spin_lock_irqsave(&rq->lock, flags);
458
459 if (rq->rd) {
460 old_rd = rq->rd;
461
462 if (cpumask_test_cpu(rq->cpu, old_rd->online))
463 set_rq_offline(rq);
464
465 cpumask_clear_cpu(rq->cpu, old_rd->span);
466
467 /*
468 * If we dont want to free the old_rd yet then
469 * set old_rd to NULL to skip the freeing later
470 * in this function:
471 */
472 if (!atomic_dec_and_test(&old_rd->refcount))
473 old_rd = NULL;
474 }
475
476 atomic_inc(&rd->refcount);
477 rq->rd = rd;
478
479 cpumask_set_cpu(rq->cpu, rd->span);
480 if (cpumask_test_cpu(rq->cpu, cpu_active_mask))
481 set_rq_online(rq);
482
483 raw_spin_unlock_irqrestore(&rq->lock, flags);
484
485 if (old_rd)
486 call_rcu(&old_rd->rcu, free_rootdomain);
487 }
488
sched_get_rd(struct root_domain * rd)489 void sched_get_rd(struct root_domain *rd)
490 {
491 atomic_inc(&rd->refcount);
492 }
493
sched_put_rd(struct root_domain * rd)494 void sched_put_rd(struct root_domain *rd)
495 {
496 if (!atomic_dec_and_test(&rd->refcount))
497 return;
498
499 call_rcu(&rd->rcu, free_rootdomain);
500 }
501
init_rootdomain(struct root_domain * rd)502 static int init_rootdomain(struct root_domain *rd)
503 {
504 if (!zalloc_cpumask_var(&rd->span, GFP_KERNEL))
505 goto out;
506 if (!zalloc_cpumask_var(&rd->online, GFP_KERNEL))
507 goto free_span;
508 if (!zalloc_cpumask_var(&rd->dlo_mask, GFP_KERNEL))
509 goto free_online;
510 if (!zalloc_cpumask_var(&rd->rto_mask, GFP_KERNEL))
511 goto free_dlo_mask;
512
513 #ifdef HAVE_RT_PUSH_IPI
514 rd->rto_cpu = -1;
515 raw_spin_lock_init(&rd->rto_lock);
516 init_irq_work(&rd->rto_push_work, rto_push_irq_work_func);
517 #endif
518
519 init_dl_bw(&rd->dl_bw);
520 if (cpudl_init(&rd->cpudl) != 0)
521 goto free_rto_mask;
522
523 if (cpupri_init(&rd->cpupri) != 0)
524 goto free_cpudl;
525
526 #ifdef CONFIG_SCHED_RT_CAS
527 rd->max_cap_orig_cpu = -1;
528 #endif
529 return 0;
530
531 free_cpudl:
532 cpudl_cleanup(&rd->cpudl);
533 free_rto_mask:
534 free_cpumask_var(rd->rto_mask);
535 free_dlo_mask:
536 free_cpumask_var(rd->dlo_mask);
537 free_online:
538 free_cpumask_var(rd->online);
539 free_span:
540 free_cpumask_var(rd->span);
541 out:
542 return -ENOMEM;
543 }
544
545 /*
546 * By default the system creates a single root-domain with all CPUs as
547 * members (mimicking the global state we have today).
548 */
549 struct root_domain def_root_domain;
550
init_defrootdomain(void)551 void init_defrootdomain(void)
552 {
553 init_rootdomain(&def_root_domain);
554
555 atomic_set(&def_root_domain.refcount, 1);
556 }
557
alloc_rootdomain(void)558 static struct root_domain *alloc_rootdomain(void)
559 {
560 struct root_domain *rd;
561
562 rd = kzalloc(sizeof(*rd), GFP_KERNEL);
563 if (!rd)
564 return NULL;
565
566 if (init_rootdomain(rd) != 0) {
567 kfree(rd);
568 return NULL;
569 }
570
571 return rd;
572 }
573
free_sched_groups(struct sched_group * sg,int free_sgc)574 static void free_sched_groups(struct sched_group *sg, int free_sgc)
575 {
576 struct sched_group *tmp, *first;
577
578 if (!sg)
579 return;
580
581 first = sg;
582 do {
583 tmp = sg->next;
584
585 if (free_sgc && atomic_dec_and_test(&sg->sgc->ref))
586 kfree(sg->sgc);
587
588 if (atomic_dec_and_test(&sg->ref))
589 kfree(sg);
590 sg = tmp;
591 } while (sg != first);
592 }
593
destroy_sched_domain(struct sched_domain * sd)594 static void destroy_sched_domain(struct sched_domain *sd)
595 {
596 /*
597 * A normal sched domain may have multiple group references, an
598 * overlapping domain, having private groups, only one. Iterate,
599 * dropping group/capacity references, freeing where none remain.
600 */
601 free_sched_groups(sd->groups, 1);
602
603 if (sd->shared && atomic_dec_and_test(&sd->shared->ref))
604 kfree(sd->shared);
605 kfree(sd);
606 }
607
destroy_sched_domains_rcu(struct rcu_head * rcu)608 static void destroy_sched_domains_rcu(struct rcu_head *rcu)
609 {
610 struct sched_domain *sd = container_of(rcu, struct sched_domain, rcu);
611
612 while (sd) {
613 struct sched_domain *parent = sd->parent;
614 destroy_sched_domain(sd);
615 sd = parent;
616 }
617 }
618
destroy_sched_domains(struct sched_domain * sd)619 static void destroy_sched_domains(struct sched_domain *sd)
620 {
621 if (sd)
622 call_rcu(&sd->rcu, destroy_sched_domains_rcu);
623 }
624
625 /*
626 * Keep a special pointer to the highest sched_domain that has
627 * SD_SHARE_PKG_RESOURCE set (Last Level Cache Domain) for this
628 * allows us to avoid some pointer chasing select_idle_sibling().
629 *
630 * Also keep a unique ID per domain (we use the first CPU number in
631 * the cpumask of the domain), this allows us to quickly tell if
632 * two CPUs are in the same cache domain, see cpus_share_cache().
633 */
634 DEFINE_PER_CPU(struct sched_domain __rcu *, sd_llc);
635 DEFINE_PER_CPU(int, sd_llc_size);
636 DEFINE_PER_CPU(int, sd_llc_id);
637 DEFINE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared);
638 DEFINE_PER_CPU(struct sched_domain __rcu *, sd_numa);
639 DEFINE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing);
640 DEFINE_PER_CPU(struct sched_domain __rcu *, sd_asym_cpucapacity);
641 DEFINE_STATIC_KEY_FALSE(sched_asym_cpucapacity);
642
update_top_cache_domain(int cpu)643 static void update_top_cache_domain(int cpu)
644 {
645 struct sched_domain_shared *sds = NULL;
646 struct sched_domain *sd;
647 int id = cpu;
648 int size = 1;
649
650 sd = highest_flag_domain(cpu, SD_SHARE_PKG_RESOURCES);
651 if (sd) {
652 id = cpumask_first(sched_domain_span(sd));
653 size = cpumask_weight(sched_domain_span(sd));
654 sds = sd->shared;
655 }
656
657 rcu_assign_pointer(per_cpu(sd_llc, cpu), sd);
658 per_cpu(sd_llc_size, cpu) = size;
659 per_cpu(sd_llc_id, cpu) = id;
660 rcu_assign_pointer(per_cpu(sd_llc_shared, cpu), sds);
661
662 sd = lowest_flag_domain(cpu, SD_NUMA);
663 rcu_assign_pointer(per_cpu(sd_numa, cpu), sd);
664
665 sd = highest_flag_domain(cpu, SD_ASYM_PACKING);
666 rcu_assign_pointer(per_cpu(sd_asym_packing, cpu), sd);
667
668 sd = lowest_flag_domain(cpu, SD_ASYM_CPUCAPACITY);
669 rcu_assign_pointer(per_cpu(sd_asym_cpucapacity, cpu), sd);
670 }
671
672 /*
673 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
674 * hold the hotplug lock.
675 */
676 static void
cpu_attach_domain(struct sched_domain * sd,struct root_domain * rd,int cpu)677 cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
678 {
679 struct rq *rq = cpu_rq(cpu);
680 struct sched_domain *tmp;
681 int numa_distance = 0;
682
683 /* Remove the sched domains which do not contribute to scheduling. */
684 for (tmp = sd; tmp; ) {
685 struct sched_domain *parent = tmp->parent;
686 if (!parent)
687 break;
688
689 if (sd_parent_degenerate(tmp, parent)) {
690 tmp->parent = parent->parent;
691 if (parent->parent)
692 parent->parent->child = tmp;
693 /*
694 * Transfer SD_PREFER_SIBLING down in case of a
695 * degenerate parent; the spans match for this
696 * so the property transfers.
697 */
698 if (parent->flags & SD_PREFER_SIBLING)
699 tmp->flags |= SD_PREFER_SIBLING;
700 destroy_sched_domain(parent);
701 } else
702 tmp = tmp->parent;
703 }
704
705 if (sd && sd_degenerate(sd)) {
706 tmp = sd;
707 sd = sd->parent;
708 destroy_sched_domain(tmp);
709 if (sd)
710 sd->child = NULL;
711 }
712
713 for (tmp = sd; tmp; tmp = tmp->parent)
714 numa_distance += !!(tmp->flags & SD_NUMA);
715
716 sched_domain_debug(sd, cpu);
717
718 rq_attach_root(rq, rd);
719 tmp = rq->sd;
720 rcu_assign_pointer(rq->sd, sd);
721 dirty_sched_domain_sysctl(cpu);
722 destroy_sched_domains(tmp);
723
724 update_top_cache_domain(cpu);
725 }
726
727 struct s_data {
728 struct sched_domain * __percpu *sd;
729 struct root_domain *rd;
730 };
731
732 enum s_alloc {
733 sa_rootdomain,
734 sa_sd,
735 sa_sd_storage,
736 sa_none,
737 };
738
739 /*
740 * Return the canonical balance CPU for this group, this is the first CPU
741 * of this group that's also in the balance mask.
742 *
743 * The balance mask are all those CPUs that could actually end up at this
744 * group. See build_balance_mask().
745 *
746 * Also see should_we_balance().
747 */
group_balance_cpu(struct sched_group * sg)748 int group_balance_cpu(struct sched_group *sg)
749 {
750 return cpumask_first(group_balance_mask(sg));
751 }
752
753
754 /*
755 * NUMA topology (first read the regular topology blurb below)
756 *
757 * Given a node-distance table, for example:
758 *
759 * node 0 1 2 3
760 * 0: 10 20 30 20
761 * 1: 20 10 20 30
762 * 2: 30 20 10 20
763 * 3: 20 30 20 10
764 *
765 * which represents a 4 node ring topology like:
766 *
767 * 0 ----- 1
768 * | |
769 * | |
770 * | |
771 * 3 ----- 2
772 *
773 * We want to construct domains and groups to represent this. The way we go
774 * about doing this is to build the domains on 'hops'. For each NUMA level we
775 * construct the mask of all nodes reachable in @level hops.
776 *
777 * For the above NUMA topology that gives 3 levels:
778 *
779 * NUMA-2 0-3 0-3 0-3 0-3
780 * groups: {0-1,3},{1-3} {0-2},{0,2-3} {1-3},{0-1,3} {0,2-3},{0-2}
781 *
782 * NUMA-1 0-1,3 0-2 1-3 0,2-3
783 * groups: {0},{1},{3} {0},{1},{2} {1},{2},{3} {0},{2},{3}
784 *
785 * NUMA-0 0 1 2 3
786 *
787 *
788 * As can be seen; things don't nicely line up as with the regular topology.
789 * When we iterate a domain in child domain chunks some nodes can be
790 * represented multiple times -- hence the "overlap" naming for this part of
791 * the topology.
792 *
793 * In order to minimize this overlap, we only build enough groups to cover the
794 * domain. For instance Node-0 NUMA-2 would only get groups: 0-1,3 and 1-3.
795 *
796 * Because:
797 *
798 * - the first group of each domain is its child domain; this
799 * gets us the first 0-1,3
800 * - the only uncovered node is 2, who's child domain is 1-3.
801 *
802 * However, because of the overlap, computing a unique CPU for each group is
803 * more complicated. Consider for instance the groups of NODE-1 NUMA-2, both
804 * groups include the CPUs of Node-0, while those CPUs would not in fact ever
805 * end up at those groups (they would end up in group: 0-1,3).
806 *
807 * To correct this we have to introduce the group balance mask. This mask
808 * will contain those CPUs in the group that can reach this group given the
809 * (child) domain tree.
810 *
811 * With this we can once again compute balance_cpu and sched_group_capacity
812 * relations.
813 *
814 * XXX include words on how balance_cpu is unique and therefore can be
815 * used for sched_group_capacity links.
816 *
817 *
818 * Another 'interesting' topology is:
819 *
820 * node 0 1 2 3
821 * 0: 10 20 20 30
822 * 1: 20 10 20 20
823 * 2: 20 20 10 20
824 * 3: 30 20 20 10
825 *
826 * Which looks a little like:
827 *
828 * 0 ----- 1
829 * | / |
830 * | / |
831 * | / |
832 * 2 ----- 3
833 *
834 * This topology is asymmetric, nodes 1,2 are fully connected, but nodes 0,3
835 * are not.
836 *
837 * This leads to a few particularly weird cases where the sched_domain's are
838 * not of the same number for each CPU. Consider:
839 *
840 * NUMA-2 0-3 0-3
841 * groups: {0-2},{1-3} {1-3},{0-2}
842 *
843 * NUMA-1 0-2 0-3 0-3 1-3
844 *
845 * NUMA-0 0 1 2 3
846 *
847 */
848
849
850 /*
851 * Build the balance mask; it contains only those CPUs that can arrive at this
852 * group and should be considered to continue balancing.
853 *
854 * We do this during the group creation pass, therefore the group information
855 * isn't complete yet, however since each group represents a (child) domain we
856 * can fully construct this using the sched_domain bits (which are already
857 * complete).
858 */
859 static void
build_balance_mask(struct sched_domain * sd,struct sched_group * sg,struct cpumask * mask)860 build_balance_mask(struct sched_domain *sd, struct sched_group *sg, struct cpumask *mask)
861 {
862 const struct cpumask *sg_span = sched_group_span(sg);
863 struct sd_data *sdd = sd->private;
864 struct sched_domain *sibling;
865 int i;
866
867 cpumask_clear(mask);
868
869 for_each_cpu(i, sg_span) {
870 sibling = *per_cpu_ptr(sdd->sd, i);
871
872 /*
873 * Can happen in the asymmetric case, where these siblings are
874 * unused. The mask will not be empty because those CPUs that
875 * do have the top domain _should_ span the domain.
876 */
877 if (!sibling->child)
878 continue;
879
880 /* If we would not end up here, we can't continue from here */
881 if (!cpumask_equal(sg_span, sched_domain_span(sibling->child)))
882 continue;
883
884 cpumask_set_cpu(i, mask);
885 }
886
887 /* We must not have empty masks here */
888 WARN_ON_ONCE(cpumask_empty(mask));
889 }
890
891 /*
892 * XXX: This creates per-node group entries; since the load-balancer will
893 * immediately access remote memory to construct this group's load-balance
894 * statistics having the groups node local is of dubious benefit.
895 */
896 static struct sched_group *
build_group_from_child_sched_domain(struct sched_domain * sd,int cpu)897 build_group_from_child_sched_domain(struct sched_domain *sd, int cpu)
898 {
899 struct sched_group *sg;
900 struct cpumask *sg_span;
901
902 sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
903 GFP_KERNEL, cpu_to_node(cpu));
904
905 if (!sg)
906 return NULL;
907
908 sg_span = sched_group_span(sg);
909 if (sd->child)
910 cpumask_copy(sg_span, sched_domain_span(sd->child));
911 else
912 cpumask_copy(sg_span, sched_domain_span(sd));
913
914 atomic_inc(&sg->ref);
915 return sg;
916 }
917
init_overlap_sched_group(struct sched_domain * sd,struct sched_group * sg)918 static void init_overlap_sched_group(struct sched_domain *sd,
919 struct sched_group *sg)
920 {
921 struct cpumask *mask = sched_domains_tmpmask2;
922 struct sd_data *sdd = sd->private;
923 struct cpumask *sg_span;
924 int cpu;
925
926 build_balance_mask(sd, sg, mask);
927 cpu = cpumask_first_and(sched_group_span(sg), mask);
928
929 sg->sgc = *per_cpu_ptr(sdd->sgc, cpu);
930 if (atomic_inc_return(&sg->sgc->ref) == 1)
931 cpumask_copy(group_balance_mask(sg), mask);
932 else
933 WARN_ON_ONCE(!cpumask_equal(group_balance_mask(sg), mask));
934
935 /*
936 * Initialize sgc->capacity such that even if we mess up the
937 * domains and no possible iteration will get us here, we won't
938 * die on a /0 trap.
939 */
940 sg_span = sched_group_span(sg);
941 sg->sgc->capacity = SCHED_CAPACITY_SCALE * cpumask_weight(sg_span);
942 sg->sgc->min_capacity = SCHED_CAPACITY_SCALE;
943 sg->sgc->max_capacity = SCHED_CAPACITY_SCALE;
944 }
945
946 static struct sched_domain *
find_descended_sibling(struct sched_domain * sd,struct sched_domain * sibling)947 find_descended_sibling(struct sched_domain *sd, struct sched_domain *sibling)
948 {
949 /*
950 * The proper descendant would be the one whose child won't span out
951 * of sd
952 */
953 while (sibling->child &&
954 !cpumask_subset(sched_domain_span(sibling->child),
955 sched_domain_span(sd)))
956 sibling = sibling->child;
957
958 /*
959 * As we are referencing sgc across different topology level, we need
960 * to go down to skip those sched_domains which don't contribute to
961 * scheduling because they will be degenerated in cpu_attach_domain
962 */
963 while (sibling->child &&
964 cpumask_equal(sched_domain_span(sibling->child),
965 sched_domain_span(sibling)))
966 sibling = sibling->child;
967
968 return sibling;
969 }
970
971 static int
build_overlap_sched_groups(struct sched_domain * sd,int cpu)972 build_overlap_sched_groups(struct sched_domain *sd, int cpu)
973 {
974 struct sched_group *first = NULL, *last = NULL, *sg;
975 const struct cpumask *span = sched_domain_span(sd);
976 struct cpumask *covered = sched_domains_tmpmask;
977 struct sd_data *sdd = sd->private;
978 struct sched_domain *sibling;
979 int i;
980
981 cpumask_clear(covered);
982
983 for_each_cpu_wrap(i, span, cpu) {
984 struct cpumask *sg_span;
985
986 if (cpumask_test_cpu(i, covered))
987 continue;
988
989 sibling = *per_cpu_ptr(sdd->sd, i);
990
991 /*
992 * Asymmetric node setups can result in situations where the
993 * domain tree is of unequal depth, make sure to skip domains
994 * that already cover the entire range.
995 *
996 * In that case build_sched_domains() will have terminated the
997 * iteration early and our sibling sd spans will be empty.
998 * Domains should always include the CPU they're built on, so
999 * check that.
1000 */
1001 if (!cpumask_test_cpu(i, sched_domain_span(sibling)))
1002 continue;
1003
1004 /*
1005 * Usually we build sched_group by sibling's child sched_domain
1006 * But for machines whose NUMA diameter are 3 or above, we move
1007 * to build sched_group by sibling's proper descendant's child
1008 * domain because sibling's child sched_domain will span out of
1009 * the sched_domain being built as below.
1010 *
1011 * Smallest diameter=3 topology is:
1012 *
1013 * node 0 1 2 3
1014 * 0: 10 20 30 40
1015 * 1: 20 10 20 30
1016 * 2: 30 20 10 20
1017 * 3: 40 30 20 10
1018 *
1019 * 0 --- 1 --- 2 --- 3
1020 *
1021 * NUMA-3 0-3 N/A N/A 0-3
1022 * groups: {0-2},{1-3} {1-3},{0-2}
1023 *
1024 * NUMA-2 0-2 0-3 0-3 1-3
1025 * groups: {0-1},{1-3} {0-2},{2-3} {1-3},{0-1} {2-3},{0-2}
1026 *
1027 * NUMA-1 0-1 0-2 1-3 2-3
1028 * groups: {0},{1} {1},{2},{0} {2},{3},{1} {3},{2}
1029 *
1030 * NUMA-0 0 1 2 3
1031 *
1032 * The NUMA-2 groups for nodes 0 and 3 are obviously buggered, as the
1033 * group span isn't a subset of the domain span.
1034 */
1035 if (sibling->child &&
1036 !cpumask_subset(sched_domain_span(sibling->child), span))
1037 sibling = find_descended_sibling(sd, sibling);
1038
1039 sg = build_group_from_child_sched_domain(sibling, cpu);
1040 if (!sg)
1041 goto fail;
1042
1043 sg_span = sched_group_span(sg);
1044 cpumask_or(covered, covered, sg_span);
1045
1046 init_overlap_sched_group(sibling, sg);
1047
1048 if (!first)
1049 first = sg;
1050 if (last)
1051 last->next = sg;
1052 last = sg;
1053 last->next = first;
1054 }
1055 sd->groups = first;
1056
1057 return 0;
1058
1059 fail:
1060 free_sched_groups(first, 0);
1061
1062 return -ENOMEM;
1063 }
1064
1065
1066 /*
1067 * Package topology (also see the load-balance blurb in fair.c)
1068 *
1069 * The scheduler builds a tree structure to represent a number of important
1070 * topology features. By default (default_topology[]) these include:
1071 *
1072 * - Simultaneous multithreading (SMT)
1073 * - Multi-Core Cache (MC)
1074 * - Package (DIE)
1075 *
1076 * Where the last one more or less denotes everything up to a NUMA node.
1077 *
1078 * The tree consists of 3 primary data structures:
1079 *
1080 * sched_domain -> sched_group -> sched_group_capacity
1081 * ^ ^ ^ ^
1082 * `-' `-'
1083 *
1084 * The sched_domains are per-CPU and have a two way link (parent & child) and
1085 * denote the ever growing mask of CPUs belonging to that level of topology.
1086 *
1087 * Each sched_domain has a circular (double) linked list of sched_group's, each
1088 * denoting the domains of the level below (or individual CPUs in case of the
1089 * first domain level). The sched_group linked by a sched_domain includes the
1090 * CPU of that sched_domain [*].
1091 *
1092 * Take for instance a 2 threaded, 2 core, 2 cache cluster part:
1093 *
1094 * CPU 0 1 2 3 4 5 6 7
1095 *
1096 * DIE [ ]
1097 * MC [ ] [ ]
1098 * SMT [ ] [ ] [ ] [ ]
1099 *
1100 * - or -
1101 *
1102 * DIE 0-7 0-7 0-7 0-7 0-7 0-7 0-7 0-7
1103 * MC 0-3 0-3 0-3 0-3 4-7 4-7 4-7 4-7
1104 * SMT 0-1 0-1 2-3 2-3 4-5 4-5 6-7 6-7
1105 *
1106 * CPU 0 1 2 3 4 5 6 7
1107 *
1108 * One way to think about it is: sched_domain moves you up and down among these
1109 * topology levels, while sched_group moves you sideways through it, at child
1110 * domain granularity.
1111 *
1112 * sched_group_capacity ensures each unique sched_group has shared storage.
1113 *
1114 * There are two related construction problems, both require a CPU that
1115 * uniquely identify each group (for a given domain):
1116 *
1117 * - The first is the balance_cpu (see should_we_balance() and the
1118 * load-balance blub in fair.c); for each group we only want 1 CPU to
1119 * continue balancing at a higher domain.
1120 *
1121 * - The second is the sched_group_capacity; we want all identical groups
1122 * to share a single sched_group_capacity.
1123 *
1124 * Since these topologies are exclusive by construction. That is, its
1125 * impossible for an SMT thread to belong to multiple cores, and cores to
1126 * be part of multiple caches. There is a very clear and unique location
1127 * for each CPU in the hierarchy.
1128 *
1129 * Therefore computing a unique CPU for each group is trivial (the iteration
1130 * mask is redundant and set all 1s; all CPUs in a group will end up at _that_
1131 * group), we can simply pick the first CPU in each group.
1132 *
1133 *
1134 * [*] in other words, the first group of each domain is its child domain.
1135 */
1136
get_group(int cpu,struct sd_data * sdd)1137 static struct sched_group *get_group(int cpu, struct sd_data *sdd)
1138 {
1139 struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu);
1140 struct sched_domain *child = sd->child;
1141 struct sched_group *sg;
1142 bool already_visited;
1143
1144 if (child)
1145 cpu = cpumask_first(sched_domain_span(child));
1146
1147 sg = *per_cpu_ptr(sdd->sg, cpu);
1148 sg->sgc = *per_cpu_ptr(sdd->sgc, cpu);
1149
1150 /* Increase refcounts for claim_allocations: */
1151 already_visited = atomic_inc_return(&sg->ref) > 1;
1152 /* sgc visits should follow a similar trend as sg */
1153 WARN_ON(already_visited != (atomic_inc_return(&sg->sgc->ref) > 1));
1154
1155 /* If we have already visited that group, it's already initialized. */
1156 if (already_visited)
1157 return sg;
1158
1159 if (child) {
1160 cpumask_copy(sched_group_span(sg), sched_domain_span(child));
1161 cpumask_copy(group_balance_mask(sg), sched_group_span(sg));
1162 } else {
1163 cpumask_set_cpu(cpu, sched_group_span(sg));
1164 cpumask_set_cpu(cpu, group_balance_mask(sg));
1165 }
1166
1167 sg->sgc->capacity = SCHED_CAPACITY_SCALE * cpumask_weight(sched_group_span(sg));
1168 sg->sgc->min_capacity = SCHED_CAPACITY_SCALE;
1169 sg->sgc->max_capacity = SCHED_CAPACITY_SCALE;
1170
1171 return sg;
1172 }
1173
1174 /*
1175 * build_sched_groups will build a circular linked list of the groups
1176 * covered by the given span, will set each group's ->cpumask correctly,
1177 * and will initialize their ->sgc.
1178 *
1179 * Assumes the sched_domain tree is fully constructed
1180 */
1181 static int
build_sched_groups(struct sched_domain * sd,int cpu)1182 build_sched_groups(struct sched_domain *sd, int cpu)
1183 {
1184 struct sched_group *first = NULL, *last = NULL;
1185 struct sd_data *sdd = sd->private;
1186 const struct cpumask *span = sched_domain_span(sd);
1187 struct cpumask *covered;
1188 int i;
1189
1190 lockdep_assert_held(&sched_domains_mutex);
1191 covered = sched_domains_tmpmask;
1192
1193 cpumask_clear(covered);
1194
1195 for_each_cpu_wrap(i, span, cpu) {
1196 struct sched_group *sg;
1197
1198 if (cpumask_test_cpu(i, covered))
1199 continue;
1200
1201 sg = get_group(i, sdd);
1202
1203 cpumask_or(covered, covered, sched_group_span(sg));
1204
1205 if (!first)
1206 first = sg;
1207 if (last)
1208 last->next = sg;
1209 last = sg;
1210 }
1211 last->next = first;
1212 sd->groups = first;
1213
1214 return 0;
1215 }
1216
1217 /*
1218 * Initialize sched groups cpu_capacity.
1219 *
1220 * cpu_capacity indicates the capacity of sched group, which is used while
1221 * distributing the load between different sched groups in a sched domain.
1222 * Typically cpu_capacity for all the groups in a sched domain will be same
1223 * unless there are asymmetries in the topology. If there are asymmetries,
1224 * group having more cpu_capacity will pickup more load compared to the
1225 * group having less cpu_capacity.
1226 */
init_sched_groups_capacity(int cpu,struct sched_domain * sd)1227 void init_sched_groups_capacity(int cpu, struct sched_domain *sd)
1228 {
1229 struct sched_group *sg = sd->groups;
1230 #ifdef CONFIG_CPU_ISOLATION_OPT
1231 cpumask_t avail_mask;
1232 #endif
1233
1234 WARN_ON(!sg);
1235
1236 do {
1237 int cpu, max_cpu = -1;
1238
1239 #ifdef CONFIG_CPU_ISOLATION_OPT
1240 cpumask_andnot(&avail_mask, sched_group_span(sg),
1241 cpu_isolated_mask);
1242 sg->group_weight = cpumask_weight(&avail_mask);
1243 #else
1244 sg->group_weight = cpumask_weight(sched_group_span(sg));
1245 #endif
1246
1247 if (!(sd->flags & SD_ASYM_PACKING))
1248 goto next;
1249
1250 for_each_cpu(cpu, sched_group_span(sg)) {
1251 if (max_cpu < 0)
1252 max_cpu = cpu;
1253 else if (sched_asym_prefer(cpu, max_cpu))
1254 max_cpu = cpu;
1255 }
1256 sg->asym_prefer_cpu = max_cpu;
1257
1258 next:
1259 sg = sg->next;
1260 } while (sg != sd->groups);
1261
1262 if (cpu != group_balance_cpu(sg))
1263 return;
1264
1265 update_group_capacity(sd, cpu);
1266 }
1267
1268 /*
1269 * Initializers for schedule domains
1270 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
1271 */
1272
1273 static int default_relax_domain_level = -1;
1274 int sched_domain_level_max;
1275
setup_relax_domain_level(char * str)1276 static int __init setup_relax_domain_level(char *str)
1277 {
1278 if (kstrtoint(str, 0, &default_relax_domain_level))
1279 pr_warn("Unable to set relax_domain_level\n");
1280
1281 return 1;
1282 }
1283 __setup("relax_domain_level=", setup_relax_domain_level);
1284
set_domain_attribute(struct sched_domain * sd,struct sched_domain_attr * attr)1285 static void set_domain_attribute(struct sched_domain *sd,
1286 struct sched_domain_attr *attr)
1287 {
1288 int request;
1289
1290 if (!attr || attr->relax_domain_level < 0) {
1291 if (default_relax_domain_level < 0)
1292 return;
1293 request = default_relax_domain_level;
1294 } else
1295 request = attr->relax_domain_level;
1296
1297 if (sd->level > request) {
1298 /* Turn off idle balance on this domain: */
1299 sd->flags &= ~(SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
1300 }
1301 }
1302
1303 static void __sdt_free(const struct cpumask *cpu_map);
1304 static int __sdt_alloc(const struct cpumask *cpu_map);
1305
__free_domain_allocs(struct s_data * d,enum s_alloc what,const struct cpumask * cpu_map)1306 static void __free_domain_allocs(struct s_data *d, enum s_alloc what,
1307 const struct cpumask *cpu_map)
1308 {
1309 switch (what) {
1310 case sa_rootdomain:
1311 if (!atomic_read(&d->rd->refcount))
1312 free_rootdomain(&d->rd->rcu);
1313 fallthrough;
1314 case sa_sd:
1315 free_percpu(d->sd);
1316 fallthrough;
1317 case sa_sd_storage:
1318 __sdt_free(cpu_map);
1319 fallthrough;
1320 case sa_none:
1321 break;
1322 }
1323 }
1324
1325 static enum s_alloc
__visit_domain_allocation_hell(struct s_data * d,const struct cpumask * cpu_map)1326 __visit_domain_allocation_hell(struct s_data *d, const struct cpumask *cpu_map)
1327 {
1328 memset(d, 0, sizeof(*d));
1329
1330 if (__sdt_alloc(cpu_map))
1331 return sa_sd_storage;
1332 d->sd = alloc_percpu(struct sched_domain *);
1333 if (!d->sd)
1334 return sa_sd_storage;
1335 d->rd = alloc_rootdomain();
1336 if (!d->rd)
1337 return sa_sd;
1338
1339 return sa_rootdomain;
1340 }
1341
1342 /*
1343 * NULL the sd_data elements we've used to build the sched_domain and
1344 * sched_group structure so that the subsequent __free_domain_allocs()
1345 * will not free the data we're using.
1346 */
claim_allocations(int cpu,struct sched_domain * sd)1347 static void claim_allocations(int cpu, struct sched_domain *sd)
1348 {
1349 struct sd_data *sdd = sd->private;
1350
1351 WARN_ON_ONCE(*per_cpu_ptr(sdd->sd, cpu) != sd);
1352 *per_cpu_ptr(sdd->sd, cpu) = NULL;
1353
1354 if (atomic_read(&(*per_cpu_ptr(sdd->sds, cpu))->ref))
1355 *per_cpu_ptr(sdd->sds, cpu) = NULL;
1356
1357 if (atomic_read(&(*per_cpu_ptr(sdd->sg, cpu))->ref))
1358 *per_cpu_ptr(sdd->sg, cpu) = NULL;
1359
1360 if (atomic_read(&(*per_cpu_ptr(sdd->sgc, cpu))->ref))
1361 *per_cpu_ptr(sdd->sgc, cpu) = NULL;
1362 }
1363
1364 #ifdef CONFIG_NUMA
1365 enum numa_topology_type sched_numa_topology_type;
1366
1367 static int sched_domains_numa_levels;
1368 static int sched_domains_curr_level;
1369
1370 int sched_max_numa_distance;
1371 static int *sched_domains_numa_distance;
1372 static struct cpumask ***sched_domains_numa_masks;
1373 int __read_mostly node_reclaim_distance = RECLAIM_DISTANCE;
1374 #endif
1375
1376 /*
1377 * SD_flags allowed in topology descriptions.
1378 *
1379 * These flags are purely descriptive of the topology and do not prescribe
1380 * behaviour. Behaviour is artificial and mapped in the below sd_init()
1381 * function:
1382 *
1383 * SD_SHARE_CPUCAPACITY - describes SMT topologies
1384 * SD_SHARE_PKG_RESOURCES - describes shared caches
1385 * SD_NUMA - describes NUMA topologies
1386 *
1387 * Odd one out, which beside describing the topology has a quirk also
1388 * prescribes the desired behaviour that goes along with it:
1389 *
1390 * SD_ASYM_PACKING - describes SMT quirks
1391 */
1392 #define TOPOLOGY_SD_FLAGS \
1393 (SD_SHARE_CPUCAPACITY | \
1394 SD_SHARE_PKG_RESOURCES | \
1395 SD_NUMA | \
1396 SD_ASYM_PACKING)
1397
1398 static struct sched_domain *
sd_init(struct sched_domain_topology_level * tl,const struct cpumask * cpu_map,struct sched_domain * child,int dflags,int cpu)1399 sd_init(struct sched_domain_topology_level *tl,
1400 const struct cpumask *cpu_map,
1401 struct sched_domain *child, int dflags, int cpu)
1402 {
1403 struct sd_data *sdd = &tl->data;
1404 struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu);
1405 int sd_id, sd_weight, sd_flags = 0;
1406
1407 #ifdef CONFIG_NUMA
1408 /*
1409 * Ugly hack to pass state to sd_numa_mask()...
1410 */
1411 sched_domains_curr_level = tl->numa_level;
1412 #endif
1413
1414 sd_weight = cpumask_weight(tl->mask(cpu));
1415
1416 if (tl->sd_flags)
1417 sd_flags = (*tl->sd_flags)();
1418 if (WARN_ONCE(sd_flags & ~TOPOLOGY_SD_FLAGS,
1419 "wrong sd_flags in topology description\n"))
1420 sd_flags &= TOPOLOGY_SD_FLAGS;
1421
1422 /* Apply detected topology flags */
1423 sd_flags |= dflags;
1424
1425 *sd = (struct sched_domain){
1426 .min_interval = sd_weight,
1427 .max_interval = 2*sd_weight,
1428 .busy_factor = 16,
1429 .imbalance_pct = 117,
1430
1431 .cache_nice_tries = 0,
1432
1433 .flags = 1*SD_BALANCE_NEWIDLE
1434 | 1*SD_BALANCE_EXEC
1435 | 1*SD_BALANCE_FORK
1436 | 0*SD_BALANCE_WAKE
1437 | 1*SD_WAKE_AFFINE
1438 | 0*SD_SHARE_CPUCAPACITY
1439 | 0*SD_SHARE_PKG_RESOURCES
1440 | 0*SD_SERIALIZE
1441 | 1*SD_PREFER_SIBLING
1442 | 0*SD_NUMA
1443 | sd_flags
1444 ,
1445
1446 .last_balance = jiffies,
1447 .balance_interval = sd_weight,
1448 .max_newidle_lb_cost = 0,
1449 .next_decay_max_lb_cost = jiffies,
1450 .child = child,
1451 #ifdef CONFIG_SCHED_DEBUG
1452 .name = tl->name,
1453 #endif
1454 };
1455
1456 cpumask_and(sched_domain_span(sd), cpu_map, tl->mask(cpu));
1457 sd_id = cpumask_first(sched_domain_span(sd));
1458
1459 /*
1460 * Convert topological properties into behaviour.
1461 */
1462
1463 /* Don't attempt to spread across CPUs of different capacities. */
1464 if ((sd->flags & SD_ASYM_CPUCAPACITY) && sd->child)
1465 sd->child->flags &= ~SD_PREFER_SIBLING;
1466
1467 if (sd->flags & SD_SHARE_CPUCAPACITY) {
1468 sd->imbalance_pct = 110;
1469
1470 } else if (sd->flags & SD_SHARE_PKG_RESOURCES) {
1471 sd->imbalance_pct = 117;
1472 sd->cache_nice_tries = 1;
1473
1474 #ifdef CONFIG_NUMA
1475 } else if (sd->flags & SD_NUMA) {
1476 sd->cache_nice_tries = 2;
1477
1478 sd->flags &= ~SD_PREFER_SIBLING;
1479 sd->flags |= SD_SERIALIZE;
1480 if (sched_domains_numa_distance[tl->numa_level] > node_reclaim_distance) {
1481 sd->flags &= ~(SD_BALANCE_EXEC |
1482 SD_BALANCE_FORK |
1483 SD_WAKE_AFFINE);
1484 }
1485
1486 #endif
1487 } else {
1488 sd->cache_nice_tries = 1;
1489 }
1490
1491 /*
1492 * For all levels sharing cache; connect a sched_domain_shared
1493 * instance.
1494 */
1495 if (sd->flags & SD_SHARE_PKG_RESOURCES) {
1496 sd->shared = *per_cpu_ptr(sdd->sds, sd_id);
1497 atomic_inc(&sd->shared->ref);
1498 atomic_set(&sd->shared->nr_busy_cpus, sd_weight);
1499 }
1500
1501 sd->private = sdd;
1502
1503 return sd;
1504 }
1505
1506 /*
1507 * Topology list, bottom-up.
1508 */
1509 static struct sched_domain_topology_level default_topology[] = {
1510 #ifdef CONFIG_SCHED_SMT
1511 { cpu_smt_mask, cpu_smt_flags, SD_INIT_NAME(SMT) },
1512 #endif
1513 #ifdef CONFIG_SCHED_MC
1514 { cpu_coregroup_mask, cpu_core_flags, SD_INIT_NAME(MC) },
1515 #endif
1516 { cpu_cpu_mask, SD_INIT_NAME(DIE) },
1517 { NULL, },
1518 };
1519
1520 static struct sched_domain_topology_level *sched_domain_topology =
1521 default_topology;
1522
1523 #define for_each_sd_topology(tl) \
1524 for (tl = sched_domain_topology; tl->mask; tl++)
1525
set_sched_topology(struct sched_domain_topology_level * tl)1526 void set_sched_topology(struct sched_domain_topology_level *tl)
1527 {
1528 if (WARN_ON_ONCE(sched_smp_initialized))
1529 return;
1530
1531 sched_domain_topology = tl;
1532 }
1533
1534 #ifdef CONFIG_NUMA
1535
sd_numa_mask(int cpu)1536 static const struct cpumask *sd_numa_mask(int cpu)
1537 {
1538 return sched_domains_numa_masks[sched_domains_curr_level][cpu_to_node(cpu)];
1539 }
1540
sched_numa_warn(const char * str)1541 static void sched_numa_warn(const char *str)
1542 {
1543 static int done = false;
1544 int i,j;
1545
1546 if (done)
1547 return;
1548
1549 done = true;
1550
1551 printk(KERN_WARNING "ERROR: %s\n\n", str);
1552
1553 for (i = 0; i < nr_node_ids; i++) {
1554 printk(KERN_WARNING " ");
1555 for (j = 0; j < nr_node_ids; j++)
1556 printk(KERN_CONT "%02d ", node_distance(i,j));
1557 printk(KERN_CONT "\n");
1558 }
1559 printk(KERN_WARNING "\n");
1560 }
1561
find_numa_distance(int distance)1562 bool find_numa_distance(int distance)
1563 {
1564 int i;
1565
1566 if (distance == node_distance(0, 0))
1567 return true;
1568
1569 for (i = 0; i < sched_domains_numa_levels; i++) {
1570 if (sched_domains_numa_distance[i] == distance)
1571 return true;
1572 }
1573
1574 return false;
1575 }
1576
1577 /*
1578 * A system can have three types of NUMA topology:
1579 * NUMA_DIRECT: all nodes are directly connected, or not a NUMA system
1580 * NUMA_GLUELESS_MESH: some nodes reachable through intermediary nodes
1581 * NUMA_BACKPLANE: nodes can reach other nodes through a backplane
1582 *
1583 * The difference between a glueless mesh topology and a backplane
1584 * topology lies in whether communication between not directly
1585 * connected nodes goes through intermediary nodes (where programs
1586 * could run), or through backplane controllers. This affects
1587 * placement of programs.
1588 *
1589 * The type of topology can be discerned with the following tests:
1590 * - If the maximum distance between any nodes is 1 hop, the system
1591 * is directly connected.
1592 * - If for two nodes A and B, located N > 1 hops away from each other,
1593 * there is an intermediary node C, which is < N hops away from both
1594 * nodes A and B, the system is a glueless mesh.
1595 */
init_numa_topology_type(void)1596 static void init_numa_topology_type(void)
1597 {
1598 int a, b, c, n;
1599
1600 n = sched_max_numa_distance;
1601
1602 if (sched_domains_numa_levels <= 2) {
1603 sched_numa_topology_type = NUMA_DIRECT;
1604 return;
1605 }
1606
1607 for_each_online_node(a) {
1608 for_each_online_node(b) {
1609 /* Find two nodes furthest removed from each other. */
1610 if (node_distance(a, b) < n)
1611 continue;
1612
1613 /* Is there an intermediary node between a and b? */
1614 for_each_online_node(c) {
1615 if (node_distance(a, c) < n &&
1616 node_distance(b, c) < n) {
1617 sched_numa_topology_type =
1618 NUMA_GLUELESS_MESH;
1619 return;
1620 }
1621 }
1622
1623 sched_numa_topology_type = NUMA_BACKPLANE;
1624 return;
1625 }
1626 }
1627 }
1628
1629 #define NR_DISTANCE_VALUES (1 << DISTANCE_BITS)
1630
sched_init_numa(void)1631 void sched_init_numa(void)
1632 {
1633 struct sched_domain_topology_level *tl;
1634 unsigned long *distance_map;
1635 int nr_levels = 0;
1636 int i, j;
1637
1638 /*
1639 * O(nr_nodes^2) deduplicating selection sort -- in order to find the
1640 * unique distances in the node_distance() table.
1641 */
1642 distance_map = bitmap_alloc(NR_DISTANCE_VALUES, GFP_KERNEL);
1643 if (!distance_map)
1644 return;
1645
1646 bitmap_zero(distance_map, NR_DISTANCE_VALUES);
1647 for (i = 0; i < nr_node_ids; i++) {
1648 for (j = 0; j < nr_node_ids; j++) {
1649 int distance = node_distance(i, j);
1650
1651 if (distance < LOCAL_DISTANCE || distance >= NR_DISTANCE_VALUES) {
1652 sched_numa_warn("Invalid distance value range");
1653 return;
1654 }
1655
1656 bitmap_set(distance_map, distance, 1);
1657 }
1658 }
1659 /*
1660 * We can now figure out how many unique distance values there are and
1661 * allocate memory accordingly.
1662 */
1663 nr_levels = bitmap_weight(distance_map, NR_DISTANCE_VALUES);
1664
1665 sched_domains_numa_distance = kcalloc(nr_levels, sizeof(int), GFP_KERNEL);
1666 if (!sched_domains_numa_distance) {
1667 bitmap_free(distance_map);
1668 return;
1669 }
1670
1671 for (i = 0, j = 0; i < nr_levels; i++, j++) {
1672 j = find_next_bit(distance_map, NR_DISTANCE_VALUES, j);
1673 sched_domains_numa_distance[i] = j;
1674 }
1675
1676 bitmap_free(distance_map);
1677
1678 /*
1679 * 'nr_levels' contains the number of unique distances
1680 *
1681 * The sched_domains_numa_distance[] array includes the actual distance
1682 * numbers.
1683 */
1684
1685 /*
1686 * Here, we should temporarily reset sched_domains_numa_levels to 0.
1687 * If it fails to allocate memory for array sched_domains_numa_masks[][],
1688 * the array will contain less then 'nr_levels' members. This could be
1689 * dangerous when we use it to iterate array sched_domains_numa_masks[][]
1690 * in other functions.
1691 *
1692 * We reset it to 'nr_levels' at the end of this function.
1693 */
1694 sched_domains_numa_levels = 0;
1695
1696 sched_domains_numa_masks = kzalloc(sizeof(void *) * nr_levels, GFP_KERNEL);
1697 if (!sched_domains_numa_masks)
1698 return;
1699
1700 /*
1701 * Now for each level, construct a mask per node which contains all
1702 * CPUs of nodes that are that many hops away from us.
1703 */
1704 for (i = 0; i < nr_levels; i++) {
1705 sched_domains_numa_masks[i] =
1706 kzalloc(nr_node_ids * sizeof(void *), GFP_KERNEL);
1707 if (!sched_domains_numa_masks[i])
1708 return;
1709
1710 for (j = 0; j < nr_node_ids; j++) {
1711 struct cpumask *mask = kzalloc(cpumask_size(), GFP_KERNEL);
1712 int k;
1713
1714 if (!mask)
1715 return;
1716
1717 sched_domains_numa_masks[i][j] = mask;
1718
1719 for_each_node(k) {
1720 if (sched_debug() && (node_distance(j, k) != node_distance(k, j)))
1721 sched_numa_warn("Node-distance not symmetric");
1722
1723 if (node_distance(j, k) > sched_domains_numa_distance[i])
1724 continue;
1725
1726 cpumask_or(mask, mask, cpumask_of_node(k));
1727 }
1728 }
1729 }
1730
1731 /* Compute default topology size */
1732 for (i = 0; sched_domain_topology[i].mask; i++);
1733
1734 tl = kzalloc((i + nr_levels + 1) *
1735 sizeof(struct sched_domain_topology_level), GFP_KERNEL);
1736 if (!tl)
1737 return;
1738
1739 /*
1740 * Copy the default topology bits..
1741 */
1742 for (i = 0; sched_domain_topology[i].mask; i++)
1743 tl[i] = sched_domain_topology[i];
1744
1745 /*
1746 * Add the NUMA identity distance, aka single NODE.
1747 */
1748 tl[i++] = (struct sched_domain_topology_level){
1749 .mask = sd_numa_mask,
1750 .numa_level = 0,
1751 SD_INIT_NAME(NODE)
1752 };
1753
1754 /*
1755 * .. and append 'j' levels of NUMA goodness.
1756 */
1757 for (j = 1; j < nr_levels; i++, j++) {
1758 tl[i] = (struct sched_domain_topology_level){
1759 .mask = sd_numa_mask,
1760 .sd_flags = cpu_numa_flags,
1761 .flags = SDTL_OVERLAP,
1762 .numa_level = j,
1763 SD_INIT_NAME(NUMA)
1764 };
1765 }
1766
1767 sched_domain_topology = tl;
1768
1769 sched_domains_numa_levels = nr_levels;
1770 sched_max_numa_distance = sched_domains_numa_distance[nr_levels - 1];
1771
1772 init_numa_topology_type();
1773 }
1774
sched_domains_numa_masks_set(unsigned int cpu)1775 void sched_domains_numa_masks_set(unsigned int cpu)
1776 {
1777 int node = cpu_to_node(cpu);
1778 int i, j;
1779
1780 for (i = 0; i < sched_domains_numa_levels; i++) {
1781 for (j = 0; j < nr_node_ids; j++) {
1782 if (node_distance(j, node) <= sched_domains_numa_distance[i])
1783 cpumask_set_cpu(cpu, sched_domains_numa_masks[i][j]);
1784 }
1785 }
1786 }
1787
sched_domains_numa_masks_clear(unsigned int cpu)1788 void sched_domains_numa_masks_clear(unsigned int cpu)
1789 {
1790 int i, j;
1791
1792 for (i = 0; i < sched_domains_numa_levels; i++) {
1793 for (j = 0; j < nr_node_ids; j++)
1794 cpumask_clear_cpu(cpu, sched_domains_numa_masks[i][j]);
1795 }
1796 }
1797
1798 /*
1799 * sched_numa_find_closest() - given the NUMA topology, find the cpu
1800 * closest to @cpu from @cpumask.
1801 * cpumask: cpumask to find a cpu from
1802 * cpu: cpu to be close to
1803 *
1804 * returns: cpu, or nr_cpu_ids when nothing found.
1805 */
sched_numa_find_closest(const struct cpumask * cpus,int cpu)1806 int sched_numa_find_closest(const struct cpumask *cpus, int cpu)
1807 {
1808 int i, j = cpu_to_node(cpu);
1809
1810 for (i = 0; i < sched_domains_numa_levels; i++) {
1811 cpu = cpumask_any_and(cpus, sched_domains_numa_masks[i][j]);
1812 if (cpu < nr_cpu_ids)
1813 return cpu;
1814 }
1815 return nr_cpu_ids;
1816 }
1817
1818 #endif /* CONFIG_NUMA */
1819
__sdt_alloc(const struct cpumask * cpu_map)1820 static int __sdt_alloc(const struct cpumask *cpu_map)
1821 {
1822 struct sched_domain_topology_level *tl;
1823 int j;
1824
1825 for_each_sd_topology(tl) {
1826 struct sd_data *sdd = &tl->data;
1827
1828 sdd->sd = alloc_percpu(struct sched_domain *);
1829 if (!sdd->sd)
1830 return -ENOMEM;
1831
1832 sdd->sds = alloc_percpu(struct sched_domain_shared *);
1833 if (!sdd->sds)
1834 return -ENOMEM;
1835
1836 sdd->sg = alloc_percpu(struct sched_group *);
1837 if (!sdd->sg)
1838 return -ENOMEM;
1839
1840 sdd->sgc = alloc_percpu(struct sched_group_capacity *);
1841 if (!sdd->sgc)
1842 return -ENOMEM;
1843
1844 for_each_cpu(j, cpu_map) {
1845 struct sched_domain *sd;
1846 struct sched_domain_shared *sds;
1847 struct sched_group *sg;
1848 struct sched_group_capacity *sgc;
1849
1850 sd = kzalloc_node(sizeof(struct sched_domain) + cpumask_size(),
1851 GFP_KERNEL, cpu_to_node(j));
1852 if (!sd)
1853 return -ENOMEM;
1854
1855 *per_cpu_ptr(sdd->sd, j) = sd;
1856
1857 sds = kzalloc_node(sizeof(struct sched_domain_shared),
1858 GFP_KERNEL, cpu_to_node(j));
1859 if (!sds)
1860 return -ENOMEM;
1861
1862 *per_cpu_ptr(sdd->sds, j) = sds;
1863
1864 sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
1865 GFP_KERNEL, cpu_to_node(j));
1866 if (!sg)
1867 return -ENOMEM;
1868
1869 sg->next = sg;
1870
1871 *per_cpu_ptr(sdd->sg, j) = sg;
1872
1873 sgc = kzalloc_node(sizeof(struct sched_group_capacity) + cpumask_size(),
1874 GFP_KERNEL, cpu_to_node(j));
1875 if (!sgc)
1876 return -ENOMEM;
1877
1878 #ifdef CONFIG_SCHED_DEBUG
1879 sgc->id = j;
1880 #endif
1881
1882 *per_cpu_ptr(sdd->sgc, j) = sgc;
1883 }
1884 }
1885
1886 return 0;
1887 }
1888
__sdt_free(const struct cpumask * cpu_map)1889 static void __sdt_free(const struct cpumask *cpu_map)
1890 {
1891 struct sched_domain_topology_level *tl;
1892 int j;
1893
1894 for_each_sd_topology(tl) {
1895 struct sd_data *sdd = &tl->data;
1896
1897 for_each_cpu(j, cpu_map) {
1898 struct sched_domain *sd;
1899
1900 if (sdd->sd) {
1901 sd = *per_cpu_ptr(sdd->sd, j);
1902 if (sd && (sd->flags & SD_OVERLAP))
1903 free_sched_groups(sd->groups, 0);
1904 kfree(*per_cpu_ptr(sdd->sd, j));
1905 }
1906
1907 if (sdd->sds)
1908 kfree(*per_cpu_ptr(sdd->sds, j));
1909 if (sdd->sg)
1910 kfree(*per_cpu_ptr(sdd->sg, j));
1911 if (sdd->sgc)
1912 kfree(*per_cpu_ptr(sdd->sgc, j));
1913 }
1914 free_percpu(sdd->sd);
1915 sdd->sd = NULL;
1916 free_percpu(sdd->sds);
1917 sdd->sds = NULL;
1918 free_percpu(sdd->sg);
1919 sdd->sg = NULL;
1920 free_percpu(sdd->sgc);
1921 sdd->sgc = NULL;
1922 }
1923 }
1924
build_sched_domain(struct sched_domain_topology_level * tl,const struct cpumask * cpu_map,struct sched_domain_attr * attr,struct sched_domain * child,int dflags,int cpu)1925 static struct sched_domain *build_sched_domain(struct sched_domain_topology_level *tl,
1926 const struct cpumask *cpu_map, struct sched_domain_attr *attr,
1927 struct sched_domain *child, int dflags, int cpu)
1928 {
1929 struct sched_domain *sd = sd_init(tl, cpu_map, child, dflags, cpu);
1930
1931 if (child) {
1932 sd->level = child->level + 1;
1933 sched_domain_level_max = max(sched_domain_level_max, sd->level);
1934 child->parent = sd;
1935
1936 if (!cpumask_subset(sched_domain_span(child),
1937 sched_domain_span(sd))) {
1938 pr_err("BUG: arch topology borken\n");
1939 #ifdef CONFIG_SCHED_DEBUG
1940 pr_err(" the %s domain not a subset of the %s domain\n",
1941 child->name, sd->name);
1942 #endif
1943 /* Fixup, ensure @sd has at least @child CPUs. */
1944 cpumask_or(sched_domain_span(sd),
1945 sched_domain_span(sd),
1946 sched_domain_span(child));
1947 }
1948
1949 }
1950 set_domain_attribute(sd, attr);
1951
1952 return sd;
1953 }
1954
1955 /*
1956 * Ensure topology masks are sane, i.e. there are no conflicts (overlaps) for
1957 * any two given CPUs at this (non-NUMA) topology level.
1958 */
topology_span_sane(struct sched_domain_topology_level * tl,const struct cpumask * cpu_map,int cpu)1959 static bool topology_span_sane(struct sched_domain_topology_level *tl,
1960 const struct cpumask *cpu_map, int cpu)
1961 {
1962 int i;
1963
1964 /* NUMA levels are allowed to overlap */
1965 if (tl->flags & SDTL_OVERLAP)
1966 return true;
1967
1968 /*
1969 * Non-NUMA levels cannot partially overlap - they must be either
1970 * completely equal or completely disjoint. Otherwise we can end up
1971 * breaking the sched_group lists - i.e. a later get_group() pass
1972 * breaks the linking done for an earlier span.
1973 */
1974 for_each_cpu(i, cpu_map) {
1975 if (i == cpu)
1976 continue;
1977 /*
1978 * We should 'and' all those masks with 'cpu_map' to exactly
1979 * match the topology we're about to build, but that can only
1980 * remove CPUs, which only lessens our ability to detect
1981 * overlaps
1982 */
1983 if (!cpumask_equal(tl->mask(cpu), tl->mask(i)) &&
1984 cpumask_intersects(tl->mask(cpu), tl->mask(i)))
1985 return false;
1986 }
1987
1988 return true;
1989 }
1990
1991 /*
1992 * Find the sched_domain_topology_level where all CPU capacities are visible
1993 * for all CPUs.
1994 */
1995 static struct sched_domain_topology_level
asym_cpu_capacity_level(const struct cpumask * cpu_map)1996 *asym_cpu_capacity_level(const struct cpumask *cpu_map)
1997 {
1998 int i, j, asym_level = 0;
1999 bool asym = false;
2000 struct sched_domain_topology_level *tl, *asym_tl = NULL;
2001 unsigned long cap;
2002
2003 /* Is there any asymmetry? */
2004 cap = arch_scale_cpu_capacity(cpumask_first(cpu_map));
2005
2006 for_each_cpu(i, cpu_map) {
2007 if (arch_scale_cpu_capacity(i) != cap) {
2008 asym = true;
2009 break;
2010 }
2011 }
2012
2013 if (!asym)
2014 return NULL;
2015
2016 /*
2017 * Examine topology from all CPU's point of views to detect the lowest
2018 * sched_domain_topology_level where a highest capacity CPU is visible
2019 * to everyone.
2020 */
2021 for_each_cpu(i, cpu_map) {
2022 unsigned long max_capacity = arch_scale_cpu_capacity(i);
2023 int tl_id = 0;
2024
2025 for_each_sd_topology(tl) {
2026 if (tl_id < asym_level)
2027 goto next_level;
2028
2029 for_each_cpu_and(j, tl->mask(i), cpu_map) {
2030 unsigned long capacity;
2031
2032 capacity = arch_scale_cpu_capacity(j);
2033
2034 if (capacity <= max_capacity)
2035 continue;
2036
2037 max_capacity = capacity;
2038 asym_level = tl_id;
2039 asym_tl = tl;
2040 }
2041 next_level:
2042 tl_id++;
2043 }
2044 }
2045
2046 return asym_tl;
2047 }
2048
2049
2050 /*
2051 * Build sched domains for a given set of CPUs and attach the sched domains
2052 * to the individual CPUs
2053 */
2054 static int
build_sched_domains(const struct cpumask * cpu_map,struct sched_domain_attr * attr)2055 build_sched_domains(const struct cpumask *cpu_map, struct sched_domain_attr *attr)
2056 {
2057 enum s_alloc alloc_state = sa_none;
2058 struct sched_domain *sd;
2059 struct s_data d;
2060 struct rq *rq = NULL;
2061 int i, ret = -ENOMEM;
2062 struct sched_domain_topology_level *tl_asym;
2063 bool has_asym = false;
2064
2065 if (WARN_ON(cpumask_empty(cpu_map)))
2066 goto error;
2067
2068 alloc_state = __visit_domain_allocation_hell(&d, cpu_map);
2069 if (alloc_state != sa_rootdomain)
2070 goto error;
2071
2072 tl_asym = asym_cpu_capacity_level(cpu_map);
2073
2074 /* Set up domains for CPUs specified by the cpu_map: */
2075 for_each_cpu(i, cpu_map) {
2076 struct sched_domain_topology_level *tl;
2077 int dflags = 0;
2078
2079 sd = NULL;
2080 for_each_sd_topology(tl) {
2081 if (tl == tl_asym) {
2082 dflags |= SD_ASYM_CPUCAPACITY;
2083 has_asym = true;
2084 }
2085
2086 if (WARN_ON(!topology_span_sane(tl, cpu_map, i)))
2087 goto error;
2088
2089 sd = build_sched_domain(tl, cpu_map, attr, sd, dflags, i);
2090
2091 if (tl == sched_domain_topology)
2092 *per_cpu_ptr(d.sd, i) = sd;
2093 if (tl->flags & SDTL_OVERLAP)
2094 sd->flags |= SD_OVERLAP;
2095 if (cpumask_equal(cpu_map, sched_domain_span(sd)))
2096 break;
2097 }
2098 }
2099
2100 /* Build the groups for the domains */
2101 for_each_cpu(i, cpu_map) {
2102 for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
2103 sd->span_weight = cpumask_weight(sched_domain_span(sd));
2104 if (sd->flags & SD_OVERLAP) {
2105 if (build_overlap_sched_groups(sd, i))
2106 goto error;
2107 } else {
2108 if (build_sched_groups(sd, i))
2109 goto error;
2110 }
2111 }
2112 }
2113
2114 /* Calculate CPU capacity for physical packages and nodes */
2115 for (i = nr_cpumask_bits-1; i >= 0; i--) {
2116 if (!cpumask_test_cpu(i, cpu_map))
2117 continue;
2118
2119 for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
2120 claim_allocations(i, sd);
2121 init_sched_groups_capacity(i, sd);
2122 }
2123 }
2124
2125 /* Attach the domains */
2126 rcu_read_lock();
2127 for_each_cpu(i, cpu_map) {
2128 #ifdef CONFIG_SCHED_RT_CAS
2129 int max_cpu = READ_ONCE(d.rd->max_cap_orig_cpu);
2130 #endif
2131
2132 rq = cpu_rq(i);
2133 sd = *per_cpu_ptr(d.sd, i);
2134
2135 #ifdef CONFIG_SCHED_RT_CAS
2136 if (max_cpu < 0 || arch_scale_cpu_capacity(i) >
2137 arch_scale_cpu_capacity(max_cpu))
2138 WRITE_ONCE(d.rd->max_cap_orig_cpu, i);
2139 #endif
2140
2141 /* Use READ_ONCE()/WRITE_ONCE() to avoid load/store tearing: */
2142 if (rq->cpu_capacity_orig > READ_ONCE(d.rd->max_cpu_capacity))
2143 WRITE_ONCE(d.rd->max_cpu_capacity, rq->cpu_capacity_orig);
2144
2145 cpu_attach_domain(sd, d.rd, i);
2146 }
2147 rcu_read_unlock();
2148
2149 if (has_asym)
2150 static_branch_inc_cpuslocked(&sched_asym_cpucapacity);
2151
2152 if (rq && sched_debug_enabled) {
2153 pr_info("root domain span: %*pbl (max cpu_capacity = %lu)\n",
2154 cpumask_pr_args(cpu_map), rq->rd->max_cpu_capacity);
2155 }
2156
2157 ret = 0;
2158 error:
2159 __free_domain_allocs(&d, alloc_state, cpu_map);
2160
2161 return ret;
2162 }
2163
2164 /* Current sched domains: */
2165 static cpumask_var_t *doms_cur;
2166
2167 /* Number of sched domains in 'doms_cur': */
2168 static int ndoms_cur;
2169
2170 /* Attribues of custom domains in 'doms_cur' */
2171 static struct sched_domain_attr *dattr_cur;
2172
2173 /*
2174 * Special case: If a kmalloc() of a doms_cur partition (array of
2175 * cpumask) fails, then fallback to a single sched domain,
2176 * as determined by the single cpumask fallback_doms.
2177 */
2178 static cpumask_var_t fallback_doms;
2179
2180 /*
2181 * arch_update_cpu_topology lets virtualized architectures update the
2182 * CPU core maps. It is supposed to return 1 if the topology changed
2183 * or 0 if it stayed the same.
2184 */
arch_update_cpu_topology(void)2185 int __weak arch_update_cpu_topology(void)
2186 {
2187 return 0;
2188 }
2189
alloc_sched_domains(unsigned int ndoms)2190 cpumask_var_t *alloc_sched_domains(unsigned int ndoms)
2191 {
2192 int i;
2193 cpumask_var_t *doms;
2194
2195 doms = kmalloc_array(ndoms, sizeof(*doms), GFP_KERNEL);
2196 if (!doms)
2197 return NULL;
2198 for (i = 0; i < ndoms; i++) {
2199 if (!alloc_cpumask_var(&doms[i], GFP_KERNEL)) {
2200 free_sched_domains(doms, i);
2201 return NULL;
2202 }
2203 }
2204 return doms;
2205 }
2206
free_sched_domains(cpumask_var_t doms[],unsigned int ndoms)2207 void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms)
2208 {
2209 unsigned int i;
2210 for (i = 0; i < ndoms; i++)
2211 free_cpumask_var(doms[i]);
2212 kfree(doms);
2213 }
2214
2215 /*
2216 * Set up scheduler domains and groups. For now this just excludes isolated
2217 * CPUs, but could be used to exclude other special cases in the future.
2218 */
sched_init_domains(const struct cpumask * cpu_map)2219 int sched_init_domains(const struct cpumask *cpu_map)
2220 {
2221 int err;
2222
2223 zalloc_cpumask_var(&sched_domains_tmpmask, GFP_KERNEL);
2224 zalloc_cpumask_var(&sched_domains_tmpmask2, GFP_KERNEL);
2225 zalloc_cpumask_var(&fallback_doms, GFP_KERNEL);
2226
2227 arch_update_cpu_topology();
2228 ndoms_cur = 1;
2229 doms_cur = alloc_sched_domains(ndoms_cur);
2230 if (!doms_cur)
2231 doms_cur = &fallback_doms;
2232 cpumask_and(doms_cur[0], cpu_map, housekeeping_cpumask(HK_FLAG_DOMAIN));
2233 err = build_sched_domains(doms_cur[0], NULL);
2234 register_sched_domain_sysctl();
2235
2236 return err;
2237 }
2238
2239 /*
2240 * Detach sched domains from a group of CPUs specified in cpu_map
2241 * These CPUs will now be attached to the NULL domain
2242 */
detach_destroy_domains(const struct cpumask * cpu_map)2243 static void detach_destroy_domains(const struct cpumask *cpu_map)
2244 {
2245 unsigned int cpu = cpumask_any(cpu_map);
2246 int i;
2247
2248 if (rcu_access_pointer(per_cpu(sd_asym_cpucapacity, cpu)))
2249 static_branch_dec_cpuslocked(&sched_asym_cpucapacity);
2250
2251 rcu_read_lock();
2252 for_each_cpu(i, cpu_map)
2253 cpu_attach_domain(NULL, &def_root_domain, i);
2254 rcu_read_unlock();
2255 }
2256
2257 /* handle null as "default" */
dattrs_equal(struct sched_domain_attr * cur,int idx_cur,struct sched_domain_attr * new,int idx_new)2258 static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
2259 struct sched_domain_attr *new, int idx_new)
2260 {
2261 struct sched_domain_attr tmp;
2262
2263 /* Fast path: */
2264 if (!new && !cur)
2265 return 1;
2266
2267 tmp = SD_ATTR_INIT;
2268
2269 return !memcmp(cur ? (cur + idx_cur) : &tmp,
2270 new ? (new + idx_new) : &tmp,
2271 sizeof(struct sched_domain_attr));
2272 }
2273
2274 /*
2275 * Partition sched domains as specified by the 'ndoms_new'
2276 * cpumasks in the array doms_new[] of cpumasks. This compares
2277 * doms_new[] to the current sched domain partitioning, doms_cur[].
2278 * It destroys each deleted domain and builds each new domain.
2279 *
2280 * 'doms_new' is an array of cpumask_var_t's of length 'ndoms_new'.
2281 * The masks don't intersect (don't overlap.) We should setup one
2282 * sched domain for each mask. CPUs not in any of the cpumasks will
2283 * not be load balanced. If the same cpumask appears both in the
2284 * current 'doms_cur' domains and in the new 'doms_new', we can leave
2285 * it as it is.
2286 *
2287 * The passed in 'doms_new' should be allocated using
2288 * alloc_sched_domains. This routine takes ownership of it and will
2289 * free_sched_domains it when done with it. If the caller failed the
2290 * alloc call, then it can pass in doms_new == NULL && ndoms_new == 1,
2291 * and partition_sched_domains() will fallback to the single partition
2292 * 'fallback_doms', it also forces the domains to be rebuilt.
2293 *
2294 * If doms_new == NULL it will be replaced with cpu_online_mask.
2295 * ndoms_new == 0 is a special case for destroying existing domains,
2296 * and it will not create the default domain.
2297 *
2298 * Call with hotplug lock and sched_domains_mutex held
2299 */
partition_sched_domains_locked(int ndoms_new,cpumask_var_t doms_new[],struct sched_domain_attr * dattr_new)2300 void partition_sched_domains_locked(int ndoms_new, cpumask_var_t doms_new[],
2301 struct sched_domain_attr *dattr_new)
2302 {
2303 bool __maybe_unused has_eas = false;
2304 int i, j, n;
2305 int new_topology;
2306
2307 lockdep_assert_held(&sched_domains_mutex);
2308
2309 /* Always unregister in case we don't destroy any domains: */
2310 unregister_sched_domain_sysctl();
2311
2312 /* Let the architecture update CPU core mappings: */
2313 new_topology = arch_update_cpu_topology();
2314
2315 if (!doms_new) {
2316 WARN_ON_ONCE(dattr_new);
2317 n = 0;
2318 doms_new = alloc_sched_domains(1);
2319 if (doms_new) {
2320 n = 1;
2321 cpumask_and(doms_new[0], cpu_active_mask,
2322 housekeeping_cpumask(HK_FLAG_DOMAIN));
2323 }
2324 } else {
2325 n = ndoms_new;
2326 }
2327
2328 /* Destroy deleted domains: */
2329 for (i = 0; i < ndoms_cur; i++) {
2330 for (j = 0; j < n && !new_topology; j++) {
2331 if (cpumask_equal(doms_cur[i], doms_new[j]) &&
2332 dattrs_equal(dattr_cur, i, dattr_new, j)) {
2333 struct root_domain *rd;
2334
2335 /*
2336 * This domain won't be destroyed and as such
2337 * its dl_bw->total_bw needs to be cleared. It
2338 * will be recomputed in function
2339 * update_tasks_root_domain().
2340 */
2341 rd = cpu_rq(cpumask_any(doms_cur[i]))->rd;
2342 dl_clear_root_domain(rd);
2343 goto match1;
2344 }
2345 }
2346 /* No match - a current sched domain not in new doms_new[] */
2347 detach_destroy_domains(doms_cur[i]);
2348 match1:
2349 ;
2350 }
2351
2352 n = ndoms_cur;
2353 if (!doms_new) {
2354 n = 0;
2355 doms_new = &fallback_doms;
2356 cpumask_and(doms_new[0], cpu_active_mask,
2357 housekeeping_cpumask(HK_FLAG_DOMAIN));
2358 }
2359
2360 /* Build new domains: */
2361 for (i = 0; i < ndoms_new; i++) {
2362 for (j = 0; j < n && !new_topology; j++) {
2363 if (cpumask_equal(doms_new[i], doms_cur[j]) &&
2364 dattrs_equal(dattr_new, i, dattr_cur, j))
2365 goto match2;
2366 }
2367 /* No match - add a new doms_new */
2368 build_sched_domains(doms_new[i], dattr_new ? dattr_new + i : NULL);
2369 match2:
2370 ;
2371 }
2372
2373 #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
2374 /* Build perf. domains: */
2375 for (i = 0; i < ndoms_new; i++) {
2376 for (j = 0; j < n && !sched_energy_update; j++) {
2377 if (cpumask_equal(doms_new[i], doms_cur[j]) &&
2378 cpu_rq(cpumask_first(doms_cur[j]))->rd->pd) {
2379 has_eas = true;
2380 goto match3;
2381 }
2382 }
2383 /* No match - add perf. domains for a new rd */
2384 has_eas |= build_perf_domains(doms_new[i]);
2385 match3:
2386 ;
2387 }
2388 sched_energy_set(has_eas);
2389 #endif
2390
2391 /* Remember the new sched domains: */
2392 if (doms_cur != &fallback_doms)
2393 free_sched_domains(doms_cur, ndoms_cur);
2394
2395 kfree(dattr_cur);
2396 doms_cur = doms_new;
2397 dattr_cur = dattr_new;
2398 ndoms_cur = ndoms_new;
2399
2400 register_sched_domain_sysctl();
2401 }
2402
2403 /*
2404 * Call with hotplug lock held
2405 */
partition_sched_domains(int ndoms_new,cpumask_var_t doms_new[],struct sched_domain_attr * dattr_new)2406 void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
2407 struct sched_domain_attr *dattr_new)
2408 {
2409 mutex_lock(&sched_domains_mutex);
2410 partition_sched_domains_locked(ndoms_new, doms_new, dattr_new);
2411 mutex_unlock(&sched_domains_mutex);
2412 }
2413