Skip to content

Commit e75eafb

Browse files
committed
genirq/msi: Switch to new irq spreading infrastructure
Switch MSI over to the new spreading code. If a pci device contains a valid pointer to a cpumask, then this mask is used for spreading otherwise the online cpu mask is used. This allows a driver to restrict the spread to a subset of CPUs, e.g. cpus on a particular node. Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Cc: Christoph Hellwig <hch@lst.de> Cc: axboe@fb.com Cc: keith.busch@intel.com Cc: agordeev@redhat.com Cc: linux-block@vger.kernel.org Link: http://lkml.kernel.org/r/1473862739-15032-4-git-send-email-hch@lst.de Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
1 parent 34c3d98 commit e75eafb

2 files changed

Lines changed: 87 additions & 72 deletions

File tree

‎drivers/pci/msi.c‎

Lines changed: 72 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -549,15 +549,23 @@ static int populate_msi_sysfs(struct pci_dev *pdev)
549549
return ret;
550550
}
551551

552-
static struct msi_desc *msi_setup_entry(struct pci_dev *dev, int nvec)
552+
static struct msi_desc *
553+
msi_setup_entry(struct pci_dev *dev, int nvec, bool affinity)
553554
{
554-
u16 control;
555+
struct cpumask *masks = NULL;
555556
struct msi_desc *entry;
557+
u16 control;
558+
559+
if (affinity) {
560+
masks = irq_create_affinity_masks(dev->irq_affinity, nvec);
561+
if (!masks)
562+
pr_err("Unable to allocate affinity masks, ignoring\n");
563+
}
556564

557565
/* MSI Entry Initialization */
558-
entry = alloc_msi_entry(&dev->dev, nvec, NULL);
566+
entry = alloc_msi_entry(&dev->dev, nvec, masks);
559567
if (!entry)
560-
return NULL;
568+
goto out;
561569

562570
pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
563571

@@ -568,7 +576,6 @@ static struct msi_desc *msi_setup_entry(struct pci_dev *dev, int nvec)
568576
entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
569577
entry->msi_attrib.multi_cap = (control & PCI_MSI_FLAGS_QMASK) >> 1;
570578
entry->msi_attrib.multiple = ilog2(__roundup_pow_of_two(nvec));
571-
entry->affinity = dev->irq_affinity;
572579

573580
if (control & PCI_MSI_FLAGS_64BIT)
574581
entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_64;
@@ -579,6 +586,8 @@ static struct msi_desc *msi_setup_entry(struct pci_dev *dev, int nvec)
579586
if (entry->msi_attrib.maskbit)
580587
pci_read_config_dword(dev, entry->mask_pos, &entry->masked);
581588

589+
out:
590+
kfree(masks);
582591
return entry;
583592
}
584593

@@ -607,15 +616,15 @@ static int msi_verify_entries(struct pci_dev *dev)
607616
* an error, and a positive return value indicates the number of interrupts
608617
* which could have been allocated.
609618
*/
610-
static int msi_capability_init(struct pci_dev *dev, int nvec)
619+
static int msi_capability_init(struct pci_dev *dev, int nvec, bool affinity)
611620
{
612621
struct msi_desc *entry;
613622
int ret;
614623
unsigned mask;
615624

616625
pci_msi_set_enable(dev, 0); /* Disable MSI during set up */
617626

618-
entry = msi_setup_entry(dev, nvec);
627+
entry = msi_setup_entry(dev, nvec, affinity);
619628
if (!entry)
620629
return -ENOMEM;
621630

@@ -678,28 +687,29 @@ static void __iomem *msix_map_region(struct pci_dev *dev, unsigned nr_entries)
678687
}
679688

