Skip to contentSkip to conversation

PCI: Remove pcim_iounmap_regions()

PATCH Superseded Philipp Stanner 4 participants 19 messages Aug 21, 2024

Subsystems (16)

v2 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v2 cover PCI: Remove pcim_iounmap_regions()

Changes in v2:
- Add a fix for the UB stack usage bug in vdap/solidrun. Separate patch, put stable kernel on CC. (Christophe, Andy).
- Drop unnecessary pcim_release_region() in mtip32xx (Andy)
- Consequently, drop patch "PCI: Make pcim_release_region() a public function", since there's no user anymore. (obsoletes the squash requested by Damien).
- Fix bug in patch "block: mtip32xx ..." where accidentally BAR 1 instead of MTIP_ABAR was requested.
- vdap/solidrun:
• make 'i' an 'unsigned short' (Andy, me)
• Use 'continue' to simplify loop (Andy)
• Remove leftover blank line
- Apply given Reviewed- / acked-bys (Andy, Damien, Bartosz)

Important things first:
This series is based on [1] and [2] which Bjorn Helgaas has currently queued for v6.12 in the PCI tree.

This series shall remove pcim_iounmap_regions() in order to make way to remove its brother, pcim_iomap_regions().

@Bjorn: Feel free to squash the PCI commits.

Regards,
P.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/

Philipp Stanner (9):
PCI: Make pcim_iounmap_region() a public function fpga/dfl-pci.c: Replace deprecated PCI functions
block: mtip32xx: Replace deprecated PCI functions
gpio: Replace deprecated PCI functions
ethernet: cavium: Replace deprecated PCI functions
ethernet: stmicro: Simplify PCI devres usage
vdpa: solidrun: Fix potential UB bug with devres
vdap: solidrun: Replace deprecated PCI functions
PCI: Remove pcim_iounmap_regions()

 .../driver-api/driver-model/devres.rst        |  1 -
 drivers/block/mtip32xx/mtip32xx.c             | 11 ++--
 drivers/fpga/dfl-pci.c                        |  9 ++--
 drivers/gpio/gpio-merrifield.c                | 14 ++---
 .../net/ethernet/cavium/common/cavium_ptp.c   | 10 ++--
 .../ethernet/stmicro/stmmac/dwmac-loongson.c  | 25 +++------
 .../net/ethernet/stmicro/stmmac/stmmac_pci.c  | 18 +++----
 drivers/pci/devres.c                          | 24 +--------
 drivers/vdpa/solidrun/snet_main.c             | 52 +++++++------------
 include/linux/pci.h                           |  2 +-
 10 files changed, 57 insertions(+), 109 deletions(-)
Signature · 1 line
2.46.0
#1 lore raw

PATCH v2 1/9 PCI: Make pcim_iounmap_region() a public function

The function pcim_iounmap_regions() is problematic because it uses a bitmask mechanism to release / iounmap multiple BARs at once. It, thus, prevents getting rid of the problematic iomap table mechanism which was deprecated in commit e354bb84a4c1 ("PCI: Deprecate pcim_iomap_table(), pcim_iomap_regions_request_all()").

Make pcim_iounmap_region() public as the successor of pcim_iounmap_regions().

---
 drivers/pci/devres.c | 3 ++-
 include/linux/pci.h  | 1 +
 2 files changed, 3 insertions(+), 1 deletion(-)
drivers/pci/devres.c +2 -1
index b97589e99fad..4dbba385e6b4 100644
@@ -771,7 +771,7 @@ EXPORT_SYMBOL(pcim_iomap_region);
  * Unmap a BAR and release its region manually. Only pass BARs that were
  * previously mapped by pcim_iomap_region().
  */
-static void pcim_iounmap_region(struct pci_dev *pdev, int bar)
+void pcim_iounmap_region(struct pci_dev *pdev, int bar)
 {
 	struct pcim_addr_devres res_searched;
 
@@ -782,6 +782,7 @@ static void pcim_iounmap_region(struct pci_dev *pdev, int bar)
 	devres_release(&pdev->dev, pcim_addr_resource_release,
 			pcim_addr_resources_match, &res_searched);
 }
