2 #include <linux/interrupt.h>
3 #include <linux/kernel.h>
4 #include <linux/slab.h>
7 static void irq_spread_init_one(struct cpumask
*irqmsk
, struct cpumask
*nmsk
,
10 const struct cpumask
*siblmsk
;
13 for ( ; cpus_per_vec
> 0; ) {
14 cpu
= cpumask_first(nmsk
);
16 /* Should not happen, but I'm too lazy to think about it */
17 if (cpu
>= nr_cpu_ids
)
20 cpumask_clear_cpu(cpu
, nmsk
);
21 cpumask_set_cpu(cpu
, irqmsk
);
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
)
30 if (!cpumask_test_and_clear_cpu(sibl
, nmsk
))
32 cpumask_set_cpu(sibl
, irqmsk
);
38 static int get_nodes_in_cpumask(const struct cpumask
*mask
, nodemask_t
*nodemsk
)
42 /* Calculate the number of nodes in the supplied affinity mask */
43 for (n
= 0, nodes
= 0; n
< num_online_nodes(); n
++) {
44 if (cpumask_intersects(mask
, cpumask_of_node(n
))) {
45 node_set(n
, *nodemsk
);
53 * irq_create_affinity_masks - Create affinity masks for multiqueue spreading
54 * @affinity: The affinity mask to spread. If NULL cpu_online_mask
56 * @nvecs: The number of vectors
58 * Returns the masks pointer or NULL if allocation failed.
60 struct cpumask
*irq_create_affinity_masks(const struct cpumask
*affinity
,
63 int n
, nodes
, vecs_per_node
, cpus_per_vec
, extra_vecs
, curvec
= 0;
64 nodemask_t nodemsk
= NODE_MASK_NONE
;
65 struct cpumask
*masks
;
68 if (!zalloc_cpumask_var(&nmsk
, GFP_KERNEL
))
71 masks
= kzalloc(nvec
* sizeof(*masks
), GFP_KERNEL
);
75 /* Stabilize the cpumasks */
77 /* If the supplied affinity mask is NULL, use cpu online mask */
79 affinity
= cpu_online_mask
;
81 nodes
= get_nodes_in_cpumask(affinity
, &nodemsk
);
84 * If the number of nodes in the mask is less than or equal the
85 * number of vectors we just spread the vectors across the nodes.
88 for_each_node_mask(n
, nodemsk
) {
89 cpumask_copy(masks
+ curvec
, cpumask_of_node(n
));
96 /* Spread the vectors per node */
97 vecs_per_node
= nvec
/ nodes
;
98 /* Account for rounding errors */
99 extra_vecs
= nvec
- (nodes
* vecs_per_node
);
101 for_each_node_mask(n
, nodemsk
) {
102 int ncpus
, v
, vecs_to_assign
= vecs_per_node
;
104 /* Get the cpus on this node which are in the mask */
105 cpumask_and(nmsk
, affinity
, cpumask_of_node(n
));
107 /* Calculate the number of cpus per vector */
108 ncpus
= cpumask_weight(nmsk
);
110 for (v
= 0; curvec
< nvec
&& v
< vecs_to_assign
; curvec
++, v
++) {
111 cpus_per_vec
= ncpus
/ vecs_to_assign
;
113 /* Account for extra vectors to compensate rounding errors */
119 irq_spread_init_one(masks
+ curvec
, nmsk
, cpus_per_vec
);
129 free_cpumask_var(nmsk
);
134 * irq_calc_affinity_vectors - Calculate to optimal number of vectors for a given affinity mask
135 * @affinity: The affinity mask to spread. If NULL cpu_online_mask
137 * @maxvec: The maximum number of vectors available
139 int irq_calc_affinity_vectors(const struct cpumask
*affinity
, int maxvec
)
143 /* Stabilize the cpumasks */
145 /* If the supplied affinity mask is NULL, use cpu online mask */
147 affinity
= cpu_online_mask
;
149 cpus
= cpumask_weight(affinity
);
150 ret
= (cpus
< maxvec
) ? cpus
: maxvec
;