Skip to contentSkip to conversation

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

PATCH Applied Wei Fang 3 participants 4 messages Apr 29 – May 1

Subsystems (4)
lore
Share & tools
lore

Wei Fang

author maintainer
#0 lore raw

PATCH v3 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")

---
v3:
1. Save si->msg after unregister_netdev() to avoid a race between the local copy and a concurrent ndo callback.
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   | 42 +++++++++++++++----
 2 files changed, 35 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 +34 -8

88 lines not on this page · raw message

Signature · 1 line
2.34.1
reviewer
#1 lore raw

From: 'Simon Horman' <[email protected]>

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

This commit attempts to fix a use-after-free issue in the VSI mailbox send path by deferring the freeing of the DMA buffer if the hardware is still processing the message. The DMA buffer information is stored in the si structure and freed on the next send or when the driver is removed.

[ ... ]

Quoted 3 lines
drivers/net/ethernet/freescale/enetc/enetc_vf.c +0 -0

[ ... ]

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

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

Quoted 6 lines
drivers/net/ethernet/freescale/enetc/enetc_vf.c +2 -0
@@ -270,7 +294,9 @@ static void enetc_vf_remove(struct pci_dev *pdev)
 
 	free_netdev(si->ndev);
 
+	msg = si->msg;
 	enetc_pci_remove(pdev);
+	enetc_msg_dma_free(&pdev->dev, &msg);

Is it possible for the mailbox to still be busy here?

The commit addresses the use-after-free in the send path by checking the mailbox busy status before freeing the previous buffer. However, during driver teardown, enetc_msg_dma_free() is called unconditionally.

If a prior message sent via enetc_msg_vsi_send() times out and the hardware is still accessing the buffer, and then the driver is unbound, could this cause the hardware to read from freed DMA memory?

Should the teardown path wait for the mailbox to become idle or ensure the hardware is safely reset before freeing the buffer?

Quoted 1 line
drivers/net/ethernet/freescale/enetc/enetc_vf.c +0 -0
 }
author maintainer
#2 lore raw
Quoted 22 lines
Quoted 9 lines
drivers/net/ethernet/freescale/enetc/enetc_vf.c +1 -0
@@ -259,6 +282,7 @@ static void enetc_vf_remove(struct pci_dev *pdev)
 {
 	struct enetc_si *si = pci_get_drvdata(pdev);
 	struct enetc_ndev_priv *priv;
+	struct enetc_msg_swbd msg;
 
 	priv = netdev_priv(si->ndev);
 	unregister_netdev(si->ndev);

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

Quoted 6 lines
drivers/net/ethernet/freescale/enetc/enetc_vf.c +2 -0
@@ -270,7 +294,9 @@ static void enetc_vf_remove(struct pci_dev *pdev)
 
 	free_netdev(si->ndev);
 
+	msg = si->msg;
 	enetc_pci_remove(pdev);
+	enetc_msg_dma_free(&pdev->dev, &msg);

Is it possible for the mailbox to still be busy here?

The commit addresses the use-after-free in the send path by checking the mailbox busy status before freeing the previous buffer. However, during driver teardown, enetc_msg_dma_free() is called unconditionally.

Quoted 7 lines

If a prior message sent via enetc_msg_vsi_send() times out and the hardware is still accessing the buffer, and then the driver is unbound, could this cause the hardware to read from freed DMA memory?

Should the teardown path wait for the mailbox to become idle or ensure the hardware is safely reset before freeing the buffer?

enetc_msg_dma_free() is called after enetc_pci_remove(), enetc_pci_remove() will disable the device, so it is safe to free the buffer.

#3 lore raw
In reply to Wei Fang
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 8 lines · On Wed, 29 Apr 2026 16:19:30 +0800 you wrote:

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:

[...]

Here is the summary with links:
- [v3,net] net: enetc: fix VSI mailbox timeout handling and DMA lifecycle

    https://git.kernel.org/netdev/net/c/26ebd12e67bf

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html