Skip to contentSkip to conversation

driver core: unify the release path for dynamically allocated platform devices

PATCH Applied Bartosz Golaszewski 5 participants 13 messages Jul 16 – Sep 22

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

v8 cover driver core: unify the release path for dynamically allocated platform devices

This is a follow-up to the fix in platform device code that introduced platform_device_release_full(). With the two prerequisite patch series applied and with the two last remaining offenders converted, we can finally unify the release path for platform devices using platform_device_register_full() as well as those using the split approach of platform_device_alloc() + platform_device_add().

With an ack from DRM and platform maintainers, this can go through the driver core tree.

---
Changes in v8:
- Move the special reference handling of primary software nodes to platform_device_release()
- Make the API contract for platform device fwnode setters stricter and
don't allow to call them for devices whose primary firmware node is a
software node
- Link to v7: https://patch.msgid.link/20260713-swnode-remove-on-dev-unreg-v7-0-6d6bc448427a@oss.qualcomm.com

Changes in v7:
- Drop patch 1/6 as it's already upstream
- Drop patch 5/6 as its been replaced by another changeset sent upstream
- Lock device when calling device_is_bound() in kunit tests
- Fix invalid reference put of the firmware node in surface_gpe
- Rebase on top of recent driver core changes
- Link to v6: https://patch.msgid.link/20260513-swnode-remove-on-dev-unreg-v6-0-f9c58939df27@oss.qualcomm.com

Changes in v6:
- Make patch 1 a minimal fix that can go into v7.1
- Don't provide platform_device_add_software_node(), instead: rework
drivers in question to use other existing interfaces instead
- Fix life-time issues in the xe_i2c patch and remove unneeded fwnode
removal
- Fix typos and tweak commit messages
- Link to v5: https://patch.msgid.link/20260512-swnode-remove-on-dev-unreg-v5-0-0035eff63812@oss.qualcomm.com

Changes in v5:
- Provide a helper for users of platform_device_alloc() that wish to
assign a software node to a platform device
- Rework drivers that could regress after the swnode leak fix
- Split the series into several patches as necessary
- Link to v4: https://patch.msgid.link/20260430-swnode-remove-on-dev-unreg-v4-0-01574da0aed3@oss.qualcomm.com

Changes in v4:
- Drop redundant !pdevinfo->swnode check in
platform_device_register_full()
- Handle all three combinations with which two software nodes can be
assigned to a platform device
- Extend the new kunit test to verify all three combinations
- Link to v3: https://patch.msgid.link/20260428-swnode-remove-on-dev-unreg-v3-0-533bdc71b22e@oss.qualcomm.com

Changes in v3:
- Make sure the reference of the primary software node we possibly take,
is always released by moving the get() before calls that may fail
- Don't allow passing two software nodes
- Add a test case for that situation
- Link to v2: https://patch.msgid.link/20260423-swnode-remove-on-dev-unreg-v2-0-0e5213cde2ed@oss.qualcomm.com

Changes in v2:
- Change the order between removing the software node and dropping the
reference to the device's OF node
- Address a situation where a software node is used as the primary
firmware node
- Add a patch adding a new kunit helper
- Add another test case
- Link to v1: https://patch.msgid.link/20260410-swnode-remove-on-dev-unreg-v1-0-cd7d305f3db2@oss.qualcomm.com

---
Bartosz Golaszewski (5):
driver core: platform: amend the API contract for fwnode setters platform/surface: gpe: use platform_device_register_full() drm/xe/i2c: use device_create_managed_software_node() driver core: platform: unify release path
driver core: platform: tests: add test cases for correct swnode removal

 drivers/base/platform.c                  |  37 ++++---
 drivers/base/test/platform-device-test.c | 180 +++++++++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_i2c.c              |  22 ++--
 drivers/gpu/drm/xe/xe_i2c.h              |   1 -
 drivers/platform/surface/surface_gpe.c   |  39 ++-----
 5 files changed, 224 insertions(+), 55 deletions(-)

---
base-commit: 0de8455a9de5713f4022b20d655f18edcd3cf468
change-id: 20260410-swnode-remove-on-dev-unreg-42bfc4b23ba8
prerequisite-message-id: [email protected]
prerequisite-patch-id: 76dea1205a0905915a03c0d7c9e94399c92501e5
prerequisite-patch-id: f6c074c48e5eb2f5e92c57fa1320b6b0fab3ce1d
prerequisite-patch-id: 07a0c422cc3233e2fe87cb9bbd6baab608d0de82
prerequisite-patch-id: 5e980cccbfdc82adef1573f8a8a9639689836432
prerequisite-patch-id: bb193e0baf41714061b83b3925cc4e28036f2b9c
prerequisite-patch-id: 5ff0ac2c7c4cc548692ed5f567db30e27e920786
prerequisite-patch-id: deb425beb85441169aad53c1282f35df513b8015
prerequisite-patch-id: b0e9e719c8daf30d97c110478a5c3c7af106472c
prerequisite-patch-id: 9f118a7cdb92893ed4e913c7c98bbdb766ff18c4
prerequisite-patch-id: 1b2391d8295e80086182f55e5189b9aa588687f8
prerequisite-patch-id: 986a0f4f9a682a255f6eef0fe2128a86c595ece5
prerequisite-patch-id: aae63ceb82cbe6f65810751e596e542df0ff513b
prerequisite-patch-id: 155601719d4f5c433fd7dce63842007f255bec81
prerequisite-patch-id: f9bebca5cd8f5ebf9b52e9c688a3c83ddc7efb7f
prerequisite-patch-id: aff2299bcf258e4e6dd1f01c591141385f31fdff
prerequisite-patch-id: 51cfbca3ab011688be133a259d598007289237c4
prerequisite-patch-id: 0e9bddaa83a1f5a99745c2f50f7c3213d7b17fbd
prerequisite-patch-id: 7ca12b7bbf0168eadaece00587dad68e1ff3cbec
prerequisite-patch-id: 8d483c4b391ed1b95897c951555e7ab6c5807e2a
prerequisite-patch-id: 495173352a585bdfd0c58721c8be0558d06a5335
prerequisite-message-id: [email protected]
prerequisite-patch-id: d7f936bb1eb8e9fdc0a6a35770e376d36a0b595e
prerequisite-patch-id: 77b6a4eb67cd6bcb6fbc974a4d0368be6a2810ef
prerequisite-patch-id: 744bdc8ea502dab2db13b1cbe963c1db49c951f0
prerequisite-patch-id: 185deea7fe874a7d4a70c93e0b60cc3bbc75ec25
prerequisite-patch-id: d9e41fb8f4070e53ac293db82ccbad83c8d2a40b

