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