mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Johan Hovold <johan@kernel.org>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	Danilo Krummrich <dakr@kernel.org>,
	Johan Hovold <johan@kernel.org>
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	Daniel Scally <djrscally@gmail.com>,
	Heikki Krogerus <heikki.krogerus@linux.intel.com>,
	Sakari Ailus <sakari.ailus@linux.intel.com>,
	Bartosz Golaszewski <brgl@kernel.org>,
	Andi Shyti <andi.shyti@kernel.org>,
	Wolfram Sang <wsa+renesas@sang-engineering.com>,
	driver-core@lists.linux.dev, linux-acpi@vger.kernel.org,
	linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org,
	Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
Subject: [PATCH v3 5/8] driver core: platform: warn on bad fwnode assignment
Date: Tue, 17 Nov 2026 11:13:33 +0100	[thread overview]
Message-ID: <20261117101336.980216-6-johan@kernel.org> (raw)
In-Reply-To: <20261117101336.980216-1-johan@kernel.org>

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 <bartosz.golaszewski@oss.qualcomm.com>
Signed-off-by: Johan Hovold <johan@kernel.org>
---
 drivers/base/platform.c | 40 ++++++++++++++++++++++++----------------
 1 file changed, 24 insertions(+), 16 deletions(-)

diff --git a/drivers/base/platform.c b/drivers/base/platform.c
index e0b38dbd5acf..06ce980b57b1 100644
--- a/drivers/base/platform.c
+++ b/drivers/base/platform.c
@@ -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


  parent reply	other threads:[~2026-11-17 10:13 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 10:13 [PATCH v3 0/8] driver core: platform: fix swnode use-after-free on device release Johan Hovold
2026-11-17 10:13 ` [PATCH v3 1/8] " Johan Hovold
2026-11-17 10:13 ` [PATCH v3 2/8] i2c: core: fix swnode use-after-free on client deregistration Johan Hovold
2026-11-17 10:13 ` [PATCH v3 3/8] software node: fix device_add_software_node() kernel doc Johan Hovold
2026-11-17 10:13 ` [PATCH v3 4/8] software node: ignore primary nodes in device_remove_software_node() Johan Hovold
2026-11-17 10:13 ` Johan Hovold [this message]
2026-11-17 10:13 ` [PATCH v3 6/8] driver core: platform: tests: fix swnode reuse test description Johan Hovold
2026-11-17 10:13 ` [PATCH v3 7/8] software node: fix software_node_notify() primary node comment Johan Hovold
2026-11-17 10:13 ` [PATCH v3 8/8] device property: fix fwnode_get_next_child_node() comments Johan Hovold

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=20261117101336.980216-6-johan@kernel.org \
    --to=johan@kernel.org \
    --cc=andi.shyti@kernel.org \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=bartosz.golaszewski@oss.qualcomm.com \
    --cc=brgl@kernel.org \
    --cc=dakr@kernel.org \
    --cc=djrscally@gmail.com \
    --cc=driver-core@lists.linux.dev \
    --cc=gregkh@linuxfoundation.org \
    --cc=heikki.krogerus@linux.intel.com \
    --cc=linux-acpi@vger.kernel.org \
    --cc=linux-i2c@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rafael@kernel.org \
    --cc=sakari.ailus@linux.intel.com \
    --cc=wsa+renesas@sang-engineering.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®