* [PATCH 0/3] driver core: swnode: fix release with shared primary fwnode
@ 2026-12-03 1:34 Dmitry Torokhov
2026-12-03 1:34 ` [PATCH 1/3] driver core: swnode: only release owned secondary software nodes Dmitry Torokhov
` (4 more replies)
0 siblings, 5 replies; 9+ messages in thread
From: Dmitry Torokhov @ 2026-12-03 1:34 UTC (permalink / raw)
To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich
Cc: Johan Hovold, Andy Shevchenko, Daniel Scally, Heikki Krogerus,
Sakari Ailus, Bartosz Golaszewski, driver-core, linux-acpi,
linux-kernel, stable
This series fixes secondary software node reference count corruption
and premature detachment when a child device shares its parent's
primary firmware node that has a secondary software node attached,
causing reference count underflow and use-after-free in the ACPI case
or early detachment and a leaked reference and sysfs link in the
non-ACPI case.
The issue came up while converting Windows ACPI GPIO lookups in
drivers/mfd/arizona-spi.c to a secondary software node, and also
affects existing ACPI MFD parents sharing their companion fwnode with
child platform devices such as
drivers/platform/x86/x86-android-tablets/lenovo.c.
When an ACPI-shared primary firmware node is detached from a child via
set_primary_fwnode(..., NULL), the child's firmware node pointer is
still left pointing to the parent's secondary software node, and
platform_device_release() drops an extra reference per child via
device_remove_software_node() or fwnode_handle_put(). When a non-ACPI
shared primary node stays attached through release,
device_remove_software_node() unconditionally clears the shared
secondary link and drops one of the parent's references, leaving the
other reference and sysfs link leaked.
The first patch adds a per-device flag tracking whether the device
attached a secondary software node, uses it in set_primary_fwnode() to
avoid leaving a shared primary node's secondary node on the child or
modifying the secondary link unless owned by the device, and gates
secondary software node removal and managed creation reference release
on that flag.
The second patch replaces the un-refcounted owner pointer in struct
swnode with a managed bitfield now that per-device ownership is tracked
via the new device flag.
The third patch adds KUnit regression tests covering shared primary
fwnodes with unmanaged and managed secondary software nodes, detachment
before release, child-owned secondary nodes on shared primary fwnodes,
release of unregistered devices with managed software nodes, and
device_remove_software_node() on a registered managed device.
This series is intentionally kept minimal for stable backporting.
Storing the secondary pointer directly on the shared struct
fwnode_handle still leaves broader limitations unaddressed:
set_primary_fwnode() only detects direct parent sharing (not grandparent
or sibling sharing), parent and child or sibling devices sharing a
primary node still contend for a single secondary slot, and cross-device
lookups of fwnode->secondary are not synchronized against concurrent
removal. A separate follow-up (such as an immutable composite
fwnode_handle) would be needed to address those cleanly.
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
Dmitry Torokhov (3):
driver core: swnode: only release owned secondary software nodes
software node: replace owner pointer with managed bitfield
driver core: platform: tests: add shared primary fwnode swnode tests
drivers/base/core.c | 15 +-
drivers/base/swnode.c | 19 +-
drivers/base/test/platform-device-test.c | 328 +++++++++++++++++++++++++++++++
include/linux/device.h | 7 +
4 files changed, 358 insertions(+), 11 deletions(-)
---
base-commit: 253f86660c1f882fbf554db5eaa34bc5d66dc7e8
change-id: 20261201-swnode-shared-primary-fix-aa415f344a97
Thanks.
--
Dmitry
^ permalink raw reply [flat|nested] 9+ messages in thread* [PATCH 1/3] driver core: swnode: only release owned secondary software nodes 2026-12-03 1:34 [PATCH 0/3] driver core: swnode: fix release with shared primary fwnode Dmitry Torokhov @ 2026-12-03 1:34 ` Dmitry Torokhov 2026-12-05 23:03 ` Danilo Krummrich 2026-12-03 1:34 ` [PATCH 2/3] software node: replace owner pointer with managed bitfield Dmitry Torokhov ` (3 subsequent siblings) 4 siblings, 1 reply; 9+ messages in thread From: Dmitry Torokhov @ 2026-12-03 1:34 UTC (permalink / raw) To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich Cc: Johan Hovold, Andy Shevchenko, Daniel Scally, Heikki Krogerus, Sakari Ailus, Bartosz Golaszewski, driver-core, linux-acpi, linux-kernel, stable When a child device shares its parent's primary firmware node carrying a secondary software node, detaching an ACPI-shared primary node via set_primary_fwnode() leaves the child pointing to the parent's secondary node without clearing the shared secondary link. That causes platform_device_release() to drop an extra reference per child (via device_remove_software_node(), or via fwnode_handle_put() even if removal is skipped), leading to underflow and use-after-free. When a non-ACPI shared primary node stays attached through release, device_remove_software_node() unconditionally clears the shared secondary link and drops one of the parent's references, prematurely detaching the parent's software node and leaking the remaining reference and sysfs link. Track secondary software node attachment per device with a new device flag. In set_primary_fwnode(), only retain a shared primary node's secondary node on the child when the child attached it, clearing the primary node's secondary link; otherwise clear the child's firmware node pointer without touching the secondary link. Gate device_remove_software_node() on the same flag before notifying or detaching, and gate managed node release in software_node_notify_remove() so calling device_remove_software_node() on a registered managed device (such as during i2c_unregister_device() or spi_unregister_device()) does not double-put. Fixes: 99aed9227073 ("device property: Don't clear secondary pointer for shared primary firmware node") Fixes: 7b5b51b2b05c ("driver core: platform: count references to all kinds of firmware nodes") Fixes: 7d80509ff0da ("driver core: platform: unify release path") Cc: stable@vger.kernel.org Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com> --- drivers/base/core.c | 15 +++++++++++---- drivers/base/swnode.c | 14 ++++++++++---- include/linux/device.h | 7 +++++++ 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/drivers/base/core.c b/drivers/base/core.c index 44f79f427b6b..673dc5941019 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -5250,12 +5250,19 @@ void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode) dev->fwnode = fwnode; } else { if (fwnode_is_primary(fn)) { - dev->fwnode = fn->secondary; - - /* Skip nullifying fn->secondary if the primary is shared */ - if (parent && fn == parent->fwnode) + /* + * If the primary fwnode is shared with the parent and + * this device did not attach the secondary node, clear + * dev->fwnode without leaving it pointing to the + * parent's secondary node or modifying fn->secondary. + */ + if (parent && fn == parent->fwnode && + !dev_swnode_added(dev)) { + dev->fwnode = NULL; return; + } + dev->fwnode = fn->secondary; /* Set fn->secondary = NULL, so fn remains the primary fwnode */ fn->secondary = NULL; } else { diff --git a/drivers/base/swnode.c b/drivers/base/swnode.c index bc51a92adcb8..da36f91f6434 100644 --- a/drivers/base/swnode.c +++ b/drivers/base/swnode.c @@ -1073,6 +1073,7 @@ int device_add_software_node(struct device *dev, const struct software_node *nod } set_secondary_fwnode(dev, &swnode->fwnode); + dev_set_swnode_added(dev); /* * If the device has been fully registered by the time this function is @@ -1088,11 +1089,12 @@ int device_add_software_node(struct device *dev, const struct software_node *nod EXPORT_SYMBOL_GPL(device_add_software_node); /** - * device_remove_software_node - Remove device's software node + * device_remove_software_node - Remove device's secondary software node * @dev: The device with the software node. * - * Remove a software node added by device_add_software_node() and - * deregister it if this is the last reference. + * Remove a secondary software node attached by device_add_software_node() + * or device_create_managed_software_node() and deregister it if this is + * the last reference. */ void device_remove_software_node(struct device *dev) { @@ -1105,6 +1107,9 @@ void device_remove_software_node(struct device *dev) if (fwnode_is_primary(&swnode->fwnode)) return; + if (!dev_test_and_clear_swnode_added(dev)) + return; + if (device_is_registered(dev)) software_node_notify_remove(dev); @@ -1145,6 +1150,7 @@ int device_create_managed_software_node(struct device *dev, to_swnode(fwnode)->owner = dev; set_secondary_fwnode(dev, fwnode); + dev_set_swnode_added(dev); if (device_is_registered(dev)) software_node_notify(dev); @@ -1208,7 +1214,7 @@ void software_node_notify_remove(struct device *dev) if (dev_test_and_clear_swnode_notified(dev)) swnode_put(swnode); - if (swnode->owner == dev) { + if (swnode->owner == dev && dev_test_and_clear_swnode_added(dev)) { set_secondary_fwnode(dev, NULL); swnode_put(swnode); } diff --git a/include/linux/device.h b/include/linux/device.h index 6b082c638d45..bdad371b177a 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -608,6 +608,11 @@ struct device_physical_location { * device and took a reference to the device's software node. * Indicates to software_node_notify_remove() that it should drop * the reference. + * @DEV_FLAG_SWNODE_ADDED: Set if a secondary software node was attached to + * this device via device_add_software_node() or + * device_create_managed_software_node(). Indicates that this + * device owns the attach reference and is allowed to detach and + * drop it. * @DEV_FLAG_COUNT: Number of defined struct_device_flags. */ enum struct_device_flags { @@ -622,6 +627,7 @@ enum struct_device_flags { DEV_FLAG_OFFLINE_DISABLED = 8, DEV_FLAG_OFFLINE = 9, DEV_FLAG_SWNODE_NOTIFIED = 10, + DEV_FLAG_SWNODE_ADDED = 11, DEV_FLAG_COUNT }; @@ -844,6 +850,7 @@ __create_dev_flag_accessors(of_node_reused, DEV_FLAG_OF_NODE_REUSED); __create_dev_flag_accessors(offline_disabled, DEV_FLAG_OFFLINE_DISABLED); __create_dev_flag_accessors(offline, DEV_FLAG_OFFLINE); __create_dev_flag_accessors(swnode_notified, DEV_FLAG_SWNODE_NOTIFIED); +__create_dev_flag_accessors(swnode_added, DEV_FLAG_SWNODE_ADDED); #undef __create_dev_flag_accessors -- 2.56.1.915.g69d5ec7298-goog ^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH 1/3] driver core: swnode: only release owned secondary software nodes 2026-12-03 1:34 ` [PATCH 1/3] driver core: swnode: only release owned secondary software nodes Dmitry Torokhov @ 2026-12-05 23:03 ` Danilo Krummrich 2026-12-06 7:07 ` Dmitry Torokhov 0 siblings, 1 reply; 9+ messages in thread From: Danilo Krummrich @ 2026-12-05 23:03 UTC (permalink / raw) To: Dmitry Torokhov Cc: Greg Kroah-Hartman, Rafael J. Wysocki, Johan Hovold, Andy Shevchenko, Daniel Scally, Heikki Krogerus, Sakari Ailus, Bartosz Golaszewski, driver-core, linux-acpi, linux-kernel, stable On Thu Dec 3, 2026 at 2:34 AM CET, Dmitry Torokhov wrote: > diff --git a/drivers/base/core.c b/drivers/base/core.c > index 44f79f427b6b..673dc5941019 100644 > --- a/drivers/base/core.c > +++ b/drivers/base/core.c > @@ -5250,12 +5250,19 @@ void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode) > dev->fwnode = fwnode; > } else { > if (fwnode_is_primary(fn)) { > - dev->fwnode = fn->secondary; > - > - /* Skip nullifying fn->secondary if the primary is shared */ > - if (parent && fn == parent->fwnode) > + /* > + * If the primary fwnode is shared with the parent and > + * this device did not attach the secondary node, clear > + * dev->fwnode without leaving it pointing to the > + * parent's secondary node or modifying fn->secondary. > + */ > + if (parent && fn == parent->fwnode && > + !dev_swnode_added(dev)) { > + dev->fwnode = NULL; > return; > + } I think the Sashiko report [1] is valid (though it was present before this patch as well). I think we need to preserve the borrowed secondary when detaching a non-shared primary, and ensure device release drops only references the device actually owns. [1] https://sashiko.dev/#/patchset/20261202-swnode-shared-primary-fix-v1-0-7ff23caaa1ed%40gmail.com ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH 1/3] driver core: swnode: only release owned secondary software nodes 2026-12-05 23:03 ` Danilo Krummrich @ 2026-12-06 7:07 ` Dmitry Torokhov 2026-12-06 15:50 ` Danilo Krummrich 0 siblings, 1 reply; 9+ messages in thread From: Dmitry Torokhov @ 2026-12-06 7:07 UTC (permalink / raw) To: Danilo Krummrich Cc: Greg Kroah-Hartman, Rafael J. Wysocki, Johan Hovold, Andy Shevchenko, Daniel Scally, Heikki Krogerus, Sakari Ailus, Bartosz Golaszewski, driver-core, linux-acpi, linux-kernel, stable Hi Danilo, On Sun, Dec 06, 2026 at 12:03:54AM +0100, Danilo Krummrich wrote: > On Thu Dec 3, 2026 at 2:34 AM CET, Dmitry Torokhov wrote: > > diff --git a/drivers/base/core.c b/drivers/base/core.c > > index 44f79f427b6b..673dc5941019 100644 > > --- a/drivers/base/core.c > > +++ b/drivers/base/core.c > > @@ -5250,12 +5250,19 @@ void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode) > > dev->fwnode = fwnode; > > } else { > > if (fwnode_is_primary(fn)) { > > - dev->fwnode = fn->secondary; > > - > > - /* Skip nullifying fn->secondary if the primary is shared */ > > - if (parent && fn == parent->fwnode) > > + /* > > + * If the primary fwnode is shared with the parent and > > + * this device did not attach the secondary node, clear > > + * dev->fwnode without leaving it pointing to the > > + * parent's secondary node or modifying fn->secondary. > > + */ > > + if (parent && fn == parent->fwnode && > > + !dev_swnode_added(dev)) { > > + dev->fwnode = NULL; > > return; > > + } > > I think the Sashiko report [1] is valid (though it was present before this patch > as well). > > I think we need to preserve the borrowed secondary when detaching a non-shared > primary, and ensure device release drops only references the device actually > owns. > > [1] https://sashiko.dev/#/patchset/20261202-swnode-shared-primary-fix-v1-0-7ff23caaa1ed%40gmail.com Right, I agree. set_primary_fwnode(dev, NULL) already preserves the borrowed secondary when "fn != parent->fwnode", so the gap is in platform_device_release() and i2c_unregister_device(), which call fwnode_handle_put(dev_fwnode(dev)) assuming device_remove_software_node() cleared any secondary node first. Before I send v2, which direction do you prefer? 1. Track borrowed dev->fwnode pointers with DEV_FLAG_FWNODE_BORROWED and add device_put_fwnode() in a prep patch (diff below, against the series base). This also covers ipu-bridge.c and mipi-disco-img.c, which write primary->secondary directly without ERR_PTR(-ENODEV). 2. Move fwnode_is_primary() to include/linux/fwnode.h and guard the put: device_remove_software_node(dev); fwnode = dev_fwnode(dev); if (fwnode_is_primary(fwnode)) fwnode_handle_put(fwnode); Smaller diff, though ipu-bridge.c and mipi-disco-img.c stay exposed until fixed separately. Those two can't set ERR_PTR(-ENODEV) ahead of DEV_FLAG_SWNODE_ADDED, since fwnode_is_primary() returning true is what keeps device_remove_software_node() from dropping their ref. I lean toward option 1 since it makes ownership explicit instead of relying on ERR_PTR(-ENODEV). Longer term it may be worth reworking how primary and secondary fwnodes are stacked and refcounted, though that is beyond a stable fix. Thanks. -- Dmitry diff --git a/drivers/base/core.c b/drivers/base/core.c index 44f79f427b6b..460dc2485160 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -5252,16 +5252,18 @@ void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode) if (fwnode_is_primary(fn)) { dev->fwnode = fn->secondary; - /* Skip nullifying fn->secondary if the primary is shared */ - if (parent && fn == parent->fwnode) - return; - - /* Set fn->secondary = NULL, so fn remains the primary fwnode */ - fn->secondary = NULL; + /* + * Set fn->secondary = NULL so fn remains the primary + * fwnode, unless the primary is shared with the parent. + */ + if (!(parent && fn == parent->fwnode)) + fn->secondary = NULL; } else { dev->fwnode = NULL; } } + + dev_assign_fwnode_borrowed(dev, dev->fwnode); } EXPORT_SYMBOL_GPL(set_primary_fwnode); @@ -5279,10 +5281,12 @@ void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode) if (fwnode) fwnode->secondary = ERR_PTR(-ENODEV); - if (fwnode_is_primary(dev->fwnode)) + if (fwnode_is_primary(dev->fwnode)) { dev->fwnode->secondary = fwnode; - else + } else { dev->fwnode = fwnode; + dev_assign_fwnode_borrowed(dev, fwnode); + } } EXPORT_SYMBOL_GPL(set_secondary_fwnode); @@ -5367,9 +5371,29 @@ void device_set_node(struct device *dev, struct fwnode_handle *fwnode) { dev->fwnode = fwnode; dev->of_node = to_of_node(fwnode); + dev_clear_fwnode_borrowed(dev); } EXPORT_SYMBOL_GPL(device_set_node); +/** + * device_put_fwnode - Drop the reference to a device's firmware node. + * @dev: Device whose firmware node reference is to be dropped. + * + * Release the firmware node reference held by @dev, matching the lookup + * precedence of dev_fwnode(): if an OF node is attached, drop its reference + * via of_node_put(); otherwise drop the reference to dev->fwnode unless it was + * assigned as a borrowed reference via set_primary_fwnode() or + * set_secondary_fwnode(). + */ +void device_put_fwnode(struct device *dev) +{ + if (IS_ENABLED(CONFIG_OF) && dev->of_node) + of_node_put(dev->of_node); + else if (!dev_fwnode_borrowed(dev)) + fwnode_handle_put(dev->fwnode); +} +EXPORT_SYMBOL_GPL(device_put_fwnode); + /** * get_dev_from_fwnode - Obtain a reference count of the struct device the * struct fwnode_handle is associated with. diff --git a/drivers/base/platform.c b/drivers/base/platform.c index 9317bb2ece2a..b71c8149a553 100644 --- a/drivers/base/platform.c +++ b/drivers/base/platform.c @@ -604,7 +604,7 @@ static void platform_device_release(struct device *dev) * Must be done after device_remove_software_node() which may have * freed a secondary firmware node and cleared the fwnode pointer. */ - fwnode_handle_put(dev_fwnode(dev)); + device_put_fwnode(dev); kfree(pa->pdev.dev.platform_data); kfree(pa->pdev.mfd_cell); diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c index 3f411a7ff592..55157e94e284 100644 --- a/drivers/i2c/i2c-core-base.c +++ b/drivers/i2c/i2c-core-base.c @@ -1071,7 +1071,7 @@ void i2c_unregister_device(struct i2c_client *client) * Must be done after device_remove_software_node() which may have * freed a secondary firmware node and cleared the fwnode pointer. */ - fwnode_handle_put(dev_fwnode(&client->dev)); + device_put_fwnode(&client->dev); device_unregister(&client->dev); } EXPORT_SYMBOL_GPL(i2c_unregister_device); diff --git a/include/linux/device.h b/include/linux/device.h index 6b082c638d45..5edb70d638e6 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -608,6 +608,10 @@ struct device_physical_location { * device and took a reference to the device's software node. * Indicates to software_node_notify_remove() that it should drop * the reference. + * @DEV_FLAG_FWNODE_BORROWED: Set if dev->fwnode was assigned without taking a + * reference via set_primary_fwnode() or set_secondary_fwnode(). + * Indicates to device_put_fwnode() that it should not drop a + * reference to dev->fwnode. * @DEV_FLAG_COUNT: Number of defined struct_device_flags. */ enum struct_device_flags { @@ -622,6 +626,7 @@ enum struct_device_flags { DEV_FLAG_OFFLINE_DISABLED = 8, DEV_FLAG_OFFLINE = 9, DEV_FLAG_SWNODE_NOTIFIED = 10, + DEV_FLAG_FWNODE_BORROWED = 11, DEV_FLAG_COUNT }; @@ -844,6 +849,7 @@ __create_dev_flag_accessors(of_node_reused, DEV_FLAG_OF_NODE_REUSED); __create_dev_flag_accessors(offline_disabled, DEV_FLAG_OFFLINE_DISABLED); __create_dev_flag_accessors(offline, DEV_FLAG_OFFLINE); __create_dev_flag_accessors(swnode_notified, DEV_FLAG_SWNODE_NOTIFIED); +__create_dev_flag_accessors(fwnode_borrowed, DEV_FLAG_FWNODE_BORROWED); #undef __create_dev_flag_accessors @@ -1283,6 +1289,7 @@ int device_online(struct device *dev); void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode); void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode); void device_set_node(struct device *dev, struct fwnode_handle *fwnode); +void device_put_fwnode(struct device *dev); int device_add_of_node(struct device *dev, struct device_node *of_node); void device_remove_of_node(struct device *dev); void device_set_of_node_from_dev(struct device *dev, const struct device *dev2); ^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH 1/3] driver core: swnode: only release owned secondary software nodes 2026-12-06 7:07 ` Dmitry Torokhov @ 2026-12-06 15:50 ` Danilo Krummrich 0 siblings, 0 replies; 9+ messages in thread From: Danilo Krummrich @ 2026-12-06 15:50 UTC (permalink / raw) To: Dmitry Torokhov Cc: Greg Kroah-Hartman, Rafael J. Wysocki, Johan Hovold, Andy Shevchenko, Daniel Scally, Heikki Krogerus, Sakari Ailus, Bartosz Golaszewski, driver-core, linux-acpi, linux-kernel, stable On Sun Dec 6, 2026 at 8:07 AM CET, Dmitry Torokhov wrote: > Hi Danilo, > > On Sun, Dec 06, 2026 at 12:03:54AM +0100, Danilo Krummrich wrote: >> On Thu Dec 3, 2026 at 2:34 AM CET, Dmitry Torokhov wrote: >> > diff --git a/drivers/base/core.c b/drivers/base/core.c >> > index 44f79f427b6b..673dc5941019 100644 >> > --- a/drivers/base/core.c >> > +++ b/drivers/base/core.c >> > @@ -5250,12 +5250,19 @@ void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode) >> > dev->fwnode = fwnode; >> > } else { >> > if (fwnode_is_primary(fn)) { >> > - dev->fwnode = fn->secondary; >> > - >> > - /* Skip nullifying fn->secondary if the primary is shared */ >> > - if (parent && fn == parent->fwnode) >> > + /* >> > + * If the primary fwnode is shared with the parent and >> > + * this device did not attach the secondary node, clear >> > + * dev->fwnode without leaving it pointing to the >> > + * parent's secondary node or modifying fn->secondary. >> > + */ >> > + if (parent && fn == parent->fwnode && >> > + !dev_swnode_added(dev)) { >> > + dev->fwnode = NULL; >> > return; >> > + } >> >> I think the Sashiko report [1] is valid (though it was present before this patch >> as well). >> >> I think we need to preserve the borrowed secondary when detaching a non-shared >> primary, and ensure device release drops only references the device actually >> owns. >> >> [1] https://sashiko.dev/#/patchset/20261202-swnode-shared-primary-fix-v1-0-7ff23caaa1ed%40gmail.com > > Right, I agree. set_primary_fwnode(dev, NULL) already preserves the > borrowed secondary when "fn != parent->fwnode", so the gap is in > platform_device_release() and i2c_unregister_device(), which call > fwnode_handle_put(dev_fwnode(dev)) assuming > device_remove_software_node() cleared any secondary node first. > > Before I send v2, which direction do you prefer? > > 1. Track borrowed dev->fwnode pointers with DEV_FLAG_FWNODE_BORROWED and > add device_put_fwnode() in a prep patch (diff below, against the > series base). This also covers ipu-bridge.c and mipi-disco-img.c, > which write primary->secondary directly without ERR_PTR(-ENODEV). > > 2. Move fwnode_is_primary() to include/linux/fwnode.h and guard the put: > > device_remove_software_node(dev); > fwnode = dev_fwnode(dev); > if (fwnode_is_primary(fwnode)) > fwnode_handle_put(fwnode); > > Smaller diff, though ipu-bridge.c and mipi-disco-img.c stay exposed > until fixed separately. Those two can't set ERR_PTR(-ENODEV) ahead of > DEV_FLAG_SWNODE_ADDED, since fwnode_is_primary() returning true is > what keeps device_remove_software_node() from dropping their ref. > > I lean toward option 1 since it makes ownership explicit instead of > relying on ERR_PTR(-ENODEV). Yes, I much prefer option 1. One thing to consider, IIUC a sequence of platform_device_set_fwnode(pdev, fwnode); set_primary_fwnode(&pdev->dev, fwnode); would leak the reference acquired by platform_device_set_fwnode() if fwnode is a primary software node. Obviously, it doesn't make sense to do that, but maybe there are cases where this could still happen through common code paths, subsystem wrappers, etc.? I think set_primary_fwnode() should explicitly document its borrowing semantics, including that it does not release any reference previously acquired by the caller or another layer. > Longer term it may be worth reworking how primary and secondary fwnodes are > stacked and refcounted, though that is beyond a stable fix. I think the relationship between primary and secondary could be improved. Otherwise, I think representing shared ownership, borrowing, and the corresponding lifetimes is simply difficult without help from the compiler. ^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 2/3] software node: replace owner pointer with managed bitfield 2026-12-03 1:34 [PATCH 0/3] driver core: swnode: fix release with shared primary fwnode Dmitry Torokhov 2026-12-03 1:34 ` [PATCH 1/3] driver core: swnode: only release owned secondary software nodes Dmitry Torokhov @ 2026-12-03 1:34 ` Dmitry Torokhov 2026-12-03 1:34 ` [PATCH 3/3] driver core: platform: tests: add shared primary fwnode swnode tests Dmitry Torokhov ` (2 subsequent siblings) 4 siblings, 0 replies; 9+ messages in thread From: Dmitry Torokhov @ 2026-12-03 1:34 UTC (permalink / raw) To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich Cc: Johan Hovold, Andy Shevchenko, Daniel Scally, Heikki Krogerus, Sakari Ailus, Bartosz Golaszewski, driver-core, linux-acpi, linux-kernel Per-device secondary software node ownership is now tracked uniformly with DEV_FLAG_SWNODE_ADDED, which also covers shared firmware nodes as addressed by commit f8623ce59649 ("software node: only let the owner release a managed node"). Replace the redundant, un-refcounted, never-cleared owner pointer in struct swnode with a single managed bitfield packed alongside the existing allocated flag. Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com> --- drivers/base/swnode.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/drivers/base/swnode.c b/drivers/base/swnode.c index da36f91f6434..f9ef8f40d760 100644 --- a/drivers/base/swnode.c +++ b/drivers/base/swnode.c @@ -29,8 +29,6 @@ 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; @@ -39,6 +37,7 @@ struct swnode { struct swnode *parent; unsigned int allocated:1; + unsigned int managed:1; }; static DEFINE_IDA(swnode_root_ids); @@ -1148,7 +1147,7 @@ int device_create_managed_software_node(struct device *dev, if (IS_ERR(fwnode)) return PTR_ERR(fwnode); - to_swnode(fwnode)->owner = dev; + to_swnode(fwnode)->managed = 1; set_secondary_fwnode(dev, fwnode); dev_set_swnode_added(dev); @@ -1214,7 +1213,7 @@ void software_node_notify_remove(struct device *dev) if (dev_test_and_clear_swnode_notified(dev)) swnode_put(swnode); - if (swnode->owner == dev && dev_test_and_clear_swnode_added(dev)) { + if (swnode->managed && dev_test_and_clear_swnode_added(dev)) { set_secondary_fwnode(dev, NULL); swnode_put(swnode); } -- 2.56.1.915.g69d5ec7298-goog ^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 3/3] driver core: platform: tests: add shared primary fwnode swnode tests 2026-12-03 1:34 [PATCH 0/3] driver core: swnode: fix release with shared primary fwnode Dmitry Torokhov 2026-12-03 1:34 ` [PATCH 1/3] driver core: swnode: only release owned secondary software nodes Dmitry Torokhov 2026-12-03 1:34 ` [PATCH 2/3] software node: replace owner pointer with managed bitfield Dmitry Torokhov @ 2026-12-03 1:34 ` Dmitry Torokhov 2026-12-03 11:35 ` [PATCH 0/3] driver core: swnode: fix release with shared primary fwnode Andy Shevchenko 2026-12-05 21:00 ` Cesar Cibrian 4 siblings, 0 replies; 9+ messages in thread From: Dmitry Torokhov @ 2026-12-03 1:34 UTC (permalink / raw) To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich Cc: Johan Hovold, Andy Shevchenko, Daniel Scally, Heikki Krogerus, Sakari Ailus, Bartosz Golaszewski, driver-core, linux-acpi, linux-kernel Add KUnit regression tests covering child platform devices that share a parent's primary firmware node with a secondary software node, including both unmanaged and managed software nodes, detachment via set_primary_fwnode() before release, child-owned secondary software nodes on a shared primary node, release of unregistered devices with a managed software node, and removal via device_remove_software_node() on a registered managed device. Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com> --- drivers/base/test/platform-device-test.c | 328 +++++++++++++++++++++++++++++++ 1 file changed, 328 insertions(+) diff --git a/drivers/base/test/platform-device-test.c b/drivers/base/test/platform-device-test.c index b0cc37d166ff..fd4f0e449089 100644 --- a/drivers/base/test/platform-device-test.c +++ b/drivers/base/test/platform-device-test.c @@ -421,10 +421,338 @@ static void platform_device_two_swnodes(struct kunit *test) "Expected errno == -EINVAL, got: %pe", pdev); } +/* + * Check that unregistering a child platform device sharing its parent's + * primary firmware node leaves the parent's secondary software node intact + * and its reference count balanced. + */ +static void platform_device_swnode_shared_primary(struct kunit *test) +{ + struct platform_device_info parent_info, child_info; + struct platform_device *parent, *child; + struct fwnode_handle *fwnode, *swnode; + + fwnode = kunit_kzalloc(test, sizeof(*fwnode), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fwnode); + fwnode_init(fwnode, NULL); + + parent_info = (struct platform_device_info){ + .name = DEVICE_NAME, + .id = 0, + .fwnode = fwnode, + .swnode = &platform_device_test_swnode, + }; + + parent = kunit_platform_device_register_full(test, &parent_info); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent); + + swnode = software_node_fwnode(&platform_device_test_swnode); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, swnode); + KUNIT_EXPECT_PTR_EQ(test, fwnode->secondary, swnode); + + child_info = (struct platform_device_info){ + .parent = &parent->dev, + .name = DEVICE_NAME, + .id = 1, + .fwnode = fwnode, + }; + + child = kunit_platform_device_register_full(test, &child_info); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, child); + + kunit_platform_device_unregister(test, child); + + KUNIT_EXPECT_PTR_EQ(test, fwnode->secondary, swnode); + KUNIT_EXPECT_PTR_EQ(test, software_node_fwnode(&platform_device_test_swnode), + swnode); + + kunit_platform_device_unregister(test, parent); + + KUNIT_EXPECT_PTR_EQ(test, fwnode->secondary, NULL); + KUNIT_EXPECT_PTR_EQ(test, software_node_fwnode(&platform_device_test_swnode), + NULL); + + parent = kunit_platform_device_register_full(test, &parent_info); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent); + kunit_platform_device_unregister(test, parent); +} + +/* + * Check that detaching a shared primary firmware node via set_primary_fwnode() + * before final release of a child device clears the child's fwnode without + * promoting or releasing the parent's secondary software node. + */ +static void platform_device_swnode_shared_primary_detached(struct kunit *test) +{ + struct platform_device_info parent_info, child_info; + struct platform_device *parent, *child; + struct fwnode_handle *fwnode, *swnode; + + fwnode = kunit_kzalloc(test, sizeof(*fwnode), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fwnode); + fwnode_init(fwnode, NULL); + + parent_info = (struct platform_device_info){ + .name = DEVICE_NAME, + .id = 0, + .fwnode = fwnode, + .swnode = &platform_device_test_swnode, + }; + + parent = kunit_platform_device_register_full(test, &parent_info); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent); + + swnode = software_node_fwnode(&platform_device_test_swnode); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, swnode); + + child_info = (struct platform_device_info){ + .parent = &parent->dev, + .name = DEVICE_NAME, + .id = 1, + .fwnode = fwnode, + }; + + child = kunit_platform_device_register_full(test, &child_info); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, child); + + /* Keep child alive past device_del() to simulate ACPI unbind before release. */ + get_device(&child->dev); + kunit_platform_device_unregister(test, child); + + set_primary_fwnode(&child->dev, NULL); + KUNIT_EXPECT_PTR_EQ(test, dev_fwnode(&child->dev), NULL); + KUNIT_EXPECT_PTR_EQ(test, fwnode->secondary, swnode); + + put_device(&child->dev); + + KUNIT_EXPECT_PTR_EQ(test, fwnode->secondary, swnode); + KUNIT_EXPECT_PTR_EQ(test, software_node_fwnode(&platform_device_test_swnode), + swnode); + + kunit_platform_device_unregister(test, parent); + + KUNIT_EXPECT_PTR_EQ(test, fwnode->secondary, NULL); + KUNIT_EXPECT_PTR_EQ(test, software_node_fwnode(&platform_device_test_swnode), + NULL); + + parent = kunit_platform_device_register_full(test, &parent_info); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent); + kunit_platform_device_unregister(test, parent); +} + +/* + * Check that unregistering a child platform device sharing a parent's primary + * firmware node with a managed secondary software node leaves the managed node + * attached and alive until the parent is unregistered. + */ +static void platform_device_managed_swnode_shared_primary(struct kunit *test) +{ + static const struct property_entry properties[] = { + PROPERTY_ENTRY_U32("foo", 42), + { } + }; + struct platform_device_info parent_info, child_info; + struct platform_device *parent, *child; + struct fwnode_handle *fwnode, *swnode; + u32 val = 0; + int ret; + + fwnode = kunit_kzalloc(test, sizeof(*fwnode), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fwnode); + fwnode_init(fwnode, NULL); + + parent_info = (struct platform_device_info){ + .name = DEVICE_NAME, + .id = 0, + .fwnode = fwnode, + .properties = properties, + }; + + parent = kunit_platform_device_register_full(test, &parent_info); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent); + + swnode = fwnode->secondary; + KUNIT_ASSERT_TRUE(test, is_software_node(swnode)); + + child_info = (struct platform_device_info){ + .parent = &parent->dev, + .name = DEVICE_NAME, + .id = 1, + .fwnode = fwnode, + }; + + child = kunit_platform_device_register_full(test, &child_info); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, child); + + kunit_platform_device_unregister(test, child); + + KUNIT_EXPECT_PTR_EQ(test, fwnode->secondary, swnode); + ret = fwnode_property_read_u32(swnode, "foo", &val); + KUNIT_EXPECT_EQ(test, ret, 0); + KUNIT_EXPECT_EQ(test, val, 42); + + child_info.id = 2; + child = kunit_platform_device_register_full(test, &child_info); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, child); + + get_device(&child->dev); + kunit_platform_device_unregister(test, child); + + set_primary_fwnode(&child->dev, NULL); + KUNIT_EXPECT_PTR_EQ(test, dev_fwnode(&child->dev), NULL); + KUNIT_EXPECT_PTR_EQ(test, fwnode->secondary, swnode); + + put_device(&child->dev); + + val = 0; + KUNIT_EXPECT_PTR_EQ(test, fwnode->secondary, swnode); + ret = fwnode_property_read_u32(swnode, "foo", &val); + KUNIT_EXPECT_EQ(test, ret, 0); + KUNIT_EXPECT_EQ(test, val, 42); + + kunit_platform_device_unregister(test, parent); + + KUNIT_EXPECT_PTR_EQ(test, fwnode->secondary, NULL); +} + +/* + * Check that a child platform device owning its own secondary software node on + * a shared parent primary firmware node detaches and releases that secondary + * software node when set_primary_fwnode(..., NULL) is called before release. + */ +static void platform_device_swnode_child_owned_on_shared_primary(struct kunit *test) +{ + struct platform_device_info parent_info, child_info; + struct platform_device *parent, *child; + struct fwnode_handle *fwnode, *swnode; + + fwnode = kunit_kzalloc(test, sizeof(*fwnode), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fwnode); + fwnode_init(fwnode, NULL); + + parent_info = (struct platform_device_info){ + .name = DEVICE_NAME, + .id = 0, + .fwnode = fwnode, + }; + + parent = kunit_platform_device_register_full(test, &parent_info); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent); + + child_info = (struct platform_device_info){ + .parent = &parent->dev, + .name = DEVICE_NAME, + .id = 1, + .fwnode = fwnode, + .swnode = &platform_device_test_swnode, + }; + + child = kunit_platform_device_register_full(test, &child_info); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, child); + + swnode = software_node_fwnode(&platform_device_test_swnode); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, swnode); + KUNIT_EXPECT_PTR_EQ(test, fwnode->secondary, swnode); + + get_device(&child->dev); + kunit_platform_device_unregister(test, child); + + set_primary_fwnode(&child->dev, NULL); + KUNIT_EXPECT_PTR_EQ(test, fwnode->secondary, NULL); + KUNIT_EXPECT_PTR_EQ(test, dev_fwnode(&child->dev), swnode); + + put_device(&child->dev); + + KUNIT_EXPECT_PTR_EQ(test, software_node_fwnode(&platform_device_test_swnode), + NULL); + + child = kunit_platform_device_register_full(test, &child_info); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, child); + + kunit_platform_device_unregister(test, child); + kunit_platform_device_unregister(test, parent); +} + +/* + * Check that releasing an unregistered platform device with a managed + * secondary software node detaches and frees the managed node cleanly. + */ +static void platform_device_managed_swnode_unregistered_release(struct kunit *test) +{ + static const struct property_entry properties[] = { + PROPERTY_ENTRY_U32("foo", 42), + { } + }; + struct platform_device *pdev; + struct fwnode_handle *fwnode; + int ret; + + fwnode = kunit_kzalloc(test, sizeof(*fwnode), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fwnode); + fwnode_init(fwnode, NULL); + + pdev = platform_device_alloc(DEVICE_NAME, PLATFORM_DEVID_NONE); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev); + + platform_device_set_fwnode(pdev, fwnode); + ret = device_create_managed_software_node(&pdev->dev, properties, NULL); + if (ret) + platform_device_put(pdev); + KUNIT_ASSERT_EQ(test, ret, 0); + + KUNIT_EXPECT_TRUE(test, is_software_node(fwnode->secondary)); + + platform_device_put(pdev); + + KUNIT_EXPECT_PTR_EQ(test, fwnode->secondary, NULL); +} + +/* + * Check that calling device_remove_software_node() on a registered platform + * device with a managed secondary software node detaches the node cleanly and + * subsequent unregistration does not double-put it. + */ +static void platform_device_managed_swnode_registered_remove(struct kunit *test) +{ + static const struct property_entry properties[] = { + PROPERTY_ENTRY_U32("foo", 42), + { } + }; + struct platform_device_info pdevinfo; + struct platform_device *pdev; + struct fwnode_handle *fwnode; + + fwnode = kunit_kzalloc(test, sizeof(*fwnode), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fwnode); + fwnode_init(fwnode, NULL); + + pdevinfo = (struct platform_device_info){ + .name = DEVICE_NAME, + .id = PLATFORM_DEVID_NONE, + .fwnode = fwnode, + .properties = properties, + }; + + pdev = kunit_platform_device_register_full(test, &pdevinfo); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev); + KUNIT_ASSERT_TRUE(test, is_software_node(fwnode->secondary)); + + device_remove_software_node(&pdev->dev); + KUNIT_EXPECT_PTR_EQ(test, fwnode->secondary, NULL); + + kunit_platform_device_unregister(test, pdev); +} + static struct kunit_case platform_device_swnode_tests[] = { KUNIT_CASE(platform_device_swnode_add_twice), KUNIT_CASE(platform_device_swnode_as_primary), KUNIT_CASE(platform_device_two_swnodes), + KUNIT_CASE(platform_device_swnode_shared_primary), + KUNIT_CASE(platform_device_swnode_shared_primary_detached), + KUNIT_CASE(platform_device_managed_swnode_shared_primary), + KUNIT_CASE(platform_device_swnode_child_owned_on_shared_primary), + KUNIT_CASE(platform_device_managed_swnode_unregistered_release), + KUNIT_CASE(platform_device_managed_swnode_registered_remove), {} }; -- 2.56.1.915.g69d5ec7298-goog ^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH 0/3] driver core: swnode: fix release with shared primary fwnode 2026-12-03 1:34 [PATCH 0/3] driver core: swnode: fix release with shared primary fwnode Dmitry Torokhov ` (2 preceding siblings ...) 2026-12-03 1:34 ` [PATCH 3/3] driver core: platform: tests: add shared primary fwnode swnode tests Dmitry Torokhov @ 2026-12-03 11:35 ` Andy Shevchenko 2026-12-05 21:00 ` Cesar Cibrian 4 siblings, 0 replies; 9+ messages in thread From: Andy Shevchenko @ 2026-12-03 11:35 UTC (permalink / raw) To: Dmitry Torokhov Cc: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich, Johan Hovold, Daniel Scally, Heikki Krogerus, Sakari Ailus, Bartosz Golaszewski, driver-core, linux-acpi, linux-kernel, stable On Wed, Dec 02, 2026 at 05:34:48PM -0800, Dmitry Torokhov wrote: ... > This series is intentionally kept minimal for stable backporting. > Storing the secondary pointer directly on the shared struct > fwnode_handle still leaves broader limitations unaddressed: > set_primary_fwnode() only detects direct parent sharing (not grandparent > or sibling sharing), parent and child or sibling devices sharing a > primary node still contend for a single secondary slot, and cross-device > lookups of fwnode->secondary are not synchronized against concurrent > removal. A separate follow-up (such as an immutable composite > fwnode_handle) would be needed to address those cleanly. Yeah, shared fwnodes is PITA. The proper solution is to have a proper list of the fwnodes (and not just primary + secondary) and have another list of shared devices. With that the tracking is a matter of atomic add and delete a device from the respective list. Thanks for the series, I have looked into the series (not carefully into the test cases but I always welcome them), Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> -- With Best Regards, Andy Shevchenko ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH 0/3] driver core: swnode: fix release with shared primary fwnode 2026-12-03 1:34 [PATCH 0/3] driver core: swnode: fix release with shared primary fwnode Dmitry Torokhov ` (3 preceding siblings ...) 2026-12-03 11:35 ` [PATCH 0/3] driver core: swnode: fix release with shared primary fwnode Andy Shevchenko @ 2026-12-05 21:00 ` Cesar Cibrian 4 siblings, 0 replies; 9+ messages in thread From: Cesar Cibrian @ 2026-12-05 21:00 UTC (permalink / raw) To: Dmitry Torokhov, Greg Kroah-Hartman, Rafael J . Wysocki, Danilo Krummrich Cc: Johan Hovold, Andy Shevchenko, Daniel Scally, Heikki Krogerus, Sakari Ailus, Bartosz Golaszewski, driver-core, linux-acpi, linux-kernel Hi Dmitry, Tested on an Intel LPSS / i2c-designware system, with the mfd-core software node removal reordered after platform_device_unregister() on top, across 10 intel-lpss + 3 i2c_designware unbind/bind cycles, as well as the platform-device-swnode, property-entry and software-node-links KUnit suites. All passed cleanly, with no sysfs duplicate-filename warnings and no -EBUSY on rebind. Tested-by: Cesar Cibrian <cesarcibrian.ai@gmail.com> Thanks, Cesar ^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2026-12-06 15:50 UTC | newest] Thread overview: 9+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-12-03 1:34 [PATCH 0/3] driver core: swnode: fix release with shared primary fwnode Dmitry Torokhov 2026-12-03 1:34 ` [PATCH 1/3] driver core: swnode: only release owned secondary software nodes Dmitry Torokhov 2026-12-05 23:03 ` Danilo Krummrich 2026-12-06 7:07 ` Dmitry Torokhov 2026-12-06 15:50 ` Danilo Krummrich 2026-12-03 1:34 ` [PATCH 2/3] software node: replace owner pointer with managed bitfield Dmitry Torokhov 2026-12-03 1:34 ` [PATCH 3/3] driver core: platform: tests: add shared primary fwnode swnode tests Dmitry Torokhov 2026-12-03 11:35 ` [PATCH 0/3] driver core: swnode: fix release with shared primary fwnode Andy Shevchenko 2026-12-05 21:00 ` Cesar Cibrian
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox