2021-12-06 22:29:12

by Thomas Gleixner

[permalink] [raw]
Subject: [patch V2 19/23] PCI/MSI: Sanitize MSIX table map handling

Unmapping the MSIX base mapping in the loops which allocate/free MSI
desciptors is daft and in the way of allowing runtime expansion of MSI-X
descriptors.

Store the mapping in struct pci_dev and free it after freeing the MSI-X
descriptors.

Signed-off-by: Thomas Gleixner <[email protected]>
Tested-by: Juergen Gross <[email protected]>
Reviewed-by: Jason Gunthorpe <[email protected]>
---
drivers/pci/msi/msi.c | 18 ++++++++----------
include/linux/pci.h | 1 +
2 files changed, 9 insertions(+), 10 deletions(-)

--- a/drivers/pci/msi/msi.c
+++ b/drivers/pci/msi/msi.c
@@ -241,14 +241,14 @@ static void free_msi_irqs(struct pci_dev
pci_msi_teardown_msi_irqs(dev);

list_for_each_entry_safe(entry, tmp, msi_list, list) {
- if (entry->pci.msi_attrib.is_msix) {
- if (list_is_last(&entry->list, msi_list))
- iounmap(entry->pci.mask_base);
- }
-
list_del(&entry->list);
free_msi_entry(entry);
}
+
+ if (dev->msix_base) {
+ iounmap(dev->msix_base);
+ dev->msix_base = NULL;
+ }
}

