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 --- 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