From: Tzung-Bi Shih <tzungbi@kernel.org>
To: Bartosz Golaszewski <brgl@kernel.org>
Cc: Jonathan Corbet <corbet@lwn.net>,
Shuah Khan <skhan@linuxfoundation.org>,
Randy Dunlap <rdunlap@infradead.org>,
Linus Walleij <linusw@kernel.org>,
driver-core@lists.linux.dev, linux-doc@vger.kernel.org,
linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org,
Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
"Rafael J. Wysocki" <rafael@kernel.org>,
Danilo Krummrich <dakr@kernel.org>
Subject: Re: [PATCH v12 3/3] gpio: Leverage revocable for accessing struct gpio_chip
Date: Sat, 12 Sep 2026 20:38:56 +0800 [thread overview]
Message-ID: <aqVH4Fl7pew8PKf1@tzungbi-laptop> (raw)
In-Reply-To: <CAMRc=McssNYy09bnzUpf9LEB94UOWimyVZNOrKRPn1EUVF8iTg@mail.gmail.com>
On Thu, Sep 10, 2026 at 12:32:48AM -0700, Bartosz Golaszewski wrote:
> On Tue, 8 Sep 2026 15:42:23 +0200, Tzung-Bi Shih <tzungbi@kernel.org> said:
> > The underlying chip can be removed asynchronously. `gdev->srcu` is used
> > to ensure the synchronization before accessing `gdev->chip`.
> >
> > Revocable encapsulates the details. Leverage revocable for accessing
> > the struct gpio_chip and remove the `gdev->srcu`.
> >
> > Tested-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
> > Signed-off-by: Tzung-Bi Shih <tzungbi@kernel.org>
> > ---
>
> Could you address the following issue pointed out by sashiko?
>
> > diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
> > index 66d2325bfae87..a7d3c4d9018c8 100644
> > --- a/drivers/gpio/gpiolib.c
> > +++ b/drivers/gpio/gpiolib.c
> > @@ -898,10 +897,10 @@ static void gpiodev_release(struct device *dev)
> > synchronize_srcu(&gdev->desc_srcu);
> > cleanup_srcu_struct(&gdev->desc_srcu);
> >
> > + revocable_put(&gdev->chip_rev);
> > ida_free(&gpio_ida, gdev->id);
> > kfree_const(gdev->label);
> > kfree(gdev->descs);
> > - cleanup_srcu_struct(&gdev->srcu);
> > kfree(gdev);
> > }
>
> Could this cause a use-after-free crash during concurrent device removal?
>
> The revocable object chip_rev is embedded directly in the gpio_device struct.
> The new access cleanup pattern drops the SRCU lock before dropping the kref
> on the revocable object.
>
> During asynchronous device removal, revocable_revoke() executes
> synchronize_srcu() to wait for readers. Because the reader drops the SRCU lock
> first, revocable_revoke() will unblock, proceed to gpio_device_put(gdev), and
> gpiodev_release() will execute kfree(gdev) while the reader thread is still
> active.
>
> The reader thread will then attempt to call kref_put() on the chip_rev that is
> embedded inside the now-freed gdev.
While callers who use embedded allocations should properly pin the container,
the revocable primitive itself shouldn't rely on that.
I have fixed this in v13[1] by caching the flag locally inside the handle,
ensuring the teardown never touches the container's memory after unlocking
SRCU.
[1] https://lore.kernel.org/all/20260912123529.7951-2-tzungbi@kernel.org
prev parent reply other threads:[~2026-09-12 12:39 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-08 13:42 [PATCH v12 0/3] drivers/base: Introduce revocable Tzung-Bi Shih
2026-09-08 13:42 ` [PATCH v12 1/3] revocable: Revocable resource management Tzung-Bi Shih
2026-09-08 13:42 ` [PATCH v12 2/3] revocable: Add KUnit test cases Tzung-Bi Shih
2026-09-08 13:42 ` [PATCH v12 3/3] gpio: Leverage revocable for accessing struct gpio_chip Tzung-Bi Shih
2026-09-10 7:32 ` Bartosz Golaszewski
2026-09-12 12:38 ` Tzung-Bi Shih [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=aqVH4Fl7pew8PKf1@tzungbi-laptop \
--to=tzungbi@kernel.org \
--cc=bartosz.golaszewski@oss.qualcomm.com \
--cc=brgl@kernel.org \
--cc=corbet@lwn.net \
--cc=dakr@kernel.org \
--cc=driver-core@lists.linux.dev \
--cc=gregkh@linuxfoundation.org \
--cc=linusw@kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-gpio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=rafael@kernel.org \
--cc=rdunlap@infradead.org \
--cc=skhan@linuxfoundation.org \
/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