1 // SPDX-License-Identifier: GPL-2.0
3 * Scheduler topology setup/handling methods
6 #include <linux/bsearch.h>
8 DEFINE_MUTEX(sched_domains_mutex);
10 /* Protected by sched_domains_mutex: */
11 static cpumask_var_t sched_domains_tmpmask;
12 static cpumask_var_t sched_domains_tmpmask2;
14 #ifdef CONFIG_SCHED_DEBUG
16 static int __init sched_debug_setup(char *str)
18 sched_debug_verbose = true;
22 early_param("sched_verbose", sched_debug_setup);
24 static inline bool sched_debug(void)
26 return sched_debug_verbose;
29 #define SD_FLAG(_name, mflags) [__##_name] = { .meta_flags = mflags, .name = #_name },
30 const struct sd_flag_debug sd_flag_debug[] = {
31 #include <linux/sched/sd_flags.h>
35 static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
36 struct cpumask *groupmask)
38 struct sched_group *group = sd->groups;
39 unsigned long flags = sd->flags;
42 cpumask_clear(groupmask);
44 printk(KERN_DEBUG "%*s domain-%d: ", level, "", level);
45 printk(KERN_CONT "span=%*pbl level=%s\n",
46 cpumask_pr_args(sched_domain_span(sd)), sd->name);
48 if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) {
49 printk(KERN_ERR "ERROR: domain->span does not contain CPU%d\n", cpu);
51 if (group && !cpumask_test_cpu(cpu, sched_group_span(group))) {
52 printk(KERN_ERR "ERROR: domain->groups does not contain CPU%d\n", cpu);
55 for_each_set_bit(idx, &flags, __SD_FLAG_CNT) {
56 unsigned int flag = BIT(idx);
57 unsigned int meta_flags = sd_flag_debug[idx].meta_flags;
59 if ((meta_flags & SDF_SHARED_CHILD) && sd->child &&
60 !(sd->child->flags & flag))
61 printk(KERN_ERR "ERROR: flag %s set here but not in child\n",
62 sd_flag_debug[idx].name);
64 if ((meta_flags & SDF_SHARED_PARENT) && sd->parent &&
65 !(sd->parent->flags & flag))
66 printk(KERN_ERR "ERROR: flag %s set here but not in parent\n",
67 sd_flag_debug[idx].name);
70 printk(KERN_DEBUG "%*s groups:", level + 1, "");
74 printk(KERN_ERR "ERROR: group is NULL\n");
78 if (cpumask_empty(sched_group_span(group))) {
79 printk(KERN_CONT "\n");
80 printk(KERN_ERR "ERROR: empty group\n");
84 if (!(sd->flags & SD_OVERLAP) &&
85 cpumask_intersects(groupmask, sched_group_span(group))) {
86 printk(KERN_CONT "\n");
87 printk(KERN_ERR "ERROR: repeated CPUs\n");
91 cpumask_or(groupmask, groupmask, sched_group_span(group));
93 printk(KERN_CONT " %d:{ span=%*pbl",
95 cpumask_pr_args(sched_group_span(group)));
97 if ((sd->flags & SD_OVERLAP) &&
98 !cpumask_equal(group_balance_mask(group), sched_group_span(group))) {
99 printk(KERN_CONT " mask=%*pbl",
100 cpumask_pr_args(group_balance_mask(group)));
103 if (group->sgc->capacity != SCHED_CAPACITY_SCALE)
104 printk(KERN_CONT " cap=%lu", group->sgc->capacity);
106 if (group == sd->groups && sd->child &&
107 !cpumask_equal(sched_domain_span(sd->child),
108 sched_group_span(group))) {
109 printk(KERN_ERR "ERROR: domain->groups does not match domain->child\n");
112 printk(KERN_CONT " }");
116 if (group != sd->groups)
117 printk(KERN_CONT ",");
119 } while (group != sd->groups);
120 printk(KERN_CONT "\n");
122 if (!cpumask_equal(sched_domain_span(sd), groupmask))
123 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
126 !cpumask_subset(groupmask, sched_domain_span(sd->parent)))
127 printk(KERN_ERR "ERROR: parent span is not a superset of domain->span\n");
131 static void sched_domain_debug(struct sched_domain *sd, int cpu)
135 if (!sched_debug_verbose)
139 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
143 printk(KERN_DEBUG "CPU%d attaching sched-domain(s):\n", cpu);
146 if (sched_domain_debug_one(sd, cpu, level, sched_domains_tmpmask))
154 #else /* !CONFIG_SCHED_DEBUG */
156 # define sched_debug_verbose 0
157 # define sched_domain_debug(sd, cpu) do { } while (0)
158 static inline bool sched_debug(void)
162 #endif /* CONFIG_SCHED_DEBUG */
164 /* Generate a mask of SD flags with the SDF_NEEDS_GROUPS metaflag */
165 #define SD_FLAG(name, mflags) (name * !!((mflags) & SDF_NEEDS_GROUPS)) |
166 static const unsigned int SD_DEGENERATE_GROUPS_MASK =
167 #include <linux/sched/sd_flags.h>
171 static int sd_degenerate(struct sched_domain *sd)
173 if (cpumask_weight(sched_domain_span(sd)) == 1)
176 /* Following flags need at least 2 groups */
177 if ((sd->flags & SD_DEGENERATE_GROUPS_MASK) &&
178 (sd->groups != sd->groups->next))
181 /* Following flags don't use groups */
182 if (sd->flags & (SD_WAKE_AFFINE))
189 sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
191 unsigned long cflags = sd->flags, pflags = parent->flags;
193 if (sd_degenerate(parent))
196 if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent)))
199 /* Flags needing groups don't count if only 1 group in parent */
200 if (parent->groups == parent->groups->next)
201 pflags &= ~SD_DEGENERATE_GROUPS_MASK;
203 if (~cflags & pflags)
209 #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
210 DEFINE_STATIC_KEY_FALSE(sched_energy_present);
211 static unsigned int sysctl_sched_energy_aware = 1;
212 static DEFINE_MUTEX(sched_energy_mutex);
213 static bool sched_energy_update;
215 void rebuild_sched_domains_energy(void)
217 mutex_lock(&sched_energy_mutex);
218 sched_energy_update = true;
219 rebuild_sched_domains();
220 sched_energy_update = false;
221 mutex_unlock(&sched_energy_mutex);
224 #ifdef CONFIG_PROC_SYSCTL
225 static int sched_energy_aware_handler(struct ctl_table *table, int write,
226 void *buffer, size_t *lenp, loff_t *ppos)
230 if (write && !capable(CAP_SYS_ADMIN))
233 ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
235 state = static_branch_unlikely(&sched_energy_present);
236 if (state != sysctl_sched_energy_aware)
237 rebuild_sched_domains_energy();
243 static struct ctl_table sched_energy_aware_sysctls[] = {
245 .procname = "sched_energy_aware",
246 .data = &sysctl_sched_energy_aware,
247 .maxlen = sizeof(unsigned int),
249 .proc_handler = sched_energy_aware_handler,
250 .extra1 = SYSCTL_ZERO,
251 .extra2 = SYSCTL_ONE,
256 static int __init sched_energy_aware_sysctl_init(void)
258 register_sysctl_init("kernel", sched_energy_aware_sysctls);
262 late_initcall(sched_energy_aware_sysctl_init);
265 static void free_pd(struct perf_domain *pd)
267 struct perf_domain *tmp;
276 static struct perf_domain *find_pd(struct perf_domain *pd, int cpu)
279 if (cpumask_test_cpu(cpu, perf_domain_span(pd)))
287 static struct perf_domain *pd_init(int cpu)
289 struct em_perf_domain *obj = em_cpu_get(cpu);
290 struct perf_domain *pd;
294 pr_info("%s: no EM found for CPU%d\n", __func__, cpu);
298 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
306 static void perf_domain_debug(const struct cpumask *cpu_map,
307 struct perf_domain *pd)
309 if (!sched_debug() || !pd)
312 printk(KERN_DEBUG "root_domain %*pbl:", cpumask_pr_args(cpu_map));
315 printk(KERN_CONT " pd%d:{ cpus=%*pbl nr_pstate=%d }",
316 cpumask_first(perf_domain_span(pd)),
317 cpumask_pr_args(perf_domain_span(pd)),
318 em_pd_nr_perf_states(pd->em_pd));
322 printk(KERN_CONT "\n");
325 static void destroy_perf_domain_rcu(struct rcu_head *rp)
327 struct perf_domain *pd;
329 pd = container_of(rp, struct perf_domain, rcu);
333 static void sched_energy_set(bool has_eas)
335 if (!has_eas && static_branch_unlikely(&sched_energy_present)) {
337 pr_info("%s: stopping EAS\n", __func__);
338 static_branch_disable_cpuslocked(&sched_energy_present);
339 } else if (has_eas && !static_branch_unlikely(&sched_energy_present)) {
341 pr_info("%s: starting EAS\n", __func__);
342 static_branch_enable_cpuslocked(&sched_energy_present);
347 * EAS can be used on a root domain if it meets all the following conditions:
348 * 1. an Energy Model (EM) is available;
349 * 2. the SD_ASYM_CPUCAPACITY flag is set in the sched_domain hierarchy.
350 * 3. no SMT is detected.
351 * 4. the EM complexity is low enough to keep scheduling overheads low;
352 * 5. schedutil is driving the frequency of all CPUs of the rd;
353 * 6. frequency invariance support is present;
355 * The complexity of the Energy Model is defined as:
357 * C = nr_pd * (nr_cpus + nr_ps)
359 * with parameters defined as:
360 * - nr_pd: the number of performance domains
361 * - nr_cpus: the number of CPUs
362 * - nr_ps: the sum of the number of performance states of all performance
363 * domains (for example, on a system with 2 performance domains,
364 * with 10 performance states each, nr_ps = 2 * 10 = 20).
366 * It is generally not a good idea to use such a model in the wake-up path on
367 * very complex platforms because of the associated scheduling overheads. The
368 * arbitrary constraint below prevents that. It makes EAS usable up to 16 CPUs
369 * with per-CPU DVFS and less than 8 performance states each, for example.
371 #define EM_MAX_COMPLEXITY 2048
373 extern struct cpufreq_governor schedutil_gov;
374 static bool build_perf_domains(const struct cpumask *cpu_map)
376 int i, nr_pd = 0, nr_ps = 0, nr_cpus = cpumask_weight(cpu_map);
377 struct perf_domain *pd = NULL, *tmp;
378 int cpu = cpumask_first(cpu_map);
379 struct root_domain *rd = cpu_rq(cpu)->rd;
380 struct cpufreq_policy *policy;
381 struct cpufreq_governor *gov;
383 if (!sysctl_sched_energy_aware)
386 /* EAS is enabled for asymmetric CPU capacity topologies. */
387 if (!per_cpu(sd_asym_cpucapacity, cpu)) {
389 pr_info("rd %*pbl: CPUs do not have asymmetric capacities\n",
390 cpumask_pr_args(cpu_map));
395 /* EAS definitely does *not* handle SMT */
396 if (sched_smt_active()) {
397 pr_warn("rd %*pbl: Disabling EAS, SMT is not supported\n",
398 cpumask_pr_args(cpu_map));
402 if (!arch_scale_freq_invariant()) {
404 pr_warn("rd %*pbl: Disabling EAS: frequency-invariant load tracking not yet supported",
405 cpumask_pr_args(cpu_map));
410 for_each_cpu(i, cpu_map) {
411 /* Skip already covered CPUs. */
415 /* Do not attempt EAS if schedutil is not being used. */
416 policy = cpufreq_cpu_get(i);
419 gov = policy->governor;
420 cpufreq_cpu_put(policy);
421 if (gov != &schedutil_gov) {
423 pr_warn("rd %*pbl: Disabling EAS, schedutil is mandatory\n",
424 cpumask_pr_args(cpu_map));
428 /* Create the new pd and add it to the local list. */
436 * Count performance domains and performance states for the
440 nr_ps += em_pd_nr_perf_states(pd->em_pd);
443 /* Bail out if the Energy Model complexity is too high. */
444 if (nr_pd * (nr_ps + nr_cpus) > EM_MAX_COMPLEXITY) {
445 WARN(1, "rd %*pbl: Failed to start EAS, EM complexity is too high\n",
446 cpumask_pr_args(cpu_map));
450 perf_domain_debug(cpu_map, pd);
452 /* Attach the new list of performance domains to the root domain. */
454 rcu_assign_pointer(rd->pd, pd);
456 call_rcu(&tmp->rcu, destroy_perf_domain_rcu);
463 rcu_assign_pointer(rd->pd, NULL);
465 call_rcu(&tmp->rcu, destroy_perf_domain_rcu);
470 static void free_pd(struct perf_domain *pd) { }
471 #endif /* CONFIG_ENERGY_MODEL && CONFIG_CPU_FREQ_GOV_SCHEDUTIL*/
473 static void free_rootdomain(struct rcu_head *rcu)
475 struct root_domain *rd = container_of(rcu, struct root_domain, rcu);
477 cpupri_cleanup(&rd->cpupri);
478 cpudl_cleanup(&rd->cpudl);
479 free_cpumask_var(rd->dlo_mask);
480 free_cpumask_var(rd->rto_mask);
481 free_cpumask_var(rd->online);
482 free_cpumask_var(rd->span);
487 void rq_attach_root(struct rq *rq, struct root_domain *rd)
489 struct root_domain *old_rd = NULL;
492 rq_lock_irqsave(rq, &rf);
497 if (cpumask_test_cpu(rq->cpu, old_rd->online))
500 cpumask_clear_cpu(rq->cpu, old_rd->span);
503 * If we dont want to free the old_rd yet then
504 * set old_rd to NULL to skip the freeing later
507 if (!atomic_dec_and_test(&old_rd->refcount))
511 atomic_inc(&rd->refcount);
514 cpumask_set_cpu(rq->cpu, rd->span);
515 if (cpumask_test_cpu(rq->cpu, cpu_active_mask))
518 rq_unlock_irqrestore(rq, &rf);
521 call_rcu(&old_rd->rcu, free_rootdomain);
524 void sched_get_rd(struct root_domain *rd)
526 atomic_inc(&rd->refcount);
529 void sched_put_rd(struct root_domain *rd)
531 if (!atomic_dec_and_test(&rd->refcount))
534 call_rcu(&rd->rcu, free_rootdomain);
537 static int init_rootdomain(struct root_domain *rd)
539 if (!zalloc_cpumask_var(&rd->span, GFP_KERNEL))
541 if (!zalloc_cpumask_var(&rd->online, GFP_KERNEL))
543 if (!zalloc_cpumask_var(&rd->dlo_mask, GFP_KERNEL))
545 if (!zalloc_cpumask_var(&rd->rto_mask, GFP_KERNEL))
548 #ifdef HAVE_RT_PUSH_IPI
550 raw_spin_lock_init(&rd->rto_lock);
551 rd->rto_push_work = IRQ_WORK_INIT_HARD(rto_push_irq_work_func);
555 init_dl_bw(&rd->dl_bw);
556 if (cpudl_init(&rd->cpudl) != 0)
559 if (cpupri_init(&rd->cpupri) != 0)
564 cpudl_cleanup(&rd->cpudl);
566 free_cpumask_var(rd->rto_mask);
568 free_cpumask_var(rd->dlo_mask);
570 free_cpumask_var(rd->online);
572 free_cpumask_var(rd->span);
578 * By default the system creates a single root-domain with all CPUs as
579 * members (mimicking the global state we have today).
581 struct root_domain def_root_domain;
583 void __init init_defrootdomain(void)
585 init_rootdomain(&def_root_domain);
587 atomic_set(&def_root_domain.refcount, 1);
590 static struct root_domain *alloc_rootdomain(void)
592 struct root_domain *rd;
594 rd = kzalloc(sizeof(*rd), GFP_KERNEL);
598 if (init_rootdomain(rd) != 0) {
606 static void free_sched_groups(struct sched_group *sg, int free_sgc)
608 struct sched_group *tmp, *first;
617 if (free_sgc && atomic_dec_and_test(&sg->sgc->ref))
620 if (atomic_dec_and_test(&sg->ref))
623 } while (sg != first);
626 static void destroy_sched_domain(struct sched_domain *sd)
629 * A normal sched domain may have multiple group references, an
630 * overlapping domain, having private groups, only one. Iterate,
631 * dropping group/capacity references, freeing where none remain.
633 free_sched_groups(sd->groups, 1);
635 if (sd->shared && atomic_dec_and_test(&sd->shared->ref))
640 static void destroy_sched_domains_rcu(struct rcu_head *rcu)
642 struct sched_domain *sd = container_of(rcu, struct sched_domain, rcu);
645 struct sched_domain *parent = sd->parent;
646 destroy_sched_domain(sd);
651 static void destroy_sched_domains(struct sched_domain *sd)
654 call_rcu(&sd->rcu, destroy_sched_domains_rcu);
658 * Keep a special pointer to the highest sched_domain that has
659 * SD_SHARE_PKG_RESOURCE set (Last Level Cache Domain) for this
660 * allows us to avoid some pointer chasing select_idle_sibling().
662 * Also keep a unique ID per domain (we use the first CPU number in
663 * the cpumask of the domain), this allows us to quickly tell if
664 * two CPUs are in the same cache domain, see cpus_share_cache().
666 DEFINE_PER_CPU(struct sched_domain __rcu *, sd_llc);
667 DEFINE_PER_CPU(int, sd_llc_size);
668 DEFINE_PER_CPU(int, sd_llc_id);
669 DEFINE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared);
670 DEFINE_PER_CPU(struct sched_domain __rcu *, sd_numa);
671 DEFINE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing);
672 DEFINE_PER_CPU(struct sched_domain __rcu *, sd_asym_cpucapacity);
673 DEFINE_STATIC_KEY_FALSE(sched_asym_cpucapacity);
675 static void update_top_cache_domain(int cpu)
677 struct sched_domain_shared *sds = NULL;
678 struct sched_domain *sd;
682 sd = highest_flag_domain(cpu, SD_SHARE_PKG_RESOURCES);
684 id = cpumask_first(sched_domain_span(sd));
685 size = cpumask_weight(sched_domain_span(sd));
689 rcu_assign_pointer(per_cpu(sd_llc, cpu), sd);
690 per_cpu(sd_llc_size, cpu) = size;
691 per_cpu(sd_llc_id, cpu) = id;
692 rcu_assign_pointer(per_cpu(sd_llc_shared, cpu), sds);
694 sd = lowest_flag_domain(cpu, SD_NUMA);
695 rcu_assign_pointer(per_cpu(sd_numa, cpu), sd);
697 sd = highest_flag_domain(cpu, SD_ASYM_PACKING);
698 rcu_assign_pointer(per_cpu(sd_asym_packing, cpu), sd);
700 sd = lowest_flag_domain(cpu, SD_ASYM_CPUCAPACITY_FULL);
701 rcu_assign_pointer(per_cpu(sd_asym_cpucapacity, cpu), sd);
705 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
706 * hold the hotplug lock.
709 cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
711 struct rq *rq = cpu_rq(cpu);
712 struct sched_domain *tmp;
714 /* Remove the sched domains which do not contribute to scheduling. */
715 for (tmp = sd; tmp; ) {
716 struct sched_domain *parent = tmp->parent;
720 if (sd_parent_degenerate(tmp, parent)) {
721 tmp->parent = parent->parent;
723 if (parent->parent) {
724 parent->parent->child = tmp;
725 if (tmp->flags & SD_SHARE_CPUCAPACITY)
726 parent->parent->groups->flags |= SD_SHARE_CPUCAPACITY;
730 * Transfer SD_PREFER_SIBLING down in case of a
731 * degenerate parent; the spans match for this
732 * so the property transfers.
734 if (parent->flags & SD_PREFER_SIBLING)
735 tmp->flags |= SD_PREFER_SIBLING;
736 destroy_sched_domain(parent);
741 if (sd && sd_degenerate(sd)) {
744 destroy_sched_domain(tmp);
746 struct sched_group *sg = sd->groups;
749 * sched groups hold the flags of the child sched
750 * domain for convenience. Clear such flags since
751 * the child is being destroyed.
755 } while (sg != sd->groups);
761 sched_domain_debug(sd, cpu);
763 rq_attach_root(rq, rd);
765 rcu_assign_pointer(rq->sd, sd);
766 dirty_sched_domain_sysctl(cpu);
767 destroy_sched_domains(tmp);
769 update_top_cache_domain(cpu);
773 struct sched_domain * __percpu *sd;
774 struct root_domain *rd;
785 * Return the canonical balance CPU for this group, this is the first CPU
786 * of this group that's also in the balance mask.
788 * The balance mask are all those CPUs that could actually end up at this
789 * group. See build_balance_mask().
791 * Also see should_we_balance().
793 int group_balance_cpu(struct sched_group *sg)
795 return cpumask_first(group_balance_mask(sg));
800 * NUMA topology (first read the regular topology blurb below)
802 * Given a node-distance table, for example:
810 * which represents a 4 node ring topology like:
818 * We want to construct domains and groups to represent this. The way we go
819 * about doing this is to build the domains on 'hops'. For each NUMA level we
820 * construct the mask of all nodes reachable in @level hops.
822 * For the above NUMA topology that gives 3 levels:
824 * NUMA-2 0-3 0-3 0-3 0-3
825 * groups: {0-1,3},{1-3} {0-2},{0,2-3} {1-3},{0-1,3} {0,2-3},{0-2}
827 * NUMA-1 0-1,3 0-2 1-3 0,2-3
828 * groups: {0},{1},{3} {0},{1},{2} {1},{2},{3} {0},{2},{3}
833 * As can be seen; things don't nicely line up as with the regular topology.
834 * When we iterate a domain in child domain chunks some nodes can be
835 * represented multiple times -- hence the "overlap" naming for this part of
838 * In order to minimize this overlap, we only build enough groups to cover the
839 * domain. For instance Node-0 NUMA-2 would only get groups: 0-1,3 and 1-3.
843 * - the first group of each domain is its child domain; this
844 * gets us the first 0-1,3
845 * - the only uncovered node is 2, who's child domain is 1-3.
847 * However, because of the overlap, computing a unique CPU for each group is
848 * more complicated. Consider for instance the groups of NODE-1 NUMA-2, both
849 * groups include the CPUs of Node-0, while those CPUs would not in fact ever
850 * end up at those groups (they would end up in group: 0-1,3).
852 * To correct this we have to introduce the group balance mask. This mask
853 * will contain those CPUs in the group that can reach this group given the
854 * (child) domain tree.
856 * With this we can once again compute balance_cpu and sched_group_capacity
859 * XXX include words on how balance_cpu is unique and therefore can be
860 * used for sched_group_capacity links.
863 * Another 'interesting' topology is:
871 * Which looks a little like:
879 * This topology is asymmetric, nodes 1,2 are fully connected, but nodes 0,3
882 * This leads to a few particularly weird cases where the sched_domain's are
883 * not of the same number for each CPU. Consider:
886 * groups: {0-2},{1-3} {1-3},{0-2}
888 * NUMA-1 0-2 0-3 0-3 1-3
896 * Build the balance mask; it contains only those CPUs that can arrive at this
897 * group and should be considered to continue balancing.
899 * We do this during the group creation pass, therefore the group information
900 * isn't complete yet, however since each group represents a (child) domain we
901 * can fully construct this using the sched_domain bits (which are already
905 build_balance_mask(struct sched_domain *sd, struct sched_group *sg, struct cpumask *mask)
907 const struct cpumask *sg_span = sched_group_span(sg);
908 struct sd_data *sdd = sd->private;
909 struct sched_domain *sibling;
914 for_each_cpu(i, sg_span) {
915 sibling = *per_cpu_ptr(sdd->sd, i);
918 * Can happen in the asymmetric case, where these siblings are
919 * unused. The mask will not be empty because those CPUs that
920 * do have the top domain _should_ span the domain.
925 /* If we would not end up here, we can't continue from here */
926 if (!cpumask_equal(sg_span, sched_domain_span(sibling->child)))
929 cpumask_set_cpu(i, mask);
932 /* We must not have empty masks here */
933 WARN_ON_ONCE(cpumask_empty(mask));
937 * XXX: This creates per-node group entries; since the load-balancer will
938 * immediately access remote memory to construct this group's load-balance
939 * statistics having the groups node local is of dubious benefit.
941 static struct sched_group *
942 build_group_from_child_sched_domain(struct sched_domain *sd, int cpu)
944 struct sched_group *sg;
945 struct cpumask *sg_span;
947 sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
948 GFP_KERNEL, cpu_to_node(cpu));
953 sg_span = sched_group_span(sg);
955 cpumask_copy(sg_span, sched_domain_span(sd->child));
956 sg->flags = sd->child->flags;
958 cpumask_copy(sg_span, sched_domain_span(sd));
961 atomic_inc(&sg->ref);
965 static void init_overlap_sched_group(struct sched_domain *sd,
966 struct sched_group *sg)
968 struct cpumask *mask = sched_domains_tmpmask2;
969 struct sd_data *sdd = sd->private;
970 struct cpumask *sg_span;
973 build_balance_mask(sd, sg, mask);
974 cpu = cpumask_first(mask);
976 sg->sgc = *per_cpu_ptr(sdd->sgc, cpu);
977 if (atomic_inc_return(&sg->sgc->ref) == 1)
978 cpumask_copy(group_balance_mask(sg), mask);
980 WARN_ON_ONCE(!cpumask_equal(group_balance_mask(sg), mask));
983 * Initialize sgc->capacity such that even if we mess up the
984 * domains and no possible iteration will get us here, we won't
987 sg_span = sched_group_span(sg);
988 sg->sgc->capacity = SCHED_CAPACITY_SCALE * cpumask_weight(sg_span);
989 sg->sgc->min_capacity = SCHED_CAPACITY_SCALE;
990 sg->sgc->max_capacity = SCHED_CAPACITY_SCALE;
993 static struct sched_domain *
994 find_descended_sibling(struct sched_domain *sd, struct sched_domain *sibling)
997 * The proper descendant would be the one whose child won't span out
1000 while (sibling->child &&
1001 !cpumask_subset(sched_domain_span(sibling->child),
1002 sched_domain_span(sd)))
1003 sibling = sibling->child;
1006 * As we are referencing sgc across different topology level, we need
1007 * to go down to skip those sched_domains which don't contribute to
1008 * scheduling because they will be degenerated in cpu_attach_domain
1010 while (sibling->child &&
1011 cpumask_equal(sched_domain_span(sibling->child),
1012 sched_domain_span(sibling)))
1013 sibling = sibling->child;
1019 build_overlap_sched_groups(struct sched_domain *sd, int cpu)
1021 struct sched_group *first = NULL, *last = NULL, *sg;
1022 const struct cpumask *span = sched_domain_span(sd);
1023 struct cpumask *covered = sched_domains_tmpmask;
1024 struct sd_data *sdd = sd->private;
1025 struct sched_domain *sibling;
1028 cpumask_clear(covered);
1030 for_each_cpu_wrap(i, span, cpu) {
1031 struct cpumask *sg_span;
1033 if (cpumask_test_cpu(i, covered))
1036 sibling = *per_cpu_ptr(sdd->sd, i);
1039 * Asymmetric node setups can result in situations where the
1040 * domain tree is of unequal depth, make sure to skip domains
1041 * that already cover the entire range.
1043 * In that case build_sched_domains() will have terminated the
1044 * iteration early and our sibling sd spans will be empty.
1045 * Domains should always include the CPU they're built on, so
1048 if (!cpumask_test_cpu(i, sched_domain_span(sibling)))
1052 * Usually we build sched_group by sibling's child sched_domain
1053 * But for machines whose NUMA diameter are 3 or above, we move
1054 * to build sched_group by sibling's proper descendant's child
1055 * domain because sibling's child sched_domain will span out of
1056 * the sched_domain being built as below.
1058 * Smallest diameter=3 topology is:
1066 * 0 --- 1 --- 2 --- 3
1068 * NUMA-3 0-3 N/A N/A 0-3
1069 * groups: {0-2},{1-3} {1-3},{0-2}
1071 * NUMA-2 0-2 0-3 0-3 1-3
1072 * groups: {0-1},{1-3} {0-2},{2-3} {1-3},{0-1} {2-3},{0-2}
1074 * NUMA-1 0-1 0-2 1-3 2-3
1075 * groups: {0},{1} {1},{2},{0} {2},{3},{1} {3},{2}
1079 * The NUMA-2 groups for nodes 0 and 3 are obviously buggered, as the
1080 * group span isn't a subset of the domain span.
1082 if (sibling->child &&
1083 !cpumask_subset(sched_domain_span(sibling->child), span))
1084 sibling = find_descended_sibling(sd, sibling);
1086 sg = build_group_from_child_sched_domain(sibling, cpu);
1090 sg_span = sched_group_span(sg);
1091 cpumask_or(covered, covered, sg_span);
1093 init_overlap_sched_group(sibling, sg);
1107 free_sched_groups(first, 0);
1114 * Package topology (also see the load-balance blurb in fair.c)
1116 * The scheduler builds a tree structure to represent a number of important
1117 * topology features. By default (default_topology[]) these include:
1119 * - Simultaneous multithreading (SMT)
1120 * - Multi-Core Cache (MC)
1123 * Where the last one more or less denotes everything up to a NUMA node.
1125 * The tree consists of 3 primary data structures:
1127 * sched_domain -> sched_group -> sched_group_capacity
1131 * The sched_domains are per-CPU and have a two way link (parent & child) and
1132 * denote the ever growing mask of CPUs belonging to that level of topology.
1134 * Each sched_domain has a circular (double) linked list of sched_group's, each
1135 * denoting the domains of the level below (or individual CPUs in case of the
1136 * first domain level). The sched_group linked by a sched_domain includes the
1137 * CPU of that sched_domain [*].
1139 * Take for instance a 2 threaded, 2 core, 2 cache cluster part:
1141 * CPU 0 1 2 3 4 5 6 7
1145 * SMT [ ] [ ] [ ] [ ]
1149 * DIE 0-7 0-7 0-7 0-7 0-7 0-7 0-7 0-7
1150 * MC 0-3 0-3 0-3 0-3 4-7 4-7 4-7 4-7
1151 * SMT 0-1 0-1 2-3 2-3 4-5 4-5 6-7 6-7
1153 * CPU 0 1 2 3 4 5 6 7
1155 * One way to think about it is: sched_domain moves you up and down among these
1156 * topology levels, while sched_group moves you sideways through it, at child
1157 * domain granularity.
1159 * sched_group_capacity ensures each unique sched_group has shared storage.
1161 * There are two related construction problems, both require a CPU that
1162 * uniquely identify each group (for a given domain):
1164 * - The first is the balance_cpu (see should_we_balance() and the
1165 * load-balance blub in fair.c); for each group we only want 1 CPU to
1166 * continue balancing at a higher domain.
1168 * - The second is the sched_group_capacity; we want all identical groups
1169 * to share a single sched_group_capacity.
1171 * Since these topologies are exclusive by construction. That is, its
1172 * impossible for an SMT thread to belong to multiple cores, and cores to
1173 * be part of multiple caches. There is a very clear and unique location
1174 * for each CPU in the hierarchy.
1176 * Therefore computing a unique CPU for each group is trivial (the iteration
1177 * mask is redundant and set all 1s; all CPUs in a group will end up at _that_
1178 * group), we can simply pick the first CPU in each group.
1181 * [*] in other words, the first group of each domain is its child domain.
1184 static struct sched_group *get_group(int cpu, struct sd_data *sdd)
1186 struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu);
1187 struct sched_domain *child = sd->child;
1188 struct sched_group *sg;
1189 bool already_visited;
1192 cpu = cpumask_first(sched_domain_span(child));
1194 sg = *per_cpu_ptr(sdd->sg, cpu);
1195 sg->sgc = *per_cpu_ptr(sdd->sgc, cpu);
1197 /* Increase refcounts for claim_allocations: */
1198 already_visited = atomic_inc_return(&sg->ref) > 1;
1199 /* sgc visits should follow a similar trend as sg */
1200 WARN_ON(already_visited != (atomic_inc_return(&sg->sgc->ref) > 1));
1202 /* If we have already visited that group, it's already initialized. */
1203 if (already_visited)
1207 cpumask_copy(sched_group_span(sg), sched_domain_span(child));
1208 cpumask_copy(group_balance_mask(sg), sched_group_span(sg));
1209 sg->flags = child->flags;
1211 cpumask_set_cpu(cpu, sched_group_span(sg));
1212 cpumask_set_cpu(cpu, group_balance_mask(sg));
1215 sg->sgc->capacity = SCHED_CAPACITY_SCALE * cpumask_weight(sched_group_span(sg));
1216 sg->sgc->min_capacity = SCHED_CAPACITY_SCALE;
1217 sg->sgc->max_capacity = SCHED_CAPACITY_SCALE;
1223 * build_sched_groups will build a circular linked list of the groups
1224 * covered by the given span, will set each group's ->cpumask correctly,
1225 * and will initialize their ->sgc.
1227 * Assumes the sched_domain tree is fully constructed
1230 build_sched_groups(struct sched_domain *sd, int cpu)
1232 struct sched_group *first = NULL, *last = NULL;
1233 struct sd_data *sdd = sd->private;
1234 const struct cpumask *span = sched_domain_span(sd);
1235 struct cpumask *covered;
1238 lockdep_assert_held(&sched_domains_mutex);
1239 covered = sched_domains_tmpmask;
1241 cpumask_clear(covered);
1243 for_each_cpu_wrap(i, span, cpu) {
1244 struct sched_group *sg;
1246 if (cpumask_test_cpu(i, covered))
1249 sg = get_group(i, sdd);
1251 cpumask_or(covered, covered, sched_group_span(sg));
1266 * Initialize sched groups cpu_capacity.
1268 * cpu_capacity indicates the capacity of sched group, which is used while
1269 * distributing the load between different sched groups in a sched domain.
1270 * Typically cpu_capacity for all the groups in a sched domain will be same
1271 * unless there are asymmetries in the topology. If there are asymmetries,
1272 * group having more cpu_capacity will pickup more load compared to the
1273 * group having less cpu_capacity.
1275 static void init_sched_groups_capacity(int cpu, struct sched_domain *sd)
1277 struct sched_group *sg = sd->groups;
1282 int cpu, max_cpu = -1;
1284 sg->group_weight = cpumask_weight(sched_group_span(sg));
1286 if (!(sd->flags & SD_ASYM_PACKING))
1289 for_each_cpu(cpu, sched_group_span(sg)) {
1292 else if (sched_asym_prefer(cpu, max_cpu))
1295 sg->asym_prefer_cpu = max_cpu;
1299 } while (sg != sd->groups);
1301 if (cpu != group_balance_cpu(sg))
1304 update_group_capacity(sd, cpu);
1308 * Asymmetric CPU capacity bits
1310 struct asym_cap_data {
1311 struct list_head link;
1312 unsigned long capacity;
1313 unsigned long cpus[];
1317 * Set of available CPUs grouped by their corresponding capacities
1318 * Each list entry contains a CPU mask reflecting CPUs that share the same
1320 * The lifespan of data is unlimited.
1322 static LIST_HEAD(asym_cap_list);
1324 #define cpu_capacity_span(asym_data) to_cpumask((asym_data)->cpus)
1327 * Verify whether there is any CPU capacity asymmetry in a given sched domain.
1328 * Provides sd_flags reflecting the asymmetry scope.
1331 asym_cpu_capacity_classify(const struct cpumask *sd_span,
1332 const struct cpumask *cpu_map)
1334 struct asym_cap_data *entry;
1335 int count = 0, miss = 0;
1338 * Count how many unique CPU capacities this domain spans across
1339 * (compare sched_domain CPUs mask with ones representing available
1340 * CPUs capacities). Take into account CPUs that might be offline:
1343 list_for_each_entry(entry, &asym_cap_list, link) {
1344 if (cpumask_intersects(sd_span, cpu_capacity_span(entry)))
1346 else if (cpumask_intersects(cpu_map, cpu_capacity_span(entry)))
1350 WARN_ON_ONCE(!count && !list_empty(&asym_cap_list));
1352 /* No asymmetry detected */
1355 /* Some of the available CPU capacity values have not been detected */
1357 return SD_ASYM_CPUCAPACITY;
1359 /* Full asymmetry */
1360 return SD_ASYM_CPUCAPACITY | SD_ASYM_CPUCAPACITY_FULL;
1364 static inline void asym_cpu_capacity_update_data(int cpu)
1366 unsigned long capacity = arch_scale_cpu_capacity(cpu);
1367 struct asym_cap_data *entry = NULL;
1369 list_for_each_entry(entry, &asym_cap_list, link) {
1370 if (capacity == entry->capacity)
1374 entry = kzalloc(sizeof(*entry) + cpumask_size(), GFP_KERNEL);
1375 if (WARN_ONCE(!entry, "Failed to allocate memory for asymmetry data\n"))
1377 entry->capacity = capacity;
1378 list_add(&entry->link, &asym_cap_list);
1380 __cpumask_set_cpu(cpu, cpu_capacity_span(entry));
1384 * Build-up/update list of CPUs grouped by their capacities
1385 * An update requires explicit request to rebuild sched domains
1386 * with state indicating CPU topology changes.
1388 static void asym_cpu_capacity_scan(void)
1390 struct asym_cap_data *entry, *next;
1393 list_for_each_entry(entry, &asym_cap_list, link)
1394 cpumask_clear(cpu_capacity_span(entry));
1396 for_each_cpu_and(cpu, cpu_possible_mask, housekeeping_cpumask(HK_TYPE_DOMAIN))
1397 asym_cpu_capacity_update_data(cpu);
1399 list_for_each_entry_safe(entry, next, &asym_cap_list, link) {
1400 if (cpumask_empty(cpu_capacity_span(entry))) {
1401 list_del(&entry->link);
1407 * Only one capacity value has been detected i.e. this system is symmetric.
1408 * No need to keep this data around.
1410 if (list_is_singular(&asym_cap_list)) {
1411 entry = list_first_entry(&asym_cap_list, typeof(*entry), link);
1412 list_del(&entry->link);
1418 * Initializers for schedule domains
1419 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
1422 static int default_relax_domain_level = -1;
1423 int sched_domain_level_max;
1425 static int __init setup_relax_domain_level(char *str)
1427 if (kstrtoint(str, 0, &default_relax_domain_level))
1428 pr_warn("Unable to set relax_domain_level\n");
1432 __setup("relax_domain_level=", setup_relax_domain_level);
1434 static void set_domain_attribute(struct sched_domain *sd,
1435 struct sched_domain_attr *attr)
1439 if (!attr || attr->relax_domain_level < 0) {
1440 if (default_relax_domain_level < 0)
1442 request = default_relax_domain_level;
1444 request = attr->relax_domain_level;
1446 if (sd->level > request) {
1447 /* Turn off idle balance on this domain: */
1448 sd->flags &= ~(SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
1452 static void __sdt_free(const struct cpumask *cpu_map);
1453 static int __sdt_alloc(const struct cpumask *cpu_map);
1455 static void __free_domain_allocs(struct s_data *d, enum s_alloc what,
1456 const struct cpumask *cpu_map)
1460 if (!atomic_read(&d->rd->refcount))
1461 free_rootdomain(&d->rd->rcu);
1467 __sdt_free(cpu_map);
1475 __visit_domain_allocation_hell(struct s_data *d, const struct cpumask *cpu_map)
1477 memset(d, 0, sizeof(*d));
1479 if (__sdt_alloc(cpu_map))
1480 return sa_sd_storage;
1481 d->sd = alloc_percpu(struct sched_domain *);
1483 return sa_sd_storage;
1484 d->rd = alloc_rootdomain();
1488 return sa_rootdomain;
1492 * NULL the sd_data elements we've used to build the sched_domain and
1493 * sched_group structure so that the subsequent __free_domain_allocs()
1494 * will not free the data we're using.
1496 static void claim_allocations(int cpu, struct sched_domain *sd)
1498 struct sd_data *sdd = sd->private;
1500 WARN_ON_ONCE(*per_cpu_ptr(sdd->sd, cpu) != sd);
1501 *per_cpu_ptr(sdd->sd, cpu) = NULL;
1503 if (atomic_read(&(*per_cpu_ptr(sdd->sds, cpu))->ref))
1504 *per_cpu_ptr(sdd->sds, cpu) = NULL;
1506 if (atomic_read(&(*per_cpu_ptr(sdd->sg, cpu))->ref))
1507 *per_cpu_ptr(sdd->sg, cpu) = NULL;
1509 if (atomic_read(&(*per_cpu_ptr(sdd->sgc, cpu))->ref))
1510 *per_cpu_ptr(sdd->sgc, cpu) = NULL;
1514 enum numa_topology_type sched_numa_topology_type;
1516 static int sched_domains_numa_levels;
1517 static int sched_domains_curr_level;
1519 int sched_max_numa_distance;
1520 static int *sched_domains_numa_distance;
1521 static struct cpumask ***sched_domains_numa_masks;
1525 * SD_flags allowed in topology descriptions.
1527 * These flags are purely descriptive of the topology and do not prescribe
1528 * behaviour. Behaviour is artificial and mapped in the below sd_init()
1531 * SD_SHARE_CPUCAPACITY - describes SMT topologies
1532 * SD_SHARE_PKG_RESOURCES - describes shared caches
1533 * SD_NUMA - describes NUMA topologies
1535 * Odd one out, which beside describing the topology has a quirk also
1536 * prescribes the desired behaviour that goes along with it:
1538 * SD_ASYM_PACKING - describes SMT quirks
1540 #define TOPOLOGY_SD_FLAGS \
1541 (SD_SHARE_CPUCAPACITY | \
1542 SD_SHARE_PKG_RESOURCES | \
1546 static struct sched_domain *
1547 sd_init(struct sched_domain_topology_level *tl,
1548 const struct cpumask *cpu_map,
1549 struct sched_domain *child, int cpu)
1551 struct sd_data *sdd = &tl->data;
1552 struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu);
1553 int sd_id, sd_weight, sd_flags = 0;
1554 struct cpumask *sd_span;
1558 * Ugly hack to pass state to sd_numa_mask()...
1560 sched_domains_curr_level = tl->numa_level;
1563 sd_weight = cpumask_weight(tl->mask(cpu));
1566 sd_flags = (*tl->sd_flags)();
1567 if (WARN_ONCE(sd_flags & ~TOPOLOGY_SD_FLAGS,
1568 "wrong sd_flags in topology description\n"))
1569 sd_flags &= TOPOLOGY_SD_FLAGS;
1571 *sd = (struct sched_domain){
1572 .min_interval = sd_weight,
1573 .max_interval = 2*sd_weight,
1575 .imbalance_pct = 117,
1577 .cache_nice_tries = 0,
1579 .flags = 1*SD_BALANCE_NEWIDLE
1584 | 0*SD_SHARE_CPUCAPACITY
1585 | 0*SD_SHARE_PKG_RESOURCES
1587 | 1*SD_PREFER_SIBLING
1592 .last_balance = jiffies,
1593 .balance_interval = sd_weight,
1594 .max_newidle_lb_cost = 0,
1595 .last_decay_max_lb_cost = jiffies,
1597 #ifdef CONFIG_SCHED_DEBUG
1602 sd_span = sched_domain_span(sd);
1603 cpumask_and(sd_span, cpu_map, tl->mask(cpu));
1604 sd_id = cpumask_first(sd_span);
1606 sd->flags |= asym_cpu_capacity_classify(sd_span, cpu_map);
1608 WARN_ONCE((sd->flags & (SD_SHARE_CPUCAPACITY | SD_ASYM_CPUCAPACITY)) ==
1609 (SD_SHARE_CPUCAPACITY | SD_ASYM_CPUCAPACITY),
1610 "CPU capacity asymmetry not supported on SMT\n");
1613 * Convert topological properties into behaviour.
1615 /* Don't attempt to spread across CPUs of different capacities. */
1616 if ((sd->flags & SD_ASYM_CPUCAPACITY) && sd->child)
1617 sd->child->flags &= ~SD_PREFER_SIBLING;
1619 if (sd->flags & SD_SHARE_CPUCAPACITY) {
1620 sd->imbalance_pct = 110;
1622 } else if (sd->flags & SD_SHARE_PKG_RESOURCES) {
1623 sd->imbalance_pct = 117;
1624 sd->cache_nice_tries = 1;
1627 } else if (sd->flags & SD_NUMA) {
1628 sd->cache_nice_tries = 2;
1630 sd->flags &= ~SD_PREFER_SIBLING;
1631 sd->flags |= SD_SERIALIZE;
1632 if (sched_domains_numa_distance[tl->numa_level] > node_reclaim_distance) {
1633 sd->flags &= ~(SD_BALANCE_EXEC |
1640 sd->cache_nice_tries = 1;
1644 * For all levels sharing cache; connect a sched_domain_shared
1647 if (sd->flags & SD_SHARE_PKG_RESOURCES) {
1648 sd->shared = *per_cpu_ptr(sdd->sds, sd_id);
1649 atomic_inc(&sd->shared->ref);
1650 atomic_set(&sd->shared->nr_busy_cpus, sd_weight);
1659 * Topology list, bottom-up.
1661 static struct sched_domain_topology_level default_topology[] = {
1662 #ifdef CONFIG_SCHED_SMT
1663 { cpu_smt_mask, cpu_smt_flags, SD_INIT_NAME(SMT) },
1666 #ifdef CONFIG_SCHED_CLUSTER
1667 { cpu_clustergroup_mask, cpu_cluster_flags, SD_INIT_NAME(CLS) },
1670 #ifdef CONFIG_SCHED_MC
1671 { cpu_coregroup_mask, cpu_core_flags, SD_INIT_NAME(MC) },
1673 { cpu_cpu_mask, SD_INIT_NAME(DIE) },
1677 static struct sched_domain_topology_level *sched_domain_topology =
1679 static struct sched_domain_topology_level *sched_domain_topology_saved;
1681 #define for_each_sd_topology(tl) \
1682 for (tl = sched_domain_topology; tl->mask; tl++)
1684 void __init set_sched_topology(struct sched_domain_topology_level *tl)
1686 if (WARN_ON_ONCE(sched_smp_initialized))
1689 sched_domain_topology = tl;
1690 sched_domain_topology_saved = NULL;
1695 static const struct cpumask *sd_numa_mask(int cpu)
1697 return sched_domains_numa_masks[sched_domains_curr_level][cpu_to_node(cpu)];
1700 static void sched_numa_warn(const char *str)
1702 static int done = false;
1710 printk(KERN_WARNING "ERROR: %s\n\n", str);
1712 for (i = 0; i < nr_node_ids; i++) {
1713 printk(KERN_WARNING " ");
1714 for (j = 0; j < nr_node_ids; j++) {
1715 if (!node_state(i, N_CPU) || !node_state(j, N_CPU))
1716 printk(KERN_CONT "(%02d) ", node_distance(i,j));
1718 printk(KERN_CONT " %02d ", node_distance(i,j));
1720 printk(KERN_CONT "\n");
1722 printk(KERN_WARNING "\n");
1725 bool find_numa_distance(int distance)
1730 if (distance == node_distance(0, 0))
1734 distances = rcu_dereference(sched_domains_numa_distance);
1737 for (i = 0; i < sched_domains_numa_levels; i++) {
1738 if (distances[i] == distance) {
1749 #define for_each_cpu_node_but(n, nbut) \
1750 for_each_node_state(n, N_CPU) \
1756 * A system can have three types of NUMA topology:
1757 * NUMA_DIRECT: all nodes are directly connected, or not a NUMA system
1758 * NUMA_GLUELESS_MESH: some nodes reachable through intermediary nodes
1759 * NUMA_BACKPLANE: nodes can reach other nodes through a backplane
1761 * The difference between a glueless mesh topology and a backplane
1762 * topology lies in whether communication between not directly
1763 * connected nodes goes through intermediary nodes (where programs
1764 * could run), or through backplane controllers. This affects
1765 * placement of programs.
1767 * The type of topology can be discerned with the following tests:
1768 * - If the maximum distance between any nodes is 1 hop, the system
1769 * is directly connected.
1770 * - If for two nodes A and B, located N > 1 hops away from each other,
1771 * there is an intermediary node C, which is < N hops away from both
1772 * nodes A and B, the system is a glueless mesh.
1774 static void init_numa_topology_type(int offline_node)
1778 n = sched_max_numa_distance;
1780 if (sched_domains_numa_levels <= 2) {
1781 sched_numa_topology_type = NUMA_DIRECT;
1785 for_each_cpu_node_but(a, offline_node) {
1786 for_each_cpu_node_but(b, offline_node) {
1787 /* Find two nodes furthest removed from each other. */
1788 if (node_distance(a, b) < n)
1791 /* Is there an intermediary node between a and b? */
1792 for_each_cpu_node_but(c, offline_node) {
1793 if (node_distance(a, c) < n &&
1794 node_distance(b, c) < n) {
1795 sched_numa_topology_type =
1801 sched_numa_topology_type = NUMA_BACKPLANE;
1806 pr_err("Failed to find a NUMA topology type, defaulting to DIRECT\n");
1807 sched_numa_topology_type = NUMA_DIRECT;
1811 #define NR_DISTANCE_VALUES (1 << DISTANCE_BITS)
1813 void sched_init_numa(int offline_node)
1815 struct sched_domain_topology_level *tl;
1816 unsigned long *distance_map;
1820 struct cpumask ***masks;
1823 * O(nr_nodes^2) deduplicating selection sort -- in order to find the
1824 * unique distances in the node_distance() table.
1826 distance_map = bitmap_alloc(NR_DISTANCE_VALUES, GFP_KERNEL);
1830 bitmap_zero(distance_map, NR_DISTANCE_VALUES);
1831 for_each_cpu_node_but(i, offline_node) {
1832 for_each_cpu_node_but(j, offline_node) {
1833 int distance = node_distance(i, j);
1835 if (distance < LOCAL_DISTANCE || distance >= NR_DISTANCE_VALUES) {
1836 sched_numa_warn("Invalid distance value range");
1837 bitmap_free(distance_map);
1841 bitmap_set(distance_map, distance, 1);
1845 * We can now figure out how many unique distance values there are and
1846 * allocate memory accordingly.
1848 nr_levels = bitmap_weight(distance_map, NR_DISTANCE_VALUES);
1850 distances = kcalloc(nr_levels, sizeof(int), GFP_KERNEL);
1852 bitmap_free(distance_map);
1856 for (i = 0, j = 0; i < nr_levels; i++, j++) {
1857 j = find_next_bit(distance_map, NR_DISTANCE_VALUES, j);
1860 rcu_assign_pointer(sched_domains_numa_distance, distances);
1862 bitmap_free(distance_map);
1865 * 'nr_levels' contains the number of unique distances
1867 * The sched_domains_numa_distance[] array includes the actual distance
1872 * Here, we should temporarily reset sched_domains_numa_levels to 0.
1873 * If it fails to allocate memory for array sched_domains_numa_masks[][],
1874 * the array will contain less then 'nr_levels' members. This could be
1875 * dangerous when we use it to iterate array sched_domains_numa_masks[][]
1876 * in other functions.
1878 * We reset it to 'nr_levels' at the end of this function.
1880 sched_domains_numa_levels = 0;
1882 masks = kzalloc(sizeof(void *) * nr_levels, GFP_KERNEL);
1887 * Now for each level, construct a mask per node which contains all
1888 * CPUs of nodes that are that many hops away from us.
1890 for (i = 0; i < nr_levels; i++) {
1891 masks[i] = kzalloc(nr_node_ids * sizeof(void *), GFP_KERNEL);
1895 for_each_cpu_node_but(j, offline_node) {
1896 struct cpumask *mask = kzalloc(cpumask_size(), GFP_KERNEL);
1904 for_each_cpu_node_but(k, offline_node) {
1905 if (sched_debug() && (node_distance(j, k) != node_distance(k, j)))
1906 sched_numa_warn("Node-distance not symmetric");
1908 if (node_distance(j, k) > sched_domains_numa_distance[i])
1911 cpumask_or(mask, mask, cpumask_of_node(k));
1915 rcu_assign_pointer(sched_domains_numa_masks, masks);
1917 /* Compute default topology size */
1918 for (i = 0; sched_domain_topology[i].mask; i++);
1920 tl = kzalloc((i + nr_levels + 1) *
1921 sizeof(struct sched_domain_topology_level), GFP_KERNEL);
1926 * Copy the default topology bits..
1928 for (i = 0; sched_domain_topology[i].mask; i++)
1929 tl[i] = sched_domain_topology[i];
1932 * Add the NUMA identity distance, aka single NODE.
1934 tl[i++] = (struct sched_domain_topology_level){
1935 .mask = sd_numa_mask,
1941 * .. and append 'j' levels of NUMA goodness.
1943 for (j = 1; j < nr_levels; i++, j++) {
1944 tl[i] = (struct sched_domain_topology_level){
1945 .mask = sd_numa_mask,
1946 .sd_flags = cpu_numa_flags,
1947 .flags = SDTL_OVERLAP,
1953 sched_domain_topology_saved = sched_domain_topology;
1954 sched_domain_topology = tl;
1956 sched_domains_numa_levels = nr_levels;
1957 WRITE_ONCE(sched_max_numa_distance, sched_domains_numa_distance[nr_levels - 1]);
1959 init_numa_topology_type(offline_node);
1963 static void sched_reset_numa(void)
1965 int nr_levels, *distances;
1966 struct cpumask ***masks;
1968 nr_levels = sched_domains_numa_levels;
1969 sched_domains_numa_levels = 0;
1970 sched_max_numa_distance = 0;
1971 sched_numa_topology_type = NUMA_DIRECT;
1972 distances = sched_domains_numa_distance;
1973 rcu_assign_pointer(sched_domains_numa_distance, NULL);
1974 masks = sched_domains_numa_masks;
1975 rcu_assign_pointer(sched_domains_numa_masks, NULL);
1976 if (distances || masks) {
1981 for (i = 0; i < nr_levels && masks; i++) {
1990 if (sched_domain_topology_saved) {
1991 kfree(sched_domain_topology);
1992 sched_domain_topology = sched_domain_topology_saved;
1993 sched_domain_topology_saved = NULL;
1998 * Call with hotplug lock held
2000 void sched_update_numa(int cpu, bool online)
2004 node = cpu_to_node(cpu);
2006 * Scheduler NUMA topology is updated when the first CPU of a
2007 * node is onlined or the last CPU of a node is offlined.
2009 if (cpumask_weight(cpumask_of_node(node)) != 1)
2013 sched_init_numa(online ? NUMA_NO_NODE : node);
2016 void sched_domains_numa_masks_set(unsigned int cpu)
2018 int node = cpu_to_node(cpu);
2021 for (i = 0; i < sched_domains_numa_levels; i++) {
2022 for (j = 0; j < nr_node_ids; j++) {
2023 if (!node_state(j, N_CPU))
2026 /* Set ourselves in the remote node's masks */
2027 if (node_distance(j, node) <= sched_domains_numa_distance[i])
2028 cpumask_set_cpu(cpu, sched_domains_numa_masks[i][j]);
2033 void sched_domains_numa_masks_clear(unsigned int cpu)
2037 for (i = 0; i < sched_domains_numa_levels; i++) {
2038 for (j = 0; j < nr_node_ids; j++) {
2039 if (sched_domains_numa_masks[i][j])
2040 cpumask_clear_cpu(cpu, sched_domains_numa_masks[i][j]);
2046 * sched_numa_find_closest() - given the NUMA topology, find the cpu
2047 * closest to @cpu from @cpumask.
2048 * cpumask: cpumask to find a cpu from
2049 * cpu: cpu to be close to
2051 * returns: cpu, or nr_cpu_ids when nothing found.
2053 int sched_numa_find_closest(const struct cpumask *cpus, int cpu)
2055 int i, j = cpu_to_node(cpu), found = nr_cpu_ids;
2056 struct cpumask ***masks;
2059 masks = rcu_dereference(sched_domains_numa_masks);
2062 for (i = 0; i < sched_domains_numa_levels; i++) {
2065 cpu = cpumask_any_and(cpus, masks[i][j]);
2066 if (cpu < nr_cpu_ids) {
2078 const struct cpumask *cpus;
2079 struct cpumask ***masks;
2085 static int hop_cmp(const void *a, const void *b)
2087 struct cpumask **prev_hop, **cur_hop = *(struct cpumask ***)b;
2088 struct __cmp_key *k = (struct __cmp_key *)a;
2090 if (cpumask_weight_and(k->cpus, cur_hop[k->node]) <= k->cpu)
2093 if (b == k->masks) {
2098 prev_hop = *((struct cpumask ***)b - 1);
2099 k->w = cpumask_weight_and(k->cpus, prev_hop[k->node]);
2107 * sched_numa_find_nth_cpu() - given the NUMA topology, find the Nth next cpu
2108 * closest to @cpu from @cpumask.
2109 * cpumask: cpumask to find a cpu from
2110 * cpu: Nth cpu to find
2112 * returns: cpu, or nr_cpu_ids when nothing found.
2114 int sched_numa_find_nth_cpu(const struct cpumask *cpus, int cpu, int node)
2116 struct __cmp_key k = { .cpus = cpus, .node = node, .cpu = cpu };
2117 struct cpumask ***hop_masks;
2118 int hop, ret = nr_cpu_ids;
2122 k.masks = rcu_dereference(sched_domains_numa_masks);
2126 hop_masks = bsearch(&k, k.masks, sched_domains_numa_levels, sizeof(k.masks[0]), hop_cmp);
2127 hop = hop_masks - k.masks;
2130 cpumask_nth_and_andnot(cpu - k.w, cpus, k.masks[hop][node], k.masks[hop-1][node]) :
2131 cpumask_nth_and(cpu, cpus, k.masks[0][node]);
2136 EXPORT_SYMBOL_GPL(sched_numa_find_nth_cpu);
2139 * sched_numa_hop_mask() - Get the cpumask of CPUs at most @hops hops away from
2141 * @node: The node to count hops from.
2142 * @hops: Include CPUs up to that many hops away. 0 means local node.
2144 * Return: On success, a pointer to a cpumask of CPUs at most @hops away from
2145 * @node, an error value otherwise.
2147 * Requires rcu_lock to be held. Returned cpumask is only valid within that
2148 * read-side section, copy it if required beyond that.
2150 * Note that not all hops are equal in distance; see sched_init_numa() for how
2151 * distances and masks are handled.
2152 * Also note that this is a reflection of sched_domains_numa_masks, which may change
2153 * during the lifetime of the system (offline nodes are taken out of the masks).
2155 const struct cpumask *sched_numa_hop_mask(unsigned int node, unsigned int hops)
2157 struct cpumask ***masks;
2159 if (node >= nr_node_ids || hops >= sched_domains_numa_levels)
2160 return ERR_PTR(-EINVAL);
2162 masks = rcu_dereference(sched_domains_numa_masks);
2164 return ERR_PTR(-EBUSY);
2166 return masks[hops][node];
2168 EXPORT_SYMBOL_GPL(sched_numa_hop_mask);
2170 #endif /* CONFIG_NUMA */
2172 static int __sdt_alloc(const struct cpumask *cpu_map)
2174 struct sched_domain_topology_level *tl;
2177 for_each_sd_topology(tl) {
2178 struct sd_data *sdd = &tl->data;
2180 sdd->sd = alloc_percpu(struct sched_domain *);
2184 sdd->sds = alloc_percpu(struct sched_domain_shared *);
2188 sdd->sg = alloc_percpu(struct sched_group *);
2192 sdd->sgc = alloc_percpu(struct sched_group_capacity *);
2196 for_each_cpu(j, cpu_map) {
2197 struct sched_domain *sd;
2198 struct sched_domain_shared *sds;
2199 struct sched_group *sg;
2200 struct sched_group_capacity *sgc;
2202 sd = kzalloc_node(sizeof(struct sched_domain) + cpumask_size(),
2203 GFP_KERNEL, cpu_to_node(j));
2207 *per_cpu_ptr(sdd->sd, j) = sd;
2209 sds = kzalloc_node(sizeof(struct sched_domain_shared),
2210 GFP_KERNEL, cpu_to_node(j));
2214 *per_cpu_ptr(sdd->sds, j) = sds;
2216 sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
2217 GFP_KERNEL, cpu_to_node(j));
2223 *per_cpu_ptr(sdd->sg, j) = sg;
2225 sgc = kzalloc_node(sizeof(struct sched_group_capacity) + cpumask_size(),
2226 GFP_KERNEL, cpu_to_node(j));
2230 #ifdef CONFIG_SCHED_DEBUG
2234 *per_cpu_ptr(sdd->sgc, j) = sgc;
2241 static void __sdt_free(const struct cpumask *cpu_map)
2243 struct sched_domain_topology_level *tl;
2246 for_each_sd_topology(tl) {
2247 struct sd_data *sdd = &tl->data;
2249 for_each_cpu(j, cpu_map) {
2250 struct sched_domain *sd;
2253 sd = *per_cpu_ptr(sdd->sd, j);
2254 if (sd && (sd->flags & SD_OVERLAP))
2255 free_sched_groups(sd->groups, 0);
2256 kfree(*per_cpu_ptr(sdd->sd, j));
2260 kfree(*per_cpu_ptr(sdd->sds, j));
2262 kfree(*per_cpu_ptr(sdd->sg, j));
2264 kfree(*per_cpu_ptr(sdd->sgc, j));
2266 free_percpu(sdd->sd);
2268 free_percpu(sdd->sds);
2270 free_percpu(sdd->sg);
2272 free_percpu(sdd->sgc);
2277 static struct sched_domain *build_sched_domain(struct sched_domain_topology_level *tl,
2278 const struct cpumask *cpu_map, struct sched_domain_attr *attr,
2279 struct sched_domain *child, int cpu)
2281 struct sched_domain *sd = sd_init(tl, cpu_map, child, cpu);
2284 sd->level = child->level + 1;
2285 sched_domain_level_max = max(sched_domain_level_max, sd->level);
2288 if (!cpumask_subset(sched_domain_span(child),
2289 sched_domain_span(sd))) {
2290 pr_err("BUG: arch topology borken\n");
2291 #ifdef CONFIG_SCHED_DEBUG
2292 pr_err(" the %s domain not a subset of the %s domain\n",
2293 child->name, sd->name);
2295 /* Fixup, ensure @sd has at least @child CPUs. */
2296 cpumask_or(sched_domain_span(sd),
2297 sched_domain_span(sd),
2298 sched_domain_span(child));
2302 set_domain_attribute(sd, attr);
2308 * Ensure topology masks are sane, i.e. there are no conflicts (overlaps) for
2309 * any two given CPUs at this (non-NUMA) topology level.
2311 static bool topology_span_sane(struct sched_domain_topology_level *tl,
2312 const struct cpumask *cpu_map, int cpu)
2316 /* NUMA levels are allowed to overlap */
2317 if (tl->flags & SDTL_OVERLAP)
2321 * Non-NUMA levels cannot partially overlap - they must be either
2322 * completely equal or completely disjoint. Otherwise we can end up
2323 * breaking the sched_group lists - i.e. a later get_group() pass
2324 * breaks the linking done for an earlier span.
2326 for_each_cpu(i, cpu_map) {
2330 * We should 'and' all those masks with 'cpu_map' to exactly
2331 * match the topology we're about to build, but that can only
2332 * remove CPUs, which only lessens our ability to detect
2335 if (!cpumask_equal(tl->mask(cpu), tl->mask(i)) &&
2336 cpumask_intersects(tl->mask(cpu), tl->mask(i)))
2344 * Build sched domains for a given set of CPUs and attach the sched domains
2345 * to the individual CPUs
2348 build_sched_domains(const struct cpumask *cpu_map, struct sched_domain_attr *attr)
2350 enum s_alloc alloc_state = sa_none;
2351 struct sched_domain *sd;
2353 struct rq *rq = NULL;
2354 int i, ret = -ENOMEM;
2355 bool has_asym = false;
2357 if (WARN_ON(cpumask_empty(cpu_map)))
2360 alloc_state = __visit_domain_allocation_hell(&d, cpu_map);
2361 if (alloc_state != sa_rootdomain)
2364 /* Set up domains for CPUs specified by the cpu_map: */
2365 for_each_cpu(i, cpu_map) {
2366 struct sched_domain_topology_level *tl;
2369 for_each_sd_topology(tl) {
2371 if (WARN_ON(!topology_span_sane(tl, cpu_map, i)))
2374 sd = build_sched_domain(tl, cpu_map, attr, sd, i);
2376 has_asym |= sd->flags & SD_ASYM_CPUCAPACITY;
2378 if (tl == sched_domain_topology)
2379 *per_cpu_ptr(d.sd, i) = sd;
2380 if (tl->flags & SDTL_OVERLAP)
2381 sd->flags |= SD_OVERLAP;
2382 if (cpumask_equal(cpu_map, sched_domain_span(sd)))
2387 /* Build the groups for the domains */
2388 for_each_cpu(i, cpu_map) {
2389 for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
2390 sd->span_weight = cpumask_weight(sched_domain_span(sd));
2391 if (sd->flags & SD_OVERLAP) {
2392 if (build_overlap_sched_groups(sd, i))
2395 if (build_sched_groups(sd, i))
2402 * Calculate an allowed NUMA imbalance such that LLCs do not get
2405 for_each_cpu(i, cpu_map) {
2406 unsigned int imb = 0;
2407 unsigned int imb_span = 1;
2409 for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
2410 struct sched_domain *child = sd->child;
2412 if (!(sd->flags & SD_SHARE_PKG_RESOURCES) && child &&
2413 (child->flags & SD_SHARE_PKG_RESOURCES)) {
2414 struct sched_domain __rcu *top_p;
2415 unsigned int nr_llcs;
2418 * For a single LLC per node, allow an
2419 * imbalance up to 12.5% of the node. This is
2420 * arbitrary cutoff based two factors -- SMT and
2421 * memory channels. For SMT-2, the intent is to
2422 * avoid premature sharing of HT resources but
2423 * SMT-4 or SMT-8 *may* benefit from a different
2424 * cutoff. For memory channels, this is a very
2425 * rough estimate of how many channels may be
2426 * active and is based on recent CPUs with
2429 * For multiple LLCs, allow an imbalance
2430 * until multiple tasks would share an LLC
2431 * on one node while LLCs on another node
2432 * remain idle. This assumes that there are
2433 * enough logical CPUs per LLC to avoid SMT
2434 * factors and that there is a correlation
2435 * between LLCs and memory channels.
2437 nr_llcs = sd->span_weight / child->span_weight;
2439 imb = sd->span_weight >> 3;
2443 sd->imb_numa_nr = imb;
2445 /* Set span based on the first NUMA domain. */
2447 while (top_p && !(top_p->flags & SD_NUMA)) {
2448 top_p = top_p->parent;
2450 imb_span = top_p ? top_p->span_weight : sd->span_weight;
2452 int factor = max(1U, (sd->span_weight / imb_span));
2454 sd->imb_numa_nr = imb * factor;
2459 /* Calculate CPU capacity for physical packages and nodes */
2460 for (i = nr_cpumask_bits-1; i >= 0; i--) {
2461 if (!cpumask_test_cpu(i, cpu_map))
2464 for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
2465 claim_allocations(i, sd);
2466 init_sched_groups_capacity(i, sd);
2470 /* Attach the domains */
2472 for_each_cpu(i, cpu_map) {
2474 sd = *per_cpu_ptr(d.sd, i);
2476 /* Use READ_ONCE()/WRITE_ONCE() to avoid load/store tearing: */
2477 if (rq->cpu_capacity_orig > READ_ONCE(d.rd->max_cpu_capacity))
2478 WRITE_ONCE(d.rd->max_cpu_capacity, rq->cpu_capacity_orig);
2480 cpu_attach_domain(sd, d.rd, i);
2485 static_branch_inc_cpuslocked(&sched_asym_cpucapacity);
2487 if (rq && sched_debug_verbose) {
2488 pr_info("root domain span: %*pbl (max cpu_capacity = %lu)\n",
2489 cpumask_pr_args(cpu_map), rq->rd->max_cpu_capacity);
2494 __free_domain_allocs(&d, alloc_state, cpu_map);
2499 /* Current sched domains: */
2500 static cpumask_var_t *doms_cur;
2502 /* Number of sched domains in 'doms_cur': */
2503 static int ndoms_cur;
2505 /* Attributes of custom domains in 'doms_cur' */
2506 static struct sched_domain_attr *dattr_cur;
2509 * Special case: If a kmalloc() of a doms_cur partition (array of
2510 * cpumask) fails, then fallback to a single sched domain,
2511 * as determined by the single cpumask fallback_doms.
2513 static cpumask_var_t fallback_doms;
2516 * arch_update_cpu_topology lets virtualized architectures update the
2517 * CPU core maps. It is supposed to return 1 if the topology changed
2518 * or 0 if it stayed the same.
2520 int __weak arch_update_cpu_topology(void)
2525 cpumask_var_t *alloc_sched_domains(unsigned int ndoms)
2528 cpumask_var_t *doms;
2530 doms = kmalloc_array(ndoms, sizeof(*doms), GFP_KERNEL);
2533 for (i = 0; i < ndoms; i++) {
2534 if (!alloc_cpumask_var(&doms[i], GFP_KERNEL)) {
2535 free_sched_domains(doms, i);
2542 void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms)
2545 for (i = 0; i < ndoms; i++)
2546 free_cpumask_var(doms[i]);
2551 * Set up scheduler domains and groups. For now this just excludes isolated
2552 * CPUs, but could be used to exclude other special cases in the future.
2554 int __init sched_init_domains(const struct cpumask *cpu_map)
2558 zalloc_cpumask_var(&sched_domains_tmpmask, GFP_KERNEL);
2559 zalloc_cpumask_var(&sched_domains_tmpmask2, GFP_KERNEL);
2560 zalloc_cpumask_var(&fallback_doms, GFP_KERNEL);
2562 arch_update_cpu_topology();
2563 asym_cpu_capacity_scan();
2565 doms_cur = alloc_sched_domains(ndoms_cur);
2567 doms_cur = &fallback_doms;
2568 cpumask_and(doms_cur[0], cpu_map, housekeeping_cpumask(HK_TYPE_DOMAIN));
2569 err = build_sched_domains(doms_cur[0], NULL);
2575 * Detach sched domains from a group of CPUs specified in cpu_map
2576 * These CPUs will now be attached to the NULL domain
2578 static void detach_destroy_domains(const struct cpumask *cpu_map)
2580 unsigned int cpu = cpumask_any(cpu_map);
2583 if (rcu_access_pointer(per_cpu(sd_asym_cpucapacity, cpu)))
2584 static_branch_dec_cpuslocked(&sched_asym_cpucapacity);
2587 for_each_cpu(i, cpu_map)
2588 cpu_attach_domain(NULL, &def_root_domain, i);
2592 /* handle null as "default" */
2593 static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
2594 struct sched_domain_attr *new, int idx_new)
2596 struct sched_domain_attr tmp;
2604 return !memcmp(cur ? (cur + idx_cur) : &tmp,
2605 new ? (new + idx_new) : &tmp,
2606 sizeof(struct sched_domain_attr));
2610 * Partition sched domains as specified by the 'ndoms_new'
2611 * cpumasks in the array doms_new[] of cpumasks. This compares
2612 * doms_new[] to the current sched domain partitioning, doms_cur[].
2613 * It destroys each deleted domain and builds each new domain.
2615 * 'doms_new' is an array of cpumask_var_t's of length 'ndoms_new'.
2616 * The masks don't intersect (don't overlap.) We should setup one
2617 * sched domain for each mask. CPUs not in any of the cpumasks will
2618 * not be load balanced. If the same cpumask appears both in the
2619 * current 'doms_cur' domains and in the new 'doms_new', we can leave
2622 * The passed in 'doms_new' should be allocated using
2623 * alloc_sched_domains. This routine takes ownership of it and will
2624 * free_sched_domains it when done with it. If the caller failed the
2625 * alloc call, then it can pass in doms_new == NULL && ndoms_new == 1,
2626 * and partition_sched_domains() will fallback to the single partition
2627 * 'fallback_doms', it also forces the domains to be rebuilt.
2629 * If doms_new == NULL it will be replaced with cpu_online_mask.
2630 * ndoms_new == 0 is a special case for destroying existing domains,
2631 * and it will not create the default domain.
2633 * Call with hotplug lock and sched_domains_mutex held
2635 void partition_sched_domains_locked(int ndoms_new, cpumask_var_t doms_new[],
2636 struct sched_domain_attr *dattr_new)
2638 bool __maybe_unused has_eas = false;
2642 lockdep_assert_held(&sched_domains_mutex);
2644 /* Let the architecture update CPU core mappings: */
2645 new_topology = arch_update_cpu_topology();
2646 /* Trigger rebuilding CPU capacity asymmetry data */
2648 asym_cpu_capacity_scan();
2651 WARN_ON_ONCE(dattr_new);
2653 doms_new = alloc_sched_domains(1);
2656 cpumask_and(doms_new[0], cpu_active_mask,
2657 housekeeping_cpumask(HK_TYPE_DOMAIN));
2663 /* Destroy deleted domains: */
2664 for (i = 0; i < ndoms_cur; i++) {
2665 for (j = 0; j < n && !new_topology; j++) {
2666 if (cpumask_equal(doms_cur[i], doms_new[j]) &&
2667 dattrs_equal(dattr_cur, i, dattr_new, j)) {
2668 struct root_domain *rd;
2671 * This domain won't be destroyed and as such
2672 * its dl_bw->total_bw needs to be cleared. It
2673 * will be recomputed in function
2674 * update_tasks_root_domain().
2676 rd = cpu_rq(cpumask_any(doms_cur[i]))->rd;
2677 dl_clear_root_domain(rd);
2681 /* No match - a current sched domain not in new doms_new[] */
2682 detach_destroy_domains(doms_cur[i]);
2690 doms_new = &fallback_doms;
2691 cpumask_and(doms_new[0], cpu_active_mask,
2692 housekeeping_cpumask(HK_TYPE_DOMAIN));
2695 /* Build new domains: */
2696 for (i = 0; i < ndoms_new; i++) {
2697 for (j = 0; j < n && !new_topology; j++) {
2698 if (cpumask_equal(doms_new[i], doms_cur[j]) &&
2699 dattrs_equal(dattr_new, i, dattr_cur, j))
2702 /* No match - add a new doms_new */
2703 build_sched_domains(doms_new[i], dattr_new ? dattr_new + i : NULL);
2708 #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
2709 /* Build perf. domains: */
2710 for (i = 0; i < ndoms_new; i++) {
2711 for (j = 0; j < n && !sched_energy_update; j++) {
2712 if (cpumask_equal(doms_new[i], doms_cur[j]) &&
2713 cpu_rq(cpumask_first(doms_cur[j]))->rd->pd) {
2718 /* No match - add perf. domains for a new rd */
2719 has_eas |= build_perf_domains(doms_new[i]);
2723 sched_energy_set(has_eas);
2726 /* Remember the new sched domains: */
2727 if (doms_cur != &fallback_doms)
2728 free_sched_domains(doms_cur, ndoms_cur);
2731 doms_cur = doms_new;
2732 dattr_cur = dattr_new;
2733 ndoms_cur = ndoms_new;
2735 update_sched_domain_debugfs();
2739 * Call with hotplug lock held
2741 void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
2742 struct sched_domain_attr *dattr_new)
2744 mutex_lock(&sched_domains_mutex);
2745 partition_sched_domains_locked(ndoms_new, doms_new, dattr_new);
2746 mutex_unlock(&sched_domains_mutex);