Commit bab65e48 authored by Thomas Gleixner's avatar Thomas Gleixner

PCI/MSI: Sanitize MSI-X checks

There is no point in doing the same sanity checks over and over in a loop
during MSI-X enablement. Put them in front of the loop and return early
when they fail.
Signed-off-by: default avatarThomas Gleixner <tglx@linutronix.de>
Reviewed-by: default avatarJason Gunthorpe <jgg@nvidia.com>
Acked-by: default avatarBjorn Helgaas <bhelgaas@google.com>
Link: https://lore.kernel.org/r/20221111122015.516946468@linutronix.de
parent 12910ffd
...@@ -721,47 +721,31 @@ static int msix_capability_init(struct pci_dev *dev, struct msix_entry *entries, ...@@ -721,47 +721,31 @@ static int msix_capability_init(struct pci_dev *dev, struct msix_entry *entries,
return ret; return ret;
} }
static int __pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries, static bool pci_msix_validate_entries(struct msix_entry *entries, int nvec, int hwsize)
int nvec, struct irq_affinity *affd, int flags)
{ {
int nr_entries;
int i, j; int i, j;
if (!pci_msi_supported(dev, nvec) || dev->current_state != PCI_D0) if (!entries)
return -EINVAL; return true;
nr_entries = pci_msix_vec_count(dev); for (i = 0; i < nvec; i++) {
if (nr_entries < 0) /* Entry within hardware limit? */
return nr_entries; if (entries[i].entry >= hwsize)
if (nvec > nr_entries && !(flags & PCI_IRQ_VIRTUAL)) return false;
return nr_entries;
if (entries) { /* Check for duplicate entries */
/* Check for any invalid entries */ for (j = i + 1; j < nvec; j++) {
for (i = 0; i < nvec; i++) { if (entries[i].entry == entries[j].entry)
if (entries[i].entry >= nr_entries) return false;
return -EINVAL; /* invalid entry */
for (j = i + 1; j < nvec; j++) {
if (entries[i].entry == entries[j].entry)
return -EINVAL; /* duplicate entry */
}
} }
} }
return true;
/* Check whether driver already requested for MSI IRQ */
if (dev->msi_enabled) {
pci_info(dev, "can't enable MSI-X (MSI IRQ already assigned)\n");
return -EINVAL;
}
return msix_capability_init(dev, entries, nvec, affd);
} }
int __pci_enable_msix_range(struct pci_dev *dev, int __pci_enable_msix_range(struct pci_dev *dev, struct msix_entry *entries, int minvec,
struct msix_entry *entries, int minvec, int maxvec, struct irq_affinity *affd, int flags)
int maxvec, struct irq_affinity *affd,
int flags)
{ {
int rc, nvec = maxvec; int hwsize, rc, nvec = maxvec;
if (maxvec < minvec) if (maxvec < minvec)
return -ERANGE; return -ERANGE;
...@@ -774,6 +758,23 @@ int __pci_enable_msix_range(struct pci_dev *dev, ...@@ -774,6 +758,23 @@ int __pci_enable_msix_range(struct pci_dev *dev,
if (WARN_ON_ONCE(dev->msix_enabled)) if (WARN_ON_ONCE(dev->msix_enabled))
return -EINVAL; return -EINVAL;
if (!pci_msi_supported(dev, nvec) || dev->current_state != PCI_D0)
return -EINVAL;
hwsize = pci_msix_vec_count(dev);
if (hwsize < 0)
return hwsize;
if (!pci_msix_validate_entries(entries, nvec, hwsize))
return -EINVAL;
/* PCI_IRQ_VIRTUAL is a horrible hack! */
if (nvec > hwsize && !(flags & PCI_IRQ_VIRTUAL))
nvec = hwsize;
if (nvec < minvec)
return -ENOSPC;
rc = pci_setup_msi_context(dev); rc = pci_setup_msi_context(dev);
if (rc) if (rc)
return rc; return rc;
...@@ -785,7 +786,7 @@ int __pci_enable_msix_range(struct pci_dev *dev, ...@@ -785,7 +786,7 @@ int __pci_enable_msix_range(struct pci_dev *dev,
return -ENOSPC; return -ENOSPC;
} }
rc = __pci_enable_msix(dev, entries, nvec, affd, flags); rc = msix_capability_init(dev, entries, nvec, affd);
if (rc == 0) if (rc == 0)
return nvec; return nvec;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment