mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/3] driver core: fix unbalanced reference counting for software nodes
@ 2026-10-15 14:46 Bartosz Golaszewski
  2026-10-15 14:46 ` [PATCH 1/3] driver core: provide a test-and-clear device flag accessor Bartosz Golaszewski
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Bartosz Golaszewski @ 2026-10-15 14:46 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Johan Hovold, Andy Shevchenko, Daniel Scally, Heikki Krogerus,
	Sakari Ailus, Eduardo Ruiz Duarte, Bartosz Golaszewski
  Cc: driver-core, linux-kernel, Rafael J. Wysocki, linux-acpi,
	Bartosz Golaszewski, stable

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

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@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: d419d682257d813f6cb7a28e42b35d0edf4ad038
change-id: 20261015-swnode-dev-notify-balance-e6f56fb9a469

Best regards,
-- 
Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/3] driver core: provide a test-and-clear device flag accessor
  2026-10-15 14:46 [PATCH 0/3] driver core: fix unbalanced reference counting for software nodes Bartosz Golaszewski
@ 2026-10-15 14:46 ` Bartosz Golaszewski
  2026-10-15 14:46 ` [PATCH 2/3] driver core: provide new device flag - DEV_FLAG_SWNODE_NOTIFIER Bartosz Golaszewski
  2026-10-15 14:46 ` [PATCH 3/3] software node: fix unbalanced reference counting Bartosz Golaszewski
  2 siblings, 0 replies; 5+ messages in thread
From: Bartosz Golaszewski @ 2026-10-15 14:46 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Johan Hovold, Andy Shevchenko, Daniel Scally, Heikki Krogerus,
	Sakari Ailus, Eduardo Ruiz Duarte, Bartosz Golaszewski
  Cc: driver-core, linux-kernel, Rafael J. Wysocki, linux-acpi,
	Bartosz Golaszewski

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

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
---
 include/linux/device.h | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/include/linux/device.h b/include/linux/device.h
index 879eb758b5eec3f634d4fee0e4a57433fb537801..c8de370d877f978dd4a160f0094812a1d8441078 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -817,6 +817,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);

-- 
2.47.3


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 2/3] driver core: provide new device flag - DEV_FLAG_SWNODE_NOTIFIER
  2026-10-15 14:46 [PATCH 0/3] driver core: fix unbalanced reference counting for software nodes Bartosz Golaszewski
  2026-10-15 14:46 ` [PATCH 1/3] driver core: provide a test-and-clear device flag accessor Bartosz Golaszewski
@ 2026-10-15 14:46 ` Bartosz Golaszewski
  2026-10-16  7:54   ` Bartosz Golaszewski
  2026-10-15 14:46 ` [PATCH 3/3] software node: fix unbalanced reference counting Bartosz Golaszewski
  2 siblings, 1 reply; 5+ messages in thread
From: Bartosz Golaszewski @ 2026-10-15 14:46 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Johan Hovold, Andy Shevchenko, Daniel Scally, Heikki Krogerus,
	Sakari Ailus, Eduardo Ruiz Duarte, Bartosz Golaszewski
  Cc: driver-core, linux-kernel, Rafael J. Wysocki, linux-acpi,
	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.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
---
 include/linux/device.h | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/include/linux/device.h b/include/linux/device.h
index c8de370d877f978dd4a160f0094812a1d8441078..b8f12d053cd704b12febc41035aaa44afe286351 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -599,6 +599,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_NOTIFIER: 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 {
@@ -612,6 +616,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
 };
@@ -833,6 +838,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
 

-- 
2.47.3


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 3/3] software node: fix unbalanced reference counting
  2026-10-15 14:46 [PATCH 0/3] driver core: fix unbalanced reference counting for software nodes Bartosz Golaszewski
  2026-10-15 14:46 ` [PATCH 1/3] driver core: provide a test-and-clear device flag accessor Bartosz Golaszewski
  2026-10-15 14:46 ` [PATCH 2/3] driver core: provide new device flag - DEV_FLAG_SWNODE_NOTIFIER Bartosz Golaszewski
@ 2026-10-15 14:46 ` Bartosz Golaszewski
  2 siblings, 0 replies; 5+ messages in thread
From: Bartosz Golaszewski @ 2026-10-15 14:46 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Johan Hovold, Andy Shevchenko, Daniel Scally, Heikki Krogerus,
	Sakari Ailus, Eduardo Ruiz Duarte, Bartosz Golaszewski
  Cc: driver-core, linux-kernel, Rafael J. Wysocki, linux-acpi,
	Bartosz Golaszewski, stable

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@vger.kernel.org
Fixes: 384f5a857bae ("software nodes: Split software_node_notify()")
Reported-by: Eduardo Ruiz Duarte <toorandom@gmail.com>
Closes: https://lore.kernel.org/all/20261011131518.48838-1-toorandom@gmail.com/
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
---
 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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 2/3] driver core: provide new device flag - DEV_FLAG_SWNODE_NOTIFIER
  2026-10-15 14:46 ` [PATCH 2/3] driver core: provide new device flag - DEV_FLAG_SWNODE_NOTIFIER Bartosz Golaszewski
@ 2026-10-16  7:54   ` Bartosz Golaszewski
  0 siblings, 0 replies; 5+ messages in thread
From: Bartosz Golaszewski @ 2026-10-16  7:54 UTC (permalink / raw)
  To: Bartosz Golaszewski
  Cc: driver-core, linux-kernel, Rafael J. Wysocki, linux-acpi,
	Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Johan Hovold, Andy Shevchenko, Daniel Scally, Heikki Krogerus,
	Sakari Ailus, Eduardo Ruiz Duarte, Bartosz Golaszewski

On Thu, 15 Oct 2026 16:46:23 +0200, Bartosz Golaszewski
<bartosz.golaszewski@oss.qualcomm.com> said:
> 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.
>
> Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
> ---
>  include/linux/device.h | 6 ++++++
>  1 file changed, 6 insertions(+)
>
> diff --git a/include/linux/device.h b/include/linux/device.h
> index c8de370d877f978dd4a160f0094812a1d8441078..b8f12d053cd704b12febc41035aaa44afe286351 100644
> --- a/include/linux/device.h
> +++ b/include/linux/device.h
> @@ -599,6 +599,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_NOTIFIER: Set if software_node_notify() was called for this

Ugh, sashiko pointed out a typo in the doc. I'll wait with resending until I
get some reviews on this.

Bart

> + *		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 {
> @@ -612,6 +616,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
>  };
> @@ -833,6 +838,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
>
>
> --
> 2.47.3
>
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-10-16  7:54 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-15 14:46 [PATCH 0/3] driver core: fix unbalanced reference counting for software nodes Bartosz Golaszewski
2026-10-15 14:46 ` [PATCH 1/3] driver core: provide a test-and-clear device flag accessor Bartosz Golaszewski
2026-10-15 14:46 ` [PATCH 2/3] driver core: provide new device flag - DEV_FLAG_SWNODE_NOTIFIER Bartosz Golaszewski
2026-10-16  7:54   ` Bartosz Golaszewski
2026-10-15 14:46 ` [PATCH 3/3] software node: fix unbalanced reference counting Bartosz Golaszewski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®