Skip to contentSkip to conversation

driver core: platform: fix swnode use-after-free on device release

PATCH In review Johan Hovold 3 participants 16 messages Nov 17 – Nov 18

Subsystems (6)
lore
Share & tools
lore
author maintainer
#5 lore raw

PATCH v3 4/8 software node: ignore primary nodes in device_remove_software_node()

In reply to Johan Hovold

device_remove_software_node() should only be used to remove secondary firmware nodes added by device_add_software_node() but will currently drop a reference also if a device has a primary software node.

Bail out early if device_remove_software_node() is called for a device whose primary firmware node is a software node and fix up the two callers that currently work around this themselves.

---
 drivers/base/base.h         | 2 ++
 drivers/base/core.c         | 2 +-
 drivers/base/platform.c     | 9 ++++-----
 drivers/base/swnode.c       | 3 +++
 drivers/i2c/i2c-core-base.c | 9 +++------
 5 files changed, 13 insertions(+), 12 deletions(-)
drivers/base/base.h +2 -0
index 866b2beb6baa..5474735e3e76 100644
@@ -315,6 +315,8 @@ static inline int devtmpfs_create_node(struct device *dev) { return 0; }
 static inline int devtmpfs_delete_node(struct device *dev) { return 0; }
 #endif
 
+bool fwnode_is_primary(struct fwnode_handle *fwnode);
+
 void software_node_init(void);
 void software_node_notify(struct device *dev);
 void software_node_notify_remove(struct device *dev);
drivers/base/core.c +1 -1
index bf97b06337a1..ef36e336a576 100644
@@ -5212,7 +5212,7 @@ int dev_warn_probe(const struct device *dev, int err, const char *fmt, ...)
 }
 EXPORT_SYMBOL_GPL(dev_warn_probe);
 
-static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
+bool fwnode_is_primary(struct fwnode_handle *fwnode)
 {
 	return fwnode && !IS_ERR(fwnode->secondary);
 }
