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; 3265c5e253SValentin Schneider unsigned long flags = sd->flags; 3365c5e253SValentin Schneider unsigned int idx; 34f2cb1360SIngo Molnar 35f2cb1360SIngo Molnar cpumask_clear(groupmask); 36f2cb1360SIngo Molnar 37005f874dSPeter Zijlstra printk(KERN_DEBUG "%*s domain-%d: ", level, "", level); 38005f874dSPeter Zijlstra printk(KERN_CONT "span=%*pbl level=%s\n", 39f2cb1360SIngo Molnar cpumask_pr_args(sched_domain_span(sd)), sd->name); 40f2cb1360SIngo Molnar 41f2cb1360SIngo Molnar if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) { 4297fb7a0aSIngo Molnar printk(KERN_ERR "ERROR: domain->span does not contain CPU%d\n", cpu); 43f2cb1360SIngo Molnar } 446cd0c583SYi Wang if (group && !cpumask_test_cpu(cpu, sched_group_span(group))) { 4597fb7a0aSIngo Molnar printk(KERN_ERR "ERROR: domain->groups does not contain CPU%d\n", cpu); 46f2cb1360SIngo Molnar } 47f2cb1360SIngo Molnar 4865c5e253SValentin Schneider for_each_set_bit(idx, &flags, __SD_FLAG_CNT) { 4965c5e253SValentin Schneider unsigned int flag = BIT(idx); 5065c5e253SValentin Schneider unsigned int meta_flags = sd_flag_debug[idx].meta_flags; 5165c5e253SValentin Schneider 5265c5e253SValentin Schneider if ((meta_flags & SDF_SHARED_CHILD) && sd->child && 5365c5e253SValentin Schneider !(sd->child->flags & flag)) 5465c5e253SValentin Schneider printk(KERN_ERR "ERROR: flag %s set here but not in child\n", 5565c5e253SValentin Schneider sd_flag_debug[idx].name); 5665c5e253SValentin Schneider 5765c5e253SValentin Schneider if ((meta_flags & SDF_SHARED_PARENT) && sd->parent && 5865c5e253SValentin Schneider !(sd->parent->flags & flag)) 5965c5e253SValentin Schneider printk(KERN_ERR "ERROR: flag %s set here but not in parent\n", 6065c5e253SValentin Schneider sd_flag_debug[idx].name); 6165c5e253SValentin Schneider } 6265c5e253SValentin Schneider 63f2cb1360SIngo Molnar printk(KERN_DEBUG "%*s groups:", level + 1, ""); 64f2cb1360SIngo Molnar do { 65f2cb1360SIngo Molnar if (!group) { 66f2cb1360SIngo Molnar printk("\n"); 67f2cb1360SIngo Molnar printk(KERN_ERR "ERROR: group is NULL\n"); 68f2cb1360SIngo Molnar break; 69f2cb1360SIngo Molnar } 70f2cb1360SIngo Molnar 71ae4df9d6SPeter Zijlstra if (!cpumask_weight(sched_group_span(group))) { 72f2cb1360SIngo Molnar printk(KERN_CONT "\n"); 73f2cb1360SIngo Molnar printk(KERN_ERR "ERROR: empty group\n"); 74f2cb1360SIngo Molnar break; 75f2cb1360SIngo Molnar } 76f2cb1360SIngo Molnar 77f2cb1360SIngo Molnar if (!(sd->flags & SD_OVERLAP) && 78ae4df9d6SPeter Zijlstra cpumask_intersects(groupmask, sched_group_span(group))) { 79f2cb1360SIngo Molnar printk(KERN_CONT "\n"); 80f2cb1360SIngo Molnar printk(KERN_ERR "ERROR: repeated CPUs\n"); 81f2cb1360SIngo Molnar break; 82f2cb1360SIngo Molnar } 83f2cb1360SIngo Molnar 84ae4df9d6SPeter Zijlstra cpumask_or(groupmask, groupmask, sched_group_span(group)); 85f2cb1360SIngo Molnar 86005f874dSPeter Zijlstra printk(KERN_CONT " %d:{ span=%*pbl", 87005f874dSPeter Zijlstra group->sgc->id, 88ae4df9d6SPeter Zijlstra cpumask_pr_args(sched_group_span(group))); 89b0151c25SPeter Zijlstra 90af218122SPeter Zijlstra if ((sd->flags & SD_OVERLAP) && 91ae4df9d6SPeter Zijlstra !cpumask_equal(group_balance_mask(group), sched_group_span(group))) { 92005f874dSPeter Zijlstra printk(KERN_CONT " mask=%*pbl", 93e5c14b1fSPeter Zijlstra cpumask_pr_args(group_balance_mask(group))); 94b0151c25SPeter Zijlstra } 95b0151c25SPeter Zijlstra 96005f874dSPeter Zijlstra if (group->sgc->capacity != SCHED_CAPACITY_SCALE) 97005f874dSPeter Zijlstra printk(KERN_CONT " cap=%lu", group->sgc->capacity); 98f2cb1360SIngo Molnar 99a420b063SPeter Zijlstra if (group == sd->groups && sd->child && 100a420b063SPeter Zijlstra !cpumask_equal(sched_domain_span(sd->child), 101ae4df9d6SPeter Zijlstra sched_group_span(group))) { 102a420b063SPeter Zijlstra printk(KERN_ERR "ERROR: domain->groups does not match domain->child\n"); 103a420b063SPeter Zijlstra } 104a420b063SPeter Zijlstra 105005f874dSPeter Zijlstra printk(KERN_CONT " }"); 106005f874dSPeter Zijlstra 107f2cb1360SIngo Molnar group = group->next; 108b0151c25SPeter Zijlstra 109b0151c25SPeter Zijlstra if (group != sd->groups) 110b0151c25SPeter Zijlstra printk(KERN_CONT ","); 111b0151c25SPeter Zijlstra 112f2cb1360SIngo Molnar } while (group != sd->groups); 113f2cb1360SIngo Molnar printk(KERN_CONT "\n"); 114f2cb1360SIngo Molnar 115f2cb1360SIngo Molnar if (!cpumask_equal(sched_domain_span(sd), groupmask)) 116f2cb1360SIngo Molnar printk(KERN_ERR "ERROR: groups don't span domain->span\n"); 117f2cb1360SIngo Molnar 118f2cb1360SIngo Molnar if (sd->parent && 119f2cb1360SIngo Molnar !cpumask_subset(groupmask, sched_domain_span(sd->parent))) 12097fb7a0aSIngo Molnar printk(KERN_ERR "ERROR: parent span is not a superset of domain->span\n"); 121f2cb1360SIngo Molnar return 0; 122f2cb1360SIngo Molnar } 123f2cb1360SIngo Molnar 124f2cb1360SIngo Molnar static void sched_domain_debug(struct sched_domain *sd, int cpu) 125f2cb1360SIngo Molnar { 126f2cb1360SIngo Molnar int level = 0; 127f2cb1360SIngo Molnar 128f2cb1360SIngo Molnar if (!sched_debug_enabled) 129f2cb1360SIngo Molnar return; 130f2cb1360SIngo Molnar 131f2cb1360SIngo Molnar if (!sd) { 132f2cb1360SIngo Molnar printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu); 133f2cb1360SIngo Molnar return; 134f2cb1360SIngo Molnar } 135f2cb1360SIngo Molnar 136005f874dSPeter Zijlstra printk(KERN_DEBUG "CPU%d attaching sched-domain(s):\n", cpu); 137f2cb1360SIngo Molnar 138f2cb1360SIngo Molnar for (;;) { 139f2cb1360SIngo Molnar if (sched_domain_debug_one(sd, cpu, level, sched_domains_tmpmask)) 140f2cb1360SIngo Molnar break; 141f2cb1360SIngo Molnar level++; 142f2cb1360SIngo Molnar sd = sd->parent; 143f2cb1360SIngo Molnar if (!sd) 144f2cb1360SIngo Molnar break; 145f2cb1360SIngo Molnar } 146f2cb1360SIngo Molnar } 147f2cb1360SIngo Molnar #else /* !CONFIG_SCHED_DEBUG */ 148f2cb1360SIngo Molnar 149f2cb1360SIngo Molnar # define sched_debug_enabled 0 150f2cb1360SIngo Molnar # define sched_domain_debug(sd, cpu) do { } while (0) 151f2cb1360SIngo Molnar static inline bool sched_debug(void) 152f2cb1360SIngo Molnar { 153f2cb1360SIngo Molnar return false; 154f2cb1360SIngo Molnar } 155f2cb1360SIngo Molnar #endif /* CONFIG_SCHED_DEBUG */ 156f2cb1360SIngo Molnar 157f2cb1360SIngo Molnar static int sd_degenerate(struct sched_domain *sd) 158f2cb1360SIngo Molnar { 159f2cb1360SIngo Molnar if (cpumask_weight(sched_domain_span(sd)) == 1) 160f2cb1360SIngo Molnar return 1; 161f2cb1360SIngo Molnar 162f2cb1360SIngo Molnar /* Following flags need at least 2 groups */ 1636f349818SValentin Schneider if ((sd->flags & SD_DEGENERATE_GROUPS_MASK) && 1646f349818SValentin Schneider (sd->groups != sd->groups->next)) 165f2cb1360SIngo Molnar return 0; 166f2cb1360SIngo Molnar 167f2cb1360SIngo Molnar /* Following flags don't use groups */ 168f2cb1360SIngo Molnar if (sd->flags & (SD_WAKE_AFFINE)) 169f2cb1360SIngo Molnar return 0; 170f2cb1360SIngo Molnar 171f2cb1360SIngo Molnar return 1; 172f2cb1360SIngo Molnar } 173f2cb1360SIngo Molnar 174f2cb1360SIngo Molnar static int 175f2cb1360SIngo Molnar sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent) 176f2cb1360SIngo Molnar { 177f2cb1360SIngo Molnar unsigned long cflags = sd->flags, pflags = parent->flags; 178f2cb1360SIngo Molnar 179f2cb1360SIngo Molnar if (sd_degenerate(parent)) 180f2cb1360SIngo Molnar return 1; 181f2cb1360SIngo Molnar 182f2cb1360SIngo Molnar if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent))) 183f2cb1360SIngo Molnar return 0; 184f2cb1360SIngo Molnar 185f2cb1360SIngo Molnar /* Flags needing groups don't count if only 1 group in parent */ 186ab65afb0SValentin Schneider if (parent->groups == parent->groups->next) 1876f349818SValentin Schneider pflags &= ~(SD_DEGENERATE_GROUPS_MASK | SD_PREFER_SIBLING); 188ab65afb0SValentin Schneider 189f2cb1360SIngo Molnar if (~cflags & pflags) 190f2cb1360SIngo Molnar return 0; 191f2cb1360SIngo Molnar 192f2cb1360SIngo Molnar return 1; 193f2cb1360SIngo Molnar } 194f2cb1360SIngo Molnar 195531b5c9fSQuentin Perret #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL) 196f8a696f2SPeter Zijlstra DEFINE_STATIC_KEY_FALSE(sched_energy_present); 1978d5d0cfbSQuentin Perret unsigned int sysctl_sched_energy_aware = 1; 198531b5c9fSQuentin Perret DEFINE_MUTEX(sched_energy_mutex); 199531b5c9fSQuentin Perret bool sched_energy_update; 200531b5c9fSQuentin Perret 2018d5d0cfbSQuentin Perret #ifdef CONFIG_PROC_SYSCTL 2028d5d0cfbSQuentin Perret int sched_energy_aware_handler(struct ctl_table *table, int write, 20332927393SChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos) 2048d5d0cfbSQuentin Perret { 2058d5d0cfbSQuentin Perret int ret, state; 2068d5d0cfbSQuentin Perret 2078d5d0cfbSQuentin Perret if (write && !capable(CAP_SYS_ADMIN)) 2088d5d0cfbSQuentin Perret return -EPERM; 2098d5d0cfbSQuentin Perret 2108d5d0cfbSQuentin Perret ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); 2118d5d0cfbSQuentin Perret if (!ret && write) { 2128d5d0cfbSQuentin Perret state = static_branch_unlikely(&sched_energy_present); 2138d5d0cfbSQuentin Perret if (state != sysctl_sched_energy_aware) { 2148d5d0cfbSQuentin Perret mutex_lock(&sched_energy_mutex); 2158d5d0cfbSQuentin Perret sched_energy_update = 1; 2168d5d0cfbSQuentin Perret rebuild_sched_domains(); 2178d5d0cfbSQuentin Perret sched_energy_update = 0; 2188d5d0cfbSQuentin Perret mutex_unlock(&sched_energy_mutex); 2198d5d0cfbSQuentin Perret } 2208d5d0cfbSQuentin Perret } 2218d5d0cfbSQuentin Perret 2228d5d0cfbSQuentin Perret return ret; 2238d5d0cfbSQuentin Perret } 2248d5d0cfbSQuentin Perret #endif 2258d5d0cfbSQuentin Perret 2266aa140faSQuentin Perret static void free_pd(struct perf_domain *pd) 2276aa140faSQuentin Perret { 2286aa140faSQuentin Perret struct perf_domain *tmp; 2296aa140faSQuentin Perret 2306aa140faSQuentin Perret while (pd) { 2316aa140faSQuentin Perret tmp = pd->next; 2326aa140faSQuentin Perret kfree(pd); 2336aa140faSQuentin Perret pd = tmp; 2346aa140faSQuentin Perret } 2356aa140faSQuentin Perret } 2366aa140faSQuentin Perret 2376aa140faSQuentin Perret static struct perf_domain *find_pd(struct perf_domain *pd, int cpu) 2386aa140faSQuentin Perret { 2396aa140faSQuentin Perret while (pd) { 2406aa140faSQuentin Perret if (cpumask_test_cpu(cpu, perf_domain_span(pd))) 2416aa140faSQuentin Perret return pd; 2426aa140faSQuentin Perret pd = pd->next; 2436aa140faSQuentin Perret } 2446aa140faSQuentin Perret 2456aa140faSQuentin Perret return NULL; 2466aa140faSQuentin Perret } 2476aa140faSQuentin Perret 2486aa140faSQuentin Perret static struct perf_domain *pd_init(int cpu) 2496aa140faSQuentin Perret { 2506aa140faSQuentin Perret struct em_perf_domain *obj = em_cpu_get(cpu); 2516aa140faSQuentin Perret struct perf_domain *pd; 2526aa140faSQuentin Perret 2536aa140faSQuentin Perret if (!obj) { 2546aa140faSQuentin Perret if (sched_debug()) 2556aa140faSQuentin Perret pr_info("%s: no EM found for CPU%d\n", __func__, cpu); 2566aa140faSQuentin Perret return NULL; 2576aa140faSQuentin Perret } 2586aa140faSQuentin Perret 2596aa140faSQuentin Perret pd = kzalloc(sizeof(*pd), GFP_KERNEL); 2606aa140faSQuentin Perret if (!pd) 2616aa140faSQuentin Perret return NULL; 2626aa140faSQuentin Perret pd->em_pd = obj; 2636aa140faSQuentin Perret 2646aa140faSQuentin Perret return pd; 2656aa140faSQuentin Perret } 2666aa140faSQuentin Perret 2676aa140faSQuentin Perret static void perf_domain_debug(const struct cpumask *cpu_map, 2686aa140faSQuentin Perret struct perf_domain *pd) 2696aa140faSQuentin Perret { 2706aa140faSQuentin Perret if (!sched_debug() || !pd) 2716aa140faSQuentin Perret return; 2726aa140faSQuentin Perret 2736aa140faSQuentin Perret printk(KERN_DEBUG "root_domain %*pbl:", cpumask_pr_args(cpu_map)); 2746aa140faSQuentin Perret 2756aa140faSQuentin Perret while (pd) { 276521b512bSLukasz Luba printk(KERN_CONT " pd%d:{ cpus=%*pbl nr_pstate=%d }", 2776aa140faSQuentin Perret cpumask_first(perf_domain_span(pd)), 2786aa140faSQuentin Perret cpumask_pr_args(perf_domain_span(pd)), 279521b512bSLukasz Luba em_pd_nr_perf_states(pd->em_pd)); 2806aa140faSQuentin Perret pd = pd->next; 2816aa140faSQuentin Perret } 2826aa140faSQuentin Perret 2836aa140faSQuentin Perret printk(KERN_CONT "\n"); 2846aa140faSQuentin Perret } 2856aa140faSQuentin Perret 2866aa140faSQuentin Perret static void destroy_perf_domain_rcu(struct rcu_head *rp) 2876aa140faSQuentin Perret { 2886aa140faSQuentin Perret struct perf_domain *pd; 2896aa140faSQuentin Perret 2906aa140faSQuentin Perret pd = container_of(rp, struct perf_domain, rcu); 2916aa140faSQuentin Perret free_pd(pd); 2926aa140faSQuentin Perret } 2936aa140faSQuentin Perret 2941f74de87SQuentin Perret static void sched_energy_set(bool has_eas) 2951f74de87SQuentin Perret { 2961f74de87SQuentin Perret if (!has_eas && static_branch_unlikely(&sched_energy_present)) { 2971f74de87SQuentin Perret if (sched_debug()) 2981f74de87SQuentin Perret pr_info("%s: stopping EAS\n", __func__); 2991f74de87SQuentin Perret static_branch_disable_cpuslocked(&sched_energy_present); 3001f74de87SQuentin Perret } else if (has_eas && !static_branch_unlikely(&sched_energy_present)) { 3011f74de87SQuentin Perret if (sched_debug()) 3021f74de87SQuentin Perret pr_info("%s: starting EAS\n", __func__); 3031f74de87SQuentin Perret static_branch_enable_cpuslocked(&sched_energy_present); 3041f74de87SQuentin Perret } 3051f74de87SQuentin Perret } 3061f74de87SQuentin Perret 307b68a4c0dSQuentin Perret /* 308b68a4c0dSQuentin Perret * EAS can be used on a root domain if it meets all the following conditions: 309b68a4c0dSQuentin Perret * 1. an Energy Model (EM) is available; 310b68a4c0dSQuentin Perret * 2. the SD_ASYM_CPUCAPACITY flag is set in the sched_domain hierarchy. 31138502ab4SValentin Schneider * 3. no SMT is detected. 31238502ab4SValentin Schneider * 4. the EM complexity is low enough to keep scheduling overheads low; 31338502ab4SValentin Schneider * 5. schedutil is driving the frequency of all CPUs of the rd; 314b68a4c0dSQuentin Perret * 315b68a4c0dSQuentin Perret * The complexity of the Energy Model is defined as: 316b68a4c0dSQuentin Perret * 317521b512bSLukasz Luba * C = nr_pd * (nr_cpus + nr_ps) 318b68a4c0dSQuentin Perret * 319b68a4c0dSQuentin Perret * with parameters defined as: 320b68a4c0dSQuentin Perret * - nr_pd: the number of performance domains 321b68a4c0dSQuentin Perret * - nr_cpus: the number of CPUs 322521b512bSLukasz Luba * - nr_ps: the sum of the number of performance states of all performance 323b68a4c0dSQuentin Perret * domains (for example, on a system with 2 performance domains, 324521b512bSLukasz Luba * with 10 performance states each, nr_ps = 2 * 10 = 20). 325b68a4c0dSQuentin Perret * 326b68a4c0dSQuentin Perret * It is generally not a good idea to use such a model in the wake-up path on 327b68a4c0dSQuentin Perret * very complex platforms because of the associated scheduling overheads. The 328b68a4c0dSQuentin Perret * arbitrary constraint below prevents that. It makes EAS usable up to 16 CPUs 329521b512bSLukasz Luba * with per-CPU DVFS and less than 8 performance states each, for example. 330b68a4c0dSQuentin Perret */ 331b68a4c0dSQuentin Perret #define EM_MAX_COMPLEXITY 2048 332b68a4c0dSQuentin Perret 333531b5c9fSQuentin Perret extern struct cpufreq_governor schedutil_gov; 3341f74de87SQuentin Perret static bool build_perf_domains(const struct cpumask *cpu_map) 3356aa140faSQuentin Perret { 336521b512bSLukasz Luba int i, nr_pd = 0, nr_ps = 0, nr_cpus = cpumask_weight(cpu_map); 3376aa140faSQuentin Perret struct perf_domain *pd = NULL, *tmp; 3386aa140faSQuentin Perret int cpu = cpumask_first(cpu_map); 3396aa140faSQuentin Perret struct root_domain *rd = cpu_rq(cpu)->rd; 340531b5c9fSQuentin Perret struct cpufreq_policy *policy; 341531b5c9fSQuentin Perret struct cpufreq_governor *gov; 342b68a4c0dSQuentin Perret 3438d5d0cfbSQuentin Perret if (!sysctl_sched_energy_aware) 3448d5d0cfbSQuentin Perret goto free; 3458d5d0cfbSQuentin Perret 346b68a4c0dSQuentin Perret /* EAS is enabled for asymmetric CPU capacity topologies. */ 347b68a4c0dSQuentin Perret if (!per_cpu(sd_asym_cpucapacity, cpu)) { 348b68a4c0dSQuentin Perret if (sched_debug()) { 349b68a4c0dSQuentin Perret pr_info("rd %*pbl: CPUs do not have asymmetric capacities\n", 350b68a4c0dSQuentin Perret cpumask_pr_args(cpu_map)); 351b68a4c0dSQuentin Perret } 352b68a4c0dSQuentin Perret goto free; 353b68a4c0dSQuentin Perret } 3546aa140faSQuentin Perret 35538502ab4SValentin Schneider /* EAS definitely does *not* handle SMT */ 35638502ab4SValentin Schneider if (sched_smt_active()) { 35738502ab4SValentin Schneider pr_warn("rd %*pbl: Disabling EAS, SMT is not supported\n", 35838502ab4SValentin Schneider cpumask_pr_args(cpu_map)); 35938502ab4SValentin Schneider goto free; 36038502ab4SValentin Schneider } 36138502ab4SValentin Schneider 3626aa140faSQuentin Perret for_each_cpu(i, cpu_map) { 3636aa140faSQuentin Perret /* Skip already covered CPUs. */ 3646aa140faSQuentin Perret if (find_pd(pd, i)) 3656aa140faSQuentin Perret continue; 3666aa140faSQuentin Perret 367531b5c9fSQuentin Perret /* Do not attempt EAS if schedutil is not being used. */ 368531b5c9fSQuentin Perret policy = cpufreq_cpu_get(i); 369531b5c9fSQuentin Perret if (!policy) 370531b5c9fSQuentin Perret goto free; 371531b5c9fSQuentin Perret gov = policy->governor; 372531b5c9fSQuentin Perret cpufreq_cpu_put(policy); 373531b5c9fSQuentin Perret if (gov != &schedutil_gov) { 374531b5c9fSQuentin Perret if (rd->pd) 375531b5c9fSQuentin Perret pr_warn("rd %*pbl: Disabling EAS, schedutil is mandatory\n", 376531b5c9fSQuentin Perret cpumask_pr_args(cpu_map)); 377531b5c9fSQuentin Perret goto free; 378531b5c9fSQuentin Perret } 379531b5c9fSQuentin Perret 3806aa140faSQuentin Perret /* Create the new pd and add it to the local list. */ 3816aa140faSQuentin Perret tmp = pd_init(i); 3826aa140faSQuentin Perret if (!tmp) 3836aa140faSQuentin Perret goto free; 3846aa140faSQuentin Perret tmp->next = pd; 3856aa140faSQuentin Perret pd = tmp; 386b68a4c0dSQuentin Perret 387b68a4c0dSQuentin Perret /* 388521b512bSLukasz Luba * Count performance domains and performance states for the 389b68a4c0dSQuentin Perret * complexity check. 390b68a4c0dSQuentin Perret */ 391b68a4c0dSQuentin Perret nr_pd++; 392521b512bSLukasz Luba nr_ps += em_pd_nr_perf_states(pd->em_pd); 393b68a4c0dSQuentin Perret } 394b68a4c0dSQuentin Perret 395b68a4c0dSQuentin Perret /* Bail out if the Energy Model complexity is too high. */ 396521b512bSLukasz Luba if (nr_pd * (nr_ps + nr_cpus) > EM_MAX_COMPLEXITY) { 397b68a4c0dSQuentin Perret WARN(1, "rd %*pbl: Failed to start EAS, EM complexity is too high\n", 398b68a4c0dSQuentin Perret cpumask_pr_args(cpu_map)); 399b68a4c0dSQuentin Perret goto free; 4006aa140faSQuentin Perret } 4016aa140faSQuentin Perret 4026aa140faSQuentin Perret perf_domain_debug(cpu_map, pd); 4036aa140faSQuentin Perret 4046aa140faSQuentin Perret /* Attach the new list of performance domains to the root domain. */ 4056aa140faSQuentin Perret tmp = rd->pd; 4066aa140faSQuentin Perret rcu_assign_pointer(rd->pd, pd); 4076aa140faSQuentin Perret if (tmp) 4086aa140faSQuentin Perret call_rcu(&tmp->rcu, destroy_perf_domain_rcu); 4096aa140faSQuentin Perret 4101f74de87SQuentin Perret return !!pd; 4116aa140faSQuentin Perret 4126aa140faSQuentin Perret free: 4136aa140faSQuentin Perret free_pd(pd); 4146aa140faSQuentin Perret tmp = rd->pd; 4156aa140faSQuentin Perret rcu_assign_pointer(rd->pd, NULL); 4166aa140faSQuentin Perret if (tmp) 4176aa140faSQuentin Perret call_rcu(&tmp->rcu, destroy_perf_domain_rcu); 4181f74de87SQuentin Perret 4191f74de87SQuentin Perret return false; 4206aa140faSQuentin Perret } 4216aa140faSQuentin Perret #else 4226aa140faSQuentin Perret static void free_pd(struct perf_domain *pd) { } 423531b5c9fSQuentin Perret #endif /* CONFIG_ENERGY_MODEL && CONFIG_CPU_FREQ_GOV_SCHEDUTIL*/ 4246aa140faSQuentin Perret 425f2cb1360SIngo Molnar static void free_rootdomain(struct rcu_head *rcu) 426f2cb1360SIngo Molnar { 427f2cb1360SIngo Molnar struct root_domain *rd = container_of(rcu, struct root_domain, rcu); 428f2cb1360SIngo Molnar 429f2cb1360SIngo Molnar cpupri_cleanup(&rd->cpupri); 430f2cb1360SIngo Molnar cpudl_cleanup(&rd->cpudl); 431f2cb1360SIngo Molnar free_cpumask_var(rd->dlo_mask); 432f2cb1360SIngo Molnar free_cpumask_var(rd->rto_mask); 433f2cb1360SIngo Molnar free_cpumask_var(rd->online); 434f2cb1360SIngo Molnar free_cpumask_var(rd->span); 4356aa140faSQuentin Perret free_pd(rd->pd); 436f2cb1360SIngo Molnar kfree(rd); 437f2cb1360SIngo Molnar } 438f2cb1360SIngo Molnar 439f2cb1360SIngo Molnar void rq_attach_root(struct rq *rq, struct root_domain *rd) 440f2cb1360SIngo Molnar { 441f2cb1360SIngo Molnar struct root_domain *old_rd = NULL; 442f2cb1360SIngo Molnar unsigned long flags; 443f2cb1360SIngo Molnar 444f2cb1360SIngo Molnar raw_spin_lock_irqsave(&rq->lock, flags); 445f2cb1360SIngo Molnar 446f2cb1360SIngo Molnar if (rq->rd) { 447f2cb1360SIngo Molnar old_rd = rq->rd; 448f2cb1360SIngo Molnar 449f2cb1360SIngo Molnar if (cpumask_test_cpu(rq->cpu, old_rd->online)) 450f2cb1360SIngo Molnar set_rq_offline(rq); 451f2cb1360SIngo Molnar 452f2cb1360SIngo Molnar cpumask_clear_cpu(rq->cpu, old_rd->span); 453f2cb1360SIngo Molnar 454f2cb1360SIngo Molnar /* 455f2cb1360SIngo Molnar * If we dont want to free the old_rd yet then 456f2cb1360SIngo Molnar * set old_rd to NULL to skip the freeing later 457f2cb1360SIngo Molnar * in this function: 458f2cb1360SIngo Molnar */ 459f2cb1360SIngo Molnar if (!atomic_dec_and_test(&old_rd->refcount)) 460f2cb1360SIngo Molnar old_rd = NULL; 461f2cb1360SIngo Molnar } 462f2cb1360SIngo Molnar 463f2cb1360SIngo Molnar atomic_inc(&rd->refcount); 464f2cb1360SIngo Molnar rq->rd = rd; 465f2cb1360SIngo Molnar 466f2cb1360SIngo Molnar cpumask_set_cpu(rq->cpu, rd->span); 467f2cb1360SIngo Molnar if (cpumask_test_cpu(rq->cpu, cpu_active_mask)) 468f2cb1360SIngo Molnar set_rq_online(rq); 469f2cb1360SIngo Molnar 470f2cb1360SIngo Molnar raw_spin_unlock_irqrestore(&rq->lock, flags); 471f2cb1360SIngo Molnar 472f2cb1360SIngo Molnar if (old_rd) 473337e9b07SPaul E. McKenney call_rcu(&old_rd->rcu, free_rootdomain); 474f2cb1360SIngo Molnar } 475f2cb1360SIngo Molnar 476364f5665SSteven Rostedt (VMware) void sched_get_rd(struct root_domain *rd) 477364f5665SSteven Rostedt (VMware) { 478364f5665SSteven Rostedt (VMware) atomic_inc(&rd->refcount); 479364f5665SSteven Rostedt (VMware) } 480364f5665SSteven Rostedt (VMware) 481364f5665SSteven Rostedt (VMware) void sched_put_rd(struct root_domain *rd) 482364f5665SSteven Rostedt (VMware) { 483364f5665SSteven Rostedt (VMware) if (!atomic_dec_and_test(&rd->refcount)) 484364f5665SSteven Rostedt (VMware) return; 485364f5665SSteven Rostedt (VMware) 486337e9b07SPaul E. McKenney call_rcu(&rd->rcu, free_rootdomain); 487364f5665SSteven Rostedt (VMware) } 488364f5665SSteven Rostedt (VMware) 489f2cb1360SIngo Molnar static int init_rootdomain(struct root_domain *rd) 490f2cb1360SIngo Molnar { 491f2cb1360SIngo Molnar if (!zalloc_cpumask_var(&rd->span, GFP_KERNEL)) 492f2cb1360SIngo Molnar goto out; 493f2cb1360SIngo Molnar if (!zalloc_cpumask_var(&rd->online, GFP_KERNEL)) 494f2cb1360SIngo Molnar goto free_span; 495f2cb1360SIngo Molnar if (!zalloc_cpumask_var(&rd->dlo_mask, GFP_KERNEL)) 496f2cb1360SIngo Molnar goto free_online; 497f2cb1360SIngo Molnar if (!zalloc_cpumask_var(&rd->rto_mask, GFP_KERNEL)) 498f2cb1360SIngo Molnar goto free_dlo_mask; 499f2cb1360SIngo Molnar 5004bdced5cSSteven Rostedt (Red Hat) #ifdef HAVE_RT_PUSH_IPI 5014bdced5cSSteven Rostedt (Red Hat) rd->rto_cpu = -1; 5024bdced5cSSteven Rostedt (Red Hat) raw_spin_lock_init(&rd->rto_lock); 5034bdced5cSSteven Rostedt (Red Hat) init_irq_work(&rd->rto_push_work, rto_push_irq_work_func); 5044bdced5cSSteven Rostedt (Red Hat) #endif 5054bdced5cSSteven Rostedt (Red Hat) 506f2cb1360SIngo Molnar init_dl_bw(&rd->dl_bw); 507f2cb1360SIngo Molnar if (cpudl_init(&rd->cpudl) != 0) 508f2cb1360SIngo Molnar goto free_rto_mask; 509f2cb1360SIngo Molnar 510f2cb1360SIngo Molnar if (cpupri_init(&rd->cpupri) != 0) 511f2cb1360SIngo Molnar goto free_cpudl; 512f2cb1360SIngo Molnar return 0; 513f2cb1360SIngo Molnar 514f2cb1360SIngo Molnar free_cpudl: 515f2cb1360SIngo Molnar cpudl_cleanup(&rd->cpudl); 516f2cb1360SIngo Molnar free_rto_mask: 517f2cb1360SIngo Molnar free_cpumask_var(rd->rto_mask); 518f2cb1360SIngo Molnar free_dlo_mask: 519f2cb1360SIngo Molnar free_cpumask_var(rd->dlo_mask); 520f2cb1360SIngo Molnar free_online: 521f2cb1360SIngo Molnar free_cpumask_var(rd->online); 522f2cb1360SIngo Molnar free_span: 523f2cb1360SIngo Molnar free_cpumask_var(rd->span); 524f2cb1360SIngo Molnar out: 525f2cb1360SIngo Molnar return -ENOMEM; 526f2cb1360SIngo Molnar } 527f2cb1360SIngo Molnar 528f2cb1360SIngo Molnar /* 529f2cb1360SIngo Molnar * By default the system creates a single root-domain with all CPUs as 530f2cb1360SIngo Molnar * members (mimicking the global state we have today). 531f2cb1360SIngo Molnar */ 532f2cb1360SIngo Molnar struct root_domain def_root_domain; 533f2cb1360SIngo Molnar 534f2cb1360SIngo Molnar void init_defrootdomain(void) 535f2cb1360SIngo Molnar { 536f2cb1360SIngo Molnar init_rootdomain(&def_root_domain); 537f2cb1360SIngo Molnar 538f2cb1360SIngo Molnar atomic_set(&def_root_domain.refcount, 1); 539f2cb1360SIngo Molnar } 540f2cb1360SIngo Molnar 541f2cb1360SIngo Molnar static struct root_domain *alloc_rootdomain(void) 542f2cb1360SIngo Molnar { 543f2cb1360SIngo Molnar struct root_domain *rd; 544f2cb1360SIngo Molnar 5454d13a06dSViresh Kumar rd = kzalloc(sizeof(*rd), GFP_KERNEL); 546f2cb1360SIngo Molnar if (!rd) 547f2cb1360SIngo Molnar return NULL; 548f2cb1360SIngo Molnar 549f2cb1360SIngo Molnar if (init_rootdomain(rd) != 0) { 550f2cb1360SIngo Molnar kfree(rd); 551f2cb1360SIngo Molnar return NULL; 552f2cb1360SIngo Molnar } 553f2cb1360SIngo Molnar 554f2cb1360SIngo Molnar return rd; 555f2cb1360SIngo Molnar } 556f2cb1360SIngo Molnar 557f2cb1360SIngo Molnar static void free_sched_groups(struct sched_group *sg, int free_sgc) 558f2cb1360SIngo Molnar { 559f2cb1360SIngo Molnar struct sched_group *tmp, *first; 560f2cb1360SIngo Molnar 561f2cb1360SIngo Molnar if (!sg) 562f2cb1360SIngo Molnar return; 563f2cb1360SIngo Molnar 564f2cb1360SIngo Molnar first = sg; 565f2cb1360SIngo Molnar do { 566f2cb1360SIngo Molnar tmp = sg->next; 567f2cb1360SIngo Molnar 568f2cb1360SIngo Molnar if (free_sgc && atomic_dec_and_test(&sg->sgc->ref)) 569f2cb1360SIngo Molnar kfree(sg->sgc); 570f2cb1360SIngo Molnar 571213c5a45SShu Wang if (atomic_dec_and_test(&sg->ref)) 572f2cb1360SIngo Molnar kfree(sg); 573f2cb1360SIngo Molnar sg = tmp; 574f2cb1360SIngo Molnar } while (sg != first); 575f2cb1360SIngo Molnar } 576f2cb1360SIngo Molnar 577f2cb1360SIngo Molnar static void destroy_sched_domain(struct sched_domain *sd) 578f2cb1360SIngo Molnar { 579f2cb1360SIngo Molnar /* 580a090c4f2SPeter Zijlstra * A normal sched domain may have multiple group references, an 581a090c4f2SPeter Zijlstra * overlapping domain, having private groups, only one. Iterate, 582a090c4f2SPeter Zijlstra * dropping group/capacity references, freeing where none remain. 583f2cb1360SIngo Molnar */ 584f2cb1360SIngo Molnar free_sched_groups(sd->groups, 1); 585213c5a45SShu Wang 586f2cb1360SIngo Molnar if (sd->shared && atomic_dec_and_test(&sd->shared->ref)) 587f2cb1360SIngo Molnar kfree(sd->shared); 588f2cb1360SIngo Molnar kfree(sd); 589f2cb1360SIngo Molnar } 590f2cb1360SIngo Molnar 591f2cb1360SIngo Molnar static void destroy_sched_domains_rcu(struct rcu_head *rcu) 592f2cb1360SIngo Molnar { 593f2cb1360SIngo Molnar struct sched_domain *sd = container_of(rcu, struct sched_domain, rcu); 594f2cb1360SIngo Molnar 595f2cb1360SIngo Molnar while (sd) { 596f2cb1360SIngo Molnar struct sched_domain *parent = sd->parent; 597f2cb1360SIngo Molnar destroy_sched_domain(sd); 598f2cb1360SIngo Molnar sd = parent; 599f2cb1360SIngo Molnar } 600f2cb1360SIngo Molnar } 601f2cb1360SIngo Molnar 602f2cb1360SIngo Molnar static void destroy_sched_domains(struct sched_domain *sd) 603f2cb1360SIngo Molnar { 604f2cb1360SIngo Molnar if (sd) 605f2cb1360SIngo Molnar call_rcu(&sd->rcu, destroy_sched_domains_rcu); 606f2cb1360SIngo Molnar } 607f2cb1360SIngo Molnar 608f2cb1360SIngo Molnar /* 609f2cb1360SIngo Molnar * Keep a special pointer to the highest sched_domain that has 610f2cb1360SIngo Molnar * SD_SHARE_PKG_RESOURCE set (Last Level Cache Domain) for this 611f2cb1360SIngo Molnar * allows us to avoid some pointer chasing select_idle_sibling(). 612f2cb1360SIngo Molnar * 613f2cb1360SIngo Molnar * Also keep a unique ID per domain (we use the first CPU number in 614f2cb1360SIngo Molnar * the cpumask of the domain), this allows us to quickly tell if 615f2cb1360SIngo Molnar * two CPUs are in the same cache domain, see cpus_share_cache(). 616f2cb1360SIngo Molnar */ 617994aeb7aSJoel Fernandes (Google) DEFINE_PER_CPU(struct sched_domain __rcu *, sd_llc); 618f2cb1360SIngo Molnar DEFINE_PER_CPU(int, sd_llc_size); 619f2cb1360SIngo Molnar DEFINE_PER_CPU(int, sd_llc_id); 620994aeb7aSJoel Fernandes (Google) DEFINE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared); 621994aeb7aSJoel Fernandes (Google) DEFINE_PER_CPU(struct sched_domain __rcu *, sd_numa); 622994aeb7aSJoel Fernandes (Google) DEFINE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing); 623994aeb7aSJoel Fernandes (Google) DEFINE_PER_CPU(struct sched_domain __rcu *, sd_asym_cpucapacity); 624df054e84SMorten Rasmussen DEFINE_STATIC_KEY_FALSE(sched_asym_cpucapacity); 625f2cb1360SIngo Molnar 626f2cb1360SIngo Molnar static void update_top_cache_domain(int cpu) 627f2cb1360SIngo Molnar { 628f2cb1360SIngo Molnar struct sched_domain_shared *sds = NULL; 629f2cb1360SIngo Molnar struct sched_domain *sd; 630f2cb1360SIngo Molnar int id = cpu; 631f2cb1360SIngo Molnar int size = 1; 632f2cb1360SIngo Molnar 633f2cb1360SIngo Molnar sd = highest_flag_domain(cpu, SD_SHARE_PKG_RESOURCES); 634f2cb1360SIngo Molnar if (sd) { 635f2cb1360SIngo Molnar id = cpumask_first(sched_domain_span(sd)); 636f2cb1360SIngo Molnar size = cpumask_weight(sched_domain_span(sd)); 637f2cb1360SIngo Molnar sds = sd->shared; 638f2cb1360SIngo Molnar } 639f2cb1360SIngo Molnar 640f2cb1360SIngo Molnar rcu_assign_pointer(per_cpu(sd_llc, cpu), sd); 641f2cb1360SIngo Molnar per_cpu(sd_llc_size, cpu) = size; 642f2cb1360SIngo Molnar per_cpu(sd_llc_id, cpu) = id; 643f2cb1360SIngo Molnar rcu_assign_pointer(per_cpu(sd_llc_shared, cpu), sds); 644f2cb1360SIngo Molnar 645f2cb1360SIngo Molnar sd = lowest_flag_domain(cpu, SD_NUMA); 646f2cb1360SIngo Molnar rcu_assign_pointer(per_cpu(sd_numa, cpu), sd); 647f2cb1360SIngo Molnar 648f2cb1360SIngo Molnar sd = highest_flag_domain(cpu, SD_ASYM_PACKING); 649011b27bbSQuentin Perret rcu_assign_pointer(per_cpu(sd_asym_packing, cpu), sd); 650011b27bbSQuentin Perret 651011b27bbSQuentin Perret sd = lowest_flag_domain(cpu, SD_ASYM_CPUCAPACITY); 652011b27bbSQuentin Perret rcu_assign_pointer(per_cpu(sd_asym_cpucapacity, cpu), sd); 653f2cb1360SIngo Molnar } 654f2cb1360SIngo Molnar 655f2cb1360SIngo Molnar /* 656f2cb1360SIngo Molnar * Attach the domain 'sd' to 'cpu' as its base domain. Callers must 657f2cb1360SIngo Molnar * hold the hotplug lock. 658f2cb1360SIngo Molnar */ 659f2cb1360SIngo Molnar static void 660f2cb1360SIngo Molnar cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu) 661f2cb1360SIngo Molnar { 662f2cb1360SIngo Molnar struct rq *rq = cpu_rq(cpu); 663f2cb1360SIngo Molnar struct sched_domain *tmp; 664f2cb1360SIngo Molnar 665f2cb1360SIngo Molnar /* Remove the sched domains which do not contribute to scheduling. */ 666f2cb1360SIngo Molnar for (tmp = sd; tmp; ) { 667f2cb1360SIngo Molnar struct sched_domain *parent = tmp->parent; 668f2cb1360SIngo Molnar if (!parent) 669f2cb1360SIngo Molnar break; 670f2cb1360SIngo Molnar 671f2cb1360SIngo Molnar if (sd_parent_degenerate(tmp, parent)) { 672f2cb1360SIngo Molnar tmp->parent = parent->parent; 673f2cb1360SIngo Molnar if (parent->parent) 674f2cb1360SIngo Molnar parent->parent->child = tmp; 675f2cb1360SIngo Molnar /* 676f2cb1360SIngo Molnar * Transfer SD_PREFER_SIBLING down in case of a 677f2cb1360SIngo Molnar * degenerate parent; the spans match for this 678f2cb1360SIngo Molnar * so the property transfers. 679f2cb1360SIngo Molnar */ 680f2cb1360SIngo Molnar if (parent->flags & SD_PREFER_SIBLING) 681f2cb1360SIngo Molnar tmp->flags |= SD_PREFER_SIBLING; 682f2cb1360SIngo Molnar destroy_sched_domain(parent); 683f2cb1360SIngo Molnar } else 684f2cb1360SIngo Molnar tmp = tmp->parent; 685f2cb1360SIngo Molnar } 686f2cb1360SIngo Molnar 687f2cb1360SIngo Molnar if (sd && sd_degenerate(sd)) { 688f2cb1360SIngo Molnar tmp = sd; 689f2cb1360SIngo Molnar sd = sd->parent; 690f2cb1360SIngo Molnar destroy_sched_domain(tmp); 691f2cb1360SIngo Molnar if (sd) 692f2cb1360SIngo Molnar sd->child = NULL; 693f2cb1360SIngo Molnar } 694f2cb1360SIngo Molnar 695f2cb1360SIngo Molnar sched_domain_debug(sd, cpu); 696f2cb1360SIngo Molnar 697f2cb1360SIngo Molnar rq_attach_root(rq, rd); 698f2cb1360SIngo Molnar tmp = rq->sd; 699f2cb1360SIngo Molnar rcu_assign_pointer(rq->sd, sd); 700bbdacdfeSPeter Zijlstra dirty_sched_domain_sysctl(cpu); 701f2cb1360SIngo Molnar destroy_sched_domains(tmp); 702f2cb1360SIngo Molnar 703f2cb1360SIngo Molnar update_top_cache_domain(cpu); 704f2cb1360SIngo Molnar } 705f2cb1360SIngo Molnar 706f2cb1360SIngo Molnar struct s_data { 70799687cdbSLuc Van Oostenryck struct sched_domain * __percpu *sd; 708f2cb1360SIngo Molnar struct root_domain *rd; 709f2cb1360SIngo Molnar }; 710f2cb1360SIngo Molnar 711f2cb1360SIngo Molnar enum s_alloc { 712f2cb1360SIngo Molnar sa_rootdomain, 713f2cb1360SIngo Molnar sa_sd, 714f2cb1360SIngo Molnar sa_sd_storage, 715f2cb1360SIngo Molnar sa_none, 716f2cb1360SIngo Molnar }; 717f2cb1360SIngo Molnar 718f2cb1360SIngo Molnar /* 71935a566e6SPeter Zijlstra * Return the canonical balance CPU for this group, this is the first CPU 720e5c14b1fSPeter Zijlstra * of this group that's also in the balance mask. 72135a566e6SPeter Zijlstra * 722e5c14b1fSPeter Zijlstra * The balance mask are all those CPUs that could actually end up at this 723e5c14b1fSPeter Zijlstra * group. See build_balance_mask(). 72435a566e6SPeter Zijlstra * 72535a566e6SPeter Zijlstra * Also see should_we_balance(). 72635a566e6SPeter Zijlstra */ 72735a566e6SPeter Zijlstra int group_balance_cpu(struct sched_group *sg) 72835a566e6SPeter Zijlstra { 729e5c14b1fSPeter Zijlstra return cpumask_first(group_balance_mask(sg)); 73035a566e6SPeter Zijlstra } 73135a566e6SPeter Zijlstra 73235a566e6SPeter Zijlstra 73335a566e6SPeter Zijlstra /* 73435a566e6SPeter Zijlstra * NUMA topology (first read the regular topology blurb below) 73535a566e6SPeter Zijlstra * 73635a566e6SPeter Zijlstra * Given a node-distance table, for example: 73735a566e6SPeter Zijlstra * 73835a566e6SPeter Zijlstra * node 0 1 2 3 73935a566e6SPeter Zijlstra * 0: 10 20 30 20 74035a566e6SPeter Zijlstra * 1: 20 10 20 30 74135a566e6SPeter Zijlstra * 2: 30 20 10 20 74235a566e6SPeter Zijlstra * 3: 20 30 20 10 74335a566e6SPeter Zijlstra * 74435a566e6SPeter Zijlstra * which represents a 4 node ring topology like: 74535a566e6SPeter Zijlstra * 74635a566e6SPeter Zijlstra * 0 ----- 1 74735a566e6SPeter Zijlstra * | | 74835a566e6SPeter Zijlstra * | | 74935a566e6SPeter Zijlstra * | | 75035a566e6SPeter Zijlstra * 3 ----- 2 75135a566e6SPeter Zijlstra * 75235a566e6SPeter Zijlstra * We want to construct domains and groups to represent this. The way we go 75335a566e6SPeter Zijlstra * about doing this is to build the domains on 'hops'. For each NUMA level we 75435a566e6SPeter Zijlstra * construct the mask of all nodes reachable in @level hops. 75535a566e6SPeter Zijlstra * 75635a566e6SPeter Zijlstra * For the above NUMA topology that gives 3 levels: 75735a566e6SPeter Zijlstra * 75835a566e6SPeter Zijlstra * NUMA-2 0-3 0-3 0-3 0-3 75935a566e6SPeter Zijlstra * groups: {0-1,3},{1-3} {0-2},{0,2-3} {1-3},{0-1,3} {0,2-3},{0-2} 76035a566e6SPeter Zijlstra * 76135a566e6SPeter Zijlstra * NUMA-1 0-1,3 0-2 1-3 0,2-3 76235a566e6SPeter Zijlstra * groups: {0},{1},{3} {0},{1},{2} {1},{2},{3} {0},{2},{3} 76335a566e6SPeter Zijlstra * 76435a566e6SPeter Zijlstra * NUMA-0 0 1 2 3 76535a566e6SPeter Zijlstra * 76635a566e6SPeter Zijlstra * 76735a566e6SPeter Zijlstra * As can be seen; things don't nicely line up as with the regular topology. 76835a566e6SPeter Zijlstra * When we iterate a domain in child domain chunks some nodes can be 76935a566e6SPeter Zijlstra * represented multiple times -- hence the "overlap" naming for this part of 77035a566e6SPeter Zijlstra * the topology. 77135a566e6SPeter Zijlstra * 77235a566e6SPeter Zijlstra * In order to minimize this overlap, we only build enough groups to cover the 77335a566e6SPeter Zijlstra * domain. For instance Node-0 NUMA-2 would only get groups: 0-1,3 and 1-3. 77435a566e6SPeter Zijlstra * 77535a566e6SPeter Zijlstra * Because: 77635a566e6SPeter Zijlstra * 77735a566e6SPeter Zijlstra * - the first group of each domain is its child domain; this 77835a566e6SPeter Zijlstra * gets us the first 0-1,3 77935a566e6SPeter Zijlstra * - the only uncovered node is 2, who's child domain is 1-3. 78035a566e6SPeter Zijlstra * 78135a566e6SPeter Zijlstra * However, because of the overlap, computing a unique CPU for each group is 78235a566e6SPeter Zijlstra * more complicated. Consider for instance the groups of NODE-1 NUMA-2, both 78335a566e6SPeter Zijlstra * groups include the CPUs of Node-0, while those CPUs would not in fact ever 78435a566e6SPeter Zijlstra * end up at those groups (they would end up in group: 0-1,3). 78535a566e6SPeter Zijlstra * 786e5c14b1fSPeter Zijlstra * To correct this we have to introduce the group balance mask. This mask 78735a566e6SPeter Zijlstra * will contain those CPUs in the group that can reach this group given the 78835a566e6SPeter Zijlstra * (child) domain tree. 78935a566e6SPeter Zijlstra * 79035a566e6SPeter Zijlstra * With this we can once again compute balance_cpu and sched_group_capacity 79135a566e6SPeter Zijlstra * relations. 79235a566e6SPeter Zijlstra * 79335a566e6SPeter Zijlstra * XXX include words on how balance_cpu is unique and therefore can be 79435a566e6SPeter Zijlstra * used for sched_group_capacity links. 79535a566e6SPeter Zijlstra * 79635a566e6SPeter Zijlstra * 79735a566e6SPeter Zijlstra * Another 'interesting' topology is: 79835a566e6SPeter Zijlstra * 79935a566e6SPeter Zijlstra * node 0 1 2 3 80035a566e6SPeter Zijlstra * 0: 10 20 20 30 80135a566e6SPeter Zijlstra * 1: 20 10 20 20 80235a566e6SPeter Zijlstra * 2: 20 20 10 20 80335a566e6SPeter Zijlstra * 3: 30 20 20 10 80435a566e6SPeter Zijlstra * 80535a566e6SPeter Zijlstra * Which looks a little like: 80635a566e6SPeter Zijlstra * 80735a566e6SPeter Zijlstra * 0 ----- 1 80835a566e6SPeter Zijlstra * | / | 80935a566e6SPeter Zijlstra * | / | 81035a566e6SPeter Zijlstra * | / | 81135a566e6SPeter Zijlstra * 2 ----- 3 81235a566e6SPeter Zijlstra * 81335a566e6SPeter Zijlstra * This topology is asymmetric, nodes 1,2 are fully connected, but nodes 0,3 81435a566e6SPeter Zijlstra * are not. 81535a566e6SPeter Zijlstra * 81635a566e6SPeter Zijlstra * This leads to a few particularly weird cases where the sched_domain's are 81797fb7a0aSIngo Molnar * not of the same number for each CPU. Consider: 81835a566e6SPeter Zijlstra * 81935a566e6SPeter Zijlstra * NUMA-2 0-3 0-3 82035a566e6SPeter Zijlstra * groups: {0-2},{1-3} {1-3},{0-2} 82135a566e6SPeter Zijlstra * 82235a566e6SPeter Zijlstra * NUMA-1 0-2 0-3 0-3 1-3 82335a566e6SPeter Zijlstra * 82435a566e6SPeter Zijlstra * NUMA-0 0 1 2 3 82535a566e6SPeter Zijlstra * 82635a566e6SPeter Zijlstra */ 82735a566e6SPeter Zijlstra 82835a566e6SPeter Zijlstra 82935a566e6SPeter Zijlstra /* 830e5c14b1fSPeter Zijlstra * Build the balance mask; it contains only those CPUs that can arrive at this 831e5c14b1fSPeter Zijlstra * group and should be considered to continue balancing. 83235a566e6SPeter Zijlstra * 83335a566e6SPeter Zijlstra * We do this during the group creation pass, therefore the group information 83435a566e6SPeter Zijlstra * isn't complete yet, however since each group represents a (child) domain we 83535a566e6SPeter Zijlstra * can fully construct this using the sched_domain bits (which are already 83635a566e6SPeter Zijlstra * complete). 837f2cb1360SIngo Molnar */ 8381676330eSPeter Zijlstra static void 839e5c14b1fSPeter Zijlstra build_balance_mask(struct sched_domain *sd, struct sched_group *sg, struct cpumask *mask) 840f2cb1360SIngo Molnar { 841ae4df9d6SPeter Zijlstra const struct cpumask *sg_span = sched_group_span(sg); 842f2cb1360SIngo Molnar struct sd_data *sdd = sd->private; 843f2cb1360SIngo Molnar struct sched_domain *sibling; 844f2cb1360SIngo Molnar int i; 845f2cb1360SIngo Molnar 8461676330eSPeter Zijlstra cpumask_clear(mask); 8471676330eSPeter Zijlstra 848f32d782eSLauro Ramos Venancio for_each_cpu(i, sg_span) { 849f2cb1360SIngo Molnar sibling = *per_cpu_ptr(sdd->sd, i); 85073bb059fSPeter Zijlstra 85173bb059fSPeter Zijlstra /* 85273bb059fSPeter Zijlstra * Can happen in the asymmetric case, where these siblings are 85373bb059fSPeter Zijlstra * unused. The mask will not be empty because those CPUs that 85473bb059fSPeter Zijlstra * do have the top domain _should_ span the domain. 85573bb059fSPeter Zijlstra */ 85673bb059fSPeter Zijlstra if (!sibling->child) 85773bb059fSPeter Zijlstra continue; 85873bb059fSPeter Zijlstra 85973bb059fSPeter Zijlstra /* If we would not end up here, we can't continue from here */ 86073bb059fSPeter Zijlstra if (!cpumask_equal(sg_span, sched_domain_span(sibling->child))) 861f2cb1360SIngo Molnar continue; 862f2cb1360SIngo Molnar 8631676330eSPeter Zijlstra cpumask_set_cpu(i, mask); 864f2cb1360SIngo Molnar } 86573bb059fSPeter Zijlstra 86673bb059fSPeter Zijlstra /* We must not have empty masks here */ 8671676330eSPeter Zijlstra WARN_ON_ONCE(cpumask_empty(mask)); 868f2cb1360SIngo Molnar } 869f2cb1360SIngo Molnar 870f2cb1360SIngo Molnar /* 87135a566e6SPeter Zijlstra * XXX: This creates per-node group entries; since the load-balancer will 87235a566e6SPeter Zijlstra * immediately access remote memory to construct this group's load-balance 87335a566e6SPeter Zijlstra * statistics having the groups node local is of dubious benefit. 874f2cb1360SIngo Molnar */ 8758c033469SLauro Ramos Venancio static struct sched_group * 8768c033469SLauro Ramos Venancio build_group_from_child_sched_domain(struct sched_domain *sd, int cpu) 8778c033469SLauro Ramos Venancio { 8788c033469SLauro Ramos Venancio struct sched_group *sg; 8798c033469SLauro Ramos Venancio struct cpumask *sg_span; 8808c033469SLauro Ramos Venancio 8818c033469SLauro Ramos Venancio sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(), 8828c033469SLauro Ramos Venancio GFP_KERNEL, cpu_to_node(cpu)); 8838c033469SLauro Ramos Venancio 8848c033469SLauro Ramos Venancio if (!sg) 8858c033469SLauro Ramos Venancio return NULL; 8868c033469SLauro Ramos Venancio 887ae4df9d6SPeter Zijlstra sg_span = sched_group_span(sg); 8888c033469SLauro Ramos Venancio if (sd->child) 8898c033469SLauro Ramos Venancio cpumask_copy(sg_span, sched_domain_span(sd->child)); 8908c033469SLauro Ramos Venancio else 8918c033469SLauro Ramos Venancio cpumask_copy(sg_span, sched_domain_span(sd)); 8928c033469SLauro Ramos Venancio 893213c5a45SShu Wang atomic_inc(&sg->ref); 8948c033469SLauro Ramos Venancio return sg; 8958c033469SLauro Ramos Venancio } 8968c033469SLauro Ramos Venancio 8978c033469SLauro Ramos Venancio static void init_overlap_sched_group(struct sched_domain *sd, 8981676330eSPeter Zijlstra struct sched_group *sg) 8998c033469SLauro Ramos Venancio { 9001676330eSPeter Zijlstra struct cpumask *mask = sched_domains_tmpmask2; 9018c033469SLauro Ramos Venancio struct sd_data *sdd = sd->private; 9028c033469SLauro Ramos Venancio struct cpumask *sg_span; 9031676330eSPeter Zijlstra int cpu; 9041676330eSPeter Zijlstra 905e5c14b1fSPeter Zijlstra build_balance_mask(sd, sg, mask); 906ae4df9d6SPeter Zijlstra cpu = cpumask_first_and(sched_group_span(sg), mask); 9078c033469SLauro Ramos Venancio 9088c033469SLauro Ramos Venancio sg->sgc = *per_cpu_ptr(sdd->sgc, cpu); 9098c033469SLauro Ramos Venancio if (atomic_inc_return(&sg->sgc->ref) == 1) 910e5c14b1fSPeter Zijlstra cpumask_copy(group_balance_mask(sg), mask); 91135a566e6SPeter Zijlstra else 912e5c14b1fSPeter Zijlstra WARN_ON_ONCE(!cpumask_equal(group_balance_mask(sg), mask)); 9138c033469SLauro Ramos Venancio 9148c033469SLauro Ramos Venancio /* 9158c033469SLauro Ramos Venancio * Initialize sgc->capacity such that even if we mess up the 9168c033469SLauro Ramos Venancio * domains and no possible iteration will get us here, we won't 9178c033469SLauro Ramos Venancio * die on a /0 trap. 9188c033469SLauro Ramos Venancio */ 919ae4df9d6SPeter Zijlstra sg_span = sched_group_span(sg); 9208c033469SLauro Ramos Venancio sg->sgc->capacity = SCHED_CAPACITY_SCALE * cpumask_weight(sg_span); 9218c033469SLauro Ramos Venancio sg->sgc->min_capacity = SCHED_CAPACITY_SCALE; 922e3d6d0cbSMorten Rasmussen sg->sgc->max_capacity = SCHED_CAPACITY_SCALE; 9238c033469SLauro Ramos Venancio } 9248c033469SLauro Ramos Venancio 925f2cb1360SIngo Molnar static int 926f2cb1360SIngo Molnar build_overlap_sched_groups(struct sched_domain *sd, int cpu) 927f2cb1360SIngo Molnar { 92891eaed0dSPeter Zijlstra struct sched_group *first = NULL, *last = NULL, *sg; 929f2cb1360SIngo Molnar const struct cpumask *span = sched_domain_span(sd); 930f2cb1360SIngo Molnar struct cpumask *covered = sched_domains_tmpmask; 931f2cb1360SIngo Molnar struct sd_data *sdd = sd->private; 932f2cb1360SIngo Molnar struct sched_domain *sibling; 933f2cb1360SIngo Molnar int i; 934f2cb1360SIngo Molnar 935f2cb1360SIngo Molnar cpumask_clear(covered); 936f2cb1360SIngo Molnar 9370372dd27SPeter Zijlstra for_each_cpu_wrap(i, span, cpu) { 938f2cb1360SIngo Molnar struct cpumask *sg_span; 939f2cb1360SIngo Molnar 940f2cb1360SIngo Molnar if (cpumask_test_cpu(i, covered)) 941f2cb1360SIngo Molnar continue; 942f2cb1360SIngo Molnar 943f2cb1360SIngo Molnar sibling = *per_cpu_ptr(sdd->sd, i); 944f2cb1360SIngo Molnar 945c20e1ea4SLauro Ramos Venancio /* 946c20e1ea4SLauro Ramos Venancio * Asymmetric node setups can result in situations where the 947c20e1ea4SLauro Ramos Venancio * domain tree is of unequal depth, make sure to skip domains 948c20e1ea4SLauro Ramos Venancio * that already cover the entire range. 949c20e1ea4SLauro Ramos Venancio * 950c20e1ea4SLauro Ramos Venancio * In that case build_sched_domains() will have terminated the 951c20e1ea4SLauro Ramos Venancio * iteration early and our sibling sd spans will be empty. 952c20e1ea4SLauro Ramos Venancio * Domains should always include the CPU they're built on, so 953c20e1ea4SLauro Ramos Venancio * check that. 954c20e1ea4SLauro Ramos Venancio */ 955f2cb1360SIngo Molnar if (!cpumask_test_cpu(i, sched_domain_span(sibling))) 956f2cb1360SIngo Molnar continue; 957f2cb1360SIngo Molnar 9588c033469SLauro Ramos Venancio sg = build_group_from_child_sched_domain(sibling, cpu); 959f2cb1360SIngo Molnar if (!sg) 960f2cb1360SIngo Molnar goto fail; 961f2cb1360SIngo Molnar 962ae4df9d6SPeter Zijlstra sg_span = sched_group_span(sg); 963f2cb1360SIngo Molnar cpumask_or(covered, covered, sg_span); 964f2cb1360SIngo Molnar 9651676330eSPeter Zijlstra init_overlap_sched_group(sd, sg); 966f2cb1360SIngo Molnar 967f2cb1360SIngo Molnar if (!first) 968f2cb1360SIngo Molnar first = sg; 969f2cb1360SIngo Molnar if (last) 970f2cb1360SIngo Molnar last->next = sg; 971f2cb1360SIngo Molnar last = sg; 972f2cb1360SIngo Molnar last->next = first; 973f2cb1360SIngo Molnar } 97491eaed0dSPeter Zijlstra sd->groups = first; 975f2cb1360SIngo Molnar 976f2cb1360SIngo Molnar return 0; 977f2cb1360SIngo Molnar 978f2cb1360SIngo Molnar fail: 979f2cb1360SIngo Molnar free_sched_groups(first, 0); 980f2cb1360SIngo Molnar 981f2cb1360SIngo Molnar return -ENOMEM; 982f2cb1360SIngo Molnar } 983f2cb1360SIngo Molnar 98435a566e6SPeter Zijlstra 98535a566e6SPeter Zijlstra /* 98635a566e6SPeter Zijlstra * Package topology (also see the load-balance blurb in fair.c) 98735a566e6SPeter Zijlstra * 98835a566e6SPeter Zijlstra * The scheduler builds a tree structure to represent a number of important 98935a566e6SPeter Zijlstra * topology features. By default (default_topology[]) these include: 99035a566e6SPeter Zijlstra * 99135a566e6SPeter Zijlstra * - Simultaneous multithreading (SMT) 99235a566e6SPeter Zijlstra * - Multi-Core Cache (MC) 99335a566e6SPeter Zijlstra * - Package (DIE) 99435a566e6SPeter Zijlstra * 99535a566e6SPeter Zijlstra * Where the last one more or less denotes everything up to a NUMA node. 99635a566e6SPeter Zijlstra * 99735a566e6SPeter Zijlstra * The tree consists of 3 primary data structures: 99835a566e6SPeter Zijlstra * 99935a566e6SPeter Zijlstra * sched_domain -> sched_group -> sched_group_capacity 100035a566e6SPeter Zijlstra * ^ ^ ^ ^ 100135a566e6SPeter Zijlstra * `-' `-' 100235a566e6SPeter Zijlstra * 100397fb7a0aSIngo Molnar * The sched_domains are per-CPU and have a two way link (parent & child) and 100435a566e6SPeter Zijlstra * denote the ever growing mask of CPUs belonging to that level of topology. 100535a566e6SPeter Zijlstra * 100635a566e6SPeter Zijlstra * Each sched_domain has a circular (double) linked list of sched_group's, each 100735a566e6SPeter Zijlstra * denoting the domains of the level below (or individual CPUs in case of the 100835a566e6SPeter Zijlstra * first domain level). The sched_group linked by a sched_domain includes the 100935a566e6SPeter Zijlstra * CPU of that sched_domain [*]. 101035a566e6SPeter Zijlstra * 101135a566e6SPeter Zijlstra * Take for instance a 2 threaded, 2 core, 2 cache cluster part: 101235a566e6SPeter Zijlstra * 101335a566e6SPeter Zijlstra * CPU 0 1 2 3 4 5 6 7 101435a566e6SPeter Zijlstra * 101535a566e6SPeter Zijlstra * DIE [ ] 101635a566e6SPeter Zijlstra * MC [ ] [ ] 101735a566e6SPeter Zijlstra * SMT [ ] [ ] [ ] [ ] 101835a566e6SPeter Zijlstra * 101935a566e6SPeter Zijlstra * - or - 102035a566e6SPeter Zijlstra * 102135a566e6SPeter Zijlstra * DIE 0-7 0-7 0-7 0-7 0-7 0-7 0-7 0-7 102235a566e6SPeter Zijlstra * MC 0-3 0-3 0-3 0-3 4-7 4-7 4-7 4-7 102335a566e6SPeter Zijlstra * SMT 0-1 0-1 2-3 2-3 4-5 4-5 6-7 6-7 102435a566e6SPeter Zijlstra * 102535a566e6SPeter Zijlstra * CPU 0 1 2 3 4 5 6 7 102635a566e6SPeter Zijlstra * 102735a566e6SPeter Zijlstra * One way to think about it is: sched_domain moves you up and down among these 102835a566e6SPeter Zijlstra * topology levels, while sched_group moves you sideways through it, at child 102935a566e6SPeter Zijlstra * domain granularity. 103035a566e6SPeter Zijlstra * 103135a566e6SPeter Zijlstra * sched_group_capacity ensures each unique sched_group has shared storage. 103235a566e6SPeter Zijlstra * 103335a566e6SPeter Zijlstra * There are two related construction problems, both require a CPU that 103435a566e6SPeter Zijlstra * uniquely identify each group (for a given domain): 103535a566e6SPeter Zijlstra * 103635a566e6SPeter Zijlstra * - The first is the balance_cpu (see should_we_balance() and the 103735a566e6SPeter Zijlstra * load-balance blub in fair.c); for each group we only want 1 CPU to 103835a566e6SPeter Zijlstra * continue balancing at a higher domain. 103935a566e6SPeter Zijlstra * 104035a566e6SPeter Zijlstra * - The second is the sched_group_capacity; we want all identical groups 104135a566e6SPeter Zijlstra * to share a single sched_group_capacity. 104235a566e6SPeter Zijlstra * 104335a566e6SPeter Zijlstra * Since these topologies are exclusive by construction. That is, its 104435a566e6SPeter Zijlstra * impossible for an SMT thread to belong to multiple cores, and cores to 104535a566e6SPeter Zijlstra * be part of multiple caches. There is a very clear and unique location 104635a566e6SPeter Zijlstra * for each CPU in the hierarchy. 104735a566e6SPeter Zijlstra * 104835a566e6SPeter Zijlstra * Therefore computing a unique CPU for each group is trivial (the iteration 104935a566e6SPeter Zijlstra * mask is redundant and set all 1s; all CPUs in a group will end up at _that_ 105035a566e6SPeter Zijlstra * group), we can simply pick the first CPU in each group. 105135a566e6SPeter Zijlstra * 105235a566e6SPeter Zijlstra * 105335a566e6SPeter Zijlstra * [*] in other words, the first group of each domain is its child domain. 105435a566e6SPeter Zijlstra */ 105535a566e6SPeter Zijlstra 10560c0e776aSPeter Zijlstra static struct sched_group *get_group(int cpu, struct sd_data *sdd) 1057f2cb1360SIngo Molnar { 1058f2cb1360SIngo Molnar struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu); 1059f2cb1360SIngo Molnar struct sched_domain *child = sd->child; 10600c0e776aSPeter Zijlstra struct sched_group *sg; 106167d4f6ffSValentin Schneider bool already_visited; 1062f2cb1360SIngo Molnar 1063f2cb1360SIngo Molnar if (child) 1064f2cb1360SIngo Molnar cpu = cpumask_first(sched_domain_span(child)); 1065f2cb1360SIngo Molnar 10660c0e776aSPeter Zijlstra sg = *per_cpu_ptr(sdd->sg, cpu); 10670c0e776aSPeter Zijlstra sg->sgc = *per_cpu_ptr(sdd->sgc, cpu); 1068f2cb1360SIngo Molnar 106967d4f6ffSValentin Schneider /* Increase refcounts for claim_allocations: */ 107067d4f6ffSValentin Schneider already_visited = atomic_inc_return(&sg->ref) > 1; 107167d4f6ffSValentin Schneider /* sgc visits should follow a similar trend as sg */ 107267d4f6ffSValentin Schneider WARN_ON(already_visited != (atomic_inc_return(&sg->sgc->ref) > 1)); 107367d4f6ffSValentin Schneider 107467d4f6ffSValentin Schneider /* If we have already visited that group, it's already initialized. */ 107567d4f6ffSValentin Schneider if (already_visited) 107667d4f6ffSValentin Schneider return sg; 10770c0e776aSPeter Zijlstra 10780c0e776aSPeter Zijlstra if (child) { 1079ae4df9d6SPeter Zijlstra cpumask_copy(sched_group_span(sg), sched_domain_span(child)); 1080ae4df9d6SPeter Zijlstra cpumask_copy(group_balance_mask(sg), sched_group_span(sg)); 10810c0e776aSPeter Zijlstra } else { 1082ae4df9d6SPeter Zijlstra cpumask_set_cpu(cpu, sched_group_span(sg)); 1083e5c14b1fSPeter Zijlstra cpumask_set_cpu(cpu, group_balance_mask(sg)); 1084f2cb1360SIngo Molnar } 1085f2cb1360SIngo Molnar 1086ae4df9d6SPeter Zijlstra sg->sgc->capacity = SCHED_CAPACITY_SCALE * cpumask_weight(sched_group_span(sg)); 10870c0e776aSPeter Zijlstra sg->sgc->min_capacity = SCHED_CAPACITY_SCALE; 1088e3d6d0cbSMorten Rasmussen sg->sgc->max_capacity = SCHED_CAPACITY_SCALE; 10890c0e776aSPeter Zijlstra 10900c0e776aSPeter Zijlstra return sg; 1091f2cb1360SIngo Molnar } 1092f2cb1360SIngo Molnar 1093f2cb1360SIngo Molnar /* 1094f2cb1360SIngo Molnar * build_sched_groups will build a circular linked list of the groups 1095d8743230SValentin Schneider * covered by the given span, will set each group's ->cpumask correctly, 1096d8743230SValentin Schneider * and will initialize their ->sgc. 1097f2cb1360SIngo Molnar * 1098f2cb1360SIngo Molnar * Assumes the sched_domain tree is fully constructed 1099f2cb1360SIngo Molnar */ 1100f2cb1360SIngo Molnar static int 1101f2cb1360SIngo Molnar build_sched_groups(struct sched_domain *sd, int cpu) 1102f2cb1360SIngo Molnar { 1103f2cb1360SIngo Molnar struct sched_group *first = NULL, *last = NULL; 1104f2cb1360SIngo Molnar struct sd_data *sdd = sd->private; 1105f2cb1360SIngo Molnar const struct cpumask *span = sched_domain_span(sd); 1106f2cb1360SIngo Molnar struct cpumask *covered; 1107f2cb1360SIngo Molnar int i; 1108f2cb1360SIngo Molnar 1109f2cb1360SIngo Molnar lockdep_assert_held(&sched_domains_mutex); 1110f2cb1360SIngo Molnar covered = sched_domains_tmpmask; 1111f2cb1360SIngo Molnar 1112f2cb1360SIngo Molnar cpumask_clear(covered); 1113f2cb1360SIngo Molnar 11140c0e776aSPeter Zijlstra for_each_cpu_wrap(i, span, cpu) { 1115f2cb1360SIngo Molnar struct sched_group *sg; 1116f2cb1360SIngo Molnar 1117f2cb1360SIngo Molnar if (cpumask_test_cpu(i, covered)) 1118f2cb1360SIngo Molnar continue; 1119f2cb1360SIngo Molnar 11200c0e776aSPeter Zijlstra sg = get_group(i, sdd); 1121f2cb1360SIngo Molnar 1122ae4df9d6SPeter Zijlstra cpumask_or(covered, covered, sched_group_span(sg)); 1123f2cb1360SIngo Molnar 1124f2cb1360SIngo Molnar if (!first) 1125f2cb1360SIngo Molnar first = sg; 1126f2cb1360SIngo Molnar if (last) 1127f2cb1360SIngo Molnar last->next = sg; 1128f2cb1360SIngo Molnar last = sg; 1129f2cb1360SIngo Molnar } 1130f2cb1360SIngo Molnar last->next = first; 11310c0e776aSPeter Zijlstra sd->groups = first; 1132f2cb1360SIngo Molnar 1133f2cb1360SIngo Molnar return 0; 1134f2cb1360SIngo Molnar } 1135f2cb1360SIngo Molnar 1136f2cb1360SIngo Molnar /* 1137f2cb1360SIngo Molnar * Initialize sched groups cpu_capacity. 1138f2cb1360SIngo Molnar * 1139f2cb1360SIngo Molnar * cpu_capacity indicates the capacity of sched group, which is used while 1140f2cb1360SIngo Molnar * distributing the load between different sched groups in a sched domain. 1141f2cb1360SIngo Molnar * Typically cpu_capacity for all the groups in a sched domain will be same 1142f2cb1360SIngo Molnar * unless there are asymmetries in the topology. If there are asymmetries, 1143f2cb1360SIngo Molnar * group having more cpu_capacity will pickup more load compared to the 1144f2cb1360SIngo Molnar * group having less cpu_capacity. 1145f2cb1360SIngo Molnar */ 1146f2cb1360SIngo Molnar static void init_sched_groups_capacity(int cpu, struct sched_domain *sd) 1147f2cb1360SIngo Molnar { 1148f2cb1360SIngo Molnar struct sched_group *sg = sd->groups; 1149f2cb1360SIngo Molnar 1150f2cb1360SIngo Molnar WARN_ON(!sg); 1151f2cb1360SIngo Molnar 1152f2cb1360SIngo Molnar do { 1153f2cb1360SIngo Molnar int cpu, max_cpu = -1; 1154f2cb1360SIngo Molnar 1155ae4df9d6SPeter Zijlstra sg->group_weight = cpumask_weight(sched_group_span(sg)); 1156f2cb1360SIngo Molnar 1157f2cb1360SIngo Molnar if (!(sd->flags & SD_ASYM_PACKING)) 1158f2cb1360SIngo Molnar goto next; 1159f2cb1360SIngo Molnar 1160ae4df9d6SPeter Zijlstra for_each_cpu(cpu, sched_group_span(sg)) { 1161f2cb1360SIngo Molnar if (max_cpu < 0) 1162f2cb1360SIngo Molnar max_cpu = cpu; 1163f2cb1360SIngo Molnar else if (sched_asym_prefer(cpu, max_cpu)) 1164f2cb1360SIngo Molnar max_cpu = cpu; 1165f2cb1360SIngo Molnar } 1166f2cb1360SIngo Molnar sg->asym_prefer_cpu = max_cpu; 1167f2cb1360SIngo Molnar 1168f2cb1360SIngo Molnar next: 1169f2cb1360SIngo Molnar sg = sg->next; 1170f2cb1360SIngo Molnar } while (sg != sd->groups); 1171f2cb1360SIngo Molnar 1172f2cb1360SIngo Molnar if (cpu != group_balance_cpu(sg)) 1173f2cb1360SIngo Molnar return; 1174f2cb1360SIngo Molnar 1175f2cb1360SIngo Molnar update_group_capacity(sd, cpu); 1176f2cb1360SIngo Molnar } 1177f2cb1360SIngo Molnar 1178f2cb1360SIngo Molnar /* 1179f2cb1360SIngo Molnar * Initializers for schedule domains 1180f2cb1360SIngo Molnar * Non-inlined to reduce accumulated stack pressure in build_sched_domains() 1181f2cb1360SIngo Molnar */ 1182f2cb1360SIngo Molnar 1183f2cb1360SIngo Molnar static int default_relax_domain_level = -1; 1184f2cb1360SIngo Molnar int sched_domain_level_max; 1185f2cb1360SIngo Molnar 1186f2cb1360SIngo Molnar static int __init setup_relax_domain_level(char *str) 1187f2cb1360SIngo Molnar { 1188f2cb1360SIngo Molnar if (kstrtoint(str, 0, &default_relax_domain_level)) 1189f2cb1360SIngo Molnar pr_warn("Unable to set relax_domain_level\n"); 1190f2cb1360SIngo Molnar 1191f2cb1360SIngo Molnar return 1; 1192f2cb1360SIngo Molnar } 1193f2cb1360SIngo Molnar __setup("relax_domain_level=", setup_relax_domain_level); 1194f2cb1360SIngo Molnar 1195f2cb1360SIngo Molnar static void set_domain_attribute(struct sched_domain *sd, 1196f2cb1360SIngo Molnar struct sched_domain_attr *attr) 1197f2cb1360SIngo Molnar { 1198f2cb1360SIngo Molnar int request; 1199f2cb1360SIngo Molnar 1200f2cb1360SIngo Molnar if (!attr || attr->relax_domain_level < 0) { 1201f2cb1360SIngo Molnar if (default_relax_domain_level < 0) 1202f2cb1360SIngo Molnar return; 1203f2cb1360SIngo Molnar request = default_relax_domain_level; 1204f2cb1360SIngo Molnar } else 1205f2cb1360SIngo Molnar request = attr->relax_domain_level; 12069ae7ab20SValentin Schneider 12079ae7ab20SValentin Schneider if (sd->level > request) { 1208f2cb1360SIngo Molnar /* Turn off idle balance on this domain: */ 1209f2cb1360SIngo Molnar sd->flags &= ~(SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE); 1210f2cb1360SIngo Molnar } 1211f2cb1360SIngo Molnar } 1212f2cb1360SIngo Molnar 1213f2cb1360SIngo Molnar static void __sdt_free(const struct cpumask *cpu_map); 1214f2cb1360SIngo Molnar static int __sdt_alloc(const struct cpumask *cpu_map); 1215f2cb1360SIngo Molnar 1216f2cb1360SIngo Molnar static void __free_domain_allocs(struct s_data *d, enum s_alloc what, 1217f2cb1360SIngo Molnar const struct cpumask *cpu_map) 1218f2cb1360SIngo Molnar { 1219f2cb1360SIngo Molnar switch (what) { 1220f2cb1360SIngo Molnar case sa_rootdomain: 1221f2cb1360SIngo Molnar if (!atomic_read(&d->rd->refcount)) 1222f2cb1360SIngo Molnar free_rootdomain(&d->rd->rcu); 1223f2cb1360SIngo Molnar /* Fall through */ 1224f2cb1360SIngo Molnar case sa_sd: 1225f2cb1360SIngo Molnar free_percpu(d->sd); 1226f2cb1360SIngo Molnar /* Fall through */ 1227f2cb1360SIngo Molnar case sa_sd_storage: 1228f2cb1360SIngo Molnar __sdt_free(cpu_map); 1229f2cb1360SIngo Molnar /* Fall through */ 1230f2cb1360SIngo Molnar case sa_none: 1231f2cb1360SIngo Molnar break; 1232f2cb1360SIngo Molnar } 1233f2cb1360SIngo Molnar } 1234f2cb1360SIngo Molnar 1235f2cb1360SIngo Molnar static enum s_alloc 1236f2cb1360SIngo Molnar __visit_domain_allocation_hell(struct s_data *d, const struct cpumask *cpu_map) 1237f2cb1360SIngo Molnar { 1238f2cb1360SIngo Molnar memset(d, 0, sizeof(*d)); 1239f2cb1360SIngo Molnar 1240f2cb1360SIngo Molnar if (__sdt_alloc(cpu_map)) 1241f2cb1360SIngo Molnar return sa_sd_storage; 1242f2cb1360SIngo Molnar d->sd = alloc_percpu(struct sched_domain *); 1243f2cb1360SIngo Molnar if (!d->sd) 1244f2cb1360SIngo Molnar return sa_sd_storage; 1245f2cb1360SIngo Molnar d->rd = alloc_rootdomain(); 1246f2cb1360SIngo Molnar if (!d->rd) 1247f2cb1360SIngo Molnar return sa_sd; 124897fb7a0aSIngo Molnar 1249f2cb1360SIngo Molnar return sa_rootdomain; 1250f2cb1360SIngo Molnar } 1251f2cb1360SIngo Molnar 1252f2cb1360SIngo Molnar /* 1253f2cb1360SIngo Molnar * NULL the sd_data elements we've used to build the sched_domain and 1254f2cb1360SIngo Molnar * sched_group structure so that the subsequent __free_domain_allocs() 1255f2cb1360SIngo Molnar * will not free the data we're using. 1256f2cb1360SIngo Molnar */ 1257f2cb1360SIngo Molnar static void claim_allocations(int cpu, struct sched_domain *sd) 1258f2cb1360SIngo Molnar { 1259f2cb1360SIngo Molnar struct sd_data *sdd = sd->private; 1260f2cb1360SIngo Molnar 1261f2cb1360SIngo Molnar WARN_ON_ONCE(*per_cpu_ptr(sdd->sd, cpu) != sd); 1262f2cb1360SIngo Molnar *per_cpu_ptr(sdd->sd, cpu) = NULL; 1263f2cb1360SIngo Molnar 1264f2cb1360SIngo Molnar if (atomic_read(&(*per_cpu_ptr(sdd->sds, cpu))->ref)) 1265f2cb1360SIngo Molnar *per_cpu_ptr(sdd->sds, cpu) = NULL; 1266f2cb1360SIngo Molnar 1267f2cb1360SIngo Molnar if (atomic_read(&(*per_cpu_ptr(sdd->sg, cpu))->ref)) 1268f2cb1360SIngo Molnar *per_cpu_ptr(sdd->sg, cpu) = NULL; 1269f2cb1360SIngo Molnar 1270f2cb1360SIngo Molnar if (atomic_read(&(*per_cpu_ptr(sdd->sgc, cpu))->ref)) 1271f2cb1360SIngo Molnar *per_cpu_ptr(sdd->sgc, cpu) = NULL; 1272f2cb1360SIngo Molnar } 1273f2cb1360SIngo Molnar 1274f2cb1360SIngo Molnar #ifdef CONFIG_NUMA 1275f2cb1360SIngo Molnar enum numa_topology_type sched_numa_topology_type; 127697fb7a0aSIngo Molnar 127797fb7a0aSIngo Molnar static int sched_domains_numa_levels; 1278f2cb1360SIngo Molnar static int sched_domains_curr_level; 127997fb7a0aSIngo Molnar 128097fb7a0aSIngo Molnar int sched_max_numa_distance; 128197fb7a0aSIngo Molnar static int *sched_domains_numa_distance; 128297fb7a0aSIngo Molnar static struct cpumask ***sched_domains_numa_masks; 1283a55c7454SMatt Fleming int __read_mostly node_reclaim_distance = RECLAIM_DISTANCE; 1284f2cb1360SIngo Molnar #endif 1285f2cb1360SIngo Molnar 1286f2cb1360SIngo Molnar /* 1287f2cb1360SIngo Molnar * SD_flags allowed in topology descriptions. 1288f2cb1360SIngo Molnar * 1289f2cb1360SIngo Molnar * These flags are purely descriptive of the topology and do not prescribe 1290f2cb1360SIngo Molnar * behaviour. Behaviour is artificial and mapped in the below sd_init() 1291f2cb1360SIngo Molnar * function: 1292f2cb1360SIngo Molnar * 1293f2cb1360SIngo Molnar * SD_SHARE_CPUCAPACITY - describes SMT topologies 1294f2cb1360SIngo Molnar * SD_SHARE_PKG_RESOURCES - describes shared caches 1295f2cb1360SIngo Molnar * SD_NUMA - describes NUMA topologies 1296f2cb1360SIngo Molnar * 1297f2cb1360SIngo Molnar * Odd one out, which beside describing the topology has a quirk also 1298f2cb1360SIngo Molnar * prescribes the desired behaviour that goes along with it: 1299f2cb1360SIngo Molnar * 1300f2cb1360SIngo Molnar * SD_ASYM_PACKING - describes SMT quirks 1301f2cb1360SIngo Molnar */ 1302f2cb1360SIngo Molnar #define TOPOLOGY_SD_FLAGS \ 1303f2cb1360SIngo Molnar (SD_SHARE_CPUCAPACITY | \ 1304f2cb1360SIngo Molnar SD_SHARE_PKG_RESOURCES | \ 1305f2cb1360SIngo Molnar SD_NUMA | \ 1306cfe7ddcbSValentin Schneider SD_ASYM_PACKING) 1307f2cb1360SIngo Molnar 1308f2cb1360SIngo Molnar static struct sched_domain * 1309f2cb1360SIngo Molnar sd_init(struct sched_domain_topology_level *tl, 1310f2cb1360SIngo Molnar const struct cpumask *cpu_map, 131105484e09SMorten Rasmussen struct sched_domain *child, int dflags, int cpu) 1312f2cb1360SIngo Molnar { 1313f2cb1360SIngo Molnar struct sd_data *sdd = &tl->data; 1314f2cb1360SIngo Molnar struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu); 1315f2cb1360SIngo Molnar int sd_id, sd_weight, sd_flags = 0; 1316f2cb1360SIngo Molnar 1317f2cb1360SIngo Molnar #ifdef CONFIG_NUMA 1318f2cb1360SIngo Molnar /* 1319f2cb1360SIngo Molnar * Ugly hack to pass state to sd_numa_mask()... 1320f2cb1360SIngo Molnar */ 1321f2cb1360SIngo Molnar sched_domains_curr_level = tl->numa_level; 1322f2cb1360SIngo Molnar #endif 1323f2cb1360SIngo Molnar 1324f2cb1360SIngo Molnar sd_weight = cpumask_weight(tl->mask(cpu)); 1325f2cb1360SIngo Molnar 1326f2cb1360SIngo Molnar if (tl->sd_flags) 1327f2cb1360SIngo Molnar sd_flags = (*tl->sd_flags)(); 1328f2cb1360SIngo Molnar if (WARN_ONCE(sd_flags & ~TOPOLOGY_SD_FLAGS, 1329f2cb1360SIngo Molnar "wrong sd_flags in topology description\n")) 13309b1b234bSPeng Liu sd_flags &= TOPOLOGY_SD_FLAGS; 1331f2cb1360SIngo Molnar 133205484e09SMorten Rasmussen /* Apply detected topology flags */ 133305484e09SMorten Rasmussen sd_flags |= dflags; 133405484e09SMorten Rasmussen 1335f2cb1360SIngo Molnar *sd = (struct sched_domain){ 1336f2cb1360SIngo Molnar .min_interval = sd_weight, 1337f2cb1360SIngo Molnar .max_interval = 2*sd_weight, 1338f2cb1360SIngo Molnar .busy_factor = 32, 1339f2cb1360SIngo Molnar .imbalance_pct = 125, 1340f2cb1360SIngo Molnar 1341f2cb1360SIngo Molnar .cache_nice_tries = 0, 1342f2cb1360SIngo Molnar 134336c5bdc4SValentin Schneider .flags = 1*SD_BALANCE_NEWIDLE 1344f2cb1360SIngo Molnar | 1*SD_BALANCE_EXEC 1345f2cb1360SIngo Molnar | 1*SD_BALANCE_FORK 1346f2cb1360SIngo Molnar | 0*SD_BALANCE_WAKE 1347f2cb1360SIngo Molnar | 1*SD_WAKE_AFFINE 1348f2cb1360SIngo Molnar | 0*SD_SHARE_CPUCAPACITY 1349f2cb1360SIngo Molnar | 0*SD_SHARE_PKG_RESOURCES 1350f2cb1360SIngo Molnar | 0*SD_SERIALIZE 13519c63e84dSMorten Rasmussen | 1*SD_PREFER_SIBLING 1352f2cb1360SIngo Molnar | 0*SD_NUMA 1353f2cb1360SIngo Molnar | sd_flags 1354f2cb1360SIngo Molnar , 1355f2cb1360SIngo Molnar 1356f2cb1360SIngo Molnar .last_balance = jiffies, 1357f2cb1360SIngo Molnar .balance_interval = sd_weight, 1358f2cb1360SIngo Molnar .max_newidle_lb_cost = 0, 1359f2cb1360SIngo Molnar .next_decay_max_lb_cost = jiffies, 1360f2cb1360SIngo Molnar .child = child, 1361f2cb1360SIngo Molnar #ifdef CONFIG_SCHED_DEBUG 1362f2cb1360SIngo Molnar .name = tl->name, 1363f2cb1360SIngo Molnar #endif 1364f2cb1360SIngo Molnar }; 1365f2cb1360SIngo Molnar 1366f2cb1360SIngo Molnar cpumask_and(sched_domain_span(sd), cpu_map, tl->mask(cpu)); 1367f2cb1360SIngo Molnar sd_id = cpumask_first(sched_domain_span(sd)); 1368f2cb1360SIngo Molnar 1369f2cb1360SIngo Molnar /* 1370f2cb1360SIngo Molnar * Convert topological properties into behaviour. 1371f2cb1360SIngo Molnar */ 1372f2cb1360SIngo Molnar 1373a526d466SMorten Rasmussen /* Don't attempt to spread across CPUs of different capacities. */ 1374a526d466SMorten Rasmussen if ((sd->flags & SD_ASYM_CPUCAPACITY) && sd->child) 13759c63e84dSMorten Rasmussen sd->child->flags &= ~SD_PREFER_SIBLING; 13769c63e84dSMorten Rasmussen 1377f2cb1360SIngo Molnar if (sd->flags & SD_SHARE_CPUCAPACITY) { 1378f2cb1360SIngo Molnar sd->imbalance_pct = 110; 1379f2cb1360SIngo Molnar 1380f2cb1360SIngo Molnar } else if (sd->flags & SD_SHARE_PKG_RESOURCES) { 1381f2cb1360SIngo Molnar sd->imbalance_pct = 117; 1382f2cb1360SIngo Molnar sd->cache_nice_tries = 1; 1383f2cb1360SIngo Molnar 1384f2cb1360SIngo Molnar #ifdef CONFIG_NUMA 1385f2cb1360SIngo Molnar } else if (sd->flags & SD_NUMA) { 1386f2cb1360SIngo Molnar sd->cache_nice_tries = 2; 1387f2cb1360SIngo Molnar 13889c63e84dSMorten Rasmussen sd->flags &= ~SD_PREFER_SIBLING; 1389f2cb1360SIngo Molnar sd->flags |= SD_SERIALIZE; 1390a55c7454SMatt Fleming if (sched_domains_numa_distance[tl->numa_level] > node_reclaim_distance) { 1391f2cb1360SIngo Molnar sd->flags &= ~(SD_BALANCE_EXEC | 1392f2cb1360SIngo Molnar SD_BALANCE_FORK | 1393f2cb1360SIngo Molnar SD_WAKE_AFFINE); 1394f2cb1360SIngo Molnar } 1395f2cb1360SIngo Molnar 1396f2cb1360SIngo Molnar #endif 1397f2cb1360SIngo Molnar } else { 1398f2cb1360SIngo Molnar sd->cache_nice_tries = 1; 1399f2cb1360SIngo Molnar } 1400f2cb1360SIngo Molnar 1401f2cb1360SIngo Molnar /* 1402f2cb1360SIngo Molnar * For all levels sharing cache; connect a sched_domain_shared 1403f2cb1360SIngo Molnar * instance. 1404f2cb1360SIngo Molnar */ 1405f2cb1360SIngo Molnar if (sd->flags & SD_SHARE_PKG_RESOURCES) { 1406f2cb1360SIngo Molnar sd->shared = *per_cpu_ptr(sdd->sds, sd_id); 1407f2cb1360SIngo Molnar atomic_inc(&sd->shared->ref); 1408f2cb1360SIngo Molnar atomic_set(&sd->shared->nr_busy_cpus, sd_weight); 1409f2cb1360SIngo Molnar } 1410f2cb1360SIngo Molnar 1411f2cb1360SIngo Molnar sd->private = sdd; 1412f2cb1360SIngo Molnar 1413f2cb1360SIngo Molnar return sd; 1414f2cb1360SIngo Molnar } 1415f2cb1360SIngo Molnar 1416f2cb1360SIngo Molnar /* 1417f2cb1360SIngo Molnar * Topology list, bottom-up. 1418f2cb1360SIngo Molnar */ 1419f2cb1360SIngo Molnar static struct sched_domain_topology_level default_topology[] = { 1420f2cb1360SIngo Molnar #ifdef CONFIG_SCHED_SMT 1421f2cb1360SIngo Molnar { cpu_smt_mask, cpu_smt_flags, SD_INIT_NAME(SMT) }, 1422f2cb1360SIngo Molnar #endif 1423f2cb1360SIngo Molnar #ifdef CONFIG_SCHED_MC 1424f2cb1360SIngo Molnar { cpu_coregroup_mask, cpu_core_flags, SD_INIT_NAME(MC) }, 1425f2cb1360SIngo Molnar #endif 1426f2cb1360SIngo Molnar { cpu_cpu_mask, SD_INIT_NAME(DIE) }, 1427f2cb1360SIngo Molnar { NULL, }, 1428f2cb1360SIngo Molnar }; 1429f2cb1360SIngo Molnar 1430f2cb1360SIngo Molnar static struct sched_domain_topology_level *sched_domain_topology = 1431f2cb1360SIngo Molnar default_topology; 1432f2cb1360SIngo Molnar 1433f2cb1360SIngo Molnar #define for_each_sd_topology(tl) \ 1434f2cb1360SIngo Molnar for (tl = sched_domain_topology; tl->mask; tl++) 1435f2cb1360SIngo Molnar 1436f2cb1360SIngo Molnar void set_sched_topology(struct sched_domain_topology_level *tl) 1437f2cb1360SIngo Molnar { 1438f2cb1360SIngo Molnar if (WARN_ON_ONCE(sched_smp_initialized)) 1439f2cb1360SIngo Molnar return; 1440f2cb1360SIngo Molnar 1441f2cb1360SIngo Molnar sched_domain_topology = tl; 1442f2cb1360SIngo Molnar } 1443f2cb1360SIngo Molnar 1444f2cb1360SIngo Molnar #ifdef CONFIG_NUMA 1445f2cb1360SIngo Molnar 1446f2cb1360SIngo Molnar static const struct cpumask *sd_numa_mask(int cpu) 1447f2cb1360SIngo Molnar { 1448f2cb1360SIngo Molnar return sched_domains_numa_masks[sched_domains_curr_level][cpu_to_node(cpu)]; 1449f2cb1360SIngo Molnar } 1450f2cb1360SIngo Molnar 1451f2cb1360SIngo Molnar static void sched_numa_warn(const char *str) 1452f2cb1360SIngo Molnar { 1453f2cb1360SIngo Molnar static int done = false; 1454f2cb1360SIngo Molnar int i,j; 1455f2cb1360SIngo Molnar 1456f2cb1360SIngo Molnar if (done) 1457f2cb1360SIngo Molnar return; 1458f2cb1360SIngo Molnar 1459f2cb1360SIngo Molnar done = true; 1460f2cb1360SIngo Molnar 1461f2cb1360SIngo Molnar printk(KERN_WARNING "ERROR: %s\n\n", str); 1462f2cb1360SIngo Molnar 1463f2cb1360SIngo Molnar for (i = 0; i < nr_node_ids; i++) { 1464f2cb1360SIngo Molnar printk(KERN_WARNING " "); 1465f2cb1360SIngo Molnar for (j = 0; j < nr_node_ids; j++) 1466f2cb1360SIngo Molnar printk(KERN_CONT "%02d ", node_distance(i,j)); 1467f2cb1360SIngo Molnar printk(KERN_CONT "\n"); 1468f2cb1360SIngo Molnar } 1469f2cb1360SIngo Molnar printk(KERN_WARNING "\n"); 1470f2cb1360SIngo Molnar } 1471f2cb1360SIngo Molnar 1472f2cb1360SIngo Molnar bool find_numa_distance(int distance) 1473f2cb1360SIngo Molnar { 1474f2cb1360SIngo Molnar int i; 1475f2cb1360SIngo Molnar 1476f2cb1360SIngo Molnar if (distance == node_distance(0, 0)) 1477f2cb1360SIngo Molnar return true; 1478f2cb1360SIngo Molnar 1479f2cb1360SIngo Molnar for (i = 0; i < sched_domains_numa_levels; i++) { 1480f2cb1360SIngo Molnar if (sched_domains_numa_distance[i] == distance) 1481f2cb1360SIngo Molnar return true; 1482f2cb1360SIngo Molnar } 1483f2cb1360SIngo Molnar 1484f2cb1360SIngo Molnar return false; 1485f2cb1360SIngo Molnar } 1486f2cb1360SIngo Molnar 1487f2cb1360SIngo Molnar /* 1488f2cb1360SIngo Molnar * A system can have three types of NUMA topology: 1489f2cb1360SIngo Molnar * NUMA_DIRECT: all nodes are directly connected, or not a NUMA system 1490f2cb1360SIngo Molnar * NUMA_GLUELESS_MESH: some nodes reachable through intermediary nodes 1491f2cb1360SIngo Molnar * NUMA_BACKPLANE: nodes can reach other nodes through a backplane 1492f2cb1360SIngo Molnar * 1493f2cb1360SIngo Molnar * The difference between a glueless mesh topology and a backplane 1494f2cb1360SIngo Molnar * topology lies in whether communication between not directly 1495f2cb1360SIngo Molnar * connected nodes goes through intermediary nodes (where programs 1496f2cb1360SIngo Molnar * could run), or through backplane controllers. This affects 1497f2cb1360SIngo Molnar * placement of programs. 1498f2cb1360SIngo Molnar * 1499f2cb1360SIngo Molnar * The type of topology can be discerned with the following tests: 1500f2cb1360SIngo Molnar * - If the maximum distance between any nodes is 1 hop, the system 1501f2cb1360SIngo Molnar * is directly connected. 1502f2cb1360SIngo Molnar * - If for two nodes A and B, located N > 1 hops away from each other, 1503f2cb1360SIngo Molnar * there is an intermediary node C, which is < N hops away from both 1504f2cb1360SIngo Molnar * nodes A and B, the system is a glueless mesh. 1505f2cb1360SIngo Molnar */ 1506f2cb1360SIngo Molnar static void init_numa_topology_type(void) 1507f2cb1360SIngo Molnar { 1508f2cb1360SIngo Molnar int a, b, c, n; 1509f2cb1360SIngo Molnar 1510f2cb1360SIngo Molnar n = sched_max_numa_distance; 1511f2cb1360SIngo Molnar 1512e5e96fafSSrikar Dronamraju if (sched_domains_numa_levels <= 2) { 1513f2cb1360SIngo Molnar sched_numa_topology_type = NUMA_DIRECT; 1514f2cb1360SIngo Molnar return; 1515f2cb1360SIngo Molnar } 1516f2cb1360SIngo Molnar 1517f2cb1360SIngo Molnar for_each_online_node(a) { 1518f2cb1360SIngo Molnar for_each_online_node(b) { 1519f2cb1360SIngo Molnar /* Find two nodes furthest removed from each other. */ 1520f2cb1360SIngo Molnar if (node_distance(a, b) < n) 1521f2cb1360SIngo Molnar continue; 1522f2cb1360SIngo Molnar 1523f2cb1360SIngo Molnar /* Is there an intermediary node between a and b? */ 1524f2cb1360SIngo Molnar for_each_online_node(c) { 1525f2cb1360SIngo Molnar if (node_distance(a, c) < n && 1526f2cb1360SIngo Molnar node_distance(b, c) < n) { 1527f2cb1360SIngo Molnar sched_numa_topology_type = 1528f2cb1360SIngo Molnar NUMA_GLUELESS_MESH; 1529f2cb1360SIngo Molnar return; 1530f2cb1360SIngo Molnar } 1531f2cb1360SIngo Molnar } 1532f2cb1360SIngo Molnar 1533f2cb1360SIngo Molnar sched_numa_topology_type = NUMA_BACKPLANE; 1534f2cb1360SIngo Molnar return; 1535f2cb1360SIngo Molnar } 1536f2cb1360SIngo Molnar } 1537f2cb1360SIngo Molnar } 1538f2cb1360SIngo Molnar 1539f2cb1360SIngo Molnar void sched_init_numa(void) 1540f2cb1360SIngo Molnar { 1541f2cb1360SIngo Molnar int next_distance, curr_distance = node_distance(0, 0); 1542f2cb1360SIngo Molnar struct sched_domain_topology_level *tl; 1543f2cb1360SIngo Molnar int level = 0; 1544f2cb1360SIngo Molnar int i, j, k; 1545f2cb1360SIngo Molnar 1546993f0b05SPeter Zijlstra sched_domains_numa_distance = kzalloc(sizeof(int) * (nr_node_ids + 1), GFP_KERNEL); 1547f2cb1360SIngo Molnar if (!sched_domains_numa_distance) 1548f2cb1360SIngo Molnar return; 1549f2cb1360SIngo Molnar 1550051f3ca0SSuravee Suthikulpanit /* Includes NUMA identity node at level 0. */ 1551051f3ca0SSuravee Suthikulpanit sched_domains_numa_distance[level++] = curr_distance; 1552051f3ca0SSuravee Suthikulpanit sched_domains_numa_levels = level; 1553051f3ca0SSuravee Suthikulpanit 1554f2cb1360SIngo Molnar /* 1555f2cb1360SIngo Molnar * O(nr_nodes^2) deduplicating selection sort -- in order to find the 1556f2cb1360SIngo Molnar * unique distances in the node_distance() table. 1557f2cb1360SIngo Molnar * 1558f2cb1360SIngo Molnar * Assumes node_distance(0,j) includes all distances in 1559f2cb1360SIngo Molnar * node_distance(i,j) in order to avoid cubic time. 1560f2cb1360SIngo Molnar */ 1561f2cb1360SIngo Molnar next_distance = curr_distance; 1562f2cb1360SIngo Molnar for (i = 0; i < nr_node_ids; i++) { 1563f2cb1360SIngo Molnar for (j = 0; j < nr_node_ids; j++) { 1564f2cb1360SIngo Molnar for (k = 0; k < nr_node_ids; k++) { 1565f2cb1360SIngo Molnar int distance = node_distance(i, k); 1566f2cb1360SIngo Molnar 1567f2cb1360SIngo Molnar if (distance > curr_distance && 1568f2cb1360SIngo Molnar (distance < next_distance || 1569f2cb1360SIngo Molnar next_distance == curr_distance)) 1570f2cb1360SIngo Molnar next_distance = distance; 1571f2cb1360SIngo Molnar 1572f2cb1360SIngo Molnar /* 1573f2cb1360SIngo Molnar * While not a strong assumption it would be nice to know 1574f2cb1360SIngo Molnar * about cases where if node A is connected to B, B is not 1575f2cb1360SIngo Molnar * equally connected to A. 1576f2cb1360SIngo Molnar */ 1577f2cb1360SIngo Molnar if (sched_debug() && node_distance(k, i) != distance) 1578f2cb1360SIngo Molnar sched_numa_warn("Node-distance not symmetric"); 1579f2cb1360SIngo Molnar 1580f2cb1360SIngo Molnar if (sched_debug() && i && !find_numa_distance(distance)) 1581f2cb1360SIngo Molnar sched_numa_warn("Node-0 not representative"); 1582f2cb1360SIngo Molnar } 1583f2cb1360SIngo Molnar if (next_distance != curr_distance) { 1584f2cb1360SIngo Molnar sched_domains_numa_distance[level++] = next_distance; 1585f2cb1360SIngo Molnar sched_domains_numa_levels = level; 1586f2cb1360SIngo Molnar curr_distance = next_distance; 1587f2cb1360SIngo Molnar } else break; 1588f2cb1360SIngo Molnar } 1589f2cb1360SIngo Molnar 1590f2cb1360SIngo Molnar /* 1591f2cb1360SIngo Molnar * In case of sched_debug() we verify the above assumption. 1592f2cb1360SIngo Molnar */ 1593f2cb1360SIngo Molnar if (!sched_debug()) 1594f2cb1360SIngo Molnar break; 1595f2cb1360SIngo Molnar } 1596f2cb1360SIngo Molnar 1597f2cb1360SIngo Molnar /* 1598051f3ca0SSuravee Suthikulpanit * 'level' contains the number of unique distances 1599f2cb1360SIngo Molnar * 1600f2cb1360SIngo Molnar * The sched_domains_numa_distance[] array includes the actual distance 1601f2cb1360SIngo Molnar * numbers. 1602f2cb1360SIngo Molnar */ 1603f2cb1360SIngo Molnar 1604f2cb1360SIngo Molnar /* 1605f2cb1360SIngo Molnar * Here, we should temporarily reset sched_domains_numa_levels to 0. 1606f2cb1360SIngo Molnar * If it fails to allocate memory for array sched_domains_numa_masks[][], 1607f2cb1360SIngo Molnar * the array will contain less then 'level' members. This could be 1608f2cb1360SIngo Molnar * dangerous when we use it to iterate array sched_domains_numa_masks[][] 1609f2cb1360SIngo Molnar * in other functions. 1610f2cb1360SIngo Molnar * 1611f2cb1360SIngo Molnar * We reset it to 'level' at the end of this function. 1612f2cb1360SIngo Molnar */ 1613f2cb1360SIngo Molnar sched_domains_numa_levels = 0; 1614f2cb1360SIngo Molnar 1615f2cb1360SIngo Molnar sched_domains_numa_masks = kzalloc(sizeof(void *) * level, GFP_KERNEL); 1616f2cb1360SIngo Molnar if (!sched_domains_numa_masks) 1617f2cb1360SIngo Molnar return; 1618f2cb1360SIngo Molnar 1619f2cb1360SIngo Molnar /* 1620f2cb1360SIngo Molnar * Now for each level, construct a mask per node which contains all 1621f2cb1360SIngo Molnar * CPUs of nodes that are that many hops away from us. 1622f2cb1360SIngo Molnar */ 1623f2cb1360SIngo Molnar for (i = 0; i < level; i++) { 1624f2cb1360SIngo Molnar sched_domains_numa_masks[i] = 1625f2cb1360SIngo Molnar kzalloc(nr_node_ids * sizeof(void *), GFP_KERNEL); 1626f2cb1360SIngo Molnar if (!sched_domains_numa_masks[i]) 1627f2cb1360SIngo Molnar return; 1628f2cb1360SIngo Molnar 1629f2cb1360SIngo Molnar for (j = 0; j < nr_node_ids; j++) { 1630f2cb1360SIngo Molnar struct cpumask *mask = kzalloc(cpumask_size(), GFP_KERNEL); 1631f2cb1360SIngo Molnar if (!mask) 1632f2cb1360SIngo Molnar return; 1633f2cb1360SIngo Molnar 1634f2cb1360SIngo Molnar sched_domains_numa_masks[i][j] = mask; 1635f2cb1360SIngo Molnar 1636f2cb1360SIngo Molnar for_each_node(k) { 1637f2cb1360SIngo Molnar if (node_distance(j, k) > sched_domains_numa_distance[i]) 1638f2cb1360SIngo Molnar continue; 1639f2cb1360SIngo Molnar 1640f2cb1360SIngo Molnar cpumask_or(mask, mask, cpumask_of_node(k)); 1641f2cb1360SIngo Molnar } 1642f2cb1360SIngo Molnar } 1643f2cb1360SIngo Molnar } 1644f2cb1360SIngo Molnar 1645f2cb1360SIngo Molnar /* Compute default topology size */ 1646f2cb1360SIngo Molnar for (i = 0; sched_domain_topology[i].mask; i++); 1647f2cb1360SIngo Molnar 1648f2cb1360SIngo Molnar tl = kzalloc((i + level + 1) * 1649f2cb1360SIngo Molnar sizeof(struct sched_domain_topology_level), GFP_KERNEL); 1650f2cb1360SIngo Molnar if (!tl) 1651f2cb1360SIngo Molnar return; 1652f2cb1360SIngo Molnar 1653f2cb1360SIngo Molnar /* 1654f2cb1360SIngo Molnar * Copy the default topology bits.. 1655f2cb1360SIngo Molnar */ 1656f2cb1360SIngo Molnar for (i = 0; sched_domain_topology[i].mask; i++) 1657f2cb1360SIngo Molnar tl[i] = sched_domain_topology[i]; 1658f2cb1360SIngo Molnar 1659f2cb1360SIngo Molnar /* 1660051f3ca0SSuravee Suthikulpanit * Add the NUMA identity distance, aka single NODE. 1661051f3ca0SSuravee Suthikulpanit */ 1662051f3ca0SSuravee Suthikulpanit tl[i++] = (struct sched_domain_topology_level){ 1663051f3ca0SSuravee Suthikulpanit .mask = sd_numa_mask, 1664051f3ca0SSuravee Suthikulpanit .numa_level = 0, 1665051f3ca0SSuravee Suthikulpanit SD_INIT_NAME(NODE) 1666051f3ca0SSuravee Suthikulpanit }; 1667051f3ca0SSuravee Suthikulpanit 1668051f3ca0SSuravee Suthikulpanit /* 1669f2cb1360SIngo Molnar * .. and append 'j' levels of NUMA goodness. 1670f2cb1360SIngo Molnar */ 1671051f3ca0SSuravee Suthikulpanit for (j = 1; j < level; i++, j++) { 1672f2cb1360SIngo Molnar tl[i] = (struct sched_domain_topology_level){ 1673f2cb1360SIngo Molnar .mask = sd_numa_mask, 1674f2cb1360SIngo Molnar .sd_flags = cpu_numa_flags, 1675f2cb1360SIngo Molnar .flags = SDTL_OVERLAP, 1676f2cb1360SIngo Molnar .numa_level = j, 1677f2cb1360SIngo Molnar SD_INIT_NAME(NUMA) 1678f2cb1360SIngo Molnar }; 1679f2cb1360SIngo Molnar } 1680f2cb1360SIngo Molnar 1681f2cb1360SIngo Molnar sched_domain_topology = tl; 1682f2cb1360SIngo Molnar 1683f2cb1360SIngo Molnar sched_domains_numa_levels = level; 1684f2cb1360SIngo Molnar sched_max_numa_distance = sched_domains_numa_distance[level - 1]; 1685f2cb1360SIngo Molnar 1686f2cb1360SIngo Molnar init_numa_topology_type(); 1687f2cb1360SIngo Molnar } 1688f2cb1360SIngo Molnar 1689f2cb1360SIngo Molnar void sched_domains_numa_masks_set(unsigned int cpu) 1690f2cb1360SIngo Molnar { 1691f2cb1360SIngo Molnar int node = cpu_to_node(cpu); 1692f2cb1360SIngo Molnar int i, j; 1693f2cb1360SIngo Molnar 1694f2cb1360SIngo Molnar for (i = 0; i < sched_domains_numa_levels; i++) { 1695f2cb1360SIngo Molnar for (j = 0; j < nr_node_ids; j++) { 1696f2cb1360SIngo Molnar if (node_distance(j, node) <= sched_domains_numa_distance[i]) 1697f2cb1360SIngo Molnar cpumask_set_cpu(cpu, sched_domains_numa_masks[i][j]); 1698f2cb1360SIngo Molnar } 1699f2cb1360SIngo Molnar } 1700f2cb1360SIngo Molnar } 1701f2cb1360SIngo Molnar 1702f2cb1360SIngo Molnar void sched_domains_numa_masks_clear(unsigned int cpu) 1703f2cb1360SIngo Molnar { 1704f2cb1360SIngo Molnar int i, j; 1705f2cb1360SIngo Molnar 1706f2cb1360SIngo Molnar for (i = 0; i < sched_domains_numa_levels; i++) { 1707f2cb1360SIngo Molnar for (j = 0; j < nr_node_ids; j++) 1708f2cb1360SIngo Molnar cpumask_clear_cpu(cpu, sched_domains_numa_masks[i][j]); 1709f2cb1360SIngo Molnar } 1710f2cb1360SIngo Molnar } 1711f2cb1360SIngo Molnar 1712e0e8d491SWanpeng Li /* 1713e0e8d491SWanpeng Li * sched_numa_find_closest() - given the NUMA topology, find the cpu 1714e0e8d491SWanpeng Li * closest to @cpu from @cpumask. 1715e0e8d491SWanpeng Li * cpumask: cpumask to find a cpu from 1716e0e8d491SWanpeng Li * cpu: cpu to be close to 1717e0e8d491SWanpeng Li * 1718e0e8d491SWanpeng Li * returns: cpu, or nr_cpu_ids when nothing found. 1719e0e8d491SWanpeng Li */ 1720e0e8d491SWanpeng Li int sched_numa_find_closest(const struct cpumask *cpus, int cpu) 1721e0e8d491SWanpeng Li { 1722e0e8d491SWanpeng Li int i, j = cpu_to_node(cpu); 1723e0e8d491SWanpeng Li 1724e0e8d491SWanpeng Li for (i = 0; i < sched_domains_numa_levels; i++) { 1725e0e8d491SWanpeng Li cpu = cpumask_any_and(cpus, sched_domains_numa_masks[i][j]); 1726e0e8d491SWanpeng Li if (cpu < nr_cpu_ids) 1727e0e8d491SWanpeng Li return cpu; 1728e0e8d491SWanpeng Li } 1729e0e8d491SWanpeng Li return nr_cpu_ids; 1730e0e8d491SWanpeng Li } 1731e0e8d491SWanpeng Li 1732f2cb1360SIngo Molnar #endif /* CONFIG_NUMA */ 1733f2cb1360SIngo Molnar 1734f2cb1360SIngo Molnar static int __sdt_alloc(const struct cpumask *cpu_map) 1735f2cb1360SIngo Molnar { 1736f2cb1360SIngo Molnar struct sched_domain_topology_level *tl; 1737f2cb1360SIngo Molnar int j; 1738f2cb1360SIngo Molnar 1739f2cb1360SIngo Molnar for_each_sd_topology(tl) { 1740f2cb1360SIngo Molnar struct sd_data *sdd = &tl->data; 1741f2cb1360SIngo Molnar 1742f2cb1360SIngo Molnar sdd->sd = alloc_percpu(struct sched_domain *); 1743f2cb1360SIngo Molnar if (!sdd->sd) 1744f2cb1360SIngo Molnar return -ENOMEM; 1745f2cb1360SIngo Molnar 1746f2cb1360SIngo Molnar sdd->sds = alloc_percpu(struct sched_domain_shared *); 1747f2cb1360SIngo Molnar if (!sdd->sds) 1748f2cb1360SIngo Molnar return -ENOMEM; 1749f2cb1360SIngo Molnar 1750f2cb1360SIngo Molnar sdd->sg = alloc_percpu(struct sched_group *); 1751f2cb1360SIngo Molnar if (!sdd->sg) 1752f2cb1360SIngo Molnar return -ENOMEM; 1753f2cb1360SIngo Molnar 1754f2cb1360SIngo Molnar sdd->sgc = alloc_percpu(struct sched_group_capacity *); 1755f2cb1360SIngo Molnar if (!sdd->sgc) 1756f2cb1360SIngo Molnar return -ENOMEM; 1757f2cb1360SIngo Molnar 1758f2cb1360SIngo Molnar for_each_cpu(j, cpu_map) { 1759f2cb1360SIngo Molnar struct sched_domain *sd; 1760f2cb1360SIngo Molnar struct sched_domain_shared *sds; 1761f2cb1360SIngo Molnar struct sched_group *sg; 1762f2cb1360SIngo Molnar struct sched_group_capacity *sgc; 1763f2cb1360SIngo Molnar 1764f2cb1360SIngo Molnar sd = kzalloc_node(sizeof(struct sched_domain) + cpumask_size(), 1765f2cb1360SIngo Molnar GFP_KERNEL, cpu_to_node(j)); 1766f2cb1360SIngo Molnar if (!sd) 1767f2cb1360SIngo Molnar return -ENOMEM; 1768f2cb1360SIngo Molnar 1769f2cb1360SIngo Molnar *per_cpu_ptr(sdd->sd, j) = sd; 1770f2cb1360SIngo Molnar 1771f2cb1360SIngo Molnar sds = kzalloc_node(sizeof(struct sched_domain_shared), 1772f2cb1360SIngo Molnar GFP_KERNEL, cpu_to_node(j)); 1773f2cb1360SIngo Molnar if (!sds) 1774f2cb1360SIngo Molnar return -ENOMEM; 1775f2cb1360SIngo Molnar 1776f2cb1360SIngo Molnar *per_cpu_ptr(sdd->sds, j) = sds; 1777f2cb1360SIngo Molnar 1778f2cb1360SIngo Molnar sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(), 1779f2cb1360SIngo Molnar GFP_KERNEL, cpu_to_node(j)); 1780f2cb1360SIngo Molnar if (!sg) 1781f2cb1360SIngo Molnar return -ENOMEM; 1782f2cb1360SIngo Molnar 1783f2cb1360SIngo Molnar sg->next = sg; 1784f2cb1360SIngo Molnar 1785f2cb1360SIngo Molnar *per_cpu_ptr(sdd->sg, j) = sg; 1786f2cb1360SIngo Molnar 1787f2cb1360SIngo Molnar sgc = kzalloc_node(sizeof(struct sched_group_capacity) + cpumask_size(), 1788f2cb1360SIngo Molnar GFP_KERNEL, cpu_to_node(j)); 1789f2cb1360SIngo Molnar if (!sgc) 1790f2cb1360SIngo Molnar return -ENOMEM; 1791f2cb1360SIngo Molnar 1792005f874dSPeter Zijlstra #ifdef CONFIG_SCHED_DEBUG 1793005f874dSPeter Zijlstra sgc->id = j; 1794005f874dSPeter Zijlstra #endif 1795005f874dSPeter Zijlstra 1796f2cb1360SIngo Molnar *per_cpu_ptr(sdd->sgc, j) = sgc; 1797f2cb1360SIngo Molnar } 1798f2cb1360SIngo Molnar } 1799f2cb1360SIngo Molnar 1800f2cb1360SIngo Molnar return 0; 1801f2cb1360SIngo Molnar } 1802f2cb1360SIngo Molnar 1803f2cb1360SIngo Molnar static void __sdt_free(const struct cpumask *cpu_map) 1804f2cb1360SIngo Molnar { 1805f2cb1360SIngo Molnar struct sched_domain_topology_level *tl; 1806f2cb1360SIngo Molnar int j; 1807f2cb1360SIngo Molnar 1808f2cb1360SIngo Molnar for_each_sd_topology(tl) { 1809f2cb1360SIngo Molnar struct sd_data *sdd = &tl->data; 1810f2cb1360SIngo Molnar 1811f2cb1360SIngo Molnar for_each_cpu(j, cpu_map) { 1812f2cb1360SIngo Molnar struct sched_domain *sd; 1813f2cb1360SIngo Molnar 1814f2cb1360SIngo Molnar if (sdd->sd) { 1815f2cb1360SIngo Molnar sd = *per_cpu_ptr(sdd->sd, j); 1816f2cb1360SIngo Molnar if (sd && (sd->flags & SD_OVERLAP)) 1817f2cb1360SIngo Molnar free_sched_groups(sd->groups, 0); 1818f2cb1360SIngo Molnar kfree(*per_cpu_ptr(sdd->sd, j)); 1819f2cb1360SIngo Molnar } 1820f2cb1360SIngo Molnar 1821f2cb1360SIngo Molnar if (sdd->sds) 1822f2cb1360SIngo Molnar kfree(*per_cpu_ptr(sdd->sds, j)); 1823f2cb1360SIngo Molnar if (sdd->sg) 1824f2cb1360SIngo Molnar kfree(*per_cpu_ptr(sdd->sg, j)); 1825f2cb1360SIngo Molnar if (sdd->sgc) 1826f2cb1360SIngo Molnar kfree(*per_cpu_ptr(sdd->sgc, j)); 1827f2cb1360SIngo Molnar } 1828f2cb1360SIngo Molnar free_percpu(sdd->sd); 1829f2cb1360SIngo Molnar sdd->sd = NULL; 1830f2cb1360SIngo Molnar free_percpu(sdd->sds); 1831f2cb1360SIngo Molnar sdd->sds = NULL; 1832f2cb1360SIngo Molnar free_percpu(sdd->sg); 1833f2cb1360SIngo Molnar sdd->sg = NULL; 1834f2cb1360SIngo Molnar free_percpu(sdd->sgc); 1835f2cb1360SIngo Molnar sdd->sgc = NULL; 1836f2cb1360SIngo Molnar } 1837f2cb1360SIngo Molnar } 1838f2cb1360SIngo Molnar 1839181a80d1SViresh Kumar static struct sched_domain *build_sched_domain(struct sched_domain_topology_level *tl, 1840f2cb1360SIngo Molnar const struct cpumask *cpu_map, struct sched_domain_attr *attr, 184105484e09SMorten Rasmussen struct sched_domain *child, int dflags, int cpu) 1842f2cb1360SIngo Molnar { 184305484e09SMorten Rasmussen struct sched_domain *sd = sd_init(tl, cpu_map, child, dflags, cpu); 1844f2cb1360SIngo Molnar 1845f2cb1360SIngo Molnar if (child) { 1846f2cb1360SIngo Molnar sd->level = child->level + 1; 1847f2cb1360SIngo Molnar sched_domain_level_max = max(sched_domain_level_max, sd->level); 1848f2cb1360SIngo Molnar child->parent = sd; 1849f2cb1360SIngo Molnar 1850f2cb1360SIngo Molnar if (!cpumask_subset(sched_domain_span(child), 1851f2cb1360SIngo Molnar sched_domain_span(sd))) { 1852f2cb1360SIngo Molnar pr_err("BUG: arch topology borken\n"); 1853f2cb1360SIngo Molnar #ifdef CONFIG_SCHED_DEBUG 1854f2cb1360SIngo Molnar pr_err(" the %s domain not a subset of the %s domain\n", 1855f2cb1360SIngo Molnar child->name, sd->name); 1856f2cb1360SIngo Molnar #endif 185797fb7a0aSIngo Molnar /* Fixup, ensure @sd has at least @child CPUs. */ 1858f2cb1360SIngo Molnar cpumask_or(sched_domain_span(sd), 1859f2cb1360SIngo Molnar sched_domain_span(sd), 1860f2cb1360SIngo Molnar sched_domain_span(child)); 1861f2cb1360SIngo Molnar } 1862f2cb1360SIngo Molnar 1863f2cb1360SIngo Molnar } 1864f2cb1360SIngo Molnar set_domain_attribute(sd, attr); 1865f2cb1360SIngo Molnar 1866f2cb1360SIngo Molnar return sd; 1867f2cb1360SIngo Molnar } 1868f2cb1360SIngo Molnar 1869f2cb1360SIngo Molnar /* 1870ccf74128SValentin Schneider * Ensure topology masks are sane, i.e. there are no conflicts (overlaps) for 1871ccf74128SValentin Schneider * any two given CPUs at this (non-NUMA) topology level. 1872ccf74128SValentin Schneider */ 1873ccf74128SValentin Schneider static bool topology_span_sane(struct sched_domain_topology_level *tl, 1874ccf74128SValentin Schneider const struct cpumask *cpu_map, int cpu) 1875ccf74128SValentin Schneider { 1876ccf74128SValentin Schneider int i; 1877ccf74128SValentin Schneider 1878ccf74128SValentin Schneider /* NUMA levels are allowed to overlap */ 1879ccf74128SValentin Schneider if (tl->flags & SDTL_OVERLAP) 1880ccf74128SValentin Schneider return true; 1881ccf74128SValentin Schneider 1882ccf74128SValentin Schneider /* 1883ccf74128SValentin Schneider * Non-NUMA levels cannot partially overlap - they must be either 1884ccf74128SValentin Schneider * completely equal or completely disjoint. Otherwise we can end up 1885ccf74128SValentin Schneider * breaking the sched_group lists - i.e. a later get_group() pass 1886ccf74128SValentin Schneider * breaks the linking done for an earlier span. 1887ccf74128SValentin Schneider */ 1888ccf74128SValentin Schneider for_each_cpu(i, cpu_map) { 1889ccf74128SValentin Schneider if (i == cpu) 1890ccf74128SValentin Schneider continue; 1891ccf74128SValentin Schneider /* 1892ccf74128SValentin Schneider * We should 'and' all those masks with 'cpu_map' to exactly 1893ccf74128SValentin Schneider * match the topology we're about to build, but that can only 1894ccf74128SValentin Schneider * remove CPUs, which only lessens our ability to detect 1895ccf74128SValentin Schneider * overlaps 1896ccf74128SValentin Schneider */ 1897ccf74128SValentin Schneider if (!cpumask_equal(tl->mask(cpu), tl->mask(i)) && 1898ccf74128SValentin Schneider cpumask_intersects(tl->mask(cpu), tl->mask(i))) 1899ccf74128SValentin Schneider return false; 1900ccf74128SValentin Schneider } 1901ccf74128SValentin Schneider 1902ccf74128SValentin Schneider return true; 1903ccf74128SValentin Schneider } 1904ccf74128SValentin Schneider 1905ccf74128SValentin Schneider /* 190605484e09SMorten Rasmussen * Find the sched_domain_topology_level where all CPU capacities are visible 190705484e09SMorten Rasmussen * for all CPUs. 190805484e09SMorten Rasmussen */ 190905484e09SMorten Rasmussen static struct sched_domain_topology_level 191005484e09SMorten Rasmussen *asym_cpu_capacity_level(const struct cpumask *cpu_map) 191105484e09SMorten Rasmussen { 191205484e09SMorten Rasmussen int i, j, asym_level = 0; 191305484e09SMorten Rasmussen bool asym = false; 191405484e09SMorten Rasmussen struct sched_domain_topology_level *tl, *asym_tl = NULL; 191505484e09SMorten Rasmussen unsigned long cap; 191605484e09SMorten Rasmussen 191705484e09SMorten Rasmussen /* Is there any asymmetry? */ 19188ec59c0fSVincent Guittot cap = arch_scale_cpu_capacity(cpumask_first(cpu_map)); 191905484e09SMorten Rasmussen 192005484e09SMorten Rasmussen for_each_cpu(i, cpu_map) { 19218ec59c0fSVincent Guittot if (arch_scale_cpu_capacity(i) != cap) { 192205484e09SMorten Rasmussen asym = true; 192305484e09SMorten Rasmussen break; 192405484e09SMorten Rasmussen } 192505484e09SMorten Rasmussen } 192605484e09SMorten Rasmussen 192705484e09SMorten Rasmussen if (!asym) 192805484e09SMorten Rasmussen return NULL; 192905484e09SMorten Rasmussen 193005484e09SMorten Rasmussen /* 193105484e09SMorten Rasmussen * Examine topology from all CPU's point of views to detect the lowest 193205484e09SMorten Rasmussen * sched_domain_topology_level where a highest capacity CPU is visible 193305484e09SMorten Rasmussen * to everyone. 193405484e09SMorten Rasmussen */ 193505484e09SMorten Rasmussen for_each_cpu(i, cpu_map) { 19368ec59c0fSVincent Guittot unsigned long max_capacity = arch_scale_cpu_capacity(i); 193705484e09SMorten Rasmussen int tl_id = 0; 193805484e09SMorten Rasmussen 193905484e09SMorten Rasmussen for_each_sd_topology(tl) { 194005484e09SMorten Rasmussen if (tl_id < asym_level) 194105484e09SMorten Rasmussen goto next_level; 194205484e09SMorten Rasmussen 194305484e09SMorten Rasmussen for_each_cpu_and(j, tl->mask(i), cpu_map) { 194405484e09SMorten Rasmussen unsigned long capacity; 194505484e09SMorten Rasmussen 19468ec59c0fSVincent Guittot capacity = arch_scale_cpu_capacity(j); 194705484e09SMorten Rasmussen 194805484e09SMorten Rasmussen if (capacity <= max_capacity) 194905484e09SMorten Rasmussen continue; 195005484e09SMorten Rasmussen 195105484e09SMorten Rasmussen max_capacity = capacity; 195205484e09SMorten Rasmussen asym_level = tl_id; 195305484e09SMorten Rasmussen asym_tl = tl; 195405484e09SMorten Rasmussen } 195505484e09SMorten Rasmussen next_level: 195605484e09SMorten Rasmussen tl_id++; 195705484e09SMorten Rasmussen } 195805484e09SMorten Rasmussen } 195905484e09SMorten Rasmussen 196005484e09SMorten Rasmussen return asym_tl; 196105484e09SMorten Rasmussen } 196205484e09SMorten Rasmussen 196305484e09SMorten Rasmussen 196405484e09SMorten Rasmussen /* 1965f2cb1360SIngo Molnar * Build sched domains for a given set of CPUs and attach the sched domains 1966f2cb1360SIngo Molnar * to the individual CPUs 1967f2cb1360SIngo Molnar */ 1968f2cb1360SIngo Molnar static int 1969f2cb1360SIngo Molnar build_sched_domains(const struct cpumask *cpu_map, struct sched_domain_attr *attr) 1970f2cb1360SIngo Molnar { 1971cd1cb335SValentin Schneider enum s_alloc alloc_state = sa_none; 1972f2cb1360SIngo Molnar struct sched_domain *sd; 1973f2cb1360SIngo Molnar struct s_data d; 1974f2cb1360SIngo Molnar struct rq *rq = NULL; 1975f2cb1360SIngo Molnar int i, ret = -ENOMEM; 197605484e09SMorten Rasmussen struct sched_domain_topology_level *tl_asym; 1977df054e84SMorten Rasmussen bool has_asym = false; 1978f2cb1360SIngo Molnar 1979cd1cb335SValentin Schneider if (WARN_ON(cpumask_empty(cpu_map))) 1980cd1cb335SValentin Schneider goto error; 1981cd1cb335SValentin Schneider 1982f2cb1360SIngo Molnar alloc_state = __visit_domain_allocation_hell(&d, cpu_map); 1983f2cb1360SIngo Molnar if (alloc_state != sa_rootdomain) 1984f2cb1360SIngo Molnar goto error; 1985f2cb1360SIngo Molnar 198605484e09SMorten Rasmussen tl_asym = asym_cpu_capacity_level(cpu_map); 198705484e09SMorten Rasmussen 1988f2cb1360SIngo Molnar /* Set up domains for CPUs specified by the cpu_map: */ 1989f2cb1360SIngo Molnar for_each_cpu(i, cpu_map) { 1990f2cb1360SIngo Molnar struct sched_domain_topology_level *tl; 1991*c200191dSValentin Schneider int dflags = 0; 1992f2cb1360SIngo Molnar 1993f2cb1360SIngo Molnar sd = NULL; 1994f2cb1360SIngo Molnar for_each_sd_topology(tl) { 1995df054e84SMorten Rasmussen if (tl == tl_asym) { 199605484e09SMorten Rasmussen dflags |= SD_ASYM_CPUCAPACITY; 1997df054e84SMorten Rasmussen has_asym = true; 1998df054e84SMorten Rasmussen } 199905484e09SMorten Rasmussen 2000ccf74128SValentin Schneider if (WARN_ON(!topology_span_sane(tl, cpu_map, i))) 2001ccf74128SValentin Schneider goto error; 2002ccf74128SValentin Schneider 200305484e09SMorten Rasmussen sd = build_sched_domain(tl, cpu_map, attr, sd, dflags, i); 200405484e09SMorten Rasmussen 2005f2cb1360SIngo Molnar if (tl == sched_domain_topology) 2006f2cb1360SIngo Molnar *per_cpu_ptr(d.sd, i) = sd; 2007af85596cSPeter Zijlstra if (tl->flags & SDTL_OVERLAP) 2008f2cb1360SIngo Molnar sd->flags |= SD_OVERLAP; 2009f2cb1360SIngo Molnar if (cpumask_equal(cpu_map, sched_domain_span(sd))) 2010f2cb1360SIngo Molnar break; 2011f2cb1360SIngo Molnar } 2012f2cb1360SIngo Molnar } 2013f2cb1360SIngo Molnar 2014f2cb1360SIngo Molnar /* Build the groups for the domains */ 2015f2cb1360SIngo Molnar for_each_cpu(i, cpu_map) { 2016f2cb1360SIngo Molnar for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) { 2017f2cb1360SIngo Molnar sd->span_weight = cpumask_weight(sched_domain_span(sd)); 2018f2cb1360SIngo Molnar if (sd->flags & SD_OVERLAP) { 2019f2cb1360SIngo Molnar if (build_overlap_sched_groups(sd, i)) 2020f2cb1360SIngo Molnar goto error; 2021f2cb1360SIngo Molnar } else { 2022f2cb1360SIngo Molnar if (build_sched_groups(sd, i)) 2023f2cb1360SIngo Molnar goto error; 2024f2cb1360SIngo Molnar } 2025f2cb1360SIngo Molnar } 2026f2cb1360SIngo Molnar } 2027f2cb1360SIngo Molnar 2028f2cb1360SIngo Molnar /* Calculate CPU capacity for physical packages and nodes */ 2029f2cb1360SIngo Molnar for (i = nr_cpumask_bits-1; i >= 0; i--) { 2030f2cb1360SIngo Molnar if (!cpumask_test_cpu(i, cpu_map)) 2031f2cb1360SIngo Molnar continue; 2032f2cb1360SIngo Molnar 2033f2cb1360SIngo Molnar for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) { 2034f2cb1360SIngo Molnar claim_allocations(i, sd); 2035f2cb1360SIngo Molnar init_sched_groups_capacity(i, sd); 2036f2cb1360SIngo Molnar } 2037f2cb1360SIngo Molnar } 2038f2cb1360SIngo Molnar 2039f2cb1360SIngo Molnar /* Attach the domains */ 2040f2cb1360SIngo Molnar rcu_read_lock(); 2041f2cb1360SIngo Molnar for_each_cpu(i, cpu_map) { 2042f2cb1360SIngo Molnar rq = cpu_rq(i); 2043f2cb1360SIngo Molnar sd = *per_cpu_ptr(d.sd, i); 2044f2cb1360SIngo Molnar 2045f2cb1360SIngo Molnar /* Use READ_ONCE()/WRITE_ONCE() to avoid load/store tearing: */ 2046f2cb1360SIngo Molnar if (rq->cpu_capacity_orig > READ_ONCE(d.rd->max_cpu_capacity)) 2047f2cb1360SIngo Molnar WRITE_ONCE(d.rd->max_cpu_capacity, rq->cpu_capacity_orig); 2048f2cb1360SIngo Molnar 2049f2cb1360SIngo Molnar cpu_attach_domain(sd, d.rd, i); 2050f2cb1360SIngo Molnar } 2051f2cb1360SIngo Molnar rcu_read_unlock(); 2052f2cb1360SIngo Molnar 2053df054e84SMorten Rasmussen if (has_asym) 2054e284df70SValentin Schneider static_branch_inc_cpuslocked(&sched_asym_cpucapacity); 2055df054e84SMorten Rasmussen 2056f2cb1360SIngo Molnar if (rq && sched_debug_enabled) { 2057bf5015a5SJuri Lelli pr_info("root domain span: %*pbl (max cpu_capacity = %lu)\n", 2058f2cb1360SIngo Molnar cpumask_pr_args(cpu_map), rq->rd->max_cpu_capacity); 2059f2cb1360SIngo Molnar } 2060f2cb1360SIngo Molnar 2061f2cb1360SIngo Molnar ret = 0; 2062f2cb1360SIngo Molnar error: 2063f2cb1360SIngo Molnar __free_domain_allocs(&d, alloc_state, cpu_map); 206497fb7a0aSIngo Molnar 2065f2cb1360SIngo Molnar return ret; 2066f2cb1360SIngo Molnar } 2067f2cb1360SIngo Molnar 2068f2cb1360SIngo Molnar /* Current sched domains: */ 2069f2cb1360SIngo Molnar static cpumask_var_t *doms_cur; 2070f2cb1360SIngo Molnar 2071f2cb1360SIngo Molnar /* Number of sched domains in 'doms_cur': */ 2072f2cb1360SIngo Molnar static int ndoms_cur; 2073f2cb1360SIngo Molnar 2074f2cb1360SIngo Molnar /* Attribues of custom domains in 'doms_cur' */ 2075f2cb1360SIngo Molnar static struct sched_domain_attr *dattr_cur; 2076f2cb1360SIngo Molnar 2077f2cb1360SIngo Molnar /* 2078f2cb1360SIngo Molnar * Special case: If a kmalloc() of a doms_cur partition (array of 2079f2cb1360SIngo Molnar * cpumask) fails, then fallback to a single sched domain, 2080f2cb1360SIngo Molnar * as determined by the single cpumask fallback_doms. 2081f2cb1360SIngo Molnar */ 20828d5dc512SPeter Zijlstra static cpumask_var_t fallback_doms; 2083f2cb1360SIngo Molnar 2084f2cb1360SIngo Molnar /* 2085f2cb1360SIngo Molnar * arch_update_cpu_topology lets virtualized architectures update the 2086f2cb1360SIngo Molnar * CPU core maps. It is supposed to return 1 if the topology changed 2087f2cb1360SIngo Molnar * or 0 if it stayed the same. 2088f2cb1360SIngo Molnar */ 2089f2cb1360SIngo Molnar int __weak arch_update_cpu_topology(void) 2090f2cb1360SIngo Molnar { 2091f2cb1360SIngo Molnar return 0; 2092f2cb1360SIngo Molnar } 2093f2cb1360SIngo Molnar 2094f2cb1360SIngo Molnar cpumask_var_t *alloc_sched_domains(unsigned int ndoms) 2095f2cb1360SIngo Molnar { 2096f2cb1360SIngo Molnar int i; 2097f2cb1360SIngo Molnar cpumask_var_t *doms; 2098f2cb1360SIngo Molnar 20996da2ec56SKees Cook doms = kmalloc_array(ndoms, sizeof(*doms), GFP_KERNEL); 2100f2cb1360SIngo Molnar if (!doms) 2101f2cb1360SIngo Molnar return NULL; 2102f2cb1360SIngo Molnar for (i = 0; i < ndoms; i++) { 2103f2cb1360SIngo Molnar if (!alloc_cpumask_var(&doms[i], GFP_KERNEL)) { 2104f2cb1360SIngo Molnar free_sched_domains(doms, i); 2105f2cb1360SIngo Molnar return NULL; 2106f2cb1360SIngo Molnar } 2107f2cb1360SIngo Molnar } 2108f2cb1360SIngo Molnar return doms; 2109f2cb1360SIngo Molnar } 2110f2cb1360SIngo Molnar 2111f2cb1360SIngo Molnar void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms) 2112f2cb1360SIngo Molnar { 2113f2cb1360SIngo Molnar unsigned int i; 2114f2cb1360SIngo Molnar for (i = 0; i < ndoms; i++) 2115f2cb1360SIngo Molnar free_cpumask_var(doms[i]); 2116f2cb1360SIngo Molnar kfree(doms); 2117f2cb1360SIngo Molnar } 2118f2cb1360SIngo Molnar 2119f2cb1360SIngo Molnar /* 2120cb0c0414SJuri Lelli * Set up scheduler domains and groups. For now this just excludes isolated 2121cb0c0414SJuri Lelli * CPUs, but could be used to exclude other special cases in the future. 2122f2cb1360SIngo Molnar */ 21238d5dc512SPeter Zijlstra int sched_init_domains(const struct cpumask *cpu_map) 2124f2cb1360SIngo Molnar { 2125f2cb1360SIngo Molnar int err; 2126f2cb1360SIngo Molnar 21278d5dc512SPeter Zijlstra zalloc_cpumask_var(&sched_domains_tmpmask, GFP_KERNEL); 21281676330eSPeter Zijlstra zalloc_cpumask_var(&sched_domains_tmpmask2, GFP_KERNEL); 21298d5dc512SPeter Zijlstra zalloc_cpumask_var(&fallback_doms, GFP_KERNEL); 21308d5dc512SPeter Zijlstra 2131f2cb1360SIngo Molnar arch_update_cpu_topology(); 2132f2cb1360SIngo Molnar ndoms_cur = 1; 2133f2cb1360SIngo Molnar doms_cur = alloc_sched_domains(ndoms_cur); 2134f2cb1360SIngo Molnar if (!doms_cur) 2135f2cb1360SIngo Molnar doms_cur = &fallback_doms; 2136edb93821SFrederic Weisbecker cpumask_and(doms_cur[0], cpu_map, housekeeping_cpumask(HK_FLAG_DOMAIN)); 2137f2cb1360SIngo Molnar err = build_sched_domains(doms_cur[0], NULL); 2138f2cb1360SIngo Molnar register_sched_domain_sysctl(); 2139f2cb1360SIngo Molnar 2140f2cb1360SIngo Molnar return err; 2141f2cb1360SIngo Molnar } 2142f2cb1360SIngo Molnar 2143f2cb1360SIngo Molnar /* 2144f2cb1360SIngo Molnar * Detach sched domains from a group of CPUs specified in cpu_map 2145f2cb1360SIngo Molnar * These CPUs will now be attached to the NULL domain 2146f2cb1360SIngo Molnar */ 2147f2cb1360SIngo Molnar static void detach_destroy_domains(const struct cpumask *cpu_map) 2148f2cb1360SIngo Molnar { 2149e284df70SValentin Schneider unsigned int cpu = cpumask_any(cpu_map); 2150f2cb1360SIngo Molnar int i; 2151f2cb1360SIngo Molnar 2152e284df70SValentin Schneider if (rcu_access_pointer(per_cpu(sd_asym_cpucapacity, cpu))) 2153e284df70SValentin Schneider static_branch_dec_cpuslocked(&sched_asym_cpucapacity); 2154e284df70SValentin Schneider 2155f2cb1360SIngo Molnar rcu_read_lock(); 2156f2cb1360SIngo Molnar for_each_cpu(i, cpu_map) 2157f2cb1360SIngo Molnar cpu_attach_domain(NULL, &def_root_domain, i); 2158f2cb1360SIngo Molnar rcu_read_unlock(); 2159f2cb1360SIngo Molnar } 2160f2cb1360SIngo Molnar 2161f2cb1360SIngo Molnar /* handle null as "default" */ 2162f2cb1360SIngo Molnar static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur, 2163f2cb1360SIngo Molnar struct sched_domain_attr *new, int idx_new) 2164f2cb1360SIngo Molnar { 2165f2cb1360SIngo Molnar struct sched_domain_attr tmp; 2166f2cb1360SIngo Molnar 2167f2cb1360SIngo Molnar /* Fast path: */ 2168f2cb1360SIngo Molnar if (!new && !cur) 2169f2cb1360SIngo Molnar return 1; 2170f2cb1360SIngo Molnar 2171f2cb1360SIngo Molnar tmp = SD_ATTR_INIT; 217297fb7a0aSIngo Molnar 2173f2cb1360SIngo Molnar return !memcmp(cur ? (cur + idx_cur) : &tmp, 2174f2cb1360SIngo Molnar new ? (new + idx_new) : &tmp, 2175f2cb1360SIngo Molnar sizeof(struct sched_domain_attr)); 2176f2cb1360SIngo Molnar } 2177f2cb1360SIngo Molnar 2178f2cb1360SIngo Molnar /* 2179f2cb1360SIngo Molnar * Partition sched domains as specified by the 'ndoms_new' 2180f2cb1360SIngo Molnar * cpumasks in the array doms_new[] of cpumasks. This compares 2181f2cb1360SIngo Molnar * doms_new[] to the current sched domain partitioning, doms_cur[]. 2182f2cb1360SIngo Molnar * It destroys each deleted domain and builds each new domain. 2183f2cb1360SIngo Molnar * 2184f2cb1360SIngo Molnar * 'doms_new' is an array of cpumask_var_t's of length 'ndoms_new'. 2185f2cb1360SIngo Molnar * The masks don't intersect (don't overlap.) We should setup one 2186f2cb1360SIngo Molnar * sched domain for each mask. CPUs not in any of the cpumasks will 2187f2cb1360SIngo Molnar * not be load balanced. If the same cpumask appears both in the 2188f2cb1360SIngo Molnar * current 'doms_cur' domains and in the new 'doms_new', we can leave 2189f2cb1360SIngo Molnar * it as it is. 2190f2cb1360SIngo Molnar * 2191f2cb1360SIngo Molnar * The passed in 'doms_new' should be allocated using 2192f2cb1360SIngo Molnar * alloc_sched_domains. This routine takes ownership of it and will 2193f2cb1360SIngo Molnar * free_sched_domains it when done with it. If the caller failed the 2194f2cb1360SIngo Molnar * alloc call, then it can pass in doms_new == NULL && ndoms_new == 1, 2195f2cb1360SIngo Molnar * and partition_sched_domains() will fallback to the single partition 2196f2cb1360SIngo Molnar * 'fallback_doms', it also forces the domains to be rebuilt. 2197f2cb1360SIngo Molnar * 2198f2cb1360SIngo Molnar * If doms_new == NULL it will be replaced with cpu_online_mask. 2199f2cb1360SIngo Molnar * ndoms_new == 0 is a special case for destroying existing domains, 2200f2cb1360SIngo Molnar * and it will not create the default domain. 2201f2cb1360SIngo Molnar * 2202c22645f4SMathieu Poirier * Call with hotplug lock and sched_domains_mutex held 2203f2cb1360SIngo Molnar */ 2204c22645f4SMathieu Poirier void partition_sched_domains_locked(int ndoms_new, cpumask_var_t doms_new[], 2205f2cb1360SIngo Molnar struct sched_domain_attr *dattr_new) 2206f2cb1360SIngo Molnar { 22071f74de87SQuentin Perret bool __maybe_unused has_eas = false; 2208f2cb1360SIngo Molnar int i, j, n; 2209f2cb1360SIngo Molnar int new_topology; 2210f2cb1360SIngo Molnar 2211c22645f4SMathieu Poirier lockdep_assert_held(&sched_domains_mutex); 2212f2cb1360SIngo Molnar 2213f2cb1360SIngo Molnar /* Always unregister in case we don't destroy any domains: */ 2214f2cb1360SIngo Molnar unregister_sched_domain_sysctl(); 2215f2cb1360SIngo Molnar 2216f2cb1360SIngo Molnar /* Let the architecture update CPU core mappings: */ 2217f2cb1360SIngo Molnar new_topology = arch_update_cpu_topology(); 2218f2cb1360SIngo Molnar 221909e0dd8eSPeter Zijlstra if (!doms_new) { 222009e0dd8eSPeter Zijlstra WARN_ON_ONCE(dattr_new); 222109e0dd8eSPeter Zijlstra n = 0; 222209e0dd8eSPeter Zijlstra doms_new = alloc_sched_domains(1); 222309e0dd8eSPeter Zijlstra if (doms_new) { 222409e0dd8eSPeter Zijlstra n = 1; 2225edb93821SFrederic Weisbecker cpumask_and(doms_new[0], cpu_active_mask, 2226edb93821SFrederic Weisbecker housekeeping_cpumask(HK_FLAG_DOMAIN)); 222709e0dd8eSPeter Zijlstra } 222809e0dd8eSPeter Zijlstra } else { 222909e0dd8eSPeter Zijlstra n = ndoms_new; 223009e0dd8eSPeter Zijlstra } 2231f2cb1360SIngo Molnar 2232f2cb1360SIngo Molnar /* Destroy deleted domains: */ 2233f2cb1360SIngo Molnar for (i = 0; i < ndoms_cur; i++) { 2234f2cb1360SIngo Molnar for (j = 0; j < n && !new_topology; j++) { 22356aa140faSQuentin Perret if (cpumask_equal(doms_cur[i], doms_new[j]) && 2236f9a25f77SMathieu Poirier dattrs_equal(dattr_cur, i, dattr_new, j)) { 2237f9a25f77SMathieu Poirier struct root_domain *rd; 2238f9a25f77SMathieu Poirier 2239f9a25f77SMathieu Poirier /* 2240f9a25f77SMathieu Poirier * This domain won't be destroyed and as such 2241f9a25f77SMathieu Poirier * its dl_bw->total_bw needs to be cleared. It 2242f9a25f77SMathieu Poirier * will be recomputed in function 2243f9a25f77SMathieu Poirier * update_tasks_root_domain(). 2244f9a25f77SMathieu Poirier */ 2245f9a25f77SMathieu Poirier rd = cpu_rq(cpumask_any(doms_cur[i]))->rd; 2246f9a25f77SMathieu Poirier dl_clear_root_domain(rd); 2247f2cb1360SIngo Molnar goto match1; 2248f2cb1360SIngo Molnar } 2249f9a25f77SMathieu Poirier } 2250f2cb1360SIngo Molnar /* No match - a current sched domain not in new doms_new[] */ 2251f2cb1360SIngo Molnar detach_destroy_domains(doms_cur[i]); 2252f2cb1360SIngo Molnar match1: 2253f2cb1360SIngo Molnar ; 2254f2cb1360SIngo Molnar } 2255f2cb1360SIngo Molnar 2256f2cb1360SIngo Molnar n = ndoms_cur; 225709e0dd8eSPeter Zijlstra if (!doms_new) { 2258f2cb1360SIngo Molnar n = 0; 2259f2cb1360SIngo Molnar doms_new = &fallback_doms; 2260edb93821SFrederic Weisbecker cpumask_and(doms_new[0], cpu_active_mask, 2261edb93821SFrederic Weisbecker housekeeping_cpumask(HK_FLAG_DOMAIN)); 2262f2cb1360SIngo Molnar } 2263f2cb1360SIngo Molnar 2264f2cb1360SIngo Molnar /* Build new domains: */ 2265f2cb1360SIngo Molnar for (i = 0; i < ndoms_new; i++) { 2266f2cb1360SIngo Molnar for (j = 0; j < n && !new_topology; j++) { 22676aa140faSQuentin Perret if (cpumask_equal(doms_new[i], doms_cur[j]) && 22686aa140faSQuentin Perret dattrs_equal(dattr_new, i, dattr_cur, j)) 2269f2cb1360SIngo Molnar goto match2; 2270f2cb1360SIngo Molnar } 2271f2cb1360SIngo Molnar /* No match - add a new doms_new */ 2272f2cb1360SIngo Molnar build_sched_domains(doms_new[i], dattr_new ? dattr_new + i : NULL); 2273f2cb1360SIngo Molnar match2: 2274f2cb1360SIngo Molnar ; 2275f2cb1360SIngo Molnar } 2276f2cb1360SIngo Molnar 2277531b5c9fSQuentin Perret #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL) 22786aa140faSQuentin Perret /* Build perf. domains: */ 22796aa140faSQuentin Perret for (i = 0; i < ndoms_new; i++) { 2280531b5c9fSQuentin Perret for (j = 0; j < n && !sched_energy_update; j++) { 22816aa140faSQuentin Perret if (cpumask_equal(doms_new[i], doms_cur[j]) && 22821f74de87SQuentin Perret cpu_rq(cpumask_first(doms_cur[j]))->rd->pd) { 22831f74de87SQuentin Perret has_eas = true; 22846aa140faSQuentin Perret goto match3; 22856aa140faSQuentin Perret } 22861f74de87SQuentin Perret } 22876aa140faSQuentin Perret /* No match - add perf. domains for a new rd */ 22881f74de87SQuentin Perret has_eas |= build_perf_domains(doms_new[i]); 22896aa140faSQuentin Perret match3: 22906aa140faSQuentin Perret ; 22916aa140faSQuentin Perret } 22921f74de87SQuentin Perret sched_energy_set(has_eas); 22936aa140faSQuentin Perret #endif 22946aa140faSQuentin Perret 2295f2cb1360SIngo Molnar /* Remember the new sched domains: */ 2296f2cb1360SIngo Molnar if (doms_cur != &fallback_doms) 2297f2cb1360SIngo Molnar free_sched_domains(doms_cur, ndoms_cur); 2298f2cb1360SIngo Molnar 2299f2cb1360SIngo Molnar kfree(dattr_cur); 2300f2cb1360SIngo Molnar doms_cur = doms_new; 2301f2cb1360SIngo Molnar dattr_cur = dattr_new; 2302f2cb1360SIngo Molnar ndoms_cur = ndoms_new; 2303f2cb1360SIngo Molnar 2304f2cb1360SIngo Molnar register_sched_domain_sysctl(); 2305c22645f4SMathieu Poirier } 2306f2cb1360SIngo Molnar 2307c22645f4SMathieu Poirier /* 2308c22645f4SMathieu Poirier * Call with hotplug lock held 2309c22645f4SMathieu Poirier */ 2310c22645f4SMathieu Poirier void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[], 2311c22645f4SMathieu Poirier struct sched_domain_attr *dattr_new) 2312c22645f4SMathieu Poirier { 2313c22645f4SMathieu Poirier mutex_lock(&sched_domains_mutex); 2314c22645f4SMathieu Poirier partition_sched_domains_locked(ndoms_new, doms_new, dattr_new); 2315f2cb1360SIngo Molnar mutex_unlock(&sched_domains_mutex); 2316f2cb1360SIngo Molnar } 2317