Netdev List
 help / color / mirror / Atom feed
* [PATCH net v9 0/3] nfc: nfcmrvl: split firmware teardown fixes
@ 2026-11-30 13:02 Shubham Antil
  2026-11-30 13:02 ` [PATCH net v9 1/3] nfc: nci: add a pre-unregister shutdown stage Shubham Antil
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Shubham Antil @ 2026-11-30 13:02 UTC (permalink / raw)
  To: netdev; +Cc: Simon Horman, gio, David Heidelberg, oe-linux-nfc

The v8 series combined the firmware teardown fix with unrelated UART work.
This standalone submission applies directly to the base below and divides
the change into core infrastructure, firmware worker synchronization, and
the nfcmrvl unregister hook.

The first patch adds the NCI pre-unregister shutdown stage and a terminal
command-drain helper.  It prevents a close-time response from scheduling
command work after that drain.  The second patch serializes Marvell
firmware completion, timeout, and receive work.  The final patch invokes
the abort while the NCI workqueues are still alive.

The issue was found during an AI-assisted source audit using OpenAI Codex,
gpt-5.6-sol.  The active-download detach condition was reproduced at runtime
by starting a firmware download and removing the N_NCI line discipline.
One hundred immediate detach iterations and timed completion-boundary modes
were exercised.  No production incident or pre-fix sanitizer report was
captured.  Patch development and test planning used the same assistant.

Each mail patch was applied independently to the base below on the Linux
test host.  The affected NFC objects built after every patch with W=1 and
sparse.  A KASAN, UBSAN, and lockdep kernel completed 100 immediate
firmware-download detach iterations and the timed completion modes without
a diagnostic, failure, or remaining device.  No physical Marvell
controller was available.

Changes since v8:

  - make firmware teardown a standalone submission on the original base
  - split NCI infrastructure, firmware synchronization, and driver hookup
  - drain late responses before the command work and timer are cancelled
  - leave overlapping-download admission to its independent patch set
  - correct the command-drain helper name in the changelog
  - remove an obsolete write-only completion marker
  - add the requested AI-assistance trailer

Shubham Antil (3):
  nfc: nci: add a pre-unregister shutdown stage
  nfc: nfcmrvl: serialize firmware completion and teardown
  nfc: nfcmrvl: abort firmware work before unregistering

 drivers/nfc/nfcmrvl/fw_dnld.c | 231 ++++++++++++++++++++++++++++++------------
 drivers/nfc/nfcmrvl/fw_dnld.h |  10 ++
 drivers/nfc/nfcmrvl/main.c    |  10 +-
 include/net/nfc/nci_core.h    |   9 ++
 net/nfc/nci/core.c            |  31 ++++--
 net/nfc/nci/rsp.c             |   3 +
 6 files changed, 222 insertions(+), 72 deletions(-)


base-commit: 30c71476d842b094651bd7712f6c8ee8716147d0
-- 
2.54.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH net v9 1/3] nfc: nci: add a pre-unregister shutdown stage
  2026-11-30 13:02 [PATCH net v9 0/3] nfc: nfcmrvl: split firmware teardown fixes Shubham Antil
@ 2026-11-30 13:02 ` Shubham Antil
  2026-12-01 14:04   ` netdev-bot+sashiko
  2026-11-30 13:02 ` [PATCH net v9 2/3] nfc: nfcmrvl: serialize firmware completion and teardown Shubham Antil
  2026-11-30 13:02 ` [PATCH net v9 3/3] nfc: nfcmrvl: abort firmware work before unregistering Shubham Antil
  2 siblings, 1 reply; 6+ messages in thread
From: Shubham Antil @ 2026-11-30 13:02 UTC (permalink / raw)
  To: netdev; +Cc: Simon Horman, gio, David Heidelberg, oe-linux-nfc

A transport may need to stop asynchronous producers while the NCI
workqueues are still alive. nci_unregister_device() currently tears those
queues down without giving the driver a point at which admission is
closed but command dependencies can still be drained.

Add a sleepable shutdown callback after core admission closes and before
workqueue destruction. Provide nci_cancel_pending_cmds() to delete the
command timer, cancel command work, and purge the command queue as one
dependency chain, including a second timer deletion for a detached
callback. Use the same helper in normal close paths.

Mark the close-time response path as closing, stop it from scheduling the
next command, and drain receive work before the final command cancel. A
response which passed the closing check is therefore finished before the
command work and timer are drained.

The callback runs without req_lock and must leave no command producer
behind.

Discovered during an AI-assisted source audit using OpenAI Codex,
gpt-5.6-sol. The active-download detach condition was reproduced at runtime
by starting a firmware download and removing the N_NCI line discipline.
One hundred immediate detach iterations and timed completion-boundary modes
were exercised. No production incident or pre-fix sanitizer report was
captured. Patch development and test planning used the same assistant.

Fixes: 3194c6870158 ("NFC: nfcmrvl: add firmware download support")
Assisted-by: LLM (OpenAI Codex, gpt-5.6-sol)
Signed-off-by: Shubham Antil <shubham@octane.security>
---
 include/net/nfc/nci_core.h |  9 +++++++++
 net/nfc/nci/core.c         | 31 +++++++++++++++++++++++++------
 net/nfc/nci/rsp.c          |  3 +++
 3 files changed, 37 insertions(+), 6 deletions(-)

diff --git a/include/net/nfc/nci_core.h b/include/net/nfc/nci_core.h
index 664d5058e..fd67dc1e9 100644
--- a/include/net/nfc/nci_core.h
+++ b/include/net/nfc/nci_core.h
@@ -28,6 +28,7 @@
 enum nci_flag {
 	NCI_INIT,
 	NCI_UP,
+	NCI_CLOSING,
 	NCI_DATA_EXCHANGE,
 	NCI_DATA_EXCHANGE_TO,
 	NCI_UNREG,
@@ -67,6 +68,13 @@ struct nci_ops {
 	int   (*open)(struct nci_dev *ndev);
 	int   (*close)(struct nci_dev *ndev);
 	int   (*send)(struct nci_dev *ndev, struct sk_buff *skb);
+	/*
+	 * Stop driver-private asynchronous activity after NFC admission has
+	 * closed and before core queues are destroyed.  This callback may sleep,
+	 * runs without req_lock held, and may use the still-live core workqueues
+	 * while draining.  It must not leave work which can submit new commands.
+	 */
+	void  (*shutdown)(struct nci_dev *ndev);
 	int   (*setup)(struct nci_dev *ndev);
 	int   (*post_setup)(struct nci_dev *ndev);
 	int   (*fw_download)(struct nci_dev *ndev, const char *firmware_name);
@@ -292,6 +300,7 @@ int nci_core_init(struct nci_dev *ndev);
 
 int nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb);
 int nci_send_frame(struct nci_dev *ndev, struct sk_buff *skb);
+void nci_cancel_pending_cmds(struct nci_dev *ndev);
 int nci_set_config(struct nci_dev *ndev, __u8 id, size_t len, const __u8 *val);
 
 int nci_nfcee_discover(struct nci_dev *ndev, u8 action);
diff --git a/net/nfc/nci/core.c b/net/nfc/nci/core.c
index 73e3a9647..2373d9bf4 100644
--- a/net/nfc/nci/core.c
+++ b/net/nfc/nci/core.c
@@ -159,6 +159,23 @@ inline int nci_request(struct nci_dev *ndev,
 	return rc;
 }
 
+/**
+ * nci_cancel_pending_cmds - stop and discard queued NCI commands
+ * @ndev: NCI device
+ *
+ * The caller must first prevent command producers from submitting new work.
+ * This helper may sleep and must not be called from cmd_work.
+ */
+void nci_cancel_pending_cmds(struct nci_dev *ndev)
+{
+	timer_delete_sync(&ndev->cmd_timer);
+	atomic_set(&ndev->cmd_cnt, 0);
+	cancel_work_sync(&ndev->cmd_work);
+	timer_delete_sync(&ndev->cmd_timer);
+	skb_queue_purge(&ndev->cmd_q);
+}
+EXPORT_SYMBOL(nci_cancel_pending_cmds);
+
 static void nci_reset_req(struct nci_dev *ndev, const void *opt)
 {
 	struct nci_core_reset_cmd cmd;
@@ -589,6 +606,7 @@ static int nci_close_device(struct nci_dev *ndev)
 	set_bit(NCI_INIT, &ndev->flags);
 	__nci_request(ndev, nci_reset_req, (void *)0,
 		      msecs_to_jiffies(NCI_RESET_TIMEOUT));
+	set_bit(NCI_CLOSING, &ndev->flags);
 
 	/* After this point our queues are empty
 	 * rx work may be running but will see that NCI_UP was cleared
@@ -597,10 +615,12 @@ static int nci_close_device(struct nci_dev *ndev)
 
 	clear_bit(NCI_INIT, &ndev->flags);
 
-	/* Flush cmd and tx wq */
-	flush_workqueue(ndev->cmd_wq);
+	/* Drain responses which completed a close-time request before cancel. */
+	mutex_unlock(&ndev->req_lock);
+	flush_workqueue(ndev->rx_wq);
+	mutex_lock(&ndev->req_lock);
 
-	timer_delete_sync(&ndev->cmd_timer);
+	nci_cancel_pending_cmds(ndev);
 	timer_delete_sync(&ndev->data_timer);
 
 	if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
@@ -612,9 +632,6 @@ static int nci_close_device(struct nci_dev *ndev)
 
 	mutex_unlock(&ndev->req_lock);
 
-	/* rx_work may take req_lock via nci_deactivate_target */
-	flush_workqueue(ndev->rx_wq);
-
 	return 0;
 }
 
@@ -1320,6 +1337,8 @@ void nci_unregister_device(struct nci_dev *ndev)
 	struct nci_conn_info *conn_info, *n;
 
 	nfc_unregister_rfkill(ndev->nfc_dev);
+	if (ndev->ops->shutdown)
+		ndev->ops->shutdown(ndev);
 
 	/* This set_bit is not protected with specialized barrier,
 	 * However, it is fine because the mutex_lock(&ndev->req_lock);
diff --git a/net/nfc/nci/rsp.c b/net/nfc/nci/rsp.c
index b0ab4f5ac..da71bf4ec 100644
--- a/net/nfc/nci/rsp.c
+++ b/net/nfc/nci/rsp.c
@@ -458,6 +458,9 @@ void nci_rsp_packet(struct nci_dev *ndev, struct sk_buff *skb)
 	kfree_skb(skb);
 
 	/* trigger the next cmd */
+	if (test_bit(NCI_CLOSING, &ndev->flags))
+		return;
+
 	atomic_set(&ndev->cmd_cnt, 1);
 	if (!skb_queue_empty(&ndev->cmd_q))
 		queue_work(ndev->cmd_wq, &ndev->cmd_work);

base-commit: 30c71476d842b094651bd7712f6c8ee8716147d0
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH net v9 2/3] nfc: nfcmrvl: serialize firmware completion and teardown
  2026-11-30 13:02 [PATCH net v9 0/3] nfc: nfcmrvl: split firmware teardown fixes Shubham Antil
  2026-11-30 13:02 ` [PATCH net v9 1/3] nfc: nci: add a pre-unregister shutdown stage Shubham Antil
