All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Adrian Ambrozewicz" <aambrozewicz@nvidia.com>
Cc: linux-gpio@vger.kernel.org
Subject: Re: [PATCH v2 3/3] gpiolib: release user space's lines when adding the chip fails late
Date: Fri, 27 Nov 2026 08:08:34 +0000	[thread overview]
Message-ID: <sashiko-outbox-236867@kernel.org> (raw)
In-Reply-To: <20261127-gpiolib-ownlines-v2-3-aambrozewicz@nvidia.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] gpiolib: race in gpiochip_add_data_with_key() freeing lines
  before list removal
--

commit f343bc6358487260064149695890aadd91c20f07
Author: Adrian Ambrozewicz <aambrozewicz@nvidia.com>

gpiolib: release user space's lines when adding the chip fails late

This commit updates the error path of gpiochip_add_data_with_key() when
gpiochip_setup_dev() fails so that lines owned by the chip and user space
are released and the device is numbed, matching gpiochip_remove().

> diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
> index 1b92ace14a5aa..6d3531f5833e7 100644
> --- a/drivers/gpio/gpiolib.c
> +++ b/drivers/gpio/gpiolib.c
> @@ -1419,13 +1419,19 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
>  	if (gpiolib_initialized) {
>  		ret = gpiochip_setup_dev(gc);
>  		if (ret)
> -			goto err_teardown_shared;
> +			goto err_release_lines;
>  	}
>  
>  	return 0;
>  
> -err_teardown_shared:
> -	gpio_device_teardown_shared(gdev);
> +err_release_lines:
> +	/* The character device may have been live: user space may hold lines. */
> +	gpiochip_free_own_lines(gc);
> +	gpiochip_numb_device(gc);
> +	gpiochip_irqchip_remove(gc);
> +	gpiochip_remove_pin_ranges(gc);
> +	/* What the labels down to err_remove_of_chip undo is done above. */
> +	goto err_remove_of_chip;

[Severity: Medium]
Can calling gpiochip_free_own_lines() and gpiochip_numb_device() here
before err_remove_from_list unlinks gdev->list and runs
synchronize_srcu(&gpio_devices_srcu) race with concurrent consumer lookups?

In gpiochip_remove() (after commit "gpiolib: release the chip's own lines
before numbing it on removal"), gdev is removed from gpio_devices and
gpio_devices_srcu is synchronized before gpiochip_free_own_lines() runs.
Here, if gcdev_register() succeeds (marking gdev->dev registered) and
gpiochip_sysfs_register() fails, an in-flight reader in
gpiod_find_and_request() can still run concurrently with
gpiochip_free_own_lines():

CPU0                                    CPU1
gpiochip_add_data_with_key()
  gpiochip_setup_dev()
    gcdev_register()
                                        gpiod_find_and_request()
                                          scoped_guard(srcu, &gpio_devices_srcu)
                                          desc = gpiod_fwnode_lookup(...)
    gpiochip_sysfs_register() -> fails
    gcdev_unregister()
  gpiochip_free_own_lines()
    gpio_device_teardown_shared()
                                          gpio_shared_add_proxy_lookup()
                                            lock(&ref->lock)
                                            ref->lookup = lookup
      gpiod_remove_lookup_table(ref->lookup)
      kfree(ref->lookup)
                                            gpiod_add_lookup_table(ref->lookup)
                                              <-- use-after-free

gpio_device_teardown_shared() unlinks and frees ref->lookup without
holding ref->lock, relying on gpio_devices_srcu having already drained
gpiod_find_and_request() callers; depending on the interleaving, this can
also dereference an uninitialized lookup->list in list_del() or leave a
lookup table leaked on gpio_lookup_list.  In addition, while gdev remains
on gpio_devices before gpiochip_numb_device() clears gdev->chip, in-flight
readers and lookups that do not check device_is_registered(&gdev->dev)
(such as gpio_to_desc() and gpio_name_to_desc()) can request hogged or
shared lines just freed by gpiochip_free_own_lines().

Removing gdev from gpio_devices under gpio_devices_lock and calling
synchronize_srcu(&gpio_devices_srcu) before gpiochip_free_own_lines(), as
gpiochip_remove() does, would close this window.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261127-gpiolib-ownlines-v2-0-aambrozewicz@nvidia.com?part=3

  reply	other threads:[~2026-11-27  8:08 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27  7:49 [PATCH v2 0/3] gpiolib: free the lines a chip owns before numbing it Adrian Ambrozewicz
2026-11-27  7:49 ` [PATCH v2 1/3] gpiolib: shared: remove the proxies before releasing the shared line Adrian Ambrozewicz
2026-11-27  7:49 ` [PATCH v2 2/3] gpiolib: release the chip's own lines before numbing it on removal Adrian Ambrozewicz
2026-11-27 11:46   ` Andy Shevchenko
2026-11-27 12:44     ` Adrian Ambrozewicz
2026-11-27 15:42       ` Andy Shevchenko
2026-11-30 10:16         ` Adrian Ambrozewicz
2026-11-27  7:49 ` [PATCH v2 3/3] gpiolib: release user space's lines when adding the chip fails late Adrian Ambrozewicz
2026-11-27  8:08   ` sashiko-bot [this message]
2026-11-27 12:44     ` Adrian Ambrozewicz

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-236867@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=aambrozewicz@nvidia.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.