Skip to contentSkip to conversation

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

PATCH Superseded Wei Fang 2 participants 2 messages Apr 24 – Apr 28

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Wei Fang

author maintainer
#0 lore raw

PATCH 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 transfers the message 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 DMA transfer when the VSI frees the buffer, the hardware may subsequently transfer the data from freed memory to PSI's DMA buffer, causing memory corruption.

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 -EBUSY 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 hardware transfer and prevents message reply mismatches.

Fixes beb74ac878c8 ("enetc: Add vf to pf messaging support")
---
 drivers/net/ethernet/freescale/enetc/enetc.h  |  1 +
 .../net/ethernet/freescale/enetc/enetc_vf.c   | 41 +++++++++++++++----
 2 files changed, 33 insertions(+), 9 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 +32 -9

80 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#1 lore raw

drivers/net/ethernet/freescale/enetc/enetc_vf.c static void enetc_msg_vsi_write_msg(struct enetc_hw *hw, · line 44

Quoted 11 lines · On Fri, 24 Apr 2026 10:15:34 +0800 Wei Fang wrote:
drivers/net/ethernet/freescale/enetc/enetc_vf.c +11 -0
+	/* The VSI mailbox may be busy if last message was not yet processed
+	 * by PSI. So need to check the mailbox status before sending.
+	 */
+	vsimsgsr = enetc_rd(&si->hw, ENETC_VSIMSGSR);
+	if (vsimsgsr & ENETC_VSIMSGSR_MB) {
+		/* It is safe to free the DMA buffer here, the caller does
+		 * not access the DMA buffer if enetc_msg_vsi_send() fails.
+		 */
+		enetc_msg_dma_free(dev, msg);
+		dev_err(dev, "VSI mailbox is busy\n");
+		return -EBUSY;

Sashiko suggests EIO here, since it's effectively an IO issue and this errno goes back to user space

Quoted 10 lines
drivers/net/ethernet/freescale/enetc/enetc_vf.c +5 -0
+	}
+
+	/* Free the DMA buffer of the last message */
+	enetc_msg_dma_free(dev, &si->msg);
+	si->msg = *msg;
 	enetc_msg_vsi_write_msg(&si->hw, msg);
 
 	do {

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

Quoted 6 lines
drivers/net/ethernet/freescale/enetc/enetc_vf.c +1 -1
@@ -269,7 +292,7 @@ static void enetc_vf_remove(struct pci_dev *pdev)
 	enetc_teardown_cbdr(&si->cbd_ring);
 
 	free_netdev(si->ndev);
-
+	enetc_msg_dma_free(&pdev->dev, &si->msg);
 	enetc_pci_remove(pdev);

Sashiko points out that if the goal is to avoid memory corruption we should only free the buffer if the mbox is not busy. If it's busy printing a warning and leaking is probably better.

Signature · 1 line
pw-bot: cr