Linux I2C development
 help / color / mirror / Atom feed
* [PATCH v2] mfd: core: Remove software node after unregistering platform device
@ 2026-12-06  0:57 Cesar Cibrian
  0 siblings, 0 replies; only message in thread
From: Cesar Cibrian @ 2026-12-06  0:57 UTC (permalink / raw)
  To: Lee Jones, Danilo Krummrich, Greg Kroah-Hartman,
	Rafael J . Wysocki, Johan Hovold
  Cc: Dmitry Torokhov, Andy Shevchenko, Heikki Krogerus,
	Bartosz Golaszewski, Sakari Ailus, Daniel Scally, mfd,
	driver-core, linux-i2c, linux-acpi, linux-kernel

mfd_add_device() attaches cell->swnode before calling
platform_device_add(), but mfd_remove_devices_fn() calls
device_remove_software_node() before platform_device_unregister().

If the child driver propagates dev_fwnode() to a sub-device (for
example, i2c-designware sharing its fwnode with the i2c_adapter
device on Intel LPSS), removing the software node first clears
fwnode->secondary while the sub-device is still registered. When
platform_device_unregister() subsequently tears down the child,
software_node_notify_remove() sees dev_to_swnode() == NULL on the
sub-device and skips cleanup, leaking a swnode reference and
leaving a stale sysfs symlink that triggers a warning on rebind:

sysfs: cannot create duplicate filename '/kernel/software_nodes/node0/i2c-0'

Move device_remove_software_node() after platform_device_unregister()
so the secondary software node link stays intact until the child
driver and any sub-devices have been torn down, while still detaching
cell->swnode synchronously before mfd_remove_devices() returns rather
than deferring to platform_device_release().

With DEV_FLAG_SWNODE_ADDED, set_primary_fwnode(dev, NULL) called from
acpi_unbind_one() moves the secondary node attached by this device
onto dev->fwnode and clears the shared fn->secondary, so the
subsequent device_remove_software_node() drops the reference taken in
mfd_add_device(). Without ACPI, dev->fwnode is not detached on unbind,
so device_remove_software_node() clears the secondary node directly.

This change strictly depends on "driver core: swnode: only release
owned secondary software nodes"; without it, reordering leaves a
shared ACPI primary node with a dangling fn->secondary
(use-after-free on rebind).

Fixes: 42e59982917a ("mfd: core: Add support for software nodes")
Signed-off-by: Cesar Cibrian <cesarcibrian.ai@gmail.com>
---
Danilo offered to take this patch through the driver-core tree on top
of Dmitry's series if Lee is fine with providing an Acked-by [2].

Changes in v2:
- Rebased on top of Dmitry Torokhov's series "driver core: swnode: fix
  release with shared primary fwnode" [1], which tracks secondary
  software node ownership via DEV_FLAG_SWNODE_ADDED and handles
  detaching shared primary fwnodes automatically in set_primary_fwnode().
- Dropped the shared_fwnode / set_secondary_fwnode(dev->parent, NULL)
  workaround from v1, which leaked cell->swnode on non-ACPI shared
  primary fwnodes.
- Documented why the reorder is safe with DEV_FLAG_SWNODE_ADDED and
  its hard dependency on Dmitry's patch 1/3 in the commit message, and
  added an inline comment in mfd_remove_devices_fn().
- Tested on Lenovo IdeaPad Slim 5 16IRL8 (Intel LPSS / i2c-designware,
  ELAN06FA:00 touchpad: 10 intel-lpss + 3 i2c_designware unbind/bind
  cycles) and KUnit (platform-device-swnode, property-entry,
  software-node-links).

v1: https://lore.kernel.org/r/20261204181605.22364-1-cesarcibrian.ai@gmail.com
[1] https://lore.kernel.org/r/20261202-swnode-shared-primary-fix-v1-0-7ff23caaa1ed@gmail.com
[2] https://lore.kernel.org/r/DND6CYGDKSSC.1ZXTPX17DK3GF@kernel.org

 drivers/mfd/mfd-core.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/drivers/mfd/mfd-core.c b/drivers/mfd/mfd-core.c
index 16ad4fd26..c425efce7 100644
--- a/drivers/mfd/mfd-core.c
+++ b/drivers/mfd/mfd-core.c
@@ -368,9 +368,6 @@ static int mfd_remove_devices_fn(struct device *dev, void *data)
 	if (level && cell->level > *level)
 		return 0;
 
-	if (cell->swnode)
-		device_remove_software_node(&pdev->dev);
-
 	scoped_guard(mutex, &mfd_of_node_mutex) {
 		list_for_each_entry_safe(of_entry, tmp, &mfd_of_node_list, list)
 			if (of_entry->dev == &pdev->dev) {
@@ -383,6 +380,14 @@ static int mfd_remove_devices_fn(struct device *dev, void *data)
 					       cell->num_parent_supplies);
 
 	platform_device_unregister(pdev);
+
+	/*
+	 * Sub-devices may share our fwnode; drop the software node only after
+	 * they are gone (device_for_each_child_reverse() holds a ref on dev).
+	 */
+	if (cell->swnode)
+		device_remove_software_node(&pdev->dev);
+
 	return 0;
 }
 

base-commit: 253f86660c1f882fbf554db5eaa34bc5d66dc7e8
prerequisite-patch-id: ed0efd312ca226c19c3c6916586712f824a1b169
prerequisite-patch-id: 81bc64d09d9fff08f990ad2644078b100f3ee30d
prerequisite-patch-id: 411121a99a0e343557184b4b20615ca0b5ceab02
-- 
2.56.1


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-06  0:58 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-06  0:57 [PATCH v2] mfd: core: Remove software node after unregistering platform device Cesar Cibrian

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