drivers/base: Introduce revocable
Subsystems (6)
Tzung-Bi Shih
maintainerv9 cover drivers/base: Introduce revocable
This series introduces the "revocable" mechanism, a synchronization primitive designed to prevent Use-After-Free errors.
- Patch 1 introduces the revocable which is an implementation of ideas from the talk [1].
- Patch 2 adds KUnit test cases.
- Patches 3 to 7 transitions the UAF prevention logic within the GPIO core (gpiolib) to use the "revocable" mechanism.
The existing code aims to prevent UAF issues when the underlying GPIO chip is removed. They replace that custom logic with the generic "revocable" API, which is designed to handle such lifecycle dependencies. There should be no changes in behavior.
- Patches 8 to 9 uses "revocable" mechanism to fix an UAF in cros_ec_chardev driver. Alternatively, [2] is a series for fixing the same issue without using "revocable".
Since v9, there are two ways to manage the resource provider handle.
- Embedded allocation: patches 3 to 7 might be the potential user.
- Dynamic allocation: patches 8 to 9 might be the potential user.
[1] https://lpc.events/event/17/contributions/1627/
[2] https://lore.kernel.org/all/[email protected]
---
v9:
- Rebase onto v7.1-rc1.
- Remove the selftests patch as it makes less sense to test revocable APIs via kselftests.
- Merge patches 7 to 11 from
https://lore.kernel.org/all/[email protected]
into the series.
- Merge patch from
https://lore.kernel.org/all/[email protected]
- Merge patch from
https://lore.kernel.org/all/[email protected]
v8: https://lore.kernel.org/all/[email protected]
- Rework on the revocable APIs. See changelog in [PATCH v8 1/3] for details.
v7: https://lore.kernel.org/all/[email protected]
- Rebase onto next-20260115.
v6: https://lore.kernel.org/all/[email protected]
- Rebase onto next-20251106.
- Separate revocable core and use cases.
v5: https://lore.kernel.org/all/[email protected]
- Rebase onto next-20251015.
- Add more context about the PoC.
- Support multiple revocable providers in the PoC.
v4: https://lore.kernel.org/all/[email protected]
- Rebase onto next-20250922.
- Remove the 5th patch from v3.
- Add fops replacement PoC in 5th - 7th patches.
v3: https://lore.kernel.org/all/[email protected]
- Rebase onto https://lore.kernel.org/all/[email protected]
and next-20250912.
- The 4th patch changed accordingly.
v2: https://lore.kernel.org/all/[email protected]
- Rename "ref_proxy" -> "revocable".
- Add test cases in Kunit and selftest.
v1: https://lore.kernel.org/all/[email protected]
Tzung-Bi Shih (9):
revocable: Revocable resource management
revocable: Add KUnit test cases
gpio: Add revocable provider handle for struct gpio_chip
gpio: cdev: Leverage revocable for accessing struct gpio_chip
gpio: Remove gpio_chip_guard by using revocable
gpio: Leverage revocable for accessing struct gpio_chip
gpio: Remove unused chip and srcu in struct gpio_device platform/chrome: Protect cros_ec_device lifecycle with revocable platform/chrome: cros_ec_chardev: Consume cros_ec_device via revocable
.../driver-api/driver-model/index.rst | 1 + .../driver-api/driver-model/revocable.rst | 384 +++++++++++++++ MAINTAINERS | 10 + drivers/base/Kconfig | 5 + drivers/base/Makefile | 5 +- drivers/base/revocable.c | 298 +++++++++++ drivers/base/revocable_test.c | 461 ++++++++++++++++++ drivers/gpio/gpiolib-cdev.c | 77 ++- drivers/gpio/gpiolib-sysfs.c | 31 +- drivers/gpio/gpiolib.c | 255 ++++------ drivers/gpio/gpiolib.h | 28 +- drivers/platform/chrome/cros_ec.c | 11 + drivers/platform/chrome/cros_ec_chardev.c | 80 ++- include/linux/platform_data/cros_ec_proto.h | 3 + include/linux/revocable.h | 214 ++++++++ 15 files changed, 1597 insertions(+), 266 deletions(-) create mode 100644 Documentation/driver-api/driver-model/revocable.rst create mode 100644 drivers/base/revocable.c create mode 100644 drivers/base/revocable_test.c create mode 100644 include/linux/revocable.h
Signature · 1 line
2.51.0
Tzung-Bi Shih
maintainerPATCH v9 1/9 revocable: Revocable resource management
The "revocable" mechanism is a synchronization primitive designed to manage safe access to resources that can be asynchronously removed or invalidated. Its primary purpose is to prevent Use-After-Free (UAF) errors when interacting with resources whose lifetimes are not guaranteed to outlast their consumers.
This is particularly useful in systems where resources can disappear unexpectedly, such as those provided by hot-pluggable devices like USB. When a consumer holds a reference to such a resource, the underlying device might be removed, causing the resource's memory to be freed. Subsequent access attempts by the consumer would then lead to UAF errors.
Revocable addresses this by providing a form of "weak reference" and a controlled access method. It allows a resource consumer to safely attempt to access the resource. The mechanism guarantees that any access granted is valid for the duration of its use. If the resource has already been revoked (i.e., freed), the access attempt will fail safely, typically by returning NULL, instead of causing a crash.
It uses a provider/consumer model built on Sleepable RCU (SRCU) to guarantee safe memory access:
- A resource provider, such as a driver for a hot-pluggable device, allocates a struct revocable and initializes it with a pointer to the resource.
- A resource consumer that wants to access the resource allocates a struct revocable_consumer containing a reference to the provider.
- To access the resource, the consumer uses revocable_try_access(). This function enters an SRCU read-side critical section and returns the pointer to the resource. If the provider has already freed the resource, it returns NULL. After use, the consumer calls revocable_withdraw_access() to exit the SRCU critical section. There are some macro level helpers for doing that.
The API provides the following contract:
- revocable_try_access() can be safely called from both process and atomic contexts.
- It is permitted to sleep within the critical section established between revocable_try_access() and revocable_withdraw_access().
- revocable_try_access() and the matching revocable_withdraw_access() must occur in the same context. For example, it is illegal to invoke revocable_withdraw_access() in an irq handler if the matching revocable_try_access() was invoked in process context.
- When the provider needs to remove the resource, it calls revocable_revoke(). This function sets the internal resource pointer to NULL and then calls synchronize_srcu() to wait for all current readers to finish before the resource can be completely torn down.
---
v9:
- Add revocable_embed_init() and revocable_embed_destroy() for embedded resource provider per
https://lore.kernel.org/all/CAMRc=MehkJc-js=Wk9vBAcXOpazqjtYDLPUEhmbN8U7Wu2YpgA@mail.gmail.com
v8: https://lore.kernel.org/all/[email protected]
- Squash:
- fdeb3ca3cca8 revocable: Remove redundant synchronize_srcu() call
- 4d7dc4d1a62d revocable: Fix races in revocable_alloc() using RCU
- 377563ce0653 revocable: fix SRCU index corruption by requiring caller-provided storage
- Rename macro names:
- REVOCABLE_TRY_ACCESS_WITH() -> revocable_try_access_with().
- REVOCABLE_TRY_ACCESS_SCOPED() -> revocable_try_access_with_scoped().
- Rename terminologies as now normal users should only "see" provider handles, using a shorter name for provider handle to echo the main concept.
- struct revocable -> struct revocable_consumer.
- struct revocable_provider -> struct revocable.
- revocable_provider_alloc() -> revocable_alloc().
- revocable_provider_revoke() -> revocable_revoke().
- New APIs:
- revocable_get().
- revocable_put().
- revocable_try_access_or_return_err().
- revocable_try_access_or_return().
- revocable_try_access_or_return_void().
- revocable_try_access_or_return_err_scoped().
- revocable_try_access_or_return_scoped().
- revocable_try_access_or_void_scoped().
- revocable_try_access_or_skip_scoped().
- Add API contract that revocable_try_access() works from process and atomic context while also allowing sleeping inside the critical
sections.
- Add revocable.h to the DRIVER CORE entry in MAINTAINERS.
v7: https://lore.kernel.org/all/[email protected]
- "2025" -> "2026" in copyright.
- Documentation/
- Rephrase section "Revocable vs. Devres (devm)".
- Include sections for struct revocable_provider and struct revocable.
- Minor rename: "revocable" -> "access_rev" for DEFINE_FREE().
- Add Acked-by tag.
v6: https://lore.kernel.org/all/[email protected]
- Rename REVOCABLE_TRY_ACCESS_WITH() -> REVOCABLE_TRY_ACCESS_SCOPED().
- Add new REVOCABLE_TRY_ACCESS_WITH().
- Remove Acked-by tags as the API names changed a bit.
v5: https://lore.kernel.org/all/[email protected]
- No changes.
v4: https://lore.kernel.org/all/[email protected]
- Rename:
- revocable_provider_free() -> revocable_provider_revoke().
- REVOCABLE() -> REVOCABLE_TRY_ACCESS_WITH().
- revocable_release() -> revocable_withdraw_access().
- rcu_dereference() -> srcu_dereference() to fix a warning from lock debugging.
- Move most docs to kernel-doc, include them in Documentation/, and modify the commit message accordingly.
- Fix some doc errors.
- Add Acked-by tags.
v3: https://lore.kernel.org/all/[email protected]
- No changes.
v2: https://lore.kernel.org/all/[email protected]
- Rename "ref_proxy" -> "revocable".
- Add introduction in kernel-doc format in revocable.c.
- Add MAINTAINERS entry.
- Add copyright.
- Move from lib/ to drivers/base/.
- EXPORT_SYMBOL() -> EXPORT_SYMBOL_GPL().
- Add Documentation/.
- Rename _get() -> try_access(); _put() -> release().
- Fix a sparse warning by removing the redundant __rcu annotations.
- Fix a sparse warning by adding __acquires() and __releases() annotations.
v1: https://lore.kernel.org/all/[email protected]
A way to verify Documentation/:
- make O=build SPHINXDIRS=driver-api/driver-model/ htmldocs.
--- .../driver-api/driver-model/index.rst | 1 + .../driver-api/driver-model/revocable.rst | 384 ++++++++++++++++++ MAINTAINERS | 9 + drivers/base/Makefile | 2 +- drivers/base/revocable.c | 298 ++++++++++++++ include/linux/revocable.h | 214 ++++++++++ 6 files changed, 907 insertions(+), 1 deletion(-) create mode 100644 Documentation/driver-api/driver-model/revocable.rst create mode 100644 drivers/base/revocable.c create mode 100644 include/linux/revocable.h
Documentation/driver-api/driver-model/index.rst +1 -0
@@ -14,3 +14,4 @@ Driver Model overview platform porting + revocable
Documentation/driver-api/driver-model/revocable.rst new file +384 -0
384 lines not on this page · raw message
MAINTAINERS +9 -0
@@ -7808,6 +7808,7 @@ F: include/linux/fwnode.h F: include/linux/kobj* F: include/linux/ksysfs.h F: include/linux/property.h +F: include/linux/revocable.h F: include/linux/sysfs.h F: kernel/ksysfs.c F: lib/kobj*
@@ -22824,6 +22825,14 @@ F: include/uapi/linux/rseq.h F: kernel/rseq.c F: tools/testing/selftests/rseq/ +REVOCABLE RESOURCE MANAGEMENT +M: Tzung-Bi Shih <[email protected]> +L: [email protected] +S: Maintained +T: git git://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git +F: drivers/base/revocable.c +F: include/linux/revocable.h + RFKILL M: Johannes Berg <[email protected]> L: [email protected]
drivers/base/Makefile +1 -1
@@ -6,7 +6,7 @@ obj-y := component.o core.o bus.o dd.o syscore.o \ cpu.o firmware.o init.o map.o devres.o \ attribute_container.o transport_class.o \ topology.o container.o property.o cacheinfo.o \ - swnode.o faux.o + swnode.o faux.o revocable.o obj-$(CONFIG_AUXILIARY_BUS) += auxiliary.o obj-$(CONFIG_DEVTMPFS) += devtmpfs.o obj-y += power/
drivers/base/revocable.c new file +298 -0
298 lines not on this page · raw message
include/linux/revocable.h new file +214 -0
214 lines not on this page · raw message
Signature · 1 line
2.51.0
Tzung-Bi Shih
maintainerPATCH v9 2/9 revocable: Add KUnit test cases
In reply to Tzung-Bi ShihAdd KUnit test cases for the revocable API.
The test cases cover the following scenarios:
- Basic: Verifies that a consumer can successfully access the resource.
- Revocation: Verifies that after the provider revokes the resource, the consumer correctly receives a NULL pointer on a subsequent access.
- Try Access Macro: Same as "Revocation" but uses the macro level helpers.
- Concurrent Access: Verifies multiple threads can access the resource.
---
v9:
- Add test cases for embedded resource provider.
v8: https://lore.kernel.org/all/[email protected]
- Squash:
- c259cd7ea3c9 revocable: fix missing module license and description
- a243f7fb11fe revocable: Add KUnit test for provider lifetime races
- 988357628c2c revocable: Add KUnit test for concurrent access
- Change accordingly due to its dependency "revocable: Revocable resource management" changes.
v7: https://lore.kernel.org/all/[email protected]
- "2025" -> "2026" in copyright.
- Rename the test name "macro" -> "try_access_macro".
v6: https://lore.kernel.org/all/[email protected]
- Rename REVOCABLE_TRY_ACCESS_WITH() -> REVOCABLE_TRY_ACCESS_SCOPED().
- Add tests for new REVOCABLE_TRY_ACCESS_WITH().
v5: https://lore.kernel.org/all/[email protected]
- No changes.
v4: https://lore.kernel.org/all/[email protected]
- REVOCABLE() -> REVOCABLE_TRY_ACCESS_WITH().
- revocable_release() -> revocable_withdraw_access().
v3: https://lore.kernel.org/all/[email protected]
- No changes.
v2: https://lore.kernel.org/all/[email protected]
- New in the series.
A way to run the test:
$ ./tools/testing/kunit/kunit.py run \
--kconfig_add CONFIG_REVOCABLE_KUNIT_TEST=y \
revocable_test
Or
$ ./tools/testing/kunit/kunit.py run \
--kconfig_add CONFIG_REVOCABLE_KUNIT_TEST=y \
--kconfig_add CONFIG_PROVE_LOCKING=y \
--kconfig_add CONFIG_DEBUG_KERNEL=y \
--kconfig_add CONFIG_DEBUG_INFO=y \
--kconfig_add CONFIG_DEBUG_INFO_DWARF5=y \
--kconfig_add CONFIG_KASAN=y \
--kconfig_add CONFIG_DETECT_HUNG_TASK=y \
--kconfig_add CONFIG_DEFAULT_HUNG_TASK_TIMEOUT="10" \
--arch=x86_64 \
--make_options="C=1 W=1" \
revocable_test--- MAINTAINERS | 1 + drivers/base/Kconfig | 5 + drivers/base/Makefile | 3 + drivers/base/revocable_test.c | 461 ++++++++++++++++++++++++++++++++++ 4 files changed, 470 insertions(+) create mode 100644 drivers/base/revocable_test.c
MAINTAINERS +1 -0
@@ -22831,6 +22831,7 @@ L: [email protected] S: Maintained T: git git://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git F: drivers/base/revocable.c +F: drivers/base/revocable_test.c F: include/linux/revocable.h RFKILL
drivers/base/Kconfig +5 -0
@@ -259,3 +259,8 @@ config FW_DEVLINK_SYNC_STATE_TIMEOUT work on. endmenu + +config REVOCABLE_KUNIT_TEST + tristate "KUnit tests for revocable" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS
drivers/base/Makefile +3 -0
@@ -35,3 +35,6 @@ ccflags-$(CONFIG_DEBUG_DRIVER) := -DDEBUG # define_trace.h needs to know how to find our header CFLAGS_trace.o := -I$(src) obj-$(CONFIG_TRACING) += trace.o + +# KUnit test cases +obj-$(CONFIG_REVOCABLE_KUNIT_TEST) += revocable_test.o
drivers/base/revocable_test.c new file +461 -0
461 lines not on this page · raw message
Signature · 1 line
2.51.0
Tzung-Bi Shih
maintainerPATCH v9 3/9 gpio: Add revocable provider handle for struct gpio_chip
In reply to Tzung-Bi ShihThe underlying chip can be removed asynchronously. gdev->srcu is used to ensure the synchronization before accessing gdev->chip.
Revocable encapsulates the details. Add revocable provider handle for the corresponding struct gpio_chip in struct gpio_device so that it can start to hide the synchronization details.
---
v9:
- New to the series.
- Use static allocated resource provider.
- Rename "chip_rp" -> "chip_rev".
v4 - v8:
- Doesn't exist.
v3: https://lore.kernel.org/all/[email protected]
- Change revocable API usages accordingly.
v2: https://lore.kernel.org/all/[email protected]
- Change usages accordingly after applying
https://lore.kernel.org/all/[email protected].
- Add __rcu for chip_rp.
- Pass pointer of pointer to revocable_provider_revoke().
- Rebase accordingly after applying
https://lore.kernel.org/all/[email protected].
v1: https://lore.kernel.org/all/[email protected]
--- drivers/gpio/gpiolib.c | 7 +++++++ drivers/gpio/gpiolib.h | 3 +++ 2 files changed, 10 insertions(+)
drivers/gpio/gpiolib.c +7 -0
@@ -23,6 +23,7 @@#include <linux/nospec.h> #include <linux/of.h> #include <linux/pinctrl/consumer.h> +#include <linux/revocable.h> #include <linux/seq_file.h> #include <linux/slab.h> #include <linux/srcu.h>
@@ -874,6 +875,7 @@ static void gpiodev_release(struct device *dev)synchronize_srcu (&gdev->desc_srcu);cleanup_srcu_struct (&gdev->desc_srcu); +revocable_embed_destroy (&gdev->chip_rev);ida_free (&gpio_ida, gdev->id);kfree_const (gdev->label);kfree (gdev->descs);
@@ -1167,6 +1169,10 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,if (ret) goto err_cleanup_desc_srcu; + ret =revocable_embed_init (&gdev->chip_rev, gc); +if (ret) + goto err_cleanup_desc_srcu; +device_initialize (&gdev->dev);/* * After this point any allocated resources to `gdev` will be
@@ -1389,6 +1395,7 @@ void gpiochip_remove(struct gpio_chip *gc)/* Numb the device, cancelling all outstanding operations */ rcu_assign_pointer (gdev->chip,NULL );synchronize_srcu (&gdev->srcu); +revocable_revoke (&gdev->chip_rev);gpio_device_teardown_shared (gdev);gpiochip_irqchip_remove (gc);acpi_gpiochip_remove (gc);
drivers/gpio/gpiolib.h +3 -0
@@ -16,6 +16,7 @@#include <linux/gpio/driver.h> #include <linux/module.h> #include <linux/notifier.h> +#include <linux/revocable.h> #include <linux/spinlock.h> #include <linux/string.h> #include <linux/srcu.h>
@@ -55,6 +56,7 @@ struct fwnode_handle;* @device_notifier: used to notify character device wait queues about the GPIO * device being unregistered * @srcu: protects the pointer to the underlying GPIO chip +* @chip_rev: revocable provider handle for the corresponding struct gpio_chip. * @pin_ranges: range of pins served by the GPIO driver * * This state container holds most of the runtime variable data
@@ -82,6 +84,7 @@ struct gpio_device {struct workqueue_struct *line_state_wq;struct blocking_notifier_head device_notifier;struct srcu_struct srcu; +struct revocable chip_rev;#ifdef CONFIG_PINCTRL /*
Signature · 1 line
2.51.0
Tzung-Bi Shih
maintainerPATCH v9 4/9 gpio: cdev: Leverage revocable for accessing struct gpio_chip
In reply to Tzung-Bi ShihStruct gpio_device now provides a revocable provider to the underlying struct gpio_chip. Leverage revocable for accessing the struct gpio_chip.
---
v9:
- New to the series.
- Rename "chip_rp" -> "chip_rev".
v4 - v8:
- Doesn't exist.
v3: https://lore.kernel.org/all/[email protected]
- Change revocable API usages accordingly.
v2: https://lore.kernel.org/all/[email protected]
- Change usages accordingly after applying
https://lore.kernel.org/all/[email protected].
- Preserve a local storage for struct revocable.
- Combine multiple patches (see "v1:").
- Fix a race condition reported in
https://lore.kernel.org/all/CAMRc=McDaipt85OHm0MksLkuf6E79dY1uNSqqbcJnoQTUs81Pw@mail.gmail.com/
and analyzed in
https://lore.kernel.org/all/aXEEUWwkxHZzCnaI@tzungbi-laptop/.
In v1, the blocking_notifier_chain_unregister() will be skipped if the chip has been removed, leading an UAF in gpiolib_cdev_unregister(). In v2, it won't skip blocking_notifier_chain_unregister().
v1:
- https://lore.kernel.org/all/[email protected]
- https://lore.kernel.org/all/[email protected]
- https://lore.kernel.org/all/[email protected]
- https://lore.kernel.org/all/[email protected]
- https://lore.kernel.org/all/[email protected]
--- drivers/gpio/gpiolib-cdev.c | 68 ++++++++++++++----------------------- 1 file changed, 26 insertions(+), 42 deletions(-)
drivers/gpio/gpiolib-cdev.c +26 -42
@@ -22,6 +22,7 @@#include <linux/overflow.h> #include <linux/pinctrl/consumer.h> #include <linux/poll.h> +#include <linux/revocable.h> #include <linux/seq_file.h> #include <linux/spinlock.h> #include <linux/string.h>
@@ -210,11 +211,9 @@ static long linehandle_ioctl(struct file *file, unsigned int cmd,DECLARE_BITMAP (vals,GPIOHANDLES_MAX );unsigned int i;int ret; +struct gpio_chip *gc; -guard (srcu)(&lh->gdev->srcu); - -if (!rcu_access_pointer (lh->gdev->chip)) -return -ENODEV ; +revocable_try_access_or_return (&lh->gdev->chip_rev, gc);switch (cmd) {case GPIOHANDLE_GET_LINE_VALUES_IOCTL :
@@ -1432,11 +1431,9 @@ static long linereq_ioctl(struct file *file, unsigned int cmd, {struct linereq *lr = file->private_data;void __user *ip = (void __user *)arg; +struct gpio_chip *gc; -guard (srcu)(&lr->gdev->srcu); - -if (!rcu_access_pointer (lr->gdev->chip)) -return -ENODEV ; +revocable_try_access_or_return (&lr->gdev->chip_rev, gc);switch (cmd) {case GPIO_V2_LINE_GET_VALUES_IOCTL :
@@ -1463,10 +1460,10 @@ static __poll_t linereq_poll(struct file *file, {struct linereq *lr = file->private_data;__poll_t events =0 ; +struct gpio_chip *gc; -guard (srcu)(&lr->gdev->srcu); - -if (!rcu_access_pointer (lr->gdev->chip)) +revocable_try_access_with (&lr->gdev->chip_rev, gc); +if (!gc)return EPOLLHUP |EPOLLERR ;poll_wait (file, &lr->wait, wait);
@@ -1485,11 +1482,9 @@ static ssize_t linereq_read(struct file *file, char __user *buf,struct gpio_v2_line_event le;ssize_t bytes_read =0 ;int ret; +struct gpio_chip *gc; -guard (srcu)(&lr->gdev->srcu); - -if (!rcu_access_pointer (lr->gdev->chip)) -return -ENODEV ; +revocable_try_access_or_return (&lr->gdev->chip_rev, gc);if (count <sizeof (le))return -EINVAL ;
@@ -1759,10 +1754,10 @@ static __poll_t lineevent_poll(struct file *file, {struct lineevent_state *le = file->private_data;__poll_t events =0 ; +struct gpio_chip *gc; -guard (srcu)(&le->gdev->srcu); - -if (!rcu_access_pointer (le->gdev->chip)) +revocable_try_access_with (&le->gdev->chip_rev, gc); +if (!gc)return EPOLLHUP |EPOLLERR ;poll_wait (file, &le->wait, wait);
@@ -1797,11 +1792,9 @@ static ssize_t lineevent_read(struct file *file, char __user *buf,ssize_t bytes_read =0 ;ssize_t ge_size;int ret; +struct gpio_chip *gc; -guard (srcu)(&le->gdev->srcu); - -if (!rcu_access_pointer (le->gdev->chip)) -return -ENODEV ; +revocable_try_access_or_return (&le->gdev->chip_rev, gc);/* * When compatible system call is being used the struct gpioevent_data,
@@ -1879,11 +1872,9 @@ static long lineevent_ioctl(struct file *file, unsigned int cmd,struct lineevent_state *le = file->private_data;void __user *ip = (void __user *)arg;struct gpiohandle_data ghd; +struct gpio_chip *gc; -guard (srcu)(&le->gdev->srcu); - -if (!rcu_access_pointer (le->gdev->chip)) -return -ENODEV ; +revocable_try_access_or_return (&le->gdev->chip_rev, gc);/* * We can get the value for an event line but not set it,
@@ -2385,12 +2376,10 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)struct gpio_chardev_data *cdev = file->private_data;struct gpio_device *gdev = cdev->gdev;void __user *ip = (void __user *)arg; - -guard (srcu)(&gdev->srcu); +struct gpio_chip *gc;/* We fail any subsequent ioctl():s when the chip is gone */ -if (!rcu_access_pointer (gdev->chip)) -return -ENODEV ; +revocable_try_access_or_return (&gdev->chip_rev, gc);/* Fill in the struct and pass to userspace */ switch (cmd) {
@@ -2448,12 +2437,9 @@ static void lineinfo_changed_func(struct work_struct *work)* Pin functions are in general much more static and while it's * not 100% bullet-proof, it's good enough for most cases. */ - scoped_guard(srcu, &ctx->gdev->srcu) { - gc =srcu_dereference (ctx->gdev->chip, &ctx->gdev->srcu); -if (gc && - !pinctrl_gpio_can_use_line (gc, ctx->chg.info.offset)) + revocable_try_access_or_skip_scoped(&ctx->gdev->chip_rev, gc) +if (!pinctrl_gpio_can_use_line (gc, ctx->chg.info.offset)) ctx->chg.info.flags |=GPIO_V2_LINE_FLAG_USED ; - } } ret = kfifo_in_spinlocked(&ctx->cdev->events, &ctx->chg, 1,
@@ -2534,10 +2520,10 @@ static __poll_t lineinfo_watch_poll(struct file *file, {struct gpio_chardev_data *cdev = file->private_data;__poll_t events =0 ; +struct gpio_chip *gc; -guard (srcu)(&cdev->gdev->srcu); - -if (!rcu_access_pointer (cdev->gdev->chip)) +revocable_try_access_with (&cdev->gdev->chip_rev, gc); +if (!gc)return EPOLLHUP |EPOLLERR ;poll_wait (file, &cdev->wait, pollt);
@@ -2557,11 +2543,9 @@ static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,ssize_t bytes_read =0 ;int ret;size_t event_size; +struct gpio_chip *gc; -guard (srcu)(&cdev->gdev->srcu); - -if (!rcu_access_pointer (cdev->gdev->chip)) -return -ENODEV ; +revocable_try_access_or_return (&cdev->gdev->chip_rev, gc);#ifndef CONFIG_GPIO_CDEV_V1 event_size =sizeof (struct gpio_v2_line_info_changed );
Signature · 1 line
2.51.0
Tzung-Bi Shih
maintainerPATCH v9 5/9 gpio: Remove gpio_chip_guard by using revocable
In reply to Tzung-Bi ShihStruct gpio_device now provides a revocable provider to the underlying struct gpio_chip. Leverage revocable for accessing the struct gpio_chip instead of using gpio_chip_guard.
---
v9:
- New to the series.
- Rename "chip_rp" -> "chip_rev".
v4 - v8:
- Doesn't exist.
v3: https://lore.kernel.org/all/[email protected]
- Change revocable API usages accordingly.
v2: https://lore.kernel.org/all/[email protected]
- Separate from v1 for including gpio_chip_guard only.
v1: https://lore.kernel.org/all/[email protected]
--- drivers/gpio/gpiolib-cdev.c | 9 +- drivers/gpio/gpiolib-sysfs.c | 31 +++---- drivers/gpio/gpiolib.c | 164 ++++++++++++++++------------------- drivers/gpio/gpiolib.h | 21 ----- 4 files changed, 91 insertions(+), 134 deletions(-)
drivers/gpio/gpiolib-cdev.c +4 -5
@@ -2156,10 +2156,9 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc,u32 debounce_period_us;unsigned long dflags;const char *label; +struct gpio_chip *gc; -CLASS (gpio_chip_guard, guard)(desc); -if (!guard.gc) -return ; +revocable_try_access_or_return_void (&desc->gdev->chip_rev, gc);memset (info,0 ,sizeof (*info)); info->offset =gpiod_hwgpio (desc);
@@ -2192,10 +2191,10 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc,test_bit (GPIOD_FLAG_IS_HOGGED , &dflags) ||test_bit (GPIOD_FLAG_EXPORT , &dflags) ||test_bit (GPIOD_FLAG_SYSFS , &dflags) || - !gpiochip_line_is_valid(guard.gc, info->offset)) { + !gpiochip_line_is_valid(gc, info->offset)) { info->flags |= GPIO_V2_LINE_FLAG_USED; } else if (!atomic) { -if (!pinctrl_gpio_can_use_line (guard.gc, info->offset)) +if (!pinctrl_gpio_can_use_line (gc, info->offset)) info->flags |=GPIO_V2_LINE_FLAG_USED ; }
drivers/gpio/gpiolib-sysfs.c +14 -17
@@ -10,6 +10,7 @@#include <linux/list.h> #include <linux/mutex.h> #include <linux/printk.h> +#include <linux/revocable.h> #include <linux/slab.h> #include <linux/string.h> #include <linux/srcu.h>
@@ -215,10 +216,9 @@ static int gpio_sysfs_request_irq(struct gpiod_data *data, unsigned char flags)struct gpio_desc *desc = data->desc;unsigned long irq_flags;int ret; +struct gpio_chip *gc; -CLASS (gpio_chip_guard, guard)(desc); -if (!guard.gc) -return -ENODEV ; +revocable_try_access_or_return (&desc->gdev->chip_rev, gc); data->irq =gpiod_to_irq (desc);if (data->irq <0 )
@@ -244,7 +244,7 @@ static int gpio_sysfs_request_irq(struct gpiod_data *data, unsigned char flags)* Remove this redundant call (along with the corresponding unlock) * when those drivers have been fixed. */ - ret =gpiochip_lock_as_irq (guard.gc,gpiod_hwgpio (desc)); + ret =gpiochip_lock_as_irq (gc,gpiod_hwgpio (desc));if (ret <0 ) goto err_clr_bits;
@@ -258,7 +258,7 @@ static int gpio_sysfs_request_irq(struct gpiod_data *data, unsigned char flags)return 0 ; err_unlock: -gpiochip_unlock_as_irq (guard.gc,gpiod_hwgpio (desc)); +gpiochip_unlock_as_irq (gc,gpiod_hwgpio (desc)); err_clr_bits:clear_bit (GPIOD_FLAG_EDGE_RISING , &desc->flags);clear_bit (GPIOD_FLAG_EDGE_FALLING , &desc->flags);
@@ -273,14 +273,13 @@ static int gpio_sysfs_request_irq(struct gpiod_data *data, unsigned char flags)static void gpio_sysfs_free_irq (struct gpiod_data *data) {struct gpio_desc *desc = data->desc; +struct gpio_chip *gc; -CLASS (gpio_chip_guard, guard)(desc); -if (!guard.gc) -return ; +revocable_try_access_or_return_void (&desc->gdev->chip_rev, gc); data->irq_flags =0 ;free_irq (data->irq, data); -gpiochip_unlock_as_irq (guard.gc,gpiod_hwgpio (desc)); +gpiochip_unlock_as_irq (gc,gpiod_hwgpio (desc));clear_bit (GPIOD_FLAG_EDGE_RISING , &desc->flags);clear_bit (GPIOD_FLAG_EDGE_FALLING , &desc->flags); }
@@ -473,13 +472,12 @@ static DEVICE_ATTR_RO(ngpio);static int export_gpio_desc (struct gpio_desc *desc) {int offset, ret; +struct gpio_chip *gc; -CLASS (gpio_chip_guard, guard)(desc); -if (!guard.gc) -return -ENODEV ; +revocable_try_access_or_return (&desc->gdev->chip_rev, gc); offset =gpiod_hwgpio (desc); -if (!gpiochip_line_is_valid (guard.gc, offset)) { +if (!gpiochip_line_is_valid (gc, offset)) {pr_debug_ratelimited ("%s: GPIO %d masked\n" , __func__,gpiod_hwgpio (desc));return -EINVAL ;
@@ -732,6 +730,7 @@ int gpiod_export(struct gpio_desc *desc, bool direction_may_change)struct gpio_device *gdev;struct attribute **attrs;int status; +struct gpio_chip *gc;/* can't export until sysfs is available ... */ if (!class_is_registered (&gpio_class)) {
@@ -744,9 +743,7 @@ int gpiod_export(struct gpio_desc *desc, bool direction_may_change)return -EINVAL ; } -CLASS (gpio_chip_guard, guard)(desc); -if (!guard.gc) -return -ENODEV ; +revocable_try_access_or_return (&desc->gdev->chip_rev, gc);if (test_and_set_bit (GPIOD_FLAG_EXPORT , &desc->flags))return -EPERM ;
@@ -769,7 +766,7 @@ int gpiod_export(struct gpio_desc *desc, bool direction_may_change) desc_data->desc = desc;mutex_init (&desc_data->mutex); -if (guard.gc->direction_input && guard.gc->direction_output) +if (gc->direction_input && gc->direction_output) desc_data->direction_can_change = direction_may_change;else desc_data->direction_can_change = false;
drivers/gpio/gpiolib.c +73 -91
408 lines not on this page · raw message
drivers/gpio/gpiolib.h +0 -21
@@ -228,27 +228,6 @@ struct gpio_desc {#define gpiod_not_found (desc) (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT) -struct gpio_chip_guard { -struct gpio_device *gdev; -struct gpio_chip *gc; -int idx; -}; - -DEFINE_CLASS (gpio_chip_guard, - struct gpio_chip_guard, -srcu_read_unlock (&_T.gdev->srcu, _T.idx), - ({ -struct gpio_chip_guard _guard; - - _guard.gdev = desc->gdev; - _guard.idx =srcu_read_lock (&_guard.gdev->srcu); - _guard.gc =srcu_dereference (_guard.gdev->chip, - &_guard.gdev->srcu); - - _guard; - }), - struct gpio_desc *desc) -int gpiod_request (struct gpio_desc *desc,const char *label);int gpiod_request_commit (struct gpio_desc *desc,const char *label);void gpiod_free (struct gpio_desc *desc);
Signature · 1 line
2.51.0
Tzung-Bi Shih
maintainerPATCH v9 6/9 gpio: Leverage revocable for accessing struct gpio_chip
In reply to Tzung-Bi ShihStruct gpio_device now provides a revocable provider to the underlying struct gpio_chip. Leverage revocable for accessing the struct gpio_chip.
---
v9:
- New to the series.
- Rename "chip_rp" -> "chip_rev".
v4 - v8:
- Doesn't exist.
v3: https://lore.kernel.org/all/[email protected]
- Change revocable API usages accordingly.
v2: https://lore.kernel.org/all/[email protected]
- Separate from v1(a) for excluding gpio_chip_guard and combine v1(b).
v1(a):
- https://lore.kernel.org/all/[email protected]
v1(b):
- https://lore.kernel.org/all/[email protected]
--- drivers/gpio/gpiolib.c | 60 +++++++++++++----------------------------- 1 file changed, 18 insertions(+), 42 deletions(-)
drivers/gpio/gpiolib.c +18 -42
@@ -335,7 +335,10 @@ EXPORT_SYMBOL(gpio_device_get_label);*/ struct gpio_chip *gpio_device_get_chip (struct gpio_device *gdev) { -return rcu_dereference_check (gdev->chip,1 ); +struct gpio_chip *gc; + +revocable_try_access_with (&gdev->chip_rev, gc); +return gc; }EXPORT_SYMBOL_GPL (gpio_device_get_chip);
@@ -561,9 +564,7 @@ static struct gpio_desc *gpio_name_to_desc(const char * const name)list_for_each_entry_srcu (gdev, &gpio_devices, list,srcu_read_lock_held (&gpio_devices_srcu)) { -guard (srcu)(&gdev->srcu); - - gc =srcu_dereference (gdev->chip, &gdev->srcu); +revocable_try_access_with (&gdev->chip_rev, gc);if (!gc)continue ;
@@ -1050,9 +1051,7 @@ static void gpiochip_setup_devs(void)list_for_each_entry_srcu (gdev, &gpio_devices, list,srcu_read_lock_held (&gpio_devices_srcu)) { -guard (srcu)(&gdev->srcu); - - gc =srcu_dereference (gdev->chip, &gdev->srcu); +revocable_try_access_with (&gdev->chip_rev, gc);if (!gc) {dev_err (&gdev->dev,"Underlying GPIO chip is gone\n" );continue ;
@@ -1455,11 +1454,11 @@ struct gpio_device *gpio_device_find(const void *data,if (!device_is_registered (&gdev->dev))continue ; -guard (srcu)(&gdev->srcu); - - gc =srcu_dereference (gdev->chip, &gdev->srcu); +revocable_try_access_with (&gdev->chip_rev, gc); +if (!gc) +continue ; -if (gc &&match (gc, data)) +if (match (gc, data))return gpio_device_get (gdev); }
@@ -3402,18 +3401,10 @@ static int gpio_chip_get_value(struct gpio_chip *gc, const struct gpio_desc *desstatic int gpiod_get_raw_value_commit (const struct gpio_desc *desc) { -struct gpio_device *gdev;struct gpio_chip *gc;int value; -/* FIXME Unable to use gpio_chip_guard due to const desc. */ - gdev = desc->gdev; - -guard (srcu)(&gdev->srcu); - - gc =srcu_dereference (gdev->chip, &gdev->srcu); -if (!gc) -return -ENODEV ; +revocable_try_access_or_return (&desc->gdev->chip_rev, gc); value =gpio_chip_get_value (gc, desc); value = value <0 ? value : !!value;
@@ -3452,9 +3443,10 @@ static int gpio_chip_get_multiple(struct gpio_chip *gc,/* The 'other' chip must be protected with its GPIO device's SRCU. */ static bool gpio_device_chip_cmp (struct gpio_device *gdev,struct gpio_chip *gc) { -guard (srcu)(&gdev->srcu); +struct gpio_chip *chip; -return gc ==srcu_dereference (gdev->chip, &gdev->srcu); +revocable_try_access_with (&gdev->chip_rev, chip); +return chip ? chip == gc : false; } int gpiod_get_array_value_complex(bool raw, bool can_sleep,
@@ -3477,11 +3469,7 @@ int gpiod_get_array_value_complex(bool raw, bool can_sleep,if (!can_sleep)WARN_ON (array_info->gdev->can_sleep); -guard (srcu)(&array_info->gdev->srcu); - gc =srcu_dereference (array_info->gdev->chip, - &array_info->gdev->srcu); -if (!gc) -return -ENODEV ; +revocable_try_access_or_return (&array_info->gdev->chip_rev, gc); ret =gpio_chip_get_multiple (gc, array_info->get_mask, value_bitmap);
@@ -3818,11 +3806,7 @@ int gpiod_set_array_value_complex(bool raw, bool can_sleep,return -EPERM ; } -guard (srcu)(&array_info->gdev->srcu); - gc =srcu_dereference (array_info->gdev->chip, - &array_info->gdev->srcu); -if (!gc) -return -ENODEV ; +revocable_try_access_or_return (&array_info->gdev->chip_rev, gc);if (!raw && !bitmap_empty (array_info->invert_mask, array_size)) bitmap_xor(value_bitmap, value_bitmap,
@@ -4117,7 +4101,6 @@ EXPORT_SYMBOL_GPL(gpiod_is_shared);*/ int gpiod_to_irq (const struct gpio_desc *desc) { -struct gpio_device *gdev;struct gpio_chip *gc;int offset;int ret;
@@ -4126,12 +4109,7 @@ int gpiod_to_irq(const struct gpio_desc *desc)if (ret <=0 )return -EINVAL ; - gdev = desc->gdev; -/* FIXME Cannot use gpio_chip_guard due to const desc. */ -guard (srcu)(&gdev->srcu); - gc =srcu_dereference (gdev->chip, &gdev->srcu); -if (!gc) -return -ENODEV ; +revocable_try_access_or_return (&desc->gdev->chip_rev, gc); offset =gpiod_hwgpio (desc);if (gc->to_irq) {
@@ -5470,9 +5448,7 @@ static int gpiolib_seq_show(struct seq_file *s, void *v)if (priv->newline)seq_putc (s,'\n' ); -guard (srcu)(&gdev->srcu); - - gc =srcu_dereference (gdev->chip, &gdev->srcu); +revocable_try_access_with (&gdev->chip_rev, gc);if (!gc) {seq_printf (s,"%s: (dangling chip)\n" ,dev_name (&gdev->dev));return 0 ;
Signature · 1 line
2.51.0
Tzung-Bi Shih
maintainerPATCH v9 7/9 gpio: Remove unused `chip` and `srcu` in struct gpio_device
In reply to Tzung-Bi Shihchip and srcu in struct gpio_device are unused as their usages are replaced to use revocable. Remove them.
---
v9:
- New to the series.
- No changes.
v4 - v8:
- Doesn't exist.
v3: https://lore.kernel.org/all/[email protected]
- No changes.
v2: https://lore.kernel.org/all/[email protected]
- No changes.
v1: https://lore.kernel.org/all/[email protected]
--- drivers/gpio/gpiolib.c | 26 +------------------------- drivers/gpio/gpiolib.h | 4 ---- 2 files changed, 1 insertion(+), 29 deletions(-)
drivers/gpio/gpiolib.c +1 -25
@@ -428,8 +428,6 @@ static int gpiochip_get_direction(struct gpio_chip *gc, unsigned int offset) {int ret; -lockdep_assert_held (&gc->gpiodev->srcu); -if (WARN_ON (!gc->get_direction))return -EOPNOTSUPP ;
@@ -879,7 +877,6 @@ static void gpiodev_release(struct device *dev)ida_free (&gpio_ida, gdev->id);kfree_const (gdev->label);kfree (gdev->descs); -cleanup_srcu_struct (&gdev->srcu);kfree (gdev); }
@@ -1154,14 +1151,9 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, goto err_free_gdev; gdev->id = ret; - ret =init_srcu_struct (&gdev->srcu); -if (ret) - goto err_free_ida; -rcu_assign_pointer (gdev->chip, gc); - ret =init_srcu_struct (&gdev->desc_srcu);if (ret) - goto err_cleanup_gdev_srcu; + goto err_free_ida; ret =dev_set_name (&gdev->dev,GPIOCHIP_NAME "%d" , gdev->id);if (ret)
@@ -1353,8 +1345,6 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, err_cleanup_desc_srcu:cleanup_srcu_struct (&gdev->desc_srcu); -err_cleanup_gdev_srcu: -cleanup_srcu_struct (&gdev->srcu); err_free_ida:ida_free (&gpio_ida, gdev->id); err_free_gdev:
@@ -1391,8 +1381,6 @@ void gpiochip_remove(struct gpio_chip *gc)synchronize_srcu (&gpio_devices_srcu);/* Numb the device, cancelling all outstanding operations */ -rcu_assign_pointer (gdev->chip,NULL ); -synchronize_srcu (&gdev->srcu);revocable_revoke (&gdev->chip_rev);gpio_device_teardown_shared (gdev);gpiochip_irqchip_remove (gc);
@@ -2903,8 +2891,6 @@ static int gpiochip_direction_input(struct gpio_chip *gc, unsigned int offset) {int ret; -lockdep_assert_held (&gc->gpiodev->srcu); -if (WARN_ON (!gc->direction_input))return -EOPNOTSUPP ;
@@ -2920,8 +2906,6 @@ static int gpiochip_direction_output(struct gpio_chip *gc, unsigned int offset, {int ret; -lockdep_assert_held (&gc->gpiodev->srcu); -if (WARN_ON (!gc->direction_output))return -EOPNOTSUPP ;
@@ -3009,8 +2993,6 @@ static int gpiochip_set(struct gpio_chip *gc, unsigned int offset, int value) {int ret; -lockdep_assert_held (&gc->gpiodev->srcu); -if (WARN_ON (unlikely (!gc->set)))return -EOPNOTSUPP ;
@@ -3358,8 +3340,6 @@ static int gpiochip_get(struct gpio_chip *gc, unsigned int offset) {int ret; -lockdep_assert_held (&gc->gpiodev->srcu); -/* Make sure this is called after checking for gc->get(). */ ret = gc->get (gc, offset);if (ret >1 ) {
@@ -3415,8 +3395,6 @@ static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)static int gpio_chip_get_multiple (struct gpio_chip *gc,unsigned long *mask,unsigned long *bits) { -lockdep_assert_held (&gc->gpiodev->srcu); -if (gc->get_multiple) {int ret;
@@ -3760,8 +3738,6 @@ static int gpiochip_set_multiple(struct gpio_chip *gc,unsigned int i;int ret; -lockdep_assert_held (&gc->gpiodev->srcu); -if (gc->set_multiple) { ret = gc->set_multiple (gc, mask, bits);if (ret >0 )
drivers/gpio/gpiolib.h +0 -4
@@ -32,7 +32,6 @@ struct fwnode_handle;* @chrdev: character device for the GPIO device * @id: numerical ID number for the GPIO chip * @owner: helps prevent removal of modules exporting active GPIOs -* @chip: pointer to the corresponding gpiochip, holding static * data for this device * @descs: array of ngpio descriptors. * @valid_mask: If not %NULL, holds bitmask of GPIOs which are valid to be
@@ -55,7 +54,6 @@ struct fwnode_handle;* process context * @device_notifier: used to notify character device wait queues about the GPIO * device being unregistered -* @srcu: protects the pointer to the underlying GPIO chip * @chip_rev: revocable provider handle for the corresponding struct gpio_chip. * @pin_ranges: range of pins served by the GPIO driver *
@@ -69,7 +67,6 @@ struct gpio_device {struct cdev chrdev;int id;struct module *owner; -struct gpio_chip __rcu *chip;struct gpio_desc *descs;unsigned long *valid_mask;struct srcu_struct desc_srcu;
@@ -83,7 +80,6 @@ struct gpio_device {rwlock_t line_state_lock;struct workqueue_struct *line_state_wq;struct blocking_notifier_head device_notifier; -struct srcu_struct srcu;struct revocable chip_rev;#ifdef CONFIG_PINCTRL
Signature · 1 line
2.51.0
Tzung-Bi Shih
maintainerPATCH v9 8/9 platform/chrome: Protect cros_ec_device lifecycle with revocable
In reply to Tzung-Bi ShihThe cros_ec_device can be unregistered when the underlying device is removed. Other kernel drivers that interact with the EC may hold a pointer to the cros_ec_device, creating a risk of a use-after-free error if the EC device is removed while still being referenced.
To prevent this, leverage the revocable and convert the underlying device drivers to resource providers of cros_ec_device.
---
v9:
- New to the series.
- Change revocable API usages accordingly.
- Rename "revocable_provider" -> "its_rev".
v5 - v8:
- Doesn't exist.
v4: https://lore.kernel.org/all/[email protected]
- No changes.
v3: https://lore.kernel.org/all/[email protected]
- Initialize the revocable provider in cros_ec_device_alloc() instead of spreading in protocol device drivers.
v2: https://lore.kernel.org/all/[email protected]
- Rename "ref_proxy" -> "revocable".
v1: https://lore.kernel.org/all/[email protected]
--- drivers/platform/chrome/cros_ec.c | 11 +++++++++++ include/linux/platform_data/cros_ec_proto.h | 3 +++ 2 files changed, 14 insertions(+)
drivers/platform/chrome/cros_ec.c +11 -0
@@ -16,6 +16,7 @@#include <linux/platform_device.h> #include <linux/platform_data/cros_ec_commands.h> #include <linux/platform_data/cros_ec_proto.h> +#include <linux/revocable.h> #include <linux/slab.h> #include <linux/suspend.h>
@@ -37,6 +38,7 @@ static void cros_ec_device_free(void *data)mutex_destroy (&ec_dev->lock);lockdep_unregister_key (&ec_dev->lockdep_key); +revocable_revoke (ec_dev->its_rev); }struct cros_ec_device *cros_ec_device_alloc (struct device *dev)
@@ -47,6 +49,15 @@ struct cros_ec_device *cros_ec_device_alloc(struct device *dev)if (!ec_dev)return NULL ; + ec_dev->its_rev =revocable_alloc (ec_dev); +if (!ec_dev->its_rev) +return NULL ; +/* +* Drop the extra reference for the caller as the caller is the +* resource provider. +*/ +revocable_put (ec_dev->its_rev); + ec_dev->din_size =sizeof (struct ec_host_response ) +sizeof (struct ec_response_get_protocol_info ) +EC_MAX_RESPONSE_OVERHEAD ;
include/linux/platform_data/cros_ec_proto.h +3 -0
@@ -12,6 +12,7 @@#include <linux/lockdep_types.h> #include <linux/mutex.h> #include <linux/notifier.h> +#include <linux/revocable.h> #include <linux/platform_data/cros_ec_commands.h>
@@ -165,6 +166,7 @@ struct cros_ec_command {* @pd: The platform_device used by the mfd driver to interface with the * PD behind an EC. * @panic_notifier: EC panic notifier. +* @its_rev: The revocable_provider to this device. */ struct cros_ec_device {/* These are used by other drivers that want to talk to the EC */
@@ -211,6 +213,7 @@ struct cros_ec_device {struct platform_device *pd;struct blocking_notifier_head panic_notifier; +struct revocable *its_rev; };/**
Signature · 1 line
2.51.0
Tzung-Bi Shih
maintainerPATCH v9 9/9 platform/chrome: cros_ec_chardev: Consume cros_ec_device via revocable
In reply to Tzung-Bi ShihThe cros_ec_chardev driver provides a character device interface to the ChromeOS EC. A file handle to this device can remain open in userspace even if the underlying EC device is removed.
This creates a classic use-after-free vulnerability. Any file operation (ioctl, release, etc.) on the open handle after the EC device has gone would access a stale pointer, leading to a system crash.
To prevent this, leverage the revocable and convert cros_ec_chardev to a resource consumer of cros_ec_device.
---
v9:
- New to the series.
- Change revocable API usages accordingly.
v4 - v8:
- Doesn't exist.
v3: https://lore.kernel.org/all/[email protected]
- Use specific labels for different cleanup in cros_ec_chardev_open().
v2: https://lore.kernel.org/all/[email protected]
- Rename "ref_proxy" -> "revocable".
- Fix a sparse warning by removing the redundant __rcu annotation.
v1: https://lore.kernel.org/all/[email protected]
--- drivers/platform/chrome/cros_ec_chardev.c | 80 +++++++++++++++++------ 1 file changed, 61 insertions(+), 19 deletions(-)
drivers/platform/chrome/cros_ec_chardev.c +61 -19
@@ -22,6 +22,7 @@#include <linux/platform_data/cros_ec_proto.h> #include <linux/platform_device.h> #include <linux/poll.h> +#include <linux/revocable.h> #include <linux/slab.h> #include <linux/types.h> #include <linux/uaccess.h>
@@ -32,7 +33,7 @@#define CROS_MAX_EVENT_LEN PAGE_SIZEstruct chardev_priv { -struct cros_ec_device *ec_dev; +struct revocable *rev;struct notifier_block notifier;wait_queue_head_t wait_event;unsigned long event_mask;
@@ -55,6 +56,7 @@ static int ec_get_version(struct chardev_priv *priv, char *str, int maxlen) };struct ec_response_get_version *resp;struct cros_ec_command *msg; +struct cros_ec_device *ec_dev;int ret; msg =kzalloc (sizeof (*msg) +sizeof (*resp),GFP_KERNEL );
@@ -64,12 +66,19 @@ static int ec_get_version(struct chardev_priv *priv, char *str, int maxlen) msg->command =EC_CMD_GET_VERSION + priv->cmd_offset; msg->insize =sizeof (*resp); - ret =cros_ec_cmd_xfer_status (priv->ec_dev, msg); -if (ret <0 ) { -snprintf (str, maxlen, -"Unknown EC version, returned error: %d\n" , - msg->result); - goto exit; + revocable_try_access_with_scoped(priv->rev, ec_dev) { +if (!ec_dev) { + ret = -ENODEV ; + goto exit; + } + + ret =cros_ec_cmd_xfer_status (ec_dev, msg); +if (ret <0 ) { +snprintf (str, maxlen, +"Unknown EC version, returned error: %d\n" , + msg->result); + goto exit; + } } resp = (struct ec_response_get_version *)msg->data;
@@ -92,10 +101,15 @@ static int cros_ec_chardev_mkbp_event(struct notifier_block *nb, { struct chardev_priv *priv = container_of(nb, struct chardev_priv, notifier); -struct cros_ec_device *ec_dev = priv->ec_dev; +struct cros_ec_device *ec_dev;struct ec_event *event; -unsigned long event_bit =1 << ec_dev->event_data.event_type; -int total_size =sizeof (*event) + ec_dev->event_size; +unsigned long event_bit; +int total_size; + +revocable_try_access_or_return_err (priv->rev, ec_dev,NOTIFY_DONE ); + + event_bit =1 << ec_dev->event_data.event_type; + total_size =sizeof (*event) + ec_dev->event_size;if (!(event_bit & priv->event_mask) || (priv->event_len + total_size) >CROS_MAX_EVENT_LEN )
@@ -166,7 +180,8 @@ static int cros_ec_chardev_open(struct inode *inode, struct file *filp)if (!priv)return -ENOMEM ; - priv->ec_dev = ec_dev; + priv->rev = ec_dev->its_rev; +revocable_get (priv->rev); priv->cmd_offset = ec->cmd_offset; filp->private_data = priv;INIT_LIST_HEAD (&priv->events);
@@ -178,6 +193,7 @@ static int cros_ec_chardev_open(struct inode *inode, struct file *filp) &priv->notifier);if (ret) {dev_err (ec_dev->dev,"failed to register event notifier\n" ); +revocable_put (priv->rev);kfree (priv); }
@@ -251,11 +267,13 @@ static ssize_t cros_ec_chardev_read(struct file *filp, char __user *buffer,static int cros_ec_chardev_release (struct inode *inode,struct file *filp) {struct chardev_priv *priv = filp->private_data; -struct cros_ec_device *ec_dev = priv->ec_dev; +struct cros_ec_device *ec_dev;struct ec_event *event, *e; -blocking_notifier_chain_unregister (&ec_dev->event_notifier, - &priv->notifier); + revocable_try_access_or_skip_scoped(priv->rev, ec_dev) +blocking_notifier_chain_unregister (&ec_dev->event_notifier, + &priv->notifier); +revocable_put (priv->rev);list_for_each_entry_safe (event, e, &priv->events, node) {list_del (&event->node);
@@ -273,6 +291,7 @@ static long cros_ec_chardev_ioctl_xcmd(struct chardev_priv *priv, void __user *a {struct cros_ec_command *s_cmd;struct cros_ec_command u_cmd; +struct cros_ec_device *ec_dev;long ret;if (copy_from_user (&u_cmd, arg,sizeof (u_cmd)))
@@ -299,10 +318,17 @@ static long cros_ec_chardev_ioctl_xcmd(struct chardev_priv *priv, void __user *a } s_cmd->command += priv->cmd_offset; - ret =cros_ec_cmd_xfer (priv->ec_dev, s_cmd); -/* Only copy data to userland if data was received. */ -if (ret <0 ) - goto exit; + revocable_try_access_with_scoped(priv->rev, ec_dev) { +if (!ec_dev) { + ret = -ENODEV ; + goto exit; + } + + ret =cros_ec_cmd_xfer (ec_dev, s_cmd); +/* Only copy data to userland if data was received. */ +if (ret <0 ) + goto exit; + }if (copy_to_user (arg, s_cmd,sizeof (*s_cmd) + s_cmd->insize)) ret = -EFAULT ;
@@ -313,10 +339,12 @@ static long cros_ec_chardev_ioctl_xcmd(struct chardev_priv *priv, void __user *astatic long cros_ec_chardev_ioctl_readmem (struct chardev_priv *priv,void __user *arg) { -struct cros_ec_device *ec_dev = priv->ec_dev; +struct cros_ec_device *ec_dev;struct cros_ec_readmem s_mem = { };long num; +revocable_try_access_or_return (priv->rev, ec_dev); +/* Not every platform supports direct reads */ if (!ec_dev->cmd_readmem)return -ENOTTY ;
@@ -370,11 +398,25 @@ static const struct file_operations chardev_fops = {#endif }; +static void cros_ec_chardev_free (void *data) +{ +struct revocable *rev = data; + +revocable_put (rev); +} +static int cros_ec_chardev_probe (struct platform_device *pdev) {struct cros_ec_dev *ec =dev_get_drvdata (pdev->dev.parent);struct cros_ec_platform *ec_platform =dev_get_platdata (ec->dev); +struct revocable *rev = ec->ec_dev->its_rev;struct miscdevice *misc; +int ret; + +revocable_get (rev); + ret =devm_add_action_or_reset (&pdev->dev, cros_ec_chardev_free, rev); +if (ret) +return ret;/* Create a char device: we want to create it anew */ misc =devm_kzalloc (&pdev->dev,sizeof (*misc),GFP_KERNEL );
Signature · 1 line
2.51.0
Bartosz Golaszewski
maintainerQuoted 24 lines · Tzung-Bi Shih
This series introduces the "revocable" mechanism, a synchronization primitive designed to prevent Use-After-Free errors.
- Patch 1 introduces the revocable which is an implementation of ideas from the talk [1].
- Patch 2 adds KUnit test cases.
- Patches 3 to 7 transitions the UAF prevention logic within the GPIO core (gpiolib) to use the "revocable" mechanism.
The existing code aims to prevent UAF issues when the underlying GPIO chip is removed. They replace that custom logic with the generic "revocable" API, which is designed to handle such lifecycle dependencies. There should be no changes in behavior.
- Patches 8 to 9 uses "revocable" mechanism to fix an UAF in
cros_ec_chardevdriver. Alternatively, [2] is a series for fixing the same issue without using "revocable".Since v9, there are two ways to manage the resource provider handle.
- Embedded allocation: patches 3 to 7 might be the potential user.
- Dynamic allocation: patches 8 to 9 might be the potential user.
Quoted 8 lines
[1] https://lpc.events/event/17/contributions/1627/
[2] https://lore.kernel.org/all/[email protected]---
v9:
- Rebase onto v7.1-rc1.
- Remove the selftests patch as it makes less sense to test revocable APIs via kselftests.
May I suggest kunit for that purpose?
Bart
Bartosz Golaszewski
maintainerQuoted 10 lines · Bartosz Golaszewski
Quoted 6 lines
---
v9:
- Rebase onto v7.1-rc1.
- Remove the selftests patch as it makes less sense to test revocable APIs via kselftests.May I suggest kunit for that purpose?
Bart
Nevermind this, you did exactly that.
Bart
Bartosz Golaszewski
maintainerRe: [PATCH v9 1/9] revocable: Revocable resource management
In reply to Tzung-Bi ShihQuoted 56 lines · Tzung-Bi Shih
The "revocable" mechanism is a synchronization primitive designed to manage safe access to resources that can be asynchronously removed or invalidated. Its primary purpose is to prevent Use-After-Free (UAF) errors when interacting with resources whose lifetimes are not guaranteed to outlast their consumers.
This is particularly useful in systems where resources can disappear unexpectedly, such as those provided by hot-pluggable devices like USB. When a consumer holds a reference to such a resource, the underlying device might be removed, causing the resource's memory to be freed. Subsequent access attempts by the consumer would then lead to UAF errors.
Revocable addresses this by providing a form of "weak reference" and a controlled access method. It allows a resource consumer to safely attempt to access the resource. The mechanism guarantees that any access granted is valid for the duration of its use. If the resource has already been revoked (i.e., freed), the access attempt will fail safely, typically by returning NULL, instead of causing a crash.
It uses a provider/consumer model built on Sleepable RCU (SRCU) to guarantee safe memory access:
- A resource provider, such as a driver for a hot-pluggable device, allocates a struct revocable and initializes it with a pointer to the resource.
- A resource consumer that wants to access the resource allocates a struct
revocable_consumercontaining a reference to the provider.- To access the resource, the consumer uses
revocable_try_access(). This function enters an SRCU read-side critical section and returns the pointer to the resource. If the provider has already freed the resource, it returns NULL. After use, the consumer callsrevocable_withdraw_access()to exit the SRCU critical section. There are some macro level helpers for doing that.The API provides the following contract:
-
revocable_try_access()can be safely called from both process and atomic contexts.
- It is permitted to sleep within the critical section established betweenrevocable_try_access()andrevocable_withdraw_access().
-revocable_try_access()and the matchingrevocable_withdraw_access()must occur in the same context. For example, it is illegal to invokerevocable_withdraw_access()in an irq handler if the matchingrevocable_try_access()was invoked in process context.- When the provider needs to remove the resource, it calls
revocable_revoke(). This function sets the internal resource pointer to NULL and then callssynchronize_srcu()to wait for all current readers to finish before the resource can be completely torn down.Signed-off-by Tzung-Bi Shih <[email protected]>---
...
Quoted 6 lines
include/linux/revocable.h new file +0
-0
include/linux/revocable.h line 10
Quoted 10 lines
include/linux/revocable.h new file +10
-0@@ -0,0 +1,214 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright 2026 Google LLC +*/ + +#ifndef __LINUX_REVOCABLE_H +#define __LINUX_REVOCABLE_H + +#include <linux/cleanup.h> +#include <linux/compiler.h>
I don't think you need this header.
Quoted 6 lines
include/linux/revocable.h new file +6
-0+#include <linux/kref.h> +#include <linux/srcu.h> + +/** +* enum revocable_alloc_type - The allocation method for a revocable provider. +* @REVOCABLE_DYNAMIC: The struct revocable was dynamically allocated using
include/linux/revocable.h line 26
Quoted 10 lines
include/linux/revocable.h new file +10
-0+* revocable_alloc() and its lifetime is managed by +* reference counting. +* @REVOCABLE_EMBEDDED: The struct revocable is embedded within another +* structure. Its lifetime is tied to the parent +* structure and is not reference counted. +*/ +enum revocable_alloc_type { +REVOCABLE_DYNAMIC , +REVOCABLE_EMBEDDED , +};
Maybe we don't need this public enum at all, we could just use a different release callback for kref_put() depending on how the revocable was allocated?
The enum is not used elsewhere so it doesn't make sense to document it as if it was part of the revocable API.
include/linux/revocable.h line 39
Quoted 13 lines
include/linux/revocable.h new file +13
-0+ +/** +* struct revocable - A handle for resource provider. +* @srcu: The SRCU to protect the resource. +* @res: The pointer of resource. It can point to anything. +* @kref: The refcount for this handle. +* @alloc_type: The memory allocation type. +*/ +struct revocable { +struct srcu_struct srcu; +void __rcu *res; +struct kref kref; +enum revocable_alloc_type alloc_type;
This could be replaced with the pointer to the release callback, assigned by revocable_alloc()/revocable_init() respectively.
include/linux/revocable.h line 45
Quoted 6 lines
include/linux/revocable.h new file +6
-0+}; + +/** +* struct revocable_consumer - A handle for resource consumer. +* @rev: The pointer of resource provider. +* @idx: The index for the SRCU critical section.
Should any of these be accessed directly by the user? Maybe document them as __private?
include/linux/revocable.h line 50
Quoted 5 lines
include/linux/revocable.h new file +5
-0+*/ +struct revocable_consumer { +struct revocable *rev; +int idx; +};
I'd rename it to struct revocable_handle which indicates better what it is:
it's a handle *owned* by the consumer.
include/linux/revocable.h line 61
Quoted 11 lines
include/linux/revocable.h new file +11
-0+ +void revocable_get (struct revocable *rev); +void revocable_put (struct revocable *rev); + +struct revocable *revocable_alloc (void *res); +void revocable_revoke (struct revocable *rev); +int revocable_embed_init (struct revocable *rev,void *res); +void revocable_embed_destroy (struct revocable *rev); + +void revocable_init (struct revocable *rev,struct revocable_consumer *rc); +void revocable_deinit (struct revocable_consumer *rc);
If we hid the release logic, we could drop revocable_embed_destroy() and use the same refcounting functions for both variants. I'd suggest the following:
For refcounting (same for both variants):
void revocable_get(struct revocable *rev); void revocable_put(struct revocable *rev);
For dynamic variant:
struct revocable *revocable_alloc(void *res);
For embedded:
int revocable_init(struct revocable *rev, void *res);
For handles:
void revocable_handle_init(struct revocable *rev, struct
revocable_consumer *rc); void revocable_handle_deinit(struct revocable_consumer *rc);
Does it make sense?
Other (try_access_*, etc.) helpers look good. And the API in general looks pretty good to me too.
Bart
Tzung-Bi Shih
maintainerRe: [PATCH v9 1/9] revocable: Revocable resource management
Quoted 11 lines · Bartosz Golaszewski
Quoted 10 lines · Tzung-Bi Shih
include/linux/revocable.h new file +4
-0@@ -0,0 +1,214 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright 2026 Google LLC +*/
Quoted 8 lines
include/linux/revocable.h line 10
Quoted 6 lines
include/linux/revocable.h new file +6
-0+ +#ifndef __LINUX_REVOCABLE_H +#define __LINUX_REVOCABLE_H + +#include <linux/cleanup.h> +#include <linux/compiler.h> I don't think you need this header.
Ack, will remove it in the next version.
Quoted 24 lines
Quoted 7 lines
include/linux/revocable.h new file +6
-0+#include <linux/kref.h> +#include <linux/srcu.h> + +/** +* enum revocable_alloc_type - The allocation method for a revocable provider. +* @REVOCABLE_DYNAMIC: The struct revocable was dynamically allocated using include/linux/revocable.h line 26
Quoted 10 lines
include/linux/revocable.h new file +10
-0+* revocable_alloc() and its lifetime is managed by +* reference counting. +* @REVOCABLE_EMBEDDED: The struct revocable is embedded within another +* structure. Its lifetime is tied to the parent +* structure and is not reference counted. +*/ +enum revocable_alloc_type { +REVOCABLE_DYNAMIC , +REVOCABLE_EMBEDDED , +};Maybe we don't need this public enum at all, we could just use a different release callback for
kref_put()depending on how the revocable was allocated?The enum is not used elsewhere so it doesn't make sense to document it as if it was part of the revocable API.
Quoted 17 lines
include/linux/revocable.h line 39
Quoted 13 lines
include/linux/revocable.h new file +13
-0+ +/** +* struct revocable - A handle for resource provider. +* @srcu: The SRCU to protect the resource. +* @res: The pointer of resource. It can point to anything. +* @kref: The refcount for this handle. +* @alloc_type: The memory allocation type. +*/ +struct revocable { +struct srcu_struct srcu; +void __rcu *res; +struct kref kref; +enum revocable_alloc_type alloc_type;This could be replaced with the pointer to the release callback, assigned by revocable_alloc()/revocable_init() respectively.
Ack, will remove the enum. A boolean is sufficient given that the allocation type is binary.
Quoted 9 lines
include/linux/revocable.h line 45
Quoted 6 lines
include/linux/revocable.h new file +6
-0+}; + +/** +* struct revocable_consumer - A handle for resource consumer. +* @rev: The pointer of resource provider. +* @idx: The index for the SRCU critical section. Should any of these be accessed directly by the user? Maybe document them as
__private?
I don't think that is necessary. All members in both struct revocable and struct revocable_consumer are intended to be opaque and should not be accessed directly by users. However, I made them public structures because:
- The try_access_* macros need to allocate a struct revocable_consumer locally.
- KUnit tests require access to these members for verification.
I can add a comment to the structure definitions noting that they should be treated as private. Does it make sense?
Quoted 9 lines
include/linux/revocable.h line 50
Quoted 6 lines
include/linux/revocable.h new file +5
-0+*/ +struct revocable_consumer { +struct revocable *rev; +int idx; +};I'd rename it to struct
revocable_handlewhich indicates better what it is:
it's a handle *owned* by the consumer.
Ack, will rename it.
Quoted 29 lines
include/linux/revocable.h line 61
Quoted 12 lines
include/linux/revocable.h new file +11
-0+ +void revocable_get (struct revocable *rev); +void revocable_put (struct revocable *rev); + +struct revocable *revocable_alloc (void *res); +void revocable_revoke (struct revocable *rev); +int revocable_embed_init (struct revocable *rev,void *res); +void revocable_embed_destroy (struct revocable *rev); + +void revocable_init (struct revocable *rev,struct revocable_consumer *rc); +void revocable_deinit (struct revocable_consumer *rc);If we hid the release logic, we could drop
revocable_embed_destroy()and use the same refcounting functions for both variants. I'd suggest the following:For refcounting (same for both variants):
void revocable_get(struct revocable *rev); void revocable_put(struct revocable *rev);For dynamic variant:
struct revocable *revocable_alloc(void *res);For embedded:
int revocable_init(struct revocable *rev, void *res);
Quoted 7 lines
For handles:
void revocable_handle_init(struct revocable *rev, struct
revocable_consumer *rc); void revocable_handle_deinit(struct revocable_consumer *rc);Does it make sense?
That makes sense. I'll fix this in the next version.