Linux ACPI
 help / color / mirror / Atom feed
* [PATCH] mfd: core: Remove software node after unregistering platform device
@ 2026-12-04 18:16 Cesar Cibrian
  2026-12-04 19:47 ` Andy Shevchenko
  0 siblings, 1 reply; 5+ messages in thread
From: Cesar Cibrian @ 2026-12-04 18:16 UTC (permalink / raw)
  To: Lee Jones
  Cc: mfd, Andy Shevchenko, Heikki Krogerus, linux-i2c, linux-acpi,
	linux-kernel, Cesar Cibrian

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()
(device_for_each_child_reverse() holds a reference on dev throughout
mfd_remove_devices_fn()). Because acpi_unbind_one() calls
set_primary_fwnode(dev, NULL), which skips clearing fn->secondary
when fn == dev->parent->fwnode, also clear the parent's secondary
fwnode when the child shared its parent's fwnode so rebinding does
not fail with -EBUSY.

Fixes: 42e59982917a ("mfd: core: Add support for software nodes")
Signed-off-by: Cesar Cibrian <cesarcibrian.ai@gmail.com>
---
 drivers/mfd/mfd-core.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/drivers/mfd/mfd-core.c b/drivers/mfd/mfd-core.c
index 16ad4fd26..aa479c2ea 100644
--- a/drivers/mfd/mfd-core.c
+++ b/drivers/mfd/mfd-core.c
@@ -358,6 +358,7 @@ static int mfd_remove_devices_fn(struct device *dev, void *data)
 	const struct mfd_cell *cell;
 	struct mfd_of_node_entry *of_entry, *tmp;
 	int *level = data;
+	bool shared_fwnode;
 
 	if (dev->type != &mfd_dev_type)
 		return 0;
@@ -368,9 +369,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) {
@@ -382,7 +380,17 @@ static int mfd_remove_devices_fn(struct device *dev, void *data)
 	regulator_bulk_unregister_supply_alias(dev, cell->parent_supplies,
 					       cell->num_parent_supplies);
 
+	shared_fwnode = dev->parent && dev_fwnode(dev) &&
+			dev_fwnode(dev) == dev_fwnode(dev->parent);
+
 	platform_device_unregister(pdev);
+
+	if (cell->swnode) {
+		if (shared_fwnode)
+			set_secondary_fwnode(dev->parent, NULL);
+		device_remove_software_node(&pdev->dev);
+	}
+
 	return 0;
 }
 
-- 
2.56.1


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

* Re: [PATCH] mfd: core: Remove software node after unregistering platform device
  2026-12-04 18:16 [PATCH] mfd: core: Remove software node after unregistering platform device Cesar Cibrian
@ 2026-12-04 19:47 ` Andy Shevchenko
  2026-12-04 20:12   ` Dmitry Torokhov
  0 siblings, 1 reply; 5+ messages in thread
From: Andy Shevchenko @ 2026-12-04 19:47 UTC (permalink / raw)
  To: Cesar Cibrian, Dmitry Torokhov
  Cc: Lee Jones, mfd, Heikki Krogerus, linux-i2c, linux-acpi,
	linux-kernel, Danilo Krummrich, Bartosz Golaszewski, Johan Hovold

On Fri, Dec 04, 2026 at 10:16:05AM -0800, Cesar Cibrian wrote:
> 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()
> (device_for_each_child_reverse() holds a reference on dev throughout
> mfd_remove_devices_fn()). Because acpi_unbind_one() calls
> set_primary_fwnode(dev, NULL), which skips clearing fn->secondary
> when fn == dev->parent->fwnode, also clear the parent's secondary
> fwnode when the child shared its parent's fwnode so rebinding does
> not fail with -EBUSY.

I believe it's a local hack against bigger issue which is discussed here
https://lore.kernel.org/lkml/20261202-swnode-shared-primary-fix-v1-0-7ff23caaa1ed@gmail.com/

I Cc'ed people from that thread. Meanwhile, can you test the proposed series
and confirm if it fixes your issue or not?

-- 
With Best Regards,
Andy Shevchenko



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

* Re: [PATCH] mfd: core: Remove software node after unregistering platform device
  2026-12-04 19:47 ` Andy Shevchenko
