mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Dmitry Torokhov <dmitry.torokhov@gmail.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	 "Rafael J. Wysocki" <rafael@kernel.org>,
	Danilo Krummrich <dakr@kernel.org>
Cc: Johan Hovold <johan@kernel.org>,
	 Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	 Daniel Scally <djrscally@gmail.com>,
	 Heikki Krogerus <heikki.krogerus@linux.intel.com>,
	 Sakari Ailus <sakari.ailus@linux.intel.com>,
	 Bartosz Golaszewski <brgl@kernel.org>,
	driver-core@lists.linux.dev,  linux-acpi@vger.kernel.org,
	linux-kernel@vger.kernel.org,  stable@vger.kernel.org
Subject: [PATCH 1/3] driver core: swnode: only release owned secondary software nodes
Date: Wed, 02 Dec 2026 17:34:49 -0800	[thread overview]
Message-ID: <20261202-swnode-shared-primary-fix-v1-1-7ff23caaa1ed@gmail.com> (raw)
In-Reply-To: <20261202-swnode-shared-primary-fix-v1-0-7ff23caaa1ed@gmail.com>

When a child device shares its parent's primary firmware node carrying
a secondary software node, detaching an ACPI-shared primary node via
set_primary_fwnode() leaves the child pointing to the parent's
secondary node without clearing the shared secondary link. That causes
platform_device_release() to drop an extra reference per child (via
device_remove_software_node(), or via fwnode_handle_put() even if
removal is skipped), leading to underflow and use-after-free.

When a non-ACPI shared primary node stays attached through release,
device_remove_software_node() unconditionally clears the shared
secondary link and drops one of the parent's references, prematurely
detaching the parent's software node and leaking the remaining
reference and sysfs link.

Track secondary software node attachment per device with a new device
flag. In set_primary_fwnode(), only retain a shared primary node's
secondary node on the child when the child attached it, clearing the
primary node's secondary link; otherwise clear the child's firmware
node pointer without touching the secondary link. Gate
device_remove_software_node() on the same flag before notifying or
detaching, and gate managed node release in
software_node_notify_remove() so calling device_remove_software_node()
on a registered managed device (such as during i2c_unregister_device()
or spi_unregister_device()) does not double-put.

Fixes: 99aed9227073 ("device property: Don't clear secondary pointer for shared primary firmware node")
Fixes: 7b5b51b2b05c ("driver core: platform: count references to all kinds of firmware nodes")
Fixes: 7d80509ff0da ("driver core: platform: unify release path")
Cc: stable@vger.kernel.org
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
 drivers/base/core.c    | 15 +++++++++++----
 drivers/base/swnode.c  | 14 ++++++++++----
 include/linux/device.h |  7 +++++++
 3 files changed, 28 insertions(+), 8 deletions(-)