@ 2026-11-30 13:02 ` Shubham Antil
  2026-12-01 14:04   ` netdev-bot+sashiko
  2026-11-30 13:02 ` [PATCH net v9 3/3] nfc: nfcmrvl: abort firmware work before unregistering Shubham Antil
  2 siblings, 1 reply; 6+ messages in thread
From: Shubham Antil @ 2026-11-30 13:02 UTC (permalink / raw)
  To: netdev; +Cc: Simon Horman, gio, David Heidelberg, oe-linux-nfc

Firmware receive work, timeout completion, and device teardown can
release or reuse the same download state concurrently. The receive worker
can also submit an NCI command after core teardown has started.

Protect active and stopping state with the firmware receive lock. Once
completion starts, reject new receive entries, drain active entries, stop
the timer and both work items, purge queued frames, and release firmware
state exactly once. Run timeout completion on the firmware workqueue and
make both workers recheck the protected state before consuming data.

Cancel work non-synchronously from completion because completion may run
from either work item itself. Drain the NCI command dependency chain
before reporting completion and keep reset-time frames queued until the
initial pulse has finished.

Discovered during an AI-assisted source audit using OpenAI Codex,
gpt-5.6-sol. The active-download detach condition was reproduced at runtime
by starting a firmware download and removing the N_NCI line discipline.
One hundred immediate detach iterations and timed completion-boundary modes
were exercised. No production incident or pre-fix sanitizer report was
captured. Patch development and test planning used the same assistant.

Fixes: 3194c6870158 ("NFC: nfcmrvl: add firmware download support")
Assisted-by: LLM (OpenAI Codex, gpt-5.6-sol)
Signed-off-by: Shubham Antil <shubham@octane.security>
---
 drivers/nfc/nfcmrvl/fw_dnld.c | 231 ++++++++++++++++++++++++----------
 drivers/nfc/nfcmrvl/fw_dnld.h |  10 ++
 2 files changed, 177 insertions(+), 64 deletions(-)

diff --git a/drivers/nfc/nfcmrvl/fw_dnld.c b/drivers/nfc/nfcmrvl/fw_dnld.c
index 8b9d52573..00c0e9e3e 100644
--- a/drivers/nfc/nfcmrvl/fw_dnld.c
+++ b/drivers/nfc/nfcmrvl/fw_dnld.c
@@ -90,22 +90,35 @@ static struct sk_buff *alloc_lc_skb(struct nfcmrvl_private *priv, uint8_t plen)
 	return skb;
 }
 
-static void fw_dnld_over(struct nfcmrvl_private *priv, u32 error)
+static void fw_dnld_over(struct nfcmrvl_private *priv, int error)
 {
-	if (priv->fw_dnld.fw) {
-		release_firmware(priv->fw_dnld.fw);
-		priv->fw_dnld.fw = NULL;
-		priv->fw_dnld.header = NULL;
-		priv->fw_dnld.binary_config = NULL;
-	}
-
-	atomic_set(&priv->ndev->cmd_cnt, 0);
-
-	if (timer_pending(&priv->ndev->cmd_timer))
-		timer_delete_sync(&priv->ndev->cmd_timer);
+	struct nfcmrvl_fw_dnld *fw_dnld = &priv->fw_dnld;
+	char name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
+	unsigned long flags;
 
-	if (timer_pending(&priv->fw_dnld.timer))
-		timer_delete_sync(&priv->fw_dnld.timer);
+	spin_lock_irqsave(&fw_dnld->rx_lock, flags);
+	if (!fw_dnld->active) {
+		spin_unlock_irqrestore(&fw_dnld->rx_lock, flags);
+		return;
+	}
+	fw_dnld->active = false;
+	spin_unlock_irqrestore(&fw_dnld->rx_lock, flags);
+
+	strscpy(name, fw_dnld->name);
+	wait_event(fw_dnld->rx_wait, !READ_ONCE(fw_dnld->rx_active));
+	timer_delete_sync(&fw_dnld->timer);
+	/* Completion can run from either work item itself. */
+	cancel_work(&fw_dnld->rx_work);
+	cancel_work(&fw_dnld->timeout_work);
+	nci_cancel_pending_cmds(priv->ndev);
+	skb_queue_purge(&fw_dnld->rx_q);
+
+	if (fw_dnld->fw) {
+		release_firmware(fw_dnld->fw);
+		fw_dnld->fw = NULL;
+		fw_dnld->header = NULL;
+		fw_dnld->binary_config = NULL;
+	}
 
 	nfc_info(priv->dev, "FW loading over (%d)]\n", error);
 
@@ -114,7 +127,7 @@ static void fw_dnld_over(struct nfcmrvl_private *priv, u32 error)
 		nfcmrvl_chip_halt(priv);
 	}
 
-	nfc_fw_download_done(priv->ndev->nfc_dev, priv->fw_dnld.name, error);
+	nfc_fw_download_done(priv->ndev->nfc_dev, name, (u32)error);
 }
 
 static void fw_dnld_timeout(struct timer_list *t)
@@ -122,6 +135,18 @@ static void fw_dnld_timeout(struct timer_list *t)
 	struct nfcmrvl_private *priv = timer_container_of(priv, t,
 							  fw_dnld.timer);
 
+	queue_work(priv->fw_dnld.rx_wq, &priv->fw_dnld.timeout_work);
+}
+
+static void fw_dnld_timeout_work(struct work_struct *work)
+{
+	struct nfcmrvl_fw_dnld *fw_dnld = container_of(work,
+						       struct nfcmrvl_fw_dnld,
+						       timeout_work);
+	struct nfcmrvl_private *priv = container_of(fw_dnld,
+						    struct nfcmrvl_private,
+						    fw_dnld);
+
 	nfc_err(priv->dev, "FW loading timeout");
 	priv->fw_dnld.state = STATE_RESET;
 	fw_dnld_over(priv, -ETIMEDOUT);
@@ -395,62 +420,80 @@ static int process_state_boot(struct nfcmrvl_private *priv,
 
 static void fw_dnld_rx_work(struct work_struct *work)
 {
-	int ret;
-	struct sk_buff *skb;
 	struct nfcmrvl_fw_dnld *fw_dnld = container_of(work,
 						       struct nfcmrvl_fw_dnld,
 						       rx_work);
 	struct nfcmrvl_private *priv = container_of(fw_dnld,
 						    struct nfcmrvl_private,
 						    fw_dnld);
+	struct sk_buff *skb;
+	unsigned long flags;
+	int ret;
 
-	while ((skb = skb_dequeue(&fw_dnld->rx_q))) {
-		nfc_send_to_raw_sock(priv->ndev->nfc_dev, skb,
-				     RAW_PAYLOAD_NCI, NFC_DIRECTION_RX);
-		switch (fw_dnld->state) {
-		case STATE_RESET:
-			ret = process_state_reset(priv, skb);
-			break;
-		case STATE_INIT:
-			ret = process_state_init(priv, skb);
-			break;
-		case STATE_SET_REF_CLOCK:
-			ret = process_state_set_ref_clock(priv, skb);
-			break;
-		case STATE_SET_HI_CONFIG:
-			ret = process_state_set_hi_config(priv, skb);
-			break;
-		case STATE_OPEN_LC:
-			ret = process_state_open_lc(priv, skb);
-			break;
-		case STATE_FW_DNLD:
-			ret = process_state_fw_dnld(priv, skb);
-			break;
-		case STATE_CLOSE_LC:
-			ret = process_state_close_lc(priv, skb);
-			break;
-		case STATE_BOOT:
-			ret = process_state_boot(priv, skb);
-			break;
-		default:
-			ret = -EFAULT;
-		}
+	spin_lock_irqsave(&fw_dnld->rx_lock, flags);
+	if (fw_dnld->stopping || fw_dnld->starting || !fw_dnld->active) {
+		spin_unlock_irqrestore(&fw_dnld->rx_lock, flags);
+		return;
+	}
+	skb = skb_dequeue(&fw_dnld->rx_q);
+	spin_unlock_irqrestore(&fw_dnld->rx_lock, flags);
+	if (!skb)
+		return;
 
-		kfree_skb(skb);
+	nfc_send_to_raw_sock(priv->ndev->nfc_dev, skb,
+			     RAW_PAYLOAD_NCI, NFC_DIRECTION_RX);
+	switch (fw_dnld->state) {
+	case STATE_RESET:
+		ret = process_state_reset(priv, skb);
+		break;
+	case STATE_INIT:
+		ret = process_state_init(priv, skb);
+		break;
+	case STATE_SET_REF_CLOCK:
+		ret = process_state_set_ref_clock(priv, skb);
+		break;
+	case STATE_SET_HI_CONFIG:
+		ret = process_state_set_hi_config(priv, skb);
+		break;
+	case STATE_OPEN_LC:
+		ret = process_state_open_lc(priv, skb);
+		break;
+	case STATE_FW_DNLD:
+		ret = process_state_fw_dnld(priv, skb);
+		break;
+	case STATE_CLOSE_LC:
+		ret = process_state_close_lc(priv, skb);
+		break;
+	case STATE_BOOT:
+		ret = process_state_boot(priv, skb);
+		break;
+	default:
+		ret = -EFAULT;
+	}
 
-		if (ret != 0) {
-			nfc_err(priv->dev, "FW loading error");
-			fw_dnld_over(priv, ret);
-			break;
-		}
+	kfree_skb(skb);
+
+	if (ret) {
+		nfc_err(priv->dev, "FW loading error");
+		fw_dnld_over(priv, ret);
 	}
+
+	spin_lock_irqsave(&fw_dnld->rx_lock, flags);
+	if (!fw_dnld->stopping && !fw_dnld->starting && fw_dnld->active &&
+	    !skb_queue_empty(&fw_dnld->rx_q))
+		queue_work(fw_dnld->rx_wq, &fw_dnld->rx_work);
+	spin_unlock_irqrestore(&fw_dnld->rx_lock, flags);
 }
 
 int nfcmrvl_fw_dnld_init(struct nfcmrvl_private *priv)
 {
 	char name[32];
 
+	spin_lock_init(&priv->fw_dnld.rx_lock);
+	init_waitqueue_head(&priv->fw_dnld.rx_wait);
 	INIT_WORK(&priv->fw_dnld.rx_work, fw_dnld_rx_work);
+	INIT_WORK(&priv->fw_dnld.timeout_work, fw_dnld_timeout_work);
+	timer_setup(&priv->fw_dnld.timer, fw_dnld_timeout, 0);
 	snprintf(name, sizeof(name), "%s_nfcmrvl_fw_dnld_rx_wq",
 		 dev_name(&priv->ndev->nfc_dev->dev));
 	priv->fw_dnld.rx_wq = create_singlethread_workqueue(name);
@@ -462,26 +505,67 @@ int nfcmrvl_fw_dnld_init(struct nfcmrvl_private *priv)
 
 void nfcmrvl_fw_dnld_deinit(struct nfcmrvl_private *priv)
 {
+	timer_shutdown_sync(&priv->fw_dnld.timer);
+	cancel_work_sync(&priv->fw_dnld.rx_work);
+	cancel_work_sync(&priv->fw_dnld.timeout_work);
+	skb_queue_purge(&priv->fw_dnld.rx_q);
 	destroy_workqueue(priv->fw_dnld.rx_wq);
 }
 
 void nfcmrvl_fw_dnld_recv_frame(struct nfcmrvl_private *priv,
 				struct sk_buff *skb)
 {
+	unsigned long flags;
+
+	spin_lock_irqsave(&priv->fw_dnld.rx_lock, flags);
+	if (priv->fw_dnld.stopping || !priv->fw_dnld.active) {
+		spin_unlock_irqrestore(&priv->fw_dnld.rx_lock, flags);
+		kfree_skb(skb);
+		return;
+	}
+	priv->fw_dnld.rx_active++;
+	spin_unlock_irqrestore(&priv->fw_dnld.rx_lock, flags);
+
 	/* Discard command timer */
-	if (timer_pending(&priv->ndev->cmd_timer))
-		timer_delete_sync(&priv->ndev->cmd_timer);
+	timer_delete_sync(&priv->ndev->cmd_timer);
 
 	/* Allow next command */
 	atomic_set(&priv->ndev->cmd_cnt, 1);
 
-	/* Queue and trigger rx work */
-	skb_queue_tail(&priv->fw_dnld.rx_q, skb);
-	queue_work(priv->fw_dnld.rx_wq, &priv->fw_dnld.rx_work);
+	spin_lock_irqsave(&priv->fw_dnld.rx_lock, flags);
+	if (!priv->fw_dnld.stopping && priv->fw_dnld.active) {
+		/* Queue the frame, but do not process it during the reset pulse. */
+		skb_queue_tail(&priv->fw_dnld.rx_q, skb);
+		if (!priv->fw_dnld.starting)
+			queue_work(priv->fw_dnld.rx_wq,
+				   &priv->fw_dnld.rx_work);
+		skb = NULL;
+	}
+	priv->fw_dnld.rx_active--;
+	if (!priv->fw_dnld.rx_active &&
+	    (priv->fw_dnld.stopping || !priv->fw_dnld.active))
+		wake_up_all(&priv->fw_dnld.rx_wait);
+	spin_unlock_irqrestore(&priv->fw_dnld.rx_lock, flags);
+	kfree_skb(skb);
 }
 
 void nfcmrvl_fw_dnld_abort(struct nfcmrvl_private *priv)
 {
+	struct nfcmrvl_fw_dnld *fw_dnld = &priv->fw_dnld;
+	unsigned long flags;
+
+	device_lock(&priv->ndev->nfc_dev->dev);
+	spin_lock_irqsave(&fw_dnld->rx_lock, flags);
+	fw_dnld->stopping = true;
+	spin_unlock_irqrestore(&fw_dnld->rx_lock, flags);
+	device_unlock(&priv->ndev->nfc_dev->dev);
+
+	wait_event(fw_dnld->rx_wait, !READ_ONCE(fw_dnld->rx_active));
+	timer_shutdown_sync(&fw_dnld->timer);
+	cancel_work_sync(&fw_dnld->rx_work);
+	cancel_work_sync(&fw_dnld->timeout_work);
+	skb_queue_purge(&fw_dnld->rx_q);
+
 	fw_dnld_over(priv, -EHOSTDOWN);
 }
 
@@ -489,6 +573,7 @@ int nfcmrvl_fw_dnld_start(struct nci_dev *ndev, const char *firmware_name)
 {
 	struct nfcmrvl_private *priv = nci_get_drvdata(ndev);
 	struct nfcmrvl_fw_dnld *fw_dnld = &priv->fw_dnld;
+	unsigned long flags;
 	int res;
 
 	if (!priv->support_fw_dnld)
@@ -497,6 +582,13 @@ int nfcmrvl_fw_dnld_start(struct nci_dev *ndev, const char *firmware_name)
 	if (!firmware_name || !firmware_name[0])
 		return -EINVAL;
 
+	spin_lock_irqsave(&fw_dnld->rx_lock, flags);
+	if (fw_dnld->stopping) {
+		spin_unlock_irqrestore(&fw_dnld->rx_lock, flags);
+		return -ENODEV;
+	}
+	spin_unlock_irqrestore(&fw_dnld->rx_lock, flags);
+
 	strscpy(fw_dnld->name, firmware_name);
 
 	/*
@@ -532,11 +624,6 @@ int nfcmrvl_fw_dnld_start(struct nci_dev *ndev, const char *firmware_name)
 		fw_dnld->binary_config = &fw_dnld->header->firmware;
 	}
 
-	/* Configure a timer for timeout */
-	timer_setup(&priv->fw_dnld.timer, fw_dnld_timeout, 0);
-	mod_timer(&priv->fw_dnld.timer,
-		  jiffies + msecs_to_jiffies(FW_DNLD_TIMEOUT));
-
 	/* Ronfigure HI to be sure that it is the bootrom values */
 	priv->if_ops->nci_update_config(priv,
 					&fw_dnld->header->bootrom.config);
@@ -546,8 +633,24 @@ int nfcmrvl_fw_dnld_start(struct nci_dev *ndev, const char *firmware_name)
 
 	/* First, reset the chip */
 	priv->fw_dnld.state = STATE_RESET;
+
+	spin_lock_irqsave(&fw_dnld->rx_lock, flags);
+	fw_dnld->active = true;
+	fw_dnld->starting = true;
+	spin_unlock_irqrestore(&fw_dnld->rx_lock, flags);
+
 	nfcmrvl_chip_reset(priv);
 
+	spin_lock_irqsave(&fw_dnld->rx_lock, flags);
+	fw_dnld->starting = false;
+	if (fw_dnld->active) {
+		mod_timer(&fw_dnld->timer,
+			  jiffies + msecs_to_jiffies(FW_DNLD_TIMEOUT));
+		if (!skb_queue_empty(&fw_dnld->rx_q))
+			queue_work(fw_dnld->rx_wq, &fw_dnld->rx_work);
+	}
+	spin_unlock_irqrestore(&fw_dnld->rx_lock, flags);
+
 	/* Now wait for CORE_RESET_NTF or timeout */
 
 	return 0;
diff --git a/drivers/nfc/nfcmrvl/fw_dnld.h b/drivers/nfc/nfcmrvl/fw_dnld.h
index 7c4d91b01..daecf772e 100644
--- a/drivers/nfc/nfcmrvl/fw_dnld.h
+++ b/drivers/nfc/nfcmrvl/fw_dnld.h
@@ -8,6 +8,8 @@
 #ifndef __NFCMRVL_FW_DNLD_H__
 #define __NFCMRVL_FW_DNLD_H__
 
+#include <linux/spinlock.h>
+#include <linux/wait.h>
 #include <linux/workqueue.h>
 
 #define NFCMRVL_FW_MAGIC		0x88888888
@@ -72,7 +74,15 @@ struct nfcmrvl_fw_dnld {
 
 	struct workqueue_struct	*rx_wq;
 	struct work_struct rx_work;
+	struct work_struct timeout_work;
 	struct sk_buff_head rx_q;
+	/* Protects stopping, starting, active, and rx_active. */
+	spinlock_t rx_lock;
+	wait_queue_head_t rx_wait;
+	unsigned int rx_active;
+	bool stopping;
+	bool starting;
+	bool active;
 
 	struct timer_list timer;
 };
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH net v9 3/3] nfc: nfcmrvl: abort firmware work before unregistering
  2026-11-30 13:02 [PATCH net v9 0/3] nfc: nfcmrvl: split firmware teardown fixes Shubham Antil
  2026-11-30 13:02 ` [PATCH net v9 1/3] nfc: nci: add a pre-unregister shutdown stage Shubham Antil
  2026-11-30 13:02 ` [PATCH net v9 2/3] nfc: nfcmrvl: serialize firmware completion and teardown Shubham Antil
@ 2026-11-30 13:02 ` Shubham Antil
  2 siblings, 0 replies; 6+ messages in thread
