Netdev List
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: arina.kharlamova@mbzuai.ac.ae
Cc: andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@kernel.org,
	kuba@kernel.org, pabeni@redhat.com, michal.simek@amd.com,
	esben@geanix.com, david@ixit.cz, xujialu@vimux.org,
	linusw@kernel.org, bartosz.golaszewski@oss.qualcomm.com,
	bh74.an@samsung.com, niklas.soderlund@ragnatech.se,
	niklas.soderlund+renesas@ragnatech.se, andrew@lunn.ch,
	netdev@vger.kernel.org, linux-renesas-soc@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org,
	oe-linux-nfc@lists.linux.dev
Subject: Re: [PATCH net 5/5] nfc: nfcmrvl: do not use devres for the reset GPIO
Date: Fri, 04 Dec 2026 10:14:25 +0000	[thread overview]
Message-ID: <179637926576.3146.8123139661626716612@kernel.org> (raw)
In-Reply-To: <20261201-s2p-lpr-net-v1-5-6d9279b0f3f1@mbzuai.ac.ae>

Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The commit message says nfcmrvl_nci_uart_open() "runs every time
  the N_NCI line discipline is set".

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the patch adds
  a new step to the same unsafe teardown window.
- [High] This problem wasn't introduced by this patch, but it is a TOCTOU
  race in the NCI UART core, and after this patch it can also leak the…

--

nfc: nfcmrvl: do not use devres for the reset GPIO

The patch moves the nfcmrvl reset GPIO request from the devres helpers
to the unmanaged gpiod_get_optional() and fwnode_gpiod_get(). The GPIO
is now released with gpiod_put() in nfcmrvl_nci_unregister_dev() and in
the error paths of nfcmrvl_nci_register_dev(). Later opens no longer
get -EBUSY when the tty device outlives the N_NCI line discipline.

> nfcmrvl_nci_uart_open() runs every time the N_NCI line discipline is
> set and passes the tty device to the code that requests the reset GPIO

[Severity: Low]
Is this description accurate? In net/nfc/nci/uart.c, the N_NCI ldisc
open hook nci_uart_tty_open() does not call the driver open callback.
It only does this and flushes the buffer:

    tty->disc_data = NULL;
    tty->receive_room = 65536;

nfcmrvl_nci_uart_open() is only reached through the NCIUARTSETDRIVER
ioctl:

nci_uart_tty_ioctl()
    case NCIUARTSETDRIVER:
        if (!nu)
            return nci_uart_set_driver(tty, (unsigned int)arg);

nci_uart_set_driver()
    ret = nu->ops.open(nu);

The matching close runs from nci_uart_tty_close(). So the cycle that
hits -EBUSY is: attach N_NCI, issue NCIUARTSETDRIVER, then detach.

The bug being fixed looks real. Could the commit message name the
NCIUARTSETDRIVER ioctl as the trigger instead of setting the ldisc?

> diff --git a/drivers/nfc/nfcmrvl/main.c b/drivers/nfc/nfcmrvl/main.c
> index 6efa832191172..0e2a7f8353b28 100644
> --- a/drivers/nfc/nfcmrvl/main.c
> +++ b/drivers/nfc/nfcmrvl/main.c

[ ... ]

> @@ -186,6 +189,8 @@ void nfcmrvl_nci_unregister_dev(struct nfcmrvl_private *priv)
>  
>  	nfcmrvl_fw_dnld_deinit(priv);
>  
> +	gpiod_put(priv->config.reset_gpio);
> +
>  	nci_free_device(ndev);
>  	kfree(priv);
>  }

[Severity: High]
This is a pre-existing issue, but the new gpiod_put() sits in the same
teardown window. Can fw_dnld_timeout() still be running when
nfcmrvl_nci_unregister_dev() puts the reset GPIO and frees priv?

nfcmrvl_nci_unregister_dev() never cancels priv->fw_dnld.timer
synchronously. It only does this:

    if (priv->ndev->nfc_dev->fw_download_in_progress)
        nfcmrvl_fw_dnld_abort(priv);

The timer handler runs in softirq context:

fw_dnld_timeout()
    fw_dnld_over(priv, -ETIMEDOUT)
        nfcmrvl_chip_halt(priv)
            gpiod_set_value(priv->config.reset_gpio, ...)
        nfc_fw_download_done(priv->ndev->nfc_dev, priv->fw_dnld.name, ...)
            dev->fw_download_in_progress = false;
            return nfc_genl_fw_download_done(dev, firmware_name, result);

nfc_fw_download_done() clears the flag before its last use of dev and
firmware_name. The unlocked check in unregister can therefore read false
and skip the abort while the handler is still running.

If the check reads true, fw_dnld_over() only waits when the timer is
pending:

    if (timer_pending(&priv->fw_dnld.timer))
        timer_delete_sync(&priv->fw_dnld.timer);

timer_pending() is false for a handler that is already running. Both
CPUs can then run fw_dnld_over() at the same time, and both can call
release_firmware() on priv->fw_dnld.fw.

In both cases unregister continues through these steps while the
handler may still be using priv->config.reset_gpio, priv->fw_dnld.name
and nfc_dev:

- nfcmrvl_fw_dnld_deinit(), which only destroys rx_wq
- gpiod_put()
- nci_free_device(), which frees nfc_dev
- kfree(priv)

With this patch, the handler can also drive a reset line that has
already been released and may have been requested again by the next
N_NCI instance.

One way to trigger this:

