Loading...
1
2#include <linux/interrupt.h>
3#include <linux/kernel.h>
4#include <linux/slab.h>
5#include <linux/cpu.h>
6
7static 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{
40 int n, nodes = 0;
41
42 /* Calculate the number of nodes in the supplied affinity mask */
43 for_each_online_node(n) {
44 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
54 * @nvecs: The total number of vectors
55 * @affd: Description of the affinity requirements
56 *
57 * Returns the masks pointer or NULL if allocation failed.
58 */
59struct cpumask *
60irq_create_affinity_masks(int nvecs, const struct irq_affinity *affd)
61{
62 int n, nodes, vecs_per_node, cpus_per_vec, extra_vecs, curvec;
63 int affv = nvecs - affd->pre_vectors - affd->post_vectors;
64 int last_affv = affv + affd->pre_vectors;
65 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
72 masks = kcalloc(nvecs, sizeof(*masks), GFP_KERNEL);
73 if (!masks)
74 goto out;
75
76 /* Fill out vectors at the beginning that don't need affinity */
77 for (curvec = 0; curvec < affd->pre_vectors; curvec++)
78 cpumask_copy(masks + curvec, irq_default_affinity);
79
80 /* Stabilize the cpumasks */
81 get_online_cpus();
82 nodes = get_nodes_in_cpumask(cpu_online_mask, &nodemsk);
83
84 /*
85 * If the number of nodes in the mask is greater than or equal the
86 * number of vectors we just spread the vectors across the nodes.
87 */
88 if (affv <= nodes) {
89 for_each_node_mask(n, nodemsk) {
90 cpumask_copy(masks + curvec, cpumask_of_node(n));
91 if (++curvec == last_affv)
92 break;
93 }
94 goto done;
95 }
96
97 /* Spread the vectors per node */
98 vecs_per_node = affv / nodes;
99 /* Account for rounding errors */
100 extra_vecs = affv - (nodes * vecs_per_node);
101
102 for_each_node_mask(n, nodemsk) {
103 int ncpus, v, vecs_to_assign = vecs_per_node;
104
105 /* Get the cpus on this node which are in the mask */
106 cpumask_and(nmsk, cpu_online_mask, cpumask_of_node(n));
107
108 /* Calculate the number of cpus per vector */
109 ncpus = cpumask_weight(nmsk);
110
111 for (v = 0; curvec < last_affv && v < vecs_to_assign;
112 curvec++, v++) {
113 cpus_per_vec = ncpus / vecs_to_assign;
114
115 /* Account for extra vectors to compensate rounding errors */
116 if (extra_vecs) {
117 cpus_per_vec++;
118 if (!--extra_vecs)
119 vecs_per_node++;
120 }
121 irq_spread_init_one(masks + curvec, nmsk, cpus_per_vec);
122 }
123
124 if (curvec >= last_affv)
125 break;
126 }
127
128done:
129 put_online_cpus();
130
131 /* Fill out vectors at the end that don't need affinity */
132 for (; curvec < nvecs; curvec++)
133 cpumask_copy(masks + curvec, irq_default_affinity);
134out:
135 free_cpumask_var(nmsk);
136 return masks;
137}
138
139/**
140 * irq_calc_affinity_vectors - Calculate the optimal number of vectors
141 * @maxvec: The maximum number of vectors available
142 * @affd: Description of the affinity requirements
143 */
144int irq_calc_affinity_vectors(int maxvec, const struct irq_affinity *affd)
145{
146 int resv = affd->pre_vectors + affd->post_vectors;
147 int vecs = maxvec - resv;
148 int cpus;
149
150 /* Stabilize the cpumasks */
151 get_online_cpus();
152 cpus = cpumask_weight(cpu_online_mask);
153 put_online_cpus();
154
155 return min(cpus, vecs) + resv;
156}
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2016 Thomas Gleixner.
4 * Copyright (C) 2016-2017 Christoph Hellwig.
5 */
6#include <linux/interrupt.h>
7#include <linux/kernel.h>
8#include <linux/slab.h>
9#include <linux/cpu.h>
10#include <linux/group_cpus.h>
11
12static void default_calc_sets(struct irq_affinity *affd, unsigned int affvecs)
13{
14 affd->nr_sets = 1;
15 affd->set_size[0] = affvecs;
16}
17
18/**
19 * irq_create_affinity_masks - Create affinity masks for multiqueue spreading
20 * @nvecs: The total number of vectors
21 * @affd: Description of the affinity requirements
22 *
23 * Returns the irq_affinity_desc pointer or NULL if allocation failed.
24 */
25struct irq_affinity_desc *
26irq_create_affinity_masks(unsigned int nvecs, struct irq_affinity *affd)
27{
28 unsigned int affvecs, curvec, usedvecs, i;
29 struct irq_affinity_desc *masks = NULL;
30
31 /*
32 * Determine the number of vectors which need interrupt affinities
33 * assigned. If the pre/post request exhausts the available vectors
34 * then nothing to do here except for invoking the calc_sets()
35 * callback so the device driver can adjust to the situation.
36 */
37 if (nvecs > affd->pre_vectors + affd->post_vectors)
38 affvecs = nvecs - affd->pre_vectors - affd->post_vectors;
39 else
40 affvecs = 0;
41
42 /*
43 * Simple invocations do not provide a calc_sets() callback. Install
44 * the generic one.
45 */
46 if (!affd->calc_sets)
47 affd->calc_sets = default_calc_sets;
48
49 /* Recalculate the sets */
50 affd->calc_sets(affd, affvecs);
51
52 if (WARN_ON_ONCE(affd->nr_sets > IRQ_AFFINITY_MAX_SETS))
53 return NULL;
54
55 /* Nothing to assign? */
56 if (!affvecs)
57 return NULL;
58
59 masks = kcalloc(nvecs, sizeof(*masks), GFP_KERNEL);
60 if (!masks)
61 return NULL;
62
63 /* Fill out vectors at the beginning that don't need affinity */
64 for (curvec = 0; curvec < affd->pre_vectors; curvec++)
65 cpumask_copy(&masks[curvec].mask, irq_default_affinity);
66
67 /*
68 * Spread on present CPUs starting from affd->pre_vectors. If we
69 * have multiple sets, build each sets affinity mask separately.
70 */
71 for (i = 0, usedvecs = 0; i < affd->nr_sets; i++) {
72 unsigned int this_vecs = affd->set_size[i];
73 int j;
74 struct cpumask *result = group_cpus_evenly(this_vecs);
75
76 if (!result) {
77 kfree(masks);
78 return NULL;
79 }
80
81 for (j = 0; j < this_vecs; j++)
82 cpumask_copy(&masks[curvec + j].mask, &result[j]);
83 kfree(result);
84
85 curvec += this_vecs;
86 usedvecs += this_vecs;
87 }
88
89 /* Fill out vectors at the end that don't need affinity */
90 if (usedvecs >= affvecs)
91 curvec = affd->pre_vectors + affvecs;
92 else
93 curvec = affd->pre_vectors + usedvecs;
94 for (; curvec < nvecs; curvec++)
95 cpumask_copy(&masks[curvec].mask, irq_default_affinity);
96
97 /* Mark the managed interrupts */
98 for (i = affd->pre_vectors; i < nvecs - affd->post_vectors; i++)
99 masks[i].is_managed = 1;
100
101 return masks;
102}
103
104/**
105 * irq_calc_affinity_vectors - Calculate the optimal number of vectors
106 * @minvec: The minimum number of vectors available
107 * @maxvec: The maximum number of vectors available
108 * @affd: Description of the affinity requirements
109 */
110unsigned int irq_calc_affinity_vectors(unsigned int minvec, unsigned int maxvec,
111 const struct irq_affinity *affd)
112{
113 unsigned int resv = affd->pre_vectors + affd->post_vectors;
114 unsigned int set_vecs;
115
116 if (resv > minvec)
117 return 0;
118
119 if (affd->calc_sets) {
120 set_vecs = maxvec - resv;
121 } else {
122 cpus_read_lock();
123 set_vecs = cpumask_weight(cpu_possible_mask);
124 cpus_read_unlock();
125 }
126
127 return resv + min(set_vecs, maxvec - resv);
128}