From: Shubham Antil @ 2026-11-30 13:02 UTC (permalink / raw)
  To: netdev; +Cc: Simon Horman, gio, David Heidelberg, oe-linux-nfc

nfcmrvl_nci_unregister_dev() destroys the NCI workqueues before draining
firmware receive work. That worker may still submit NCI commands, and the
later firmware cleanup can release state the worker is using.

Abort an active download from the NCI shutdown callback, after core
admission has closed but before its workqueues are destroyed. Keep final
firmware workqueue destruction separate so unregister does not execute
the abort path twice.

Discovered during an AI-assisted source audit using OpenAI Codex,
gpt-5.6-sol. The active-download detach condition was reproduced at runtime
by starting a firmware download and removing the N_NCI line discipline.
One hundred immediate detach iterations and timed completion-boundary modes
were exercised. No production incident or pre-fix sanitizer report was
captured. Patch development and test planning used the same assistant. No
physical NFC controller was available.

Fixes: 3194c6870158 ("NFC: nfcmrvl: add firmware download support")
Assisted-by: LLM (OpenAI Codex, gpt-5.6-sol)
Signed-off-by: Shubham Antil <shubham@octane.security>
---
 drivers/nfc/nfcmrvl/main.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/drivers/nfc/nfcmrvl/main.c b/drivers/nfc/nfcmrvl/main.c
