1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2016 Thomas Gleixner.
4 * Copyright (C) 2016-2017 Christoph Hellwig.
6 #include <linux/interrupt.h>
7 #include <linux/kernel.h>
8 #include <linux/slab.h>
11 static void irq_spread_init_one(struct cpumask *irqmsk, struct cpumask *nmsk,
14 const struct cpumask *siblmsk;
17 for ( ; cpus_per_vec > 0; ) {
18 cpu = cpumask_first(nmsk);
20 /* Should not happen, but I'm too lazy to think about it */
21 if (cpu >= nr_cpu_ids)
24 cpumask_clear_cpu(cpu, nmsk);
25 cpumask_set_cpu(cpu, irqmsk);
28 /* If the cpu has siblings, use them first */
29 siblmsk = topology_sibling_cpumask(cpu);
30 for (sibl = -1; cpus_per_vec > 0; ) {
31 sibl = cpumask_next(sibl, siblmsk);
32 if (sibl >= nr_cpu_ids)
34 if (!cpumask_test_and_clear_cpu(sibl, nmsk))
36 cpumask_set_cpu(sibl, irqmsk);
42 static cpumask_var_t *alloc_node_to_cpumask(void)
47 masks = kcalloc(nr_node_ids, sizeof(cpumask_var_t), GFP_KERNEL);
51 for (node = 0; node < nr_node_ids; node++) {
52 if (!zalloc_cpumask_var(&masks[node], GFP_KERNEL))
60 free_cpumask_var(masks[node]);
65 static void free_node_to_cpumask(cpumask_var_t *masks)
69 for (node = 0; node < nr_node_ids; node++)
70 free_cpumask_var(masks[node]);
74 static void build_node_to_cpumask(cpumask_var_t *masks)
78 for_each_possible_cpu(cpu)
79 cpumask_set_cpu(cpu, masks[cpu_to_node(cpu)]);
82 static int get_nodes_in_cpumask(cpumask_var_t *node_to_cpumask,
83 const struct cpumask *mask, nodemask_t *nodemsk)
87 /* Calculate the number of nodes in the supplied affinity mask */
89 if (cpumask_intersects(mask, node_to_cpumask[n])) {
90 node_set(n, *nodemsk);
97 static int __irq_build_affinity_masks(const struct irq_affinity *affd,
98 int startvec, int numvecs, int firstvec,
99 cpumask_var_t *node_to_cpumask,
100 const struct cpumask *cpu_mask,
101 struct cpumask *nmsk,
102 struct cpumask *masks)
104 int n, nodes, cpus_per_vec, extra_vecs, done = 0;
105 int last_affv = firstvec + numvecs;
106 int curvec = startvec;
107 nodemask_t nodemsk = NODE_MASK_NONE;
109 if (!cpumask_weight(cpu_mask))
112 nodes = get_nodes_in_cpumask(node_to_cpumask, cpu_mask, &nodemsk);
115 * If the number of nodes in the mask is greater than or equal the
116 * number of vectors we just spread the vectors across the nodes.
118 if (numvecs <= nodes) {
119 for_each_node_mask(n, nodemsk) {
120 cpumask_or(masks + curvec, masks + curvec, node_to_cpumask[n]);
121 if (++curvec == last_affv)
128 for_each_node_mask(n, nodemsk) {
129 int ncpus, v, vecs_to_assign, vecs_per_node;
131 /* Spread the vectors per node */
132 vecs_per_node = (numvecs - (curvec - firstvec)) / nodes;
134 /* Get the cpus on this node which are in the mask */
135 cpumask_and(nmsk, cpu_mask, node_to_cpumask[n]);
137 /* Calculate the number of cpus per vector */
138 ncpus = cpumask_weight(nmsk);
139 vecs_to_assign = min(vecs_per_node, ncpus);
141 /* Account for rounding errors */
142 extra_vecs = ncpus - vecs_to_assign * (ncpus / vecs_to_assign);
144 for (v = 0; curvec < last_affv && v < vecs_to_assign;
146 cpus_per_vec = ncpus / vecs_to_assign;
148 /* Account for extra vectors to compensate rounding errors */
153 irq_spread_init_one(masks + curvec, nmsk, cpus_per_vec);
159 if (curvec >= last_affv)
169 * build affinity in two stages:
170 * 1) spread present CPU on these vectors
171 * 2) spread other possible CPUs on these vectors
173 static int irq_build_affinity_masks(const struct irq_affinity *affd,
174 int startvec, int numvecs, int firstvec,
175 cpumask_var_t *node_to_cpumask,
176 struct cpumask *masks)
178 int curvec = startvec, nr_present, nr_others;
180 cpumask_var_t nmsk, npresmsk;
182 if (!zalloc_cpumask_var(&nmsk, GFP_KERNEL))
185 if (!zalloc_cpumask_var(&npresmsk, GFP_KERNEL))
189 /* Stabilize the cpumasks */
191 build_node_to_cpumask(node_to_cpumask);
193 /* Spread on present CPUs starting from affd->pre_vectors */
194 nr_present = __irq_build_affinity_masks(affd, curvec, numvecs,
195 firstvec, node_to_cpumask,
196 cpu_present_mask, nmsk, masks);
199 * Spread on non present CPUs starting from the next vector to be
200 * handled. If the spreading of present CPUs already exhausted the
201 * vector space, assign the non present CPUs to the already spread
204 if (nr_present >= numvecs)
207 curvec = firstvec + nr_present;
208 cpumask_andnot(npresmsk, cpu_possible_mask, cpu_present_mask);
209 nr_others = __irq_build_affinity_masks(affd, curvec, numvecs,
210 firstvec, node_to_cpumask,
211 npresmsk, nmsk, masks);
214 if (nr_present < numvecs)
215 WARN_ON(nr_present + nr_others < numvecs);
217 free_cpumask_var(npresmsk);
220 free_cpumask_var(nmsk);
225 * irq_create_affinity_masks - Create affinity masks for multiqueue spreading
226 * @nvecs: The total number of vectors
227 * @affd: Description of the affinity requirements
229 * Returns the masks pointer or NULL if allocation failed.
232 irq_create_affinity_masks(int nvecs, const struct irq_affinity *affd)
234 int affvecs = nvecs - affd->pre_vectors - affd->post_vectors;
235 int curvec, usedvecs;
236 cpumask_var_t *node_to_cpumask;
237 struct cpumask *masks = NULL;
241 * If there aren't any vectors left after applying the pre/post
242 * vectors don't bother with assigning affinity.
244 if (nvecs == affd->pre_vectors + affd->post_vectors)
247 node_to_cpumask = alloc_node_to_cpumask();
248 if (!node_to_cpumask)
251 masks = kcalloc(nvecs, sizeof(*masks), GFP_KERNEL);
255 /* Fill out vectors at the beginning that don't need affinity */
256 for (curvec = 0; curvec < affd->pre_vectors; curvec++)
257 cpumask_copy(masks + curvec, irq_default_affinity);
260 * Spread on present CPUs starting from affd->pre_vectors. If we
261 * have multiple sets, build each sets affinity mask separately.
263 nr_sets = affd->nr_sets;
267 for (i = 0, usedvecs = 0; i < nr_sets; i++) {
268 int this_vecs = affd->sets ? affd->sets[i] : affvecs;
271 ret = irq_build_affinity_masks(affd, curvec, this_vecs,
272 curvec, node_to_cpumask, masks);
279 usedvecs += this_vecs;
282 /* Fill out vectors at the end that don't need affinity */
283 if (usedvecs >= affvecs)
284 curvec = affd->pre_vectors + affvecs;
286 curvec = affd->pre_vectors + usedvecs;
287 for (; curvec < nvecs; curvec++)
288 cpumask_copy(masks + curvec, irq_default_affinity);
291 free_node_to_cpumask(node_to_cpumask);
296 * irq_calc_affinity_vectors - Calculate the optimal number of vectors
297 * @minvec: The minimum number of vectors available
298 * @maxvec: The maximum number of vectors available
299 * @affd: Description of the affinity requirements
301 int irq_calc_affinity_vectors(int minvec, int maxvec, const struct irq_affinity *affd)
303 int resv = affd->pre_vectors + affd->post_vectors;
304 int vecs = maxvec - resv;
313 for (i = 0, set_vecs = 0; i < affd->nr_sets; i++)
314 set_vecs += affd->sets[i];
317 set_vecs = cpumask_weight(cpu_possible_mask);
321 return resv + min(set_vecs, vecs);