Best regards,

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

PATCH v8 1/5 driver core: platform: amend the API contract for fwnode setters

Calling platform_device_set_fwnode() (and by extension:
platform_device_set_of_node()) of platform_device_set_of_node_from_dev() for a dynamically allocated platform device whose primary firmware node is already assigned and is a software node leads to a resource leak as we never perform the corresponding call to software_node_notify_remove(). As there are currently no users for this special case - just disallow it and warn loudly if it ever happens.

---
 drivers/base/platform.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)
drivers/base/platform.c +18 -3
index a71015f1d915340aa01ebfae67196d4d7ccecba8..fb57d487a6c2aba799b4ca5ccd089006cf2d8bf6 100644
@@ -707,7 +707,9 @@ 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.
+ * 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.
  */
 void platform_device_set_of_node(struct platform_device *pdev,
 				 struct device_node *np)
@@ -723,11 +725,20 @@ 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.
+ * 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.
  */
 void platform_device_set_fwnode(struct platform_device *pdev,
 				struct fwnode_handle *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.
+	 */
+	WARN_ON(is_software_node(dev_fwnode(&pdev->dev)));
 	fwnode_handle_put(pdev->dev.fwnode);
 	device_set_node(&pdev->dev, fwnode_handle_get(fwnode));
 }
@@ -739,11 +750,15 @@ 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.
+ * 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.
  */
 void platform_device_set_of_node_from_dev(struct platform_device *pdev,
 					  const struct device *dev2)
 {
+	/* See platform_device_set_fwnode(). */
+	WARN_ON(is_software_node(dev_fwnode(&pdev->dev)));
 	device_set_of_node_from_dev(&pdev->dev, dev2);
 	pdev->dev.fwnode = of_fwnode_handle(pdev->dev.of_node);
 }
Signature · 1 line
2.47.3
#2 lore raw

Re: [PATCH v8 1/5] driver core: platform: amend the API contract for fwnode setters

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The patch fails to implement its intended behavior because it uses `WARN_ON()` as a standalone…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The patch fails to implement its intended behavior because it uses WARN_ON() as a standalone statement without an early return. WARN_ON() evaluates the condition and emits a warning traceback but does not halt or return from execution. As a result, the operation is not disallowed, and execution falls through, continuing to overwrite the firmware node and leaving the resource leak unaddressed. --

--- Patch [1]: [PATCH v8 1/5] driver core: platform: amend the API contract for fwnode setters --- commit 0ad30205c2e10306a33e3a3823c155c95eca3ce4
Author: Bartosz Golaszewski <[email protected]>

driver core: platform: amend the API contract for fwnode setters

Calling platform_device_set_fwnode() for a dynamically allocated platform device whose primary firmware node is already a software node leads to a resource leak. This commit intends to disallow this scenario and warn loudly if it ever occurs.

Quoted 4 lines
drivers/base/platform.c +0 -0
index a71015f1d9153..fb57d487a6c2a 100644

[ ... ]

Quoted 9 lines
drivers/base/platform.c +3 -1
@@ -723,11 +725,20 @@ 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.
+ * 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.
  */

drivers/base/platform.c EXPORT_SYMBOL_GPL(platform_device_set_of_node); · line 741

Quoted 10 lines
drivers/base/platform.c +7 -0
 void platform_device_set_fwnode(struct platform_device *pdev,
 				struct fwnode_handle *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.
+	 */
+	WARN_ON(is_software_node(dev_fwnode(&pdev->dev)));

[Severity: Medium]
Does this code actually disallow the operation?

Since WARN_ON() evaluates the condition and emits a warning but does not return from the function, execution will fall through and still overwrite the node, leaving the resource leak unaddressed.

Would it be better to use an early return here, such as:
if (WARN_ON(is_software_node(dev_fwnode(&pdev->dev))))

	return;
Quoted 3 lines
drivers/base/platform.c +0 -0
 	fwnode_handle_put(pdev->dev.fwnode);
 	device_set_node(&pdev->dev, fwnode_handle_get(fwnode));
 }

[ ... ]

Quoted 4 lines
drivers/base/platform.c +0 -0
@@ -739,11 +750,15 @@ 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

drivers/base/platform.c EXPORT_SYMBOL_GPL(platform_device_set_fwnode); · line 761

