From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796133886; b=omYwQ3zEFFF6M2I5UCHzi5pvuiHy5+O/Kc5QqSkMLeAAz12OaUckzyRO3DMAgfr74jHS 17tbGu53KI5gzmQZKBUqHqL9EPE0YBlHh/1GUWdCxXIMXSqW5UmmTYizFpRQcYuafIfyG HoGQW5GxXSdZEP4oB5/jGglCfgDITHqXYWzffAailbgGq96DCmmB3WWMw8DqugmmZBwo5 aWaVrEG+pbl5FcrGME+Wj2/pR13pBPuVdO7HpF6HWv+2yzmL2J3mrl0YadxotiY64GvFM 7u6+77VY3rsG3n5pwP0Zo5V2/xy5mvLhlqCyIA95Bx5jeXEbBdDi+iYgdjh3CgIGBgg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796133886; h=Received:DKIM-Signature:Subject:From:To:Cc:Date:Message-ID: In-Reply-To:References:X-sashiko-severity:Content-Type: Content-Transfer-Encoding:MIME-Version; bh=PBqAnWAnjRoTJUE5oUjftdJOiDtlyyoMUPoAvbqnZEI=; b=Q2KX+Zxo6HuS26s2Zh3/bC35AnbmfsyH/rUkTPWpw28q5d+tI2ro6Swpyhu/YtS/zO8O xeGiqXo7zZmiJVs4VtnYv+VJhtPXlGhOXhhN4M2Si3QB3o5SUlPDTv2zZYJwDAuT9BMVl iWRd24Gt2MfcBeiWdIIKOqnUdyip68tMFOurbISwHVKLhpE0pICEp+54snzhWYpE5RFsM gvAc3fHBnpZGiUrS2XSaPFeJIDqHcpXcarxjdVO0kwfGcbXH8/B6WET8kp4RKsTBgTJ/X ayunWJ1OKTDVhQYPuDuS7EaKLV0AhP7z+5jWAKwH5ZSCd/Txxy/YV3PsgBzOwL7QSgQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=TdpRLdHH; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 288AE606A327; Tue, 1 Dec 2026 14:04:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=TdpRLdHH Received: by smtp.kernel.org (Postfix) with ESMTPSA id E24B51F008A0; Tue, 1 Dec 2026 14:04:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796133885; bh=PBqAnWAnjRoTJUE5oUjftdJOiDtlyyoMUPoAvbqnZEI=; h=Subject:From:To:Cc:Date:In-Reply-To:References; b=TdpRLdHHrP4EsCPOPgIVwuk0D/KTPtw/X3AKo2kZHdrBF5a1a0FqhncIFT18VFKrq bjx+2qiu1Gc7BHQGfTizPo576W7WKD7MQMRLbrxdX4WAVNfCfctWs00KbZBHWWawUe oXqnI8EKsko6YR46tlvdXeF0CBoKOiQPp44nbnANayRJQdqrOM/nQ2Rb0U6kvfL0Nr mRwTwdPEhTSW9F5VpsWmrQPMXZjplggXkY77HTI/ySGiQsmTohOvTUh6QeESy4xgk8 njDqGDBW0Jy7shg89/E3RmAd+PadKOoGQdtfKlzRd7u5+u+Gg9WeO8oQeMGjtD1bvk UbUN8e+wK/IwA== Subject: Re: [PATCH net v9 1/3] nfc: nci: add a pre-unregister shutdown stage From: netdev-bot+sashiko@kernel.org To: shubham@octane.security Cc: netdev@vger.kernel.org, horms@kernel.org, gio@octane.security, david@ixit.cz, oe-linux-nfc@lists.linux.dev, kuba@kernel.org Date: Tue, 01 Dec 2026 14:04:44 +0000 Message-ID: <179613388446.3146.16478558901406623474@kernel.org> In-Reply-To: <20261130130240.65411-2-shubham@octane.security> References: <20261130130240.65411-2-shubham@octane.security> X-sashiko-severity: High Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit 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 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