1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2016 Thomas Gleixner.
4 * Copyright (C) 2016-2017 Christoph Hellwig.
6 #include <linux/interrupt.h>
7 #include <linux/kernel.h>
8 #include <linux/slab.h>
11 static void irq_spread_init_one(struct cpumask
*irqmsk
, struct cpumask
*nmsk
,
14 const struct cpumask
*siblmsk
;
17 for ( ; cpus_per_vec
> 0; ) {
18 cpu
= cpumask_first(nmsk
);
20 /* Should not happen, but I'm too lazy to think about it */
21 if (cpu
>= nr_cpu_ids
)
24 cpumask_clear_cpu(cpu
, nmsk
);
25 cpumask_set_cpu(cpu
, irqmsk
);
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
)
34 if (!cpumask_test_and_clear_cpu(sibl
, nmsk
))
36 cpumask_set_cpu(sibl
, irqmsk
);
42 static cpumask_var_t
*alloc_node_to_present_cpumask(void)
47 masks
= kcalloc(nr_node_ids
, sizeof(cpumask_var_t
), GFP_KERNEL
);
51 for (node
= 0; node
< nr_node_ids
; node
++) {
52 if (!zalloc_cpumask_var(&masks
[node
], GFP_KERNEL
))
60 free_cpumask_var(masks
[node
]);
65 static void free_node_to_present_cpumask(cpumask_var_t
*masks
)
69 for (node
= 0; node
< nr_node_ids
; node
++)
70 free_cpumask_var(masks
[node
]);
74 static void build_node_to_present_cpumask(cpumask_var_t
*masks
)
78 for_each_present_cpu(cpu
)
79 cpumask_set_cpu(cpu
, masks
[cpu_to_node(cpu
)]);
82 static int get_nodes_in_cpumask(cpumask_var_t
*node_to_present_cpumask
,
83 const struct cpumask
*mask
, nodemask_t
*nodemsk
)
87 /* Calculate the number of nodes in the supplied affinity mask */
89 if (cpumask_intersects(mask
, node_to_present_cpumask
[n
])) {
90 node_set(n
, *nodemsk
);
98 * irq_create_affinity_masks - Create affinity masks for multiqueue spreading
99 * @nvecs: The total number of vectors
100 * @affd: Description of the affinity requirements
102 * Returns the masks pointer or NULL if allocation failed.
105 irq_create_affinity_masks(int nvecs
, const struct irq_affinity
*affd
)
107 int n
, nodes
, cpus_per_vec
, extra_vecs
, curvec
;
108 int affv
= nvecs
- affd
->pre_vectors
- affd
->post_vectors
;
109 int last_affv
= affv
+ affd
->pre_vectors
;
110 nodemask_t nodemsk
= NODE_MASK_NONE
;
111 struct cpumask
*masks
;
112 cpumask_var_t nmsk
, *node_to_present_cpumask
;
115 * If there aren't any vectors left after applying the pre/post
116 * vectors don't bother with assigning affinity.
121 if (!zalloc_cpumask_var(&nmsk
, GFP_KERNEL
))
124 masks
= kcalloc(nvecs
, sizeof(*masks
), GFP_KERNEL
);
128 node_to_present_cpumask
= alloc_node_to_present_cpumask();
129 if (!node_to_present_cpumask
)
132 /* Fill out vectors at the beginning that don't need affinity */
133 for (curvec
= 0; curvec
< affd
->pre_vectors
; curvec
++)
134 cpumask_copy(masks
+ curvec
, irq_default_affinity
);
136 /* Stabilize the cpumasks */
138 build_node_to_present_cpumask(node_to_present_cpumask
);
139 nodes
= get_nodes_in_cpumask(node_to_present_cpumask
, cpu_present_mask
,
143 * If the number of nodes in the mask is greater than or equal the
144 * number of vectors we just spread the vectors across the nodes.
147 for_each_node_mask(n
, nodemsk
) {
148 cpumask_copy(masks
+ curvec
,
149 node_to_present_cpumask
[n
]);
150 if (++curvec
== last_affv
)
156 for_each_node_mask(n
, nodemsk
) {
157 int ncpus
, v
, vecs_to_assign
, vecs_per_node
;
159 /* Spread the vectors per node */
160 vecs_per_node
= (affv
- (curvec
- affd
->pre_vectors
)) / nodes
;
162 /* Get the cpus on this node which are in the mask */
163 cpumask_and(nmsk
, cpu_present_mask
, node_to_present_cpumask
[n
]);
165 /* Calculate the number of cpus per vector */
166 ncpus
= cpumask_weight(nmsk
);
167 vecs_to_assign
= min(vecs_per_node
, ncpus
);
169 /* Account for rounding errors */
170 extra_vecs
= ncpus
- vecs_to_assign
* (ncpus
/ vecs_to_assign
);
172 for (v
= 0; curvec
< last_affv
&& v
< vecs_to_assign
;
174 cpus_per_vec
= ncpus
/ vecs_to_assign
;
176 /* Account for extra vectors to compensate rounding errors */
181 irq_spread_init_one(masks
+ curvec
, nmsk
, cpus_per_vec
);
184 if (curvec
>= last_affv
)
192 /* Fill out vectors at the end that don't need affinity */
193 for (; curvec
< nvecs
; curvec
++)
194 cpumask_copy(masks
+ curvec
, irq_default_affinity
);
195 free_node_to_present_cpumask(node_to_present_cpumask
);
197 free_cpumask_var(nmsk
);
202 * irq_calc_affinity_vectors - Calculate the optimal number of vectors
203 * @minvec: The minimum number of vectors available
204 * @maxvec: The maximum number of vectors available
205 * @affd: Description of the affinity requirements
207 int irq_calc_affinity_vectors(int minvec
, int maxvec
, const struct irq_affinity
*affd
)
209 int resv
= affd
->pre_vectors
+ affd
->post_vectors
;
210 int vecs
= maxvec
- resv
;
217 ret
= min_t(int, cpumask_weight(cpu_present_mask
), vecs
) + resv
;