mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH net] nfc: nfcmrvl: free skb of deferred TX URBs
@ 2026-12-01  7:29 Bilal Yazıcıoğlu
  0 siblings, 0 replies; only message in thread
From: Bilal Yazıcıoğlu @ 2026-12-01  7:29 UTC (permalink / raw)
  To: netdev
  Cc: David Heidelberg, oe-linux-nfc, Simon Horman,
	Luiz Augusto von Dentz, Kyle Pepper, linux-kernel,
	Bilal Yazıcıoğlu

While the device is suspended, nfcmrvl_usb_nci_send() does not submit
TX URBs but anchors them on drv_data->deferred, so that
nfcmrvl_play_deferred() submits them on resume. Until then the driver
owns the skb in urb->context, which is normally freed by
nfcmrvl_tx_complete().

When these URBs are dropped instead of submitted, only the URB itself
is freed:

 - nfcmrvl_usb_nci_close() and the failed path of nfcmrvl_resume()
   discard them with usb_scuttle_anchored_urbs(), leaking the skb.
 - nfcmrvl_play_deferred() frees the setup packet but not the skb,
   both when usb_submit_urb() fails and for the URBs left on the
   anchor after that.

This driver was based on btusb and has the same leak that was fixed
there by "Bluetooth: btusb: free skb and setup packet of deferred TX
URBs" (in bluetooth-next). Commit 8a4d480702b7 ("nfc: nfcmrvl: Fix
memory leak in nfcmrvl_play_deferred") already ported the earlier btusb
play_deferred() fix to this driver, without the skb.

Add nfcmrvl_free_deferred(), which frees the setup packet, the skb and
the URB of every URB on the deferred anchor, and use it in
nfcmrvl_usb_nci_close(), nfcmrvl_resume() and nfcmrvl_play_deferred().
Also free the skb when usb_submit_urb() fails in
nfcmrvl_play_deferred().

Compile tested only, with W=1 and sparse; I do not have the hardware.
The same fix in btusb was verified on a ThinkPad T480 with kmemleak.

Claude Code pointed out that this driver shares the btusb leak. I wrote
the fix.

Fixes: f26e30cc6b50 ("NFC: nfcmrvl: Initial commit for Marvell NFC driver")
Fixes: 8a4d480702b7 ("nfc: nfcmrvl: Fix memory leak in nfcmrvl_play_deferred")
Signed-off-by: Bilal Yazıcıoğlu <ahmetbilalyazicioglu@gmail.com>
---
 drivers/nfc/nfcmrvl/usb.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/drivers/nfc/nfcmrvl/usb.c b/drivers/nfc/nfcmrvl/usb.c
index c7f2afe00..3944f0307 100644
--- a/drivers/nfc/nfcmrvl/usb.c
+++ b/drivers/nfc/nfcmrvl/usb.c
@@ -162,6 +162,18 @@ static void nfcmrvl_tx_complete(struct urb *urb)
 	kfree_skb(skb);
 }
 
+static void nfcmrvl_free_deferred(struct nfcmrvl_usb_drv_data *drv_data)
+{
+	struct urb *urb;
+
+	/* Deferred URBs were never submitted, so their completion won't run. */
+	while ((urb = usb_get_from_anchor(&drv_data->deferred))) {
+		kfree(urb->setup_packet);
+		kfree_skb(urb->context);
+		usb_free_urb(urb);
+	}
+}
+
 static int nfcmrvl_usb_nci_open(struct nfcmrvl_private *priv)
 {
 	struct nfcmrvl_usb_drv_data *drv_data = priv->drv_data;
@@ -212,7 +224,7 @@ static int nfcmrvl_usb_nci_close(struct nfcmrvl_private *priv)
 	usb_autopm_put_interface(drv_data->intf);
 
 failed:
-	usb_scuttle_anchored_urbs(&drv_data->deferred);
+	nfcmrvl_free_deferred(drv_data);
 	return 0;
 }
 
@@ -380,6 +392,7 @@ static void nfcmrvl_play_deferred(struct nfcmrvl_usb_drv_data *drv_data)
 		err = usb_submit_urb(urb, GFP_ATOMIC);
 		if (err) {
 			kfree(urb->setup_packet);
+			kfree_skb(urb->context);
 			usb_unanchor_urb(urb);
 			usb_free_urb(urb);
 			break;
@@ -390,10 +403,7 @@ static void nfcmrvl_play_deferred(struct nfcmrvl_usb_drv_data *drv_data)
 	}
 
 	/* Cleanup the rest deferred urbs. */
-	while ((urb = usb_get_from_anchor(&drv_data->deferred))) {
-		kfree(urb->setup_packet);
-		usb_free_urb(urb);
-	}
+	nfcmrvl_free_deferred(drv_data);
 }
 
 static int nfcmrvl_resume(struct usb_interface *intf)
@@ -427,7 +437,7 @@ static int nfcmrvl_resume(struct usb_interface *intf)
 	return 0;
 
 failed:
-	usb_scuttle_anchored_urbs(&drv_data->deferred);
+	nfcmrvl_free_deferred(drv_data);
 done:
 	spin_lock_irq(&drv_data->txlock);
 	clear_bit(NFCMRVL_USB_SUSPENDING, &drv_data->flags);
-- 
2.56.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-01  7:29 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-01  7:29 [PATCH net] nfc: nfcmrvl: free skb of deferred TX URBs Bilal Yazıcıoğlu

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®