@ 2026-12-04 20:12   ` Dmitry Torokhov
  2026-12-04 20:21     ` Andy Shevchenko
  0 siblings, 1 reply; 5+ messages in thread
From: Dmitry Torokhov @ 2026-12-04 20:12 UTC (permalink / raw)
  To: Andy Shevchenko, Cesar Cibrian
  Cc: Lee Jones, mfd, Heikki Krogerus, linux-i2c, linux-acpi,
	linux-kernel, Danilo Krummrich, Bartosz Golaszewski, Johan Hovold

Hi Andy,

On Fri, Dec 04, 2026 at 09:47:14PM +0200, Andy Shevchenko wrote:
> On Fri, Dec 04, 2026 at 10:16:05AM -0800, Cesar Cibrian wrote:
> > 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()
> > (device_for_each_child_reverse() holds a reference on dev throughout
> > mfd_remove_devices_fn()). Because acpi_unbind_one() calls
> > set_primary_fwnode(dev, NULL), which skips clearing fn->secondary
> > when fn == dev->parent->fwnode, also clear the parent's secondary
> > fwnode when the child shared its parent's fwnode so rebinding does
> > not fail with -EBUSY.
>
> I believe it's a local hack against bigger issue which is discussed here
> https://lore.kernel.org/lkml/20261202-swnode-shared-primary-fix-v1-0-7ff23caaa1ed@gmail.com/
>
> I Cc'ed people from that thread. Meanwhile, can you test the proposed series
> and confirm if it fixes your issue or not?

My series will not fix this on its own. We still need to reorder
software node removal so it stays intact while child devices are being
torn down.

What my series does fix is the shared_fwnode/set_secondary_fwnode()
workaround in this patch. Once device_del() unbinds the driver first and
tears down i2c_adapter while the secondary link is intact,
set_primary_fwnode(&pdev->dev, NULL) sees that pdev->dev attached the
secondary node, moves it onto pdev->dev.fwnode, and clears fn->secondary
automatically.

Also, on non-ACPI shared primary nodes where acpi_unbind_one() does not
run, clearing dev->parent's secondary fwnode before calling
device_remove_software_node(&pdev->dev) makes dev_to_swnode(&pdev->dev)
return NULL and actually leaks cell->swnode's reference.

Thanks.

-- 
Dmitry

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

* Re: [PATCH] mfd: core: Remove software node after unregistering platform device
  2026-12-04 20:12   ` Dmitry Torokhov
@ 2026-12-04 20:21     ` Andy Shevchenko
  2026-12-04 20:27       ` Dmitry Torokhov
  0 siblings, 1 reply; 5+ messages in thread
From: Andy Shevchenko @ 2026-12-04 20:21 UTC (permalink / raw)
  To: Dmitry Torokhov
  Cc: Cesar Cibrian, Lee Jones, mfd, Heikki Krogerus, linux-i2c,
	linux-acpi, linux-kernel, Danilo Krummrich, Bartosz Golaszewski,
	Johan Hovold

On Fri, Dec 04, 2026 at 12:12:00PM -0800, Dmitry Torokhov wrote:
> On Fri, Dec 04, 2026 at 09:47:14PM +0200, Andy Shevchenko wrote:
> > On Fri, Dec 04, 2026 at 10:16:05AM -0800, Cesar Cibrian wrote:
> > > 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()
> > > (device_for_each_child_reverse() holds a reference on dev throughout
> > > mfd_remove_devices_fn()). Because acpi_unbind_one() calls
> > > set_primary_fwnode(dev, NULL), which skips clearing fn->secondary
> > > when fn == dev->parent->fwnode, also clear the parent's secondary
> > > fwnode when the child shared its parent's fwnode so rebinding does
> > > not fail with -EBUSY.
> >
> > I believe it's a local hack against bigger issue which is discussed here
> > https://lore.kernel.org/lkml/20261202-swnode-shared-primary-fix-v1-0-7ff23caaa1ed@gmail.com/
> >
> > I Cc'ed people from that thread. Meanwhile, can you test the proposed series
> > and confirm if it fixes your issue or not?

Dmitry, thanks for chiming in!

> My series will not fix this on its own. We still need to reorder
> software node removal so it stays intact while child devices are being
> torn down.
> 
> What my series does fix is the shared_fwnode/set_secondary_fwnode()
> workaround in this patch. Once device_del() unbinds the driver first and
> tears down i2c_adapter while the secondary link is intact,
> set_primary_fwnode(&pdev->dev, NULL) sees that pdev->dev attached the
> secondary node, moves it onto pdev->dev.fwnode, and clears fn->secondary
> automatically.
> 
> Also, on non-ACPI shared primary nodes where acpi_unbind_one() does not
> run, clearing dev->parent's secondary fwnode before calling
> device_remove_software_node(&pdev->dev) makes dev_to_swnode(&pdev->dev)
> return NULL and actually leaks cell->swnode's reference.

Hmm... Does it mean that your series fixes the issue on non-ACPI cases as well?

Cesar, based on Dmitry's answer can you apply his series and develop the
missing piece on top?

-- 
With Best Regards,
Andy Shevchenko



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

* Re: [PATCH] mfd: core: Remove software node after unregistering platform device
  2026-12-04 20:21     ` Andy Shevchenko
