On Wed, Dec 02, 2026 at 10:45:51PM -0800, Dmitry Torokhov wrote: > cros_ec_gpio_probe() attaches the underlying EC protocol device's > firmware node to the child platform device using device_set_node() > without taking a reference. On teardown, platform_device_release() drops > a firmware node reference and removes any attached software node, > underflowing the protocol device's reference count and prematurely > tearing down an attached software node. > > Take a reference with fwnode_handle_get() when attaching the protocol > device's firmware node if the platform device does not already have one, > and register cros_ec_gpio_put_fwnode() via devm_add_action_or_reset() > before devm_gpiochip_add_data() to clear the platform device's node and > drop the reference. Because devres runs in LIFO order, the GPIO chip and > acpi_gpiochip_remove() on the parent device finish before the node is > cleared, and device_del() unbinds the driver before > device_platform_notify_remove() and platform_device_release(). > > This preserves ACPI companion attachment for acpi_gpiochip_add(), which > inspects the parent device while cros-ec-dev does not propagate it, > while gpio_chip.fwnode is also assigned explicitly. > > Fixes: f837fe1bffe6 ("gpio: Add ChromeOS EC GPIO driver") > Cc: stable@vger.kernel.org > Signed-off-by: Dmitry Torokhov Reviewed-by: Benson Leung > --- > drivers/gpio/gpio-cros-ec.c | 26 ++++++++++++++++++++++++-- > 1 file changed, 24 insertions(+), 2 deletions(-) > > diff --git a/drivers/gpio/gpio-cros-ec.c b/drivers/gpio/gpio-cros-ec.c > index d06d74bd86d8..4a77ff3acd54 100644 > --- a/drivers/gpio/gpio-cros-ec.c > +++ b/drivers/gpio/gpio-cros-ec.c > @@ -155,18 +155,39 @@ static int cros_ec_gpio_ngpios(struct cros_ec_device *cros_ec) > return response.get_count.val; > } > > +static void cros_ec_gpio_put_fwnode(void *data) > +{ > + struct device *dev = data; > + struct fwnode_handle *fwnode = dev_fwnode(dev); > + > + device_set_node(dev, NULL); > + fwnode_handle_put(fwnode); > +} > + > static int cros_ec_gpio_probe(struct platform_device *pdev) > { > struct device *dev = &pdev->dev; > struct device *parent = dev->parent; > struct cros_ec_dev *ec_dev = dev_get_drvdata(parent); > struct cros_ec_device *cros_ec = ec_dev->ec_dev; > + struct fwnode_handle *fwnode = dev_fwnode(cros_ec->dev); > struct gpio_chip *gc; > int ngpios; > int ret; > > - /* Use the fwnode from the protocol device, e.g. cros-ec-spi */ > - device_set_node(dev, dev_fwnode(cros_ec->dev)); > + if (!dev_fwnode(dev) && fwnode) { > + /* > + * Use the fwnode from the protocol device, e.g. cros-ec-spi, > + * so acpi_gpiochip_add() sees its ACPI companion. Clear it on > + * unbind so platform_device_release() does not drop a parent > + * reference or tear down a shared software node. > + */ > + device_set_node(dev, fwnode_handle_get(fwnode)); > + ret = devm_add_action_or_reset(dev, cros_ec_gpio_put_fwnode, > + dev); > + if (ret) > + return ret; > + } > > ngpios = cros_ec_gpio_ngpios(cros_ec); > if (ngpios < 0) { > @@ -180,6 +201,7 @@ static int cros_ec_gpio_probe(struct platform_device *pdev) > > gc->ngpio = ngpios; > gc->parent = dev; > + gc->fwnode = fwnode; > ret = cros_ec_gpio_init_names(cros_ec, gc); > if (ret) > return ret; > > -- > 2.56.1.915.g69d5ec7298-goog > >