Skip to contentSkip to conversation

switchtec: Fix stdev_release crash after suprise device loss.

PATCH Superseded Daniel Stodden 2 participants 6 messages Nov 10, 2023

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover switchtec: Fix stdev_release crash after suprise device loss.

Hello.

Sorry for sending the same set twice in a row, but I found a typo in yesterday's recipients list.

Here goes a small shell script to crash a kernel in switchtec.ko's stdev_release.

--snip--
#!/bin/bash -x

# open cdev
exec 3<>/dev/switchtec0
# remove pdev
addr=$(basename $(realpath /sys/class/switchtec/switchtec0/../..))
echo 1 > /sys/bus/pci/devices/$addr/remove
# close cdev
exec 3>&-
--snip--

It worked on v5.10. I believe it has been working ever since. The problem is that keeping the stdev pinned past the pci_driver removal will defer the mrpc dma shutdown until way after devres_release_all, which unmapped stdev->mmio_mrpc. Also, stdev->pdev would be a stale pointer by then.

Followed-up with a humble proposal how to fix it.

Thanks,
Daniel

Daniel Stodden (1):
switchtec: Fix stdev_release crash after suprise device loss.

 drivers/pci/switch/switchtec.c | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)
Signature · 1 line
2.41.0
#1 lore raw

PATCH v2 1/1 switchtec: Fix stdev_release crash after suprise device loss.

A pci device hot removal may occur while stdev->cdev is held open. The call to stdev_release is then delivered during close or exit, at a point way past switchtec_pci_remove. Otherwise the last ref would vanish with the trailing put_device, just before return.

At that later point in time, the device layer has alreay removed stdev->mrpc_mmio map. Also, the stdev->pdev reference was not a counted one. Therefore, in dma mode, the iowrite32 in stdev_release will cause a fatal page fault, and the subsequent dma_free_coherent, if reached, would pass a stale &stdev->pdev->dev pointer.

Fixed by moving mrpc dma shutdown into switchtec_pci_remove, after stdev_kill. Counting the stdev->pdev ref is now optional, but may prevent future accidents.

---
 drivers/pci/switch/switchtec.c | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)
drivers/pci/switch/switchtec.c +21 -8
index e69cac84b605..002d0205d263 100644
@@ -1247,17 +1247,17 @@ static void enable_dma_mrpc(struct switchtec_dev *stdev)
 	iowrite32(SWITCHTEC_DMA_MRPC_EN, &stdev->mmio_mrpc->dma_en);
 }
 
+static void disable_dma_mrpc(struct switchtec_dev *stdev)
+{
+	iowrite32(0, &stdev->mmio_mrpc->dma_en);
+	flush_wc_buf(stdev);
+	writeq(0, &stdev->mmio_mrpc->dma_addr);
+}
+
 static void stdev_release(struct device *dev)
 {
 	struct switchtec_dev *stdev = to_stdev(dev);
 
-	if (stdev->dma_mrpc) {
-		iowrite32(0, &stdev->mmio_mrpc->dma_en);
-		flush_wc_buf(stdev);
-		writeq(0, &stdev->mmio_mrpc->dma_addr);
-		dma_free_coherent(&stdev->pdev->dev, sizeof(*stdev->dma_mrpc),
-				stdev->dma_mrpc, stdev->dma_mrpc_dma_addr);
-	}
 	kfree(stdev);
 }
 
@@ -1301,7 +1301,7 @@ static struct switchtec_dev *stdev_create(struct pci_dev *pdev)
 		return ERR_PTR(-ENOMEM);
 
 	stdev->alive = true;
-	stdev->pdev = pdev;
+	stdev->pdev = pci_dev_get(pdev);
 	INIT_LIST_HEAD(&stdev->mrpc_queue);
 	mutex_init(&stdev->mrpc_mutex);
 	stdev->mrpc_busy = 0;
@@ -1587,6 +1587,16 @@ static int switchtec_init_pci(struct switchtec_dev *stdev,
 	return 0;
 }
 
