Skip to contentSkip to conversation

net: enetc: fix VSI mailbox timeout handling and DMA lifecycle

PATCH Superseded Wei Fang 2 participants 3 messages Apr 28 – Apr 30

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Wei Fang

author maintainer
#0 lore raw

PATCH v2 1/1 net: enetc: fix VSI mailbox timeout handling and DMA lifecycle

In the current VSI mailbox implementation, the VSI allocates a DMA buffer to store the message sent to the PSI. When the PSI receives the message request from the VSI, the hardware copies the message data from this DMA buffer to PSI's DMA buffer for processing.

When enetc_msg_vsi_send() times out, two scenarios can occur:

1) Use-after-free: If the hardware hasn't completed message copying when the VSI frees the buffer, the hardware may subsequently copy the data from freed memory to PSI's DMA buffer.

2) Message race: If PSI hasn't processed the previous message when the next message is sent, the VSI may receive the previous message's reply, leading to incorrect handling.

To address these issues, implement the following changes:

- Check the mailbox busy status before sending a new message. If the mailbox is in busy state, it indicates the previous message is still being processed, so return an error immediately.

- Add the 'msg' field to struct enetc_si to preserve the DMA buffer information. The caller of enetc_msg_vsi_send() no longer frees the DMA buffer. Instead, defer freeing until it is safe to do so (when mailbox is not busy on next send).

- Add cleanup in enetc_vf_remove() to free the last message buffer.

This ensures the DMA buffer remains valid during message copying and prevents message reply mismatches.

Fixes beb74ac878c8 ("enetc: Add vf to pf messaging support")

---
v2:
1. Update commit message
2. Return -EIO instead of -EBUSY when VSIMSGSR_MB bit check fails
3. Move enetc_msg_dma_free() after enetc_pci_remove()

---
 drivers/net/ethernet/freescale/enetc/enetc.h  |  1 +
 .../net/ethernet/freescale/enetc/enetc_vf.c   | 41 +++++++++++++++----
 2 files changed, 34 insertions(+), 8 deletions(-)
drivers/net/ethernet/freescale/enetc/enetc.h +1 -0
index e663bb5e614e..e691144e8756 100644
@@ -330,6 +330,7 @@ struct enetc_si {
 	struct workqueue_struct *workqueue;
 	struct work_struct rx_mode_task;
 	struct dentry *debugfs_root;
+	struct enetc_msg_swbd msg; /* Only valid for VSI */
 };
 
 #define ENETC_SI_ALIGN	32
drivers/net/ethernet/freescale/enetc/enetc_vf.c +33 -8

86 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#1 lore raw

This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
---
net: enetc: fix VSI mailbox timeout handling and DMA lifecycle

This patch preserves the VSI mailbox DMA buffer in si->msg so it can be freed on the next send (once the mailbox is no longer busy), and adds a final free in enetc_vf_remove(). It also bails out early if the mailbox is still busy when a new message is submitted.

Quoted 4 lines
drivers/net/ethernet/freescale/enetc/enetc_vf.c +0 -0
index 6c4b374bcb0e..e8c5adee9743 100644

[ ... ]

Quoted 9 lines
drivers/net/ethernet/freescale/enetc/enetc_vf.c +1 -0
@@ -258,6 +281,7 @@ static int enetc_vf_probe(struct pci_dev *pdev,
 static void enetc_vf_remove(struct pci_dev *pdev)
 {
 	struct enetc_si *si = pci_get_drvdata(pdev);
+	struct enetc_msg_swbd msg = si->msg;
 	struct enetc_ndev_priv *priv;
 
 	priv = netdev_priv(si->ndev);

unregister_netdev(si->ndev);

Is there a race between this local copy of si->msg and a concurrent ndo callback?

The local 'msg' is captured at function entry, before unregister_netdev() has run, and .remove() does not hold RTNL. During that window, userspace (for example 'ip link set dev ethX address ...') can acquire RTNL and invoke enetc_vf_set_mac_addr() -> enetc_msg_vsi_set_primary_mac_addr() -> enetc_msg_vsi_send() on the same si.

Inside enetc_msg_vsi_send(), on the non-busy path:

	/* Free the DMA buffer of the last message */
	enetc_msg_dma_free(dev, &si->msg);
	si->msg = *msg;

the buffer whose (vaddr, dma, size) our local 'msg' still holds a copy of gets freed, and si->msg is replaced with a new allocation.

drivers/net/ethernet/freescale/enetc/enetc_vf.c static void enetc_vf_remove(struct pci_dev *pdev) · line 299

Quoted 11 lines
drivers/net/ethernet/freescale/enetc/enetc_vf.c +1 -0
 	enetc_free_msix(priv);
 
 	enetc_free_si_resources(priv);
 	enetc_teardown_cbdr(&si->cbd_ring);
 
 	free_netdev(si->ndev);
 
 	enetc_pci_remove(pdev);
+	enetc_msg_dma_free(&pdev->dev, &msg);
 }

When this trailing enetc_msg_dma_free(&pdev->dev, &msg) runs, can it call dma_free_coherent() on a buffer that enetc_msg_vsi_send() already freed, and also leak the newer buffer now sitting in si->msg (which is no longer referenced by anything)?

Would capturing msg = si->msg after unregister_netdev() returns (i.e. once no further ndo callbacks can race) avoid both the double free and the leak?

Signature · 1 line
pw-bot: cr
author maintainer
#2 lore raw
Quoted 7 lines
Quoted 7 lines
drivers/net/ethernet/freescale/enetc/enetc_vf.c +1 -0
@@ -258,6 +281,7 @@ static int enetc_vf_probe(struct pci_dev *pdev,
 static void enetc_vf_remove(struct pci_dev *pdev)
 {
 	struct enetc_si *si = pci_get_drvdata(pdev);
+	struct enetc_msg_swbd msg = si->msg;
 	struct enetc_ndev_priv *priv;
 
Quoted 5 lines
Quoted 2 lines
drivers/net/ethernet/freescale/enetc/enetc_vf.c +0 -0
 	priv = netdev_priv(si->ndev);

unregister_netdev(si->ndev);

Is there a race between this local copy of si->msg and a concurrent ndo callback?

Yes, it is a risk. I noticed this warning from patchwork so I sent v3 yesterday.