Netdev List
 help / color / mirror / Atom feed
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
Subject: Re: [PATCH net v9 1/3] nfc: nci: add a pre-unregister shutdown stage
Date: Tue, 01 Dec 2026 14:04:44 +0000	[thread overview]
Message-ID: <179613388446.3146.16478558901406623474@kernel.org> (raw)
In-Reply-To: <20261130130240.65411-2-shubham@octane.security>

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

  reply	other threads:[~2026-12-01 14:04 UTC|newest]

Thread overview: 6+ 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 ` [PATCH net v9 1/3] nfc: nci: add a pre-unregister shutdown stage Shubham Antil
2026-12-01 14:04   ` netdev-bot+sashiko [this message]
2026-11-30 13:02 ` [PATCH net v9 2/3] nfc: nfcmrvl: serialize firmware completion and teardown Shubham Antil
2026-12-01 14:04   ` netdev-bot+sashiko
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=179613388446.3146.16478558901406623474@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=david@ixit.cz \
    --cc=gio@octane.security \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=oe-linux-nfc@lists.linux.dev \
    --cc=shubham@octane.security \
    /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