index 6efa83219..7677bcaf5 100644
--- a/drivers/nfc/nfcmrvl/main.c
+++ b/drivers/nfc/nfcmrvl/main.c
@@ -67,6 +67,13 @@ static int nfcmrvl_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
 	return priv->if_ops->nci_send(priv, skb);
 }
 
+static void nfcmrvl_nci_shutdown(struct nci_dev *ndev)
+{
+	struct nfcmrvl_private *priv = nci_get_drvdata(ndev);
+
+	nfcmrvl_fw_dnld_abort(priv);
+}
+
 static int nfcmrvl_nci_setup(struct nci_dev *ndev)
 {
 	__u8 val = 1;
@@ -85,6 +92,7 @@ static const struct nci_ops nfcmrvl_nci_ops = {
 	.open = nfcmrvl_nci_open,
 	.close = nfcmrvl_nci_close,
 	.send = nfcmrvl_nci_send,
+	.shutdown = nfcmrvl_nci_shutdown,
 	.setup = nfcmrvl_nci_setup,
 	.fw_download = nfcmrvl_nci_fw_download,
 };
@@ -181,8 +189,6 @@ void nfcmrvl_nci_unregister_dev(struct nfcmrvl_private *priv)
 	struct nci_dev *ndev = priv->ndev;
 
 	nci_unregister_device(ndev);
-	if (priv->ndev->nfc_dev->fw_download_in_progress)
-		nfcmrvl_fw_dnld_abort(priv);
 
 	nfcmrvl_fw_dnld_deinit(priv);
 
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH net v9 1/3] nfc: nci: add a pre-unregister shutdown stage
  2026-11-30 13:02 ` [PATCH net v9 1/3] nfc: nci: add a pre-unregister shutdown stage Shubham Antil
@ 2026-12-01 14:04   ` netdev-bot+sashiko
  0 siblings, 0 replies; 6+ messages in thread
