nci_close_device() can cancel command work while an NCI response already owned by the receive worker makes another command runnable. The close path also resets the controller without first asking the transport whether queued bytes preserve frame boundaries. Close command admission, drain pre-close receive work, and cancel the command dependency chain before transport shutdown. Add ready and flush lifecycle callbacks so drivers can publish transport state before registration and preserve framing before the close-time reset. Reopen admission only for the reset and driver close callback, then drain any response and cancel every command left by an interrupted request. Publish NCI_UP before clearing NCI_INIT after a successful open. Receive callbacks can therefore never observe both states clear during the transition. Discovered during an AI-assisted source audit using OpenAI Codex, gpt-5.6-sol. The implicated close and reset transitions were exercised by repeated open, down, reopen, and unregister cycles and by an incomplete-RX close. No separate pre-fix failure was isolated for this command dependency race. Patch development and test planning used the same assistant. Confirmed with repeated pty-backed NCI UART open, command, reset, and hangup cycles. No physical NFC controller was available. Fixes: 9961127d4bce ("NFC: nci: add generic uart support") Assisted-by: LLM (OpenAI Codex, gpt-5.6-sol) Signed-off-by: Shubham Antil --- include/net/nfc/nci_core.h | 10 +++++ net/nfc/nci/core.c | 83 +++++++++++++++++++++++++++++--------- net/nfc/nci/rsp.c | 3 ++ 3 files changed, 76 insertions(+), 20 deletions(-) diff --git a/include/net/nfc/nci_core.h b/include/net/nfc/nci_core.h index 664d5058e..43b38aeab 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,15 @@ 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); + /* Publish transport callbacks after core setup, before registration. */ + void (*ready)(struct nci_dev *ndev); + /* + * Flush transport-owned frames before the close-time reset. This may + * sleep and is called with req_lock held after command producers have + * stopped. Return an error if framing cannot be preserved; the core + * will then skip the reset exchange. + */ + int (*flush)(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); diff --git a/net/nfc/nci/core.c b/net/nfc/nci/core.c index 73e3a9647..13fb8c3bc 100644 --- a/net/nfc/nci/core.c +++ b/net/nfc/nci/core.c @@ -530,14 +530,14 @@ static int nci_open_device(struct nci_dev *ndev) msecs_to_jiffies(NCI_INIT_TIMEOUT)); } - clear_bit(NCI_INIT, &ndev->flags); - if (!rc) { set_bit(NCI_UP, &ndev->flags); + clear_bit(NCI_INIT, &ndev->flags); nci_clear_target_list(ndev); atomic_set(&ndev->state, NCI_IDLE); } else { /* Init failed, cleanup */ + clear_bit(NCI_INIT, &ndev->flags); skb_queue_purge(&ndev->cmd_q); skb_queue_purge(&ndev->rx_q); skb_queue_purge(&ndev->tx_q); @@ -553,6 +553,8 @@ static int nci_open_device(struct nci_dev *ndev) static int nci_close_device(struct nci_dev *ndev) { + int flush_rc = 0; + nci_req_cancel(ndev, ENODEV); /* This mutex needs to be held as a barrier for @@ -574,33 +576,76 @@ static int nci_close_device(struct nci_dev *ndev) mutex_unlock(&ndev->req_lock); return 0; } + set_bit(NCI_CLOSING, &ndev->flags); /* Drop RX and TX queues */ skb_queue_purge(&ndev->rx_q); skb_queue_purge(&ndev->tx_q); - /* Flush TX wq, RX wq flush can't be under the lock */ + /* + * An RX worker may already own a response which can make another + * command runnable. No new frame is admitted after NCI_UP was + * cleared. Drop req_lock so an old RX worker can finish a request, + * then cancel every command it made runnable. + */ + mutex_unlock(&ndev->req_lock); + flush_workqueue(ndev->rx_wq); + mutex_lock(&ndev->req_lock); + + /* Flush TX wq after the last pre-close RX work has completed. */ flush_workqueue(ndev->tx_wq); - /* Reset device */ + /* Stop command producers before flushing the transport backlog. */ + timer_delete_sync(&ndev->cmd_timer); skb_queue_purge(&ndev->cmd_q); - atomic_set(&ndev->cmd_cnt, 1); + flush_workqueue(ndev->cmd_wq); + timer_delete_sync(&ndev->cmd_timer); + flush_workqueue(ndev->cmd_wq); + if (ndev->ops->flush) + flush_rc = ndev->ops->flush(ndev); + + if (!flush_rc) { + /* Reset device only when the transport preserved framing. */ + atomic_set(&ndev->cmd_cnt, 1); + + set_bit(NCI_INIT, &ndev->flags); + clear_bit(NCI_CLOSING, &ndev->flags); + __nci_request(ndev, nci_reset_req, (void *)0, + msecs_to_jiffies(NCI_RESET_TIMEOUT)); + set_bit(NCI_CLOSING, &ndev->flags); + mutex_unlock(&ndev->req_lock); + flush_workqueue(ndev->rx_wq); + mutex_lock(&ndev->req_lock); - set_bit(NCI_INIT, &ndev->flags); - __nci_request(ndev, nci_reset_req, (void *)0, - msecs_to_jiffies(NCI_RESET_TIMEOUT)); + /* + * A signal can end the request before cmd_work dequeues its skb. + * Remove that command before admitting a driver close callback, + * some of which issue their own synchronous NCI command. + */ + 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); + atomic_set(&ndev->cmd_cnt, 1); + clear_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 - */ + /* Let driver close callbacks complete their final command exchange. */ ndev->ops->close(ndev); + set_bit(NCI_CLOSING, &ndev->flags); clear_bit(NCI_INIT, &ndev->flags); - - /* Flush cmd and tx wq */ - flush_workqueue(ndev->cmd_wq); + mutex_unlock(&ndev->req_lock); + flush_workqueue(ndev->rx_wq); + mutex_lock(&ndev->req_lock); 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); + timer_delete_sync(&ndev->data_timer); if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags)) @@ -612,9 +657,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; } @@ -1254,8 +1296,6 @@ int nci_register_device(struct nci_dev *ndev) struct device *dev = &ndev->nfc_dev->dev; char name[32]; - ndev->flags = 0; - INIT_WORK(&ndev->cmd_work, nci_cmd_work); snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev)); ndev->cmd_wq = create_singlethread_workqueue(name); @@ -1289,6 +1329,8 @@ int nci_register_device(struct nci_dev *ndev) mutex_init(&ndev->req_lock); INIT_LIST_HEAD(&ndev->conn_info_list); + if (ndev->ops->ready) + ndev->ops->ready(ndev); rc = nfc_register_device(ndev->nfc_dev); if (rc) @@ -1320,7 +1362,6 @@ void nci_unregister_device(struct nci_dev *ndev) struct nci_conn_info *conn_info, *n; nfc_unregister_rfkill(ndev->nfc_dev); - /* This set_bit is not protected with specialized barrier, * However, it is fine because the mutex_lock(&ndev->req_lock); * in nci_close_device() will help to emit one. @@ -1614,6 +1655,8 @@ static void nci_cmd_work(struct work_struct *work) struct sk_buff *skb; pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt)); + if (test_bit(NCI_CLOSING, &ndev->flags)) + return; /* Send queued command */ if (atomic_read(&ndev->cmd_cnt)) { 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); -- 2.54.0