+static void switchtec_exit_pci(struct switchtec_dev *stdev)
+{
+	if (stdev->dma_mrpc) {
+		disable_dma_mrpc(stdev);
+		dma_free_coherent(&stdev->pdev->dev, sizeof(*stdev->dma_mrpc),
+				  stdev->dma_mrpc, stdev->dma_mrpc_dma_addr);
+		stdev->dma_mrpc = NULL;
+	}
+}
+
 static int switchtec_pci_probe(struct pci_dev *pdev,
 			       const struct pci_device_id *id)
 {
@@ -1646,6 +1656,9 @@ static void switchtec_pci_remove(struct pci_dev *pdev)
 	ida_simple_remove(&switchtec_minor_ida, MINOR(stdev->dev.devt));
 	dev_info(&stdev->dev, "unregistered.\n");
 	stdev_kill(stdev);
+	switchtec_exit_pci(stdev);
+	pci_dev_put(stdev->pdev);
+	stdev->pdev = NULL;
 	put_device(&stdev->dev);
 }
 
Signature · 1 line
2.41.0
maintainer Reviewed-by on v2 1/1
#2 lore raw
Quoted 11 lines · Daniel Stodden

A pci device hot removal may occur while stdev->cdev is held open. The call to stdev_release is then delivered during close or exit, at a point way past switchtec_pci_remove. Otherwise the last ref would vanish with the trailing put_device, just before return.

At that later point in time, the device layer has alreay removed stdev->mrpc_mmio map. Also, the stdev->pdev reference was not a counted one. Therefore, in dma mode, the iowrite32 in stdev_release will cause a fatal page fault, and the subsequent dma_free_coherent, if reached, would pass a stale &stdev->pdev->dev pointer.

Quoted 5 lines

Fixed by moving mrpc dma shutdown into switchtec_pci_remove, after stdev_kill. Counting the stdev->pdev ref is now optional, but may prevent future accidents.

Nice catch, thanks!

The solution looks good to me, though I might quibble slightly about
style: I'm not sure why we need two helper functions (disable_dma_mrpc() and switchtec_exit_pci()) that are only called in one place. I'd probably just open code both of them.

Other than that:

Logan

Quoted 69 lines
---
 drivers/pci/switch/switchtec.c | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)
drivers/pci/switch/switchtec.c +21 -8
index e69cac84b605..002d0205d263 100644
@@ -1247,17 +1247,17 @@ static void enable_dma_mrpc(struct switchtec_dev *stdev)
 	iowrite32(SWITCHTEC_DMA_MRPC_EN, &stdev->mmio_mrpc->dma_en);
 }
 
+static void disable_dma_mrpc(struct switchtec_dev *stdev)
+{
+	iowrite32(0, &stdev->mmio_mrpc->dma_en);
+	flush_wc_buf(stdev);
+	writeq(0, &stdev->mmio_mrpc->dma_addr);
+}
+
 static void stdev_release(struct device *dev)
 {
 	struct switchtec_dev *stdev = to_stdev(dev);
 
-	if (stdev->dma_mrpc) {
-		iowrite32(0, &stdev->mmio_mrpc->dma_en);
-		flush_wc_buf(stdev);
-		writeq(0, &stdev->mmio_mrpc->dma_addr);
-		dma_free_coherent(&stdev->pdev->dev, sizeof(*stdev->dma_mrpc),
-				stdev->dma_mrpc, stdev->dma_mrpc_dma_addr);
-	}
 	kfree(stdev);
 }
 
@@ -1301,7 +1301,7 @@ static struct switchtec_dev *stdev_create(struct pci_dev *pdev)
 		return ERR_PTR(-ENOMEM);
 
 	stdev->alive = true;
-	stdev->pdev = pdev;
+	stdev->pdev = pci_dev_get(pdev);
 	INIT_LIST_HEAD(&stdev->mrpc_queue);
 	mutex_init(&stdev->mrpc_mutex);
 	stdev->mrpc_busy = 0;
@@ -1587,6 +1587,16 @@ static int switchtec_init_pci(struct switchtec_dev *stdev,
 	return 0;
 }
 
