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