+EXPORT_SYMBOL(pcim_iounmap_region);
 
 /**
  * pcim_iomap_regions - Request and iomap PCI BARs (DEPRECATED)
include/linux/pci.h +1 -0
index 01b9f1a351be..9625d8a7b655 100644
@@ -2297,6 +2297,7 @@ void __iomem * const *pcim_iomap_table(struct pci_dev *pdev);
 int pcim_request_region(struct pci_dev *pdev, int bar, const char *name);
 void __iomem *pcim_iomap_region(struct pci_dev *pdev, int bar,
 				       const char *name);
+void pcim_iounmap_region(struct pci_dev *pdev, int bar);
 int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name);
 int pcim_iomap_regions_request_all(struct pci_dev *pdev, int mask,
 				   const char *name);
Signature · 1 line
2.46.0
#2 lore raw

PATCH v2 2/9 fpga/dfl-pci.c: Replace deprecated PCI functions

In reply to Philipp Stanner

pcim_iomap_regions() and pcim_iomap_table() have been deprecated by the PCI subsystem in commit e354bb84a4c1 ("PCI: Deprecate pcim_iomap_table(), pcim_iomap_regions_request_all()").

Port dfl-pci.c to the successor, pcim_iomap_region().

Consistently, replace pcim_iounmap_regions() with pcim_iounmap_region().

---
 drivers/fpga/dfl-pci.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/fpga/dfl-pci.c +6 -3
index 80cac3a5f976..2099c497feec 100644
@@ -41,10 +41,13 @@ struct cci_drvdata {
 
 static void __iomem *cci_pci_ioremap_bar0(struct pci_dev *pcidev)
 {
-	if (pcim_iomap_regions(pcidev, BIT(0), DRV_NAME))
+	void __iomem *bar0;
+
+	bar0 = pcim_iomap_region(pcidev, 0, DRV_NAME);
+	if (IS_ERR(bar0))
 		return NULL;
 
-	return pcim_iomap_table(pcidev)[0];
+	return bar0;
 }
 
 static int cci_pci_alloc_irq(struct pci_dev *pcidev)
@@ -296,7 +299,7 @@ static int find_dfls_by_default(struct pci_dev *pcidev,
 	}
 
 	/* release I/O mappings for next step enumeration */
-	pcim_iounmap_regions(pcidev, BIT(0));
+	pcim_iounmap_region(pcidev, 0);
 
 	return ret;
 }
Signature · 1 line
2.46.0
#3 lore raw

Re: [PATCH v2 2/9] fpga/dfl-pci.c: Replace deprecated PCI functions

Quoted 11 lines · Philipp Stanner

pcim_iomap_regions() and pcim_iomap_table() have been deprecated by the PCI subsystem in commit e354bb84a4c1 ("PCI: Deprecate pcim_iomap_table(), pcim_iomap_regions_request_all()").

Port dfl-pci.c to the successor, pcim_iomap_region().

Consistently, replace pcim_iounmap_regions() with pcim_iounmap_region().

