Linux GPIO subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Dmitry Torokhov" <dmitry.torokhov@gmail.com>
Cc: linux-gpio@vger.kernel.org
Subject: Re: [PATCH 6/6] gpio: cros-ec: Fix firmware node reference count underflow
Date: Thu, 03 Dec 2026 07:11:39 +0000	[thread overview]
Message-ID: <sashiko-outbox-247270@kernel.org> (raw)
In-Reply-To: <20261202-gpio-arizona-fwnode-fix-v1-6-3899179bf9d1@gmail.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] gpio: cros-ec: unsynchronized device_set_node() mutation on
  live device
--

commit f1ff3a2fe4430374ca0d36040d3e5873f66a4112
Author: Dmitry Torokhov <dmitry.torokhov@gmail.com>

gpio: cros-ec: Fix firmware node reference count underflow

This commit takes a firmware node reference when attaching the EC protocol
device's fwnode to the cros-ec-gpio platform device, and registers a devm
action to clear it and drop the reference on unbind.  It also sets
gc->fwnode explicitly.

> diff --git a/drivers/gpio/gpio-cros-ec.c b/drivers/gpio/gpio-cros-ec.c
> index d06d74bd86d88..4a77ff3acd54b 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;
> +	}

[Severity: Medium]
Can mutating dev->fwnode and dev->of_node with device_set_node() here race
with lockless sysfs readers on the live platform_device?

The cros-ec-gpio platform_device is already registered in sysfs during
probe, and remains live during driver unbind or probe failure when
cros_ec_gpio_put_fwnode() clears dev->fwnode and dev->of_node to NULL.
Both paths only hold device_lock(dev) and perform plain stores.

Sysfs uevent readers access dev->of_node without device_lock(dev) or
READ_ONCE().  On DT systems, clearing dev->of_node while a reader is in
of_device_uevent_modalias() triggers a KCSAN data race and lets the reader
reload NULL after its initial check:

CPU0                                CPU1
uevent_show()
  ...
  of_device_uevent_modalias()
    if (!dev->of_node) /* false */
    add_uevent_var(env, "MODALIAS=")
                                    unbind_store()
                                      ...
                                      cros_ec_gpio_put_fwnode()
                                        device_set_node(dev, NULL)
    of_modalias(dev->of_node, ...)  <-- reloads NULL

Passing NULL to of_modalias() emits a malformed MODALIAS=of:N(null)T(null)
uevent entry (and of_device_uevent() similarly emits OF_FULLNAME=(null)).

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202-gpio-arizona-fwnode-fix-v1-0-3899179bf9d1@gmail.com?part=6

      reply	other threads:[~2026-12-03  7:11 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03  6:45 [PATCH 0/6] gpio: fix firmware node reference count underflows Dmitry Torokhov
2026-12-03  6:45 ` [PATCH 1/6] gpio: arizona: Fix firmware node reference count underflow Dmitry Torokhov
2026-12-03  6:45 ` [PATCH 2/6] gpio: tps6586x: " Dmitry Torokhov
2026-12-03  6:45 ` [PATCH 3/6] gpio: tps65910: " Dmitry Torokhov
2026-12-03  6:45 ` [PATCH 4/6] gpio: twl6040: " Dmitry Torokhov
2026-12-03  6:45 ` [PATCH 5/6] gpio: wm831x: " Dmitry Torokhov
2026-12-03  6:45 ` [PATCH 6/6] gpio: cros-ec: " Dmitry Torokhov
2026-12-03  7:11   ` sashiko-bot [this message]

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=sashiko-outbox-247270@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dmitry.torokhov@gmail.com \
    --cc=linux-gpio@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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