πŸš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #26866

Re: [PATCH net 5/5] nfc: nfcmrvl: do not use devres for the reset GPIO

#26866
From: netdev-bot+sashiko
Date: Fri, 04 Dec 2026 10:14
201 lines
15482 bytes
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
Technical Details
Message-ID: <179637926576.3146.8123139661626716612@kernel.org>
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-377244-linux-kernel-netdev=dewy.ru
References: <20261201-s2p-lpr-net-v1-5-6d9279b0f3f1@mbzuai.ac.ae>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-377244-linux-kernel-netdev=dewy.ru
From: netdev-bot+sashiko@kernel.org
Newsgroups: linux.kernel.netdev
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
Organization: Usenet Network
Message-ID: <179637926576.3146.8123139661626716612@kernel.org>
References: <20261201-s2p-lpr-net-v1-5-6d9279b0f3f1@mbzuai.ac.ae>
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="2736016"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:EQ0vwDi70tjlzVk/zMy0m0wlgYs= sha256:HjyYBHCg2idX1eqOy2BpstM+ElaMiQ5A6KF/utRCgYc=
	sha1:usUOr3yeE7Jm9c4UkYQjsHoRALk= sha256:VBMs+Nvsm82KyPWfkavKkxm8SzxpncmIRPyCZ9qPhPw=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id E397B117B95; Fri,  4 Dec 2026 13:14:42 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.739
