Skip to contentSkip to conversation

driver core: fix unbalanced reference counting for software nodes

PATCH Applied Bartosz Golaszewski 3 participants 6 messages Oct 23 – Nov 3

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v2 cover driver core: fix unbalanced reference counting for software nodes

Eduardo reported an issue with reference underflow when setting a software node with set_secondary_fwnode() and then repeatedly destroying and re-creating a device. This is caused by the fact that software_node_notify() takes a reference to the software node which is unconditionally dropped in software_node_notify_remove(). However the software node can be assigned after device_add() so the latter should only do it if it was bumped by the former.

This series proposes a fix in driver core. The first two patches extend the device flag API with a new accessor and value. The third is the actual fix in swnode.c and should be backported to stable (along the first two as dependencies).

---
Changes in v2:
- Fix typo in kerneldoc for the new flag
- Link to v1: https://patch.msgid.link/20261015-swnode-dev-notify-balance-v1-0-9e7537bd2869@oss.qualcomm.com

---
Bartosz Golaszewski (3):
driver core: provide a test-and-clear device flag accessor driver core: provide new device flag - DEV_FLAG_SWNODE_NOTIFIER software node: fix unbalanced reference counting

 drivers/base/swnode.c  |  5 ++++-
 include/linux/device.h | 10 ++++++++++
 2 files changed, 14 insertions(+), 1 deletion(-)

---
base-commit: bd1ecbb61afefc6a8b876ccbc31e9228b44e9eab
change-id: 20261015-swnode-dev-notify-balance-e6f56fb9a469

Best regards,

Signature · 1 line
Bartosz Golaszewski <[email protected]>
#1 lore raw

PATCH v2 1/3 driver core: provide a test-and-clear device flag accessor

Extend the device flag accessors with a test-and-clear operation.

---
 include/linux/device.h | 4 ++++
 1 file changed, 4 insertions(+)
include/linux/device.h +4 -0
index 429e39f647cf907d2a9868878d1f89526345c3cd..f31fd62dca9a89c1c9ad565b1cd673b71b740faf 100644
@@ -822,6 +822,10 @@ static inline void dev_assign_##accessor_name(struct device *dev, bool value) \
 static inline bool dev_test_and_set_##accessor_name(struct device *dev) \
 { \
 	return test_and_set_bit(flag_name, dev->flags); \
+} \
+static inline bool dev_test_and_clear_##accessor_name(struct device *dev) \
+{ \
+	return test_and_clear_bit(flag_name, dev->flags); \
 }
 
 __create_dev_flag_accessors(ready_to_probe, DEV_FLAG_READY_TO_PROBE);
Signature · 1 line
2.47.3
#2 lore raw

PATCH v2 2/3 driver core: provide new device flag - DEV_FLAG_SWNODE_NOTIFIER

In reply to Bartosz Golaszewski

Extend the set of device flags with one that indicates to driver core that this device was passed as argument to software_node_notify() and took a reference to this device's software node.

---
 include/linux/device.h | 6 ++++++
 1 file changed, 6 insertions(+)
include/linux/device.h +6 -0
index f31fd62dca9a89c1c9ad565b1cd673b71b740faf..6b082c638d451deef62a607095335e807b0841e2 100644
@@ -604,6 +604,10 @@ struct device_physical_location {
  *		ancestor device.
  * @DEV_FLAG_OFFLINE_DISABLED: If set, the device is permanently online.
  * @DEV_FLAG_OFFLINE: Set after successful invocation of bus type's .offline().
+ * @DEV_FLAG_SWNODE_NOTIFIED: Set if software_node_notify() was called for this
+ *		device and took a reference to the device's software node.
+ *		Indicates to software_node_notify_remove() that it should drop
+ *		the reference.
  * @DEV_FLAG_COUNT: Number of defined struct_device_flags.
  */
 enum struct_device_flags {
@@ -617,6 +621,7 @@ enum struct_device_flags {
 	DEV_FLAG_OF_NODE_REUSED = 7,
 	DEV_FLAG_OFFLINE_DISABLED = 8,
 	DEV_FLAG_OFFLINE = 9,
+	DEV_FLAG_SWNODE_NOTIFIED = 10,
 
 	DEV_FLAG_COUNT
 };
@@ -838,6 +843,7 @@ __create_dev_flag_accessors(dma_coherent, DEV_FLAG_DMA_COHERENT);
 __create_dev_flag_accessors(of_node_reused, DEV_FLAG_OF_NODE_REUSED);
 __create_dev_flag_accessors(offline_disabled, DEV_FLAG_OFFLINE_DISABLED);
 __create_dev_flag_accessors(offline, DEV_FLAG_OFFLINE);
+__create_dev_flag_accessors(swnode_notified, DEV_FLAG_SWNODE_NOTIFIED);
 
 #undef __create_dev_flag_accessors
 
Signature · 1 line
2.47.3
#3 lore raw

PATCH v2 3/3 software node: fix unbalanced reference counting

In reply to Bartosz Golaszewski

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().

Fixes 384f5a857bae ("software nodes: Split software_node_notify()")
---
 drivers/base/swnode.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/base/swnode.c +4 -1
index 84a77ffa3dc4bbcca62bbe67c3390693500b01ea..9207d7f96f20f8cfaff2a4eb8915609c499cce24 100644
@@ -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);
Signature · 1 line
2.47.3
maintainer Applied
#4 lore raw
In reply to Bartosz Golaszewski
Quoted 1 line · Bartosz Golaszewski

[PATCH v2 0/3] driver core: fix unbalanced reference counting for software nodes

Applied, thanks!

Branch: driver-core-testing
Tree: git://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git

[1/3] driver core: provide a test-and-clear device flag accessor

      commit: a03f144a4a12

[2/3] driver core: provide new device flag - DEV_FLAG_SWNODE_NOTIFIED
commit: 2d2dbe2f1bf7

[ Fix typo in the commit subject --
s/DEV_FLAG_SWNODE_NOTIFIER/DEV_FLAG_SWNODE_NOTIFIED/. - Danilo ]

[3/3] software node: fix unbalanced reference counting
commit: 071bbeab6aad

The patches will appear in the next linux-next integration (typically within 24 hours on weekdays).

The patches are in the driver-core-testing branch and will be promoted to driver-core-next after validation.

Tested-by on the v2 series
#5 lore raw
In reply to Bartosz Golaszewski

Tested your v2 on a Surface Pro 7+, where ipu-bridge attaches the IPU's software node with set_secondary_fwnode() after device_add(), which is the case the series is about. Reading the node's kobject reference count directly across a PCI remove of 0000:00:05.0:

6.19.8, unpatched: 4 bound, 3 while removed, stuck at 3 7.3 + this series: 4 bound, 4 while removed

The 4 is one registration reference plus three child port nodes. No refcount_t, KASAN or other splats either way.

How much that covers: the rescan does not re-probe, because mainline ipu-bridge only unregisters its IPU HID node on the error path of ipu_bridge_init(), so the next software_node_register() hits -EEXIST. Separate leak, unrelated to your patch, but it means only the first cycle really exercises the fixed path.

Assisted-by LLM