Re: [PATCH v3 4/8] software node: ignore primary nodes in device_remove_software_node()
From: Johan Hovold
Date: Wed Nov 18 2026 - 04:36:56 EST
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:
> > device_remove_software_node() should only be used to remove secondary
> > firmware nodes added by device_add_software_node() but will currently
> > drop a reference also if a device has a primary software node.
> >
> > Bail out early if device_remove_software_node() is called for a device
> > whose primary firmware node is a software node and fix up the two
> > callers that currently work around this themselves.
>
> ...
>
> > +++ 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).
Johan