+static void switchtec_exit_pci(struct switchtec_dev *stdev)
+{
+	if (stdev->dma_mrpc) {
+		disable_dma_mrpc(stdev);
+		dma_free_coherent(&stdev->pdev->dev, sizeof(*stdev->dma_mrpc),
+				  stdev->dma_mrpc, stdev->dma_mrpc_dma_addr);
+		stdev->dma_mrpc = NULL;
+	}
+}
+
 static int switchtec_pci_probe(struct pci_dev *pdev,
 			       const struct pci_device_id *id)
 {
@@ -1646,6 +1656,9 @@ static void switchtec_pci_remove(struct pci_dev *pdev)
 	ida_simple_remove(&switchtec_minor_ida, MINOR(stdev->dev.devt));
 	dev_info(&stdev->dev, "unregistered.\n");
 	stdev_kill(stdev);
+	switchtec_exit_pci(stdev);
+	pci_dev_put(stdev->pdev);
+	stdev->pdev = NULL;
 	put_device(&stdev->dev);
 }
 
#3 lore raw

Hi.

[Sorry, sending this twice, again, hoping this time it won't bounce, because html]

Quoted 10 lines

Nice catch, thanks!

The solution looks good to me, though I might quibble slightly about
style: I'm not sure why we need two helper functions (disable_dma_mrpc() and switchtec_exit_pci()) that are only called in one place. I'd probably just open code both of them.

Other than that:

I'm fine with it either way. I added those mainly because of the enable_dma_mrpc/switctec_init_pci counterparts were already there. And likewise single-use, as would be usual.

So the subroutines would maintain symmetry between probe() and remove().

So.. just don't add the new remove()-relevant ones? (One alternative would be to send another change right after, which goes after single-use pairs altogether.)

Daniel

Quoted 70 lines

Logan

Quoted 68 lines

---
drivers/pci/switch/switchtec.c | 29 +++++++++++++++++++++--------

1 file changed, 21 insertions(+), 8 deletions(-)
drivers/pci/switch/switchtec.c +21 -8
index e69cac84b605..002d0205d263 100644
@@ -1247,17 +1247,17 @@ static void enable_dma_mrpc(struct switchtec_dev *stdev)
 iowrite32(SWITCHTEC_DMA_MRPC_EN, &stdev->mmio_mrpc->dma_en);
 }
 
+static void disable_dma_mrpc(struct switchtec_dev *stdev)
+{
+ iowrite32(0, &stdev->mmio_mrpc->dma_en);
+ flush_wc_buf(stdev);
+ writeq(0, &stdev->mmio_mrpc->dma_addr);
+}
+
 static void stdev_release(struct device *dev)
 {
 struct switchtec_dev *stdev = to_stdev(dev);
 
- if (stdev->dma_mrpc) {
- iowrite32(0, &stdev->mmio_mrpc->dma_en);
- flush_wc_buf(stdev);
- writeq(0, &stdev->mmio_mrpc->dma_addr);
- dma_free_coherent(&stdev->pdev->dev, sizeof(*stdev->dma_mrpc),
- stdev->dma_mrpc, stdev->dma_mrpc_dma_addr);
- }
 kfree(stdev);
 }
 
@@ -1301,7 +1301,7 @@ static struct switchtec_dev *stdev_create(struct pci_dev *pdev)
 return ERR_PTR(-ENOMEM);
 
 stdev->alive = true;
- stdev->pdev = pdev;
+ stdev->pdev = pci_dev_get(pdev);
 INIT_LIST_HEAD(&stdev->mrpc_queue);
 mutex_init(&stdev->mrpc_mutex);
 stdev->mrpc_busy = 0;
@@ -1587,6 +1587,16 @@ static int switchtec_init_pci(struct switchtec_dev *stdev,
 return 0;
 }
 
