blob: 921dedde2ee1d613b3e2c3d38b0ff5a8e5ddd7c8 [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{
560 struct sched_group *first = NULL, *last = NULL, *groups = NULL, *sg;
561 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
590 /*
591 * Make sure the first group of this domain contains the
592 * canonical balance CPU. Otherwise the sched_domain iteration
593 * breaks. See update_sg_lb_stats().
594 */
595 if ((!groups && cpumask_test_cpu(cpu, sg_span)) ||
596 group_balance_cpu(sg) == cpu)
597 groups = sg;
598
599 if (!first)
600 first = sg;
601 if (last)
602 last->next = sg;
603 last = sg;
604 last->next = first;
605 }
606 sd->groups = groups;
607
608 return 0;
609
610fail:
611 free_sched_groups(first, 0);
612
613 return -ENOMEM;
614}
615
616static int get_group(int cpu, struct sd_data *sdd, struct sched_group **sg)
617{
618 struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu);
619 struct sched_domain *child = sd->child;
620
621 if (child)
622 cpu = cpumask_first(sched_domain_span(child));
623
624 if (sg) {
625 *sg = *per_cpu_ptr(sdd->sg, cpu);
626 (*sg)->sgc = *per_cpu_ptr(sdd->sgc, cpu);
627
628 /* For claim_allocations: */
629 atomic_set(&(*sg)->sgc->ref, 1);
630 }
631
632 return cpu;
633}
634
635/*
636 * build_sched_groups will build a circular linked list of the groups
637 * covered by the given span, and will set each group's ->cpumask correctly,
638 * and ->cpu_capacity to 0.
639 *
640 * Assumes the sched_domain tree is fully constructed
641 */
642static int
643build_sched_groups(struct sched_domain *sd, int cpu)
644{
645 struct sched_group *first = NULL, *last = NULL;
646 struct sd_data *sdd = sd->private;
647 const struct cpumask *span = sched_domain_span(sd);
648 struct cpumask *covered;
649 int i;
650
651 get_group(cpu, sdd, &sd->groups);
652 atomic_inc(&sd->groups->ref);
653
654 if (cpu != cpumask_first(span))
655 return 0;
656
657 lockdep_assert_held(&sched_domains_mutex);
658 covered = sched_domains_tmpmask;
659
660 cpumask_clear(covered);
661
662 for_each_cpu(i, span) {
663 struct sched_group *sg;
664 int group, j;
665
666 if (cpumask_test_cpu(i, covered))
667 continue;
668
669 group = get_group(i, sdd, &sg);
670 cpumask_setall(sched_group_mask(sg));
671
672 for_each_cpu(j, span) {
673 if (get_group(j, sdd, NULL) != group)
674 continue;
675
676 cpumask_set_cpu(j, covered);
677 cpumask_set_cpu(j, sched_group_cpus(sg));
678 }
679
680 if (!first)
681 first = sg;
682 if (last)
683 last->next = sg;
684 last = sg;
685 }
686 last->next = first;
687
688 return 0;
689}
690
691/*
692 * Initialize sched groups cpu_capacity.
693 *
694 * cpu_capacity indicates the capacity of sched group, which is used while
695 * distributing the load between different sched groups in a sched domain.
696 * Typically cpu_capacity for all the groups in a sched domain will be same
697 * unless there are asymmetries in the topology. If there are asymmetries,
698 * group having more cpu_capacity will pickup more load compared to the
699 * group having less cpu_capacity.
700 */
701static void init_sched_groups_capacity(int cpu, struct sched_domain *sd)
702{
703 struct sched_group *sg = sd->groups;
704
705 WARN_ON(!sg);
706
707 do {
708 int cpu, max_cpu = -1;
709
710 sg->group_weight = cpumask_weight(sched_group_cpus(sg));
711
712 if (!(sd->flags & SD_ASYM_PACKING))
713 goto next;
714
715 for_each_cpu(cpu, sched_group_cpus(sg)) {
716 if (max_cpu < 0)
717 max_cpu = cpu;
718 else if (sched_asym_prefer(cpu, max_cpu))
719 max_cpu = cpu;
720 }
721 sg->asym_prefer_cpu = max_cpu;
722
723next:
724 sg = sg->next;
725 } while (sg != sd->groups);
726
727 if (cpu != group_balance_cpu(sg))
728 return;
729
730 update_group_capacity(sd, cpu);
731}
732
733/*
734 * Initializers for schedule domains
735 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
736 */
737
738static int default_relax_domain_level = -1;
739int sched_domain_level_max;
740
741static int __init setup_relax_domain_level(char *str)
742{
743 if (kstrtoint(str, 0, &default_relax_domain_level))
744 pr_warn("Unable to set relax_domain_level\n");
745
746 return 1;
747}
748__setup("relax_domain_level=", setup_relax_domain_level);
749
750static void set_domain_attribute(struct sched_domain *sd,
751 struct sched_domain_attr *attr)
752{
753 int request;
754
755 if (!attr || attr->relax_domain_level < 0) {
756 if (default_relax_domain_level < 0)
757 return;
758 else
759 request = default_relax_domain_level;
760 } else
761 request = attr->relax_domain_level;
762 if (request < sd->level) {
763 /* Turn off idle balance on this domain: */
764 sd->flags &= ~(SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
765 } else {
766 /* Turn on idle balance on this domain: */
767 sd->flags |= (SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
768 }
769}
770
771static void __sdt_free(const struct cpumask *cpu_map);
772static int __sdt_alloc(const struct cpumask *cpu_map);
773
774static void __free_domain_allocs(struct s_data *d, enum s_alloc what,
775 const struct cpumask *cpu_map)
776{
777 switch (what) {
778 case sa_rootdomain:
779 if (!atomic_read(&d->rd->refcount))
780 free_rootdomain(&d->rd->rcu);
781 /* Fall through */
782 case sa_sd:
783 free_percpu(d->sd);
784 /* Fall through */
785 case sa_sd_storage:
786 __sdt_free(cpu_map);
787 /* Fall through */
788 case sa_none:
789 break;
790 }
791}
792
793static enum s_alloc
794__visit_domain_allocation_hell(struct s_data *d, const struct cpumask *cpu_map)
795{
796 memset(d, 0, sizeof(*d));
797
798 if (__sdt_alloc(cpu_map))
799 return sa_sd_storage;
800 d->sd = alloc_percpu(struct sched_domain *);
801 if (!d->sd)
802 return sa_sd_storage;
803 d->rd = alloc_rootdomain();
804 if (!d->rd)
805 return sa_sd;
806 return sa_rootdomain;
807}
808
809/*
810 * NULL the sd_data elements we've used to build the sched_domain and
811 * sched_group structure so that the subsequent __free_domain_allocs()
812 * will not free the data we're using.
813 */
814static void claim_allocations(int cpu, struct sched_domain *sd)
815{
816 struct sd_data *sdd = sd->private;
817
818 WARN_ON_ONCE(*per_cpu_ptr(sdd->sd, cpu) != sd);
819 *per_cpu_ptr(sdd->sd, cpu) = NULL;
820
821 if (atomic_read(&(*per_cpu_ptr(sdd->sds, cpu))->ref))
822 *per_cpu_ptr(sdd->sds, cpu) = NULL;
823
824 if (atomic_read(&(*per_cpu_ptr(sdd->sg, cpu))->ref))
825 *per_cpu_ptr(sdd->sg, cpu) = NULL;
826
827 if (atomic_read(&(*per_cpu_ptr(sdd->sgc, cpu))->ref))
828 *per_cpu_ptr(sdd->sgc, cpu) = NULL;
829}
830
831#ifdef CONFIG_NUMA
832static int sched_domains_numa_levels;
833enum numa_topology_type sched_numa_topology_type;
834static int *sched_domains_numa_distance;
835int sched_max_numa_distance;
836static struct cpumask ***sched_domains_numa_masks;
837static int sched_domains_curr_level;
838#endif
839
840/*
841 * SD_flags allowed in topology descriptions.
842 *
843 * These flags are purely descriptive of the topology and do not prescribe
844 * behaviour. Behaviour is artificial and mapped in the below sd_init()
845 * function:
846 *
847 * SD_SHARE_CPUCAPACITY - describes SMT topologies
848 * SD_SHARE_PKG_RESOURCES - describes shared caches
849 * SD_NUMA - describes NUMA topologies
850 * SD_SHARE_POWERDOMAIN - describes shared power domain
851 * SD_ASYM_CPUCAPACITY - describes mixed capacity topologies
852 *
853 * Odd one out, which beside describing the topology has a quirk also
854 * prescribes the desired behaviour that goes along with it:
855 *
856 * SD_ASYM_PACKING - describes SMT quirks
857 */
858#define TOPOLOGY_SD_FLAGS \
859 (SD_SHARE_CPUCAPACITY | \
860 SD_SHARE_PKG_RESOURCES | \
861 SD_NUMA | \
862 SD_ASYM_PACKING | \
863 SD_ASYM_CPUCAPACITY | \
864 SD_SHARE_POWERDOMAIN)
865
866static struct sched_domain *
867sd_init(struct sched_domain_topology_level *tl,
868 const struct cpumask *cpu_map,
869 struct sched_domain *child, int cpu)
870{
871 struct sd_data *sdd = &tl->data;
872 struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu);
873 int sd_id, sd_weight, sd_flags = 0;
874
875#ifdef CONFIG_NUMA
876 /*
877 * Ugly hack to pass state to sd_numa_mask()...
878 */
879 sched_domains_curr_level = tl->numa_level;
880#endif
881
882 sd_weight = cpumask_weight(tl->mask(cpu));
883
884 if (tl->sd_flags)
885 sd_flags = (*tl->sd_flags)();
886 if (WARN_ONCE(sd_flags & ~TOPOLOGY_SD_FLAGS,
887 "wrong sd_flags in topology description\n"))
888 sd_flags &= ~TOPOLOGY_SD_FLAGS;
889
890 *sd = (struct sched_domain){
891 .min_interval = sd_weight,
892 .max_interval = 2*sd_weight,
893 .busy_factor = 32,
894 .imbalance_pct = 125,
895
896 .cache_nice_tries = 0,
897 .busy_idx = 0,
898 .idle_idx = 0,
899 .newidle_idx = 0,
900 .wake_idx = 0,
901 .forkexec_idx = 0,
902
903 .flags = 1*SD_LOAD_BALANCE
904 | 1*SD_BALANCE_NEWIDLE
905 | 1*SD_BALANCE_EXEC
906 | 1*SD_BALANCE_FORK
907 | 0*SD_BALANCE_WAKE
908 | 1*SD_WAKE_AFFINE
909 | 0*SD_SHARE_CPUCAPACITY
910 | 0*SD_SHARE_PKG_RESOURCES
911 | 0*SD_SERIALIZE
912 | 0*SD_PREFER_SIBLING
913 | 0*SD_NUMA
914 | sd_flags
915 ,
916
917 .last_balance = jiffies,
918 .balance_interval = sd_weight,
919 .smt_gain = 0,
920 .max_newidle_lb_cost = 0,
921 .next_decay_max_lb_cost = jiffies,
922 .child = child,
923#ifdef CONFIG_SCHED_DEBUG
924 .name = tl->name,
925#endif
926 };
927
928 cpumask_and(sched_domain_span(sd), cpu_map, tl->mask(cpu));
929 sd_id = cpumask_first(sched_domain_span(sd));
930
931 /*
932 * Convert topological properties into behaviour.
933 */
934
935 if (sd->flags & SD_ASYM_CPUCAPACITY) {
936 struct sched_domain *t = sd;
937
938 for_each_lower_domain(t)
939 t->flags |= SD_BALANCE_WAKE;
940 }
941
942 if (sd->flags & SD_SHARE_CPUCAPACITY) {
943 sd->flags |= SD_PREFER_SIBLING;
944 sd->imbalance_pct = 110;
945 sd->smt_gain = 1178; /* ~15% */
946
947 } else if (sd->flags & SD_SHARE_PKG_RESOURCES) {
948 sd->imbalance_pct = 117;
949 sd->cache_nice_tries = 1;
950 sd->busy_idx = 2;
951
952#ifdef CONFIG_NUMA
953 } else if (sd->flags & SD_NUMA) {
954 sd->cache_nice_tries = 2;
955 sd->busy_idx = 3;
956 sd->idle_idx = 2;
957
958 sd->flags |= SD_SERIALIZE;
959 if (sched_domains_numa_distance[tl->numa_level] > RECLAIM_DISTANCE) {
960 sd->flags &= ~(SD_BALANCE_EXEC |
961 SD_BALANCE_FORK |
962 SD_WAKE_AFFINE);
963 }
964
965#endif
966 } else {
967 sd->flags |= SD_PREFER_SIBLING;
968 sd->cache_nice_tries = 1;
969 sd->busy_idx = 2;
970 sd->idle_idx = 1;
971 }
972
973 /*
974 * For all levels sharing cache; connect a sched_domain_shared
975 * instance.
976 */
977 if (sd->flags & SD_SHARE_PKG_RESOURCES) {
978 sd->shared = *per_cpu_ptr(sdd->sds, sd_id);
979 atomic_inc(&sd->shared->ref);
980 atomic_set(&sd->shared->nr_busy_cpus, sd_weight);
981 }
982
983 sd->private = sdd;
984
985 return sd;
986}
987
988/*
989 * Topology list, bottom-up.
990 */
991static struct sched_domain_topology_level default_topology[] = {
992#ifdef CONFIG_SCHED_SMT
993 { cpu_smt_mask, cpu_smt_flags, SD_INIT_NAME(SMT) },
994#endif
995#ifdef CONFIG_SCHED_MC
996 { cpu_coregroup_mask, cpu_core_flags, SD_INIT_NAME(MC) },
997#endif
998 { cpu_cpu_mask, SD_INIT_NAME(DIE) },
999 { NULL, },
1000};
1001
1002static struct sched_domain_topology_level *sched_domain_topology =
1003 default_topology;
1004
1005#define for_each_sd_topology(tl) \
1006 for (tl = sched_domain_topology; tl->mask; tl++)
1007
1008void set_sched_topology(struct sched_domain_topology_level *tl)
1009{
1010 if (WARN_ON_ONCE(sched_smp_initialized))
1011 return;
1012
1013 sched_domain_topology = tl;
1014}
1015
1016#ifdef CONFIG_NUMA
1017
1018static const struct cpumask *sd_numa_mask(int cpu)
1019{
1020 return sched_domains_numa_masks[sched_domains_curr_level][cpu_to_node(cpu)];
1021}
1022
1023static void sched_numa_warn(const char *str)
1024{
1025 static int done = false;
1026 int i,j;
1027
1028 if (done)
1029 return;
1030
1031 done = true;
1032
1033 printk(KERN_WARNING "ERROR: %s\n\n", str);
1034
1035 for (i = 0; i < nr_node_ids; i++) {
1036 printk(KERN_WARNING " ");
1037 for (j = 0; j < nr_node_ids; j++)
1038 printk(KERN_CONT "%02d ", node_distance(i,j));
1039 printk(KERN_CONT "\n");
1040 }
1041 printk(KERN_WARNING "\n");
1042}
1043
1044bool find_numa_distance(int distance)
1045{
1046 int i;
1047
1048 if (distance == node_distance(0, 0))
1049 return true;
1050
1051 for (i = 0; i < sched_domains_numa_levels; i++) {
1052 if (sched_domains_numa_distance[i] == distance)
1053 return true;
1054 }
1055
1056 return false;
1057}
1058
1059/*
1060 * A system can have three types of NUMA topology:
1061 * NUMA_DIRECT: all nodes are directly connected, or not a NUMA system
1062 * NUMA_GLUELESS_MESH: some nodes reachable through intermediary nodes
1063 * NUMA_BACKPLANE: nodes can reach other nodes through a backplane
1064 *
1065 * The difference between a glueless mesh topology and a backplane
1066 * topology lies in whether communication between not directly
1067 * connected nodes goes through intermediary nodes (where programs
1068 * could run), or through backplane controllers. This affects
1069 * placement of programs.
1070 *
1071 * The type of topology can be discerned with the following tests:
1072 * - If the maximum distance between any nodes is 1 hop, the system
1073 * is directly connected.
1074 * - If for two nodes A and B, located N > 1 hops away from each other,
1075 * there is an intermediary node C, which is < N hops away from both
1076 * nodes A and B, the system is a glueless mesh.
1077 */
1078static void init_numa_topology_type(void)
1079{
1080 int a, b, c, n;
1081
1082 n = sched_max_numa_distance;
1083
1084 if (sched_domains_numa_levels <= 1) {
1085 sched_numa_topology_type = NUMA_DIRECT;
1086 return;
1087 }
1088
1089 for_each_online_node(a) {
1090 for_each_online_node(b) {
1091 /* Find two nodes furthest removed from each other. */
1092 if (node_distance(a, b) < n)
1093 continue;
1094
1095 /* Is there an intermediary node between a and b? */
1096 for_each_online_node(c) {
1097 if (node_distance(a, c) < n &&
1098 node_distance(b, c) < n) {
1099 sched_numa_topology_type =
1100 NUMA_GLUELESS_MESH;
1101 return;
1102 }
1103 }
1104
1105 sched_numa_topology_type = NUMA_BACKPLANE;
1106 return;
1107 }
1108 }
1109}
1110
1111void sched_init_numa(void)
1112{
1113 int next_distance, curr_distance = node_distance(0, 0);
1114 struct sched_domain_topology_level *tl;
1115 int level = 0;
1116 int i, j, k;
1117
1118 sched_domains_numa_distance = kzalloc(sizeof(int) * nr_node_ids, GFP_KERNEL);
1119 if (!sched_domains_numa_distance)
1120 return;
1121
1122 /*
1123 * O(nr_nodes^2) deduplicating selection sort -- in order to find the
1124 * unique distances in the node_distance() table.
1125 *
1126 * Assumes node_distance(0,j) includes all distances in
1127 * node_distance(i,j) in order to avoid cubic time.
1128 */
1129 next_distance = curr_distance;
1130 for (i = 0; i < nr_node_ids; i++) {
1131 for (j = 0; j < nr_node_ids; j++) {
1132 for (k = 0; k < nr_node_ids; k++) {
1133 int distance = node_distance(i, k);
1134
1135 if (distance > curr_distance &&
1136 (distance < next_distance ||
1137 next_distance == curr_distance))
1138 next_distance = distance;
1139
1140 /*
1141 * While not a strong assumption it would be nice to know
1142 * about cases where if node A is connected to B, B is not
1143 * equally connected to A.
1144 */
1145 if (sched_debug() && node_distance(k, i) != distance)
1146 sched_numa_warn("Node-distance not symmetric");
1147
1148 if (sched_debug() && i && !find_numa_distance(distance))
1149 sched_numa_warn("Node-0 not representative");
1150 }
1151 if (next_distance != curr_distance) {
1152 sched_domains_numa_distance[level++] = next_distance;
1153 sched_domains_numa_levels = level;
1154 curr_distance = next_distance;
1155 } else break;
1156 }
1157
1158 /*
1159 * In case of sched_debug() we verify the above assumption.
1160 */
1161 if (!sched_debug())
1162 break;
1163 }
1164
1165 if (!level)
1166 return;
1167
1168 /*
1169 * 'level' contains the number of unique distances, excluding the
1170 * identity distance node_distance(i,i).
1171 *
1172 * The sched_domains_numa_distance[] array includes the actual distance
1173 * numbers.
1174 */
1175
1176 /*
1177 * Here, we should temporarily reset sched_domains_numa_levels to 0.
1178 * If it fails to allocate memory for array sched_domains_numa_masks[][],
1179 * the array will contain less then 'level' members. This could be
1180 * dangerous when we use it to iterate array sched_domains_numa_masks[][]
1181 * in other functions.
1182 *
1183 * We reset it to 'level' at the end of this function.
1184 */
1185 sched_domains_numa_levels = 0;
1186
1187 sched_domains_numa_masks = kzalloc(sizeof(void *) * level, GFP_KERNEL);
1188 if (!sched_domains_numa_masks)
1189 return;
1190
1191 /*
1192 * Now for each level, construct a mask per node which contains all
1193 * CPUs of nodes that are that many hops away from us.
1194 */
1195 for (i = 0; i < level; i++) {
1196 sched_domains_numa_masks[i] =
1197 kzalloc(nr_node_ids * sizeof(void *), GFP_KERNEL);
1198 if (!sched_domains_numa_masks[i])
1199 return;
1200
1201 for (j = 0; j < nr_node_ids; j++) {
1202 struct cpumask *mask = kzalloc(cpumask_size(), GFP_KERNEL);
1203 if (!mask)
1204 return;
1205
1206 sched_domains_numa_masks[i][j] = mask;
1207
1208 for_each_node(k) {
1209 if (node_distance(j, k) > sched_domains_numa_distance[i])
1210 continue;
1211
1212 cpumask_or(mask, mask, cpumask_of_node(k));
1213 }
1214 }
1215 }
1216
1217 /* Compute default topology size */
1218 for (i = 0; sched_domain_topology[i].mask; i++);
1219
1220 tl = kzalloc((i + level + 1) *
1221 sizeof(struct sched_domain_topology_level), GFP_KERNEL);
1222 if (!tl)
1223 return;
1224
1225 /*
1226 * Copy the default topology bits..
1227 */
1228 for (i = 0; sched_domain_topology[i].mask; i++)
1229 tl[i] = sched_domain_topology[i];
1230
1231 /*
1232 * .. and append 'j' levels of NUMA goodness.
1233 */
1234 for (j = 0; j < level; i++, j++) {
1235 tl[i] = (struct sched_domain_topology_level){
1236 .mask = sd_numa_mask,
1237 .sd_flags = cpu_numa_flags,
1238 .flags = SDTL_OVERLAP,
1239 .numa_level = j,
1240 SD_INIT_NAME(NUMA)
1241 };
1242 }
1243
1244 sched_domain_topology = tl;
1245
1246 sched_domains_numa_levels = level;
1247 sched_max_numa_distance = sched_domains_numa_distance[level - 1];
1248
1249 init_numa_topology_type();
1250}
1251
1252void sched_domains_numa_masks_set(unsigned int cpu)
1253{
1254 int node = cpu_to_node(cpu);
1255 int i, j;
1256
1257 for (i = 0; i < sched_domains_numa_levels; i++) {
1258 for (j = 0; j < nr_node_ids; j++) {
1259 if (node_distance(j, node) <= sched_domains_numa_distance[i])
1260 cpumask_set_cpu(cpu, sched_domains_numa_masks[i][j]);
1261 }
1262 }
1263}
1264
1265void sched_domains_numa_masks_clear(unsigned int cpu)
1266{
1267 int i, j;
1268
1269 for (i = 0; i < sched_domains_numa_levels; i++) {
1270 for (j = 0; j < nr_node_ids; j++)
1271 cpumask_clear_cpu(cpu, sched_domains_numa_masks[i][j]);
1272 }
1273}
1274
1275#endif /* CONFIG_NUMA */
1276
1277static int __sdt_alloc(const struct cpumask *cpu_map)
1278{
1279 struct sched_domain_topology_level *tl;
1280 int j;
1281
1282 for_each_sd_topology(tl) {
1283 struct sd_data *sdd = &tl->data;
1284
1285 sdd->sd = alloc_percpu(struct sched_domain *);
1286 if (!sdd->sd)
1287 return -ENOMEM;
1288
1289 sdd->sds = alloc_percpu(struct sched_domain_shared *);
1290 if (!sdd->sds)
1291 return -ENOMEM;
1292
1293 sdd->sg = alloc_percpu(struct sched_group *);
1294 if (!sdd->sg)
1295 return -ENOMEM;
1296
1297 sdd->sgc = alloc_percpu(struct sched_group_capacity *);
1298 if (!sdd->sgc)
1299 return -ENOMEM;
1300
1301 for_each_cpu(j, cpu_map) {
1302 struct sched_domain *sd;
1303 struct sched_domain_shared *sds;
1304 struct sched_group *sg;
1305 struct sched_group_capacity *sgc;
1306
1307 sd = kzalloc_node(sizeof(struct sched_domain) + cpumask_size(),
1308 GFP_KERNEL, cpu_to_node(j));
1309 if (!sd)
1310 return -ENOMEM;
1311
1312 *per_cpu_ptr(sdd->sd, j) = sd;
1313
1314 sds = kzalloc_node(sizeof(struct sched_domain_shared),
1315 GFP_KERNEL, cpu_to_node(j));
1316 if (!sds)
1317 return -ENOMEM;
1318
1319 *per_cpu_ptr(sdd->sds, j) = sds;
1320
1321 sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
1322 GFP_KERNEL, cpu_to_node(j));
1323 if (!sg)
1324 return -ENOMEM;
1325
1326 sg->next = sg;
1327
1328 *per_cpu_ptr(sdd->sg, j) = sg;
1329
1330 sgc = kzalloc_node(sizeof(struct sched_group_capacity) + cpumask_size(),
1331 GFP_KERNEL, cpu_to_node(j));
1332 if (!sgc)
1333 return -ENOMEM;
1334
1335 *per_cpu_ptr(sdd->sgc, j) = sgc;
1336 }
1337 }
1338
1339 return 0;
1340}
1341
1342static void __sdt_free(const struct cpumask *cpu_map)
1343{
1344 struct sched_domain_topology_level *tl;
1345 int j;
1346
1347 for_each_sd_topology(tl) {
1348 struct sd_data *sdd = &tl->data;
1349
1350 for_each_cpu(j, cpu_map) {
1351 struct sched_domain *sd;
1352
1353 if (sdd->sd) {
1354 sd = *per_cpu_ptr(sdd->sd, j);
1355 if (sd && (sd->flags & SD_OVERLAP))
1356 free_sched_groups(sd->groups, 0);
1357 kfree(*per_cpu_ptr(sdd->sd, j));
1358 }
1359
1360 if (sdd->sds)
1361 kfree(*per_cpu_ptr(sdd->sds, j));
1362 if (sdd->sg)
1363 kfree(*per_cpu_ptr(sdd->sg, j));
1364 if (sdd->sgc)
1365 kfree(*per_cpu_ptr(sdd->sgc, j));
1366 }
1367 free_percpu(sdd->sd);
1368 sdd->sd = NULL;
1369 free_percpu(sdd->sds);
1370 sdd->sds = NULL;
1371 free_percpu(sdd->sg);
1372 sdd->sg = NULL;
1373 free_percpu(sdd->sgc);
1374 sdd->sgc = NULL;
1375 }
1376}
1377
1378struct sched_domain *build_sched_domain(struct sched_domain_topology_level *tl,
1379 const struct cpumask *cpu_map, struct sched_domain_attr *attr,
1380 struct sched_domain *child, int cpu)
1381{
1382 struct sched_domain *sd = sd_init(tl, cpu_map, child, cpu);
1383
1384 if (child) {
1385 sd->level = child->level + 1;
1386 sched_domain_level_max = max(sched_domain_level_max, sd->level);
1387 child->parent = sd;
1388
1389 if (!cpumask_subset(sched_domain_span(child),
1390 sched_domain_span(sd))) {
1391 pr_err("BUG: arch topology borken\n");
1392#ifdef CONFIG_SCHED_DEBUG
1393 pr_err(" the %s domain not a subset of the %s domain\n",
1394 child->name, sd->name);
1395#endif
1396 /* Fixup, ensure @sd has at least @child cpus. */
1397 cpumask_or(sched_domain_span(sd),
1398 sched_domain_span(sd),
1399 sched_domain_span(child));
1400 }
1401
1402 }
1403 set_domain_attribute(sd, attr);
1404
1405 return sd;
1406}
1407
1408/*
1409 * Build sched domains for a given set of CPUs and attach the sched domains
1410 * to the individual CPUs
1411 */
1412static int
1413build_sched_domains(const struct cpumask *cpu_map, struct sched_domain_attr *attr)
1414{
1415 enum s_alloc alloc_state;
1416 struct sched_domain *sd;
1417 struct s_data d;
1418 struct rq *rq = NULL;
1419 int i, ret = -ENOMEM;
1420
1421 alloc_state = __visit_domain_allocation_hell(&d, cpu_map);
1422 if (alloc_state != sa_rootdomain)
1423 goto error;
1424
1425 /* Set up domains for CPUs specified by the cpu_map: */
1426 for_each_cpu(i, cpu_map) {
1427 struct sched_domain_topology_level *tl;
1428
1429 sd = NULL;
1430 for_each_sd_topology(tl) {
1431 sd = build_sched_domain(tl, cpu_map, attr, sd, i);
1432 if (tl == sched_domain_topology)
1433 *per_cpu_ptr(d.sd, i) = sd;
1434 if (tl->flags & SDTL_OVERLAP || sched_feat(FORCE_SD_OVERLAP))
1435 sd->flags |= SD_OVERLAP;
1436 if (cpumask_equal(cpu_map, sched_domain_span(sd)))
1437 break;
1438 }
1439 }
1440
1441 /* Build the groups for the domains */
1442 for_each_cpu(i, cpu_map) {
1443 for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
1444 sd->span_weight = cpumask_weight(sched_domain_span(sd));
1445 if (sd->flags & SD_OVERLAP) {
1446 if (build_overlap_sched_groups(sd, i))
1447 goto error;
1448 } else {
1449 if (build_sched_groups(sd, i))
1450 goto error;
1451 }
1452 }
1453 }
1454
1455 /* Calculate CPU capacity for physical packages and nodes */
1456 for (i = nr_cpumask_bits-1; i >= 0; i--) {
1457 if (!cpumask_test_cpu(i, cpu_map))
1458 continue;
1459
1460 for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
1461 claim_allocations(i, sd);
1462 init_sched_groups_capacity(i, sd);
1463 }
1464 }
1465
1466 /* Attach the domains */
1467 rcu_read_lock();
1468 for_each_cpu(i, cpu_map) {
1469 rq = cpu_rq(i);
1470 sd = *per_cpu_ptr(d.sd, i);
1471
1472 /* Use READ_ONCE()/WRITE_ONCE() to avoid load/store tearing: */
1473 if (rq->cpu_capacity_orig > READ_ONCE(d.rd->max_cpu_capacity))
1474 WRITE_ONCE(d.rd->max_cpu_capacity, rq->cpu_capacity_orig);
1475
1476 cpu_attach_domain(sd, d.rd, i);
1477 }
1478 rcu_read_unlock();
1479
1480 if (rq && sched_debug_enabled) {
1481 pr_info("span: %*pbl (max cpu_capacity = %lu)\n",
1482 cpumask_pr_args(cpu_map), rq->rd->max_cpu_capacity);
1483 }
1484
1485 ret = 0;
1486error:
1487 __free_domain_allocs(&d, alloc_state, cpu_map);
1488 return ret;
1489}
1490
1491/* Current sched domains: */
1492static cpumask_var_t *doms_cur;
1493
1494/* Number of sched domains in 'doms_cur': */
1495static int ndoms_cur;
1496
1497/* Attribues of custom domains in 'doms_cur' */
1498static struct sched_domain_attr *dattr_cur;
1499
1500/*
1501 * Special case: If a kmalloc() of a doms_cur partition (array of
1502 * cpumask) fails, then fallback to a single sched domain,
1503 * as determined by the single cpumask fallback_doms.
1504 */
1505cpumask_var_t fallback_doms;
1506
1507/*
1508 * arch_update_cpu_topology lets virtualized architectures update the
1509 * CPU core maps. It is supposed to return 1 if the topology changed
1510 * or 0 if it stayed the same.
1511 */
1512int __weak arch_update_cpu_topology(void)
1513{
1514 return 0;
1515}
1516
1517cpumask_var_t *alloc_sched_domains(unsigned int ndoms)
1518{
1519 int i;
1520 cpumask_var_t *doms;
1521
1522 doms = kmalloc(sizeof(*doms) * ndoms, GFP_KERNEL);
1523 if (!doms)
1524 return NULL;
1525 for (i = 0; i < ndoms; i++) {
1526 if (!alloc_cpumask_var(&doms[i], GFP_KERNEL)) {
1527 free_sched_domains(doms, i);
1528 return NULL;
1529 }
1530 }
1531 return doms;
1532}
1533
1534void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms)
1535{
1536 unsigned int i;
1537 for (i = 0; i < ndoms; i++)
1538 free_cpumask_var(doms[i]);
1539 kfree(doms);
1540}
1541
1542/*
1543 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
1544 * For now this just excludes isolated CPUs, but could be used to
1545 * exclude other special cases in the future.
1546 */
1547int init_sched_domains(const struct cpumask *cpu_map)
1548{
1549 int err;
1550
1551 arch_update_cpu_topology();
1552 ndoms_cur = 1;
1553 doms_cur = alloc_sched_domains(ndoms_cur);
1554 if (!doms_cur)
1555 doms_cur = &fallback_doms;
1556 cpumask_andnot(doms_cur[0], cpu_map, cpu_isolated_map);
1557 err = build_sched_domains(doms_cur[0], NULL);
1558 register_sched_domain_sysctl();
1559
1560 return err;
1561}
1562
1563/*
1564 * Detach sched domains from a group of CPUs specified in cpu_map
1565 * These CPUs will now be attached to the NULL domain
1566 */
1567static void detach_destroy_domains(const struct cpumask *cpu_map)
1568{
1569 int i;
1570
1571 rcu_read_lock();
1572 for_each_cpu(i, cpu_map)
1573 cpu_attach_domain(NULL, &def_root_domain, i);
1574 rcu_read_unlock();
1575}
1576
1577/* handle null as "default" */
1578static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
1579 struct sched_domain_attr *new, int idx_new)
1580{
1581 struct sched_domain_attr tmp;
1582
1583 /* Fast path: */
1584 if (!new && !cur)
1585 return 1;
1586
1587 tmp = SD_ATTR_INIT;
1588 return !memcmp(cur ? (cur + idx_cur) : &tmp,
1589 new ? (new + idx_new) : &tmp,
1590 sizeof(struct sched_domain_attr));
1591}
1592
1593/*
1594 * Partition sched domains as specified by the 'ndoms_new'
1595 * cpumasks in the array doms_new[] of cpumasks. This compares
1596 * doms_new[] to the current sched domain partitioning, doms_cur[].
1597 * It destroys each deleted domain and builds each new domain.
1598 *
1599 * 'doms_new' is an array of cpumask_var_t's of length 'ndoms_new'.
1600 * The masks don't intersect (don't overlap.) We should setup one
1601 * sched domain for each mask. CPUs not in any of the cpumasks will
1602 * not be load balanced. If the same cpumask appears both in the
1603 * current 'doms_cur' domains and in the new 'doms_new', we can leave
1604 * it as it is.
1605 *
1606 * The passed in 'doms_new' should be allocated using
1607 * alloc_sched_domains. This routine takes ownership of it and will
1608 * free_sched_domains it when done with it. If the caller failed the
1609 * alloc call, then it can pass in doms_new == NULL && ndoms_new == 1,
1610 * and partition_sched_domains() will fallback to the single partition
1611 * 'fallback_doms', it also forces the domains to be rebuilt.
1612 *
1613 * If doms_new == NULL it will be replaced with cpu_online_mask.
1614 * ndoms_new == 0 is a special case for destroying existing domains,
1615 * and it will not create the default domain.
1616 *
1617 * Call with hotplug lock held
1618 */
1619void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
1620 struct sched_domain_attr *dattr_new)
1621{
1622 int i, j, n;
1623 int new_topology;
1624
1625 mutex_lock(&sched_domains_mutex);
1626
1627 /* Always unregister in case we don't destroy any domains: */
1628 unregister_sched_domain_sysctl();
1629
1630 /* Let the architecture update CPU core mappings: */
1631 new_topology = arch_update_cpu_topology();
1632
1633 n = doms_new ? ndoms_new : 0;
1634
1635 /* Destroy deleted domains: */
1636 for (i = 0; i < ndoms_cur; i++) {
1637 for (j = 0; j < n && !new_topology; j++) {
1638 if (cpumask_equal(doms_cur[i], doms_new[j])
1639 && dattrs_equal(dattr_cur, i, dattr_new, j))
1640 goto match1;
1641 }
1642 /* No match - a current sched domain not in new doms_new[] */
1643 detach_destroy_domains(doms_cur[i]);
1644match1:
1645 ;
1646 }
1647
1648 n = ndoms_cur;
1649 if (doms_new == NULL) {
1650 n = 0;
1651 doms_new = &fallback_doms;
1652 cpumask_andnot(doms_new[0], cpu_active_mask, cpu_isolated_map);
1653 WARN_ON_ONCE(dattr_new);
1654 }
1655
1656 /* Build new domains: */
1657 for (i = 0; i < ndoms_new; i++) {
1658 for (j = 0; j < n && !new_topology; j++) {
1659 if (cpumask_equal(doms_new[i], doms_cur[j])
1660 && dattrs_equal(dattr_new, i, dattr_cur, j))
1661 goto match2;
1662 }
1663 /* No match - add a new doms_new */
1664 build_sched_domains(doms_new[i], dattr_new ? dattr_new + i : NULL);
1665match2:
1666 ;
1667 }
1668
1669 /* Remember the new sched domains: */
1670 if (doms_cur != &fallback_doms)
1671 free_sched_domains(doms_cur, ndoms_cur);
1672
1673 kfree(dattr_cur);
1674 doms_cur = doms_new;
1675 dattr_cur = dattr_new;
1676 ndoms_cur = ndoms_new;
1677
1678 register_sched_domain_sysctl();
1679
1680 mutex_unlock(&sched_domains_mutex);
1681}
1682