Re: [PATCH v3 4/8] software node: ignore primary nodes in device_remove_software_node()

From: Andy Shevchenko

Date: Wed Nov 18 2026 - 05:39:50 EST


On Wed, Nov 18, 2026 at 10:36:50AM +0100, Johan Hovold wrote:
> On Tue, Nov 17, 2026 at 04:01:50PM +0200, Andy Shevchenko wrote:
> > On Tue, Nov 17, 2026 at 11:13:32AM +0100, Johan Hovold wrote:

...

> > > +++ b/drivers/i2c/i2c-core-base.c
> >
> > > void i2c_unregister_device(struct i2c_client *client)
> > > {
> > > struct fwnode_handle *fwnode;
> > > - bool fwnode_is_software_node;
> > >
> > > if (IS_ERR_OR_NULL(client))
> > > return;
> >
> > > - fwnode_is_software_node = is_software_node(fwnode);
> > > device_remove_software_node(&client->dev);
> > > /*
> > > - * If the firmware node is a software node, its reference count
> > > - * was decremented by device_remove_software_node().
> > > + * Must be done after device_remove_software_node() which may have
> > > + * freed a secondary firmware node and cleared the fwnode pointer.
> > > */
> > > - if (!fwnode_is_software_node)
> > > - fwnode_handle_put(fwnode);
> > > + fwnode_handle_put(client->dev.fwnode);
> >
> > We have fwnode getter, so better use it
> >
> > fwnode_handle_put(dev_fwnode(&client->dev));
>
> I used the fwnode pointer directly here on purpose since that is the
> pointer that gets cleared by device_remove_software_node() and which the
> comment refers to.
>
> This hopefully also reduces the risk of anyone introducing a UAF by
> reusing the fwnode variable (or the compiler doing something silly since
> dev_fwnode() returns the dev->of_node->fwnode when CONFIG_OF is
> enabled).

This makes explicit use of the member outside of driver core. It adds
an unneeded burden on the possible changes in the future if we decide
to make the fwnode stuff being hidden in struct device (in a sense
of having say list head instead of that). I think we really need to
mark fwnode with __private there.

--
With Best Regards,
Andy Shevchenko