X-Spam-Status: No, score=-3.739 tagged_above=-9999 required=6.31
	tests=[DKIMWL_WL_HIGH=-0.24, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
	DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MAILING_LIST_MULTI=-1,
	RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001,
	URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=kernel.org
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id XGnVLlaUvUDN;
	Fri,  4 Dec 2026 13:14:40 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.232.135.74; helo=sto.lore.kernel.org; envelope-from=netdev+bounces-377244-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org
X-Received: from sto.lore.kernel.org (sto.lore.kernel.org [172.232.135.74])
	by mail.dewy.ru (Postfix) with ESMTPS id E48B2117B99; Fri,  4 Dec 2026 13:14:31 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sto.lore.kernel.org (Postfix) with ESMTP id 5E12A601197; Fri,  4 Dec 2026 10:14:31 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1796379268;
	b=tGeX+Sy9Nzd3Q4bsA7FiPSa/5Mdy4yw3jczFLbr+Zs99ZvvkX5Od7TRUzOThAhMeIHlQ
	 G9xdOV5DZs/8D5bl3Zpp/GmNjspGQW8fCWBhFCJgbWD6eI3RAKEx7zeTW8y2dPPwfPWp5
	 6zGZ92QlLCD6EYyM6i5f7YacBEJK5LRUNZNCjWVBdEif7bzf1WRQdWyMpmy+LXnLmFmsv
	 DF8nnJhGKwxcdceGiYGfWCfo6uSnAlbh+ACv8IM4n+48RwEs00OVUCvy5eyQqEJyCfIbg
	 6m1rMYoLSpU1DxzQLKcpaiFPl8cc8joLHD/dCB4qgsvQs9xfWURkmI6od6R5+0bRygg==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796379268;
	h=X-Original-To:Received:Received:DKIM-Signature:Subject:From:To:Cc:
	 Date:Message-ID:In-Reply-To:References:X-sashiko-severity:
	 Content-Type:Content-Transfer-Encoding:X-Mailing-List:List-Id:
	 List-Post:List-Help:List-Subscribe:List-Unsubscribe:List-Owner:
	 Precedence:MIME-Version;
	bh=5786JSbMYykkoe9kWcjlS6yMEO7zFqvD+xJ5Rxrs/QM=;
	b=llLpg72xAt9IJc+4WGSZ3y96F08LuFmLc3wr4X0ShagdMC2QZskaqWVdzmqUkR8zolRa
	 HYNEjlcHfgt4jTiYCHuNFgFNZp1vvswp/7wmHh/weHzLYc1t0zdNnLgRnoH4rRtDC+Hdt
	 +pNdoiLwWmoeeDOlQd+Apd+FFvek6LYj+n0jrwi3zF5XrSEQ+JhhFIsQSgHXroXn1WPpT
	 bYfiaqTJPOG2t4P6t5t94kBjBdXG7imHGDV/6jLt5x4yGFCCzEky0wcQCU43MI+K1pGKi
	 lVI2LfyR3KrVy24UBwGIiflv3n4+W99PEkNSoRcmgCRS7etzsfjQSC4H7tjPV5uVpBQ==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=YlthSlLm;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 5245A606A682;
	Fri,  4 Dec 2026 10:14:28 +0000 (UTC)
X-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=YlthSlLm
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1796379267;
	b=row0JS8PJsf5v2oM/GFRPIttSvOBCUwlB5e5G69QQWbeoDBhgEVv84ebXkRSsUz3rHmM
	 vb/tpDi5DTJalR1bbNDiiartjUJ/8MSmuNbWYMGEvm6nR6+up4ypSrwmtH4QxBHBvwpFA
	 fPY3gfpP0xwz6YX+X6VTbJUWGTdF8thT8fR/S+Qq+QwOZzrqO0ZQIZSyLlMBE2rwdhDhq
	 7Vttz2xVuEpMuHWL0l43la+QO/5fkg62kcLcYhTIWunc9mtzr0nsyZVIvLtcfQ1Yuwlrh
	 LS5Dnz2GFL6JyAIOALQ8xyIq2Z597+ZZGG2yhVGlWyII5qBslTKgbCPRz6VsXMA0oQA==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796379267;
	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=5786JSbMYykkoe9kWcjlS6yMEO7zFqvD+xJ5Rxrs/QM=;
	b=bprwKr0lDrqsnul5dmdCWk6lFE5uFeRPHDC5AgIaTmLFwZPi3BgbpwUJNCq6250NY6z8
	 yndwi3sNtq0WYzjUYFJW6whacGdQt+dg6NExl39G0re5IkmUcdxahBSLg/iaP/iXKIS3p
	 uGoUIeL9d21wDIDh84Ns4ljlUbCKVuLniU3/zv/hBeS4R4C+zZyelMP/R38fHDqBohj5n
	 tU71ZTtDOJLkD1I4nkG9koByHFWrcUo2YPz0TVXSNhR525HJFuGbvDlzSiaGd7Bf0kS/t
	 MVVh8Z7cpPGiiEFq/mLPP6yxfg3uvRBIc5LfBv/33P+S7MNzHLhrZ8g40JvWHszs6xg==
X-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=YlthSlLm;
	arc=none smtp.remote-ip=100.103.45.18
X-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 7101B606A339;
	Fri,  4 Dec 2026 10:14:27 +0000 (UTC)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2D7491F000CE;
	Fri,  4 Dec 2026 10:14:26 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
	s=k20260515; t=1796379267;
	bh=5786JSbMYykkoe9kWcjlS6yMEO7zFqvD+xJ5Rxrs/QM=;
	h=Subject:From:To:Cc:Date:In-Reply-To:References;
	b=YlthSlLm7AcE6D+QhVCeq6D5n1zMhu73n4xidhwMhn2URg39PjIIqG/sw4TYDLFXA
	 2KD4kQAbfZZEBlv2GcjuS2Om1VQpIsB8TswGQD/PmEDH/XMla3QboJi4PNjBMr4Enj
	 OALPsC92URj/EBVbg+swOq3oynNKZ58GsPdnYbwMnxxxbyq3s6InM1z1tefhW6hcf8
	 b1ALDFisH0XIVhZL3A4y1CdlnhBAIsOKHTCdGFIk3EoGTrwIvaQ8zdXXWuwJAX08an
	 EmN0VjdUKDHoil1C/L/ZSJLfJ0GSjPiTMtr+YBz33hNis21YSFjCo7TRhO1KNxMKmu
	 c0xMu2qa1Yq5Q==
X-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
X-In-Reply-To: <20261201-s2p-lpr-net-v1-5-6d9279b0f3f1@mbzuai.ac.ae>
X-sashiko-severity: Low
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list