Quoted 10 lines
drivers/base/platform.c +5 -1
- * internally keeping track of reference counting.
+ * 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.
  */
 void platform_device_set_of_node_from_dev(struct platform_device *pdev,
 					  const struct device *dev2)
 {
+	/* See platform_device_set_fwnode(). */
+	WARN_ON(is_software_node(dev_fwnode(&pdev->dev)));

[Severity: Medium]
Similar to platform_device_set_fwnode(), does this prevent the overwrite?

A standalone WARN_ON() allows execution to continue and overwrite pdev->dev.fwnode.

Quoted 3 lines
drivers/base/platform.c +0 -0
 	device_set_of_node_from_dev(&pdev->dev, dev2);
 	pdev->dev.fwnode = of_fwnode_handle(pdev->dev.of_node);
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/20260716-swnode-remove-on-dev-unreg-v8-0-5c2b8cc38c28@oss.qualcomm.com?part=1
maintainer
#3 lore raw

Re: [PATCH v8 1/5] driver core: platform: amend the API contract for fwnode setters

In reply to Bartosz Golaszewski
Quoted 12 lines · Bartosz Golaszewski
drivers/base/platform.c +3 -1
@@ -723,11 +725,20 @@ 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.
+ * 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.
  */
 void platform_device_set_fwnode(struct platform_device *pdev,
 				struct fwnode_handle *fwnode)
 {

drivers/base/platform.c EXPORT_SYMBOL_GPL(platform_device_set_of_node); · line 744

Quoted 10 lines
drivers/base/platform.c +7 -0
+	/*
+	 * 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.
+	 */
+	WARN_ON(is_software_node(dev_fwnode(&pdev->dev)));
 	fwnode_handle_put(pdev->dev.fwnode);
 	device_set_node(&pdev->dev, fwnode_handle_get(fwnode));
 }

When I suggested this I actually meant to never allow to replace an existing node (not only for swnodes), as I think there's no use-case for this anyway and it makes the API more consistent.

However, this can be done in a follow-up too; the patch is fine as is, as it addresses the problem I pointed out about software_node_notify_remove() not being called independently.

So, I'll pick this up later today.

Thanks,
Danilo

#4 lore raw

PATCH v8 2/5 platform/surface: gpe: use platform_device_register_full()

In reply to Bartosz Golaszewski

Creating a software node for a given set of properties and adding it to a platform device can be achieved with a single call to platform_device_register_full(). There's nothing in this driver that suggests using the more fine-grained interfaces was intentional so switch to using the high-level helper.

---
 drivers/platform/surface/surface_gpe.c | 39 ++++++++++------------------------
 1 file changed, 11 insertions(+), 28 deletions(-)
drivers/platform/surface/surface_gpe.c +11 -28
index 40896a8544b0a4da4261ea881b1eaed62d93b32b..4ca7ad76f3b9d2b6c455a5d5b37f23a85eb69c69 100644
@@ -290,9 +290,9 @@ static struct platform_device *surface_gpe_device;
 
 static int __init surface_gpe_init(void)
 {
+	struct platform_device_info pdevinfo;
 	const struct dmi_system_id *match;
 	struct platform_device *pdev;
-	struct fwnode_handle *fwnode;
 	int status;
 
 	match = dmi_first_match(dmi_lid_device_table);
@@ -305,44 +305,27 @@ static int __init surface_gpe_init(void)
 	if (status)
 		return status;
 
-	fwnode = fwnode_create_software_node(match->driver_data, NULL);
-	if (IS_ERR(fwnode)) {
-		status = PTR_ERR(fwnode);
-		goto err_node;
-	}
-
-	pdev = platform_device_alloc("surface_gpe", PLATFORM_DEVID_NONE);
-	if (!pdev) {
-		status = -ENOMEM;
-		goto err_alloc;
+	pdevinfo = (struct platform_device_info){
+		.name = "surface_gpe",
+		.id = PLATFORM_DEVID_NONE,
+		.properties = match->driver_data,
+	};
+
+	pdev = platform_device_register_full(&pdevinfo);
+	if (IS_ERR(pdev)) {
+		platform_driver_unregister(&surface_gpe_driver);
+		return PTR_ERR(pdev);
 	}
 
-	platform_device_set_fwnode(pdev, fwnode);
-
-	status = platform_device_add(pdev);
-	if (status)
-		goto err_add;
-
 	surface_gpe_device = pdev;
 	return 0;
-
-err_add:
-	platform_device_put(pdev);
-err_alloc:
-	fwnode_remove_software_node(fwnode);
-err_node:
-	platform_driver_unregister(&surface_gpe_driver);
-	return status;
 }
 module_init(surface_gpe_init);
 
 static void __exit surface_gpe_exit(void)
 {
-	struct fwnode_handle *fwnode = surface_gpe_device->dev.fwnode;
-
 	platform_device_unregister(surface_gpe_device);
 	platform_driver_unregister(&surface_gpe_driver);
-	fwnode_remove_software_node(fwnode);
 }
 module_exit(surface_gpe_exit);
 
Signature · 1 line
2.47.3
#5 lore raw

PATCH v8 3/5 drm/xe/i2c: use device_create_managed_software_node()

In reply to Bartosz Golaszewski

This driver intentionally uses the fine-grained approach to creating platform devices. It assigns a software node as the primary firmware node of the device it creates. Ahead of improving the reference counting of platform device software nodes, switch to using device_create_managed_software_node(). This way, we create a dynamic software node whose life-time is tied to the device to which it's assigned.

---
 drivers/gpu/drm/xe/xe_i2c.c | 22 ++++++++--------------
 drivers/gpu/drm/xe/xe_i2c.h |  1 -
 2 files changed, 8 insertions(+), 15 deletions(-)
drivers/gpu/drm/xe/xe_i2c.c +8 -14
index 3d80eafb9a1318996d10db11e14af4cfde07ba55..af6e973e5e68125a9c006bb646447ca6e377a6e6 100644
@@ -93,13 +93,8 @@ static int xe_i2c_register_adapter(struct xe_i2c *i2c)
 {
 	struct pci_dev *pci = to_pci_dev(i2c->drm_dev);
 	struct platform_device *pdev;
-	struct fwnode_handle *fwnode;
 	int ret;
 
-	fwnode = fwnode_create_software_node(xe_i2c_adapter_properties, NULL);
-	if (IS_ERR(fwnode))
-		return PTR_ERR(fwnode);
-
 	/*
 	 * Not using platform_device_register_full() here because we don't have
 	 * a handle to the platform_device before it returns. xe_i2c_notifier()
@@ -107,10 +102,14 @@ static int xe_i2c_register_adapter(struct xe_i2c *i2c)
 	 * platform_device_register_full() is done.
 	 */
 	pdev = platform_device_alloc(adapter_name, pci_dev_id(pci));
-	if (!pdev) {
-		ret = -ENOMEM;
-		goto err_fwnode_remove;
-	}
+	if (!pdev)
+		return -ENOMEM;
+
+	ret = device_create_managed_software_node(&pdev->dev,
+						  xe_i2c_adapter_properties,
+						  NULL);
+	if (ret)
+		goto err_pdev_put;
 
 	if (i2c->adapter_irq) {
 		struct resource res;
@@ -123,8 +122,6 @@ static int xe_i2c_register_adapter(struct xe_i2c *i2c)
 	}
 
 	pdev->dev.parent = i2c->drm_dev;
-	platform_device_set_fwnode(pdev, fwnode);
-	i2c->adapter_node = fwnode;
 	i2c->pdev = pdev;
 
 	ret = platform_device_add(pdev);
@@ -135,8 +132,6 @@ static int xe_i2c_register_adapter(struct xe_i2c *i2c)
 
 err_pdev_put:
 	platform_device_put(pdev);
-err_fwnode_remove:
-	fwnode_remove_software_node(fwnode);
 
 	return ret;
 }
@@ -144,7 +139,6 @@ static int xe_i2c_register_adapter(struct xe_i2c *i2c)
 static void xe_i2c_unregister_adapter(struct xe_i2c *i2c)
 {
 	platform_device_unregister(i2c->pdev);
-	fwnode_remove_software_node(i2c->adapter_node);
 }
 
 /**
drivers/gpu/drm/xe/xe_i2c.h +0 -1
index 425d8160835f4648891ff75f2a9c06284241710e..b28229f056c5a664e1d41dc0cc1978782fe1c2f1 100644
@@ -30,7 +30,6 @@ struct xe_i2c_endpoint {
 };
 
 struct xe_i2c {
-	struct fwnode_handle *adapter_node;
 	struct platform_device *pdev;
 	struct i2c_adapter *adapter;
 	struct i2c_client *client[XE_I2C_MAX_CLIENTS];
Signature · 1 line
2.47.3
#6 lore raw

Re: [PATCH v8 3/5] drm/xe/i2c: use device_create_managed_software_node()

Quoted 11 lines · Bartosz Golaszewski

This driver intentionally uses the fine-grained approach to creating platform devices. It assigns a software node as the primary firmware node of the device it creates. Ahead of improving the reference counting of platform device software nodes, switch to using device_create_managed_software_node(). This way, we create a dynamic software node whose life-time is tied to the device to which it's assigned.

What happened to this one? I can see the commit 0f485f88656a ("drm/xe/i2c: use device_create_managed_software_node()"), but not the code change?

There is no revert and there are no other commits touching this part of the driver, so it's as if the patch was never actually applied?

Thanks,

Quoted 83 lines
---
 drivers/gpu/drm/xe/xe_i2c.c | 22 ++++++++--------------
 drivers/gpu/drm/xe/xe_i2c.h |  1 -
 2 files changed, 8 insertions(+), 15 deletions(-)
drivers/gpu/drm/xe/xe_i2c.c +8 -14
index 3d80eafb9a1318996d10db11e14af4cfde07ba55..af6e973e5e68125a9c006bb646447ca6e377a6e6 100644
@@ -93,13 +93,8 @@ static int xe_i2c_register_adapter(struct xe_i2c *i2c)
 {
 	struct pci_dev *pci = to_pci_dev(i2c->drm_dev);
 	struct platform_device *pdev;
-	struct fwnode_handle *fwnode;
 	int ret;
 
-	fwnode = fwnode_create_software_node(xe_i2c_adapter_properties, NULL);
-	if (IS_ERR(fwnode))
-		return PTR_ERR(fwnode);
-
 	/*
 	 * Not using platform_device_register_full() here because we don't have
 	 * a handle to the platform_device before it returns. xe_i2c_notifier()
@@ -107,10 +102,14 @@ static int xe_i2c_register_adapter(struct xe_i2c *i2c)
 	 * platform_device_register_full() is done.
 	 */
 	pdev = platform_device_alloc(adapter_name, pci_dev_id(pci));
-	if (!pdev) {
-		ret = -ENOMEM;
-		goto err_fwnode_remove;
-	}
+	if (!pdev)
+		return -ENOMEM;
+
+	ret = device_create_managed_software_node(&pdev->dev,
+						  xe_i2c_adapter_properties,
+						  NULL);
+	if (ret)
+		goto err_pdev_put;
 
 	if (i2c->adapter_irq) {
 		struct resource res;
@@ -123,8 +122,6 @@ static int xe_i2c_register_adapter(struct xe_i2c *i2c)
 	}
 
 	pdev->dev.parent = i2c->drm_dev;
-	platform_device_set_fwnode(pdev, fwnode);
-	i2c->adapter_node = fwnode;
 	i2c->pdev = pdev;
 
 	ret = platform_device_add(pdev);
@@ -135,8 +132,6 @@ static int xe_i2c_register_adapter(struct xe_i2c *i2c)
 
 err_pdev_put:
 	platform_device_put(pdev);
-err_fwnode_remove:
-	fwnode_remove_software_node(fwnode);
 
 	return ret;
 }
@@ -144,7 +139,6 @@ static int xe_i2c_register_adapter(struct xe_i2c *i2c)
 static void xe_i2c_unregister_adapter(struct xe_i2c *i2c)
 {
 	platform_device_unregister(i2c->pdev);
-	fwnode_remove_software_node(i2c->adapter_node);
 }
 
 /**
drivers/gpu/drm/xe/xe_i2c.h +0 -1
index 425d8160835f4648891ff75f2a9c06284241710e..b28229f056c5a664e1d41dc0cc1978782fe1c2f1 100644
@@ -30,7 +30,6 @@ struct xe_i2c_endpoint {
 };
 
 struct xe_i2c {
-	struct fwnode_handle *adapter_node;
 	struct platform_device *pdev;
 	struct i2c_adapter *adapter;
 	struct i2c_client *client[XE_I2C_MAX_CLIENTS];
Signature · 2 lines
-- 
2.47.3
Signature · 1 line
heikki
#7 lore raw

Re: [PATCH v8 3/5] drm/xe/i2c: use device_create_managed_software_node()

Quoted 13 lines · Heikki Krogerus
Quoted 11 lines · Bartosz Golaszewski

This driver intentionally uses the fine-grained approach to creating platform devices. It assigns a software node as the primary firmware node of the device it creates. Ahead of improving the reference counting of platform device software nodes, switch to using device_create_managed_software_node(). This way, we create a dynamic software node whose life-time is tied to the device to which it's assigned.

Quoted 7 lines

What happened to this one? I can see the commit 0f485f88656a ("drm/xe/i2c: use device_create_managed_software_node()"), but not the code change?

There is no revert and there are no other commits touching this part of the driver, so it's as if the patch was never actually applied?

I had no idea what happened here so I had the robot look into it and it came back with this:

--
Your commit 0f485f88656a ("drm/xe/i2c: use device_create_managed_software_node()") landed upstream on 2026-07-16, but it went in through the driver-core tree (it's signed off by Danilo Krummrich, a driver-core maintainer), not through drm-xe. It reached mainline via the "Merge tag 'driver-core-7.3-rc1'" pull, and was present in v7.3-rc1.

Meanwhile, Intel's drm-xe-next tree had last synced with mainline back on 2026-08-06 (merge-base ec3e9419371d, before your commit existed). Off that stale base, Heikki Krogerus landed a large I2C IRQ-handling rewrite on 2026-08-11 (a55b76b8bc2c "Fix the interrupt handling", 76cc14e2faed "Keep the i2c controller always enabled") that touched xe_i2c_register_adapter()/xe_i2c_unregister_adapter() — the same function your patch changed — but built on the pre-conversion fwnode_create_software_node() code, since drm-xe-next didn't have your commit yet.

The regression happened here:

cc467969646b — "Merge drm/drm-next into drm-xe-next" by Matthew Brost (Intel), 2026-08-31, merging:
- parent 1: drm-xe-next's tip (Heikki's rewrite, on the old fwnode code)
- parent 2: v7.3-rc1 (mainline, with your new device_create_managed_software_node() code)

I diffed the merge result against both parents directly: for the xe_i2c_register_adapter/unregister_adapter hunk, the merge commit is byte-for-byte identical to parent 1 — it silently dropped your API-migration change and kept the old fwnode code (plus Heikki's IRQ rework on top of it). No Conflicts: trailer is in the merge message, so this wasn't a manual conflict resolution that anyone flagged — either it merged cleanly in a way that just discarded your side, or it was resolved without git noting it in the message.

That merge (drm-xe-next-2026-08-31-1) then flowed drm-xe-next → drm-next → mainline, and it's still that way in current mainline (verified: HEAD, v7.3-rc4-based, still has the old code — confirmed via git blame).

Bottom line: your change wasn't reverted on purpose — it was silently clobbered by a stale-base merge conflict resolution when drm-xe-next resynced with mainline on 2026-08-31, because drm-xe-next hadn't picked up your driver-core-authored commit before building new code on top of the same function. This is a real upstream bug worth flagging (to Matthew Brost / Heikki Krogerus / Danilo Krummrich): the driver-core migration for xe_i2c.c needs to be reapplied on current mainline.
--

I think we need to reapply this patch.

Bart

#8 lore raw

PATCH v8 4/5 driver core: platform: unify release path

In reply to Bartosz Golaszewski

With no drivers that manually assign software nodes to platform devices created with platform_device_alloc(), we can now unify the release path and remove platform_device_release_full().

---
 drivers/base/platform.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
drivers/base/platform.c +7 -9
index fb57d487a6c2aba799b4ca5ccd089006cf2d8bf6..66f9ec73d47e90cecac9217ff6e0f051fea5b057 100644
@@ -599,19 +599,19 @@ static void platform_device_release(struct device *dev)
 	struct platform_object *pa = container_of(dev, struct platform_object,
 						  pdev.dev);
 
-	fwnode_handle_put(pa->pdev.dev.fwnode);
+	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 (!is_software_node(dev_fwnode(dev)))
+		fwnode_handle_put(pa->pdev.dev.fwnode);
 	kfree(pa->pdev.dev.platform_data);
 	kfree(pa->pdev.mfd_cell);
 	kfree(pa->pdev.resource);
 	kfree(pa);
 }
 
-static void platform_device_release_full(struct device *dev)
-{
-	device_remove_software_node(dev);
-	platform_device_release(dev);
-}
-
 /**
  * platform_device_alloc - create a platform device
  * @name: base name of the device we're adding
@@ -960,8 +960,6 @@ struct platform_device *platform_device_register_full(const struct platform_devi
 		ret = device_add_software_node(&pdev->dev, pdevinfo->swnode);
 		if (ret)
 			goto err;
-
-		pdev->dev.release = platform_device_release_full;
 	} else if (pdevinfo->properties) {
 		ret = device_create_managed_software_node(&pdev->dev,
 							  pdevinfo->properties, NULL);
Signature · 1 line
2.47.3
#9 lore raw

Re: [PATCH v8 4/5] driver core: platform: unify release path

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Dereferencing a freed primary software node during platform device release, leading to a…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Dereferencing a freed primary software node during platform device release, leading to a Use-After-Free and possible double-put. --

--- Patch [4]: [PATCH v8 4/5] driver core: platform: unify release path --- commit a4c9de720c7986b23ccb07ee951d4a99f15666ec
Author: Bartosz Golaszewski <[email protected]>

driver core: platform: unify release path

With no drivers that manually assign software nodes to platform devices created with platform_device_alloc(), we can now unify the release path and remove platform_device_release_full().

Quoted 4 lines
drivers/base/platform.c +0 -0
index fb57d487a6c2a..66f9ec73d47e9 100644

[ ... ]

drivers/base/platform.c static void platform_device_release(struct device *dev) · line 607

Quoted 11 lines
drivers/base/platform.c +6 -1
@@ -599,19 +599,19 @@ static void platform_device_release(struct device *dev)
 	struct platform_object *pa = container_of(dev, struct platform_object,
 						  pdev.dev);
 
-	fwnode_handle_put(pa->pdev.dev.fwnode);
+	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 (!is_software_node(dev_fwnode(dev)))

[Severity: High]
Does this check result in a use-after-free of the primary software node?

If a platform device is registered with a primary software node (for example via platform_device_register_full()), dev->fwnode points to it.

During device release, device_remove_software_node() retrieves the primary software node and drops the reference, potentially freeing the node:

device_remove_software_node()
swnode_put(swnode);

Because dev->fwnode is primary, set_secondary_fwnode(dev, NULL) inside device_remove_software_node() only clears the secondary pointer, leaving dev->fwnode still pointing to the freed primary node.

The subsequent call to is_software_node(dev_fwnode(dev)) would then dereference this dangling pointer.

Quoted 4 lines
drivers/base/platform.c +1 -0
+		fwnode_handle_put(pa->pdev.dev.fwnode);
 	kfree(pa->pdev.dev.platform_data);
 	kfree(pa->pdev.mfd_cell);
 	kfree(pa->pdev.resource);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/20260716-swnode-remove-on-dev-unreg-v8-0-5c2b8cc38c28@oss.qualcomm.com?part=4
#10 lore raw

PATCH v8 5/5 driver core: platform: tests: add test cases for correct swnode removal

In reply to Bartosz Golaszewski

Extend the kunit module for platform devices with test cases verifying that the same software node can be added to platform devices repeatedly.

---
 drivers/base/test/platform-device-test.c | 180 +++++++++++++++++++++++++++++++
 1 file changed, 180 insertions(+)
drivers/base/test/platform-device-test.c +180 -0
index 6355a2231b741791b54eb78af42e13f31f745184..9ce563f76aad6943472dc78f3639ea1a2d72dbde 100644
@@ -1,12 +1,15 @@
 // SPDX-License-Identifier: GPL-2.0
 
+#include <kunit/fwnode.h>
 #include <kunit/platform_device.h>
 #include <kunit/resource.h>
 
 #include <linux/device.h>
 #include <linux/device/bus.h>
+#include <linux/fwnode.h>
 #include <linux/of_platform.h>
 #include <linux/platform_device.h>
+#include <linux/property.h>
 
 #define DEVICE_NAME "test"
 
@@ -253,9 +256,186 @@ static struct kunit_suite platform_device_match_test_suite = {
 	.test_cases = platform_device_match_tests,
 };
 
+static int platform_device_swnode_test_probe(struct platform_device *pdev)
+{
+	return 0;
+}
+
+static struct platform_driver platform_swnode_test_driver = {
+	.probe = platform_device_swnode_test_probe,
+	.driver = {
+		.name = DEVICE_NAME,
+	},
+};
+
+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()
+ * callback or the subsequent call to platform_device_register_full() will fail
+ * with -EBUSY due to the software node already having been registered.
+ */
+static void platform_device_swnode_add_twice(struct kunit *test)
+{
+	struct platform_device_info pdevinfo;
+	struct platform_device *pdev;
+	struct fwnode_handle *fwnode;
+	bool bound = false;
+	int ret;
+
+	fwnode = kunit_kzalloc(test, sizeof(*fwnode), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fwnode);
+
+	ret = kunit_platform_driver_register(test, &platform_swnode_test_driver);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
+	fwnode_init(fwnode, NULL);
+	pdevinfo = (struct platform_device_info){
+		.name = DEVICE_NAME,
+		.id = PLATFORM_DEVID_NONE,
+		.fwnode = fwnode,
+		.swnode = &platform_device_test_swnode,
+	};
+
+	pdev = platform_device_register_full(&pdevinfo);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
+
+	wait_for_device_probe();
+	scoped_guard(device, &pdev->dev)
+		bound = device_is_bound(&pdev->dev);
+
+	KUNIT_ASSERT_TRUE(test, bound);
+
+	platform_device_unregister(pdev);
+
+	pdev = platform_device_register_full(&pdevinfo);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
+
+	wait_for_device_probe();
+	scoped_guard(device, &pdev->dev)
+		bound = device_is_bound(&pdev->dev);
+
+	KUNIT_ASSERT_TRUE(test, bound);
+
+	platform_device_unregister(pdev);
+}
+
+/*
+ * Check that passing a software node as the primary firmware node of the
+ * platform device does not result in it being unregistered by the call to
+ * device_remove_software_node() in its release path.
+ */
+static void platform_device_swnode_as_primary(struct kunit *test)
+{
+	struct platform_device_info pdevinfo;
+	struct platform_device *pdev;
+	struct fwnode_handle *fwnode;
+	bool bound = false;
+	int ret;
+
+	ret = kunit_platform_driver_register(test, &platform_swnode_test_driver);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
+	fwnode = kunit_software_node_register(test, &platform_device_test_swnode);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fwnode);
+
+	pdevinfo = (struct platform_device_info){
+		.name = DEVICE_NAME,
+		.id = PLATFORM_DEVID_NONE,
+		.fwnode = fwnode,
+	};
+
+	pdev = platform_device_register_full(&pdevinfo);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
+
+	wait_for_device_probe();
+	scoped_guard(device, &pdev->dev)
+		bound = device_is_bound(&pdev->dev);
+
+	KUNIT_ASSERT_TRUE(test, bound);
+
+	platform_device_unregister(pdev);
+
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, software_node_fwnode(&platform_device_test_swnode));
+}
+
+/*
+ * Check that passing two software nodes to platform_device_register_full()
+ * fails.
+ */
+static void platform_device_two_swnodes(struct kunit *test)
+{
+	static const struct property_entry properties[] = {
+		PROPERTY_ENTRY_U32("foo", 42),
+		{ }
+	};
+
+	struct platform_device_info pdevinfo;
+	struct platform_device *pdev;
+	struct fwnode_handle *fwnode;
+	int ret;
+
+	ret = kunit_platform_driver_register(test, &platform_swnode_test_driver);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
+	fwnode = kunit_software_node_register(test, &platform_device_test_swnode);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fwnode);
+
+	pdevinfo = (struct platform_device_info){
+		.name = DEVICE_NAME,
+		.id = PLATFORM_DEVID_NONE,
+		.fwnode = fwnode,
+		.swnode = &platform_device_test_swnode,
+	};
+
+	pdev = platform_device_register_full(&pdevinfo);
+	KUNIT_ASSERT_TRUE(test, IS_ERR(pdev));
+	KUNIT_ASSERT_EQ_MSG(test, PTR_ERR(pdev), -EINVAL,
+			    "Expected errno == -EINVAL, got: %pe", pdev);
+
+	pdevinfo = (struct platform_device_info){
+		.name = DEVICE_NAME,
+		.id = PLATFORM_DEVID_NONE,
+		.swnode = &platform_device_test_swnode,
+		.properties = properties,
+	};
+
+	pdev = platform_device_register_full(&pdevinfo);
+	KUNIT_ASSERT_TRUE(test, IS_ERR(pdev));
+	KUNIT_ASSERT_EQ_MSG(test, PTR_ERR(pdev), -EINVAL,
+			    "Expected errno == -EINVAL, got: %pe", pdev);
+
+	pdevinfo = (struct platform_device_info){
+		.name = DEVICE_NAME,
+		.id = PLATFORM_DEVID_NONE,
+		.fwnode = fwnode,
+		.properties = properties,
+	};
+
+	pdev = platform_device_register_full(&pdevinfo);
+	KUNIT_ASSERT_TRUE(test, IS_ERR(pdev));
+	KUNIT_ASSERT_EQ_MSG(test, PTR_ERR(pdev), -EINVAL,
+			    "Expected errno == -EINVAL, got: %pe", pdev);
+}
+
+static struct kunit_case platform_device_swnode_tests[] = {
+	KUNIT_CASE(platform_device_swnode_add_twice),
+	KUNIT_CASE(platform_device_swnode_as_primary),
+	KUNIT_CASE(platform_device_two_swnodes),
+	{}
+};
+
+static struct kunit_suite platform_device_swnode_test_suite = {
+	.name = "platform-device-swnode",
+	.test_cases = platform_device_swnode_tests,
+};
+
 kunit_test_suites(
 	&platform_device_devm_test_suite,
 	&platform_device_match_test_suite,
+	&platform_device_swnode_test_suite,
 );
 
 MODULE_DESCRIPTION("Test module for platform devices");