From: netdev-bot+sashiko @ 2026-12-01 14:04 UTC (permalink / raw)
  To: shubham; +Cc: netdev, horms, gio, david, oe-linux-nfc, kuba

Thank you for your contribution! Sashiko AI review found 4 potential
issue(s) to consider.

Critical: 0 · High: 1 (1) · Medium: 0 · Low: 3

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] nfc: nci: premature NCI_CLOSING before ops->close() stalls close
  commands
- [Low] The new `struct nci_ops::shutdown` kernel-doc
  (include/net/nfc/nci_core.h:71-76) says the callback runs "after NFC…
- [Low] In the NCI_UP branch of nci_close_device(), the patch moves
  `flush_workqueue(ndev->rx_wq)` (net/nfc/nci/core.c:620) ahead of…
- [Low] nci_cancel_pending_cmds() (net/nfc/nci/core.c:169-176) is meant
  to break the cmd_timer -> cmd_work -> cmd_timer chain "as one…

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the patch
  reworks the exact flush involved.

--

nfc: nci: add a pre-unregister shutdown stage

This adds an optional sleepable nci_ops->shutdown callback. It is invoked
from nci_unregister_device() before the NCI workqueues are destroyed. It also
adds an exported nci_cancel_pending_cmds() helper for the command timer, work
and queue, plus an NCI_CLOSING flag that stops close-time responses from
scheduling the next command, and it moves the rx_wq flush in
nci_close_device() ahead of the final command cancel.