diff --git a/drivers/base/core.c b/drivers/base/core.c
index 44f79f427b6b..673dc5941019 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -5250,12 +5250,19 @@ void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
 		dev->fwnode = fwnode;
 	} else {
 		if (fwnode_is_primary(fn)) {
-			dev->fwnode = fn->secondary;
-
-			/* Skip nullifying fn->secondary if the primary is shared */
-			if (parent && fn == parent->fwnode)
+			/*
+			 * If the primary fwnode is shared with the parent and
+			 * this device did not attach the secondary node, clear
+			 * dev->fwnode without leaving it pointing to the
+			 * parent's secondary node or modifying fn->secondary.
+			 */
+			if (parent && fn == parent->fwnode &&
+			    !dev_swnode_added(dev)) {
+				dev->fwnode = NULL;
 				return;
+			}
 
+			dev->fwnode = fn->secondary;
 			/* Set fn->secondary = NULL, so fn remains the primary fwnode */
 			fn->secondary = NULL;
 		} else {
diff --git a/drivers/base/swnode.c b/drivers/base/swnode.c
index bc51a92adcb8..da36f91f6434 100644
--- a/drivers/base/swnode.c
+++ b/drivers/base/swnode.c
@@ -1073,6 +1073,7 @@ int device_add_software_node(struct device *dev, const struct software_node *nod
 	}
 
 	set_secondary_fwnode(dev, &swnode->fwnode);
+	dev_set_swnode_added(dev);
 
 	/*
 	 * If the device has been fully registered by the time this function is
@@ -1088,11 +1089,12 @@ int device_add_software_node(struct device *dev, const struct software_node *nod
 EXPORT_SYMBOL_GPL(device_add_software_node);
 
 /**
- * device_remove_software_node - Remove device's software node
+ * device_remove_software_node - Remove device's secondary software node
  * @dev: The device with the software node.
  *
- * Remove a software node added by device_add_software_node() and
- * deregister it if this is the last reference.
+ * Remove a secondary software node attached by device_add_software_node()
+ * or device_create_managed_software_node() and deregister it if this is
+ * the last reference.
  */
 void device_remove_software_node(struct device *dev)
 {
@@ -1105,6 +1107,9 @@ void device_remove_software_node(struct device *dev)
 	if (fwnode_is_primary(&swnode->fwnode))
 		return;
 
+	if (!dev_test_and_clear_swnode_added(dev))
+		return;
+
 	if (device_is_registered(dev))
 		software_node_notify_remove(dev);
 
@@ -1145,6 +1150,7 @@ int device_create_managed_software_node(struct device *dev,
 
 	to_swnode(fwnode)->owner = dev;
 	set_secondary_fwnode(dev, fwnode);
+	dev_set_swnode_added(dev);
 
 	if (device_is_registered(dev))
 		software_node_notify(dev);
@@ -1208,7 +1214,7 @@ void software_node_notify_remove(struct device *dev)
 	if (dev_test_and_clear_swnode_notified(dev))
 		swnode_put(swnode);
 
-	if (swnode->owner == dev) {
+	if (swnode->owner == dev && dev_test_and_clear_swnode_added(dev)) {
 		set_secondary_fwnode(dev, NULL);
 		swnode_put(swnode);
 	}
diff --git a/include/linux/device.h b/include/linux/device.h
index 6b082c638d45..bdad371b177a 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -608,6 +608,11 @@ struct device_physical_location {
  *		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_SWNODE_ADDED: Set if a secondary software node was attached to
+ *		this device via device_add_software_node() or
+ *		device_create_managed_software_node(). Indicates that this
+ *		device owns the attach reference and is allowed to detach and
+ *		drop it.
  * @DEV_FLAG_COUNT: Number of defined struct_device_flags.
  */
 enum struct_device_flags {
@@ -622,6 +627,7 @@ enum struct_device_flags {
 	DEV_FLAG_OFFLINE_DISABLED = 8,
 	DEV_FLAG_OFFLINE = 9,
 	DEV_FLAG_SWNODE_NOTIFIED = 10,
+	DEV_FLAG_SWNODE_ADDED = 11,
 
 	DEV_FLAG_COUNT
 };
@@ -844,6 +850,7 @@ __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);
+__create_dev_flag_accessors(swnode_added, DEV_FLAG_SWNODE_ADDED);
 
 #undef __create_dev_flag_accessors
 

-- 
2.56.1.915.g69d5ec7298-goog


  reply	other threads:[~2026-12-03  1:35 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03  1:34 [PATCH 0/3] driver core: swnode: fix release with shared primary fwnode Dmitry Torokhov
2026-12-03  1:34 ` Dmitry Torokhov [this message]
2026-12-03  1:34 ` [PATCH 2/3] software node: replace owner pointer with managed bitfield Dmitry Torokhov
2026-12-03  1:34 ` [PATCH 3/3] driver core: platform: tests: add shared primary fwnode swnode tests Dmitry Torokhov
2026-12-03 11:35 ` [PATCH 0/3] driver core: swnode: fix release with shared primary fwnode Andy Shevchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261202-swnode-shared-primary-fix-v1-1-7ff23caaa1ed@gmail.com \
    --to=dmitry.torokhov@gmail.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=brgl@kernel.org \
    --cc=dakr@kernel.org \
    --cc=djrscally@gmail.com \
    --cc=driver-core@lists.linux.dev \
    --cc=gregkh@linuxfoundation.org \
    --cc=heikki.krogerus@linux.intel.com \
    --cc=johan@kernel.org \
    --cc=linux-acpi@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rafael@kernel.org \
    --cc=sakari.ailus@linux.intel.com \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®