blob: dc529116f7e6ce2b4025926254d253c765a86f71 [file] [log] [blame]
Christoph Hellwig5e385a62016-07-04 17:39:27 +09001
2#include <linux/interrupt.h>
3#include <linux/kernel.h>
4#include <linux/slab.h>
5#include <linux/cpu.h>
6
Thomas Gleixner34c3d982016-09-14 16:18:48 +02007static void irq_spread_init_one(struct cpumask *irqmsk, struct cpumask *nmsk,
8 int cpus_per_vec)
9{
10 const struct cpumask *siblmsk;
11 int cpu, sibl;
12
13 for ( ; cpus_per_vec > 0; ) {
14 cpu = cpumask_first(nmsk);
15
16 /* Should not happen, but I'm too lazy to think about it */
17 if (cpu >= nr_cpu_ids)
18 return;
19
20 cpumask_clear_cpu(cpu, nmsk);
21 cpumask_set_cpu(cpu, irqmsk);
22 cpus_per_vec--;
23
24 /* If the cpu has siblings, use them first */
25 siblmsk = topology_sibling_cpumask(cpu);
26 for (sibl = -1; cpus_per_vec > 0; ) {
27 sibl = cpumask_next(sibl, siblmsk);
28 if (sibl >= nr_cpu_ids)
29 break;
30 if (!cpumask_test_and_clear_cpu(sibl, nmsk))
31 continue;
32 cpumask_set_cpu(sibl, irqmsk);
33 cpus_per_vec--;
34 }
35 }
36}
37
38static int get_nodes_in_cpumask(const struct cpumask *mask, nodemask_t *nodemsk)
39{
Guilherme G. Piccolic0af5242016-12-14 16:01:12 -020040 int n, nodes = 0;
Thomas Gleixner34c3d982016-09-14 16:18:48 +020041
42 /* Calculate the number of nodes in the supplied affinity mask */
Guilherme G. Piccolic0af5242016-12-14 16:01:12 -020043 for_each_online_node(n) {
Thomas Gleixner34c3d982016-09-14 16:18:48 +020044 if (cpumask_intersects(mask, cpumask_of_node(n))) {
45 node_set(n, *nodemsk);
46 nodes++;
47 }
48 }
49 return nodes;
50}
51
52/**
53 * irq_create_affinity_masks - Create affinity masks for multiqueue spreading
Christoph Hellwig67c93c22016-11-08 17:15:03 -080054 * @nvecs: The total number of vectors
55 * @affd: Description of the affinity requirements
Thomas Gleixner34c3d982016-09-14 16:18:48 +020056 *
57 * Returns the masks pointer or NULL if allocation failed.
58 */
Christoph Hellwig67c93c22016-11-08 17:15:03 -080059struct cpumask *
60irq_create_affinity_masks(int nvecs, const struct irq_affinity *affd)
Thomas Gleixner34c3d982016-09-14 16:18:48 +020061{
Keith Busch7bf82222017-04-03 15:25:53 -040062 int n, nodes, cpus_per_vec, extra_vecs, curvec;
Christoph Hellwig67c93c22016-11-08 17:15:03 -080063 int affv = nvecs - affd->pre_vectors - affd->post_vectors;
Christoph Hellwigbfe13072016-11-15 10:12:58 +010064 int last_affv = affv + affd->pre_vectors;
Thomas Gleixner34c3d982016-09-14 16:18:48 +020065 nodemask_t nodemsk = NODE_MASK_NONE;
66 struct cpumask *masks;
67 cpumask_var_t nmsk;
68
69 if (!zalloc_cpumask_var(&nmsk, GFP_KERNEL))
70 return NULL;
71
Christoph Hellwig67c93c22016-11-08 17:15:03 -080072 masks = kcalloc(nvecs, sizeof(*masks), GFP_KERNEL);
Thomas Gleixner34c3d982016-09-14 16:18:48 +020073 if (!masks)
74 goto out;
75
Christoph Hellwig67c93c22016-11-08 17:15:03 -080076 /* Fill out vectors at the beginning that don't need affinity */
77 for (curvec = 0; curvec < affd->pre_vectors; curvec++)
Thomas Gleixnerb6e5d5b2016-11-16 18:36:44 +010078 cpumask_copy(masks + curvec, irq_default_affinity);
Christoph Hellwig67c93c22016-11-08 17:15:03 -080079
Thomas Gleixner34c3d982016-09-14 16:18:48 +020080 /* Stabilize the cpumasks */
81 get_online_cpus();
Christoph Hellwig67c93c22016-11-08 17:15:03 -080082 nodes = get_nodes_in_cpumask(cpu_online_mask, &nodemsk);
Thomas Gleixner34c3d982016-09-14 16:18:48 +020083
84 /*
Guilherme G. Piccolic0af5242016-12-14 16:01:12 -020085 * If the number of nodes in the mask is greater than or equal the
Thomas Gleixner34c3d982016-09-14 16:18:48 +020086 * number of vectors we just spread the vectors across the nodes.
87 */
Christoph Hellwig67c93c22016-11-08 17:15:03 -080088 if (affv <= nodes) {
Thomas Gleixner34c3d982016-09-14 16:18:48 +020089 for_each_node_mask(n, nodemsk) {
90 cpumask_copy(masks + curvec, cpumask_of_node(n));
Christoph Hellwigbfe13072016-11-15 10:12:58 +010091 if (++curvec == last_affv)
Thomas Gleixner34c3d982016-09-14 16:18:48 +020092 break;
93 }
Christoph Hellwig67c93c22016-11-08 17:15:03 -080094 goto done;
Thomas Gleixner34c3d982016-09-14 16:18:48 +020095 }
96
Thomas Gleixner34c3d982016-09-14 16:18:48 +020097 for_each_node_mask(n, nodemsk) {
Keith Busch7bf82222017-04-03 15:25:53 -040098 int ncpus, v, vecs_to_assign, vecs_per_node;
99
100 /* Spread the vectors per node */
101 vecs_per_node = (affv - curvec) / nodes;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200102
103 /* Get the cpus on this node which are in the mask */
Christoph Hellwig67c93c22016-11-08 17:15:03 -0800104 cpumask_and(nmsk, cpu_online_mask, cpumask_of_node(n));
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200105
106 /* Calculate the number of cpus per vector */
107 ncpus = cpumask_weight(nmsk);
Keith Busch7bf82222017-04-03 15:25:53 -0400108 vecs_to_assign = min(vecs_per_node, ncpus);
109
110 /* Account for rounding errors */
111 extra_vecs = ncpus - vecs_to_assign;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200112
Christoph Hellwigbfe13072016-11-15 10:12:58 +0100113 for (v = 0; curvec < last_affv && v < vecs_to_assign;
114 curvec++, v++) {
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200115 cpus_per_vec = ncpus / vecs_to_assign;
116
117 /* Account for extra vectors to compensate rounding errors */
118 if (extra_vecs) {
119 cpus_per_vec++;
Keith Busch7bf82222017-04-03 15:25:53 -0400120 --extra_vecs;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200121 }
122 irq_spread_init_one(masks + curvec, nmsk, cpus_per_vec);
123 }
124
Christoph Hellwigbfe13072016-11-15 10:12:58 +0100125 if (curvec >= last_affv)
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200126 break;
Keith Busch7bf82222017-04-03 15:25:53 -0400127 --nodes;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200128 }
129
Christoph Hellwig67c93c22016-11-08 17:15:03 -0800130done:
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200131 put_online_cpus();
Christoph Hellwig67c93c22016-11-08 17:15:03 -0800132
133 /* Fill out vectors at the end that don't need affinity */
134 for (; curvec < nvecs; curvec++)
Thomas Gleixnerb6e5d5b2016-11-16 18:36:44 +0100135 cpumask_copy(masks + curvec, irq_default_affinity);
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200136out:
137 free_cpumask_var(nmsk);
138 return masks;
139}
140
141/**
Christoph Hellwig212bd842016-11-08 17:15:02 -0800142 * irq_calc_affinity_vectors - Calculate the optimal number of vectors
143 * @maxvec: The maximum number of vectors available
144 * @affd: Description of the affinity requirements
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200145 */
Christoph Hellwig212bd842016-11-08 17:15:02 -0800146int irq_calc_affinity_vectors(int maxvec, const struct irq_affinity *affd)
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200147{
Christoph Hellwig212bd842016-11-08 17:15:02 -0800148 int resv = affd->pre_vectors + affd->post_vectors;
149 int vecs = maxvec - resv;
150 int cpus;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200151
152 /* Stabilize the cpumasks */
153 get_online_cpus();
Christoph Hellwig212bd842016-11-08 17:15:02 -0800154 cpus = cpumask_weight(cpu_online_mask);
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200155 put_online_cpus();
Christoph Hellwig212bd842016-11-08 17:15:02 -0800156
157 return min(cpus, vecs) + resv;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200158}