Re: [PATCH v3 4/8] software node: ignore primary nodes in device_remove_software_node()
From: Johan Hovold
Date: Mon Nov 23 2026 - 03:31:37 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:
> > > 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).
I've convinced myself that the compiler can't mess this up so I'll
switch to dev_fwnode() in both places when applying.
Johan