> diff --git a/include/net/nfc/nci_core.h b/include/net/nfc/nci_core.h
> index 664d5058e66e0..fd67dc1e91e7a 100644
> --- a/include/net/nfc/nci_core.h
> +++ b/include/net/nfc/nci_core.h

[ ... ]

> @@ -67,6 +68,13 @@ struct nci_ops {
>  	int   (*open)(struct nci_dev *ndev);
>  	int   (*close)(struct nci_dev *ndev);
>  	int   (*send)(struct nci_dev *ndev, struct sk_buff *skb);
> +	/*
> +	 * Stop driver-private asynchronous activity after NFC admission has
> +	 * closed and before core queues are destroyed.  This callback may sleep,
> +	 * runs without req_lock held, and may use the still-live core workqueues
> +	 * while draining.  It must not leave work which can submit new commands.
> +	 */
> +	void  (*shutdown)(struct nci_dev *ndev);

[Severity: Low]
Is "after NFC admission has closed" accurate for the point where this
callback runs? The commit message makes the same claim ("after core
admission closes").

In nci_unregister_device() the callback runs right after
nfc_unregister_rfkill(). That call only sets nfc_dev->shutting_down:

nci_unregister_device()
    nfc_unregister_rfkill(ndev->nfc_dev);
    if (ndev->ops->shutdown)
        ndev->ops->shutdown(ndev);
    set_bit(NCI_UNREG, &ndev->flags);
    nci_close_device(ndev);

Not every NFC entry point checks shutting_down. For example,
nfc_genl_vendor_cmd() in net/nfc/netlink.c calls cmd->doit(dev, data,
data_len) without checking it. nfc_se_io() only checks
device_is_registered() and dev_up.

At the NCI level, NCI_UNREG is not set yet, and NCI_UP is still set for an
up device, so nci_request() still admits requests. rx_work can still reach
nci_rsp_packet() and re-queue cmd_work, because NCI_CLOSING is clear.
cmd_timer and data_timer are also still live.

Suppose a driver follows this comment and calls nci_cancel_pending_cmds()
from ->shutdown on an up device. Would it race with producers it has no
way to stop?

The nfcmrvl user later in the series only drains while a firmware download
is active. NCI_UP is clear then, so it isn't affected. Could the comment and
commit message be narrowed to what this ordering actually guarantees?

[ ... ]

> diff --git a/net/nfc/nci/core.c b/net/nfc/nci/core.c
> index 73e3a96470ace..2373d9bf4ae7b 100644
> --- a/net/nfc/nci/core.c
> +++ b/net/nfc/nci/core.c
> @@ -159,6 +159,23 @@ inline int nci_request(struct nci_dev *ndev,
>  	return rc;
>  }
>  
> +/**
> + * nci_cancel_pending_cmds - stop and discard queued NCI commands
> + * @ndev: NCI device
> + *
> + * The caller must first prevent command producers from submitting new work.
> + * This helper may sleep and must not be called from cmd_work.
> + */
> +void nci_cancel_pending_cmds(struct nci_dev *ndev)
> +{
> +	timer_delete_sync(&ndev->cmd_timer);
> +	atomic_set(&ndev->cmd_cnt, 0);
> +	cancel_work_sync(&ndev->cmd_work);
> +	timer_delete_sync(&ndev->cmd_timer);
> +	skb_queue_purge(&ndev->cmd_q);
> +}

[Severity: Low]
Does this fully break the cmd_timer -> cmd_work -> cmd_timer chain?

Suppose nci_cmd_work() has already passed the cmd_cnt check when
atomic_set(&ndev->cmd_cnt, 0) runs. It still decrements:

nci_cmd_work()
    if (atomic_read(&ndev->cmd_cnt)) {
        skb = skb_dequeue(&ndev->cmd_q);
        ...
        atomic_dec(&ndev->cmd_cnt);
        ...
        nci_send_frame(ndev, skb);
        mod_timer(&ndev->cmd_timer,
                  jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT));

That leaves cmd_cnt at -1, and nci_cmd_work() treats any non-zero value as
a credit.

The same cmd_work also re-arms cmd_timer. If that timer fires between
cancel_work_sync() and the second timer_delete_sync(), nci_cmd_timer()
queues the work again after the only cancel has returned:

nci_cmd_timer()
    atomic_set(&ndev->cmd_cnt, 1);
    queue_work(ndev->cmd_wq, &ndev->cmd_work);

Nothing cancels that cmd_work. It races with skb_queue_purge(&ndev->cmd_q).
If an skb is still queued, it gets sent and cmd_timer is re-armed after the
helper has returned.

The window is narrow, since cmd_timer is armed for NCI_CMD_TIMEOUT just
before. Later users also set cmd_cnt back to 1 before use, so the -1 has no
visible effect today. A concurrent cmd_work can happen, though, for example
during an nfcmrvl firmware download when fw_dnld_over() runs from
timeout_work while cmd_work is sending.

Would disable_work_sync(), or a closing check in nci_cmd_timer(), close
this loop?

[ ... ]

> @@ -597,10 +615,12 @@ static int nci_close_device(struct nci_dev *ndev)
>  
>  	clear_bit(NCI_INIT, &ndev->flags);
>  
> -	/* Flush cmd and tx wq */
> -	flush_workqueue(ndev->cmd_wq);
> +	/* Drain responses which completed a close-time request before cancel. */
> +	mutex_unlock(&ndev->req_lock);
> +	flush_workqueue(ndev->rx_wq);
> +	mutex_lock(&ndev->req_lock);

[Severity: High]
This is a pre-existing issue and was not introduced by this patch. Since the
patch reworks this flush, though: can it deadlock against device_lock?

nfc_dev_down() holds device_lock(&dev->dev) across dev->ops->dev_down().
That reaches the flush through nci_dev_down() -> nci_close_device() ->
flush_workqueue(ndev->rx_wq).

Meanwhile rx_work can be in nci_ntf_packet() ->
nci_rf_discover_ntf_packet() or nci_target_auto_activated(). Both call
nfc_targets_found(), which takes device_lock before it checks polling:

nfc_targets_found()
    device_lock(&dev->dev);

    if (dev->polling == false) {
        device_unlock(&dev->dev);
        return 0;
    }

If rx_work blocks on device_lock while nfc_dev_down() holds it, the
single-threaded rx_wq can't serve the close-time
__nci_request(nci_reset_req), so that request waits out NCI_RESET_TIMEOUT.
After that, flush_workqueue(ndev->rx_wq) waits on rx_work, and rx_work
waits on device_lock.

nci_rf_discover_ntf_packet() has no NCI state gate. A late or misbehaving
RF_DISCOVER_NTF or RF_INTF_ACTIVATED_NTF can therefore arrive during
NFC_CMD_DEV_DOWN or an rfkill block, for example after stop_poll. Because
device->mutex is in lockdep's novalidate class, lockdep won't report it.
Once stuck, a later nci_unregister_device() would also block in
nfc_unregister_rfkill().

The old code did the same flush at the end of nci_close_device() under the
same device_lock. This patch moves it earlier and drops req_lock around it,
but it still runs inside nfc_dev_down()'s device_lock.

>  
> -	timer_delete_sync(&ndev->cmd_timer);
> +	nci_cancel_pending_cmds(ndev);
>  	timer_delete_sync(&ndev->data_timer);

[Severity: Low]
The rx_wq flush now comes before data_timer is deleted. Before this patch,
data_timer was deleted first and rx_wq was flushed last, so rx_work was
idle when nci_close_device() returned.

Can nci_data_timer() fire between flush_workqueue(ndev->rx_wq) and
timer_delete_sync(&ndev->data_timer)?

nci_data_timer()
    set_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
    queue_work(ndev->rx_wq, &ndev->rx_work);

Nothing drains that rx_work before nci_close_device() returns. On the
nfc_dev_down() path, nci_rx_work() can then call
nci_data_exchange_complete(..., -ETIMEDOUT). That can happen at the same
time as close's own -ENODEV completion, or after close has returned.

test_and_clear_bit(NCI_DATA_EXCHANGE) still prevents a double callback.
If rx_work wins that race, though, data_exchange_cb gets -ETIMEDOUT instead
of -ENODEV, and it may still be running after nfc_dev_down() returns. On the
unregister path, destroy_workqueue(rx_wq) still drains it.

Would deleting data_timer before the rx_wq flush avoid this?

[ ... ]

> @@ -589,6 +606,7 @@ static int nci_close_device(struct nci_dev *ndev)
>  	set_bit(NCI_INIT, &ndev->flags);
>  	__nci_request(ndev, nci_reset_req, (void *)0,
>  		      msecs_to_jiffies(NCI_RESET_TIMEOUT));
> +	set_bit(NCI_CLOSING, &ndev->flags);
>  
>  	/* After this point our queues are empty
>  	 * rx work may be running but will see that NCI_UP was cleared

[Severity: High]
Does setting NCI_CLOSING here, before ndev->ops->close() runs, break
drivers which send commands from their close callback?

ndev->ops->close() is deliberately called while NCI_INIT is still set,
see "NFC: nci: Move close ops call in nci_close_device", so that
drivers can issue commands through the core command/response state
machine. st-nci relies on this:

st_nci_close()
    ndlc_close()
        nci_prop_cmd(ndlc->ndev, ST_NCI_CORE_PROP, ...)
            __nci_request()

After nci_cmd_work() sends the reset request just above, cmd_cnt is 0.
Only the "trigger the next cmd" step in nci_rsp_packet() puts it back
to 1. On an NCI 1.x controller, nci_core_reset_rsp_packet() calls
nci_req_complete() before nci_rsp_packet() gets to that step:

nci_rsp_packet()
    timer_delete(&ndev->cmd_timer);
    nci_core_reset_rsp_packet()
        nci_req_complete()    <- close thread wakes, sets NCI_CLOSING
    nci_core_rsp_packet(ndev, rsp_opcode, skb);
    kfree_skb(skb);
    if (test_bit(NCI_CLOSING, &ndev->flags))
        return;               <- cmd_cnt stays 0

If the woken close thread sets NCI_CLOSING first, cmd_cnt is never
restored. The response already deleted cmd_timer, so nci_cmd_timer()
won't restore it either. The command queued from ndlc_close() then sits
in cmd_q and nci_cmd_work() never sends it. Close stalls for
NCI_CMD_TIMEOUT before __nci_request() returns -ETIMEDOUT. ndlc_close()
then powers the PHY down without the mode-set command ever reaching the
controller.

Even when the reset response wins that race, the response to the first
close-time command will see NCI_CLOSING. A close callback which sends
more than one command would then stall on the second one.

The rx_wq flush and nci_cancel_pending_cmds() only run after
ops->close() returns. Would setting NCI_CLOSING after ops->close(), for
example next to clear_bit(NCI_INIT), still give the ordering the commit
message describes? Or does the nfcmrvl shutdown path later in the
series depend on it being set before ops->close()?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130130240.65411-1-shubham%40octane.security

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH net v9 2/3] nfc: nfcmrvl: serialize firmware completion and teardown
  2026-11-30 13:02 ` [PATCH net v9 2/3] nfc: nfcmrvl: serialize firmware completion and teardown Shubham Antil
@ 2026-12-01 14:04   ` netdev-bot+sashiko
  0 siblings, 0 replies; 6+ messages in thread
From: netdev-bot+sashiko @ 2026-12-01 14:04 UTC (permalink / raw)
  To: shubham; +Cc: netdev, horms, gio, david, oe-linux-nfc, kuba

Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.

Critical: 0 · High: 1 (2) · Medium: 0 · Low: 0

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] nfcmrvl_fw_dnld_start() (drivers/nfc/nfcmrvl/fw_dnld.c) checks
  only fw_dnld->stopping.

Pre-existing issues:
- [High] process_state_fw_dnld() (drivers/nfc/nfcmrvl/fw_dnld.c) trusts
  the 16-bit chunk length that the NFC controller sends.
- [High] On the I2C and SPI PHYs, the NFCC interrupt is requested with
  devm_request_threaded_irq() in probe.

--

nfc: nfcmrvl: serialize firmware completion and teardown

This adds spinlock-protected active, stopping and starting state, plus an
rx_active counter, to the nfcmrvl firmware download code. The goal is to
serialize receive work, timeout completion and teardown. Timeout handling
moves to a work item, and fw_dnld_over() drains receivers, stops the timer
and work items, and cancels pending NCI commands before releasing the
firmware.

> diff --git a/drivers/nfc/nfcmrvl/fw_dnld.c b/drivers/nfc/nfcmrvl/fw_dnld.c
> index 8b9d5257320dc..00c0e9e3e548b 100644
> --- a/drivers/nfc/nfcmrvl/fw_dnld.c
> +++ b/drivers/nfc/nfcmrvl/fw_dnld.c

[ ... ]

> @@ -395,62 +420,80 @@ static int process_state_boot(struct nfcmrvl_private *priv,
>  
>  static void fw_dnld_rx_work(struct work_struct *work)
>  {

[ ... ]

> +	spin_lock_irqsave(&fw_dnld->rx_lock, flags);
> +	if (fw_dnld->stopping || fw_dnld->starting || !fw_dnld->active) {
> +		spin_unlock_irqrestore(&fw_dnld->rx_lock, flags);
> +		return;
> +	}
> +	skb = skb_dequeue(&fw_dnld->rx_q);
> +	spin_unlock_irqrestore(&fw_dnld->rx_lock, flags);
> +	if (!skb)
> +		return;

[ ... ]

> +	case STATE_FW_DNLD:
> +		ret = process_state_fw_dnld(priv, skb);
> +		break;

[Severity: High]
This isn't a bug introduced by this patch, but this path is being reworked,
so it seemed worth asking. Does process_state_fw_dnld() trust the chunk
length from the controller too much?

In SUBSTATE_WAIT_COMMAND the length comes straight from the device. The only
check is against a complement that the device also supplies:

	len = get_unaligned_le16(skb->data);
	...
	if (((~len) & 0xFFFF) != comp_len) {
	...
	priv->fw_dnld.chunk_len = len;

Then in SUBSTATE_WAIT_ACK_CREDIT:

	out_skb = alloc_lc_skb(priv, priv->fw_dnld.chunk_len);
	...
	skb_put_data(out_skb,
		     ((uint8_t *)priv->fw_dnld.fw->data) + priv->fw_dnld.offset,
		     priv->fw_dnld.chunk_len);

alloc_lc_skb() takes a uint8_t plen. A chunk_len such as 0xFF00 therefore
sizes the skb for only len & 0xff payload bytes, and hdr->plen is also
wrong.

Can skb_put_data() then hit skb_over_panic() once chunk_len exceeds the
tailroom?

Separately, fw_dnld.offset starts from binary_config->offset in
process_state_open_lc() and grows by chunk_len on every data credit. It is
never compared against fw->size.

Can a controller make the driver read past the end of fw->data and send that
memory to the device?

nfcmrvl_fw_dnld_start() also casts fw->data to struct nfcmrvl_fw and reads
magic, phy, helper and firmware without checking that fw->size is at least
sizeof(struct nfcmrvl_fw). Would a truncated firmware file cause an
out-of-bounds read there?

[ ... ]

>  void nfcmrvl_fw_dnld_recv_frame(struct nfcmrvl_private *priv,
>  				struct sk_buff *skb)
>  {
> +	unsigned long flags;
> +
> +	spin_lock_irqsave(&priv->fw_dnld.rx_lock, flags);
> +	if (priv->fw_dnld.stopping || !priv->fw_dnld.active) {
> +		spin_unlock_irqrestore(&priv->fw_dnld.rx_lock, flags);
> +		kfree_skb(skb);
> +		return;
> +	}
> +	priv->fw_dnld.rx_active++;
> +	spin_unlock_irqrestore(&priv->fw_dnld.rx_lock, flags);

[Severity: High]
This is a pre-existing issue, but does this gate cover receive entries that
come from the I2C and SPI interrupt threads after teardown?

Both PHYs request the NFCC interrupt with devm_request_threaded_irq(). The
IRQ stays requested and enabled until devres runs after remove() returns.

nfcmrvl_i2c_remove() and nfcmrvl_spi_remove() only call
nfcmrvl_nci_unregister_dev(), which does:

	nci_unregister_device(ndev);

	nfcmrvl_fw_dnld_deinit(priv);

	nci_free_device(ndev);
	kfree(priv);

Nothing disables or frees the IRQ first, and drv_data->priv is never
cleared. So this guard in nfcmrvl_i2c_int_irq_thread_fn() still passes:

	if (!drv_data->priv)
		return IRQ_HANDLED;

	if (test_bit(NFCMRVL_PHY_ERROR, &drv_data->priv->flags))

Suppose the controller raises an interrupt after kfree(priv). Can the
thread handler then read the freed priv and ndev through nci_skb_alloc()
and nfcmrvl_nci_recv_frame(), and possibly enter
nfcmrvl_fw_dnld_recv_frame()?

The SPI thread handler calls nfcmrvl_nci_recv_frame(drv_data->priv, ...) in
the same way.

The stopping and rx_active state lives inside the freed priv, so it can't
reject entries that begin after the free. No patch in this series quiesces
the IRQ either.

[ ... ]

> @@ -497,6 +582,13 @@ int nfcmrvl_fw_dnld_start(struct nci_dev *ndev, const char *firmware_name)
>  	if (!firmware_name || !firmware_name[0])
>  		return -EINVAL;
>  
> +	spin_lock_irqsave(&fw_dnld->rx_lock, flags);
> +	if (fw_dnld->stopping) {
> +		spin_unlock_irqrestore(&fw_dnld->rx_lock, flags);
> +		return -ENODEV;
> +	}
> +	spin_unlock_irqrestore(&fw_dnld->rx_lock, flags);
> +
>  	strscpy(fw_dnld->name, firmware_name);

[Severity: High]
Should this also reject a start while fw_dnld->active is set, or while a
fw_dnld_over() is still running?

nfc_fw_download() in net/nfc/core.c only rejects shutting_down and dev_up.
It does not check fw_download_in_progress, unlike nfc_dev_up().

So a second NFC_CMD_FW_DOWNLOAD during an active download reaches this
point. It then overwrites fw_dnld->name, fw, header, binary_config and
state while rx_work, timeout_work or abort still own them.

For example, take download A, active with its timer armed. A second request
names an existing file with the wrong magic or phy:

nfcmrvl_fw_dnld_start()
    request_firmware(&fw_dnld->fw, ...)   <- A's firmware leaks
    release_firmware(fw_dnld->fw);
    fw_dnld->header = NULL;
    return -EINVAL;                       <- fw_dnld->fw left dangling
nfc_fw_download()
    dev->fw_download_in_progress = false;

fw_dnld->active is still true, so when A's timer fires:

fw_dnld_timeout_work()
    fw_dnld_over()
        release_firmware(fw_dnld->fw);    <- second release

Can this double free the struct firmware?

If a queued frame is processed before the timer fires,
process_state_init() would dereference the NULL header. Alternatively,
process_state_fw_dnld() would read the freed fw->data.

If the second request names a file that doesn't exist, request_firmware()
sets fw_dnld->fw to NULL. Would A's state machine then dereference
fw_dnld->fw->data with fw_dnld->fw NULL?

There is also a window in fw_dnld_over() itself:

	fw_dnld->active = false;
	spin_unlock_irqrestore(&fw_dnld->rx_lock, flags);

	strscpy(name, fw_dnld->name);
	wait_event(fw_dnld->rx_wait, !READ_ONCE(fw_dnld->rx_active));
	timer_delete_sync(&fw_dnld->timer);
	...
	nci_cancel_pending_cmds(priv->ndev);
	skb_queue_purge(&fw_dnld->rx_q);

	if (fw_dnld->fw) {
		release_firmware(fw_dnld->fw);

fw_dnld_over() runs from the work items and from abort without
device_lock. If start() runs during these sleeping steps, could
fw_dnld_over() release the new download's firmware, purge its rx_q,
delete its timer and clear fw_download_in_progress?

The commit message says firmware state is released "exactly once". Does
that hold while start() does not check the new active flag?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130130240.65411-1-shubham%40octane.security

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-12-01 14:04 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 13:02 [PATCH net v9 0/3] nfc: nfcmrvl: split firmware teardown fixes Shubham Antil
2026-11-30 13:02 ` [PATCH net v9 1/3] nfc: nci: add a pre-unregister shutdown stage Shubham Antil
2026-12-01 14:04   ` netdev-bot+sashiko
2026-11-30 13:02 ` [PATCH net v9 2/3] nfc: nfcmrvl: serialize firmware completion and teardown Shubham Antil
2026-12-01 14:04   ` netdev-bot+sashiko
2026-11-30 13:02 ` [PATCH net v9 3/3] nfc: nfcmrvl: abort firmware work before unregistering Shubham Antil

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox