Skip to contentSkip to conversation

can: sja1000: Fix pci_iounmap() buffer

PATCH In review Thomas Fourier 3 participants 4 messages Mar 30 – Apr 7

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 can: sja1000: Fix pci_iounmap() buffer

The base_addr is mapped in kvaser_pci_init_one() and the pointer is copied to priv->reg_base in kvaser_pci_add_chan() with offset channel * KVASER_PCI_PORT_BYTES but unmapped without the offset.

Cancel the offset before calling pci_iounmap().

Fixes 255a9154319d ("can: sja1000: stop misusing member base_addr of struct net_device")
---
 drivers/net/can/sja1000/kvaser_pci.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/can/sja1000/kvaser_pci.c +3 -1
index 95fe9ee1ce32..213fd0eb07e7 100644
@@ -161,6 +161,7 @@ static void kvaser_pci_del_chan(struct net_device *dev)
 {
 	struct sja1000_priv *priv;
 	struct kvaser_pci *board;
+	void __iomem *base_addr;
 	int i;
 
 	if (!dev)
@@ -186,7 +187,8 @@ static void kvaser_pci_del_chan(struct net_device *dev)
 	}
 	unregister_sja1000dev(dev);
 
-	pci_iounmap(board->pci_dev, priv->reg_base);
+	base_addr = priv->reg_base - board->channel * KVASER_PCI_PORT_BYTES;
+	pci_iounmap(board->pci_dev, base_addr);
 	pci_iounmap(board->pci_dev, board->conf_addr);
 	pci_iounmap(board->pci_dev, board->res_addr);
 
Signature · 1 line
2.43.0
#1 lore raw
Quoted 5 lines

The base_addr is mapped in kvaser_pci_init_one() and the pointer is copied to priv->reg_base in kvaser_pci_add_chan() with offset channel * KVASER_PCI_PORT_BYTES but unmapped without the offset.

Cancel the offset before calling pci_iounmap().

Which action would indicate “cancelling” here?

Would it occasionally be preferred to specify selected message recipients also in the header “To”?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v7.0-rc6#n231

Regards,
Markus

#2 lore raw
In reply to Thomas Fourier
Quoted 9 lines · Thomas Fourier

The base_addr is mapped in kvaser_pci_init_one() and the pointer is copied to priv->reg_base in kvaser_pci_add_chan() with offset channel * KVASER_PCI_PORT_BYTES but unmapped without the offset.

Cancel the offset before calling pci_iounmap().

Fixes 255a9154319d ("can: sja1000: stop misusing member base_addr of struct net_device")

The cleanup functions in this driver are a mess. kvaser_pci_del_chan() should only delete one channel, but it deletes all. It also unmaps the iomem, which belongs into kvaser_pci_remove_one().

What about switching the driver to pcim_enable_device(), pcim_request_region(), pcim_iomap() functions instead?

Quoted 17 lines
---
 drivers/net/can/sja1000/kvaser_pci.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/can/sja1000/kvaser_pci.c +1 -0
index 95fe9ee1ce32..213fd0eb07e7 100644
@@ -161,6 +161,7 @@ static void kvaser_pci_del_chan(struct net_device *dev)
 {
 	struct sja1000_priv *priv;
 	struct kvaser_pci *board;
+	void __iomem *base_addr;
 	int i;
 
 	if (!dev)

drivers/net/can/sja1000/kvaser_pci.c static void kvaser_pci_del_chan(struct net_device *dev) · line 191

Quoted 6 lines
drivers/net/can/sja1000/kvaser_pci.c +2 -1
@@ -186,7 +187,8 @@ static void kvaser_pci_del_chan(struct net_device *dev)
 	}
 	unregister_sja1000dev(dev);
 
-	pci_iounmap(board->pci_dev, priv->reg_base);
+	base_addr = priv->reg_base - board->channel * KVASER_PCI_PORT_BYTES;
+	pci_iounmap(board->pci_dev, base_addr);

When called from kvaser_pci_remove_one(), "dev" points to the master dev, which uses priv->reg_base without an offset, as it's board->channel is "0", right?

When called from the error path of kvaser_pci_add_chan(), things go wrong, and in the error path of kvaser_pci_init_one(), the pci mem is unmapped again.

regards,
Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde          |
Embedded Linux                   | https://www.pengutronix.de |
Vertretung Nürnberg              | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-9   |
#3 lore raw
Quoted 3 lines · Marc Kleine-Budde

The cleanup functions in this driver are a mess. kvaser_pci_del_chan() should only delete one channel, but it deletes all. It also unmaps the iomem, which belongs into kvaser_pci_remove_one().

I'm not quite sure because kvaser_pci_init_one() allocs and registers all channels, so kvaser_pci_remove_one() should too?

Quoted 2 lines

What about switching the driver to pcim_enable_device(), pcim_request_region(), pcim_iomap() functions instead?

I can write a second patch to do so, this would for sure solve the problem. Should I? I have no way to test it.

Quoted 3 lines

When called from kvaser_pci_remove_one(), "dev" points to the master dev, which uses priv->reg_base without an offset, as it's board->channel is "0", right?

I think you are right, the normal path is fine, but not in the error paths for devices with channel other than 0.

Regards,
Thomas

Quoted 7 lines

When called from the error path of kvaser_pci_add_chan(), things go wrong, and in the error path of kvaser_pci_init_one(), the pci mem is unmapped again.

regards,
Marc