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=1796379267; b=row0JS8PJsf5v2oM/GFRPIttSvOBCUwlB5e5G69QQWbeoDBhgEVv84ebXkRSsUz3rHmM vb/tpDi5DTJalR1bbNDiiartjUJ/8MSmuNbWYMGEvm6nR6+up4ypSrwmtH4QxBHBvwpFA fPY3gfpP0xwz6YX+X6VTbJUWGTdF8thT8fR/S+Qq+QwOZzrqO0ZQIZSyLlMBE2rwdhDhq 7Vttz2xVuEpMuHWL0l43la+QO/5fkg62kcLcYhTIWunc9mtzr0nsyZVIvLtcfQ1Yuwlrh LS5Dnz2GFL6JyAIOALQ8xyIq2Z597+ZZGG2yhVGlWyII5qBslTKgbCPRz6VsXMA0oQA== 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== 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 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) 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 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2D7491F000CE; Fri, 4 Dec 2026 10:14:26 +0000 (UTC) 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== Subject: Re: [PATCH net 5/5] nfc: nfcmrvl: do not use devres for the reset GPIO 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 Date: Fri, 04 Dec 2026 10:14:25 +0000 Message-ID: <179637926576.3146.8123139661626716612@kernel.org> In-Reply-To: <20261201-s2p-lpr-net-v1-5-6d9279b0f3f1@mbzuai.ac.ae> References: <20261201-s2p-lpr-net-v1-5-6d9279b0f3f1@mbzuai.ac.ae> X-sashiko-severity: Low 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 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