Signature · 1 line
2.47.3
#11 lore raw

Re: [PATCH v8 5/5] driver core: platform: tests: add test cases for correct swnode removal

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] Failing KUNIT_ASSERT_* macros abort the test early without unregistering the platform device…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Failing KUNIT_ASSERT_* macros abort the test early without unregistering the platform device, leaking it and leaving dangling pointers.
- [High] Binding a test-scoped fwnode to a refcounted platform_device leads to a Use-After-Free if the device outlives the test, even on the success path.
- [Low] Missing <kunit/fwnode.h> dependency causes build breakage.
--

--- Patch [5]: [PATCH v8 5/5] driver core: platform: tests: add test cases for correct swnode removal --- commit 34e0068267b7ef119d11aacf8edb960ce02d5583
Author: Bartosz Golaszewski <[email protected]>

driver core: platform: tests: add test cases for correct swnode removal

This commit extends the kunit module for platform devices by adding test cases to verify that the same software node can be added to platform devices repeatedly.

drivers/base/test/platform-device-test.c line 3

Quoted 8 lines
drivers/base/test/platform-device-test.c +1 -0
index 6355a2231b741..9ce563f76aad6 100644
@@ -1,12 +1,15 @@
 // SPDX-License-Identifier: GPL-2.0
 
+#include <kunit/fwnode.h>

