blob: 08c904eb727982647993c3518ede965121061d7c [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Christoph Hellwig9a0ef982017-06-20 01:37:55 +02002/*
3 * Copyright (C) 2016 Thomas Gleixner.
4 * Copyright (C) 2016-2017 Christoph Hellwig.
5 */
Christoph Hellwig5e385a62016-07-04 17:39:27 +09006#include <linux/interrupt.h>
7#include <linux/kernel.h>
8#include <linux/slab.h>
9#include <linux/cpu.h>
10
Thomas Gleixner34c3d982016-09-14 16:18:48 +020011static void irq_spread_init_one(struct cpumask *irqmsk, struct cpumask *nmsk,
12 int cpus_per_vec)
13{
14 const struct cpumask *siblmsk;
15 int cpu, sibl;
16
17 for ( ; cpus_per_vec > 0; ) {
18 cpu = cpumask_first(nmsk);
19
20 /* Should not happen, but I'm too lazy to think about it */
21 if (cpu >= nr_cpu_ids)
22 return;
23
24 cpumask_clear_cpu(cpu, nmsk);
25 cpumask_set_cpu(cpu, irqmsk);
26 cpus_per_vec--;
27
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)
33 break;
34 if (!cpumask_test_and_clear_cpu(sibl, nmsk))
35 continue;
36 cpumask_set_cpu(sibl, irqmsk);
37 cpus_per_vec--;
38 }
39 }
40}
41
Ming Lei47778f332018-03-08 18:53:55 +080042static cpumask_var_t *alloc_node_to_cpumask(void)
Christoph Hellwig9a0ef982017-06-20 01:37:55 +020043{
44 cpumask_var_t *masks;
45 int node;
46
47 masks = kcalloc(nr_node_ids, sizeof(cpumask_var_t), GFP_KERNEL);
48 if (!masks)
49 return NULL;
50
51 for (node = 0; node < nr_node_ids; node++) {
52 if (!zalloc_cpumask_var(&masks[node], GFP_KERNEL))
53 goto out_unwind;
54 }
55
56 return masks;
57
58out_unwind:
59 while (--node >= 0)
60 free_cpumask_var(masks[node]);
61 kfree(masks);
62 return NULL;
63}
64
Ming Lei47778f332018-03-08 18:53:55 +080065static void free_node_to_cpumask(cpumask_var_t *masks)
Christoph Hellwig9a0ef982017-06-20 01:37:55 +020066{
67 int node;
68
69 for (node = 0; node < nr_node_ids; node++)
70 free_cpumask_var(masks[node]);
71 kfree(masks);
72}
73
Ming Lei47778f332018-03-08 18:53:55 +080074static void build_node_to_cpumask(cpumask_var_t *masks)
Christoph Hellwig9a0ef982017-06-20 01:37:55 +020075{
76 int cpu;
77
Christoph Hellwig84676c12018-01-12 10:53:05 +080078 for_each_possible_cpu(cpu)
Christoph Hellwig9a0ef982017-06-20 01:37:55 +020079 cpumask_set_cpu(cpu, masks[cpu_to_node(cpu)]);
80}
81
Ming Lei47778f332018-03-08 18:53:55 +080082static int get_nodes_in_cpumask(cpumask_var_t *node_to_cpumask,
Christoph Hellwig9a0ef982017-06-20 01:37:55 +020083 const struct cpumask *mask, nodemask_t *nodemsk)
Thomas Gleixner34c3d982016-09-14 16:18:48 +020084{
Guilherme G. Piccolic0af5242016-12-14 16:01:12 -020085 int n, nodes = 0;
Thomas Gleixner34c3d982016-09-14 16:18:48 +020086
87 /* Calculate the number of nodes in the supplied affinity mask */
Christoph Hellwig9a0ef982017-06-20 01:37:55 +020088 for_each_node(n) {
Ming Lei47778f332018-03-08 18:53:55 +080089 if (cpumask_intersects(mask, node_to_cpumask[n])) {
Thomas Gleixner34c3d982016-09-14 16:18:48 +020090 node_set(n, *nodemsk);
91 nodes++;
92 }
93 }
94 return nodes;
95}
96
Ming Lei5c903e102018-11-02 22:59:49 +080097static int __irq_build_affinity_masks(const struct irq_affinity *affd,
Ming Lei060746d2018-11-02 22:59:50 +080098 int startvec, int numvecs, int firstvec,
Ming Leib3e6aaa2018-03-08 18:53:56 +080099 cpumask_var_t *node_to_cpumask,
100 const struct cpumask *cpu_mask,
101 struct cpumask *nmsk,
102 struct cpumask *masks)
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200103{
Ming Lei1a2d0912018-03-08 18:53:57 +0800104 int n, nodes, cpus_per_vec, extra_vecs, done = 0;
Ming Lei060746d2018-11-02 22:59:50 +0800105 int last_affv = firstvec + numvecs;
Ming Lei1a2d0912018-03-08 18:53:57 +0800106 int curvec = startvec;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200107 nodemask_t nodemsk = NODE_MASK_NONE;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200108
Ming Leid3056812018-03-08 18:53:58 +0800109 if (!cpumask_weight(cpu_mask))
110 return 0;
111
Ming Leib3e6aaa2018-03-08 18:53:56 +0800112 nodes = get_nodes_in_cpumask(node_to_cpumask, cpu_mask, &nodemsk);
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200113
114 /*
Guilherme G. Piccolic0af5242016-12-14 16:01:12 -0200115 * If the number of nodes in the mask is greater than or equal the
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200116 * number of vectors we just spread the vectors across the nodes.
117 */
Ming Lei1a2d0912018-03-08 18:53:57 +0800118 if (numvecs <= nodes) {
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200119 for_each_node_mask(n, nodemsk) {
Long Lib8259212018-11-02 18:02:48 +0000120 cpumask_or(masks + curvec, masks + curvec, node_to_cpumask[n]);
Ming Lei1a2d0912018-03-08 18:53:57 +0800121 if (++curvec == last_affv)
Ming Lei060746d2018-11-02 22:59:50 +0800122 curvec = firstvec;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200123 }
Long Lib8259212018-11-02 18:02:48 +0000124 done = numvecs;
Ming Leib3e6aaa2018-03-08 18:53:56 +0800125 goto out;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200126 }
127
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200128 for_each_node_mask(n, nodemsk) {
Keith Busch7bf82222017-04-03 15:25:53 -0400129 int ncpus, v, vecs_to_assign, vecs_per_node;
130
131 /* Spread the vectors per node */
Ming Lei060746d2018-11-02 22:59:50 +0800132 vecs_per_node = (numvecs - (curvec - firstvec)) / nodes;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200133
134 /* Get the cpus on this node which are in the mask */
Ming Leib3e6aaa2018-03-08 18:53:56 +0800135 cpumask_and(nmsk, cpu_mask, node_to_cpumask[n]);
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200136
137 /* Calculate the number of cpus per vector */
138 ncpus = cpumask_weight(nmsk);
Keith Busch7bf82222017-04-03 15:25:53 -0400139 vecs_to_assign = min(vecs_per_node, ncpus);
140
141 /* Account for rounding errors */
Keith Busch34123862017-04-13 13:28:12 -0400142 extra_vecs = ncpus - vecs_to_assign * (ncpus / vecs_to_assign);
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200143
Christoph Hellwigbfe13072016-11-15 10:12:58 +0100144 for (v = 0; curvec < last_affv && v < vecs_to_assign;
145 curvec++, v++) {
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200146 cpus_per_vec = ncpus / vecs_to_assign;
147
148 /* Account for extra vectors to compensate rounding errors */
149 if (extra_vecs) {
150 cpus_per_vec++;
Keith Busch7bf82222017-04-03 15:25:53 -0400151 --extra_vecs;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200152 }
153 irq_spread_init_one(masks + curvec, nmsk, cpus_per_vec);
154 }
155
Ming Lei1a2d0912018-03-08 18:53:57 +0800156 done += v;
157 if (done >= numvecs)
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200158 break;
Ming Lei1a2d0912018-03-08 18:53:57 +0800159 if (curvec >= last_affv)
Ming Lei060746d2018-11-02 22:59:50 +0800160 curvec = firstvec;
Keith Busch7bf82222017-04-03 15:25:53 -0400161 --nodes;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200162 }
163
Ming Leib3e6aaa2018-03-08 18:53:56 +0800164out:
Ming Lei1a2d0912018-03-08 18:53:57 +0800165 return done;
Ming Leib3e6aaa2018-03-08 18:53:56 +0800166}
167
Ming Lei5c903e102018-11-02 22:59:49 +0800168/*
169 * build affinity in two stages:
170 * 1) spread present CPU on these vectors
171 * 2) spread other possible CPUs on these vectors
172 */
173static int irq_build_affinity_masks(const struct irq_affinity *affd,
Jens Axboe6da4b3a2018-11-02 22:59:51 +0800174 int startvec, int numvecs, int firstvec,
Ming Lei5c903e102018-11-02 22:59:49 +0800175 cpumask_var_t *node_to_cpumask,
176 struct cpumask *masks)
177{
Jens Axboe6da4b3a2018-11-02 22:59:51 +0800178 int curvec = startvec, nr_present, nr_others;
179 int ret = -ENOMEM;
Ming Lei5c903e102018-11-02 22:59:49 +0800180 cpumask_var_t nmsk, npresmsk;
181
182 if (!zalloc_cpumask_var(&nmsk, GFP_KERNEL))
Jens Axboe6da4b3a2018-11-02 22:59:51 +0800183 return ret;
Ming Lei5c903e102018-11-02 22:59:49 +0800184
185 if (!zalloc_cpumask_var(&npresmsk, GFP_KERNEL))
186 goto fail;
187
Jens Axboe6da4b3a2018-11-02 22:59:51 +0800188 ret = 0;
Ming Lei5c903e102018-11-02 22:59:49 +0800189 /* Stabilize the cpumasks */
190 get_online_cpus();
191 build_node_to_cpumask(node_to_cpumask);
192
193 /* Spread on present CPUs starting from affd->pre_vectors */
Jens Axboe6da4b3a2018-11-02 22:59:51 +0800194 nr_present = __irq_build_affinity_masks(affd, curvec, numvecs,
195 firstvec, node_to_cpumask,
196 cpu_present_mask, nmsk, masks);
Ming Lei5c903e102018-11-02 22:59:49 +0800197
198 /*
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
202 * out vectors.
203 */
Jens Axboe6da4b3a2018-11-02 22:59:51 +0800204 if (nr_present >= numvecs)
205 curvec = firstvec;
Ming Lei5c903e102018-11-02 22:59:49 +0800206 else
Jens Axboe6da4b3a2018-11-02 22:59:51 +0800207 curvec = firstvec + nr_present;
Ming Lei5c903e102018-11-02 22:59:49 +0800208 cpumask_andnot(npresmsk, cpu_possible_mask, cpu_present_mask);
Jens Axboe6da4b3a2018-11-02 22:59:51 +0800209 nr_others = __irq_build_affinity_masks(affd, curvec, numvecs,
210 firstvec, node_to_cpumask,
211 npresmsk, nmsk, masks);
Ming Lei5c903e102018-11-02 22:59:49 +0800212 put_online_cpus();
213
Jens Axboe6da4b3a2018-11-02 22:59:51 +0800214 if (nr_present < numvecs)
215 WARN_ON(nr_present + nr_others < numvecs);
216
Ming Lei5c903e102018-11-02 22:59:49 +0800217 free_cpumask_var(npresmsk);
218
219 fail:
220 free_cpumask_var(nmsk);
Jens Axboe6da4b3a2018-11-02 22:59:51 +0800221 return ret;
Ming Lei5c903e102018-11-02 22:59:49 +0800222}
223
Ming Leib3e6aaa2018-03-08 18:53:56 +0800224/**
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
228 *
229 * Returns the masks pointer or NULL if allocation failed.
230 */
231struct cpumask *
232irq_create_affinity_masks(int nvecs, const struct irq_affinity *affd)
233{
Ming Leid3056812018-03-08 18:53:58 +0800234 int affvecs = nvecs - affd->pre_vectors - affd->post_vectors;
235 int curvec, usedvecs;
Ming Lei5c903e102018-11-02 22:59:49 +0800236 cpumask_var_t *node_to_cpumask;
Ming Leib3e6aaa2018-03-08 18:53:56 +0800237 struct cpumask *masks = NULL;
Jens Axboe6da4b3a2018-11-02 22:59:51 +0800238 int i, nr_sets;
Ming Leib3e6aaa2018-03-08 18:53:56 +0800239
240 /*
241 * If there aren't any vectors left after applying the pre/post
242 * vectors don't bother with assigning affinity.
243 */
244 if (nvecs == affd->pre_vectors + affd->post_vectors)
245 return NULL;
246
Ming Leib3e6aaa2018-03-08 18:53:56 +0800247 node_to_cpumask = alloc_node_to_cpumask();
248 if (!node_to_cpumask)
Ming Lei5c903e102018-11-02 22:59:49 +0800249 return NULL;
Ming Leib3e6aaa2018-03-08 18:53:56 +0800250
251 masks = kcalloc(nvecs, sizeof(*masks), GFP_KERNEL);
252 if (!masks)
253 goto outnodemsk;
254
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);
258
Jens Axboe6da4b3a2018-11-02 22:59:51 +0800259 /*
260 * Spread on present CPUs starting from affd->pre_vectors. If we
261 * have multiple sets, build each sets affinity mask separately.
262 */
263 nr_sets = affd->nr_sets;
264 if (!nr_sets)
265 nr_sets = 1;
266
267 for (i = 0, usedvecs = 0; i < nr_sets; i++) {
268 int this_vecs = affd->sets ? affd->sets[i] : affvecs;
269 int ret;
270
271 ret = irq_build_affinity_masks(affd, curvec, this_vecs,
272 curvec, node_to_cpumask, masks);
273 if (ret) {
274 kfree(masks);
275 masks = NULL;
276 goto outnodemsk;
277 }
278 curvec += this_vecs;
279 usedvecs += this_vecs;
280 }
Christoph Hellwig67c93c22016-11-08 17:15:03 -0800281
282 /* Fill out vectors at the end that don't need affinity */
Ming Leid3056812018-03-08 18:53:58 +0800283 if (usedvecs >= affvecs)
284 curvec = affd->pre_vectors + affvecs;
285 else
286 curvec = affd->pre_vectors + usedvecs;
Christoph Hellwig67c93c22016-11-08 17:15:03 -0800287 for (; curvec < nvecs; curvec++)
Thomas Gleixnerb6e5d5b2016-11-16 18:36:44 +0100288 cpumask_copy(masks + curvec, irq_default_affinity);
Ming Leid3056812018-03-08 18:53:58 +0800289
Thomas Gleixner0211e122018-04-04 12:40:07 +0200290outnodemsk:
Ming Lei47778f332018-03-08 18:53:55 +0800291 free_node_to_cpumask(node_to_cpumask);
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200292 return masks;
293}
294
295/**
Christoph Hellwig212bd842016-11-08 17:15:02 -0800296 * irq_calc_affinity_vectors - Calculate the optimal number of vectors
Michael Hernandez6f9a22b2017-05-18 10:47:47 -0700297 * @minvec: The minimum number of vectors available
Christoph Hellwig212bd842016-11-08 17:15:02 -0800298 * @maxvec: The maximum number of vectors available
299 * @affd: Description of the affinity requirements
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200300 */
Michael Hernandez6f9a22b2017-05-18 10:47:47 -0700301int irq_calc_affinity_vectors(int minvec, int maxvec, const struct irq_affinity *affd)
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200302{
Christoph Hellwig212bd842016-11-08 17:15:02 -0800303 int resv = affd->pre_vectors + affd->post_vectors;
304 int vecs = maxvec - resv;
Jens Axboe6da4b3a2018-11-02 22:59:51 +0800305 int set_vecs;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200306
Michael Hernandez6f9a22b2017-05-18 10:47:47 -0700307 if (resv > minvec)
308 return 0;
309
Jens Axboe6da4b3a2018-11-02 22:59:51 +0800310 if (affd->nr_sets) {
311 int i;
312
313 for (i = 0, set_vecs = 0; i < affd->nr_sets; i++)
314 set_vecs += affd->sets[i];
315 } else {
316 get_online_cpus();
317 set_vecs = cpumask_weight(cpu_possible_mask);
318 put_online_cpus();
319 }
320
321 return resv + min(set_vecs, vecs);
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200322}