]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - kernel/sched/topology.c
sched: Clean up and harmonize the coding style of the scheduler code base
[mirror_ubuntu-jammy-kernel.git] / kernel / sched / topology.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Scheduler topology setup/handling methods
4 */
5 #include <linux/sched.h>
6 #include <linux/mutex.h>
7 #include <linux/sched/isolation.h>
8
9 #include "sched.h"
10
11 DEFINE_MUTEX(sched_domains_mutex);
12
13 /* Protected by sched_domains_mutex: */
14 cpumask_var_t sched_domains_tmpmask;
15 cpumask_var_t sched_domains_tmpmask2;
16
17 #ifdef CONFIG_SCHED_DEBUG
18
19 static int __init sched_debug_setup(char *str)
20 {
21 sched_debug_enabled = true;
22
23 return 0;
24 }
25 early_param("sched_debug", sched_debug_setup);
26
27 static inline bool sched_debug(void)
28 {
29 return sched_debug_enabled;
30 }
31
32 static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
33 struct cpumask *groupmask)
34 {
35 struct sched_group *group = sd->groups;
36
37 cpumask_clear(groupmask);
38
39 printk(KERN_DEBUG "%*s domain-%d: ", level, "", level);
40
41 if (!(sd->flags & SD_LOAD_BALANCE)) {
42 printk("does not load-balance\n");
43 if (sd->parent)
44 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain has parent");
45 return -1;
46 }
47
48 printk(KERN_CONT "span=%*pbl level=%s\n",
49 cpumask_pr_args(sched_domain_span(sd)), sd->name);
50
51 if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) {
52 printk(KERN_ERR "ERROR: domain->span does not contain CPU%d\n", cpu);
53 }
54 if (!cpumask_test_cpu(cpu, sched_group_span(group))) {
55 printk(KERN_ERR "ERROR: domain->groups does not contain CPU%d\n", cpu);
56 }
57
58 printk(KERN_DEBUG "%*s groups:", level + 1, "");
59 do {
60 if (!group) {
61 printk("\n");
62 printk(KERN_ERR "ERROR: group is NULL\n");
63 break;
64 }
65
66 if (!cpumask_weight(sched_group_span(group))) {
67 printk(KERN_CONT "\n");
68 printk(KERN_ERR "ERROR: empty group\n");
69 break;
70 }
71
72 if (!(sd->flags & SD_OVERLAP) &&
73 cpumask_intersects(groupmask, sched_group_span(group))) {
74 printk(KERN_CONT "\n");
75 printk(KERN_ERR "ERROR: repeated CPUs\n");
76 break;
77 }
78
79 cpumask_or(groupmask, groupmask, sched_group_span(group));
80
81 printk(KERN_CONT " %d:{ span=%*pbl",
82 group->sgc->id,
83 cpumask_pr_args(sched_group_span(group)));
84
85 if ((sd->flags & SD_OVERLAP) &&
86 !cpumask_equal(group_balance_mask(group), sched_group_span(group))) {
87 printk(KERN_CONT " mask=%*pbl",
88 cpumask_pr_args(group_balance_mask(group)));
89 }
90
91 if (group->sgc->capacity != SCHED_CAPACITY_SCALE)
92 printk(KERN_CONT " cap=%lu", group->sgc->capacity);
93
94 if (group == sd->groups && sd->child &&
95 !cpumask_equal(sched_domain_span(sd->child),
96 sched_group_span(group))) {
97 printk(KERN_ERR "ERROR: domain->groups does not match domain->child\n");
98 }
99
100 printk(KERN_CONT " }");
101
102 group = group->next;
103
104 if (group != sd->groups)
105 printk(KERN_CONT ",");
106
107 } while (group != sd->groups);
108 printk(KERN_CONT "\n");
109
110 if (!cpumask_equal(sched_domain_span(sd), groupmask))
111 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
112
113 if (sd->parent &&
114 !cpumask_subset(groupmask, sched_domain_span(sd->parent)))
115 printk(KERN_ERR "ERROR: parent span is not a superset of domain->span\n");
116 return 0;
117 }
118
119 static void sched_domain_debug(struct sched_domain *sd, int cpu)
120 {
121 int level = 0;
122
123 if (!sched_debug_enabled)
124 return;
125
126 if (!sd) {
127 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
128 return;
129 }
130
131 printk(KERN_DEBUG "CPU%d attaching sched-domain(s):\n", cpu);
132
133 for (;;) {
134 if (sched_domain_debug_one(sd, cpu, level, sched_domains_tmpmask))
135 break;
136 level++;
137 sd = sd->parent;
138 if (!sd)
139 break;
140 }
141 }
142 #else /* !CONFIG_SCHED_DEBUG */
143
144 # define sched_debug_enabled 0
145 # define sched_domain_debug(sd, cpu) do { } while (0)
146 static inline bool sched_debug(void)
147 {
148 return false;
149 }
150 #endif /* CONFIG_SCHED_DEBUG */
151
152 static int sd_degenerate(struct sched_domain *sd)
153 {
154 if (cpumask_weight(sched_domain_span(sd)) == 1)
155 return 1;
156
157 /* Following flags need at least 2 groups */
158 if (sd->flags & (SD_LOAD_BALANCE |
159 SD_BALANCE_NEWIDLE |
160 SD_BALANCE_FORK |
161 SD_BALANCE_EXEC |
162 SD_SHARE_CPUCAPACITY |
163 SD_ASYM_CPUCAPACITY |
164 SD_SHARE_PKG_RESOURCES |
165 SD_SHARE_POWERDOMAIN)) {
166 if (sd->groups != sd->groups->next)
167 return 0;
168 }
169
170 /* Following flags don't use groups */
171 if (sd->flags & (SD_WAKE_AFFINE))
172 return 0;
173
174 return 1;
175 }
176
177 static int
178 sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
179 {
180 unsigned long cflags = sd->flags, pflags = parent->flags;
181
182 if (sd_degenerate(parent))
183 return 1;
184
185 if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent)))
186 return 0;
187
188 /* Flags needing groups don't count if only 1 group in parent */
189 if (parent->groups == parent->groups->next) {
190 pflags &= ~(SD_LOAD_BALANCE |
191 SD_BALANCE_NEWIDLE |
192 SD_BALANCE_FORK |
193 SD_BALANCE_EXEC |
194 SD_ASYM_CPUCAPACITY |
195 SD_SHARE_CPUCAPACITY |
196 SD_SHARE_PKG_RESOURCES |
197 SD_PREFER_SIBLING |
198 SD_SHARE_POWERDOMAIN);
199 if (nr_node_ids == 1)
200 pflags &= ~SD_SERIALIZE;
201 }
202 if (~cflags & pflags)
203 return 0;
204
205 return 1;
206 }
207
208 static void free_rootdomain(struct rcu_head *rcu)
209 {
210 struct root_domain *rd = container_of(rcu, struct root_domain, rcu);
211
212 cpupri_cleanup(&rd->cpupri);
213 cpudl_cleanup(&rd->cpudl);
214 free_cpumask_var(rd->dlo_mask);
215 free_cpumask_var(rd->rto_mask);
216 free_cpumask_var(rd->online);
217 free_cpumask_var(rd->span);
218 kfree(rd);
219 }
220
221 void rq_attach_root(struct rq *rq, struct root_domain *rd)
222 {
223 struct root_domain *old_rd = NULL;
224 unsigned long flags;
225
226 raw_spin_lock_irqsave(&rq->lock, flags);
227
228 if (rq->rd) {
229 old_rd = rq->rd;
230
231 if (cpumask_test_cpu(rq->cpu, old_rd->online))
232 set_rq_offline(rq);
233
234 cpumask_clear_cpu(rq->cpu, old_rd->span);
235
236 /*
237 * If we dont want to free the old_rd yet then
238 * set old_rd to NULL to skip the freeing later
239 * in this function:
240 */
241 if (!atomic_dec_and_test(&old_rd->refcount))
242 old_rd = NULL;
243 }
244
245 atomic_inc(&rd->refcount);
246 rq->rd = rd;
247
248 cpumask_set_cpu(rq->cpu, rd->span);
249 if (cpumask_test_cpu(rq->cpu, cpu_active_mask))
250 set_rq_online(rq);
251
252 raw_spin_unlock_irqrestore(&rq->lock, flags);
253
254 if (old_rd)
255 call_rcu_sched(&old_rd->rcu, free_rootdomain);
256 }
257
258 void sched_get_rd(struct root_domain *rd)
259 {
260 atomic_inc(&rd->refcount);
261 }
262
263 void sched_put_rd(struct root_domain *rd)
264 {
265 if (!atomic_dec_and_test(&rd->refcount))
266 return;
267
268 call_rcu_sched(&rd->rcu, free_rootdomain);
269 }
270
271 static int init_rootdomain(struct root_domain *rd)
272 {
273 if (!zalloc_cpumask_var(&rd->span, GFP_KERNEL))
274 goto out;
275 if (!zalloc_cpumask_var(&rd->online, GFP_KERNEL))
276 goto free_span;
277 if (!zalloc_cpumask_var(&rd->dlo_mask, GFP_KERNEL))
278 goto free_online;
279 if (!zalloc_cpumask_var(&rd->rto_mask, GFP_KERNEL))
280 goto free_dlo_mask;
281
282 #ifdef HAVE_RT_PUSH_IPI
283 rd->rto_cpu = -1;
284 raw_spin_lock_init(&rd->rto_lock);
285 init_irq_work(&rd->rto_push_work, rto_push_irq_work_func);
286 #endif
287
288 init_dl_bw(&rd->dl_bw);
289 if (cpudl_init(&rd->cpudl) != 0)
290 goto free_rto_mask;
291
292 if (cpupri_init(&rd->cpupri) != 0)
293 goto free_cpudl;
294 return 0;
295
296 free_cpudl:
297 cpudl_cleanup(&rd->cpudl);
298 free_rto_mask:
299 free_cpumask_var(rd->rto_mask);
300 free_dlo_mask:
301 free_cpumask_var(rd->dlo_mask);
302 free_online:
303 free_cpumask_var(rd->online);
304 free_span:
305 free_cpumask_var(rd->span);
306 out:
307 return -ENOMEM;
308 }
309
310 /*
311 * By default the system creates a single root-domain with all CPUs as
312 * members (mimicking the global state we have today).
313 */
314 struct root_domain def_root_domain;
315
316 void init_defrootdomain(void)
317 {
318 init_rootdomain(&def_root_domain);
319
320 atomic_set(&def_root_domain.refcount, 1);
321 }
322
323 static struct root_domain *alloc_rootdomain(void)
324 {
325 struct root_domain *rd;
326
327 rd = kzalloc(sizeof(*rd), GFP_KERNEL);
328 if (!rd)
329 return NULL;
330
331 if (init_rootdomain(rd) != 0) {
332 kfree(rd);
333 return NULL;
334 }
335
336 return rd;
337 }
338
339 static void free_sched_groups(struct sched_group *sg, int free_sgc)
340 {
341 struct sched_group *tmp, *first;
342
343 if (!sg)
344 return;
345
346 first = sg;
347 do {
348 tmp = sg->next;
349
350 if (free_sgc && atomic_dec_and_test(&sg->sgc->ref))
351 kfree(sg->sgc);
352
353 if (atomic_dec_and_test(&sg->ref))
354 kfree(sg);
355 sg = tmp;
356 } while (sg != first);
357 }
358
359 static void destroy_sched_domain(struct sched_domain *sd)
360 {
361 /*
362 * A normal sched domain may have multiple group references, an
363 * overlapping domain, having private groups, only one. Iterate,
364 * dropping group/capacity references, freeing where none remain.
365 */
366 free_sched_groups(sd->groups, 1);
367
368 if (sd->shared && atomic_dec_and_test(&sd->shared->ref))
369 kfree(sd->shared);
370 kfree(sd);
371 }
372
373 static void destroy_sched_domains_rcu(struct rcu_head *rcu)
374 {
375 struct sched_domain *sd = container_of(rcu, struct sched_domain, rcu);
376
377 while (sd) {
378 struct sched_domain *parent = sd->parent;
379 destroy_sched_domain(sd);
380 sd = parent;
381 }
382 }
383
384 static void destroy_sched_domains(struct sched_domain *sd)
385 {
386 if (sd)
387 call_rcu(&sd->rcu, destroy_sched_domains_rcu);
388 }
389
390 /*
391 * Keep a special pointer to the highest sched_domain that has
392 * SD_SHARE_PKG_RESOURCE set (Last Level Cache Domain) for this
393 * allows us to avoid some pointer chasing select_idle_sibling().
394 *
395 * Also keep a unique ID per domain (we use the first CPU number in
396 * the cpumask of the domain), this allows us to quickly tell if
397 * two CPUs are in the same cache domain, see cpus_share_cache().
398 */
399 DEFINE_PER_CPU(struct sched_domain *, sd_llc);
400 DEFINE_PER_CPU(int, sd_llc_size);
401 DEFINE_PER_CPU(int, sd_llc_id);
402 DEFINE_PER_CPU(struct sched_domain_shared *, sd_llc_shared);
403 DEFINE_PER_CPU(struct sched_domain *, sd_numa);
404 DEFINE_PER_CPU(struct sched_domain *, sd_asym);
405
406 static void update_top_cache_domain(int cpu)
407 {
408 struct sched_domain_shared *sds = NULL;
409 struct sched_domain *sd;
410 int id = cpu;
411 int size = 1;
412
413 sd = highest_flag_domain(cpu, SD_SHARE_PKG_RESOURCES);
414 if (sd) {
415 id = cpumask_first(sched_domain_span(sd));
416 size = cpumask_weight(sched_domain_span(sd));
417 sds = sd->shared;
418 }
419
420 rcu_assign_pointer(per_cpu(sd_llc, cpu), sd);
421 per_cpu(sd_llc_size, cpu) = size;
422 per_cpu(sd_llc_id, cpu) = id;
423 rcu_assign_pointer(per_cpu(sd_llc_shared, cpu), sds);
424
425 sd = lowest_flag_domain(cpu, SD_NUMA);
426 rcu_assign_pointer(per_cpu(sd_numa, cpu), sd);
427
428 sd = highest_flag_domain(cpu, SD_ASYM_PACKING);
429 rcu_assign_pointer(per_cpu(sd_asym, cpu), sd);
430 }
431
432 /*
433 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
434 * hold the hotplug lock.
435 */
436 static void
437 cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
438 {
439 struct rq *rq = cpu_rq(cpu);
440 struct sched_domain *tmp;
441
442 /* Remove the sched domains which do not contribute to scheduling. */
443 for (tmp = sd; tmp; ) {
444 struct sched_domain *parent = tmp->parent;
445 if (!parent)
446 break;
447
448 if (sd_parent_degenerate(tmp, parent)) {
449 tmp->parent = parent->parent;
450 if (parent->parent)
451 parent->parent->child = tmp;
452 /*
453 * Transfer SD_PREFER_SIBLING down in case of a
454 * degenerate parent; the spans match for this
455 * so the property transfers.
456 */
457 if (parent->flags & SD_PREFER_SIBLING)
458 tmp->flags |= SD_PREFER_SIBLING;
459 destroy_sched_domain(parent);
460 } else
461 tmp = tmp->parent;
462 }
463
464 if (sd && sd_degenerate(sd)) {
465 tmp = sd;
466 sd = sd->parent;
467 destroy_sched_domain(tmp);
468 if (sd)
469 sd->child = NULL;
470 }
471
472 sched_domain_debug(sd, cpu);
473
474 rq_attach_root(rq, rd);
475 tmp = rq->sd;
476 rcu_assign_pointer(rq->sd, sd);
477 dirty_sched_domain_sysctl(cpu);
478 destroy_sched_domains(tmp);
479
480 update_top_cache_domain(cpu);
481 }
482
483 struct s_data {
484 struct sched_domain ** __percpu sd;
485 struct root_domain *rd;
486 };
487
488 enum s_alloc {
489 sa_rootdomain,
490 sa_sd,
491 sa_sd_storage,
492 sa_none,
493 };
494
495 /*
496 * Return the canonical balance CPU for this group, this is the first CPU
497 * of this group that's also in the balance mask.
498 *
499 * The balance mask are all those CPUs that could actually end up at this
500 * group. See build_balance_mask().
501 *
502 * Also see should_we_balance().
503 */
504 int group_balance_cpu(struct sched_group *sg)
505 {
506 return cpumask_first(group_balance_mask(sg));
507 }
508
509
510 /*
511 * NUMA topology (first read the regular topology blurb below)
512 *
513 * Given a node-distance table, for example:
514 *
515 * node 0 1 2 3
516 * 0: 10 20 30 20
517 * 1: 20 10 20 30
518 * 2: 30 20 10 20
519 * 3: 20 30 20 10
520 *
521 * which represents a 4 node ring topology like:
522 *
523 * 0 ----- 1
524 * | |
525 * | |
526 * | |
527 * 3 ----- 2
528 *
529 * We want to construct domains and groups to represent this. The way we go
530 * about doing this is to build the domains on 'hops'. For each NUMA level we
531 * construct the mask of all nodes reachable in @level hops.
532 *
533 * For the above NUMA topology that gives 3 levels:
534 *
535 * NUMA-2 0-3 0-3 0-3 0-3
536 * groups: {0-1,3},{1-3} {0-2},{0,2-3} {1-3},{0-1,3} {0,2-3},{0-2}
537 *
538 * NUMA-1 0-1,3 0-2 1-3 0,2-3
539 * groups: {0},{1},{3} {0},{1},{2} {1},{2},{3} {0},{2},{3}
540 *
541 * NUMA-0 0 1 2 3
542 *
543 *
544 * As can be seen; things don't nicely line up as with the regular topology.
545 * When we iterate a domain in child domain chunks some nodes can be
546 * represented multiple times -- hence the "overlap" naming for this part of
547 * the topology.
548 *
549 * In order to minimize this overlap, we only build enough groups to cover the
550 * domain. For instance Node-0 NUMA-2 would only get groups: 0-1,3 and 1-3.
551 *
552 * Because:
553 *
554 * - the first group of each domain is its child domain; this
555 * gets us the first 0-1,3
556 * - the only uncovered node is 2, who's child domain is 1-3.
557 *
558 * However, because of the overlap, computing a unique CPU for each group is
559 * more complicated. Consider for instance the groups of NODE-1 NUMA-2, both
560 * groups include the CPUs of Node-0, while those CPUs would not in fact ever
561 * end up at those groups (they would end up in group: 0-1,3).
562 *
563 * To correct this we have to introduce the group balance mask. This mask
564 * will contain those CPUs in the group that can reach this group given the
565 * (child) domain tree.
566 *
567 * With this we can once again compute balance_cpu and sched_group_capacity
568 * relations.
569 *
570 * XXX include words on how balance_cpu is unique and therefore can be
571 * used for sched_group_capacity links.
572 *
573 *
574 * Another 'interesting' topology is:
575 *
576 * node 0 1 2 3
577 * 0: 10 20 20 30
578 * 1: 20 10 20 20
579 * 2: 20 20 10 20
580 * 3: 30 20 20 10
581 *
582 * Which looks a little like:
583 *
584 * 0 ----- 1
585 * | / |
586 * | / |
587 * | / |
588 * 2 ----- 3
589 *
590 * This topology is asymmetric, nodes 1,2 are fully connected, but nodes 0,3
591 * are not.
592 *
593 * This leads to a few particularly weird cases where the sched_domain's are
594 * not of the same number for each CPU. Consider:
595 *
596 * NUMA-2 0-3 0-3
597 * groups: {0-2},{1-3} {1-3},{0-2}
598 *
599 * NUMA-1 0-2 0-3 0-3 1-3
600 *
601 * NUMA-0 0 1 2 3
602 *
603 */
604
605
606 /*
607 * Build the balance mask; it contains only those CPUs that can arrive at this
608 * group and should be considered to continue balancing.
609 *
610 * We do this during the group creation pass, therefore the group information
611 * isn't complete yet, however since each group represents a (child) domain we
612 * can fully construct this using the sched_domain bits (which are already
613 * complete).
614 */
615 static void
616 build_balance_mask(struct sched_domain *sd, struct sched_group *sg, struct cpumask *mask)
617 {
618 const struct cpumask *sg_span = sched_group_span(sg);
619 struct sd_data *sdd = sd->private;
620 struct sched_domain *sibling;
621 int i;
622
623 cpumask_clear(mask);
624
625 for_each_cpu(i, sg_span) {
626 sibling = *per_cpu_ptr(sdd->sd, i);
627
628 /*
629 * Can happen in the asymmetric case, where these siblings are
630 * unused. The mask will not be empty because those CPUs that
631 * do have the top domain _should_ span the domain.
632 */
633 if (!sibling->child)
634 continue;
635
636 /* If we would not end up here, we can't continue from here */
637 if (!cpumask_equal(sg_span, sched_domain_span(sibling->child)))
638 continue;
639
640 cpumask_set_cpu(i, mask);
641 }
642
643 /* We must not have empty masks here */
644 WARN_ON_ONCE(cpumask_empty(mask));
645 }
646
647 /*
648 * XXX: This creates per-node group entries; since the load-balancer will
649 * immediately access remote memory to construct this group's load-balance
650 * statistics having the groups node local is of dubious benefit.
651 */
652 static struct sched_group *
653 build_group_from_child_sched_domain(struct sched_domain *sd, int cpu)
654 {
655 struct sched_group *sg;
656 struct cpumask *sg_span;
657
658 sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
659 GFP_KERNEL, cpu_to_node(cpu));
660
661 if (!sg)
662 return NULL;
663
664 sg_span = sched_group_span(sg);
665 if (sd->child)
666 cpumask_copy(sg_span, sched_domain_span(sd->child));
667 else
668 cpumask_copy(sg_span, sched_domain_span(sd));
669
670 atomic_inc(&sg->ref);
671 return sg;
672 }
673
674 static void init_overlap_sched_group(struct sched_domain *sd,
675 struct sched_group *sg)
676 {
677 struct cpumask *mask = sched_domains_tmpmask2;
678 struct sd_data *sdd = sd->private;
679 struct cpumask *sg_span;
680 int cpu;
681
682 build_balance_mask(sd, sg, mask);
683 cpu = cpumask_first_and(sched_group_span(sg), mask);
684
685 sg->sgc = *per_cpu_ptr(sdd->sgc, cpu);
686 if (atomic_inc_return(&sg->sgc->ref) == 1)
687 cpumask_copy(group_balance_mask(sg), mask);
688 else
689 WARN_ON_ONCE(!cpumask_equal(group_balance_mask(sg), mask));
690
691 /*
692 * Initialize sgc->capacity such that even if we mess up the
693 * domains and no possible iteration will get us here, we won't
694 * die on a /0 trap.
695 */
696 sg_span = sched_group_span(sg);
697 sg->sgc->capacity = SCHED_CAPACITY_SCALE * cpumask_weight(sg_span);
698 sg->sgc->min_capacity = SCHED_CAPACITY_SCALE;
699 }
700
701 static int
702 build_overlap_sched_groups(struct sched_domain *sd, int cpu)
703 {
704 struct sched_group *first = NULL, *last = NULL, *sg;
705 const struct cpumask *span = sched_domain_span(sd);
706 struct cpumask *covered = sched_domains_tmpmask;
707 struct sd_data *sdd = sd->private;
708 struct sched_domain *sibling;
709 int i;
710
711 cpumask_clear(covered);
712
713 for_each_cpu_wrap(i, span, cpu) {
714 struct cpumask *sg_span;
715
716 if (cpumask_test_cpu(i, covered))
717 continue;
718
719 sibling = *per_cpu_ptr(sdd->sd, i);
720
721 /*
722 * Asymmetric node setups can result in situations where the
723 * domain tree is of unequal depth, make sure to skip domains
724 * that already cover the entire range.
725 *
726 * In that case build_sched_domains() will have terminated the
727 * iteration early and our sibling sd spans will be empty.
728 * Domains should always include the CPU they're built on, so
729 * check that.
730 */
731 if (!cpumask_test_cpu(i, sched_domain_span(sibling)))
732 continue;
733
734 sg = build_group_from_child_sched_domain(sibling, cpu);
735 if (!sg)
736 goto fail;
737
738 sg_span = sched_group_span(sg);
739 cpumask_or(covered, covered, sg_span);
740
741 init_overlap_sched_group(sd, sg);
742
743 if (!first)
744 first = sg;
745 if (last)
746 last->next = sg;
747 last = sg;
748 last->next = first;
749 }
750 sd->groups = first;
751
752 return 0;
753
754 fail:
755 free_sched_groups(first, 0);
756
757 return -ENOMEM;
758 }
759
760
761 /*
762 * Package topology (also see the load-balance blurb in fair.c)
763 *
764 * The scheduler builds a tree structure to represent a number of important
765 * topology features. By default (default_topology[]) these include:
766 *
767 * - Simultaneous multithreading (SMT)
768 * - Multi-Core Cache (MC)
769 * - Package (DIE)
770 *
771 * Where the last one more or less denotes everything up to a NUMA node.
772 *
773 * The tree consists of 3 primary data structures:
774 *
775 * sched_domain -> sched_group -> sched_group_capacity
776 * ^ ^ ^ ^
777 * `-' `-'
778 *
779 * The sched_domains are per-CPU and have a two way link (parent & child) and
780 * denote the ever growing mask of CPUs belonging to that level of topology.
781 *
782 * Each sched_domain has a circular (double) linked list of sched_group's, each
783 * denoting the domains of the level below (or individual CPUs in case of the
784 * first domain level). The sched_group linked by a sched_domain includes the
785 * CPU of that sched_domain [*].
786 *
787 * Take for instance a 2 threaded, 2 core, 2 cache cluster part:
788 *
789 * CPU 0 1 2 3 4 5 6 7
790 *
791 * DIE [ ]
792 * MC [ ] [ ]
793 * SMT [ ] [ ] [ ] [ ]
794 *
795 * - or -
796 *
797 * DIE 0-7 0-7 0-7 0-7 0-7 0-7 0-7 0-7
798 * MC 0-3 0-3 0-3 0-3 4-7 4-7 4-7 4-7
799 * SMT 0-1 0-1 2-3 2-3 4-5 4-5 6-7 6-7
800 *
801 * CPU 0 1 2 3 4 5 6 7
802 *
803 * One way to think about it is: sched_domain moves you up and down among these
804 * topology levels, while sched_group moves you sideways through it, at child
805 * domain granularity.
806 *
807 * sched_group_capacity ensures each unique sched_group has shared storage.
808 *
809 * There are two related construction problems, both require a CPU that
810 * uniquely identify each group (for a given domain):
811 *
812 * - The first is the balance_cpu (see should_we_balance() and the
813 * load-balance blub in fair.c); for each group we only want 1 CPU to
814 * continue balancing at a higher domain.
815 *
816 * - The second is the sched_group_capacity; we want all identical groups
817 * to share a single sched_group_capacity.
818 *
819 * Since these topologies are exclusive by construction. That is, its
820 * impossible for an SMT thread to belong to multiple cores, and cores to
821 * be part of multiple caches. There is a very clear and unique location
822 * for each CPU in the hierarchy.
823 *
824 * Therefore computing a unique CPU for each group is trivial (the iteration
825 * mask is redundant and set all 1s; all CPUs in a group will end up at _that_
826 * group), we can simply pick the first CPU in each group.
827 *
828 *
829 * [*] in other words, the first group of each domain is its child domain.
830 */
831
832 static struct sched_group *get_group(int cpu, struct sd_data *sdd)
833 {
834 struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu);
835 struct sched_domain *child = sd->child;
836 struct sched_group *sg;
837
838 if (child)
839 cpu = cpumask_first(sched_domain_span(child));
840
841 sg = *per_cpu_ptr(sdd->sg, cpu);
842 sg->sgc = *per_cpu_ptr(sdd->sgc, cpu);
843
844 /* For claim_allocations: */
845 atomic_inc(&sg->ref);
846 atomic_inc(&sg->sgc->ref);
847
848 if (child) {
849 cpumask_copy(sched_group_span(sg), sched_domain_span(child));
850 cpumask_copy(group_balance_mask(sg), sched_group_span(sg));
851 } else {
852 cpumask_set_cpu(cpu, sched_group_span(sg));
853 cpumask_set_cpu(cpu, group_balance_mask(sg));
854 }
855
856 sg->sgc->capacity = SCHED_CAPACITY_SCALE * cpumask_weight(sched_group_span(sg));
857 sg->sgc->min_capacity = SCHED_CAPACITY_SCALE;
858
859 return sg;
860 }
861
862 /*
863 * build_sched_groups will build a circular linked list of the groups
864 * covered by the given span, and will set each group's ->cpumask correctly,
865 * and ->cpu_capacity to 0.
866 *
867 * Assumes the sched_domain tree is fully constructed
868 */
869 static int
870 build_sched_groups(struct sched_domain *sd, int cpu)
871 {
872 struct sched_group *first = NULL, *last = NULL;
873 struct sd_data *sdd = sd->private;
874 const struct cpumask *span = sched_domain_span(sd);
875 struct cpumask *covered;
876 int i;
877
878 lockdep_assert_held(&sched_domains_mutex);
879 covered = sched_domains_tmpmask;
880
881 cpumask_clear(covered);
882
883 for_each_cpu_wrap(i, span, cpu) {
884 struct sched_group *sg;
885
886 if (cpumask_test_cpu(i, covered))
887 continue;
888
889 sg = get_group(i, sdd);
890
891 cpumask_or(covered, covered, sched_group_span(sg));
892
893 if (!first)
894 first = sg;
895 if (last)
896 last->next = sg;
897 last = sg;
898 }
899 last->next = first;
900 sd->groups = first;
901
902 return 0;
903 }
904
905 /*
906 * Initialize sched groups cpu_capacity.
907 *
908 * cpu_capacity indicates the capacity of sched group, which is used while
909 * distributing the load between different sched groups in a sched domain.
910 * Typically cpu_capacity for all the groups in a sched domain will be same
911 * unless there are asymmetries in the topology. If there are asymmetries,
912 * group having more cpu_capacity will pickup more load compared to the
913 * group having less cpu_capacity.
914 */
915 static void init_sched_groups_capacity(int cpu, struct sched_domain *sd)
916 {
917 struct sched_group *sg = sd->groups;
918
919 WARN_ON(!sg);
920
921 do {
922 int cpu, max_cpu = -1;
923
924 sg->group_weight = cpumask_weight(sched_group_span(sg));
925
926 if (!(sd->flags & SD_ASYM_PACKING))
927 goto next;
928
929 for_each_cpu(cpu, sched_group_span(sg)) {
930 if (max_cpu < 0)
931 max_cpu = cpu;
932 else if (sched_asym_prefer(cpu, max_cpu))
933 max_cpu = cpu;
934 }
935 sg->asym_prefer_cpu = max_cpu;
936
937 next:
938 sg = sg->next;
939 } while (sg != sd->groups);
940
941 if (cpu != group_balance_cpu(sg))
942 return;
943
944 update_group_capacity(sd, cpu);
945 }
946
947 /*
948 * Initializers for schedule domains
949 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
950 */
951
952 static int default_relax_domain_level = -1;
953 int sched_domain_level_max;
954
955 static int __init setup_relax_domain_level(char *str)
956 {
957 if (kstrtoint(str, 0, &default_relax_domain_level))
958 pr_warn("Unable to set relax_domain_level\n");
959
960 return 1;
961 }
962 __setup("relax_domain_level=", setup_relax_domain_level);
963
964 static void set_domain_attribute(struct sched_domain *sd,
965 struct sched_domain_attr *attr)
966 {
967 int request;
968
969 if (!attr || attr->relax_domain_level < 0) {
970 if (default_relax_domain_level < 0)
971 return;
972 else
973 request = default_relax_domain_level;
974 } else
975 request = attr->relax_domain_level;
976 if (request < sd->level) {
977 /* Turn off idle balance on this domain: */
978 sd->flags &= ~(SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
979 } else {
980 /* Turn on idle balance on this domain: */
981 sd->flags |= (SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
982 }
983 }
984
985 static void __sdt_free(const struct cpumask *cpu_map);
986 static int __sdt_alloc(const struct cpumask *cpu_map);
987
988 static void __free_domain_allocs(struct s_data *d, enum s_alloc what,
989 const struct cpumask *cpu_map)
990 {
991 switch (what) {
992 case sa_rootdomain:
993 if (!atomic_read(&d->rd->refcount))
994 free_rootdomain(&d->rd->rcu);
995 /* Fall through */
996 case sa_sd:
997 free_percpu(d->sd);
998 /* Fall through */
999 case sa_sd_storage:
1000 __sdt_free(cpu_map);
1001 /* Fall through */
1002 case sa_none:
1003 break;
1004 }
1005 }
1006
1007 static enum s_alloc
1008 __visit_domain_allocation_hell(struct s_data *d, const struct cpumask *cpu_map)
1009 {
1010 memset(d, 0, sizeof(*d));
1011
1012 if (__sdt_alloc(cpu_map))
1013 return sa_sd_storage;
1014 d->sd = alloc_percpu(struct sched_domain *);
1015 if (!d->sd)
1016 return sa_sd_storage;
1017 d->rd = alloc_rootdomain();
1018 if (!d->rd)
1019 return sa_sd;
1020
1021 return sa_rootdomain;
1022 }
1023
1024 /*
1025 * NULL the sd_data elements we've used to build the sched_domain and
1026 * sched_group structure so that the subsequent __free_domain_allocs()
1027 * will not free the data we're using.
1028 */
1029 static void claim_allocations(int cpu, struct sched_domain *sd)
1030 {
1031 struct sd_data *sdd = sd->private;
1032
1033 WARN_ON_ONCE(*per_cpu_ptr(sdd->sd, cpu) != sd);
1034 *per_cpu_ptr(sdd->sd, cpu) = NULL;
1035
1036 if (atomic_read(&(*per_cpu_ptr(sdd->sds, cpu))->ref))
1037 *per_cpu_ptr(sdd->sds, cpu) = NULL;
1038
1039 if (atomic_read(&(*per_cpu_ptr(sdd->sg, cpu))->ref))
1040 *per_cpu_ptr(sdd->sg, cpu) = NULL;
1041
1042 if (atomic_read(&(*per_cpu_ptr(sdd->sgc, cpu))->ref))
1043 *per_cpu_ptr(sdd->sgc, cpu) = NULL;
1044 }
1045
1046 #ifdef CONFIG_NUMA
1047 enum numa_topology_type sched_numa_topology_type;
1048
1049 static int sched_domains_numa_levels;
1050 static int sched_domains_curr_level;
1051
1052 int sched_max_numa_distance;
1053 static int *sched_domains_numa_distance;
1054 static struct cpumask ***sched_domains_numa_masks;
1055 #endif
1056
1057 /*
1058 * SD_flags allowed in topology descriptions.
1059 *
1060 * These flags are purely descriptive of the topology and do not prescribe
1061 * behaviour. Behaviour is artificial and mapped in the below sd_init()
1062 * function:
1063 *
1064 * SD_SHARE_CPUCAPACITY - describes SMT topologies
1065 * SD_SHARE_PKG_RESOURCES - describes shared caches
1066 * SD_NUMA - describes NUMA topologies
1067 * SD_SHARE_POWERDOMAIN - describes shared power domain
1068 * SD_ASYM_CPUCAPACITY - describes mixed capacity topologies
1069 *
1070 * Odd one out, which beside describing the topology has a quirk also
1071 * prescribes the desired behaviour that goes along with it:
1072 *
1073 * SD_ASYM_PACKING - describes SMT quirks
1074 */
1075 #define TOPOLOGY_SD_FLAGS \
1076 (SD_SHARE_CPUCAPACITY | \
1077 SD_SHARE_PKG_RESOURCES | \
1078 SD_NUMA | \
1079 SD_ASYM_PACKING | \
1080 SD_ASYM_CPUCAPACITY | \
1081 SD_SHARE_POWERDOMAIN)
1082
1083 static struct sched_domain *
1084 sd_init(struct sched_domain_topology_level *tl,
1085 const struct cpumask *cpu_map,
1086 struct sched_domain *child, int cpu)
1087 {
1088 struct sd_data *sdd = &tl->data;
1089 struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu);
1090 int sd_id, sd_weight, sd_flags = 0;
1091
1092 #ifdef CONFIG_NUMA
1093 /*
1094 * Ugly hack to pass state to sd_numa_mask()...
1095 */
1096 sched_domains_curr_level = tl->numa_level;
1097 #endif
1098
1099 sd_weight = cpumask_weight(tl->mask(cpu));
1100
1101 if (tl->sd_flags)
1102 sd_flags = (*tl->sd_flags)();
1103 if (WARN_ONCE(sd_flags & ~TOPOLOGY_SD_FLAGS,
1104 "wrong sd_flags in topology description\n"))
1105 sd_flags &= ~TOPOLOGY_SD_FLAGS;
1106
1107 *sd = (struct sched_domain){
1108 .min_interval = sd_weight,
1109 .max_interval = 2*sd_weight,
1110 .busy_factor = 32,
1111 .imbalance_pct = 125,
1112
1113 .cache_nice_tries = 0,
1114 .busy_idx = 0,
1115 .idle_idx = 0,
1116 .newidle_idx = 0,
1117 .wake_idx = 0,
1118 .forkexec_idx = 0,
1119
1120 .flags = 1*SD_LOAD_BALANCE
1121 | 1*SD_BALANCE_NEWIDLE
1122 | 1*SD_BALANCE_EXEC
1123 | 1*SD_BALANCE_FORK
1124 | 0*SD_BALANCE_WAKE
1125 | 1*SD_WAKE_AFFINE
1126 | 0*SD_SHARE_CPUCAPACITY
1127 | 0*SD_SHARE_PKG_RESOURCES
1128 | 0*SD_SERIALIZE
1129 | 0*SD_PREFER_SIBLING
1130 | 0*SD_NUMA
1131 | sd_flags
1132 ,
1133
1134 .last_balance = jiffies,
1135 .balance_interval = sd_weight,
1136 .smt_gain = 0,
1137 .max_newidle_lb_cost = 0,
1138 .next_decay_max_lb_cost = jiffies,
1139 .child = child,
1140 #ifdef CONFIG_SCHED_DEBUG
1141 .name = tl->name,
1142 #endif
1143 };
1144
1145 cpumask_and(sched_domain_span(sd), cpu_map, tl->mask(cpu));
1146 sd_id = cpumask_first(sched_domain_span(sd));
1147
1148 /*
1149 * Convert topological properties into behaviour.
1150 */
1151
1152 if (sd->flags & SD_ASYM_CPUCAPACITY) {
1153 struct sched_domain *t = sd;
1154
1155 for_each_lower_domain(t)
1156 t->flags |= SD_BALANCE_WAKE;
1157 }
1158
1159 if (sd->flags & SD_SHARE_CPUCAPACITY) {
1160 sd->flags |= SD_PREFER_SIBLING;
1161 sd->imbalance_pct = 110;
1162 sd->smt_gain = 1178; /* ~15% */
1163
1164 } else if (sd->flags & SD_SHARE_PKG_RESOURCES) {
1165 sd->flags |= SD_PREFER_SIBLING;
1166 sd->imbalance_pct = 117;
1167 sd->cache_nice_tries = 1;
1168 sd->busy_idx = 2;
1169
1170 #ifdef CONFIG_NUMA
1171 } else if (sd->flags & SD_NUMA) {
1172 sd->cache_nice_tries = 2;
1173 sd->busy_idx = 3;
1174 sd->idle_idx = 2;
1175
1176 sd->flags |= SD_SERIALIZE;
1177 if (sched_domains_numa_distance[tl->numa_level] > RECLAIM_DISTANCE) {
1178 sd->flags &= ~(SD_BALANCE_EXEC |
1179 SD_BALANCE_FORK |
1180 SD_WAKE_AFFINE);
1181 }
1182
1183 #endif
1184 } else {
1185 sd->flags |= SD_PREFER_SIBLING;
1186 sd->cache_nice_tries = 1;
1187 sd->busy_idx = 2;
1188 sd->idle_idx = 1;
1189 }
1190
1191 /*
1192 * For all levels sharing cache; connect a sched_domain_shared
1193 * instance.
1194 */
1195 if (sd->flags & SD_SHARE_PKG_RESOURCES) {
1196 sd->shared = *per_cpu_ptr(sdd->sds, sd_id);
1197 atomic_inc(&sd->shared->ref);
1198 atomic_set(&sd->shared->nr_busy_cpus, sd_weight);
1199 }
1200
1201 sd->private = sdd;
1202
1203 return sd;
1204 }
1205
1206 /*
1207 * Topology list, bottom-up.
1208 */
1209 static struct sched_domain_topology_level default_topology[] = {
1210 #ifdef CONFIG_SCHED_SMT
1211 { cpu_smt_mask, cpu_smt_flags, SD_INIT_NAME(SMT) },
1212 #endif
1213 #ifdef CONFIG_SCHED_MC
1214 { cpu_coregroup_mask, cpu_core_flags, SD_INIT_NAME(MC) },
1215 #endif
1216 { cpu_cpu_mask, SD_INIT_NAME(DIE) },
1217 { NULL, },
1218 };
1219
1220 static struct sched_domain_topology_level *sched_domain_topology =
1221 default_topology;
1222
1223 #define for_each_sd_topology(tl) \
1224 for (tl = sched_domain_topology; tl->mask; tl++)
1225
1226 void set_sched_topology(struct sched_domain_topology_level *tl)
1227 {
1228 if (WARN_ON_ONCE(sched_smp_initialized))
1229 return;
1230
1231 sched_domain_topology = tl;
1232 }
1233
1234 #ifdef CONFIG_NUMA
1235
1236 static const struct cpumask *sd_numa_mask(int cpu)
1237 {
1238 return sched_domains_numa_masks[sched_domains_curr_level][cpu_to_node(cpu)];
1239 }
1240
1241 static void sched_numa_warn(const char *str)
1242 {
1243 static int done = false;
1244 int i,j;
1245
1246 if (done)
1247 return;
1248
1249 done = true;
1250
1251 printk(KERN_WARNING "ERROR: %s\n\n", str);
1252
1253 for (i = 0; i < nr_node_ids; i++) {
1254 printk(KERN_WARNING " ");
1255 for (j = 0; j < nr_node_ids; j++)
1256 printk(KERN_CONT "%02d ", node_distance(i,j));
1257 printk(KERN_CONT "\n");
1258 }
1259 printk(KERN_WARNING "\n");
1260 }
1261
1262 bool find_numa_distance(int distance)
1263 {
1264 int i;
1265
1266 if (distance == node_distance(0, 0))
1267 return true;
1268
1269 for (i = 0; i < sched_domains_numa_levels; i++) {
1270 if (sched_domains_numa_distance[i] == distance)
1271 return true;
1272 }
1273
1274 return false;
1275 }
1276
1277 /*
1278 * A system can have three types of NUMA topology:
1279 * NUMA_DIRECT: all nodes are directly connected, or not a NUMA system
1280 * NUMA_GLUELESS_MESH: some nodes reachable through intermediary nodes
1281 * NUMA_BACKPLANE: nodes can reach other nodes through a backplane
1282 *
1283 * The difference between a glueless mesh topology and a backplane
1284 * topology lies in whether communication between not directly
1285 * connected nodes goes through intermediary nodes (where programs
1286 * could run), or through backplane controllers. This affects
1287 * placement of programs.
1288 *
1289 * The type of topology can be discerned with the following tests:
1290 * - If the maximum distance between any nodes is 1 hop, the system
1291 * is directly connected.
1292 * - If for two nodes A and B, located N > 1 hops away from each other,
1293 * there is an intermediary node C, which is < N hops away from both
1294 * nodes A and B, the system is a glueless mesh.
1295 */
1296 static void init_numa_topology_type(void)
1297 {
1298 int a, b, c, n;
1299
1300 n = sched_max_numa_distance;
1301
1302 if (sched_domains_numa_levels <= 1) {
1303 sched_numa_topology_type = NUMA_DIRECT;
1304 return;
1305 }
1306
1307 for_each_online_node(a) {
1308 for_each_online_node(b) {
1309 /* Find two nodes furthest removed from each other. */
1310 if (node_distance(a, b) < n)
1311 continue;
1312
1313 /* Is there an intermediary node between a and b? */
1314 for_each_online_node(c) {
1315 if (node_distance(a, c) < n &&
1316 node_distance(b, c) < n) {
1317 sched_numa_topology_type =
1318 NUMA_GLUELESS_MESH;
1319 return;
1320 }
1321 }
1322
1323 sched_numa_topology_type = NUMA_BACKPLANE;
1324 return;
1325 }
1326 }
1327 }
1328
1329 void sched_init_numa(void)
1330 {
1331 int next_distance, curr_distance = node_distance(0, 0);
1332 struct sched_domain_topology_level *tl;
1333 int level = 0;
1334 int i, j, k;
1335
1336 sched_domains_numa_distance = kzalloc(sizeof(int) * nr_node_ids, GFP_KERNEL);
1337 if (!sched_domains_numa_distance)
1338 return;
1339
1340 /* Includes NUMA identity node at level 0. */
1341 sched_domains_numa_distance[level++] = curr_distance;
1342 sched_domains_numa_levels = level;
1343
1344 /*
1345 * O(nr_nodes^2) deduplicating selection sort -- in order to find the
1346 * unique distances in the node_distance() table.
1347 *
1348 * Assumes node_distance(0,j) includes all distances in
1349 * node_distance(i,j) in order to avoid cubic time.
1350 */
1351 next_distance = curr_distance;
1352 for (i = 0; i < nr_node_ids; i++) {
1353 for (j = 0; j < nr_node_ids; j++) {
1354 for (k = 0; k < nr_node_ids; k++) {
1355 int distance = node_distance(i, k);
1356
1357 if (distance > curr_distance &&
1358 (distance < next_distance ||
1359 next_distance == curr_distance))
1360 next_distance = distance;
1361
1362 /*
1363 * While not a strong assumption it would be nice to know
1364 * about cases where if node A is connected to B, B is not
1365 * equally connected to A.
1366 */
1367 if (sched_debug() && node_distance(k, i) != distance)
1368 sched_numa_warn("Node-distance not symmetric");
1369
1370 if (sched_debug() && i && !find_numa_distance(distance))
1371 sched_numa_warn("Node-0 not representative");
1372 }
1373 if (next_distance != curr_distance) {
1374 sched_domains_numa_distance[level++] = next_distance;
1375 sched_domains_numa_levels = level;
1376 curr_distance = next_distance;
1377 } else break;
1378 }
1379
1380 /*
1381 * In case of sched_debug() we verify the above assumption.
1382 */
1383 if (!sched_debug())
1384 break;
1385 }
1386
1387 if (!level)
1388 return;
1389
1390 /*
1391 * 'level' contains the number of unique distances
1392 *
1393 * The sched_domains_numa_distance[] array includes the actual distance
1394 * numbers.
1395 */
1396
1397 /*
1398 * Here, we should temporarily reset sched_domains_numa_levels to 0.
1399 * If it fails to allocate memory for array sched_domains_numa_masks[][],
1400 * the array will contain less then 'level' members. This could be
1401 * dangerous when we use it to iterate array sched_domains_numa_masks[][]
1402 * in other functions.
1403 *
1404 * We reset it to 'level' at the end of this function.
1405 */
1406 sched_domains_numa_levels = 0;
1407
1408 sched_domains_numa_masks = kzalloc(sizeof(void *) * level, GFP_KERNEL);
1409 if (!sched_domains_numa_masks)
1410 return;
1411
1412 /*
1413 * Now for each level, construct a mask per node which contains all
1414 * CPUs of nodes that are that many hops away from us.
1415 */
1416 for (i = 0; i < level; i++) {
1417 sched_domains_numa_masks[i] =
1418 kzalloc(nr_node_ids * sizeof(void *), GFP_KERNEL);
1419 if (!sched_domains_numa_masks[i])
1420 return;
1421
1422 for (j = 0; j < nr_node_ids; j++) {
1423 struct cpumask *mask = kzalloc(cpumask_size(), GFP_KERNEL);
1424 if (!mask)
1425 return;
1426
1427 sched_domains_numa_masks[i][j] = mask;
1428
1429 for_each_node(k) {
1430 if (node_distance(j, k) > sched_domains_numa_distance[i])
1431 continue;
1432
1433 cpumask_or(mask, mask, cpumask_of_node(k));
1434 }
1435 }
1436 }
1437
1438 /* Compute default topology size */
1439 for (i = 0; sched_domain_topology[i].mask; i++);
1440
1441 tl = kzalloc((i + level + 1) *
1442 sizeof(struct sched_domain_topology_level), GFP_KERNEL);
1443 if (!tl)
1444 return;
1445
1446 /*
1447 * Copy the default topology bits..
1448 */
1449 for (i = 0; sched_domain_topology[i].mask; i++)
1450 tl[i] = sched_domain_topology[i];
1451
1452 /*
1453 * Add the NUMA identity distance, aka single NODE.
1454 */
1455 tl[i++] = (struct sched_domain_topology_level){
1456 .mask = sd_numa_mask,
1457 .numa_level = 0,
1458 SD_INIT_NAME(NODE)
1459 };
1460
1461 /*
1462 * .. and append 'j' levels of NUMA goodness.
1463 */
1464 for (j = 1; j < level; i++, j++) {
1465 tl[i] = (struct sched_domain_topology_level){
1466 .mask = sd_numa_mask,
1467 .sd_flags = cpu_numa_flags,
1468 .flags = SDTL_OVERLAP,
1469 .numa_level = j,
1470 SD_INIT_NAME(NUMA)
1471 };
1472 }
1473
1474 sched_domain_topology = tl;
1475
1476 sched_domains_numa_levels = level;
1477 sched_max_numa_distance = sched_domains_numa_distance[level - 1];
1478
1479 init_numa_topology_type();
1480 }
1481
1482 void sched_domains_numa_masks_set(unsigned int cpu)
1483 {
1484 int node = cpu_to_node(cpu);
1485 int i, j;
1486
1487 for (i = 0; i < sched_domains_numa_levels; i++) {
1488 for (j = 0; j < nr_node_ids; j++) {
1489 if (node_distance(j, node) <= sched_domains_numa_distance[i])
1490 cpumask_set_cpu(cpu, sched_domains_numa_masks[i][j]);
1491 }
1492 }
1493 }
1494
1495 void sched_domains_numa_masks_clear(unsigned int cpu)
1496 {
1497 int i, j;
1498
1499 for (i = 0; i < sched_domains_numa_levels; i++) {
1500 for (j = 0; j < nr_node_ids; j++)
1501 cpumask_clear_cpu(cpu, sched_domains_numa_masks[i][j]);
1502 }
1503 }
1504
1505 #endif /* CONFIG_NUMA */
1506
1507 static int __sdt_alloc(const struct cpumask *cpu_map)
1508 {
1509 struct sched_domain_topology_level *tl;
1510 int j;
1511
1512 for_each_sd_topology(tl) {
1513 struct sd_data *sdd = &tl->data;
1514
1515 sdd->sd = alloc_percpu(struct sched_domain *);
1516 if (!sdd->sd)
1517 return -ENOMEM;
1518
1519 sdd->sds = alloc_percpu(struct sched_domain_shared *);
1520 if (!sdd->sds)
1521 return -ENOMEM;
1522
1523 sdd->sg = alloc_percpu(struct sched_group *);
1524 if (!sdd->sg)
1525 return -ENOMEM;
1526
1527 sdd->sgc = alloc_percpu(struct sched_group_capacity *);
1528 if (!sdd->sgc)
1529 return -ENOMEM;
1530
1531 for_each_cpu(j, cpu_map) {
1532 struct sched_domain *sd;
1533 struct sched_domain_shared *sds;
1534 struct sched_group *sg;
1535 struct sched_group_capacity *sgc;
1536
1537 sd = kzalloc_node(sizeof(struct sched_domain) + cpumask_size(),
1538 GFP_KERNEL, cpu_to_node(j));
1539 if (!sd)
1540 return -ENOMEM;
1541
1542 *per_cpu_ptr(sdd->sd, j) = sd;
1543
1544 sds = kzalloc_node(sizeof(struct sched_domain_shared),
1545 GFP_KERNEL, cpu_to_node(j));
1546 if (!sds)
1547 return -ENOMEM;
1548
1549 *per_cpu_ptr(sdd->sds, j) = sds;
1550
1551 sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
1552 GFP_KERNEL, cpu_to_node(j));
1553 if (!sg)
1554 return -ENOMEM;
1555
1556 sg->next = sg;
1557
1558 *per_cpu_ptr(sdd->sg, j) = sg;
1559
1560 sgc = kzalloc_node(sizeof(struct sched_group_capacity) + cpumask_size(),
1561 GFP_KERNEL, cpu_to_node(j));
1562 if (!sgc)
1563 return -ENOMEM;
1564
1565 #ifdef CONFIG_SCHED_DEBUG
1566 sgc->id = j;
1567 #endif
1568
1569 *per_cpu_ptr(sdd->sgc, j) = sgc;
1570 }
1571 }
1572
1573 return 0;
1574 }
1575
1576 static void __sdt_free(const struct cpumask *cpu_map)
1577 {
1578 struct sched_domain_topology_level *tl;
1579 int j;
1580
1581 for_each_sd_topology(tl) {
1582 struct sd_data *sdd = &tl->data;
1583
1584 for_each_cpu(j, cpu_map) {
1585 struct sched_domain *sd;
1586
1587 if (sdd->sd) {
1588 sd = *per_cpu_ptr(sdd->sd, j);
1589 if (sd && (sd->flags & SD_OVERLAP))
1590 free_sched_groups(sd->groups, 0);
1591 kfree(*per_cpu_ptr(sdd->sd, j));
1592 }
1593
1594 if (sdd->sds)
1595 kfree(*per_cpu_ptr(sdd->sds, j));
1596 if (sdd->sg)
1597 kfree(*per_cpu_ptr(sdd->sg, j));
1598 if (sdd->sgc)
1599 kfree(*per_cpu_ptr(sdd->sgc, j));
1600 }
1601 free_percpu(sdd->sd);
1602 sdd->sd = NULL;
1603 free_percpu(sdd->sds);
1604 sdd->sds = NULL;
1605 free_percpu(sdd->sg);
1606 sdd->sg = NULL;
1607 free_percpu(sdd->sgc);
1608 sdd->sgc = NULL;
1609 }
1610 }
1611
1612 static struct sched_domain *build_sched_domain(struct sched_domain_topology_level *tl,
1613 const struct cpumask *cpu_map, struct sched_domain_attr *attr,
1614 struct sched_domain *child, int cpu)
1615 {
1616 struct sched_domain *sd = sd_init(tl, cpu_map, child, cpu);
1617
1618 if (child) {
1619 sd->level = child->level + 1;
1620 sched_domain_level_max = max(sched_domain_level_max, sd->level);
1621 child->parent = sd;
1622
1623 if (!cpumask_subset(sched_domain_span(child),
1624 sched_domain_span(sd))) {
1625 pr_err("BUG: arch topology borken\n");
1626 #ifdef CONFIG_SCHED_DEBUG
1627 pr_err(" the %s domain not a subset of the %s domain\n",
1628 child->name, sd->name);
1629 #endif
1630 /* Fixup, ensure @sd has at least @child CPUs. */
1631 cpumask_or(sched_domain_span(sd),
1632 sched_domain_span(sd),
1633 sched_domain_span(child));
1634 }
1635
1636 }
1637 set_domain_attribute(sd, attr);
1638
1639 return sd;
1640 }
1641
1642 /*
1643 * Build sched domains for a given set of CPUs and attach the sched domains
1644 * to the individual CPUs
1645 */
1646 static int
1647 build_sched_domains(const struct cpumask *cpu_map, struct sched_domain_attr *attr)
1648 {
1649 enum s_alloc alloc_state;
1650 struct sched_domain *sd;
1651 struct s_data d;
1652 struct rq *rq = NULL;
1653 int i, ret = -ENOMEM;
1654
1655 alloc_state = __visit_domain_allocation_hell(&d, cpu_map);
1656 if (alloc_state != sa_rootdomain)
1657 goto error;
1658
1659 /* Set up domains for CPUs specified by the cpu_map: */
1660 for_each_cpu(i, cpu_map) {
1661 struct sched_domain_topology_level *tl;
1662
1663 sd = NULL;
1664 for_each_sd_topology(tl) {
1665 sd = build_sched_domain(tl, cpu_map, attr, sd, i);
1666 if (tl == sched_domain_topology)
1667 *per_cpu_ptr(d.sd, i) = sd;
1668 if (tl->flags & SDTL_OVERLAP)
1669 sd->flags |= SD_OVERLAP;
1670 if (cpumask_equal(cpu_map, sched_domain_span(sd)))
1671 break;
1672 }
1673 }
1674
1675 /* Build the groups for the domains */
1676 for_each_cpu(i, cpu_map) {
1677 for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
1678 sd->span_weight = cpumask_weight(sched_domain_span(sd));
1679 if (sd->flags & SD_OVERLAP) {
1680 if (build_overlap_sched_groups(sd, i))
1681 goto error;
1682 } else {
1683 if (build_sched_groups(sd, i))
1684 goto error;
1685 }
1686 }
1687 }
1688
1689 /* Calculate CPU capacity for physical packages and nodes */
1690 for (i = nr_cpumask_bits-1; i >= 0; i--) {
1691 if (!cpumask_test_cpu(i, cpu_map))
1692 continue;
1693
1694 for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
1695 claim_allocations(i, sd);
1696 init_sched_groups_capacity(i, sd);
1697 }
1698 }
1699
1700 /* Attach the domains */
1701 rcu_read_lock();
1702 for_each_cpu(i, cpu_map) {
1703 rq = cpu_rq(i);
1704 sd = *per_cpu_ptr(d.sd, i);
1705
1706 /* Use READ_ONCE()/WRITE_ONCE() to avoid load/store tearing: */
1707 if (rq->cpu_capacity_orig > READ_ONCE(d.rd->max_cpu_capacity))
1708 WRITE_ONCE(d.rd->max_cpu_capacity, rq->cpu_capacity_orig);
1709
1710 cpu_attach_domain(sd, d.rd, i);
1711 }
1712 rcu_read_unlock();
1713
1714 if (rq && sched_debug_enabled) {
1715 pr_info("span: %*pbl (max cpu_capacity = %lu)\n",
1716 cpumask_pr_args(cpu_map), rq->rd->max_cpu_capacity);
1717 }
1718
1719 ret = 0;
1720 error:
1721 __free_domain_allocs(&d, alloc_state, cpu_map);
1722
1723 return ret;
1724 }
1725
1726 /* Current sched domains: */
1727 static cpumask_var_t *doms_cur;
1728
1729 /* Number of sched domains in 'doms_cur': */
1730 static int ndoms_cur;
1731
1732 /* Attribues of custom domains in 'doms_cur' */
1733 static struct sched_domain_attr *dattr_cur;
1734
1735 /*
1736 * Special case: If a kmalloc() of a doms_cur partition (array of
1737 * cpumask) fails, then fallback to a single sched domain,
1738 * as determined by the single cpumask fallback_doms.
1739 */
1740 static cpumask_var_t fallback_doms;
1741
1742 /*
1743 * arch_update_cpu_topology lets virtualized architectures update the
1744 * CPU core maps. It is supposed to return 1 if the topology changed
1745 * or 0 if it stayed the same.
1746 */
1747 int __weak arch_update_cpu_topology(void)
1748 {
1749 return 0;
1750 }
1751
1752 cpumask_var_t *alloc_sched_domains(unsigned int ndoms)
1753 {
1754 int i;
1755 cpumask_var_t *doms;
1756
1757 doms = kmalloc(sizeof(*doms) * ndoms, GFP_KERNEL);
1758 if (!doms)
1759 return NULL;
1760 for (i = 0; i < ndoms; i++) {
1761 if (!alloc_cpumask_var(&doms[i], GFP_KERNEL)) {
1762 free_sched_domains(doms, i);
1763 return NULL;
1764 }
1765 }
1766 return doms;
1767 }
1768
1769 void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms)
1770 {
1771 unsigned int i;
1772 for (i = 0; i < ndoms; i++)
1773 free_cpumask_var(doms[i]);
1774 kfree(doms);
1775 }
1776
1777 /*
1778 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
1779 * For now this just excludes isolated CPUs, but could be used to
1780 * exclude other special cases in the future.
1781 */
1782 int sched_init_domains(const struct cpumask *cpu_map)
1783 {
1784 int err;
1785
1786 zalloc_cpumask_var(&sched_domains_tmpmask, GFP_KERNEL);
1787 zalloc_cpumask_var(&sched_domains_tmpmask2, GFP_KERNEL);
1788 zalloc_cpumask_var(&fallback_doms, GFP_KERNEL);
1789
1790 arch_update_cpu_topology();
1791 ndoms_cur = 1;
1792 doms_cur = alloc_sched_domains(ndoms_cur);
1793 if (!doms_cur)
1794 doms_cur = &fallback_doms;
1795 cpumask_and(doms_cur[0], cpu_map, housekeeping_cpumask(HK_FLAG_DOMAIN));
1796 err = build_sched_domains(doms_cur[0], NULL);
1797 register_sched_domain_sysctl();
1798
1799 return err;
1800 }
1801
1802 /*
1803 * Detach sched domains from a group of CPUs specified in cpu_map
1804 * These CPUs will now be attached to the NULL domain
1805 */
1806 static void detach_destroy_domains(const struct cpumask *cpu_map)
1807 {
1808 int i;
1809
1810 rcu_read_lock();
1811 for_each_cpu(i, cpu_map)
1812 cpu_attach_domain(NULL, &def_root_domain, i);
1813 rcu_read_unlock();
1814 }
1815
1816 /* handle null as "default" */
1817 static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
1818 struct sched_domain_attr *new, int idx_new)
1819 {
1820 struct sched_domain_attr tmp;
1821
1822 /* Fast path: */
1823 if (!new && !cur)
1824 return 1;
1825
1826 tmp = SD_ATTR_INIT;
1827
1828 return !memcmp(cur ? (cur + idx_cur) : &tmp,
1829 new ? (new + idx_new) : &tmp,
1830 sizeof(struct sched_domain_attr));
1831 }
1832
1833 /*
1834 * Partition sched domains as specified by the 'ndoms_new'
1835 * cpumasks in the array doms_new[] of cpumasks. This compares
1836 * doms_new[] to the current sched domain partitioning, doms_cur[].
1837 * It destroys each deleted domain and builds each new domain.
1838 *
1839 * 'doms_new' is an array of cpumask_var_t's of length 'ndoms_new'.
1840 * The masks don't intersect (don't overlap.) We should setup one
1841 * sched domain for each mask. CPUs not in any of the cpumasks will
1842 * not be load balanced. If the same cpumask appears both in the
1843 * current 'doms_cur' domains and in the new 'doms_new', we can leave
1844 * it as it is.
1845 *
1846 * The passed in 'doms_new' should be allocated using
1847 * alloc_sched_domains. This routine takes ownership of it and will
1848 * free_sched_domains it when done with it. If the caller failed the
1849 * alloc call, then it can pass in doms_new == NULL && ndoms_new == 1,
1850 * and partition_sched_domains() will fallback to the single partition
1851 * 'fallback_doms', it also forces the domains to be rebuilt.
1852 *
1853 * If doms_new == NULL it will be replaced with cpu_online_mask.
1854 * ndoms_new == 0 is a special case for destroying existing domains,
1855 * and it will not create the default domain.
1856 *
1857 * Call with hotplug lock held
1858 */
1859 void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
1860 struct sched_domain_attr *dattr_new)
1861 {
1862 int i, j, n;
1863 int new_topology;
1864
1865 mutex_lock(&sched_domains_mutex);
1866
1867 /* Always unregister in case we don't destroy any domains: */
1868 unregister_sched_domain_sysctl();
1869
1870 /* Let the architecture update CPU core mappings: */
1871 new_topology = arch_update_cpu_topology();
1872
1873 if (!doms_new) {
1874 WARN_ON_ONCE(dattr_new);
1875 n = 0;
1876 doms_new = alloc_sched_domains(1);
1877 if (doms_new) {
1878 n = 1;
1879 cpumask_and(doms_new[0], cpu_active_mask,
1880 housekeeping_cpumask(HK_FLAG_DOMAIN));
1881 }
1882 } else {
1883 n = ndoms_new;
1884 }
1885
1886 /* Destroy deleted domains: */
1887 for (i = 0; i < ndoms_cur; i++) {
1888 for (j = 0; j < n && !new_topology; j++) {
1889 if (cpumask_equal(doms_cur[i], doms_new[j])
1890 && dattrs_equal(dattr_cur, i, dattr_new, j))
1891 goto match1;
1892 }
1893 /* No match - a current sched domain not in new doms_new[] */
1894 detach_destroy_domains(doms_cur[i]);
1895 match1:
1896 ;
1897 }
1898
1899 n = ndoms_cur;
1900 if (!doms_new) {
1901 n = 0;
1902 doms_new = &fallback_doms;
1903 cpumask_and(doms_new[0], cpu_active_mask,
1904 housekeeping_cpumask(HK_FLAG_DOMAIN));
1905 }
1906
1907 /* Build new domains: */
1908 for (i = 0; i < ndoms_new; i++) {
1909 for (j = 0; j < n && !new_topology; j++) {
1910 if (cpumask_equal(doms_new[i], doms_cur[j])
1911 && dattrs_equal(dattr_new, i, dattr_cur, j))
1912 goto match2;
1913 }
1914 /* No match - add a new doms_new */
1915 build_sched_domains(doms_new[i], dattr_new ? dattr_new + i : NULL);
1916 match2:
1917 ;
1918 }
1919
1920 /* Remember the new sched domains: */
1921 if (doms_cur != &fallback_doms)
1922 free_sched_domains(doms_cur, ndoms_cur);
1923
1924 kfree(dattr_cur);
1925 doms_cur = doms_new;
1926 dattr_cur = dattr_new;
1927 ndoms_cur = ndoms_new;
1928
1929 register_sched_domain_sysctl();
1930
1931 mutex_unlock(&sched_domains_mutex);
1932 }