Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/hid/hid
[linux-2.6-block.git] / kernel / irq / affinity.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
9a0ef98e
CH
2/*
3 * Copyright (C) 2016 Thomas Gleixner.
4 * Copyright (C) 2016-2017 Christoph Hellwig.
5 */
5e385a6e
CH
6#include <linux/interrupt.h>
7#include <linux/kernel.h>
8#include <linux/slab.h>
9#include <linux/cpu.h>
10
34c3d981 11static void irq_spread_init_one(struct cpumask *irqmsk, struct cpumask *nmsk,
0145c30e 12 unsigned int cpus_per_vec)
34c3d981
TG
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
47778f33 42static cpumask_var_t *alloc_node_to_cpumask(void)
9a0ef98e
CH
43{
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
47778f33 65static void free_node_to_cpumask(cpumask_var_t *masks)
9a0ef98e
CH
66{
67 int node;
68
69 for (node = 0; node < nr_node_ids; node++)
70 free_cpumask_var(masks[node]);
71 kfree(masks);
72}
73
47778f33 74static void build_node_to_cpumask(cpumask_var_t *masks)
9a0ef98e
CH
75{
76 int cpu;
77
84676c1f 78 for_each_possible_cpu(cpu)
9a0ef98e
CH
79 cpumask_set_cpu(cpu, masks[cpu_to_node(cpu)]);
80}
81
47778f33 82static int get_nodes_in_cpumask(cpumask_var_t *node_to_cpumask,
9a0ef98e 83 const struct cpumask *mask, nodemask_t *nodemsk)
34c3d981 84{
c0af5243 85 int n, nodes = 0;
34c3d981
TG
86
87 /* Calculate the number of nodes in the supplied affinity mask */
9a0ef98e 88 for_each_node(n) {
47778f33 89 if (cpumask_intersects(mask, node_to_cpumask[n])) {
34c3d981
TG
90 node_set(n, *nodemsk);
91 nodes++;
92 }
93 }
94 return nodes;
95}
96
5c903e10 97static int __irq_build_affinity_masks(const struct irq_affinity *affd,
0145c30e
TG
98 unsigned int startvec,
99 unsigned int numvecs,
100 unsigned int firstvec,
c2899c34
TG
101 cpumask_var_t *node_to_cpumask,
102 const struct cpumask *cpu_mask,
103 struct cpumask *nmsk,
bec04037 104 struct irq_affinity_desc *masks)
34c3d981 105{
0145c30e
TG
106 unsigned int n, nodes, cpus_per_vec, extra_vecs, done = 0;
107 unsigned int last_affv = firstvec + numvecs;
108 unsigned int curvec = startvec;
34c3d981 109 nodemask_t nodemsk = NODE_MASK_NONE;
34c3d981 110
d3056812
ML
111 if (!cpumask_weight(cpu_mask))
112 return 0;
113
b3e6aaa8 114 nodes = get_nodes_in_cpumask(node_to_cpumask, cpu_mask, &nodemsk);
34c3d981
TG
115
116 /*
c0af5243 117 * If the number of nodes in the mask is greater than or equal the
34c3d981
TG
118 * number of vectors we just spread the vectors across the nodes.
119 */
1a2d0914 120 if (numvecs <= nodes) {
34c3d981 121 for_each_node_mask(n, nodemsk) {
0145c30e
TG
122 cpumask_or(&masks[curvec].mask, &masks[curvec].mask,
123 node_to_cpumask[n]);
1a2d0914 124 if (++curvec == last_affv)
060746d9 125 curvec = firstvec;
34c3d981 126 }
0145c30e 127 return numvecs;
34c3d981
TG
128 }
129
34c3d981 130 for_each_node_mask(n, nodemsk) {
0145c30e 131 unsigned int ncpus, v, vecs_to_assign, vecs_per_node;
7bf8222b
KB
132
133 /* Spread the vectors per node */
060746d9 134 vecs_per_node = (numvecs - (curvec - firstvec)) / nodes;
34c3d981
TG
135
136 /* Get the cpus on this node which are in the mask */
b3e6aaa8 137 cpumask_and(nmsk, cpu_mask, node_to_cpumask[n]);
34c3d981
TG
138
139 /* Calculate the number of cpus per vector */
140 ncpus = cpumask_weight(nmsk);
7bf8222b
KB
141 vecs_to_assign = min(vecs_per_node, ncpus);
142
143 /* Account for rounding errors */
3412386b 144 extra_vecs = ncpus - vecs_to_assign * (ncpus / vecs_to_assign);
34c3d981 145
bfe13077
CH
146 for (v = 0; curvec < last_affv && v < vecs_to_assign;
147 curvec++, v++) {
34c3d981
TG
148 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++;
7bf8222b 153 --extra_vecs;
34c3d981 154 }
bec04037
DL
155 irq_spread_init_one(&masks[curvec].mask, nmsk,
156 cpus_per_vec);
34c3d981
TG
157 }
158
1a2d0914
ML
159 done += v;
160 if (done >= numvecs)
34c3d981 161 break;
1a2d0914 162 if (curvec >= last_affv)
060746d9 163 curvec = firstvec;
7bf8222b 164 --nodes;
34c3d981 165 }
1a2d0914 166 return done;
b3e6aaa8
ML
167}
168
5c903e10
ML
169/*
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,
0145c30e
TG
175 unsigned int startvec, unsigned int numvecs,
176 unsigned int firstvec,
bec04037 177 struct irq_affinity_desc *masks)
5c903e10 178{
0145c30e 179 unsigned int curvec = startvec, nr_present, nr_others;
347253c4 180 cpumask_var_t *node_to_cpumask;
0145c30e
TG
181 cpumask_var_t nmsk, npresmsk;
182 int ret = -ENOMEM;
5c903e10
ML
183
184 if (!zalloc_cpumask_var(&nmsk, GFP_KERNEL))
c2899c34 185 return ret;
5c903e10
ML
186
187 if (!zalloc_cpumask_var(&npresmsk, GFP_KERNEL))
347253c4
ML
188 goto fail_nmsk;
189
190 node_to_cpumask = alloc_node_to_cpumask();
191 if (!node_to_cpumask)
192 goto fail_npresmsk;
5c903e10 193
6da4b3ab 194 ret = 0;
5c903e10
ML
195 /* 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 */
6da4b3ab
JA
200 nr_present = __irq_build_affinity_masks(affd, curvec, numvecs,
201 firstvec, node_to_cpumask,
202 cpu_present_mask, nmsk, masks);
5c903e10
ML
203
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 */
6da4b3ab
JA
210 if (nr_present >= numvecs)
211 curvec = firstvec;
5c903e10 212 else
6da4b3ab 213 curvec = firstvec + nr_present;
5c903e10 214 cpumask_andnot(npresmsk, cpu_possible_mask, cpu_present_mask);
6da4b3ab
JA
215 nr_others = __irq_build_affinity_masks(affd, curvec, numvecs,
216 firstvec, node_to_cpumask,
217 npresmsk, nmsk, masks);
5c903e10
ML
218 put_online_cpus();
219
6da4b3ab 220 if (nr_present < numvecs)
c2899c34 221 WARN_ON(nr_present + nr_others < numvecs);
6da4b3ab 222
347253c4
ML
223 free_node_to_cpumask(node_to_cpumask);
224
225 fail_npresmsk:
5c903e10
ML
226 free_cpumask_var(npresmsk);
227
347253c4 228 fail_nmsk:
5c903e10 229 free_cpumask_var(nmsk);
6da4b3ab 230 return ret;
5c903e10
ML
231}
232
c66d4bd1
ML
233static void default_calc_sets(struct irq_affinity *affd, unsigned int affvecs)
234{
235 affd->nr_sets = 1;
236 affd->set_size[0] = affvecs;
237}
238
b3e6aaa8
ML
239/**
240 * irq_create_affinity_masks - Create affinity masks for multiqueue spreading
241 * @nvecs: The total number of vectors
242 * @affd: Description of the affinity requirements
243 *
bec04037 244 * Returns the irq_affinity_desc pointer or NULL if allocation failed.
b3e6aaa8 245 */
bec04037 246struct irq_affinity_desc *
9cfef55b 247irq_create_affinity_masks(unsigned int nvecs, struct irq_affinity *affd)
b3e6aaa8 248{
c66d4bd1 249 unsigned int affvecs, curvec, usedvecs, i;
bec04037 250 struct irq_affinity_desc *masks = NULL;
b3e6aaa8
ML
251
252 /*
c66d4bd1
ML
253 * Determine the number of vectors which need interrupt affinities
254 * assigned. If the pre/post request exhausts the available vectors
255 * then nothing to do here except for invoking the calc_sets()
256 * callback so the device driver can adjust to the situation. If there
257 * is only a single vector, then managing the queue is pointless as
258 * well.
b3e6aaa8 259 */
c66d4bd1
ML
260 if (nvecs > 1 && nvecs > affd->pre_vectors + affd->post_vectors)
261 affvecs = nvecs - affd->pre_vectors - affd->post_vectors;
262 else
263 affvecs = 0;
264
265 /*
266 * Simple invocations do not provide a calc_sets() callback. Install
a6a309ed 267 * the generic one.
c66d4bd1 268 */
a6a309ed 269 if (!affd->calc_sets)
c66d4bd1
ML
270 affd->calc_sets = default_calc_sets;
271
a6a309ed
TG
272 /* Recalculate the sets */
273 affd->calc_sets(affd, affvecs);
b3e6aaa8 274
9cfef55b
ML
275 if (WARN_ON_ONCE(affd->nr_sets > IRQ_AFFINITY_MAX_SETS))
276 return NULL;
277
c66d4bd1
ML
278 /* Nothing to assign? */
279 if (!affvecs)
280 return NULL;
281
b3e6aaa8
ML
282 masks = kcalloc(nvecs, sizeof(*masks), GFP_KERNEL);
283 if (!masks)
347253c4 284 return NULL;
b3e6aaa8
ML
285
286 /* Fill out vectors at the beginning that don't need affinity */
287 for (curvec = 0; curvec < affd->pre_vectors; curvec++)
bec04037 288 cpumask_copy(&masks[curvec].mask, irq_default_affinity);
c66d4bd1 289
6da4b3ab
JA
290 /*
291 * Spread on present CPUs starting from affd->pre_vectors. If we
292 * have multiple sets, build each sets affinity mask separately.
293 */
c66d4bd1
ML
294 for (i = 0, usedvecs = 0; i < affd->nr_sets; i++) {
295 unsigned int this_vecs = affd->set_size[i];
6da4b3ab
JA
296 int ret;
297
298 ret = irq_build_affinity_masks(affd, curvec, this_vecs,
0145c30e 299 curvec, masks);
6da4b3ab 300 if (ret) {
c2899c34 301 kfree(masks);
347253c4 302 return NULL;
6da4b3ab
JA
303 }
304 curvec += this_vecs;
305 usedvecs += this_vecs;
306 }
67c93c21
CH
307
308 /* Fill out vectors at the end that don't need affinity */
d3056812
ML
309 if (usedvecs >= affvecs)
310 curvec = affd->pre_vectors + affvecs;
311 else
312 curvec = affd->pre_vectors + usedvecs;
67c93c21 313 for (; curvec < nvecs; curvec++)
bec04037 314 cpumask_copy(&masks[curvec].mask, irq_default_affinity);
d3056812 315
c410abbb
DL
316 /* Mark the managed interrupts */
317 for (i = affd->pre_vectors; i < nvecs - affd->post_vectors; i++)
318 masks[i].is_managed = 1;
319
34c3d981
TG
320 return masks;
321}
322
323/**
212bd846 324 * irq_calc_affinity_vectors - Calculate the optimal number of vectors
6f9a22bc 325 * @minvec: The minimum number of vectors available
212bd846
CH
326 * @maxvec: The maximum number of vectors available
327 * @affd: Description of the affinity requirements
34c3d981 328 */
0145c30e
TG
329unsigned int irq_calc_affinity_vectors(unsigned int minvec, unsigned int maxvec,
330 const struct irq_affinity *affd)
34c3d981 331{
0145c30e
TG
332 unsigned int resv = affd->pre_vectors + affd->post_vectors;
333 unsigned int set_vecs;
34c3d981 334
6f9a22bc
MH
335 if (resv > minvec)
336 return 0;
337
c66d4bd1
ML
338 if (affd->calc_sets) {
339 set_vecs = maxvec - resv;
6da4b3ab
JA
340 } else {
341 get_online_cpus();
342 set_vecs = cpumask_weight(cpu_possible_mask);
343 put_online_cpus();
344 }
345
0145c30e 346 return resv + min(set_vecs, maxvec - resv);
34c3d981 347}