drivers/base: Introduce revocable
Subsystems (5)
v7 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/chrome-platform/[email protected]
[2] https://lpc.events/event/17/contributions/1627/
v7:
- 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 | 152 +++++++++++ MAINTAINERS | 9 + drivers/base/Kconfig | 8 + drivers/base/Makefile | 5 +- drivers/base/revocable.c | 242 ++++++++++++++++++ drivers/base/revocable_test.c | 139 ++++++++++ include/linux/revocable.h | 69 +++++ tools/testing/selftests/Makefile | 1 + .../selftests/drivers/base/revocable/Makefile | 7 + .../drivers/base/revocable/revocable_test.c | 136 ++++++++++ .../drivers/base/revocable/test-revocable.sh | 39 +++ .../base/revocable/test_modules/Makefile | 10 + .../revocable/test_modules/revocable_test.c | 195 ++++++++++++++ 14 files changed, 1012 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/drivers/base/revocable/Makefile create mode 100644 tools/testing/selftests/drivers/base/revocable/revocable_test.c create mode 100755 tools/testing/selftests/drivers/base/revocable/test-revocable.sh create mode 100644 tools/testing/selftests/drivers/base/revocable/test_modules/Makefile create mode 100644 tools/testing/selftests/drivers/base/revocable/test_modules/revocable_test.c
Signature · 1 line
2.52.0.457.g6b5491de43-goog
PATCH v7 1/3 revocable: Revocable resource management
Some resources can be removed asynchronously, for example, resources provided by a hot-pluggable device like USB. When holding a reference to such a resource, it's possible for the resource to be removed and its memory freed, leading to use-after-free errors on subsequent access.
The "revocable" mechanism addresses this by establishing a weak reference to a resource that might be freed at any time. It allows a resource consumer to safely attempt to access the resource, guaranteeing that the access is valid for the duration of its use, or it fails safely if the resource has already been revoked.
The implementation 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_provider and initializes it with a pointer to the resource.
- A resource consumer that wants to access the resource allocates a struct revocable which acts as a handle 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. The REVOCABLE_TRY_ACCESS_WITH() and REVOCABLE_TRY_ACCESS_SCOPED() are convenient helpers for doing that.
- When the provider needs to remove the resource, it calls revocable_provider_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.
---
v7:
- "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]
Note (for my reference):
- An optional .release() callback for revocable provider-managed resource hasn't added.
- make O=build SPHINXDIRS=driver-api/driver-model/ htmldocs a way to verify the Documentation/.
.../driver-api/driver-model/index.rst | 1 + .../driver-api/driver-model/revocable.rst | 152 +++++++++++ MAINTAINERS | 7 + drivers/base/Makefile | 2 +- drivers/base/revocable.c | 242 ++++++++++++++++++ include/linux/revocable.h | 69 +++++ 6 files changed, 472 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 +152 -0
152 lines not on this page · raw message
MAINTAINERS +7 -0
@@ -22517,6 +22517,13 @@ 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 +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 +242 -0
242 lines not on this page · raw message
include/linux/revocable.h new file +69 -0
69 lines not on this page · raw message
Signature · 1 line
2.52.0.457.g6b5491de43-goog
Danilo Krummrich
maintainerRe: [PATCH v7 1/3] revocable: Revocable resource management
Quoted 5 lines · Tzung-Bi Shih
MAINTAINERS +0
-0
MAINTAINERS F: include/uapi/linux/rseq.h · line 22525
Quoted 9 lines
MAINTAINERS +6
-0@@ -22517,6 +22517,13 @@ 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 +F: drivers/base/revocable.c +F: include/linux/revocable.h
NIT: I think we should add this include to the DRIVER CORE entry as well.
Bartosz Golaszewski
maintainerRe: [PATCH v7 1/3] revocable: Revocable resource management
Quoted 10 lines · Danilo Krummrich
Quoted 8 lines · Tzung-Bi Shih
MAINTAINERS +0
-0@@ -22517,6 +22517,13 @@ F: include/uapi/linux/rseq.h F: kernel/rseq.c F: tools/testing/selftests/rseq/
Quoted 8 lines
MAINTAINERS F: include/uapi/linux/rseq.h · line 22525
Quoted 6 lines
MAINTAINERS +6
-0+REVOCABLE RESOURCE MANAGEMENT +M: Tzung-Bi Shih <[email protected]> +L: [email protected] +S: Maintained +F: drivers/base/revocable.c +F: include/linux/revocable.hNIT: I think we should add this include to the DRIVER CORE entry as well.
FWIW: I'm not even sure drivers/base/ is the right place for this. Except for a few devm_ helpers, nothing here is inherently tied into the driver model This could be useful outside of device drivers and I would suggest to put it under lib/ with devres factored out into a separate source file.
Bartosz
Danilo Krummrich
maintainerRe: [PATCH v7 1/3] revocable: Revocable resource management
Quoted 20 lines · Bartosz Golaszewski
Quoted 10 lines · Danilo Krummrich
Quoted 8 lines · Tzung-Bi Shih
MAINTAINERS +0
-0@@ -22517,6 +22517,13 @@ F: include/uapi/linux/rseq.h F: kernel/rseq.c F: tools/testing/selftests/rseq/Quoted 8 lines
MAINTAINERS F: include/uapi/linux/rseq.h · line 22525
Quoted 6 lines
MAINTAINERS +6
-0+REVOCABLE RESOURCE MANAGEMENT +M: Tzung-Bi Shih <[email protected]> +L: [email protected] +S: Maintained +F: drivers/base/revocable.c +F: include/linux/revocable.hNIT: I think we should add this include to the DRIVER CORE entry as well.
Quoted 5 lines
FWIW: I'm not even sure
drivers/base/is the right place for this. Except for a fewdevm_helpers, nothing here is inherently tied into the driver model This could be useful outside of device drivers and I would suggest to put it underlib/with devres factored out into a separate source file.
It is indeed correct that in the end revocable is more of a generic synchronization primitve.
In fact, the Revocable type in Rust is not listed under driver core infrastructure either. However, it was introduced for use with Devres (which is still the only user).
I think the, by far, most common use-case where a resource is revoked from an entity are device resources revoked by devres.
So, I'd say let's factor it out once we see interest by more users?
Re: [PATCH v7 1/3] revocable: Revocable resource management
In reply to Bartosz GolaszewskiQuoted 19 lines · Bartosz Golaszewski
Quoted 9 lines · Danilo Krummrich
Quoted 8 lines · Tzung-Bi Shih
MAINTAINERS +0
-0@@ -22517,6 +22517,13 @@ F: include/uapi/linux/rseq.h F: kernel/rseq.c F: tools/testing/selftests/rseq/Quoted 8 lines
MAINTAINERS F: include/uapi/linux/rseq.h · line 22525
Quoted 6 lines
MAINTAINERS +6
-0+REVOCABLE RESOURCE MANAGEMENT +M: Tzung-Bi Shih <[email protected]> +L: [email protected] +S: Maintained +F: drivers/base/revocable.c +F: include/linux/revocable.hNIT: I think we should add this include to the DRIVER CORE entry as well.
Quoted 5 lines
FWIW: I'm not even sure
drivers/base/is the right place for this. Except for a fewdevm_helpers, nothing here is inherently tied into the driver model This could be useful outside of device drivers and I would suggest to put it underlib/with devres factored out into a separate source file.
I agree. Based on the discussions we had at LPC, the revocable resource management API is not the right solution to handle races between device removal and userspace access. It is however a possibly useful tool for races between producers and consumers *inside the kernel*. lib/ is a better location.
Signature · 3 lines
Regards, Laurent Pinchart
Danilo Krummrich
maintainerRe: [PATCH v7 1/3] revocable: Revocable resource management
Quoted 3 lines · Laurent Pinchart
Based on the discussions we had at LPC, the revocable resource management API is not the right solution to handle races between device removal and userspace access.
Please see: https://lore.kernel.org/all/[email protected]/
Quoted 2 lines
It is however a possibly useful tool for races between producers and consumers *inside the kernel*.
Do you have an example for such a case?
Bartosz Golaszewski
maintainerRe: [PATCH v7 1/3] revocable: Revocable resource management
Quoted 6 lines · Danilo Krummrich
Quoted 3 lines · Laurent Pinchart
Based on the discussions we had at LPC, the revocable resource management API is not the right solution to handle races between device removal and userspace access.
Quoted 6 lines
Please see: https://lore.kernel.org/all/[email protected]/
Quoted 2 lines
It is however a possibly useful tool for races between producers and consumers *inside the kernel*.
Do you have an example for such a case?
Isn't the GPIO use-case - which the series on top of it addresses - one?
With fw_devlink=off it's quite easy to trigger all kinds of crashes with in-kernel users. I suggested using revocable in GPIO myself but I won't accept it as long as it impacts performance over "naked" SRCU.
Bart
Re: [PATCH v7 1/3] revocable: Revocable resource management
Quoted 12 lines · Bartosz Golaszewski
Quoted 11 lines · Danilo Krummrich
Quoted 3 lines · Laurent Pinchart
Based on the discussions we had at LPC, the revocable resource management API is not the right solution to handle races between device removal and userspace access.
Please see: https://lore.kernel.org/all/[email protected]/
Quoted 2 lines
It is however a possibly useful tool for races between producers and consumers *inside the kernel*.
Quoted 6 lines
Quoted 1 line
Do you have an example for such a case?
Isn't the GPIO use-case - which the series on top of it addresses - one?
With fw_devlink=off it's quite easy to trigger all kinds of crashes with in-kernel users.
Does this series solve that? It looked to me like it just replaces the existing SRCU with a wrapper?
Jason
Bartosz Golaszewski
maintainerRe: [PATCH v7 1/3] revocable: Revocable resource management
Quoted 16 lines · Jason Gunthorpe
Quoted 14 lines · Bartosz Golaszewski
Quoted 12 lines · Danilo Krummrich
Quoted 3 lines · Laurent Pinchart
Based on the discussions we had at LPC, the revocable resource management API is not the right solution to handle races between device removal and userspace access.
Please see: https://lore.kernel.org/all/[email protected]/
Quoted 2 lines
It is however a possibly useful tool for races between producers and consumers *inside the kernel*.
Do you have an example for such a case?
Quoted 8 lines
Quoted 4 lines
Isn't the GPIO use-case - which the series on top of it addresses - one?
With fw_devlink=off it's quite easy to trigger all kinds of crashes with in-kernel users.
Does this series solve that? It looked to me like it just replaces the existing SRCU with a wrapper?
SRCU already *did* solve it. Revocable *is* a wrapper around SRCU that generalizes the initial solution.
Replacing SRCU with a generalized wrapper is fine but there are subsystems out there, where the problem is much less trivial. Take I2C for example: the struct device management is so broken that there isn't even anything *to revoke* yet. It'll take years of little reworks before we can even use revocable at all.
I'm not against it as a library of functions. But TBH I looked at the series and - besides making the code run slower - it also kind of makes it harder to read. With *naked* SRCU it's very clear what's going on, when you start hiding the logic, it becomes needlessly obfuscated.
I want to first see revocable match current GPIO performance and then we can talk about accepting it.
Bartosz
Bartosz Golaszewski
maintainerRe: [PATCH v7 1/3] revocable: Revocable resource management
In reply to Tzung-Bi ShihQuoted 40 lines · Tzung-Bi Shih
Some resources can be removed asynchronously, for example, resources provided by a hot-pluggable device like USB. When holding a reference to such a resource, it's possible for the resource to be removed and its memory freed, leading to use-after-free errors on subsequent access.
The "revocable" mechanism addresses this by establishing a weak reference to a resource that might be freed at any time. It allows a resource consumer to safely attempt to access the resource, guaranteeing that the access is valid for the duration of its use, or it fails safely if the resource has already been revoked.
The implementation 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_providerand initializes it with a pointer to the resource.- A resource consumer that wants to access the resource allocates a struct revocable which acts as a handle 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 callsrevocable_withdraw_access()to exit the SRCU critical section. TheREVOCABLE_TRY_ACCESS_WITH()andREVOCABLE_TRY_ACCESS_SCOPED()are convenient helpers for doing that.- When the provider needs to remove the resource, it calls
revocable_provider_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.Acked-by Danilo Krummrich <[email protected]>Signed-off-by Tzung-Bi Shih <[email protected]>---
[snip]
Quoted 62 lines
include/linux/revocable.h new file +56
-0@@ -0,0 +1,69 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright 2026 Google LLC +*/ + +#ifndef __LINUX_REVOCABLE_H +#define __LINUX_REVOCABLE_H + +#include <linux/compiler.h> +#include <linux/cleanup.h> + +struct device ; +struct revocable ; +struct revocable_provider ; + +struct revocable_provider *revocable_provider_alloc (void *res); +void revocable_provider_revoke (struct revocable_provider *rp); +struct revocable_provider *devm_revocable_provider_alloc (struct device *dev, +void *res); + +struct revocable *revocable_alloc (struct revocable_provider *rp); +void revocable_free (struct revocable *rev); +void *revocable_try_access(struct revocable *rev) __acquires(&rev->rp->srcu); +void revocable_withdraw_access(struct revocable *rev) __releases(&rev->rp->srcu); + +DEFINE_FREE(access_rev, struct revocable *, if (_T) revocable_withdraw_access(_T)) + +/** +* REVOCABLE_TRY_ACCESS_WITH() - A helper for accessing revocable resource +* @_rev: The consumer's ``struct revocable *`` handle. +* @_res: A pointer variable that will be assigned the resource. +* +* The macro simplifies the access-release cycle for consumers, ensuring that +* revocable_withdraw_access() is always called, even in the case of an early +* exit. +* +* It creates a local variable in the current scope. @_res is populated with +* the result of revocable_try_access(). The consumer code **must** check if +* @_res is ``NULL`` before using it. The revocable_withdraw_access() function +* is automatically called when the scope is exited. +* +* Note: It shares the same issue with guard() in cleanup.h. No goto statements +* are allowed before the helper. Otherwise, the compiler fails with +* "jump bypasses initialization of variable with __attribute__((cleanup))". + */ +#define REVOCABLE_TRY_ACCESS_WITH (_rev, _res) \ + struct revocable *__UNIQUE_ID(name) __free(access_rev) = _rev; \ + _res = revocable_try_access(_rev) + +#define _REVOCABLE_TRY_ACCESS_SCOPED (_rev, _label, _res) \ + for (struct revocable *__UNIQUE_ID(name) __free(access_rev) = _rev; \ + (_res = revocable_try_access(_rev)) || true; ({ goto _label; })) \ + if (0) { \ +_label: \ + break; \ + } else
include/linux/revocable.h line 66
Quoted 10 lines
include/linux/revocable.h new file +10
-0+ +/** +* REVOCABLE_TRY_ACCESS_SCOPED() - A helper for accessing revocable resource +* @_rev: The consumer's ``struct revocable *`` handle. +* @_res: A pointer variable that will be assigned the resource. +* +* Similar to REVOCABLE_TRY_ACCESS_WITH() but with an explicit scope from a +* temporary ``for`` loop. +*/ +#define REVOCABLE_TRY_ACCESS_SCOPED (_rev, _res) \
Seeing this code used in GPIO, I would suggest a coding-style change before it makes its way into other places:
revocable_try_access_with() looks better and is in line with most other macros. There's no reason for capitalisation IMO and it makes the code using it look like there was some issue with it, as if this was a WARN() or something.
I'd suggest changing all these macros to using lowercase and queuing it for v7.0.
Bartosz
Quoted 6 lines
include/linux/revocable.h new file +3
-1+ _REVOCABLE_TRY_ACCESS_SCOPED(_rev, __UNIQUE_ID(label), _res) + +#endif /* __LINUX_REVOCABLE_H */ --2.52.0.457.g6b5491de43-goog
PATCH v7 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 provided via the provider.
- 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 REVOCABLE_TRY_ACCESS_WITH() and REVOCABLE_TRY_ACCESS_SCOPED().
A way to run the test:
$ ./tools/testing/kunit/kunit.py run \
--kconfig_add CONFIG_REVOCABLE_KUNIT_TEST=y \
revocable_test---
v7:
- "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 + drivers/base/Kconfig | 8 ++ drivers/base/Makefile | 3 + drivers/base/revocable_test.c | 139 ++++++++++++++++++++++++++++++++++ 4 files changed, 151 insertions(+) create mode 100644 drivers/base/revocable_test.c
MAINTAINERS +1 -0
@@ -22522,6 +22522,7 @@ M: Tzung-Bi Shih <[email protected]> L: [email protected] S: Maintained F: drivers/base/revocable.c +F: drivers/base/revocable_test.c F: include/linux/revocable.h RFKILL
drivers/base/Kconfig +8 -0
@@ -250,3 +250,11 @@ config FW_DEVLINK_SYNC_STATE_TIMEOUT work on. endmenu + +# Kunit test cases +config REVOCABLE_KUNIT_TEST + tristate "Kunit tests for revocable" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS + help + Kunit tests for the revocable API.
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 +139 -0
139 lines not on this page · raw message
Signature · 1 line
2.52.0.457.g6b5491de43-goog
PATCH v7 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 interface with /provider and /consumer files.
- A user-space C program (revocable_test) that uses the kselftest harness to interact with the debugfs files.
- 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 provided via the provider.
- 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 REVOCABLE_TRY_ACCESS_WITH() and REVOCABLE_TRY_ACCESS_SCOPED().
---
v7:
- "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 kselftest (for my reference):
- Update kernel to the DUT.
- mkdir build and copy the kernel config to build/.
- make O=build LLVM=1 -j32 (for generated headers and built-in symbols).
- make O=build LLVM=1 KDIR=$(pwd) -C tools/testing/selftests/ TARGETS=drivers/base/revocable gen_tar.
- Copy build/kselftest/kselftest_install/kselftest-packages/kselftest.tar.gz to the DUT, extract, and execute the run_kselftest.sh.
MAINTAINERS | 1 + tools/testing/selftests/Makefile | 1 + .../selftests/drivers/base/revocable/Makefile | 7 + .../drivers/base/revocable/revocable_test.c | 136 ++++++++++++ .../drivers/base/revocable/test-revocable.sh | 39 ++++ .../base/revocable/test_modules/Makefile | 10 + .../revocable/test_modules/revocable_test.c | 195 ++++++++++++++++++ 7 files changed, 389 insertions(+) create mode 100644 tools/testing/selftests/drivers/base/revocable/Makefile create mode 100644 tools/testing/selftests/drivers/base/revocable/revocable_test.c create mode 100755 tools/testing/selftests/drivers/base/revocable/test-revocable.sh create mode 100644 tools/testing/selftests/drivers/base/revocable/test_modules/Makefile create mode 100644 tools/testing/selftests/drivers/base/revocable/test_modules/revocable_test.c
MAINTAINERS +1 -0
@@ -22524,6 +22524,7 @@ S: Maintained F: drivers/base/revocable.c F: drivers/base/revocable_test.c F: include/linux/revocable.h +F: tools/testing/selftests/drivers/base/revocable/ RFKILL M: Johannes Berg <[email protected]>
tools/testing/selftests/Makefile +1 -0
@@ -17,6 +17,7 @@ TARGETS += damon TARGETS += devices/error_logs TARGETS += devices/probe TARGETS += dmabuf-heaps +TARGETS += drivers/base/revocable TARGETS += drivers/dma-buf TARGETS += drivers/ntsync TARGETS += drivers/s390x/uvdevice
tools/testing/selftests/drivers/base/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/drivers/base/revocable/revocable_test.c new file +136 -0
136 lines not on this page · raw message
tools/testing/selftests/drivers/base/revocable/test-revocable.sh new file +39 -0
@@ -0,0 +1,39 @@ +#!/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 + +insmod test_modules/"${mod_name}".ko + +if [ ! -d /sys/kernel/debug/revocable_test/ ]; then + mount -t debugfs none /sys/kernel/debug/ + + if [ ! -d /sys/kernel/debug/revocable_test/ ]; then + echo "$0: Error mounting debugfs" + exit "$ksft_fail" + fi +fi + +./revocable_test +ret=$? + +rmmod "${mod_name}" + +exit "${ret}"
tools/testing/selftests/drivers/base/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/drivers/base/revocable/test_modules/revocable_test.c new file +195 -0
195 lines not on this page · raw message
Signature · 1 line
2.52.0.457.g6b5491de43-goog
Greg Kroah-Hartman
maintainerQuoted 7 lines · Tzung-Bi Shih
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].
Quoted 7 lines
The 2nd and 3rd patches add test cases for revocable in Kunit and selftest.
[1] https://lore.kernel.org/chrome-platform/[email protected]
[2] https://lpc.events/event/17/contributions/1627/v7:
- Rebase onto next-20260115.
Thanks for sticking with this, I've queued this up in the driver-core testing branch for now, thanks!
greg k-h
Quoted 10 lines · Greg KH
Quoted 9 lines · Tzung-Bi Shih
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.
Quoted 8 lines
Quoted 5 lines
[1] https://lore.kernel.org/chrome-platform/[email protected]
[2] https://lpc.events/event/17/contributions/1627/v7:
- Rebase onto next-20260115.Thanks for sticking with this, I've queued this up in the driver-core testing branch for now, thanks!
I'd like to see this being successfully used before we merge it. There's now one patch series for the GPIO subsystem, which I think is a very good test case. Bartosz is already reviewing it, and there's a performance issue that needs to be investigated and addressed.
As discussed during LPC, the UAF issue in the CrOS EC that this series was initially supposed to fix is best solved with reference counting and refactoring of the CrOS EC driver framework. The revocable mechanism isn't the right solution for races between device removal and userspace access.
Signature · 3 lines
Regards, Laurent Pinchart
Danilo Krummrich
maintainerQuoted 2 lines · Laurent Pinchart
The revocable mechanism isn't the right solution for races between device removal and userspace access.
I think you have to differenciate, as it depends on the resource:
If the resource is a device resource (e.g. MMIO resource regions) that must not be held by the driver after its bound device has been unbound, you have to revoke the resource from the driver, i.e. you can't just fix it with a reference count.
Effectively, that's what devres does, it releases the resource when the device is unbound. Revocable takes care of avoiding a UAF of a subsequent access.
We subsystems that invented subsystem specific implementations for a revocable mechanism for exactly those cases.
For instance, there is drm_dev_enter() drm_dev_exit() and drm_dev_unplug() which ultimately does the same things as DRM specific implementation.
If the resource is not a device resource, then revocable is clearly not the correct solution however.
Quoted 10 lines · Danilo Krummrich
Quoted 2 lines · Laurent Pinchart
The revocable mechanism isn't the right solution for races between device removal and userspace access.
I think you have to differenciate, as it depends on the resource:
If the resource is a device resource (e.g. MMIO resource regions) that must not be held by the driver after its bound device has been unbound, you have to revoke the resource from the driver, i.e. you can't just fix it with a reference count.
The C code doesn't really work like that, it works on sync teardown flows. If you want to write correct C code you need to think about all the concurrency the driver has and ensure that removal undoes it
I think Laurent is referring to the lack of a sync unregister for fops.
In this series patches 13-16 are all fops related callbacks.
#17 kind of looks like a cancel_work_sync() is missing (ie what is preventing this work func from running after the module unloads?)
#19 is all sysfs stuff, and sysfs already has a sync unregister, so maybe these SRCU's can never fail?
Jason
Danilo Krummrich
maintainerQuoted 3 lines · Jason Gunthorpe
The C code doesn't really work like that, it works on sync teardown flows. If you want to write correct C code you need to think about all the concurrency the driver has and ensure that removal undoes it
Again, it depends: Sometimes a synchronized teardown is not possible. Iff a synchronized teardown is not possible by design, this is where revocable is useful instead.
However, a synchronized teardown should of course always be preferred.
And just to clarify, since you said "the C code": The Rust code follows exactly the the same principle, prefer synchronized teardown whenever possible.
(The only difference with Rust is that we can always guard device resources and iff synchronized teardown is ensured through the type system the guard becomes zero-cost for accesses.)