Skip to content

Commit 5e385a6

Browse files
Christoph HellwigKAGA-KOKO
authored andcommitted
genirq: Add a helper to spread an affinity mask for MSI/MSI-X vectors
This is lifted from the blk-mq code and adopted to use the affinity mask concept just introduced in the irq handling code. It tries to keep the algorithm the same as the one current used by blk-mq, but improvements like assining vectors on a per-node basis instead of just per sibling are possible with this simple move and refactoring. Signed-off-by: Christoph Hellwig <hch@lst.de> Cc: linux-block@vger.kernel.org Cc: linux-pci@vger.kernel.org Cc: linux-nvme@lists.infradead.org Cc: axboe@fb.com Cc: agordeev@redhat.com Link: http://lkml.kernel.org/r/1467621574-8277-7-git-send-email-hch@lst.de Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
1 parent 0972fa5 commit 5e385a6

3 files changed

Lines changed: 70 additions & 0 deletions

File tree

‎include/linux/interrupt.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ extern int irq_set_affinity_hint(unsigned int irq, const struct cpumask *m);
278278
extern int
279279
irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify);
280280

281+
struct cpumask *irq_create_affinity_mask(unsigned int *nr_vecs);
282+
281283
#else /* CONFIG_SMP */
282284

283285
static inline int irq_set_affinity(unsigned int irq, const struct cpumask *m)
@@ -308,6 +310,12 @@ irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify)
308310
{
309311
return 0;
310312
}
313+
314+
static inline struct cpumask *irq_create_affinity_mask(unsigned int *nr_vecs)
315+
{
316+
*nr_vecs = 1;
317+
return NULL;
318+
}
311319
#endif /* CONFIG_SMP */
312320

313321
/*

‎kernel/irq/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ obj-$(CONFIG_GENERIC_IRQ_MIGRATION) += cpuhotplug.o
99
obj-$(CONFIG_PM_SLEEP) += pm.o
1010
obj-$(CONFIG_GENERIC_MSI_IRQ) += msi.o
1111
obj-$(CONFIG_GENERIC_IRQ_IPI) += ipi.o
12+
obj-$(CONFIG_SMP) += affinity.o

‎kernel/irq/affinity.c‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
#include <linux/interrupt.h>
3+
#include <linux/kernel.h>
4+
#include <linux/slab.h>
5+
#include <linux/cpu.h>
6+
7+
static int get_first_sibling(unsigned int cpu)
8+
{
9+
unsigned int ret;
10+
11+
ret = cpumask_first(topology_sibling_cpumask(cpu));
12+
if (ret < nr_cpu_ids)
13+
return ret;
14+
return cpu;
15+
}
16+
17+
/*
18+
* Take a map of online CPUs and the number of available interrupt vectors
19+
* and generate an output cpumask suitable for spreading MSI/MSI-X vectors
20+
* so that they are distributed as good as possible around the CPUs. If
21+
* more vectors than CPUs are available we'll map one to each CPU,
22+
* otherwise we map one to the first sibling of each socket.
23+
*
24+
* If there are more vectors than CPUs we will still only have one bit
25+
* set per CPU, but interrupt code will keep on assigning the vectors from
26+
* the start of the bitmap until we run out of vectors.
27+
*/
28+
struct cpumask *irq_create_affinity_mask(unsigned int *nr_vecs)
29+
{
30+
struct cpumask *affinity_mask;
31+
unsigned int max_vecs = *nr_vecs;
32+
33+
if (max_vecs == 1)
34+
return NULL;
35+
36+
affinity_mask = kzalloc(cpumask_size(), GFP_KERNEL);
37+
if (!affinity_mask) {
38+
*nr_vecs = 1;
39+
return NULL;
40+
}
41+
42+
if (max_vecs >= num_online_cpus()) {
43+
cpumask_copy(affinity_mask, cpu_online_mask);
44+
*nr_vecs = num_online_cpus();
45+
} else {
46+
unsigned int vecs = 0, cpu;
47+
48+
for_each_online_cpu(cpu) {
49+
if (cpu == get_first_sibling(cpu)) {
50+
cpumask_set_cpu(cpu, affinity_mask);
51+
vecs++;
52+
}
53+
54+
if (--max_vecs == 0)
55+
break;
56+
}
57+
*nr_vecs = vecs;
58+
}
59+
60+
return affinity_mask;
61+
}

0 commit comments

Comments
 (0)