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/2026…%40mbzuai.ac.ae