Linux 4.15.6
[linux/fpc-iii.git] / kernel / sched / topology.c
blob519b024f4e94f9385e2e7df913dba2fb314410d1
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>
9 #include "sched.h"
11 DEFINE_MUTEX(sched_domains_mutex);
13 /* Protected by sched_domains_mutex: */
14 cpumask_var_t sched_domains_tmpmask;
15 cpumask_var_t sched_domains_tmpmask2;
17 #ifdef CONFIG_SCHED_DEBUG
19 static int __init sched_debug_setup(char *str)
21 sched_debug_enabled = true;
23 return 0;
25 early_param("sched_debug", sched_debug_setup);
27 static inline bool sched_debug(void)
29 return sched_debug_enabled;
32 static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
33 struct cpumask *groupmask)
35 struct sched_group *group = sd->groups;
37 cpumask_clear(groupmask);
39 printk(KERN_DEBUG "%*s domain-%d: ", level, "", level);
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"
45 " has parent");
46 return -1;
49 printk(KERN_CONT "span=%*pbl level=%s\n",
50 cpumask_pr_args(sched_domain_span(sd)), sd->name);
52 if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) {
53 printk(KERN_ERR "ERROR: domain->span does not contain "
54 "CPU%d\n", cpu);
56 if (!cpumask_test_cpu(cpu, sched_group_span(group))) {
57 printk(KERN_ERR "ERROR: domain->groups does not contain"
58 " CPU%d\n", cpu);
61 printk(KERN_DEBUG "%*s groups:", level + 1, "");
62 do {
63 if (!group) {
64 printk("\n");
65 printk(KERN_ERR "ERROR: group is NULL\n");
66 break;
69 if (!cpumask_weight(sched_group_span(group))) {
70 printk(KERN_CONT "\n");
71 printk(KERN_ERR "ERROR: empty group\n");
72 break;
75 if (!(sd->flags & SD_OVERLAP) &&
76 cpumask_intersects(groupmask, sched_group_span(group))) {
77 printk(KERN_CONT "\n");
78 printk(KERN_ERR "ERROR: repeated CPUs\n");
79 break;
82 cpumask_or(groupmask, groupmask, sched_group_span(group));
84 printk(KERN_CONT " %d:{ span=%*pbl",
85 group->sgc->id,
86 cpumask_pr_args(sched_group_span(group)));
88 if ((sd->flags & SD_OVERLAP) &&
89 !cpumask_equal(group_balance_mask(group), sched_group_span(group))) {
90 printk(KERN_CONT " mask=%*pbl",
91 cpumask_pr_args(group_balance_mask(group)));
94 if (group->sgc->capacity != SCHED_CAPACITY_SCALE)
95 printk(KERN_CONT " cap=%lu", group->sgc->capacity);
97 if (group == sd->groups && sd->child &&
98 !cpumask_equal(sched_domain_span(sd->child),
99 sched_group_span(group))) {
100 printk(KERN_ERR "ERROR: domain->groups does not match domain->child\n");
103 printk(KERN_CONT " }");
105 group = group->next;
107 if (group != sd->groups)
108 printk(KERN_CONT ",");
110 } while (group != sd->groups);
111 printk(KERN_CONT "\n");
113 if (!cpumask_equal(sched_domain_span(sd), groupmask))
114 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
116 if (sd->parent &&
117 !cpumask_subset(groupmask, sched_domain_span(sd->parent)))
118 printk(KERN_ERR "ERROR: parent span is not a superset "
119 "of domain->span\n");
120 return 0;
123 static void sched_domain_debug(struct sched_domain *sd, int cpu)
125 int level = 0;
127 if (!sched_debug_enabled)
128 return;
130 if (!sd) {
131 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
132 return;
135 printk(KERN_DEBUG "CPU%d attaching sched-domain(s):\n", cpu);
137 for (;;) {
138 if (sched_domain_debug_one(sd, cpu, level, sched_domains_tmpmask))
139 break;
140 level++;
141 sd = sd->parent;
142 if (!sd)
143 break;
146 #else /* !CONFIG_SCHED_DEBUG */
148 # define sched_debug_enabled 0
149 # define sched_domain_debug(sd, cpu) do { } while (0)
150 static inline bool sched_debug(void)
152 return false;
154 #endif /* CONFIG_SCHED_DEBUG */
156 static int sd_degenerate(struct sched_domain *sd)
158 if (cpumask_weight(sched_domain_span(sd)) == 1)
159 return 1;
161 /* Following flags need at least 2 groups */
162 if (sd->flags & (SD_LOAD_BALANCE |
163 SD_BALANCE_NEWIDLE |
164 SD_BALANCE_FORK |
165 SD_BALANCE_EXEC |
166 SD_SHARE_CPUCAPACITY |
167 SD_ASYM_CPUCAPACITY |
168 SD_SHARE_PKG_RESOURCES |
169 SD_SHARE_POWERDOMAIN)) {
170 if (sd->groups != sd->groups->next)
171 return 0;
174 /* Following flags don't use groups */
175 if (sd->flags & (SD_WAKE_AFFINE))
176 return 0;
178 return 1;
181 static int
182 sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
184 unsigned long cflags = sd->flags, pflags = parent->flags;
186 if (sd_degenerate(parent))
187 return 1;
189 if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent)))
190 return 0;
192 /* Flags needing groups don't count if only 1 group in parent */
193 if (parent->groups == parent->groups->next) {
194 pflags &= ~(SD_LOAD_BALANCE |
195 SD_BALANCE_NEWIDLE |
196 SD_BALANCE_FORK |
197 SD_BALANCE_EXEC |
198 SD_ASYM_CPUCAPACITY |
199 SD_SHARE_CPUCAPACITY |
200 SD_SHARE_PKG_RESOURCES |
201 SD_PREFER_SIBLING |
202 SD_SHARE_POWERDOMAIN);
203 if (nr_node_ids == 1)
204 pflags &= ~SD_SERIALIZE;
206 if (~cflags & pflags)
207 return 0;
209 return 1;
212 static void free_rootdomain(struct rcu_head *rcu)
214 struct root_domain *rd = container_of(rcu, struct root_domain, rcu);
216 cpupri_cleanup(&rd->cpupri);
217 cpudl_cleanup(&rd->cpudl);
218 free_cpumask_var(rd->dlo_mask);
219 free_cpumask_var(rd->rto_mask);
220 free_cpumask_var(rd->online);
221 free_cpumask_var(rd->span);
222 kfree(rd);
225 void rq_attach_root(struct rq *rq, struct root_domain *rd)
227 struct root_domain *old_rd = NULL;
228 unsigned long flags;
230 raw_spin_lock_irqsave(&rq->lock, flags);
232 if (rq->rd) {
233 old_rd = rq->rd;
235 if (cpumask_test_cpu(rq->cpu, old_rd->online))
236 set_rq_offline(rq);
238 cpumask_clear_cpu(rq->cpu, old_rd->span);
241 * If we dont want to free the old_rd yet then
242 * set old_rd to NULL to skip the freeing later
243 * in this function:
245 if (!atomic_dec_and_test(&old_rd->refcount))
246 old_rd = NULL;
249 atomic_inc(&rd->refcount);
250 rq->rd = rd;
252 cpumask_set_cpu(rq->cpu, rd->span);
253 if (cpumask_test_cpu(rq->cpu, cpu_active_mask))
254 set_rq_online(rq);
256 raw_spin_unlock_irqrestore(&rq->lock, flags);
258 if (old_rd)
259 call_rcu_sched(&old_rd->rcu, free_rootdomain);
262 void sched_get_rd(struct root_domain *rd)
264 atomic_inc(&rd->refcount);
267 void sched_put_rd(struct root_domain *rd)
269 if (!atomic_dec_and_test(&rd->refcount))
270 return;
272 call_rcu_sched(&rd->rcu, free_rootdomain);
275 static int init_rootdomain(struct root_domain *rd)
277 if (!zalloc_cpumask_var(&rd->span, GFP_KERNEL))
278 goto out;
279 if (!zalloc_cpumask_var(&rd->online, GFP_KERNEL))
280 goto free_span;
281 if (!zalloc_cpumask_var(&rd->dlo_mask, GFP_KERNEL))
282 goto free_online;
283 if (!zalloc_cpumask_var(&rd->rto_mask, GFP_KERNEL))
284 goto free_dlo_mask;
286 #ifdef HAVE_RT_PUSH_IPI
287 rd->rto_cpu = -1;
288 raw_spin_lock_init(&rd->rto_lock);
289 init_irq_work(&rd->rto_push_work, rto_push_irq_work_func);
290 #endif
292 init_dl_bw(&rd->dl_bw);
293 if (cpudl_init(&rd->cpudl) != 0)
294 goto free_rto_mask;
296 if (cpupri_init(&rd->cpupri) != 0)
297 goto free_cpudl;
298 return 0;
300 free_cpudl:
301 cpudl_cleanup(&rd->cpudl);
302 free_rto_mask:
303 free_cpumask_var(rd->rto_mask);
304 free_dlo_mask:
305 free_cpumask_var(rd->dlo_mask);
306 free_online:
307 free_cpumask_var(rd->online);
308 free_span:
309 free_cpumask_var(rd->span);
310 out:
311 return -ENOMEM;
315 * By default the system creates a single root-domain with all CPUs as
316 * members (mimicking the global state we have today).
318 struct root_domain def_root_domain;
320 void init_defrootdomain(void)
322 init_rootdomain(&def_root_domain);
324 atomic_set(&def_root_domain.refcount, 1);
327 static struct root_domain *alloc_rootdomain(void)
329 struct root_domain *rd;
331 rd = kzalloc(sizeof(*rd), GFP_KERNEL);
332 if (!rd)
333 return NULL;
335 if (init_rootdomain(rd) != 0) {
336 kfree(rd);
337 return NULL;
340 return rd;
343 static void free_sched_groups(struct sched_group *sg, int free_sgc)
345 struct sched_group *tmp, *first;
347 if (!sg)
348 return;
350 first = sg;
351 do {
352 tmp = sg->next;
354 if (free_sgc && atomic_dec_and_test(&sg->sgc->ref))
355 kfree(sg->sgc);
357 if (atomic_dec_and_test(&sg->ref))
358 kfree(sg);
359 sg = tmp;
360 } while (sg != first);
363 static void destroy_sched_domain(struct sched_domain *sd)
366 * A normal sched domain may have multiple group references, an
367 * overlapping domain, having private groups, only one. Iterate,
368 * dropping group/capacity references, freeing where none remain.
370 free_sched_groups(sd->groups, 1);
372 if (sd->shared && atomic_dec_and_test(&sd->shared->ref))
373 kfree(sd->shared);
374 kfree(sd);
377 static void destroy_sched_domains_rcu(struct rcu_head *rcu)
379 struct sched_domain *sd = container_of(rcu, struct sched_domain, rcu);
381 while (sd) {
382 struct sched_domain *parent = sd->parent;
383 destroy_sched_domain(sd);
384 sd = parent;
388 static void destroy_sched_domains(struct sched_domain *sd)
390 if (sd)
391 call_rcu(&sd->rcu, destroy_sched_domains_rcu);
395 * Keep a special pointer to the highest sched_domain that has
396 * SD_SHARE_PKG_RESOURCE set (Last Level Cache Domain) for this
397 * allows us to avoid some pointer chasing select_idle_sibling().
399 * Also keep a unique ID per domain (we use the first CPU number in
400 * the cpumask of the domain), this allows us to quickly tell if
401 * two CPUs are in the same cache domain, see cpus_share_cache().
403 DEFINE_PER_CPU(struct sched_domain *, sd_llc);
404 DEFINE_PER_CPU(int, sd_llc_size);
405 DEFINE_PER_CPU(int, sd_llc_id);
406 DEFINE_PER_CPU(struct sched_domain_shared *, sd_llc_shared);
407 DEFINE_PER_CPU(struct sched_domain *, sd_numa);
408 DEFINE_PER_CPU(struct sched_domain *, sd_asym);
410 static void update_top_cache_domain(int cpu)
412 struct sched_domain_shared *sds = NULL;
413 struct sched_domain *sd;
414 int id = cpu;
415 int size = 1;
417 sd = highest_flag_domain(cpu, SD_SHARE_PKG_RESOURCES);
418 if (sd) {
419 id = cpumask_first(sched_domain_span(sd));
420 size = cpumask_weight(sched_domain_span(sd));
421 sds = sd->shared;
424 rcu_assign_pointer(per_cpu(sd_llc, cpu), sd);
425 per_cpu(sd_llc_size, cpu) = size;
426 per_cpu(sd_llc_id, cpu) = id;
427 rcu_assign_pointer(per_cpu(sd_llc_shared, cpu), sds);
429 sd = lowest_flag_domain(cpu, SD_NUMA);
430 rcu_assign_pointer(per_cpu(sd_numa, cpu), sd);
432 sd = highest_flag_domain(cpu, SD_ASYM_PACKING);
433 rcu_assign_pointer(per_cpu(sd_asym, cpu), sd);
437 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
438 * hold the hotplug lock.
440 static void
441 cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
443 struct rq *rq = cpu_rq(cpu);
444 struct sched_domain *tmp;
446 /* Remove the sched domains which do not contribute to scheduling. */
447 for (tmp = sd; tmp; ) {
448 struct sched_domain *parent = tmp->parent;
449 if (!parent)
450 break;
452 if (sd_parent_degenerate(tmp, parent)) {
453 tmp->parent = parent->parent;
454 if (parent->parent)
455 parent->parent->child = tmp;
457 * Transfer SD_PREFER_SIBLING down in case of a
458 * degenerate parent; the spans match for this
459 * so the property transfers.
461 if (parent->flags & SD_PREFER_SIBLING)
462 tmp->flags |= SD_PREFER_SIBLING;
463 destroy_sched_domain(parent);
464 } else
465 tmp = tmp->parent;
468 if (sd && sd_degenerate(sd)) {
469 tmp = sd;
470 sd = sd->parent;
471 destroy_sched_domain(tmp);
472 if (sd)
473 sd->child = NULL;
476 sched_domain_debug(sd, cpu);
478 rq_attach_root(rq, rd);
479 tmp = rq->sd;
480 rcu_assign_pointer(rq->sd, sd);
481 dirty_sched_domain_sysctl(cpu);
482 destroy_sched_domains(tmp);
484 update_top_cache_domain(cpu);
487 struct s_data {
488 struct sched_domain ** __percpu sd;
489 struct root_domain *rd;
492 enum s_alloc {
493 sa_rootdomain,
494 sa_sd,
495 sa_sd_storage,
496 sa_none,
500 * Return the canonical balance CPU for this group, this is the first CPU
501 * of this group that's also in the balance mask.
503 * The balance mask are all those CPUs that could actually end up at this
504 * group. See build_balance_mask().
506 * Also see should_we_balance().
508 int group_balance_cpu(struct sched_group *sg)
510 return cpumask_first(group_balance_mask(sg));
515 * NUMA topology (first read the regular topology blurb below)
517 * Given a node-distance table, for example:
519 * node 0 1 2 3
520 * 0: 10 20 30 20
521 * 1: 20 10 20 30
522 * 2: 30 20 10 20
523 * 3: 20 30 20 10
525 * which represents a 4 node ring topology like:
527 * 0 ----- 1
528 * | |
529 * | |
530 * | |
531 * 3 ----- 2
533 * We want to construct domains and groups to represent this. The way we go
534 * about doing this is to build the domains on 'hops'. For each NUMA level we
535 * construct the mask of all nodes reachable in @level hops.
537 * For the above NUMA topology that gives 3 levels:
539 * NUMA-2 0-3 0-3 0-3 0-3
540 * groups: {0-1,3},{1-3} {0-2},{0,2-3} {1-3},{0-1,3} {0,2-3},{0-2}
542 * NUMA-1 0-1,3 0-2 1-3 0,2-3
543 * groups: {0},{1},{3} {0},{1},{2} {1},{2},{3} {0},{2},{3}
545 * NUMA-0 0 1 2 3
548 * As can be seen; things don't nicely line up as with the regular topology.
549 * When we iterate a domain in child domain chunks some nodes can be
550 * represented multiple times -- hence the "overlap" naming for this part of
551 * the topology.
553 * In order to minimize this overlap, we only build enough groups to cover the
554 * domain. For instance Node-0 NUMA-2 would only get groups: 0-1,3 and 1-3.
556 * Because:
558 * - the first group of each domain is its child domain; this
559 * gets us the first 0-1,3
560 * - the only uncovered node is 2, who's child domain is 1-3.
562 * However, because of the overlap, computing a unique CPU for each group is
563 * more complicated. Consider for instance the groups of NODE-1 NUMA-2, both
564 * groups include the CPUs of Node-0, while those CPUs would not in fact ever
565 * end up at those groups (they would end up in group: 0-1,3).
567 * To correct this we have to introduce the group balance mask. This mask
568 * will contain those CPUs in the group that can reach this group given the
569 * (child) domain tree.
571 * With this we can once again compute balance_cpu and sched_group_capacity
572 * relations.
574 * XXX include words on how balance_cpu is unique and therefore can be
575 * used for sched_group_capacity links.
578 * Another 'interesting' topology is:
580 * node 0 1 2 3
581 * 0: 10 20 20 30
582 * 1: 20 10 20 20
583 * 2: 20 20 10 20
584 * 3: 30 20 20 10
586 * Which looks a little like:
588 * 0 ----- 1
589 * | / |
590 * | / |
591 * | / |
592 * 2 ----- 3
594 * This topology is asymmetric, nodes 1,2 are fully connected, but nodes 0,3
595 * are not.
597 * This leads to a few particularly weird cases where the sched_domain's are
598 * not of the same number for each cpu. Consider:
600 * NUMA-2 0-3 0-3
601 * groups: {0-2},{1-3} {1-3},{0-2}
603 * NUMA-1 0-2 0-3 0-3 1-3
605 * NUMA-0 0 1 2 3
611 * Build the balance mask; it contains only those CPUs that can arrive at this
612 * group and should be considered to continue balancing.
614 * We do this during the group creation pass, therefore the group information
615 * isn't complete yet, however since each group represents a (child) domain we
616 * can fully construct this using the sched_domain bits (which are already
617 * complete).
619 static void
620 build_balance_mask(struct sched_domain *sd, struct sched_group *sg, struct cpumask *mask)
622 const struct cpumask *sg_span = sched_group_span(sg);
623 struct sd_data *sdd = sd->private;
624 struct sched_domain *sibling;
625 int i;
627 cpumask_clear(mask);
629 for_each_cpu(i, sg_span) {
630 sibling = *per_cpu_ptr(sdd->sd, i);
633 * Can happen in the asymmetric case, where these siblings are
634 * unused. The mask will not be empty because those CPUs that
635 * do have the top domain _should_ span the domain.
637 if (!sibling->child)
638 continue;
640 /* If we would not end up here, we can't continue from here */
641 if (!cpumask_equal(sg_span, sched_domain_span(sibling->child)))
642 continue;
644 cpumask_set_cpu(i, mask);
647 /* We must not have empty masks here */
648 WARN_ON_ONCE(cpumask_empty(mask));
652 * XXX: This creates per-node group entries; since the load-balancer will
653 * immediately access remote memory to construct this group's load-balance
654 * statistics having the groups node local is of dubious benefit.
656 static struct sched_group *
657 build_group_from_child_sched_domain(struct sched_domain *sd, int cpu)
659 struct sched_group *sg;
660 struct cpumask *sg_span;
662 sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
663 GFP_KERNEL, cpu_to_node(cpu));
665 if (!sg)
666 return NULL;
668 sg_span = sched_group_span(sg);
669 if (sd->child)
670 cpumask_copy(sg_span, sched_domain_span(sd->child));
671 else
672 cpumask_copy(sg_span, sched_domain_span(sd));
674 atomic_inc(&sg->ref);
675 return sg;
678 static void init_overlap_sched_group(struct sched_domain *sd,
679 struct sched_group *sg)
681 struct cpumask *mask = sched_domains_tmpmask2;
682 struct sd_data *sdd = sd->private;
683 struct cpumask *sg_span;
684 int cpu;
686 build_balance_mask(sd, sg, mask);
687 cpu = cpumask_first_and(sched_group_span(sg), mask);
689 sg->sgc = *per_cpu_ptr(sdd->sgc, cpu);
690 if (atomic_inc_return(&sg->sgc->ref) == 1)
691 cpumask_copy(group_balance_mask(sg), mask);
692 else
693 WARN_ON_ONCE(!cpumask_equal(group_balance_mask(sg), mask));
696 * Initialize sgc->capacity such that even if we mess up the
697 * domains and no possible iteration will get us here, we won't
698 * die on a /0 trap.
700 sg_span = sched_group_span(sg);
701 sg->sgc->capacity = SCHED_CAPACITY_SCALE * cpumask_weight(sg_span);
702 sg->sgc->min_capacity = SCHED_CAPACITY_SCALE;
705 static int
706 build_overlap_sched_groups(struct sched_domain *sd, int cpu)
708 struct sched_group *first = NULL, *last = NULL, *sg;
709 const struct cpumask *span = sched_domain_span(sd);
710 struct cpumask *covered = sched_domains_tmpmask;
711 struct sd_data *sdd = sd->private;
712 struct sched_domain *sibling;
713 int i;
715 cpumask_clear(covered);
717 for_each_cpu_wrap(i, span, cpu) {
718 struct cpumask *sg_span;
720 if (cpumask_test_cpu(i, covered))
721 continue;
723 sibling = *per_cpu_ptr(sdd->sd, i);
726 * Asymmetric node setups can result in situations where the
727 * domain tree is of unequal depth, make sure to skip domains
728 * that already cover the entire range.
730 * In that case build_sched_domains() will have terminated the
731 * iteration early and our sibling sd spans will be empty.
732 * Domains should always include the CPU they're built on, so
733 * check that.
735 if (!cpumask_test_cpu(i, sched_domain_span(sibling)))
736 continue;
738 sg = build_group_from_child_sched_domain(sibling, cpu);
739 if (!sg)
740 goto fail;
742 sg_span = sched_group_span(sg);
743 cpumask_or(covered, covered, sg_span);
745 init_overlap_sched_group(sd, sg);
747 if (!first)
748 first = sg;
749 if (last)
750 last->next = sg;
751 last = sg;
752 last->next = first;
754 sd->groups = first;
756 return 0;
758 fail:
759 free_sched_groups(first, 0);
761 return -ENOMEM;
766 * Package topology (also see the load-balance blurb in fair.c)
768 * The scheduler builds a tree structure to represent a number of important
769 * topology features. By default (default_topology[]) these include:
771 * - Simultaneous multithreading (SMT)
772 * - Multi-Core Cache (MC)
773 * - Package (DIE)
775 * Where the last one more or less denotes everything up to a NUMA node.
777 * The tree consists of 3 primary data structures:
779 * sched_domain -> sched_group -> sched_group_capacity
780 * ^ ^ ^ ^
781 * `-' `-'
783 * The sched_domains are per-cpu and have a two way link (parent & child) and
784 * denote the ever growing mask of CPUs belonging to that level of topology.
786 * Each sched_domain has a circular (double) linked list of sched_group's, each
787 * denoting the domains of the level below (or individual CPUs in case of the
788 * first domain level). The sched_group linked by a sched_domain includes the
789 * CPU of that sched_domain [*].
791 * Take for instance a 2 threaded, 2 core, 2 cache cluster part:
793 * CPU 0 1 2 3 4 5 6 7
795 * DIE [ ]
796 * MC [ ] [ ]
797 * SMT [ ] [ ] [ ] [ ]
799 * - or -
801 * DIE 0-7 0-7 0-7 0-7 0-7 0-7 0-7 0-7
802 * MC 0-3 0-3 0-3 0-3 4-7 4-7 4-7 4-7
803 * SMT 0-1 0-1 2-3 2-3 4-5 4-5 6-7 6-7
805 * CPU 0 1 2 3 4 5 6 7
807 * One way to think about it is: sched_domain moves you up and down among these
808 * topology levels, while sched_group moves you sideways through it, at child
809 * domain granularity.
811 * sched_group_capacity ensures each unique sched_group has shared storage.
813 * There are two related construction problems, both require a CPU that
814 * uniquely identify each group (for a given domain):
816 * - The first is the balance_cpu (see should_we_balance() and the
817 * load-balance blub in fair.c); for each group we only want 1 CPU to
818 * continue balancing at a higher domain.
820 * - The second is the sched_group_capacity; we want all identical groups
821 * to share a single sched_group_capacity.
823 * Since these topologies are exclusive by construction. That is, its
824 * impossible for an SMT thread to belong to multiple cores, and cores to
825 * be part of multiple caches. There is a very clear and unique location
826 * for each CPU in the hierarchy.
828 * Therefore computing a unique CPU for each group is trivial (the iteration
829 * mask is redundant and set all 1s; all CPUs in a group will end up at _that_
830 * group), we can simply pick the first CPU in each group.
833 * [*] in other words, the first group of each domain is its child domain.
836 static struct sched_group *get_group(int cpu, struct sd_data *sdd)
838 struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu);
839 struct sched_domain *child = sd->child;
840 struct sched_group *sg;
842 if (child)
843 cpu = cpumask_first(sched_domain_span(child));
845 sg = *per_cpu_ptr(sdd->sg, cpu);
846 sg->sgc = *per_cpu_ptr(sdd->sgc, cpu);
848 /* For claim_allocations: */
849 atomic_inc(&sg->ref);
850 atomic_inc(&sg->sgc->ref);
852 if (child) {
853 cpumask_copy(sched_group_span(sg), sched_domain_span(child));
854 cpumask_copy(group_balance_mask(sg), sched_group_span(sg));
855 } else {
856 cpumask_set_cpu(cpu, sched_group_span(sg));
857 cpumask_set_cpu(cpu, group_balance_mask(sg));
860 sg->sgc->capacity = SCHED_CAPACITY_SCALE * cpumask_weight(sched_group_span(sg));
861 sg->sgc->min_capacity = SCHED_CAPACITY_SCALE;
863 return sg;
867 * build_sched_groups will build a circular linked list of the groups
868 * covered by the given span, and will set each group's ->cpumask correctly,
869 * and ->cpu_capacity to 0.
871 * Assumes the sched_domain tree is fully constructed
873 static int
874 build_sched_groups(struct sched_domain *sd, int cpu)
876 struct sched_group *first = NULL, *last = NULL;
877 struct sd_data *sdd = sd->private;
878 const struct cpumask *span = sched_domain_span(sd);
879 struct cpumask *covered;
880 int i;
882 lockdep_assert_held(&sched_domains_mutex);
883 covered = sched_domains_tmpmask;
885 cpumask_clear(covered);
887 for_each_cpu_wrap(i, span, cpu) {
888 struct sched_group *sg;
890 if (cpumask_test_cpu(i, covered))
891 continue;
893 sg = get_group(i, sdd);
895 cpumask_or(covered, covered, sched_group_span(sg));
897 if (!first)
898 first = sg;
899 if (last)
900 last->next = sg;
901 last = sg;
903 last->next = first;
904 sd->groups = first;
906 return 0;
910 * Initialize sched groups cpu_capacity.
912 * cpu_capacity indicates the capacity of sched group, which is used while
913 * distributing the load between different sched groups in a sched domain.
914 * Typically cpu_capacity for all the groups in a sched domain will be same
915 * unless there are asymmetries in the topology. If there are asymmetries,
916 * group having more cpu_capacity will pickup more load compared to the
917 * group having less cpu_capacity.
919 static void init_sched_groups_capacity(int cpu, struct sched_domain *sd)
921 struct sched_group *sg = sd->groups;
923 WARN_ON(!sg);
925 do {
926 int cpu, max_cpu = -1;
928 sg->group_weight = cpumask_weight(sched_group_span(sg));
930 if (!(sd->flags & SD_ASYM_PACKING))
931 goto next;
933 for_each_cpu(cpu, sched_group_span(sg)) {
934 if (max_cpu < 0)
935 max_cpu = cpu;
936 else if (sched_asym_prefer(cpu, max_cpu))
937 max_cpu = cpu;
939 sg->asym_prefer_cpu = max_cpu;
941 next:
942 sg = sg->next;
943 } while (sg != sd->groups);
945 if (cpu != group_balance_cpu(sg))
946 return;
948 update_group_capacity(sd, cpu);
952 * Initializers for schedule domains
953 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
956 static int default_relax_domain_level = -1;
957 int sched_domain_level_max;
959 static int __init setup_relax_domain_level(char *str)
961 if (kstrtoint(str, 0, &default_relax_domain_level))
962 pr_warn("Unable to set relax_domain_level\n");
964 return 1;
966 __setup("relax_domain_level=", setup_relax_domain_level);
968 static void set_domain_attribute(struct sched_domain *sd,
969 struct sched_domain_attr *attr)
971 int request;
973 if (!attr || attr->relax_domain_level < 0) {
974 if (default_relax_domain_level < 0)
975 return;
976 else
977 request = default_relax_domain_level;
978 } else
979 request = attr->relax_domain_level;
980 if (request < sd->level) {
981 /* Turn off idle balance on this domain: */
982 sd->flags &= ~(SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
983 } else {
984 /* Turn on idle balance on this domain: */
985 sd->flags |= (SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
989 static void __sdt_free(const struct cpumask *cpu_map);
990 static int __sdt_alloc(const struct cpumask *cpu_map);
992 static void __free_domain_allocs(struct s_data *d, enum s_alloc what,
993 const struct cpumask *cpu_map)
995 switch (what) {
996 case sa_rootdomain:
997 if (!atomic_read(&d->rd->refcount))
998 free_rootdomain(&d->rd->rcu);
999 /* Fall through */
1000 case sa_sd:
1001 free_percpu(d->sd);
1002 /* Fall through */
1003 case sa_sd_storage:
1004 __sdt_free(cpu_map);
1005 /* Fall through */
1006 case sa_none:
1007 break;
1011 static enum s_alloc
1012 __visit_domain_allocation_hell(struct s_data *d, const struct cpumask *cpu_map)
1014 memset(d, 0, sizeof(*d));
1016 if (__sdt_alloc(cpu_map))
1017 return sa_sd_storage;
1018 d->sd = alloc_percpu(struct sched_domain *);
1019 if (!d->sd)
1020 return sa_sd_storage;
1021 d->rd = alloc_rootdomain();
1022 if (!d->rd)
1023 return sa_sd;
1024 return sa_rootdomain;
1028 * NULL the sd_data elements we've used to build the sched_domain and
1029 * sched_group structure so that the subsequent __free_domain_allocs()
1030 * will not free the data we're using.
1032 static void claim_allocations(int cpu, struct sched_domain *sd)
1034 struct sd_data *sdd = sd->private;
1036 WARN_ON_ONCE(*per_cpu_ptr(sdd->sd, cpu) != sd);
1037 *per_cpu_ptr(sdd->sd, cpu) = NULL;
1039 if (atomic_read(&(*per_cpu_ptr(sdd->sds, cpu))->ref))
1040 *per_cpu_ptr(sdd->sds, cpu) = NULL;
1042 if (atomic_read(&(*per_cpu_ptr(sdd->sg, cpu))->ref))
1043 *per_cpu_ptr(sdd->sg, cpu) = NULL;
1045 if (atomic_read(&(*per_cpu_ptr(sdd->sgc, cpu))->ref))
1046 *per_cpu_ptr(sdd->sgc, cpu) = NULL;
1049 #ifdef CONFIG_NUMA
1050 static int sched_domains_numa_levels;
1051 enum numa_topology_type sched_numa_topology_type;
1052 static int *sched_domains_numa_distance;
1053 int sched_max_numa_distance;
1054 static struct cpumask ***sched_domains_numa_masks;
1055 static int sched_domains_curr_level;
1056 #endif
1059 * SD_flags allowed in topology descriptions.
1061 * These flags are purely descriptive of the topology and do not prescribe
1062 * behaviour. Behaviour is artificial and mapped in the below sd_init()
1063 * function:
1065 * SD_SHARE_CPUCAPACITY - describes SMT topologies
1066 * SD_SHARE_PKG_RESOURCES - describes shared caches
1067 * SD_NUMA - describes NUMA topologies
1068 * SD_SHARE_POWERDOMAIN - describes shared power domain
1069 * SD_ASYM_CPUCAPACITY - describes mixed capacity topologies
1071 * Odd one out, which beside describing the topology has a quirk also
1072 * prescribes the desired behaviour that goes along with it:
1074 * SD_ASYM_PACKING - describes SMT quirks
1076 #define TOPOLOGY_SD_FLAGS \
1077 (SD_SHARE_CPUCAPACITY | \
1078 SD_SHARE_PKG_RESOURCES | \
1079 SD_NUMA | \
1080 SD_ASYM_PACKING | \
1081 SD_ASYM_CPUCAPACITY | \
1082 SD_SHARE_POWERDOMAIN)
1084 static struct sched_domain *
1085 sd_init(struct sched_domain_topology_level *tl,
1086 const struct cpumask *cpu_map,
1087 struct sched_domain *child, int cpu)
1089 struct sd_data *sdd = &tl->data;
1090 struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu);
1091 int sd_id, sd_weight, sd_flags = 0;
1093 #ifdef CONFIG_NUMA
1095 * Ugly hack to pass state to sd_numa_mask()...
1097 sched_domains_curr_level = tl->numa_level;
1098 #endif
1100 sd_weight = cpumask_weight(tl->mask(cpu));
1102 if (tl->sd_flags)
1103 sd_flags = (*tl->sd_flags)();
1104 if (WARN_ONCE(sd_flags & ~TOPOLOGY_SD_FLAGS,
1105 "wrong sd_flags in topology description\n"))
1106 sd_flags &= ~TOPOLOGY_SD_FLAGS;
1108 *sd = (struct sched_domain){
1109 .min_interval = sd_weight,
1110 .max_interval = 2*sd_weight,
1111 .busy_factor = 32,
1112 .imbalance_pct = 125,
1114 .cache_nice_tries = 0,
1115 .busy_idx = 0,
1116 .idle_idx = 0,
1117 .newidle_idx = 0,
1118 .wake_idx = 0,
1119 .forkexec_idx = 0,
1121 .flags = 1*SD_LOAD_BALANCE
1122 | 1*SD_BALANCE_NEWIDLE
1123 | 1*SD_BALANCE_EXEC
1124 | 1*SD_BALANCE_FORK
1125 | 0*SD_BALANCE_WAKE
1126 | 1*SD_WAKE_AFFINE
1127 | 0*SD_SHARE_CPUCAPACITY
1128 | 0*SD_SHARE_PKG_RESOURCES
1129 | 0*SD_SERIALIZE
1130 | 0*SD_PREFER_SIBLING
1131 | 0*SD_NUMA
1132 | sd_flags
1135 .last_balance = jiffies,
1136 .balance_interval = sd_weight,
1137 .smt_gain = 0,
1138 .max_newidle_lb_cost = 0,
1139 .next_decay_max_lb_cost = jiffies,
1140 .child = child,
1141 #ifdef CONFIG_SCHED_DEBUG
1142 .name = tl->name,
1143 #endif
1146 cpumask_and(sched_domain_span(sd), cpu_map, tl->mask(cpu));
1147 sd_id = cpumask_first(sched_domain_span(sd));
1150 * Convert topological properties into behaviour.
1153 if (sd->flags & SD_ASYM_CPUCAPACITY) {
1154 struct sched_domain *t = sd;
1156 for_each_lower_domain(t)
1157 t->flags |= SD_BALANCE_WAKE;
1160 if (sd->flags & SD_SHARE_CPUCAPACITY) {
1161 sd->flags |= SD_PREFER_SIBLING;
1162 sd->imbalance_pct = 110;
1163 sd->smt_gain = 1178; /* ~15% */
1165 } else if (sd->flags & SD_SHARE_PKG_RESOURCES) {
1166 sd->flags |= SD_PREFER_SIBLING;
1167 sd->imbalance_pct = 117;
1168 sd->cache_nice_tries = 1;
1169 sd->busy_idx = 2;
1171 #ifdef CONFIG_NUMA
1172 } else if (sd->flags & SD_NUMA) {
1173 sd->cache_nice_tries = 2;
1174 sd->busy_idx = 3;
1175 sd->idle_idx = 2;
1177 sd->flags |= SD_SERIALIZE;
1178 if (sched_domains_numa_distance[tl->numa_level] > RECLAIM_DISTANCE) {
1179 sd->flags &= ~(SD_BALANCE_EXEC |
1180 SD_BALANCE_FORK |
1181 SD_WAKE_AFFINE);
1184 #endif
1185 } else {
1186 sd->flags |= SD_PREFER_SIBLING;
1187 sd->cache_nice_tries = 1;
1188 sd->busy_idx = 2;
1189 sd->idle_idx = 1;
1193 * For all levels sharing cache; connect a sched_domain_shared
1194 * instance.
1196 if (sd->flags & SD_SHARE_PKG_RESOURCES) {
1197 sd->shared = *per_cpu_ptr(sdd->sds, sd_id);
1198 atomic_inc(&sd->shared->ref);
1199 atomic_set(&sd->shared->nr_busy_cpus, sd_weight);
1202 sd->private = sdd;
1204 return sd;
1208 * Topology list, bottom-up.
1210 static struct sched_domain_topology_level default_topology[] = {
1211 #ifdef CONFIG_SCHED_SMT
1212 { cpu_smt_mask, cpu_smt_flags, SD_INIT_NAME(SMT) },
1213 #endif
1214 #ifdef CONFIG_SCHED_MC
1215 { cpu_coregroup_mask, cpu_core_flags, SD_INIT_NAME(MC) },
1216 #endif
1217 { cpu_cpu_mask, SD_INIT_NAME(DIE) },
1218 { NULL, },
1221 static struct sched_domain_topology_level *sched_domain_topology =
1222 default_topology;
1224 #define for_each_sd_topology(tl) \
1225 for (tl = sched_domain_topology; tl->mask; tl++)
1227 void set_sched_topology(struct sched_domain_topology_level *tl)
1229 if (WARN_ON_ONCE(sched_smp_initialized))
1230 return;
1232 sched_domain_topology = tl;
1235 #ifdef CONFIG_NUMA
1237 static const struct cpumask *sd_numa_mask(int cpu)
1239 return sched_domains_numa_masks[sched_domains_curr_level][cpu_to_node(cpu)];
1242 static void sched_numa_warn(const char *str)
1244 static int done = false;
1245 int i,j;
1247 if (done)
1248 return;
1250 done = true;
1252 printk(KERN_WARNING "ERROR: %s\n\n", str);
1254 for (i = 0; i < nr_node_ids; i++) {
1255 printk(KERN_WARNING " ");
1256 for (j = 0; j < nr_node_ids; j++)
1257 printk(KERN_CONT "%02d ", node_distance(i,j));
1258 printk(KERN_CONT "\n");
1260 printk(KERN_WARNING "\n");
1263 bool find_numa_distance(int distance)
1265 int i;
1267 if (distance == node_distance(0, 0))
1268 return true;
1270 for (i = 0; i < sched_domains_numa_levels; i++) {
1271 if (sched_domains_numa_distance[i] == distance)
1272 return true;
1275 return false;
1279 * A system can have three types of NUMA topology:
1280 * NUMA_DIRECT: all nodes are directly connected, or not a NUMA system
1281 * NUMA_GLUELESS_MESH: some nodes reachable through intermediary nodes
1282 * NUMA_BACKPLANE: nodes can reach other nodes through a backplane
1284 * The difference between a glueless mesh topology and a backplane
1285 * topology lies in whether communication between not directly
1286 * connected nodes goes through intermediary nodes (where programs
1287 * could run), or through backplane controllers. This affects
1288 * placement of programs.
1290 * The type of topology can be discerned with the following tests:
1291 * - If the maximum distance between any nodes is 1 hop, the system
1292 * is directly connected.
1293 * - If for two nodes A and B, located N > 1 hops away from each other,
1294 * there is an intermediary node C, which is < N hops away from both
1295 * nodes A and B, the system is a glueless mesh.
1297 static void init_numa_topology_type(void)
1299 int a, b, c, n;
1301 n = sched_max_numa_distance;
1303 if (sched_domains_numa_levels <= 1) {
1304 sched_numa_topology_type = NUMA_DIRECT;
1305 return;
1308 for_each_online_node(a) {
1309 for_each_online_node(b) {
1310 /* Find two nodes furthest removed from each other. */
1311 if (node_distance(a, b) < n)
1312 continue;
1314 /* Is there an intermediary node between a and b? */
1315 for_each_online_node(c) {
1316 if (node_distance(a, c) < n &&
1317 node_distance(b, c) < n) {
1318 sched_numa_topology_type =
1319 NUMA_GLUELESS_MESH;
1320 return;
1324 sched_numa_topology_type = NUMA_BACKPLANE;
1325 return;
1330 void sched_init_numa(void)
1332 int next_distance, curr_distance = node_distance(0, 0);
1333 struct sched_domain_topology_level *tl;
1334 int level = 0;
1335 int i, j, k;
1337 sched_domains_numa_distance = kzalloc(sizeof(int) * nr_node_ids, GFP_KERNEL);
1338 if (!sched_domains_numa_distance)
1339 return;
1341 /* Includes NUMA identity node at level 0. */
1342 sched_domains_numa_distance[level++] = curr_distance;
1343 sched_domains_numa_levels = level;
1346 * O(nr_nodes^2) deduplicating selection sort -- in order to find the
1347 * unique distances in the node_distance() table.
1349 * Assumes node_distance(0,j) includes all distances in
1350 * node_distance(i,j) in order to avoid cubic time.
1352 next_distance = curr_distance;
1353 for (i = 0; i < nr_node_ids; i++) {
1354 for (j = 0; j < nr_node_ids; j++) {
1355 for (k = 0; k < nr_node_ids; k++) {
1356 int distance = node_distance(i, k);
1358 if (distance > curr_distance &&
1359 (distance < next_distance ||
1360 next_distance == curr_distance))
1361 next_distance = distance;
1364 * While not a strong assumption it would be nice to know
1365 * about cases where if node A is connected to B, B is not
1366 * equally connected to A.
1368 if (sched_debug() && node_distance(k, i) != distance)
1369 sched_numa_warn("Node-distance not symmetric");
1371 if (sched_debug() && i && !find_numa_distance(distance))
1372 sched_numa_warn("Node-0 not representative");
1374 if (next_distance != curr_distance) {
1375 sched_domains_numa_distance[level++] = next_distance;
1376 sched_domains_numa_levels = level;
1377 curr_distance = next_distance;
1378 } else break;
1382 * In case of sched_debug() we verify the above assumption.
1384 if (!sched_debug())
1385 break;
1388 if (!level)
1389 return;
1392 * 'level' contains the number of unique distances
1394 * The sched_domains_numa_distance[] array includes the actual distance
1395 * numbers.
1399 * Here, we should temporarily reset sched_domains_numa_levels to 0.
1400 * If it fails to allocate memory for array sched_domains_numa_masks[][],
1401 * the array will contain less then 'level' members. This could be
1402 * dangerous when we use it to iterate array sched_domains_numa_masks[][]
1403 * in other functions.
1405 * We reset it to 'level' at the end of this function.
1407 sched_domains_numa_levels = 0;
1409 sched_domains_numa_masks = kzalloc(sizeof(void *) * level, GFP_KERNEL);
1410 if (!sched_domains_numa_masks)
1411 return;
1414 * Now for each level, construct a mask per node which contains all
1415 * CPUs of nodes that are that many hops away from us.
1417 for (i = 0; i < level; i++) {
1418 sched_domains_numa_masks[i] =
1419 kzalloc(nr_node_ids * sizeof(void *), GFP_KERNEL);
1420 if (!sched_domains_numa_masks[i])
1421 return;
1423 for (j = 0; j < nr_node_ids; j++) {
1424 struct cpumask *mask = kzalloc(cpumask_size(), GFP_KERNEL);
1425 if (!mask)
1426 return;
1428 sched_domains_numa_masks[i][j] = mask;
1430 for_each_node(k) {
1431 if (node_distance(j, k) > sched_domains_numa_distance[i])
1432 continue;
1434 cpumask_or(mask, mask, cpumask_of_node(k));
1439 /* Compute default topology size */
1440 for (i = 0; sched_domain_topology[i].mask; i++);
1442 tl = kzalloc((i + level + 1) *
1443 sizeof(struct sched_domain_topology_level), GFP_KERNEL);
1444 if (!tl)
1445 return;
1448 * Copy the default topology bits..
1450 for (i = 0; sched_domain_topology[i].mask; i++)
1451 tl[i] = sched_domain_topology[i];
1454 * Add the NUMA identity distance, aka single NODE.
1456 tl[i++] = (struct sched_domain_topology_level){
1457 .mask = sd_numa_mask,
1458 .numa_level = 0,
1459 SD_INIT_NAME(NODE)
1463 * .. and append 'j' levels of NUMA goodness.
1465 for (j = 1; j < level; i++, j++) {
1466 tl[i] = (struct sched_domain_topology_level){
1467 .mask = sd_numa_mask,
1468 .sd_flags = cpu_numa_flags,
1469 .flags = SDTL_OVERLAP,
1470 .numa_level = j,
1471 SD_INIT_NAME(NUMA)
1475 sched_domain_topology = tl;
1477 sched_domains_numa_levels = level;
1478 sched_max_numa_distance = sched_domains_numa_distance[level - 1];
1480 init_numa_topology_type();
1483 void sched_domains_numa_masks_set(unsigned int cpu)
1485 int node = cpu_to_node(cpu);
1486 int i, j;
1488 for (i = 0; i < sched_domains_numa_levels; i++) {
1489 for (j = 0; j < nr_node_ids; j++) {
1490 if (node_distance(j, node) <= sched_domains_numa_distance[i])
1491 cpumask_set_cpu(cpu, sched_domains_numa_masks[i][j]);
1496 void sched_domains_numa_masks_clear(unsigned int cpu)
1498 int i, j;
1500 for (i = 0; i < sched_domains_numa_levels; i++) {
1501 for (j = 0; j < nr_node_ids; j++)
1502 cpumask_clear_cpu(cpu, sched_domains_numa_masks[i][j]);
1506 #endif /* CONFIG_NUMA */
1508 static int __sdt_alloc(const struct cpumask *cpu_map)
1510 struct sched_domain_topology_level *tl;
1511 int j;
1513 for_each_sd_topology(tl) {
1514 struct sd_data *sdd = &tl->data;
1516 sdd->sd = alloc_percpu(struct sched_domain *);
1517 if (!sdd->sd)
1518 return -ENOMEM;
1520 sdd->sds = alloc_percpu(struct sched_domain_shared *);
1521 if (!sdd->sds)
1522 return -ENOMEM;
1524 sdd->sg = alloc_percpu(struct sched_group *);
1525 if (!sdd->sg)
1526 return -ENOMEM;
1528 sdd->sgc = alloc_percpu(struct sched_group_capacity *);
1529 if (!sdd->sgc)
1530 return -ENOMEM;
1532 for_each_cpu(j, cpu_map) {
1533 struct sched_domain *sd;
1534 struct sched_domain_shared *sds;
1535 struct sched_group *sg;
1536 struct sched_group_capacity *sgc;
1538 sd = kzalloc_node(sizeof(struct sched_domain) + cpumask_size(),
1539 GFP_KERNEL, cpu_to_node(j));
1540 if (!sd)
1541 return -ENOMEM;
1543 *per_cpu_ptr(sdd->sd, j) = sd;
1545 sds = kzalloc_node(sizeof(struct sched_domain_shared),
1546 GFP_KERNEL, cpu_to_node(j));
1547 if (!sds)
1548 return -ENOMEM;
1550 *per_cpu_ptr(sdd->sds, j) = sds;
1552 sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
1553 GFP_KERNEL, cpu_to_node(j));
1554 if (!sg)
1555 return -ENOMEM;
1557 sg->next = sg;
1559 *per_cpu_ptr(sdd->sg, j) = sg;
1561 sgc = kzalloc_node(sizeof(struct sched_group_capacity) + cpumask_size(),
1562 GFP_KERNEL, cpu_to_node(j));
1563 if (!sgc)
1564 return -ENOMEM;
1566 #ifdef CONFIG_SCHED_DEBUG
1567 sgc->id = j;
1568 #endif
1570 *per_cpu_ptr(sdd->sgc, j) = sgc;
1574 return 0;
1577 static void __sdt_free(const struct cpumask *cpu_map)
1579 struct sched_domain_topology_level *tl;
1580 int j;
1582 for_each_sd_topology(tl) {
1583 struct sd_data *sdd = &tl->data;
1585 for_each_cpu(j, cpu_map) {
1586 struct sched_domain *sd;
1588 if (sdd->sd) {
1589 sd = *per_cpu_ptr(sdd->sd, j);
1590 if (sd && (sd->flags & SD_OVERLAP))
1591 free_sched_groups(sd->groups, 0);
1592 kfree(*per_cpu_ptr(sdd->sd, j));
1595 if (sdd->sds)
1596 kfree(*per_cpu_ptr(sdd->sds, j));
1597 if (sdd->sg)
1598 kfree(*per_cpu_ptr(sdd->sg, j));
1599 if (sdd->sgc)
1600 kfree(*per_cpu_ptr(sdd->sgc, j));
1602 free_percpu(sdd->sd);
1603 sdd->sd = NULL;
1604 free_percpu(sdd->sds);
1605 sdd->sds = NULL;
1606 free_percpu(sdd->sg);
1607 sdd->sg = NULL;
1608 free_percpu(sdd->sgc);
1609 sdd->sgc = NULL;
1613 static struct sched_domain *build_sched_domain(struct sched_domain_topology_level *tl,
1614 const struct cpumask *cpu_map, struct sched_domain_attr *attr,
1615 struct sched_domain *child, int cpu)
1617 struct sched_domain *sd = sd_init(tl, cpu_map, child, cpu);
1619 if (child) {
1620 sd->level = child->level + 1;
1621 sched_domain_level_max = max(sched_domain_level_max, sd->level);
1622 child->parent = sd;
1624 if (!cpumask_subset(sched_domain_span(child),
1625 sched_domain_span(sd))) {
1626 pr_err("BUG: arch topology borken\n");
1627 #ifdef CONFIG_SCHED_DEBUG
1628 pr_err(" the %s domain not a subset of the %s domain\n",
1629 child->name, sd->name);
1630 #endif
1631 /* Fixup, ensure @sd has at least @child cpus. */
1632 cpumask_or(sched_domain_span(sd),
1633 sched_domain_span(sd),
1634 sched_domain_span(child));
1638 set_domain_attribute(sd, attr);
1640 return sd;
1644 * Build sched domains for a given set of CPUs and attach the sched domains
1645 * to the individual CPUs
1647 static int
1648 build_sched_domains(const struct cpumask *cpu_map, struct sched_domain_attr *attr)
1650 enum s_alloc alloc_state;
1651 struct sched_domain *sd;
1652 struct s_data d;
1653 struct rq *rq = NULL;
1654 int i, ret = -ENOMEM;
1656 alloc_state = __visit_domain_allocation_hell(&d, cpu_map);
1657 if (alloc_state != sa_rootdomain)
1658 goto error;
1660 /* Set up domains for CPUs specified by the cpu_map: */
1661 for_each_cpu(i, cpu_map) {
1662 struct sched_domain_topology_level *tl;
1664 sd = NULL;
1665 for_each_sd_topology(tl) {
1666 sd = build_sched_domain(tl, cpu_map, attr, sd, i);
1667 if (tl == sched_domain_topology)
1668 *per_cpu_ptr(d.sd, i) = sd;
1669 if (tl->flags & SDTL_OVERLAP)
1670 sd->flags |= SD_OVERLAP;
1671 if (cpumask_equal(cpu_map, sched_domain_span(sd)))
1672 break;
1676 /* Build the groups for the domains */
1677 for_each_cpu(i, cpu_map) {
1678 for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
1679 sd->span_weight = cpumask_weight(sched_domain_span(sd));
1680 if (sd->flags & SD_OVERLAP) {
1681 if (build_overlap_sched_groups(sd, i))
1682 goto error;
1683 } else {
1684 if (build_sched_groups(sd, i))
1685 goto error;
1690 /* Calculate CPU capacity for physical packages and nodes */
1691 for (i = nr_cpumask_bits-1; i >= 0; i--) {
1692 if (!cpumask_test_cpu(i, cpu_map))
1693 continue;
1695 for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
1696 claim_allocations(i, sd);
1697 init_sched_groups_capacity(i, sd);
1701 /* Attach the domains */
1702 rcu_read_lock();
1703 for_each_cpu(i, cpu_map) {
1704 rq = cpu_rq(i);
1705 sd = *per_cpu_ptr(d.sd, i);
1707 /* Use READ_ONCE()/WRITE_ONCE() to avoid load/store tearing: */
1708 if (rq->cpu_capacity_orig > READ_ONCE(d.rd->max_cpu_capacity))
1709 WRITE_ONCE(d.rd->max_cpu_capacity, rq->cpu_capacity_orig);
1711 cpu_attach_domain(sd, d.rd, i);
1713 rcu_read_unlock();
1715 if (rq && sched_debug_enabled) {
1716 pr_info("span: %*pbl (max cpu_capacity = %lu)\n",
1717 cpumask_pr_args(cpu_map), rq->rd->max_cpu_capacity);
1720 ret = 0;
1721 error:
1722 __free_domain_allocs(&d, alloc_state, cpu_map);
1723 return ret;
1726 /* Current sched domains: */
1727 static cpumask_var_t *doms_cur;
1729 /* Number of sched domains in 'doms_cur': */
1730 static int ndoms_cur;
1732 /* Attribues of custom domains in 'doms_cur' */
1733 static struct sched_domain_attr *dattr_cur;
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.
1740 static cpumask_var_t fallback_doms;
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.
1747 int __weak arch_update_cpu_topology(void)
1749 return 0;
1752 cpumask_var_t *alloc_sched_domains(unsigned int ndoms)
1754 int i;
1755 cpumask_var_t *doms;
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;
1766 return doms;
1769 void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms)
1771 unsigned int i;
1772 for (i = 0; i < ndoms; i++)
1773 free_cpumask_var(doms[i]);
1774 kfree(doms);
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.
1782 int sched_init_domains(const struct cpumask *cpu_map)
1784 int err;
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);
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();
1799 return err;
1803 * Detach sched domains from a group of CPUs specified in cpu_map
1804 * These CPUs will now be attached to the NULL domain
1806 static void detach_destroy_domains(const struct cpumask *cpu_map)
1808 int i;
1810 rcu_read_lock();
1811 for_each_cpu(i, cpu_map)
1812 cpu_attach_domain(NULL, &def_root_domain, i);
1813 rcu_read_unlock();
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)
1820 struct sched_domain_attr tmp;
1822 /* Fast path: */
1823 if (!new && !cur)
1824 return 1;
1826 tmp = SD_ATTR_INIT;
1827 return !memcmp(cur ? (cur + idx_cur) : &tmp,
1828 new ? (new + idx_new) : &tmp,
1829 sizeof(struct sched_domain_attr));
1833 * Partition sched domains as specified by the 'ndoms_new'
1834 * cpumasks in the array doms_new[] of cpumasks. This compares
1835 * doms_new[] to the current sched domain partitioning, doms_cur[].
1836 * It destroys each deleted domain and builds each new domain.
1838 * 'doms_new' is an array of cpumask_var_t's of length 'ndoms_new'.
1839 * The masks don't intersect (don't overlap.) We should setup one
1840 * sched domain for each mask. CPUs not in any of the cpumasks will
1841 * not be load balanced. If the same cpumask appears both in the
1842 * current 'doms_cur' domains and in the new 'doms_new', we can leave
1843 * it as it is.
1845 * The passed in 'doms_new' should be allocated using
1846 * alloc_sched_domains. This routine takes ownership of it and will
1847 * free_sched_domains it when done with it. If the caller failed the
1848 * alloc call, then it can pass in doms_new == NULL && ndoms_new == 1,
1849 * and partition_sched_domains() will fallback to the single partition
1850 * 'fallback_doms', it also forces the domains to be rebuilt.
1852 * If doms_new == NULL it will be replaced with cpu_online_mask.
1853 * ndoms_new == 0 is a special case for destroying existing domains,
1854 * and it will not create the default domain.
1856 * Call with hotplug lock held
1858 void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
1859 struct sched_domain_attr *dattr_new)
1861 int i, j, n;
1862 int new_topology;
1864 mutex_lock(&sched_domains_mutex);
1866 /* Always unregister in case we don't destroy any domains: */
1867 unregister_sched_domain_sysctl();
1869 /* Let the architecture update CPU core mappings: */
1870 new_topology = arch_update_cpu_topology();
1872 if (!doms_new) {
1873 WARN_ON_ONCE(dattr_new);
1874 n = 0;
1875 doms_new = alloc_sched_domains(1);
1876 if (doms_new) {
1877 n = 1;
1878 cpumask_and(doms_new[0], cpu_active_mask,
1879 housekeeping_cpumask(HK_FLAG_DOMAIN));
1881 } else {
1882 n = ndoms_new;
1885 /* Destroy deleted domains: */
1886 for (i = 0; i < ndoms_cur; i++) {
1887 for (j = 0; j < n && !new_topology; j++) {
1888 if (cpumask_equal(doms_cur[i], doms_new[j])
1889 && dattrs_equal(dattr_cur, i, dattr_new, j))
1890 goto match1;
1892 /* No match - a current sched domain not in new doms_new[] */
1893 detach_destroy_domains(doms_cur[i]);
1894 match1:
1898 n = ndoms_cur;
1899 if (!doms_new) {
1900 n = 0;
1901 doms_new = &fallback_doms;
1902 cpumask_and(doms_new[0], cpu_active_mask,
1903 housekeeping_cpumask(HK_FLAG_DOMAIN));
1906 /* Build new domains: */
1907 for (i = 0; i < ndoms_new; i++) {
1908 for (j = 0; j < n && !new_topology; j++) {
1909 if (cpumask_equal(doms_new[i], doms_cur[j])
1910 && dattrs_equal(dattr_new, i, dattr_cur, j))
1911 goto match2;
1913 /* No match - add a new doms_new */
1914 build_sched_domains(doms_new[i], dattr_new ? dattr_new + i : NULL);
1915 match2:
1919 /* Remember the new sched domains: */
1920 if (doms_cur != &fallback_doms)
1921 free_sched_domains(doms_cur, ndoms_cur);
1923 kfree(dattr_cur);
1924 doms_cur = doms_new;
1925 dattr_cur = dattr_new;
1926 ndoms_cur = ndoms_new;
1928 register_sched_domain_sysctl();
1930 mutex_unlock(&sched_domains_mutex);