+static void switchtec_exit_pci(struct switchtec_dev *stdev)
+{
+ if (stdev->dma_mrpc) {
+ disable_dma_mrpc(stdev);
+ dma_free_coherent(&stdev->pdev->dev, sizeof(*stdev->dma_mrpc),
+   stdev->dma_mrpc, stdev->dma_mrpc_dma_addr);
+ stdev->dma_mrpc = NULL;
+ }
+}
+
 static int switchtec_pci_probe(struct pci_dev *pdev,
       const struct pci_device_id *id)
 {
@@ -1646,6 +1656,9 @@ static void switchtec_pci_remove(struct pci_dev *pdev)
 ida_simple_remove(&switchtec_minor_ida, MINOR(stdev->dev.devt));
 dev_info(&stdev->dev, "unregistered.\n");
 stdev_kill(stdev);
+ switchtec_exit_pci(stdev);
+ pci_dev_put(stdev->pdev);
+ stdev->pdev = NULL;
 put_device(&stdev->dev);
 }
maintainer
#4 lore raw
Quoted 16 lines · Daniel Stodden

Hi.

[Sorry, sending this twice, again, hoping this time it won't bounce, because html]

Quoted 10 lines

Nice catch, thanks!

The solution looks good to me, though I might quibble slightly about
style: I'm not sure why we need two helper functions (disable_dma_mrpc() and switchtec_exit_pci()) that are only called in one place. I'd probably just open code both of them.

Other than that:

Quoted 7 lines

I'm fine with it either way. I added those mainly because of the enable_dma_mrpc/switctec_init_pci counterparts were already there. And likewise single-use, as would be usual.

So the subroutines would maintain symmetry between probe() and remove().

So.. just don't add the new remove()-relevant ones? (One alternative would be to send another change right after, which goes after single-use pairs altogether.)

Ah, I see. Just because it's single use doesn't necessarily mean it's wrong. I would say switchtec_init_pci() should not be inlined because it's rather long and keeps switchtec_pci_probe() a little more readable.

enable_dma_mrpc() more questionable. I'm not sure why it was done that way. Perhaps something changed.

In any case, I'm not all that concerned. If you want to leave it the way it is, I wouldn't mind.

Thanks,

Logan

#5 lore raw

I don’t mind all.

I’ll remove disable_dma_mrpc(). That will make things about the size as the old stdev_release() again. It’s pretty gratuitous.

I’ll keep switchtec_exit_pci(), because it helps switchtec_pci_remove to remain a carefully ordered sequence of one-liners.

I looked a little more through the code, and noticed that the two non-delayed work structs, mrpc_work and mrpc_timeout, are not cancelled in stdev_kill(). We seem to stop the ISR which schedules them, but do not stop the bh before teardown.

And these handlers do not recognize stdev->alive (ok, they should not, because they do not hold a strong reference to stdev.)

So there’s probably more coming soon-ish. Just mentioning it in case I get hit by a piano until then.

Cheers,
Daniel

Quoted 42 lines · Logan Gunthorpe
Quoted 16 lines · Daniel Stodden

Hi.

[Sorry, sending this twice, again, hoping this time it won't bounce, because html]

Quoted 10 lines

Nice catch, thanks!

The solution looks good to me, though I might quibble slightly about
style: I'm not sure why we need two helper functions (disable_dma_mrpc() and switchtec_exit_pci()) that are only called in one place. I'd probably just open code both of them.

Other than that:

Quoted 7 lines

I'm fine with it either way. I added those mainly because of the enable_dma_mrpc/switctec_init_pci counterparts were already there. And likewise single-use, as would be usual.

So the subroutines would maintain symmetry between probe() and remove().

So.. just don't add the new remove()-relevant ones? (One alternative would be to send another change right after, which goes after single-use pairs altogether.)

Ah, I see. Just because it's single use doesn't necessarily mean it's wrong. I would say switchtec_init_pci() should not be inlined because it's rather long and keeps switchtec_pci_probe() a little more readable.

enable_dma_mrpc() more questionable. I'm not sure why it was done that way. Perhaps something changed.

In any case, I'm not all that concerned. If you want to leave it the way it is, I wouldn't mind.

Thanks,

Logan