blob: d2747f9c5707a0686af739b088868de9018d7a18 [file] [log] [blame]
Christoph Hellwig9a0ef982017-06-20 01:37:55 +02001/*
2 * Copyright (C) 2016 Thomas Gleixner.
3 * Copyright (C) 2016-2017 Christoph Hellwig.
4 */
Christoph Hellwig5e385a62016-07-04 17:39:27 +09005#include <linux/interrupt.h>
6#include <linux/kernel.h>
7#include <linux/slab.h>
8#include <linux/cpu.h>
9
Thomas Gleixner34c3d982016-09-14 16:18:48 +020010static void irq_spread_init_one(struct cpumask *irqmsk, struct cpumask *nmsk,
11 int cpus_per_vec)
12{
13 const struct cpumask *siblmsk;
14 int cpu, sibl;
15
16 for ( ; cpus_per_vec > 0; ) {
17 cpu = cpumask_first(nmsk);
18
19 /* Should not happen, but I'm too lazy to think about it */
20 if (cpu >= nr_cpu_ids)
21 return;
22
23 cpumask_clear_cpu(cpu, nmsk);
24 cpumask_set_cpu(cpu, irqmsk);
25 cpus_per_vec--;
26
27 /* If the cpu has siblings, use them first */
28 siblmsk = topology_sibling_cpumask(cpu);
29 for (sibl = -1; cpus_per_vec > 0; ) {
30 sibl = cpumask_next(sibl, siblmsk);
31 if (sibl >= nr_cpu_ids)
32 break;
33 if (!cpumask_test_and_clear_cpu(sibl, nmsk))
34 continue;
35 cpumask_set_cpu(sibl, irqmsk);
36 cpus_per_vec--;
37 }
38 }
39}
40
Christoph Hellwig9a0ef982017-06-20 01:37:55 +020041static cpumask_var_t *alloc_node_to_present_cpumask(void)
42{
43 cpumask_var_t *masks;
44 int node;
45
46 masks = kcalloc(nr_node_ids, sizeof(cpumask_var_t), GFP_KERNEL);
47 if (!masks)
48 return NULL;
49
50 for (node = 0; node < nr_node_ids; node++) {
51 if (!zalloc_cpumask_var(&masks[node], GFP_KERNEL))
52 goto out_unwind;
53 }
54
55 return masks;
56
57out_unwind:
58 while (--node >= 0)
59 free_cpumask_var(masks[node]);
60 kfree(masks);
61 return NULL;
62}
63
64static void free_node_to_present_cpumask(cpumask_var_t *masks)
65{
66 int node;
67
68 for (node = 0; node < nr_node_ids; node++)
69 free_cpumask_var(masks[node]);
70 kfree(masks);
71}
72
73static void build_node_to_present_cpumask(cpumask_var_t *masks)
74{
75 int cpu;
76
77 for_each_present_cpu(cpu)
78 cpumask_set_cpu(cpu, masks[cpu_to_node(cpu)]);
79}
80
81static int get_nodes_in_cpumask(cpumask_var_t *node_to_present_cpumask,
82 const struct cpumask *mask, nodemask_t *nodemsk)
Thomas Gleixner34c3d982016-09-14 16:18:48 +020083{
Guilherme G. Piccolic0af5242016-12-14 16:01:12 -020084 int n, nodes = 0;
Thomas Gleixner34c3d982016-09-14 16:18:48 +020085
86 /* Calculate the number of nodes in the supplied affinity mask */
Christoph Hellwig9a0ef982017-06-20 01:37:55 +020087 for_each_node(n) {
88 if (cpumask_intersects(mask, node_to_present_cpumask[n])) {
Thomas Gleixner34c3d982016-09-14 16:18:48 +020089 node_set(n, *nodemsk);
90 nodes++;
91 }
92 }
93 return nodes;
94}
95
96/**
97 * irq_create_affinity_masks - Create affinity masks for multiqueue spreading
Christoph Hellwig67c93c22016-11-08 17:15:03 -080098 * @nvecs: The total number of vectors
99 * @affd: Description of the affinity requirements
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200100 *
101 * Returns the masks pointer or NULL if allocation failed.
102 */
Christoph Hellwig67c93c22016-11-08 17:15:03 -0800103struct cpumask *
104irq_create_affinity_masks(int nvecs, const struct irq_affinity *affd)
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200105{
Keith Busch7bf82222017-04-03 15:25:53 -0400106 int n, nodes, cpus_per_vec, extra_vecs, curvec;
Christoph Hellwig67c93c22016-11-08 17:15:03 -0800107 int affv = nvecs - affd->pre_vectors - affd->post_vectors;
Christoph Hellwigbfe13072016-11-15 10:12:58 +0100108 int last_affv = affv + affd->pre_vectors;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200109 nodemask_t nodemsk = NODE_MASK_NONE;
110 struct cpumask *masks;
Christoph Hellwig9a0ef982017-06-20 01:37:55 +0200111 cpumask_var_t nmsk, *node_to_present_cpumask;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200112
113 if (!zalloc_cpumask_var(&nmsk, GFP_KERNEL))
114 return NULL;
115
Christoph Hellwig67c93c22016-11-08 17:15:03 -0800116 masks = kcalloc(nvecs, sizeof(*masks), GFP_KERNEL);
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200117 if (!masks)
118 goto out;
119
Christoph Hellwig9a0ef982017-06-20 01:37:55 +0200120 node_to_present_cpumask = alloc_node_to_present_cpumask();
121 if (!node_to_present_cpumask)
122 goto out;
123
Christoph Hellwig67c93c22016-11-08 17:15:03 -0800124 /* Fill out vectors at the beginning that don't need affinity */
125 for (curvec = 0; curvec < affd->pre_vectors; curvec++)
Thomas Gleixnerb6e5d5b2016-11-16 18:36:44 +0100126 cpumask_copy(masks + curvec, irq_default_affinity);
Christoph Hellwig67c93c22016-11-08 17:15:03 -0800127
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200128 /* Stabilize the cpumasks */
129 get_online_cpus();
Christoph Hellwig9a0ef982017-06-20 01:37:55 +0200130 build_node_to_present_cpumask(node_to_present_cpumask);
131 nodes = get_nodes_in_cpumask(node_to_present_cpumask, cpu_present_mask,
132 &nodemsk);
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200133
134 /*
Guilherme G. Piccolic0af5242016-12-14 16:01:12 -0200135 * If the number of nodes in the mask is greater than or equal the
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200136 * number of vectors we just spread the vectors across the nodes.
137 */
Christoph Hellwig67c93c22016-11-08 17:15:03 -0800138 if (affv <= nodes) {
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200139 for_each_node_mask(n, nodemsk) {
Christoph Hellwig9a0ef982017-06-20 01:37:55 +0200140 cpumask_copy(masks + curvec,
141 node_to_present_cpumask[n]);
Christoph Hellwigbfe13072016-11-15 10:12:58 +0100142 if (++curvec == last_affv)
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200143 break;
144 }
Christoph Hellwig67c93c22016-11-08 17:15:03 -0800145 goto done;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200146 }
147
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200148 for_each_node_mask(n, nodemsk) {
Keith Busch7bf82222017-04-03 15:25:53 -0400149 int ncpus, v, vecs_to_assign, vecs_per_node;
150
151 /* Spread the vectors per node */
Keith Buschb72f8052017-04-19 19:51:10 -0400152 vecs_per_node = (affv - (curvec - affd->pre_vectors)) / nodes;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200153
154 /* Get the cpus on this node which are in the mask */
Christoph Hellwig9a0ef982017-06-20 01:37:55 +0200155 cpumask_and(nmsk, cpu_present_mask, node_to_present_cpumask[n]);
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200156
157 /* Calculate the number of cpus per vector */
158 ncpus = cpumask_weight(nmsk);
Keith Busch7bf82222017-04-03 15:25:53 -0400159 vecs_to_assign = min(vecs_per_node, ncpus);
160
161 /* Account for rounding errors */
Keith Busch34123862017-04-13 13:28:12 -0400162 extra_vecs = ncpus - vecs_to_assign * (ncpus / vecs_to_assign);
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200163
Christoph Hellwigbfe13072016-11-15 10:12:58 +0100164 for (v = 0; curvec < last_affv && v < vecs_to_assign;
165 curvec++, v++) {
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200166 cpus_per_vec = ncpus / vecs_to_assign;
167
168 /* Account for extra vectors to compensate rounding errors */
169 if (extra_vecs) {
170 cpus_per_vec++;
Keith Busch7bf82222017-04-03 15:25:53 -0400171 --extra_vecs;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200172 }
173 irq_spread_init_one(masks + curvec, nmsk, cpus_per_vec);
174 }
175
Christoph Hellwigbfe13072016-11-15 10:12:58 +0100176 if (curvec >= last_affv)
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200177 break;
Keith Busch7bf82222017-04-03 15:25:53 -0400178 --nodes;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200179 }
180
Christoph Hellwig67c93c22016-11-08 17:15:03 -0800181done:
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200182 put_online_cpus();
Christoph Hellwig67c93c22016-11-08 17:15:03 -0800183
184 /* Fill out vectors at the end that don't need affinity */
185 for (; curvec < nvecs; curvec++)
Thomas Gleixnerb6e5d5b2016-11-16 18:36:44 +0100186 cpumask_copy(masks + curvec, irq_default_affinity);
Christoph Hellwig9a0ef982017-06-20 01:37:55 +0200187 free_node_to_present_cpumask(node_to_present_cpumask);
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200188out:
189 free_cpumask_var(nmsk);
190 return masks;
191}
192
193/**
Christoph Hellwig212bd842016-11-08 17:15:02 -0800194 * irq_calc_affinity_vectors - Calculate the optimal number of vectors
195 * @maxvec: The maximum number of vectors available
196 * @affd: Description of the affinity requirements
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200197 */
Christoph Hellwig212bd842016-11-08 17:15:02 -0800198int irq_calc_affinity_vectors(int maxvec, const struct irq_affinity *affd)
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200199{
Christoph Hellwig212bd842016-11-08 17:15:02 -0800200 int resv = affd->pre_vectors + affd->post_vectors;
201 int vecs = maxvec - resv;
Christoph Hellwig9a0ef982017-06-20 01:37:55 +0200202 int ret;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200203
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200204 get_online_cpus();
Christoph Hellwig9a0ef982017-06-20 01:37:55 +0200205 ret = min_t(int, cpumask_weight(cpu_present_mask), vecs) + resv;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200206 put_online_cpus();
Christoph Hellwig9a0ef982017-06-20 01:37:55 +0200207 return ret;
Thomas Gleixner34c3d982016-09-14 16:18:48 +0200208}