[Severity: Low]
This include, and the kunit_software_node_register() function used later in the test, don't seem to exist in the tree at this point.

Will this cause a build failure?

[ ... ]

Quoted 13 lines
drivers/base/test/platform-device-test.c +13 -0
+static void platform_device_swnode_add_twice(struct kunit *test)
+{
+	struct platform_device_info pdevinfo;
+	struct platform_device *pdev;
+	struct fwnode_handle *fwnode;
+	bool bound = false;
+	int ret;
+
+	fwnode = kunit_kzalloc(test, sizeof(*fwnode), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fwnode);
+
+	ret = kunit_platform_driver_register(test, &platform_swnode_test_driver);
+	KUNIT_ASSERT_EQ(test, ret, 0);

drivers/base/test/platform-device-test.c static struct kunit_suite platform_device_match_test_suite = { · line 302

Quoted 10 lines
drivers/base/test/platform-device-test.c +10 -0
+
+	fwnode_init(fwnode, NULL);
+	pdevinfo = (struct platform_device_info){
+		.name = DEVICE_NAME,
+		.id = PLATFORM_DEVID_NONE,
+		.fwnode = fwnode,
+		.swnode = &platform_device_test_swnode,
+	};
+
+	pdev = platform_device_register_full(&pdevinfo);

[Severity: High]
Since the fwnode is allocated with kunit_kzalloc(), its lifetime is tied strictly to the test execution. However, platform devices are refcounted, and platform_device_unregister() only drops a reference count.

If the final platform_device_release() is delayed (for example, by external subsystems holding references), and the test finishes in the meantime, the fwnode memory will be freed early.

When the delayed release finally executes, it checks is_software_node(dev_fwnode(dev)). Does this result in a use-after-free on the kunit-managed fwnode pointer?

drivers/base/test/platform-device-test.c static struct kunit_suite platform_device_match_test_suite = { · line 309

Quoted 7 lines
drivers/base/test/platform-device-test.c +7 -0
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
+
+	wait_for_device_probe();
+	scoped_guard(device, &pdev->dev)
+		bound = device_is_bound(&pdev->dev);
+
+	KUNIT_ASSERT_TRUE(test, bound);

[Severity: High]
If this assertion fails, KUNIT_ASSERT_TRUE will abort the test immediately and skip the subsequent platform_device_unregister(pdev) call.

Since the platform device was registered globally, does an assertion failure here permanently leak the device and leave it pointing to test-scoped memory after the test exits?

[ ... ]

Quoted 17 lines
drivers/base/test/platform-device-test.c +17 -0
+static void platform_device_two_swnodes(struct kunit *test)
+{
+	static const struct property_entry properties[] = {
+		PROPERTY_ENTRY_U32("foo", 42),
+		{ }
+	};
+
+	struct platform_device_info pdevinfo;
+	struct platform_device *pdev;
+	struct fwnode_handle *fwnode;
+	int ret;
+
+	ret = kunit_platform_driver_register(test, &platform_swnode_test_driver);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
+	fwnode = kunit_software_node_register(test, &platform_device_test_swnode);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fwnode);

drivers/base/test/platform-device-test.c static struct kunit_suite platform_device_match_test_suite = { · line 394

Quoted 10 lines
drivers/base/test/platform-device-test.c +10 -0
+
+	pdevinfo = (struct platform_device_info){
+		.name = DEVICE_NAME,
+		.id = PLATFORM_DEVID_NONE,
+		.fwnode = fwnode,
+		.swnode = &platform_device_test_swnode,
+	};
+
+	pdev = platform_device_register_full(&pdevinfo);
+	KUNIT_ASSERT_TRUE(test, IS_ERR(pdev));

[Severity: High]
If the invalid device registration surprisingly succeeds, this assertion will fail and abort the test without unregistering the newly created device.

Could this permanently leak the platform device if the test fails?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/20260716-swnode-remove-on-dev-unreg-v8-0-5c2b8cc38c28@oss.qualcomm.com?part=5
maintainer Applied
#12 lore raw
In reply to Bartosz Golaszewski
Quoted 1 line · Bartosz Golaszewski

[PATCH v8 0/5] driver core: unify the release path for dynamically allocated platform devices

Applied, thanks!

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

[1/5] driver core: platform: amend the API contract for fwnode setters

      commit: e6054f410c97
[2/5] platform/surface: gpe: use platform_device_register_full()
      commit: 1e0bd438b876
[3/5] drm/xe/i2c: use device_create_managed_software_node()
      commit: 0f485f88656a
[4/5] driver core: platform: unify release path
      commit: 7d80509ff0da
[5/5] driver core: platform: tests: add test cases for correct swnode removal
      commit: 397f0a3958c9

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.