1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Scheduler topology setup/handling methods 4 */ 5 6 #include <linux/bsearch.h> 7 8 DEFINE_MUTEX(sched_domains_mutex); 9 void sched_domains_mutex_lock(void) 10 { 11 mutex_lock(&sched_domains_mutex); 12 } 13 void sched_domains_mutex_unlock(void) 14 { 15 mutex_unlock(&sched_domains_mutex); 16 } 17 18 /* Protected by sched_domains_mutex: */ 19 static cpumask_var_t sched_domains_tmpmask; 20 static cpumask_var_t sched_domains_tmpmask2; 21 22 static int __init sched_debug_setup(char *str) 23 { 24 sched_debug_verbose = true; 25 26 return 0; 27 } 28 early_param("sched_verbose", sched_debug_setup); 29 30 static inline bool sched_debug(void) 31 { 32 return sched_debug_verbose; 33 } 34 35 #define SD_FLAG(_name, mflags) [__##_name] = { .meta_flags = mflags, .name = #_name }, 36 const struct sd_flag_debug sd_flag_debug[] = { 37 #include <linux/sched/sd_flags.h> 38 }; 39 #undef SD_FLAG 40 41 static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level, 42 struct cpumask *groupmask) 43 { 44 struct sched_group *group = sd->groups; 45 unsigned long flags = sd->flags; 46 unsigned int idx; 47 48 cpumask_clear(groupmask); 49 50 printk(KERN_DEBUG "%*s domain-%d: ", level, "", level); 51 printk(KERN_CONT "span=%*pbl level=%s\n", 52 cpumask_pr_args(sched_domain_span(sd)), sd->name); 53 54 if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) { 55 printk(KERN_ERR "ERROR: domain->span does not contain CPU%d\n", cpu); 56 } 57 if (group && !cpumask_test_cpu(cpu, sched_group_span(group))) { 58 printk(KERN_ERR "ERROR: domain->groups does not contain CPU%d\n", cpu); 59 } 60 61 for_each_set_bit(idx, &flags, __SD_FLAG_CNT) { 62 unsigned int flag = BIT(idx); 63 unsigned int meta_flags = sd_flag_debug[idx].meta_flags; 64 65 if ((meta_flags & SDF_SHARED_CHILD) && sd->child && 66 !(sd->child->flags & flag)) 67 printk(KERN_ERR "ERROR: flag %s set here but not in child\n", 68 sd_flag_debug[idx].name); 69 70 if ((meta_flags & SDF_SHARED_PARENT) && sd->parent && 71 !(sd->parent->flags & flag)) 72 printk(KERN_ERR "ERROR: flag %s set here but not in parent\n", 73 sd_flag_debug[idx].name); 74 } 75 76 printk(KERN_DEBUG "%*s groups:", level + 1, ""); 77 do { 78 if (!group) { 79 printk("\n"); 80 printk(KERN_ERR "ERROR: group is NULL\n"); 81 break; 82 } 83 84 if (cpumask_empty(sched_group_span(group))) { 85 printk(KERN_CONT "\n"); 86 printk(KERN_ERR "ERROR: empty group\n"); 87 break; 88 } 89 90 if (!(sd->flags & SD_OVERLAP) && 91 cpumask_intersects(groupmask, sched_group_span(group))) { 92 printk(KERN_CONT "\n"); 93 printk(KERN_ERR "ERROR: repeated CPUs\n"); 94 break; 95 } 96 97 cpumask_or(groupmask, groupmask, sched_group_span(group)); 98 99 printk(KERN_CONT " %d:{ span=%*pbl", 100 group->sgc->id, 101 cpumask_pr_args(sched_group_span(group))); 102 103 if ((sd->flags & SD_OVERLAP) && 104 !cpumask_equal(group_balance_mask(group), sched_group_span(group))) { 105 printk(KERN_CONT " mask=%*pbl", 106 cpumask_pr_args(group_balance_mask(group))); 107 } 108 109 if (group->sgc->capacity != SCHED_CAPACITY_SCALE) 110 printk(KERN_CONT " cap=%lu", group->sgc->capacity); 111 112 if (group == sd->groups && sd->child && 113 !cpumask_equal(sched_domain_span(sd->child), 114 sched_group_span(group))) { 115 printk(KERN_ERR "ERROR: domain->groups does not match domain->child\n"); 116 } 117 118 printk(KERN_CONT " }"); 119 120 group = group->next; 121 122 if (group != sd->groups) 123 printk(KERN_CONT ","); 124 125 } while (group != sd->groups); 126 printk(KERN_CONT "\n"); 127 128 if (!cpumask_equal(sched_domain_span(sd), groupmask)) 129 printk(KERN_ERR "ERROR: groups don't span domain->span\n"); 130 131 if (sd->parent && 132 !cpumask_subset(groupmask, sched_domain_span(sd->parent))) 133 printk(KERN_ERR "ERROR: parent span is not a superset of domain->span\n"); 134 return 0; 135 } 136 137 static void sched_domain_debug(struct sched_domain *sd, int cpu) 138 { 139 int level = 0; 140 141 if (!sched_debug_verbose) 142 return; 143 144 if (!sd) { 145 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu); 146 return; 147 } 148 149 printk(KERN_DEBUG "CPU%d attaching sched-domain(s):\n", cpu); 150 151 for (;;) { 152 if (sched_domain_debug_one(sd, cpu, level, sched_domains_tmpmask)) 153 break; 154 level++; 155 sd = sd->parent; 156 if (!sd) 157 break; 158 } 159 } 160 161 /* Generate a mask of SD flags with the SDF_NEEDS_GROUPS metaflag */ 162 #define SD_FLAG(name, mflags) (name * !!((mflags) & SDF_NEEDS_GROUPS)) | 163 static const unsigned int SD_DEGENERATE_GROUPS_MASK = 164 #include <linux/sched/sd_flags.h> 165 0; 166 #undef SD_FLAG 167 168 static int sd_degenerate(struct sched_domain *sd) 169 { 170 if (cpumask_weight(sched_domain_span(sd)) == 1) 171 return 1; 172 173 /* Following flags need at least 2 groups */ 174 if ((sd->flags & SD_DEGENERATE_GROUPS_MASK) && 175 (sd->groups != sd->groups->next)) 176 return 0; 177 178 /* Following flags don't use groups */ 179 if (sd->flags & (SD_WAKE_AFFINE)) 180 return 0; 181 182 return 1; 183 } 184 185 static int 186 sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent) 187 { 188 unsigned long cflags = sd->flags, pflags = parent->flags; 189 190 if (sd_degenerate(parent)) 191 return 1; 192 193 if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent))) 194 return 0; 195 196 /* Flags needing groups don't count if only 1 group in parent */ 197 if (parent->groups == parent->groups->next) 198 pflags &= ~SD_DEGENERATE_GROUPS_MASK; 199 200 if (~cflags & pflags) 201 return 0; 202 203 return 1; 204 } 205 206 #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL) 207 DEFINE_STATIC_KEY_FALSE(sched_energy_present); 208 static unsigned int sysctl_sched_energy_aware = 1; 209 static DEFINE_MUTEX(sched_energy_mutex); 210 static bool sched_energy_update; 211 212 static bool sched_is_eas_possible(const struct cpumask *cpu_mask) 213 { 214 bool any_asym_capacity = false; 215 struct cpufreq_policy *policy; 216 struct cpufreq_governor *gov; 217 int i; 218 219 /* EAS is enabled for asymmetric CPU capacity topologies. */ 220 for_each_cpu(i, cpu_mask) { 221 if (rcu_access_pointer(per_cpu(sd_asym_cpucapacity, i))) { 222 any_asym_capacity = true; 223 break; 224 } 225 } 226 if (!any_asym_capacity) { 227 if (sched_debug()) { 228 pr_info("rd %*pbl: Checking EAS, CPUs do not have asymmetric capacities\n", 229 cpumask_pr_args(cpu_mask)); 230 } 231 return false; 232 } 233 234 /* EAS definitely does *not* handle SMT */ 235 if (sched_smt_active()) { 236 if (sched_debug()) { 237 pr_info("rd %*pbl: Checking EAS, SMT is not supported\n", 238 cpumask_pr_args(cpu_mask)); 239 } 240 return false; 241 } 242 243 if (!arch_scale_freq_invariant()) { 244 if (sched_debug()) { 245 pr_info("rd %*pbl: Checking EAS: frequency-invariant load tracking not yet supported", 246 cpumask_pr_args(cpu_mask)); 247 } 248 return false; 249 } 250 251 /* Do not attempt EAS if schedutil is not being used. */ 252 for_each_cpu(i, cpu_mask) { 253 policy = cpufreq_cpu_get(i); 254 if (!policy) { 255 if (sched_debug()) { 256 pr_info("rd %*pbl: Checking EAS, cpufreq policy not set for CPU: %d", 257 cpumask_pr_args(cpu_mask), i); 258 } 259 return false; 260 } 261 gov = policy->governor; 262 cpufreq_cpu_put(policy); 263 if (gov != &schedutil_gov) { 264 if (sched_debug()) { 265 pr_info("rd %*pbl: Checking EAS, schedutil is mandatory\n", 266 cpumask_pr_args(cpu_mask)); 267 } 268 return false; 269 } 270 } 271 272 return true; 273 } 274 275 void rebuild_sched_domains_energy(void) 276 { 277 mutex_lock(&sched_energy_mutex); 278 sched_energy_update = true; 279 rebuild_sched_domains(); 280 sched_energy_update = false; 281 mutex_unlock(&sched_energy_mutex); 282 } 283 284 #ifdef CONFIG_PROC_SYSCTL 285 static int sched_energy_aware_handler(const struct ctl_table *table, int write, 286 void *buffer, size_t *lenp, loff_t *ppos) 287 { 288 int ret, state; 289 290 if (write && !capable(CAP_SYS_ADMIN)) 291 return -EPERM; 292 293 if (!sched_is_eas_possible(cpu_active_mask)) { 294 if (write) { 295 return -EOPNOTSUPP; 296 } else { 297 *lenp = 0; 298 return 0; 299 } 300 } 301 302 ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); 303 if (!ret && write) { 304 state = static_branch_unlikely(&sched_energy_present); 305 if (state != sysctl_sched_energy_aware) 306 rebuild_sched_domains_energy(); 307 } 308 309 return ret; 310 } 311 312 static const struct ctl_table sched_energy_aware_sysctls[] = { 313 { 314 .procname = "sched_energy_aware", 315 .data = &sysctl_sched_energy_aware, 316 .maxlen = sizeof(unsigned int), 317 .mode = 0644, 318 .proc_handler = sched_energy_aware_handler, 319 .extra1 = SYSCTL_ZERO, 320 .extra2 = SYSCTL_ONE, 321 }, 322 }; 323 324 static int __init sched_energy_aware_sysctl_init(void) 325 { 326 register_sysctl_init("kernel", sched_energy_aware_sysctls); 327 return 0; 328 } 329 330 late_initcall(sched_energy_aware_sysctl_init); 331 #endif 332 333 static void free_pd(struct perf_domain *pd) 334 { 335 struct perf_domain *tmp; 336 337 while (pd) { 338 tmp = pd->next; 339 kfree(pd); 340 pd = tmp; 341 } 342 } 343 344 static struct perf_domain *find_pd(struct perf_domain *pd, int cpu) 345 { 346 while (pd) { 347 if (cpumask_test_cpu(cpu, perf_domain_span(pd))) 348 return pd; 349 pd = pd->next; 350 } 351 352 return NULL; 353 } 354 355 static struct perf_domain *pd_init(int cpu) 356 { 357 struct em_perf_domain *obj = em_cpu_get(cpu); 358 struct perf_domain *pd; 359 360 if (!obj) { 361 if (sched_debug()) 362 pr_info("%s: no EM found for CPU%d\n", __func__, cpu); 363 return NULL; 364 } 365 366 pd = kzalloc(sizeof(*pd), GFP_KERNEL); 367 if (!pd) 368 return NULL; 369 pd->em_pd = obj; 370 371 return pd; 372 } 373 374 static void perf_domain_debug(const struct cpumask *cpu_map, 375 struct perf_domain *pd) 376 { 377 if (!sched_debug() || !pd) 378 return; 379 380 printk(KERN_DEBUG "root_domain %*pbl:", cpumask_pr_args(cpu_map)); 381 382 while (pd) { 383 printk(KERN_CONT " pd%d:{ cpus=%*pbl nr_pstate=%d }", 384 cpumask_first(perf_domain_span(pd)), 385 cpumask_pr_args(perf_domain_span(pd)), 386 em_pd_nr_perf_states(pd->em_pd)); 387 pd = pd->next; 388 } 389 390 printk(KERN_CONT "\n"); 391 } 392 393 static void destroy_perf_domain_rcu(struct rcu_head *rp) 394 { 395 struct perf_domain *pd; 396 397 pd = container_of(rp, struct perf_domain, rcu); 398 free_pd(pd); 399 } 400 401 static void sched_energy_set(bool has_eas) 402 { 403 if (!has_eas && static_branch_unlikely(&sched_energy_present)) { 404 if (sched_debug()) 405 pr_info("%s: stopping EAS\n", __func__); 406 static_branch_disable_cpuslocked(&sched_energy_present); 407 } else if (has_eas && !static_branch_unlikely(&sched_energy_present)) { 408 if (sched_debug()) 409 pr_info("%s: starting EAS\n", __func__); 410 static_branch_enable_cpuslocked(&sched_energy_present); 411 } 412 } 413 414 /* 415 * EAS can be used on a root domain if it meets all the following conditions: 416 * 1. an Energy Model (EM) is available; 417 * 2. the SD_ASYM_CPUCAPACITY flag is set in the sched_domain hierarchy. 418 * 3. no SMT is detected. 419 * 4. schedutil is driving the frequency of all CPUs of the rd; 420 * 5. frequency invariance support is present; 421 */ 422 static bool build_perf_domains(const struct cpumask *cpu_map) 423 { 424 int i; 425 struct perf_domain *pd = NULL, *tmp; 426 int cpu = cpumask_first(cpu_map); 427 struct root_domain *rd = cpu_rq(cpu)->rd; 428 429 if (!sysctl_sched_energy_aware) 430 goto free; 431 432 if (!sched_is_eas_possible(cpu_map)) 433 goto free; 434 435 for_each_cpu(i, cpu_map) { 436 /* Skip already covered CPUs. */ 437 if (find_pd(pd, i)) 438 continue; 439 440 /* Create the new pd and add it to the local list. */ 441 tmp = pd_init(i); 442 if (!tmp) 443 goto free; 444 tmp->next = pd; 445 pd = tmp; 446 } 447 448 perf_domain_debug(cpu_map, pd); 449 450 /* Attach the new list of performance domains to the root domain. */ 451 tmp = rd->pd; 452 rcu_assign_pointer(rd->pd, pd); 453 if (tmp) 454 call_rcu(&tmp->rcu, destroy_perf_domain_rcu); 455 456 return !!pd; 457 458 free: 459 free_pd(pd); 460 tmp = rd->pd; 461 rcu_assign_pointer(rd->pd, NULL); 462 if (tmp) 463 call_rcu(&tmp->rcu, destroy_perf_domain_rcu); 464 465 return false; 466 } 467 #else 468 static void free_pd(struct perf_domain *pd) { } 469 #endif /* CONFIG_ENERGY_MODEL && CONFIG_CPU_FREQ_GOV_SCHEDUTIL*/ 470 471 static void free_rootdomain(struct rcu_head *rcu) 472 { 473 struct root_domain *rd = container_of(rcu, struct root_domain, rcu); 474 475 cpupri_cleanup(&rd->cpupri); 476 cpudl_cleanup(&rd->cpudl); 477 free_cpumask_var(rd->dlo_mask); 478 free_cpumask_var(rd->rto_mask); 479 free_cpumask_var(rd->online); 480 free_cpumask_var(rd->span); 481 free_pd(rd->pd); 482 kfree(rd); 483 } 484 485 void rq_attach_root(struct rq *rq, struct root_domain *rd) 486 { 487 struct root_domain *old_rd = NULL; 488 struct rq_flags rf; 489 490 rq_lock_irqsave(rq, &rf); 491 492 if (rq->rd) { 493 old_rd = rq->rd; 494 495 if (cpumask_test_cpu(rq->cpu, old_rd->online)) 496 set_rq_offline(rq); 497 498 cpumask_clear_cpu(rq->cpu, old_rd->span); 499 500 /* 501 * If we don't want to free the old_rd yet then 502 * set old_rd to NULL to skip the freeing later 503 * in this function: 504 */ 505 if (!atomic_dec_and_test(&old_rd->refcount)) 506 old_rd = NULL; 507 } 508 509 atomic_inc(&rd->refcount); 510 rq->rd = rd; 511 512 cpumask_set_cpu(rq->cpu, rd->span); 513 if (cpumask_test_cpu(rq->cpu, cpu_active_mask)) 514 set_rq_online(rq); 515 516 /* 517 * Because the rq is not a task, dl_add_task_root_domain() did not 518 * move the fair server bw to the rd if it already started. 519 * Add it now. 520 */ 521 if (rq->fair_server.dl_server) 522 __dl_server_attach_root(&rq->fair_server, rq); 523 524 rq_unlock_irqrestore(rq, &rf); 525 526 if (old_rd) 527 call_rcu(&old_rd->rcu, free_rootdomain); 528 } 529 530 void sched_get_rd(struct root_domain *rd) 531 { 532 atomic_inc(&rd->refcount); 533 } 534 535 void sched_put_rd(struct root_domain *rd) 536 { 537 if (!atomic_dec_and_test(&rd->refcount)) 538 return; 539 540 call_rcu(&rd->rcu, free_rootdomain); 541 } 542 543 static int init_rootdomain(struct root_domain *rd) 544 { 545 if (!zalloc_cpumask_var(&rd->span, GFP_KERNEL)) 546 goto out; 547 if (!zalloc_cpumask_var(&rd->online, GFP_KERNEL)) 548 goto free_span; 549 if (!zalloc_cpumask_var(&rd->dlo_mask, GFP_KERNEL)) 550 goto free_online; 551 if (!zalloc_cpumask_var(&rd->rto_mask, GFP_KERNEL)) 552 goto free_dlo_mask; 553 554 #ifdef HAVE_RT_PUSH_IPI 555 rd->rto_cpu = -1; 556 raw_spin_lock_init(&rd->rto_lock); 557 rd->rto_push_work = IRQ_WORK_INIT_HARD(rto_push_irq_work_func); 558 #endif 559 560 rd->visit_cookie = 0; 561 init_dl_bw(&rd->dl_bw); 562 if (cpudl_init(&rd->cpudl) != 0) 563 goto free_rto_mask; 564 565 if (cpupri_init(&rd->cpupri) != 0) 566 goto free_cpudl; 567 return 0; 568 569 free_cpudl: 570 cpudl_cleanup(&rd->cpudl); 571 free_rto_mask: 572 free_cpumask_var(rd->rto_mask); 573 free_dlo_mask: 574 free_cpumask_var(rd->dlo_mask); 575 free_online: 576 free_cpumask_var(rd->online); 577 free_span: 578 free_cpumask_var(rd->span); 579 out: 580 return -ENOMEM; 581 } 582 583 /* 584 * By default the system creates a single root-domain with all CPUs as 585 * members (mimicking the global state we have today). 586 */ 587 struct root_domain def_root_domain; 588 589 void __init init_defrootdomain(void) 590 { 591 init_rootdomain(&def_root_domain); 592 593 atomic_set(&def_root_domain.refcount, 1); 594 } 595 596 static struct root_domain *alloc_rootdomain(void) 597 { 598 struct root_domain *rd; 599 600 rd = kzalloc(sizeof(*rd), GFP_KERNEL); 601 if (!rd) 602 return NULL; 603 604 if (init_rootdomain(rd) != 0) { 605 kfree(rd); 606 return NULL; 607 } 608 609 return rd; 610 } 611 612 static void free_sched_groups(struct sched_group *sg, int free_sgc) 613 { 614 struct sched_group *tmp, *first; 615 616 if (!sg) 617 return; 618 619 first = sg; 620 do { 621 tmp = sg->next; 622 623 if (free_sgc && atomic_dec_and_test(&sg->sgc->ref)) 624 kfree(sg->sgc); 625 626 if (atomic_dec_and_test(&sg->ref)) 627 kfree(sg); 628 sg = tmp; 629 } while (sg != first); 630 } 631 632 static void destroy_sched_domain(struct sched_domain *sd) 633 { 634 /* 635 * A normal sched domain may have multiple group references, an 636 * overlapping domain, having private groups, only one. Iterate, 637 * dropping group/capacity references, freeing where none remain. 638 */ 639 free_sched_groups(sd->groups, 1); 640 641 if (sd->shared && atomic_dec_and_test(&sd->shared->ref)) 642 kfree(sd->shared); 643 kfree(sd); 644 } 645 646 static void destroy_sched_domains_rcu(struct rcu_head *rcu) 647 { 648 struct sched_domain *sd = container_of(rcu, struct sched_domain, rcu); 649 650 while (sd) { 651 struct sched_domain *parent = sd->parent; 652 destroy_sched_domain(sd); 653 sd = parent; 654 } 655 } 656 657 static void destroy_sched_domains(struct sched_domain *sd) 658 { 659 if (sd) 660 call_rcu(&sd->rcu, destroy_sched_domains_rcu); 661 } 662 663 /* 664 * Keep a special pointer to the highest sched_domain that has SD_SHARE_LLC set 665 * (Last Level Cache Domain) for this allows us to avoid some pointer chasing 666 * select_idle_sibling(). 667 * 668 * Also keep a unique ID per domain (we use the first CPU number in the cpumask 669 * of the domain), this allows us to quickly tell if two CPUs are in the same 670 * cache domain, see cpus_share_cache(). 671 */ 672 DEFINE_PER_CPU(struct sched_domain __rcu *, sd_llc); 673 DEFINE_PER_CPU(int, sd_llc_size); 674 DEFINE_PER_CPU(int, sd_llc_id); 675 DEFINE_PER_CPU(int, sd_share_id); 676 DEFINE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared); 677 DEFINE_PER_CPU(struct sched_domain __rcu *, sd_numa); 678 DEFINE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing); 679 DEFINE_PER_CPU(struct sched_domain __rcu *, sd_asym_cpucapacity); 680 681 DEFINE_STATIC_KEY_FALSE(sched_asym_cpucapacity); 682 DEFINE_STATIC_KEY_FALSE(sched_cluster_active); 683 684 static void update_top_cache_domain(int cpu) 685 { 686 struct sched_domain_shared *sds = NULL; 687 struct sched_domain *sd; 688 int id = cpu; 689 int size = 1; 690 691 sd = highest_flag_domain(cpu, SD_SHARE_LLC); 692 if (sd) { 693 id = cpumask_first(sched_domain_span(sd)); 694 size = cpumask_weight(sched_domain_span(sd)); 695 sds = sd->shared; 696 } 697 698 rcu_assign_pointer(per_cpu(sd_llc, cpu), sd); 699 per_cpu(sd_llc_size, cpu) = size; 700 per_cpu(sd_llc_id, cpu) = id; 701 rcu_assign_pointer(per_cpu(sd_llc_shared, cpu), sds); 702 703 sd = lowest_flag_domain(cpu, SD_CLUSTER); 704 if (sd) 705 id = cpumask_first(sched_domain_span(sd)); 706 707 /* 708 * This assignment should be placed after the sd_llc_id as 709 * we want this id equals to cluster id on cluster machines 710 * but equals to LLC id on non-Cluster machines. 711 */ 712 per_cpu(sd_share_id, cpu) = id; 713 714 sd = lowest_flag_domain(cpu, SD_NUMA); 715 rcu_assign_pointer(per_cpu(sd_numa, cpu), sd); 716 717 sd = highest_flag_domain(cpu, SD_ASYM_PACKING); 718 rcu_assign_pointer(per_cpu(sd_asym_packing, cpu), sd); 719 720 sd = lowest_flag_domain(cpu, SD_ASYM_CPUCAPACITY_FULL); 721 rcu_assign_pointer(per_cpu(sd_asym_cpucapacity, cpu), sd); 722 } 723 724 /* 725 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must 726 * hold the hotplug lock. 727 */ 728 static void 729 cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu) 730 { 731 struct rq *rq = cpu_rq(cpu); 732 struct sched_domain *tmp; 733 734 /* Remove the sched domains which do not contribute to scheduling. */ 735 for (tmp = sd; tmp; ) { 736 struct sched_domain *parent = tmp->parent; 737 if (!parent) 738 break; 739 740 if (sd_parent_degenerate(tmp, parent)) { 741 tmp->parent = parent->parent; 742 743 if (parent->parent) { 744 parent->parent->child = tmp; 745 parent->parent->groups->flags = tmp->flags; 746 } 747 748 /* 749 * Transfer SD_PREFER_SIBLING down in case of a 750 * degenerate parent; the spans match for this 751 * so the property transfers. 752 */ 753 if (parent->flags & SD_PREFER_SIBLING) 754 tmp->flags |= SD_PREFER_SIBLING; 755 destroy_sched_domain(parent); 756 } else 757 tmp = tmp->parent; 758 } 759 760 if (sd && sd_degenerate(sd)) { 761 tmp = sd; 762 sd = sd->parent; 763 destroy_sched_domain(tmp); 764 if (sd) { 765 struct sched_group *sg = sd->groups; 766 767 /* 768 * sched groups hold the flags of the child sched 769 * domain for convenience. Clear such flags since 770 * the child is being destroyed. 771 */ 772 do { 773 sg->flags = 0; 774 } while (sg != sd->groups); 775 776 sd->child = NULL; 777 } 778 } 779 780 sched_domain_debug(sd, cpu); 781 782 rq_attach_root(rq, rd); 783 tmp = rq->sd; 784 rcu_assign_pointer(rq->sd, sd); 785 dirty_sched_domain_sysctl(cpu); 786 destroy_sched_domains(tmp); 787 788 update_top_cache_domain(cpu); 789 } 790 791 struct s_data { 792 struct sched_domain * __percpu *sd; 793 struct root_domain *rd; 794 }; 795 796 enum s_alloc { 797 sa_rootdomain, 798 sa_sd, 799 sa_sd_storage, 800 sa_none, 801 }; 802 803 /* 804 * Return the canonical balance CPU for this group, this is the first CPU 805 * of this group that's also in the balance mask. 806 * 807 * The balance mask are all those CPUs that could actually end up at this 808 * group. See build_balance_mask(). 809 * 810 * Also see should_we_balance(). 811 */ 812 int group_balance_cpu(struct sched_group *sg) 813 { 814 return cpumask_first(group_balance_mask(sg)); 815 } 816 817 818 /* 819 * NUMA topology (first read the regular topology blurb below) 820 * 821 * Given a node-distance table, for example: 822 * 823 * node 0 1 2 3 824 * 0: 10 20 30 20 825 * 1: 20 10 20 30 826 * 2: 30 20 10 20 827 * 3: 20 30 20 10 828 * 829 * which represents a 4 node ring topology like: 830 * 831 * 0 ----- 1 832 * | | 833 * | | 834 * | | 835 * 3 ----- 2 836 * 837 * We want to construct domains and groups to represent this. The way we go 838 * about doing this is to build the domains on 'hops'. For each NUMA level we 839 * construct the mask of all nodes reachable in @level hops. 840 * 841 * For the above NUMA topology that gives 3 levels: 842 * 843 * NUMA-2 0-3 0-3 0-3 0-3 844 * groups: {0-1,3},{1-3} {0-2},{0,2-3} {1-3},{0-1,3} {0,2-3},{0-2} 845 * 846 * NUMA-1 0-1,3 0-2 1-3 0,2-3 847 * groups: {0},{1},{3} {0},{1},{2} {1},{2},{3} {0},{2},{3} 848 * 849 * NUMA-0 0 1 2 3 850 * 851 * 852 * As can be seen; things don't nicely line up as with the regular topology. 853 * When we iterate a domain in child domain chunks some nodes can be 854 * represented multiple times -- hence the "overlap" naming for this part of 855 * the topology. 856 * 857 * In order to minimize this overlap, we only build enough groups to cover the 858 * domain. For instance Node-0 NUMA-2 would only get groups: 0-1,3 and 1-3. 859 * 860 * Because: 861 * 862 * - the first group of each domain is its child domain; this 863 * gets us the first 0-1,3 864 * - the only uncovered node is 2, who's child domain is 1-3. 865 * 866 * However, because of the overlap, computing a unique CPU for each group is 867 * more complicated. Consider for instance the groups of NODE-1 NUMA-2, both 868 * groups include the CPUs of Node-0, while those CPUs would not in fact ever 869 * end up at those groups (they would end up in group: 0-1,3). 870 * 871 * To correct this we have to introduce the group balance mask. This mask 872 * will contain those CPUs in the group that can reach this group given the 873 * (child) domain tree. 874 * 875 * With this we can once again compute balance_cpu and sched_group_capacity 876 * relations. 877 * 878 * XXX include words on how balance_cpu is unique and therefore can be 879 * used for sched_group_capacity links. 880 * 881 * 882 * Another 'interesting' topology is: 883 * 884 * node 0 1 2 3 885 * 0: 10 20 20 30 886 * 1: 20 10 20 20 887 * 2: 20 20 10 20 888 * 3: 30 20 20 10 889 * 890 * Which looks a little like: 891 * 892 * 0 ----- 1 893 * | / | 894 * | / | 895 * | / | 896 * 2 ----- 3 897 * 898 * This topology is asymmetric, nodes 1,2 are fully connected, but nodes 0,3 899 * are not. 900 * 901 * This leads to a few particularly weird cases where the sched_domain's are 902 * not of the same number for each CPU. Consider: 903 * 904 * NUMA-2 0-3 0-3 905 * groups: {0-2},{1-3} {1-3},{0-2} 906 * 907 * NUMA-1 0-2 0-3 0-3 1-3 908 * 909 * NUMA-0 0 1 2 3 910 * 911 */ 912 913 914 /* 915 * Build the balance mask; it contains only those CPUs that can arrive at this 916 * group and should be considered to continue balancing. 917 * 918 * We do this during the group creation pass, therefore the group information 919 * isn't complete yet, however since each group represents a (child) domain we 920 * can fully construct this using the sched_domain bits (which are already 921 * complete). 922 */ 923 static void 924 build_balance_mask(struct sched_domain *sd, struct sched_group *sg, struct cpumask *mask) 925 { 926 const struct cpumask *sg_span = sched_group_span(sg); 927 struct sd_data *sdd = sd->private; 928 struct sched_domain *sibling; 929 int i; 930 931 cpumask_clear(mask); 932 933 for_each_cpu(i, sg_span) { 934 sibling = *per_cpu_ptr(sdd->sd, i); 935 936 /* 937 * Can happen in the asymmetric case, where these siblings are 938 * unused. The mask will not be empty because those CPUs that 939 * do have the top domain _should_ span the domain. 940 */ 941 if (!sibling->child) 942 continue; 943 944 /* If we would not end up here, we can't continue from here */ 945 if (!cpumask_equal(sg_span, sched_domain_span(sibling->child))) 946 continue; 947 948 cpumask_set_cpu(i, mask); 949 } 950 951 /* We must not have empty masks here */ 952 WARN_ON_ONCE(cpumask_empty(mask)); 953 } 954 955 /* 956 * XXX: This creates per-node group entries; since the load-balancer will 957 * immediately access remote memory to construct this group's load-balance 958 * statistics having the groups node local is of dubious benefit. 959 */ 960 static struct sched_group * 961 build_group_from_child_sched_domain(struct sched_domain *sd, int cpu) 962 { 963 struct sched_group *sg; 964 struct cpumask *sg_span; 965 966 sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(), 967 GFP_KERNEL, cpu_to_node(cpu)); 968 969 if (!sg) 970 return NULL; 971 972 sg_span = sched_group_span(sg); 973 if (sd->child) { 974 cpumask_copy(sg_span, sched_domain_span(sd->child)); 975 sg->flags = sd->child->flags; 976 } else { 977 cpumask_copy(sg_span, sched_domain_span(sd)); 978 } 979 980 atomic_inc(&sg->ref); 981 return sg; 982 } 983 984 static void init_overlap_sched_group(struct sched_domain *sd, 985 struct sched_group *sg) 986 { 987 struct cpumask *mask = sched_domains_tmpmask2; 988 struct sd_data *sdd = sd->private; 989 struct cpumask *sg_span; 990 int cpu; 991 992 build_balance_mask(sd, sg, mask); 993 cpu = cpumask_first(mask); 994 995 sg->sgc = *per_cpu_ptr(sdd->sgc, cpu); 996 if (atomic_inc_return(&sg->sgc->ref) == 1) 997 cpumask_copy(group_balance_mask(sg), mask); 998 else 999 WARN_ON_ONCE(!cpumask_equal(group_balance_mask(sg), mask)); 1000 1001 /* 1002 * Initialize sgc->capacity such that even if we mess up the 1003 * domains and no possible iteration will get us here, we won't 1004 * die on a /0 trap. 1005 */ 1006 sg_span = sched_group_span(sg); 1007 sg->sgc->capacity = SCHED_CAPACITY_SCALE * cpumask_weight(sg_span); 1008 sg->sgc->min_capacity = SCHED_CAPACITY_SCALE; 1009 sg->sgc->max_capacity = SCHED_CAPACITY_SCALE; 1010 } 1011 1012 static struct sched_domain * 1013 find_descended_sibling(struct sched_domain *sd, struct sched_domain *sibling) 1014 { 1015 /* 1016 * The proper descendant would be the one whose child won't span out 1017 * of sd 1018 */ 1019 while (sibling->child && 1020 !cpumask_subset(sched_domain_span(sibling->child), 1021 sched_domain_span(sd))) 1022 sibling = sibling->child; 1023 1024 /* 1025 * As we are referencing sgc across different topology level, we need 1026 * to go down to skip those sched_domains which don't contribute to 1027 * scheduling because they will be degenerated in cpu_attach_domain 1028 */ 1029 while (sibling->child && 1030 cpumask_equal(sched_domain_span(sibling->child), 1031 sched_domain_span(sibling))) 1032 sibling = sibling->child; 1033 1034 return sibling; 1035 } 1036 1037 static int 1038 build_overlap_sched_groups(struct sched_domain *sd, int cpu) 1039 { 1040 struct sched_group *first = NULL, *last = NULL, *sg; 1041 const struct cpumask *span = sched_domain_span(sd); 1042 struct cpumask *covered = sched_domains_tmpmask; 1043 struct sd_data *sdd = sd->private; 1044 struct sched_domain *sibling; 1045 int i; 1046 1047 cpumask_clear(covered); 1048 1049 for_each_cpu_wrap(i, span, cpu) { 1050 struct cpumask *sg_span; 1051 1052 if (cpumask_test_cpu(i, covered)) 1053 continue; 1054 1055 sibling = *per_cpu_ptr(sdd->sd, i); 1056 1057 /* 1058 * Asymmetric node setups can result in situations where the 1059 * domain tree is of unequal depth, make sure to skip domains 1060 * that already cover the entire range. 1061 * 1062 * In that case build_sched_domains() will have terminated the 1063 * iteration early and our sibling sd spans will be empty. 1064 * Domains should always include the CPU they're built on, so 1065 * check that. 1066 */ 1067 if (!cpumask_test_cpu(i, sched_domain_span(sibling))) 1068 continue; 1069 1070 /* 1071 * Usually we build sched_group by sibling's child sched_domain 1072 * But for machines whose NUMA diameter are 3 or above, we move 1073 * to build sched_group by sibling's proper descendant's child 1074 * domain because sibling's child sched_domain will span out of 1075 * the sched_domain being built as below. 1076 * 1077 * Smallest diameter=3 topology is: 1078 * 1079 * node 0 1 2 3 1080 * 0: 10 20 30 40 1081 * 1: 20 10 20 30 1082 * 2: 30 20 10 20 1083 * 3: 40 30 20 10 1084 * 1085 * 0 --- 1 --- 2 --- 3 1086 * 1087 * NUMA-3 0-3 N/A N/A 0-3 1088 * groups: {0-2},{1-3} {1-3},{0-2} 1089 * 1090 * NUMA-2 0-2 0-3 0-3 1-3 1091 * groups: {0-1},{1-3} {0-2},{2-3} {1-3},{0-1} {2-3},{0-2} 1092 * 1093 * NUMA-1 0-1 0-2 1-3 2-3 1094 * groups: {0},{1} {1},{2},{0} {2},{3},{1} {3},{2} 1095 * 1096 * NUMA-0 0 1 2 3 1097 * 1098 * The NUMA-2 groups for nodes 0 and 3 are obviously buggered, as the 1099 * group span isn't a subset of the domain span. 1100 */ 1101 if (sibling->child && 1102 !cpumask_subset(sched_domain_span(sibling->child), span)) 1103 sibling = find_descended_sibling(sd, sibling); 1104 1105 sg = build_group_from_child_sched_domain(sibling, cpu); 1106 if (!sg) 1107 goto fail; 1108 1109 sg_span = sched_group_span(sg); 1110 cpumask_or(covered, covered, sg_span); 1111 1112 init_overlap_sched_group(sibling, sg); 1113 1114 if (!first) 1115 first = sg; 1116 if (last) 1117 last->next = sg; 1118 last = sg; 1119 last->next = first; 1120 } 1121 sd->groups = first; 1122 1123 return 0; 1124 1125 fail: 1126 free_sched_groups(first, 0); 1127 1128 return -ENOMEM; 1129 } 1130 1131 1132 /* 1133 * Package topology (also see the load-balance blurb in fair.c) 1134 * 1135 * The scheduler builds a tree structure to represent a number of important 1136 * topology features. By default (default_topology[]) these include: 1137 * 1138 * - Simultaneous multithreading (SMT) 1139 * - Multi-Core Cache (MC) 1140 * - Package (PKG) 1141 * 1142 * Where the last one more or less denotes everything up to a NUMA node. 1143 * 1144 * The tree consists of 3 primary data structures: 1145 * 1146 * sched_domain -> sched_group -> sched_group_capacity 1147 * ^ ^ ^ ^ 1148 * `-' `-' 1149 * 1150 * The sched_domains are per-CPU and have a two way link (parent & child) and 1151 * denote the ever growing mask of CPUs belonging to that level of topology. 1152 * 1153 * Each sched_domain has a circular (double) linked list of sched_group's, each 1154 * denoting the domains of the level below (or individual CPUs in case of the 1155 * first domain level). The sched_group linked by a sched_domain includes the 1156 * CPU of that sched_domain [*]. 1157 * 1158 * Take for instance a 2 threaded, 2 core, 2 cache cluster part: 1159 * 1160 * CPU 0 1 2 3 4 5 6 7 1161 * 1162 * PKG [ ] 1163 * MC [ ] [ ] 1164 * SMT [ ] [ ] [ ] [ ] 1165 * 1166 * - or - 1167 * 1168 * PKG 0-7 0-7 0-7 0-7 0-7 0-7 0-7 0-7 1169 * MC 0-3 0-3 0-3 0-3 4-7 4-7 4-7 4-7 1170 * SMT 0-1 0-1 2-3 2-3 4-5 4-5 6-7 6-7 1171 * 1172 * CPU 0 1 2 3 4 5 6 7 1173 * 1174 * One way to think about it is: sched_domain moves you up and down among these 1175 * topology levels, while sched_group moves you sideways through it, at child 1176 * domain granularity. 1177 * 1178 * sched_group_capacity ensures each unique sched_group has shared storage. 1179 * 1180 * There are two related construction problems, both require a CPU that 1181 * uniquely identify each group (for a given domain): 1182 * 1183 * - The first is the balance_cpu (see should_we_balance() and the 1184 * load-balance blurb in fair.c); for each group we only want 1 CPU to 1185 * continue balancing at a higher domain. 1186 * 1187 * - The second is the sched_group_capacity; we want all identical groups 1188 * to share a single sched_group_capacity. 1189 * 1190 * Since these topologies are exclusive by construction. That is, its 1191 * impossible for an SMT thread to belong to multiple cores, and cores to 1192 * be part of multiple caches. There is a very clear and unique location 1193 * for each CPU in the hierarchy. 1194 * 1195 * Therefore computing a unique CPU for each group is trivial (the iteration 1196 * mask is redundant and set all 1s; all CPUs in a group will end up at _that_ 1197 * group), we can simply pick the first CPU in each group. 1198 * 1199 * 1200 * [*] in other words, the first group of each domain is its child domain. 1201 */ 1202 1203 static struct sched_group *get_group(int cpu, struct sd_data *sdd) 1204 { 1205 struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu); 1206 struct sched_domain *child = sd->child; 1207 struct sched_group *sg; 1208 bool already_visited; 1209 1210 if (child) 1211 cpu = cpumask_first(sched_domain_span(child)); 1212 1213 sg = *per_cpu_ptr(sdd->sg, cpu); 1214 sg->sgc = *per_cpu_ptr(sdd->sgc, cpu); 1215 1216 /* Increase refcounts for claim_allocations: */ 1217 already_visited = atomic_inc_return(&sg->ref) > 1; 1218 /* sgc visits should follow a similar trend as sg */ 1219 WARN_ON(already_visited != (atomic_inc_return(&sg->sgc->ref) > 1)); 1220 1221 /* If we have already visited that group, it's already initialized. */ 1222 if (already_visited) 1223 return sg; 1224 1225 if (child) { 1226 cpumask_copy(sched_group_span(sg), sched_domain_span(child)); 1227 cpumask_copy(group_balance_mask(sg), sched_group_span(sg)); 1228 sg->flags = child->flags; 1229 } else { 1230 cpumask_set_cpu(cpu, sched_group_span(sg)); 1231 cpumask_set_cpu(cpu, group_balance_mask(sg)); 1232 } 1233 1234 sg->sgc->capacity = SCHED_CAPACITY_SCALE * cpumask_weight(sched_group_span(sg)); 1235 sg->sgc->min_capacity = SCHED_CAPACITY_SCALE; 1236 sg->sgc->max_capacity = SCHED_CAPACITY_SCALE; 1237 1238 return sg; 1239 } 1240 1241 /* 1242 * build_sched_groups will build a circular linked list of the groups 1243 * covered by the given span, will set each group's ->cpumask correctly, 1244 * and will initialize their ->sgc. 1245 * 1246 * Assumes the sched_domain tree is fully constructed 1247 */ 1248 static int 1249 build_sched_groups(struct sched_domain *sd, int cpu) 1250 { 1251 struct sched_group *first = NULL, *last = NULL; 1252 struct sd_data *sdd = sd->private; 1253 const struct cpumask *span = sched_domain_span(sd); 1254 struct cpumask *covered; 1255 int i; 1256 1257 lockdep_assert_held(&sched_domains_mutex); 1258 covered = sched_domains_tmpmask; 1259 1260 cpumask_clear(covered); 1261 1262 for_each_cpu_wrap(i, span, cpu) { 1263 struct sched_group *sg; 1264 1265 if (cpumask_test_cpu(i, covered)) 1266 continue; 1267 1268 sg = get_group(i, sdd); 1269 1270 cpumask_or(covered, covered, sched_group_span(sg)); 1271 1272 if (!first) 1273 first = sg; 1274 if (last) 1275 last->next = sg; 1276 last = sg; 1277 } 1278 last->next = first; 1279 sd->groups = first; 1280 1281 return 0; 1282 } 1283 1284 /* 1285 * Initialize sched groups cpu_capacity. 1286 * 1287 * cpu_capacity indicates the capacity of sched group, which is used while 1288 * distributing the load between different sched groups in a sched domain. 1289 * Typically cpu_capacity for all the groups in a sched domain will be same 1290 * unless there are asymmetries in the topology. If there are asymmetries, 1291 * group having more cpu_capacity will pickup more load compared to the 1292 * group having less cpu_capacity. 1293 */ 1294 static void init_sched_groups_capacity(int cpu, struct sched_domain *sd) 1295 { 1296 struct sched_group *sg = sd->groups; 1297 struct cpumask *mask = sched_domains_tmpmask2; 1298 1299 WARN_ON(!sg); 1300 1301 do { 1302 int cpu, cores = 0, max_cpu = -1; 1303 1304 sg->group_weight = cpumask_weight(sched_group_span(sg)); 1305 1306 cpumask_copy(mask, sched_group_span(sg)); 1307 for_each_cpu(cpu, mask) { 1308 cores++; 1309 #ifdef CONFIG_SCHED_SMT 1310 cpumask_andnot(mask, mask, cpu_smt_mask(cpu)); 1311 #endif 1312 } 1313 sg->cores = cores; 1314 1315 if (!(sd->flags & SD_ASYM_PACKING)) 1316 goto next; 1317 1318 for_each_cpu(cpu, sched_group_span(sg)) { 1319 if (max_cpu < 0) 1320 max_cpu = cpu; 1321 else if (sched_asym_prefer(cpu, max_cpu)) 1322 max_cpu = cpu; 1323 } 1324 sg->asym_prefer_cpu = max_cpu; 1325 1326 next: 1327 sg = sg->next; 1328 } while (sg != sd->groups); 1329 1330 if (cpu != group_balance_cpu(sg)) 1331 return; 1332 1333 update_group_capacity(sd, cpu); 1334 } 1335 1336 #ifdef CONFIG_SMP 1337 1338 /* Update the "asym_prefer_cpu" when arch_asym_cpu_priority() changes. */ 1339 void sched_update_asym_prefer_cpu(int cpu, int old_prio, int new_prio) 1340 { 1341 int asym_prefer_cpu = cpu; 1342 struct sched_domain *sd; 1343 1344 guard(rcu)(); 1345 1346 for_each_domain(cpu, sd) { 1347 struct sched_group *sg; 1348 int group_cpu; 1349 1350 if (!(sd->flags & SD_ASYM_PACKING)) 1351 continue; 1352 1353 /* 1354 * Groups of overlapping domain are replicated per NUMA 1355 * node and will require updating "asym_prefer_cpu" on 1356 * each local copy. 1357 * 1358 * If you are hitting this warning, consider moving 1359 * "sg->asym_prefer_cpu" to "sg->sgc->asym_prefer_cpu" 1360 * which is shared by all the overlapping groups. 1361 */ 1362 WARN_ON_ONCE(sd->flags & SD_OVERLAP); 1363 1364 sg = sd->groups; 1365 if (cpu != sg->asym_prefer_cpu) { 1366 /* 1367 * Since the parent is a superset of the current group, 1368 * if the cpu is not the "asym_prefer_cpu" at the 1369 * current level, it cannot be the preferred CPU at a 1370 * higher levels either. 1371 */ 1372 if (!sched_asym_prefer(cpu, sg->asym_prefer_cpu)) 1373 return; 1374 1375 WRITE_ONCE(sg->asym_prefer_cpu, cpu); 1376 continue; 1377 } 1378 1379 /* Ranking has improved; CPU is still the preferred one. */ 1380 if (new_prio >= old_prio) 1381 continue; 1382 1383 for_each_cpu(group_cpu, sched_group_span(sg)) { 1384 if (sched_asym_prefer(group_cpu, asym_prefer_cpu)) 1385 asym_prefer_cpu = group_cpu; 1386 } 1387 1388 WRITE_ONCE(sg->asym_prefer_cpu, asym_prefer_cpu); 1389 } 1390 } 1391 1392 #endif /* CONFIG_SMP */ 1393 1394 /* 1395 * Set of available CPUs grouped by their corresponding capacities 1396 * Each list entry contains a CPU mask reflecting CPUs that share the same 1397 * capacity. 1398 * The lifespan of data is unlimited. 1399 */ 1400 LIST_HEAD(asym_cap_list); 1401 1402 /* 1403 * Verify whether there is any CPU capacity asymmetry in a given sched domain. 1404 * Provides sd_flags reflecting the asymmetry scope. 1405 */ 1406 static inline int 1407 asym_cpu_capacity_classify(const struct cpumask *sd_span, 1408 const struct cpumask *cpu_map) 1409 { 1410 struct asym_cap_data *entry; 1411 int count = 0, miss = 0; 1412 1413 /* 1414 * Count how many unique CPU capacities this domain spans across 1415 * (compare sched_domain CPUs mask with ones representing available 1416 * CPUs capacities). Take into account CPUs that might be offline: 1417 * skip those. 1418 */ 1419 list_for_each_entry(entry, &asym_cap_list, link) { 1420 if (cpumask_intersects(sd_span, cpu_capacity_span(entry))) 1421 ++count; 1422 else if (cpumask_intersects(cpu_map, cpu_capacity_span(entry))) 1423 ++miss; 1424 } 1425 1426 WARN_ON_ONCE(!count && !list_empty(&asym_cap_list)); 1427 1428 /* No asymmetry detected */ 1429 if (count < 2) 1430 return 0; 1431 /* Some of the available CPU capacity values have not been detected */ 1432 if (miss) 1433 return SD_ASYM_CPUCAPACITY; 1434 1435 /* Full asymmetry */ 1436 return SD_ASYM_CPUCAPACITY | SD_ASYM_CPUCAPACITY_FULL; 1437 1438 } 1439 1440 static void free_asym_cap_entry(struct rcu_head *head) 1441 { 1442 struct asym_cap_data *entry = container_of(head, struct asym_cap_data, rcu); 1443 kfree(entry); 1444 } 1445 1446 static inline void asym_cpu_capacity_update_data(int cpu) 1447 { 1448 unsigned long capacity = arch_scale_cpu_capacity(cpu); 1449 struct asym_cap_data *insert_entry = NULL; 1450 struct asym_cap_data *entry; 1451 1452 /* 1453 * Search if capacity already exits. If not, track which the entry 1454 * where we should insert to keep the list ordered descending. 1455 */ 1456 list_for_each_entry(entry, &asym_cap_list, link) { 1457 if (capacity == entry->capacity) 1458 goto done; 1459 else if (!insert_entry && capacity > entry->capacity) 1460 insert_entry = list_prev_entry(entry, link); 1461 } 1462 1463 entry = kzalloc(sizeof(*entry) + cpumask_size(), GFP_KERNEL); 1464 if (WARN_ONCE(!entry, "Failed to allocate memory for asymmetry data\n")) 1465 return; 1466 entry->capacity = capacity; 1467 1468 /* If NULL then the new capacity is the smallest, add last. */ 1469 if (!insert_entry) 1470 list_add_tail_rcu(&entry->link, &asym_cap_list); 1471 else 1472 list_add_rcu(&entry->link, &insert_entry->link); 1473 done: 1474 __cpumask_set_cpu(cpu, cpu_capacity_span(entry)); 1475 } 1476 1477 /* 1478 * Build-up/update list of CPUs grouped by their capacities 1479 * An update requires explicit request to rebuild sched domains 1480 * with state indicating CPU topology changes. 1481 */ 1482 static void asym_cpu_capacity_scan(void) 1483 { 1484 struct asym_cap_data *entry, *next; 1485 int cpu; 1486 1487 list_for_each_entry(entry, &asym_cap_list, link) 1488 cpumask_clear(cpu_capacity_span(entry)); 1489 1490 for_each_cpu_and(cpu, cpu_possible_mask, housekeeping_cpumask(HK_TYPE_DOMAIN)) 1491 asym_cpu_capacity_update_data(cpu); 1492 1493 list_for_each_entry_safe(entry, next, &asym_cap_list, link) { 1494 if (cpumask_empty(cpu_capacity_span(entry))) { 1495 list_del_rcu(&entry->link); 1496 call_rcu(&entry->rcu, free_asym_cap_entry); 1497 } 1498 } 1499 1500 /* 1501 * Only one capacity value has been detected i.e. this system is symmetric. 1502 * No need to keep this data around. 1503 */ 1504 if (list_is_singular(&asym_cap_list)) { 1505 entry = list_first_entry(&asym_cap_list, typeof(*entry), link); 1506 list_del_rcu(&entry->link); 1507 call_rcu(&entry->rcu, free_asym_cap_entry); 1508 } 1509 } 1510 1511 /* 1512 * Initializers for schedule domains 1513 * Non-inlined to reduce accumulated stack pressure in build_sched_domains() 1514 */ 1515 1516 static int default_relax_domain_level = -1; 1517 int sched_domain_level_max; 1518 1519 static int __init setup_relax_domain_level(char *str) 1520 { 1521 if (kstrtoint(str, 0, &default_relax_domain_level)) 1522 pr_warn("Unable to set relax_domain_level\n"); 1523 1524 return 1; 1525 } 1526 __setup("relax_domain_level=", setup_relax_domain_level); 1527 1528 static void set_domain_attribute(struct sched_domain *sd, 1529 struct sched_domain_attr *attr) 1530 { 1531 int request; 1532 1533 if (!attr || attr->relax_domain_level < 0) { 1534 if (default_relax_domain_level < 0) 1535 return; 1536 request = default_relax_domain_level; 1537 } else 1538 request = attr->relax_domain_level; 1539 1540 if (sd->level >= request) { 1541 /* Turn off idle balance on this domain: */ 1542 sd->flags &= ~(SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE); 1543 } 1544 } 1545 1546 static void __sdt_free(const struct cpumask *cpu_map); 1547 static int __sdt_alloc(const struct cpumask *cpu_map); 1548 1549 static void __free_domain_allocs(struct s_data *d, enum s_alloc what, 1550 const struct cpumask *cpu_map) 1551 { 1552 switch (what) { 1553 case sa_rootdomain: 1554 if (!atomic_read(&d->rd->refcount)) 1555 free_rootdomain(&d->rd->rcu); 1556 fallthrough; 1557 case sa_sd: 1558 free_percpu(d->sd); 1559 fallthrough; 1560 case sa_sd_storage: 1561 __sdt_free(cpu_map); 1562 fallthrough; 1563 case sa_none: 1564 break; 1565 } 1566 } 1567 1568 static enum s_alloc 1569 __visit_domain_allocation_hell(struct s_data *d, const struct cpumask *cpu_map) 1570 { 1571 memset(d, 0, sizeof(*d)); 1572 1573 if (__sdt_alloc(cpu_map)) 1574 return sa_sd_storage; 1575 d->sd = alloc_percpu(struct sched_domain *); 1576 if (!d->sd) 1577 return sa_sd_storage; 1578 d->rd = alloc_rootdomain(); 1579 if (!d->rd) 1580 return sa_sd; 1581 1582 return sa_rootdomain; 1583 } 1584 1585 /* 1586 * NULL the sd_data elements we've used to build the sched_domain and 1587 * sched_group structure so that the subsequent __free_domain_allocs() 1588 * will not free the data we're using. 1589 */ 1590 static void claim_allocations(int cpu, struct sched_domain *sd) 1591 { 1592 struct sd_data *sdd = sd->private; 1593 1594 WARN_ON_ONCE(*per_cpu_ptr(sdd->sd, cpu) != sd); 1595 *per_cpu_ptr(sdd->sd, cpu) = NULL; 1596 1597 if (atomic_read(&(*per_cpu_ptr(sdd->sds, cpu))->ref)) 1598 *per_cpu_ptr(sdd->sds, cpu) = NULL; 1599 1600 if (atomic_read(&(*per_cpu_ptr(sdd->sg, cpu))->ref)) 1601 *per_cpu_ptr(sdd->sg, cpu) = NULL; 1602 1603 if (atomic_read(&(*per_cpu_ptr(sdd->sgc, cpu))->ref)) 1604 *per_cpu_ptr(sdd->sgc, cpu) = NULL; 1605 } 1606 1607 #ifdef CONFIG_NUMA 1608 enum numa_topology_type sched_numa_topology_type; 1609 1610 static int sched_domains_numa_levels; 1611 static int sched_domains_curr_level; 1612 1613 int sched_max_numa_distance; 1614 static int *sched_domains_numa_distance; 1615 static struct cpumask ***sched_domains_numa_masks; 1616 #endif 1617 1618 /* 1619 * SD_flags allowed in topology descriptions. 1620 * 1621 * These flags are purely descriptive of the topology and do not prescribe 1622 * behaviour. Behaviour is artificial and mapped in the below sd_init() 1623 * function. For details, see include/linux/sched/sd_flags.h. 1624 * 1625 * SD_SHARE_CPUCAPACITY 1626 * SD_SHARE_LLC 1627 * SD_CLUSTER 1628 * SD_NUMA 1629 * 1630 * Odd one out, which beside describing the topology has a quirk also 1631 * prescribes the desired behaviour that goes along with it: 1632 * 1633 * SD_ASYM_PACKING - describes SMT quirks 1634 */ 1635 #define TOPOLOGY_SD_FLAGS \ 1636 (SD_SHARE_CPUCAPACITY | \ 1637 SD_CLUSTER | \ 1638 SD_SHARE_LLC | \ 1639 SD_NUMA | \ 1640 SD_ASYM_PACKING) 1641 1642 static struct sched_domain * 1643 sd_init(struct sched_domain_topology_level *tl, 1644 const struct cpumask *cpu_map, 1645 struct sched_domain *child, int cpu) 1646 { 1647 struct sd_data *sdd = &tl->data; 1648 struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu); 1649 int sd_id, sd_weight, sd_flags = 0; 1650 struct cpumask *sd_span; 1651 1652 #ifdef CONFIG_NUMA 1653 /* 1654 * Ugly hack to pass state to sd_numa_mask()... 1655 */ 1656 sched_domains_curr_level = tl->numa_level; 1657 #endif 1658 1659 sd_weight = cpumask_weight(tl->mask(cpu)); 1660 1661 if (tl->sd_flags) 1662 sd_flags = (*tl->sd_flags)(); 1663 if (WARN_ONCE(sd_flags & ~TOPOLOGY_SD_FLAGS, 1664 "wrong sd_flags in topology description\n")) 1665 sd_flags &= TOPOLOGY_SD_FLAGS; 1666 1667 *sd = (struct sched_domain){ 1668 .min_interval = sd_weight, 1669 .max_interval = 2*sd_weight, 1670 .busy_factor = 16, 1671 .imbalance_pct = 117, 1672 1673 .cache_nice_tries = 0, 1674 1675 .flags = 1*SD_BALANCE_NEWIDLE 1676 | 1*SD_BALANCE_EXEC 1677 | 1*SD_BALANCE_FORK 1678 | 0*SD_BALANCE_WAKE 1679 | 1*SD_WAKE_AFFINE 1680 | 0*SD_SHARE_CPUCAPACITY 1681 | 0*SD_SHARE_LLC 1682 | 0*SD_SERIALIZE 1683 | 1*SD_PREFER_SIBLING 1684 | 0*SD_NUMA 1685 | sd_flags 1686 , 1687 1688 .last_balance = jiffies, 1689 .balance_interval = sd_weight, 1690 .max_newidle_lb_cost = 0, 1691 .last_decay_max_lb_cost = jiffies, 1692 .child = child, 1693 .name = tl->name, 1694 }; 1695 1696 sd_span = sched_domain_span(sd); 1697 cpumask_and(sd_span, cpu_map, tl->mask(cpu)); 1698 sd_id = cpumask_first(sd_span); 1699 1700 sd->flags |= asym_cpu_capacity_classify(sd_span, cpu_map); 1701 1702 WARN_ONCE((sd->flags & (SD_SHARE_CPUCAPACITY | SD_ASYM_CPUCAPACITY)) == 1703 (SD_SHARE_CPUCAPACITY | SD_ASYM_CPUCAPACITY), 1704 "CPU capacity asymmetry not supported on SMT\n"); 1705 1706 /* 1707 * Convert topological properties into behaviour. 1708 */ 1709 /* Don't attempt to spread across CPUs of different capacities. */ 1710 if ((sd->flags & SD_ASYM_CPUCAPACITY) && sd->child) 1711 sd->child->flags &= ~SD_PREFER_SIBLING; 1712 1713 if (sd->flags & SD_SHARE_CPUCAPACITY) { 1714 sd->imbalance_pct = 110; 1715 1716 } else if (sd->flags & SD_SHARE_LLC) { 1717 sd->imbalance_pct = 117; 1718 sd->cache_nice_tries = 1; 1719 1720 #ifdef CONFIG_NUMA 1721 } else if (sd->flags & SD_NUMA) { 1722 sd->cache_nice_tries = 2; 1723 1724 sd->flags &= ~SD_PREFER_SIBLING; 1725 sd->flags |= SD_SERIALIZE; 1726 if (sched_domains_numa_distance[tl->numa_level] > node_reclaim_distance) { 1727 sd->flags &= ~(SD_BALANCE_EXEC | 1728 SD_BALANCE_FORK | 1729 SD_WAKE_AFFINE); 1730 } 1731 1732 #endif 1733 } else { 1734 sd->cache_nice_tries = 1; 1735 } 1736 1737 /* 1738 * For all levels sharing cache; connect a sched_domain_shared 1739 * instance. 1740 */ 1741 if (sd->flags & SD_SHARE_LLC) { 1742 sd->shared = *per_cpu_ptr(sdd->sds, sd_id); 1743 atomic_inc(&sd->shared->ref); 1744 atomic_set(&sd->shared->nr_busy_cpus, sd_weight); 1745 } 1746 1747 sd->private = sdd; 1748 1749 return sd; 1750 } 1751 1752 /* 1753 * Topology list, bottom-up. 1754 */ 1755 static struct sched_domain_topology_level default_topology[] = { 1756 #ifdef CONFIG_SCHED_SMT 1757 { cpu_smt_mask, cpu_smt_flags, SD_INIT_NAME(SMT) }, 1758 #endif 1759 1760 #ifdef CONFIG_SCHED_CLUSTER 1761 { cpu_clustergroup_mask, cpu_cluster_flags, SD_INIT_NAME(CLS) }, 1762 #endif 1763 1764 #ifdef CONFIG_SCHED_MC 1765 { cpu_coregroup_mask, cpu_core_flags, SD_INIT_NAME(MC) }, 1766 #endif 1767 { cpu_cpu_mask, SD_INIT_NAME(PKG) }, 1768 { NULL, }, 1769 }; 1770 1771 static struct sched_domain_topology_level *sched_domain_topology = 1772 default_topology; 1773 static struct sched_domain_topology_level *sched_domain_topology_saved; 1774 1775 #define for_each_sd_topology(tl) \ 1776 for (tl = sched_domain_topology; tl->mask; tl++) 1777 1778 void __init set_sched_topology(struct sched_domain_topology_level *tl) 1779 { 1780 if (WARN_ON_ONCE(sched_smp_initialized)) 1781 return; 1782 1783 sched_domain_topology = tl; 1784 sched_domain_topology_saved = NULL; 1785 } 1786 1787 #ifdef CONFIG_NUMA 1788 1789 static const struct cpumask *sd_numa_mask(int cpu) 1790 { 1791 return sched_domains_numa_masks[sched_domains_curr_level][cpu_to_node(cpu)]; 1792 } 1793 1794 static void sched_numa_warn(const char *str) 1795 { 1796 static int done = false; 1797 int i,j; 1798 1799 if (done) 1800 return; 1801 1802 done = true; 1803 1804 printk(KERN_WARNING "ERROR: %s\n\n", str); 1805 1806 for (i = 0; i < nr_node_ids; i++) { 1807 printk(KERN_WARNING " "); 1808 for (j = 0; j < nr_node_ids; j++) { 1809 if (!node_state(i, N_CPU) || !node_state(j, N_CPU)) 1810 printk(KERN_CONT "(%02d) ", node_distance(i,j)); 1811 else 1812 printk(KERN_CONT " %02d ", node_distance(i,j)); 1813 } 1814 printk(KERN_CONT "\n"); 1815 } 1816 printk(KERN_WARNING "\n"); 1817 } 1818 1819 bool find_numa_distance(int distance) 1820 { 1821 bool found = false; 1822 int i, *distances; 1823 1824 if (distance == node_distance(0, 0)) 1825 return true; 1826 1827 rcu_read_lock(); 1828 distances = rcu_dereference(sched_domains_numa_distance); 1829 if (!distances) 1830 goto unlock; 1831 for (i = 0; i < sched_domains_numa_levels; i++) { 1832 if (distances[i] == distance) { 1833 found = true; 1834 break; 1835 } 1836 } 1837 unlock: 1838 rcu_read_unlock(); 1839 1840 return found; 1841 } 1842 1843 #define for_each_cpu_node_but(n, nbut) \ 1844 for_each_node_state(n, N_CPU) \ 1845 if (n == nbut) \ 1846 continue; \ 1847 else 1848 1849 /* 1850 * A system can have three types of NUMA topology: 1851 * NUMA_DIRECT: all nodes are directly connected, or not a NUMA system 1852 * NUMA_GLUELESS_MESH: some nodes reachable through intermediary nodes 1853 * NUMA_BACKPLANE: nodes can reach other nodes through a backplane 1854 * 1855 * The difference between a glueless mesh topology and a backplane 1856 * topology lies in whether communication between not directly 1857 * connected nodes goes through intermediary nodes (where programs 1858 * could run), or through backplane controllers. This affects 1859 * placement of programs. 1860 * 1861 * The type of topology can be discerned with the following tests: 1862 * - If the maximum distance between any nodes is 1 hop, the system 1863 * is directly connected. 1864 * - If for two nodes A and B, located N > 1 hops away from each other, 1865 * there is an intermediary node C, which is < N hops away from both 1866 * nodes A and B, the system is a glueless mesh. 1867 */ 1868 static void init_numa_topology_type(int offline_node) 1869 { 1870 int a, b, c, n; 1871 1872 n = sched_max_numa_distance; 1873 1874 if (sched_domains_numa_levels <= 2) { 1875 sched_numa_topology_type = NUMA_DIRECT; 1876 return; 1877 } 1878 1879 for_each_cpu_node_but(a, offline_node) { 1880 for_each_cpu_node_but(b, offline_node) { 1881 /* Find two nodes furthest removed from each other. */ 1882 if (node_distance(a, b) < n) 1883 continue; 1884 1885 /* Is there an intermediary node between a and b? */ 1886 for_each_cpu_node_but(c, offline_node) { 1887 if (node_distance(a, c) < n && 1888 node_distance(b, c) < n) { 1889 sched_numa_topology_type = 1890 NUMA_GLUELESS_MESH; 1891 return; 1892 } 1893 } 1894 1895 sched_numa_topology_type = NUMA_BACKPLANE; 1896 return; 1897 } 1898 } 1899 1900 pr_err("Failed to find a NUMA topology type, defaulting to DIRECT\n"); 1901 sched_numa_topology_type = NUMA_DIRECT; 1902 } 1903 1904 1905 #define NR_DISTANCE_VALUES (1 << DISTANCE_BITS) 1906 1907 void sched_init_numa(int offline_node) 1908 { 1909 struct sched_domain_topology_level *tl; 1910 unsigned long *distance_map; 1911 int nr_levels = 0; 1912 int i, j; 1913 int *distances; 1914 struct cpumask ***masks; 1915 1916 /* 1917 * O(nr_nodes^2) de-duplicating selection sort -- in order to find the 1918 * unique distances in the node_distance() table. 1919 */ 1920 distance_map = bitmap_alloc(NR_DISTANCE_VALUES, GFP_KERNEL); 1921 if (!distance_map) 1922 return; 1923 1924 bitmap_zero(distance_map, NR_DISTANCE_VALUES); 1925 for_each_cpu_node_but(i, offline_node) { 1926 for_each_cpu_node_but(j, offline_node) { 1927 int distance = node_distance(i, j); 1928 1929 if (distance < LOCAL_DISTANCE || distance >= NR_DISTANCE_VALUES) { 1930 sched_numa_warn("Invalid distance value range"); 1931 bitmap_free(distance_map); 1932 return; 1933 } 1934 1935 bitmap_set(distance_map, distance, 1); 1936 } 1937 } 1938 /* 1939 * We can now figure out how many unique distance values there are and 1940 * allocate memory accordingly. 1941 */ 1942 nr_levels = bitmap_weight(distance_map, NR_DISTANCE_VALUES); 1943 1944 distances = kcalloc(nr_levels, sizeof(int), GFP_KERNEL); 1945 if (!distances) { 1946 bitmap_free(distance_map); 1947 return; 1948 } 1949 1950 for (i = 0, j = 0; i < nr_levels; i++, j++) { 1951 j = find_next_bit(distance_map, NR_DISTANCE_VALUES, j); 1952 distances[i] = j; 1953 } 1954 rcu_assign_pointer(sched_domains_numa_distance, distances); 1955 1956 bitmap_free(distance_map); 1957 1958 /* 1959 * 'nr_levels' contains the number of unique distances 1960 * 1961 * The sched_domains_numa_distance[] array includes the actual distance 1962 * numbers. 1963 */ 1964 1965 /* 1966 * Here, we should temporarily reset sched_domains_numa_levels to 0. 1967 * If it fails to allocate memory for array sched_domains_numa_masks[][], 1968 * the array will contain less then 'nr_levels' members. This could be 1969 * dangerous when we use it to iterate array sched_domains_numa_masks[][] 1970 * in other functions. 1971 * 1972 * We reset it to 'nr_levels' at the end of this function. 1973 */ 1974 sched_domains_numa_levels = 0; 1975 1976 masks = kzalloc(sizeof(void *) * nr_levels, GFP_KERNEL); 1977 if (!masks) 1978 return; 1979 1980 /* 1981 * Now for each level, construct a mask per node which contains all 1982 * CPUs of nodes that are that many hops away from us. 1983 */ 1984 for (i = 0; i < nr_levels; i++) { 1985 masks[i] = kzalloc(nr_node_ids * sizeof(void *), GFP_KERNEL); 1986 if (!masks[i]) 1987 return; 1988 1989 for_each_cpu_node_but(j, offline_node) { 1990 struct cpumask *mask = kzalloc(cpumask_size(), GFP_KERNEL); 1991 int k; 1992 1993 if (!mask) 1994 return; 1995 1996 masks[i][j] = mask; 1997 1998 for_each_cpu_node_but(k, offline_node) { 1999 if (sched_debug() && (node_distance(j, k) != node_distance(k, j))) 2000 sched_numa_warn("Node-distance not symmetric"); 2001 2002 if (node_distance(j, k) > sched_domains_numa_distance[i]) 2003 continue; 2004 2005 cpumask_or(mask, mask, cpumask_of_node(k)); 2006 } 2007 } 2008 } 2009 rcu_assign_pointer(sched_domains_numa_masks, masks); 2010 2011 /* Compute default topology size */ 2012 for (i = 0; sched_domain_topology[i].mask; i++); 2013 2014 tl = kzalloc((i + nr_levels + 1) * 2015 sizeof(struct sched_domain_topology_level), GFP_KERNEL); 2016 if (!tl) 2017 return; 2018 2019 /* 2020 * Copy the default topology bits.. 2021 */ 2022 for (i = 0; sched_domain_topology[i].mask; i++) 2023 tl[i] = sched_domain_topology[i]; 2024 2025 /* 2026 * Add the NUMA identity distance, aka single NODE. 2027 */ 2028 tl[i++] = (struct sched_domain_topology_level){ 2029 .mask = sd_numa_mask, 2030 .numa_level = 0, 2031 SD_INIT_NAME(NODE) 2032 }; 2033 2034 /* 2035 * .. and append 'j' levels of NUMA goodness. 2036 */ 2037 for (j = 1; j < nr_levels; i++, j++) { 2038 tl[i] = (struct sched_domain_topology_level){ 2039 .mask = sd_numa_mask, 2040 .sd_flags = cpu_numa_flags, 2041 .flags = SDTL_OVERLAP, 2042 .numa_level = j, 2043 SD_INIT_NAME(NUMA) 2044 }; 2045 } 2046 2047 sched_domain_topology_saved = sched_domain_topology; 2048 sched_domain_topology = tl; 2049 2050 sched_domains_numa_levels = nr_levels; 2051 WRITE_ONCE(sched_max_numa_distance, sched_domains_numa_distance[nr_levels - 1]); 2052 2053 init_numa_topology_type(offline_node); 2054 } 2055 2056 2057 static void sched_reset_numa(void) 2058 { 2059 int nr_levels, *distances; 2060 struct cpumask ***masks; 2061 2062 nr_levels = sched_domains_numa_levels; 2063 sched_domains_numa_levels = 0; 2064 sched_max_numa_distance = 0; 2065 sched_numa_topology_type = NUMA_DIRECT; 2066 distances = sched_domains_numa_distance; 2067 rcu_assign_pointer(sched_domains_numa_distance, NULL); 2068 masks = sched_domains_numa_masks; 2069 rcu_assign_pointer(sched_domains_numa_masks, NULL); 2070 if (distances || masks) { 2071 int i, j; 2072 2073 synchronize_rcu(); 2074 kfree(distances); 2075 for (i = 0; i < nr_levels && masks; i++) { 2076 if (!masks[i]) 2077 continue; 2078 for_each_node(j) 2079 kfree(masks[i][j]); 2080 kfree(masks[i]); 2081 } 2082 kfree(masks); 2083 } 2084 if (sched_domain_topology_saved) { 2085 kfree(sched_domain_topology); 2086 sched_domain_topology = sched_domain_topology_saved; 2087 sched_domain_topology_saved = NULL; 2088 } 2089 } 2090 2091 /* 2092 * Call with hotplug lock held 2093 */ 2094 void sched_update_numa(int cpu, bool online) 2095 { 2096 int node; 2097 2098 node = cpu_to_node(cpu); 2099 /* 2100 * Scheduler NUMA topology is updated when the first CPU of a 2101 * node is onlined or the last CPU of a node is offlined. 2102 */ 2103 if (cpumask_weight(cpumask_of_node(node)) != 1) 2104 return; 2105 2106 sched_reset_numa(); 2107 sched_init_numa(online ? NUMA_NO_NODE : node); 2108 } 2109 2110 void sched_domains_numa_masks_set(unsigned int cpu) 2111 { 2112 int node = cpu_to_node(cpu); 2113 int i, j; 2114 2115 for (i = 0; i < sched_domains_numa_levels; i++) { 2116 for (j = 0; j < nr_node_ids; j++) { 2117 if (!node_state(j, N_CPU)) 2118 continue; 2119 2120 /* Set ourselves in the remote node's masks */ 2121 if (node_distance(j, node) <= sched_domains_numa_distance[i]) 2122 cpumask_set_cpu(cpu, sched_domains_numa_masks[i][j]); 2123 } 2124 } 2125 } 2126 2127 void sched_domains_numa_masks_clear(unsigned int cpu) 2128 { 2129 int i, j; 2130 2131 for (i = 0; i < sched_domains_numa_levels; i++) { 2132 for (j = 0; j < nr_node_ids; j++) { 2133 if (sched_domains_numa_masks[i][j]) 2134 cpumask_clear_cpu(cpu, sched_domains_numa_masks[i][j]); 2135 } 2136 } 2137 } 2138 2139 /* 2140 * sched_numa_find_closest() - given the NUMA topology, find the cpu 2141 * closest to @cpu from @cpumask. 2142 * cpumask: cpumask to find a cpu from 2143 * cpu: cpu to be close to 2144 * 2145 * returns: cpu, or nr_cpu_ids when nothing found. 2146 */ 2147 int sched_numa_find_closest(const struct cpumask *cpus, int cpu) 2148 { 2149 int i, j = cpu_to_node(cpu), found = nr_cpu_ids; 2150 struct cpumask ***masks; 2151 2152 rcu_read_lock(); 2153 masks = rcu_dereference(sched_domains_numa_masks); 2154 if (!masks) 2155 goto unlock; 2156 for (i = 0; i < sched_domains_numa_levels; i++) { 2157 if (!masks[i][j]) 2158 break; 2159 cpu = cpumask_any_and_distribute(cpus, masks[i][j]); 2160 if (cpu < nr_cpu_ids) { 2161 found = cpu; 2162 break; 2163 } 2164 } 2165 unlock: 2166 rcu_read_unlock(); 2167 2168 return found; 2169 } 2170 2171 struct __cmp_key { 2172 const struct cpumask *cpus; 2173 struct cpumask ***masks; 2174 int node; 2175 int cpu; 2176 int w; 2177 }; 2178 2179 static int hop_cmp(const void *a, const void *b) 2180 { 2181 struct cpumask **prev_hop, **cur_hop = *(struct cpumask ***)b; 2182 struct __cmp_key *k = (struct __cmp_key *)a; 2183 2184 if (cpumask_weight_and(k->cpus, cur_hop[k->node]) <= k->cpu) 2185 return 1; 2186 2187 if (b == k->masks) { 2188 k->w = 0; 2189 return 0; 2190 } 2191 2192 prev_hop = *((struct cpumask ***)b - 1); 2193 k->w = cpumask_weight_and(k->cpus, prev_hop[k->node]); 2194 if (k->w <= k->cpu) 2195 return 0; 2196 2197 return -1; 2198 } 2199 2200 /** 2201 * sched_numa_find_nth_cpu() - given the NUMA topology, find the Nth closest CPU 2202 * from @cpus to @cpu, taking into account distance 2203 * from a given @node. 2204 * @cpus: cpumask to find a cpu from 2205 * @cpu: CPU to start searching 2206 * @node: NUMA node to order CPUs by distance 2207 * 2208 * Return: cpu, or nr_cpu_ids when nothing found. 2209 */ 2210 int sched_numa_find_nth_cpu(const struct cpumask *cpus, int cpu, int node) 2211 { 2212 struct __cmp_key k = { .cpus = cpus, .cpu = cpu }; 2213 struct cpumask ***hop_masks; 2214 int hop, ret = nr_cpu_ids; 2215 2216 if (node == NUMA_NO_NODE) 2217 return cpumask_nth_and(cpu, cpus, cpu_online_mask); 2218 2219 rcu_read_lock(); 2220 2221 /* CPU-less node entries are uninitialized in sched_domains_numa_masks */ 2222 node = numa_nearest_node(node, N_CPU); 2223 k.node = node; 2224 2225 k.masks = rcu_dereference(sched_domains_numa_masks); 2226 if (!k.masks) 2227 goto unlock; 2228 2229 hop_masks = bsearch(&k, k.masks, sched_domains_numa_levels, sizeof(k.masks[0]), hop_cmp); 2230 hop = hop_masks - k.masks; 2231 2232 ret = hop ? 2233 cpumask_nth_and_andnot(cpu - k.w, cpus, k.masks[hop][node], k.masks[hop-1][node]) : 2234 cpumask_nth_and(cpu, cpus, k.masks[0][node]); 2235 unlock: 2236 rcu_read_unlock(); 2237 return ret; 2238 } 2239 EXPORT_SYMBOL_GPL(sched_numa_find_nth_cpu); 2240 2241 /** 2242 * sched_numa_hop_mask() - Get the cpumask of CPUs at most @hops hops away from 2243 * @node 2244 * @node: The node to count hops from. 2245 * @hops: Include CPUs up to that many hops away. 0 means local node. 2246 * 2247 * Return: On success, a pointer to a cpumask of CPUs at most @hops away from 2248 * @node, an error value otherwise. 2249 * 2250 * Requires rcu_lock to be held. Returned cpumask is only valid within that 2251 * read-side section, copy it if required beyond that. 2252 * 2253 * Note that not all hops are equal in distance; see sched_init_numa() for how 2254 * distances and masks are handled. 2255 * Also note that this is a reflection of sched_domains_numa_masks, which may change 2256 * during the lifetime of the system (offline nodes are taken out of the masks). 2257 */ 2258 const struct cpumask *sched_numa_hop_mask(unsigned int node, unsigned int hops) 2259 { 2260 struct cpumask ***masks; 2261 2262 if (node >= nr_node_ids || hops >= sched_domains_numa_levels) 2263 return ERR_PTR(-EINVAL); 2264 2265 masks = rcu_dereference(sched_domains_numa_masks); 2266 if (!masks) 2267 return ERR_PTR(-EBUSY); 2268 2269 return masks[hops][node]; 2270 } 2271 EXPORT_SYMBOL_GPL(sched_numa_hop_mask); 2272 2273 #endif /* CONFIG_NUMA */ 2274 2275 static int __sdt_alloc(const struct cpumask *cpu_map) 2276 { 2277 struct sched_domain_topology_level *tl; 2278 int j; 2279 2280 for_each_sd_topology(tl) { 2281 struct sd_data *sdd = &tl->data; 2282 2283 sdd->sd = alloc_percpu(struct sched_domain *); 2284 if (!sdd->sd) 2285 return -ENOMEM; 2286 2287 sdd->sds = alloc_percpu(struct sched_domain_shared *); 2288 if (!sdd->sds) 2289 return -ENOMEM; 2290 2291 sdd->sg = alloc_percpu(struct sched_group *); 2292 if (!sdd->sg) 2293 return -ENOMEM; 2294 2295 sdd->sgc = alloc_percpu(struct sched_group_capacity *); 2296 if (!sdd->sgc) 2297 return -ENOMEM; 2298 2299 for_each_cpu(j, cpu_map) { 2300 struct sched_domain *sd; 2301 struct sched_domain_shared *sds; 2302 struct sched_group *sg; 2303 struct sched_group_capacity *sgc; 2304 2305 sd = kzalloc_node(sizeof(struct sched_domain) + cpumask_size(), 2306 GFP_KERNEL, cpu_to_node(j)); 2307 if (!sd) 2308 return -ENOMEM; 2309 2310 *per_cpu_ptr(sdd->sd, j) = sd; 2311 2312 sds = kzalloc_node(sizeof(struct sched_domain_shared), 2313 GFP_KERNEL, cpu_to_node(j)); 2314 if (!sds) 2315 return -ENOMEM; 2316 2317 *per_cpu_ptr(sdd->sds, j) = sds; 2318 2319 sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(), 2320 GFP_KERNEL, cpu_to_node(j)); 2321 if (!sg) 2322 return -ENOMEM; 2323 2324 sg->next = sg; 2325 2326 *per_cpu_ptr(sdd->sg, j) = sg; 2327 2328 sgc = kzalloc_node(sizeof(struct sched_group_capacity) + cpumask_size(), 2329 GFP_KERNEL, cpu_to_node(j)); 2330 if (!sgc) 2331 return -ENOMEM; 2332 2333 sgc->id = j; 2334 2335 *per_cpu_ptr(sdd->sgc, j) = sgc; 2336 } 2337 } 2338 2339 return 0; 2340 } 2341 2342 static void __sdt_free(const struct cpumask *cpu_map) 2343 { 2344 struct sched_domain_topology_level *tl; 2345 int j; 2346 2347 for_each_sd_topology(tl) { 2348 struct sd_data *sdd = &tl->data; 2349 2350 for_each_cpu(j, cpu_map) { 2351 struct sched_domain *sd; 2352 2353 if (sdd->sd) { 2354 sd = *per_cpu_ptr(sdd->sd, j); 2355 if (sd && (sd->flags & SD_OVERLAP)) 2356 free_sched_groups(sd->groups, 0); 2357 kfree(*per_cpu_ptr(sdd->sd, j)); 2358 } 2359 2360 if (sdd->sds) 2361 kfree(*per_cpu_ptr(sdd->sds, j)); 2362 if (sdd->sg) 2363 kfree(*per_cpu_ptr(sdd->sg, j)); 2364 if (sdd->sgc) 2365 kfree(*per_cpu_ptr(sdd->sgc, j)); 2366 } 2367 free_percpu(sdd->sd); 2368 sdd->sd = NULL; 2369 free_percpu(sdd->sds); 2370 sdd->sds = NULL; 2371 free_percpu(sdd->sg); 2372 sdd->sg = NULL; 2373 free_percpu(sdd->sgc); 2374 sdd->sgc = NULL; 2375 } 2376 } 2377 2378 static struct sched_domain *build_sched_domain(struct sched_domain_topology_level *tl, 2379 const struct cpumask *cpu_map, struct sched_domain_attr *attr, 2380 struct sched_domain *child, int cpu) 2381 { 2382 struct sched_domain *sd = sd_init(tl, cpu_map, child, cpu); 2383 2384 if (child) { 2385 sd->level = child->level + 1; 2386 sched_domain_level_max = max(sched_domain_level_max, sd->level); 2387 child->parent = sd; 2388 2389 if (!cpumask_subset(sched_domain_span(child), 2390 sched_domain_span(sd))) { 2391 pr_err("BUG: arch topology borken\n"); 2392 pr_err(" the %s domain not a subset of the %s domain\n", 2393 child->name, sd->name); 2394 /* Fixup, ensure @sd has at least @child CPUs. */ 2395 cpumask_or(sched_domain_span(sd), 2396 sched_domain_span(sd), 2397 sched_domain_span(child)); 2398 } 2399 2400 } 2401 set_domain_attribute(sd, attr); 2402 2403 return sd; 2404 } 2405 2406 /* 2407 * Ensure topology masks are sane, i.e. there are no conflicts (overlaps) for 2408 * any two given CPUs on non-NUMA topology levels. 2409 */ 2410 static bool topology_span_sane(const struct cpumask *cpu_map) 2411 { 2412 struct sched_domain_topology_level *tl; 2413 struct cpumask *covered, *id_seen; 2414 int cpu; 2415 2416 lockdep_assert_held(&sched_domains_mutex); 2417 covered = sched_domains_tmpmask; 2418 id_seen = sched_domains_tmpmask2; 2419 2420 for_each_sd_topology(tl) { 2421 2422 /* NUMA levels are allowed to overlap */ 2423 if (tl->flags & SDTL_OVERLAP) 2424 continue; 2425 2426 cpumask_clear(covered); 2427 cpumask_clear(id_seen); 2428 2429 /* 2430 * Non-NUMA levels cannot partially overlap - they must be either 2431 * completely equal or completely disjoint. Otherwise we can end up 2432 * breaking the sched_group lists - i.e. a later get_group() pass 2433 * breaks the linking done for an earlier span. 2434 */ 2435 for_each_cpu(cpu, cpu_map) { 2436 const struct cpumask *tl_cpu_mask = tl->mask(cpu); 2437 int id; 2438 2439 /* lowest bit set in this mask is used as a unique id */ 2440 id = cpumask_first(tl_cpu_mask); 2441 2442 if (cpumask_test_cpu(id, id_seen)) { 2443 /* First CPU has already been seen, ensure identical spans */ 2444 if (!cpumask_equal(tl->mask(id), tl_cpu_mask)) 2445 return false; 2446 } else { 2447 /* First CPU hasn't been seen before, ensure it's a completely new span */ 2448 if (cpumask_intersects(tl_cpu_mask, covered)) 2449 return false; 2450 2451 cpumask_or(covered, covered, tl_cpu_mask); 2452 cpumask_set_cpu(id, id_seen); 2453 } 2454 } 2455 } 2456 return true; 2457 } 2458 2459 /* 2460 * Build sched domains for a given set of CPUs and attach the sched domains 2461 * to the individual CPUs 2462 */ 2463 static int 2464 build_sched_domains(const struct cpumask *cpu_map, struct sched_domain_attr *attr) 2465 { 2466 enum s_alloc alloc_state = sa_none; 2467 struct sched_domain *sd; 2468 struct s_data d; 2469 struct rq *rq = NULL; 2470 int i, ret = -ENOMEM; 2471 bool has_asym = false; 2472 bool has_cluster = false; 2473 2474 if (WARN_ON(cpumask_empty(cpu_map))) 2475 goto error; 2476 2477 alloc_state = __visit_domain_allocation_hell(&d, cpu_map); 2478 if (alloc_state != sa_rootdomain) 2479 goto error; 2480 2481 /* Set up domains for CPUs specified by the cpu_map: */ 2482 for_each_cpu(i, cpu_map) { 2483 struct sched_domain_topology_level *tl; 2484 2485 sd = NULL; 2486 for_each_sd_topology(tl) { 2487 2488 sd = build_sched_domain(tl, cpu_map, attr, sd, i); 2489 2490 has_asym |= sd->flags & SD_ASYM_CPUCAPACITY; 2491 2492 if (tl == sched_domain_topology) 2493 *per_cpu_ptr(d.sd, i) = sd; 2494 if (tl->flags & SDTL_OVERLAP) 2495 sd->flags |= SD_OVERLAP; 2496 if (cpumask_equal(cpu_map, sched_domain_span(sd))) 2497 break; 2498 } 2499 } 2500 2501 if (WARN_ON(!topology_span_sane(cpu_map))) 2502 goto error; 2503 2504 /* Build the groups for the domains */ 2505 for_each_cpu(i, cpu_map) { 2506 for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) { 2507 sd->span_weight = cpumask_weight(sched_domain_span(sd)); 2508 if (sd->flags & SD_OVERLAP) { 2509 if (build_overlap_sched_groups(sd, i)) 2510 goto error; 2511 } else { 2512 if (build_sched_groups(sd, i)) 2513 goto error; 2514 } 2515 } 2516 } 2517 2518 /* 2519 * Calculate an allowed NUMA imbalance such that LLCs do not get 2520 * imbalanced. 2521 */ 2522 for_each_cpu(i, cpu_map) { 2523 unsigned int imb = 0; 2524 unsigned int imb_span = 1; 2525 2526 for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) { 2527 struct sched_domain *child = sd->child; 2528 2529 if (!(sd->flags & SD_SHARE_LLC) && child && 2530 (child->flags & SD_SHARE_LLC)) { 2531 struct sched_domain __rcu *top_p; 2532 unsigned int nr_llcs; 2533 2534 /* 2535 * For a single LLC per node, allow an 2536 * imbalance up to 12.5% of the node. This is 2537 * arbitrary cutoff based two factors -- SMT and 2538 * memory channels. For SMT-2, the intent is to 2539 * avoid premature sharing of HT resources but 2540 * SMT-4 or SMT-8 *may* benefit from a different 2541 * cutoff. For memory channels, this is a very 2542 * rough estimate of how many channels may be 2543 * active and is based on recent CPUs with 2544 * many cores. 2545 * 2546 * For multiple LLCs, allow an imbalance 2547 * until multiple tasks would share an LLC 2548 * on one node while LLCs on another node 2549 * remain idle. This assumes that there are 2550 * enough logical CPUs per LLC to avoid SMT 2551 * factors and that there is a correlation 2552 * between LLCs and memory channels. 2553 */ 2554 nr_llcs = sd->span_weight / child->span_weight; 2555 if (nr_llcs == 1) 2556 imb = sd->span_weight >> 3; 2557 else 2558 imb = nr_llcs; 2559 imb = max(1U, imb); 2560 sd->imb_numa_nr = imb; 2561 2562 /* Set span based on the first NUMA domain. */ 2563 top_p = sd->parent; 2564 while (top_p && !(top_p->flags & SD_NUMA)) { 2565 top_p = top_p->parent; 2566 } 2567 imb_span = top_p ? top_p->span_weight : sd->span_weight; 2568 } else { 2569 int factor = max(1U, (sd->span_weight / imb_span)); 2570 2571 sd->imb_numa_nr = imb * factor; 2572 } 2573 } 2574 } 2575 2576 /* Calculate CPU capacity for physical packages and nodes */ 2577 for (i = nr_cpumask_bits-1; i >= 0; i--) { 2578 if (!cpumask_test_cpu(i, cpu_map)) 2579 continue; 2580 2581 for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) { 2582 claim_allocations(i, sd); 2583 init_sched_groups_capacity(i, sd); 2584 } 2585 } 2586 2587 /* Attach the domains */ 2588 rcu_read_lock(); 2589 for_each_cpu(i, cpu_map) { 2590 rq = cpu_rq(i); 2591 sd = *per_cpu_ptr(d.sd, i); 2592 2593 cpu_attach_domain(sd, d.rd, i); 2594 2595 if (lowest_flag_domain(i, SD_CLUSTER)) 2596 has_cluster = true; 2597 } 2598 rcu_read_unlock(); 2599 2600 if (has_asym) 2601 static_branch_inc_cpuslocked(&sched_asym_cpucapacity); 2602 2603 if (has_cluster) 2604 static_branch_inc_cpuslocked(&sched_cluster_active); 2605 2606 if (rq && sched_debug_verbose) 2607 pr_info("root domain span: %*pbl\n", cpumask_pr_args(cpu_map)); 2608 2609 ret = 0; 2610 error: 2611 __free_domain_allocs(&d, alloc_state, cpu_map); 2612 2613 return ret; 2614 } 2615 2616 /* Current sched domains: */ 2617 static cpumask_var_t *doms_cur; 2618 2619 /* Number of sched domains in 'doms_cur': */ 2620 static int ndoms_cur; 2621 2622 /* Attributes of custom domains in 'doms_cur' */ 2623 static struct sched_domain_attr *dattr_cur; 2624 2625 /* 2626 * Special case: If a kmalloc() of a doms_cur partition (array of 2627 * cpumask) fails, then fallback to a single sched domain, 2628 * as determined by the single cpumask fallback_doms. 2629 */ 2630 static cpumask_var_t fallback_doms; 2631 2632 /* 2633 * arch_update_cpu_topology lets virtualized architectures update the 2634 * CPU core maps. It is supposed to return 1 if the topology changed 2635 * or 0 if it stayed the same. 2636 */ 2637 int __weak arch_update_cpu_topology(void) 2638 { 2639 return 0; 2640 } 2641 2642 cpumask_var_t *alloc_sched_domains(unsigned int ndoms) 2643 { 2644 int i; 2645 cpumask_var_t *doms; 2646 2647 doms = kmalloc_array(ndoms, sizeof(*doms), GFP_KERNEL); 2648 if (!doms) 2649 return NULL; 2650 for (i = 0; i < ndoms; i++) { 2651 if (!alloc_cpumask_var(&doms[i], GFP_KERNEL)) { 2652 free_sched_domains(doms, i); 2653 return NULL; 2654 } 2655 } 2656 return doms; 2657 } 2658 2659 void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms) 2660 { 2661 unsigned int i; 2662 for (i = 0; i < ndoms; i++) 2663 free_cpumask_var(doms[i]); 2664 kfree(doms); 2665 } 2666 2667 /* 2668 * Set up scheduler domains and groups. For now this just excludes isolated 2669 * CPUs, but could be used to exclude other special cases in the future. 2670 */ 2671 int __init sched_init_domains(const struct cpumask *cpu_map) 2672 { 2673 int err; 2674 2675 zalloc_cpumask_var(&sched_domains_tmpmask, GFP_KERNEL); 2676 zalloc_cpumask_var(&sched_domains_tmpmask2, GFP_KERNEL); 2677 zalloc_cpumask_var(&fallback_doms, GFP_KERNEL); 2678 2679 arch_update_cpu_topology(); 2680 asym_cpu_capacity_scan(); 2681 ndoms_cur = 1; 2682 doms_cur = alloc_sched_domains(ndoms_cur); 2683 if (!doms_cur) 2684 doms_cur = &fallback_doms; 2685 cpumask_and(doms_cur[0], cpu_map, housekeeping_cpumask(HK_TYPE_DOMAIN)); 2686 err = build_sched_domains(doms_cur[0], NULL); 2687 2688 return err; 2689 } 2690 2691 /* 2692 * Detach sched domains from a group of CPUs specified in cpu_map 2693 * These CPUs will now be attached to the NULL domain 2694 */ 2695 static void detach_destroy_domains(const struct cpumask *cpu_map) 2696 { 2697 unsigned int cpu = cpumask_any(cpu_map); 2698 int i; 2699 2700 if (rcu_access_pointer(per_cpu(sd_asym_cpucapacity, cpu))) 2701 static_branch_dec_cpuslocked(&sched_asym_cpucapacity); 2702 2703 if (static_branch_unlikely(&sched_cluster_active)) 2704 static_branch_dec_cpuslocked(&sched_cluster_active); 2705 2706 rcu_read_lock(); 2707 for_each_cpu(i, cpu_map) 2708 cpu_attach_domain(NULL, &def_root_domain, i); 2709 rcu_read_unlock(); 2710 } 2711 2712 /* handle null as "default" */ 2713 static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur, 2714 struct sched_domain_attr *new, int idx_new) 2715 { 2716 struct sched_domain_attr tmp; 2717 2718 /* Fast path: */ 2719 if (!new && !cur) 2720 return 1; 2721 2722 tmp = SD_ATTR_INIT; 2723 2724 return !memcmp(cur ? (cur + idx_cur) : &tmp, 2725 new ? (new + idx_new) : &tmp, 2726 sizeof(struct sched_domain_attr)); 2727 } 2728 2729 /* 2730 * Partition sched domains as specified by the 'ndoms_new' 2731 * cpumasks in the array doms_new[] of cpumasks. This compares 2732 * doms_new[] to the current sched domain partitioning, doms_cur[]. 2733 * It destroys each deleted domain and builds each new domain. 2734 * 2735 * 'doms_new' is an array of cpumask_var_t's of length 'ndoms_new'. 2736 * The masks don't intersect (don't overlap.) We should setup one 2737 * sched domain for each mask. CPUs not in any of the cpumasks will 2738 * not be load balanced. If the same cpumask appears both in the 2739 * current 'doms_cur' domains and in the new 'doms_new', we can leave 2740 * it as it is. 2741 * 2742 * The passed in 'doms_new' should be allocated using 2743 * alloc_sched_domains. This routine takes ownership of it and will 2744 * free_sched_domains it when done with it. If the caller failed the 2745 * alloc call, then it can pass in doms_new == NULL && ndoms_new == 1, 2746 * and partition_sched_domains() will fallback to the single partition 2747 * 'fallback_doms', it also forces the domains to be rebuilt. 2748 * 2749 * If doms_new == NULL it will be replaced with cpu_online_mask. 2750 * ndoms_new == 0 is a special case for destroying existing domains, 2751 * and it will not create the default domain. 2752 * 2753 * Call with hotplug lock and sched_domains_mutex held 2754 */ 2755 static void partition_sched_domains_locked(int ndoms_new, cpumask_var_t doms_new[], 2756 struct sched_domain_attr *dattr_new) 2757 { 2758 bool __maybe_unused has_eas = false; 2759 int i, j, n; 2760 int new_topology; 2761 2762 lockdep_assert_held(&sched_domains_mutex); 2763 2764 /* Let the architecture update CPU core mappings: */ 2765 new_topology = arch_update_cpu_topology(); 2766 /* Trigger rebuilding CPU capacity asymmetry data */ 2767 if (new_topology) 2768 asym_cpu_capacity_scan(); 2769 2770 if (!doms_new) { 2771 WARN_ON_ONCE(dattr_new); 2772 n = 0; 2773 doms_new = alloc_sched_domains(1); 2774 if (doms_new) { 2775 n = 1; 2776 cpumask_and(doms_new[0], cpu_active_mask, 2777 housekeeping_cpumask(HK_TYPE_DOMAIN)); 2778 } 2779 } else { 2780 n = ndoms_new; 2781 } 2782 2783 /* Destroy deleted domains: */ 2784 for (i = 0; i < ndoms_cur; i++) { 2785 for (j = 0; j < n && !new_topology; j++) { 2786 if (cpumask_equal(doms_cur[i], doms_new[j]) && 2787 dattrs_equal(dattr_cur, i, dattr_new, j)) 2788 goto match1; 2789 } 2790 /* No match - a current sched domain not in new doms_new[] */ 2791 detach_destroy_domains(doms_cur[i]); 2792 match1: 2793 ; 2794 } 2795 2796 n = ndoms_cur; 2797 if (!doms_new) { 2798 n = 0; 2799 doms_new = &fallback_doms; 2800 cpumask_and(doms_new[0], cpu_active_mask, 2801 housekeeping_cpumask(HK_TYPE_DOMAIN)); 2802 } 2803 2804 /* Build new domains: */ 2805 for (i = 0; i < ndoms_new; i++) { 2806 for (j = 0; j < n && !new_topology; j++) { 2807 if (cpumask_equal(doms_new[i], doms_cur[j]) && 2808 dattrs_equal(dattr_new, i, dattr_cur, j)) 2809 goto match2; 2810 } 2811 /* No match - add a new doms_new */ 2812 build_sched_domains(doms_new[i], dattr_new ? dattr_new + i : NULL); 2813 match2: 2814 ; 2815 } 2816 2817 #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL) 2818 /* Build perf domains: */ 2819 for (i = 0; i < ndoms_new; i++) { 2820 for (j = 0; j < n && !sched_energy_update; j++) { 2821 if (cpumask_equal(doms_new[i], doms_cur[j]) && 2822 cpu_rq(cpumask_first(doms_cur[j]))->rd->pd) { 2823 has_eas = true; 2824 goto match3; 2825 } 2826 } 2827 /* No match - add perf domains for a new rd */ 2828 has_eas |= build_perf_domains(doms_new[i]); 2829 match3: 2830 ; 2831 } 2832 sched_energy_set(has_eas); 2833 #endif 2834 2835 /* Remember the new sched domains: */ 2836 if (doms_cur != &fallback_doms) 2837 free_sched_domains(doms_cur, ndoms_cur); 2838 2839 kfree(dattr_cur); 2840 doms_cur = doms_new; 2841 dattr_cur = dattr_new; 2842 ndoms_cur = ndoms_new; 2843 2844 update_sched_domain_debugfs(); 2845 dl_rebuild_rd_accounting(); 2846 } 2847 2848 /* 2849 * Call with hotplug lock held 2850 */ 2851 void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[], 2852 struct sched_domain_attr *dattr_new) 2853 { 2854 sched_domains_mutex_lock(); 2855 partition_sched_domains_locked(ndoms_new, doms_new, dattr_new); 2856 sched_domains_mutex_unlock(); 2857 } 2858