static void __iomem *cci_pci_ioremap_bar0(struct pci_dev *pcidev) {

drivers/fpga/dfl-pci.c struct cci_drvdata { · line 51

Quoted 10 lines
drivers/fpga/dfl-pci.c +5 -2
-       if (pcim_iomap_regions(pcidev, BIT(0), DRV_NAME))
+       void __iomem *bar0;
+
+       bar0 = pcim_iomap_region(pcidev, 0, DRV_NAME);
+       if (IS_ERR(bar0))
                return NULL;
 
-       return pcim_iomap_table(pcidev)[0];
+       return bar0;
 }

Now this becomes an unneeded wrapper on pcim_ioremap_region(). Can we kill this helper completely?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw

PATCH v2 3/9 block: mtip32xx: Replace deprecated PCI functions

In reply to Philipp Stanner

pcim_iomap_regions() and pcim_iomap_table() have been deprecated by the PCI subsystem in commit e354bb84a4c1 ("PCI: Deprecate pcim_iomap_table(), pcim_iomap_regions_request_all()").

In mtip32xx, these functions can easily be replaced by their respective successors, pcim_request_region() and pcim_iomap(). Moreover, the driver's calls to pcim_iounmap_regions() in probe()'s error path and in remove() are not necessary. Cleanup can be performed by PCI devres automatically.

Replace pcim_iomap_regions() and pcim_iomap_table().

Remove the calls to pcim_iounmap_regions().

---
 drivers/block/mtip32xx/mtip32xx.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/block/mtip32xx/mtip32xx.c +5 -6
index c6ef0546ffc9..fcd5806621c7 100644
@@ -2716,7 +2716,9 @@ static int mtip_hw_init(struct driver_data *dd)
 	int rv;
 	unsigned long timeout, timetaken;
 
-	dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
+	dd->mmio = pcim_iomap(dd->pdev, MTIP_ABAR, 0);
+	if (!dd->mmio)
+		return -ENOMEM;
 
 	mtip_detect_product(dd);
 	if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
@@ -3726,9 +3728,9 @@ static int mtip_pci_probe(struct pci_dev *pdev,
 	}
 
 	/* Map BAR5 to memory. */
-	rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
+	rv = pcim_request_region(pdev, MTIP_ABAR, MTIP_DRV_NAME);
 	if (rv < 0) {
-		dev_err(&pdev->dev, "Unable to map regions\n");
+		dev_err(&pdev->dev, "Unable to request regions\n");
 		goto iomap_err;
 	}
 
@@ -3849,8 +3851,6 @@ static int mtip_pci_probe(struct pci_dev *pdev,
 		drop_cpu(dd->work[2].cpu_binding);
 	}
 setmask_err:
-	pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
-
 iomap_err:
 	kfree(dd);
 	pci_set_drvdata(pdev, NULL);
@@ -3925,7 +3925,6 @@ static void mtip_pci_remove(struct pci_dev *pdev)
 
 	pci_disable_msi(pdev);
 
-	pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
 	pci_set_drvdata(pdev, NULL);
 
 	put_disk(dd->disk);
Signature · 1 line
2.46.0
#5 lore raw

Re: [PATCH v2 3/9] block: mtip32xx: Replace deprecated PCI functions

Quoted 14 lines · Philipp Stanner

pcim_iomap_regions() and pcim_iomap_table() have been deprecated by the PCI subsystem in commit e354bb84a4c1 ("PCI: Deprecate pcim_iomap_table(), pcim_iomap_regions_request_all()").

In mtip32xx, these functions can easily be replaced by their respective successors, pcim_request_region() and pcim_iomap(). Moreover, the driver's calls to pcim_iounmap_regions() in probe()'s error path and in remove() are not necessary. Cleanup can be performed by PCI devres automatically.

Replace pcim_iomap_regions() and pcim_iomap_table().

Remove the calls to pcim_iounmap_regions().

...

drivers/block/mtip32xx/mtip32xx.c static int mtip_pci_probe(struct pci_dev *pdev, · line 3854

Quoted 4 lines

setmask_err:

drivers/block/mtip32xx/mtip32xx.c +0 -2
-       pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
-
 iomap_err:

Now one of the labels is redundant.

Quoted 2 lines
        kfree(dd);
        pci_set_drvdata(pdev, NULL);
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw

PATCH v2 4/9 gpio: Replace deprecated PCI functions

In reply to Philipp Stanner

pcim_iomap_regions() and pcim_iomap_table() have been deprecated by the PCI subsystem in commit e354bb84a4c1 ("PCI: Deprecate pcim_iomap_table(), pcim_iomap_regions_request_all()").

Replace those functions with calls to pcim_iomap_region().

---
 drivers/gpio/gpio-merrifield.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/gpio/gpio-merrifield.c +7 -7
index 421d7e3a6c66..274afcba31e6 100644
@@ -78,24 +78,24 @@ static int mrfld_gpio_probe(struct pci_dev *pdev, const struct pci_device_id *id
 	if (retval)
 		return retval;
 
-	retval = pcim_iomap_regions(pdev, BIT(1) | BIT(0), pci_name(pdev));
-	if (retval)
-		return dev_err_probe(dev, retval, "I/O memory mapping error\n");
-
-	base = pcim_iomap_table(pdev)[1];
+	base = pcim_iomap_region(pdev, 1, pci_name(pdev));
+	if (IS_ERR(base))
+		return dev_err_probe(dev, PTR_ERR(base), "I/O memory mapping error\n");
 
 	irq_base = readl(base + 0 * sizeof(u32));
 	gpio_base = readl(base + 1 * sizeof(u32));
 
 	/* Release the IO mapping, since we already get the info from BAR1 */
-	pcim_iounmap_regions(pdev, BIT(1));
+	pcim_iounmap_region(pdev, 1);
 
 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
 	if (!priv)
 		return -ENOMEM;
 
 	priv->dev = dev;
-	priv->reg_base = pcim_iomap_table(pdev)[0];
+	priv->reg_base = pcim_iomap_region(pdev, 0, pci_name(pdev));
+	if (IS_ERR(priv->reg_base))
+		return dev_err_probe(dev, PTR_ERR(base), "I/O memory mapping error\n");
 
 	priv->pin_info.pin_ranges = mrfld_gpio_ranges;
 	priv->pin_info.nranges = ARRAY_SIZE(mrfld_gpio_ranges);
Signature · 1 line
2.46.0
#7 lore raw

PATCH v2 5/9 ethernet: cavium: Replace deprecated PCI functions

In reply to Philipp Stanner

pcim_iomap_regions() and pcim_iomap_table() have been deprecated by the PCI subsystem in commit e354bb84a4c1 ("PCI: Deprecate pcim_iomap_table(), pcim_iomap_regions_request_all()").

Replace these functions with the function pcim_iomap_region().

---
 drivers/net/ethernet/cavium/common/cavium_ptp.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/net/ethernet/cavium/common/cavium_ptp.c +5 -5
index 9fd717b9cf69..1849c62cde1d 100644
@@ -239,11 +239,11 @@ static int cavium_ptp_probe(struct pci_dev *pdev,
 	if (err)
 		goto error_free;
 
-	err = pcim_iomap_regions(pdev, 1 << PCI_PTP_BAR_NO, pci_name(pdev));
-	if (err)
+	clock->reg_base = pcim_iomap_region(pdev, PCI_PTP_BAR_NO, pci_name(pdev));
+	if (IS_ERR(clock->reg_base)) {
+		err = PTR_ERR(clock->reg_base);
 		goto error_free;
-
-	clock->reg_base = pcim_iomap_table(pdev)[PCI_PTP_BAR_NO];
+	}
 
 	spin_lock_init(&clock->spin_lock);
 
@@ -292,7 +292,7 @@ static int cavium_ptp_probe(struct pci_dev *pdev,
 	clock_cfg = readq(clock->reg_base + PTP_CLOCK_CFG);
 	clock_cfg &= ~PTP_CLOCK_CFG_PTP_EN;
 	writeq(clock_cfg, clock->reg_base + PTP_CLOCK_CFG);
-	pcim_iounmap_regions(pdev, 1 << PCI_PTP_BAR_NO);
+	pcim_iounmap_region(pdev, PCI_PTP_BAR_NO);
 
 error_free:
 	devm_kfree(dev, clock);
Signature · 1 line
2.46.0
#8 lore raw

PATCH v2 6/9 ethernet: stmicro: Simplify PCI devres usage

In reply to Philipp Stanner

stmicro uses PCI devres in the wrong way. Resources requested through pcim_* functions don't need to be cleaned up manually in the remove() callback or in the error unwind path of a probe() function.

Moreover, there is an unnecessary loop which only requests and ioremaps BAR 0, but iterates over all BARs nevertheless.

Furthermore, pcim_iomap_regions() and pcim_iomap_table() have been deprecated by the PCI subsystem in commit e354bb84a4c1 ("PCI: Deprecate pcim_iomap_table(), pcim_iomap_regions_request_all()").

Replace these functions with pcim_iomap_region().

Remove the unnecessary manual pcim_* cleanup calls.

Remove the unnecessary loop over all BARs.

---
 .../ethernet/stmicro/stmmac/dwmac-loongson.c  | 25 +++++--------------
 .../net/ethernet/stmicro/stmmac/stmmac_pci.c  | 18 +++++--------
 2 files changed, 12 insertions(+), 31 deletions(-)
drivers/net/ethernet/stmicro/stmmac/dwmac-loongson.c +6 -19
index 9e40c28d453a..5d42a9fad672 100644
@@ -50,7 +50,7 @@ static int loongson_dwmac_probe(struct pci_dev *pdev, const struct pci_device_id
 	struct plat_stmmacenet_data *plat;
 	struct stmmac_resources res;
 	struct device_node *np;
-	int ret, i, phy_mode;
+	int ret, phy_mode;
 
 	np = dev_of_node(&pdev->dev);
 
@@ -88,14 +88,11 @@ static int loongson_dwmac_probe(struct pci_dev *pdev, const struct pci_device_id
 		goto err_put_node;
 	}
 
-	/* Get the base address of device */
-	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
-		if (pci_resource_len(pdev, i) == 0)
-			continue;
-		ret = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev));
-		if (ret)
-			goto err_disable_device;
-		break;
+	memset(&res, 0, sizeof(res));
+	res.addr = pcim_iomap_region(pdev, 0, pci_name(pdev));
+	if (IS_ERR(res.addr)) {
+		ret = PTR_ERR(res.addr);
+		goto err_disable_device;
 	}
 
 	plat->bus_id = of_alias_get_id(np, "ethernet");
@@ -116,8 +113,6 @@ static int loongson_dwmac_probe(struct pci_dev *pdev, const struct pci_device_id
 
 	loongson_default_data(plat);
 	pci_enable_msi(pdev);
-	memset(&res, 0, sizeof(res));
-	res.addr = pcim_iomap_table(pdev)[0];
 
 	res.irq = of_irq_get_byname(np, "macirq");
 	if (res.irq < 0) {
@@ -158,18 +153,10 @@ static void loongson_dwmac_remove(struct pci_dev *pdev)
 {
 	struct net_device *ndev = dev_get_drvdata(&pdev->dev);
 	struct stmmac_priv *priv = netdev_priv(ndev);
-	int i;
 
 	of_node_put(priv->plat->mdio_node);
 	stmmac_dvr_remove(&pdev->dev);
 
-	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
-		if (pci_resource_len(pdev, i) == 0)
-			continue;
-		pcim_iounmap_regions(pdev, BIT(i));
-		break;
-	}
-
 	pci_disable_msi(pdev);
 	pci_disable_device(pdev);
 }
drivers/net/ethernet/stmicro/stmmac/stmmac_pci.c +6 -12
index 352b01678c22..f89a8a54c4e8 100644
@@ -188,11 +188,11 @@ static int stmmac_pci_probe(struct pci_dev *pdev,
 		return ret;
 	}
 
-	/* Get the base address of device */
+	/* Request the base address BAR of device */
 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
 		if (pci_resource_len(pdev, i) == 0)
 			continue;
-		ret = pcim_iomap_regions(pdev, BIT(i), pci_name(pdev));
+		ret = pcim_request_region(pdev, i, pci_name(pdev));
 		if (ret)
 			return ret;
 		break;
@@ -205,7 +205,10 @@ static int stmmac_pci_probe(struct pci_dev *pdev,
 		return ret;
 
 	memset(&res, 0, sizeof(res));
-	res.addr = pcim_iomap_table(pdev)[i];
+	/* Get the base address of device */
+	res.addr = pcim_iomap(pdev, i, 0);
+	if (!res.addr)
+		return -ENOMEM;
 	res.wol_irq = pdev->irq;
 	res.irq = pdev->irq;
 
@@ -231,16 +234,7 @@ static int stmmac_pci_probe(struct pci_dev *pdev,
  */
 static void stmmac_pci_remove(struct pci_dev *pdev)
 {
-	int i;
-
 	stmmac_dvr_remove(&pdev->dev);
-
-	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
-		if (pci_resource_len(pdev, i) == 0)
-			continue;
-		pcim_iounmap_regions(pdev, BIT(i));
-		break;
-	}
 }
 
 static int __maybe_unused stmmac_pci_suspend(struct device *dev)
Signature · 1 line
2.46.0
#9 lore raw

Re: [PATCH v2 6/9] ethernet: stmicro: Simplify PCI devres usage

Quoted 7 lines · Philipp Stanner

stmicro uses PCI devres in the wrong way. Resources requested through pcim_* functions don't need to be cleaned up manually in the remove() callback or in the error unwind path of a probe() function.

Moreover, there is an unnecessary loop which only requests and ioremaps BAR 0, but iterates over all BARs nevertheless.

Seems like loongson was cargo-culted a lot without a clear understanding of this code in the main driver...

Quoted 9 lines

Furthermore, pcim_iomap_regions() and pcim_iomap_table() have been deprecated by the PCI subsystem in commit e354bb84a4c1 ("PCI: Deprecate pcim_iomap_table(), pcim_iomap_regions_request_all()").

Replace these functions with pcim_iomap_region().

Remove the unnecessary manual pcim_* cleanup calls.

Remove the unnecessary loop over all BARs.

...

drivers/net/ethernet/stmicro/stmmac/dwmac-loongson.c static void loongson_dwmac_remove(struct pci_dev *pdev) · line 169

Quoted 4 lines
drivers/net/ethernet/stmicro/stmmac/dwmac-loongson.c +0 -4
-       for (i = 0; i < PCI_STD_NUM_BARS; i++) {
-               if (pci_resource_len(pdev, i) == 0)
-                       continue;
-               pcim_iounmap_regions(pdev, BIT(i));

Here is the BARx, which contradicts the probe :-)

Quoted 2 lines
(quoted diff) +0 -2
-               break;
-       }
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#10 lore raw

Re: [PATCH v2 6/9] ethernet: stmicro: Simplify PCI devres usage

Quoted 27 lines · Andy Shevchenko
Quoted 10 lines · Philipp Stanner

stmicro uses PCI devres in the wrong way. Resources requested through pcim_* functions don't need to be cleaned up manually in the
remove() callback or in the error unwind path of a probe() function.

Moreover, there is an unnecessary loop which only requests and ioremaps
BAR 0, but iterates over all BARs nevertheless.

Seems like loongson was cargo-culted a lot without a clear understanding of this code in the main driver...

Quoted 10 lines

Furthermore, pcim_iomap_regions() and pcim_iomap_table() have been deprecated by the PCI subsystem in commit e354bb84a4c1 ("PCI:
Deprecate
pcim_iomap_table(), pcim_iomap_regions_request_all()").

Replace these functions with pcim_iomap_region().

Remove the unnecessary manual pcim_* cleanup calls.

Remove the unnecessary loop over all BARs.

Quoted 8 lines

...

Quoted 4 lines

-       for (i = 0; i < PCI_STD_NUM_BARS; i++) { -               if (pci_resource_len(pdev, i) == 0) -                       continue;
-               pcim_iounmap_regions(pdev, BIT(i));

Here is the BARx, which contradicts the probe :-)

I'm not sure what should be done about it. The only interesting question is whether the other code with pcim_iomap_regions(... BIT(i) does also only grap BAR 0.
In that case the driver wouldn't even be knowing what its own hardware is / does, though.

P.

Quoted 4 lines
Quoted 3 lines

-               break;
-       }

#11 lore raw

Re: [PATCH v2 6/9] ethernet: stmicro: Simplify PCI devres usage

Quoted 3 lines · Philipp Stanner
Quoted 2 lines · Andy Shevchenko

On Wed, Aug 21, 2024 at 10:19 AM Philipp Stanner
<[email protected]> wrote:

...

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

-       for (i = 0; i < PCI_STD_NUM_BARS; i++) { -               if (pci_resource_len(pdev, i) == 0) -                       continue;
-               pcim_iounmap_regions(pdev, BIT(i));

Quoted 7 lines
Quoted 1 line

Here is the BARx, which contradicts the probe :-)

I'm not sure what should be done about it. The only interesting question is whether the other code with pcim_iomap_regions(... BIT(i) does also only grap BAR 0.
In that case the driver wouldn't even be knowing what its own hardware is / does, though.

I think your patch does the right thing already.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

-               break;
-       }

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#12 lore raw

PATCH v2 7/9 vdpa: solidrun: Fix potential UB bug with devres

In reply to Philipp Stanner

In psnet_open_pf_bar() a string later passed to pcim_iomap_regions() is placed on the stack. Neither pcim_iomap_regions() nor the functions it calls copy that string.

Should the string later ever be used, this, consequently, causes undefined behavior since the stack frame will by then have disappeared.

Fix the bug by allocating the string on the heap through devm_kasprintf().

---
 drivers/vdpa/solidrun/snet_main.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/vdpa/solidrun/snet_main.c +5 -2
index 99428a04068d..4d42a05d70fc 100644
@@ -555,7 +555,7 @@ static const struct vdpa_config_ops snet_config_ops = {
 
 static int psnet_open_pf_bar(struct pci_dev *pdev, struct psnet *psnet)
 {
-	char name[50];
+	char *name;
 	int ret, i, mask = 0;
 	/* We don't know which BAR will be used to communicate..
 	 * We will map every bar with len > 0.
@@ -573,7 +573,10 @@ static int psnet_open_pf_bar(struct pci_dev *pdev, struct psnet *psnet)
 		return -ENODEV;
 	}
 
-	snprintf(name, sizeof(name), "psnet[%s]-bars", pci_name(pdev));
+	name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "psnet[%s]-bars", pci_name(pdev));
+	if (!name)
+		return -ENOMEM;
+
 	ret = pcim_iomap_regions(pdev, mask, name);
 	if (ret) {
 		SNET_ERR(pdev, "Failed to request and map PCI BARs\n");
Signature · 1 line
2.46.0
#13 lore raw

Re: [PATCH v2 7/9] vdpa: solidrun: Fix potential UB bug with devres

Le 21/08/2024 à 09:18, Philipp Stanner a écrit :

Quoted 25 lines

In psnet_open_pf_bar() a string later passed to pcim_iomap_regions() is placed on the stack. Neither pcim_iomap_regions() nor the functions it calls copy that string.

Should the string later ever be used, this, consequently, causes undefined behavior since the stack frame will by then have disappeared.

Fix the bug by allocating the string on the heap through devm_kasprintf().

---
  drivers/vdpa/solidrun/snet_main.c | 7 +++++--
  1 file changed, 5 insertions(+), 2 deletions(-)
drivers/vdpa/solidrun/snet_main.c +0 -0
index 99428a04068d..4d42a05d70fc 100644

drivers/vdpa/solidrun/snet_main.c static const struct vdpa_config_ops snet_config_ops = { · line 556

Quoted 2 lines
drivers/vdpa/solidrun/snet_main.c +0 -0
@@ -555,7 +555,7 @@ static const struct vdpa_config_ops snet_config_ops = {
 
  static int psnet_open_pf_bar(struct pci_dev *pdev, struct psnet *psnet)

snet_open_vf_bar() also needs the same modification (see Andy's comment on patch 8/9)

CJ

Quoted 18 lines
drivers/vdpa/solidrun/snet_main.c +5 -2
  {
-	char name[50];
+	char *name;
  	int ret, i, mask = 0;
  	/* We don't know which BAR will be used to communicate..
  	 * We will map every bar with len > 0.
@@ -573,7 +573,10 @@ static int psnet_open_pf_bar(struct pci_dev *pdev, struct psnet *psnet)
  		return -ENODEV;
  	}
 
-	snprintf(name, sizeof(name), "psnet[%s]-bars", pci_name(pdev));
+	name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "psnet[%s]-bars", pci_name(pdev));
+	if (!name)
+		return -ENOMEM;
+
  	ret = pcim_iomap_regions(pdev, mask, name);
  	if (ret) {
  		SNET_ERR(pdev, "Failed to request and map PCI BARs\n");
#14 lore raw

Re: [PATCH v2 7/9] vdpa: solidrun: Fix potential UB bug with devres

In reply to Philipp Stanner
Quoted 10 lines · Philipp Stanner

In psnet_open_pf_bar() a string later passed to pcim_iomap_regions() is placed on the stack. Neither pcim_iomap_regions() nor the functions it calls copy that string.

Should the string later ever be used, this, consequently, causes undefined behavior since the stack frame will by then have disappeared.

Fix the bug by allocating the string on the heap through devm_kasprintf().

Quoted 6 lines

I don't get why is this a part of a cleanup series - looks like an unrelated bugfix?

Quoted 31 lines
---
 drivers/vdpa/solidrun/snet_main.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/vdpa/solidrun/snet_main.c +5 -2
index 99428a04068d..4d42a05d70fc 100644
@@ -555,7 +555,7 @@ static const struct vdpa_config_ops snet_config_ops = {
 
 static int psnet_open_pf_bar(struct pci_dev *pdev, struct psnet *psnet)
 {
-	char name[50];
+	char *name;
 	int ret, i, mask = 0;
 	/* We don't know which BAR will be used to communicate..
 	 * We will map every bar with len > 0.
@@ -573,7 +573,10 @@ static int psnet_open_pf_bar(struct pci_dev *pdev, struct psnet *psnet)
 		return -ENODEV;
 	}
 
-	snprintf(name, sizeof(name), "psnet[%s]-bars", pci_name(pdev));
+	name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "psnet[%s]-bars", pci_name(pdev));
+	if (!name)
+		return -ENOMEM;
+
 	ret = pcim_iomap_regions(pdev, mask, name);
 	if (ret) {
 		SNET_ERR(pdev, "Failed to request and map PCI BARs\n");
Signature · 1 line
2.46.0
#15 lore raw

Re: [PATCH v2 7/9] vdpa: solidrun: Fix potential UB bug with devres

Quoted 22 lines · Michael S. Tsirkin
Quoted 13 lines · Philipp Stanner

In psnet_open_pf_bar() a string later passed to pcim_iomap_regions() is
placed on the stack. Neither pcim_iomap_regions() nor the functions it
calls copy that string.

Should the string later ever be used, this, consequently, causes undefined behavior since the stack frame will by then have disappeared.

Fix the bug by allocating the string on the heap through devm_kasprintf().

Quoted 7 lines
Fixes 51a8f9d7f587 ("virtio: vdpa: new SolidNET DPU driver.")

Closes:
https://lore.kernel.org/all/[email protected]/

Quoted 2 lines

I don't get why is this a part of a cleanup series - looks like an unrelated bugfix?

It was discovered in the discussion of v1 of this series.

It indeed is an unrelated bugfix and could be merged separately. But my patch #8 depends on it.

So it would be convenient to merge it into mainline through this series, and have stable just pick patch #7.

Or should it be done differently, in your opinion?

P.

Quoted 41 lines
Quoted 40 lines

---
 drivers/vdpa/solidrun/snet_main.c | 7 +++++--

 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/vdpa/solidrun/snet_main.c +0 -0
index 99428a04068d..4d42a05d70fc 100644
@@ -555,7 +555,7 @@ static const struct vdpa_config_ops
 snet_config_ops = {


 static int psnet_open_pf_bar(struct pci_dev *pdev, struct psnet *psnet)
 {

drivers/vdpa/solidrun/snet_main.c +1 -1
-	char name[50];
+	char *name;

  int ret, i, mask = 0;
  /* We don't know which BAR will be used to communicate..
  * We will map every bar with len > 0.

drivers/vdpa/solidrun/snet_main.c +0 -0
@@ -573,7 +573,10 @@ static int psnet_open_pf_bar(struct pci_dev
 *pdev, struct psnet *psnet)
  		return -ENODEV;
  	}

drivers/vdpa/solidrun/snet_main.c +0 -1
-	snprintf(name, sizeof(name), "psnet[%s]-bars",

pci_name(pdev));

drivers/vdpa/solidrun/snet_main.c +1 -0
+	name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "psnet[%s]-

bars", pci_name(pdev));

drivers/vdpa/solidrun/snet_main.c +3 -0
+	if (!name)
+		return -ENOMEM;
+

  ret = pcim_iomap_regions(pdev, mask, name); if (ret) {
  SNET_ERR(pdev, "Failed to request and map PCI BARs\n");

Signature · 1 line
2.46.0
#16 lore raw

PATCH v2 8/9 vdap: solidrun: Replace deprecated PCI functions

In reply to Philipp Stanner

solidrun utilizes pcim_iomap_regions(), which has been deprecated by the PCI subsystem in commit e354bb84a4c1 ("PCI: Deprecate pcim_iomap_table(), pcim_iomap_regions_request_all()"), among other things because it forces usage of quite a complicated bitmask mechanism. The bitmask handling code can entirely be removed by replacing pcim_iomap_regions() and pcim_iomap_table().

Replace pcim_iomap_regions() and pcim_iomap_table() with pci_iomap_region().

---
 drivers/vdpa/solidrun/snet_main.c | 53 +++++++++++--------------------
 1 file changed, 18 insertions(+), 35 deletions(-)
drivers/vdpa/solidrun/snet_main.c +18 -35
index 4d42a05d70fc..ce336f261649 100644
@@ -556,36 +556,25 @@ static const struct vdpa_config_ops snet_config_ops = {
 static int psnet_open_pf_bar(struct pci_dev *pdev, struct psnet *psnet)
 {
 	char *name;
-	int ret, i, mask = 0;
-	/* We don't know which BAR will be used to communicate..
-	 * We will map every bar with len > 0.
-	 *
-	 * Later, we will discover the BAR and unmap all other BARs.
-	 */
-	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
-		if (pci_resource_len(pdev, i))
-			mask |= (1 << i);
-	}
-
-	/* No BAR can be used.. */
-	if (!mask) {
-		SNET_ERR(pdev, "Failed to find a PCI BAR\n");
-		return -ENODEV;
-	}
+	unsigned short i;
 
 	name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "psnet[%s]-bars", pci_name(pdev));
 	if (!name)
 		return -ENOMEM;
 
-	ret = pcim_iomap_regions(pdev, mask, name);
-	if (ret) {
-		SNET_ERR(pdev, "Failed to request and map PCI BARs\n");
-		return ret;
-	}
-
+	/* We don't know which BAR will be used to communicate..
+	 * We will map every bar with len > 0.
+	 *
+	 * Later, we will discover the BAR and unmap all other BARs.
+	 */
 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
-		if (mask & (1 << i))
-			psnet->bars[i] = pcim_iomap_table(pdev)[i];
+		if (!pci_resource_len(pdev, i))
+			continue;
+		psnet->bars[i] = pcim_iomap_region(pdev, i, name);
+		if (IS_ERR(psnet->bars[i])) {
+			SNET_ERR(pdev, "Failed to request and map PCI BARs\n");
+			return PTR_ERR(psnet->bars[i]);
+		}
 	}
 
 	return 0;
@@ -594,18 +583,15 @@ static int psnet_open_pf_bar(struct pci_dev *pdev, struct psnet *psnet)
 static int snet_open_vf_bar(struct pci_dev *pdev, struct snet *snet)
 {
 	char name[50];
-	int ret;
 
 	snprintf(name, sizeof(name), "snet[%s]-bar", pci_name(pdev));
 	/* Request and map BAR */
-	ret = pcim_iomap_regions(pdev, BIT(snet->psnet->cfg.vf_bar), name);
-	if (ret) {
+	snet->bar = pcim_iomap_region(pdev, snet->psnet->cfg.vf_bar, name);
+	if (IS_ERR(snet->bar)) {
 		SNET_ERR(pdev, "Failed to request and map PCI BAR for a VF\n");
-		return ret;
+		return PTR_ERR(snet->bar);
 	}
 
-	snet->bar = pcim_iomap_table(pdev)[snet->psnet->cfg.vf_bar];
-
 	return 0;
 }
 
@@ -653,15 +639,12 @@ static int psnet_detect_bar(struct psnet *psnet, u32 off)
 
 static void psnet_unmap_unused_bars(struct pci_dev *pdev, struct psnet *psnet)
 {
-	int i, mask = 0;
+	int i;
 
 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
 		if (psnet->bars[i] && i != psnet->barno)
-			mask |= (1 << i);
+			pcim_iounmap_region(pdev, i);
 	}
-
-	if (mask)
-		pcim_iounmap_regions(pdev, mask);
 }
 
 /* Read SNET config from PCI BAR */
Signature · 1 line
2.46.0
#17 lore raw

Re: [PATCH v2 8/9] vdap: solidrun: Replace deprecated PCI functions

Quoted 10 lines · Philipp Stanner

solidrun utilizes pcim_iomap_regions(), which has been deprecated by the PCI subsystem in commit e354bb84a4c1 ("PCI: Deprecate pcim_iomap_table(), pcim_iomap_regions_request_all()"), among other things because it forces usage of quite a complicated bitmask mechanism. The bitmask handling code can entirely be removed by replacing pcim_iomap_regions() and pcim_iomap_table().

Replace pcim_iomap_regions() and pcim_iomap_table() with pci_iomap_region().

...

Quoted 6 lines

static int snet_open_vf_bar(struct pci_dev *pdev, struct snet *snet)

 {
        char name[50];

- int ret;

        snprintf(name, sizeof(name), "snet[%s]-bar", pci_name(pdev));

Shouldn't this be also devm_kasprintf()?

Quoted 14 lines
        /* Request and map BAR */
drivers/vdpa/solidrun/snet_main.c +3 -5
-       ret = pcim_iomap_regions(pdev, BIT(snet->psnet->cfg.vf_bar), name);
-       if (ret) {
+       snet->bar = pcim_iomap_region(pdev, snet->psnet->cfg.vf_bar, name);
+       if (IS_ERR(snet->bar)) {
                SNET_ERR(pdev, "Failed to request and map PCI BAR for a VF\n");
-               return ret;
+               return PTR_ERR(snet->bar);
        }
 
-       snet->bar = pcim_iomap_table(pdev)[snet->psnet->cfg.vf_bar];
-
        return 0;
 }
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#18 lore raw

PATCH v2 9/9 PCI: Remove pcim_iounmap_regions()

In reply to Philipp Stanner

All users of pcim_iounmap_regions() have been removed by now.

Remove pcim_iounmap_regions().

---
 .../driver-api/driver-model/devres.rst        |  1 -
 drivers/pci/devres.c                          | 21 -------------------
 include/linux/pci.h                           |  1 -
 3 files changed, 23 deletions(-)
Documentation/driver-api/driver-model/devres.rst +0 -1
index ac9ee7441887..525f08694984 100644
@@ -397,7 +397,6 @@ PCI
   pcim_iomap_regions_request_all() : do request_region() on all and iomap() on multiple BARs
   pcim_iomap_table()		: array of mapped addresses indexed by BAR
   pcim_iounmap()		: do iounmap() on a single BAR
-  pcim_iounmap_regions()	: do iounmap() and release_region() on multiple BARs
   pcim_pin_device()		: keep PCI device enabled after release
   pcim_set_mwi()		: enable Memory-Write-Invalidate PCI transaction
 
drivers/pci/devres.c +0 -21
index 4dbba385e6b4..022c0bb243ad 100644
@@ -1013,27 +1013,6 @@ int pcim_iomap_regions_request_all(struct pci_dev *pdev, int mask,
 }
 EXPORT_SYMBOL(pcim_iomap_regions_request_all);
 
-/**
- * pcim_iounmap_regions - Unmap and release PCI BARs
- * @pdev: PCI device to map IO resources for
- * @mask: Mask of BARs to unmap and release
- *
- * Unmap and release regions specified by @mask.
- */
-void pcim_iounmap_regions(struct pci_dev *pdev, int mask)
-{
-	int i;
-
-	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
-		if (!mask_contains_bar(mask, i))
-			continue;
-
-		pcim_iounmap_region(pdev, i);
-		pcim_remove_bar_from_legacy_table(pdev, i);
-	}
-}
-EXPORT_SYMBOL(pcim_iounmap_regions);
-
 /**
  * pcim_iomap_range - Create a ranged __iomap mapping within a PCI BAR
  * @pdev: PCI device to map IO resources for
include/linux/pci.h +0 -1
index 9625d8a7b655..6c60f063c672 100644
@@ -2301,7 +2301,6 @@ void pcim_iounmap_region(struct pci_dev *pdev, int bar);
 int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name);
 int pcim_iomap_regions_request_all(struct pci_dev *pdev, int mask,
 				   const char *name);
-void pcim_iounmap_regions(struct pci_dev *pdev, int mask);
 void __iomem *pcim_iomap_range(struct pci_dev *pdev, int bar,
 				unsigned long offset, unsigned long len);
 
Signature · 1 line
2.46.0