mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: ZhaoJinming <zhaojinming@uniontech.com>
To: FUJITA Tomonori <fujita.tomonori@gmail.com>,
	Andrew Lunn <andrew+netdev@lunn.ch>,
	"David S . Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>
Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	ZhaoJinming <zhaojinming@uniontech.com>
Subject: [PATCH] net: tn40xx: fix netdev and NAPI leak in probe error paths
Date: Mon, 15 Jun 2026 14:42:56 +0800	[thread overview]
Message-ID: <20260615064256.1068059-1-zhaojinming@uniontech.com> (raw)

In tn40_probe(), after tn40_netdev_alloc() and netif_napi_add() succeed,
none of the subsequent error paths call netif_napi_del() or free_netdev()
to undo these operations.  On any probe failure after netif_napi_add() the
NAPI structure (embedded in the netdev private data) remains on the
per-netdev napi_list while the backing memory is never freed, causing:

  - A permanent memory leak of the net_device and its private data
    (includes DMA rings, descriptor arrays and other internal structures
    allocated by tn40_priv_init).
  - A stale napi_list node that references already-freed memory, which
    is a use-after-free if the list is traversed after probe failure.

Fix by renaming err_unset_drvdata to err_free_netdev, adding
netif_napi_del() and free_netdev() calls there, and redirecting the
tn40_hw_reset() and pci_alloc_irq_vectors() failure paths to this new
label so that all error paths after netdev allocation properly release
the netdev and NAPI resources.

Fixes: dd2a0ff55408 ("net: tn40xx: add basic Tx handling")
Signed-off-by: ZhaoJinming <zhaojinming@uniontech.com>
---
 drivers/net/ethernet/tehuti/tn40.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/drivers/net/ethernet/tehuti/tn40.c b/drivers/net/ethernet/tehuti/tn40.c
index 558b791a97ed..532703e9c28e 100644
--- a/drivers/net/ethernet/tehuti/tn40.c
+++ b/drivers/net/ethernet/tehuti/tn40.c
@@ -1751,13 +1751,13 @@ static int tn40_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
 	ret = tn40_hw_reset(priv);
 	if (ret) {
 		dev_err(&pdev->dev, "failed to reset HW.\n");
-		goto err_unset_drvdata;
+		goto err_free_netdev;
 	}
 
 	ret = pci_alloc_irq_vectors(pdev, 1, nvec, PCI_IRQ_MSI);
 	if (ret < 0) {
 		dev_err(&pdev->dev, "failed to allocate irq.\n");
-		goto err_unset_drvdata;
+		goto err_free_netdev;
 	}
 
 	ret = tn40_mdiobus_init(priv);
@@ -1799,8 +1799,10 @@ static int tn40_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
 	tn40_swnodes_cleanup(priv);
 err_free_irq:
 	pci_free_irq_vectors(pdev);
-err_unset_drvdata:
+err_free_netdev:
+	netif_napi_del(&priv->napi);
 	pci_set_drvdata(pdev, NULL);
+	free_netdev(ndev);
 err_iounmap:
 	iounmap(regs);
 err_free_regions:
-- 
2.20.1


             reply	other threads:[~2026-06-15  6:44 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-15  6:42 ZhaoJinming [this message]
2026-06-17 23:33 ` Jakub Kicinski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260615064256.1068059-1-zhaojinming@uniontech.com \
    --to=zhaojinming@uniontech.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=fujita.tomonori@gmail.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®