* [PATCH] spi: serialize OF notifier device removal with controller removal
@ 2026-11-28 2:48 Debjit Ghosh
2026-11-28 3:09 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Debjit Ghosh @ 2026-11-28 2:48 UTC (permalink / raw)
To: broonie, robh, saravanak; +Cc: linux-spi, devicetree, Debjit Ghosh
From: Debjit Ghosh <debjit.ghosh@hpe.com>
The OF reconfig notifier and spi_unregister_controller() can both
unregister the same SPI device concurrently. However,
spi_unregister_controller() unregisters children holding ctlr->add_lock,
but of_spi_notify() does not. So both contexts, sometimes executing
concurrently, would call device_del() on the same device potentially
corrupting the controller's child klist.
Oops: general protection fault, probably for non-canonical
address 0xdead0000000000f8
CPU: 13 UID: 0 PID: 173 Comm: irq/38-pciehp
RIP: 0010:klist_next+0x6e/0x140
device_for_each_child+0x5d/0xb0
spi_unregister_controller+0x3a/0x140
...
mfd_remove_devices+0x36/0x50
pci_device_remove+0x3d/0xb0
...
pciehp_ist+0x13c/0x180
while an overlay unload mechanism was concurrently removing the same
device through of_overlay_remove() -> of_spi_notify().
Serialize the notifier's removal path with the controller's child
unregistration using ctlr->add_lock, and recheck OF_POPULATED under the
lock before calling spi_unregister_device().
Signed-off-by: Debjit Ghosh <debjit.ghosh@hpe.com>
---
drivers/spi/spi.c | 21 +++++++++++++++++++--
1 file changed, 19 insertions(+), 2 deletions(-)
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 8a06fb4e309a..6562e3ac4dd6 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -5308,8 +5308,25 @@ static int of_spi_notify(struct notifier_block *nb, unsigned long action,
if (spi == NULL)
return NOTIFY_OK; /* No? not meant for us */
- /* Unregister takes one ref away */
- spi_unregister_device(spi);
+ /*
+ * Serialize against spi_unregister_controller. The device
+ * can be unregistered concurrently by its controller being
+ * torn down (e.g. on surprise hot removal of the underlying
+ * bus hosting the controller).
+ */
+ ctlr = spi->controller;
+ if (IS_ENABLED(CONFIG_SPI_DYNAMIC))
+ mutex_lock(&ctlr->add_lock);
+
+ /*
+ * Recheck under the lock as whichever context executes first
+ * will depopulate the flag.
+ */
+ if (of_node_check_flag(rd->dn, OF_POPULATED))
+ spi_unregister_device(spi); /* takes one ref away */
+
+ if (IS_ENABLED(CONFIG_SPI_DYNAMIC))
+ mutex_unlock(&ctlr->add_lock);
/* And put the reference of the find */
put_device(&spi->dev);
--
2.25.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] spi: serialize OF notifier device removal with controller removal
2026-11-28 2:48 [PATCH] spi: serialize OF notifier device removal with controller removal Debjit Ghosh
@ 2026-11-28 3:09 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-28 3:09 UTC (permalink / raw)
To: Debjit Ghosh; +Cc: devicetree, robh, conor+dt
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] spi: incomplete serialization in of_spi_notify() allows of_node
UAF
- [Low] spi: missing Fixes trailer for klist corruption fix in
of_spi_notify()
--
commit d8aab75e4e977ea5193817573284d57a2881b77e
Author: Debjit Ghosh <debjit.ghosh@hpe.com>
> spi: serialize OF notifier device removal with controller removal
>
> The OF reconfig notifier and spi_unregister_controller() can both
> unregister the same SPI device concurrently. However,
> spi_unregister_controller() unregisters children holding ctlr->add_lock,
> but of_spi_notify() does not. So both contexts, sometimes executing
> concurrently, would call device_del() on the same device potentially
> corrupting the controller's child klist.
>
> Oops: general protection fault, probably for non-canonical
> address 0xdead0000000000f8
> CPU: 13 UID: 0 PID: 173 Comm: irq/38-pciehp
> RIP: 0010:klist_next+0x6e/0x140
> device_for_each_child+0x5d/0xb0
> spi_unregister_controller+0x3a/0x140
> ...
> mfd_remove_devices+0x36/0x50
> pci_device_remove+0x3d/0xb0
> ...
> pciehp_ist+0x13c/0x180
>
> while an overlay unload mechanism was concurrently removing the same
> device through of_overlay_remove() -> of_spi_notify().
>
> Serialize the notifier's removal path with the controller's child
> unregistration using ctlr->add_lock, and recheck OF_POPULATED under the
> lock before calling spi_unregister_device().
>
> Signed-off-by: Debjit Ghosh <debjit.ghosh@hpe.com>
[Severity: Low]
Should this bug fix include a Fixes tag?
Fixes: ce79d54ae447 ("spi/of: Add OF notifier handler")
> diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
> index 0569f30785e9f..6a6c590649c1a 100644
> --- a/drivers/spi/spi.c
> +++ b/drivers/spi/spi.c
> @@ -5390,8 +5390,25 @@ static int of_spi_notify(struct notifier_block *nb, unsigned long action,
> if (spi == NULL)
> return NOTIFY_OK; /* No? not meant for us */
>
> - /* Unregister takes one ref away */
> - spi_unregister_device(spi);
> + /*
> + * Serialize against spi_unregister_controller. The device
> + * can be unregistered concurrently by its controller being
> + * torn down (e.g. on surprise hot removal of the underlying
> + * bus hosting the controller).
> + */
> + ctlr = spi->controller;
> + if (IS_ENABLED(CONFIG_SPI_DYNAMIC))
> + mutex_lock(&ctlr->add_lock);
> +
> + /*
> + * Recheck under the lock as whichever context executes first
> + * will depopulate the flag.
> + */
> + if (of_node_check_flag(rd->dn, OF_POPULATED))
> + spi_unregister_device(spi); /* takes one ref away */
> +
> + if (IS_ENABLED(CONFIG_SPI_DYNAMIC))
> + mutex_unlock(&ctlr->add_lock);
[Severity: High]
Can spi->dev.of_node still be freed while spi_unregister_controller() is
in the middle of device_del(&spi->dev)?
spi_unregister_device() clears OF_POPULATED and drops its of_node
reference before calling device_del(&spi->dev):
drivers/spi/spi.c:spi_unregister_device() {
...
if (is_of_node(fwnode)) {
of_node_clear_flag(to_of_node(fwnode), OF_POPULATED);
of_node_put(to_of_node(fwnode));
} else if (is_acpi_device_node(fwnode)) {
acpi_device_clear_enumerated(to_acpi_device_node(fwnode));
}
device_remove_software_node(&spi->dev);
device_del(&spi->dev);
...
}
Because of_spi_notify() checks OF_POPULATED (and calls
of_find_spi_device_by_node()) before acquiring ctlr->add_lock, it can
return NOTIFY_OK without waiting for spi_unregister_device() to finish:
CPU0 CPU1
spi_unregister_controller()
mutex_lock(&ctlr->add_lock)
...
spi_unregister_device(spi)
of_node_clear_flag(dn,
OF_POPULATED)
of_node_put(dn)
of_overlay_remove()
...
of_spi_notify()
!of_node_check_flag(rd->dn,
OF_POPULATED)
return NOTIFY_OK
free_overlay_changeset()
...
of_node_put(ce->np)
of_node_release()
device_del(&spi->dev)
...
of_device_uevent()
dev->of_node <-- use-after-free
When CPU1 sees OF_POPULATED already cleared (or fails to find the device
on spi_bus_type after bus_remove_device()), of_overlay_remove() proceeds
to destroy the changeset and free rd->dn and the overlay FDT memory while
CPU0 is still running driver remove callbacks and of_device_uevent() on
spi->dev.of_node.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261128024830.57367-1-dghosh@juniper.net?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-28 3:09 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-28 2:48 [PATCH] spi: serialize OF notifier device removal with controller removal Debjit Ghosh
2026-11-28 3:09 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox