From: "Danilo Krummrich" <dakr@kernel.org>
To: "Dmitry Torokhov" <dmitry.torokhov@gmail.com>
Cc: "Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
"Rafael J. Wysocki" <rafael@kernel.org>,
"Johan Hovold" <johan@kernel.org>,
"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>,
<driver-core@lists.linux.dev>, <linux-acpi@vger.kernel.org>,
<linux-kernel@vger.kernel.org>, <stable@vger.kernel.org>
Subject: Re: [PATCH 1/3] driver core: swnode: only release owned secondary software nodes
Date: Sun, 06 Dec 2026 16:50:50 +0100 [thread overview]
Message-ID: <DNDRPLVWFR5X.3UDRJJJLFMU5D@kernel.org> (raw)
In-Reply-To: <052097db57228acd881b7684032e7eb0@gmail.com>
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.
next prev parent reply other threads:[~2026-12-06 15:50 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
2026-12-03 11:35 ` [PATCH 0/3] driver core: swnode: fix release with shared primary fwnode Andy Shevchenko
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=DNDRPLVWFR5X.3UDRJJJLFMU5D@kernel.org \
--to=dakr@kernel.org \
--cc=andriy.shevchenko@linux.intel.com \
--cc=brgl@kernel.org \
--cc=djrscally@gmail.com \
--cc=dmitry.torokhov@gmail.com \
--cc=driver-core@lists.linux.dev \
--cc=gregkh@linuxfoundation.org \
--cc=heikki.krogerus@linux.intel.com \
--cc=johan@kernel.org \
--cc=linux-acpi@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=rafael@kernel.org \
--cc=sakari.ailus@linux.intel.com \
--cc=stable@vger.kernel.org \
/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