From: Shubham Antil <shubham@octane.security>
To: netdev@vger.kernel.org
Cc: Simon Horman <horms@kernel.org>,
gio@octane.security, David Heidelberg <david@ixit.cz>,
oe-linux-nfc@lists.linux.dev
Subject: [PATCH net v9 1/3] nfc: nci: add a pre-unregister shutdown stage
Date: Mon, 30 Nov 2026 18:32:38 +0530 [thread overview]
Message-ID: <20261130130240.65411-2-shubham@octane.security> (raw)
In-Reply-To: <20261130130240.65411-1-shubham@octane.security>
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
next prev parent reply other threads:[~2026-11-30 13:02 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
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
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=20261130130240.65411-2-shubham@octane.security \
--to=shubham@octane.security \
--cc=david@ixit.cz \
--cc=gio@octane.security \
--cc=horms@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=oe-linux-nfc@lists.linux.dev \
/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