Re: [PATCH] software node: make swnodes hold their own references on parents
From: Andy Shevchenko
Date: Mon Nov 09 2026 - 14:57:43 EST
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