drivers/base: Introduce revocable
Subsystems (5)
v15 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 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 4 is the user.
[1] https://lpc.events/event/17/contributions/1627/
[2] https://lpc.events/event/19/contributions/2262/
---
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 4).
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.
---
v15:
- Rebase onto v7.4-rc1.
- Fix resource and kthread cleanup on error paths in KUnit tests.
- Rename revocable struct members in GPIO and reset subsystems.
- Separate patch 4 (KUnit tests for reset controller) out.
v14: https://lore.kernel.org/all/[email protected]
- 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 (4):
revocable: Revocable resource management
revocable: Add KUnit test cases
gpio: Leverage revocable for accessing struct gpio_chip
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 | 598 ++++++++++++++++++ drivers/gpio/gpiolib-cdev.c | 77 +-- drivers/gpio/gpiolib-sysfs.c | 32 +- drivers/gpio/gpiolib.c | 262 ++++---- drivers/gpio/gpiolib.h | 28 +- drivers/reset/core.c | 125 ++-- include/linux/reset-controller.h | 3 + include/linux/revocable.h | 220 +++++++ 15 files changed, 1781 insertions(+), 330 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 include/linux/revocable.h
Signature · 1 line
2.53.0
PATCH v15 1/4 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.
---
Test:
$ make O=build SPHINXDIRS=driver-api/driver-model/ htmldocs
$ python3 -m http.server --directory \
build/Documentation/output/driver-api/driver-model/
---
v15:
- No changes.v14: https://lore.kernel.org/all/[email protected]
- 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
@@ -8161,6 +8161,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
@@ -23982,6 +23983,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 v15 2/4 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.
---
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
---
v15:
- Clean up kthreads and revocable handles on error paths in concurrent
access tests.v14: https://lore.kernel.org/all/[email protected]
- 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 | 598 +++++++++++++++++++++++++++++ 4 files changed, 606 insertions(+) create mode 100644 drivers/base/test/revocable-test.c
MAINTAINERS +1 -0
@@ -23990,6 +23990,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 +598 -0
598 lines not on this page · raw message
Signature · 1 line
2.53.0
PATCH v15 3/4 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.
---
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
---
v15:
- Rename "chip_rev" -> "gc_revocable" in struct gpio_device.
- Destroy gc_revocable before desc_srcu in gpiodev_release() to match
the reverse initialization order.
- Rebase onto v7.4-rc1.
- Fix conflicts with c28846ca1ac6 ("gpiolib: fix memory leak in
gpiod_set_array_value_complex() on error paths") and 8381e1f15e27
("gpiolib: Free chip resources when allocating the label fails").v14: https://lore.kernel.org/all/[email protected]
- 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 | 262 +++++++++++++++-------------------- drivers/gpio/gpiolib.h | 28 +--- 4 files changed, 156 insertions(+), 243 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->gc_revocable, gc);switch (cmd) {case GPIOHANDLE_GET_LINE_VALUES_IOCTL :
@@ -1442,11 +1441,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->gc_revocable, gc);switch (cmd) {case GPIO_V2_LINE_GET_VALUES_IOCTL :
@@ -1473,10 +1470,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->gc_revocable, gc); +if (!gc)return EPOLLHUP |EPOLLERR ;poll_wait (file, &lr->wait, wait);
@@ -1495,11 +1492,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->gc_revocable, gc);if (count <sizeof (le))return -EINVAL ;
@@ -1769,10 +1764,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->gc_revocable, gc); +if (!gc)return EPOLLHUP |EPOLLERR ;poll_wait (file, &le->wait, wait);
@@ -1807,11 +1802,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->gc_revocable, gc);/* * When compatible system call is being used the struct gpioevent_data,
@@ -1889,11 +1882,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->gc_revocable, gc);/* * We can get the value for an event line but not set it,
@@ -2175,12 +2166,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->gc_revocable, gc); info->offset =gpiod_hwgpio (desc);
@@ -2212,10 +2202,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 ; }
@@ -2406,12 +2396,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->gc_revocable, gc);/* Fill in the struct and pass to userspace */ switch (cmd) {
@@ -2469,12 +2457,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->gc_revocable, 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,
@@ -2562,10 +2547,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->gc_revocable, gc); +if (!gc)return EPOLLHUP |EPOLLERR ;poll_wait (file, &cdev->wait, pollt);
@@ -2585,11 +2570,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->gc_revocable, 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->gc_revocable, 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->gc_revocable, 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->gc_revocable, 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->gc_revocable, 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 +109 -153
678 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 +* @gc_revocable: 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 gc_revocable;#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
PATCH v15 4/4 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().
---
Applied after https://lore.kernel.org/all/[email protected]
---
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 \
--raw_output=all \
reset_test
---
v15:
- Rename:
- "rev" -> "revocable" in struct reset_controller_dev.
- "rev" -> "rcdev_revocable" in struct reset_control.v14: https://lore.kernel.org/all/[email protected]
- New to the series.
--- drivers/reset/core.c | 125 ++++++++++--------------------- include/linux/reset-controller.h | 3 + 2 files changed, 42 insertions(+), 86 deletions(-)
drivers/reset/core.c +39 -86
@@ -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 +* @rcdev_revocable: 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 *rcdev_revocable;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->revocable =revocable_alloc (rcdev); +if (!rcdev->revocable) +return -ENOMEM ; +if (rcdev->of_xlate) rcdev->fwnode_reset_n_cells = rcdev->of_reset_n_cells;
@@ -160,14 +161,20 @@ int reset_controller_register(struct reset_controller_dev *rcdev) }EXPORT_SYMBOL_GPL (reset_controller_register); -static void reset_control_put_internal (struct reset_control *rstc); -static void reset_controller_remove (struct reset_controller_dev *rcdev,struct reset_control *rstc) {lockdep_assert_held (&rcdev->lock); -list_del (&rstc->list); +/* +* reset_control_put_internal() may observe a non-NULL rcdev before +* revocable_revoke() runs, and then wait on rcdev->lock while +* reset_controller_unregister() already detaches rstc. +*/ +if (list_empty (&rstc->list)) +return ; +list_del_init (&rstc->list); +module_put (rcdev->owner);put_device (rcdev->dev); }
@@ -179,38 +186,22 @@ static void reset_controller_remove(struct reset_controller_dev *rcdev,void reset_controller_unregister (struct reset_controller_dev *rcdev) {struct reset_control *rstc, *pos; -LIST_HEAD (reset_control_head); scoped_guard(mutex, &reset_list_mutex)list_del (&rcdev->list); scoped_guard(mutex, &rcdev->lock) { -/* -* Numb but don't free the remaining reset control handles that are -* still held by consumers. -* -* Hold a temporary reference on rstc before clearing rstc->rcdev: -* once rstc->rcdev is NULL, a concurrent reset_control_put_internal() -* will not acquire rcdev->lock and could otherwise free rstc. -*/ -list_for_each_entry_safe (rstc, pos, &rcdev->reset_control_head, list) { -kref_get (&rstc->refcnt); -rcu_assign_pointer (rstc->rcdev,NULL ); +list_for_each_entry_safe (rstc, pos, &rcdev->reset_control_head, list)reset_controller_remove (rcdev, rstc); -list_add (&rstc->list, &reset_control_head); - } }/* -* Run synchronize_srcu() outside rcdev->lock to avoid a circular +* Run revocable_revoke() outside rcdev->lock to avoid a circular * locking dependency with paths that acquire rcdev->lock inside -* the rstc->srcu read-side critical section. +* the revocable read-side critical section. */ -list_for_each_entry_safe (rstc, pos, &reset_control_head, list) { -list_del (&rstc->list); -synchronize_srcu (&rstc->srcu); -reset_control_put_internal (rstc); - } +revocable_revoke (rcdev->revocable); +revocable_put (rcdev->revocable);mutex_destroy (&rcdev->lock); }
@@ -401,11 +392,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->rcdev_revocable, rcdev);if (!rcdev->ops->reset)return -ENOTSUPP ;
@@ -522,11 +509,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->rcdev_revocable, rcdev);if (rstc->shared) {if (WARN_ON (atomic_read (&rstc->triggered_count) !=0 ))
@@ -618,11 +601,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->rcdev_revocable, rcdev);if (rstc->shared) {if (WARN_ON (atomic_read (&rstc->triggered_count) !=0 ))
@@ -698,11 +677,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->rcdev_revocable, rcdev);if (rcdev->ops->status)return rcdev->ops->status (rcdev, rstc->id);
@@ -750,11 +725,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->rcdev_revocable, rcdev); scoped_guard(mutex, &rcdev->lock) {list_for_each_entry (rc, &rcdev->reset_control_head, list) {
@@ -850,7 +821,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);
@@ -881,19 +851,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->revocable); + rstc->rcdev_revocable = rcdev->revocable;list_add (&rstc->list, &rcdev->reset_control_head); rstc->id = index;kref_init (&rstc->refcnt);
@@ -909,17 +873,9 @@ 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->rcdev_revocable); }static void reset_control_put_internal (struct reset_control *rstc)
@@ -930,22 +886,19 @@ static void reset_control_put_internal(struct reset_control *rstc)if (IS_ERR_OR_NULL (rstc))return ; - scoped_guard(srcu, &rstc->srcu) { - rcdev =srcu_dereference (rstc->rcdev, &rstc->srcu); -if (rcdev) { -guard (mutex)(&rcdev->lock); - ret =kref_put (&rstc->refcnt, __reset_control_release); - }else { -/* Already detached from rcdev. */ - ret =kref_put (&rstc->refcnt, __reset_control_release); - } +revocable_try_access_with (rstc->rcdev_revocable, rcdev); +if (rcdev) { +guard (mutex)(&rcdev->lock); + ret =kref_put (&rstc->refcnt, __reset_control_release); +if (ret) +reset_controller_remove (rcdev, rstc); + }else { +/* Already detached from rcdev. */ + ret =kref_put (&rstc->refcnt, __reset_control_release); } -if (ret) { -synchronize_srcu (&rstc->srcu); -cleanup_srcu_struct (&rstc->srcu); +if (ret)kfree (rstc); - } }static void reset_gpio_aux_device_release (struct device *dev)
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 +* @revocable: 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 *revocable; };#if IS_ENABLED (CONFIG_RESET_CONTROLLER )
Signature · 1 line
2.53.0
Philipp Zabel
maintainerRe: [PATCH v15 4/4] reset: Leverage revocable for accessing struct reset_controller_dev
Quoted 96 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().Signed-off-by Tzung-Bi Shih <[email protected]>---
Applied after https://lore.kernel.org/all/[email protected]
---
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 \ --raw_output=all \ reset_test --- v15: - Rename: - "rev" -> "revocable" in struct reset_controller_dev. - "rev" -> "rcdev_revocable" in struct reset_control.v14: https://lore.kernel.org/all/[email protected]
- New to the series.--- drivers/reset/core.c | 125 ++++++++++--------------------- include/linux/reset-controller.h | 3 + 2 files changed, 42 insertions(+), 86 deletions(-)drivers/reset/core.c +7
-6@@ -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 +* @rcdev_revocable: 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 *rcdev_revocable;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->revocable =revocable_alloc (rcdev); +if (!rcdev->revocable) +return -ENOMEM ; +if (rcdev->of_xlate) rcdev->fwnode_reset_n_cells = rcdev->of_reset_n_cells;
drivers/reset/core.c int reset_controller_register(struct reset_controller_dev *rcdev) · line 164
Quoted 5 lines
drivers/reset/core.c +0
-2@@ -160,14 +161,20 @@ int reset_controller_register(struct reset_controller_dev *rcdev) }EXPORT_SYMBOL_GPL (reset_controller_register); -static void reset_control_put_internal (struct reset_control *rstc); -
See what I said about the fix series that just introduced this.
Quoted 5 lines
drivers/reset/core.c +0
-0static void reset_controller_remove (struct reset_controller_dev *rcdev,struct reset_control *rstc) {lockdep_assert_held (&rcdev->lock);
drivers/reset/core.c int reset_controller_register(struct reset_controller_dev *rcdev) · line 177
Quoted 10 lines
drivers/reset/core.c +9
-1-list_del (&rstc->list); +/* +* reset_control_put_internal() may observe a non-NULL rcdev before +* revocable_revoke() runs, and then wait on rcdev->lock while +* reset_controller_unregister() already detaches rstc. +*/ +if (list_empty (&rstc->list)) +return ; +list_del_init (&rstc->list); +
See also what I said about the fix series. If this were split into list_del() and module_put()/put/device() parts already, this wouldn't be necessary.
Quoted 148 lines
drivers/reset/core.c +12
-57module_put (rcdev->owner);put_device (rcdev->dev); }@@ -179,38 +186,22 @@ static void reset_controller_remove(struct reset_controller_dev *rcdev,void reset_controller_unregister (struct reset_controller_dev *rcdev) {struct reset_control *rstc, *pos; -LIST_HEAD (reset_control_head); scoped_guard(mutex, &reset_list_mutex)list_del (&rcdev->list); scoped_guard(mutex, &rcdev->lock) { -/* -* Numb but don't free the remaining reset control handles that are -* still held by consumers. -* -* Hold a temporary reference on rstc before clearing rstc->rcdev: -* once rstc->rcdev is NULL, a concurrent reset_control_put_internal() -* will not acquire rcdev->lock and could otherwise free rstc. -*/ -list_for_each_entry_safe (rstc, pos, &rcdev->reset_control_head, list) { -kref_get (&rstc->refcnt); -rcu_assign_pointer (rstc->rcdev,NULL ); +list_for_each_entry_safe (rstc, pos, &rcdev->reset_control_head, list)reset_controller_remove (rcdev, rstc); -list_add (&rstc->list, &reset_control_head); - } }/* -* Run synchronize_srcu() outside rcdev->lock to avoid a circular +* Run revocable_revoke() outside rcdev->lock to avoid a circular * locking dependency with paths that acquire rcdev->lock inside -* the rstc->srcu read-side critical section. +* the revocable read-side critical section. */ -list_for_each_entry_safe (rstc, pos, &reset_control_head, list) { -list_del (&rstc->list); -synchronize_srcu (&rstc->srcu); -reset_control_put_internal (rstc); - } +revocable_revoke (rcdev->revocable); +revocable_put (rcdev->revocable);mutex_destroy (&rcdev->lock); }@@ -401,11 +392,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->rcdev_revocable, rcdev);if (!rcdev->ops->reset)return -ENOTSUPP ;@@ -522,11 +509,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->rcdev_revocable, rcdev);if (rstc->shared) {if (WARN_ON (atomic_read (&rstc->triggered_count) !=0 ))@@ -618,11 +601,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->rcdev_revocable, rcdev);if (rstc->shared) {if (WARN_ON (atomic_read (&rstc->triggered_count) !=0 ))@@ -698,11 +677,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->rcdev_revocable, rcdev);if (rcdev->ops->status)return rcdev->ops->status (rcdev, rstc->id);@@ -750,11 +725,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->rcdev_revocable, rcdev); scoped_guard(mutex, &rcdev->lock) {list_for_each_entry (rc, &rcdev->reset_control_head, list) {@@ -850,7 +821,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);@@ -881,19 +851,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->revocable); + rstc->rcdev_revocable = rcdev->revocable;list_add (&rstc->list, &rcdev->reset_control_head); rstc->id = index;kref_init (&rstc->refcnt);@@ -909,17 +873,9 @@ static void __reset_control_release(struct kref *kref) { struct reset_control *rstc = container_of(kref, struct reset_control, refcnt); -struct reset_controller_dev *rcdev; -
drivers/reset/core.c static void __reset_control_release(struct kref *kref) · line 878
Quoted 10 lines
drivers/reset/core.c +1
-7-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->rcdev_revocable);
I think now we can move the kfree(rstc) back in here?
Quoted 15 lines
drivers/reset/core.c +0
-8}static void reset_control_put_internal (struct reset_control *rstc)@@ -930,22 +886,19 @@ static void reset_control_put_internal(struct reset_control *rstc)if (IS_ERR_OR_NULL (rstc))return ; - scoped_guard(srcu, &rstc->srcu) { - rcdev =srcu_dereference (rstc->rcdev, &rstc->srcu); -if (rcdev) { -guard (mutex)(&rcdev->lock); - ret =kref_put (&rstc->refcnt, __reset_control_release); - }else { -/* Already detached from rcdev. */ - ret =kref_put (&rstc->refcnt, __reset_control_release);
drivers/reset/core.c static void reset_control_put_internal(struct reset_control *rstc) · line 897
Quoted 10 lines
drivers/reset/core.c +9
-1- } +revocable_try_access_with (rstc->rcdev_revocable, rcdev); +if (rcdev) { +guard (mutex)(&rcdev->lock); + ret =kref_put (&rstc->refcnt, __reset_control_release); +if (ret) +reset_controller_remove (rcdev, rstc); + }else { +/* Already detached from rcdev. */ + ret =kref_put (&rstc->refcnt, __reset_control_release);
If I am not mistaken, this is all that is actually needed from the reset_control_put_internal() call in reset_controller_unregister(), just call it directly.
drivers/reset/core.c static void reset_control_put_internal(struct reset_control *rstc) · line 901
Quoted 7 lines
drivers/reset/core.c +1
-3} -if (ret) { -synchronize_srcu (&rstc->srcu); -cleanup_srcu_struct (&rstc->srcu); +if (ret)kfree (rstc);
This could be moved back into __reset_control_release().
regards
Philipp