680689
static int msix_setup_entries(struct pci_dev *dev, void __iomem *base,
681-
struct msix_entry *entries, int nvec)
690+
struct msix_entry *entries, int nvec,
691+
bool affinity)
682692
{
683-
const struct cpumask *mask = NULL;
693+
struct cpumask *curmsk, *masks = NULL;
684694
struct msi_desc *entry;
685-
int cpu = -1, i;
686-
687-
for (i = 0; i < nvec; i++) {
688-
if (dev->irq_affinity) {
689-
cpu = cpumask_next(cpu, dev->irq_affinity);
690-
if (cpu >= nr_cpu_ids)
691-
cpu = cpumask_first(dev->irq_affinity);
692-
mask = cpumask_of(cpu);
693-
}
695+
int ret, i;
694696

695-
entry = alloc_msi_entry(&dev->dev, 1, NULL);
697+
if (affinity) {
698+
masks = irq_create_affinity_masks(dev->irq_affinity, nvec);
699+
if (!masks)
700+
pr_err("Unable to allocate affinity masks, ignoring\n");
701+
}
702+
703+
for (i = 0, curmsk = masks; i < nvec; i++) {
704+
entry = alloc_msi_entry(&dev->dev, 1, curmsk);
696705
if (!entry) {
697706
if (!i)
698707
iounmap(base);
699708
else
700709
free_msi_irqs(dev);
701710
/* No enough memory. Don't try again */
702-
return -ENOMEM;
711+
ret = -ENOMEM;
712+
goto out;
703713
}
704714

705715
entry->msi_attrib.is_msix = 1;
@@ -710,11 +720,14 @@ static int msix_setup_entries(struct pci_dev *dev, void __iomem *base,
710720
entry->msi_attrib.entry_nr = i;
711721
entry->msi_attrib.default_irq = dev->irq;
712722
entry->mask_base = base;
713-
entry->affinity = mask;
714723

715724
list_add_tail(&entry->list, dev_to_msi_list(&dev->dev));
725+
if (masks)
726+
curmsk++;
716727
}
717-
728+
ret = 0;
729+
out:
730+
kfree(masks);
718731
return 0;
719732
}
720733

@@ -743,8 +756,8 @@ static void msix_program_entries(struct pci_dev *dev,
743756
* single MSI-X irq. A return of zero indicates the successful setup of
744757
* requested MSI-X entries with allocated irqs or non-zero for otherwise.
745758
**/
746-
static int msix_capability_init(struct pci_dev *dev,
747-
struct msix_entry *entries, int nvec)
759+
static int msix_capability_init(struct pci_dev *dev, struct msix_entry *entries,
760+
int nvec, bool affinity)
748761
{
749762
int ret;
750763
u16 control;
@@ -759,7 +772,7 @@ static int msix_capability_init(struct pci_dev *dev,
759772
if (!base)
760773
return -ENOMEM;
761774

762-
ret = msix_setup_entries(dev, base, entries, nvec);
775+
ret = msix_setup_entries(dev, base, entries, nvec, affinity);
763776
if (ret)
764777
return ret;
765778

@@ -939,22 +952,8 @@ int pci_msix_vec_count(struct pci_dev *dev)
939952
}
940953
EXPORT_SYMBOL(pci_msix_vec_count);
941954

942-
/**
943-
* pci_enable_msix - configure device's MSI-X capability structure
944-
* @dev: pointer to the pci_dev data structure of MSI-X device function
945-
* @entries: pointer to an array of MSI-X entries (optional)
946-
* @nvec: number of MSI-X irqs requested for allocation by device driver
947-
*
948-
* Setup the MSI-X capability structure of device function with the number
949-
* of requested irqs upon its software driver call to request for
950-
* MSI-X mode enabled on its hardware device function. A return of zero
951-
* indicates the successful configuration of MSI-X capability structure
952-
* with new allocated MSI-X irqs. A return of < 0 indicates a failure.
953-
* Or a return of > 0 indicates that driver request is exceeding the number
954-
* of irqs or MSI-X vectors available. Driver should use the returned value to
955-
* re-send its request.
956-
**/
957-
int pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries, int nvec)
955+
static int __pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries,
956+
int nvec, bool affinity)
958957
{
959958
int nr_entries;
960959
int i, j;
@@ -986,7 +985,27 @@ int pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries, int nvec)
986985
dev_info(&dev->dev, "can't enable MSI-X (MSI IRQ already assigned)\n");
987986
return -EINVAL;
988987
}
989-
return msix_capability_init(dev, entries, nvec);
988+
return msix_capability_init(dev, entries, nvec, affinity);
989+
}
990+
991+
/**
992+
* pci_enable_msix - configure device's MSI-X capability structure
993+
* @dev: pointer to the pci_dev data structure of MSI-X device function
994+
* @entries: pointer to an array of MSI-X entries (optional)
995+
* @nvec: number of MSI-X irqs requested for allocation by device driver
996+
*
997+
* Setup the MSI-X capability structure of device function with the number
998+
* of requested irqs upon its software driver call to request for
999+
* MSI-X mode enabled on its hardware device function. A return of zero
1000+
* indicates the successful configuration of MSI-X capability structure
1001+
* with new allocated MSI-X irqs. A return of < 0 indicates a failure.
1002+
* Or a return of > 0 indicates that driver request is exceeding the number
1003+
* of irqs or MSI-X vectors available. Driver should use the returned value to
1004+
* re-send its request.
1005+
**/
1006+
int pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries, int nvec)
1007+
{
1008+
return __pci_enable_msix(dev, entries, nvec, false);
9901009
}
9911010
EXPORT_SYMBOL(pci_enable_msix);
9921011