@ 2026-12-04 20:27       ` Dmitry Torokhov
  0 siblings, 0 replies; 5+ messages in thread
From: Dmitry Torokhov @ 2026-12-04 20:27 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Cesar Cibrian, Lee Jones, mfd, Heikki Krogerus, linux-i2c,
	linux-acpi, linux-kernel, Danilo Krummrich, Bartosz Golaszewski,
	Johan Hovold

On Fri, Dec 04, 2026 at 10:21:00PM +0200, Andy Shevchenko wrote:
> On Fri, Dec 04, 2026 at 12:12:00PM -0800, Dmitry Torokhov wrote:
> > On Fri, Dec 04, 2026 at 09:47:14PM +0200, Andy Shevchenko wrote:
> > > On Fri, Dec 04, 2026 at 10:16:05AM -0800, Cesar Cibrian wrote:
> > > > 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()
> > > > (device_for_each_child_reverse() holds a reference on dev throughout
> > > > mfd_remove_devices_fn()). Because acpi_unbind_one() calls
> > > > set_primary_fwnode(dev, NULL), which skips clearing fn->secondary
> > > > when fn == dev->parent->fwnode, also clear the parent's secondary
> > > > fwnode when the child shared its parent's fwnode so rebinding does
> > > > not fail with -EBUSY.
> > >
> > > I believe it's a local hack against bigger issue which is discussed here
> > > https://lore.kernel.org/lkml/20261202-swnode-shared-primary-fix-v1-0-7ff23caaa1ed@gmail.com/
> > >
> > > I Cc'ed people from that thread. Meanwhile, can you test the proposed series
> > > and confirm if it fixes your issue or not?
> 
> Dmitry, thanks for chiming in!
> 
> > My series will not fix this on its own. We still need to reorder
> > software node removal so it stays intact while child devices are being
> > torn down.
> > 
> > What my series does fix is the shared_fwnode/set_secondary_fwnode()
> > workaround in this patch. Once device_del() unbinds the driver first and
> > tears down i2c_adapter while the secondary link is intact,
> > set_primary_fwnode(&pdev->dev, NULL) sees that pdev->dev attached the
> > secondary node, moves it onto pdev->dev.fwnode, and clears fn->secondary
> > automatically.
> > 
> > Also, on non-ACPI shared primary nodes where acpi_unbind_one() does not
> > run, clearing dev->parent's secondary fwnode before calling
> > device_remove_software_node(&pdev->dev) makes dev_to_swnode(&pdev->dev)
> > return NULL and actually leaks cell->swnode's reference.
> 
> Hmm... Does it mean that your series fixes the issue on non-ACPI cases as well?

I was trying to say that I think Cesar's patch introduces a leak on
non-ACPI systems...

> 
> Cesar, based on Dmitry's answer can you apply his series and develop the
> missing piece on top?
> 

Thanks.

-- 
Dmitry

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

end of thread, other threads:[~2026-12-04 20:27 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 18:16 [PATCH] mfd: core: Remove software node after unregistering platform device Cesar Cibrian
2026-12-04 19:47 ` Andy Shevchenko
2026-12-04 20:12   ` Dmitry Torokhov
2026-12-04 20:21     ` Andy Shevchenko
2026-12-04 20:27       ` Dmitry Torokhov

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