From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.182 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796043773; b=ngeis1jSOuBuVqAWms44sOLYKg+4YBChzKjgcAf2Gu7mr7OLTuDrgGIKoT2EnBDD2X6p qRcfZ3fX1cyn51tv57Nw6wcylmZKEBFy6ZDDBrR0us56jt8nAJmpNX9WJVS9JNNyVubA8 UkNrQMnciA3q+YzKhDsYcsWvuHhPVwgfwJnIZapZWTaLRSnY4ZxSUH7xrprzDJ2JWlPSe dtPPuRqqjce3ahdC9E76n7lJ8/8u48SRw0/veMbujnzUQkV0EOnkeUA8Bh++ZpFsqWaMz +t7Q3+D3pR4ubx9YTSK3BxT9ZAXgWA3jfGAE6tee00B9xmsEXzt3I+x/gQvvDvtWlkA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796043773; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=Qd3/MjPPhOSmwpnsRn8mpgT6fHJRZgAsiZqTVVWW7zU=; b=XLef2XoyaQpP42HH9JgJS7AwbgWx8Vq8iPPsbidL6kwcGv6WiXTF2BiQAnRSVBTnW1Zs 0FYYEaNZOlJKbgZa6xBNfcWFXjfWQwIjIUFHhBynn0IckaRLqELL2NAJBOHcIhkoNFvHW Zkh3kF+0nTjmhlCrYDs3lEu44dNCXEZ/pNyXeurY9GhEo7e3vPgWllPxh9iMI4aFcZjgJ 8j6KsUu0WbAykKY2PcOzLJDP6lycqwpqZnRkkAiS8uJ1XZ0sAOlpirC+lslqN0wsElNvO neszWO+K2NVtbMeblSu4Qk5UoSN0+cuZOOIfVrtzNhhHWtAmim8JtseOrFU4PwM/qjA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=octane.security; spf=pass smtp.mailfrom=octane.security; dkim=pass header.d=octane.security header.i=@octane.security header.a=rsa-sha256 header.s=google header.b=OSMCIHao; arc=none smtp.remote-ip=74.125.82.182 Received: from mail-dy1-f182.google.com (mail-dy1-f182.google.com [74.125.82.182]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DD75E606A334 for ; Mon, 30 Nov 2026 13:02:52 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org DD75E606A334 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=octane.security Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=octane.security Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=octane.security header.i=@octane.security header.a=rsa-sha256 header.s=google header.b=OSMCIHao Received: by mail-dy1-f182.google.com with SMTP id 5a478bee46e88-386ab9ab619so15454483eec.1 for ; Mon, 30 Nov 2026 05:02:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=octane.security; s=google; t=1796043772; x=1796648572; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=Qd3/MjPPhOSmwpnsRn8mpgT6fHJRZgAsiZqTVVWW7zU=; b=OSMCIHaopof7BIF3yv8K8WSiRXr41BACTUb6Xi2cSC3/Ui9nRanBU1P6KUrScU6HT3 IWuNXLUFA8pBjoNMZTR6z4+BsLjVD98WB3D31AMLkn+skuhYjwYeAqogGDvhEe4mfc2m ez1XeH7BfaJ+2F1WBB9EYjxl4Bn39uIdmGFYBtVmKxbac5Nqhhibheyz+vrX6Qg02Rzi 35rhirXYhCxds26zTsKM63PZ2OCB1cpkvNAFZpcl+djLgG2g44nYO3urQPItaXuldJ06 wdvE7FYFKJdG2EKCKsZsVNiRC9p+eYdw4Xq4Hq65Fi3VoP4jcz4Ney2v0BjdMIULA05r cd7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796043772; x=1796648572; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=Qd3/MjPPhOSmwpnsRn8mpgT6fHJRZgAsiZqTVVWW7zU=; b=z702K5IBGhsgCTZzRPlak1K/P+GId9B2mjFTPmpfA6k2LpgtZjz0LF/41Ns2EQ3e2j c1be0ZFKbs3+qHwrhTNrPA2ghA+grqXl9TnO1MaD+KT1nfi70utqerjJQ+FFToAcCu0I ObK7XoIBgCPJsrppGgg/cekbvhFIoA3WtMAPKy09XhJg/yCAAIw48nQuTi20R8/UNz8j UrKIGWzSRsCB+GjvgeIe4++41pWpXNArbZWsS+QHh6LA3oVK1L+wS31vg35PubY2jz5Q NtsyqKbbfWBLpnNE15mhsnByVwFZf+3HlOpS6Om6r/RDUNMWS5voPcrSOC/u8IARulbi Thmw== X-Gm-Message-State: APuU4Z8nr94Q1J+vlOIZZ1HV3ji/kaf/JNi1DNDVzttWS7g0Tx7J1iYN fGKbBWH8X7eI9Wo2CSO+vNNR3a1SiFoANyiGFKi10zbcOCIzUAS3PLJdU/zsYaIwZPw5hnbnuk/ UR/Lp X-Gm-Gg: AZFdTZds3+96sh1sos9j7XJmMRn7nZyJUDPEF5CuZUt3xj1ZNMOdJda3RZs1N4RJ85O CCsWuKRmqJs+ryTJd9qJKikCfu0FntJPIpMgQka6UqzXL8ejRbnh1ZOOocDsV6yl0mXJ80vKFGt ehefNxtsdHg0omQhsx0kzZojx0xV9+3cfQqGrs08/6Ofj61H0YGcmGzMCbNQgWLKQBf8l9dxONO mH4Rk4xv92J6MiikzWav18dhsVAmHT6JFVPUsLJK6i1c4r8G9efKHrfiOHjRxVi2oUZ4Bna44jS Mi2mxamrKfVCcYRWi/DNwSxZJaboYeJWB0OiksNJyXW0M4C0SUXIYWrHgz5x8KZFEdTYVRyCB18 PGTtIuB/CIIDY30oCIFnolr6ATCm6AMmd+j8e8vYBGJoVxfZCCX0BfSMxm75l8SHzvqbVq1z6oW qi9Cn/NEVwnEJkfPp8+F7YCgEziMBIQLegAuSzwfvxMWgJzKQWrpPQf6kMB92HNzsPnRLRhHM0B TR3DhYmURxWaLgalYIf/qGkPrutMjpn/fN5IqwwQunX42MS+0y3Q5XYNgVFGeqKKoy4AMHxAnAs j2FpqNZ1SHMOXSQ7t3uH4w1s5ERkdOBYLLRuFk9hwPns3byEN2P/+gIsQ+AEnU3g5pEKI+nbOAp z1RVwiVaUULxmlSnbzt+QH5xenOMesnCnDxVfT7qUbb1iIyzijQ== X-Received: by 2002:a05:7301:d809:20b0:386:b070:6f1c with SMTP id 5a478bee46e88-386b0707151mr24500087eec.21.1796043768760; Mon, 30 Nov 2026 05:02:48 -0800 (PST) Received: from localhost.localdomain ([157.119.125.90]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-385677c0c1dsm111829929eec.21.2026.11.30.05.02.46 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 30 Nov 2026 05:02:48 -0800 (PST) From: Shubham Antil To: netdev@vger.kernel.org Cc: Simon Horman , gio@octane.security, David Heidelberg , 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 Message-ID: <20261130130240.65411-2-shubham@octane.security> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261130130240.65411-1-shubham@octane.security> References: <20261130130240.65411-1-shubham@octane.security> X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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