blob: ed55ae7ad3698c509ccbdfe42fc40a1229f8a305 [file]
From f8623ce596492d2e96ca57eeec992938d3d79f00 Mon Sep 17 00:00:00 2001
From: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
Date: Mon, 2 Nov 2026 16:25:16 +0100
Subject: software node: only let the owner release a managed node
From: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
commit f8623ce596492d2e96ca57eeec992938d3d79f00 upstream.
device_create_managed_software_node() creates a node whose lifetime is
tied to the device it was created for. software_node_notify_remove() is
called for every device that has the node as its fwnode (primary OR
secondary) and, if the node is managed, unconditionally detaches it from
that device and drops the creation reference.
This breaks when the node is shared with another device. Example: the
i2c-designware copies the parent's fwnode to the adapter's struct device.
When the adapter is deleted, it releases the reference that belongs to
the owner and the latter ends up with a dangling pointer.
Record the device's "manager" and only perform the actual release when
notify is called for that "owner" device.
Note that it was never the intention that managed software nodes would
be shared, but descendants reusing ancestor firmware nodes is not
uncommon and such use is hard to predict when allocating software nodes.
Reported-by: Balasubramani Vivekanandan <balasubramani.vivekanandan@intel.com>
Closes: https://lore.kernel.org/all/20261009164241.923756-2-balasubramani.vivekanandan@intel.com/
Fixes: 151f6ff78cdf ("software node: Provide replacement for device_add_properties()")
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
Fixes: 0f485f88656a ("drm/xe/i2c: use device_create_managed_software_node()")
Reviewed-by: Balasubramani Vivekanandan <balasubramani.vivekanandan@intel.com>
Tested-by: Balasubramani Vivekanandan <balasubramani.vivekanandan@intel.com>
Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Cc: stable@vger.kernel.org
Link: https://patch.msgid.link/20261102-swnode-only-owner-release-v1-1-ee4c9022a629@oss.qualcomm.com
[ johan: drop temporary variable; skip clearing owner field;
mention reuse not intended in commit message ]
Signed-off-by: Johan Hovold <johan@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
drivers/base/swnode.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
--- a/drivers/base/swnode.c
+++ b/drivers/base/swnode.c
@@ -29,6 +29,8 @@ struct swnode {
struct fwnode_handle fwnode;
const struct software_node *node;
int id;
+ /* Device the managed node's lifetime is tied to. */
+ struct device *owner;
/* hierarchy */
struct ida child_ids;
@@ -37,7 +39,6 @@ struct swnode {
struct swnode *parent;
unsigned int allocated:1;
- unsigned int managed:1;
};
static DEFINE_IDA(swnode_root_ids);
@@ -1145,7 +1146,7 @@ int device_create_managed_software_node(
if (IS_ERR(fwnode))
return PTR_ERR(fwnode);
- to_swnode(fwnode)->managed = true;
+ to_swnode(fwnode)->owner = dev;
set_secondary_fwnode(dev, fwnode);
if (device_is_registered(dev))
@@ -1208,7 +1209,7 @@ void software_node_notify_remove(struct
swnode_put(swnode);
- if (swnode->managed) {
+ if (swnode->owner == dev) {
set_secondary_fwnode(dev, NULL);
swnode_put(swnode);
}