mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Cesar Cibrian <cesarcibrian.ai@gmail.com>
To: Lee Jones <lee@kernel.org>, Danilo Krummrich <dakr@kernel.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	"Rafael J . Wysocki" <rafael@kernel.org>,
	Johan Hovold <johan@kernel.org>
Cc: Dmitry Torokhov <dmitry.torokhov@gmail.com>,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	Heikki Krogerus <heikki.krogerus@linux.intel.com>,
	Bartosz Golaszewski <brgl@kernel.org>,
	Sakari Ailus <sakari.ailus@linux.intel.com>,
	Daniel Scally <djrscally@gmail.com>,
	mfd@lists.linux.dev, driver-core@lists.linux.dev,
	linux-i2c@vger.kernel.org, linux-acpi@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH v2] mfd: core: Remove software node after unregistering platform device
Date: Sat,  5 Dec 2026 16:57:55 -0800	[thread overview]
Message-ID: <20261206005755.27703-1-cesarcibrian.ai@gmail.com> (raw)

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


                 reply	other threads:[~2026-12-06  0:58 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

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

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

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

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

  git send-email \
    --in-reply-to=20261206005755.27703-1-cesarcibrian.ai@gmail.com \
    --to=cesarcibrian.ai@gmail.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=brgl@kernel.org \
    --cc=dakr@kernel.org \
    --cc=djrscally@gmail.com \
    --cc=dmitry.torokhov@gmail.com \
    --cc=driver-core@lists.linux.dev \
    --cc=gregkh@linuxfoundation.org \
    --cc=heikki.krogerus@linux.intel.com \
    --cc=johan@kernel.org \
    --cc=lee@kernel.org \
    --cc=linux-acpi@vger.kernel.org \
    --cc=linux-i2c@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mfd@lists.linux.dev \
    --cc=rafael@kernel.org \
    --cc=sakari.ailus@linux.intel.com \
    /path/to/YOUR_REPLY

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

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®