]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - kernel/irq/affinity.c
UBUNTU: Ubuntu-4.15.0-96.97
[mirror_ubuntu-bionic-kernel.git] / kernel / irq / affinity.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2016 Thomas Gleixner.
4 * Copyright (C) 2016-2017 Christoph Hellwig.
5 */
6 #include <linux/interrupt.h>
7 #include <linux/kernel.h>
8 #include <linux/slab.h>
9 #include <linux/cpu.h>
10
11 static 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
42 static cpumask_var_t *alloc_node_to_cpumask(void)
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
58 out_unwind:
59 while (--node >= 0)
60 free_cpumask_var(masks[node]);
61 kfree(masks);
62 return NULL;
63 }
64
65 static void free_node_to_cpumask(cpumask_var_t *masks)
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
74 static void build_node_to_cpumask(cpumask_var_t *masks)
75 {
76 int cpu;
77
78 for_each_possible_cpu(cpu)
79 cpumask_set_cpu(cpu, masks[cpu_to_node(cpu)]);
80 }
81
82 static int get_nodes_in_cpumask(cpumask_var_t *node_to_cpumask,
83 const struct cpumask *mask, nodemask_t *nodemsk)
84 {
85 int n, nodes = 0;
86
87 /* Calculate the number of nodes in the supplied affinity mask */
88 for_each_node(n) {
89 if (cpumask_intersects(mask, node_to_cpumask[n])) {
90 node_set(n, *nodemsk);
91 nodes++;
92 }
93 }
94 return nodes;
95 }
96
97 static int irq_build_affinity_masks(const struct irq_affinity *affd,
98 int startvec, int numvecs,
99 cpumask_var_t *node_to_cpumask,
100 const struct cpumask *cpu_mask,
101 struct cpumask *nmsk,
102 struct cpumask *masks)
103 {
104 int n, nodes, cpus_per_vec, extra_vecs, done = 0;
105 int last_affv = affd->pre_vectors + numvecs;
106 int curvec = startvec;
107 nodemask_t nodemsk = NODE_MASK_NONE;
108
109 if (!cpumask_weight(cpu_mask))
110 return 0;
111
112 nodes = get_nodes_in_cpumask(node_to_cpumask, cpu_mask, &nodemsk);
113
114 /*
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.
117 */
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)
122 curvec = affd->pre_vectors;
123 }
124 done = numvecs;
125 goto out;
126 }
127
128 for_each_node_mask(n, nodemsk) {
129 int ncpus, v, vecs_to_assign, vecs_per_node;
130
131 /* Spread the vectors per node */
132 vecs_per_node = (numvecs - (curvec - affd->pre_vectors)) / nodes;
133
134 /* Get the cpus on this node which are in the mask */
135 cpumask_and(nmsk, cpu_mask, node_to_cpumask[n]);
136
137 /* Calculate the number of cpus per vector */
138 ncpus = cpumask_weight(nmsk);
139 vecs_to_assign = min(vecs_per_node, ncpus);
140
141 /* Account for rounding errors */
142 extra_vecs = ncpus - vecs_to_assign * (ncpus / vecs_to_assign);
143
144 for (v = 0; curvec < last_affv && v < vecs_to_assign;
145 curvec++, v++) {
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++;
151 --extra_vecs;
152 }
153 irq_spread_init_one(masks + curvec, nmsk, cpus_per_vec);
154 }
155
156 done += v;
157 if (done >= numvecs)
158 break;
159 if (curvec >= last_affv)
160 curvec = affd->pre_vectors;
161 --nodes;
162 }
163
164 out:
165 return done;
166 }
167
168 /**
169 * irq_create_affinity_masks - Create affinity masks for multiqueue spreading
170 * @nvecs: The total number of vectors
171 * @affd: Description of the affinity requirements
172 *
173 * Returns the masks pointer or NULL if allocation failed.
174 */
175 struct cpumask *
176 irq_create_affinity_masks(int nvecs, const struct irq_affinity *affd)
177 {
178 int affvecs = nvecs - affd->pre_vectors - affd->post_vectors;
179 int curvec, usedvecs;
180 cpumask_var_t nmsk, npresmsk, *node_to_cpumask;
181 struct cpumask *masks = NULL;
182
183 /*
184 * If there aren't any vectors left after applying the pre/post
185 * vectors don't bother with assigning affinity.
186 */
187 if (nvecs == affd->pre_vectors + affd->post_vectors)
188 return NULL;
189
190 if (!zalloc_cpumask_var(&nmsk, GFP_KERNEL))
191 return NULL;
192
193 if (!zalloc_cpumask_var(&npresmsk, GFP_KERNEL))
194 goto outcpumsk;
195
196 node_to_cpumask = alloc_node_to_cpumask();
197 if (!node_to_cpumask)
198 goto outnpresmsk;
199
200 masks = kcalloc(nvecs, sizeof(*masks), GFP_KERNEL);
201 if (!masks)
202 goto outnodemsk;
203
204 /* Fill out vectors at the beginning that don't need affinity */
205 for (curvec = 0; curvec < affd->pre_vectors; curvec++)
206 cpumask_copy(masks + curvec, irq_default_affinity);
207
208 /* Stabilize the cpumasks */
209 get_online_cpus();
210 build_node_to_cpumask(node_to_cpumask);
211
212 /* Spread on present CPUs starting from affd->pre_vectors */
213 usedvecs = irq_build_affinity_masks(affd, curvec, affvecs,
214 node_to_cpumask, cpu_present_mask,
215 nmsk, masks);
216
217 /*
218 * Spread on non present CPUs starting from the next vector to be
219 * handled. If the spreading of present CPUs already exhausted the
220 * vector space, assign the non present CPUs to the already spread
221 * out vectors.
222 */
223 if (usedvecs >= affvecs)
224 curvec = affd->pre_vectors;
225 else
226 curvec = affd->pre_vectors + usedvecs;
227 cpumask_andnot(npresmsk, cpu_possible_mask, cpu_present_mask);
228 usedvecs += irq_build_affinity_masks(affd, curvec, affvecs,
229 node_to_cpumask, npresmsk,
230 nmsk, masks);
231 put_online_cpus();
232
233 /* Fill out vectors at the end that don't need affinity */
234 if (usedvecs >= affvecs)
235 curvec = affd->pre_vectors + affvecs;
236 else
237 curvec = affd->pre_vectors + usedvecs;
238 for (; curvec < nvecs; curvec++)
239 cpumask_copy(masks + curvec, irq_default_affinity);
240
241 outnodemsk:
242 free_node_to_cpumask(node_to_cpumask);
243 outnpresmsk:
244 free_cpumask_var(npresmsk);
245 outcpumsk:
246 free_cpumask_var(nmsk);
247 return masks;
248 }
249
250 /**
251 * irq_calc_affinity_vectors - Calculate the optimal number of vectors
252 * @minvec: The minimum number of vectors available
253 * @maxvec: The maximum number of vectors available
254 * @affd: Description of the affinity requirements
255 */
256 int irq_calc_affinity_vectors(int minvec, int maxvec, const struct irq_affinity *affd)
257 {
258 int resv = affd->pre_vectors + affd->post_vectors;
259 int vecs = maxvec - resv;
260 int ret;
261
262 if (resv > minvec)
263 return 0;
264
265 get_online_cpus();
266 ret = min_t(int, cpumask_weight(cpu_possible_mask), vecs) + resv;
267 put_online_cpus();
268 return ret;
269 }