1. A user with CAP_NET_ADMIN starts NFC_CMD_FW_DOWNLOAD.
2. The chip does not answer, so fw_dnld_timeout() fires.
3. At the same time the tty is closed:
   nci_uart_tty_close()->nfcmrvl_nci_uart_close()->
   nfcmrvl_nci_unregister_dev()

Would an unconditional timer_delete_sync(&priv->fw_dnld.timer), or
timer_shutdown_sync(), before gpiod_put(), nci_free_device() and
kfree() close this window?

> diff --git a/drivers/nfc/nfcmrvl/uart.c b/drivers/nfc/nfcmrvl/uart.c
> index 9aedd168759c7..3023757ab976b 100644
> --- a/drivers/nfc/nfcmrvl/uart.c
> +++ b/drivers/nfc/nfcmrvl/uart.c

[ ... ]

> @@ -86,11 +85,11 @@ static int nfcmrvl_uart_parse_dt(struct device_node *node,
>  	pdata->flow_control = of_property_read_bool(matched_node, "flow-control");
>  	pdata->break_control = of_property_read_bool(matched_node, "break-control");
>  
> -	reset_gpio = devm_fwnode_gpiod_get_optional(dev,
> -						    of_fwnode_handle(matched_node),
> -						    "reset", GPIOD_OUT_HIGH,
> -						    "nfcmrvl_reset_n");
> -	if (IS_ERR(reset_gpio)) {
> +	reset_gpio = fwnode_gpiod_get(of_fwnode_handle(matched_node), "reset",
> +				      GPIOD_OUT_HIGH, "nfcmrvl_reset_n");

[Severity: High]
This isn't a bug introduced by this patch, but a race in the NCI UART
core can now leak this unmanaged descriptor for good. Can two
NCIUARTSETDRIVER ioctls issued at the same time on one tty both
register a device?

tty_ioctl() holds only an ldisc reference around ld->ops->ioctl(), not
the tty lock. nci_uart_tty_ioctl() checks tty->disc_data without a
lock. nci_uart_set_driver() only publishes it after the open callback
returns:

nci_uart_set_driver()
    ret = nu->ops.open(nu);
    ...
    tty->disc_data = nu;

That window covers all of nfcmrvl_nci_register_dev(). A possible
sequence:

Thread A: nfcmrvl_uart_parse_dt() gets the reset GPIO from
          fwnode_gpiod_get()
Thread B: fwnode_gpiod_get() returns -EBUSY, so it falls back to the
          module params
Both:     register an NCI/NFC device, take a module reference and
          store tty->disc_data (the last store wins)
Close:    nci_uart_tty_close() closes only the winning nu

Nothing releases the losing nu. Its nfcmrvl_private, its registered NFC
device and its module reference leak. If the loser was thread A, its
reset GPIO leaks too.

The leaked nu->tty also points at a tty that is freed after the final
close. If the leaked NFC device is later brought up,
nci_uart_send()->nci_uart_write_work() would dereference it.

Before this patch, the devres GPIO was at least released when the tty
device went away. Now the loser's descriptor is never put. Every later
open of the port would then lose the reset line and the DT settings,
which is the symptom this patch sets out to fix.

> +	if (reset_gpio == ERR_PTR(-ENOENT)) {
> +		reset_gpio = NULL;
> +	} else if (IS_ERR(reset_gpio)) {
>  		of_node_put(matched_node);
>  		return PTR_ERR(reset_gpio);
>  	}

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261201-s2p-lpr-net-v1-0-6d9279b0f3f1%40mbzuai.ac.ae

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

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 19:13 [PATCH net 0/5] net: free per-open allocations and interrupts Arina Kharlamova via B4 Relay
2026-12-01 19:13 ` [PATCH net 1/5] net: rtsn: fix irq name leak in rtsn_request_irq() Arina Kharlamova via B4 Relay
2026-12-01 19:13 ` [PATCH net 2/5] net: sxgbe: free the interrupts in sxgbe_release() Arina Kharlamova via B4 Relay
2026-12-04 10:14   ` netdev-bot+sashiko
2026-12-01 19:13 ` [PATCH net 3/5] net: sxgbe: free the TX ring arrays in free_tx_ring() Arina Kharlamova via B4 Relay
2026-12-01 19:13 ` [PATCH net 4/5] net: ll_temac: fix rx_skb leak in temac_dma_bd_init() Arina Kharlamova via B4 Relay
2026-12-01 19:13 ` [PATCH net 5/5] nfc: nfcmrvl: do not use devres for the reset GPIO Arina Kharlamova via B4 Relay
2026-12-04 10:14   ` netdev-bot+sashiko [this message]

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=179637926576.3146.8123139661626716612@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=andrew+netdev@lunn.ch \
    --cc=andrew@lunn.ch \
    --cc=arina.kharlamova@mbzuai.ac.ae \
    --cc=bartosz.golaszewski@oss.qualcomm.com \
    --cc=bh74.an@samsung.com \
    --cc=davem@davemloft.net \
    --cc=david@ixit.cz \
    --cc=edumazet@kernel.org \
    --cc=esben@geanix.com \
    --cc=kuba@kernel.org \
    --cc=linusw@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-renesas-soc@vger.kernel.org \
    --cc=michal.simek@amd.com \
    --cc=netdev@vger.kernel.org \
    --cc=niklas.soderlund+renesas@ragnatech.se \
    --cc=niklas.soderlund@ragnatech.se \
    --cc=oe-linux-nfc@lists.linux.dev \
    --cc=pabeni@redhat.com \
    --cc=xujialu@vimux.org \
    /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