xref: /openbmc/linux/kernel/sched/topology.c (revision 3b87f136f8fccddf7da016ab7d04bb3cf9b180f0)
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 
28848785dfSValentin Schneider #define SD_FLAG(_name, mflags) [__##_name] = { .meta_flags = mflags, .name = #_name },
29848785dfSValentin Schneider const struct sd_flag_debug sd_flag_debug[] = {
30848785dfSValentin Schneider #include <linux/sched/sd_flags.h>
31848785dfSValentin Schneider };
32848785dfSValentin Schneider #undef SD_FLAG
33848785dfSValentin Schneider 
34f2cb1360SIngo Molnar static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
35f2cb1360SIngo Molnar 				  struct cpumask *groupmask)
36f2cb1360SIngo Molnar {
37f2cb1360SIngo Molnar 	struct sched_group *group = sd->groups;
3865c5e253SValentin Schneider 	unsigned long flags = sd->flags;
3965c5e253SValentin Schneider 	unsigned int idx;
40f2cb1360SIngo Molnar 
41f2cb1360SIngo Molnar 	cpumask_clear(groupmask);
42f2cb1360SIngo Molnar 
43005f874dSPeter Zijlstra 	printk(KERN_DEBUG "%*s domain-%d: ", level, "", level);
44005f874dSPeter Zijlstra 	printk(KERN_CONT "span=%*pbl level=%s\n",
45f2cb1360SIngo Molnar 	       cpumask_pr_args(sched_domain_span(sd)), sd->name);
46f2cb1360SIngo Molnar 
47f2cb1360SIngo Molnar 	if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) {
4897fb7a0aSIngo Molnar 		printk(KERN_ERR "ERROR: domain->span does not contain CPU%d\n", cpu);
49f2cb1360SIngo Molnar 	}
506cd0c583SYi Wang 	if (group && !cpumask_test_cpu(cpu, sched_group_span(group))) {
5197fb7a0aSIngo Molnar 		printk(KERN_ERR "ERROR: domain->groups does not contain CPU%d\n", cpu);
52f2cb1360SIngo Molnar 	}
53f2cb1360SIngo Molnar 
5465c5e253SValentin Schneider 	for_each_set_bit(idx, &flags, __SD_FLAG_CNT) {
5565c5e253SValentin Schneider 		unsigned int flag = BIT(idx);
5665c5e253SValentin Schneider 		unsigned int meta_flags = sd_flag_debug[idx].meta_flags;
5765c5e253SValentin Schneider 
5865c5e253SValentin Schneider 		if ((meta_flags & SDF_SHARED_CHILD) && sd->child &&
5965c5e253SValentin Schneider 		    !(sd->child->flags & flag))
6065c5e253SValentin Schneider 			printk(KERN_ERR "ERROR: flag %s set here but not in child\n",
6165c5e253SValentin Schneider 			       sd_flag_debug[idx].name);
6265c5e253SValentin Schneider 
6365c5e253SValentin Schneider 		if ((meta_flags & SDF_SHARED_PARENT) && sd->parent &&
6465c5e253SValentin Schneider 		    !(sd->parent->flags & flag))
6565c5e253SValentin Schneider 			printk(KERN_ERR "ERROR: flag %s set here but not in parent\n",
6665c5e253SValentin Schneider 			       sd_flag_debug[idx].name);
6765c5e253SValentin Schneider 	}
6865c5e253SValentin Schneider 
69f2cb1360SIngo Molnar 	printk(KERN_DEBUG "%*s groups:", level + 1, "");
70f2cb1360SIngo Molnar 	do {
71f2cb1360SIngo Molnar 		if (!group) {
72f2cb1360SIngo Molnar 			printk("\n");
73f2cb1360SIngo Molnar 			printk(KERN_ERR "ERROR: group is NULL\n");
74f2cb1360SIngo Molnar 			break;
75f2cb1360SIngo Molnar 		}
76f2cb1360SIngo Molnar 
77ae4df9d6SPeter Zijlstra 		if (!cpumask_weight(sched_group_span(group))) {
78f2cb1360SIngo Molnar 			printk(KERN_CONT "\n");
79f2cb1360SIngo Molnar 			printk(KERN_ERR "ERROR: empty group\n");
80f2cb1360SIngo Molnar 			break;
81f2cb1360SIngo Molnar 		}
82f2cb1360SIngo Molnar 
83f2cb1360SIngo Molnar 		if (!(sd->flags & SD_OVERLAP) &&
84ae4df9d6SPeter Zijlstra 		    cpumask_intersects(groupmask, sched_group_span(group))) {
85f2cb1360SIngo Molnar 			printk(KERN_CONT "\n");
86f2cb1360SIngo Molnar 			printk(KERN_ERR "ERROR: repeated CPUs\n");
87f2cb1360SIngo Molnar 			break;
88f2cb1360SIngo Molnar 		}
89f2cb1360SIngo Molnar 
90ae4df9d6SPeter Zijlstra 		cpumask_or(groupmask, groupmask, sched_group_span(group));
91f2cb1360SIngo Molnar 
92005f874dSPeter Zijlstra 		printk(KERN_CONT " %d:{ span=%*pbl",
93005f874dSPeter Zijlstra 				group->sgc->id,
94ae4df9d6SPeter Zijlstra 				cpumask_pr_args(sched_group_span(group)));
95b0151c25SPeter Zijlstra 
96af218122SPeter Zijlstra 		if ((sd->flags & SD_OVERLAP) &&
97ae4df9d6SPeter Zijlstra 		    !cpumask_equal(group_balance_mask(group), sched_group_span(group))) {
98005f874dSPeter Zijlstra 			printk(KERN_CONT " mask=%*pbl",
99e5c14b1fSPeter Zijlstra 				cpumask_pr_args(group_balance_mask(group)));
100b0151c25SPeter Zijlstra 		}
101b0151c25SPeter Zijlstra 
102005f874dSPeter Zijlstra 		if (group->sgc->capacity != SCHED_CAPACITY_SCALE)
103005f874dSPeter Zijlstra 			printk(KERN_CONT " cap=%lu", group->sgc->capacity);
104f2cb1360SIngo Molnar 
105a420b063SPeter Zijlstra 		if (group == sd->groups && sd->child &&
106a420b063SPeter Zijlstra 		    !cpumask_equal(sched_domain_span(sd->child),
107ae4df9d6SPeter Zijlstra 				   sched_group_span(group))) {
108a420b063SPeter Zijlstra 			printk(KERN_ERR "ERROR: domain->groups does not match domain->child\n");
109a420b063SPeter Zijlstra 		}
110a420b063SPeter Zijlstra 
111005f874dSPeter Zijlstra 		printk(KERN_CONT " }");
112005f874dSPeter Zijlstra 
113f2cb1360SIngo Molnar 		group = group->next;
114b0151c25SPeter Zijlstra 
115b0151c25SPeter Zijlstra 		if (group != sd->groups)
116b0151c25SPeter Zijlstra 			printk(KERN_CONT ",");
117b0151c25SPeter Zijlstra 
118f2cb1360SIngo Molnar 	} while (group != sd->groups);
119f2cb1360SIngo Molnar 	printk(KERN_CONT "\n");
120f2cb1360SIngo Molnar 
121f2cb1360SIngo Molnar 	if (!cpumask_equal(sched_domain_span(sd), groupmask))
122f2cb1360SIngo Molnar 		printk(KERN_ERR "ERROR: groups don't span domain->span\n");
123f2cb1360SIngo Molnar 
124f2cb1360SIngo Molnar 	if (sd->parent &&
125f2cb1360SIngo Molnar 	    !cpumask_subset(groupmask, sched_domain_span(sd->parent)))
12697fb7a0aSIngo Molnar 		printk(KERN_ERR "ERROR: parent span is not a superset of domain->span\n");
127f2cb1360SIngo Molnar 	return 0;
128f2cb1360SIngo Molnar }
129f2cb1360SIngo Molnar 
130f2cb1360SIngo Molnar static void sched_domain_debug(struct sched_domain *sd, int cpu)
131f2cb1360SIngo Molnar {
132f2cb1360SIngo Molnar 	int level = 0;
133f2cb1360SIngo Molnar 
134f2cb1360SIngo Molnar 	if (!sched_debug_enabled)
135f2cb1360SIngo Molnar 		return;
136f2cb1360SIngo Molnar 
137f2cb1360SIngo Molnar 	if (!sd) {
138f2cb1360SIngo Molnar 		printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
139f2cb1360SIngo Molnar 		return;
140f2cb1360SIngo Molnar 	}
141f2cb1360SIngo Molnar 
142005f874dSPeter Zijlstra 	printk(KERN_DEBUG "CPU%d attaching sched-domain(s):\n", cpu);
143f2cb1360SIngo Molnar 
144f2cb1360SIngo Molnar 	for (;;) {
145f2cb1360SIngo Molnar 		if (sched_domain_debug_one(sd, cpu, level, sched_domains_tmpmask))
146f2cb1360SIngo Molnar 			break;
147f2cb1360SIngo Molnar 		level++;
148f2cb1360SIngo Molnar 		sd = sd->parent;
149f2cb1360SIngo Molnar 		if (!sd)
150f2cb1360SIngo Molnar 			break;
151f2cb1360SIngo Molnar 	}
152f2cb1360SIngo Molnar }
153f2cb1360SIngo Molnar #else /* !CONFIG_SCHED_DEBUG */
154f2cb1360SIngo Molnar 
155f2cb1360SIngo Molnar # define sched_debug_enabled 0
156f2cb1360SIngo Molnar # define sched_domain_debug(sd, cpu) do { } while (0)
157f2cb1360SIngo Molnar static inline bool sched_debug(void)
158f2cb1360SIngo Molnar {
159f2cb1360SIngo Molnar 	return false;
160f2cb1360SIngo Molnar }
161f2cb1360SIngo Molnar #endif /* CONFIG_SCHED_DEBUG */
162f2cb1360SIngo Molnar 
1634fc472f1SValentin Schneider /* Generate a mask of SD flags with the SDF_NEEDS_GROUPS metaflag */
1644fc472f1SValentin Schneider #define SD_FLAG(name, mflags) (name * !!((mflags) & SDF_NEEDS_GROUPS)) |
1654fc472f1SValentin Schneider static const unsigned int SD_DEGENERATE_GROUPS_MASK =
1664fc472f1SValentin Schneider #include <linux/sched/sd_flags.h>
1674fc472f1SValentin Schneider 0;
1684fc472f1SValentin Schneider #undef SD_FLAG
1694fc472f1SValentin Schneider 
170f2cb1360SIngo Molnar static int sd_degenerate(struct sched_domain *sd)
171f2cb1360SIngo Molnar {
172f2cb1360SIngo Molnar 	if (cpumask_weight(sched_domain_span(sd)) == 1)
173f2cb1360SIngo Molnar 		return 1;
174f2cb1360SIngo Molnar 
175f2cb1360SIngo Molnar 	/* Following flags need at least 2 groups */
1766f349818SValentin Schneider 	if ((sd->flags & SD_DEGENERATE_GROUPS_MASK) &&
1776f349818SValentin Schneider 	    (sd->groups != sd->groups->next))
178f2cb1360SIngo Molnar 		return 0;
179f2cb1360SIngo Molnar 
180f2cb1360SIngo Molnar 	/* Following flags don't use groups */
181f2cb1360SIngo Molnar 	if (sd->flags & (SD_WAKE_AFFINE))
182f2cb1360SIngo Molnar 		return 0;
183f2cb1360SIngo Molnar 
184f2cb1360SIngo Molnar 	return 1;
185f2cb1360SIngo Molnar }
186f2cb1360SIngo Molnar 
187f2cb1360SIngo Molnar static int
188f2cb1360SIngo Molnar sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
189f2cb1360SIngo Molnar {
190f2cb1360SIngo Molnar 	unsigned long cflags = sd->flags, pflags = parent->flags;
191f2cb1360SIngo Molnar 
192f2cb1360SIngo Molnar 	if (sd_degenerate(parent))
193f2cb1360SIngo Molnar 		return 1;
194f2cb1360SIngo Molnar 
195f2cb1360SIngo Molnar 	if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent)))
196f2cb1360SIngo Molnar 		return 0;
197f2cb1360SIngo Molnar 
198f2cb1360SIngo Molnar 	/* Flags needing groups don't count if only 1 group in parent */
199ab65afb0SValentin Schneider 	if (parent->groups == parent->groups->next)
2003a6712c7SValentin Schneider 		pflags &= ~SD_DEGENERATE_GROUPS_MASK;
201ab65afb0SValentin Schneider 
202f2cb1360SIngo Molnar 	if (~cflags & pflags)
203f2cb1360SIngo Molnar 		return 0;
204f2cb1360SIngo Molnar 
205f2cb1360SIngo Molnar 	return 1;
206f2cb1360SIngo Molnar }
207f2cb1360SIngo Molnar 
208531b5c9fSQuentin Perret #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
209f8a696f2SPeter Zijlstra DEFINE_STATIC_KEY_FALSE(sched_energy_present);
2108d5d0cfbSQuentin Perret unsigned int sysctl_sched_energy_aware = 1;
211531b5c9fSQuentin Perret DEFINE_MUTEX(sched_energy_mutex);
212531b5c9fSQuentin Perret bool sched_energy_update;
213531b5c9fSQuentin Perret 
21431f6a8c0SIonela Voinescu void rebuild_sched_domains_energy(void)
21531f6a8c0SIonela Voinescu {
21631f6a8c0SIonela Voinescu 	mutex_lock(&sched_energy_mutex);
21731f6a8c0SIonela Voinescu 	sched_energy_update = true;
21831f6a8c0SIonela Voinescu 	rebuild_sched_domains();
21931f6a8c0SIonela Voinescu 	sched_energy_update = false;
22031f6a8c0SIonela Voinescu 	mutex_unlock(&sched_energy_mutex);
22131f6a8c0SIonela Voinescu }
22231f6a8c0SIonela Voinescu 
2238d5d0cfbSQuentin Perret #ifdef CONFIG_PROC_SYSCTL
2248d5d0cfbSQuentin Perret int sched_energy_aware_handler(struct ctl_table *table, int write,
22532927393SChristoph Hellwig 		void *buffer, size_t *lenp, loff_t *ppos)
2268d5d0cfbSQuentin Perret {
2278d5d0cfbSQuentin Perret 	int ret, state;
2288d5d0cfbSQuentin Perret 
2298d5d0cfbSQuentin Perret 	if (write && !capable(CAP_SYS_ADMIN))
2308d5d0cfbSQuentin Perret 		return -EPERM;
2318d5d0cfbSQuentin Perret 
2328d5d0cfbSQuentin Perret 	ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
2338d5d0cfbSQuentin Perret 	if (!ret && write) {
2348d5d0cfbSQuentin Perret 		state = static_branch_unlikely(&sched_energy_present);
23531f6a8c0SIonela Voinescu 		if (state != sysctl_sched_energy_aware)
23631f6a8c0SIonela Voinescu 			rebuild_sched_domains_energy();
2378d5d0cfbSQuentin Perret 	}
2388d5d0cfbSQuentin Perret 
2398d5d0cfbSQuentin Perret 	return ret;
2408d5d0cfbSQuentin Perret }
2418d5d0cfbSQuentin Perret #endif
2428d5d0cfbSQuentin Perret 
2436aa140faSQuentin Perret static void free_pd(struct perf_domain *pd)
2446aa140faSQuentin Perret {
2456aa140faSQuentin Perret 	struct perf_domain *tmp;
2466aa140faSQuentin Perret 
2476aa140faSQuentin Perret 	while (pd) {
2486aa140faSQuentin Perret 		tmp = pd->next;
2496aa140faSQuentin Perret 		kfree(pd);
2506aa140faSQuentin Perret 		pd = tmp;
2516aa140faSQuentin Perret 	}
2526aa140faSQuentin Perret }
2536aa140faSQuentin Perret 
2546aa140faSQuentin Perret static struct perf_domain *find_pd(struct perf_domain *pd, int cpu)
2556aa140faSQuentin Perret {
2566aa140faSQuentin Perret 	while (pd) {
2576aa140faSQuentin Perret 		if (cpumask_test_cpu(cpu, perf_domain_span(pd)))
2586aa140faSQuentin Perret 			return pd;
2596aa140faSQuentin Perret 		pd = pd->next;
2606aa140faSQuentin Perret 	}
2616aa140faSQuentin Perret 
2626aa140faSQuentin Perret 	return NULL;
2636aa140faSQuentin Perret }
2646aa140faSQuentin Perret 
2656aa140faSQuentin Perret static struct perf_domain *pd_init(int cpu)
2666aa140faSQuentin Perret {
2676aa140faSQuentin Perret 	struct em_perf_domain *obj = em_cpu_get(cpu);
2686aa140faSQuentin Perret 	struct perf_domain *pd;
2696aa140faSQuentin Perret 
2706aa140faSQuentin Perret 	if (!obj) {
2716aa140faSQuentin Perret 		if (sched_debug())
2726aa140faSQuentin Perret 			pr_info("%s: no EM found for CPU%d\n", __func__, cpu);
2736aa140faSQuentin Perret 		return NULL;
2746aa140faSQuentin Perret 	}
2756aa140faSQuentin Perret 
2766aa140faSQuentin Perret 	pd = kzalloc(sizeof(*pd), GFP_KERNEL);
2776aa140faSQuentin Perret 	if (!pd)
2786aa140faSQuentin Perret 		return NULL;
2796aa140faSQuentin Perret 	pd->em_pd = obj;
2806aa140faSQuentin Perret 
2816aa140faSQuentin Perret 	return pd;
2826aa140faSQuentin Perret }
2836aa140faSQuentin Perret 
2846aa140faSQuentin Perret static void perf_domain_debug(const struct cpumask *cpu_map,
2856aa140faSQuentin Perret 						struct perf_domain *pd)
2866aa140faSQuentin Perret {
2876aa140faSQuentin Perret 	if (!sched_debug() || !pd)
2886aa140faSQuentin Perret 		return;
2896aa140faSQuentin Perret 
2906aa140faSQuentin Perret 	printk(KERN_DEBUG "root_domain %*pbl:", cpumask_pr_args(cpu_map));
2916aa140faSQuentin Perret 
2926aa140faSQuentin Perret 	while (pd) {
293521b512bSLukasz Luba 		printk(KERN_CONT " pd%d:{ cpus=%*pbl nr_pstate=%d }",
2946aa140faSQuentin Perret 				cpumask_first(perf_domain_span(pd)),
2956aa140faSQuentin Perret 				cpumask_pr_args(perf_domain_span(pd)),
296521b512bSLukasz Luba 				em_pd_nr_perf_states(pd->em_pd));
2976aa140faSQuentin Perret 		pd = pd->next;
2986aa140faSQuentin Perret 	}
2996aa140faSQuentin Perret 
3006aa140faSQuentin Perret 	printk(KERN_CONT "\n");
3016aa140faSQuentin Perret }
3026aa140faSQuentin Perret 
3036aa140faSQuentin Perret static void destroy_perf_domain_rcu(struct rcu_head *rp)
3046aa140faSQuentin Perret {
3056aa140faSQuentin Perret 	struct perf_domain *pd;
3066aa140faSQuentin Perret 
3076aa140faSQuentin Perret 	pd = container_of(rp, struct perf_domain, rcu);
3086aa140faSQuentin Perret 	free_pd(pd);
3096aa140faSQuentin Perret }
3106aa140faSQuentin Perret 
3111f74de87SQuentin Perret static void sched_energy_set(bool has_eas)
3121f74de87SQuentin Perret {
3131f74de87SQuentin Perret 	if (!has_eas && static_branch_unlikely(&sched_energy_present)) {
3141f74de87SQuentin Perret 		if (sched_debug())
3151f74de87SQuentin Perret 			pr_info("%s: stopping EAS\n", __func__);
3161f74de87SQuentin Perret 		static_branch_disable_cpuslocked(&sched_energy_present);
3171f74de87SQuentin Perret 	} else if (has_eas && !static_branch_unlikely(&sched_energy_present)) {
3181f74de87SQuentin Perret 		if (sched_debug())
3191f74de87SQuentin Perret 			pr_info("%s: starting EAS\n", __func__);
3201f74de87SQuentin Perret 		static_branch_enable_cpuslocked(&sched_energy_present);
3211f74de87SQuentin Perret 	}
3221f74de87SQuentin Perret }
3231f74de87SQuentin Perret 
324b68a4c0dSQuentin Perret /*
325b68a4c0dSQuentin Perret  * EAS can be used on a root domain if it meets all the following conditions:
326b68a4c0dSQuentin Perret  *    1. an Energy Model (EM) is available;
327b68a4c0dSQuentin Perret  *    2. the SD_ASYM_CPUCAPACITY flag is set in the sched_domain hierarchy.
32838502ab4SValentin Schneider  *    3. no SMT is detected.
32938502ab4SValentin Schneider  *    4. the EM complexity is low enough to keep scheduling overheads low;
33038502ab4SValentin Schneider  *    5. schedutil is driving the frequency of all CPUs of the rd;
331fa50e2b4SIonela Voinescu  *    6. frequency invariance support is present;
332b68a4c0dSQuentin Perret  *
333b68a4c0dSQuentin Perret  * The complexity of the Energy Model is defined as:
334b68a4c0dSQuentin Perret  *
335521b512bSLukasz Luba  *              C = nr_pd * (nr_cpus + nr_ps)
336b68a4c0dSQuentin Perret  *
337b68a4c0dSQuentin Perret  * with parameters defined as:
338b68a4c0dSQuentin Perret  *  - nr_pd:    the number of performance domains
339b68a4c0dSQuentin Perret  *  - nr_cpus:  the number of CPUs
340521b512bSLukasz Luba  *  - nr_ps:    the sum of the number of performance states of all performance
341b68a4c0dSQuentin Perret  *              domains (for example, on a system with 2 performance domains,
342521b512bSLukasz Luba  *              with 10 performance states each, nr_ps = 2 * 10 = 20).
343b68a4c0dSQuentin Perret  *
344b68a4c0dSQuentin Perret  * It is generally not a good idea to use such a model in the wake-up path on
345b68a4c0dSQuentin Perret  * very complex platforms because of the associated scheduling overheads. The
346b68a4c0dSQuentin Perret  * arbitrary constraint below prevents that. It makes EAS usable up to 16 CPUs
347521b512bSLukasz Luba  * with per-CPU DVFS and less than 8 performance states each, for example.
348b68a4c0dSQuentin Perret  */
349b68a4c0dSQuentin Perret #define EM_MAX_COMPLEXITY 2048
350b68a4c0dSQuentin Perret 
351531b5c9fSQuentin Perret extern struct cpufreq_governor schedutil_gov;
3521f74de87SQuentin Perret static bool build_perf_domains(const struct cpumask *cpu_map)
3536aa140faSQuentin Perret {
354521b512bSLukasz Luba 	int i, nr_pd = 0, nr_ps = 0, nr_cpus = cpumask_weight(cpu_map);
3556aa140faSQuentin Perret 	struct perf_domain *pd = NULL, *tmp;
3566aa140faSQuentin Perret 	int cpu = cpumask_first(cpu_map);
3576aa140faSQuentin Perret 	struct root_domain *rd = cpu_rq(cpu)->rd;
358531b5c9fSQuentin Perret 	struct cpufreq_policy *policy;
359531b5c9fSQuentin Perret 	struct cpufreq_governor *gov;
360b68a4c0dSQuentin Perret 
3618d5d0cfbSQuentin Perret 	if (!sysctl_sched_energy_aware)
3628d5d0cfbSQuentin Perret 		goto free;
3638d5d0cfbSQuentin Perret 
364b68a4c0dSQuentin Perret 	/* EAS is enabled for asymmetric CPU capacity topologies. */
365b68a4c0dSQuentin Perret 	if (!per_cpu(sd_asym_cpucapacity, cpu)) {
366b68a4c0dSQuentin Perret 		if (sched_debug()) {
367b68a4c0dSQuentin Perret 			pr_info("rd %*pbl: CPUs do not have asymmetric capacities\n",
368b68a4c0dSQuentin Perret 					cpumask_pr_args(cpu_map));
369b68a4c0dSQuentin Perret 		}
370b68a4c0dSQuentin Perret 		goto free;
371b68a4c0dSQuentin Perret 	}
3726aa140faSQuentin Perret 
37338502ab4SValentin Schneider 	/* EAS definitely does *not* handle SMT */
37438502ab4SValentin Schneider 	if (sched_smt_active()) {
37538502ab4SValentin Schneider 		pr_warn("rd %*pbl: Disabling EAS, SMT is not supported\n",
37638502ab4SValentin Schneider 			cpumask_pr_args(cpu_map));
37738502ab4SValentin Schneider 		goto free;
37838502ab4SValentin Schneider 	}
37938502ab4SValentin Schneider 
380fa50e2b4SIonela Voinescu 	if (!arch_scale_freq_invariant()) {
381fa50e2b4SIonela Voinescu 		if (sched_debug()) {
382fa50e2b4SIonela Voinescu 			pr_warn("rd %*pbl: Disabling EAS: frequency-invariant load tracking not yet supported",
383fa50e2b4SIonela Voinescu 				cpumask_pr_args(cpu_map));
384fa50e2b4SIonela Voinescu 		}
385fa50e2b4SIonela Voinescu 		goto free;
386fa50e2b4SIonela Voinescu 	}
387fa50e2b4SIonela Voinescu 
3886aa140faSQuentin Perret 	for_each_cpu(i, cpu_map) {
3896aa140faSQuentin Perret 		/* Skip already covered CPUs. */
3906aa140faSQuentin Perret 		if (find_pd(pd, i))
3916aa140faSQuentin Perret 			continue;
3926aa140faSQuentin Perret 
393531b5c9fSQuentin Perret 		/* Do not attempt EAS if schedutil is not being used. */
394531b5c9fSQuentin Perret 		policy = cpufreq_cpu_get(i);
395531b5c9fSQuentin Perret 		if (!policy)
396531b5c9fSQuentin Perret 			goto free;
397531b5c9fSQuentin Perret 		gov = policy->governor;
398531b5c9fSQuentin Perret 		cpufreq_cpu_put(policy);
399531b5c9fSQuentin Perret 		if (gov != &schedutil_gov) {
400531b5c9fSQuentin Perret 			if (rd->pd)
401531b5c9fSQuentin Perret 				pr_warn("rd %*pbl: Disabling EAS, schedutil is mandatory\n",
402531b5c9fSQuentin Perret 						cpumask_pr_args(cpu_map));
403531b5c9fSQuentin Perret 			goto free;
404531b5c9fSQuentin Perret 		}
405531b5c9fSQuentin Perret 
4066aa140faSQuentin Perret 		/* Create the new pd and add it to the local list. */
4076aa140faSQuentin Perret 		tmp = pd_init(i);
4086aa140faSQuentin Perret 		if (!tmp)
4096aa140faSQuentin Perret 			goto free;
4106aa140faSQuentin Perret 		tmp->next = pd;
4116aa140faSQuentin Perret 		pd = tmp;
412b68a4c0dSQuentin Perret 
413b68a4c0dSQuentin Perret 		/*
414521b512bSLukasz Luba 		 * Count performance domains and performance states for the
415b68a4c0dSQuentin Perret 		 * complexity check.
416b68a4c0dSQuentin Perret 		 */
417b68a4c0dSQuentin Perret 		nr_pd++;
418521b512bSLukasz Luba 		nr_ps += em_pd_nr_perf_states(pd->em_pd);
419b68a4c0dSQuentin Perret 	}
420b68a4c0dSQuentin Perret 
421b68a4c0dSQuentin Perret 	/* Bail out if the Energy Model complexity is too high. */
422521b512bSLukasz Luba 	if (nr_pd * (nr_ps + nr_cpus) > EM_MAX_COMPLEXITY) {
423b68a4c0dSQuentin Perret 		WARN(1, "rd %*pbl: Failed to start EAS, EM complexity is too high\n",
424b68a4c0dSQuentin Perret 						cpumask_pr_args(cpu_map));
425b68a4c0dSQuentin Perret 		goto free;
4266aa140faSQuentin Perret 	}
4276aa140faSQuentin Perret 
4286aa140faSQuentin Perret 	perf_domain_debug(cpu_map, pd);
4296aa140faSQuentin Perret 
4306aa140faSQuentin Perret 	/* Attach the new list of performance domains to the root domain. */
4316aa140faSQuentin Perret 	tmp = rd->pd;
4326aa140faSQuentin Perret 	rcu_assign_pointer(rd->pd, pd);
4336aa140faSQuentin Perret 	if (tmp)
4346aa140faSQuentin Perret 		call_rcu(&tmp->rcu, destroy_perf_domain_rcu);
4356aa140faSQuentin Perret 
4361f74de87SQuentin Perret 	return !!pd;
4376aa140faSQuentin Perret 
4386aa140faSQuentin Perret free:
4396aa140faSQuentin Perret 	free_pd(pd);
4406aa140faSQuentin Perret 	tmp = rd->pd;
4416aa140faSQuentin Perret 	rcu_assign_pointer(rd->pd, NULL);
4426aa140faSQuentin Perret 	if (tmp)
4436aa140faSQuentin Perret 		call_rcu(&tmp->rcu, destroy_perf_domain_rcu);
4441f74de87SQuentin Perret 
4451f74de87SQuentin Perret 	return false;
4466aa140faSQuentin Perret }
4476aa140faSQuentin Perret #else
4486aa140faSQuentin Perret static void free_pd(struct perf_domain *pd) { }
449531b5c9fSQuentin Perret #endif /* CONFIG_ENERGY_MODEL && CONFIG_CPU_FREQ_GOV_SCHEDUTIL*/
4506aa140faSQuentin Perret 
451f2cb1360SIngo Molnar static void free_rootdomain(struct rcu_head *rcu)
452f2cb1360SIngo Molnar {
453f2cb1360SIngo Molnar 	struct root_domain *rd = container_of(rcu, struct root_domain, rcu);
454f2cb1360SIngo Molnar 
455f2cb1360SIngo Molnar 	cpupri_cleanup(&rd->cpupri);
456f2cb1360SIngo Molnar 	cpudl_cleanup(&rd->cpudl);
457f2cb1360SIngo Molnar 	free_cpumask_var(rd->dlo_mask);
458f2cb1360SIngo Molnar 	free_cpumask_var(rd->rto_mask);
459f2cb1360SIngo Molnar 	free_cpumask_var(rd->online);
460f2cb1360SIngo Molnar 	free_cpumask_var(rd->span);
4616aa140faSQuentin Perret 	free_pd(rd->pd);
462f2cb1360SIngo Molnar 	kfree(rd);
463f2cb1360SIngo Molnar }
464f2cb1360SIngo Molnar 
465f2cb1360SIngo Molnar void rq_attach_root(struct rq *rq, struct root_domain *rd)
466f2cb1360SIngo Molnar {
467f2cb1360SIngo Molnar 	struct root_domain *old_rd = NULL;
468f2cb1360SIngo Molnar 	unsigned long flags;
469f2cb1360SIngo Molnar 
470f2cb1360SIngo Molnar 	raw_spin_lock_irqsave(&rq->lock, flags);
471f2cb1360SIngo Molnar 
472f2cb1360SIngo Molnar 	if (rq->rd) {
473f2cb1360SIngo Molnar 		old_rd = rq->rd;
474f2cb1360SIngo Molnar 
475f2cb1360SIngo Molnar 		if (cpumask_test_cpu(rq->cpu, old_rd->online))
476f2cb1360SIngo Molnar 			set_rq_offline(rq);
477f2cb1360SIngo Molnar 
478f2cb1360SIngo Molnar 		cpumask_clear_cpu(rq->cpu, old_rd->span);
479f2cb1360SIngo Molnar 
480f2cb1360SIngo Molnar 		/*
481f2cb1360SIngo Molnar 		 * If we dont want to free the old_rd yet then
482f2cb1360SIngo Molnar 		 * set old_rd to NULL to skip the freeing later
483f2cb1360SIngo Molnar 		 * in this function:
484f2cb1360SIngo Molnar 		 */
485f2cb1360SIngo Molnar 		if (!atomic_dec_and_test(&old_rd->refcount))
486f2cb1360SIngo Molnar 			old_rd = NULL;
487f2cb1360SIngo Molnar 	}
488f2cb1360SIngo Molnar 
489f2cb1360SIngo Molnar 	atomic_inc(&rd->refcount);
490f2cb1360SIngo Molnar 	rq->rd = rd;
491f2cb1360SIngo Molnar 
492f2cb1360SIngo Molnar 	cpumask_set_cpu(rq->cpu, rd->span);
493f2cb1360SIngo Molnar 	if (cpumask_test_cpu(rq->cpu, cpu_active_mask))
494f2cb1360SIngo Molnar 		set_rq_online(rq);
495f2cb1360SIngo Molnar 
496f2cb1360SIngo Molnar 	raw_spin_unlock_irqrestore(&rq->lock, flags);
497f2cb1360SIngo Molnar 
498f2cb1360SIngo Molnar 	if (old_rd)
499337e9b07SPaul E. McKenney 		call_rcu(&old_rd->rcu, free_rootdomain);
500f2cb1360SIngo Molnar }
501f2cb1360SIngo Molnar 
502364f5665SSteven Rostedt (VMware) void sched_get_rd(struct root_domain *rd)
503364f5665SSteven Rostedt (VMware) {
504364f5665SSteven Rostedt (VMware) 	atomic_inc(&rd->refcount);
505364f5665SSteven Rostedt (VMware) }
506364f5665SSteven Rostedt (VMware) 
507364f5665SSteven Rostedt (VMware) void sched_put_rd(struct root_domain *rd)
508364f5665SSteven Rostedt (VMware) {
509364f5665SSteven Rostedt (VMware) 	if (!atomic_dec_and_test(&rd->refcount))
510364f5665SSteven Rostedt (VMware) 		return;
511364f5665SSteven Rostedt (VMware) 
512337e9b07SPaul E. McKenney 	call_rcu(&rd->rcu, free_rootdomain);
513364f5665SSteven Rostedt (VMware) }
514364f5665SSteven Rostedt (VMware) 
515f2cb1360SIngo Molnar static int init_rootdomain(struct root_domain *rd)
516f2cb1360SIngo Molnar {
517f2cb1360SIngo Molnar 	if (!zalloc_cpumask_var(&rd->span, GFP_KERNEL))
518f2cb1360SIngo Molnar 		goto out;
519f2cb1360SIngo Molnar 	if (!zalloc_cpumask_var(&rd->online, GFP_KERNEL))
520f2cb1360SIngo Molnar 		goto free_span;
521f2cb1360SIngo Molnar 	if (!zalloc_cpumask_var(&rd->dlo_mask, GFP_KERNEL))
522f2cb1360SIngo Molnar 		goto free_online;
523f2cb1360SIngo Molnar 	if (!zalloc_cpumask_var(&rd->rto_mask, GFP_KERNEL))
524f2cb1360SIngo Molnar 		goto free_dlo_mask;
525f2cb1360SIngo Molnar 
5264bdced5cSSteven Rostedt (Red Hat) #ifdef HAVE_RT_PUSH_IPI
5274bdced5cSSteven Rostedt (Red Hat) 	rd->rto_cpu = -1;
5284bdced5cSSteven Rostedt (Red Hat) 	raw_spin_lock_init(&rd->rto_lock);
5294bdced5cSSteven Rostedt (Red Hat) 	init_irq_work(&rd->rto_push_work, rto_push_irq_work_func);
5304bdced5cSSteven Rostedt (Red Hat) #endif
5314bdced5cSSteven Rostedt (Red Hat) 
53226762423SPeng Liu 	rd->visit_gen = 0;
533f2cb1360SIngo Molnar 	init_dl_bw(&rd->dl_bw);
534f2cb1360SIngo Molnar 	if (cpudl_init(&rd->cpudl) != 0)
535f2cb1360SIngo Molnar 		goto free_rto_mask;
536f2cb1360SIngo Molnar 
537f2cb1360SIngo Molnar 	if (cpupri_init(&rd->cpupri) != 0)
538f2cb1360SIngo Molnar 		goto free_cpudl;
539f2cb1360SIngo Molnar 	return 0;
540f2cb1360SIngo Molnar 
541f2cb1360SIngo Molnar free_cpudl:
542f2cb1360SIngo Molnar 	cpudl_cleanup(&rd->cpudl);
543f2cb1360SIngo Molnar free_rto_mask:
544f2cb1360SIngo Molnar 	free_cpumask_var(rd->rto_mask);
545f2cb1360SIngo Molnar free_dlo_mask:
546f2cb1360SIngo Molnar 	free_cpumask_var(rd->dlo_mask);
547f2cb1360SIngo Molnar free_online:
548f2cb1360SIngo Molnar 	free_cpumask_var(rd->online);
549f2cb1360SIngo Molnar free_span:
550f2cb1360SIngo Molnar 	free_cpumask_var(rd->span);
551f2cb1360SIngo Molnar out:
552f2cb1360SIngo Molnar 	return -ENOMEM;
553f2cb1360SIngo Molnar }
554f2cb1360SIngo Molnar 
555f2cb1360SIngo Molnar /*
556f2cb1360SIngo Molnar  * By default the system creates a single root-domain with all CPUs as
557f2cb1360SIngo Molnar  * members (mimicking the global state we have today).
558f2cb1360SIngo Molnar  */
559f2cb1360SIngo Molnar struct root_domain def_root_domain;
560f2cb1360SIngo Molnar 
561f2cb1360SIngo Molnar void init_defrootdomain(void)
562f2cb1360SIngo Molnar {
563f2cb1360SIngo Molnar 	init_rootdomain(&def_root_domain);
564f2cb1360SIngo Molnar 
565f2cb1360SIngo Molnar 	atomic_set(&def_root_domain.refcount, 1);
566f2cb1360SIngo Molnar }
567f2cb1360SIngo Molnar 
568f2cb1360SIngo Molnar static struct root_domain *alloc_rootdomain(void)
569f2cb1360SIngo Molnar {
570f2cb1360SIngo Molnar 	struct root_domain *rd;
571f2cb1360SIngo Molnar 
5724d13a06dSViresh Kumar 	rd = kzalloc(sizeof(*rd), GFP_KERNEL);
573f2cb1360SIngo Molnar 	if (!rd)
574f2cb1360SIngo Molnar 		return NULL;
575f2cb1360SIngo Molnar 
576f2cb1360SIngo Molnar 	if (init_rootdomain(rd) != 0) {
577f2cb1360SIngo Molnar 		kfree(rd);
578f2cb1360SIngo Molnar 		return NULL;
579f2cb1360SIngo Molnar 	}
580f2cb1360SIngo Molnar 
581f2cb1360SIngo Molnar 	return rd;
582f2cb1360SIngo Molnar }
583f2cb1360SIngo Molnar 
584f2cb1360SIngo Molnar static void free_sched_groups(struct sched_group *sg, int free_sgc)
585f2cb1360SIngo Molnar {
586f2cb1360SIngo Molnar 	struct sched_group *tmp, *first;
587f2cb1360SIngo Molnar 
588f2cb1360SIngo Molnar 	if (!sg)
589f2cb1360SIngo Molnar 		return;
590f2cb1360SIngo Molnar 
591f2cb1360SIngo Molnar 	first = sg;
592f2cb1360SIngo Molnar 	do {
593f2cb1360SIngo Molnar 		tmp = sg->next;
594f2cb1360SIngo Molnar 
595f2cb1360SIngo Molnar 		if (free_sgc && atomic_dec_and_test(&sg->sgc->ref))
596f2cb1360SIngo Molnar 			kfree(sg->sgc);
597f2cb1360SIngo Molnar 
598213c5a45SShu Wang 		if (atomic_dec_and_test(&sg->ref))
599f2cb1360SIngo Molnar 			kfree(sg);
600f2cb1360SIngo Molnar 		sg = tmp;
601f2cb1360SIngo Molnar 	} while (sg != first);
602f2cb1360SIngo Molnar }
603f2cb1360SIngo Molnar 
604f2cb1360SIngo Molnar static void destroy_sched_domain(struct sched_domain *sd)
605f2cb1360SIngo Molnar {
606f2cb1360SIngo Molnar 	/*
607a090c4f2SPeter Zijlstra 	 * A normal sched domain may have multiple group references, an
608a090c4f2SPeter Zijlstra 	 * overlapping domain, having private groups, only one.  Iterate,
609a090c4f2SPeter Zijlstra 	 * dropping group/capacity references, freeing where none remain.
610f2cb1360SIngo Molnar 	 */
611f2cb1360SIngo Molnar 	free_sched_groups(sd->groups, 1);
612213c5a45SShu Wang 
613f2cb1360SIngo Molnar 	if (sd->shared && atomic_dec_and_test(&sd->shared->ref))
614f2cb1360SIngo Molnar 		kfree(sd->shared);
615f2cb1360SIngo Molnar 	kfree(sd);
616f2cb1360SIngo Molnar }
617f2cb1360SIngo Molnar 
618f2cb1360SIngo Molnar static void destroy_sched_domains_rcu(struct rcu_head *rcu)
619f2cb1360SIngo Molnar {
620f2cb1360SIngo Molnar 	struct sched_domain *sd = container_of(rcu, struct sched_domain, rcu);
621f2cb1360SIngo Molnar 
622f2cb1360SIngo Molnar 	while (sd) {
623f2cb1360SIngo Molnar 		struct sched_domain *parent = sd->parent;
624f2cb1360SIngo Molnar 		destroy_sched_domain(sd);
625f2cb1360SIngo Molnar 		sd = parent;
626f2cb1360SIngo Molnar 	}
627f2cb1360SIngo Molnar }
628f2cb1360SIngo Molnar 
629f2cb1360SIngo Molnar static void destroy_sched_domains(struct sched_domain *sd)
630f2cb1360SIngo Molnar {
631f2cb1360SIngo Molnar 	if (sd)
632f2cb1360SIngo Molnar 		call_rcu(&sd->rcu, destroy_sched_domains_rcu);
633f2cb1360SIngo Molnar }
634f2cb1360SIngo Molnar 
635f2cb1360SIngo Molnar /*
636f2cb1360SIngo Molnar  * Keep a special pointer to the highest sched_domain that has
637f2cb1360SIngo Molnar  * SD_SHARE_PKG_RESOURCE set (Last Level Cache Domain) for this
638f2cb1360SIngo Molnar  * allows us to avoid some pointer chasing select_idle_sibling().
639f2cb1360SIngo Molnar  *
640f2cb1360SIngo Molnar  * Also keep a unique ID per domain (we use the first CPU number in
641f2cb1360SIngo Molnar  * the cpumask of the domain), this allows us to quickly tell if
642f2cb1360SIngo Molnar  * two CPUs are in the same cache domain, see cpus_share_cache().
643f2cb1360SIngo Molnar  */
644994aeb7aSJoel Fernandes (Google) DEFINE_PER_CPU(struct sched_domain __rcu *, sd_llc);
645f2cb1360SIngo Molnar DEFINE_PER_CPU(int, sd_llc_size);
646f2cb1360SIngo Molnar DEFINE_PER_CPU(int, sd_llc_id);
647994aeb7aSJoel Fernandes (Google) DEFINE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared);
648994aeb7aSJoel Fernandes (Google) DEFINE_PER_CPU(struct sched_domain __rcu *, sd_numa);
649994aeb7aSJoel Fernandes (Google) DEFINE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing);
650994aeb7aSJoel Fernandes (Google) DEFINE_PER_CPU(struct sched_domain __rcu *, sd_asym_cpucapacity);
651df054e84SMorten Rasmussen DEFINE_STATIC_KEY_FALSE(sched_asym_cpucapacity);
652f2cb1360SIngo Molnar 
653f2cb1360SIngo Molnar static void update_top_cache_domain(int cpu)
654f2cb1360SIngo Molnar {
655f2cb1360SIngo Molnar 	struct sched_domain_shared *sds = NULL;
656f2cb1360SIngo Molnar 	struct sched_domain *sd;
657f2cb1360SIngo Molnar 	int id = cpu;
658f2cb1360SIngo Molnar 	int size = 1;
659f2cb1360SIngo Molnar 
660f2cb1360SIngo Molnar 	sd = highest_flag_domain(cpu, SD_SHARE_PKG_RESOURCES);
661f2cb1360SIngo Molnar 	if (sd) {
662f2cb1360SIngo Molnar 		id = cpumask_first(sched_domain_span(sd));
663f2cb1360SIngo Molnar 		size = cpumask_weight(sched_domain_span(sd));
664f2cb1360SIngo Molnar 		sds = sd->shared;
665f2cb1360SIngo Molnar 	}
666f2cb1360SIngo Molnar 
667f2cb1360SIngo Molnar 	rcu_assign_pointer(per_cpu(sd_llc, cpu), sd);
668f2cb1360SIngo Molnar 	per_cpu(sd_llc_size, cpu) = size;
669f2cb1360SIngo Molnar 	per_cpu(sd_llc_id, cpu) = id;
670f2cb1360SIngo Molnar 	rcu_assign_pointer(per_cpu(sd_llc_shared, cpu), sds);
671f2cb1360SIngo Molnar 
672f2cb1360SIngo Molnar 	sd = lowest_flag_domain(cpu, SD_NUMA);
673f2cb1360SIngo Molnar 	rcu_assign_pointer(per_cpu(sd_numa, cpu), sd);
674f2cb1360SIngo Molnar 
675f2cb1360SIngo Molnar 	sd = highest_flag_domain(cpu, SD_ASYM_PACKING);
676011b27bbSQuentin Perret 	rcu_assign_pointer(per_cpu(sd_asym_packing, cpu), sd);
677011b27bbSQuentin Perret 
678011b27bbSQuentin Perret 	sd = lowest_flag_domain(cpu, SD_ASYM_CPUCAPACITY);
679011b27bbSQuentin Perret 	rcu_assign_pointer(per_cpu(sd_asym_cpucapacity, cpu), sd);
680f2cb1360SIngo Molnar }
681f2cb1360SIngo Molnar 
682f2cb1360SIngo Molnar /*
683f2cb1360SIngo Molnar  * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
684f2cb1360SIngo Molnar  * hold the hotplug lock.
685f2cb1360SIngo Molnar  */
686f2cb1360SIngo Molnar static void
687f2cb1360SIngo Molnar cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
688f2cb1360SIngo Molnar {
689f2cb1360SIngo Molnar 	struct rq *rq = cpu_rq(cpu);
690f2cb1360SIngo Molnar 	struct sched_domain *tmp;
691b5b21734SValentin Schneider 	int numa_distance = 0;
692f2cb1360SIngo Molnar 
693f2cb1360SIngo Molnar 	/* Remove the sched domains which do not contribute to scheduling. */
694f2cb1360SIngo Molnar 	for (tmp = sd; tmp; ) {
695f2cb1360SIngo Molnar 		struct sched_domain *parent = tmp->parent;
696f2cb1360SIngo Molnar 		if (!parent)
697f2cb1360SIngo Molnar 			break;
698f2cb1360SIngo Molnar 
699f2cb1360SIngo Molnar 		if (sd_parent_degenerate(tmp, parent)) {
700f2cb1360SIngo Molnar 			tmp->parent = parent->parent;
701f2cb1360SIngo Molnar 			if (parent->parent)
702f2cb1360SIngo Molnar 				parent->parent->child = tmp;
703f2cb1360SIngo Molnar 			/*
704f2cb1360SIngo Molnar 			 * Transfer SD_PREFER_SIBLING down in case of a
705f2cb1360SIngo Molnar 			 * degenerate parent; the spans match for this
706f2cb1360SIngo Molnar 			 * so the property transfers.
707f2cb1360SIngo Molnar 			 */
708f2cb1360SIngo Molnar 			if (parent->flags & SD_PREFER_SIBLING)
709f2cb1360SIngo Molnar 				tmp->flags |= SD_PREFER_SIBLING;
710f2cb1360SIngo Molnar 			destroy_sched_domain(parent);
711f2cb1360SIngo Molnar 		} else
712f2cb1360SIngo Molnar 			tmp = tmp->parent;
713f2cb1360SIngo Molnar 	}
714f2cb1360SIngo Molnar 
715f2cb1360SIngo Molnar 	if (sd && sd_degenerate(sd)) {
716f2cb1360SIngo Molnar 		tmp = sd;
717f2cb1360SIngo Molnar 		sd = sd->parent;
718f2cb1360SIngo Molnar 		destroy_sched_domain(tmp);
719f2cb1360SIngo Molnar 		if (sd)
720f2cb1360SIngo Molnar 			sd->child = NULL;
721f2cb1360SIngo Molnar 	}
722f2cb1360SIngo Molnar 
723b5b21734SValentin Schneider 	for (tmp = sd; tmp; tmp = tmp->parent)
724b5b21734SValentin Schneider 		numa_distance += !!(tmp->flags & SD_NUMA);
725b5b21734SValentin Schneider 
726f2cb1360SIngo Molnar 	sched_domain_debug(sd, cpu);
727f2cb1360SIngo Molnar 
728f2cb1360SIngo Molnar 	rq_attach_root(rq, rd);
729f2cb1360SIngo Molnar 	tmp = rq->sd;
730f2cb1360SIngo Molnar 	rcu_assign_pointer(rq->sd, sd);
731bbdacdfeSPeter Zijlstra 	dirty_sched_domain_sysctl(cpu);
732f2cb1360SIngo Molnar 	destroy_sched_domains(tmp);
733f2cb1360SIngo Molnar 
734f2cb1360SIngo Molnar 	update_top_cache_domain(cpu);
735f2cb1360SIngo Molnar }
736f2cb1360SIngo Molnar 
737f2cb1360SIngo Molnar struct s_data {
73899687cdbSLuc Van Oostenryck 	struct sched_domain * __percpu *sd;
739f2cb1360SIngo Molnar 	struct root_domain	*rd;
740f2cb1360SIngo Molnar };
741f2cb1360SIngo Molnar 
742f2cb1360SIngo Molnar enum s_alloc {
743f2cb1360SIngo Molnar 	sa_rootdomain,
744f2cb1360SIngo Molnar 	sa_sd,
745f2cb1360SIngo Molnar 	sa_sd_storage,
746f2cb1360SIngo Molnar 	sa_none,
747f2cb1360SIngo Molnar };
748f2cb1360SIngo Molnar 
749f2cb1360SIngo Molnar /*
75035a566e6SPeter Zijlstra  * Return the canonical balance CPU for this group, this is the first CPU
751e5c14b1fSPeter Zijlstra  * of this group that's also in the balance mask.
75235a566e6SPeter Zijlstra  *
753e5c14b1fSPeter Zijlstra  * The balance mask are all those CPUs that could actually end up at this
754e5c14b1fSPeter Zijlstra  * group. See build_balance_mask().
75535a566e6SPeter Zijlstra  *
75635a566e6SPeter Zijlstra  * Also see should_we_balance().
75735a566e6SPeter Zijlstra  */
75835a566e6SPeter Zijlstra int group_balance_cpu(struct sched_group *sg)
75935a566e6SPeter Zijlstra {
760e5c14b1fSPeter Zijlstra 	return cpumask_first(group_balance_mask(sg));
76135a566e6SPeter Zijlstra }
76235a566e6SPeter Zijlstra 
76335a566e6SPeter Zijlstra 
76435a566e6SPeter Zijlstra /*
76535a566e6SPeter Zijlstra  * NUMA topology (first read the regular topology blurb below)
76635a566e6SPeter Zijlstra  *
76735a566e6SPeter Zijlstra  * Given a node-distance table, for example:
76835a566e6SPeter Zijlstra  *
76935a566e6SPeter Zijlstra  *   node   0   1   2   3
77035a566e6SPeter Zijlstra  *     0:  10  20  30  20
77135a566e6SPeter Zijlstra  *     1:  20  10  20  30
77235a566e6SPeter Zijlstra  *     2:  30  20  10  20
77335a566e6SPeter Zijlstra  *     3:  20  30  20  10
77435a566e6SPeter Zijlstra  *
77535a566e6SPeter Zijlstra  * which represents a 4 node ring topology like:
77635a566e6SPeter Zijlstra  *
77735a566e6SPeter Zijlstra  *   0 ----- 1
77835a566e6SPeter Zijlstra  *   |       |
77935a566e6SPeter Zijlstra  *   |       |
78035a566e6SPeter Zijlstra  *   |       |
78135a566e6SPeter Zijlstra  *   3 ----- 2
78235a566e6SPeter Zijlstra  *
78335a566e6SPeter Zijlstra  * We want to construct domains and groups to represent this. The way we go
78435a566e6SPeter Zijlstra  * about doing this is to build the domains on 'hops'. For each NUMA level we
78535a566e6SPeter Zijlstra  * construct the mask of all nodes reachable in @level hops.
78635a566e6SPeter Zijlstra  *
78735a566e6SPeter Zijlstra  * For the above NUMA topology that gives 3 levels:
78835a566e6SPeter Zijlstra  *
78935a566e6SPeter Zijlstra  * NUMA-2	0-3		0-3		0-3		0-3
79035a566e6SPeter Zijlstra  *  groups:	{0-1,3},{1-3}	{0-2},{0,2-3}	{1-3},{0-1,3}	{0,2-3},{0-2}
79135a566e6SPeter Zijlstra  *
79235a566e6SPeter Zijlstra  * NUMA-1	0-1,3		0-2		1-3		0,2-3
79335a566e6SPeter Zijlstra  *  groups:	{0},{1},{3}	{0},{1},{2}	{1},{2},{3}	{0},{2},{3}
79435a566e6SPeter Zijlstra  *
79535a566e6SPeter Zijlstra  * NUMA-0	0		1		2		3
79635a566e6SPeter Zijlstra  *
79735a566e6SPeter Zijlstra  *
79835a566e6SPeter Zijlstra  * As can be seen; things don't nicely line up as with the regular topology.
79935a566e6SPeter Zijlstra  * When we iterate a domain in child domain chunks some nodes can be
80035a566e6SPeter Zijlstra  * represented multiple times -- hence the "overlap" naming for this part of
80135a566e6SPeter Zijlstra  * the topology.
80235a566e6SPeter Zijlstra  *
80335a566e6SPeter Zijlstra  * In order to minimize this overlap, we only build enough groups to cover the
80435a566e6SPeter Zijlstra  * domain. For instance Node-0 NUMA-2 would only get groups: 0-1,3 and 1-3.
80535a566e6SPeter Zijlstra  *
80635a566e6SPeter Zijlstra  * Because:
80735a566e6SPeter Zijlstra  *
80835a566e6SPeter Zijlstra  *  - the first group of each domain is its child domain; this
80935a566e6SPeter Zijlstra  *    gets us the first 0-1,3
81035a566e6SPeter Zijlstra  *  - the only uncovered node is 2, who's child domain is 1-3.
81135a566e6SPeter Zijlstra  *
81235a566e6SPeter Zijlstra  * However, because of the overlap, computing a unique CPU for each group is
81335a566e6SPeter Zijlstra  * more complicated. Consider for instance the groups of NODE-1 NUMA-2, both
81435a566e6SPeter Zijlstra  * groups include the CPUs of Node-0, while those CPUs would not in fact ever
81535a566e6SPeter Zijlstra  * end up at those groups (they would end up in group: 0-1,3).
81635a566e6SPeter Zijlstra  *
817e5c14b1fSPeter Zijlstra  * To correct this we have to introduce the group balance mask. This mask
81835a566e6SPeter Zijlstra  * will contain those CPUs in the group that can reach this group given the
81935a566e6SPeter Zijlstra  * (child) domain tree.
82035a566e6SPeter Zijlstra  *
82135a566e6SPeter Zijlstra  * With this we can once again compute balance_cpu and sched_group_capacity
82235a566e6SPeter Zijlstra  * relations.
82335a566e6SPeter Zijlstra  *
82435a566e6SPeter Zijlstra  * XXX include words on how balance_cpu is unique and therefore can be
82535a566e6SPeter Zijlstra  * used for sched_group_capacity links.
82635a566e6SPeter Zijlstra  *
82735a566e6SPeter Zijlstra  *
82835a566e6SPeter Zijlstra  * Another 'interesting' topology is:
82935a566e6SPeter Zijlstra  *
83035a566e6SPeter Zijlstra  *   node   0   1   2   3
83135a566e6SPeter Zijlstra  *     0:  10  20  20  30
83235a566e6SPeter Zijlstra  *     1:  20  10  20  20
83335a566e6SPeter Zijlstra  *     2:  20  20  10  20
83435a566e6SPeter Zijlstra  *     3:  30  20  20  10
83535a566e6SPeter Zijlstra  *
83635a566e6SPeter Zijlstra  * Which looks a little like:
83735a566e6SPeter Zijlstra  *
83835a566e6SPeter Zijlstra  *   0 ----- 1
83935a566e6SPeter Zijlstra  *   |     / |
84035a566e6SPeter Zijlstra  *   |   /   |
84135a566e6SPeter Zijlstra  *   | /     |
84235a566e6SPeter Zijlstra  *   2 ----- 3
84335a566e6SPeter Zijlstra  *
84435a566e6SPeter Zijlstra  * This topology is asymmetric, nodes 1,2 are fully connected, but nodes 0,3
84535a566e6SPeter Zijlstra  * are not.
84635a566e6SPeter Zijlstra  *
84735a566e6SPeter Zijlstra  * This leads to a few particularly weird cases where the sched_domain's are
84897fb7a0aSIngo Molnar  * not of the same number for each CPU. Consider:
84935a566e6SPeter Zijlstra  *
85035a566e6SPeter Zijlstra  * NUMA-2	0-3						0-3
85135a566e6SPeter Zijlstra  *  groups:	{0-2},{1-3}					{1-3},{0-2}
85235a566e6SPeter Zijlstra  *
85335a566e6SPeter Zijlstra  * NUMA-1	0-2		0-3		0-3		1-3
85435a566e6SPeter Zijlstra  *
85535a566e6SPeter Zijlstra  * NUMA-0	0		1		2		3
85635a566e6SPeter Zijlstra  *
85735a566e6SPeter Zijlstra  */
85835a566e6SPeter Zijlstra 
85935a566e6SPeter Zijlstra 
86035a566e6SPeter Zijlstra /*
861e5c14b1fSPeter Zijlstra  * Build the balance mask; it contains only those CPUs that can arrive at this
862e5c14b1fSPeter Zijlstra  * group and should be considered to continue balancing.
86335a566e6SPeter Zijlstra  *
86435a566e6SPeter Zijlstra  * We do this during the group creation pass, therefore the group information
86535a566e6SPeter Zijlstra  * isn't complete yet, however since each group represents a (child) domain we
86635a566e6SPeter Zijlstra  * can fully construct this using the sched_domain bits (which are already
86735a566e6SPeter Zijlstra  * complete).
868f2cb1360SIngo Molnar  */
8691676330eSPeter Zijlstra static void
870e5c14b1fSPeter Zijlstra build_balance_mask(struct sched_domain *sd, struct sched_group *sg, struct cpumask *mask)
871f2cb1360SIngo Molnar {
872ae4df9d6SPeter Zijlstra 	const struct cpumask *sg_span = sched_group_span(sg);
873f2cb1360SIngo Molnar 	struct sd_data *sdd = sd->private;
874f2cb1360SIngo Molnar 	struct sched_domain *sibling;
875f2cb1360SIngo Molnar 	int i;
876f2cb1360SIngo Molnar 
8771676330eSPeter Zijlstra 	cpumask_clear(mask);
8781676330eSPeter Zijlstra 
879f32d782eSLauro Ramos Venancio 	for_each_cpu(i, sg_span) {
880f2cb1360SIngo Molnar 		sibling = *per_cpu_ptr(sdd->sd, i);
88173bb059fSPeter Zijlstra 
88273bb059fSPeter Zijlstra 		/*
88373bb059fSPeter Zijlstra 		 * Can happen in the asymmetric case, where these siblings are
88473bb059fSPeter Zijlstra 		 * unused. The mask will not be empty because those CPUs that
88573bb059fSPeter Zijlstra 		 * do have the top domain _should_ span the domain.
88673bb059fSPeter Zijlstra 		 */
88773bb059fSPeter Zijlstra 		if (!sibling->child)
88873bb059fSPeter Zijlstra 			continue;
88973bb059fSPeter Zijlstra 
89073bb059fSPeter Zijlstra 		/* If we would not end up here, we can't continue from here */
89173bb059fSPeter Zijlstra 		if (!cpumask_equal(sg_span, sched_domain_span(sibling->child)))
892f2cb1360SIngo Molnar 			continue;
893f2cb1360SIngo Molnar 
8941676330eSPeter Zijlstra 		cpumask_set_cpu(i, mask);
895f2cb1360SIngo Molnar 	}
89673bb059fSPeter Zijlstra 
89773bb059fSPeter Zijlstra 	/* We must not have empty masks here */
8981676330eSPeter Zijlstra 	WARN_ON_ONCE(cpumask_empty(mask));
899f2cb1360SIngo Molnar }
900f2cb1360SIngo Molnar 
901f2cb1360SIngo Molnar /*
90235a566e6SPeter Zijlstra  * XXX: This creates per-node group entries; since the load-balancer will
90335a566e6SPeter Zijlstra  * immediately access remote memory to construct this group's load-balance
90435a566e6SPeter Zijlstra  * statistics having the groups node local is of dubious benefit.
905f2cb1360SIngo Molnar  */
9068c033469SLauro Ramos Venancio static struct sched_group *
9078c033469SLauro Ramos Venancio build_group_from_child_sched_domain(struct sched_domain *sd, int cpu)
9088c033469SLauro Ramos Venancio {
9098c033469SLauro Ramos Venancio 	struct sched_group *sg;
9108c033469SLauro Ramos Venancio 	struct cpumask *sg_span;
9118c033469SLauro Ramos Venancio 
9128c033469SLauro Ramos Venancio 	sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
9138c033469SLauro Ramos Venancio 			GFP_KERNEL, cpu_to_node(cpu));
9148c033469SLauro Ramos Venancio 
9158c033469SLauro Ramos Venancio 	if (!sg)
9168c033469SLauro Ramos Venancio 		return NULL;
9178c033469SLauro Ramos Venancio 
918ae4df9d6SPeter Zijlstra 	sg_span = sched_group_span(sg);
9198c033469SLauro Ramos Venancio 	if (sd->child)
9208c033469SLauro Ramos Venancio 		cpumask_copy(sg_span, sched_domain_span(sd->child));
9218c033469SLauro Ramos Venancio 	else
9228c033469SLauro Ramos Venancio 		cpumask_copy(sg_span, sched_domain_span(sd));
9238c033469SLauro Ramos Venancio 
924213c5a45SShu Wang 	atomic_inc(&sg->ref);
9258c033469SLauro Ramos Venancio 	return sg;
9268c033469SLauro Ramos Venancio }
9278c033469SLauro Ramos Venancio 
9288c033469SLauro Ramos Venancio static void init_overlap_sched_group(struct sched_domain *sd,
9291676330eSPeter Zijlstra 				     struct sched_group *sg)
9308c033469SLauro Ramos Venancio {
9311676330eSPeter Zijlstra 	struct cpumask *mask = sched_domains_tmpmask2;
9328c033469SLauro Ramos Venancio 	struct sd_data *sdd = sd->private;
9338c033469SLauro Ramos Venancio 	struct cpumask *sg_span;
9341676330eSPeter Zijlstra 	int cpu;
9351676330eSPeter Zijlstra 
936e5c14b1fSPeter Zijlstra 	build_balance_mask(sd, sg, mask);
9370a2b65c0SBarry Song 	cpu = cpumask_first(mask);
9388c033469SLauro Ramos Venancio 
9398c033469SLauro Ramos Venancio 	sg->sgc = *per_cpu_ptr(sdd->sgc, cpu);
9408c033469SLauro Ramos Venancio 	if (atomic_inc_return(&sg->sgc->ref) == 1)
941e5c14b1fSPeter Zijlstra 		cpumask_copy(group_balance_mask(sg), mask);
94235a566e6SPeter Zijlstra 	else
943e5c14b1fSPeter Zijlstra 		WARN_ON_ONCE(!cpumask_equal(group_balance_mask(sg), mask));
9448c033469SLauro Ramos Venancio 
9458c033469SLauro Ramos Venancio 	/*
9468c033469SLauro Ramos Venancio 	 * Initialize sgc->capacity such that even if we mess up the
9478c033469SLauro Ramos Venancio 	 * domains and no possible iteration will get us here, we won't
9488c033469SLauro Ramos Venancio 	 * die on a /0 trap.
9498c033469SLauro Ramos Venancio 	 */
950ae4df9d6SPeter Zijlstra 	sg_span = sched_group_span(sg);
9518c033469SLauro Ramos Venancio 	sg->sgc->capacity = SCHED_CAPACITY_SCALE * cpumask_weight(sg_span);
9528c033469SLauro Ramos Venancio 	sg->sgc->min_capacity = SCHED_CAPACITY_SCALE;
953e3d6d0cbSMorten Rasmussen 	sg->sgc->max_capacity = SCHED_CAPACITY_SCALE;
9548c033469SLauro Ramos Venancio }
9558c033469SLauro Ramos Venancio 
956585b6d27SBarry Song static struct sched_domain *
957585b6d27SBarry Song find_descended_sibling(struct sched_domain *sd, struct sched_domain *sibling)
958585b6d27SBarry Song {
959585b6d27SBarry Song 	/*
960585b6d27SBarry Song 	 * The proper descendant would be the one whose child won't span out
961585b6d27SBarry Song 	 * of sd
962585b6d27SBarry Song 	 */
963585b6d27SBarry Song 	while (sibling->child &&
964585b6d27SBarry Song 	       !cpumask_subset(sched_domain_span(sibling->child),
965585b6d27SBarry Song 			       sched_domain_span(sd)))
966585b6d27SBarry Song 		sibling = sibling->child;
967585b6d27SBarry Song 
968585b6d27SBarry Song 	/*
969585b6d27SBarry Song 	 * As we are referencing sgc across different topology level, we need
970585b6d27SBarry Song 	 * to go down to skip those sched_domains which don't contribute to
971585b6d27SBarry Song 	 * scheduling because they will be degenerated in cpu_attach_domain
972585b6d27SBarry Song 	 */
973585b6d27SBarry Song 	while (sibling->child &&
974585b6d27SBarry Song 	       cpumask_equal(sched_domain_span(sibling->child),
975585b6d27SBarry Song 			     sched_domain_span(sibling)))
976585b6d27SBarry Song 		sibling = sibling->child;
977585b6d27SBarry Song 
978585b6d27SBarry Song 	return sibling;
979585b6d27SBarry Song }
980585b6d27SBarry Song 
981f2cb1360SIngo Molnar static int
982f2cb1360SIngo Molnar build_overlap_sched_groups(struct sched_domain *sd, int cpu)
983f2cb1360SIngo Molnar {
98491eaed0dSPeter Zijlstra 	struct sched_group *first = NULL, *last = NULL, *sg;
985f2cb1360SIngo Molnar 	const struct cpumask *span = sched_domain_span(sd);
986f2cb1360SIngo Molnar 	struct cpumask *covered = sched_domains_tmpmask;
987f2cb1360SIngo Molnar 	struct sd_data *sdd = sd->private;
988f2cb1360SIngo Molnar 	struct sched_domain *sibling;
989f2cb1360SIngo Molnar 	int i;
990f2cb1360SIngo Molnar 
991f2cb1360SIngo Molnar 	cpumask_clear(covered);
992f2cb1360SIngo Molnar 
9930372dd27SPeter Zijlstra 	for_each_cpu_wrap(i, span, cpu) {
994f2cb1360SIngo Molnar 		struct cpumask *sg_span;
995f2cb1360SIngo Molnar 
996f2cb1360SIngo Molnar 		if (cpumask_test_cpu(i, covered))
997f2cb1360SIngo Molnar 			continue;
998f2cb1360SIngo Molnar 
999f2cb1360SIngo Molnar 		sibling = *per_cpu_ptr(sdd->sd, i);
1000f2cb1360SIngo Molnar 
1001c20e1ea4SLauro Ramos Venancio 		/*
1002c20e1ea4SLauro Ramos Venancio 		 * Asymmetric node setups can result in situations where the
1003c20e1ea4SLauro Ramos Venancio 		 * domain tree is of unequal depth, make sure to skip domains
1004c20e1ea4SLauro Ramos Venancio 		 * that already cover the entire range.
1005c20e1ea4SLauro Ramos Venancio 		 *
1006c20e1ea4SLauro Ramos Venancio 		 * In that case build_sched_domains() will have terminated the
1007c20e1ea4SLauro Ramos Venancio 		 * iteration early and our sibling sd spans will be empty.
1008c20e1ea4SLauro Ramos Venancio 		 * Domains should always include the CPU they're built on, so
1009c20e1ea4SLauro Ramos Venancio 		 * check that.
1010c20e1ea4SLauro Ramos Venancio 		 */
1011f2cb1360SIngo Molnar 		if (!cpumask_test_cpu(i, sched_domain_span(sibling)))
1012f2cb1360SIngo Molnar 			continue;
1013f2cb1360SIngo Molnar 
1014585b6d27SBarry Song 		/*
1015585b6d27SBarry Song 		 * Usually we build sched_group by sibling's child sched_domain
1016585b6d27SBarry Song 		 * But for machines whose NUMA diameter are 3 or above, we move
1017585b6d27SBarry Song 		 * to build sched_group by sibling's proper descendant's child
1018585b6d27SBarry Song 		 * domain because sibling's child sched_domain will span out of
1019585b6d27SBarry Song 		 * the sched_domain being built as below.
1020585b6d27SBarry Song 		 *
1021585b6d27SBarry Song 		 * Smallest diameter=3 topology is:
1022585b6d27SBarry Song 		 *
1023585b6d27SBarry Song 		 *   node   0   1   2   3
1024585b6d27SBarry Song 		 *     0:  10  20  30  40
1025585b6d27SBarry Song 		 *     1:  20  10  20  30
1026585b6d27SBarry Song 		 *     2:  30  20  10  20
1027585b6d27SBarry Song 		 *     3:  40  30  20  10
1028585b6d27SBarry Song 		 *
1029585b6d27SBarry Song 		 *   0 --- 1 --- 2 --- 3
1030585b6d27SBarry Song 		 *
1031585b6d27SBarry Song 		 * NUMA-3       0-3             N/A             N/A             0-3
1032585b6d27SBarry Song 		 *  groups:     {0-2},{1-3}                                     {1-3},{0-2}
1033585b6d27SBarry Song 		 *
1034585b6d27SBarry Song 		 * NUMA-2       0-2             0-3             0-3             1-3
1035585b6d27SBarry Song 		 *  groups:     {0-1},{1-3}     {0-2},{2-3}     {1-3},{0-1}     {2-3},{0-2}
1036585b6d27SBarry Song 		 *
1037585b6d27SBarry Song 		 * NUMA-1       0-1             0-2             1-3             2-3
1038585b6d27SBarry Song 		 *  groups:     {0},{1}         {1},{2},{0}     {2},{3},{1}     {3},{2}
1039585b6d27SBarry Song 		 *
1040585b6d27SBarry Song 		 * NUMA-0       0               1               2               3
1041585b6d27SBarry Song 		 *
1042585b6d27SBarry Song 		 * The NUMA-2 groups for nodes 0 and 3 are obviously buggered, as the
1043585b6d27SBarry Song 		 * group span isn't a subset of the domain span.
1044585b6d27SBarry Song 		 */
1045585b6d27SBarry Song 		if (sibling->child &&
1046585b6d27SBarry Song 		    !cpumask_subset(sched_domain_span(sibling->child), span))
1047585b6d27SBarry Song 			sibling = find_descended_sibling(sd, sibling);
1048585b6d27SBarry Song 
10498c033469SLauro Ramos Venancio 		sg = build_group_from_child_sched_domain(sibling, cpu);
1050f2cb1360SIngo Molnar 		if (!sg)
1051f2cb1360SIngo Molnar 			goto fail;
1052f2cb1360SIngo Molnar 
1053ae4df9d6SPeter Zijlstra 		sg_span = sched_group_span(sg);
1054f2cb1360SIngo Molnar 		cpumask_or(covered, covered, sg_span);
1055f2cb1360SIngo Molnar 
1056585b6d27SBarry Song 		init_overlap_sched_group(sibling, sg);
1057f2cb1360SIngo Molnar 
1058f2cb1360SIngo Molnar 		if (!first)
1059f2cb1360SIngo Molnar 			first = sg;
1060f2cb1360SIngo Molnar 		if (last)
1061f2cb1360SIngo Molnar 			last->next = sg;
1062f2cb1360SIngo Molnar 		last = sg;
1063f2cb1360SIngo Molnar 		last->next = first;
1064f2cb1360SIngo Molnar 	}
106591eaed0dSPeter Zijlstra 	sd->groups = first;
1066f2cb1360SIngo Molnar 
1067f2cb1360SIngo Molnar 	return 0;
1068f2cb1360SIngo Molnar 
1069f2cb1360SIngo Molnar fail:
1070f2cb1360SIngo Molnar 	free_sched_groups(first, 0);
1071f2cb1360SIngo Molnar 
1072f2cb1360SIngo Molnar 	return -ENOMEM;
1073f2cb1360SIngo Molnar }
1074f2cb1360SIngo Molnar 
107535a566e6SPeter Zijlstra 
107635a566e6SPeter Zijlstra /*
107735a566e6SPeter Zijlstra  * Package topology (also see the load-balance blurb in fair.c)
107835a566e6SPeter Zijlstra  *
107935a566e6SPeter Zijlstra  * The scheduler builds a tree structure to represent a number of important
108035a566e6SPeter Zijlstra  * topology features. By default (default_topology[]) these include:
108135a566e6SPeter Zijlstra  *
108235a566e6SPeter Zijlstra  *  - Simultaneous multithreading (SMT)
108335a566e6SPeter Zijlstra  *  - Multi-Core Cache (MC)
108435a566e6SPeter Zijlstra  *  - Package (DIE)
108535a566e6SPeter Zijlstra  *
108635a566e6SPeter Zijlstra  * Where the last one more or less denotes everything up to a NUMA node.
108735a566e6SPeter Zijlstra  *
108835a566e6SPeter Zijlstra  * The tree consists of 3 primary data structures:
108935a566e6SPeter Zijlstra  *
109035a566e6SPeter Zijlstra  *	sched_domain -> sched_group -> sched_group_capacity
109135a566e6SPeter Zijlstra  *	    ^ ^             ^ ^
109235a566e6SPeter Zijlstra  *          `-'             `-'
109335a566e6SPeter Zijlstra  *
109497fb7a0aSIngo Molnar  * The sched_domains are per-CPU and have a two way link (parent & child) and
109535a566e6SPeter Zijlstra  * denote the ever growing mask of CPUs belonging to that level of topology.
109635a566e6SPeter Zijlstra  *
109735a566e6SPeter Zijlstra  * Each sched_domain has a circular (double) linked list of sched_group's, each
109835a566e6SPeter Zijlstra  * denoting the domains of the level below (or individual CPUs in case of the
109935a566e6SPeter Zijlstra  * first domain level). The sched_group linked by a sched_domain includes the
110035a566e6SPeter Zijlstra  * CPU of that sched_domain [*].
110135a566e6SPeter Zijlstra  *
110235a566e6SPeter Zijlstra  * Take for instance a 2 threaded, 2 core, 2 cache cluster part:
110335a566e6SPeter Zijlstra  *
110435a566e6SPeter Zijlstra  * CPU   0   1   2   3   4   5   6   7
110535a566e6SPeter Zijlstra  *
110635a566e6SPeter Zijlstra  * DIE  [                             ]
110735a566e6SPeter Zijlstra  * MC   [             ] [             ]
110835a566e6SPeter Zijlstra  * SMT  [     ] [     ] [     ] [     ]
110935a566e6SPeter Zijlstra  *
111035a566e6SPeter Zijlstra  *  - or -
111135a566e6SPeter Zijlstra  *
111235a566e6SPeter Zijlstra  * DIE  0-7 0-7 0-7 0-7 0-7 0-7 0-7 0-7
111335a566e6SPeter Zijlstra  * MC	0-3 0-3 0-3 0-3 4-7 4-7 4-7 4-7
111435a566e6SPeter Zijlstra  * SMT  0-1 0-1 2-3 2-3 4-5 4-5 6-7 6-7
111535a566e6SPeter Zijlstra  *
111635a566e6SPeter Zijlstra  * CPU   0   1   2   3   4   5   6   7
111735a566e6SPeter Zijlstra  *
111835a566e6SPeter Zijlstra  * One way to think about it is: sched_domain moves you up and down among these
111935a566e6SPeter Zijlstra  * topology levels, while sched_group moves you sideways through it, at child
112035a566e6SPeter Zijlstra  * domain granularity.
112135a566e6SPeter Zijlstra  *
112235a566e6SPeter Zijlstra  * sched_group_capacity ensures each unique sched_group has shared storage.
112335a566e6SPeter Zijlstra  *
112435a566e6SPeter Zijlstra  * There are two related construction problems, both require a CPU that
112535a566e6SPeter Zijlstra  * uniquely identify each group (for a given domain):
112635a566e6SPeter Zijlstra  *
112735a566e6SPeter Zijlstra  *  - The first is the balance_cpu (see should_we_balance() and the
112835a566e6SPeter Zijlstra  *    load-balance blub in fair.c); for each group we only want 1 CPU to
112935a566e6SPeter Zijlstra  *    continue balancing at a higher domain.
113035a566e6SPeter Zijlstra  *
113135a566e6SPeter Zijlstra  *  - The second is the sched_group_capacity; we want all identical groups
113235a566e6SPeter Zijlstra  *    to share a single sched_group_capacity.
113335a566e6SPeter Zijlstra  *
113435a566e6SPeter Zijlstra  * Since these topologies are exclusive by construction. That is, its
113535a566e6SPeter Zijlstra  * impossible for an SMT thread to belong to multiple cores, and cores to
113635a566e6SPeter Zijlstra  * be part of multiple caches. There is a very clear and unique location
113735a566e6SPeter Zijlstra  * for each CPU in the hierarchy.
113835a566e6SPeter Zijlstra  *
113935a566e6SPeter Zijlstra  * Therefore computing a unique CPU for each group is trivial (the iteration
114035a566e6SPeter Zijlstra  * mask is redundant and set all 1s; all CPUs in a group will end up at _that_
114135a566e6SPeter Zijlstra  * group), we can simply pick the first CPU in each group.
114235a566e6SPeter Zijlstra  *
114335a566e6SPeter Zijlstra  *
114435a566e6SPeter Zijlstra  * [*] in other words, the first group of each domain is its child domain.
114535a566e6SPeter Zijlstra  */
114635a566e6SPeter Zijlstra 
11470c0e776aSPeter Zijlstra static struct sched_group *get_group(int cpu, struct sd_data *sdd)
1148f2cb1360SIngo Molnar {
1149f2cb1360SIngo Molnar 	struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu);
1150f2cb1360SIngo Molnar 	struct sched_domain *child = sd->child;
11510c0e776aSPeter Zijlstra 	struct sched_group *sg;
115267d4f6ffSValentin Schneider 	bool already_visited;
1153f2cb1360SIngo Molnar 
1154f2cb1360SIngo Molnar 	if (child)
1155f2cb1360SIngo Molnar 		cpu = cpumask_first(sched_domain_span(child));
1156f2cb1360SIngo Molnar 
11570c0e776aSPeter Zijlstra 	sg = *per_cpu_ptr(sdd->sg, cpu);
11580c0e776aSPeter Zijlstra 	sg->sgc = *per_cpu_ptr(sdd->sgc, cpu);
1159f2cb1360SIngo Molnar 
116067d4f6ffSValentin Schneider 	/* Increase refcounts for claim_allocations: */
116167d4f6ffSValentin Schneider 	already_visited = atomic_inc_return(&sg->ref) > 1;
116267d4f6ffSValentin Schneider 	/* sgc visits should follow a similar trend as sg */
116367d4f6ffSValentin Schneider 	WARN_ON(already_visited != (atomic_inc_return(&sg->sgc->ref) > 1));
116467d4f6ffSValentin Schneider 
116567d4f6ffSValentin Schneider 	/* If we have already visited that group, it's already initialized. */
116667d4f6ffSValentin Schneider 	if (already_visited)
116767d4f6ffSValentin Schneider 		return sg;
11680c0e776aSPeter Zijlstra 
11690c0e776aSPeter Zijlstra 	if (child) {
1170ae4df9d6SPeter Zijlstra 		cpumask_copy(sched_group_span(sg), sched_domain_span(child));
1171ae4df9d6SPeter Zijlstra 		cpumask_copy(group_balance_mask(sg), sched_group_span(sg));
11720c0e776aSPeter Zijlstra 	} else {
1173ae4df9d6SPeter Zijlstra 		cpumask_set_cpu(cpu, sched_group_span(sg));
1174e5c14b1fSPeter Zijlstra 		cpumask_set_cpu(cpu, group_balance_mask(sg));
1175f2cb1360SIngo Molnar 	}
1176f2cb1360SIngo Molnar 
1177ae4df9d6SPeter Zijlstra 	sg->sgc->capacity = SCHED_CAPACITY_SCALE * cpumask_weight(sched_group_span(sg));
11780c0e776aSPeter Zijlstra 	sg->sgc->min_capacity = SCHED_CAPACITY_SCALE;
1179e3d6d0cbSMorten Rasmussen 	sg->sgc->max_capacity = SCHED_CAPACITY_SCALE;
11800c0e776aSPeter Zijlstra 
11810c0e776aSPeter Zijlstra 	return sg;
1182f2cb1360SIngo Molnar }
1183f2cb1360SIngo Molnar 
1184f2cb1360SIngo Molnar /*
1185f2cb1360SIngo Molnar  * build_sched_groups will build a circular linked list of the groups
1186d8743230SValentin Schneider  * covered by the given span, will set each group's ->cpumask correctly,
1187d8743230SValentin Schneider  * and will initialize their ->sgc.
1188f2cb1360SIngo Molnar  *
1189f2cb1360SIngo Molnar  * Assumes the sched_domain tree is fully constructed
1190f2cb1360SIngo Molnar  */
1191f2cb1360SIngo Molnar static int
1192f2cb1360SIngo Molnar build_sched_groups(struct sched_domain *sd, int cpu)
1193f2cb1360SIngo Molnar {
1194f2cb1360SIngo Molnar 	struct sched_group *first = NULL, *last = NULL;
1195f2cb1360SIngo Molnar 	struct sd_data *sdd = sd->private;
1196f2cb1360SIngo Molnar 	const struct cpumask *span = sched_domain_span(sd);
1197f2cb1360SIngo Molnar 	struct cpumask *covered;
1198f2cb1360SIngo Molnar 	int i;
1199f2cb1360SIngo Molnar 
1200f2cb1360SIngo Molnar 	lockdep_assert_held(&sched_domains_mutex);
1201f2cb1360SIngo Molnar 	covered = sched_domains_tmpmask;
1202f2cb1360SIngo Molnar 
1203f2cb1360SIngo Molnar 	cpumask_clear(covered);
1204f2cb1360SIngo Molnar 
12050c0e776aSPeter Zijlstra 	for_each_cpu_wrap(i, span, cpu) {
1206f2cb1360SIngo Molnar 		struct sched_group *sg;
1207f2cb1360SIngo Molnar 
1208f2cb1360SIngo Molnar 		if (cpumask_test_cpu(i, covered))
1209f2cb1360SIngo Molnar 			continue;
1210f2cb1360SIngo Molnar 
12110c0e776aSPeter Zijlstra 		sg = get_group(i, sdd);
1212f2cb1360SIngo Molnar 
1213ae4df9d6SPeter Zijlstra 		cpumask_or(covered, covered, sched_group_span(sg));
1214f2cb1360SIngo Molnar 
1215f2cb1360SIngo Molnar 		if (!first)
1216f2cb1360SIngo Molnar 			first = sg;
1217f2cb1360SIngo Molnar 		if (last)
1218f2cb1360SIngo Molnar 			last->next = sg;
1219f2cb1360SIngo Molnar 		last = sg;
1220f2cb1360SIngo Molnar 	}
1221f2cb1360SIngo Molnar 	last->next = first;
12220c0e776aSPeter Zijlstra 	sd->groups = first;
1223f2cb1360SIngo Molnar 
1224f2cb1360SIngo Molnar 	return 0;
1225f2cb1360SIngo Molnar }
1226f2cb1360SIngo Molnar 
1227f2cb1360SIngo Molnar /*
1228f2cb1360SIngo Molnar  * Initialize sched groups cpu_capacity.
1229f2cb1360SIngo Molnar  *
1230f2cb1360SIngo Molnar  * cpu_capacity indicates the capacity of sched group, which is used while
1231f2cb1360SIngo Molnar  * distributing the load between different sched groups in a sched domain.
1232f2cb1360SIngo Molnar  * Typically cpu_capacity for all the groups in a sched domain will be same
1233f2cb1360SIngo Molnar  * unless there are asymmetries in the topology. If there are asymmetries,
1234f2cb1360SIngo Molnar  * group having more cpu_capacity will pickup more load compared to the
1235f2cb1360SIngo Molnar  * group having less cpu_capacity.
1236f2cb1360SIngo Molnar  */
1237f2cb1360SIngo Molnar static void init_sched_groups_capacity(int cpu, struct sched_domain *sd)
1238f2cb1360SIngo Molnar {
1239f2cb1360SIngo Molnar 	struct sched_group *sg = sd->groups;
1240f2cb1360SIngo Molnar 
1241f2cb1360SIngo Molnar 	WARN_ON(!sg);
1242f2cb1360SIngo Molnar 
1243f2cb1360SIngo Molnar 	do {
1244f2cb1360SIngo Molnar 		int cpu, max_cpu = -1;
1245f2cb1360SIngo Molnar 
1246ae4df9d6SPeter Zijlstra 		sg->group_weight = cpumask_weight(sched_group_span(sg));
1247f2cb1360SIngo Molnar 
1248f2cb1360SIngo Molnar 		if (!(sd->flags & SD_ASYM_PACKING))
1249f2cb1360SIngo Molnar 			goto next;
1250f2cb1360SIngo Molnar 
1251ae4df9d6SPeter Zijlstra 		for_each_cpu(cpu, sched_group_span(sg)) {
1252f2cb1360SIngo Molnar 			if (max_cpu < 0)
1253f2cb1360SIngo Molnar 				max_cpu = cpu;
1254f2cb1360SIngo Molnar 			else if (sched_asym_prefer(cpu, max_cpu))
1255f2cb1360SIngo Molnar 				max_cpu = cpu;
1256f2cb1360SIngo Molnar 		}
1257f2cb1360SIngo Molnar 		sg->asym_prefer_cpu = max_cpu;
1258f2cb1360SIngo Molnar 
1259f2cb1360SIngo Molnar next:
1260f2cb1360SIngo Molnar 		sg = sg->next;
1261f2cb1360SIngo Molnar 	} while (sg != sd->groups);
1262f2cb1360SIngo Molnar 
1263f2cb1360SIngo Molnar 	if (cpu != group_balance_cpu(sg))
1264f2cb1360SIngo Molnar 		return;
1265f2cb1360SIngo Molnar 
1266f2cb1360SIngo Molnar 	update_group_capacity(sd, cpu);
1267f2cb1360SIngo Molnar }
1268f2cb1360SIngo Molnar 
1269f2cb1360SIngo Molnar /*
1270f2cb1360SIngo Molnar  * Initializers for schedule domains
1271f2cb1360SIngo Molnar  * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
1272f2cb1360SIngo Molnar  */
1273f2cb1360SIngo Molnar 
1274f2cb1360SIngo Molnar static int default_relax_domain_level = -1;
1275f2cb1360SIngo Molnar int sched_domain_level_max;
1276f2cb1360SIngo Molnar 
1277f2cb1360SIngo Molnar static int __init setup_relax_domain_level(char *str)
1278f2cb1360SIngo Molnar {
1279f2cb1360SIngo Molnar 	if (kstrtoint(str, 0, &default_relax_domain_level))
1280f2cb1360SIngo Molnar 		pr_warn("Unable to set relax_domain_level\n");
1281f2cb1360SIngo Molnar 
1282f2cb1360SIngo Molnar 	return 1;
1283f2cb1360SIngo Molnar }
1284f2cb1360SIngo Molnar __setup("relax_domain_level=", setup_relax_domain_level);
1285f2cb1360SIngo Molnar 
1286f2cb1360SIngo Molnar static void set_domain_attribute(struct sched_domain *sd,
1287f2cb1360SIngo Molnar 				 struct sched_domain_attr *attr)
1288f2cb1360SIngo Molnar {
1289f2cb1360SIngo Molnar 	int request;
1290f2cb1360SIngo Molnar 
1291f2cb1360SIngo Molnar 	if (!attr || attr->relax_domain_level < 0) {
1292f2cb1360SIngo Molnar 		if (default_relax_domain_level < 0)
1293f2cb1360SIngo Molnar 			return;
1294f2cb1360SIngo Molnar 		request = default_relax_domain_level;
1295f2cb1360SIngo Molnar 	} else
1296f2cb1360SIngo Molnar 		request = attr->relax_domain_level;
12979ae7ab20SValentin Schneider 
12989ae7ab20SValentin Schneider 	if (sd->level > request) {
1299f2cb1360SIngo Molnar 		/* Turn off idle balance on this domain: */
1300f2cb1360SIngo Molnar 		sd->flags &= ~(SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
1301f2cb1360SIngo Molnar 	}
1302f2cb1360SIngo Molnar }
1303f2cb1360SIngo Molnar 
1304f2cb1360SIngo Molnar static void __sdt_free(const struct cpumask *cpu_map);
1305f2cb1360SIngo Molnar static int __sdt_alloc(const struct cpumask *cpu_map);
1306f2cb1360SIngo Molnar 
1307f2cb1360SIngo Molnar static void __free_domain_allocs(struct s_data *d, enum s_alloc what,
1308f2cb1360SIngo Molnar 				 const struct cpumask *cpu_map)
1309f2cb1360SIngo Molnar {
1310f2cb1360SIngo Molnar 	switch (what) {
1311f2cb1360SIngo Molnar 	case sa_rootdomain:
1312f2cb1360SIngo Molnar 		if (!atomic_read(&d->rd->refcount))
1313f2cb1360SIngo Molnar 			free_rootdomain(&d->rd->rcu);
1314df561f66SGustavo A. R. Silva 		fallthrough;
1315f2cb1360SIngo Molnar 	case sa_sd:
1316f2cb1360SIngo Molnar 		free_percpu(d->sd);
1317df561f66SGustavo A. R. Silva 		fallthrough;
1318f2cb1360SIngo Molnar 	case sa_sd_storage:
1319f2cb1360SIngo Molnar 		__sdt_free(cpu_map);
1320df561f66SGustavo A. R. Silva 		fallthrough;
1321f2cb1360SIngo Molnar 	case sa_none:
1322f2cb1360SIngo Molnar 		break;
1323f2cb1360SIngo Molnar 	}
1324f2cb1360SIngo Molnar }
1325f2cb1360SIngo Molnar 
1326f2cb1360SIngo Molnar static enum s_alloc
1327f2cb1360SIngo Molnar __visit_domain_allocation_hell(struct s_data *d, const struct cpumask *cpu_map)
1328f2cb1360SIngo Molnar {
1329f2cb1360SIngo Molnar 	memset(d, 0, sizeof(*d));
1330f2cb1360SIngo Molnar 
1331f2cb1360SIngo Molnar 	if (__sdt_alloc(cpu_map))
1332f2cb1360SIngo Molnar 		return sa_sd_storage;
1333f2cb1360SIngo Molnar 	d->sd = alloc_percpu(struct sched_domain *);
1334f2cb1360SIngo Molnar 	if (!d->sd)
1335f2cb1360SIngo Molnar 		return sa_sd_storage;
1336f2cb1360SIngo Molnar 	d->rd = alloc_rootdomain();
1337f2cb1360SIngo Molnar 	if (!d->rd)
1338f2cb1360SIngo Molnar 		return sa_sd;
133997fb7a0aSIngo Molnar 
1340f2cb1360SIngo Molnar 	return sa_rootdomain;
1341f2cb1360SIngo Molnar }
1342f2cb1360SIngo Molnar 
1343f2cb1360SIngo Molnar /*
1344f2cb1360SIngo Molnar  * NULL the sd_data elements we've used to build the sched_domain and
1345f2cb1360SIngo Molnar  * sched_group structure so that the subsequent __free_domain_allocs()
1346f2cb1360SIngo Molnar  * will not free the data we're using.
1347f2cb1360SIngo Molnar  */
1348f2cb1360SIngo Molnar static void claim_allocations(int cpu, struct sched_domain *sd)
1349f2cb1360SIngo Molnar {
1350f2cb1360SIngo Molnar 	struct sd_data *sdd = sd->private;
1351f2cb1360SIngo Molnar 
1352f2cb1360SIngo Molnar 	WARN_ON_ONCE(*per_cpu_ptr(sdd->sd, cpu) != sd);
1353f2cb1360SIngo Molnar 	*per_cpu_ptr(sdd->sd, cpu) = NULL;
1354f2cb1360SIngo Molnar 
1355f2cb1360SIngo Molnar 	if (atomic_read(&(*per_cpu_ptr(sdd->sds, cpu))->ref))
1356f2cb1360SIngo Molnar 		*per_cpu_ptr(sdd->sds, cpu) = NULL;
1357f2cb1360SIngo Molnar 
1358f2cb1360SIngo Molnar 	if (atomic_read(&(*per_cpu_ptr(sdd->sg, cpu))->ref))
1359f2cb1360SIngo Molnar 		*per_cpu_ptr(sdd->sg, cpu) = NULL;
1360f2cb1360SIngo Molnar 
1361f2cb1360SIngo Molnar 	if (atomic_read(&(*per_cpu_ptr(sdd->sgc, cpu))->ref))
1362f2cb1360SIngo Molnar 		*per_cpu_ptr(sdd->sgc, cpu) = NULL;
1363f2cb1360SIngo Molnar }
1364f2cb1360SIngo Molnar 
1365f2cb1360SIngo Molnar #ifdef CONFIG_NUMA
1366f2cb1360SIngo Molnar enum numa_topology_type sched_numa_topology_type;
136797fb7a0aSIngo Molnar 
136897fb7a0aSIngo Molnar static int			sched_domains_numa_levels;
1369f2cb1360SIngo Molnar static int			sched_domains_curr_level;
137097fb7a0aSIngo Molnar 
137197fb7a0aSIngo Molnar int				sched_max_numa_distance;
137297fb7a0aSIngo Molnar static int			*sched_domains_numa_distance;
137397fb7a0aSIngo Molnar static struct cpumask		***sched_domains_numa_masks;
1374a55c7454SMatt Fleming int __read_mostly		node_reclaim_distance = RECLAIM_DISTANCE;
1375f2cb1360SIngo Molnar #endif
1376f2cb1360SIngo Molnar 
1377f2cb1360SIngo Molnar /*
1378f2cb1360SIngo Molnar  * SD_flags allowed in topology descriptions.
1379f2cb1360SIngo Molnar  *
1380f2cb1360SIngo Molnar  * These flags are purely descriptive of the topology and do not prescribe
1381f2cb1360SIngo Molnar  * behaviour. Behaviour is artificial and mapped in the below sd_init()
1382f2cb1360SIngo Molnar  * function:
1383f2cb1360SIngo Molnar  *
1384f2cb1360SIngo Molnar  *   SD_SHARE_CPUCAPACITY   - describes SMT topologies
1385f2cb1360SIngo Molnar  *   SD_SHARE_PKG_RESOURCES - describes shared caches
1386f2cb1360SIngo Molnar  *   SD_NUMA                - describes NUMA topologies
1387f2cb1360SIngo Molnar  *
1388f2cb1360SIngo Molnar  * Odd one out, which beside describing the topology has a quirk also
1389f2cb1360SIngo Molnar  * prescribes the desired behaviour that goes along with it:
1390f2cb1360SIngo Molnar  *
1391f2cb1360SIngo Molnar  *   SD_ASYM_PACKING        - describes SMT quirks
1392f2cb1360SIngo Molnar  */
1393f2cb1360SIngo Molnar #define TOPOLOGY_SD_FLAGS		\
1394f2cb1360SIngo Molnar 	(SD_SHARE_CPUCAPACITY	|	\
1395f2cb1360SIngo Molnar 	 SD_SHARE_PKG_RESOURCES |	\
1396f2cb1360SIngo Molnar 	 SD_NUMA		|	\
1397cfe7ddcbSValentin Schneider 	 SD_ASYM_PACKING)
1398f2cb1360SIngo Molnar 
1399f2cb1360SIngo Molnar static struct sched_domain *
1400f2cb1360SIngo Molnar sd_init(struct sched_domain_topology_level *tl,
1401f2cb1360SIngo Molnar 	const struct cpumask *cpu_map,
140205484e09SMorten Rasmussen 	struct sched_domain *child, int dflags, int cpu)
1403f2cb1360SIngo Molnar {
1404f2cb1360SIngo Molnar 	struct sd_data *sdd = &tl->data;
1405f2cb1360SIngo Molnar 	struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu);
1406f2cb1360SIngo Molnar 	int sd_id, sd_weight, sd_flags = 0;
1407f2cb1360SIngo Molnar 
1408f2cb1360SIngo Molnar #ifdef CONFIG_NUMA
1409f2cb1360SIngo Molnar 	/*
1410f2cb1360SIngo Molnar 	 * Ugly hack to pass state to sd_numa_mask()...
1411f2cb1360SIngo Molnar 	 */
1412f2cb1360SIngo Molnar 	sched_domains_curr_level = tl->numa_level;
1413f2cb1360SIngo Molnar #endif
1414f2cb1360SIngo Molnar 
1415f2cb1360SIngo Molnar 	sd_weight = cpumask_weight(tl->mask(cpu));
1416f2cb1360SIngo Molnar 
1417f2cb1360SIngo Molnar 	if (tl->sd_flags)
1418f2cb1360SIngo Molnar 		sd_flags = (*tl->sd_flags)();
1419f2cb1360SIngo Molnar 	if (WARN_ONCE(sd_flags & ~TOPOLOGY_SD_FLAGS,
1420f2cb1360SIngo Molnar 			"wrong sd_flags in topology description\n"))
14219b1b234bSPeng Liu 		sd_flags &= TOPOLOGY_SD_FLAGS;
1422f2cb1360SIngo Molnar 
142305484e09SMorten Rasmussen 	/* Apply detected topology flags */
142405484e09SMorten Rasmussen 	sd_flags |= dflags;
142505484e09SMorten Rasmussen 
1426f2cb1360SIngo Molnar 	*sd = (struct sched_domain){
1427f2cb1360SIngo Molnar 		.min_interval		= sd_weight,
1428f2cb1360SIngo Molnar 		.max_interval		= 2*sd_weight,
14296e749913SVincent Guittot 		.busy_factor		= 16,
14302208cdaaSVincent Guittot 		.imbalance_pct		= 117,
1431f2cb1360SIngo Molnar 
1432f2cb1360SIngo Molnar 		.cache_nice_tries	= 0,
1433f2cb1360SIngo Molnar 
143436c5bdc4SValentin Schneider 		.flags			= 1*SD_BALANCE_NEWIDLE
1435f2cb1360SIngo Molnar 					| 1*SD_BALANCE_EXEC
1436f2cb1360SIngo Molnar 					| 1*SD_BALANCE_FORK
1437f2cb1360SIngo Molnar 					| 0*SD_BALANCE_WAKE
1438f2cb1360SIngo Molnar 					| 1*SD_WAKE_AFFINE
1439f2cb1360SIngo Molnar 					| 0*SD_SHARE_CPUCAPACITY
1440f2cb1360SIngo Molnar 					| 0*SD_SHARE_PKG_RESOURCES
1441f2cb1360SIngo Molnar 					| 0*SD_SERIALIZE
14429c63e84dSMorten Rasmussen 					| 1*SD_PREFER_SIBLING
1443f2cb1360SIngo Molnar 					| 0*SD_NUMA
1444f2cb1360SIngo Molnar 					| sd_flags
1445f2cb1360SIngo Molnar 					,
1446f2cb1360SIngo Molnar 
1447f2cb1360SIngo Molnar 		.last_balance		= jiffies,
1448f2cb1360SIngo Molnar 		.balance_interval	= sd_weight,
1449f2cb1360SIngo Molnar 		.max_newidle_lb_cost	= 0,
1450f2cb1360SIngo Molnar 		.next_decay_max_lb_cost	= jiffies,
1451f2cb1360SIngo Molnar 		.child			= child,
1452f2cb1360SIngo Molnar #ifdef CONFIG_SCHED_DEBUG
1453f2cb1360SIngo Molnar 		.name			= tl->name,
1454f2cb1360SIngo Molnar #endif
1455f2cb1360SIngo Molnar 	};
1456f2cb1360SIngo Molnar 
1457f2cb1360SIngo Molnar 	cpumask_and(sched_domain_span(sd), cpu_map, tl->mask(cpu));
1458f2cb1360SIngo Molnar 	sd_id = cpumask_first(sched_domain_span(sd));
1459f2cb1360SIngo Molnar 
1460f2cb1360SIngo Molnar 	/*
1461f2cb1360SIngo Molnar 	 * Convert topological properties into behaviour.
1462f2cb1360SIngo Molnar 	 */
1463f2cb1360SIngo Molnar 
1464a526d466SMorten Rasmussen 	/* Don't attempt to spread across CPUs of different capacities. */
1465a526d466SMorten Rasmussen 	if ((sd->flags & SD_ASYM_CPUCAPACITY) && sd->child)
14669c63e84dSMorten Rasmussen 		sd->child->flags &= ~SD_PREFER_SIBLING;
14679c63e84dSMorten Rasmussen 
1468f2cb1360SIngo Molnar 	if (sd->flags & SD_SHARE_CPUCAPACITY) {
1469f2cb1360SIngo Molnar 		sd->imbalance_pct = 110;
1470f2cb1360SIngo Molnar 
1471f2cb1360SIngo Molnar 	} else if (sd->flags & SD_SHARE_PKG_RESOURCES) {
1472f2cb1360SIngo Molnar 		sd->imbalance_pct = 117;
1473f2cb1360SIngo Molnar 		sd->cache_nice_tries = 1;
1474f2cb1360SIngo Molnar 
1475f2cb1360SIngo Molnar #ifdef CONFIG_NUMA
1476f2cb1360SIngo Molnar 	} else if (sd->flags & SD_NUMA) {
1477f2cb1360SIngo Molnar 		sd->cache_nice_tries = 2;
1478f2cb1360SIngo Molnar 
14799c63e84dSMorten Rasmussen 		sd->flags &= ~SD_PREFER_SIBLING;
1480f2cb1360SIngo Molnar 		sd->flags |= SD_SERIALIZE;
1481a55c7454SMatt Fleming 		if (sched_domains_numa_distance[tl->numa_level] > node_reclaim_distance) {
1482f2cb1360SIngo Molnar 			sd->flags &= ~(SD_BALANCE_EXEC |
1483f2cb1360SIngo Molnar 				       SD_BALANCE_FORK |
1484f2cb1360SIngo Molnar 				       SD_WAKE_AFFINE);
1485f2cb1360SIngo Molnar 		}
1486f2cb1360SIngo Molnar 
1487f2cb1360SIngo Molnar #endif
1488f2cb1360SIngo Molnar 	} else {
1489f2cb1360SIngo Molnar 		sd->cache_nice_tries = 1;
1490f2cb1360SIngo Molnar 	}
1491f2cb1360SIngo Molnar 
1492f2cb1360SIngo Molnar 	/*
1493f2cb1360SIngo Molnar 	 * For all levels sharing cache; connect a sched_domain_shared
1494f2cb1360SIngo Molnar 	 * instance.
1495f2cb1360SIngo Molnar 	 */
1496f2cb1360SIngo Molnar 	if (sd->flags & SD_SHARE_PKG_RESOURCES) {
1497f2cb1360SIngo Molnar 		sd->shared = *per_cpu_ptr(sdd->sds, sd_id);
1498f2cb1360SIngo Molnar 		atomic_inc(&sd->shared->ref);
1499f2cb1360SIngo Molnar 		atomic_set(&sd->shared->nr_busy_cpus, sd_weight);
1500f2cb1360SIngo Molnar 	}
1501f2cb1360SIngo Molnar 
1502f2cb1360SIngo Molnar 	sd->private = sdd;
1503f2cb1360SIngo Molnar 
1504f2cb1360SIngo Molnar 	return sd;
1505f2cb1360SIngo Molnar }
1506f2cb1360SIngo Molnar 
1507f2cb1360SIngo Molnar /*
1508f2cb1360SIngo Molnar  * Topology list, bottom-up.
1509f2cb1360SIngo Molnar  */
1510f2cb1360SIngo Molnar static struct sched_domain_topology_level default_topology[] = {
1511f2cb1360SIngo Molnar #ifdef CONFIG_SCHED_SMT
1512f2cb1360SIngo Molnar 	{ cpu_smt_mask, cpu_smt_flags, SD_INIT_NAME(SMT) },
1513f2cb1360SIngo Molnar #endif
1514f2cb1360SIngo Molnar #ifdef CONFIG_SCHED_MC
1515f2cb1360SIngo Molnar 	{ cpu_coregroup_mask, cpu_core_flags, SD_INIT_NAME(MC) },
1516f2cb1360SIngo Molnar #endif
1517f2cb1360SIngo Molnar 	{ cpu_cpu_mask, SD_INIT_NAME(DIE) },
1518f2cb1360SIngo Molnar 	{ NULL, },
1519f2cb1360SIngo Molnar };
1520f2cb1360SIngo Molnar 
1521f2cb1360SIngo Molnar static struct sched_domain_topology_level *sched_domain_topology =
1522f2cb1360SIngo Molnar 	default_topology;
1523f2cb1360SIngo Molnar 
1524f2cb1360SIngo Molnar #define for_each_sd_topology(tl)			\
1525f2cb1360SIngo Molnar 	for (tl = sched_domain_topology; tl->mask; tl++)
1526f2cb1360SIngo Molnar 
1527f2cb1360SIngo Molnar void set_sched_topology(struct sched_domain_topology_level *tl)
1528f2cb1360SIngo Molnar {
1529f2cb1360SIngo Molnar 	if (WARN_ON_ONCE(sched_smp_initialized))
1530f2cb1360SIngo Molnar 		return;
1531f2cb1360SIngo Molnar 
1532f2cb1360SIngo Molnar 	sched_domain_topology = tl;
1533f2cb1360SIngo Molnar }
1534f2cb1360SIngo Molnar 
1535f2cb1360SIngo Molnar #ifdef CONFIG_NUMA
1536f2cb1360SIngo Molnar 
1537f2cb1360SIngo Molnar static const struct cpumask *sd_numa_mask(int cpu)
1538f2cb1360SIngo Molnar {
1539f2cb1360SIngo Molnar 	return sched_domains_numa_masks[sched_domains_curr_level][cpu_to_node(cpu)];
1540f2cb1360SIngo Molnar }
1541f2cb1360SIngo Molnar 
1542f2cb1360SIngo Molnar static void sched_numa_warn(const char *str)
1543f2cb1360SIngo Molnar {
1544f2cb1360SIngo Molnar 	static int done = false;
1545f2cb1360SIngo Molnar 	int i,j;
1546f2cb1360SIngo Molnar 
1547f2cb1360SIngo Molnar 	if (done)
1548f2cb1360SIngo Molnar 		return;
1549f2cb1360SIngo Molnar 
1550f2cb1360SIngo Molnar 	done = true;
1551f2cb1360SIngo Molnar 
1552f2cb1360SIngo Molnar 	printk(KERN_WARNING "ERROR: %s\n\n", str);
1553f2cb1360SIngo Molnar 
1554f2cb1360SIngo Molnar 	for (i = 0; i < nr_node_ids; i++) {
1555f2cb1360SIngo Molnar 		printk(KERN_WARNING "  ");
1556f2cb1360SIngo Molnar 		for (j = 0; j < nr_node_ids; j++)
1557f2cb1360SIngo Molnar 			printk(KERN_CONT "%02d ", node_distance(i,j));
1558f2cb1360SIngo Molnar 		printk(KERN_CONT "\n");
1559f2cb1360SIngo Molnar 	}
1560f2cb1360SIngo Molnar 	printk(KERN_WARNING "\n");
1561f2cb1360SIngo Molnar }
1562f2cb1360SIngo Molnar 
1563f2cb1360SIngo Molnar bool find_numa_distance(int distance)
1564f2cb1360SIngo Molnar {
1565f2cb1360SIngo Molnar 	int i;
1566f2cb1360SIngo Molnar 
1567f2cb1360SIngo Molnar 	if (distance == node_distance(0, 0))
1568f2cb1360SIngo Molnar 		return true;
1569f2cb1360SIngo Molnar 
1570f2cb1360SIngo Molnar 	for (i = 0; i < sched_domains_numa_levels; i++) {
1571f2cb1360SIngo Molnar 		if (sched_domains_numa_distance[i] == distance)
1572f2cb1360SIngo Molnar 			return true;
1573f2cb1360SIngo Molnar 	}
1574f2cb1360SIngo Molnar 
1575f2cb1360SIngo Molnar 	return false;
1576f2cb1360SIngo Molnar }
1577f2cb1360SIngo Molnar 
1578f2cb1360SIngo Molnar /*
1579f2cb1360SIngo Molnar  * A system can have three types of NUMA topology:
1580f2cb1360SIngo Molnar  * NUMA_DIRECT: all nodes are directly connected, or not a NUMA system
1581f2cb1360SIngo Molnar  * NUMA_GLUELESS_MESH: some nodes reachable through intermediary nodes
1582f2cb1360SIngo Molnar  * NUMA_BACKPLANE: nodes can reach other nodes through a backplane
1583f2cb1360SIngo Molnar  *
1584f2cb1360SIngo Molnar  * The difference between a glueless mesh topology and a backplane
1585f2cb1360SIngo Molnar  * topology lies in whether communication between not directly
1586f2cb1360SIngo Molnar  * connected nodes goes through intermediary nodes (where programs
1587f2cb1360SIngo Molnar  * could run), or through backplane controllers. This affects
1588f2cb1360SIngo Molnar  * placement of programs.
1589f2cb1360SIngo Molnar  *
1590f2cb1360SIngo Molnar  * The type of topology can be discerned with the following tests:
1591f2cb1360SIngo Molnar  * - If the maximum distance between any nodes is 1 hop, the system
1592f2cb1360SIngo Molnar  *   is directly connected.
1593f2cb1360SIngo Molnar  * - If for two nodes A and B, located N > 1 hops away from each other,
1594f2cb1360SIngo Molnar  *   there is an intermediary node C, which is < N hops away from both
1595f2cb1360SIngo Molnar  *   nodes A and B, the system is a glueless mesh.
1596f2cb1360SIngo Molnar  */
1597f2cb1360SIngo Molnar static void init_numa_topology_type(void)
1598f2cb1360SIngo Molnar {
1599f2cb1360SIngo Molnar 	int a, b, c, n;
1600f2cb1360SIngo Molnar 
1601f2cb1360SIngo Molnar 	n = sched_max_numa_distance;
1602f2cb1360SIngo Molnar 
1603e5e96fafSSrikar Dronamraju 	if (sched_domains_numa_levels <= 2) {
1604f2cb1360SIngo Molnar 		sched_numa_topology_type = NUMA_DIRECT;
1605f2cb1360SIngo Molnar 		return;
1606f2cb1360SIngo Molnar 	}
1607f2cb1360SIngo Molnar 
1608f2cb1360SIngo Molnar 	for_each_online_node(a) {
1609f2cb1360SIngo Molnar 		for_each_online_node(b) {
1610f2cb1360SIngo Molnar 			/* Find two nodes furthest removed from each other. */
1611f2cb1360SIngo Molnar 			if (node_distance(a, b) < n)
1612f2cb1360SIngo Molnar 				continue;
1613f2cb1360SIngo Molnar 
1614f2cb1360SIngo Molnar 			/* Is there an intermediary node between a and b? */
1615f2cb1360SIngo Molnar 			for_each_online_node(c) {
1616f2cb1360SIngo Molnar 				if (node_distance(a, c) < n &&
1617f2cb1360SIngo Molnar 				    node_distance(b, c) < n) {
1618f2cb1360SIngo Molnar 					sched_numa_topology_type =
1619f2cb1360SIngo Molnar 							NUMA_GLUELESS_MESH;
1620f2cb1360SIngo Molnar 					return;
1621f2cb1360SIngo Molnar 				}
1622f2cb1360SIngo Molnar 			}
1623f2cb1360SIngo Molnar 
1624f2cb1360SIngo Molnar 			sched_numa_topology_type = NUMA_BACKPLANE;
1625f2cb1360SIngo Molnar 			return;
1626f2cb1360SIngo Molnar 		}
1627f2cb1360SIngo Molnar 	}
1628f2cb1360SIngo Molnar }
1629f2cb1360SIngo Molnar 
1630620a6dc4SValentin Schneider 
1631620a6dc4SValentin Schneider #define NR_DISTANCE_VALUES (1 << DISTANCE_BITS)
1632620a6dc4SValentin Schneider 
1633f2cb1360SIngo Molnar void sched_init_numa(void)
1634f2cb1360SIngo Molnar {
1635f2cb1360SIngo Molnar 	struct sched_domain_topology_level *tl;
1636620a6dc4SValentin Schneider 	unsigned long *distance_map;
1637620a6dc4SValentin Schneider 	int nr_levels = 0;
1638620a6dc4SValentin Schneider 	int i, j;
1639051f3ca0SSuravee Suthikulpanit 
1640f2cb1360SIngo Molnar 	/*
1641f2cb1360SIngo Molnar 	 * O(nr_nodes^2) deduplicating selection sort -- in order to find the
1642f2cb1360SIngo Molnar 	 * unique distances in the node_distance() table.
1643f2cb1360SIngo Molnar 	 */
1644620a6dc4SValentin Schneider 	distance_map = bitmap_alloc(NR_DISTANCE_VALUES, GFP_KERNEL);
1645620a6dc4SValentin Schneider 	if (!distance_map)
1646620a6dc4SValentin Schneider 		return;
1647620a6dc4SValentin Schneider 
1648620a6dc4SValentin Schneider 	bitmap_zero(distance_map, NR_DISTANCE_VALUES);
1649f2cb1360SIngo Molnar 	for (i = 0; i < nr_node_ids; i++) {
1650f2cb1360SIngo Molnar 		for (j = 0; j < nr_node_ids; j++) {
1651620a6dc4SValentin Schneider 			int distance = node_distance(i, j);
1652f2cb1360SIngo Molnar 
1653620a6dc4SValentin Schneider 			if (distance < LOCAL_DISTANCE || distance >= NR_DISTANCE_VALUES) {
1654620a6dc4SValentin Schneider 				sched_numa_warn("Invalid distance value range");
1655620a6dc4SValentin Schneider 				return;
1656620a6dc4SValentin Schneider 			}
1657f2cb1360SIngo Molnar 
1658620a6dc4SValentin Schneider 			bitmap_set(distance_map, distance, 1);
1659620a6dc4SValentin Schneider 		}
1660620a6dc4SValentin Schneider 	}
1661f2cb1360SIngo Molnar 	/*
1662620a6dc4SValentin Schneider 	 * We can now figure out how many unique distance values there are and
1663620a6dc4SValentin Schneider 	 * allocate memory accordingly.
1664f2cb1360SIngo Molnar 	 */
1665620a6dc4SValentin Schneider 	nr_levels = bitmap_weight(distance_map, NR_DISTANCE_VALUES);
1666f2cb1360SIngo Molnar 
1667620a6dc4SValentin Schneider 	sched_domains_numa_distance = kcalloc(nr_levels, sizeof(int), GFP_KERNEL);
1668620a6dc4SValentin Schneider 	if (!sched_domains_numa_distance) {
1669620a6dc4SValentin Schneider 		bitmap_free(distance_map);
1670620a6dc4SValentin Schneider 		return;
1671f2cb1360SIngo Molnar 	}
1672620a6dc4SValentin Schneider 
1673620a6dc4SValentin Schneider 	for (i = 0, j = 0; i < nr_levels; i++, j++) {
1674620a6dc4SValentin Schneider 		j = find_next_bit(distance_map, NR_DISTANCE_VALUES, j);
1675620a6dc4SValentin Schneider 		sched_domains_numa_distance[i] = j;
1676f2cb1360SIngo Molnar 	}
1677f2cb1360SIngo Molnar 
1678620a6dc4SValentin Schneider 	bitmap_free(distance_map);
1679620a6dc4SValentin Schneider 
1680f2cb1360SIngo Molnar 	/*
1681620a6dc4SValentin Schneider 	 * 'nr_levels' contains the number of unique distances
1682f2cb1360SIngo Molnar 	 *
1683f2cb1360SIngo Molnar 	 * The sched_domains_numa_distance[] array includes the actual distance
1684f2cb1360SIngo Molnar 	 * numbers.
1685f2cb1360SIngo Molnar 	 */
1686f2cb1360SIngo Molnar 
1687f2cb1360SIngo Molnar 	/*
1688f2cb1360SIngo Molnar 	 * Here, we should temporarily reset sched_domains_numa_levels to 0.
1689f2cb1360SIngo Molnar 	 * If it fails to allocate memory for array sched_domains_numa_masks[][],
1690620a6dc4SValentin Schneider 	 * the array will contain less then 'nr_levels' members. This could be
1691f2cb1360SIngo Molnar 	 * dangerous when we use it to iterate array sched_domains_numa_masks[][]
1692f2cb1360SIngo Molnar 	 * in other functions.
1693f2cb1360SIngo Molnar 	 *
1694620a6dc4SValentin Schneider 	 * We reset it to 'nr_levels' at the end of this function.
1695f2cb1360SIngo Molnar 	 */
1696f2cb1360SIngo Molnar 	sched_domains_numa_levels = 0;
1697f2cb1360SIngo Molnar 
1698620a6dc4SValentin Schneider 	sched_domains_numa_masks = kzalloc(sizeof(void *) * nr_levels, GFP_KERNEL);
1699f2cb1360SIngo Molnar 	if (!sched_domains_numa_masks)
1700f2cb1360SIngo Molnar 		return;
1701f2cb1360SIngo Molnar 
1702f2cb1360SIngo Molnar 	/*
1703f2cb1360SIngo Molnar 	 * Now for each level, construct a mask per node which contains all
1704f2cb1360SIngo Molnar 	 * CPUs of nodes that are that many hops away from us.
1705f2cb1360SIngo Molnar 	 */
1706620a6dc4SValentin Schneider 	for (i = 0; i < nr_levels; i++) {
1707f2cb1360SIngo Molnar 		sched_domains_numa_masks[i] =
1708f2cb1360SIngo Molnar 			kzalloc(nr_node_ids * sizeof(void *), GFP_KERNEL);
1709f2cb1360SIngo Molnar 		if (!sched_domains_numa_masks[i])
1710f2cb1360SIngo Molnar 			return;
1711f2cb1360SIngo Molnar 
1712f2cb1360SIngo Molnar 		for (j = 0; j < nr_node_ids; j++) {
1713f2cb1360SIngo Molnar 			struct cpumask *mask = kzalloc(cpumask_size(), GFP_KERNEL);
1714620a6dc4SValentin Schneider 			int k;
1715620a6dc4SValentin Schneider 
1716f2cb1360SIngo Molnar 			if (!mask)
1717f2cb1360SIngo Molnar 				return;
1718f2cb1360SIngo Molnar 
1719f2cb1360SIngo Molnar 			sched_domains_numa_masks[i][j] = mask;
1720f2cb1360SIngo Molnar 
1721f2cb1360SIngo Molnar 			for_each_node(k) {
1722620a6dc4SValentin Schneider 				if (sched_debug() && (node_distance(j, k) != node_distance(k, j)))
1723620a6dc4SValentin Schneider 					sched_numa_warn("Node-distance not symmetric");
1724620a6dc4SValentin Schneider 
1725f2cb1360SIngo Molnar 				if (node_distance(j, k) > sched_domains_numa_distance[i])
1726f2cb1360SIngo Molnar 					continue;
1727f2cb1360SIngo Molnar 
1728f2cb1360SIngo Molnar 				cpumask_or(mask, mask, cpumask_of_node(k));
1729f2cb1360SIngo Molnar 			}
1730f2cb1360SIngo Molnar 		}
1731f2cb1360SIngo Molnar 	}
1732f2cb1360SIngo Molnar 
1733f2cb1360SIngo Molnar 	/* Compute default topology size */
1734f2cb1360SIngo Molnar 	for (i = 0; sched_domain_topology[i].mask; i++);
1735f2cb1360SIngo Molnar 
173671e5f664SDietmar Eggemann 	tl = kzalloc((i + nr_levels + 1) *
1737f2cb1360SIngo Molnar 			sizeof(struct sched_domain_topology_level), GFP_KERNEL);
1738f2cb1360SIngo Molnar 	if (!tl)
1739f2cb1360SIngo Molnar 		return;
1740f2cb1360SIngo Molnar 
1741f2cb1360SIngo Molnar 	/*
1742f2cb1360SIngo Molnar 	 * Copy the default topology bits..
1743f2cb1360SIngo Molnar 	 */
1744f2cb1360SIngo Molnar 	for (i = 0; sched_domain_topology[i].mask; i++)
1745f2cb1360SIngo Molnar 		tl[i] = sched_domain_topology[i];
1746f2cb1360SIngo Molnar 
1747f2cb1360SIngo Molnar 	/*
1748051f3ca0SSuravee Suthikulpanit 	 * Add the NUMA identity distance, aka single NODE.
1749051f3ca0SSuravee Suthikulpanit 	 */
1750051f3ca0SSuravee Suthikulpanit 	tl[i++] = (struct sched_domain_topology_level){
1751051f3ca0SSuravee Suthikulpanit 		.mask = sd_numa_mask,
1752051f3ca0SSuravee Suthikulpanit 		.numa_level = 0,
1753051f3ca0SSuravee Suthikulpanit 		SD_INIT_NAME(NODE)
1754051f3ca0SSuravee Suthikulpanit 	};
1755051f3ca0SSuravee Suthikulpanit 
1756051f3ca0SSuravee Suthikulpanit 	/*
1757f2cb1360SIngo Molnar 	 * .. and append 'j' levels of NUMA goodness.
1758f2cb1360SIngo Molnar 	 */
1759620a6dc4SValentin Schneider 	for (j = 1; j < nr_levels; i++, j++) {
1760f2cb1360SIngo Molnar 		tl[i] = (struct sched_domain_topology_level){
1761f2cb1360SIngo Molnar 			.mask = sd_numa_mask,
1762f2cb1360SIngo Molnar 			.sd_flags = cpu_numa_flags,
1763f2cb1360SIngo Molnar 			.flags = SDTL_OVERLAP,
1764f2cb1360SIngo Molnar 			.numa_level = j,
1765f2cb1360SIngo Molnar 			SD_INIT_NAME(NUMA)
1766f2cb1360SIngo Molnar 		};
1767f2cb1360SIngo Molnar 	}
1768f2cb1360SIngo Molnar 
1769f2cb1360SIngo Molnar 	sched_domain_topology = tl;
1770f2cb1360SIngo Molnar 
1771620a6dc4SValentin Schneider 	sched_domains_numa_levels = nr_levels;
1772620a6dc4SValentin Schneider 	sched_max_numa_distance = sched_domains_numa_distance[nr_levels - 1];
1773f2cb1360SIngo Molnar 
1774f2cb1360SIngo Molnar 	init_numa_topology_type();
1775f2cb1360SIngo Molnar }
1776f2cb1360SIngo Molnar 
1777f2cb1360SIngo Molnar void sched_domains_numa_masks_set(unsigned int cpu)
1778f2cb1360SIngo Molnar {
1779f2cb1360SIngo Molnar 	int node = cpu_to_node(cpu);
1780f2cb1360SIngo Molnar 	int i, j;
1781f2cb1360SIngo Molnar 
1782f2cb1360SIngo Molnar 	for (i = 0; i < sched_domains_numa_levels; i++) {
1783f2cb1360SIngo Molnar 		for (j = 0; j < nr_node_ids; j++) {
1784f2cb1360SIngo Molnar 			if (node_distance(j, node) <= sched_domains_numa_distance[i])
1785f2cb1360SIngo Molnar 				cpumask_set_cpu(cpu, sched_domains_numa_masks[i][j]);
1786f2cb1360SIngo Molnar 		}
1787f2cb1360SIngo Molnar 	}
1788f2cb1360SIngo Molnar }
1789f2cb1360SIngo Molnar 
1790f2cb1360SIngo Molnar void sched_domains_numa_masks_clear(unsigned int cpu)
1791f2cb1360SIngo Molnar {
1792f2cb1360SIngo Molnar 	int i, j;
1793f2cb1360SIngo Molnar 
1794f2cb1360SIngo Molnar 	for (i = 0; i < sched_domains_numa_levels; i++) {
1795f2cb1360SIngo Molnar 		for (j = 0; j < nr_node_ids; j++)
1796f2cb1360SIngo Molnar 			cpumask_clear_cpu(cpu, sched_domains_numa_masks[i][j]);
1797f2cb1360SIngo Molnar 	}
1798f2cb1360SIngo Molnar }
1799f2cb1360SIngo Molnar 
1800e0e8d491SWanpeng Li /*
1801e0e8d491SWanpeng Li  * sched_numa_find_closest() - given the NUMA topology, find the cpu
1802e0e8d491SWanpeng Li  *                             closest to @cpu from @cpumask.
1803e0e8d491SWanpeng Li  * cpumask: cpumask to find a cpu from
1804e0e8d491SWanpeng Li  * cpu: cpu to be close to
1805e0e8d491SWanpeng Li  *
1806e0e8d491SWanpeng Li  * returns: cpu, or nr_cpu_ids when nothing found.
1807e0e8d491SWanpeng Li  */
1808e0e8d491SWanpeng Li int sched_numa_find_closest(const struct cpumask *cpus, int cpu)
1809e0e8d491SWanpeng Li {
1810e0e8d491SWanpeng Li 	int i, j = cpu_to_node(cpu);
1811e0e8d491SWanpeng Li 
1812e0e8d491SWanpeng Li 	for (i = 0; i < sched_domains_numa_levels; i++) {
1813e0e8d491SWanpeng Li 		cpu = cpumask_any_and(cpus, sched_domains_numa_masks[i][j]);
1814e0e8d491SWanpeng Li 		if (cpu < nr_cpu_ids)
1815e0e8d491SWanpeng Li 			return cpu;
1816e0e8d491SWanpeng Li 	}
1817e0e8d491SWanpeng Li 	return nr_cpu_ids;
1818e0e8d491SWanpeng Li }
1819e0e8d491SWanpeng Li 
1820f2cb1360SIngo Molnar #endif /* CONFIG_NUMA */
1821f2cb1360SIngo Molnar 
1822f2cb1360SIngo Molnar static int __sdt_alloc(const struct cpumask *cpu_map)
1823f2cb1360SIngo Molnar {
1824f2cb1360SIngo Molnar 	struct sched_domain_topology_level *tl;
1825f2cb1360SIngo Molnar 	int j;
1826f2cb1360SIngo Molnar 
1827f2cb1360SIngo Molnar 	for_each_sd_topology(tl) {
1828f2cb1360SIngo Molnar 		struct sd_data *sdd = &tl->data;
1829f2cb1360SIngo Molnar 
1830f2cb1360SIngo Molnar 		sdd->sd = alloc_percpu(struct sched_domain *);
1831f2cb1360SIngo Molnar 		if (!sdd->sd)
1832f2cb1360SIngo Molnar 			return -ENOMEM;
1833f2cb1360SIngo Molnar 
1834f2cb1360SIngo Molnar 		sdd->sds = alloc_percpu(struct sched_domain_shared *);
1835f2cb1360SIngo Molnar 		if (!sdd->sds)
1836f2cb1360SIngo Molnar 			return -ENOMEM;
1837f2cb1360SIngo Molnar 
1838f2cb1360SIngo Molnar 		sdd->sg = alloc_percpu(struct sched_group *);
1839f2cb1360SIngo Molnar 		if (!sdd->sg)
1840f2cb1360SIngo Molnar 			return -ENOMEM;
1841f2cb1360SIngo Molnar 
1842f2cb1360SIngo Molnar 		sdd->sgc = alloc_percpu(struct sched_group_capacity *);
1843f2cb1360SIngo Molnar 		if (!sdd->sgc)
1844f2cb1360SIngo Molnar 			return -ENOMEM;
1845f2cb1360SIngo Molnar 
1846f2cb1360SIngo Molnar 		for_each_cpu(j, cpu_map) {
1847f2cb1360SIngo Molnar 			struct sched_domain *sd;
1848f2cb1360SIngo Molnar 			struct sched_domain_shared *sds;
1849f2cb1360SIngo Molnar 			struct sched_group *sg;
1850f2cb1360SIngo Molnar 			struct sched_group_capacity *sgc;
1851f2cb1360SIngo Molnar 
1852f2cb1360SIngo Molnar 			sd = kzalloc_node(sizeof(struct sched_domain) + cpumask_size(),
1853f2cb1360SIngo Molnar 					GFP_KERNEL, cpu_to_node(j));
1854f2cb1360SIngo Molnar 			if (!sd)
1855f2cb1360SIngo Molnar 				return -ENOMEM;
1856f2cb1360SIngo Molnar 
1857f2cb1360SIngo Molnar 			*per_cpu_ptr(sdd->sd, j) = sd;
1858f2cb1360SIngo Molnar 
1859f2cb1360SIngo Molnar 			sds = kzalloc_node(sizeof(struct sched_domain_shared),
1860f2cb1360SIngo Molnar 					GFP_KERNEL, cpu_to_node(j));
1861f2cb1360SIngo Molnar 			if (!sds)
1862f2cb1360SIngo Molnar 				return -ENOMEM;
1863f2cb1360SIngo Molnar 
1864f2cb1360SIngo Molnar 			*per_cpu_ptr(sdd->sds, j) = sds;
1865f2cb1360SIngo Molnar 
1866f2cb1360SIngo Molnar 			sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
1867f2cb1360SIngo Molnar 					GFP_KERNEL, cpu_to_node(j));
1868f2cb1360SIngo Molnar 			if (!sg)
1869f2cb1360SIngo Molnar 				return -ENOMEM;
1870f2cb1360SIngo Molnar 
1871f2cb1360SIngo Molnar 			sg->next = sg;
1872f2cb1360SIngo Molnar 
1873f2cb1360SIngo Molnar 			*per_cpu_ptr(sdd->sg, j) = sg;
1874f2cb1360SIngo Molnar 
1875f2cb1360SIngo Molnar 			sgc = kzalloc_node(sizeof(struct sched_group_capacity) + cpumask_size(),
1876f2cb1360SIngo Molnar 					GFP_KERNEL, cpu_to_node(j));
1877f2cb1360SIngo Molnar 			if (!sgc)
1878f2cb1360SIngo Molnar 				return -ENOMEM;
1879f2cb1360SIngo Molnar 
1880005f874dSPeter Zijlstra #ifdef CONFIG_SCHED_DEBUG
1881005f874dSPeter Zijlstra 			sgc->id = j;
1882005f874dSPeter Zijlstra #endif
1883005f874dSPeter Zijlstra 
1884f2cb1360SIngo Molnar 			*per_cpu_ptr(sdd->sgc, j) = sgc;
1885f2cb1360SIngo Molnar 		}
1886f2cb1360SIngo Molnar 	}
1887f2cb1360SIngo Molnar 
1888f2cb1360SIngo Molnar 	return 0;
1889f2cb1360SIngo Molnar }
1890f2cb1360SIngo Molnar 
1891f2cb1360SIngo Molnar static void __sdt_free(const struct cpumask *cpu_map)
1892f2cb1360SIngo Molnar {
1893f2cb1360SIngo Molnar 	struct sched_domain_topology_level *tl;
1894f2cb1360SIngo Molnar 	int j;
1895f2cb1360SIngo Molnar 
1896f2cb1360SIngo Molnar 	for_each_sd_topology(tl) {
1897f2cb1360SIngo Molnar 		struct sd_data *sdd = &tl->data;
1898f2cb1360SIngo Molnar 
1899f2cb1360SIngo Molnar 		for_each_cpu(j, cpu_map) {
1900f2cb1360SIngo Molnar 			struct sched_domain *sd;
1901f2cb1360SIngo Molnar 
1902f2cb1360SIngo Molnar 			if (sdd->sd) {
1903f2cb1360SIngo Molnar 				sd = *per_cpu_ptr(sdd->sd, j);
1904f2cb1360SIngo Molnar 				if (sd && (sd->flags & SD_OVERLAP))
1905f2cb1360SIngo Molnar 					free_sched_groups(sd->groups, 0);
1906f2cb1360SIngo Molnar 				kfree(*per_cpu_ptr(sdd->sd, j));
1907f2cb1360SIngo Molnar 			}
1908f2cb1360SIngo Molnar 
1909f2cb1360SIngo Molnar 			if (sdd->sds)
1910f2cb1360SIngo Molnar 				kfree(*per_cpu_ptr(sdd->sds, j));
1911f2cb1360SIngo Molnar 			if (sdd->sg)
1912f2cb1360SIngo Molnar 				kfree(*per_cpu_ptr(sdd->sg, j));
1913f2cb1360SIngo Molnar 			if (sdd->sgc)
1914f2cb1360SIngo Molnar 				kfree(*per_cpu_ptr(sdd->sgc, j));
1915f2cb1360SIngo Molnar 		}
1916f2cb1360SIngo Molnar 		free_percpu(sdd->sd);
1917f2cb1360SIngo Molnar 		sdd->sd = NULL;
1918f2cb1360SIngo Molnar 		free_percpu(sdd->sds);
1919f2cb1360SIngo Molnar 		sdd->sds = NULL;
1920f2cb1360SIngo Molnar 		free_percpu(sdd->sg);
1921f2cb1360SIngo Molnar 		sdd->sg = NULL;
1922f2cb1360SIngo Molnar 		free_percpu(sdd->sgc);
1923f2cb1360SIngo Molnar 		sdd->sgc = NULL;
1924f2cb1360SIngo Molnar 	}
1925f2cb1360SIngo Molnar }
1926f2cb1360SIngo Molnar 
1927181a80d1SViresh Kumar static struct sched_domain *build_sched_domain(struct sched_domain_topology_level *tl,
1928f2cb1360SIngo Molnar 		const struct cpumask *cpu_map, struct sched_domain_attr *attr,
192905484e09SMorten Rasmussen 		struct sched_domain *child, int dflags, int cpu)
1930f2cb1360SIngo Molnar {
193105484e09SMorten Rasmussen 	struct sched_domain *sd = sd_init(tl, cpu_map, child, dflags, cpu);
1932f2cb1360SIngo Molnar 
1933f2cb1360SIngo Molnar 	if (child) {
1934f2cb1360SIngo Molnar 		sd->level = child->level + 1;
1935f2cb1360SIngo Molnar 		sched_domain_level_max = max(sched_domain_level_max, sd->level);
1936f2cb1360SIngo Molnar 		child->parent = sd;
1937f2cb1360SIngo Molnar 
1938f2cb1360SIngo Molnar 		if (!cpumask_subset(sched_domain_span(child),
1939f2cb1360SIngo Molnar 				    sched_domain_span(sd))) {
1940f2cb1360SIngo Molnar 			pr_err("BUG: arch topology borken\n");
1941f2cb1360SIngo Molnar #ifdef CONFIG_SCHED_DEBUG
1942f2cb1360SIngo Molnar 			pr_err("     the %s domain not a subset of the %s domain\n",
1943f2cb1360SIngo Molnar 					child->name, sd->name);
1944f2cb1360SIngo Molnar #endif
194597fb7a0aSIngo Molnar 			/* Fixup, ensure @sd has at least @child CPUs. */
1946f2cb1360SIngo Molnar 			cpumask_or(sched_domain_span(sd),
1947f2cb1360SIngo Molnar 				   sched_domain_span(sd),
1948f2cb1360SIngo Molnar 				   sched_domain_span(child));
1949f2cb1360SIngo Molnar 		}
1950f2cb1360SIngo Molnar 
1951f2cb1360SIngo Molnar 	}
1952f2cb1360SIngo Molnar 	set_domain_attribute(sd, attr);
1953f2cb1360SIngo Molnar 
1954f2cb1360SIngo Molnar 	return sd;
1955f2cb1360SIngo Molnar }
1956f2cb1360SIngo Molnar 
1957f2cb1360SIngo Molnar /*
1958ccf74128SValentin Schneider  * Ensure topology masks are sane, i.e. there are no conflicts (overlaps) for
1959ccf74128SValentin Schneider  * any two given CPUs at this (non-NUMA) topology level.
1960ccf74128SValentin Schneider  */
1961ccf74128SValentin Schneider static bool topology_span_sane(struct sched_domain_topology_level *tl,
1962ccf74128SValentin Schneider 			      const struct cpumask *cpu_map, int cpu)
1963ccf74128SValentin Schneider {
1964ccf74128SValentin Schneider 	int i;
1965ccf74128SValentin Schneider 
1966ccf74128SValentin Schneider 	/* NUMA levels are allowed to overlap */
1967ccf74128SValentin Schneider 	if (tl->flags & SDTL_OVERLAP)
1968ccf74128SValentin Schneider 		return true;
1969ccf74128SValentin Schneider 
1970ccf74128SValentin Schneider 	/*
1971ccf74128SValentin Schneider 	 * Non-NUMA levels cannot partially overlap - they must be either
1972ccf74128SValentin Schneider 	 * completely equal or completely disjoint. Otherwise we can end up
1973ccf74128SValentin Schneider 	 * breaking the sched_group lists - i.e. a later get_group() pass
1974ccf74128SValentin Schneider 	 * breaks the linking done for an earlier span.
1975ccf74128SValentin Schneider 	 */
1976ccf74128SValentin Schneider 	for_each_cpu(i, cpu_map) {
1977ccf74128SValentin Schneider 		if (i == cpu)
1978ccf74128SValentin Schneider 			continue;
1979ccf74128SValentin Schneider 		/*
1980ccf74128SValentin Schneider 		 * We should 'and' all those masks with 'cpu_map' to exactly
1981ccf74128SValentin Schneider 		 * match the topology we're about to build, but that can only
1982ccf74128SValentin Schneider 		 * remove CPUs, which only lessens our ability to detect
1983ccf74128SValentin Schneider 		 * overlaps
1984ccf74128SValentin Schneider 		 */
1985ccf74128SValentin Schneider 		if (!cpumask_equal(tl->mask(cpu), tl->mask(i)) &&
1986ccf74128SValentin Schneider 		    cpumask_intersects(tl->mask(cpu), tl->mask(i)))
1987ccf74128SValentin Schneider 			return false;
1988ccf74128SValentin Schneider 	}
1989ccf74128SValentin Schneider 
1990ccf74128SValentin Schneider 	return true;
1991ccf74128SValentin Schneider }
1992ccf74128SValentin Schneider 
1993ccf74128SValentin Schneider /*
199405484e09SMorten Rasmussen  * Find the sched_domain_topology_level where all CPU capacities are visible
199505484e09SMorten Rasmussen  * for all CPUs.
199605484e09SMorten Rasmussen  */
199705484e09SMorten Rasmussen static struct sched_domain_topology_level
199805484e09SMorten Rasmussen *asym_cpu_capacity_level(const struct cpumask *cpu_map)
199905484e09SMorten Rasmussen {
200005484e09SMorten Rasmussen 	int i, j, asym_level = 0;
200105484e09SMorten Rasmussen 	bool asym = false;
200205484e09SMorten Rasmussen 	struct sched_domain_topology_level *tl, *asym_tl = NULL;
200305484e09SMorten Rasmussen 	unsigned long cap;
200405484e09SMorten Rasmussen 
200505484e09SMorten Rasmussen 	/* Is there any asymmetry? */
20068ec59c0fSVincent Guittot 	cap = arch_scale_cpu_capacity(cpumask_first(cpu_map));
200705484e09SMorten Rasmussen 
200805484e09SMorten Rasmussen 	for_each_cpu(i, cpu_map) {
20098ec59c0fSVincent Guittot 		if (arch_scale_cpu_capacity(i) != cap) {
201005484e09SMorten Rasmussen 			asym = true;
201105484e09SMorten Rasmussen 			break;
201205484e09SMorten Rasmussen 		}
201305484e09SMorten Rasmussen 	}
201405484e09SMorten Rasmussen 
201505484e09SMorten Rasmussen 	if (!asym)
201605484e09SMorten Rasmussen 		return NULL;
201705484e09SMorten Rasmussen 
201805484e09SMorten Rasmussen 	/*
201905484e09SMorten Rasmussen 	 * Examine topology from all CPU's point of views to detect the lowest
202005484e09SMorten Rasmussen 	 * sched_domain_topology_level where a highest capacity CPU is visible
202105484e09SMorten Rasmussen 	 * to everyone.
202205484e09SMorten Rasmussen 	 */
202305484e09SMorten Rasmussen 	for_each_cpu(i, cpu_map) {
20248ec59c0fSVincent Guittot 		unsigned long max_capacity = arch_scale_cpu_capacity(i);
202505484e09SMorten Rasmussen 		int tl_id = 0;
202605484e09SMorten Rasmussen 
202705484e09SMorten Rasmussen 		for_each_sd_topology(tl) {
202805484e09SMorten Rasmussen 			if (tl_id < asym_level)
202905484e09SMorten Rasmussen 				goto next_level;
203005484e09SMorten Rasmussen 
203105484e09SMorten Rasmussen 			for_each_cpu_and(j, tl->mask(i), cpu_map) {
203205484e09SMorten Rasmussen 				unsigned long capacity;
203305484e09SMorten Rasmussen 
20348ec59c0fSVincent Guittot 				capacity = arch_scale_cpu_capacity(j);
203505484e09SMorten Rasmussen 
203605484e09SMorten Rasmussen 				if (capacity <= max_capacity)
203705484e09SMorten Rasmussen 					continue;
203805484e09SMorten Rasmussen 
203905484e09SMorten Rasmussen 				max_capacity = capacity;
204005484e09SMorten Rasmussen 				asym_level = tl_id;
204105484e09SMorten Rasmussen 				asym_tl = tl;
204205484e09SMorten Rasmussen 			}
204305484e09SMorten Rasmussen next_level:
204405484e09SMorten Rasmussen 			tl_id++;
204505484e09SMorten Rasmussen 		}
204605484e09SMorten Rasmussen 	}
204705484e09SMorten Rasmussen 
204805484e09SMorten Rasmussen 	return asym_tl;
204905484e09SMorten Rasmussen }
205005484e09SMorten Rasmussen 
205105484e09SMorten Rasmussen 
205205484e09SMorten Rasmussen /*
2053f2cb1360SIngo Molnar  * Build sched domains for a given set of CPUs and attach the sched domains
2054f2cb1360SIngo Molnar  * to the individual CPUs
2055f2cb1360SIngo Molnar  */
2056f2cb1360SIngo Molnar static int
2057f2cb1360SIngo Molnar build_sched_domains(const struct cpumask *cpu_map, struct sched_domain_attr *attr)
2058f2cb1360SIngo Molnar {
2059cd1cb335SValentin Schneider 	enum s_alloc alloc_state = sa_none;
2060f2cb1360SIngo Molnar 	struct sched_domain *sd;
2061f2cb1360SIngo Molnar 	struct s_data d;
2062f2cb1360SIngo Molnar 	struct rq *rq = NULL;
2063f2cb1360SIngo Molnar 	int i, ret = -ENOMEM;
206405484e09SMorten Rasmussen 	struct sched_domain_topology_level *tl_asym;
2065df054e84SMorten Rasmussen 	bool has_asym = false;
2066f2cb1360SIngo Molnar 
2067cd1cb335SValentin Schneider 	if (WARN_ON(cpumask_empty(cpu_map)))
2068cd1cb335SValentin Schneider 		goto error;
2069cd1cb335SValentin Schneider 
2070f2cb1360SIngo Molnar 	alloc_state = __visit_domain_allocation_hell(&d, cpu_map);
2071f2cb1360SIngo Molnar 	if (alloc_state != sa_rootdomain)
2072f2cb1360SIngo Molnar 		goto error;
2073f2cb1360SIngo Molnar 
207405484e09SMorten Rasmussen 	tl_asym = asym_cpu_capacity_level(cpu_map);
207505484e09SMorten Rasmussen 
2076f2cb1360SIngo Molnar 	/* Set up domains for CPUs specified by the cpu_map: */
2077f2cb1360SIngo Molnar 	for_each_cpu(i, cpu_map) {
2078f2cb1360SIngo Molnar 		struct sched_domain_topology_level *tl;
2079c200191dSValentin Schneider 		int dflags = 0;
2080f2cb1360SIngo Molnar 
2081f2cb1360SIngo Molnar 		sd = NULL;
2082f2cb1360SIngo Molnar 		for_each_sd_topology(tl) {
2083df054e84SMorten Rasmussen 			if (tl == tl_asym) {
208405484e09SMorten Rasmussen 				dflags |= SD_ASYM_CPUCAPACITY;
2085df054e84SMorten Rasmussen 				has_asym = true;
2086df054e84SMorten Rasmussen 			}
208705484e09SMorten Rasmussen 
2088ccf74128SValentin Schneider 			if (WARN_ON(!topology_span_sane(tl, cpu_map, i)))
2089ccf74128SValentin Schneider 				goto error;
2090ccf74128SValentin Schneider 
209105484e09SMorten Rasmussen 			sd = build_sched_domain(tl, cpu_map, attr, sd, dflags, i);
209205484e09SMorten Rasmussen 
2093f2cb1360SIngo Molnar 			if (tl == sched_domain_topology)
2094f2cb1360SIngo Molnar 				*per_cpu_ptr(d.sd, i) = sd;
2095af85596cSPeter Zijlstra 			if (tl->flags & SDTL_OVERLAP)
2096f2cb1360SIngo Molnar 				sd->flags |= SD_OVERLAP;
2097f2cb1360SIngo Molnar 			if (cpumask_equal(cpu_map, sched_domain_span(sd)))
2098f2cb1360SIngo Molnar 				break;
2099f2cb1360SIngo Molnar 		}
2100f2cb1360SIngo Molnar 	}
2101f2cb1360SIngo Molnar 
2102f2cb1360SIngo Molnar 	/* Build the groups for the domains */
2103f2cb1360SIngo Molnar 	for_each_cpu(i, cpu_map) {
2104f2cb1360SIngo Molnar 		for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
2105f2cb1360SIngo Molnar 			sd->span_weight = cpumask_weight(sched_domain_span(sd));
2106f2cb1360SIngo Molnar 			if (sd->flags & SD_OVERLAP) {
2107f2cb1360SIngo Molnar 				if (build_overlap_sched_groups(sd, i))
2108f2cb1360SIngo Molnar 					goto error;
2109f2cb1360SIngo Molnar 			} else {
2110f2cb1360SIngo Molnar 				if (build_sched_groups(sd, i))
2111f2cb1360SIngo Molnar 					goto error;
2112f2cb1360SIngo Molnar 			}
2113f2cb1360SIngo Molnar 		}
2114f2cb1360SIngo Molnar 	}
2115f2cb1360SIngo Molnar 
2116f2cb1360SIngo Molnar 	/* Calculate CPU capacity for physical packages and nodes */
2117f2cb1360SIngo Molnar 	for (i = nr_cpumask_bits-1; i >= 0; i--) {
2118f2cb1360SIngo Molnar 		if (!cpumask_test_cpu(i, cpu_map))
2119f2cb1360SIngo Molnar 			continue;
2120f2cb1360SIngo Molnar 
2121f2cb1360SIngo Molnar 		for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
2122f2cb1360SIngo Molnar 			claim_allocations(i, sd);
2123f2cb1360SIngo Molnar 			init_sched_groups_capacity(i, sd);
2124f2cb1360SIngo Molnar 		}
2125f2cb1360SIngo Molnar 	}
2126f2cb1360SIngo Molnar 
2127f2cb1360SIngo Molnar 	/* Attach the domains */
2128f2cb1360SIngo Molnar 	rcu_read_lock();
2129f2cb1360SIngo Molnar 	for_each_cpu(i, cpu_map) {
2130f2cb1360SIngo Molnar 		rq = cpu_rq(i);
2131f2cb1360SIngo Molnar 		sd = *per_cpu_ptr(d.sd, i);
2132f2cb1360SIngo Molnar 
2133f2cb1360SIngo Molnar 		/* Use READ_ONCE()/WRITE_ONCE() to avoid load/store tearing: */
2134f2cb1360SIngo Molnar 		if (rq->cpu_capacity_orig > READ_ONCE(d.rd->max_cpu_capacity))
2135f2cb1360SIngo Molnar 			WRITE_ONCE(d.rd->max_cpu_capacity, rq->cpu_capacity_orig);
2136f2cb1360SIngo Molnar 
2137f2cb1360SIngo Molnar 		cpu_attach_domain(sd, d.rd, i);
2138f2cb1360SIngo Molnar 	}
2139f2cb1360SIngo Molnar 	rcu_read_unlock();
2140f2cb1360SIngo Molnar 
2141df054e84SMorten Rasmussen 	if (has_asym)
2142e284df70SValentin Schneider 		static_branch_inc_cpuslocked(&sched_asym_cpucapacity);
2143df054e84SMorten Rasmussen 
2144f2cb1360SIngo Molnar 	if (rq && sched_debug_enabled) {
2145bf5015a5SJuri Lelli 		pr_info("root domain span: %*pbl (max cpu_capacity = %lu)\n",
2146f2cb1360SIngo Molnar 			cpumask_pr_args(cpu_map), rq->rd->max_cpu_capacity);
2147f2cb1360SIngo Molnar 	}
2148f2cb1360SIngo Molnar 
2149f2cb1360SIngo Molnar 	ret = 0;
2150f2cb1360SIngo Molnar error:
2151f2cb1360SIngo Molnar 	__free_domain_allocs(&d, alloc_state, cpu_map);
215297fb7a0aSIngo Molnar 
2153f2cb1360SIngo Molnar 	return ret;
2154f2cb1360SIngo Molnar }
2155f2cb1360SIngo Molnar 
2156f2cb1360SIngo Molnar /* Current sched domains: */
2157f2cb1360SIngo Molnar static cpumask_var_t			*doms_cur;
2158f2cb1360SIngo Molnar 
2159f2cb1360SIngo Molnar /* Number of sched domains in 'doms_cur': */
2160f2cb1360SIngo Molnar static int				ndoms_cur;
2161f2cb1360SIngo Molnar 
21623b03706fSIngo Molnar /* Attributes of custom domains in 'doms_cur' */
2163f2cb1360SIngo Molnar static struct sched_domain_attr		*dattr_cur;
2164f2cb1360SIngo Molnar 
2165f2cb1360SIngo Molnar /*
2166f2cb1360SIngo Molnar  * Special case: If a kmalloc() of a doms_cur partition (array of
2167f2cb1360SIngo Molnar  * cpumask) fails, then fallback to a single sched domain,
2168f2cb1360SIngo Molnar  * as determined by the single cpumask fallback_doms.
2169f2cb1360SIngo Molnar  */
21708d5dc512SPeter Zijlstra static cpumask_var_t			fallback_doms;
2171f2cb1360SIngo Molnar 
2172f2cb1360SIngo Molnar /*
2173f2cb1360SIngo Molnar  * arch_update_cpu_topology lets virtualized architectures update the
2174f2cb1360SIngo Molnar  * CPU core maps. It is supposed to return 1 if the topology changed
2175f2cb1360SIngo Molnar  * or 0 if it stayed the same.
2176f2cb1360SIngo Molnar  */
2177f2cb1360SIngo Molnar int __weak arch_update_cpu_topology(void)
2178f2cb1360SIngo Molnar {
2179f2cb1360SIngo Molnar 	return 0;
2180f2cb1360SIngo Molnar }
2181f2cb1360SIngo Molnar 
2182f2cb1360SIngo Molnar cpumask_var_t *alloc_sched_domains(unsigned int ndoms)
2183f2cb1360SIngo Molnar {
2184f2cb1360SIngo Molnar 	int i;
2185f2cb1360SIngo Molnar 	cpumask_var_t *doms;
2186f2cb1360SIngo Molnar 
21876da2ec56SKees Cook 	doms = kmalloc_array(ndoms, sizeof(*doms), GFP_KERNEL);
2188f2cb1360SIngo Molnar 	if (!doms)
2189f2cb1360SIngo Molnar 		return NULL;
2190f2cb1360SIngo Molnar 	for (i = 0; i < ndoms; i++) {
2191f2cb1360SIngo Molnar 		if (!alloc_cpumask_var(&doms[i], GFP_KERNEL)) {
2192f2cb1360SIngo Molnar 			free_sched_domains(doms, i);
2193f2cb1360SIngo Molnar 			return NULL;
2194f2cb1360SIngo Molnar 		}
2195f2cb1360SIngo Molnar 	}
2196f2cb1360SIngo Molnar 	return doms;
2197f2cb1360SIngo Molnar }
2198f2cb1360SIngo Molnar 
2199f2cb1360SIngo Molnar void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms)
2200f2cb1360SIngo Molnar {
2201f2cb1360SIngo Molnar 	unsigned int i;
2202f2cb1360SIngo Molnar 	for (i = 0; i < ndoms; i++)
2203f2cb1360SIngo Molnar 		free_cpumask_var(doms[i]);
2204f2cb1360SIngo Molnar 	kfree(doms);
2205f2cb1360SIngo Molnar }
2206f2cb1360SIngo Molnar 
2207f2cb1360SIngo Molnar /*
2208cb0c0414SJuri Lelli  * Set up scheduler domains and groups.  For now this just excludes isolated
2209cb0c0414SJuri Lelli  * CPUs, but could be used to exclude other special cases in the future.
2210f2cb1360SIngo Molnar  */
22118d5dc512SPeter Zijlstra int sched_init_domains(const struct cpumask *cpu_map)
2212f2cb1360SIngo Molnar {
2213f2cb1360SIngo Molnar 	int err;
2214f2cb1360SIngo Molnar 
22158d5dc512SPeter Zijlstra 	zalloc_cpumask_var(&sched_domains_tmpmask, GFP_KERNEL);
22161676330eSPeter Zijlstra 	zalloc_cpumask_var(&sched_domains_tmpmask2, GFP_KERNEL);
22178d5dc512SPeter Zijlstra 	zalloc_cpumask_var(&fallback_doms, GFP_KERNEL);
22188d5dc512SPeter Zijlstra 
2219f2cb1360SIngo Molnar 	arch_update_cpu_topology();
2220f2cb1360SIngo Molnar 	ndoms_cur = 1;
2221f2cb1360SIngo Molnar 	doms_cur = alloc_sched_domains(ndoms_cur);
2222f2cb1360SIngo Molnar 	if (!doms_cur)
2223f2cb1360SIngo Molnar 		doms_cur = &fallback_doms;
2224edb93821SFrederic Weisbecker 	cpumask_and(doms_cur[0], cpu_map, housekeeping_cpumask(HK_FLAG_DOMAIN));
2225f2cb1360SIngo Molnar 	err = build_sched_domains(doms_cur[0], NULL);
2226f2cb1360SIngo Molnar 
2227f2cb1360SIngo Molnar 	return err;
2228f2cb1360SIngo Molnar }
2229f2cb1360SIngo Molnar 
2230f2cb1360SIngo Molnar /*
2231f2cb1360SIngo Molnar  * Detach sched domains from a group of CPUs specified in cpu_map
2232f2cb1360SIngo Molnar  * These CPUs will now be attached to the NULL domain
2233f2cb1360SIngo Molnar  */
2234f2cb1360SIngo Molnar static void detach_destroy_domains(const struct cpumask *cpu_map)
2235f2cb1360SIngo Molnar {
2236e284df70SValentin Schneider 	unsigned int cpu = cpumask_any(cpu_map);
2237f2cb1360SIngo Molnar 	int i;
2238f2cb1360SIngo Molnar 
2239e284df70SValentin Schneider 	if (rcu_access_pointer(per_cpu(sd_asym_cpucapacity, cpu)))
2240e284df70SValentin Schneider 		static_branch_dec_cpuslocked(&sched_asym_cpucapacity);
2241e284df70SValentin Schneider 
2242f2cb1360SIngo Molnar 	rcu_read_lock();
2243f2cb1360SIngo Molnar 	for_each_cpu(i, cpu_map)
2244f2cb1360SIngo Molnar 		cpu_attach_domain(NULL, &def_root_domain, i);
2245f2cb1360SIngo Molnar 	rcu_read_unlock();
2246f2cb1360SIngo Molnar }
2247f2cb1360SIngo Molnar 
2248f2cb1360SIngo Molnar /* handle null as "default" */
2249f2cb1360SIngo Molnar static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
2250f2cb1360SIngo Molnar 			struct sched_domain_attr *new, int idx_new)
2251f2cb1360SIngo Molnar {
2252f2cb1360SIngo Molnar 	struct sched_domain_attr tmp;
2253f2cb1360SIngo Molnar 
2254f2cb1360SIngo Molnar 	/* Fast path: */
2255f2cb1360SIngo Molnar 	if (!new && !cur)
2256f2cb1360SIngo Molnar 		return 1;
2257f2cb1360SIngo Molnar 
2258f2cb1360SIngo Molnar 	tmp = SD_ATTR_INIT;
225997fb7a0aSIngo Molnar 
2260f2cb1360SIngo Molnar 	return !memcmp(cur ? (cur + idx_cur) : &tmp,
2261f2cb1360SIngo Molnar 			new ? (new + idx_new) : &tmp,
2262f2cb1360SIngo Molnar 			sizeof(struct sched_domain_attr));
2263f2cb1360SIngo Molnar }
2264f2cb1360SIngo Molnar 
2265f2cb1360SIngo Molnar /*
2266f2cb1360SIngo Molnar  * Partition sched domains as specified by the 'ndoms_new'
2267f2cb1360SIngo Molnar  * cpumasks in the array doms_new[] of cpumasks. This compares
2268f2cb1360SIngo Molnar  * doms_new[] to the current sched domain partitioning, doms_cur[].
2269f2cb1360SIngo Molnar  * It destroys each deleted domain and builds each new domain.
2270f2cb1360SIngo Molnar  *
2271f2cb1360SIngo Molnar  * 'doms_new' is an array of cpumask_var_t's of length 'ndoms_new'.
2272f2cb1360SIngo Molnar  * The masks don't intersect (don't overlap.) We should setup one
2273f2cb1360SIngo Molnar  * sched domain for each mask. CPUs not in any of the cpumasks will
2274f2cb1360SIngo Molnar  * not be load balanced. If the same cpumask appears both in the
2275f2cb1360SIngo Molnar  * current 'doms_cur' domains and in the new 'doms_new', we can leave
2276f2cb1360SIngo Molnar  * it as it is.
2277f2cb1360SIngo Molnar  *
2278f2cb1360SIngo Molnar  * The passed in 'doms_new' should be allocated using
2279f2cb1360SIngo Molnar  * alloc_sched_domains.  This routine takes ownership of it and will
2280f2cb1360SIngo Molnar  * free_sched_domains it when done with it. If the caller failed the
2281f2cb1360SIngo Molnar  * alloc call, then it can pass in doms_new == NULL && ndoms_new == 1,
2282f2cb1360SIngo Molnar  * and partition_sched_domains() will fallback to the single partition
2283f2cb1360SIngo Molnar  * 'fallback_doms', it also forces the domains to be rebuilt.
2284f2cb1360SIngo Molnar  *
2285f2cb1360SIngo Molnar  * If doms_new == NULL it will be replaced with cpu_online_mask.
2286f2cb1360SIngo Molnar  * ndoms_new == 0 is a special case for destroying existing domains,
2287f2cb1360SIngo Molnar  * and it will not create the default domain.
2288f2cb1360SIngo Molnar  *
2289c22645f4SMathieu Poirier  * Call with hotplug lock and sched_domains_mutex held
2290f2cb1360SIngo Molnar  */
2291c22645f4SMathieu Poirier void partition_sched_domains_locked(int ndoms_new, cpumask_var_t doms_new[],
2292f2cb1360SIngo Molnar 				    struct sched_domain_attr *dattr_new)
2293f2cb1360SIngo Molnar {
22941f74de87SQuentin Perret 	bool __maybe_unused has_eas = false;
2295f2cb1360SIngo Molnar 	int i, j, n;
2296f2cb1360SIngo Molnar 	int new_topology;
2297f2cb1360SIngo Molnar 
2298c22645f4SMathieu Poirier 	lockdep_assert_held(&sched_domains_mutex);
2299f2cb1360SIngo Molnar 
2300f2cb1360SIngo Molnar 	/* Let the architecture update CPU core mappings: */
2301f2cb1360SIngo Molnar 	new_topology = arch_update_cpu_topology();
2302f2cb1360SIngo Molnar 
230309e0dd8eSPeter Zijlstra 	if (!doms_new) {
230409e0dd8eSPeter Zijlstra 		WARN_ON_ONCE(dattr_new);
230509e0dd8eSPeter Zijlstra 		n = 0;
230609e0dd8eSPeter Zijlstra 		doms_new = alloc_sched_domains(1);
230709e0dd8eSPeter Zijlstra 		if (doms_new) {
230809e0dd8eSPeter Zijlstra 			n = 1;
2309edb93821SFrederic Weisbecker 			cpumask_and(doms_new[0], cpu_active_mask,
2310edb93821SFrederic Weisbecker 				    housekeeping_cpumask(HK_FLAG_DOMAIN));
231109e0dd8eSPeter Zijlstra 		}
231209e0dd8eSPeter Zijlstra 	} else {
231309e0dd8eSPeter Zijlstra 		n = ndoms_new;
231409e0dd8eSPeter Zijlstra 	}
2315f2cb1360SIngo Molnar 
2316f2cb1360SIngo Molnar 	/* Destroy deleted domains: */
2317f2cb1360SIngo Molnar 	for (i = 0; i < ndoms_cur; i++) {
2318f2cb1360SIngo Molnar 		for (j = 0; j < n && !new_topology; j++) {
23196aa140faSQuentin Perret 			if (cpumask_equal(doms_cur[i], doms_new[j]) &&
2320f9a25f77SMathieu Poirier 			    dattrs_equal(dattr_cur, i, dattr_new, j)) {
2321f9a25f77SMathieu Poirier 				struct root_domain *rd;
2322f9a25f77SMathieu Poirier 
2323f9a25f77SMathieu Poirier 				/*
2324f9a25f77SMathieu Poirier 				 * This domain won't be destroyed and as such
2325f9a25f77SMathieu Poirier 				 * its dl_bw->total_bw needs to be cleared.  It
2326f9a25f77SMathieu Poirier 				 * will be recomputed in function
2327f9a25f77SMathieu Poirier 				 * update_tasks_root_domain().
2328f9a25f77SMathieu Poirier 				 */
2329f9a25f77SMathieu Poirier 				rd = cpu_rq(cpumask_any(doms_cur[i]))->rd;
2330f9a25f77SMathieu Poirier 				dl_clear_root_domain(rd);
2331f2cb1360SIngo Molnar 				goto match1;
2332f2cb1360SIngo Molnar 			}
2333f9a25f77SMathieu Poirier 		}
2334f2cb1360SIngo Molnar 		/* No match - a current sched domain not in new doms_new[] */
2335f2cb1360SIngo Molnar 		detach_destroy_domains(doms_cur[i]);
2336f2cb1360SIngo Molnar match1:
2337f2cb1360SIngo Molnar 		;
2338f2cb1360SIngo Molnar 	}
2339f2cb1360SIngo Molnar 
2340f2cb1360SIngo Molnar 	n = ndoms_cur;
234109e0dd8eSPeter Zijlstra 	if (!doms_new) {
2342f2cb1360SIngo Molnar 		n = 0;
2343f2cb1360SIngo Molnar 		doms_new = &fallback_doms;
2344edb93821SFrederic Weisbecker 		cpumask_and(doms_new[0], cpu_active_mask,
2345edb93821SFrederic Weisbecker 			    housekeeping_cpumask(HK_FLAG_DOMAIN));
2346f2cb1360SIngo Molnar 	}
2347f2cb1360SIngo Molnar 
2348f2cb1360SIngo Molnar 	/* Build new domains: */
2349f2cb1360SIngo Molnar 	for (i = 0; i < ndoms_new; i++) {
2350f2cb1360SIngo Molnar 		for (j = 0; j < n && !new_topology; j++) {
23516aa140faSQuentin Perret 			if (cpumask_equal(doms_new[i], doms_cur[j]) &&
23526aa140faSQuentin Perret 			    dattrs_equal(dattr_new, i, dattr_cur, j))
2353f2cb1360SIngo Molnar 				goto match2;
2354f2cb1360SIngo Molnar 		}
2355f2cb1360SIngo Molnar 		/* No match - add a new doms_new */
2356f2cb1360SIngo Molnar 		build_sched_domains(doms_new[i], dattr_new ? dattr_new + i : NULL);
2357f2cb1360SIngo Molnar match2:
2358f2cb1360SIngo Molnar 		;
2359f2cb1360SIngo Molnar 	}
2360f2cb1360SIngo Molnar 
2361531b5c9fSQuentin Perret #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
23626aa140faSQuentin Perret 	/* Build perf. domains: */
23636aa140faSQuentin Perret 	for (i = 0; i < ndoms_new; i++) {
2364531b5c9fSQuentin Perret 		for (j = 0; j < n && !sched_energy_update; j++) {
23656aa140faSQuentin Perret 			if (cpumask_equal(doms_new[i], doms_cur[j]) &&
23661f74de87SQuentin Perret 			    cpu_rq(cpumask_first(doms_cur[j]))->rd->pd) {
23671f74de87SQuentin Perret 				has_eas = true;
23686aa140faSQuentin Perret 				goto match3;
23696aa140faSQuentin Perret 			}
23701f74de87SQuentin Perret 		}
23716aa140faSQuentin Perret 		/* No match - add perf. domains for a new rd */
23721f74de87SQuentin Perret 		has_eas |= build_perf_domains(doms_new[i]);
23736aa140faSQuentin Perret match3:
23746aa140faSQuentin Perret 		;
23756aa140faSQuentin Perret 	}
23761f74de87SQuentin Perret 	sched_energy_set(has_eas);
23776aa140faSQuentin Perret #endif
23786aa140faSQuentin Perret 
2379f2cb1360SIngo Molnar 	/* Remember the new sched domains: */
2380f2cb1360SIngo Molnar 	if (doms_cur != &fallback_doms)
2381f2cb1360SIngo Molnar 		free_sched_domains(doms_cur, ndoms_cur);
2382f2cb1360SIngo Molnar 
2383f2cb1360SIngo Molnar 	kfree(dattr_cur);
2384f2cb1360SIngo Molnar 	doms_cur = doms_new;
2385f2cb1360SIngo Molnar 	dattr_cur = dattr_new;
2386f2cb1360SIngo Molnar 	ndoms_cur = ndoms_new;
2387f2cb1360SIngo Molnar 
2388*3b87f136SPeter Zijlstra 	update_sched_domain_debugfs();
2389c22645f4SMathieu Poirier }
2390f2cb1360SIngo Molnar 
2391c22645f4SMathieu Poirier /*
2392c22645f4SMathieu Poirier  * Call with hotplug lock held
2393c22645f4SMathieu Poirier  */
2394c22645f4SMathieu Poirier void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
2395c22645f4SMathieu Poirier 			     struct sched_domain_attr *dattr_new)
2396c22645f4SMathieu Poirier {
2397c22645f4SMathieu Poirier 	mutex_lock(&sched_domains_mutex);
2398c22645f4SMathieu Poirier 	partition_sched_domains_locked(ndoms_new, doms_new, dattr_new);
2399f2cb1360SIngo Molnar 	mutex_unlock(&sched_domains_mutex);
2400f2cb1360SIngo Molnar }
2401