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