blob: 278289c091bb1fee58dc4a3ede037717d3dca3b2 [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,
Thomas Gleixner0145c302019-02-16 18:13:07 +010012 unsigned int cpus_per_vec)
Thomas Gleixner34c3d982016-09-14 16:18:48 +020013{
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,
Thomas Gleixner0145c302019-02-16 18:13:07 +010098 unsigned int startvec,
99 unsigned int numvecs,
100 unsigned int firstvec,
Thomas Gleixnerc2899c32018-12-18 16:06:53 +0100101 cpumask_var_t *node_to_cpumask,
102 const struct cpumask *cpu_mask,
103 struct cpumask *nmsk,
Dou Liyangbec04032018-12-04 23:51:20 +0800104 struct irq_affinity_desc *masks)
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200105{
Thomas Gleixner0145c302019-02-16 18:13:07 +0100106 unsigned int n, nodes, cpus_per_vec, extra_vecs, done = 0;
107 unsigned int last_affv = firstvec + numvecs;
108 unsigned int curvec = startvec;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200109 nodemask_t nodemsk = NODE_MASK_NONE;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200110
Ming Leid3056812018-03-08 18:53:58 +0800111 if (!cpumask_weight(cpu_mask))
112 return 0;
113
Ming Leib3e6aaa2018-03-08 18:53:56 +0800114 nodes = get_nodes_in_cpumask(node_to_cpumask, cpu_mask, &nodemsk);
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200115
116 /*
Guilherme G. Piccolic0af5242016-12-14 16:01:12 -0200117 * If the number of nodes in the mask is greater than or equal the
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200118 * number of vectors we just spread the vectors across the nodes.
119 */
Ming Lei1a2d0912018-03-08 18:53:57 +0800120 if (numvecs <= nodes) {
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200121 for_each_node_mask(n, nodemsk) {
Thomas Gleixner0145c302019-02-16 18:13:07 +0100122 cpumask_or(&masks[curvec].mask, &masks[curvec].mask,
123 node_to_cpumask[n]);
Ming Lei1a2d0912018-03-08 18:53:57 +0800124 if (++curvec == last_affv)
Ming Lei060746d2018-11-02 22:59:50 +0800125 curvec = firstvec;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200126 }
Thomas Gleixner0145c302019-02-16 18:13:07 +0100127 return numvecs;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200128 }
129
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200130 for_each_node_mask(n, nodemsk) {
Thomas Gleixner0145c302019-02-16 18:13:07 +0100131 unsigned int ncpus, v, vecs_to_assign, vecs_per_node;
Keith Busch7bf82222017-04-03 15:25:53 -0400132
133 /* Spread the vectors per node */
Ming Lei060746d2018-11-02 22:59:50 +0800134 vecs_per_node = (numvecs - (curvec - firstvec)) / nodes;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200135
136 /* Get the cpus on this node which are in the mask */
Ming Leib3e6aaa2018-03-08 18:53:56 +0800137 cpumask_and(nmsk, cpu_mask, node_to_cpumask[n]);
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200138
139 /* Calculate the number of cpus per vector */
140 ncpus = cpumask_weight(nmsk);
Keith Busch7bf82222017-04-03 15:25:53 -0400141 vecs_to_assign = min(vecs_per_node, ncpus);
142
143 /* Account for rounding errors */
Keith Busch34123862017-04-13 13:28:12 -0400144 extra_vecs = ncpus - vecs_to_assign * (ncpus / vecs_to_assign);
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200145
Christoph Hellwigbfe13072016-11-15 10:12:58 +0100146 for (v = 0; curvec < last_affv && v < vecs_to_assign;
147 curvec++, v++) {
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200148 cpus_per_vec = ncpus / vecs_to_assign;
149
150 /* Account for extra vectors to compensate rounding errors */
151 if (extra_vecs) {
152 cpus_per_vec++;
Keith Busch7bf82222017-04-03 15:25:53 -0400153 --extra_vecs;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200154 }
Dou Liyangbec04032018-12-04 23:51:20 +0800155 irq_spread_init_one(&masks[curvec].mask, nmsk,
156 cpus_per_vec);
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200157 }
158
Ming Lei1a2d0912018-03-08 18:53:57 +0800159 done += v;
160 if (done >= numvecs)
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200161 break;
Ming Lei1a2d0912018-03-08 18:53:57 +0800162 if (curvec >= last_affv)
Ming Lei060746d2018-11-02 22:59:50 +0800163 curvec = firstvec;
Keith Busch7bf82222017-04-03 15:25:53 -0400164 --nodes;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200165 }
Ming Lei1a2d0912018-03-08 18:53:57 +0800166 return done;
Ming Leib3e6aaa2018-03-08 18:53:56 +0800167}
168
Ming Lei5c903e102018-11-02 22:59:49 +0800169/*
170 * build affinity in two stages:
171 * 1) spread present CPU on these vectors
172 * 2) spread other possible CPUs on these vectors
173 */
174static int irq_build_affinity_masks(const struct irq_affinity *affd,
Thomas Gleixner0145c302019-02-16 18:13:07 +0100175 unsigned int startvec, unsigned int numvecs,
176 unsigned int firstvec,
Dou Liyangbec04032018-12-04 23:51:20 +0800177 struct irq_affinity_desc *masks)
Ming Lei5c903e102018-11-02 22:59:49 +0800178{
Thomas Gleixner0145c302019-02-16 18:13:07 +0100179 unsigned int curvec = startvec, nr_present, nr_others;
Ming Lei347253c2019-01-25 17:53:43 +0800180 cpumask_var_t *node_to_cpumask;
Thomas Gleixner0145c302019-02-16 18:13:07 +0100181 cpumask_var_t nmsk, npresmsk;
182 int ret = -ENOMEM;
Ming Lei5c903e102018-11-02 22:59:49 +0800183
184 if (!zalloc_cpumask_var(&nmsk, GFP_KERNEL))
Thomas Gleixnerc2899c32018-12-18 16:06:53 +0100185 return ret;
Ming Lei5c903e102018-11-02 22:59:49 +0800186
187 if (!zalloc_cpumask_var(&npresmsk, GFP_KERNEL))
Ming Lei347253c2019-01-25 17:53:43 +0800188 goto fail_nmsk;
189
190 node_to_cpumask = alloc_node_to_cpumask();
191 if (!node_to_cpumask)
192 goto fail_npresmsk;
Ming Lei5c903e102018-11-02 22:59:49 +0800193
Jens Axboe6da4b3a2018-11-02 22:59:51 +0800194 ret = 0;
Ming Lei5c903e102018-11-02 22:59:49 +0800195 /* Stabilize the cpumasks */
196 get_online_cpus();
197 build_node_to_cpumask(node_to_cpumask);
198
199 /* Spread on present CPUs starting from affd->pre_vectors */
Jens Axboe6da4b3a2018-11-02 22:59:51 +0800200 nr_present = __irq_build_affinity_masks(affd, curvec, numvecs,
201 firstvec, node_to_cpumask,
202 cpu_present_mask, nmsk, masks);
Ming Lei5c903e102018-11-02 22:59:49 +0800203
204 /*
205 * Spread on non present CPUs starting from the next vector to be
206 * handled. If the spreading of present CPUs already exhausted the
207 * vector space, assign the non present CPUs to the already spread
208 * out vectors.
209 */
Jens Axboe6da4b3a2018-11-02 22:59:51 +0800210 if (nr_present >= numvecs)
211 curvec = firstvec;
Ming Lei5c903e102018-11-02 22:59:49 +0800212 else
Jens Axboe6da4b3a2018-11-02 22:59:51 +0800213 curvec = firstvec + nr_present;
Ming Lei5c903e102018-11-02 22:59:49 +0800214 cpumask_andnot(npresmsk, cpu_possible_mask, cpu_present_mask);
Jens Axboe6da4b3a2018-11-02 22:59:51 +0800215 nr_others = __irq_build_affinity_masks(affd, curvec, numvecs,
216 firstvec, node_to_cpumask,
217 npresmsk, nmsk, masks);
Ming Lei5c903e102018-11-02 22:59:49 +0800218 put_online_cpus();
219
Jens Axboe6da4b3a2018-11-02 22:59:51 +0800220 if (nr_present < numvecs)
Thomas Gleixnerc2899c32018-12-18 16:06:53 +0100221 WARN_ON(nr_present + nr_others < numvecs);
Jens Axboe6da4b3a2018-11-02 22:59:51 +0800222
Ming Lei347253c2019-01-25 17:53:43 +0800223 free_node_to_cpumask(node_to_cpumask);
224
225 fail_npresmsk:
Ming Lei5c903e102018-11-02 22:59:49 +0800226 free_cpumask_var(npresmsk);
227
Ming Lei347253c2019-01-25 17:53:43 +0800228 fail_nmsk:
Ming Lei5c903e102018-11-02 22:59:49 +0800229 free_cpumask_var(nmsk);
Jens Axboe6da4b3a2018-11-02 22:59:51 +0800230 return ret;
Ming Lei5c903e102018-11-02 22:59:49 +0800231}
232
Ming Leib3e6aaa2018-03-08 18:53:56 +0800233/**
234 * irq_create_affinity_masks - Create affinity masks for multiqueue spreading
235 * @nvecs: The total number of vectors
236 * @affd: Description of the affinity requirements
237 *
Dou Liyangbec04032018-12-04 23:51:20 +0800238 * Returns the irq_affinity_desc pointer or NULL if allocation failed.
Ming Leib3e6aaa2018-03-08 18:53:56 +0800239 */
Dou Liyangbec04032018-12-04 23:51:20 +0800240struct irq_affinity_desc *
Ming Lei9cfef552019-02-16 18:13:08 +0100241irq_create_affinity_masks(unsigned int nvecs, struct irq_affinity *affd)
Ming Leib3e6aaa2018-03-08 18:53:56 +0800242{
Thomas Gleixner0145c302019-02-16 18:13:07 +0100243 unsigned int affvecs, curvec, usedvecs, nr_sets, i;
Ming Lei9cfef552019-02-16 18:13:08 +0100244 unsigned int set_size[IRQ_AFFINITY_MAX_SETS];
Dou Liyangbec04032018-12-04 23:51:20 +0800245 struct irq_affinity_desc *masks = NULL;
Ming Leib3e6aaa2018-03-08 18:53:56 +0800246
247 /*
248 * If there aren't any vectors left after applying the pre/post
249 * vectors don't bother with assigning affinity.
250 */
251 if (nvecs == affd->pre_vectors + affd->post_vectors)
252 return NULL;
253
Ming Lei9cfef552019-02-16 18:13:08 +0100254 if (WARN_ON_ONCE(affd->nr_sets > IRQ_AFFINITY_MAX_SETS))
255 return NULL;
256
Ming Leib3e6aaa2018-03-08 18:53:56 +0800257 masks = kcalloc(nvecs, sizeof(*masks), GFP_KERNEL);
258 if (!masks)
Ming Lei347253c2019-01-25 17:53:43 +0800259 return NULL;
Ming Leib3e6aaa2018-03-08 18:53:56 +0800260
261 /* Fill out vectors at the beginning that don't need affinity */
262 for (curvec = 0; curvec < affd->pre_vectors; curvec++)
Dou Liyangbec04032018-12-04 23:51:20 +0800263 cpumask_copy(&masks[curvec].mask, irq_default_affinity);
Jens Axboe6da4b3a2018-11-02 22:59:51 +0800264 /*
265 * Spread on present CPUs starting from affd->pre_vectors. If we
266 * have multiple sets, build each sets affinity mask separately.
267 */
Thomas Gleixner0145c302019-02-16 18:13:07 +0100268 affvecs = nvecs - affd->pre_vectors - affd->post_vectors;
Jens Axboe6da4b3a2018-11-02 22:59:51 +0800269 nr_sets = affd->nr_sets;
Ming Lei9cfef552019-02-16 18:13:08 +0100270 if (!nr_sets) {
Jens Axboe6da4b3a2018-11-02 22:59:51 +0800271 nr_sets = 1;
Ming Lei9cfef552019-02-16 18:13:08 +0100272 set_size[0] = affvecs;
273 } else {
274 memcpy(set_size, affd->set_size, nr_sets * sizeof(unsigned int));
275 }
Jens Axboe6da4b3a2018-11-02 22:59:51 +0800276
277 for (i = 0, usedvecs = 0; i < nr_sets; i++) {
Ming Lei9cfef552019-02-16 18:13:08 +0100278 unsigned int this_vecs = set_size[i];
Jens Axboe6da4b3a2018-11-02 22:59:51 +0800279 int ret;
280
281 ret = irq_build_affinity_masks(affd, curvec, this_vecs,
Thomas Gleixner0145c302019-02-16 18:13:07 +0100282 curvec, masks);
Jens Axboe6da4b3a2018-11-02 22:59:51 +0800283 if (ret) {
Thomas Gleixnerc2899c32018-12-18 16:06:53 +0100284 kfree(masks);
Ming Lei347253c2019-01-25 17:53:43 +0800285 return NULL;
Jens Axboe6da4b3a2018-11-02 22:59:51 +0800286 }
287 curvec += this_vecs;
288 usedvecs += this_vecs;
289 }
Christoph Hellwig67c93c22016-11-08 17:15:03 -0800290
291 /* Fill out vectors at the end that don't need affinity */
Ming Leid3056812018-03-08 18:53:58 +0800292 if (usedvecs >= affvecs)
293 curvec = affd->pre_vectors + affvecs;
294 else
295 curvec = affd->pre_vectors + usedvecs;
Christoph Hellwig67c93c22016-11-08 17:15:03 -0800296 for (; curvec < nvecs; curvec++)
Dou Liyangbec04032018-12-04 23:51:20 +0800297 cpumask_copy(&masks[curvec].mask, irq_default_affinity);
Ming Leid3056812018-03-08 18:53:58 +0800298
Dou Liyangc410abbb2018-12-04 23:51:21 +0800299 /* Mark the managed interrupts */
300 for (i = affd->pre_vectors; i < nvecs - affd->post_vectors; i++)
301 masks[i].is_managed = 1;
302
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200303 return masks;
304}
305
306/**
Christoph Hellwig212bd842016-11-08 17:15:02 -0800307 * irq_calc_affinity_vectors - Calculate the optimal number of vectors
Michael Hernandez6f9a22b2017-05-18 10:47:47 -0700308 * @minvec: The minimum number of vectors available
Christoph Hellwig212bd842016-11-08 17:15:02 -0800309 * @maxvec: The maximum number of vectors available
310 * @affd: Description of the affinity requirements
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200311 */
Thomas Gleixner0145c302019-02-16 18:13:07 +0100312unsigned int irq_calc_affinity_vectors(unsigned int minvec, unsigned int maxvec,
313 const struct irq_affinity *affd)
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200314{
Thomas Gleixner0145c302019-02-16 18:13:07 +0100315 unsigned int resv = affd->pre_vectors + affd->post_vectors;
316 unsigned int set_vecs;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200317
Michael Hernandez6f9a22b2017-05-18 10:47:47 -0700318 if (resv > minvec)
319 return 0;
320
Jens Axboe6da4b3a2018-11-02 22:59:51 +0800321 if (affd->nr_sets) {
Thomas Gleixner0145c302019-02-16 18:13:07 +0100322 unsigned int i;
Jens Axboe6da4b3a2018-11-02 22:59:51 +0800323
324 for (i = 0, set_vecs = 0; i < affd->nr_sets; i++)
Ming Lei9cfef552019-02-16 18:13:08 +0100325 set_vecs += affd->set_size[i];
Jens Axboe6da4b3a2018-11-02 22:59:51 +0800326 } else {
327 get_online_cpus();
328 set_vecs = cpumask_weight(cpu_possible_mask);
329 put_online_cpus();
330 }
331
Thomas Gleixner0145c302019-02-16 18:13:07 +0100332 return resv + min(set_vecs, maxvec - resv);
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200333}