genirq/affinity: Pass first vector to __irq_build_affinity_masks()
[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
TG
11static 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
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,
060746d9 98 int startvec, int numvecs, int firstvec,
b3e6aaa8
ML
99 cpumask_var_t *node_to_cpumask,
100 const struct cpumask *cpu_mask,
101 struct cpumask *nmsk,
102 struct cpumask *masks)
34c3d981 103{
1a2d0914 104 int n, nodes, cpus_per_vec, extra_vecs, done = 0;
060746d9 105 int last_affv = firstvec + numvecs;
1a2d0914 106 int curvec = startvec;
34c3d981 107 nodemask_t nodemsk = NODE_MASK_NONE;
34c3d981 108
d3056812
ML
109 if (!cpumask_weight(cpu_mask))
110 return 0;
111
b3e6aaa8 112 nodes = get_nodes_in_cpumask(node_to_cpumask, cpu_mask, &nodemsk);
34c3d981
TG
113
114 /*
c0af5243 115 * If the number of nodes in the mask is greater than or equal the
34c3d981
TG
116 * number of vectors we just spread the vectors across the nodes.
117 */
1a2d0914 118 if (numvecs <= nodes) {
34c3d981 119 for_each_node_mask(n, nodemsk) {
b8259219 120 cpumask_or(masks + curvec, masks + curvec, node_to_cpumask[n]);
1a2d0914 121 if (++curvec == last_affv)
060746d9 122 curvec = firstvec;
34c3d981 123 }
b8259219 124 done = numvecs;
b3e6aaa8 125 goto out;
34c3d981
TG
126 }
127
34c3d981 128 for_each_node_mask(n, nodemsk) {
7bf8222b
KB
129 int ncpus, v, vecs_to_assign, vecs_per_node;
130
131 /* Spread the vectors per node */
060746d9 132 vecs_per_node = (numvecs - (curvec - firstvec)) / nodes;
34c3d981
TG
133
134 /* Get the cpus on this node which are in the mask */
b3e6aaa8 135 cpumask_and(nmsk, cpu_mask, node_to_cpumask[n]);
34c3d981
TG
136
137 /* Calculate the number of cpus per vector */
138 ncpus = cpumask_weight(nmsk);
7bf8222b
KB
139 vecs_to_assign = min(vecs_per_node, ncpus);
140
141 /* Account for rounding errors */
3412386b 142 extra_vecs = ncpus - vecs_to_assign * (ncpus / vecs_to_assign);
34c3d981 143
bfe13077
CH
144 for (v = 0; curvec < last_affv && v < vecs_to_assign;
145 curvec++, v++) {
34c3d981
TG
146 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++;
7bf8222b 151 --extra_vecs;
34c3d981
TG
152 }
153 irq_spread_init_one(masks + curvec, nmsk, cpus_per_vec);
154 }
155
1a2d0914
ML
156 done += v;
157 if (done >= numvecs)
34c3d981 158 break;
1a2d0914 159 if (curvec >= last_affv)
060746d9 160 curvec = firstvec;
7bf8222b 161 --nodes;
34c3d981
TG
162 }
163
b3e6aaa8 164out:
1a2d0914 165 return done;
b3e6aaa8
ML
166}
167
5c903e10
ML
168/*
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,
174 int startvec, int numvecs,
175 cpumask_var_t *node_to_cpumask,
176 struct cpumask *masks)
177{
178 int curvec = startvec, usedvecs = -1;
179 cpumask_var_t nmsk, npresmsk;
180
181 if (!zalloc_cpumask_var(&nmsk, GFP_KERNEL))
182 return usedvecs;
183
184 if (!zalloc_cpumask_var(&npresmsk, GFP_KERNEL))
185 goto fail;
186
187 /* Stabilize the cpumasks */
188 get_online_cpus();
189 build_node_to_cpumask(node_to_cpumask);
190
191 /* Spread on present CPUs starting from affd->pre_vectors */
192 usedvecs = __irq_build_affinity_masks(affd, curvec, numvecs,
060746d9
ML
193 affd->pre_vectors,
194 node_to_cpumask,
195 cpu_present_mask, nmsk, masks);
5c903e10
ML
196
197 /*
198 * Spread on non present CPUs starting from the next vector to be
199 * handled. If the spreading of present CPUs already exhausted the
200 * vector space, assign the non present CPUs to the already spread
201 * out vectors.
202 */
203 if (usedvecs >= numvecs)
204 curvec = affd->pre_vectors;
205 else
206 curvec = affd->pre_vectors + usedvecs;
207 cpumask_andnot(npresmsk, cpu_possible_mask, cpu_present_mask);
208 usedvecs += __irq_build_affinity_masks(affd, curvec, numvecs,
060746d9
ML
209 affd->pre_vectors,
210 node_to_cpumask, npresmsk,
211 nmsk, masks);
5c903e10
ML
212 put_online_cpus();
213
214 free_cpumask_var(npresmsk);
215
216 fail:
217 free_cpumask_var(nmsk);
218
219 return usedvecs;
220}
221
b3e6aaa8
ML
222/**
223 * irq_create_affinity_masks - Create affinity masks for multiqueue spreading
224 * @nvecs: The total number of vectors
225 * @affd: Description of the affinity requirements
226 *
227 * Returns the masks pointer or NULL if allocation failed.
228 */
229struct cpumask *
230irq_create_affinity_masks(int nvecs, const struct irq_affinity *affd)
231{
d3056812
ML
232 int affvecs = nvecs - affd->pre_vectors - affd->post_vectors;
233 int curvec, usedvecs;
5c903e10 234 cpumask_var_t *node_to_cpumask;
b3e6aaa8 235 struct cpumask *masks = NULL;
b3e6aaa8
ML
236
237 /*
238 * If there aren't any vectors left after applying the pre/post
239 * vectors don't bother with assigning affinity.
240 */
241 if (nvecs == affd->pre_vectors + affd->post_vectors)
242 return NULL;
243
b3e6aaa8
ML
244 node_to_cpumask = alloc_node_to_cpumask();
245 if (!node_to_cpumask)
5c903e10 246 return NULL;
b3e6aaa8
ML
247
248 masks = kcalloc(nvecs, sizeof(*masks), GFP_KERNEL);
249 if (!masks)
250 goto outnodemsk;
251
252 /* Fill out vectors at the beginning that don't need affinity */
253 for (curvec = 0; curvec < affd->pre_vectors; curvec++)
254 cpumask_copy(masks + curvec, irq_default_affinity);
255
d3056812 256 usedvecs = irq_build_affinity_masks(affd, curvec, affvecs,
5c903e10 257 node_to_cpumask, masks);
67c93c21
CH
258
259 /* Fill out vectors at the end that don't need affinity */
d3056812
ML
260 if (usedvecs >= affvecs)
261 curvec = affd->pre_vectors + affvecs;
262 else
263 curvec = affd->pre_vectors + usedvecs;
67c93c21 264 for (; curvec < nvecs; curvec++)
b6e5d5b9 265 cpumask_copy(masks + curvec, irq_default_affinity);
d3056812 266
0211e12d 267outnodemsk:
47778f33 268 free_node_to_cpumask(node_to_cpumask);
34c3d981
TG
269 return masks;
270}
271
272/**
212bd846 273 * irq_calc_affinity_vectors - Calculate the optimal number of vectors
6f9a22bc 274 * @minvec: The minimum number of vectors available
212bd846
CH
275 * @maxvec: The maximum number of vectors available
276 * @affd: Description of the affinity requirements
34c3d981 277 */
6f9a22bc 278int irq_calc_affinity_vectors(int minvec, int maxvec, const struct irq_affinity *affd)
34c3d981 279{
212bd846
CH
280 int resv = affd->pre_vectors + affd->post_vectors;
281 int vecs = maxvec - resv;
9a0ef98e 282 int ret;
34c3d981 283
6f9a22bc
MH
284 if (resv > minvec)
285 return 0;
286
34c3d981 287 get_online_cpus();
84676c1f 288 ret = min_t(int, cpumask_weight(cpu_possible_mask), vecs) + resv;
34c3d981 289 put_online_cpus();
9a0ef98e 290 return ret;
34c3d981 291}