drivers/base: Introduce revocable
Subsystems (5)
v14 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][2].
- Patch 2 adds KUnit test cases.
- Patch 3 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. The patch replaces that custom logic with the generic "revocable" API, which is designed to handle such lifecycle dependencies. There should be no changes in behavior.
- Patch 4 adds KUnit test cases for the reset controller framework to verify its lifecycle and concurrent unregistration behavior.
- Patch 5 transitions the reset controller framework to use the "revocable" mechanism.
Since v9, there are two ways to manage the resource provider handle.
- Embedded allocation: patch 3 is the user.
- Dynamic allocation: patch 5 is the user.
[1] https://lpc.events/event/17/contributions/1627/
[2] https://lpc.events/event/19/contributions/2262/
---
v14:
- Rebase onto next-20261009.
- See changelogs in [PATCH v14 1/5] and [PATCH v14 2/5] for revocable API and KUnit test updates.
- Add patches 4 and 5 for the reset controller framework.
v13: https://lore.kernel.org/all/[email protected]
- Rebase onto next-20260911.
v12: https://lore.kernel.org/all/[email protected]
- Rebase onto next-20260907.
- Drop patch 4 and 5.
v11: https://lore.kernel.org/all/[email protected]
- Rebase onto v7.1-rc3.
- Squash patches 4 to 7 into patch 3. A single patch for GPIO.
v10: https://lore.kernel.org/all/[email protected]
- Unify handling of embedded and dynamic allocation.
v9: https://lore.kernel.org/all/[email protected]
- 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 (5):
revocable: Revocable resource management
revocable: Add KUnit test cases
gpio: Leverage revocable for accessing struct gpio_chip
reset: Add KUnit test cases
reset: Leverage revocable for accessing struct reset_controller_dev
.../driver-api/driver-model/index.rst | 1 + .../driver-api/driver-model/revocable.rst | 440 ++++++++++++++ MAINTAINERS | 11 + drivers/base/Makefile | 2 +- drivers/base/revocable.c | 305 ++++++++++ drivers/base/test/Kconfig | 5 + drivers/base/test/Makefile | 2 + drivers/base/test/revocable-test.c | 560 ++++++++++++++++++ drivers/gpio/gpiolib-cdev.c | 77 +-- drivers/gpio/gpiolib-sysfs.c | 32 +- drivers/gpio/gpiolib.c | 257 ++++---- drivers/gpio/gpiolib.h | 28 +- drivers/reset/Kconfig | 5 + drivers/reset/Makefile | 2 + drivers/reset/core-test.c | 329 ++++++++++ drivers/reset/core.c | 93 +-- include/linux/reset-controller.h | 3 + include/linux/revocable.h | 220 +++++++ 18 files changed, 2060 insertions(+), 312 deletions(-) create mode 100644 Documentation/driver-api/driver-model/revocable.rst create mode 100644 drivers/base/revocable.c create mode 100644 drivers/base/test/revocable-test.c create mode 100644 drivers/reset/core-test.c create mode 100644 include/linux/revocable.h
Signature · 1 line
2.53.0
PATCH v14 1/5 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, initializes a struct revocable with a pointer to the resource. The provider handle can either be embedded in another structure or dynamically allocated on the heap.
- A resource consumer that wants to access the resource allocates a struct revocable_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. 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.
---
Bartosz: There haven't been major changes to embedded provider handles,
so I kept your T-b tag.
---
Test:
$ make O=build SPHINXDIRS=driver-api/driver-model/ htmldocs
$ python3 -m http.server --directory \
build/Documentation/output/driver-api/driver-model/
---
v14:
- Add Tested-by tag.
- Rename:
- revocable_deinit() -> revocable_destroy().
- revocable_handle_deinit() -> revocable_handle_destroy().
- revocable_alloc() now holds 1 initial reference instead of 2.
- revocable_revoke() only invalidates the resource; it doesn't drop the
reference count now. An explicit revocable_put() is required if the
handle is dynamically allocated.v13: https://lore.kernel.org/all/[email protected]
- Invert flag "embedded" -> "dynamic" so that "embedded" looks like default one since it has concrete user.
- Embedded allocation:
- Don't work with kref as it's meaningless to embedded provider handle.
- Introduce revocable_deinit() to clean up.
- Cache 'dynamic' flag in struct revocable_handle so that it doesn't rely on the memory from the container of struct revocable.
- Dynamic allocation:
- Defer cleanup to a workqueue to prevent sleeping-in-atomic panics.
v12: https://lore.kernel.org/all/[email protected]
- Rebase onto next-20260907.
- Fix "struct revocable_consumer" -> "struct revocable_handle" in the commit message.
- Add "Documentation/driver-api/driver-model/revocable.rst" to MAINTAINERS.
v11: https://lore.kernel.org/all/[email protected]
- Add Reviewed-by and Tested-by tags.
v10: https://lore.kernel.org/all/[email protected]
- Drop unused header.
- Unify handling of embedded and dynamic allocation.
- Rename:
- struct revocable_consumer -> struct revocable_handle.
- revocable_init() -> revocable_handle_init().
- revocable_deinit() -> revocable_handle_deinit().
- revocable_embed_init() -> revocable_init().
v9: https://lore.kernel.org/all/[email protected]
- 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]
--- .../driver-api/driver-model/index.rst | 1 + .../driver-api/driver-model/revocable.rst | 440 ++++++++++++++++++ MAINTAINERS | 10 + drivers/base/Makefile | 2 +- drivers/base/revocable.c | 305 ++++++++++++ include/linux/revocable.h | 220 +++++++++ 6 files changed, 977 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 +440 -0
440 lines not on this page · raw message
MAINTAINERS +10 -0
@@ -8087,6 +8087,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/sys_soc.h F: include/linux/sysfs.h F: kernel/ksysfs.c
@@ -23847,6 +23848,15 @@ 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: Documentation/driver-api/driver-model/revocable.rst +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 +305 -0
305 lines not on this page · raw message
include/linux/revocable.h new file +220 -0
220 lines not on this page · raw message
Signature · 1 line
2.53.0
PATCH v14 2/5 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.
---
Bartosz: There haven't been major changes to embedded provider handles,
so I kept your T-b tag.
---
Test:
$ ./tools/testing/kunit/kunit.py run \
--kconfig_add CONFIG_REVOCABLE_KUNIT_TEST=y \
revocable_test$ ./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
---
v14:
- Add Tested-by tag.
- Change revocable API usages accordingly:
- revocable_deinit() -> revocable_destroy().
- revocable_handle_deinit() -> revocable_handle_destroy().
- Adjust expected refcounts as revocable_alloc() holds 1 initial
reference and revocable_revoke() no longer drops a reference.
- Refactor embedded test cases and concurrent access setup to reduce
code duplication.
- Replace msleep() with completion/polling in concurrent access tests.
- Hold task_struct references with get_task_struct() and stop threads
with kthread_stop_put() to prevent Use-After-Free.v13: https://lore.kernel.org/all/[email protected]
- Explicitly indicate "dynamic" vs. "embedded" in test case names.
- Embedded allocation
- Add tests and separate "stack" vs. "heap".
- Add tests on macros and concurrent access.
v12: https://lore.kernel.org/all/[email protected]
- Rebase onto next-20260907.
- Fix path in MAINTAINERS: "drivers/base/revocable_test.c" -> "drivers/base/test/revocable-test.c".
v11: https://lore.kernel.org/all/[email protected]
- Move the test to drivers/base/test/.
- Add R-b tag.
v10: https://lore.kernel.org/all/[email protected]
- Merge revocable_test_try_access_macro*() cases.
- Change revocable API usages accordingly.
v9: https://lore.kernel.org/all/[email protected]
- 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.
--- MAINTAINERS | 1 + drivers/base/test/Kconfig | 5 + drivers/base/test/Makefile | 2 + drivers/base/test/revocable-test.c | 560 +++++++++++++++++++++++++++++ 4 files changed, 568 insertions(+) create mode 100644 drivers/base/test/revocable-test.c
MAINTAINERS +1 -0
@@ -23855,6 +23855,7 @@ S: Maintained T: git git://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git F: Documentation/driver-api/driver-model/revocable.rst F: drivers/base/revocable.c +F: drivers/base/test/revocable-test.c F: include/linux/revocable.h RFKILL
drivers/base/test/Kconfig +5 -0
@@ -23,3 +23,8 @@ config DRIVER_SWNODE_KUNIT_TEST tristate "KUnit Tests for software node fw_devlink links" if !KUNIT_ALL_TESTS depends on KUNIT default KUNIT_ALL_TESTS + +config REVOCABLE_KUNIT_TEST + tristate "KUnit tests for revocable" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS
drivers/base/test/Makefile +2 -0
@@ -8,3 +8,5 @@ obj-$(CONFIG_DRIVER_PE_KUNIT_TEST) += property-entry-test.o CFLAGS_property-entry-test.o += $(DISABLE_STRUCTLEAK_PLUGIN) obj-$(CONFIG_DRIVER_SWNODE_KUNIT_TEST) += swnode-devlink-test.o + +obj-$(CONFIG_REVOCABLE_KUNIT_TEST) += revocable-test.o
drivers/base/test/revocable-test.c new file +560 -0
560 lines not on this page · raw message
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v14 2/5] revocable: Add KUnit test cases
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] kunit: leaked kthreads and UAF on KUNIT_ASSERT_* failure in concurrent test --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] kunit: leaked kthreads and UAF on KUNIT_ASSERT_* failure in concurrent test --
commit 5bce09dba3407560b819288b85de7f22b1f0a604
Author: Tzung-Bi Shih <[email protected]>
revocable: Add KUnit test cases
This commit adds KUnit test cases for the revocable API. The tests cover basic functionality, revocation, macro helpers, and concurrent access.
Quoted 6 lines
drivers/base/test/revocable-test.c new file +0
-0@@ -0,0 +1,560 @@
[ ... ]
Quoted 22 lines
drivers/base/test/revocable-test.c new file +22
-0+static struct test_concurrent_access_context * +revocable_test_concurrent_access_prepare (struct kunit *test, +struct revocable *rev,bool dynamic) +{ +struct test_concurrent_access_context *ctx; +int i; + + ctx =kunit_kmalloc_array (test,3 ,sizeof (*ctx),GFP_KERNEL ); +KUNIT_ASSERT_NOT_NULL (test, ctx); + +for (i =0 ; i <3 ; ++i) { +init_completion (&ctx[i].started); +init_completion (&ctx[i].enter); + +if (i ==0 ) { + ctx[i].rev = rev; + ctx[i].thread =kthread_run ( + test_concurrent_access_provider, ctx + i, +"provider%d" , i); + }else { + ctx[i].test = test; +init_completion (&ctx[i].entered);
drivers/base/test/revocable-test.c line 438
Quoted 10 lines
drivers/base/test/revocable-test.c new file +10
-0+init_completion (&ctx[i].exit); +revocable_handle_init (rev, &ctx[i].rh); +if (dynamic) +KUNIT_EXPECT_EQ (test,get_refcount (rev),1 + i); + + ctx[i].thread =kthread_run ( + test_concurrent_access_consumer, ctx + i, +"consumer%d" , i); + } +KUNIT_ASSERT_FALSE (test,IS_ERR (ctx[i].thread));
[Severity: Medium]
If this assertion fails (for instance, if a later kthread_run() call fails), the KUnit framework will immediately abort the test.
Because the test is aborted, the manual kthread_stop_put() teardown loops found in the caller functions will be bypassed. Does this leave the previously spawned kthreads running indefinitely against the ctx array that KUnit automatically frees?
[ ... ]
Quoted 13 lines
drivers/base/test/revocable-test.c new file +13
-0+static void revocable_embedded_test_concurrent_access (struct kunit *test) +{ +struct revocable rev_on_stack, *rev = &rev_on_stack; +void *real_res = (void *)0x12345678 , *res; +struct test_concurrent_access_context *ctx; +int ret, i; + + ret =revocable_init (rev, real_res); +KUNIT_ASSERT_EQ (test, ret,0 ); +KUNIT_EXPECT_FALSE (test, rev->dynamic); + + ctx =revocable_test_concurrent_access_prepare (test, rev, false); +
drivers/base/test/revocable-test.c line 469
Quoted 10 lines
drivers/base/test/revocable-test.c new file +10
-0+ ctx[1 ].expected_res = real_res; +/* consumer1 enters read-side critical section. */ +complete (&ctx[1 ].enter); +wait_for_completion (&ctx[1 ].entered); + +/* provider0 revokes the resource. */ +complete (&ctx[0 ].enter); + ret =read_poll_timeout (rcu_access_pointer, res, !res,1000 ,100000 , + false, rev->res); +KUNIT_ASSERT_EQ (test, ret,0 );
[Severity: Medium]
If read_poll_timeout() reaches its timeout, ret will be non-zero and KUNIT_ASSERT_EQ() will abort the test.
Will this bypass the kthread_stop_put() teardown loop below, resulting in a use-after-free when the still-running kthreads attempt to access the freed ctx array or the rev_on_stack variable that goes out of scope?
Quoted 19 lines
drivers/base/test/revocable-test.c new file +19
-0+/* provider0 can't exit. It's waiting for the grace period. */ + + ctx[2 ].expected_res =NULL ; +/* consumer2 enters read-side critical section. */ +complete (&ctx[2 ].enter); +wait_for_completion (&ctx[2 ].entered); + +/* consumer{1,2} exit read-side critical section. */ +for (i =1 ; i <3 ; ++i) { +complete (&ctx[i].exit); +kthread_stop_put (ctx[i].thread); +revocable_handle_destroy (&ctx[i].rh); + } + +/* provider0 exits as all readers exit their critical section. */ +kthread_stop_put (ctx[0 ].thread); + +revocable_destroy (rev); +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v14 3/5 gpio: Leverage revocable for accessing 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. Leverage revocable for accessing the struct gpio_chip and remove the gdev->srcu.
---
Bartosz: There haven't been major changes to embedded provider handles,
so I kept your T-b tag.
---
Test:
Prepare gpiod-test.
(https://libgpiod.readthedocs.io/en/master/building.html#building)
$ cd libgpiod-2.3.1 $ mkdir build && cd build $ meson setup .. -Dtools=enabled $ ninja
Prepare kernel.
$ cat >test_config <<EOF CONFIG_IKCONFIG=y CONFIG_IKCONFIG_PROC=y CONFIG_KASAN=y CONFIG_GPIOLIB=y CONFIG_GPIO_SYSFS=y CONFIG_GPIO_SIM=y CONFIG_EXPERT=y CONFIG_PROVE_LOCKING=y CONFIG_PANIC_ON_OOPS=y CONFIG_DEBUG_KERNEL=y CONFIG_LOCKDEP=y EOF $ export KBUILD_OUTPUT=build $ vng -k -f test_config && vng -b
Run gpiod-test.
$ vng
$ ./libgpiod-2.3.1/build/tests/gpiod-test
$ time ./libgpiod-2.3.1/build/tests/gpiod-test >/dev/null 2>&1
---
v14:
- Add Tested-by tag.
- Rebase onto next-20261009.
- Fix conflicts with 1feb5d39b05a ("gpio: cdev: fix kernel stack leak
to user-space in error path").
- Change revocable API usages accordingly:
- revocable_deinit() -> revocable_destroy().v13: https://lore.kernel.org/all/[email protected]
- Call revocable_deinit() instead of revocable_put() as it uses embedded allocation.
v12: https://lore.kernel.org/all/[email protected]
- Rebase onto next-20260907.
- Fix conflicts mainly with aa7e8b7ef031 ("gpio: core: fix const-correctness of gpio_chip_guard").
- Fix error unwinding in gpiochip_add_data_with_key() by initializing chip_rev before device_initialize() and ensuring both references are dropped on error.
v11: https://lore.kernel.org/all/[email protected]
- Add Tested-by tag.
- Squash
- 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]
v10: https://lore.kernel.org/all/[email protected]
- Change revocable API usages accordingly.
v9: https://lore.kernel.org/all/[email protected]
- 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-cdev.c | 77 ++++------- drivers/gpio/gpiolib-sysfs.c | 32 ++--- drivers/gpio/gpiolib.c | 257 +++++++++++++++-------------------- drivers/gpio/gpiolib.h | 28 +--- 4 files changed, 153 insertions(+), 241 deletions(-)
drivers/gpio/gpiolib-cdev.c +30 -47
@@ -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 :
@@ -1447,11 +1446,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 :
@@ -1478,10 +1475,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);
@@ -1500,11 +1497,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 ;
@@ -1774,10 +1769,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);
@@ -1812,11 +1807,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,
@@ -1894,11 +1887,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,
@@ -2180,12 +2171,11 @@ static int gpio_desc_to_lineinfo(struct gpio_desc *desc,u32 debounce_period_us;unsigned long dflags;const char *label; +struct gpio_chip *gc;memset (info,0 ,sizeof (*info)); -CLASS (gpio_chip_guard, guard)(desc); -if (!guard.gc) -return -ENODEV ; +revocable_try_access_or_return (&desc->gdev->chip_rev, gc); info->offset =gpiod_hwgpio (desc);
@@ -2217,10 +2207,10 @@ static int 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 ; }
@@ -2411,12 +2401,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) {
@@ -2474,12 +2462,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,
@@ -2567,10 +2552,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);
@@ -2590,11 +2575,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 );
drivers/gpio/gpiolib-sysfs.c +14 -18
@@ -10,9 +10,9 @@#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> #include <linux/sysfs.h> #include <linux/types.h>
@@ -215,10 +215,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 +243,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 +257,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 +272,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 +471,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 +729,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 +742,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 +765,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 +106 -151
664 lines not on this page · raw message
drivers/gpio/gpiolib.h +3 -25
@@ -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>
@@ -31,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
@@ -54,7 +54,7 @@ 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 * * This state container holds most of the runtime variable data
@@ -67,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;
@@ -81,7 +80,7 @@ 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 /*
@@ -225,27 +224,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; - }), - const 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.53.0
[email protected]
AI reviewRe: [PATCH v14 3/5] gpio: Leverage revocable for accessing struct gpio_chip
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider: - [High] gpio: ABBA deadlock during chip removal due to SRCU lock held while sleeping in read() - [High]…
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [High] gpio: ABBA deadlock during chip removal due to SRCU lock held while sleeping in read()
- [High] gpio: TOCTOU race in gpiod_get_direction() overwrites concurrent desc->flags updates
- [Medium] gpio: memory leak of desc->label when chip is revoked before gpiod_free_commit
- [Low] core: initialization and teardown sequence mismatch (LIFO violation) in gpiodev_release
- [Low] gpio: UAF risk in callers of deprecated gpio_device_get_chip()
--
commit 1cecc3cf824694935c1167959e5066cdaf4d16c5
Author: Tzung-Bi Shih <[email protected]>
gpio: Leverage revocable for accessing struct gpio_chip
The underlying chip can be removed asynchronously. gdev->srcu is used to ensure the synchronization before accessing gdev->chip. Revocable encapsulates the details. Leverage revocable for accessing the struct gpio_chip and remove the gdev->srcu.
Quoted 5 lines
drivers/gpio/gpiolib-cdev.c +0
-0
drivers/gpio/gpiolib-cdev.c static ssize_t lineinfo_watch_read(struct file *file, char __user *buf, · line 2580
Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +2
-4@@ -2590,11 +2575,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);
[Severity: High]
Does this introduce a potential ABBA deadlock during chip removal?
When userspace reads from the character device (e.g., in lineinfo_watch_read(), linereq_read(), or lineevent_read()), this macro acquires the chip_rev SRCU read lock and holds it for the duration of the function.
If the event FIFO is empty, the reader sleeps indefinitely in wait_event_interruptible_locked() while holding the SRCU read lock.
Concurrently, if the device is hot-unplugged, gpiochip_remove() calls revocable_revoke(), which blocks on synchronize_srcu() waiting for all readers to exit. The reader, however, is waiting to be woken up by the device unregistered notifier, which is triggered via gcdev_unregister() after revocable_revoke() has finished. This would cause the removal process to hang.
Quoted 5 lines
drivers/gpio/gpiolib.c +0
-0
drivers/gpio/gpiolib.c EXPORT_SYMBOL(gpio_device_get_label); · line 345
Quoted 10 lines
drivers/gpio/gpiolib.c +4
-1@@ -336,7 +337,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);
[Severity: Low]
This isn't a bug, but can this lead to a Use-After-Free for callers of this deprecated API?
The revocable_try_access_with macro establishes a scoped cleanup guard that releases the SRCU lock when exiting the function. The caller thus receives a bare, unprotected pointer. If the GPIO chip is concurrently removed, any subsequent dereference by callers (e.g. qe_pin_request() accessing gc->base without synchronization primitives) could result in a UAF.
[ ... ]
Quoted 18 lines
drivers/gpio/gpiolib.c +2
-3@@ -455,14 +457,13 @@ int gpiod_get_direction(struct gpio_desc *desc)unsigned long flags;unsigned int offset;int ret; +struct gpio_chip *gc; ret =validate_desc (desc, __func__);if (ret <=0 )return -EINVAL ; -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); flags =READ_ONCE (desc->flags);
drivers/gpio/gpiolib.c int gpiod_get_direction(struct gpio_desc *desc) · line 479
Quoted 5 lines
drivers/gpio/gpiolib.c +1
-1@@ -475,7 +476,7 @@ int gpiod_get_direction(struct gpio_desc *desc) test_bit(GPIOD_FLAG_IS_OUT, &flags))return 0 ; - ret =gpiochip_get_direction (guard.gc, offset); + ret =gpiochip_get_direction (gc, offset);
[Severity: High]
Could this sequence cause a Time-of-Check to Time-of-Use (TOCTOU) race where concurrent atomic updates to desc->flags are overwritten?
This function reads desc->flags into a local variable and then calls the potentially sleeping gpiochip_get_direction(). If a concurrent operation modifies desc->flags atomically (e.g., gpiochip_lock_as_irq() setting GPIOD_FLAG_USED_AS_IRQ), the subsequent non-atomic WRITE_ONCE(desc->flags, flags) at the end of gpiod_get_direction() will clobber that update.
If GPIOD_FLAG_USED_AS_IRQ is lost, the subsystem loses track of the fact that the pin is used as an interrupt, allowing concurrent code to reconfigure the pin as an output.
[ ... ]
drivers/gpio/gpiolib.c static void gpiodev_release(struct device *dev) · line 900
Quoted 5 lines
drivers/gpio/gpiolib.c +1
-0@@ -898,10 +897,10 @@ static void gpiodev_release(struct device *dev)synchronize_srcu (&gdev->desc_srcu);cleanup_srcu_struct (&gdev->desc_srcu); +revocable_destroy (&gdev->chip_rev);
[Severity: Low]
Does this teardown sequence violate LIFO (Last-In-First-Out) ordering rules?
In gpiochip_add_data_with_key(), gdev->desc_srcu is initialized before gdev->chip_rev. Here in gpiodev_release(), gdev->desc_srcu is cleaned up before gdev->chip_rev is destroyed.
While this may not cause an immediate crash, this fragile lifecycle management is an anti-pattern.
[ ... ]
Quoted 9 lines
drivers/gpio/gpiolib.c +1
-1@@ -2680,16 +2674,17 @@ int gpiod_request(struct gpio_desc *desc, const char *label)void gpiod_free_commit (struct gpio_desc *desc) {unsigned long flags; +struct gpio_chip *gc;might_sleep (); -CLASS (gpio_chip_guard, guard)(desc);
drivers/gpio/gpiolib.c int gpiod_request(struct gpio_desc *desc, const char *label) · line 2687
Quoted 10 lines
drivers/gpio/gpiolib.c +4
-3+revocable_try_access_or_return_void (&desc->gdev->chip_rev, gc); flags =READ_ONCE (desc->flags); -if (guard.gc &&test_bit (GPIOD_FLAG_REQUESTED , &flags)) { -if (guard.gc->free) - guard.gc->free (guard.gc,gpiod_hwgpio (desc)); +if (test_bit (GPIOD_FLAG_REQUESTED , &flags)) { +if (gc->free) + gc->free (gc,gpiod_hwgpio (desc));
[Severity: Medium]
With the early return introduced here, does this skip the rest of the cleanup block if the chip has been revoked, leaking the label?
If a GPIO line is requested and a label is dynamically allocated, and later the chip is unbound (revoking the chip_rev handle), calling gpiod_free_commit() will return early. This skips the desc_set_label(desc, NULL) cleanup further down in the function.
When gpiodev_release() later frees the descriptor array, the active labels are not freed, which results in a memory leak.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH v14 4/5 reset: Add KUnit test cases
In reply to Tzung-Bi ShihSince commit 9035073d0ef1 ("reset: convert reset core to using firmware nodes"), reset suppliers and consumers can be wired up using software nodes without requiring Device Tree or hardware devices. Leverage this to add KUnit test cases for the reset controller framework.
The test cases cover the following scenarios:
- Basic: Verifies that a consumer can acquire a reset control via fwnode and perform reset, assert, deassert, status, release, and acquire operations.
- Use After Unregister: Verifies that after the supplier unregisters the reset_controller_dev while a consumer still holds a reset_control handle, subsequent consumer operations fail safely with -ENODEV.
- Concurrent Unregister: Verifies that unregistering the reset_controller_dev concurrently with an in-flight reset operation waits for the active operation to complete before returning.
---
Test:
$ ./tools/testing/kunit/kunit.py run \
--kconfig_add CONFIG_RESET_CONTROLLER=y \
--kconfig_add CONFIG_RESET_KUNIT_TEST=y \
reset_test$ ./tools/testing/kunit/kunit.py run \
--kconfig_add CONFIG_RESET_CONTROLLER=y \
--kconfig_add CONFIG_RESET_KUNIT_TEST=y \
--kconfig_add CONFIG_PROVE_LOCKING=y \
--kconfig_add CONFIG_DEBUG_KERNEL=y \
--kconfig_add CONFIG_KASAN=y \
--arch=x86_64 \
reset_test
---
v14:
- New to the series.
---
drivers/reset/Kconfig | 5 +
drivers/reset/Makefile | 2 +
drivers/reset/core-test.c | 329 ++++++++++++++++++++++++++++++++++++++
3 files changed, 336 insertions(+)
create mode 100644 drivers/reset/core-test.cdrivers/reset/Kconfig +5 -0
@@ -457,4 +457,9 @@ source "drivers/reset/starfive/Kconfig" source "drivers/reset/sti/Kconfig" source "drivers/reset/tegra/Kconfig" +config RESET_KUNIT_TEST + tristate "KUnit tests for reset controller" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS + endif
drivers/reset/Makefile +2 -0
@@ -55,3 +55,5 @@ obj-$(CONFIG_RESET_UNIPHIER) += reset-uniphier.o obj-$(CONFIG_RESET_UNIPHIER_GLUE) += reset-uniphier-glue.o obj-$(CONFIG_RESET_ZYNQ) += reset-zynq.o obj-$(CONFIG_RESET_ZYNQMP) += reset-zynqmp.o + +obj-$(CONFIG_RESET_KUNIT_TEST) += core-test.o
drivers/reset/core-test.c new file +329 -0
329 lines not on this page · raw message
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v14 4/5] reset: Add KUnit test cases
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] reset: test: KUnit assertion failures cause UAF and unkillable thread leaks --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] reset: test: KUnit assertion failures cause UAF and unkillable thread leaks --
commit 222c19f0c2d0f6b0dadf93fc51cab0de65a40955
Author: Tzung-Bi Shih <[email protected]>
reset: Add KUnit test cases
Since commit 9035073d0ef1 ("reset: convert reset core to using firmware nodes"), reset suppliers and consumers can be wired up using software nodes without requiring Device Tree or hardware devices. Leverage this to add KUnit test cases for the reset controller framework.
Quoted 5 lines
drivers/reset/core-test.c new file +0
-0
[ ... ]
Quoted 2 lines
drivers/reset/core-test.c new file +2
-0+static int reset_test_init (struct kunit *test) +{
[ ... ]
drivers/reset/core-test.c line 131
Quoted 10 lines
drivers/reset/core-test.c new file +10
-0+ ctx->rcdev.ops = &reset_test_ops; + ctx->rcdev.nr_resets =1 ; + ctx->rcdev.fwnode = ctx->supp_fwnode; + + ret =reset_controller_register (&ctx->rcdev); +KUNIT_ASSERT_EQ (test, ret,0 ); + + ctx->rstc =__fwnode_reset_control_get (ctx->cons_fwnode,NULL ,0 , +RESET_CONTROL_EXCLUSIVE ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, ctx->rstc);
[Severity: High]
If __fwnode_reset_control_get() fails and triggers this assertion, the test will abort immediately without unregistering ctx->rcdev. Since ctx was allocated with kunit_kzalloc(), it will be automatically freed.
Could this leave a dangling pointer in the global reset_controller_list, leading to memory corruption?
Would using a deferred cleanup action via kunit_add_action() be appropriate immediately after registering the controller?
[ ... ]
Quoted 19 lines
drivers/reset/core-test.c new file +19
-0+static void reset_test_concurrent_unregister (struct kunit *test) +{ +struct reset_test_context *rtc = test->priv; +struct test_concurrent_unregister_context *ctx; +int i, ret, val; + + rtc->rcdev.ops = &reset_test_concurrent_unregister_ops; + + ctx =kunit_kmalloc_array (test,3 ,sizeof (*ctx),GFP_KERNEL ); +KUNIT_ASSERT_NOT_NULL (test, ctx); + +for (i =0 ; i <3 ; ++i) { + ctx[i].test = test; +init_completion (&ctx[i].started); +init_completion (&ctx[i].enter); + +if (i ==0 ) { + ctx[i].thread = kthread_run( + test_concurrent_unregister_supplier, ctx + i,
drivers/reset/core-test.c line 276
Quoted 10 lines
drivers/reset/core-test.c new file +10
-0+ "supplier%d", i); + }else { +init_completion (&ctx[i].entered); +init_completion (&ctx[i].exit); + + ctx[i].thread =kthread_run ( + test_concurrent_unregister_consumer, ctx + i, +"consumer%d" , i); + } +KUNIT_ASSERT_FALSE (test,IS_ERR (ctx[i].thread));
[Severity: High]
If thread 1 or 2 fails to spawn, this assertion will abort the test immediately. However, previously spawned threads might still be waiting on synchronization variables (like ctx[0].enter) inside the test-managed memory.
When KUnit frees ctx on abort, will those still-running threads access freed memory, resulting in a use-after-free?
Quoted 8 lines
drivers/reset/core-test.c new file +8
-0+get_task_struct (ctx[i].thread); + +wait_for_completion (&ctx[i].started); + } + + rtc->priv = &ctx[1 ]; + ctx[1 ].expected_ret =0x12345678 ; +/* consumer1 enters reset(). */
drivers/reset/core-test.c line 294
Quoted 10 lines
drivers/reset/core-test.c new file +10
-0+KUNIT_EXPECT_EQ (test, rtc->reset_count,0 ); +complete (&ctx[1 ].enter); +wait_for_completion (&ctx[1 ].entered); +KUNIT_EXPECT_EQ (test, rtc->reset_count,1 ); + +/* supplier0 unregisters. */ +complete (&ctx[0 ].enter); + ret =read_poll_timeout (reset_control_status, val, val == -ENODEV , +1000 ,100000 , false, rtc->rstc); +KUNIT_ASSERT_EQ (test, ret,0 );
[Severity: High]
If the poll times out, this assertion aborts the test without stopping the spawned concurrent kthreads.
Since kthread_stop_put() is skipped, does this result in thread leaks and a use-after-free when KUnit cleans up the test memory while threads are still active?
Quoted 1 line
drivers/reset/core-test.c new file +1
-0+/* supplier0 can't exit. It's waiting for all consumers. */
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Philipp Zabel
maintainerRe: [PATCH v14 4/5] reset: Add KUnit test cases
In reply to Tzung-Bi ShihQuoted 17 lines · Tzung-Bi Shih
Since commit
9035073d0ef1("reset: convert reset core to using firmware nodes"), reset suppliers and consumers can be wired up using software nodes without requiring Device Tree or hardware devices. Leverage this to add KUnit test cases for the reset controller framework.The test cases cover the following scenarios:
- Basic: Verifies that a consumer can acquire a reset control via fwnode and perform reset, assert, deassert, status, release, and acquire operations.
- Use After Unregister: Verifies that after the supplier unregisters thereset_controller_devwhile a consumer still holds areset_controlhandle, subsequent consumer operations fail safely with-ENODEV.
- Concurrent Unregister: Verifies that unregistering thereset_controller_devconcurrently with an in-flight reset operation waits for the active operation to complete before returning.
Quoted 1 line
Signed-off-by Tzung-Bi Shih <[email protected]>
It looks like the Kunit tests are independent from the reset of the series and could be sent individually.
Is there any dependency that I am missing?
Quoted 178 lines
---
Test:$ ./tools/testing/kunit/kunit.py run \ --kconfig_add CONFIG_RESET_CONTROLLER=y \ --kconfig_add CONFIG_RESET_KUNIT_TEST=y \ reset_test$ ./tools/testing/kunit/kunit.py run \ --kconfig_add CONFIG_RESET_CONTROLLER=y \ --kconfig_add CONFIG_RESET_KUNIT_TEST=y \ --kconfig_add CONFIG_PROVE_LOCKING=y \ --kconfig_add CONFIG_DEBUG_KERNEL=y \ --kconfig_add CONFIG_KASAN=y \ --arch=x86_64 \ reset_test --- v14: - New to the series. --- drivers/reset/Kconfig | 5 + drivers/reset/Makefile | 2 + drivers/reset/core-test.c | 329 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 336 insertions(+) create mode 100644 drivers/reset/core-test.cdrivers/reset/Kconfig +5
-0@@ -457,4 +457,9 @@ source "drivers/reset/starfive/Kconfig" source "drivers/reset/sti/Kconfig" source "drivers/reset/tegra/Kconfig" +config RESET_KUNIT_TEST + tristate "KUnit tests for reset controller" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS + endifdrivers/reset/Makefile +2
-0@@ -55,3 +55,5 @@ obj-$(CONFIG_RESET_UNIPHIER) += reset-uniphier.o obj-$(CONFIG_RESET_UNIPHIER_GLUE) += reset-uniphier-glue.o obj-$(CONFIG_RESET_ZYNQ) += reset-zynq.o obj-$(CONFIG_RESET_ZYNQMP) += reset-zynqmp.o + +obj-$(CONFIG_RESET_KUNIT_TEST) += core-test.odrivers/reset/core-test.c new file +123
-0@@ -0,0 +1,329 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright 2026 Google LLC +* +* KUnit tests for the reset controller. +* +* The test cases cover the following scenarios: +* +* - Basic: Verifies that a consumer can acquire a reset control via +* fwnode and perform reset, assert, deassert, status, release, and +* acquire operations. +* +* - Use After Unregister: Verifies that after the supplier unregisters +* the reset_controller_dev while a consumer still holds a reset_control +* handle, subsequent consumer operations fail safely with -ENODEV. +* +* - Concurrent Unregister: Verifies that unregistering the +* reset_controller_dev concurrently with an in-flight reset operation +* waits for the active operation to complete before returning. +*/ + +#include <kunit/fwnode.h> +#include <kunit/test.h> + +#include <linux/completion.h> +#include <linux/fwnode.h> +#include <linux/iopoll.h> +#include <linux/kthread.h> +#include <linux/property.h> +#include <linux/reset-controller.h> +#include <linux/reset.h> +#include <linux/sched/task.h> + +struct reset_test_context { +struct fwnode_handle *supp_fwnode; +struct fwnode_handle *cons_fwnode; +struct reset_controller_dev rcdev; +struct reset_control *rstc; + +int reset_count; +int assert_count; +int deassert_count; +int status_count; + +void *priv; +}; + +static int reset_test_op_reset (struct reset_controller_dev *rcdev, +unsigned long id) +{ +struct reset_test_context *ctx =container_of (rcdev,typeof (*ctx), rcdev); + + ctx->reset_count++; +return 0 ; +} + +static int reset_test_op_assert (struct reset_controller_dev *rcdev, +unsigned long id) +{ +struct reset_test_context *ctx =container_of (rcdev,typeof (*ctx), rcdev); + + ctx->assert_count++; +return 0 ; +} + +static int reset_test_op_deassert (struct reset_controller_dev *rcdev, +unsigned long id) +{ +struct reset_test_context *ctx =container_of (rcdev,typeof (*ctx), rcdev); + + ctx->deassert_count++; +return 0 ; +} + +static int reset_test_op_status (struct reset_controller_dev *rcdev, +unsigned long id) +{ +struct reset_test_context *ctx =container_of (rcdev,typeof (*ctx), rcdev); + + ctx->status_count++; +return 0 ; +} + +static const struct reset_control_ops reset_test_ops = { + .reset = reset_test_op_reset, + .assert = reset_test_op_assert, + .deassert = reset_test_op_deassert, + .status = reset_test_op_status, +}; + +static int reset_test_init (struct kunit *test) +{ +static const struct property_entry supp_props[] = { +PROPERTY_ENTRY_U32 ("#reset-cells" ,1 ), + {} + }; +static const struct software_node supp_swnode = { + .name ="reset-test-supplier" , + .properties = supp_props, + }; +static const struct software_node_ref_args refs[] = { +SOFTWARE_NODE_REFERENCE (&supp_swnode,0 ), + }; +static const struct property_entry cons_props[] = { +PROPERTY_ENTRY_REF_ARRAY ("resets" , refs), + {} + }; + +struct reset_test_context *ctx; +int ret; + + ctx =kunit_kzalloc (test,sizeof (*ctx),GFP_KERNEL ); +KUNIT_ASSERT_NOT_NULL (test, ctx); + + ctx->supp_fwnode =kunit_software_node_register (test, &supp_swnode); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, ctx->supp_fwnode); + + ctx->cons_fwnode =kunit_fwnode_create_software_node (test, cons_props, +NULL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, ctx->cons_fwnode); + + ctx->rcdev.ops = &reset_test_ops; + ctx->rcdev.nr_resets =1 ;
drivers/reset/core-test.c line 133
Quoted 10 lines
drivers/reset/core-test.c new file +10
-0+ ctx->rcdev.fwnode = ctx->supp_fwnode; + + ret =reset_controller_register (&ctx->rcdev); +KUNIT_ASSERT_EQ (test, ret,0 ); + + ctx->rstc =__fwnode_reset_control_get (ctx->cons_fwnode,NULL ,0 , +RESET_CONTROL_EXCLUSIVE ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, ctx->rstc); + + test->priv = ctx;
The rcdev and rstc could be cleaned up in .exit for the error cases.
Quoted 19 lines
drivers/reset/core-test.c new file +19
-0+return 0 ; +} + +static void reset_test_basic (struct kunit *test) +{ +struct reset_test_context *ctx = test->priv; +struct reset_control *rstc = ctx->rstc; + +KUNIT_EXPECT_EQ (test, ctx->reset_count,0 ); +KUNIT_EXPECT_EQ (test,reset_control_reset (rstc),0 ); +KUNIT_EXPECT_EQ (test, ctx->reset_count,1 ); + +KUNIT_EXPECT_EQ (test, ctx->assert_count,0 ); +KUNIT_EXPECT_EQ (test,reset_control_assert (rstc),0 ); +KUNIT_EXPECT_EQ (test, ctx->assert_count,1 ); + +KUNIT_EXPECT_EQ (test, ctx->deassert_count,0 ); +KUNIT_EXPECT_EQ (test,reset_control_deassert (rstc),0 ); +KUNIT_EXPECT_EQ (test, ctx->deassert_count,1 );
drivers/reset/core-test.c line 162
Quoted 10 lines
drivers/reset/core-test.c new file +10
-0+ +KUNIT_EXPECT_EQ (test, ctx->status_count,0 ); +KUNIT_EXPECT_EQ (test,reset_control_status (rstc),0 ); +KUNIT_EXPECT_EQ (test, ctx->status_count,1 ); + +reset_control_release (rstc); +KUNIT_EXPECT_EQ (test,reset_control_acquire (rstc),0 ); + +reset_control_put (rstc); +reset_controller_unregister (&ctx->rcdev);
Could be cleaned up by .exit.
drivers/reset/core-test.c line 175
Quoted 13 lines
drivers/reset/core-test.c new file +13
-0+} + +static void reset_test_use_after_unregister (struct kunit *test) +{ +struct reset_test_context *ctx = test->priv; +struct reset_control *rstc = ctx->rstc; + +KUNIT_EXPECT_EQ (test, ctx->reset_count,0 ); +KUNIT_EXPECT_EQ (test,reset_control_reset (rstc),0 ); +KUNIT_EXPECT_EQ (test, ctx->reset_count,1 ); + +/* Unregister supplier while consumer still holds rstc. */ +reset_controller_unregister (&ctx->rcdev);
If you allocate rcdev on its own instead of embedding it in the context, you could
reset_controller_unregister(ctx->rcdev); ctx->rcdev = NULL;
here and still have normal cleanup if not NULL in .exit.
drivers/reset/core-test.c line 188
Quoted 13 lines
drivers/reset/core-test.c new file +13
-0+ +KUNIT_EXPECT_EQ (test, ctx->reset_count,1 ); +KUNIT_EXPECT_EQ (test,reset_control_reset (rstc), -ENODEV ); +KUNIT_EXPECT_EQ (test, ctx->reset_count,1 ); + +KUNIT_EXPECT_EQ (test,reset_control_assert (rstc), -ENODEV ); +KUNIT_EXPECT_EQ (test,reset_control_deassert (rstc), -ENODEV ); +KUNIT_EXPECT_EQ (test,reset_control_status (rstc), -ENODEV ); + +reset_control_release (rstc); +KUNIT_EXPECT_EQ (test,reset_control_acquire (rstc), -ENODEV ); + +reset_control_put (rstc);
This would not be required either.
Quoted 78 lines
drivers/reset/core-test.c new file +78
-0+} + +struct test_concurrent_unregister_context { +struct kunit *test; +struct completion started, enter; +struct task_struct *thread; + +/* Used by test consumer. */ +struct completion entered, exit; +int expected_ret; +}; + +static int test_concurrent_unregister_supplier (void *data) +{ +struct test_concurrent_unregister_context *ctx = data; +struct reset_test_context *rtc = ctx->test->priv; + +complete (&ctx->started); + +wait_for_completion (&ctx->enter); +reset_controller_unregister (&rtc->rcdev); + +return 0 ; +} + +static int test_concurrent_unregister_consumer (void *data) +{ +struct test_concurrent_unregister_context *ctx = data; +struct reset_test_context *rtc = ctx->test->priv; +int ret; + +complete (&ctx->started); + +wait_for_completion (&ctx->enter); + + ret =reset_control_reset (rtc->rstc); +KUNIT_EXPECT_EQ (ctx->test, ret, ctx->expected_ret); + +return 0 ; +} + +static int +reset_test_concurrent_unregister_op_reset (struct reset_controller_dev *rcdev, +unsigned long id) +{ +struct reset_test_context *rtc =container_of (rcdev,typeof (*rtc), rcdev); +struct test_concurrent_unregister_context *ctx = rtc->priv; + + rtc->reset_count++; +complete (&ctx->entered); + +wait_for_completion (&ctx->exit); +return ctx->expected_ret; +} + +static const struct reset_control_ops reset_test_concurrent_unregister_ops = { + .reset = reset_test_concurrent_unregister_op_reset, +}; + +static void reset_test_concurrent_unregister (struct kunit *test) +{ +struct reset_test_context *rtc = test->priv; +struct test_concurrent_unregister_context *ctx; +int i, ret, val; + + rtc->rcdev.ops = &reset_test_concurrent_unregister_ops; + + ctx =kunit_kmalloc_array (test,3 ,sizeof (*ctx),GFP_KERNEL ); +KUNIT_ASSERT_NOT_NULL (test, ctx); + +for (i =0 ; i <3 ; ++i) { + ctx[i].test = test; +init_completion (&ctx[i].started); +init_completion (&ctx[i].enter); + +if (i ==0 ) { + ctx[i].thread = kthread_run( + test_concurrent_unregister_supplier, ctx + i,
drivers/reset/core-test.c line 276
Quoted 10 lines
drivers/reset/core-test.c new file +10
-0+ "supplier%d", i); + }else { +init_completion (&ctx[i].entered); +init_completion (&ctx[i].exit); + + ctx[i].thread =kthread_run ( + test_concurrent_unregister_consumer, ctx + i, +"consumer%d" , i); + } +KUNIT_ASSERT_FALSE (test,IS_ERR (ctx[i].thread));
Isn't this missing kthread cleanup in the error cases?
Quoted 23 lines
drivers/reset/core-test.c new file +23
-0+get_task_struct (ctx[i].thread); + +wait_for_completion (&ctx[i].started); + } + + rtc->priv = &ctx[1 ]; + ctx[1 ].expected_ret =0x12345678 ; +/* consumer1 enters reset(). */ +KUNIT_EXPECT_EQ (test, rtc->reset_count,0 ); +complete (&ctx[1 ].enter); +wait_for_completion (&ctx[1 ].entered); +KUNIT_EXPECT_EQ (test, rtc->reset_count,1 ); + +/* supplier0 unregisters. */ +complete (&ctx[0 ].enter); + ret =read_poll_timeout (reset_control_status, val, val == -ENODEV , +1000 ,100000 , false, rtc->rstc); +KUNIT_ASSERT_EQ (test, ret,0 ); +/* supplier0 can't exit. It's waiting for all consumers. */ + + ctx[2 ].expected_ret = -ENODEV ; +/* consumer2 must see -ENODEV. */ +complete (&ctx[2 ].enter);
drivers/reset/core-test.c line 309
Quoted 10 lines
drivers/reset/core-test.c new file +10
-0+kthread_stop_put (ctx[2 ].thread); + +/* consumer1 exits reset(). */ +complete (&ctx[1 ].exit); +kthread_stop_put (ctx[1 ].thread); +kthread_stop_put (ctx[0 ].thread); + +/* Subsequent access must see -ENODEV. */ +KUNIT_EXPECT_EQ (test,reset_control_reset (rtc->rstc), -ENODEV ); +reset_control_put (rtc->rstc);
reset_control_put() could be done by .exit.
regards
Philipp
Re: [PATCH v14 4/5] reset: Add KUnit test cases
Quoted 18 lines · Philipp Zabel
Quoted 17 lines · Tzung-Bi Shih
Since commit
9035073d0ef1("reset: convert reset core to using firmware nodes"), reset suppliers and consumers can be wired up using software nodes without requiring Device Tree or hardware devices. Leverage this to add KUnit test cases for the reset controller framework.The test cases cover the following scenarios:
- Basic: Verifies that a consumer can acquire a reset control via fwnode and perform reset, assert, deassert, status, release, and acquire operations.
- Use After Unregister: Verifies that after the supplier unregisters thereset_controller_devwhile a consumer still holds areset_controlhandle, subsequent consumer operations fail safely with-ENODEV.
- Concurrent Unregister: Verifies that unregistering thereset_controller_devconcurrently with an in-flight reset operation waits for the active operation to complete before returning.
Quoted 6 lines
Quoted 1 line
Signed-off-by Tzung-Bi Shih <[email protected]>It looks like the Kunit tests are independent from the reset of the series and could be sent individually.
Is there any dependency that I am missing?
No, but I was using it to verify if [PATCH v14 5/5] breaks anything. I'll send it out independently.
Quoted 2 lines
Quoted 2 lines
(quoted diff) +2
-0+static int reset_test_init(struct kunit *test) +{
...
Quoted 6 lines
Quoted 6 lines
drivers/reset/core-test.c new file +6
-0+ ctx->rcdev.ops = &reset_test_ops; + ctx->rcdev.nr_resets =1 ; + ctx->rcdev.fwnode = ctx->supp_fwnode; + + ret =reset_controller_register (&ctx->rcdev); +KUNIT_ASSERT_EQ (test, ret,0 );
Quoted 8 lines
drivers/reset/core-test.c line 133
Quoted 6 lines
drivers/reset/core-test.c new file +6
-0+ + ctx->rstc =__fwnode_reset_control_get (ctx->cons_fwnode,NULL ,0 , +RESET_CONTROL_EXCLUSIVE ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, ctx->rstc); + + test->priv = ctx;The rcdev and rstc could be cleaned up in .exit for the error cases.
Ack, will add a .exit() callback to clean up rstc and rcdev.
Quoted 2 lines
Quoted 2 lines
(quoted diff) +2
-0+static void reset_test_basic(struct kunit *test) +{
...
Quoted 7 lines
drivers/reset/core-test.c line 162
Quoted 5 lines
drivers/reset/core-test.c new file +5
-0+reset_control_release (rstc); +KUNIT_EXPECT_EQ (test,reset_control_acquire (rstc),0 ); + +reset_control_put (rstc); +reset_controller_unregister (&ctx->rcdev);Could be cleaned up by .exit.
Ack, will let .exit() handle them.
Quoted 15 lines
drivers/reset/core-test.c line 175
Quoted 11 lines
drivers/reset/core-test.c new file +11
-0+static void reset_test_use_after_unregister (struct kunit *test) +{ +struct reset_test_context *ctx = test->priv; +struct reset_control *rstc = ctx->rstc; + +KUNIT_EXPECT_EQ (test, ctx->reset_count,0 ); +KUNIT_EXPECT_EQ (test,reset_control_reset (rstc),0 ); +KUNIT_EXPECT_EQ (test, ctx->reset_count,1 ); + +/* Unregister supplier while consumer still holds rstc. */ +reset_controller_unregister (&ctx->rcdev);If you allocate rcdev on its own instead of embedding it in the context, you could
Quoted 4 lines
reset_controller_unregister(ctx->rcdev); ctx->rcdev = NULL;here and still have normal cleanup if not NULL in .exit.
The reset_control_ops callbacks use container_of(...) to obtain ctx, so keeping rcdev embedded in ctx is convenient. Will add a bool flag so .exit() only unregisters rcdev if it is still registered.
Quoted 6 lines
Quoted 6 lines
drivers/reset/core-test.c new file +6
-0+KUNIT_EXPECT_EQ (test, ctx->reset_count,1 ); +KUNIT_EXPECT_EQ (test,reset_control_reset (rstc), -ENODEV ); +KUNIT_EXPECT_EQ (test, ctx->reset_count,1 ); + +KUNIT_EXPECT_EQ (test,reset_control_assert (rstc), -ENODEV ); +KUNIT_EXPECT_EQ (test,reset_control_deassert (rstc), -ENODEV );
Quoted 8 lines
drivers/reset/core-test.c line 188
Quoted 6 lines
drivers/reset/core-test.c new file +6
-0+KUNIT_EXPECT_EQ (test,reset_control_status (rstc), -ENODEV ); + +reset_control_release (rstc); +KUNIT_EXPECT_EQ (test,reset_control_acquire (rstc), -ENODEV ); + +reset_control_put (rstc);This would not be required either.
Ack, will let .exit() handle it.
Quoted 23 lines
Quoted 23 lines
drivers/reset/core-test.c new file +23
-0+static void reset_test_concurrent_unregister (struct kunit *test) +{ +struct reset_test_context *rtc = test->priv; +struct test_concurrent_unregister_context *ctx; +int i, ret, val; + + rtc->rcdev.ops = &reset_test_concurrent_unregister_ops; + + ctx =kunit_kmalloc_array (test,3 ,sizeof (*ctx),GFP_KERNEL ); +KUNIT_ASSERT_NOT_NULL (test, ctx); + +for (i =0 ; i <3 ; ++i) { + ctx[i].test = test; +init_completion (&ctx[i].started); +init_completion (&ctx[i].enter); + +if (i ==0 ) { + ctx[i].thread =kthread_run ( + test_concurrent_unregister_supplier, ctx + i, +"supplier%d" , i); + }else { +init_completion (&ctx[i].entered); +init_completion (&ctx[i].exit);
Quoted 8 lines
drivers/reset/core-test.c line 276
Quoted 6 lines
drivers/reset/core-test.c new file +6
-0+ + ctx[i].thread =kthread_run ( + test_concurrent_unregister_consumer, ctx + i, +"consumer%d" , i); + } +KUNIT_ASSERT_FALSE (test,IS_ERR (ctx[i].thread));Isn't this missing kthread cleanup in the error cases?
Correct, will fix it.
Quoted 5 lines
drivers/reset/core-test.c line 309
Quoted 3 lines
drivers/reset/core-test.c new file +3
-0+/* Subsequent access must see -ENODEV. */ +KUNIT_EXPECT_EQ (test,reset_control_reset (rtc->rstc), -ENODEV ); +reset_control_put (rtc->rstc);
reset_control_put()could be done by .exit.
Ack, will let .exit() handle it.
PATCH v14 5/5 reset: Leverage revocable for accessing struct reset_controller_dev
In reply to Tzung-Bi ShihCommit 78ebbff6d1a0 ("reset: handle removing supplier before consumers") protected the rcdev pointer in struct reset_control with SRCU so that the reset core can handle suppliers unbinding while consumers still hold reset control handles.
Because struct reset_control handles can outlive rcdev, each of the N handles had to embed its own srcu_struct and call synchronize_srcu() on release, and reset_controller_unregister() had to call synchronize_srcu() N times in a loop.
Leverage dynamic revocable to allocate a single reference-counted struct revocable for rcdev shared across all N struct reset_control handles (1-to-N). This reduces N srcu_struct instances per controller to 1, drops synchronize_srcu() from reset_control_put_internal(), and replaces the N synchronize_srcu() calls on unregistration with a single revocable_revoke().
In addition, calling revocable_revoke() outside rcdev->lock avoids an ABBA lock inversion between rcdev->lock and SRCU, as reset_control_acquire() and reset_control_put_internal() acquire rcdev->lock inside the SRCU read-side critical section.
---
v14:
- New to the series.
--- drivers/reset/core.c | 93 ++++++++------------------------ include/linux/reset-controller.h | 3 ++ 2 files changed, 26 insertions(+), 70 deletions(-)
drivers/reset/core.c +23 -70
@@ -24,8 +24,8 @@#include <linux/property.h> #include <linux/reset.h> #include <linux/reset-controller.h> +#include <linux/revocable.h> #include <linux/slab.h> -#include <linux/srcu.h> static DEFINE_MUTEX (reset_list_mutex);static LIST_HEAD (reset_controller_list);
@@ -37,9 +37,7 @@ static DEFINE_IDA(reset_gpio_ida);/** * struct reset_control - a reset control -* @rcdev: a pointer to the reset controller device -* this reset control belongs to -* @srcu: protects the rcdev pointer from removal during consumer access +* @rev: revocable handle for accessing the reset controller device * @list: list entry for the rcdev's reset controller list * @id: ID of the reset controller in the reset * controller device
@@ -54,8 +52,7 @@ static DEFINE_IDA(reset_gpio_ida);* @lock: serializes the internals of reset_control_acquire() */ struct reset_control { -struct reset_controller_dev __rcu *rcdev; -struct srcu_struct srcu; +struct revocable *rev;struct list_head list;unsigned int id;struct kref refcnt;
@@ -141,6 +138,10 @@ int reset_controller_register(struct reset_controller_dev *rcdev)return -EINVAL ; } + rcdev->rev =revocable_alloc (rcdev); +if (!rcdev->rev) +return -ENOMEM ; +if (rcdev->of_xlate) rcdev->fwnode_reset_n_cells = rcdev->of_reset_n_cells;
@@ -181,18 +182,14 @@ void reset_controller_unregister(struct reset_controller_dev *rcdev) scoped_guard(mutex, &reset_list_mutex)list_del (&rcdev->list); +revocable_revoke (rcdev->rev); + scoped_guard(mutex, &rcdev->lock) { -/* -* Numb but don't free the remaining reset control handles that are -* still held by consumers. -*/ -list_for_each_entry_safe (rstc, pos, &rcdev->reset_control_head, list) { -rcu_assign_pointer (rstc->rcdev,NULL ); -synchronize_srcu (&rstc->srcu); +list_for_each_entry_safe (rstc, pos, &rcdev->reset_control_head, list)reset_controller_remove (rcdev, rstc); - } } +revocable_put (rcdev->rev);mutex_destroy (&rcdev->lock); }EXPORT_SYMBOL_GPL (reset_controller_unregister);
@@ -382,11 +379,7 @@ int reset_control_reset(struct reset_control *rstc)if (reset_control_is_array (rstc))return reset_control_array_reset (rstc_to_array (rstc)); -guard (srcu)(&rstc->srcu); - - rcdev =srcu_dereference (rstc->rcdev, &rstc->srcu); -if (!rcdev) -return -ENODEV ; +revocable_try_access_or_return (rstc->rev, rcdev);if (!rcdev->ops->reset)return -ENOTSUPP ;
@@ -503,11 +496,7 @@ int reset_control_assert(struct reset_control *rstc)if (reset_control_is_array (rstc))return reset_control_array_assert (rstc_to_array (rstc)); -guard (srcu)(&rstc->srcu); - - rcdev =srcu_dereference (rstc->rcdev, &rstc->srcu); -if (!rcdev) -return -ENODEV ; +revocable_try_access_or_return (rstc->rev, rcdev);if (rstc->shared) {if (WARN_ON (atomic_read (&rstc->triggered_count) !=0 ))
@@ -599,11 +588,7 @@ int reset_control_deassert(struct reset_control *rstc)if (reset_control_is_array (rstc))return reset_control_array_deassert (rstc_to_array (rstc)); -guard (srcu)(&rstc->srcu); - - rcdev =srcu_dereference (rstc->rcdev, &rstc->srcu); -if (!rcdev) -return -ENODEV ; +revocable_try_access_or_return (rstc->rev, rcdev);if (rstc->shared) {if (WARN_ON (atomic_read (&rstc->triggered_count) !=0 ))
@@ -679,11 +664,7 @@ int reset_control_status(struct reset_control *rstc)if (WARN_ON (IS_ERR (rstc)) ||reset_control_is_array (rstc))return -EINVAL ; -guard (srcu)(&rstc->srcu); - - rcdev =srcu_dereference (rstc->rcdev, &rstc->srcu); -if (!rcdev) -return -ENODEV ; +revocable_try_access_or_return (rstc->rev, rcdev);if (rcdev->ops->status)return rcdev->ops->status (rcdev, rstc->id);
@@ -731,11 +712,7 @@ int reset_control_acquire(struct reset_control *rstc)if (rstc->acquired)return 0 ; -guard (srcu)(&rstc->srcu); - - rcdev =srcu_dereference (rstc->rcdev, &rstc->srcu); -if (!rcdev) -return -ENODEV ; +revocable_try_access_or_return (rstc->rev, rcdev); scoped_guard(mutex, &rcdev->lock) {list_for_each_entry (rc, &rcdev->reset_control_head, list) {
@@ -831,7 +808,6 @@ __reset_control_get_internal(struct reset_controller_dev *rcdev,bool shared = flags &RESET_CONTROL_FLAGS_BIT_SHARED ;bool acquired = flags &RESET_CONTROL_FLAGS_BIT_ACQUIRED ;struct reset_control *rstc; -int ret;lockdep_assert_held (&rcdev->lock);
@@ -862,19 +838,13 @@ __reset_control_get_internal(struct reset_controller_dev *rcdev,if (!rstc)return ERR_PTR (-ENOMEM ); - ret =init_srcu_struct (&rstc->srcu); -if (ret) { -kfree (rstc); -return ERR_PTR (ret); - } -if (!try_module_get (rcdev->owner)) { -cleanup_srcu_struct (&rstc->srcu);kfree (rstc);return ERR_PTR (-ENODEV ); } -rcu_assign_pointer (rstc->rcdev, rcdev); +revocable_get (rcdev->rev); + rstc->rev = rcdev->rev;list_add (&rstc->list, &rcdev->reset_control_head); rstc->id = index;kref_init (&rstc->refcnt);
@@ -890,40 +860,23 @@ static void __reset_control_release(struct kref *kref) { struct reset_control *rstc = container_of(kref, struct reset_control, refcnt); -struct reset_controller_dev *rcdev; - -lockdep_assert_held (&rstc->srcu); - - rcdev =rcu_replace_pointer (rstc->rcdev,NULL , true); -if (rcdev) { -lockdep_assert_held (&rcdev->lock); -reset_controller_remove (rcdev, rstc); - }mutex_destroy (&rstc->lock); +revocable_put (rstc->rev); }static void reset_control_put_internal (struct reset_control *rstc) {struct reset_controller_dev *rcdev; -int ret =0 ;if (IS_ERR_OR_NULL (rstc))return ; - scoped_guard(srcu, &rstc->srcu) { - rcdev =srcu_dereference (rstc->rcdev, &rstc->srcu); -if (!rcdev) -/* Already released. */ -return ; - -guard (mutex)(&rcdev->lock); - ret =kref_put (&rstc->refcnt, __reset_control_release); - } +revocable_try_access_or_return_void (rstc->rev, rcdev); -if (ret) { -synchronize_srcu (&rstc->srcu); -cleanup_srcu_struct (&rstc->srcu); +guard (mutex)(&rcdev->lock); +if (kref_put (&rstc->refcnt, __reset_control_release)) { +reset_controller_remove (rcdev, rstc);kfree (rstc); } }
include/linux/reset-controller.h +3 -0
@@ -28,6 +28,7 @@ struct reset_control_ops {struct module ;struct device_node ;struct of_phandle_args ; +struct revocable ;/** * struct reset_controller_dev - reset controller entity that might
@@ -48,6 +49,7 @@ struct of_phandle_args;* :c:func:`fwnode_reset_simple_xlate` * @nr_resets: number of reset controls in this reset controller device * @lock: protects the reset control list from concurrent access +* @rev: revocable provider handle for itself */ struct reset_controller_dev {const struct reset_control_ops *ops;
@@ -65,6 +67,7 @@ struct reset_controller_dev { const struct fwnode_reference_args *reset_spec);unsigned int nr_resets;struct mutex lock; +struct revocable *rev; };#if IS_ENABLED (CONFIG_RESET_CONTROLLER )
Signature · 1 line
2.53.0
Bartosz Golaszewski
maintainerRe: [PATCH v14 5/5] reset: Leverage revocable for accessing struct reset_controller_dev
Quoted 46 lines · Tzung-Bi Shih
Commit
78ebbff6d1a0("reset: handle removing supplier before consumers") protected thercdevpointer instruct reset_controlwith SRCU so that the reset core can handle suppliers unbinding while consumers still hold reset control handles.Because
struct reset_controlhandles can outlivercdev, each of the N handles had to embed its ownsrcu_structand callsynchronize_srcu()on release, andreset_controller_unregister()had to callsynchronize_srcu()N times in a loop.Leverage dynamic revocable to allocate a single reference-counted
struct revocableforrcdevshared across all Nstruct reset_controlhandles (1-to-N). This reduces Nsrcu_structinstances per controller to 1, dropssynchronize_srcu()fromreset_control_put_internal(), and replaces the Nsynchronize_srcu()calls on unregistration with a singlerevocable_revoke().In addition, calling
revocable_revoke()outsidercdev->lockavoids an ABBA lock inversion betweenrcdev->lockand SRCU, asreset_control_acquire()andreset_control_put_internal()acquirercdev->lockinside the SRCU read-side critical section.Signed-off-by Tzung-Bi Shih <[email protected]>---
v14:
- New to the series.--- drivers/reset/core.c | 93 ++++++++------------------------ include/linux/reset-controller.h | 3 ++ 2 files changed, 26 insertions(+), 70 deletions(-)drivers/reset/core.c +1
-1@@ -24,8 +24,8 @@#include <linux/property.h> #include <linux/reset.h> #include <linux/reset-controller.h> +#include <linux/revocable.h> #include <linux/slab.h> -#include <linux/srcu.h> static DEFINE_MUTEX (reset_list_mutex);static LIST_HEAD (reset_controller_list);
drivers/reset/core.c static DEFINE_IDA(reset_gpio_ida); · line 40
Quoted 7 lines
drivers/reset/core.c +1
-3@@ -37,9 +37,7 @@ static DEFINE_IDA(reset_gpio_ida);/** * struct reset_control - a reset control -* @rcdev: a pointer to the reset controller device -* this reset control belongs to -* @srcu: protects the rcdev pointer from removal during consumer access +* @rev: revocable handle for accessing the reset controller device
Please call it "revocable". The name "rev" does not indicate what it is at all. Same elsewhere if you call it the same.
Otherwise looks good to me and nicely shrinks the code! We still don't know what the fate of that series will be though, right?
Bart
Re: [PATCH v14 5/5] reset: Leverage revocable for accessing struct reset_controller_dev
Quoted 16 lines · Bartosz Golaszewski
Quoted 16 lines
drivers/reset/core.c +1
-1@@ -24,8 +24,8 @@#include <linux/property.h> #include <linux/reset.h> #include <linux/reset-controller.h> +#include <linux/revocable.h> #include <linux/slab.h> -#include <linux/srcu.h> static DEFINE_MUTEX (reset_list_mutex);static LIST_HEAD (reset_controller_list);@@ -37,9 +37,7 @@ static DEFINE_IDA(reset_gpio_ida);
Quoted 9 lines
drivers/reset/core.c static DEFINE_IDA(reset_gpio_ida); · line 40
Quoted 6 lines
drivers/reset/core.c +1
-3/** * struct reset_control - a reset control -* @rcdev: a pointer to the reset controller device -* this reset control belongs to -* @srcu: protects the rcdev pointer from removal during consumer access +* @rev: revocable handle for accessing the reset controller device Please call it "revocable". The name "rev" does not indicate what it is at all. Same elsewhere if you call it the same.
Ack, I initially used "rev" to keep the lines shorter with the revocable APIs, but "rev" alone can indeed be confusing. On the other hand, I feel "revocable" is a bit verbose and doesn't indicate which target object it protects. How about calling it "rcdev_rev"?
Quoted 2 lines
Otherwise looks good to me and nicely shrinks the code! We still don't know what the fate of that series will be though, right?
Yes, that is also what I would like to check before sending v15.
Greg, Danilo, Rafael, Johan,
Since the revert in February, the API and lifetime model have been redesigned, and the series now has concrete in-kernel users for both embedded allocation (gpiolib in patch 3) and dynamic allocation (reset in patch 5).
Do you think this series is heading in a direction that driver-core can accept, or do you still have fundamental concerns with the approach? I would appreciate your thoughts on whether we should continue working on this series.
Philipp Zabel
maintainerRe: [PATCH v14 5/5] reset: Leverage revocable for accessing struct reset_controller_dev
Quoted 27 lines · Tzung-Bi Shih
Quoted 16 lines · Bartosz Golaszewski
Quoted 16 lines
drivers/reset/core.c +1
-1@@ -24,8 +24,8 @@#include <linux/property.h> #include <linux/reset.h> #include <linux/reset-controller.h> +#include <linux/revocable.h> #include <linux/slab.h> -#include <linux/srcu.h> static DEFINE_MUTEX (reset_list_mutex);static LIST_HEAD (reset_controller_list);@@ -37,9 +37,7 @@ static DEFINE_IDA(reset_gpio_ida);Quoted 9 lines
drivers/reset/core.c static DEFINE_IDA(reset_gpio_ida); · line 40
Quoted 6 lines
drivers/reset/core.c +1
-3/** * struct reset_control - a reset control -* @rcdev: a pointer to the reset controller device -* this reset control belongs to -* @srcu: protects the rcdev pointer from removal during consumer access +* @rev: revocable handle for accessing the reset controller device Please call it "revocable". The name "rev" does not indicate what it is at all. Same elsewhere if you call it the same.
Quoted 4 lines
Ack, I initially used "rev" to keep the lines shorter with the revocable APIs, but "rev" alone can indeed be confusing. On the other hand, I feel "revocable" is a bit verbose and doesn't indicate which target object it protects. How about calling it "
rcdev_rev"?
I strongly associate "rev" with "revision".
I'd be fine with "rcdev_revocable" (in struct reset_control and "revocable" in struct reset_controller_device).
regards
Philipp
Bartosz Golaszewski
maintainerRe: [PATCH v14 5/5] reset: Leverage revocable for accessing struct reset_controller_dev
Quoted 33 lines · Philipp Zabel
Quoted 27 lines · Tzung-Bi Shih
Quoted 16 lines · Bartosz Golaszewski
Quoted 16 lines
drivers/reset/core.c +1
-1@@ -24,8 +24,8 @@#include <linux/property.h> #include <linux/reset.h> #include <linux/reset-controller.h> +#include <linux/revocable.h> #include <linux/slab.h> -#include <linux/srcu.h> static DEFINE_MUTEX (reset_list_mutex);static LIST_HEAD (reset_controller_list);@@ -37,9 +37,7 @@ static DEFINE_IDA(reset_gpio_ida);Quoted 9 lines
drivers/reset/core.c static DEFINE_IDA(reset_gpio_ida); · line 40
Quoted 6 lines
drivers/reset/core.c +1
-3/** * struct reset_control - a reset control -* @rcdev: a pointer to the reset controller device -* this reset control belongs to -* @srcu: protects the rcdev pointer from removal during consumer access +* @rev: revocable handle for accessing the reset controller device Please call it "revocable". The name "rev" does not indicate what it is at all. Same elsewhere if you call it the same.
Quoted 4 lines
Ack, I initially used "rev" to keep the lines shorter with the revocable APIs, but "rev" alone can indeed be confusing. On the other hand, I feel "revocable" is a bit verbose and doesn't indicate which target object it protects. How about calling it "
rcdev_rev"?
Quoted 5 lines
I strongly associate "rev" with "revision".
I'd be fine with "
rcdev_revocable" (in structreset_controland "revocable" in structreset_controller_device).
I think "revocable" in both cases is fine. After all: we know what type the dereferenced variable is.
Bart
Re: [PATCH v14 5/5] reset: Leverage revocable for accessing struct reset_controller_dev
Quoted 18 lines · Bartosz Golaszewski
Quoted 17 lines · Philipp Zabel
Quoted 15 lines · Tzung-Bi Shih
Quoted 9 lines · Bartosz Golaszewski
Quoted 6 lines
/**
* structreset_control- a reset control
- * @rcdev: a pointer to the reset controller device
- * this reset control belongs to
- * @srcu: protects the rcdev pointer from removal during consumer access
+ * @rev: revocable handle for accessing the reset controller devicePlease call it "revocable". The name "rev" does not indicate what it is at all. Same elsewhere if you call it the same.
Ack, I initially used "rev" to keep the lines shorter with the revocable APIs, but "rev" alone can indeed be confusing. On the other hand, I feel "revocable" is a bit verbose and doesn't indicate which target object it protects. How about calling it "
rcdev_rev"?
Quoted 8 lines
Quoted 5 lines
I strongly associate "rev" with "revision".
I'd be fine with "
rcdev_revocable" (in structreset_controland "revocable" in structreset_controller_device).I think "revocable" in both cases is fine. After all: we know what type the dereferenced variable is.
Ack, will rename them to rcdev_revocable (in struct reset_control) and revocable (in struct reset_controller_dev).