static void pci_intx_for_msi(struct pci_dev *dev, int enable)
@@ -501,10 +501,6 @@ static int msix_setup_entries(struct pci
for (i = 0, curmsk = masks; i < nvec; i++) {
entry = alloc_msi_entry(&dev->dev, 1, curmsk);
if (!entry) {
- if (!i)
- iounmap(base);
- else
- free_msi_irqs(dev);
/* No enough memory. Don't try again */
ret = -ENOMEM;
goto out;
@@ -602,12 +598,14 @@ static int msix_capability_init(struct p
goto out_disable;
}

+ dev->msix_base = base;
+
/* Ensure that all table entries are masked. */
msix_mask_all(base, tsize);

ret = msix_setup_entries(dev, base, entries, nvec, affd);
if (ret)
- goto out_disable;
+ goto out_free;

ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
if (ret)
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -473,6 +473,7 @@ struct pci_dev {
u8 ptm_granularity;
#endif
#ifdef CONFIG_PCI_MSI
+ void __iomem *msix_base;
const struct attribute_group **msi_irq_groups;
#endif
struct pci_vpd vpd;



2021-12-07 21:01:49

by Bjorn Helgaas

[permalink] [raw]
Subject: Re: [patch V2 19/23] PCI/MSI: Sanitize MSIX table map handling

On Mon, Dec 06, 2021 at 11:27:54PM +0100, Thomas Gleixner wrote:
> Unmapping the MSIX base mapping in the loops which allocate/free MSI
> desciptors is daft and in the way of allowing runtime expansion of MSI-X
> descriptors.

s/MSIX/MSI-X/ (subject and first use in commit log)
s/desciptors/descriptors/

> Store the mapping in struct pci_dev and free it after freeing the MSI-X
> descriptors.
>
> Signed-off-by: Thomas Gleixner <[email protected]>
> Tested-by: Juergen Gross <[email protected]>
> Reviewed-by: Jason Gunthorpe <[email protected]>

Acked-by: Bjorn Helgaas <[email protected]>

> ---
> drivers/pci/msi/msi.c | 18 ++++++++----------
> include/linux/pci.h | 1 +
> 2 files changed, 9 insertions(+), 10 deletions(-)
>
> --- a/drivers/pci/msi/msi.c
> +++ b/drivers/pci/msi/msi.c
> @@ -241,14 +241,14 @@ static void free_msi_irqs(struct pci_dev
> pci_msi_teardown_msi_irqs(dev);
>
> list_for_each_entry_safe(entry, tmp, msi_list, list) {
> - if (entry->pci.msi_attrib.is_msix) {
> - if (list_is_last(&entry->list, msi_list))
> - iounmap(entry->pci.mask_base);
> - }
> -
> list_del(&entry->list);
> free_msi_entry(entry);
> }
> +
> + if (dev->msix_base) {
> + iounmap(dev->msix_base);
> + dev->msix_base = NULL;
> + }
> }
>
> static void pci_intx_for_msi(struct pci_dev *dev, int enable)
> @@ -501,10 +501,6 @@ static int msix_setup_entries(struct pci
> for (i = 0, curmsk = masks; i < nvec; i++) {
> entry = alloc_msi_entry(&dev->dev, 1, curmsk);
> if (!entry) {
> - if (!i)
> - iounmap(base);
> - else
> - free_msi_irqs(dev);
> /* No enough memory. Don't try again */
> ret = -ENOMEM;
> goto out;
> @@ -602,12 +598,14 @@ static int msix_capability_init(struct p
> goto out_disable;
> }
>
> + dev->msix_base = base;
> +
> /* Ensure that all table entries are masked. */
> msix_mask_all(base, tsize);
>
> ret = msix_setup_entries(dev, base, entries, nvec, affd);
> if (ret)
> - goto out_disable;
> + goto out_free;
>
> ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
> if (ret)
> --- a/include/linux/pci.h
> +++ b/include/linux/pci.h
> @@ -473,6 +473,7 @@ struct pci_dev {
> u8 ptm_granularity;
> #endif
> #ifdef CONFIG_PCI_MSI
> + void __iomem *msix_base;
> const struct attribute_group **msi_irq_groups;
> #endif
> struct pci_vpd vpd;
>

Subject: [tip: irq/msi] PCI/MSI: Sanitize MSI-X table map handling

The following commit has been merged into the irq/msi branch of tip:

Commit-ID: 85aa607e79f8343f1ea028b29bdf8b6bc99c729a
Gitweb: https://git.kernel.org/tip/85aa607e79f8343f1ea028b29bdf8b6bc99c729a
Author: Thomas Gleixner <[email protected]>
AuthorDate: Mon, 06 Dec 2021 23:27:54 +01:00
Committer: Thomas Gleixner <[email protected]>
CommitterDate: Thu, 09 Dec 2021 11:52:22 +01:00

PCI/MSI: Sanitize MSI-X table map handling

Unmapping the MSI-X base mapping in the loops which allocate/free MSI
descriptors is daft and in the way of allowing runtime expansion of MSI-X
descriptors.

Store the mapping in struct pci_dev and free it after freeing the MSI-X
descriptors.

Signed-off-by: Thomas Gleixner <[email protected]>
Tested-by: Juergen Gross <[email protected]>
Reviewed-by: Jason Gunthorpe <[email protected]>
Acked-by: Bjorn Helgaas <[email protected]>
Link: https://lore.kernel.org/r/[email protected]

---
drivers/pci/msi/msi.c | 18 ++++++++----------
include/linux/pci.h | 1 +
2 files changed, 9 insertions(+), 10 deletions(-)

diff --git a/drivers/pci/msi/msi.c b/drivers/pci/msi/msi.c
index 8926a6c..465fe9e 100644
--- a/drivers/pci/msi/msi.c
+++ b/drivers/pci/msi/msi.c
@@ -241,14 +241,14 @@ static void free_msi_irqs(struct pci_dev *dev)
pci_msi_teardown_msi_irqs(dev);

list_for_each_entry_safe(entry, tmp, msi_list, list) {
- if (entry->pci.msi_attrib.is_msix) {
- if (list_is_last(&entry->list, msi_list))
- iounmap(entry->pci.mask_base);
- }
-
list_del(&entry->list);
free_msi_entry(entry);
}
+
+ if (dev->msix_base) {
+ iounmap(dev->msix_base);
+ dev->msix_base = NULL;
+ }
}

static void pci_intx_for_msi(struct pci_dev *dev, int enable)
@@ -501,10 +501,6 @@ static int msix_setup_entries(struct pci_dev *dev, void __iomem *base,
for (i = 0, curmsk = masks; i < nvec; i++) {
entry = alloc_msi_entry(&dev->dev, 1, curmsk);
if (!entry) {
- if (!i)
- iounmap(base);
- else
- free_msi_irqs(dev);
/* No enough memory. Don't try again */
ret = -ENOMEM;
goto out;
@@ -602,12 +598,14 @@ static int msix_capability_init(struct pci_dev *dev, struct msix_entry *entries,
goto out_disable;
}

+ dev->msix_base = base;
+
/* Ensure that all table entries are masked. */
msix_mask_all(base, tsize);

ret = msix_setup_entries(dev, base, entries, nvec, affd);
if (ret)
- goto out_disable;
+ goto out_free;

ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
if (ret)
diff --git a/include/linux/pci.h b/include/linux/pci.h
index 18a75c8..8cb1036 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -473,6 +473,7 @@ struct pci_dev {
u8 ptm_granularity;
#endif
#ifdef CONFIG_PCI_MSI
+ void __iomem *msix_base;
const struct attribute_group **msi_irq_groups;
#endif
struct pci_vpd vpd;