drivers/base: Introduce revocable
Subsystems (4)
v8 cover drivers/base: Introduce revocable
The series is separated from [1] to show the independency and compare potential use cases easier. This is the revocable core part. Use cases are in other series.
The 1st patch introduces the revocable which is an implementation of ideas from the talk [2].
The 2nd and 3rd patches add test cases for revocable in KUnit and selftest.
[1] https://lore.kernel.org/all/[email protected]
[2] https://lpc.events/event/17/contributions/1627/
---
v8:
- 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/chrome-platform/[email protected]
- Rebase onto next-20251106.
- Separate revocable core and use cases.
v5: https://lore.kernel.org/chrome-platform/[email protected]
- Rebase onto next-20251015.
- Add more context about the PoC.
- Support multiple revocable providers in the PoC.
v4: https://lore.kernel.org/chrome-platform/[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/chrome-platform/[email protected]
- Rebase onto https://lore.kernel.org/chrome-platform/[email protected]
and next-20250912.
- The 4th patch changed accordingly.
v2: https://lore.kernel.org/chrome-platform/[email protected]
- Rename "ref_proxy" -> "revocable".
- Add test cases in Kunit and selftest.
v1: https://lore.kernel.org/chrome-platform/[email protected]
Tzung-Bi Shih (3):
revocable: Revocable resource management
revocable: Add KUnit test cases
selftests: revocable: Add kselftest cases
.../driver-api/driver-model/index.rst | 1 + .../driver-api/driver-model/revocable.rst | 256 +++++++++++ MAINTAINERS | 11 + drivers/base/Kconfig | 5 + drivers/base/Makefile | 5 +- drivers/base/revocable.c | 228 ++++++++++ drivers/base/revocable_test.c | 410 ++++++++++++++++++ include/linux/revocable.h | 194 +++++++++ tools/testing/selftests/revocable/Makefile | 7 + .../selftests/revocable/revocable_test.c | 177 ++++++++ .../selftests/revocable/revocable_test.h | 20 + .../selftests/revocable/test-revocable.sh | 34 ++ .../selftests/revocable/test_modules/Makefile | 10 + .../revocable/test_modules/revocable_test.c | 234 ++++++++++ 14 files changed, 1591 insertions(+), 1 deletion(-) 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 create mode 100644 tools/testing/selftests/revocable/Makefile create mode 100644 tools/testing/selftests/revocable/revocable_test.c create mode 100644 tools/testing/selftests/revocable/revocable_test.h create mode 100755 tools/testing/selftests/revocable/test-revocable.sh create mode 100644 tools/testing/selftests/revocable/test_modules/Makefile create mode 100644 tools/testing/selftests/revocable/test_modules/revocable_test.c
Signature · 1 line
2.53.0.310.g728cabbaf7-goog
PATCH v8 1/3 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.
---
v8:
- 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/chrome-platform/[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/chrome-platform/[email protected]
- No changes.
v4: https://lore.kernel.org/chrome-platform/[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/chrome-platform/[email protected]
- No changes.
v2: https://lore.kernel.org/chrome-platform/[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/chrome-platform/[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 | 256 ++++++++++++++++++ MAINTAINERS | 9 + drivers/base/Makefile | 2 +- drivers/base/revocable.c | 228 ++++++++++++++++ include/linux/revocable.h | 194 +++++++++++++ 6 files changed, 689 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,6 +14,7 @@ Driver Model overview platform porting + revocable .. only:: subproject and html
Documentation/driver-api/driver-model/revocable.rst new file +256 -0
256 lines not on this page · raw message
MAINTAINERS +9 -0
@@ -7702,6 +7702,7 @@ F: include/linux/device.h F: include/linux/fwnode.h F: include/linux/kobj* F: include/linux/property.h +F: include/linux/revocable.h F: include/linux/sysfs.h F: lib/kobj* F: rust/kernel/debugfs.rs
@@ -22407,6 +22408,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 +228 -0
228 lines not on this page · raw message
include/linux/revocable.h new file +194 -0
194 lines not on this page · raw message
Signature · 1 line
2.53.0.310.g728cabbaf7-goog
Re: [PATCH v8 1/3] revocable: Revocable resource management
Quoted 6 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.
[snip]
Quoted 2 lines
include/linux/revocable.h +1
-0+
[snip]
include/linux/revocable.h line 38
Quoted 13 lines
include/linux/revocable.h +13
-0+/** +* struct revocable_consumer - A handle for resource consumer. +* @rp: The pointer of resource provider. +* @idx: The index for the SRCU critical section. +*/ +struct revocable_consumer { +struct revocable *rp; +int idx; +}; + +void revocable_get (struct revocable *rp); +void revocable_put (struct revocable *rp); +struct revocable *revocable_alloc (void *res);
Is there any reason why we couldn't do:
int revocable_init(struct revocable *rp, void *res);
and then:
struct gpio_device {
struct revocable rp;
...
};revocable_init(&gdev->rp, gc);
? It would be functionally equivalent to what we're doing now in GPIO.
Bartosz
Re: [PATCH v8 1/3] revocable: Revocable resource management
Quoted 26 lines · Bartosz Golaszewski
Quoted 2 lines · Tzung-Bi Shih
include/linux/revocable.h +1
-0+[snip]
include/linux/revocable.h line 38
Quoted 13 lines
include/linux/revocable.h +13
-0+/** +* struct revocable_consumer - A handle for resource consumer. +* @rp: The pointer of resource provider. +* @idx: The index for the SRCU critical section. +*/ +struct revocable_consumer { +struct revocable *rp; +int idx; +}; + +void revocable_get (struct revocable *rp); +void revocable_put (struct revocable *rp); +struct revocable *revocable_alloc (void *res);Is there any reason why we couldn't do:
int
revocable_init(struct revocable *rp, void *res);and then:
Quoted 8 lines
struct gpio_device { struct revocable rp; ... };
revocable_init(&gdev->rp, gc);? It would be functionally equivalent to what we're doing now in GPIO.
In the case of struct gpio_device, it is possible to embed struct revocable directly. Since the lifetime of gpio_device matches the revocable object, the reference counting mechanism in struct revocable is redundant in this specific instance.
However, the design allows for cases where there is no persistent parent object. For example, a revocable handle may exist in the dev-managed context of a kernel thread. In such cases, consumers are required to hold a reference to the handle.
PATCH v8 2/3 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.
---
v8:
- 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/chrome-platform/[email protected]
- Rename REVOCABLE_TRY_ACCESS_WITH() -> REVOCABLE_TRY_ACCESS_SCOPED().
- Add tests for new REVOCABLE_TRY_ACCESS_WITH().
v5: https://lore.kernel.org/chrome-platform/[email protected]
- No changes.
v4: https://lore.kernel.org/chrome-platform/[email protected]
- REVOCABLE() -> REVOCABLE_TRY_ACCESS_WITH().
- revocable_release() -> revocable_withdraw_access().
v3: https://lore.kernel.org/chrome-platform/[email protected]
- No changes.
v2: https://lore.kernel.org/chrome-platform/[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_testMAINTAINERS | 1 + drivers/base/Kconfig | 5 + drivers/base/Makefile | 3 + drivers/base/revocable_test.c | 410 ++++++++++++++++++++++++++++++++++ 4 files changed, 419 insertions(+) create mode 100644 drivers/base/revocable_test.c
MAINTAINERS +1 -0
@@ -22414,6 +22414,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
@@ -250,3 +250,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 +410 -0
410 lines not on this page · raw message
Signature · 1 line
2.53.0.310.g728cabbaf7-goog
PATCH v8 3/3 selftests: revocable: Add kselftest cases
In reply to Tzung-Bi ShihAdd kselftest cases for the revocable API.
The test consists of three parts:
- A kernel module (revocable_test.ko) that creates a debugfs file.
- A user-space C program (revocable_test) that uses the kselftest harness to interact with the debugfs file.
- An orchestrating shell script (test-revocable.sh) that loads the module, runs the C program, and unloads the module.
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.
---
v8:
- Squash:
- 4d7dc4d1a62d revocable: Fix races in revocable_alloc() using RCU
- 377563ce0653 revocable: fix SRCU index corruption by requiring caller-provided storage
- Change accordingly due to its dependency "revocable: Revocable resource management" changes.
- Move:
- tools/testing/selftests/drivers/base/revocable/ -> tools/testing/selftests/revocable/.
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/chrome-platform/[email protected]
- Rename REVOCABLE_TRY_ACCESS_WITH() -> REVOCABLE_TRY_ACCESS_SCOPED().
- Add tests for new REVOCABLE_TRY_ACCESS_WITH().
v5: https://lore.kernel.org/chrome-platform/[email protected]
- No changes.
v4: https://lore.kernel.org/chrome-platform/[email protected]
- REVOCABLE() -> REVOCABLE_TRY_ACCESS_WITH().
- revocable_release() -> revocable_withdraw_access().
v3: https://lore.kernel.org/chrome-platform/[email protected]
- No changes.
v2: https://lore.kernel.org/chrome-platform/[email protected]
- New in the series.
MAINTAINERS | 1 + tools/testing/selftests/revocable/Makefile | 7 + .../selftests/revocable/revocable_test.c | 177 +++++++++++++ .../selftests/revocable/revocable_test.h | 20 ++ .../selftests/revocable/test-revocable.sh | 34 +++ .../selftests/revocable/test_modules/Makefile | 10 + .../revocable/test_modules/revocable_test.c | 234 ++++++++++++++++++ 7 files changed, 483 insertions(+) create mode 100644 tools/testing/selftests/revocable/Makefile create mode 100644 tools/testing/selftests/revocable/revocable_test.c create mode 100644 tools/testing/selftests/revocable/revocable_test.h create mode 100755 tools/testing/selftests/revocable/test-revocable.sh create mode 100644 tools/testing/selftests/revocable/test_modules/Makefile create mode 100644 tools/testing/selftests/revocable/test_modules/revocable_test.c
MAINTAINERS +1 -0
@@ -22416,6 +22416,7 @@ 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 +F: tools/testing/selftests/revocable/ RFKILL M: Johannes Berg <[email protected]>
tools/testing/selftests/revocable/Makefile new file +7 -0
@@ -0,0 +1,7 @@ +# SPDX-License-Identifier: GPL-2.0 + +TEST_GEN_MODS_DIR := test_modules +TEST_GEN_PROGS_EXTENDED := revocable_test +TEST_PROGS := test-revocable.sh + +include ../lib.mk
tools/testing/selftests/revocable/revocable_test.c new file +177 -0
177 lines not on this page · raw message
tools/testing/selftests/revocable/revocable_test.h new file +20 -0
@@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright 2026 Google LLC +*/ + +#ifndef __REVOCABLE_TEST_H +#define __REVOCABLE_TEST_H + +#define TEST_DATA "12345678" +#define TEST_DATA_NULL "(null)" + +#define TEST_MAGIC_OFFSET_RAW 0x0 +#define TEST_MAGIC_OFFSET_MACRO_TRY_ACCESS_WITH 0x1 +#define TEST_MAGIC_OFFSET_MACRO_TRY_ACCESS_OR_RETURN_ERR 0x2 +#define TEST_MAGIC_OFFSET_MACRO_TRY_ACCESS_OR_RETURN 0x3 +#define TEST_MAGIC_OFFSET_MACRO_TRY_ACCESS_WITH_SCOPED 0x4 +#define TEST_MAGIC_OFFSET_MACRO_TRY_ACCESS_OR_RETURN_ERR_SCOPED 0x5 +#define TEST_MAGIC_OFFSET_MACRO_TRY_ACCESS_OR_RETURN_SCOPED 0x6 + +#endif /* __REVOCABLE_TEST_H */
tools/testing/selftests/revocable/test-revocable.sh new file +34 -0
@@ -0,0 +1,34 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 + +mod_name="revocable_test" +ksft_fail=1 +ksft_skip=4 + +if [ "$(id -u)" -ne 0 ]; then + echo "$0: Must be run as root" + exit "$ksft_skip" +fi + +if ! which insmod > /dev/null 2>&1; then + echo "$0: Need insmod" + exit "$ksft_skip" +fi + +if ! which rmmod > /dev/null 2>&1; then + echo "$0: Need rmmod" + exit "$ksft_skip" +fi + +if ! mountpoint -q /sys/kernel/debug; then + mount -t debugfs none /sys/kernel/debug +fi + +insmod test_modules/"${mod_name}".ko + +./revocable_test +ret=$? + +rmmod "${mod_name}" + +exit "${ret}"
tools/testing/selftests/revocable/test_modules/Makefile new file +10 -0
@@ -0,0 +1,10 @@ +TESTMODS_DIR := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST))))) +KDIR ?= /lib/modules/$(shell uname -r)/build + +obj-m += revocable_test.o + +all: + $(Q)$(MAKE) -C $(KDIR) M=$(TESTMODS_DIR) + +clean: + $(Q)$(MAKE) -C $(KDIR) M=$(TESTMODS_DIR) clean
tools/testing/selftests/revocable/test_modules/revocable_test.c new file +234 -0
234 lines not on this page · raw message
Signature · 1 line
2.53.0.310.g728cabbaf7-goog