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