* [PATCH] software node: make swnodes hold their own references on parents
@ 2026-11-06 11:13 Bartosz Golaszewski
2026-11-09 19:52 ` Andy Shevchenko
2026-11-17 12:10 ` Johan Hovold
0 siblings, 2 replies; 4+ messages in thread
From: Bartosz Golaszewski @ 2026-11-06 11:13 UTC (permalink / raw)
To: Andy Shevchenko, Daniel Scally, Heikki Krogerus, Sakari Ailus,
Bartosz Golaszewski, Greg Kroah-Hartman, Rafael J. Wysocki,
Danilo Krummrich, Johan Hovold, Linus Walleij
Cc: Rafael J. Wysocki, linux-acpi, driver-core, linux-kernel, stable,
Bartosz Golaszewski
In swnode_register(), swnode->parent is assigned before calling
kobject_init_and_add(), which normally acquires a reference on
parent->kobj via kobject_add_internal() that is held until after
software_node_release() completes. However, if kobject_init_and_add()
fails before acquiring the parent reference or drops it on error in
kobject_add_internal(), swnode->kobj holds no reference on parent->kobj
while swnode->parent remains set.
With CONFIG_DEBUG_KOBJECT_RELEASE enabled, the random delay in
kobject_delayed_cleanup() may create a situation where - when
unregistering the child node and then the parent - the parent object
gets released before the child who's left with a dangling pointer.
Acquire a refernce to the parent swnode when registering the child and
drop it on child's release.
Cc: stable@vger.kernel.org
Fixes: 59abd83672f7 ("drivers: base: Introducing software nodes to the firmware node framework")
Closes: https://sashiko.dev/#/bug/linux-82933c14-d212-49f1-bd2f-d4ab1e16fed7
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
---
drivers/base/swnode.c | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)
diff --git a/drivers/base/swnode.c b/drivers/base/swnode.c
index 84a77ffa3dc4bbcca62bbe67c3390693500b01ea..986dcb933d2c9274e9000968f115d7fccc2d103f 100644
--- a/drivers/base/swnode.c
+++ b/drivers/base/swnode.c
@@ -843,11 +843,12 @@ static void software_node_free(const struct software_node *node)
static void software_node_release(struct kobject *kobj)
{
struct swnode *swnode = kobj_to_swnode(kobj);
+ struct swnode *parent = swnode->parent;
fwnode_links_purge(&swnode->fwnode);
- if (swnode->parent) {
- ida_free(&swnode->parent->child_ids, swnode->id);
+ if (parent) {
+ ida_free(&parent->child_ids, swnode->id);
list_del(&swnode->entry);
} else {
ida_free(&swnode_root_ids, swnode->id);
@@ -858,6 +859,9 @@ static void software_node_release(struct kobject *kobj)
ida_destroy(&swnode->child_ids);
kfree(swnode);
+
+ if (parent)
+ swnode_put(parent);
}
static const struct kobj_type software_node_type = {
@@ -885,7 +889,7 @@ swnode_register(const struct software_node *node, struct swnode *parent,
swnode->id = ret;
swnode->node = node;
- swnode->parent = parent;
+ swnode->parent = parent ? swnode_get(parent) : NULL;
swnode->kobj.kset = swnode_kset;
fwnode_init(&swnode->fwnode, &software_node_ops);
---
base-commit: ddfe51b0a0f5dc9e415cc2c8ad8db17532dd426b
change-id: 20261106-swnode-kobject-parent-ref-3bd9b9b75aeb
Best regards,
--
Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] software node: make swnodes hold their own references on parents
2026-11-06 11:13 [PATCH] software node: make swnodes hold their own references on parents Bartosz Golaszewski
@ 2026-11-09 19:52 ` Andy Shevchenko
2026-11-10 8:57 ` Bartosz Golaszewski
2026-11-17 12:10 ` Johan Hovold
1 sibling, 1 reply; 4+ messages in thread
From: Andy Shevchenko @ 2026-11-09 19:52 UTC (permalink / raw)
To: Bartosz Golaszewski
Cc: Daniel Scally, Heikki Krogerus, Sakari Ailus, Bartosz Golaszewski,
Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
Johan Hovold, Linus Walleij, Rafael J. Wysocki, linux-acpi,
driver-core, linux-kernel, stable
On Fri, Nov 06, 2026 at 12:13:28PM +0100, Bartosz Golaszewski wrote:
> In swnode_register(), swnode->parent is assigned before calling
> kobject_init_and_add(), which normally acquires a reference on
> parent->kobj via kobject_add_internal() that is held until after
> software_node_release() completes. However, if kobject_init_and_add()
> fails before acquiring the parent reference or drops it on error in
> kobject_add_internal(), swnode->kobj holds no reference on parent->kobj
> while swnode->parent remains set.
>
> With CONFIG_DEBUG_KOBJECT_RELEASE enabled, the random delay in
> kobject_delayed_cleanup() may create a situation where - when
> unregistering the child node and then the parent - the parent object
> gets released before the child who's left with a dangling pointer.
>
> Acquire a refernce to the parent swnode when registering the child and
> drop it on child's release.
...
> + if (parent)
> + swnode_put(parent);
This will be the second case of potentially NULL being passed to swnode_put().
Since it's a static internal API, I would just add a check there and hope that
compiler will inline and that will be optimized away on the cases when the
parameter is known not to be NULL.
Maybe in a separate change.
...
> - swnode->parent = parent;
> + swnode->parent = parent ? swnode_get(parent) : NULL;
For the consistency's sake, same to swnode_get().
Note, that it also has a case in the current code. So with that patch it will
be two users of each.
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] software node: make swnodes hold their own references on parents
2026-11-09 19:52 ` Andy Shevchenko
@ 2026-11-10 8:57 ` Bartosz Golaszewski
0 siblings, 0 replies; 4+ messages in thread
From: Bartosz Golaszewski @ 2026-11-10 8:57 UTC (permalink / raw)
To: Andy Shevchenko
Cc: Bartosz Golaszewski, Daniel Scally, Heikki Krogerus, Sakari Ailus,
Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
Johan Hovold, Linus Walleij, Rafael J. Wysocki, linux-acpi,
driver-core, linux-kernel, stable
On Mon, Nov 09, 2026 at 09:52:12PM +0200, Andy Shevchenko wrote:
> On Fri, Nov 06, 2026 at 12:13:28PM +0100, Bartosz Golaszewski wrote:
> > In swnode_register(), swnode->parent is assigned before calling
> > kobject_init_and_add(), which normally acquires a reference on
> > parent->kobj via kobject_add_internal() that is held until after
> > software_node_release() completes. However, if kobject_init_and_add()
> > fails before acquiring the parent reference or drops it on error in
> > kobject_add_internal(), swnode->kobj holds no reference on parent->kobj
> > while swnode->parent remains set.
> >
> > With CONFIG_DEBUG_KOBJECT_RELEASE enabled, the random delay in
> > kobject_delayed_cleanup() may create a situation where - when
> > unregistering the child node and then the parent - the parent object
> > gets released before the child who's left with a dangling pointer.
> >
> > Acquire a refernce to the parent swnode when registering the child and
> > drop it on child's release.
>
> ...
>
> > + if (parent)
> > + swnode_put(parent);
>
> This will be the second case of potentially NULL being passed to swnode_put().
> Since it's a static internal API, I would just add a check there and hope that
> compiler will inline and that will be optimized away on the cases when the
> parameter is known not to be NULL.
>
> Maybe in a separate change.
>
Yes, I actually added it to my TODO list but this is clearly a fix, so I
wanted to keep it small.
Bart
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] software node: make swnodes hold their own references on parents
2026-11-06 11:13 [PATCH] software node: make swnodes hold their own references on parents Bartosz Golaszewski
2026-11-09 19:52 ` Andy Shevchenko
@ 2026-11-17 12:10 ` Johan Hovold
1 sibling, 0 replies; 4+ messages in thread
From: Johan Hovold @ 2026-11-17 12:10 UTC (permalink / raw)
To: Bartosz Golaszewski
Cc: Andy Shevchenko, Daniel Scally, Heikki Krogerus, Sakari Ailus,
Bartosz Golaszewski, Greg Kroah-Hartman, Rafael J. Wysocki,
Danilo Krummrich, Linus Walleij, Rafael J. Wysocki, linux-acpi,
driver-core, linux-kernel, stable
On Fri, Nov 06, 2026 at 12:13:28PM +0100, Bartosz Golaszewski wrote:
> In swnode_register(), swnode->parent is assigned before calling
> kobject_init_and_add(), which normally acquires a reference on
> parent->kobj via kobject_add_internal() that is held until after
> software_node_release() completes. However, if kobject_init_and_add()
> fails before acquiring the parent reference or drops it on error in
> kobject_add_internal(), swnode->kobj holds no reference on parent->kobj
> while swnode->parent remains set.
>
> With CONFIG_DEBUG_KOBJECT_RELEASE enabled, the random delay in
> kobject_delayed_cleanup() may create a situation where - when
> unregistering the child node and then the parent - the parent object
> gets released before the child who's left with a dangling pointer.
>
> Acquire a refernce to the parent swnode when registering the child and
> drop it on child's release.
>
> Cc: stable@vger.kernel.org
> Fixes: 59abd83672f7 ("drivers: base: Introducing software nodes to the firmware node framework")
> Closes: https://sashiko.dev/#/bug/linux-82933c14-d212-49f1-bd2f-d4ab1e16fed7
> Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
> ---
> drivers/base/swnode.c | 10 +++++++---
> 1 file changed, 7 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/base/swnode.c b/drivers/base/swnode.c
> index 84a77ffa3dc4bbcca62bbe67c3390693500b01ea..986dcb933d2c9274e9000968f115d7fccc2d103f 100644
> --- a/drivers/base/swnode.c
> +++ b/drivers/base/swnode.c
> @@ -843,11 +843,12 @@ static void software_node_free(const struct software_node *node)
> static void software_node_release(struct kobject *kobj)
> {
> struct swnode *swnode = kobj_to_swnode(kobj);
> + struct swnode *parent = swnode->parent;
>
> fwnode_links_purge(&swnode->fwnode);
>
> - if (swnode->parent) {
> - ida_free(&swnode->parent->child_ids, swnode->id);
> + if (parent) {
> + ida_free(&parent->child_ids, swnode->id);
> list_del(&swnode->entry);
Why not simply
swnode_put(swnode->parent);
here?
> } else {
> ida_free(&swnode_root_ids, swnode->id);
> @@ -858,6 +859,9 @@ static void software_node_release(struct kobject *kobj)
>
> ida_destroy(&swnode->child_ids);
> kfree(swnode);
> +
> + if (parent)
> + swnode_put(parent);
> }
Johan
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-17 12:10 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-06 11:13 [PATCH] software node: make swnodes hold their own references on parents Bartosz Golaszewski
2026-11-09 19:52 ` Andy Shevchenko
2026-11-10 8:57 ` Bartosz Golaszewski
2026-11-17 12:10 ` Johan Hovold
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox