1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2f2cb1360SIngo Molnar /* 3f2cb1360SIngo Molnar * Scheduler topology setup/handling methods 4f2cb1360SIngo Molnar */ 5f2cb1360SIngo Molnar #include "sched.h" 6f2cb1360SIngo Molnar 7f2cb1360SIngo Molnar DEFINE_MUTEX(sched_domains_mutex); 8f2cb1360SIngo Molnar 9f2cb1360SIngo Molnar /* Protected by sched_domains_mutex: */ 10ace80310Szhong jiang static cpumask_var_t sched_domains_tmpmask; 11ace80310Szhong jiang static cpumask_var_t sched_domains_tmpmask2; 12f2cb1360SIngo Molnar 13f2cb1360SIngo Molnar #ifdef CONFIG_SCHED_DEBUG 14f2cb1360SIngo Molnar 15f2cb1360SIngo Molnar static int __init sched_debug_setup(char *str) 16f2cb1360SIngo Molnar { 179469eb01SPeter Zijlstra sched_debug_enabled = true; 18f2cb1360SIngo Molnar 19f2cb1360SIngo Molnar return 0; 20f2cb1360SIngo Molnar } 21f2cb1360SIngo Molnar early_param("sched_debug", sched_debug_setup); 22f2cb1360SIngo Molnar 23f2cb1360SIngo Molnar static inline bool sched_debug(void) 24f2cb1360SIngo Molnar { 25f2cb1360SIngo Molnar return sched_debug_enabled; 26f2cb1360SIngo Molnar } 27f2cb1360SIngo Molnar 28f2cb1360SIngo Molnar static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level, 29f2cb1360SIngo Molnar struct cpumask *groupmask) 30f2cb1360SIngo Molnar { 31f2cb1360SIngo Molnar struct sched_group *group = sd->groups; 32f2cb1360SIngo Molnar 33f2cb1360SIngo Molnar cpumask_clear(groupmask); 34f2cb1360SIngo Molnar 35005f874dSPeter Zijlstra printk(KERN_DEBUG "%*s domain-%d: ", level, "", level); 36f2cb1360SIngo Molnar 37f2cb1360SIngo Molnar if (!(sd->flags & SD_LOAD_BALANCE)) { 38f2cb1360SIngo Molnar printk("does not load-balance\n"); 39f2cb1360SIngo Molnar if (sd->parent) 4097fb7a0aSIngo Molnar printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain has parent"); 41f2cb1360SIngo Molnar return -1; 42f2cb1360SIngo Molnar } 43f2cb1360SIngo Molnar 44005f874dSPeter Zijlstra printk(KERN_CONT "span=%*pbl level=%s\n", 45f2cb1360SIngo Molnar cpumask_pr_args(sched_domain_span(sd)), sd->name); 46f2cb1360SIngo Molnar 47f2cb1360SIngo Molnar if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) { 4897fb7a0aSIngo Molnar printk(KERN_ERR "ERROR: domain->span does not contain CPU%d\n", cpu); 49f2cb1360SIngo Molnar } 506cd0c583SYi Wang if (group && !cpumask_test_cpu(cpu, sched_group_span(group))) { 5197fb7a0aSIngo Molnar printk(KERN_ERR "ERROR: domain->groups does not contain CPU%d\n", cpu); 52f2cb1360SIngo Molnar } 53f2cb1360SIngo Molnar 54f2cb1360SIngo Molnar printk(KERN_DEBUG "%*s groups:", level + 1, ""); 55f2cb1360SIngo Molnar do { 56f2cb1360SIngo Molnar if (!group) { 57f2cb1360SIngo Molnar printk("\n"); 58f2cb1360SIngo Molnar printk(KERN_ERR "ERROR: group is NULL\n"); 59f2cb1360SIngo Molnar break; 60f2cb1360SIngo Molnar } 61f2cb1360SIngo Molnar 62ae4df9d6SPeter Zijlstra if (!cpumask_weight(sched_group_span(group))) { 63f2cb1360SIngo Molnar printk(KERN_CONT "\n"); 64f2cb1360SIngo Molnar printk(KERN_ERR "ERROR: empty group\n"); 65f2cb1360SIngo Molnar break; 66f2cb1360SIngo Molnar } 67f2cb1360SIngo Molnar 68f2cb1360SIngo Molnar if (!(sd->flags & SD_OVERLAP) && 69ae4df9d6SPeter Zijlstra cpumask_intersects(groupmask, sched_group_span(group))) { 70f2cb1360SIngo Molnar printk(KERN_CONT "\n"); 71f2cb1360SIngo Molnar printk(KERN_ERR "ERROR: repeated CPUs\n"); 72f2cb1360SIngo Molnar break; 73f2cb1360SIngo Molnar } 74f2cb1360SIngo Molnar 75ae4df9d6SPeter Zijlstra cpumask_or(groupmask, groupmask, sched_group_span(group)); 76f2cb1360SIngo Molnar 77005f874dSPeter Zijlstra printk(KERN_CONT " %d:{ span=%*pbl", 78005f874dSPeter Zijlstra group->sgc->id, 79ae4df9d6SPeter Zijlstra cpumask_pr_args(sched_group_span(group))); 80b0151c25SPeter Zijlstra 81af218122SPeter Zijlstra if ((sd->flags & SD_OVERLAP) && 82ae4df9d6SPeter Zijlstra !cpumask_equal(group_balance_mask(group), sched_group_span(group))) { 83005f874dSPeter Zijlstra printk(KERN_CONT " mask=%*pbl", 84e5c14b1fSPeter Zijlstra cpumask_pr_args(group_balance_mask(group))); 85b0151c25SPeter Zijlstra } 86b0151c25SPeter Zijlstra 87005f874dSPeter Zijlstra if (group->sgc->capacity != SCHED_CAPACITY_SCALE) 88005f874dSPeter Zijlstra printk(KERN_CONT " cap=%lu", group->sgc->capacity); 89f2cb1360SIngo Molnar 90a420b063SPeter Zijlstra if (group == sd->groups && sd->child && 91a420b063SPeter Zijlstra !cpumask_equal(sched_domain_span(sd->child), 92ae4df9d6SPeter Zijlstra sched_group_span(group))) { 93a420b063SPeter Zijlstra printk(KERN_ERR "ERROR: domain->groups does not match domain->child\n"); 94a420b063SPeter Zijlstra } 95a420b063SPeter Zijlstra 96005f874dSPeter Zijlstra printk(KERN_CONT " }"); 97005f874dSPeter Zijlstra 98f2cb1360SIngo Molnar group = group->next; 99b0151c25SPeter Zijlstra 100b0151c25SPeter Zijlstra if (group != sd->groups) 101b0151c25SPeter Zijlstra printk(KERN_CONT ","); 102b0151c25SPeter Zijlstra 103f2cb1360SIngo Molnar } while (group != sd->groups); 104f2cb1360SIngo Molnar printk(KERN_CONT "\n"); 105f2cb1360SIngo Molnar 106f2cb1360SIngo Molnar if (!cpumask_equal(sched_domain_span(sd), groupmask)) 107f2cb1360SIngo Molnar printk(KERN_ERR "ERROR: groups don't span domain->span\n"); 108f2cb1360SIngo Molnar 109f2cb1360SIngo Molnar if (sd->parent && 110f2cb1360SIngo Molnar !cpumask_subset(groupmask, sched_domain_span(sd->parent))) 11197fb7a0aSIngo Molnar printk(KERN_ERR "ERROR: parent span is not a superset of domain->span\n"); 112f2cb1360SIngo Molnar return 0; 113f2cb1360SIngo Molnar } 114f2cb1360SIngo Molnar 115f2cb1360SIngo Molnar static void sched_domain_debug(struct sched_domain *sd, int cpu) 116f2cb1360SIngo Molnar { 117f2cb1360SIngo Molnar int level = 0; 118f2cb1360SIngo Molnar 119f2cb1360SIngo Molnar if (!sched_debug_enabled) 120f2cb1360SIngo Molnar return; 121f2cb1360SIngo Molnar 122f2cb1360SIngo Molnar if (!sd) { 123f2cb1360SIngo Molnar printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu); 124f2cb1360SIngo Molnar return; 125f2cb1360SIngo Molnar } 126f2cb1360SIngo Molnar 127005f874dSPeter Zijlstra printk(KERN_DEBUG "CPU%d attaching sched-domain(s):\n", cpu); 128f2cb1360SIngo Molnar 129f2cb1360SIngo Molnar for (;;) { 130f2cb1360SIngo Molnar if (sched_domain_debug_one(sd, cpu, level, sched_domains_tmpmask)) 131f2cb1360SIngo Molnar break; 132f2cb1360SIngo Molnar level++; 133f2cb1360SIngo Molnar sd = sd->parent; 134f2cb1360SIngo Molnar if (!sd) 135f2cb1360SIngo Molnar break; 136f2cb1360SIngo Molnar } 137f2cb1360SIngo Molnar } 138f2cb1360SIngo Molnar #else /* !CONFIG_SCHED_DEBUG */ 139f2cb1360SIngo Molnar 140f2cb1360SIngo Molnar # define sched_debug_enabled 0 141f2cb1360SIngo Molnar # define sched_domain_debug(sd, cpu) do { } while (0) 142f2cb1360SIngo Molnar static inline bool sched_debug(void) 143f2cb1360SIngo Molnar { 144f2cb1360SIngo Molnar return false; 145f2cb1360SIngo Molnar } 146f2cb1360SIngo Molnar #endif /* CONFIG_SCHED_DEBUG */ 147f2cb1360SIngo Molnar 148f2cb1360SIngo Molnar static int sd_degenerate(struct sched_domain *sd) 149f2cb1360SIngo Molnar { 150f2cb1360SIngo Molnar if (cpumask_weight(sched_domain_span(sd)) == 1) 151f2cb1360SIngo Molnar return 1; 152f2cb1360SIngo Molnar 153f2cb1360SIngo Molnar /* Following flags need at least 2 groups */ 154f2cb1360SIngo Molnar if (sd->flags & (SD_LOAD_BALANCE | 155f2cb1360SIngo Molnar SD_BALANCE_NEWIDLE | 156f2cb1360SIngo Molnar SD_BALANCE_FORK | 157f2cb1360SIngo Molnar SD_BALANCE_EXEC | 158f2cb1360SIngo Molnar SD_SHARE_CPUCAPACITY | 159f2cb1360SIngo Molnar SD_ASYM_CPUCAPACITY | 160f2cb1360SIngo Molnar SD_SHARE_PKG_RESOURCES | 161f2cb1360SIngo Molnar SD_SHARE_POWERDOMAIN)) { 162f2cb1360SIngo Molnar if (sd->groups != sd->groups->next) 163f2cb1360SIngo Molnar return 0; 164f2cb1360SIngo Molnar } 165f2cb1360SIngo Molnar 166f2cb1360SIngo Molnar /* Following flags don't use groups */ 167f2cb1360SIngo Molnar if (sd->flags & (SD_WAKE_AFFINE)) 168f2cb1360SIngo Molnar return 0; 169f2cb1360SIngo Molnar 170f2cb1360SIngo Molnar return 1; 171f2cb1360SIngo Molnar } 172f2cb1360SIngo Molnar 173f2cb1360SIngo Molnar static int 174f2cb1360SIngo Molnar sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent) 175f2cb1360SIngo Molnar { 176f2cb1360SIngo Molnar unsigned long cflags = sd->flags, pflags = parent->flags; 177f2cb1360SIngo Molnar 178f2cb1360SIngo Molnar if (sd_degenerate(parent)) 179f2cb1360SIngo Molnar return 1; 180f2cb1360SIngo Molnar 181f2cb1360SIngo Molnar if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent))) 182f2cb1360SIngo Molnar return 0; 183f2cb1360SIngo Molnar 184f2cb1360SIngo Molnar /* Flags needing groups don't count if only 1 group in parent */ 185f2cb1360SIngo Molnar if (parent->groups == parent->groups->next) { 186f2cb1360SIngo Molnar pflags &= ~(SD_LOAD_BALANCE | 187f2cb1360SIngo Molnar SD_BALANCE_NEWIDLE | 188f2cb1360SIngo Molnar SD_BALANCE_FORK | 189f2cb1360SIngo Molnar SD_BALANCE_EXEC | 190f2cb1360SIngo Molnar SD_ASYM_CPUCAPACITY | 191f2cb1360SIngo Molnar SD_SHARE_CPUCAPACITY | 192f2cb1360SIngo Molnar SD_SHARE_PKG_RESOURCES | 193f2cb1360SIngo Molnar SD_PREFER_SIBLING | 194f2cb1360SIngo Molnar SD_SHARE_POWERDOMAIN); 195f2cb1360SIngo Molnar if (nr_node_ids == 1) 196f2cb1360SIngo Molnar pflags &= ~SD_SERIALIZE; 197f2cb1360SIngo Molnar } 198f2cb1360SIngo Molnar if (~cflags & pflags) 199f2cb1360SIngo Molnar return 0; 200f2cb1360SIngo Molnar 201f2cb1360SIngo Molnar return 1; 202f2cb1360SIngo Molnar } 203f2cb1360SIngo Molnar 204*6aa140faSQuentin Perret #ifdef CONFIG_ENERGY_MODEL 205*6aa140faSQuentin Perret static void free_pd(struct perf_domain *pd) 206*6aa140faSQuentin Perret { 207*6aa140faSQuentin Perret struct perf_domain *tmp; 208*6aa140faSQuentin Perret 209*6aa140faSQuentin Perret while (pd) { 210*6aa140faSQuentin Perret tmp = pd->next; 211*6aa140faSQuentin Perret kfree(pd); 212*6aa140faSQuentin Perret pd = tmp; 213*6aa140faSQuentin Perret } 214*6aa140faSQuentin Perret } 215*6aa140faSQuentin Perret 216*6aa140faSQuentin Perret static struct perf_domain *find_pd(struct perf_domain *pd, int cpu) 217*6aa140faSQuentin Perret { 218*6aa140faSQuentin Perret while (pd) { 219*6aa140faSQuentin Perret if (cpumask_test_cpu(cpu, perf_domain_span(pd))) 220*6aa140faSQuentin Perret return pd; 221*6aa140faSQuentin Perret pd = pd->next; 222*6aa140faSQuentin Perret } 223*6aa140faSQuentin Perret 224*6aa140faSQuentin Perret return NULL; 225*6aa140faSQuentin Perret } 226*6aa140faSQuentin Perret 227*6aa140faSQuentin Perret static struct perf_domain *pd_init(int cpu) 228*6aa140faSQuentin Perret { 229*6aa140faSQuentin Perret struct em_perf_domain *obj = em_cpu_get(cpu); 230*6aa140faSQuentin Perret struct perf_domain *pd; 231*6aa140faSQuentin Perret 232*6aa140faSQuentin Perret if (!obj) { 233*6aa140faSQuentin Perret if (sched_debug()) 234*6aa140faSQuentin Perret pr_info("%s: no EM found for CPU%d\n", __func__, cpu); 235*6aa140faSQuentin Perret return NULL; 236*6aa140faSQuentin Perret } 237*6aa140faSQuentin Perret 238*6aa140faSQuentin Perret pd = kzalloc(sizeof(*pd), GFP_KERNEL); 239*6aa140faSQuentin Perret if (!pd) 240*6aa140faSQuentin Perret return NULL; 241*6aa140faSQuentin Perret pd->em_pd = obj; 242*6aa140faSQuentin Perret 243*6aa140faSQuentin Perret return pd; 244*6aa140faSQuentin Perret } 245*6aa140faSQuentin Perret 246*6aa140faSQuentin Perret static void perf_domain_debug(const struct cpumask *cpu_map, 247*6aa140faSQuentin Perret struct perf_domain *pd) 248*6aa140faSQuentin Perret { 249*6aa140faSQuentin Perret if (!sched_debug() || !pd) 250*6aa140faSQuentin Perret return; 251*6aa140faSQuentin Perret 252*6aa140faSQuentin Perret printk(KERN_DEBUG "root_domain %*pbl:", cpumask_pr_args(cpu_map)); 253*6aa140faSQuentin Perret 254*6aa140faSQuentin Perret while (pd) { 255*6aa140faSQuentin Perret printk(KERN_CONT " pd%d:{ cpus=%*pbl nr_cstate=%d }", 256*6aa140faSQuentin Perret cpumask_first(perf_domain_span(pd)), 257*6aa140faSQuentin Perret cpumask_pr_args(perf_domain_span(pd)), 258*6aa140faSQuentin Perret em_pd_nr_cap_states(pd->em_pd)); 259*6aa140faSQuentin Perret pd = pd->next; 260*6aa140faSQuentin Perret } 261*6aa140faSQuentin Perret 262*6aa140faSQuentin Perret printk(KERN_CONT "\n"); 263*6aa140faSQuentin Perret } 264*6aa140faSQuentin Perret 265*6aa140faSQuentin Perret static void destroy_perf_domain_rcu(struct rcu_head *rp) 266*6aa140faSQuentin Perret { 267*6aa140faSQuentin Perret struct perf_domain *pd; 268*6aa140faSQuentin Perret 269*6aa140faSQuentin Perret pd = container_of(rp, struct perf_domain, rcu); 270*6aa140faSQuentin Perret free_pd(pd); 271*6aa140faSQuentin Perret } 272*6aa140faSQuentin Perret 273*6aa140faSQuentin Perret static void build_perf_domains(const struct cpumask *cpu_map) 274*6aa140faSQuentin Perret { 275*6aa140faSQuentin Perret struct perf_domain *pd = NULL, *tmp; 276*6aa140faSQuentin Perret int cpu = cpumask_first(cpu_map); 277*6aa140faSQuentin Perret struct root_domain *rd = cpu_rq(cpu)->rd; 278*6aa140faSQuentin Perret int i; 279*6aa140faSQuentin Perret 280*6aa140faSQuentin Perret for_each_cpu(i, cpu_map) { 281*6aa140faSQuentin Perret /* Skip already covered CPUs. */ 282*6aa140faSQuentin Perret if (find_pd(pd, i)) 283*6aa140faSQuentin Perret continue; 284*6aa140faSQuentin Perret 285*6aa140faSQuentin Perret /* Create the new pd and add it to the local list. */ 286*6aa140faSQuentin Perret tmp = pd_init(i); 287*6aa140faSQuentin Perret if (!tmp) 288*6aa140faSQuentin Perret goto free; 289*6aa140faSQuentin Perret tmp->next = pd; 290*6aa140faSQuentin Perret pd = tmp; 291*6aa140faSQuentin Perret } 292*6aa140faSQuentin Perret 293*6aa140faSQuentin Perret perf_domain_debug(cpu_map, pd); 294*6aa140faSQuentin Perret 295*6aa140faSQuentin Perret /* Attach the new list of performance domains to the root domain. */ 296*6aa140faSQuentin Perret tmp = rd->pd; 297*6aa140faSQuentin Perret rcu_assign_pointer(rd->pd, pd); 298*6aa140faSQuentin Perret if (tmp) 299*6aa140faSQuentin Perret call_rcu(&tmp->rcu, destroy_perf_domain_rcu); 300*6aa140faSQuentin Perret 301*6aa140faSQuentin Perret return; 302*6aa140faSQuentin Perret 303*6aa140faSQuentin Perret free: 304*6aa140faSQuentin Perret free_pd(pd); 305*6aa140faSQuentin Perret tmp = rd->pd; 306*6aa140faSQuentin Perret rcu_assign_pointer(rd->pd, NULL); 307*6aa140faSQuentin Perret if (tmp) 308*6aa140faSQuentin Perret call_rcu(&tmp->rcu, destroy_perf_domain_rcu); 309*6aa140faSQuentin Perret } 310*6aa140faSQuentin Perret #else 311*6aa140faSQuentin Perret static void free_pd(struct perf_domain *pd) { } 312*6aa140faSQuentin Perret #endif /* CONFIG_ENERGY_MODEL */ 313*6aa140faSQuentin Perret 314f2cb1360SIngo Molnar static void free_rootdomain(struct rcu_head *rcu) 315f2cb1360SIngo Molnar { 316f2cb1360SIngo Molnar struct root_domain *rd = container_of(rcu, struct root_domain, rcu); 317f2cb1360SIngo Molnar 318f2cb1360SIngo Molnar cpupri_cleanup(&rd->cpupri); 319f2cb1360SIngo Molnar cpudl_cleanup(&rd->cpudl); 320f2cb1360SIngo Molnar free_cpumask_var(rd->dlo_mask); 321f2cb1360SIngo Molnar free_cpumask_var(rd->rto_mask); 322f2cb1360SIngo Molnar free_cpumask_var(rd->online); 323f2cb1360SIngo Molnar free_cpumask_var(rd->span); 324*6aa140faSQuentin Perret free_pd(rd->pd); 325f2cb1360SIngo Molnar kfree(rd); 326f2cb1360SIngo Molnar } 327f2cb1360SIngo Molnar 328f2cb1360SIngo Molnar void rq_attach_root(struct rq *rq, struct root_domain *rd) 329f2cb1360SIngo Molnar { 330f2cb1360SIngo Molnar struct root_domain *old_rd = NULL; 331f2cb1360SIngo Molnar unsigned long flags; 332f2cb1360SIngo Molnar 333f2cb1360SIngo Molnar raw_spin_lock_irqsave(&rq->lock, flags); 334f2cb1360SIngo Molnar 335f2cb1360SIngo Molnar if (rq->rd) { 336f2cb1360SIngo Molnar old_rd = rq->rd; 337f2cb1360SIngo Molnar 338f2cb1360SIngo Molnar if (cpumask_test_cpu(rq->cpu, old_rd->online)) 339f2cb1360SIngo Molnar set_rq_offline(rq); 340f2cb1360SIngo Molnar 341f2cb1360SIngo Molnar cpumask_clear_cpu(rq->cpu, old_rd->span); 342f2cb1360SIngo Molnar 343f2cb1360SIngo Molnar /* 344f2cb1360SIngo Molnar * If we dont want to free the old_rd yet then 345f2cb1360SIngo Molnar * set old_rd to NULL to skip the freeing later 346f2cb1360SIngo Molnar * in this function: 347f2cb1360SIngo Molnar */ 348f2cb1360SIngo Molnar if (!atomic_dec_and_test(&old_rd->refcount)) 349f2cb1360SIngo Molnar old_rd = NULL; 350f2cb1360SIngo Molnar } 351f2cb1360SIngo Molnar 352f2cb1360SIngo Molnar atomic_inc(&rd->refcount); 353f2cb1360SIngo Molnar rq->rd = rd; 354f2cb1360SIngo Molnar 355f2cb1360SIngo Molnar cpumask_set_cpu(rq->cpu, rd->span); 356f2cb1360SIngo Molnar if (cpumask_test_cpu(rq->cpu, cpu_active_mask)) 357f2cb1360SIngo Molnar set_rq_online(rq); 358f2cb1360SIngo Molnar 359f2cb1360SIngo Molnar raw_spin_unlock_irqrestore(&rq->lock, flags); 360f2cb1360SIngo Molnar 361f2cb1360SIngo Molnar if (old_rd) 362f2cb1360SIngo Molnar call_rcu_sched(&old_rd->rcu, free_rootdomain); 363f2cb1360SIngo Molnar } 364f2cb1360SIngo Molnar 365364f5665SSteven Rostedt (VMware) void sched_get_rd(struct root_domain *rd) 366364f5665SSteven Rostedt (VMware) { 367364f5665SSteven Rostedt (VMware) atomic_inc(&rd->refcount); 368364f5665SSteven Rostedt (VMware) } 369364f5665SSteven Rostedt (VMware) 370364f5665SSteven Rostedt (VMware) void sched_put_rd(struct root_domain *rd) 371364f5665SSteven Rostedt (VMware) { 372364f5665SSteven Rostedt (VMware) if (!atomic_dec_and_test(&rd->refcount)) 373364f5665SSteven Rostedt (VMware) return; 374364f5665SSteven Rostedt (VMware) 375364f5665SSteven Rostedt (VMware) call_rcu_sched(&rd->rcu, free_rootdomain); 376364f5665SSteven Rostedt (VMware) } 377364f5665SSteven Rostedt (VMware) 378f2cb1360SIngo Molnar static int init_rootdomain(struct root_domain *rd) 379f2cb1360SIngo Molnar { 380f2cb1360SIngo Molnar if (!zalloc_cpumask_var(&rd->span, GFP_KERNEL)) 381f2cb1360SIngo Molnar goto out; 382f2cb1360SIngo Molnar if (!zalloc_cpumask_var(&rd->online, GFP_KERNEL)) 383f2cb1360SIngo Molnar goto free_span; 384f2cb1360SIngo Molnar if (!zalloc_cpumask_var(&rd->dlo_mask, GFP_KERNEL)) 385f2cb1360SIngo Molnar goto free_online; 386f2cb1360SIngo Molnar if (!zalloc_cpumask_var(&rd->rto_mask, GFP_KERNEL)) 387f2cb1360SIngo Molnar goto free_dlo_mask; 388f2cb1360SIngo Molnar 3894bdced5cSSteven Rostedt (Red Hat) #ifdef HAVE_RT_PUSH_IPI 3904bdced5cSSteven Rostedt (Red Hat) rd->rto_cpu = -1; 3914bdced5cSSteven Rostedt (Red Hat) raw_spin_lock_init(&rd->rto_lock); 3924bdced5cSSteven Rostedt (Red Hat) init_irq_work(&rd->rto_push_work, rto_push_irq_work_func); 3934bdced5cSSteven Rostedt (Red Hat) #endif 3944bdced5cSSteven Rostedt (Red Hat) 395f2cb1360SIngo Molnar init_dl_bw(&rd->dl_bw); 396f2cb1360SIngo Molnar if (cpudl_init(&rd->cpudl) != 0) 397f2cb1360SIngo Molnar goto free_rto_mask; 398f2cb1360SIngo Molnar 399f2cb1360SIngo Molnar if (cpupri_init(&rd->cpupri) != 0) 400f2cb1360SIngo Molnar goto free_cpudl; 401f2cb1360SIngo Molnar return 0; 402f2cb1360SIngo Molnar 403f2cb1360SIngo Molnar free_cpudl: 404f2cb1360SIngo Molnar cpudl_cleanup(&rd->cpudl); 405f2cb1360SIngo Molnar free_rto_mask: 406f2cb1360SIngo Molnar free_cpumask_var(rd->rto_mask); 407f2cb1360SIngo Molnar free_dlo_mask: 408f2cb1360SIngo Molnar free_cpumask_var(rd->dlo_mask); 409f2cb1360SIngo Molnar free_online: 410f2cb1360SIngo Molnar free_cpumask_var(rd->online); 411f2cb1360SIngo Molnar free_span: 412f2cb1360SIngo Molnar free_cpumask_var(rd->span); 413f2cb1360SIngo Molnar out: 414f2cb1360SIngo Molnar return -ENOMEM; 415f2cb1360SIngo Molnar } 416f2cb1360SIngo Molnar 417f2cb1360SIngo Molnar /* 418f2cb1360SIngo Molnar * By default the system creates a single root-domain with all CPUs as 419f2cb1360SIngo Molnar * members (mimicking the global state we have today). 420f2cb1360SIngo Molnar */ 421f2cb1360SIngo Molnar struct root_domain def_root_domain; 422f2cb1360SIngo Molnar 423f2cb1360SIngo Molnar void init_defrootdomain(void) 424f2cb1360SIngo Molnar { 425f2cb1360SIngo Molnar init_rootdomain(&def_root_domain); 426f2cb1360SIngo Molnar 427f2cb1360SIngo Molnar atomic_set(&def_root_domain.refcount, 1); 428f2cb1360SIngo Molnar } 429f2cb1360SIngo Molnar 430f2cb1360SIngo Molnar static struct root_domain *alloc_rootdomain(void) 431f2cb1360SIngo Molnar { 432f2cb1360SIngo Molnar struct root_domain *rd; 433f2cb1360SIngo Molnar 4344d13a06dSViresh Kumar rd = kzalloc(sizeof(*rd), GFP_KERNEL); 435f2cb1360SIngo Molnar if (!rd) 436f2cb1360SIngo Molnar return NULL; 437f2cb1360SIngo Molnar 438f2cb1360SIngo Molnar if (init_rootdomain(rd) != 0) { 439f2cb1360SIngo Molnar kfree(rd); 440f2cb1360SIngo Molnar return NULL; 441f2cb1360SIngo Molnar } 442f2cb1360SIngo Molnar 443f2cb1360SIngo Molnar return rd; 444f2cb1360SIngo Molnar } 445f2cb1360SIngo Molnar 446f2cb1360SIngo Molnar static void free_sched_groups(struct sched_group *sg, int free_sgc) 447f2cb1360SIngo Molnar { 448f2cb1360SIngo Molnar struct sched_group *tmp, *first; 449f2cb1360SIngo Molnar 450f2cb1360SIngo Molnar if (!sg) 451f2cb1360SIngo Molnar return; 452f2cb1360SIngo Molnar 453f2cb1360SIngo Molnar first = sg; 454f2cb1360SIngo Molnar do { 455f2cb1360SIngo Molnar tmp = sg->next; 456f2cb1360SIngo Molnar 457f2cb1360SIngo Molnar if (free_sgc && atomic_dec_and_test(&sg->sgc->ref)) 458f2cb1360SIngo Molnar kfree(sg->sgc); 459f2cb1360SIngo Molnar 460213c5a45SShu Wang if (atomic_dec_and_test(&sg->ref)) 461f2cb1360SIngo Molnar kfree(sg); 462f2cb1360SIngo Molnar sg = tmp; 463f2cb1360SIngo Molnar } while (sg != first); 464f2cb1360SIngo Molnar } 465f2cb1360SIngo Molnar 466f2cb1360SIngo Molnar static void destroy_sched_domain(struct sched_domain *sd) 467f2cb1360SIngo Molnar { 468f2cb1360SIngo Molnar /* 469a090c4f2SPeter Zijlstra * A normal sched domain may have multiple group references, an 470a090c4f2SPeter Zijlstra * overlapping domain, having private groups, only one. Iterate, 471a090c4f2SPeter Zijlstra * dropping group/capacity references, freeing where none remain. 472f2cb1360SIngo Molnar */ 473f2cb1360SIngo Molnar free_sched_groups(sd->groups, 1); 474213c5a45SShu Wang 475f2cb1360SIngo Molnar if (sd->shared && atomic_dec_and_test(&sd->shared->ref)) 476f2cb1360SIngo Molnar kfree(sd->shared); 477f2cb1360SIngo Molnar kfree(sd); 478f2cb1360SIngo Molnar } 479f2cb1360SIngo Molnar 480f2cb1360SIngo Molnar static void destroy_sched_domains_rcu(struct rcu_head *rcu) 481f2cb1360SIngo Molnar { 482f2cb1360SIngo Molnar struct sched_domain *sd = container_of(rcu, struct sched_domain, rcu); 483f2cb1360SIngo Molnar 484f2cb1360SIngo Molnar while (sd) { 485f2cb1360SIngo Molnar struct sched_domain *parent = sd->parent; 486f2cb1360SIngo Molnar destroy_sched_domain(sd); 487f2cb1360SIngo Molnar sd = parent; 488f2cb1360SIngo Molnar } 489f2cb1360SIngo Molnar } 490f2cb1360SIngo Molnar 491f2cb1360SIngo Molnar static void destroy_sched_domains(struct sched_domain *sd) 492f2cb1360SIngo Molnar { 493f2cb1360SIngo Molnar if (sd) 494f2cb1360SIngo Molnar call_rcu(&sd->rcu, destroy_sched_domains_rcu); 495f2cb1360SIngo Molnar } 496f2cb1360SIngo Molnar 497f2cb1360SIngo Molnar /* 498f2cb1360SIngo Molnar * Keep a special pointer to the highest sched_domain that has 499f2cb1360SIngo Molnar * SD_SHARE_PKG_RESOURCE set (Last Level Cache Domain) for this 500f2cb1360SIngo Molnar * allows us to avoid some pointer chasing select_idle_sibling(). 501f2cb1360SIngo Molnar * 502f2cb1360SIngo Molnar * Also keep a unique ID per domain (we use the first CPU number in 503f2cb1360SIngo Molnar * the cpumask of the domain), this allows us to quickly tell if 504f2cb1360SIngo Molnar * two CPUs are in the same cache domain, see cpus_share_cache(). 505f2cb1360SIngo Molnar */ 506f2cb1360SIngo Molnar DEFINE_PER_CPU(struct sched_domain *, sd_llc); 507f2cb1360SIngo Molnar DEFINE_PER_CPU(int, sd_llc_size); 508f2cb1360SIngo Molnar DEFINE_PER_CPU(int, sd_llc_id); 509f2cb1360SIngo Molnar DEFINE_PER_CPU(struct sched_domain_shared *, sd_llc_shared); 510f2cb1360SIngo Molnar DEFINE_PER_CPU(struct sched_domain *, sd_numa); 511f2cb1360SIngo Molnar DEFINE_PER_CPU(struct sched_domain *, sd_asym); 512df054e84SMorten Rasmussen DEFINE_STATIC_KEY_FALSE(sched_asym_cpucapacity); 513f2cb1360SIngo Molnar 514f2cb1360SIngo Molnar static void update_top_cache_domain(int cpu) 515f2cb1360SIngo Molnar { 516f2cb1360SIngo Molnar struct sched_domain_shared *sds = NULL; 517f2cb1360SIngo Molnar struct sched_domain *sd; 518f2cb1360SIngo Molnar int id = cpu; 519f2cb1360SIngo Molnar int size = 1; 520f2cb1360SIngo Molnar 521f2cb1360SIngo Molnar sd = highest_flag_domain(cpu, SD_SHARE_PKG_RESOURCES); 522f2cb1360SIngo Molnar if (sd) { 523f2cb1360SIngo Molnar id = cpumask_first(sched_domain_span(sd)); 524f2cb1360SIngo Molnar size = cpumask_weight(sched_domain_span(sd)); 525f2cb1360SIngo Molnar sds = sd->shared; 526f2cb1360SIngo Molnar } 527f2cb1360SIngo Molnar 528f2cb1360SIngo Molnar rcu_assign_pointer(per_cpu(sd_llc, cpu), sd); 529f2cb1360SIngo Molnar per_cpu(sd_llc_size, cpu) = size; 530f2cb1360SIngo Molnar per_cpu(sd_llc_id, cpu) = id; 531f2cb1360SIngo Molnar rcu_assign_pointer(per_cpu(sd_llc_shared, cpu), sds); 532f2cb1360SIngo Molnar 533f2cb1360SIngo Molnar sd = lowest_flag_domain(cpu, SD_NUMA); 534f2cb1360SIngo Molnar rcu_assign_pointer(per_cpu(sd_numa, cpu), sd); 535f2cb1360SIngo Molnar 536f2cb1360SIngo Molnar sd = highest_flag_domain(cpu, SD_ASYM_PACKING); 537f2cb1360SIngo Molnar rcu_assign_pointer(per_cpu(sd_asym, cpu), sd); 538f2cb1360SIngo Molnar } 539f2cb1360SIngo Molnar 540f2cb1360SIngo Molnar /* 541f2cb1360SIngo Molnar * Attach the domain 'sd' to 'cpu' as its base domain. Callers must 542f2cb1360SIngo Molnar * hold the hotplug lock. 543f2cb1360SIngo Molnar */ 544f2cb1360SIngo Molnar static void 545f2cb1360SIngo Molnar cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu) 546f2cb1360SIngo Molnar { 547f2cb1360SIngo Molnar struct rq *rq = cpu_rq(cpu); 548f2cb1360SIngo Molnar struct sched_domain *tmp; 549f2cb1360SIngo Molnar 550f2cb1360SIngo Molnar /* Remove the sched domains which do not contribute to scheduling. */ 551f2cb1360SIngo Molnar for (tmp = sd; tmp; ) { 552f2cb1360SIngo Molnar struct sched_domain *parent = tmp->parent; 553f2cb1360SIngo Molnar if (!parent) 554f2cb1360SIngo Molnar break; 555f2cb1360SIngo Molnar 556f2cb1360SIngo Molnar if (sd_parent_degenerate(tmp, parent)) { 557f2cb1360SIngo Molnar tmp->parent = parent->parent; 558f2cb1360SIngo Molnar if (parent->parent) 559f2cb1360SIngo Molnar parent->parent->child = tmp; 560f2cb1360SIngo Molnar /* 561f2cb1360SIngo Molnar * Transfer SD_PREFER_SIBLING down in case of a 562f2cb1360SIngo Molnar * degenerate parent; the spans match for this 563f2cb1360SIngo Molnar * so the property transfers. 564f2cb1360SIngo Molnar */ 565f2cb1360SIngo Molnar if (parent->flags & SD_PREFER_SIBLING) 566f2cb1360SIngo Molnar tmp->flags |= SD_PREFER_SIBLING; 567f2cb1360SIngo Molnar destroy_sched_domain(parent); 568f2cb1360SIngo Molnar } else 569f2cb1360SIngo Molnar tmp = tmp->parent; 570f2cb1360SIngo Molnar } 571f2cb1360SIngo Molnar 572f2cb1360SIngo Molnar if (sd && sd_degenerate(sd)) { 573f2cb1360SIngo Molnar tmp = sd; 574f2cb1360SIngo Molnar sd = sd->parent; 575f2cb1360SIngo Molnar destroy_sched_domain(tmp); 576f2cb1360SIngo Molnar if (sd) 577f2cb1360SIngo Molnar sd->child = NULL; 578f2cb1360SIngo Molnar } 579f2cb1360SIngo Molnar 580f2cb1360SIngo Molnar sched_domain_debug(sd, cpu); 581f2cb1360SIngo Molnar 582f2cb1360SIngo Molnar rq_attach_root(rq, rd); 583f2cb1360SIngo Molnar tmp = rq->sd; 584f2cb1360SIngo Molnar rcu_assign_pointer(rq->sd, sd); 585bbdacdfeSPeter Zijlstra dirty_sched_domain_sysctl(cpu); 586f2cb1360SIngo Molnar destroy_sched_domains(tmp); 587f2cb1360SIngo Molnar 588f2cb1360SIngo Molnar update_top_cache_domain(cpu); 589f2cb1360SIngo Molnar } 590f2cb1360SIngo Molnar 591f2cb1360SIngo Molnar struct s_data { 592f2cb1360SIngo Molnar struct sched_domain ** __percpu sd; 593f2cb1360SIngo Molnar struct root_domain *rd; 594f2cb1360SIngo Molnar }; 595f2cb1360SIngo Molnar 596f2cb1360SIngo Molnar enum s_alloc { 597f2cb1360SIngo Molnar sa_rootdomain, 598f2cb1360SIngo Molnar sa_sd, 599f2cb1360SIngo Molnar sa_sd_storage, 600f2cb1360SIngo Molnar sa_none, 601f2cb1360SIngo Molnar }; 602f2cb1360SIngo Molnar 603f2cb1360SIngo Molnar /* 60435a566e6SPeter Zijlstra * Return the canonical balance CPU for this group, this is the first CPU 605e5c14b1fSPeter Zijlstra * of this group that's also in the balance mask. 60635a566e6SPeter Zijlstra * 607e5c14b1fSPeter Zijlstra * The balance mask are all those CPUs that could actually end up at this 608e5c14b1fSPeter Zijlstra * group. See build_balance_mask(). 60935a566e6SPeter Zijlstra * 61035a566e6SPeter Zijlstra * Also see should_we_balance(). 61135a566e6SPeter Zijlstra */ 61235a566e6SPeter Zijlstra int group_balance_cpu(struct sched_group *sg) 61335a566e6SPeter Zijlstra { 614e5c14b1fSPeter Zijlstra return cpumask_first(group_balance_mask(sg)); 61535a566e6SPeter Zijlstra } 61635a566e6SPeter Zijlstra 61735a566e6SPeter Zijlstra 61835a566e6SPeter Zijlstra /* 61935a566e6SPeter Zijlstra * NUMA topology (first read the regular topology blurb below) 62035a566e6SPeter Zijlstra * 62135a566e6SPeter Zijlstra * Given a node-distance table, for example: 62235a566e6SPeter Zijlstra * 62335a566e6SPeter Zijlstra * node 0 1 2 3 62435a566e6SPeter Zijlstra * 0: 10 20 30 20 62535a566e6SPeter Zijlstra * 1: 20 10 20 30 62635a566e6SPeter Zijlstra * 2: 30 20 10 20 62735a566e6SPeter Zijlstra * 3: 20 30 20 10 62835a566e6SPeter Zijlstra * 62935a566e6SPeter Zijlstra * which represents a 4 node ring topology like: 63035a566e6SPeter Zijlstra * 63135a566e6SPeter Zijlstra * 0 ----- 1 63235a566e6SPeter Zijlstra * | | 63335a566e6SPeter Zijlstra * | | 63435a566e6SPeter Zijlstra * | | 63535a566e6SPeter Zijlstra * 3 ----- 2 63635a566e6SPeter Zijlstra * 63735a566e6SPeter Zijlstra * We want to construct domains and groups to represent this. The way we go 63835a566e6SPeter Zijlstra * about doing this is to build the domains on 'hops'. For each NUMA level we 63935a566e6SPeter Zijlstra * construct the mask of all nodes reachable in @level hops. 64035a566e6SPeter Zijlstra * 64135a566e6SPeter Zijlstra * For the above NUMA topology that gives 3 levels: 64235a566e6SPeter Zijlstra * 64335a566e6SPeter Zijlstra * NUMA-2 0-3 0-3 0-3 0-3 64435a566e6SPeter Zijlstra * groups: {0-1,3},{1-3} {0-2},{0,2-3} {1-3},{0-1,3} {0,2-3},{0-2} 64535a566e6SPeter Zijlstra * 64635a566e6SPeter Zijlstra * NUMA-1 0-1,3 0-2 1-3 0,2-3 64735a566e6SPeter Zijlstra * groups: {0},{1},{3} {0},{1},{2} {1},{2},{3} {0},{2},{3} 64835a566e6SPeter Zijlstra * 64935a566e6SPeter Zijlstra * NUMA-0 0 1 2 3 65035a566e6SPeter Zijlstra * 65135a566e6SPeter Zijlstra * 65235a566e6SPeter Zijlstra * As can be seen; things don't nicely line up as with the regular topology. 65335a566e6SPeter Zijlstra * When we iterate a domain in child domain chunks some nodes can be 65435a566e6SPeter Zijlstra * represented multiple times -- hence the "overlap" naming for this part of 65535a566e6SPeter Zijlstra * the topology. 65635a566e6SPeter Zijlstra * 65735a566e6SPeter Zijlstra * In order to minimize this overlap, we only build enough groups to cover the 65835a566e6SPeter Zijlstra * domain. For instance Node-0 NUMA-2 would only get groups: 0-1,3 and 1-3. 65935a566e6SPeter Zijlstra * 66035a566e6SPeter Zijlstra * Because: 66135a566e6SPeter Zijlstra * 66235a566e6SPeter Zijlstra * - the first group of each domain is its child domain; this 66335a566e6SPeter Zijlstra * gets us the first 0-1,3 66435a566e6SPeter Zijlstra * - the only uncovered node is 2, who's child domain is 1-3. 66535a566e6SPeter Zijlstra * 66635a566e6SPeter Zijlstra * However, because of the overlap, computing a unique CPU for each group is 66735a566e6SPeter Zijlstra * more complicated. Consider for instance the groups of NODE-1 NUMA-2, both 66835a566e6SPeter Zijlstra * groups include the CPUs of Node-0, while those CPUs would not in fact ever 66935a566e6SPeter Zijlstra * end up at those groups (they would end up in group: 0-1,3). 67035a566e6SPeter Zijlstra * 671e5c14b1fSPeter Zijlstra * To correct this we have to introduce the group balance mask. This mask 67235a566e6SPeter Zijlstra * will contain those CPUs in the group that can reach this group given the 67335a566e6SPeter Zijlstra * (child) domain tree. 67435a566e6SPeter Zijlstra * 67535a566e6SPeter Zijlstra * With this we can once again compute balance_cpu and sched_group_capacity 67635a566e6SPeter Zijlstra * relations. 67735a566e6SPeter Zijlstra * 67835a566e6SPeter Zijlstra * XXX include words on how balance_cpu is unique and therefore can be 67935a566e6SPeter Zijlstra * used for sched_group_capacity links. 68035a566e6SPeter Zijlstra * 68135a566e6SPeter Zijlstra * 68235a566e6SPeter Zijlstra * Another 'interesting' topology is: 68335a566e6SPeter Zijlstra * 68435a566e6SPeter Zijlstra * node 0 1 2 3 68535a566e6SPeter Zijlstra * 0: 10 20 20 30 68635a566e6SPeter Zijlstra * 1: 20 10 20 20 68735a566e6SPeter Zijlstra * 2: 20 20 10 20 68835a566e6SPeter Zijlstra * 3: 30 20 20 10 68935a566e6SPeter Zijlstra * 69035a566e6SPeter Zijlstra * Which looks a little like: 69135a566e6SPeter Zijlstra * 69235a566e6SPeter Zijlstra * 0 ----- 1 69335a566e6SPeter Zijlstra * | / | 69435a566e6SPeter Zijlstra * | / | 69535a566e6SPeter Zijlstra * | / | 69635a566e6SPeter Zijlstra * 2 ----- 3 69735a566e6SPeter Zijlstra * 69835a566e6SPeter Zijlstra * This topology is asymmetric, nodes 1,2 are fully connected, but nodes 0,3 69935a566e6SPeter Zijlstra * are not. 70035a566e6SPeter Zijlstra * 70135a566e6SPeter Zijlstra * This leads to a few particularly weird cases where the sched_domain's are 70297fb7a0aSIngo Molnar * not of the same number for each CPU. Consider: 70335a566e6SPeter Zijlstra * 70435a566e6SPeter Zijlstra * NUMA-2 0-3 0-3 70535a566e6SPeter Zijlstra * groups: {0-2},{1-3} {1-3},{0-2} 70635a566e6SPeter Zijlstra * 70735a566e6SPeter Zijlstra * NUMA-1 0-2 0-3 0-3 1-3 70835a566e6SPeter Zijlstra * 70935a566e6SPeter Zijlstra * NUMA-0 0 1 2 3 71035a566e6SPeter Zijlstra * 71135a566e6SPeter Zijlstra */ 71235a566e6SPeter Zijlstra 71335a566e6SPeter Zijlstra 71435a566e6SPeter Zijlstra /* 715e5c14b1fSPeter Zijlstra * Build the balance mask; it contains only those CPUs that can arrive at this 716e5c14b1fSPeter Zijlstra * group and should be considered to continue balancing. 71735a566e6SPeter Zijlstra * 71835a566e6SPeter Zijlstra * We do this during the group creation pass, therefore the group information 71935a566e6SPeter Zijlstra * isn't complete yet, however since each group represents a (child) domain we 72035a566e6SPeter Zijlstra * can fully construct this using the sched_domain bits (which are already 72135a566e6SPeter Zijlstra * complete). 722f2cb1360SIngo Molnar */ 7231676330eSPeter Zijlstra static void 724e5c14b1fSPeter Zijlstra build_balance_mask(struct sched_domain *sd, struct sched_group *sg, struct cpumask *mask) 725f2cb1360SIngo Molnar { 726ae4df9d6SPeter Zijlstra const struct cpumask *sg_span = sched_group_span(sg); 727f2cb1360SIngo Molnar struct sd_data *sdd = sd->private; 728f2cb1360SIngo Molnar struct sched_domain *sibling; 729f2cb1360SIngo Molnar int i; 730f2cb1360SIngo Molnar 7311676330eSPeter Zijlstra cpumask_clear(mask); 7321676330eSPeter Zijlstra 733f32d782eSLauro Ramos Venancio for_each_cpu(i, sg_span) { 734f2cb1360SIngo Molnar sibling = *per_cpu_ptr(sdd->sd, i); 73573bb059fSPeter Zijlstra 73673bb059fSPeter Zijlstra /* 73773bb059fSPeter Zijlstra * Can happen in the asymmetric case, where these siblings are 73873bb059fSPeter Zijlstra * unused. The mask will not be empty because those CPUs that 73973bb059fSPeter Zijlstra * do have the top domain _should_ span the domain. 74073bb059fSPeter Zijlstra */ 74173bb059fSPeter Zijlstra if (!sibling->child) 74273bb059fSPeter Zijlstra continue; 74373bb059fSPeter Zijlstra 74473bb059fSPeter Zijlstra /* If we would not end up here, we can't continue from here */ 74573bb059fSPeter Zijlstra if (!cpumask_equal(sg_span, sched_domain_span(sibling->child))) 746f2cb1360SIngo Molnar continue; 747f2cb1360SIngo Molnar 7481676330eSPeter Zijlstra cpumask_set_cpu(i, mask); 749f2cb1360SIngo Molnar } 75073bb059fSPeter Zijlstra 75173bb059fSPeter Zijlstra /* We must not have empty masks here */ 7521676330eSPeter Zijlstra WARN_ON_ONCE(cpumask_empty(mask)); 753f2cb1360SIngo Molnar } 754f2cb1360SIngo Molnar 755f2cb1360SIngo Molnar /* 75635a566e6SPeter Zijlstra * XXX: This creates per-node group entries; since the load-balancer will 75735a566e6SPeter Zijlstra * immediately access remote memory to construct this group's load-balance 75835a566e6SPeter Zijlstra * statistics having the groups node local is of dubious benefit. 759f2cb1360SIngo Molnar */ 7608c033469SLauro Ramos Venancio static struct sched_group * 7618c033469SLauro Ramos Venancio build_group_from_child_sched_domain(struct sched_domain *sd, int cpu) 7628c033469SLauro Ramos Venancio { 7638c033469SLauro Ramos Venancio struct sched_group *sg; 7648c033469SLauro Ramos Venancio struct cpumask *sg_span; 7658c033469SLauro Ramos Venancio 7668c033469SLauro Ramos Venancio sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(), 7678c033469SLauro Ramos Venancio GFP_KERNEL, cpu_to_node(cpu)); 7688c033469SLauro Ramos Venancio 7698c033469SLauro Ramos Venancio if (!sg) 7708c033469SLauro Ramos Venancio return NULL; 7718c033469SLauro Ramos Venancio 772ae4df9d6SPeter Zijlstra sg_span = sched_group_span(sg); 7738c033469SLauro Ramos Venancio if (sd->child) 7748c033469SLauro Ramos Venancio cpumask_copy(sg_span, sched_domain_span(sd->child)); 7758c033469SLauro Ramos Venancio else 7768c033469SLauro Ramos Venancio cpumask_copy(sg_span, sched_domain_span(sd)); 7778c033469SLauro Ramos Venancio 778213c5a45SShu Wang atomic_inc(&sg->ref); 7798c033469SLauro Ramos Venancio return sg; 7808c033469SLauro Ramos Venancio } 7818c033469SLauro Ramos Venancio 7828c033469SLauro Ramos Venancio static void init_overlap_sched_group(struct sched_domain *sd, 7831676330eSPeter Zijlstra struct sched_group *sg) 7848c033469SLauro Ramos Venancio { 7851676330eSPeter Zijlstra struct cpumask *mask = sched_domains_tmpmask2; 7868c033469SLauro Ramos Venancio struct sd_data *sdd = sd->private; 7878c033469SLauro Ramos Venancio struct cpumask *sg_span; 7881676330eSPeter Zijlstra int cpu; 7891676330eSPeter Zijlstra 790e5c14b1fSPeter Zijlstra build_balance_mask(sd, sg, mask); 791ae4df9d6SPeter Zijlstra cpu = cpumask_first_and(sched_group_span(sg), mask); 7928c033469SLauro Ramos Venancio 7938c033469SLauro Ramos Venancio sg->sgc = *per_cpu_ptr(sdd->sgc, cpu); 7948c033469SLauro Ramos Venancio if (atomic_inc_return(&sg->sgc->ref) == 1) 795e5c14b1fSPeter Zijlstra cpumask_copy(group_balance_mask(sg), mask); 79635a566e6SPeter Zijlstra else 797e5c14b1fSPeter Zijlstra WARN_ON_ONCE(!cpumask_equal(group_balance_mask(sg), mask)); 7988c033469SLauro Ramos Venancio 7998c033469SLauro Ramos Venancio /* 8008c033469SLauro Ramos Venancio * Initialize sgc->capacity such that even if we mess up the 8018c033469SLauro Ramos Venancio * domains and no possible iteration will get us here, we won't 8028c033469SLauro Ramos Venancio * die on a /0 trap. 8038c033469SLauro Ramos Venancio */ 804ae4df9d6SPeter Zijlstra sg_span = sched_group_span(sg); 8058c033469SLauro Ramos Venancio sg->sgc->capacity = SCHED_CAPACITY_SCALE * cpumask_weight(sg_span); 8068c033469SLauro Ramos Venancio sg->sgc->min_capacity = SCHED_CAPACITY_SCALE; 807e3d6d0cbSMorten Rasmussen sg->sgc->max_capacity = SCHED_CAPACITY_SCALE; 8088c033469SLauro Ramos Venancio } 8098c033469SLauro Ramos Venancio 810f2cb1360SIngo Molnar static int 811f2cb1360SIngo Molnar build_overlap_sched_groups(struct sched_domain *sd, int cpu) 812f2cb1360SIngo Molnar { 81391eaed0dSPeter Zijlstra struct sched_group *first = NULL, *last = NULL, *sg; 814f2cb1360SIngo Molnar const struct cpumask *span = sched_domain_span(sd); 815f2cb1360SIngo Molnar struct cpumask *covered = sched_domains_tmpmask; 816f2cb1360SIngo Molnar struct sd_data *sdd = sd->private; 817f2cb1360SIngo Molnar struct sched_domain *sibling; 818f2cb1360SIngo Molnar int i; 819f2cb1360SIngo Molnar 820f2cb1360SIngo Molnar cpumask_clear(covered); 821f2cb1360SIngo Molnar 8220372dd27SPeter Zijlstra for_each_cpu_wrap(i, span, cpu) { 823f2cb1360SIngo Molnar struct cpumask *sg_span; 824f2cb1360SIngo Molnar 825f2cb1360SIngo Molnar if (cpumask_test_cpu(i, covered)) 826f2cb1360SIngo Molnar continue; 827f2cb1360SIngo Molnar 828f2cb1360SIngo Molnar sibling = *per_cpu_ptr(sdd->sd, i); 829f2cb1360SIngo Molnar 830c20e1ea4SLauro Ramos Venancio /* 831c20e1ea4SLauro Ramos Venancio * Asymmetric node setups can result in situations where the 832c20e1ea4SLauro Ramos Venancio * domain tree is of unequal depth, make sure to skip domains 833c20e1ea4SLauro Ramos Venancio * that already cover the entire range. 834c20e1ea4SLauro Ramos Venancio * 835c20e1ea4SLauro Ramos Venancio * In that case build_sched_domains() will have terminated the 836c20e1ea4SLauro Ramos Venancio * iteration early and our sibling sd spans will be empty. 837c20e1ea4SLauro Ramos Venancio * Domains should always include the CPU they're built on, so 838c20e1ea4SLauro Ramos Venancio * check that. 839c20e1ea4SLauro Ramos Venancio */ 840f2cb1360SIngo Molnar if (!cpumask_test_cpu(i, sched_domain_span(sibling))) 841f2cb1360SIngo Molnar continue; 842f2cb1360SIngo Molnar 8438c033469SLauro Ramos Venancio sg = build_group_from_child_sched_domain(sibling, cpu); 844f2cb1360SIngo Molnar if (!sg) 845f2cb1360SIngo Molnar goto fail; 846f2cb1360SIngo Molnar 847ae4df9d6SPeter Zijlstra sg_span = sched_group_span(sg); 848f2cb1360SIngo Molnar cpumask_or(covered, covered, sg_span); 849f2cb1360SIngo Molnar 8501676330eSPeter Zijlstra init_overlap_sched_group(sd, sg); 851f2cb1360SIngo Molnar 852f2cb1360SIngo Molnar if (!first) 853f2cb1360SIngo Molnar first = sg; 854f2cb1360SIngo Molnar if (last) 855f2cb1360SIngo Molnar last->next = sg; 856f2cb1360SIngo Molnar last = sg; 857f2cb1360SIngo Molnar last->next = first; 858f2cb1360SIngo Molnar } 85991eaed0dSPeter Zijlstra sd->groups = first; 860f2cb1360SIngo Molnar 861f2cb1360SIngo Molnar return 0; 862f2cb1360SIngo Molnar 863f2cb1360SIngo Molnar fail: 864f2cb1360SIngo Molnar free_sched_groups(first, 0); 865f2cb1360SIngo Molnar 866f2cb1360SIngo Molnar return -ENOMEM; 867f2cb1360SIngo Molnar } 868f2cb1360SIngo Molnar 86935a566e6SPeter Zijlstra 87035a566e6SPeter Zijlstra /* 87135a566e6SPeter Zijlstra * Package topology (also see the load-balance blurb in fair.c) 87235a566e6SPeter Zijlstra * 87335a566e6SPeter Zijlstra * The scheduler builds a tree structure to represent a number of important 87435a566e6SPeter Zijlstra * topology features. By default (default_topology[]) these include: 87535a566e6SPeter Zijlstra * 87635a566e6SPeter Zijlstra * - Simultaneous multithreading (SMT) 87735a566e6SPeter Zijlstra * - Multi-Core Cache (MC) 87835a566e6SPeter Zijlstra * - Package (DIE) 87935a566e6SPeter Zijlstra * 88035a566e6SPeter Zijlstra * Where the last one more or less denotes everything up to a NUMA node. 88135a566e6SPeter Zijlstra * 88235a566e6SPeter Zijlstra * The tree consists of 3 primary data structures: 88335a566e6SPeter Zijlstra * 88435a566e6SPeter Zijlstra * sched_domain -> sched_group -> sched_group_capacity 88535a566e6SPeter Zijlstra * ^ ^ ^ ^ 88635a566e6SPeter Zijlstra * `-' `-' 88735a566e6SPeter Zijlstra * 88897fb7a0aSIngo Molnar * The sched_domains are per-CPU and have a two way link (parent & child) and 88935a566e6SPeter Zijlstra * denote the ever growing mask of CPUs belonging to that level of topology. 89035a566e6SPeter Zijlstra * 89135a566e6SPeter Zijlstra * Each sched_domain has a circular (double) linked list of sched_group's, each 89235a566e6SPeter Zijlstra * denoting the domains of the level below (or individual CPUs in case of the 89335a566e6SPeter Zijlstra * first domain level). The sched_group linked by a sched_domain includes the 89435a566e6SPeter Zijlstra * CPU of that sched_domain [*]. 89535a566e6SPeter Zijlstra * 89635a566e6SPeter Zijlstra * Take for instance a 2 threaded, 2 core, 2 cache cluster part: 89735a566e6SPeter Zijlstra * 89835a566e6SPeter Zijlstra * CPU 0 1 2 3 4 5 6 7 89935a566e6SPeter Zijlstra * 90035a566e6SPeter Zijlstra * DIE [ ] 90135a566e6SPeter Zijlstra * MC [ ] [ ] 90235a566e6SPeter Zijlstra * SMT [ ] [ ] [ ] [ ] 90335a566e6SPeter Zijlstra * 90435a566e6SPeter Zijlstra * - or - 90535a566e6SPeter Zijlstra * 90635a566e6SPeter Zijlstra * DIE 0-7 0-7 0-7 0-7 0-7 0-7 0-7 0-7 90735a566e6SPeter Zijlstra * MC 0-3 0-3 0-3 0-3 4-7 4-7 4-7 4-7 90835a566e6SPeter Zijlstra * SMT 0-1 0-1 2-3 2-3 4-5 4-5 6-7 6-7 90935a566e6SPeter Zijlstra * 91035a566e6SPeter Zijlstra * CPU 0 1 2 3 4 5 6 7 91135a566e6SPeter Zijlstra * 91235a566e6SPeter Zijlstra * One way to think about it is: sched_domain moves you up and down among these 91335a566e6SPeter Zijlstra * topology levels, while sched_group moves you sideways through it, at child 91435a566e6SPeter Zijlstra * domain granularity. 91535a566e6SPeter Zijlstra * 91635a566e6SPeter Zijlstra * sched_group_capacity ensures each unique sched_group has shared storage. 91735a566e6SPeter Zijlstra * 91835a566e6SPeter Zijlstra * There are two related construction problems, both require a CPU that 91935a566e6SPeter Zijlstra * uniquely identify each group (for a given domain): 92035a566e6SPeter Zijlstra * 92135a566e6SPeter Zijlstra * - The first is the balance_cpu (see should_we_balance() and the 92235a566e6SPeter Zijlstra * load-balance blub in fair.c); for each group we only want 1 CPU to 92335a566e6SPeter Zijlstra * continue balancing at a higher domain. 92435a566e6SPeter Zijlstra * 92535a566e6SPeter Zijlstra * - The second is the sched_group_capacity; we want all identical groups 92635a566e6SPeter Zijlstra * to share a single sched_group_capacity. 92735a566e6SPeter Zijlstra * 92835a566e6SPeter Zijlstra * Since these topologies are exclusive by construction. That is, its 92935a566e6SPeter Zijlstra * impossible for an SMT thread to belong to multiple cores, and cores to 93035a566e6SPeter Zijlstra * be part of multiple caches. There is a very clear and unique location 93135a566e6SPeter Zijlstra * for each CPU in the hierarchy. 93235a566e6SPeter Zijlstra * 93335a566e6SPeter Zijlstra * Therefore computing a unique CPU for each group is trivial (the iteration 93435a566e6SPeter Zijlstra * mask is redundant and set all 1s; all CPUs in a group will end up at _that_ 93535a566e6SPeter Zijlstra * group), we can simply pick the first CPU in each group. 93635a566e6SPeter Zijlstra * 93735a566e6SPeter Zijlstra * 93835a566e6SPeter Zijlstra * [*] in other words, the first group of each domain is its child domain. 93935a566e6SPeter Zijlstra */ 94035a566e6SPeter Zijlstra 9410c0e776aSPeter Zijlstra static struct sched_group *get_group(int cpu, struct sd_data *sdd) 942f2cb1360SIngo Molnar { 943f2cb1360SIngo Molnar struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu); 944f2cb1360SIngo Molnar struct sched_domain *child = sd->child; 9450c0e776aSPeter Zijlstra struct sched_group *sg; 946f2cb1360SIngo Molnar 947f2cb1360SIngo Molnar if (child) 948f2cb1360SIngo Molnar cpu = cpumask_first(sched_domain_span(child)); 949f2cb1360SIngo Molnar 9500c0e776aSPeter Zijlstra sg = *per_cpu_ptr(sdd->sg, cpu); 9510c0e776aSPeter Zijlstra sg->sgc = *per_cpu_ptr(sdd->sgc, cpu); 952f2cb1360SIngo Molnar 953f2cb1360SIngo Molnar /* For claim_allocations: */ 9540c0e776aSPeter Zijlstra atomic_inc(&sg->ref); 9550c0e776aSPeter Zijlstra atomic_inc(&sg->sgc->ref); 9560c0e776aSPeter Zijlstra 9570c0e776aSPeter Zijlstra if (child) { 958ae4df9d6SPeter Zijlstra cpumask_copy(sched_group_span(sg), sched_domain_span(child)); 959ae4df9d6SPeter Zijlstra cpumask_copy(group_balance_mask(sg), sched_group_span(sg)); 9600c0e776aSPeter Zijlstra } else { 961ae4df9d6SPeter Zijlstra cpumask_set_cpu(cpu, sched_group_span(sg)); 962e5c14b1fSPeter Zijlstra cpumask_set_cpu(cpu, group_balance_mask(sg)); 963f2cb1360SIngo Molnar } 964f2cb1360SIngo Molnar 965ae4df9d6SPeter Zijlstra sg->sgc->capacity = SCHED_CAPACITY_SCALE * cpumask_weight(sched_group_span(sg)); 9660c0e776aSPeter Zijlstra sg->sgc->min_capacity = SCHED_CAPACITY_SCALE; 967e3d6d0cbSMorten Rasmussen sg->sgc->max_capacity = SCHED_CAPACITY_SCALE; 9680c0e776aSPeter Zijlstra 9690c0e776aSPeter Zijlstra return sg; 970f2cb1360SIngo Molnar } 971f2cb1360SIngo Molnar 972f2cb1360SIngo Molnar /* 973f2cb1360SIngo Molnar * build_sched_groups will build a circular linked list of the groups 974f2cb1360SIngo Molnar * covered by the given span, and will set each group's ->cpumask correctly, 975f2cb1360SIngo Molnar * and ->cpu_capacity to 0. 976f2cb1360SIngo Molnar * 977f2cb1360SIngo Molnar * Assumes the sched_domain tree is fully constructed 978f2cb1360SIngo Molnar */ 979f2cb1360SIngo Molnar static int 980f2cb1360SIngo Molnar build_sched_groups(struct sched_domain *sd, int cpu) 981f2cb1360SIngo Molnar { 982f2cb1360SIngo Molnar struct sched_group *first = NULL, *last = NULL; 983f2cb1360SIngo Molnar struct sd_data *sdd = sd->private; 984f2cb1360SIngo Molnar const struct cpumask *span = sched_domain_span(sd); 985f2cb1360SIngo Molnar struct cpumask *covered; 986f2cb1360SIngo Molnar int i; 987f2cb1360SIngo Molnar 988f2cb1360SIngo Molnar lockdep_assert_held(&sched_domains_mutex); 989f2cb1360SIngo Molnar covered = sched_domains_tmpmask; 990f2cb1360SIngo Molnar 991f2cb1360SIngo Molnar cpumask_clear(covered); 992f2cb1360SIngo Molnar 9930c0e776aSPeter Zijlstra for_each_cpu_wrap(i, span, cpu) { 994f2cb1360SIngo Molnar struct sched_group *sg; 995f2cb1360SIngo Molnar 996f2cb1360SIngo Molnar if (cpumask_test_cpu(i, covered)) 997f2cb1360SIngo Molnar continue; 998f2cb1360SIngo Molnar 9990c0e776aSPeter Zijlstra sg = get_group(i, sdd); 1000f2cb1360SIngo Molnar 1001ae4df9d6SPeter Zijlstra cpumask_or(covered, covered, sched_group_span(sg)); 1002f2cb1360SIngo Molnar 1003f2cb1360SIngo Molnar if (!first) 1004f2cb1360SIngo Molnar first = sg; 1005f2cb1360SIngo Molnar if (last) 1006f2cb1360SIngo Molnar last->next = sg; 1007f2cb1360SIngo Molnar last = sg; 1008f2cb1360SIngo Molnar } 1009f2cb1360SIngo Molnar last->next = first; 10100c0e776aSPeter Zijlstra sd->groups = first; 1011f2cb1360SIngo Molnar 1012f2cb1360SIngo Molnar return 0; 1013f2cb1360SIngo Molnar } 1014f2cb1360SIngo Molnar 1015f2cb1360SIngo Molnar /* 1016f2cb1360SIngo Molnar * Initialize sched groups cpu_capacity. 1017f2cb1360SIngo Molnar * 1018f2cb1360SIngo Molnar * cpu_capacity indicates the capacity of sched group, which is used while 1019f2cb1360SIngo Molnar * distributing the load between different sched groups in a sched domain. 1020f2cb1360SIngo Molnar * Typically cpu_capacity for all the groups in a sched domain will be same 1021f2cb1360SIngo Molnar * unless there are asymmetries in the topology. If there are asymmetries, 1022f2cb1360SIngo Molnar * group having more cpu_capacity will pickup more load compared to the 1023f2cb1360SIngo Molnar * group having less cpu_capacity. 1024f2cb1360SIngo Molnar */ 1025f2cb1360SIngo Molnar static void init_sched_groups_capacity(int cpu, struct sched_domain *sd) 1026f2cb1360SIngo Molnar { 1027f2cb1360SIngo Molnar struct sched_group *sg = sd->groups; 1028f2cb1360SIngo Molnar 1029f2cb1360SIngo Molnar WARN_ON(!sg); 1030f2cb1360SIngo Molnar 1031f2cb1360SIngo Molnar do { 1032f2cb1360SIngo Molnar int cpu, max_cpu = -1; 1033f2cb1360SIngo Molnar 1034ae4df9d6SPeter Zijlstra sg->group_weight = cpumask_weight(sched_group_span(sg)); 1035f2cb1360SIngo Molnar 1036f2cb1360SIngo Molnar if (!(sd->flags & SD_ASYM_PACKING)) 1037f2cb1360SIngo Molnar goto next; 1038f2cb1360SIngo Molnar 1039ae4df9d6SPeter Zijlstra for_each_cpu(cpu, sched_group_span(sg)) { 1040f2cb1360SIngo Molnar if (max_cpu < 0) 1041f2cb1360SIngo Molnar max_cpu = cpu; 1042f2cb1360SIngo Molnar else if (sched_asym_prefer(cpu, max_cpu)) 1043f2cb1360SIngo Molnar max_cpu = cpu; 1044f2cb1360SIngo Molnar } 1045f2cb1360SIngo Molnar sg->asym_prefer_cpu = max_cpu; 1046f2cb1360SIngo Molnar 1047f2cb1360SIngo Molnar next: 1048f2cb1360SIngo Molnar sg = sg->next; 1049f2cb1360SIngo Molnar } while (sg != sd->groups); 1050f2cb1360SIngo Molnar 1051f2cb1360SIngo Molnar if (cpu != group_balance_cpu(sg)) 1052f2cb1360SIngo Molnar return; 1053f2cb1360SIngo Molnar 1054f2cb1360SIngo Molnar update_group_capacity(sd, cpu); 1055f2cb1360SIngo Molnar } 1056f2cb1360SIngo Molnar 1057f2cb1360SIngo Molnar /* 1058f2cb1360SIngo Molnar * Initializers for schedule domains 1059f2cb1360SIngo Molnar * Non-inlined to reduce accumulated stack pressure in build_sched_domains() 1060f2cb1360SIngo Molnar */ 1061f2cb1360SIngo Molnar 1062f2cb1360SIngo Molnar static int default_relax_domain_level = -1; 1063f2cb1360SIngo Molnar int sched_domain_level_max; 1064f2cb1360SIngo Molnar 1065f2cb1360SIngo Molnar static int __init setup_relax_domain_level(char *str) 1066f2cb1360SIngo Molnar { 1067f2cb1360SIngo Molnar if (kstrtoint(str, 0, &default_relax_domain_level)) 1068f2cb1360SIngo Molnar pr_warn("Unable to set relax_domain_level\n"); 1069f2cb1360SIngo Molnar 1070f2cb1360SIngo Molnar return 1; 1071f2cb1360SIngo Molnar } 1072f2cb1360SIngo Molnar __setup("relax_domain_level=", setup_relax_domain_level); 1073f2cb1360SIngo Molnar 1074f2cb1360SIngo Molnar static void set_domain_attribute(struct sched_domain *sd, 1075f2cb1360SIngo Molnar struct sched_domain_attr *attr) 1076f2cb1360SIngo Molnar { 1077f2cb1360SIngo Molnar int request; 1078f2cb1360SIngo Molnar 1079f2cb1360SIngo Molnar if (!attr || attr->relax_domain_level < 0) { 1080f2cb1360SIngo Molnar if (default_relax_domain_level < 0) 1081f2cb1360SIngo Molnar return; 1082f2cb1360SIngo Molnar else 1083f2cb1360SIngo Molnar request = default_relax_domain_level; 1084f2cb1360SIngo Molnar } else 1085f2cb1360SIngo Molnar request = attr->relax_domain_level; 1086f2cb1360SIngo Molnar if (request < sd->level) { 1087f2cb1360SIngo Molnar /* Turn off idle balance on this domain: */ 1088f2cb1360SIngo Molnar sd->flags &= ~(SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE); 1089f2cb1360SIngo Molnar } else { 1090f2cb1360SIngo Molnar /* Turn on idle balance on this domain: */ 1091f2cb1360SIngo Molnar sd->flags |= (SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE); 1092f2cb1360SIngo Molnar } 1093f2cb1360SIngo Molnar } 1094f2cb1360SIngo Molnar 1095f2cb1360SIngo Molnar static void __sdt_free(const struct cpumask *cpu_map); 1096f2cb1360SIngo Molnar static int __sdt_alloc(const struct cpumask *cpu_map); 1097f2cb1360SIngo Molnar 1098f2cb1360SIngo Molnar static void __free_domain_allocs(struct s_data *d, enum s_alloc what, 1099f2cb1360SIngo Molnar const struct cpumask *cpu_map) 1100f2cb1360SIngo Molnar { 1101f2cb1360SIngo Molnar switch (what) { 1102f2cb1360SIngo Molnar case sa_rootdomain: 1103f2cb1360SIngo Molnar if (!atomic_read(&d->rd->refcount)) 1104f2cb1360SIngo Molnar free_rootdomain(&d->rd->rcu); 1105f2cb1360SIngo Molnar /* Fall through */ 1106f2cb1360SIngo Molnar case sa_sd: 1107f2cb1360SIngo Molnar free_percpu(d->sd); 1108f2cb1360SIngo Molnar /* Fall through */ 1109f2cb1360SIngo Molnar case sa_sd_storage: 1110f2cb1360SIngo Molnar __sdt_free(cpu_map); 1111f2cb1360SIngo Molnar /* Fall through */ 1112f2cb1360SIngo Molnar case sa_none: 1113f2cb1360SIngo Molnar break; 1114f2cb1360SIngo Molnar } 1115f2cb1360SIngo Molnar } 1116f2cb1360SIngo Molnar 1117f2cb1360SIngo Molnar static enum s_alloc 1118f2cb1360SIngo Molnar __visit_domain_allocation_hell(struct s_data *d, const struct cpumask *cpu_map) 1119f2cb1360SIngo Molnar { 1120f2cb1360SIngo Molnar memset(d, 0, sizeof(*d)); 1121f2cb1360SIngo Molnar 1122f2cb1360SIngo Molnar if (__sdt_alloc(cpu_map)) 1123f2cb1360SIngo Molnar return sa_sd_storage; 1124f2cb1360SIngo Molnar d->sd = alloc_percpu(struct sched_domain *); 1125f2cb1360SIngo Molnar if (!d->sd) 1126f2cb1360SIngo Molnar return sa_sd_storage; 1127f2cb1360SIngo Molnar d->rd = alloc_rootdomain(); 1128f2cb1360SIngo Molnar if (!d->rd) 1129f2cb1360SIngo Molnar return sa_sd; 113097fb7a0aSIngo Molnar 1131f2cb1360SIngo Molnar return sa_rootdomain; 1132f2cb1360SIngo Molnar } 1133f2cb1360SIngo Molnar 1134f2cb1360SIngo Molnar /* 1135f2cb1360SIngo Molnar * NULL the sd_data elements we've used to build the sched_domain and 1136f2cb1360SIngo Molnar * sched_group structure so that the subsequent __free_domain_allocs() 1137f2cb1360SIngo Molnar * will not free the data we're using. 1138f2cb1360SIngo Molnar */ 1139f2cb1360SIngo Molnar static void claim_allocations(int cpu, struct sched_domain *sd) 1140f2cb1360SIngo Molnar { 1141f2cb1360SIngo Molnar struct sd_data *sdd = sd->private; 1142f2cb1360SIngo Molnar 1143f2cb1360SIngo Molnar WARN_ON_ONCE(*per_cpu_ptr(sdd->sd, cpu) != sd); 1144f2cb1360SIngo Molnar *per_cpu_ptr(sdd->sd, cpu) = NULL; 1145f2cb1360SIngo Molnar 1146f2cb1360SIngo Molnar if (atomic_read(&(*per_cpu_ptr(sdd->sds, cpu))->ref)) 1147f2cb1360SIngo Molnar *per_cpu_ptr(sdd->sds, cpu) = NULL; 1148f2cb1360SIngo Molnar 1149f2cb1360SIngo Molnar if (atomic_read(&(*per_cpu_ptr(sdd->sg, cpu))->ref)) 1150f2cb1360SIngo Molnar *per_cpu_ptr(sdd->sg, cpu) = NULL; 1151f2cb1360SIngo Molnar 1152f2cb1360SIngo Molnar if (atomic_read(&(*per_cpu_ptr(sdd->sgc, cpu))->ref)) 1153f2cb1360SIngo Molnar *per_cpu_ptr(sdd->sgc, cpu) = NULL; 1154f2cb1360SIngo Molnar } 1155f2cb1360SIngo Molnar 1156f2cb1360SIngo Molnar #ifdef CONFIG_NUMA 1157f2cb1360SIngo Molnar enum numa_topology_type sched_numa_topology_type; 115897fb7a0aSIngo Molnar 115997fb7a0aSIngo Molnar static int sched_domains_numa_levels; 1160f2cb1360SIngo Molnar static int sched_domains_curr_level; 116197fb7a0aSIngo Molnar 116297fb7a0aSIngo Molnar int sched_max_numa_distance; 116397fb7a0aSIngo Molnar static int *sched_domains_numa_distance; 116497fb7a0aSIngo Molnar static struct cpumask ***sched_domains_numa_masks; 1165f2cb1360SIngo Molnar #endif 1166f2cb1360SIngo Molnar 1167f2cb1360SIngo Molnar /* 1168f2cb1360SIngo Molnar * SD_flags allowed in topology descriptions. 1169f2cb1360SIngo Molnar * 1170f2cb1360SIngo Molnar * These flags are purely descriptive of the topology and do not prescribe 1171f2cb1360SIngo Molnar * behaviour. Behaviour is artificial and mapped in the below sd_init() 1172f2cb1360SIngo Molnar * function: 1173f2cb1360SIngo Molnar * 1174f2cb1360SIngo Molnar * SD_SHARE_CPUCAPACITY - describes SMT topologies 1175f2cb1360SIngo Molnar * SD_SHARE_PKG_RESOURCES - describes shared caches 1176f2cb1360SIngo Molnar * SD_NUMA - describes NUMA topologies 1177f2cb1360SIngo Molnar * SD_SHARE_POWERDOMAIN - describes shared power domain 1178f2cb1360SIngo Molnar * 1179f2cb1360SIngo Molnar * Odd one out, which beside describing the topology has a quirk also 1180f2cb1360SIngo Molnar * prescribes the desired behaviour that goes along with it: 1181f2cb1360SIngo Molnar * 1182f2cb1360SIngo Molnar * SD_ASYM_PACKING - describes SMT quirks 1183f2cb1360SIngo Molnar */ 1184f2cb1360SIngo Molnar #define TOPOLOGY_SD_FLAGS \ 1185f2cb1360SIngo Molnar (SD_SHARE_CPUCAPACITY | \ 1186f2cb1360SIngo Molnar SD_SHARE_PKG_RESOURCES | \ 1187f2cb1360SIngo Molnar SD_NUMA | \ 1188f2cb1360SIngo Molnar SD_ASYM_PACKING | \ 1189f2cb1360SIngo Molnar SD_SHARE_POWERDOMAIN) 1190f2cb1360SIngo Molnar 1191f2cb1360SIngo Molnar static struct sched_domain * 1192f2cb1360SIngo Molnar sd_init(struct sched_domain_topology_level *tl, 1193f2cb1360SIngo Molnar const struct cpumask *cpu_map, 119405484e09SMorten Rasmussen struct sched_domain *child, int dflags, int cpu) 1195f2cb1360SIngo Molnar { 1196f2cb1360SIngo Molnar struct sd_data *sdd = &tl->data; 1197f2cb1360SIngo Molnar struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu); 1198f2cb1360SIngo Molnar int sd_id, sd_weight, sd_flags = 0; 1199f2cb1360SIngo Molnar 1200f2cb1360SIngo Molnar #ifdef CONFIG_NUMA 1201f2cb1360SIngo Molnar /* 1202f2cb1360SIngo Molnar * Ugly hack to pass state to sd_numa_mask()... 1203f2cb1360SIngo Molnar */ 1204f2cb1360SIngo Molnar sched_domains_curr_level = tl->numa_level; 1205f2cb1360SIngo Molnar #endif 1206f2cb1360SIngo Molnar 1207f2cb1360SIngo Molnar sd_weight = cpumask_weight(tl->mask(cpu)); 1208f2cb1360SIngo Molnar 1209f2cb1360SIngo Molnar if (tl->sd_flags) 1210f2cb1360SIngo Molnar sd_flags = (*tl->sd_flags)(); 1211f2cb1360SIngo Molnar if (WARN_ONCE(sd_flags & ~TOPOLOGY_SD_FLAGS, 1212f2cb1360SIngo Molnar "wrong sd_flags in topology description\n")) 1213f2cb1360SIngo Molnar sd_flags &= ~TOPOLOGY_SD_FLAGS; 1214f2cb1360SIngo Molnar 121505484e09SMorten Rasmussen /* Apply detected topology flags */ 121605484e09SMorten Rasmussen sd_flags |= dflags; 121705484e09SMorten Rasmussen 1218f2cb1360SIngo Molnar *sd = (struct sched_domain){ 1219f2cb1360SIngo Molnar .min_interval = sd_weight, 1220f2cb1360SIngo Molnar .max_interval = 2*sd_weight, 1221f2cb1360SIngo Molnar .busy_factor = 32, 1222f2cb1360SIngo Molnar .imbalance_pct = 125, 1223f2cb1360SIngo Molnar 1224f2cb1360SIngo Molnar .cache_nice_tries = 0, 1225f2cb1360SIngo Molnar .busy_idx = 0, 1226f2cb1360SIngo Molnar .idle_idx = 0, 1227f2cb1360SIngo Molnar .newidle_idx = 0, 1228f2cb1360SIngo Molnar .wake_idx = 0, 1229f2cb1360SIngo Molnar .forkexec_idx = 0, 1230f2cb1360SIngo Molnar 1231f2cb1360SIngo Molnar .flags = 1*SD_LOAD_BALANCE 1232f2cb1360SIngo Molnar | 1*SD_BALANCE_NEWIDLE 1233f2cb1360SIngo Molnar | 1*SD_BALANCE_EXEC 1234f2cb1360SIngo Molnar | 1*SD_BALANCE_FORK 1235f2cb1360SIngo Molnar | 0*SD_BALANCE_WAKE 1236f2cb1360SIngo Molnar | 1*SD_WAKE_AFFINE 1237f2cb1360SIngo Molnar | 0*SD_SHARE_CPUCAPACITY 1238f2cb1360SIngo Molnar | 0*SD_SHARE_PKG_RESOURCES 1239f2cb1360SIngo Molnar | 0*SD_SERIALIZE 12409c63e84dSMorten Rasmussen | 1*SD_PREFER_SIBLING 1241f2cb1360SIngo Molnar | 0*SD_NUMA 1242f2cb1360SIngo Molnar | sd_flags 1243f2cb1360SIngo Molnar , 1244f2cb1360SIngo Molnar 1245f2cb1360SIngo Molnar .last_balance = jiffies, 1246f2cb1360SIngo Molnar .balance_interval = sd_weight, 1247f2cb1360SIngo Molnar .max_newidle_lb_cost = 0, 1248f2cb1360SIngo Molnar .next_decay_max_lb_cost = jiffies, 1249f2cb1360SIngo Molnar .child = child, 1250f2cb1360SIngo Molnar #ifdef CONFIG_SCHED_DEBUG 1251f2cb1360SIngo Molnar .name = tl->name, 1252f2cb1360SIngo Molnar #endif 1253f2cb1360SIngo Molnar }; 1254f2cb1360SIngo Molnar 1255f2cb1360SIngo Molnar cpumask_and(sched_domain_span(sd), cpu_map, tl->mask(cpu)); 1256f2cb1360SIngo Molnar sd_id = cpumask_first(sched_domain_span(sd)); 1257f2cb1360SIngo Molnar 1258f2cb1360SIngo Molnar /* 1259f2cb1360SIngo Molnar * Convert topological properties into behaviour. 1260f2cb1360SIngo Molnar */ 1261f2cb1360SIngo Molnar 1262f2cb1360SIngo Molnar if (sd->flags & SD_ASYM_CPUCAPACITY) { 1263f2cb1360SIngo Molnar struct sched_domain *t = sd; 1264f2cb1360SIngo Molnar 12659c63e84dSMorten Rasmussen /* 12669c63e84dSMorten Rasmussen * Don't attempt to spread across CPUs of different capacities. 12679c63e84dSMorten Rasmussen */ 12689c63e84dSMorten Rasmussen if (sd->child) 12699c63e84dSMorten Rasmussen sd->child->flags &= ~SD_PREFER_SIBLING; 12709c63e84dSMorten Rasmussen 1271f2cb1360SIngo Molnar for_each_lower_domain(t) 1272f2cb1360SIngo Molnar t->flags |= SD_BALANCE_WAKE; 1273f2cb1360SIngo Molnar } 1274f2cb1360SIngo Molnar 1275f2cb1360SIngo Molnar if (sd->flags & SD_SHARE_CPUCAPACITY) { 1276f2cb1360SIngo Molnar sd->imbalance_pct = 110; 1277f2cb1360SIngo Molnar 1278f2cb1360SIngo Molnar } else if (sd->flags & SD_SHARE_PKG_RESOURCES) { 1279f2cb1360SIngo Molnar sd->imbalance_pct = 117; 1280f2cb1360SIngo Molnar sd->cache_nice_tries = 1; 1281f2cb1360SIngo Molnar sd->busy_idx = 2; 1282f2cb1360SIngo Molnar 1283f2cb1360SIngo Molnar #ifdef CONFIG_NUMA 1284f2cb1360SIngo Molnar } else if (sd->flags & SD_NUMA) { 1285f2cb1360SIngo Molnar sd->cache_nice_tries = 2; 1286f2cb1360SIngo Molnar sd->busy_idx = 3; 1287f2cb1360SIngo Molnar sd->idle_idx = 2; 1288f2cb1360SIngo Molnar 12899c63e84dSMorten Rasmussen sd->flags &= ~SD_PREFER_SIBLING; 1290f2cb1360SIngo Molnar sd->flags |= SD_SERIALIZE; 1291f2cb1360SIngo Molnar if (sched_domains_numa_distance[tl->numa_level] > RECLAIM_DISTANCE) { 1292f2cb1360SIngo Molnar sd->flags &= ~(SD_BALANCE_EXEC | 1293f2cb1360SIngo Molnar SD_BALANCE_FORK | 1294f2cb1360SIngo Molnar SD_WAKE_AFFINE); 1295f2cb1360SIngo Molnar } 1296f2cb1360SIngo Molnar 1297f2cb1360SIngo Molnar #endif 1298f2cb1360SIngo Molnar } else { 1299f2cb1360SIngo Molnar sd->cache_nice_tries = 1; 1300f2cb1360SIngo Molnar sd->busy_idx = 2; 1301f2cb1360SIngo Molnar sd->idle_idx = 1; 1302f2cb1360SIngo Molnar } 1303f2cb1360SIngo Molnar 1304f2cb1360SIngo Molnar /* 1305f2cb1360SIngo Molnar * For all levels sharing cache; connect a sched_domain_shared 1306f2cb1360SIngo Molnar * instance. 1307f2cb1360SIngo Molnar */ 1308f2cb1360SIngo Molnar if (sd->flags & SD_SHARE_PKG_RESOURCES) { 1309f2cb1360SIngo Molnar sd->shared = *per_cpu_ptr(sdd->sds, sd_id); 1310f2cb1360SIngo Molnar atomic_inc(&sd->shared->ref); 1311f2cb1360SIngo Molnar atomic_set(&sd->shared->nr_busy_cpus, sd_weight); 1312f2cb1360SIngo Molnar } 1313f2cb1360SIngo Molnar 1314f2cb1360SIngo Molnar sd->private = sdd; 1315f2cb1360SIngo Molnar 1316f2cb1360SIngo Molnar return sd; 1317f2cb1360SIngo Molnar } 1318f2cb1360SIngo Molnar 1319f2cb1360SIngo Molnar /* 1320f2cb1360SIngo Molnar * Topology list, bottom-up. 1321f2cb1360SIngo Molnar */ 1322f2cb1360SIngo Molnar static struct sched_domain_topology_level default_topology[] = { 1323f2cb1360SIngo Molnar #ifdef CONFIG_SCHED_SMT 1324f2cb1360SIngo Molnar { cpu_smt_mask, cpu_smt_flags, SD_INIT_NAME(SMT) }, 1325f2cb1360SIngo Molnar #endif 1326f2cb1360SIngo Molnar #ifdef CONFIG_SCHED_MC 1327f2cb1360SIngo Molnar { cpu_coregroup_mask, cpu_core_flags, SD_INIT_NAME(MC) }, 1328f2cb1360SIngo Molnar #endif 1329f2cb1360SIngo Molnar { cpu_cpu_mask, SD_INIT_NAME(DIE) }, 1330f2cb1360SIngo Molnar { NULL, }, 1331f2cb1360SIngo Molnar }; 1332f2cb1360SIngo Molnar 1333f2cb1360SIngo Molnar static struct sched_domain_topology_level *sched_domain_topology = 1334f2cb1360SIngo Molnar default_topology; 1335f2cb1360SIngo Molnar 1336f2cb1360SIngo Molnar #define for_each_sd_topology(tl) \ 1337f2cb1360SIngo Molnar for (tl = sched_domain_topology; tl->mask; tl++) 1338f2cb1360SIngo Molnar 1339f2cb1360SIngo Molnar void set_sched_topology(struct sched_domain_topology_level *tl) 1340f2cb1360SIngo Molnar { 1341f2cb1360SIngo Molnar if (WARN_ON_ONCE(sched_smp_initialized)) 1342f2cb1360SIngo Molnar return; 1343f2cb1360SIngo Molnar 1344f2cb1360SIngo Molnar sched_domain_topology = tl; 1345f2cb1360SIngo Molnar } 1346f2cb1360SIngo Molnar 1347f2cb1360SIngo Molnar #ifdef CONFIG_NUMA 1348f2cb1360SIngo Molnar 1349f2cb1360SIngo Molnar static const struct cpumask *sd_numa_mask(int cpu) 1350f2cb1360SIngo Molnar { 1351f2cb1360SIngo Molnar return sched_domains_numa_masks[sched_domains_curr_level][cpu_to_node(cpu)]; 1352f2cb1360SIngo Molnar } 1353f2cb1360SIngo Molnar 1354f2cb1360SIngo Molnar static void sched_numa_warn(const char *str) 1355f2cb1360SIngo Molnar { 1356f2cb1360SIngo Molnar static int done = false; 1357f2cb1360SIngo Molnar int i,j; 1358f2cb1360SIngo Molnar 1359f2cb1360SIngo Molnar if (done) 1360f2cb1360SIngo Molnar return; 1361f2cb1360SIngo Molnar 1362f2cb1360SIngo Molnar done = true; 1363f2cb1360SIngo Molnar 1364f2cb1360SIngo Molnar printk(KERN_WARNING "ERROR: %s\n\n", str); 1365f2cb1360SIngo Molnar 1366f2cb1360SIngo Molnar for (i = 0; i < nr_node_ids; i++) { 1367f2cb1360SIngo Molnar printk(KERN_WARNING " "); 1368f2cb1360SIngo Molnar for (j = 0; j < nr_node_ids; j++) 1369f2cb1360SIngo Molnar printk(KERN_CONT "%02d ", node_distance(i,j)); 1370f2cb1360SIngo Molnar printk(KERN_CONT "\n"); 1371f2cb1360SIngo Molnar } 1372f2cb1360SIngo Molnar printk(KERN_WARNING "\n"); 1373f2cb1360SIngo Molnar } 1374f2cb1360SIngo Molnar 1375f2cb1360SIngo Molnar bool find_numa_distance(int distance) 1376f2cb1360SIngo Molnar { 1377f2cb1360SIngo Molnar int i; 1378f2cb1360SIngo Molnar 1379f2cb1360SIngo Molnar if (distance == node_distance(0, 0)) 1380f2cb1360SIngo Molnar return true; 1381f2cb1360SIngo Molnar 1382f2cb1360SIngo Molnar for (i = 0; i < sched_domains_numa_levels; i++) { 1383f2cb1360SIngo Molnar if (sched_domains_numa_distance[i] == distance) 1384f2cb1360SIngo Molnar return true; 1385f2cb1360SIngo Molnar } 1386f2cb1360SIngo Molnar 1387f2cb1360SIngo Molnar return false; 1388f2cb1360SIngo Molnar } 1389f2cb1360SIngo Molnar 1390f2cb1360SIngo Molnar /* 1391f2cb1360SIngo Molnar * A system can have three types of NUMA topology: 1392f2cb1360SIngo Molnar * NUMA_DIRECT: all nodes are directly connected, or not a NUMA system 1393f2cb1360SIngo Molnar * NUMA_GLUELESS_MESH: some nodes reachable through intermediary nodes 1394f2cb1360SIngo Molnar * NUMA_BACKPLANE: nodes can reach other nodes through a backplane 1395f2cb1360SIngo Molnar * 1396f2cb1360SIngo Molnar * The difference between a glueless mesh topology and a backplane 1397f2cb1360SIngo Molnar * topology lies in whether communication between not directly 1398f2cb1360SIngo Molnar * connected nodes goes through intermediary nodes (where programs 1399f2cb1360SIngo Molnar * could run), or through backplane controllers. This affects 1400f2cb1360SIngo Molnar * placement of programs. 1401f2cb1360SIngo Molnar * 1402f2cb1360SIngo Molnar * The type of topology can be discerned with the following tests: 1403f2cb1360SIngo Molnar * - If the maximum distance between any nodes is 1 hop, the system 1404f2cb1360SIngo Molnar * is directly connected. 1405f2cb1360SIngo Molnar * - If for two nodes A and B, located N > 1 hops away from each other, 1406f2cb1360SIngo Molnar * there is an intermediary node C, which is < N hops away from both 1407f2cb1360SIngo Molnar * nodes A and B, the system is a glueless mesh. 1408f2cb1360SIngo Molnar */ 1409f2cb1360SIngo Molnar static void init_numa_topology_type(void) 1410f2cb1360SIngo Molnar { 1411f2cb1360SIngo Molnar int a, b, c, n; 1412f2cb1360SIngo Molnar 1413f2cb1360SIngo Molnar n = sched_max_numa_distance; 1414f2cb1360SIngo Molnar 1415e5e96fafSSrikar Dronamraju if (sched_domains_numa_levels <= 2) { 1416f2cb1360SIngo Molnar sched_numa_topology_type = NUMA_DIRECT; 1417f2cb1360SIngo Molnar return; 1418f2cb1360SIngo Molnar } 1419f2cb1360SIngo Molnar 1420f2cb1360SIngo Molnar for_each_online_node(a) { 1421f2cb1360SIngo Molnar for_each_online_node(b) { 1422f2cb1360SIngo Molnar /* Find two nodes furthest removed from each other. */ 1423f2cb1360SIngo Molnar if (node_distance(a, b) < n) 1424f2cb1360SIngo Molnar continue; 1425f2cb1360SIngo Molnar 1426f2cb1360SIngo Molnar /* Is there an intermediary node between a and b? */ 1427f2cb1360SIngo Molnar for_each_online_node(c) { 1428f2cb1360SIngo Molnar if (node_distance(a, c) < n && 1429f2cb1360SIngo Molnar node_distance(b, c) < n) { 1430f2cb1360SIngo Molnar sched_numa_topology_type = 1431f2cb1360SIngo Molnar NUMA_GLUELESS_MESH; 1432f2cb1360SIngo Molnar return; 1433f2cb1360SIngo Molnar } 1434f2cb1360SIngo Molnar } 1435f2cb1360SIngo Molnar 1436f2cb1360SIngo Molnar sched_numa_topology_type = NUMA_BACKPLANE; 1437f2cb1360SIngo Molnar return; 1438f2cb1360SIngo Molnar } 1439f2cb1360SIngo Molnar } 1440f2cb1360SIngo Molnar } 1441f2cb1360SIngo Molnar 1442f2cb1360SIngo Molnar void sched_init_numa(void) 1443f2cb1360SIngo Molnar { 1444f2cb1360SIngo Molnar int next_distance, curr_distance = node_distance(0, 0); 1445f2cb1360SIngo Molnar struct sched_domain_topology_level *tl; 1446f2cb1360SIngo Molnar int level = 0; 1447f2cb1360SIngo Molnar int i, j, k; 1448f2cb1360SIngo Molnar 1449993f0b05SPeter Zijlstra sched_domains_numa_distance = kzalloc(sizeof(int) * (nr_node_ids + 1), GFP_KERNEL); 1450f2cb1360SIngo Molnar if (!sched_domains_numa_distance) 1451f2cb1360SIngo Molnar return; 1452f2cb1360SIngo Molnar 1453051f3ca0SSuravee Suthikulpanit /* Includes NUMA identity node at level 0. */ 1454051f3ca0SSuravee Suthikulpanit sched_domains_numa_distance[level++] = curr_distance; 1455051f3ca0SSuravee Suthikulpanit sched_domains_numa_levels = level; 1456051f3ca0SSuravee Suthikulpanit 1457f2cb1360SIngo Molnar /* 1458f2cb1360SIngo Molnar * O(nr_nodes^2) deduplicating selection sort -- in order to find the 1459f2cb1360SIngo Molnar * unique distances in the node_distance() table. 1460f2cb1360SIngo Molnar * 1461f2cb1360SIngo Molnar * Assumes node_distance(0,j) includes all distances in 1462f2cb1360SIngo Molnar * node_distance(i,j) in order to avoid cubic time. 1463f2cb1360SIngo Molnar */ 1464f2cb1360SIngo Molnar next_distance = curr_distance; 1465f2cb1360SIngo Molnar for (i = 0; i < nr_node_ids; i++) { 1466f2cb1360SIngo Molnar for (j = 0; j < nr_node_ids; j++) { 1467f2cb1360SIngo Molnar for (k = 0; k < nr_node_ids; k++) { 1468f2cb1360SIngo Molnar int distance = node_distance(i, k); 1469f2cb1360SIngo Molnar 1470f2cb1360SIngo Molnar if (distance > curr_distance && 1471f2cb1360SIngo Molnar (distance < next_distance || 1472f2cb1360SIngo Molnar next_distance == curr_distance)) 1473f2cb1360SIngo Molnar next_distance = distance; 1474f2cb1360SIngo Molnar 1475f2cb1360SIngo Molnar /* 1476f2cb1360SIngo Molnar * While not a strong assumption it would be nice to know 1477f2cb1360SIngo Molnar * about cases where if node A is connected to B, B is not 1478f2cb1360SIngo Molnar * equally connected to A. 1479f2cb1360SIngo Molnar */ 1480f2cb1360SIngo Molnar if (sched_debug() && node_distance(k, i) != distance) 1481f2cb1360SIngo Molnar sched_numa_warn("Node-distance not symmetric"); 1482f2cb1360SIngo Molnar 1483f2cb1360SIngo Molnar if (sched_debug() && i && !find_numa_distance(distance)) 1484f2cb1360SIngo Molnar sched_numa_warn("Node-0 not representative"); 1485f2cb1360SIngo Molnar } 1486f2cb1360SIngo Molnar if (next_distance != curr_distance) { 1487f2cb1360SIngo Molnar sched_domains_numa_distance[level++] = next_distance; 1488f2cb1360SIngo Molnar sched_domains_numa_levels = level; 1489f2cb1360SIngo Molnar curr_distance = next_distance; 1490f2cb1360SIngo Molnar } else break; 1491f2cb1360SIngo Molnar } 1492f2cb1360SIngo Molnar 1493f2cb1360SIngo Molnar /* 1494f2cb1360SIngo Molnar * In case of sched_debug() we verify the above assumption. 1495f2cb1360SIngo Molnar */ 1496f2cb1360SIngo Molnar if (!sched_debug()) 1497f2cb1360SIngo Molnar break; 1498f2cb1360SIngo Molnar } 1499f2cb1360SIngo Molnar 1500f2cb1360SIngo Molnar /* 1501051f3ca0SSuravee Suthikulpanit * 'level' contains the number of unique distances 1502f2cb1360SIngo Molnar * 1503f2cb1360SIngo Molnar * The sched_domains_numa_distance[] array includes the actual distance 1504f2cb1360SIngo Molnar * numbers. 1505f2cb1360SIngo Molnar */ 1506f2cb1360SIngo Molnar 1507f2cb1360SIngo Molnar /* 1508f2cb1360SIngo Molnar * Here, we should temporarily reset sched_domains_numa_levels to 0. 1509f2cb1360SIngo Molnar * If it fails to allocate memory for array sched_domains_numa_masks[][], 1510f2cb1360SIngo Molnar * the array will contain less then 'level' members. This could be 1511f2cb1360SIngo Molnar * dangerous when we use it to iterate array sched_domains_numa_masks[][] 1512f2cb1360SIngo Molnar * in other functions. 1513f2cb1360SIngo Molnar * 1514f2cb1360SIngo Molnar * We reset it to 'level' at the end of this function. 1515f2cb1360SIngo Molnar */ 1516f2cb1360SIngo Molnar sched_domains_numa_levels = 0; 1517f2cb1360SIngo Molnar 1518f2cb1360SIngo Molnar sched_domains_numa_masks = kzalloc(sizeof(void *) * level, GFP_KERNEL); 1519f2cb1360SIngo Molnar if (!sched_domains_numa_masks) 1520f2cb1360SIngo Molnar return; 1521f2cb1360SIngo Molnar 1522f2cb1360SIngo Molnar /* 1523f2cb1360SIngo Molnar * Now for each level, construct a mask per node which contains all 1524f2cb1360SIngo Molnar * CPUs of nodes that are that many hops away from us. 1525f2cb1360SIngo Molnar */ 1526f2cb1360SIngo Molnar for (i = 0; i < level; i++) { 1527f2cb1360SIngo Molnar sched_domains_numa_masks[i] = 1528f2cb1360SIngo Molnar kzalloc(nr_node_ids * sizeof(void *), GFP_KERNEL); 1529f2cb1360SIngo Molnar if (!sched_domains_numa_masks[i]) 1530f2cb1360SIngo Molnar return; 1531f2cb1360SIngo Molnar 1532f2cb1360SIngo Molnar for (j = 0; j < nr_node_ids; j++) { 1533f2cb1360SIngo Molnar struct cpumask *mask = kzalloc(cpumask_size(), GFP_KERNEL); 1534f2cb1360SIngo Molnar if (!mask) 1535f2cb1360SIngo Molnar return; 1536f2cb1360SIngo Molnar 1537f2cb1360SIngo Molnar sched_domains_numa_masks[i][j] = mask; 1538f2cb1360SIngo Molnar 1539f2cb1360SIngo Molnar for_each_node(k) { 1540f2cb1360SIngo Molnar if (node_distance(j, k) > sched_domains_numa_distance[i]) 1541f2cb1360SIngo Molnar continue; 1542f2cb1360SIngo Molnar 1543f2cb1360SIngo Molnar cpumask_or(mask, mask, cpumask_of_node(k)); 1544f2cb1360SIngo Molnar } 1545f2cb1360SIngo Molnar } 1546f2cb1360SIngo Molnar } 1547f2cb1360SIngo Molnar 1548f2cb1360SIngo Molnar /* Compute default topology size */ 1549f2cb1360SIngo Molnar for (i = 0; sched_domain_topology[i].mask; i++); 1550f2cb1360SIngo Molnar 1551f2cb1360SIngo Molnar tl = kzalloc((i + level + 1) * 1552f2cb1360SIngo Molnar sizeof(struct sched_domain_topology_level), GFP_KERNEL); 1553f2cb1360SIngo Molnar if (!tl) 1554f2cb1360SIngo Molnar return; 1555f2cb1360SIngo Molnar 1556f2cb1360SIngo Molnar /* 1557f2cb1360SIngo Molnar * Copy the default topology bits.. 1558f2cb1360SIngo Molnar */ 1559f2cb1360SIngo Molnar for (i = 0; sched_domain_topology[i].mask; i++) 1560f2cb1360SIngo Molnar tl[i] = sched_domain_topology[i]; 1561f2cb1360SIngo Molnar 1562f2cb1360SIngo Molnar /* 1563051f3ca0SSuravee Suthikulpanit * Add the NUMA identity distance, aka single NODE. 1564051f3ca0SSuravee Suthikulpanit */ 1565051f3ca0SSuravee Suthikulpanit tl[i++] = (struct sched_domain_topology_level){ 1566051f3ca0SSuravee Suthikulpanit .mask = sd_numa_mask, 1567051f3ca0SSuravee Suthikulpanit .numa_level = 0, 1568051f3ca0SSuravee Suthikulpanit SD_INIT_NAME(NODE) 1569051f3ca0SSuravee Suthikulpanit }; 1570051f3ca0SSuravee Suthikulpanit 1571051f3ca0SSuravee Suthikulpanit /* 1572f2cb1360SIngo Molnar * .. and append 'j' levels of NUMA goodness. 1573f2cb1360SIngo Molnar */ 1574051f3ca0SSuravee Suthikulpanit for (j = 1; j < level; i++, j++) { 1575f2cb1360SIngo Molnar tl[i] = (struct sched_domain_topology_level){ 1576f2cb1360SIngo Molnar .mask = sd_numa_mask, 1577f2cb1360SIngo Molnar .sd_flags = cpu_numa_flags, 1578f2cb1360SIngo Molnar .flags = SDTL_OVERLAP, 1579f2cb1360SIngo Molnar .numa_level = j, 1580f2cb1360SIngo Molnar SD_INIT_NAME(NUMA) 1581f2cb1360SIngo Molnar }; 1582f2cb1360SIngo Molnar } 1583f2cb1360SIngo Molnar 1584f2cb1360SIngo Molnar sched_domain_topology = tl; 1585f2cb1360SIngo Molnar 1586f2cb1360SIngo Molnar sched_domains_numa_levels = level; 1587f2cb1360SIngo Molnar sched_max_numa_distance = sched_domains_numa_distance[level - 1]; 1588f2cb1360SIngo Molnar 1589f2cb1360SIngo Molnar init_numa_topology_type(); 1590f2cb1360SIngo Molnar } 1591f2cb1360SIngo Molnar 1592f2cb1360SIngo Molnar void sched_domains_numa_masks_set(unsigned int cpu) 1593f2cb1360SIngo Molnar { 1594f2cb1360SIngo Molnar int node = cpu_to_node(cpu); 1595f2cb1360SIngo Molnar int i, j; 1596f2cb1360SIngo Molnar 1597f2cb1360SIngo Molnar for (i = 0; i < sched_domains_numa_levels; i++) { 1598f2cb1360SIngo Molnar for (j = 0; j < nr_node_ids; j++) { 1599f2cb1360SIngo Molnar if (node_distance(j, node) <= sched_domains_numa_distance[i]) 1600f2cb1360SIngo Molnar cpumask_set_cpu(cpu, sched_domains_numa_masks[i][j]); 1601f2cb1360SIngo Molnar } 1602f2cb1360SIngo Molnar } 1603f2cb1360SIngo Molnar } 1604f2cb1360SIngo Molnar 1605f2cb1360SIngo Molnar void sched_domains_numa_masks_clear(unsigned int cpu) 1606f2cb1360SIngo Molnar { 1607f2cb1360SIngo Molnar int i, j; 1608f2cb1360SIngo Molnar 1609f2cb1360SIngo Molnar for (i = 0; i < sched_domains_numa_levels; i++) { 1610f2cb1360SIngo Molnar for (j = 0; j < nr_node_ids; j++) 1611f2cb1360SIngo Molnar cpumask_clear_cpu(cpu, sched_domains_numa_masks[i][j]); 1612f2cb1360SIngo Molnar } 1613f2cb1360SIngo Molnar } 1614f2cb1360SIngo Molnar 1615f2cb1360SIngo Molnar #endif /* CONFIG_NUMA */ 1616f2cb1360SIngo Molnar 1617f2cb1360SIngo Molnar static int __sdt_alloc(const struct cpumask *cpu_map) 1618f2cb1360SIngo Molnar { 1619f2cb1360SIngo Molnar struct sched_domain_topology_level *tl; 1620f2cb1360SIngo Molnar int j; 1621f2cb1360SIngo Molnar 1622f2cb1360SIngo Molnar for_each_sd_topology(tl) { 1623f2cb1360SIngo Molnar struct sd_data *sdd = &tl->data; 1624f2cb1360SIngo Molnar 1625f2cb1360SIngo Molnar sdd->sd = alloc_percpu(struct sched_domain *); 1626f2cb1360SIngo Molnar if (!sdd->sd) 1627f2cb1360SIngo Molnar return -ENOMEM; 1628f2cb1360SIngo Molnar 1629f2cb1360SIngo Molnar sdd->sds = alloc_percpu(struct sched_domain_shared *); 1630f2cb1360SIngo Molnar if (!sdd->sds) 1631f2cb1360SIngo Molnar return -ENOMEM; 1632f2cb1360SIngo Molnar 1633f2cb1360SIngo Molnar sdd->sg = alloc_percpu(struct sched_group *); 1634f2cb1360SIngo Molnar if (!sdd->sg) 1635f2cb1360SIngo Molnar return -ENOMEM; 1636f2cb1360SIngo Molnar 1637f2cb1360SIngo Molnar sdd->sgc = alloc_percpu(struct sched_group_capacity *); 1638f2cb1360SIngo Molnar if (!sdd->sgc) 1639f2cb1360SIngo Molnar return -ENOMEM; 1640f2cb1360SIngo Molnar 1641f2cb1360SIngo Molnar for_each_cpu(j, cpu_map) { 1642f2cb1360SIngo Molnar struct sched_domain *sd; 1643f2cb1360SIngo Molnar struct sched_domain_shared *sds; 1644f2cb1360SIngo Molnar struct sched_group *sg; 1645f2cb1360SIngo Molnar struct sched_group_capacity *sgc; 1646f2cb1360SIngo Molnar 1647f2cb1360SIngo Molnar sd = kzalloc_node(sizeof(struct sched_domain) + cpumask_size(), 1648f2cb1360SIngo Molnar GFP_KERNEL, cpu_to_node(j)); 1649f2cb1360SIngo Molnar if (!sd) 1650f2cb1360SIngo Molnar return -ENOMEM; 1651f2cb1360SIngo Molnar 1652f2cb1360SIngo Molnar *per_cpu_ptr(sdd->sd, j) = sd; 1653f2cb1360SIngo Molnar 1654f2cb1360SIngo Molnar sds = kzalloc_node(sizeof(struct sched_domain_shared), 1655f2cb1360SIngo Molnar GFP_KERNEL, cpu_to_node(j)); 1656f2cb1360SIngo Molnar if (!sds) 1657f2cb1360SIngo Molnar return -ENOMEM; 1658f2cb1360SIngo Molnar 1659f2cb1360SIngo Molnar *per_cpu_ptr(sdd->sds, j) = sds; 1660f2cb1360SIngo Molnar 1661f2cb1360SIngo Molnar sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(), 1662f2cb1360SIngo Molnar GFP_KERNEL, cpu_to_node(j)); 1663f2cb1360SIngo Molnar if (!sg) 1664f2cb1360SIngo Molnar return -ENOMEM; 1665f2cb1360SIngo Molnar 1666f2cb1360SIngo Molnar sg->next = sg; 1667f2cb1360SIngo Molnar 1668f2cb1360SIngo Molnar *per_cpu_ptr(sdd->sg, j) = sg; 1669f2cb1360SIngo Molnar 1670f2cb1360SIngo Molnar sgc = kzalloc_node(sizeof(struct sched_group_capacity) + cpumask_size(), 1671f2cb1360SIngo Molnar GFP_KERNEL, cpu_to_node(j)); 1672f2cb1360SIngo Molnar if (!sgc) 1673f2cb1360SIngo Molnar return -ENOMEM; 1674f2cb1360SIngo Molnar 1675005f874dSPeter Zijlstra #ifdef CONFIG_SCHED_DEBUG 1676005f874dSPeter Zijlstra sgc->id = j; 1677005f874dSPeter Zijlstra #endif 1678005f874dSPeter Zijlstra 1679f2cb1360SIngo Molnar *per_cpu_ptr(sdd->sgc, j) = sgc; 1680f2cb1360SIngo Molnar } 1681f2cb1360SIngo Molnar } 1682f2cb1360SIngo Molnar 1683f2cb1360SIngo Molnar return 0; 1684f2cb1360SIngo Molnar } 1685f2cb1360SIngo Molnar 1686f2cb1360SIngo Molnar static void __sdt_free(const struct cpumask *cpu_map) 1687f2cb1360SIngo Molnar { 1688f2cb1360SIngo Molnar struct sched_domain_topology_level *tl; 1689f2cb1360SIngo Molnar int j; 1690f2cb1360SIngo Molnar 1691f2cb1360SIngo Molnar for_each_sd_topology(tl) { 1692f2cb1360SIngo Molnar struct sd_data *sdd = &tl->data; 1693f2cb1360SIngo Molnar 1694f2cb1360SIngo Molnar for_each_cpu(j, cpu_map) { 1695f2cb1360SIngo Molnar struct sched_domain *sd; 1696f2cb1360SIngo Molnar 1697f2cb1360SIngo Molnar if (sdd->sd) { 1698f2cb1360SIngo Molnar sd = *per_cpu_ptr(sdd->sd, j); 1699f2cb1360SIngo Molnar if (sd && (sd->flags & SD_OVERLAP)) 1700f2cb1360SIngo Molnar free_sched_groups(sd->groups, 0); 1701f2cb1360SIngo Molnar kfree(*per_cpu_ptr(sdd->sd, j)); 1702f2cb1360SIngo Molnar } 1703f2cb1360SIngo Molnar 1704f2cb1360SIngo Molnar if (sdd->sds) 1705f2cb1360SIngo Molnar kfree(*per_cpu_ptr(sdd->sds, j)); 1706f2cb1360SIngo Molnar if (sdd->sg) 1707f2cb1360SIngo Molnar kfree(*per_cpu_ptr(sdd->sg, j)); 1708f2cb1360SIngo Molnar if (sdd->sgc) 1709f2cb1360SIngo Molnar kfree(*per_cpu_ptr(sdd->sgc, j)); 1710f2cb1360SIngo Molnar } 1711f2cb1360SIngo Molnar free_percpu(sdd->sd); 1712f2cb1360SIngo Molnar sdd->sd = NULL; 1713f2cb1360SIngo Molnar free_percpu(sdd->sds); 1714f2cb1360SIngo Molnar sdd->sds = NULL; 1715f2cb1360SIngo Molnar free_percpu(sdd->sg); 1716f2cb1360SIngo Molnar sdd->sg = NULL; 1717f2cb1360SIngo Molnar free_percpu(sdd->sgc); 1718f2cb1360SIngo Molnar sdd->sgc = NULL; 1719f2cb1360SIngo Molnar } 1720f2cb1360SIngo Molnar } 1721f2cb1360SIngo Molnar 1722181a80d1SViresh Kumar static struct sched_domain *build_sched_domain(struct sched_domain_topology_level *tl, 1723f2cb1360SIngo Molnar const struct cpumask *cpu_map, struct sched_domain_attr *attr, 172405484e09SMorten Rasmussen struct sched_domain *child, int dflags, int cpu) 1725f2cb1360SIngo Molnar { 172605484e09SMorten Rasmussen struct sched_domain *sd = sd_init(tl, cpu_map, child, dflags, cpu); 1727f2cb1360SIngo Molnar 1728f2cb1360SIngo Molnar if (child) { 1729f2cb1360SIngo Molnar sd->level = child->level + 1; 1730f2cb1360SIngo Molnar sched_domain_level_max = max(sched_domain_level_max, sd->level); 1731f2cb1360SIngo Molnar child->parent = sd; 1732f2cb1360SIngo Molnar 1733f2cb1360SIngo Molnar if (!cpumask_subset(sched_domain_span(child), 1734f2cb1360SIngo Molnar sched_domain_span(sd))) { 1735f2cb1360SIngo Molnar pr_err("BUG: arch topology borken\n"); 1736f2cb1360SIngo Molnar #ifdef CONFIG_SCHED_DEBUG 1737f2cb1360SIngo Molnar pr_err(" the %s domain not a subset of the %s domain\n", 1738f2cb1360SIngo Molnar child->name, sd->name); 1739f2cb1360SIngo Molnar #endif 174097fb7a0aSIngo Molnar /* Fixup, ensure @sd has at least @child CPUs. */ 1741f2cb1360SIngo Molnar cpumask_or(sched_domain_span(sd), 1742f2cb1360SIngo Molnar sched_domain_span(sd), 1743f2cb1360SIngo Molnar sched_domain_span(child)); 1744f2cb1360SIngo Molnar } 1745f2cb1360SIngo Molnar 1746f2cb1360SIngo Molnar } 1747f2cb1360SIngo Molnar set_domain_attribute(sd, attr); 1748f2cb1360SIngo Molnar 1749f2cb1360SIngo Molnar return sd; 1750f2cb1360SIngo Molnar } 1751f2cb1360SIngo Molnar 1752f2cb1360SIngo Molnar /* 175305484e09SMorten Rasmussen * Find the sched_domain_topology_level where all CPU capacities are visible 175405484e09SMorten Rasmussen * for all CPUs. 175505484e09SMorten Rasmussen */ 175605484e09SMorten Rasmussen static struct sched_domain_topology_level 175705484e09SMorten Rasmussen *asym_cpu_capacity_level(const struct cpumask *cpu_map) 175805484e09SMorten Rasmussen { 175905484e09SMorten Rasmussen int i, j, asym_level = 0; 176005484e09SMorten Rasmussen bool asym = false; 176105484e09SMorten Rasmussen struct sched_domain_topology_level *tl, *asym_tl = NULL; 176205484e09SMorten Rasmussen unsigned long cap; 176305484e09SMorten Rasmussen 176405484e09SMorten Rasmussen /* Is there any asymmetry? */ 176505484e09SMorten Rasmussen cap = arch_scale_cpu_capacity(NULL, cpumask_first(cpu_map)); 176605484e09SMorten Rasmussen 176705484e09SMorten Rasmussen for_each_cpu(i, cpu_map) { 176805484e09SMorten Rasmussen if (arch_scale_cpu_capacity(NULL, i) != cap) { 176905484e09SMorten Rasmussen asym = true; 177005484e09SMorten Rasmussen break; 177105484e09SMorten Rasmussen } 177205484e09SMorten Rasmussen } 177305484e09SMorten Rasmussen 177405484e09SMorten Rasmussen if (!asym) 177505484e09SMorten Rasmussen return NULL; 177605484e09SMorten Rasmussen 177705484e09SMorten Rasmussen /* 177805484e09SMorten Rasmussen * Examine topology from all CPU's point of views to detect the lowest 177905484e09SMorten Rasmussen * sched_domain_topology_level where a highest capacity CPU is visible 178005484e09SMorten Rasmussen * to everyone. 178105484e09SMorten Rasmussen */ 178205484e09SMorten Rasmussen for_each_cpu(i, cpu_map) { 178305484e09SMorten Rasmussen unsigned long max_capacity = arch_scale_cpu_capacity(NULL, i); 178405484e09SMorten Rasmussen int tl_id = 0; 178505484e09SMorten Rasmussen 178605484e09SMorten Rasmussen for_each_sd_topology(tl) { 178705484e09SMorten Rasmussen if (tl_id < asym_level) 178805484e09SMorten Rasmussen goto next_level; 178905484e09SMorten Rasmussen 179005484e09SMorten Rasmussen for_each_cpu_and(j, tl->mask(i), cpu_map) { 179105484e09SMorten Rasmussen unsigned long capacity; 179205484e09SMorten Rasmussen 179305484e09SMorten Rasmussen capacity = arch_scale_cpu_capacity(NULL, j); 179405484e09SMorten Rasmussen 179505484e09SMorten Rasmussen if (capacity <= max_capacity) 179605484e09SMorten Rasmussen continue; 179705484e09SMorten Rasmussen 179805484e09SMorten Rasmussen max_capacity = capacity; 179905484e09SMorten Rasmussen asym_level = tl_id; 180005484e09SMorten Rasmussen asym_tl = tl; 180105484e09SMorten Rasmussen } 180205484e09SMorten Rasmussen next_level: 180305484e09SMorten Rasmussen tl_id++; 180405484e09SMorten Rasmussen } 180505484e09SMorten Rasmussen } 180605484e09SMorten Rasmussen 180705484e09SMorten Rasmussen return asym_tl; 180805484e09SMorten Rasmussen } 180905484e09SMorten Rasmussen 181005484e09SMorten Rasmussen 181105484e09SMorten Rasmussen /* 1812f2cb1360SIngo Molnar * Build sched domains for a given set of CPUs and attach the sched domains 1813f2cb1360SIngo Molnar * to the individual CPUs 1814f2cb1360SIngo Molnar */ 1815f2cb1360SIngo Molnar static int 1816f2cb1360SIngo Molnar build_sched_domains(const struct cpumask *cpu_map, struct sched_domain_attr *attr) 1817f2cb1360SIngo Molnar { 1818f2cb1360SIngo Molnar enum s_alloc alloc_state; 1819f2cb1360SIngo Molnar struct sched_domain *sd; 1820f2cb1360SIngo Molnar struct s_data d; 1821f2cb1360SIngo Molnar struct rq *rq = NULL; 1822f2cb1360SIngo Molnar int i, ret = -ENOMEM; 182305484e09SMorten Rasmussen struct sched_domain_topology_level *tl_asym; 1824df054e84SMorten Rasmussen bool has_asym = false; 1825f2cb1360SIngo Molnar 1826f2cb1360SIngo Molnar alloc_state = __visit_domain_allocation_hell(&d, cpu_map); 1827f2cb1360SIngo Molnar if (alloc_state != sa_rootdomain) 1828f2cb1360SIngo Molnar goto error; 1829f2cb1360SIngo Molnar 183005484e09SMorten Rasmussen tl_asym = asym_cpu_capacity_level(cpu_map); 183105484e09SMorten Rasmussen 1832f2cb1360SIngo Molnar /* Set up domains for CPUs specified by the cpu_map: */ 1833f2cb1360SIngo Molnar for_each_cpu(i, cpu_map) { 1834f2cb1360SIngo Molnar struct sched_domain_topology_level *tl; 1835f2cb1360SIngo Molnar 1836f2cb1360SIngo Molnar sd = NULL; 1837f2cb1360SIngo Molnar for_each_sd_topology(tl) { 183805484e09SMorten Rasmussen int dflags = 0; 183905484e09SMorten Rasmussen 1840df054e84SMorten Rasmussen if (tl == tl_asym) { 184105484e09SMorten Rasmussen dflags |= SD_ASYM_CPUCAPACITY; 1842df054e84SMorten Rasmussen has_asym = true; 1843df054e84SMorten Rasmussen } 184405484e09SMorten Rasmussen 184505484e09SMorten Rasmussen sd = build_sched_domain(tl, cpu_map, attr, sd, dflags, i); 184605484e09SMorten Rasmussen 1847f2cb1360SIngo Molnar if (tl == sched_domain_topology) 1848f2cb1360SIngo Molnar *per_cpu_ptr(d.sd, i) = sd; 1849af85596cSPeter Zijlstra if (tl->flags & SDTL_OVERLAP) 1850f2cb1360SIngo Molnar sd->flags |= SD_OVERLAP; 1851f2cb1360SIngo Molnar if (cpumask_equal(cpu_map, sched_domain_span(sd))) 1852f2cb1360SIngo Molnar break; 1853f2cb1360SIngo Molnar } 1854f2cb1360SIngo Molnar } 1855f2cb1360SIngo Molnar 1856f2cb1360SIngo Molnar /* Build the groups for the domains */ 1857f2cb1360SIngo Molnar for_each_cpu(i, cpu_map) { 1858f2cb1360SIngo Molnar for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) { 1859f2cb1360SIngo Molnar sd->span_weight = cpumask_weight(sched_domain_span(sd)); 1860f2cb1360SIngo Molnar if (sd->flags & SD_OVERLAP) { 1861f2cb1360SIngo Molnar if (build_overlap_sched_groups(sd, i)) 1862f2cb1360SIngo Molnar goto error; 1863f2cb1360SIngo Molnar } else { 1864f2cb1360SIngo Molnar if (build_sched_groups(sd, i)) 1865f2cb1360SIngo Molnar goto error; 1866f2cb1360SIngo Molnar } 1867f2cb1360SIngo Molnar } 1868f2cb1360SIngo Molnar } 1869f2cb1360SIngo Molnar 1870f2cb1360SIngo Molnar /* Calculate CPU capacity for physical packages and nodes */ 1871f2cb1360SIngo Molnar for (i = nr_cpumask_bits-1; i >= 0; i--) { 1872f2cb1360SIngo Molnar if (!cpumask_test_cpu(i, cpu_map)) 1873f2cb1360SIngo Molnar continue; 1874f2cb1360SIngo Molnar 1875f2cb1360SIngo Molnar for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) { 1876f2cb1360SIngo Molnar claim_allocations(i, sd); 1877f2cb1360SIngo Molnar init_sched_groups_capacity(i, sd); 1878f2cb1360SIngo Molnar } 1879f2cb1360SIngo Molnar } 1880f2cb1360SIngo Molnar 1881f2cb1360SIngo Molnar /* Attach the domains */ 1882f2cb1360SIngo Molnar rcu_read_lock(); 1883f2cb1360SIngo Molnar for_each_cpu(i, cpu_map) { 1884f2cb1360SIngo Molnar rq = cpu_rq(i); 1885f2cb1360SIngo Molnar sd = *per_cpu_ptr(d.sd, i); 1886f2cb1360SIngo Molnar 1887f2cb1360SIngo Molnar /* Use READ_ONCE()/WRITE_ONCE() to avoid load/store tearing: */ 1888f2cb1360SIngo Molnar if (rq->cpu_capacity_orig > READ_ONCE(d.rd->max_cpu_capacity)) 1889f2cb1360SIngo Molnar WRITE_ONCE(d.rd->max_cpu_capacity, rq->cpu_capacity_orig); 1890f2cb1360SIngo Molnar 1891f2cb1360SIngo Molnar cpu_attach_domain(sd, d.rd, i); 1892f2cb1360SIngo Molnar } 1893f2cb1360SIngo Molnar rcu_read_unlock(); 1894f2cb1360SIngo Molnar 1895df054e84SMorten Rasmussen if (has_asym) 1896df054e84SMorten Rasmussen static_branch_enable_cpuslocked(&sched_asym_cpucapacity); 1897df054e84SMorten Rasmussen 1898f2cb1360SIngo Molnar if (rq && sched_debug_enabled) { 1899bf5015a5SJuri Lelli pr_info("root domain span: %*pbl (max cpu_capacity = %lu)\n", 1900f2cb1360SIngo Molnar cpumask_pr_args(cpu_map), rq->rd->max_cpu_capacity); 1901f2cb1360SIngo Molnar } 1902f2cb1360SIngo Molnar 1903f2cb1360SIngo Molnar ret = 0; 1904f2cb1360SIngo Molnar error: 1905f2cb1360SIngo Molnar __free_domain_allocs(&d, alloc_state, cpu_map); 190697fb7a0aSIngo Molnar 1907f2cb1360SIngo Molnar return ret; 1908f2cb1360SIngo Molnar } 1909f2cb1360SIngo Molnar 1910f2cb1360SIngo Molnar /* Current sched domains: */ 1911f2cb1360SIngo Molnar static cpumask_var_t *doms_cur; 1912f2cb1360SIngo Molnar 1913f2cb1360SIngo Molnar /* Number of sched domains in 'doms_cur': */ 1914f2cb1360SIngo Molnar static int ndoms_cur; 1915f2cb1360SIngo Molnar 1916f2cb1360SIngo Molnar /* Attribues of custom domains in 'doms_cur' */ 1917f2cb1360SIngo Molnar static struct sched_domain_attr *dattr_cur; 1918f2cb1360SIngo Molnar 1919f2cb1360SIngo Molnar /* 1920f2cb1360SIngo Molnar * Special case: If a kmalloc() of a doms_cur partition (array of 1921f2cb1360SIngo Molnar * cpumask) fails, then fallback to a single sched domain, 1922f2cb1360SIngo Molnar * as determined by the single cpumask fallback_doms. 1923f2cb1360SIngo Molnar */ 19248d5dc512SPeter Zijlstra static cpumask_var_t fallback_doms; 1925f2cb1360SIngo Molnar 1926f2cb1360SIngo Molnar /* 1927f2cb1360SIngo Molnar * arch_update_cpu_topology lets virtualized architectures update the 1928f2cb1360SIngo Molnar * CPU core maps. It is supposed to return 1 if the topology changed 1929f2cb1360SIngo Molnar * or 0 if it stayed the same. 1930f2cb1360SIngo Molnar */ 1931f2cb1360SIngo Molnar int __weak arch_update_cpu_topology(void) 1932f2cb1360SIngo Molnar { 1933f2cb1360SIngo Molnar return 0; 1934f2cb1360SIngo Molnar } 1935f2cb1360SIngo Molnar 1936f2cb1360SIngo Molnar cpumask_var_t *alloc_sched_domains(unsigned int ndoms) 1937f2cb1360SIngo Molnar { 1938f2cb1360SIngo Molnar int i; 1939f2cb1360SIngo Molnar cpumask_var_t *doms; 1940f2cb1360SIngo Molnar 19416da2ec56SKees Cook doms = kmalloc_array(ndoms, sizeof(*doms), GFP_KERNEL); 1942f2cb1360SIngo Molnar if (!doms) 1943f2cb1360SIngo Molnar return NULL; 1944f2cb1360SIngo Molnar for (i = 0; i < ndoms; i++) { 1945f2cb1360SIngo Molnar if (!alloc_cpumask_var(&doms[i], GFP_KERNEL)) { 1946f2cb1360SIngo Molnar free_sched_domains(doms, i); 1947f2cb1360SIngo Molnar return NULL; 1948f2cb1360SIngo Molnar } 1949f2cb1360SIngo Molnar } 1950f2cb1360SIngo Molnar return doms; 1951f2cb1360SIngo Molnar } 1952f2cb1360SIngo Molnar 1953f2cb1360SIngo Molnar void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms) 1954f2cb1360SIngo Molnar { 1955f2cb1360SIngo Molnar unsigned int i; 1956f2cb1360SIngo Molnar for (i = 0; i < ndoms; i++) 1957f2cb1360SIngo Molnar free_cpumask_var(doms[i]); 1958f2cb1360SIngo Molnar kfree(doms); 1959f2cb1360SIngo Molnar } 1960f2cb1360SIngo Molnar 1961f2cb1360SIngo Molnar /* 1962f2cb1360SIngo Molnar * Set up scheduler domains and groups. Callers must hold the hotplug lock. 1963f2cb1360SIngo Molnar * For now this just excludes isolated CPUs, but could be used to 1964f2cb1360SIngo Molnar * exclude other special cases in the future. 1965f2cb1360SIngo Molnar */ 19668d5dc512SPeter Zijlstra int sched_init_domains(const struct cpumask *cpu_map) 1967f2cb1360SIngo Molnar { 1968f2cb1360SIngo Molnar int err; 1969f2cb1360SIngo Molnar 19708d5dc512SPeter Zijlstra zalloc_cpumask_var(&sched_domains_tmpmask, GFP_KERNEL); 19711676330eSPeter Zijlstra zalloc_cpumask_var(&sched_domains_tmpmask2, GFP_KERNEL); 19728d5dc512SPeter Zijlstra zalloc_cpumask_var(&fallback_doms, GFP_KERNEL); 19738d5dc512SPeter Zijlstra 1974f2cb1360SIngo Molnar arch_update_cpu_topology(); 1975f2cb1360SIngo Molnar ndoms_cur = 1; 1976f2cb1360SIngo Molnar doms_cur = alloc_sched_domains(ndoms_cur); 1977f2cb1360SIngo Molnar if (!doms_cur) 1978f2cb1360SIngo Molnar doms_cur = &fallback_doms; 1979edb93821SFrederic Weisbecker cpumask_and(doms_cur[0], cpu_map, housekeeping_cpumask(HK_FLAG_DOMAIN)); 1980f2cb1360SIngo Molnar err = build_sched_domains(doms_cur[0], NULL); 1981f2cb1360SIngo Molnar register_sched_domain_sysctl(); 1982f2cb1360SIngo Molnar 1983f2cb1360SIngo Molnar return err; 1984f2cb1360SIngo Molnar } 1985f2cb1360SIngo Molnar 1986f2cb1360SIngo Molnar /* 1987f2cb1360SIngo Molnar * Detach sched domains from a group of CPUs specified in cpu_map 1988f2cb1360SIngo Molnar * These CPUs will now be attached to the NULL domain 1989f2cb1360SIngo Molnar */ 1990f2cb1360SIngo Molnar static void detach_destroy_domains(const struct cpumask *cpu_map) 1991f2cb1360SIngo Molnar { 1992f2cb1360SIngo Molnar int i; 1993f2cb1360SIngo Molnar 1994f2cb1360SIngo Molnar rcu_read_lock(); 1995f2cb1360SIngo Molnar for_each_cpu(i, cpu_map) 1996f2cb1360SIngo Molnar cpu_attach_domain(NULL, &def_root_domain, i); 1997f2cb1360SIngo Molnar rcu_read_unlock(); 1998f2cb1360SIngo Molnar } 1999f2cb1360SIngo Molnar 2000f2cb1360SIngo Molnar /* handle null as "default" */ 2001f2cb1360SIngo Molnar static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur, 2002f2cb1360SIngo Molnar struct sched_domain_attr *new, int idx_new) 2003f2cb1360SIngo Molnar { 2004f2cb1360SIngo Molnar struct sched_domain_attr tmp; 2005f2cb1360SIngo Molnar 2006f2cb1360SIngo Molnar /* Fast path: */ 2007f2cb1360SIngo Molnar if (!new && !cur) 2008f2cb1360SIngo Molnar return 1; 2009f2cb1360SIngo Molnar 2010f2cb1360SIngo Molnar tmp = SD_ATTR_INIT; 201197fb7a0aSIngo Molnar 2012f2cb1360SIngo Molnar return !memcmp(cur ? (cur + idx_cur) : &tmp, 2013f2cb1360SIngo Molnar new ? (new + idx_new) : &tmp, 2014f2cb1360SIngo Molnar sizeof(struct sched_domain_attr)); 2015f2cb1360SIngo Molnar } 2016f2cb1360SIngo Molnar 2017f2cb1360SIngo Molnar /* 2018f2cb1360SIngo Molnar * Partition sched domains as specified by the 'ndoms_new' 2019f2cb1360SIngo Molnar * cpumasks in the array doms_new[] of cpumasks. This compares 2020f2cb1360SIngo Molnar * doms_new[] to the current sched domain partitioning, doms_cur[]. 2021f2cb1360SIngo Molnar * It destroys each deleted domain and builds each new domain. 2022f2cb1360SIngo Molnar * 2023f2cb1360SIngo Molnar * 'doms_new' is an array of cpumask_var_t's of length 'ndoms_new'. 2024f2cb1360SIngo Molnar * The masks don't intersect (don't overlap.) We should setup one 2025f2cb1360SIngo Molnar * sched domain for each mask. CPUs not in any of the cpumasks will 2026f2cb1360SIngo Molnar * not be load balanced. If the same cpumask appears both in the 2027f2cb1360SIngo Molnar * current 'doms_cur' domains and in the new 'doms_new', we can leave 2028f2cb1360SIngo Molnar * it as it is. 2029f2cb1360SIngo Molnar * 2030f2cb1360SIngo Molnar * The passed in 'doms_new' should be allocated using 2031f2cb1360SIngo Molnar * alloc_sched_domains. This routine takes ownership of it and will 2032f2cb1360SIngo Molnar * free_sched_domains it when done with it. If the caller failed the 2033f2cb1360SIngo Molnar * alloc call, then it can pass in doms_new == NULL && ndoms_new == 1, 2034f2cb1360SIngo Molnar * and partition_sched_domains() will fallback to the single partition 2035f2cb1360SIngo Molnar * 'fallback_doms', it also forces the domains to be rebuilt. 2036f2cb1360SIngo Molnar * 2037f2cb1360SIngo Molnar * If doms_new == NULL it will be replaced with cpu_online_mask. 2038f2cb1360SIngo Molnar * ndoms_new == 0 is a special case for destroying existing domains, 2039f2cb1360SIngo Molnar * and it will not create the default domain. 2040f2cb1360SIngo Molnar * 2041f2cb1360SIngo Molnar * Call with hotplug lock held 2042f2cb1360SIngo Molnar */ 2043f2cb1360SIngo Molnar void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[], 2044f2cb1360SIngo Molnar struct sched_domain_attr *dattr_new) 2045f2cb1360SIngo Molnar { 2046f2cb1360SIngo Molnar int i, j, n; 2047f2cb1360SIngo Molnar int new_topology; 2048f2cb1360SIngo Molnar 2049f2cb1360SIngo Molnar mutex_lock(&sched_domains_mutex); 2050f2cb1360SIngo Molnar 2051f2cb1360SIngo Molnar /* Always unregister in case we don't destroy any domains: */ 2052f2cb1360SIngo Molnar unregister_sched_domain_sysctl(); 2053f2cb1360SIngo Molnar 2054f2cb1360SIngo Molnar /* Let the architecture update CPU core mappings: */ 2055f2cb1360SIngo Molnar new_topology = arch_update_cpu_topology(); 2056f2cb1360SIngo Molnar 205709e0dd8eSPeter Zijlstra if (!doms_new) { 205809e0dd8eSPeter Zijlstra WARN_ON_ONCE(dattr_new); 205909e0dd8eSPeter Zijlstra n = 0; 206009e0dd8eSPeter Zijlstra doms_new = alloc_sched_domains(1); 206109e0dd8eSPeter Zijlstra if (doms_new) { 206209e0dd8eSPeter Zijlstra n = 1; 2063edb93821SFrederic Weisbecker cpumask_and(doms_new[0], cpu_active_mask, 2064edb93821SFrederic Weisbecker housekeeping_cpumask(HK_FLAG_DOMAIN)); 206509e0dd8eSPeter Zijlstra } 206609e0dd8eSPeter Zijlstra } else { 206709e0dd8eSPeter Zijlstra n = ndoms_new; 206809e0dd8eSPeter Zijlstra } 2069f2cb1360SIngo Molnar 2070f2cb1360SIngo Molnar /* Destroy deleted domains: */ 2071f2cb1360SIngo Molnar for (i = 0; i < ndoms_cur; i++) { 2072f2cb1360SIngo Molnar for (j = 0; j < n && !new_topology; j++) { 2073*6aa140faSQuentin Perret if (cpumask_equal(doms_cur[i], doms_new[j]) && 2074*6aa140faSQuentin Perret dattrs_equal(dattr_cur, i, dattr_new, j)) 2075f2cb1360SIngo Molnar goto match1; 2076f2cb1360SIngo Molnar } 2077f2cb1360SIngo Molnar /* No match - a current sched domain not in new doms_new[] */ 2078f2cb1360SIngo Molnar detach_destroy_domains(doms_cur[i]); 2079f2cb1360SIngo Molnar match1: 2080f2cb1360SIngo Molnar ; 2081f2cb1360SIngo Molnar } 2082f2cb1360SIngo Molnar 2083f2cb1360SIngo Molnar n = ndoms_cur; 208409e0dd8eSPeter Zijlstra if (!doms_new) { 2085f2cb1360SIngo Molnar n = 0; 2086f2cb1360SIngo Molnar doms_new = &fallback_doms; 2087edb93821SFrederic Weisbecker cpumask_and(doms_new[0], cpu_active_mask, 2088edb93821SFrederic Weisbecker housekeeping_cpumask(HK_FLAG_DOMAIN)); 2089f2cb1360SIngo Molnar } 2090f2cb1360SIngo Molnar 2091f2cb1360SIngo Molnar /* Build new domains: */ 2092f2cb1360SIngo Molnar for (i = 0; i < ndoms_new; i++) { 2093f2cb1360SIngo Molnar for (j = 0; j < n && !new_topology; j++) { 2094*6aa140faSQuentin Perret if (cpumask_equal(doms_new[i], doms_cur[j]) && 2095*6aa140faSQuentin Perret dattrs_equal(dattr_new, i, dattr_cur, j)) 2096f2cb1360SIngo Molnar goto match2; 2097f2cb1360SIngo Molnar } 2098f2cb1360SIngo Molnar /* No match - add a new doms_new */ 2099f2cb1360SIngo Molnar build_sched_domains(doms_new[i], dattr_new ? dattr_new + i : NULL); 2100f2cb1360SIngo Molnar match2: 2101f2cb1360SIngo Molnar ; 2102f2cb1360SIngo Molnar } 2103f2cb1360SIngo Molnar 2104*6aa140faSQuentin Perret #ifdef CONFIG_ENERGY_MODEL 2105*6aa140faSQuentin Perret /* Build perf. domains: */ 2106*6aa140faSQuentin Perret for (i = 0; i < ndoms_new; i++) { 2107*6aa140faSQuentin Perret for (j = 0; j < n; j++) { 2108*6aa140faSQuentin Perret if (cpumask_equal(doms_new[i], doms_cur[j]) && 2109*6aa140faSQuentin Perret cpu_rq(cpumask_first(doms_cur[j]))->rd->pd) 2110*6aa140faSQuentin Perret goto match3; 2111*6aa140faSQuentin Perret } 2112*6aa140faSQuentin Perret /* No match - add perf. domains for a new rd */ 2113*6aa140faSQuentin Perret build_perf_domains(doms_new[i]); 2114*6aa140faSQuentin Perret match3: 2115*6aa140faSQuentin Perret ; 2116*6aa140faSQuentin Perret } 2117*6aa140faSQuentin Perret #endif 2118*6aa140faSQuentin Perret 2119f2cb1360SIngo Molnar /* Remember the new sched domains: */ 2120f2cb1360SIngo Molnar if (doms_cur != &fallback_doms) 2121f2cb1360SIngo Molnar free_sched_domains(doms_cur, ndoms_cur); 2122f2cb1360SIngo Molnar 2123f2cb1360SIngo Molnar kfree(dattr_cur); 2124f2cb1360SIngo Molnar doms_cur = doms_new; 2125f2cb1360SIngo Molnar dattr_cur = dattr_new; 2126f2cb1360SIngo Molnar ndoms_cur = ndoms_new; 2127f2cb1360SIngo Molnar 2128f2cb1360SIngo Molnar register_sched_domain_sysctl(); 2129f2cb1360SIngo Molnar 2130f2cb1360SIngo Molnar mutex_unlock(&sched_domains_mutex); 2131f2cb1360SIngo Molnar } 2132