@@ -1039,6 +1058,7 @@ EXPORT_SYMBOL(pci_msi_enabled);
10391058
static int __pci_enable_msi_range(struct pci_dev *dev, int minvec, int maxvec,
10401059
unsigned int flags)
10411060
{
1061+
bool affinity = flags & PCI_IRQ_AFFINITY;
10421062
int nvec;
10431063
int rc;
10441064

@@ -1067,19 +1087,17 @@ static int __pci_enable_msi_range(struct pci_dev *dev, int minvec, int maxvec,
10671087
nvec = maxvec;
10681088

10691089
for (;;) {
1070-
if (flags & PCI_IRQ_AFFINITY) {
1071-
dev->irq_affinity = irq_create_affinity_mask(&nvec);
1090+
if (affinity) {
1091+
nvec = irq_calc_affinity_vectors(dev->irq_affinity,
1092+
nvec);
10721093
if (nvec < minvec)
10731094
return -ENOSPC;
10741095
}
10751096

1076-
rc = msi_capability_init(dev, nvec);
1097+
rc = msi_capability_init(dev, nvec, affinity);
10771098
if (rc == 0)
10781099
return nvec;
10791100

1080-
kfree(dev->irq_affinity);
1081-
dev->irq_affinity = NULL;
1082-
10831101
if (rc < 0)
10841102
return rc;
10851103
if (rc < minvec)
@@ -1111,26 +1129,24 @@ static int __pci_enable_msix_range(struct pci_dev *dev,
11111129
struct msix_entry *entries, int minvec, int maxvec,
11121130
unsigned int flags)
11131131
{
1114-
int nvec = maxvec;
1115-
int rc;
1132+
bool affinity = flags & PCI_IRQ_AFFINITY;
1133+
int rc, nvec = maxvec;
11161134

11171135
if (maxvec < minvec)
11181136
return -ERANGE;
11191137

11201138
for (;;) {
1121-
if (flags & PCI_IRQ_AFFINITY) {
1122-
dev->irq_affinity = irq_create_affinity_mask(&nvec);
1139+
if (affinity) {
1140+
nvec = irq_calc_affinity_vectors(dev->irq_affinity,
1141+
nvec);
11231142
if (nvec < minvec)
11241143
return -ENOSPC;
11251144
}
11261145

1127-
rc = pci_enable_msix(dev, entries, nvec);
1146+
rc = __pci_enable_msix(dev, entries, nvec, affinity);
11281147
if (rc == 0)
11291148
return nvec;
11301149

1131-
kfree(dev->irq_affinity);
1132-
dev->irq_affinity = NULL;
1133-
11341150
if (rc < 0)
11351151
return rc;
11361152
if (rc < minvec)

‎kernel/irq/irqdesc.c‎

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -236,25 +236,24 @@ static int alloc_descs(unsigned int start, unsigned int cnt, int node,
236236
const struct cpumask *mask = NULL;
237237
struct irq_desc *desc;
238238
unsigned int flags;
239-
int i, cpu = -1;
239+
int i;
240240

241-
if (affinity && cpumask_empty(affinity))
242-
return -EINVAL;
241+
/* Validate affinity mask(s) */
242+
if (affinity) {
243+
for (i = 0, mask = affinity; i < cnt; i++, mask++) {
244+
if (cpumask_empty(mask))
245+
return -EINVAL;
246+
}
247+
}
243248

244249
flags = affinity ? IRQD_AFFINITY_MANAGED : 0;
250+
mask = NULL;
245251

246252
for (i = 0; i < cnt; i++) {
247253
if (affinity) {
248-
cpu = cpumask_next(cpu, affinity);
249-
if (cpu >= nr_cpu_ids)
250-
cpu = cpumask_first(affinity);
251-
node = cpu_to_node(cpu);
252-
253-
/*
254-
* For single allocations we use the caller provided
255-
* mask otherwise we use the mask of the target cpu
256-
*/
257-
mask = cnt == 1 ? affinity : cpumask_of(cpu);
254+
node = cpu_to_node(cpumask_first(affinity));
255+
mask = affinity;
256+
affinity++;
258257
}
259258
desc = alloc_desc(start + i, node, flags, mask, owner);
260259
if (!desc)
@@ -481,9 +480,9 @@ EXPORT_SYMBOL_GPL(irq_free_descs);
481480
* @cnt: Number of consecutive irqs to allocate.
482481
* @node: Preferred node on which the irq descriptor should be allocated
483482
* @owner: Owning module (can be NULL)
484-
* @affinity: Optional pointer to an affinity mask which hints where the
485-
* irq descriptors should be allocated and which default
486-
* affinities to use
483+
* @affinity: Optional pointer to an affinity mask array of size @cnt which
484+
* hints where the irq descriptors should be allocated and which
485+
* default affinities to use
487486
*
488487
* Returns the first irq number or error code
489488
*/

0 commit comments

Comments
 (0)