drivers/base/platform.c +4 -5
index be5d2f315fdb..e0b38dbd5acf 100644
@@ -598,15 +598,14 @@ static void platform_device_release(struct device *dev)
 {
 	struct platform_object *pa = container_of(dev, struct platform_object,
 						  pdev.dev);
-	bool fwnode_is_software_node = is_software_node(dev_fwnode(dev));
 
 	device_remove_software_node(dev);
 	/*
-	 * If the firmware node is a software node, its reference count
-	 * was decremented by device_remove_software_node().
+	 * Must be done after device_remove_software_node() which may have
+	 * freed a secondary firmware node and cleared the fwnode pointer.
 	 */
-	if (!fwnode_is_software_node)
-		fwnode_handle_put(pa->pdev.dev.fwnode);
+	fwnode_handle_put(dev->fwnode);
+
 	kfree(pa->pdev.dev.platform_data);
 	kfree(pa->pdev.mfd_cell);
 	kfree(pa->pdev.resource);
drivers/base/swnode.c +3 -0
index b2593b2fcada..95283d8bda26 100644
@@ -1101,6 +1101,9 @@ void device_remove_software_node(struct device *dev)
 	if (!swnode)
 		return;
 
+	if (fwnode_is_primary(&swnode->fwnode))
+		return;
+
 	if (device_is_registered(dev))
 		software_node_notify_remove(dev);
 
drivers/i2c/i2c-core-base.c +3 -6
index c55ca14eaad9..af3ae2a83626 100644
@@ -1056,7 +1056,6 @@ EXPORT_SYMBOL_GPL(i2c_new_client_device);
 void i2c_unregister_device(struct i2c_client *client)
 {
 	struct fwnode_handle *fwnode;
-	bool fwnode_is_software_node;
 
 	if (IS_ERR_OR_NULL(client))
 		return;
@@ -1067,14 +1066,12 @@ void i2c_unregister_device(struct i2c_client *client)
 	else if (is_acpi_device_node(fwnode))
 		acpi_device_clear_enumerated(to_acpi_device_node(fwnode));
 
-	fwnode_is_software_node = is_software_node(fwnode);
 	device_remove_software_node(&client->dev);
 	/*
-	 * If the firmware node is a software node, its reference count
-	 * was decremented by device_remove_software_node().
+	 * Must be done after device_remove_software_node() which may have
+	 * freed a secondary firmware node and cleared the fwnode pointer.
 	 */
-	if (!fwnode_is_software_node)
-		fwnode_handle_put(fwnode);
+	fwnode_handle_put(client->dev.fwnode);
 	device_unregister(&client->dev);
 }
 EXPORT_SYMBOL_GPL(i2c_unregister_device);
Signature · 1 line
2.56.1

Johan Hovold

author maintainer
#0 lore raw

v3 cover driver core: platform: fix swnode use-after-free on device release

When fixing a use-after-free regression in 7.3, I noticed that the software node implementation is internally inconsistent.

Specifically, the firmware node pointer of a struct device may point to either a primary or secondary firmware node, but in several places kernel-doc and comments suggests that the firmware node pointer always points to a primary node (cf. [1][2]).

This series fixes the 7.3 regression along with a similar issue in i2c core due which are both stem from the fact that device_remove_software_node() happens to drop a reference to a primary software node that was never added by device_add_software_node().

The next two patches clarify that device_remove_software_node() should only be used to remove nodes added by device_add_software_node() and stops touching primary nodes.

The rest of the series addresses further inconsistencies.

The first two fixes could go into 7.4 and the rest (which depends on the fixes) into 7.5. And preferably with an ack from the i2c maintainer for patches 2/8 and 4/8.

Johan

[1] 97badf873ab6 ("device property: Make it possible to use secondary firmware nodes")
[2] 3f7bddaf5d5a ("device property: add description of fwnode cases")

Changes in v3
- drop stray "the" in commit message (1/8)
- refer to device_add_software_node() in summary (3/8)
- use "software node:" as patch summary prefix (3,4,7/8)
- don't export fwnode_is_primary() (4/8)
- only warn when assigning a firmware node to a device which already has a software node or secondary firmware node assigned (5/8)
- fix "at" typo in commit message (8/8)
- drop one comment instead of updating, and rephrase the other (8/8)

Changes in v2
- rename temporary variable and amend comment as dev->fwnode is not always a primary node (patch 1/8)
- fix related UAF in i2c core (patch 2/8, new)
- fix inconsistencies in interfaces, kernel-doc, and comments (patches 3-8, new).

Johan Hovold (8):
driver core: platform: fix swnode use-after-free on device release
i2c: core: fix swnode use-after-free on client deregistration software node: fix device_add_software_node() kernel doc software node: ignore primary nodes in device_remove_software_node() driver core: platform: warn on bad fwnode assignment driver core: platform: tests: fix swnode reuse test description software node: fix software_node_notify() primary node comment device property: fix fwnode_get_next_child_node() comments

 drivers/base/base.h                      |  2 +
 drivers/base/core.c                      |  2 +-
 drivers/base/platform.c                  | 48 ++++++++++++++----------
 drivers/base/property.c                  |  5 +--
 drivers/base/swnode.c                    | 20 +++++-----
 drivers/base/test/platform-device-test.c |  5 ++-
 drivers/i2c/i2c-core-base.c              | 10 ++---
 7 files changed, 51 insertions(+), 41 deletions(-)
Signature · 1 line
2.56.1
author maintainer
#1 lore raw

PATCH v3 1/8 driver core: platform: fix swnode use-after-free on device release

When a device has a software node, device_remove_software_node() may drop the last reference and free it during platform_device_release(). If the software node is also the primary node then the device fwnode pointer is never cleared.

Use a temporary variable to track whether the firmware node is a software node to avoid a use-after-free in is_software_node() and having fwnode_handle_put() call a function pointer via poisoned or reallocated memory when the primary node is a software node.

This issue was flagged by Sashiko when reviewing a related fix.

Fixes 7d80509ff0da ("driver core: platform: unify release path")
Cc Bartosz Golaszewski <[email protected]>
---
 drivers/base/platform.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/base/platform.c +4 -3
index 979593e5cca1..be5d2f315fdb 100644
@@ -598,13 +598,14 @@ static void platform_device_release(struct device *dev)
 {
 	struct platform_object *pa = container_of(dev, struct platform_object,
 						  pdev.dev);
+	bool fwnode_is_software_node = is_software_node(dev_fwnode(dev));
 
 	device_remove_software_node(dev);
 	/*
-	 * If the primary firmware node is a software node, its reference count
-	 * was already decreased by the call to device_remove_software_node().
+	 * If the firmware node is a software node, its reference count
+	 * was decremented by device_remove_software_node().
 	 */
-	if (!is_software_node(dev_fwnode(dev)))
+	if (!fwnode_is_software_node)
 		fwnode_handle_put(pa->pdev.dev.fwnode);
 	kfree(pa->pdev.dev.platform_data);
 	kfree(pa->pdev.mfd_cell);
Signature · 1 line
2.56.1
author maintainer
#2 lore raw

PATCH v3 2/8 i2c: core: fix swnode use-after-free on client deregistration

In reply to Johan Hovold

If a client has both a primary and a secondary firmware node, the secondary node is accessed via the ->secondary pointer of the primary node.

Use a temporary variable to track whether the firmware node is a software node and remove any software node before dropping its reference to avoid a use-after-free in device_remove_software_node() when accessing the secondary firmware node.

Fixes df6d7277e552 ("i2c: core: Do not dereference fwnode in struct device")
Fixes 1c24e5fc0c70 ("i2c: core: Fix double-free of fwnode in i2c_unregister_device()")
Cc Andy Shevchenko <[email protected]>
Cc Hans de Goede <[email protected]>
---
 drivers/i2c/i2c-core-base.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
drivers/i2c/i2c-core-base.c +6 -5
index 81603317b64d..c55ca14eaad9 100644
@@ -1056,6 +1056,7 @@ EXPORT_SYMBOL_GPL(i2c_new_client_device);
 void i2c_unregister_device(struct i2c_client *client)
 {
 	struct fwnode_handle *fwnode;
+	bool fwnode_is_software_node;
 
 	if (IS_ERR_OR_NULL(client))
 		return;
@@ -1066,14 +1067,14 @@ void i2c_unregister_device(struct i2c_client *client)
 	else if (is_acpi_device_node(fwnode))
 		acpi_device_clear_enumerated(to_acpi_device_node(fwnode));
 
+	fwnode_is_software_node = is_software_node(fwnode);
+	device_remove_software_node(&client->dev);
 	/*
-	 * If the primary fwnode is a software node it is free-ed by
-	 * device_remove_software_node() below, avoid double-free.
+	 * If the firmware node is a software node, its reference count
+	 * was decremented by device_remove_software_node().
 	 */
-	if (!is_software_node(fwnode))
+	if (!fwnode_is_software_node)
 		fwnode_handle_put(fwnode);
-
-	device_remove_software_node(&client->dev);
 	device_unregister(&client->dev);
 }
 EXPORT_SYMBOL_GPL(i2c_unregister_device);
Signature · 1 line
2.56.1
author maintainer
#3 lore raw

PATCH v3 3/8 software node: fix device_add_software_node() kernel doc

In reply to Johan Hovold

Software nodes added to a device by device_add_software_node() are always secondary firmware nodes even if they may be stored in the dev->fwnode pointer.

Fix the kernel doc which incorrectly claims that a software node added using this interface "could become the primary node".

Also clarify that device_remove_software_node() should only be used to remove software nodes added by device_add_software_node().

Fixes e68d0119e328 ("software node: Introduce device_add_software_node()")
Cc Heikki Krogerus <[email protected]>
---
 drivers/base/swnode.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/base/swnode.c +4 -4
index ce4efc506b9c..b2593b2fcada 100644
@@ -1048,9 +1048,8 @@ EXPORT_SYMBOL_GPL(fwnode_remove_software_node);
  * @dev: The device the software node is meant for.
  * @node: The software node.
  *
- * This function will make @node the secondary firmware node pointer of @dev. If
- * @dev has no primary node, then @node will become the primary node. The
- * function will register @node automatically if it wasn't already registered.
+ * Register @node if it was not already registered and make it the secondary
+ * firmware node of @dev.
  */
 int device_add_software_node(struct device *dev, const struct software_node *node)
 {
@@ -1091,7 +1090,8 @@ EXPORT_SYMBOL_GPL(device_add_software_node);
  * device_remove_software_node - Remove device's software node
  * @dev: The device with the software node.
  *
- * This function will unregister the software node of @dev.
+ * Remove a software node added by device_add_software_node() and
+ * deregister it if this is the last reference.
  */
 void device_remove_software_node(struct device *dev)
 {
Signature · 1 line
2.56.1
author maintainer
#9 lore raw

PATCH v3 5/8 driver core: platform: warn on bad fwnode assignment

In reply to Johan Hovold

Commit e6054f410c97 ("driver core: platform: amend the API contract for fwnode setters") added a warning to platform_device_set_fwnode() in case the current firmware node pointer points to a software node as that may prevent software_node_notify_remove() from being called.

This is only an issue if the platform device has been registered however as software_node_notify() is only called for registered devices. A bigger issue is that a secondary firmware node assigned before the call may be silently lost (and leaked in case of software nodes) as these helpers do not use set_primary_fwnode().

Amend the current software node check, which fails to trigger if the device also has a primary non-software node, with a warning in case the device already has a secondary firmware node assigned to catch any (hard-to-debug) issues due to a secondary node being assigned before the call.

Note that this also avoids making claims about the device firmware node pointing to the primary firmware node, which is not the case when the device only has a secondary node.

Fixes e6054f410c97 ("driver core: platform: amend the API contract for fwnode setters")
Cc Bartosz Golaszewski <[email protected]>
---
 drivers/base/platform.c | 40 ++++++++++++++++++++++++----------------
 1 file changed, 24 insertions(+), 16 deletions(-)
drivers/base/platform.c +24 -16
index e0b38dbd5acf..06ce980b57b1 100644
@@ -708,9 +708,10 @@ EXPORT_SYMBOL_GPL(platform_device_add_data);
  *
  * Assign an OF node to this platform device. Internally keep track of the
  * reference count. Devices created with platform_device_alloc() must use this
- * function instead of assigning the node manually. This function must not be
- * called for a platform device that already has a software node as its primary
- * firmware node assigned.
+ * function instead of assigning the node manually.
+ *
+ * This function must not be called for a platform device that already has a
+ * software node or secondary firmware node assigned.
  */
 void platform_device_set_of_node(struct platform_device *pdev,
 				 struct device_node *np)
@@ -726,21 +727,24 @@ EXPORT_SYMBOL_GPL(platform_device_set_of_node);
  *
  * Assign a firmware node to this platform device. Internally keep track of the
  * reference count. Devices created with platform_device_alloc() must use this
- * function instead of assigning the node manually. This function must not be
- * called for a platform device that already has a software node as its primary
- * firmware node assigned.
+ * function instead of assigning the node manually.
+ *
+ * This function must not be called for a platform device that already has a
+ * software node or secondary firmware node assigned.
  */
 void platform_device_set_fwnode(struct platform_device *pdev,
 				struct fwnode_handle *fwnode)
 {
+	struct fwnode_handle *old = pdev->dev.fwnode;
+
 	/*
-	 * If we call this function for a platform device whose primary
-	 * firmware node is a software node, we'll never end up calling the
-	 * symmetric software_node_notify_remove(). There are no users for this
-	 * right now in the tree so just disallow it.
+	 * A secondary firmware node may be lost as set_primary_fwnode() is
+	 * not used here, and software_node_notify_remove() is not called for
+	 * a registered device with a software node, warn for now.
 	 */
-	WARN_ON(is_software_node(dev_fwnode(&pdev->dev)));
-	fwnode_handle_put(pdev->dev.fwnode);
+	WARN_ON(old && (old->secondary || is_software_node(old)));
+
+	fwnode_handle_put(old);
 	device_set_node(&pdev->dev, fwnode_handle_get(fwnode));
 }
 EXPORT_SYMBOL_GPL(platform_device_set_fwnode);
@@ -751,15 +755,19 @@ EXPORT_SYMBOL_GPL(platform_device_set_fwnode);
  * @dev2: device whose OF node to reuse
  *
  * Reuses the OF node of another device in this platform device while
- * internally keeping track of reference counting. This function must not be
- * called for a platform device that already has a software node as its primary
- * firmware node assigned.
+ * internally keeping track of reference counting.
+ *
+ * This function must not be called for a platform device that already has a
+ * software node or secondary firmware node assigned.
  */
 void platform_device_set_of_node_from_dev(struct platform_device *pdev,
 					  const struct device *dev2)
 {
+	struct fwnode_handle *old = pdev->dev.fwnode;
+
 	/* See platform_device_set_fwnode(). */
-	WARN_ON(is_software_node(dev_fwnode(&pdev->dev)));
+	WARN_ON(old && (old->secondary || is_software_node(old)));
+
 	device_set_of_node_from_dev(&pdev->dev, dev2);
 	pdev->dev.fwnode = of_fwnode_handle(pdev->dev.of_node);
 }
Signature · 1 line
2.56.1
author maintainer
#11 lore raw

PATCH v3 6/8 driver core: platform: tests: fix swnode reuse test description

In reply to Johan Hovold

The description of the software node reuse test is misleading as a software node being registered does not prevent it from being reused (e.g. as software nodes can be shared).

Fix the description by clarifying that the test would fail if the secondary software node is not removed from the device so that the reused primary firmware node still points to the software node.

Fixes 397f0a3958c9 ("driver core: platform: tests: add test cases for correct swnode removal")
Cc Bartosz Golaszewski <[email protected]>
---
 drivers/base/test/platform-device-test.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/base/test/platform-device-test.c +3 -2
index 9ce563f76aad..b0cc37d166ff 100644
@@ -273,9 +273,10 @@ static const struct software_node platform_device_test_swnode = { };
 /*
  * Check that reusing a software node works correctly. If the call to
  * platform_device_register_full() fails after adding the secondary firmware
- * node, the software node must be unregistered in the device's release()
+ * node, the software node must be removed in the device's release()
  * callback or the subsequent call to platform_device_register_full() will fail
- * with -EBUSY due to the software node already having been registered.
+ * with -EBUSY due to the primary firmware node still pointing at the
+ * secondary software node.
  */
 static void platform_device_swnode_add_twice(struct kunit *test)
 {
Signature · 1 line
2.56.1
author maintainer
#12 lore raw

PATCH v3 7/8 software node: fix software_node_notify() primary node comment

In reply to Johan Hovold

The firmware node pointer in struct device may point to either a primary or a secondary firmware node and driver core sets the dev pointer of that node when registering devices.

Fix the comments in software_node_notify() and software_node_notify_remove() which incorrectly suggest that driver core only sets the dev pointer of primary firmware nodes.

Fixes 37e586675993 ("software node: add fw_devlink support")
Cc Bartosz Golaszewski <[email protected]>
---
 drivers/base/swnode.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/base/swnode.c +4 -5
index 95283d8bda26..78e224539b02 100644
@@ -1162,8 +1162,8 @@ void software_node_notify(struct device *dev)
 		return;
 
 	/*
-	 * When the software node is the device's secondary firmware node,
-	 * the core only records the owning device on the primary fwnode
+	 * When a device has both a primary and a secondary firmware node the
+	 * core only records the owning device in the primary node
 	 * (see device_add()). fw_devlink resolves a supplier device through
 	 * fwnode->dev, so without this a consumer referencing the software
 	 * node could never find the supplier device and would defer forever.
@@ -1198,9 +1198,8 @@ void software_node_notify_remove(struct device *dev)
 	sysfs_remove_link(&dev->kobj, "software_node");
 
 	/*
-	 * Drop the device pointer mirrored onto a secondary software node in
-	 * software_node_notify(). For a primary software node the core owns
-	 * fwnode->dev and clears it in device_del().
+	 * Clear any device pointer set in a secondary software node in
+	 * software_node_notify().
 	 */
 	if (!device_match_fwnode(dev, &swnode->fwnode) && swnode->fwnode.dev == dev)
 		swnode->fwnode.dev = NULL;
Signature · 1 line
2.56.1
author maintainer
#14 lore raw

PATCH v3 8/8 device property: fix fwnode_get_next_child_node() comments

In reply to Johan Hovold

The parent pointer in fwnode_get_next_child_node() may not point to a primary firmware node so update the comments to reflect that.

While at it, fix an indefinite article in an earlier comment.

Fixes 190069255582 ("device property: fix infinite loop in fwnode_for_each_child_node()")
Cc Xu Yang <[email protected]>
---
 drivers/base/property.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/base/property.c +2 -3
index 51109794d727..bfc424a193b8 100644
@@ -810,7 +810,7 @@ fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
 
 	/*
 	 * If this function is in a loop and the previous iteration returned
-	 * an child from fwnode->secondary, then we need to use the secondary
+	 * a child from fwnode->secondary, then we need to use the secondary
 	 * as parent rather than @fwnode.
 	 */
 	if (child) {
@@ -822,12 +822,11 @@ fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
 	if (IS_ERR_OR_NULL(parent))
 		return NULL;
 
-	/* Try to find a child in primary fwnode */
 	next = fwnode_call_ptr_op(parent, get_next_child_node, child);
 	if (next)
 		return next;
 
-	/* When no more children in primary, continue with secondary */
+	/* When no more children, continue with any secondary node */
 	return fwnode_get_next_child_node(parent->secondary, NULL);
 }
 EXPORT_SYMBOL_GPL(fwnode_get_next_child_node);
Signature · 1 line
2.56.1
#6 lore raw

Re: [PATCH v3 4/8] software node: ignore primary nodes in device_remove_software_node()

In reply to Johan Hovold
Quoted 7 lines · Johan Hovold

device_remove_software_node() should only be used to remove secondary firmware nodes added by device_add_software_node() but will currently drop a reference also if a device has a primary software node.

Bail out early if device_remove_software_node() is called for a device whose primary firmware node is a software node and fix up the two callers that currently work around this themselves.

...

Quoted 11 lines

+++ b/drivers/i2c/i2c-core-base.c

void i2c_unregister_device(struct i2c_client *client) {

drivers/i2c/i2c-core-base.c +0 -2
 	struct fwnode_handle *fwnode;
-	bool fwnode_is_software_node;
 
 	if (IS_ERR_OR_NULL(client))
 		return;
 
-	fwnode_is_software_node = is_software_node(fwnode);

drivers/i2c/i2c-core-base.c void i2c_unregister_device(struct i2c_client *client) · line 1074

Quoted 10 lines
drivers/i2c/i2c-core-base.c +3 -4
 	device_remove_software_node(&client->dev);
 	/*
-	 * If the firmware node is a software node, its reference count
-	 * was decremented by device_remove_software_node().
+	 * Must be done after device_remove_software_node() which may have
+	 * freed a secondary firmware node and cleared the fwnode pointer.
 	 */
-	if (!fwnode_is_software_node)
-		fwnode_handle_put(fwnode);
+	fwnode_handle_put(client->dev.fwnode);

We have fwnode getter, so better use it

	fwnode_handle_put(dev_fwnode(&client->dev));

Same can be applied to the platform device case above (in this patch).

Quoted 2 lines

device_unregister(&client->dev);
}

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer Acked-by on v3 3/8
#4 lore raw

Re: [PATCH v3 3/8] software node: fix device_add_software_node() kernel doc

In reply to Johan Hovold
Quoted 8 lines · Johan Hovold

Software nodes added to a device by device_add_software_node() are always secondary firmware nodes even if they may be stored in the dev->fwnode pointer.

Fix the kernel doc which incorrectly claims that a software node added using this interface "could become the primary node".

Quoted 6 lines

Also clarify that device_remove_software_node() should only be used to remove software nodes added by device_add_software_node().

Fixes e68d0119e328 ("software node: Introduce device_add_software_node()")
Cc Heikki Krogerus <[email protected]>

LGTM

Quoted 34 lines
---
 drivers/base/swnode.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/base/swnode.c +4 -5
index ce4efc506b9c..b2593b2fcada 100644
@@ -1048,9 +1048,8 @@ EXPORT_SYMBOL_GPL(fwnode_remove_software_node);
  * @dev: The device the software node is meant for.
  * @node: The software node.
  *
- * This function will make @node the secondary firmware node pointer of @dev. If
- * @dev has no primary node, then @node will become the primary node. The
- * function will register @node automatically if it wasn't already registered.
+ * Register @node if it was not already registered and make it the secondary
+ * firmware node of @dev.
  */
 int device_add_software_node(struct device *dev, const struct software_node *node)
 {
@@ -1091,7 +1090,8 @@ EXPORT_SYMBOL_GPL(device_add_software_node);
  * device_remove_software_node - Remove device's software node
  * @dev: The device with the software node.
  *
- * This function will unregister the software node of @dev.
+ * Remove a software node added by device_add_software_node() and
+ * deregister it if this is the last reference.
  */
 void device_remove_software_node(struct device *dev)
 {
--

2.56.1

maintainer Acked-by on v3 5/8
#10 lore raw

Re: [PATCH v3 5/8] driver core: platform: warn on bad fwnode assignment

In reply to Johan Hovold
Quoted 18 lines · Johan Hovold

Commit e6054f410c97 ("driver core: platform: amend the API contract for fwnode setters") added a warning to platform_device_set_fwnode() in case the current firmware node pointer points to a software node as that may prevent software_node_notify_remove() from being called.

This is only an issue if the platform device has been registered however as software_node_notify() is only called for registered devices. A bigger issue is that a secondary firmware node assigned before the call may be silently lost (and leaked in case of software nodes) as these helpers do not use set_primary_fwnode().

Amend the current software node check, which fails to trigger if the device also has a primary non-software node, with a warning in case the device already has a secondary firmware node assigned to catch any (hard-to-debug) issues due to a secondary node being assigned before the call.

Quoted 7 lines

Note that this also avoids making claims about the device firmware node pointing to the primary firmware node, which is not the case when the device only has a secondary node.

Fixes e6054f410c97 ("driver core: platform: amend the API contract for fwnode setters")
Cc Bartosz Golaszewski <[email protected]>

LGTM

Quoted 84 lines
---
 drivers/base/platform.c | 40 ++++++++++++++++++++++++----------------
 1 file changed, 24 insertions(+), 16 deletions(-)
drivers/base/platform.c +24 -17
index e0b38dbd5acf..06ce980b57b1 100644
@@ -708,9 +708,10 @@ EXPORT_SYMBOL_GPL(platform_device_add_data);
  *
  * Assign an OF node to this platform device. Internally keep track of the
  * reference count. Devices created with platform_device_alloc() must use this
- * function instead of assigning the node manually. This function must not be
- * called for a platform device that already has a software node as its primary
- * firmware node assigned.
+ * function instead of assigning the node manually.
+ *
+ * This function must not be called for a platform device that already has a
+ * software node or secondary firmware node assigned.
  */
 void platform_device_set_of_node(struct platform_device *pdev,
                                 struct device_node *np)
@@ -726,21 +727,24 @@ EXPORT_SYMBOL_GPL(platform_device_set_of_node);
  *
  * Assign a firmware node to this platform device. Internally keep track of the
  * reference count. Devices created with platform_device_alloc() must use this
- * function instead of assigning the node manually. This function must not be
- * called for a platform device that already has a software node as its primary
- * firmware node assigned.
+ * function instead of assigning the node manually.
+ *
+ * This function must not be called for a platform device that already has a
+ * software node or secondary firmware node assigned.
  */
 void platform_device_set_fwnode(struct platform_device *pdev,
                                struct fwnode_handle *fwnode)
 {
+       struct fwnode_handle *old = pdev->dev.fwnode;
+
        /*
-        * If we call this function for a platform device whose primary
-        * firmware node is a software node, we'll never end up calling the
-        * symmetric software_node_notify_remove(). There are no users for this
-        * right now in the tree so just disallow it.
+        * A secondary firmware node may be lost as set_primary_fwnode() is
+        * not used here, and software_node_notify_remove() is not called for
+        * a registered device with a software node, warn for now.
         */
-       WARN_ON(is_software_node(dev_fwnode(&pdev->dev)));
-       fwnode_handle_put(pdev->dev.fwnode);
+       WARN_ON(old && (old->secondary || is_software_node(old)));
+
+       fwnode_handle_put(old);
        device_set_node(&pdev->dev, fwnode_handle_get(fwnode));
 }
 EXPORT_SYMBOL_GPL(platform_device_set_fwnode);
@@ -751,15 +755,19 @@ EXPORT_SYMBOL_GPL(platform_device_set_fwnode);
  * @dev2: device whose OF node to reuse
  *
  * Reuses the OF node of another device in this platform device while
- * internally keeping track of reference counting. This function must not be
- * called for a platform device that already has a software node as its primary
- * firmware node assigned.
+ * internally keeping track of reference counting.
+ *
+ * This function must not be called for a platform device that already has a
+ * software node or secondary firmware node assigned.
  */
 void platform_device_set_of_node_from_dev(struct platform_device *pdev,
                                          const struct device *dev2)
 {
+       struct fwnode_handle *old = pdev->dev.fwnode;
+
        /* See platform_device_set_fwnode(). */
-       WARN_ON(is_software_node(dev_fwnode(&pdev->dev)));
+       WARN_ON(old && (old->secondary || is_software_node(old)));
+
        device_set_of_node_from_dev(&pdev->dev, dev2);
        pdev->dev.fwnode = of_fwnode_handle(pdev->dev.of_node);
 }
--

2.56.1

maintainer Acked-by on v3 7/8
#13 lore raw

Re: [PATCH v3 7/8] software node: fix software_node_notify() primary node comment

In reply to Johan Hovold
Quoted 5 lines · Johan Hovold

The firmware node pointer in struct device may point to either a primary or a secondary firmware node and driver core sets the dev pointer of that node when registering devices.

Quoted 7 lines

Fix the comments in software_node_notify() and software_node_notify_remove() which incorrectly suggest that driver core only sets the dev pointer of primary firmware nodes.

Fixes 37e586675993 ("software node: add fw_devlink support")
Cc Bartosz Golaszewski <[email protected]>

LGTM

Quoted 35 lines
---
 drivers/base/swnode.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/base/swnode.c +4 -6
index 95283d8bda26..78e224539b02 100644
@@ -1162,8 +1162,8 @@ void software_node_notify(struct device *dev)
                return;
 
        /*
-        * When the software node is the device's secondary firmware node,
-        * the core only records the owning device on the primary fwnode
+        * When a device has both a primary and a secondary firmware node the
+        * core only records the owning device in the primary node
         * (see device_add()). fw_devlink resolves a supplier device through
         * fwnode->dev, so without this a consumer referencing the software
         * node could never find the supplier device and would defer forever.
@@ -1198,9 +1198,8 @@ void software_node_notify_remove(struct device *dev)
        sysfs_remove_link(&dev->kobj, "software_node");
 
        /*
-        * Drop the device pointer mirrored onto a secondary software node in
-        * software_node_notify(). For a primary software node the core owns
-        * fwnode->dev and clears it in device_del().
+        * Clear any device pointer set in a secondary software node in
+        * software_node_notify().
         */
        if (!device_match_fwnode(dev, &swnode->fwnode) && swnode->fwnode.dev == dev)
                swnode->fwnode.dev = NULL;
--

2.56.1

maintainer Acked-by on v3 8/8
#15 lore raw

Re: [PATCH v3 8/8] device property: fix fwnode_get_next_child_node() comments

In reply to Johan Hovold
Quoted 9 lines · Johan Hovold

The parent pointer in fwnode_get_next_child_node() may not point to a primary firmware node so update the comments to reflect that.

While at it, fix an indefinite article in an earlier comment.

Fixes 190069255582 ("device property: fix infinite loop in fwnode_for_each_child_node()")
Cc Xu Yang <[email protected]>

LGTM

Quoted 35 lines
---
 drivers/base/property.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/base/property.c +2 -4
index 51109794d727..bfc424a193b8 100644
@@ -810,7 +810,7 @@ fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
 
        /*
         * If this function is in a loop and the previous iteration returned
-        * an child from fwnode->secondary, then we need to use the secondary
+        * a child from fwnode->secondary, then we need to use the secondary
         * as parent rather than @fwnode.
         */
        if (child) {
@@ -822,12 +822,11 @@ fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
        if (IS_ERR_OR_NULL(parent))
                return NULL;
 
-       /* Try to find a child in primary fwnode */
        next = fwnode_call_ptr_op(parent, get_next_child_node, child);
        if (next)
                return next;
 
-       /* When no more children in primary, continue with secondary */
+       /* When no more children, continue with any secondary node */
        return fwnode_get_next_child_node(parent->secondary, NULL);
 }
 EXPORT_SYMBOL_GPL(fwnode_get_next_child_node);
--

2.56.1

author maintainer
#7 lore raw

Re: [PATCH v3 4/8] software node: ignore primary nodes in device_remove_software_node()

In reply to Andy Shevchenko
Quoted 35 lines · Andy Shevchenko
Quoted 7 lines · Johan Hovold

device_remove_software_node() should only be used to remove secondary firmware nodes added by device_add_software_node() but will currently drop a reference also if a device has a primary software node.

Bail out early if device_remove_software_node() is called for a device whose primary firmware node is a software node and fix up the two callers that currently work around this themselves.

...

Quoted 11 lines

+++ b/drivers/i2c/i2c-core-base.c

void i2c_unregister_device(struct i2c_client *client) {

drivers/i2c/i2c-core-base.c +0 -2
 	struct fwnode_handle *fwnode;
-	bool fwnode_is_software_node;
 
 	if (IS_ERR_OR_NULL(client))
 		return;
 
-	fwnode_is_software_node = is_software_node(fwnode);

drivers/i2c/i2c-core-base.c void i2c_unregister_device(struct i2c_client *client) · line 1074

Quoted 10 lines
drivers/i2c/i2c-core-base.c +3 -4
 	device_remove_software_node(&client->dev);
 	/*
-	 * If the firmware node is a software node, its reference count
-	 * was decremented by device_remove_software_node().
+	 * Must be done after device_remove_software_node() which may have
+	 * freed a secondary firmware node and cleared the fwnode pointer.
 	 */
-	if (!fwnode_is_software_node)
-		fwnode_handle_put(fwnode);
+	fwnode_handle_put(client->dev.fwnode);

We have fwnode getter, so better use it

Quoted 1 line
	fwnode_handle_put(dev_fwnode(&client->dev));

I used the fwnode pointer directly here on purpose since that is the pointer that gets cleared by device_remove_software_node() and which the comment refers to.

This hopefully also reduces the risk of anyone introducing a UAF by reusing the fwnode variable (or the compiler doing something silly since dev_fwnode() returns the dev->of_node->fwnode when CONFIG_OF is enabled).

Johan

#8 lore raw

Re: [PATCH v3 4/8] software node: ignore primary nodes in device_remove_software_node()

Quoted 2 lines · Johan Hovold
Quoted 1 line · Andy Shevchenko

On Tue, Nov 17, 2026 at 11:13:32AM +0100, Johan Hovold wrote:

...

Quoted 26 lines
Quoted 24 lines
Quoted 11 lines

+++ b/drivers/i2c/i2c-core-base.c

void i2c_unregister_device(struct i2c_client *client) {

drivers/i2c/i2c-core-base.c +0 -2
 	struct fwnode_handle *fwnode;
-	bool fwnode_is_software_node;
 
 	if (IS_ERR_OR_NULL(client))
 		return;
 
-	fwnode_is_software_node = is_software_node(fwnode);

drivers/i2c/i2c-core-base.c void i2c_unregister_device(struct i2c_client *client) · line 1074

Quoted 10 lines
drivers/i2c/i2c-core-base.c +3 -4
 	device_remove_software_node(&client->dev);
 	/*
-	 * If the firmware node is a software node, its reference count
-	 * was decremented by device_remove_software_node().
+	 * Must be done after device_remove_software_node() which may have
+	 * freed a secondary firmware node and cleared the fwnode pointer.
 	 */
-	if (!fwnode_is_software_node)
-		fwnode_handle_put(fwnode);
+	fwnode_handle_put(client->dev.fwnode);

We have fwnode getter, so better use it

Quoted 1 line
	fwnode_handle_put(dev_fwnode(&client->dev));
Quoted 8 lines

I used the fwnode pointer directly here on purpose since that is the pointer that gets cleared by device_remove_software_node() and which the comment refers to.

This hopefully also reduces the risk of anyone introducing a UAF by reusing the fwnode variable (or the compiler doing something silly since dev_fwnode() returns the dev->of_node->fwnode when CONFIG_OF is enabled).

This makes explicit use of the member outside of driver core. It adds an unneeded burden on the possible changes in the future if we decide to make the fwnode stuff being hidden in struct device (in a sense of having say list head instead of that). I think we really need to mark fwnode with __private there.

Signature · 2 lines
With Best Regards,
Andy Shevchenko