blob: a9c36904500cd41f1ae3c834eeb9629895449d8a [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 Leib3e6aaa2018-03-08 18:53:56 +080097static int irq_build_affinity_masks(int nvecs, const struct irq_affinity *affd,
98 cpumask_var_t *node_to_cpumask,
99 const struct cpumask *cpu_mask,
100 struct cpumask *nmsk,
101 struct cpumask *masks)
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200102{
Christoph Hellwig67c93c22016-11-08 17:15:03 -0800103 int affv = nvecs - affd->pre_vectors - affd->post_vectors;
Christoph Hellwigbfe13072016-11-15 10:12:58 +0100104 int last_affv = affv + affd->pre_vectors;
Ming Leib3e6aaa2018-03-08 18:53:56 +0800105 int curvec = affd->pre_vectors;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200106 nodemask_t nodemsk = NODE_MASK_NONE;
Ming Leib3e6aaa2018-03-08 18:53:56 +0800107 int n, nodes, cpus_per_vec, extra_vecs;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200108
Ming Leib3e6aaa2018-03-08 18:53:56 +0800109 nodes = get_nodes_in_cpumask(node_to_cpumask, cpu_mask, &nodemsk);
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200110
111 /*
Guilherme G. Piccolic0af5242016-12-14 16:01:12 -0200112 * If the number of nodes in the mask is greater than or equal the
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200113 * number of vectors we just spread the vectors across the nodes.
114 */
Christoph Hellwig67c93c22016-11-08 17:15:03 -0800115 if (affv <= nodes) {
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200116 for_each_node_mask(n, nodemsk) {
Christoph Hellwig9a0ef982017-06-20 01:37:55 +0200117 cpumask_copy(masks + curvec,
Ming Lei47778f332018-03-08 18:53:55 +0800118 node_to_cpumask[n]);
Christoph Hellwigbfe13072016-11-15 10:12:58 +0100119 if (++curvec == last_affv)
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200120 break;
121 }
Ming Leib3e6aaa2018-03-08 18:53:56 +0800122 goto out;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200123 }
124
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200125 for_each_node_mask(n, nodemsk) {
Keith Busch7bf82222017-04-03 15:25:53 -0400126 int ncpus, v, vecs_to_assign, vecs_per_node;
127
128 /* Spread the vectors per node */
Keith Buschb72f8052017-04-19 19:51:10 -0400129 vecs_per_node = (affv - (curvec - affd->pre_vectors)) / nodes;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200130
131 /* Get the cpus on this node which are in the mask */
Ming Leib3e6aaa2018-03-08 18:53:56 +0800132 cpumask_and(nmsk, cpu_mask, node_to_cpumask[n]);
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200133
134 /* Calculate the number of cpus per vector */
135 ncpus = cpumask_weight(nmsk);
Keith Busch7bf82222017-04-03 15:25:53 -0400136 vecs_to_assign = min(vecs_per_node, ncpus);
137
138 /* Account for rounding errors */
Keith Busch34123862017-04-13 13:28:12 -0400139 extra_vecs = ncpus - vecs_to_assign * (ncpus / vecs_to_assign);
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200140
Christoph Hellwigbfe13072016-11-15 10:12:58 +0100141 for (v = 0; curvec < last_affv && v < vecs_to_assign;
142 curvec++, v++) {
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200143 cpus_per_vec = ncpus / vecs_to_assign;
144
145 /* Account for extra vectors to compensate rounding errors */
146 if (extra_vecs) {
147 cpus_per_vec++;
Keith Busch7bf82222017-04-03 15:25:53 -0400148 --extra_vecs;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200149 }
150 irq_spread_init_one(masks + curvec, nmsk, cpus_per_vec);
151 }
152
Christoph Hellwigbfe13072016-11-15 10:12:58 +0100153 if (curvec >= last_affv)
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200154 break;
Keith Busch7bf82222017-04-03 15:25:53 -0400155 --nodes;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200156 }
157
Ming Leib3e6aaa2018-03-08 18:53:56 +0800158out:
159 return curvec - affd->pre_vectors;
160}
161
162/**
163 * irq_create_affinity_masks - Create affinity masks for multiqueue spreading
164 * @nvecs: The total number of vectors
165 * @affd: Description of the affinity requirements
166 *
167 * Returns the masks pointer or NULL if allocation failed.
168 */
169struct cpumask *
170irq_create_affinity_masks(int nvecs, const struct irq_affinity *affd)
171{
172 cpumask_var_t nmsk, *node_to_cpumask;
173 struct cpumask *masks = NULL;
174 int curvec;
175
176 /*
177 * If there aren't any vectors left after applying the pre/post
178 * vectors don't bother with assigning affinity.
179 */
180 if (nvecs == affd->pre_vectors + affd->post_vectors)
181 return NULL;
182
183 if (!zalloc_cpumask_var(&nmsk, GFP_KERNEL))
184 return NULL;
185
186 node_to_cpumask = alloc_node_to_cpumask();
187 if (!node_to_cpumask)
188 goto outcpumsk;
189
190 masks = kcalloc(nvecs, sizeof(*masks), GFP_KERNEL);
191 if (!masks)
192 goto outnodemsk;
193
194 /* Fill out vectors at the beginning that don't need affinity */
195 for (curvec = 0; curvec < affd->pre_vectors; curvec++)
196 cpumask_copy(masks + curvec, irq_default_affinity);
197
198 /* Stabilize the cpumasks */
199 get_online_cpus();
200 build_node_to_cpumask(node_to_cpumask);
201 curvec += irq_build_affinity_masks(nvecs, affd, node_to_cpumask,
202 cpu_possible_mask, nmsk, masks);
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200203 put_online_cpus();
Christoph Hellwig67c93c22016-11-08 17:15:03 -0800204
205 /* Fill out vectors at the end that don't need affinity */
206 for (; curvec < nvecs; curvec++)
Thomas Gleixnerb6e5d5b2016-11-16 18:36:44 +0100207 cpumask_copy(masks + curvec, irq_default_affinity);
Thomas Gleixner0211e122018-04-04 12:40:07 +0200208outnodemsk:
Ming Lei47778f332018-03-08 18:53:55 +0800209 free_node_to_cpumask(node_to_cpumask);
Thomas Gleixner0211e122018-04-04 12:40:07 +0200210outcpumsk:
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200211 free_cpumask_var(nmsk);
212 return masks;
213}
214
215/**
Christoph Hellwig212bd842016-11-08 17:15:02 -0800216 * irq_calc_affinity_vectors - Calculate the optimal number of vectors
Michael Hernandez6f9a22b2017-05-18 10:47:47 -0700217 * @minvec: The minimum number of vectors available
Christoph Hellwig212bd842016-11-08 17:15:02 -0800218 * @maxvec: The maximum number of vectors available
219 * @affd: Description of the affinity requirements
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200220 */
Michael Hernandez6f9a22b2017-05-18 10:47:47 -0700221int irq_calc_affinity_vectors(int minvec, int maxvec, const struct irq_affinity *affd)
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200222{
Christoph Hellwig212bd842016-11-08 17:15:02 -0800223 int resv = affd->pre_vectors + affd->post_vectors;
224 int vecs = maxvec - resv;
Christoph Hellwig9a0ef982017-06-20 01:37:55 +0200225 int ret;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200226
Michael Hernandez6f9a22b2017-05-18 10:47:47 -0700227 if (resv > minvec)
228 return 0;
229
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200230 get_online_cpus();
Christoph Hellwig84676c12018-01-12 10:53:05 +0800231 ret = min_t(int, cpumask_weight(cpu_possible_mask), vecs) + resv;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200232 put_online_cpus();
Christoph Hellwig9a0ef982017-06-20 01:37:55 +0200233 return ret;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200234}