Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH v2 0/3] driver core: fix unbalanced reference counting for software nodes
@ 2026-10-23 10:14 Bartosz Golaszewski
  2026-10-23 10:14 ` [PATCH v2 3/3] software node: fix unbalanced reference counting Bartosz Golaszewski
  2026-11-02 20:15 ` [PATCH v2 0/3] driver core: fix unbalanced reference counting for software nodes Danilo Krummrich
  0 siblings, 2 replies; 3+ messages in thread
From: Bartosz Golaszewski @ 2026-10-23 10:14 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>
---
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,
-- 
Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>


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

* [PATCH v2 3/3] software node: fix unbalanced reference counting
  2026-10-23 10:14 [PATCH v2 0/3] driver core: fix unbalanced reference counting for software nodes Bartosz Golaszewski
@ 2026-10-23 10:14 ` Bartosz Golaszewski
  2026-11-02 20:15 ` [PATCH v2 0/3] driver core: fix unbalanced reference counting for software nodes Danilo Krummrich
  1 sibling, 0 replies; 3+ messages in thread
From: Bartosz Golaszewski @ 2026-10-23 10:14 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 related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2 0/3] driver core: fix unbalanced reference counting for software nodes
  2026-10-23 10:14 [PATCH v2 0/3] driver core: fix unbalanced reference counting for software nodes Bartosz Golaszewski
  2026-10-23 10:14 ` [PATCH v2 3/3] software node: fix unbalanced reference counting Bartosz Golaszewski
@ 2026-11-02 20:15 ` Danilo Krummrich
  1 sibling, 0 replies; 3+ messages in thread
From: Danilo Krummrich @ 2026-11-02 20:15 UTC (permalink / raw)
  To: Bartosz Golaszewski
  Cc: Greg Kroah-Hartman, Rafael J . Wysocki, Danilo Krummrich,
	Johan Hovold, Andy Shevchenko, Daniel Scally, Heikki Krogerus,
	Sakari Ailus, Eduardo Ruiz Duarte, Bartosz Golaszewski,
	driver-core, linux-kernel, Rafael J . Wysocki, linux-acpi, stable

On Fri, 23 Oct 2026 12:14:02 +0200, Bartosz Golaszewski wrote:
> [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.

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

end of thread, other threads:[~2026-11-02 20:17 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-23 10:14 [PATCH v2 0/3] driver core: fix unbalanced reference counting for software nodes Bartosz Golaszewski
2026-10-23 10:14 ` [PATCH v2 3/3] software node: fix unbalanced reference counting Bartosz Golaszewski
2026-11-02 20:15 ` [PATCH v2 0/3] driver core: fix unbalanced reference counting for software nodes Danilo Krummrich

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