[PATCH 3/3] software node: fix unbalanced reference counting
From: Bartosz Golaszewski
Date: Thu Oct 15 2026 - 11:43:54 EST
software_node_notify() takes a reference to the device's software node.
software_node_notify_remove() drops it unconditionally. A software node
attached with set_secondary_fwnode() after the device was registered
never gets it reference count increased but it gets decreased in
software_node_notify_remove() all the same causing an underflow and a
use-after-free. Use the new device flag to mark a device that needs the
reference to its software node decreased on device_del() and do it
conditionally in software_node_notify_remove().
Cc: stable@xxxxxxxxxxxxxxx
Fixes: 384f5a857bae ("software nodes: Split software_node_notify()")
Reported-by: Eduardo Ruiz Duarte <toorandom@xxxxxxxxx>
Closes: https://lore.kernel.org/all/20261011131518.48838-1-toorandom@xxxxxxxxx/
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@xxxxxxxxxxxxxxxx>
---
drivers/base/swnode.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/drivers/base/swnode.c b/drivers/base/swnode.c
index 84a77ffa3dc4bbcca62bbe67c3390693500b01ea..9207d7f96f20f8cfaff2a4eb8915609c499cce24 100644
--- a/drivers/base/swnode.c
+++ b/drivers/base/swnode.c
@@ -1176,6 +1176,8 @@ void software_node_notify(struct device *dev)
swnode->fwnode.dev = dev;
swnode_get(swnode);
+ dev_set_swnode_notified(dev);
+
ret = sysfs_create_link(&dev->kobj, &swnode->kobj, "software_node");
if (ret)
return;
@@ -1206,7 +1208,8 @@ void software_node_notify_remove(struct device *dev)
if (!device_match_fwnode(dev, &swnode->fwnode) && swnode->fwnode.dev == dev)
swnode->fwnode.dev = NULL;
- swnode_put(swnode);
+ if (dev_test_and_clear_swnode_notified(dev))
+ swnode_put(swnode);
if (swnode->managed) {
set_secondary_fwnode(dev, NULL);
--
2.47.3