drivers/base: Introduce revocable
Subsystems (6)
Tzung-Bi Shih
maintainerv10 cover drivers/base: Introduce revocable
This series introduces the "revocable" mechanism, a synchronization primitive designed to prevent Use-After-Free errors.
- Patch 1 introduces the revocable which is an implementation of ideas from the talk [1].
- Patch 2 adds KUnit test cases.
- Patches 3 to 7 transitions the UAF prevention logic within the GPIO core (gpiolib) to use the "revocable" mechanism.
The existing code aims to prevent UAF issues when the underlying GPIO chip is removed. They replace that custom logic with the generic "revocable" API, which is designed to handle such lifecycle dependencies. There should be no changes in behavior.
- Patches 8 to 9 uses "revocable" mechanism to fix an UAF in cros_ec_chardev driver. Alternatively, [2] is a series for fixing the same issue without using "revocable".
Since v9, there are two ways to manage the resource provider handle.
- Embedded allocation: patches 3 to 7 might be the potential user.
- Dynamic allocation: patches 8 to 9 might be the potential user.
[1] https://lpc.events/event/17/contributions/1627/
[2] https://lore.kernel.org/all/[email protected]
---
v10:
- 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 (9):
revocable: Revocable resource management
revocable: Add KUnit test cases
gpio: Add revocable provider handle for struct gpio_chip
gpio: cdev: Leverage revocable for accessing struct gpio_chip
gpio: Remove gpio_chip_guard by using revocable
gpio: Leverage revocable for accessing struct gpio_chip
gpio: Remove unused chip and srcu in struct gpio_device platform/chrome: Protect cros_ec_device lifecycle with revocable platform/chrome: cros_ec_chardev: Consume cros_ec_device via revocable
.../driver-api/driver-model/index.rst | 1 + .../driver-api/driver-model/revocable.rst | 384 +++++++++++++++++ MAINTAINERS | 10 + drivers/base/Kconfig | 5 + drivers/base/Makefile | 5 +- drivers/base/revocable.c | 267 ++++++++++++ drivers/base/revocable_test.c | 405 ++++++++++++++++++ drivers/gpio/gpiolib-cdev.c | 77 ++-- drivers/gpio/gpiolib-sysfs.c | 31 +- drivers/gpio/gpiolib.c | 263 +++++------- drivers/gpio/gpiolib.h | 28 +- drivers/platform/chrome/cros_ec.c | 11 + drivers/platform/chrome/cros_ec_chardev.c | 80 +++- include/linux/platform_data/cros_ec_proto.h | 3 + include/linux/revocable.h | 204 +++++++++ 15 files changed, 1505 insertions(+), 269 deletions(-) create mode 100644 Documentation/driver-api/driver-model/revocable.rst create mode 100644 drivers/base/revocable.c create mode 100644 drivers/base/revocable_test.c create mode 100644 include/linux/revocable.h
Signature · 1 line
2.51.0
Tzung-Bi Shih
maintainerPATCH v10 1/9 revocable: Revocable resource management
The "revocable" mechanism is a synchronization primitive designed to manage safe access to resources that can be asynchronously removed or invalidated. Its primary purpose is to prevent Use-After-Free (UAF) errors when interacting with resources whose lifetimes are not guaranteed to outlast their consumers.
This is particularly useful in systems where resources can disappear unexpectedly, such as those provided by hot-pluggable devices like USB. When a consumer holds a reference to such a resource, the underlying device might be removed, causing the resource's memory to be freed. Subsequent access attempts by the consumer would then lead to UAF errors.
Revocable addresses this by providing a form of "weak reference" and a controlled access method. It allows a resource consumer to safely attempt to access the resource. The mechanism guarantees that any access granted is valid for the duration of its use. If the resource has already been revoked (i.e., freed), the access attempt will fail safely, typically by returning NULL, instead of causing a crash.
It uses a provider/consumer model built on Sleepable RCU (SRCU) to guarantee safe memory access:
- A resource provider, such as a driver for a hot-pluggable device, allocates a struct revocable and initializes it with a pointer to the resource.
- A resource consumer that wants to access the resource allocates a struct revocable_consumer containing a reference to the provider.
- To access the resource, the consumer uses revocable_try_access(). This function enters an SRCU read-side critical section and returns the pointer to the resource. If the provider has already freed the resource, it returns NULL. After use, the consumer calls revocable_withdraw_access() to exit the SRCU critical section. There are some macro level helpers for doing that.
The API provides the following contract:
- revocable_try_access() can be safely called from both process and atomic contexts.
- It is permitted to sleep within the critical section established between revocable_try_access() and revocable_withdraw_access().
- revocable_try_access() and the matching revocable_withdraw_access() must occur in the same context. For example, it is illegal to invoke revocable_withdraw_access() in an irq handler if the matching revocable_try_access() was invoked in process context.
- When the provider needs to remove the resource, it calls revocable_revoke(). This function sets the internal resource pointer to NULL and then calls synchronize_srcu() to wait for all current readers to finish before the resource can be completely torn down.
---
v10:
- 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]
A way to verify Documentation/:
- make O=build SPHINXDIRS=driver-api/driver-model/ htmldocs.
--- .../driver-api/driver-model/index.rst | 1 + .../driver-api/driver-model/revocable.rst | 384 ++++++++++++++++++ MAINTAINERS | 9 + drivers/base/Makefile | 2 +- drivers/base/revocable.c | 267 ++++++++++++ include/linux/revocable.h | 204 ++++++++++ 6 files changed, 866 insertions(+), 1 deletion(-) create mode 100644 Documentation/driver-api/driver-model/revocable.rst create mode 100644 drivers/base/revocable.c create mode 100644 include/linux/revocable.h
Documentation/driver-api/driver-model/index.rst +1 -0
@@ -14,3 +14,4 @@ Driver Model overview platform porting + revocable
Documentation/driver-api/driver-model/revocable.rst new file +384 -0
384 lines not on this page · raw message
MAINTAINERS +9 -0
@@ -7808,6 +7808,7 @@ F: include/linux/fwnode.h F: include/linux/kobj* F: include/linux/ksysfs.h F: include/linux/property.h +F: include/linux/revocable.h F: include/linux/sysfs.h F: kernel/ksysfs.c F: lib/kobj*
@@ -22824,6 +22825,14 @@ F: include/uapi/linux/rseq.h F: kernel/rseq.c F: tools/testing/selftests/rseq/ +REVOCABLE RESOURCE MANAGEMENT +M: Tzung-Bi Shih <[email protected]> +L: [email protected] +S: Maintained +T: git git://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git +F: drivers/base/revocable.c +F: include/linux/revocable.h + RFKILL M: Johannes Berg <[email protected]> L: [email protected]
drivers/base/Makefile +1 -1
@@ -6,7 +6,7 @@ obj-y := component.o core.o bus.o dd.o syscore.o \ cpu.o firmware.o init.o map.o devres.o \ attribute_container.o transport_class.o \ topology.o container.o property.o cacheinfo.o \ - swnode.o faux.o + swnode.o faux.o revocable.o obj-$(CONFIG_AUXILIARY_BUS) += auxiliary.o obj-$(CONFIG_DEVTMPFS) += devtmpfs.o obj-y += power/
drivers/base/revocable.c new file +267 -0
267 lines not on this page · raw message
include/linux/revocable.h new file +204 -0
204 lines not on this page · raw message
Signature · 1 line
2.51.0
Tzung-Bi Shih
maintainerPATCH v10 2/9 revocable: Add KUnit test cases
In reply to Tzung-Bi ShihAdd KUnit test cases for the revocable API.
The test cases cover the following scenarios:
- Basic: Verifies that a consumer can successfully access the resource.
- Revocation: Verifies that after the provider revokes the resource, the consumer correctly receives a NULL pointer on a subsequent access.
- Try Access Macro: Same as "Revocation" but uses the macro level helpers.
- Concurrent Access: Verifies multiple threads can access the resource.
---
v10:
- 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.
A way to run the test:
$ ./tools/testing/kunit/kunit.py run \
--kconfig_add CONFIG_REVOCABLE_KUNIT_TEST=y \
revocable_test
Or
$ ./tools/testing/kunit/kunit.py run \
--kconfig_add CONFIG_REVOCABLE_KUNIT_TEST=y \
--kconfig_add CONFIG_PROVE_LOCKING=y \
--kconfig_add CONFIG_DEBUG_KERNEL=y \
--kconfig_add CONFIG_DEBUG_INFO=y \
--kconfig_add CONFIG_DEBUG_INFO_DWARF5=y \
--kconfig_add CONFIG_KASAN=y \
--kconfig_add CONFIG_DETECT_HUNG_TASK=y \
--kconfig_add CONFIG_DEFAULT_HUNG_TASK_TIMEOUT="10" \
--arch=x86_64 \
--make_options="C=1 W=1" \
revocable_test--- MAINTAINERS | 1 + drivers/base/Kconfig | 5 + drivers/base/Makefile | 3 + drivers/base/revocable_test.c | 405 ++++++++++++++++++++++++++++++++++ 4 files changed, 414 insertions(+) create mode 100644 drivers/base/revocable_test.c
MAINTAINERS +1 -0
@@ -22831,6 +22831,7 @@ L: [email protected] S: Maintained T: git git://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git F: drivers/base/revocable.c +F: drivers/base/revocable_test.c F: include/linux/revocable.h RFKILL
drivers/base/Kconfig +5 -0
@@ -259,3 +259,8 @@ config FW_DEVLINK_SYNC_STATE_TIMEOUT work on. endmenu + +config REVOCABLE_KUNIT_TEST + tristate "KUnit tests for revocable" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS
drivers/base/Makefile +3 -0
@@ -35,3 +35,6 @@ ccflags-$(CONFIG_DEBUG_DRIVER) := -DDEBUG # define_trace.h needs to know how to find our header CFLAGS_trace.o := -I$(src) obj-$(CONFIG_TRACING) += trace.o + +# KUnit test cases +obj-$(CONFIG_REVOCABLE_KUNIT_TEST) += revocable_test.o
drivers/base/revocable_test.c new file +405 -0
405 lines not on this page · raw message
Signature · 1 line
2.51.0
Tzung-Bi Shih
maintainerPATCH v10 3/9 gpio: Add revocable provider handle for struct gpio_chip
In reply to Tzung-Bi ShihThe underlying chip can be removed asynchronously. gdev->srcu is used to ensure the synchronization before accessing gdev->chip.
Revocable encapsulates the details. Add revocable provider handle for the corresponding struct gpio_chip in struct gpio_device so that it can start to hide the synchronization details.
---
v10:
- 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.c | 13 +++++++++++-- drivers/gpio/gpiolib.h | 3 +++ 2 files changed, 14 insertions(+), 2 deletions(-)
drivers/gpio/gpiolib.c +11 -2
@@ -23,6 +23,7 @@#include <linux/nospec.h> #include <linux/of.h> #include <linux/pinctrl/consumer.h> +#include <linux/revocable.h> #include <linux/seq_file.h> #include <linux/slab.h> #include <linux/srcu.h>
@@ -874,6 +875,7 @@ static void gpiodev_release(struct device *dev)synchronize_srcu (&gdev->desc_srcu);cleanup_srcu_struct (&gdev->desc_srcu); +revocable_put (&gdev->chip_rev);ida_free (&gpio_ida, gdev->id);kfree_const (gdev->label);kfree (gdev->descs);
@@ -1210,6 +1212,10 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,else gdev->owner =THIS_MODULE ; + ret =revocable_init (&gdev->chip_rev, gc); +if (ret) + goto err_put_device; + scoped_guard(mutex, &gpio_devices_lock) {/* * TODO: this allocates a Linux GPIO number base in the global
@@ -1224,7 +1230,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,if (base <0 ) { ret = base; base =0 ; - goto err_put_device; + goto err_revoke_chip_rev; }/*
@@ -1244,7 +1250,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, ret =gpiodev_add_to_list_unlocked (gdev);if (ret) {gpiochip_err (gc,"GPIO integer space overlap, cannot add chip\n" ); - goto err_put_device; + goto err_revoke_chip_rev; } }
@@ -1343,6 +1349,8 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, scoped_guard(mutex, &gpio_devices_lock)list_del_rcu (&gdev->list);synchronize_srcu (&gpio_devices_srcu); +err_revoke_chip_rev: +revocable_revoke (&gdev->chip_rev); err_put_device:gpio_device_put (gdev); goto err_print_message;
@@ -1389,6 +1397,7 @@ void gpiochip_remove(struct gpio_chip *gc)/* Numb the device, cancelling all outstanding operations */ rcu_assign_pointer (gdev->chip,NULL );synchronize_srcu (&gdev->srcu); +revocable_revoke (&gdev->chip_rev);gpio_device_teardown_shared (gdev);gpiochip_irqchip_remove (gc);acpi_gpiochip_remove (gc);
drivers/gpio/gpiolib.h +3 -0
@@ -16,6 +16,7 @@#include <linux/gpio/driver.h> #include <linux/module.h> #include <linux/notifier.h> +#include <linux/revocable.h> #include <linux/spinlock.h> #include <linux/string.h> #include <linux/srcu.h>
@@ -55,6 +56,7 @@ struct fwnode_handle;* @device_notifier: used to notify character device wait queues about the GPIO * device being unregistered * @srcu: protects the pointer to the underlying GPIO chip +* @chip_rev: revocable provider handle for the corresponding struct gpio_chip. * @pin_ranges: range of pins served by the GPIO driver * * This state container holds most of the runtime variable data
@@ -82,6 +84,7 @@ struct gpio_device {struct workqueue_struct *line_state_wq;struct blocking_notifier_head device_notifier;struct srcu_struct srcu; +struct revocable chip_rev;#ifdef CONFIG_PINCTRL /*
Signature · 1 line
2.51.0
Tzung-Bi Shih
maintainerPATCH v10 4/9 gpio: cdev: Leverage revocable for accessing struct gpio_chip
In reply to Tzung-Bi ShihStruct gpio_device now provides a revocable provider to the underlying struct gpio_chip. Leverage revocable for accessing the struct gpio_chip.
---
v10:
- No changes.
v9: https://lore.kernel.org/all/[email protected]
- New to the series.
- Rename "chip_rp" -> "chip_rev".
v4 - v8:
- Doesn't exist.
v3: https://lore.kernel.org/all/[email protected]
- Change revocable API usages accordingly.
v2: https://lore.kernel.org/all/[email protected]
- Change usages accordingly after applying
https://lore.kernel.org/all/[email protected].
- Preserve a local storage for struct revocable.
- Combine multiple patches (see "v1:").
- Fix a race condition reported in
https://lore.kernel.org/all/CAMRc=McDaipt85OHm0MksLkuf6E79dY1uNSqqbcJnoQTUs81Pw@mail.gmail.com/
and analyzed in
https://lore.kernel.org/all/aXEEUWwkxHZzCnaI@tzungbi-laptop/.
In v1, the blocking_notifier_chain_unregister() will be skipped if the chip has been removed, leading an UAF in gpiolib_cdev_unregister(). In v2, it won't skip blocking_notifier_chain_unregister().
v1:
- https://lore.kernel.org/all/[email protected]
- https://lore.kernel.org/all/[email protected]
- https://lore.kernel.org/all/[email protected]
- https://lore.kernel.org/all/[email protected]
- https://lore.kernel.org/all/[email protected]
--- drivers/gpio/gpiolib-cdev.c | 68 ++++++++++++++----------------------- 1 file changed, 26 insertions(+), 42 deletions(-)
drivers/gpio/gpiolib-cdev.c +26 -42
@@ -22,6 +22,7 @@#include <linux/overflow.h> #include <linux/pinctrl/consumer.h> #include <linux/poll.h> +#include <linux/revocable.h> #include <linux/seq_file.h> #include <linux/spinlock.h> #include <linux/string.h>
@@ -210,11 +211,9 @@ static long linehandle_ioctl(struct file *file, unsigned int cmd,DECLARE_BITMAP (vals,GPIOHANDLES_MAX );unsigned int i;int ret; +struct gpio_chip *gc; -guard (srcu)(&lh->gdev->srcu); - -if (!rcu_access_pointer (lh->gdev->chip)) -return -ENODEV ; +revocable_try_access_or_return (&lh->gdev->chip_rev, gc);switch (cmd) {case GPIOHANDLE_GET_LINE_VALUES_IOCTL :
@@ -1432,11 +1431,9 @@ static long linereq_ioctl(struct file *file, unsigned int cmd, {struct linereq *lr = file->private_data;void __user *ip = (void __user *)arg; +struct gpio_chip *gc; -guard (srcu)(&lr->gdev->srcu); - -if (!rcu_access_pointer (lr->gdev->chip)) -return -ENODEV ; +revocable_try_access_or_return (&lr->gdev->chip_rev, gc);switch (cmd) {case GPIO_V2_LINE_GET_VALUES_IOCTL :
@@ -1463,10 +1460,10 @@ static __poll_t linereq_poll(struct file *file, {struct linereq *lr = file->private_data;__poll_t events =0 ; +struct gpio_chip *gc; -guard (srcu)(&lr->gdev->srcu); - -if (!rcu_access_pointer (lr->gdev->chip)) +revocable_try_access_with (&lr->gdev->chip_rev, gc); +if (!gc)return EPOLLHUP |EPOLLERR ;poll_wait (file, &lr->wait, wait);
@@ -1485,11 +1482,9 @@ static ssize_t linereq_read(struct file *file, char __user *buf,struct gpio_v2_line_event le;ssize_t bytes_read =0 ;int ret; +struct gpio_chip *gc; -guard (srcu)(&lr->gdev->srcu); - -if (!rcu_access_pointer (lr->gdev->chip)) -return -ENODEV ; +revocable_try_access_or_return (&lr->gdev->chip_rev, gc);if (count <sizeof (le))return -EINVAL ;
@@ -1759,10 +1754,10 @@ static __poll_t lineevent_poll(struct file *file, {struct lineevent_state *le = file->private_data;__poll_t events =0 ; +struct gpio_chip *gc; -guard (srcu)(&le->gdev->srcu); - -if (!rcu_access_pointer (le->gdev->chip)) +revocable_try_access_with (&le->gdev->chip_rev, gc); +if (!gc)return EPOLLHUP |EPOLLERR ;poll_wait (file, &le->wait, wait);
@@ -1797,11 +1792,9 @@ static ssize_t lineevent_read(struct file *file, char __user *buf,ssize_t bytes_read =0 ;ssize_t ge_size;int ret; +struct gpio_chip *gc; -guard (srcu)(&le->gdev->srcu); - -if (!rcu_access_pointer (le->gdev->chip)) -return -ENODEV ; +revocable_try_access_or_return (&le->gdev->chip_rev, gc);/* * When compatible system call is being used the struct gpioevent_data,
@@ -1879,11 +1872,9 @@ static long lineevent_ioctl(struct file *file, unsigned int cmd,struct lineevent_state *le = file->private_data;void __user *ip = (void __user *)arg;struct gpiohandle_data ghd; +struct gpio_chip *gc; -guard (srcu)(&le->gdev->srcu); - -if (!rcu_access_pointer (le->gdev->chip)) -return -ENODEV ; +revocable_try_access_or_return (&le->gdev->chip_rev, gc);/* * We can get the value for an event line but not set it,
@@ -2385,12 +2376,10 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)struct gpio_chardev_data *cdev = file->private_data;struct gpio_device *gdev = cdev->gdev;void __user *ip = (void __user *)arg; - -guard (srcu)(&gdev->srcu); +struct gpio_chip *gc;/* We fail any subsequent ioctl():s when the chip is gone */ -if (!rcu_access_pointer (gdev->chip)) -return -ENODEV ; +revocable_try_access_or_return (&gdev->chip_rev, gc);/* Fill in the struct and pass to userspace */ switch (cmd) {
@@ -2448,12 +2437,9 @@ static void lineinfo_changed_func(struct work_struct *work)* Pin functions are in general much more static and while it's * not 100% bullet-proof, it's good enough for most cases. */ - scoped_guard(srcu, &ctx->gdev->srcu) { - gc =srcu_dereference (ctx->gdev->chip, &ctx->gdev->srcu); -if (gc && - !pinctrl_gpio_can_use_line (gc, ctx->chg.info.offset)) + revocable_try_access_or_skip_scoped(&ctx->gdev->chip_rev, gc) +if (!pinctrl_gpio_can_use_line (gc, ctx->chg.info.offset)) ctx->chg.info.flags |=GPIO_V2_LINE_FLAG_USED ; - } } ret = kfifo_in_spinlocked(&ctx->cdev->events, &ctx->chg, 1,
@@ -2534,10 +2520,10 @@ static __poll_t lineinfo_watch_poll(struct file *file, {struct gpio_chardev_data *cdev = file->private_data;__poll_t events =0 ; +struct gpio_chip *gc; -guard (srcu)(&cdev->gdev->srcu); - -if (!rcu_access_pointer (cdev->gdev->chip)) +revocable_try_access_with (&cdev->gdev->chip_rev, gc); +if (!gc)return EPOLLHUP |EPOLLERR ;poll_wait (file, &cdev->wait, pollt);
@@ -2557,11 +2543,9 @@ static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,ssize_t bytes_read =0 ;int ret;size_t event_size; +struct gpio_chip *gc; -guard (srcu)(&cdev->gdev->srcu); - -if (!rcu_access_pointer (cdev->gdev->chip)) -return -ENODEV ; +revocable_try_access_or_return (&cdev->gdev->chip_rev, gc);#ifndef CONFIG_GPIO_CDEV_V1 event_size =sizeof (struct gpio_v2_line_info_changed );
Signature · 1 line
2.51.0
Tzung-Bi Shih
maintainerPATCH v10 5/9 gpio: Remove gpio_chip_guard by using revocable
In reply to Tzung-Bi ShihStruct gpio_device now provides a revocable provider to the underlying struct gpio_chip. Leverage revocable for accessing the struct gpio_chip instead of using gpio_chip_guard.
---
v10:
- No changes.
v9: https://lore.kernel.org/all/[email protected]
- New to the series.
- Rename "chip_rp" -> "chip_rev".
v4 - v8:
- Doesn't exist.
v3: https://lore.kernel.org/all/[email protected]
- Change revocable API usages accordingly.
v2: https://lore.kernel.org/all/[email protected]
- Separate from v1 for including gpio_chip_guard only.
v1: https://lore.kernel.org/all/[email protected]
--- drivers/gpio/gpiolib-cdev.c | 9 +- drivers/gpio/gpiolib-sysfs.c | 31 +++---- drivers/gpio/gpiolib.c | 164 ++++++++++++++++------------------- drivers/gpio/gpiolib.h | 21 ----- 4 files changed, 91 insertions(+), 134 deletions(-)
drivers/gpio/gpiolib-cdev.c +4 -5
@@ -2156,10 +2156,9 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc,u32 debounce_period_us;unsigned long dflags;const char *label; +struct gpio_chip *gc; -CLASS (gpio_chip_guard, guard)(desc); -if (!guard.gc) -return ; +revocable_try_access_or_return_void (&desc->gdev->chip_rev, gc);memset (info,0 ,sizeof (*info)); info->offset =gpiod_hwgpio (desc);
@@ -2192,10 +2191,10 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc,test_bit (GPIOD_FLAG_IS_HOGGED , &dflags) ||test_bit (GPIOD_FLAG_EXPORT , &dflags) ||test_bit (GPIOD_FLAG_SYSFS , &dflags) || - !gpiochip_line_is_valid(guard.gc, info->offset)) { + !gpiochip_line_is_valid(gc, info->offset)) { info->flags |= GPIO_V2_LINE_FLAG_USED; } else if (!atomic) { -if (!pinctrl_gpio_can_use_line (guard.gc, info->offset)) +if (!pinctrl_gpio_can_use_line (gc, info->offset)) info->flags |=GPIO_V2_LINE_FLAG_USED ; }
drivers/gpio/gpiolib-sysfs.c +14 -17
@@ -10,6 +10,7 @@#include <linux/list.h> #include <linux/mutex.h> #include <linux/printk.h> +#include <linux/revocable.h> #include <linux/slab.h> #include <linux/string.h> #include <linux/srcu.h>
@@ -215,10 +216,9 @@ static int gpio_sysfs_request_irq(struct gpiod_data *data, unsigned char flags)struct gpio_desc *desc = data->desc;unsigned long irq_flags;int ret; +struct gpio_chip *gc; -CLASS (gpio_chip_guard, guard)(desc); -if (!guard.gc) -return -ENODEV ; +revocable_try_access_or_return (&desc->gdev->chip_rev, gc); data->irq =gpiod_to_irq (desc);if (data->irq <0 )
@@ -244,7 +244,7 @@ static int gpio_sysfs_request_irq(struct gpiod_data *data, unsigned char flags)* Remove this redundant call (along with the corresponding unlock) * when those drivers have been fixed. */ - ret =gpiochip_lock_as_irq (guard.gc,gpiod_hwgpio (desc)); + ret =gpiochip_lock_as_irq (gc,gpiod_hwgpio (desc));if (ret <0 ) goto err_clr_bits;
@@ -258,7 +258,7 @@ static int gpio_sysfs_request_irq(struct gpiod_data *data, unsigned char flags)return 0 ; err_unlock: -gpiochip_unlock_as_irq (guard.gc,gpiod_hwgpio (desc)); +gpiochip_unlock_as_irq (gc,gpiod_hwgpio (desc)); err_clr_bits:clear_bit (GPIOD_FLAG_EDGE_RISING , &desc->flags);clear_bit (GPIOD_FLAG_EDGE_FALLING , &desc->flags);
@@ -273,14 +273,13 @@ static int gpio_sysfs_request_irq(struct gpiod_data *data, unsigned char flags)static void gpio_sysfs_free_irq (struct gpiod_data *data) {struct gpio_desc *desc = data->desc; +struct gpio_chip *gc; -CLASS (gpio_chip_guard, guard)(desc); -if (!guard.gc) -return ; +revocable_try_access_or_return_void (&desc->gdev->chip_rev, gc); data->irq_flags =0 ;free_irq (data->irq, data); -gpiochip_unlock_as_irq (guard.gc,gpiod_hwgpio (desc)); +gpiochip_unlock_as_irq (gc,gpiod_hwgpio (desc));clear_bit (GPIOD_FLAG_EDGE_RISING , &desc->flags);clear_bit (GPIOD_FLAG_EDGE_FALLING , &desc->flags); }
@@ -473,13 +472,12 @@ static DEVICE_ATTR_RO(ngpio);static int export_gpio_desc (struct gpio_desc *desc) {int offset, ret; +struct gpio_chip *gc; -CLASS (gpio_chip_guard, guard)(desc); -if (!guard.gc) -return -ENODEV ; +revocable_try_access_or_return (&desc->gdev->chip_rev, gc); offset =gpiod_hwgpio (desc); -if (!gpiochip_line_is_valid (guard.gc, offset)) { +if (!gpiochip_line_is_valid (gc, offset)) {pr_debug_ratelimited ("%s: GPIO %d masked\n" , __func__,gpiod_hwgpio (desc));return -EINVAL ;
@@ -732,6 +730,7 @@ int gpiod_export(struct gpio_desc *desc, bool direction_may_change)struct gpio_device *gdev;struct attribute **attrs;int status; +struct gpio_chip *gc;/* can't export until sysfs is available ... */ if (!class_is_registered (&gpio_class)) {
@@ -744,9 +743,7 @@ int gpiod_export(struct gpio_desc *desc, bool direction_may_change)return -EINVAL ; } -CLASS (gpio_chip_guard, guard)(desc); -if (!guard.gc) -return -ENODEV ; +revocable_try_access_or_return (&desc->gdev->chip_rev, gc);if (test_and_set_bit (GPIOD_FLAG_EXPORT , &desc->flags))return -EPERM ;
@@ -769,7 +766,7 @@ int gpiod_export(struct gpio_desc *desc, bool direction_may_change) desc_data->desc = desc;mutex_init (&desc_data->mutex); -if (guard.gc->direction_input && guard.gc->direction_output) +if (gc->direction_input && gc->direction_output) desc_data->direction_can_change = direction_may_change;else desc_data->direction_can_change = false;
drivers/gpio/gpiolib.c +73 -91
408 lines not on this page · raw message
drivers/gpio/gpiolib.h +0 -21
@@ -228,27 +228,6 @@ struct gpio_desc {#define gpiod_not_found (desc) (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT) -struct gpio_chip_guard { -struct gpio_device *gdev; -struct gpio_chip *gc; -int idx; -}; - -DEFINE_CLASS (gpio_chip_guard, - struct gpio_chip_guard, -srcu_read_unlock (&_T.gdev->srcu, _T.idx), - ({ -struct gpio_chip_guard _guard; - - _guard.gdev = desc->gdev; - _guard.idx =srcu_read_lock (&_guard.gdev->srcu); - _guard.gc =srcu_dereference (_guard.gdev->chip, - &_guard.gdev->srcu); - - _guard; - }), - struct gpio_desc *desc) -int gpiod_request (struct gpio_desc *desc,const char *label);int gpiod_request_commit (struct gpio_desc *desc,const char *label);void gpiod_free (struct gpio_desc *desc);
Signature · 1 line
2.51.0
Tzung-Bi Shih
maintainerPATCH v10 6/9 gpio: Leverage revocable for accessing struct gpio_chip
In reply to Tzung-Bi ShihStruct gpio_device now provides a revocable provider to the underlying struct gpio_chip. Leverage revocable for accessing the struct gpio_chip.
---
v10:
- No changes.
v9: https://lore.kernel.org/all/[email protected]
- New to the series.
- Rename "chip_rp" -> "chip_rev".
v4 - v8:
- Doesn't exist.
v3: https://lore.kernel.org/all/[email protected]
- Change revocable API usages accordingly.
v2: https://lore.kernel.org/all/[email protected]
- Separate from v1(a) for excluding gpio_chip_guard and combine v1(b).
v1(a):
- https://lore.kernel.org/all/[email protected]
v1(b):
- https://lore.kernel.org/all/[email protected]
--- drivers/gpio/gpiolib.c | 60 +++++++++++++----------------------------- 1 file changed, 18 insertions(+), 42 deletions(-)
drivers/gpio/gpiolib.c +18 -42
@@ -335,7 +335,10 @@ EXPORT_SYMBOL(gpio_device_get_label);*/ struct gpio_chip *gpio_device_get_chip (struct gpio_device *gdev) { -return rcu_dereference_check (gdev->chip,1 ); +struct gpio_chip *gc; + +revocable_try_access_with (&gdev->chip_rev, gc); +return gc; }EXPORT_SYMBOL_GPL (gpio_device_get_chip);
@@ -561,9 +564,7 @@ static struct gpio_desc *gpio_name_to_desc(const char * const name)list_for_each_entry_srcu (gdev, &gpio_devices, list,srcu_read_lock_held (&gpio_devices_srcu)) { -guard (srcu)(&gdev->srcu); - - gc =srcu_dereference (gdev->chip, &gdev->srcu); +revocable_try_access_with (&gdev->chip_rev, gc);if (!gc)continue ;
@@ -1050,9 +1051,7 @@ static void gpiochip_setup_devs(void)list_for_each_entry_srcu (gdev, &gpio_devices, list,srcu_read_lock_held (&gpio_devices_srcu)) { -guard (srcu)(&gdev->srcu); - - gc =srcu_dereference (gdev->chip, &gdev->srcu); +revocable_try_access_with (&gdev->chip_rev, gc);if (!gc) {dev_err (&gdev->dev,"Underlying GPIO chip is gone\n" );continue ;
@@ -1457,11 +1456,11 @@ struct gpio_device *gpio_device_find(const void *data,if (!device_is_registered (&gdev->dev))continue ; -guard (srcu)(&gdev->srcu); - - gc =srcu_dereference (gdev->chip, &gdev->srcu); +revocable_try_access_with (&gdev->chip_rev, gc); +if (!gc) +continue ; -if (gc &&match (gc, data)) +if (match (gc, data))return gpio_device_get (gdev); }
@@ -3404,18 +3403,10 @@ static int gpio_chip_get_value(struct gpio_chip *gc, const struct gpio_desc *desstatic int gpiod_get_raw_value_commit (const struct gpio_desc *desc) { -struct gpio_device *gdev;struct gpio_chip *gc;int value; -/* FIXME Unable to use gpio_chip_guard due to const desc. */ - gdev = desc->gdev; - -guard (srcu)(&gdev->srcu); - - gc =srcu_dereference (gdev->chip, &gdev->srcu); -if (!gc) -return -ENODEV ; +revocable_try_access_or_return (&desc->gdev->chip_rev, gc); value =gpio_chip_get_value (gc, desc); value = value <0 ? value : !!value;
@@ -3454,9 +3445,10 @@ static int gpio_chip_get_multiple(struct gpio_chip *gc,/* The 'other' chip must be protected with its GPIO device's SRCU. */ static bool gpio_device_chip_cmp (struct gpio_device *gdev,struct gpio_chip *gc) { -guard (srcu)(&gdev->srcu); +struct gpio_chip *chip; -return gc ==srcu_dereference (gdev->chip, &gdev->srcu); +revocable_try_access_with (&gdev->chip_rev, chip); +return chip ? chip == gc : false; } int gpiod_get_array_value_complex(bool raw, bool can_sleep,
@@ -3479,11 +3471,7 @@ int gpiod_get_array_value_complex(bool raw, bool can_sleep,if (!can_sleep)WARN_ON (array_info->gdev->can_sleep); -guard (srcu)(&array_info->gdev->srcu); - gc =srcu_dereference (array_info->gdev->chip, - &array_info->gdev->srcu); -if (!gc) -return -ENODEV ; +revocable_try_access_or_return (&array_info->gdev->chip_rev, gc); ret =gpio_chip_get_multiple (gc, array_info->get_mask, value_bitmap);
@@ -3820,11 +3808,7 @@ int gpiod_set_array_value_complex(bool raw, bool can_sleep,return -EPERM ; } -guard (srcu)(&array_info->gdev->srcu); - gc =srcu_dereference (array_info->gdev->chip, - &array_info->gdev->srcu); -if (!gc) -return -ENODEV ; +revocable_try_access_or_return (&array_info->gdev->chip_rev, gc);if (!raw && !bitmap_empty (array_info->invert_mask, array_size)) bitmap_xor(value_bitmap, value_bitmap,
@@ -4119,7 +4103,6 @@ EXPORT_SYMBOL_GPL(gpiod_is_shared);*/ int gpiod_to_irq (const struct gpio_desc *desc) { -struct gpio_device *gdev;struct gpio_chip *gc;int offset;int ret;
@@ -4128,12 +4111,7 @@ int gpiod_to_irq(const struct gpio_desc *desc)if (ret <=0 )return -EINVAL ; - gdev = desc->gdev; -/* FIXME Cannot use gpio_chip_guard due to const desc. */ -guard (srcu)(&gdev->srcu); - gc =srcu_dereference (gdev->chip, &gdev->srcu); -if (!gc) -return -ENODEV ; +revocable_try_access_or_return (&desc->gdev->chip_rev, gc); offset =gpiod_hwgpio (desc);if (gc->to_irq) {
@@ -5472,9 +5450,7 @@ static int gpiolib_seq_show(struct seq_file *s, void *v)if (priv->newline)seq_putc (s,'\n' ); -guard (srcu)(&gdev->srcu); - - gc =srcu_dereference (gdev->chip, &gdev->srcu); +revocable_try_access_with (&gdev->chip_rev, gc);if (!gc) {seq_printf (s,"%s: (dangling chip)\n" ,dev_name (&gdev->dev));return 0 ;
Signature · 1 line
2.51.0
Tzung-Bi Shih
maintainerPATCH v10 7/9 gpio: Remove unused `chip` and `srcu` in struct gpio_device
In reply to Tzung-Bi Shihchip and srcu in struct gpio_device are unused as their usages are replaced to use revocable. Remove them.
---
v10:
- No changes.
v9: https://lore.kernel.org/all/[email protected]
- New to the series.
- No changes.
v4 - v8:
- Doesn't exist.
v3: https://lore.kernel.org/all/[email protected]
- No changes.
v2: https://lore.kernel.org/all/[email protected]
- No changes.
v1: https://lore.kernel.org/all/[email protected]
--- drivers/gpio/gpiolib.c | 26 +------------------------- drivers/gpio/gpiolib.h | 4 ---- 2 files changed, 1 insertion(+), 29 deletions(-)
drivers/gpio/gpiolib.c +1 -25
@@ -428,8 +428,6 @@ static int gpiochip_get_direction(struct gpio_chip *gc, unsigned int offset) {int ret; -lockdep_assert_held (&gc->gpiodev->srcu); -if (WARN_ON (!gc->get_direction))return -EOPNOTSUPP ;
@@ -879,7 +877,6 @@ static void gpiodev_release(struct device *dev)ida_free (&gpio_ida, gdev->id);kfree_const (gdev->label);kfree (gdev->descs); -cleanup_srcu_struct (&gdev->srcu);kfree (gdev); }
@@ -1154,14 +1151,9 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, goto err_free_gdev; gdev->id = ret; - ret =init_srcu_struct (&gdev->srcu); -if (ret) - goto err_free_ida; -rcu_assign_pointer (gdev->chip, gc); - ret =init_srcu_struct (&gdev->desc_srcu);if (ret) - goto err_cleanup_gdev_srcu; + goto err_free_ida; ret =dev_set_name (&gdev->dev,GPIOCHIP_NAME "%d" , gdev->id);if (ret)
@@ -1355,8 +1347,6 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, err_cleanup_desc_srcu:cleanup_srcu_struct (&gdev->desc_srcu); -err_cleanup_gdev_srcu: -cleanup_srcu_struct (&gdev->srcu); err_free_ida:ida_free (&gpio_ida, gdev->id); err_free_gdev:
@@ -1393,8 +1383,6 @@ void gpiochip_remove(struct gpio_chip *gc)synchronize_srcu (&gpio_devices_srcu);/* Numb the device, cancelling all outstanding operations */ -rcu_assign_pointer (gdev->chip,NULL ); -synchronize_srcu (&gdev->srcu);revocable_revoke (&gdev->chip_rev);gpio_device_teardown_shared (gdev);gpiochip_irqchip_remove (gc);
@@ -2905,8 +2893,6 @@ static int gpiochip_direction_input(struct gpio_chip *gc, unsigned int offset) {int ret; -lockdep_assert_held (&gc->gpiodev->srcu); -if (WARN_ON (!gc->direction_input))return -EOPNOTSUPP ;
@@ -2922,8 +2908,6 @@ static int gpiochip_direction_output(struct gpio_chip *gc, unsigned int offset, {int ret; -lockdep_assert_held (&gc->gpiodev->srcu); -if (WARN_ON (!gc->direction_output))return -EOPNOTSUPP ;
@@ -3011,8 +2995,6 @@ static int gpiochip_set(struct gpio_chip *gc, unsigned int offset, int value) {int ret; -lockdep_assert_held (&gc->gpiodev->srcu); -if (WARN_ON (unlikely (!gc->set)))return -EOPNOTSUPP ;
@@ -3360,8 +3342,6 @@ static int gpiochip_get(struct gpio_chip *gc, unsigned int offset) {int ret; -lockdep_assert_held (&gc->gpiodev->srcu); -/* Make sure this is called after checking for gc->get(). */ ret = gc->get (gc, offset);if (ret >1 ) {
@@ -3417,8 +3397,6 @@ static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)static int gpio_chip_get_multiple (struct gpio_chip *gc,unsigned long *mask,unsigned long *bits) { -lockdep_assert_held (&gc->gpiodev->srcu); -if (gc->get_multiple) {int ret;
@@ -3762,8 +3740,6 @@ static int gpiochip_set_multiple(struct gpio_chip *gc,unsigned int i;int ret; -lockdep_assert_held (&gc->gpiodev->srcu); -if (gc->set_multiple) { ret = gc->set_multiple (gc, mask, bits);if (ret >0 )
drivers/gpio/gpiolib.h +0 -4
@@ -32,7 +32,6 @@ struct fwnode_handle;* @chrdev: character device for the GPIO device * @id: numerical ID number for the GPIO chip * @owner: helps prevent removal of modules exporting active GPIOs -* @chip: pointer to the corresponding gpiochip, holding static * data for this device * @descs: array of ngpio descriptors. * @valid_mask: If not %NULL, holds bitmask of GPIOs which are valid to be
@@ -55,7 +54,6 @@ struct fwnode_handle;* process context * @device_notifier: used to notify character device wait queues about the GPIO * device being unregistered -* @srcu: protects the pointer to the underlying GPIO chip * @chip_rev: revocable provider handle for the corresponding struct gpio_chip. * @pin_ranges: range of pins served by the GPIO driver *
@@ -69,7 +67,6 @@ struct gpio_device {struct cdev chrdev;int id;struct module *owner; -struct gpio_chip __rcu *chip;struct gpio_desc *descs;unsigned long *valid_mask;struct srcu_struct desc_srcu;
@@ -83,7 +80,6 @@ struct gpio_device {rwlock_t line_state_lock;struct workqueue_struct *line_state_wq;struct blocking_notifier_head device_notifier; -struct srcu_struct srcu;struct revocable chip_rev;#ifdef CONFIG_PINCTRL
Signature · 1 line
2.51.0
Tzung-Bi Shih
maintainerPATCH v10 8/9 platform/chrome: Protect cros_ec_device lifecycle with revocable
In reply to Tzung-Bi ShihThe cros_ec_device can be unregistered when the underlying device is removed. Other kernel drivers that interact with the EC may hold a pointer to the cros_ec_device, creating a risk of a use-after-free error if the EC device is removed while still being referenced.
To prevent this, leverage the revocable and convert the underlying device drivers to resource providers of cros_ec_device.
---
v10:
- No changes.
v9: https://lore.kernel.org/all/[email protected]
- New to the series.
- Change revocable API usages accordingly.
- Rename "revocable_provider" -> "its_rev".
v5 - v8:
- Doesn't exist.
v4: https://lore.kernel.org/all/[email protected]
- No changes.
v3: https://lore.kernel.org/all/[email protected]
- Initialize the revocable provider in cros_ec_device_alloc() instead of spreading in protocol device drivers.
v2: https://lore.kernel.org/all/[email protected]
- Rename "ref_proxy" -> "revocable".
v1: https://lore.kernel.org/all/[email protected]
--- drivers/platform/chrome/cros_ec.c | 11 +++++++++++ include/linux/platform_data/cros_ec_proto.h | 3 +++ 2 files changed, 14 insertions(+)
drivers/platform/chrome/cros_ec.c +11 -0
@@ -16,6 +16,7 @@#include <linux/platform_device.h> #include <linux/platform_data/cros_ec_commands.h> #include <linux/platform_data/cros_ec_proto.h> +#include <linux/revocable.h> #include <linux/slab.h> #include <linux/suspend.h>
@@ -37,6 +38,7 @@ static void cros_ec_device_free(void *data)mutex_destroy (&ec_dev->lock);lockdep_unregister_key (&ec_dev->lockdep_key); +revocable_revoke (ec_dev->its_rev); }struct cros_ec_device *cros_ec_device_alloc (struct device *dev)
@@ -47,6 +49,15 @@ struct cros_ec_device *cros_ec_device_alloc(struct device *dev)if (!ec_dev)return NULL ; + ec_dev->its_rev =revocable_alloc (ec_dev); +if (!ec_dev->its_rev) +return NULL ; +/* +* Drop the extra reference for the caller as the caller is the +* resource provider. +*/ +revocable_put (ec_dev->its_rev); + ec_dev->din_size =sizeof (struct ec_host_response ) +sizeof (struct ec_response_get_protocol_info ) +EC_MAX_RESPONSE_OVERHEAD ;
include/linux/platform_data/cros_ec_proto.h +3 -0
@@ -12,6 +12,7 @@#include <linux/lockdep_types.h> #include <linux/mutex.h> #include <linux/notifier.h> +#include <linux/revocable.h> #include <linux/platform_data/cros_ec_commands.h>
@@ -165,6 +166,7 @@ struct cros_ec_command {* @pd: The platform_device used by the mfd driver to interface with the * PD behind an EC. * @panic_notifier: EC panic notifier. +* @its_rev: The revocable_provider to this device. */ struct cros_ec_device {/* These are used by other drivers that want to talk to the EC */
@@ -211,6 +213,7 @@ struct cros_ec_device {struct platform_device *pd;struct blocking_notifier_head panic_notifier; +struct revocable *its_rev; };/**
Signature · 1 line
2.51.0
Tzung-Bi Shih
maintainerPATCH v10 9/9 platform/chrome: cros_ec_chardev: Consume cros_ec_device via revocable
In reply to Tzung-Bi ShihThe cros_ec_chardev driver provides a character device interface to the ChromeOS EC. A file handle to this device can remain open in userspace even if the underlying EC device is removed.
This creates a classic use-after-free vulnerability. Any file operation (ioctl, release, etc.) on the open handle after the EC device has gone would access a stale pointer, leading to a system crash.
To prevent this, leverage the revocable and convert cros_ec_chardev to a resource consumer of cros_ec_device.
---
v10:
- No changes.
v9: https://lore.kernel.org/all/[email protected]
- New to the series.
- Change revocable API usages accordingly.
v4 - v8:
- Doesn't exist.
v3: https://lore.kernel.org/all/[email protected]
- Use specific labels for different cleanup in cros_ec_chardev_open().
v2: https://lore.kernel.org/all/[email protected]
- Rename "ref_proxy" -> "revocable".
- Fix a sparse warning by removing the redundant __rcu annotation.
v1: https://lore.kernel.org/all/[email protected]
--- drivers/platform/chrome/cros_ec_chardev.c | 80 +++++++++++++++++------ 1 file changed, 61 insertions(+), 19 deletions(-)
drivers/platform/chrome/cros_ec_chardev.c +61 -19
@@ -22,6 +22,7 @@#include <linux/platform_data/cros_ec_proto.h> #include <linux/platform_device.h> #include <linux/poll.h> +#include <linux/revocable.h> #include <linux/slab.h> #include <linux/types.h> #include <linux/uaccess.h>
@@ -32,7 +33,7 @@#define CROS_MAX_EVENT_LEN PAGE_SIZEstruct chardev_priv { -struct cros_ec_device *ec_dev; +struct revocable *rev;struct notifier_block notifier;wait_queue_head_t wait_event;unsigned long event_mask;
@@ -55,6 +56,7 @@ static int ec_get_version(struct chardev_priv *priv, char *str, int maxlen) };struct ec_response_get_version *resp;struct cros_ec_command *msg; +struct cros_ec_device *ec_dev;int ret; msg =kzalloc (sizeof (*msg) +sizeof (*resp),GFP_KERNEL );
@@ -64,12 +66,19 @@ static int ec_get_version(struct chardev_priv *priv, char *str, int maxlen) msg->command =EC_CMD_GET_VERSION + priv->cmd_offset; msg->insize =sizeof (*resp); - ret =cros_ec_cmd_xfer_status (priv->ec_dev, msg); -if (ret <0 ) { -snprintf (str, maxlen, -"Unknown EC version, returned error: %d\n" , - msg->result); - goto exit; + revocable_try_access_with_scoped(priv->rev, ec_dev) { +if (!ec_dev) { + ret = -ENODEV ; + goto exit; + } + + ret =cros_ec_cmd_xfer_status (ec_dev, msg); +if (ret <0 ) { +snprintf (str, maxlen, +"Unknown EC version, returned error: %d\n" , + msg->result); + goto exit; + } } resp = (struct ec_response_get_version *)msg->data;
@@ -92,10 +101,15 @@ static int cros_ec_chardev_mkbp_event(struct notifier_block *nb, { struct chardev_priv *priv = container_of(nb, struct chardev_priv, notifier); -struct cros_ec_device *ec_dev = priv->ec_dev; +struct cros_ec_device *ec_dev;struct ec_event *event; -unsigned long event_bit =1 << ec_dev->event_data.event_type; -int total_size =sizeof (*event) + ec_dev->event_size; +unsigned long event_bit; +int total_size; + +revocable_try_access_or_return_err (priv->rev, ec_dev,NOTIFY_DONE ); + + event_bit =1 << ec_dev->event_data.event_type; + total_size =sizeof (*event) + ec_dev->event_size;if (!(event_bit & priv->event_mask) || (priv->event_len + total_size) >CROS_MAX_EVENT_LEN )
@@ -166,7 +180,8 @@ static int cros_ec_chardev_open(struct inode *inode, struct file *filp)if (!priv)return -ENOMEM ; - priv->ec_dev = ec_dev; + priv->rev = ec_dev->its_rev; +revocable_get (priv->rev); priv->cmd_offset = ec->cmd_offset; filp->private_data = priv;INIT_LIST_HEAD (&priv->events);
@@ -178,6 +193,7 @@ static int cros_ec_chardev_open(struct inode *inode, struct file *filp) &priv->notifier);if (ret) {dev_err (ec_dev->dev,"failed to register event notifier\n" ); +revocable_put (priv->rev);kfree (priv); }
@@ -251,11 +267,13 @@ static ssize_t cros_ec_chardev_read(struct file *filp, char __user *buffer,static int cros_ec_chardev_release (struct inode *inode,struct file *filp) {struct chardev_priv *priv = filp->private_data; -struct cros_ec_device *ec_dev = priv->ec_dev; +struct cros_ec_device *ec_dev;struct ec_event *event, *e; -blocking_notifier_chain_unregister (&ec_dev->event_notifier, - &priv->notifier); + revocable_try_access_or_skip_scoped(priv->rev, ec_dev) +blocking_notifier_chain_unregister (&ec_dev->event_notifier, + &priv->notifier); +revocable_put (priv->rev);list_for_each_entry_safe (event, e, &priv->events, node) {list_del (&event->node);
@@ -273,6 +291,7 @@ static long cros_ec_chardev_ioctl_xcmd(struct chardev_priv *priv, void __user *a {struct cros_ec_command *s_cmd;struct cros_ec_command u_cmd; +struct cros_ec_device *ec_dev;long ret;if (copy_from_user (&u_cmd, arg,sizeof (u_cmd)))
@@ -299,10 +318,17 @@ static long cros_ec_chardev_ioctl_xcmd(struct chardev_priv *priv, void __user *a } s_cmd->command += priv->cmd_offset; - ret =cros_ec_cmd_xfer (priv->ec_dev, s_cmd); -/* Only copy data to userland if data was received. */ -if (ret <0 ) - goto exit; + revocable_try_access_with_scoped(priv->rev, ec_dev) { +if (!ec_dev) { + ret = -ENODEV ; + goto exit; + } + + ret =cros_ec_cmd_xfer (ec_dev, s_cmd); +/* Only copy data to userland if data was received. */ +if (ret <0 ) + goto exit; + }if (copy_to_user (arg, s_cmd,sizeof (*s_cmd) + s_cmd->insize)) ret = -EFAULT ;
@@ -313,10 +339,12 @@ static long cros_ec_chardev_ioctl_xcmd(struct chardev_priv *priv, void __user *astatic long cros_ec_chardev_ioctl_readmem (struct chardev_priv *priv,void __user *arg) { -struct cros_ec_device *ec_dev = priv->ec_dev; +struct cros_ec_device *ec_dev;struct cros_ec_readmem s_mem = { };long num; +revocable_try_access_or_return (priv->rev, ec_dev); +/* Not every platform supports direct reads */ if (!ec_dev->cmd_readmem)return -ENOTTY ;
@@ -370,11 +398,25 @@ static const struct file_operations chardev_fops = {#endif }; +static void cros_ec_chardev_free (void *data) +{ +struct revocable *rev = data; + +revocable_put (rev); +} +static int cros_ec_chardev_probe (struct platform_device *pdev) {struct cros_ec_dev *ec =dev_get_drvdata (pdev->dev.parent);struct cros_ec_platform *ec_platform =dev_get_platdata (ec->dev); +struct revocable *rev = ec->ec_dev->its_rev;struct miscdevice *misc; +int ret; + +revocable_get (rev); + ret =devm_add_action_or_reset (&pdev->dev, cros_ec_chardev_free, rev); +if (ret) +return ret;/* Create a char device: we want to create it anew */ misc =devm_kzalloc (&pdev->dev,sizeof (*misc),GFP_KERNEL );
Signature · 1 line
2.51.0
Re: [PATCH v10 8/9] platform/chrome: Protect cros_ec_device lifecycle with revocable
In reply to Tzung-Bi ShihQuoted 7 lines · Tzung-Bi Shih
struct
cros_ec_device*cros_ec_device_alloc(struct device *dev)drivers/platform/chrome/cros_ec.c +2
-0@@ -47,6 +49,15 @@ struct cros_ec_device *cros_ec_device_alloc(struct device *dev)if (!ec_dev)return NULL ; + ec_dev->its_rev =revocable_alloc (ec_dev); +if (!ec_dev->its_rev)
drivers/platform/chrome/cros_ec.c struct cros_ec_device *cros_ec_device_alloc(struct device *dev) · line 63
Quoted 10 lines
drivers/platform/chrome/cros_ec.c +7
-0+return NULL ; +/* +* Drop the extra reference for the caller as the caller is the +* resource provider. +*/ +revocable_put (ec_dev->its_rev); + ec_dev->din_size =sizeof (struct ec_host_response ) +sizeof (struct ec_response_get_protocol_info ) +EC_MAX_RESPONSE_OVERHEAD ;
FWIW I am still very much against seeing any revokable concept used *between two drivers*. That will turn the kernel's lifetime model into spaghetti code.
Your other series where you only have to change drivers/platform/chrome/cros_ec_chardev.c just confirms how wrong this approach is.
Given you say this is such a bug I think you really should be sending a series that is patches 5 through 7 from the other series and a simple rwsem instead of misc_deregister_sync() to deal with this bug ASAP. No need to complicate a simple bug fix in a driver with all these core changes.
Once the bug is fixed you can continue to try to propose more general solutions.
Jason
Re: [PATCH v10 2/9] revocable: Add KUnit test cases
In reply to Tzung-Bi ShihQuoted 13 lines · Tzung-Bi Shih
Add 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.Signed-off-by Tzung-Bi Shih <[email protected]>---
...
drivers/base/revocable_test.c
Quoted 17 lines
drivers/base/Kconfig +5
-0+ +config REVOCABLE_KUNIT_TEST + tristate "KUnit tests for revocable" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTSdrivers/base/Makefile +3
-0@@ -35,3 +35,6 @@ ccflags-$(CONFIG_DEBUG_DRIVER) := -DDEBUG # define_trace.h needs to know how to find our header CFLAGS_trace.o := -I$(src) obj-$(CONFIG_TRACING) += trace.o + +# KUnit test cases +obj-$(CONFIG_REVOCABLE_KUNIT_TEST) += revocable_test.odrivers/base/revocable_test.c +0
-0
Please move this under drivers/base/tests/ where the rest of kunit modules live and name it revocable-test.c for consistency with the existing ones.
Quoted 15 lines
new file mode 100644
index 000000000000..bb776a747ff7drivers/base/revocable_test.c new file +10
-0@@ -0,0 +1,405 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright 2026 Google LLC +* +* KUnit tests for the revocable API. +* +* The test cases cover the following scenarios: +* +* - Basic: Verifies that a consumer can successfully access the resource. +*
drivers/base/revocable_test.c line 20
Quoted 10 lines
drivers/base/revocable_test.c new file +10
-0+* - 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. +*/ + +#include <kunit/test.h>
Add a newline here as do other kunit modules.
drivers/base/revocable_test.c line 26
Quoted 6 lines
drivers/base/revocable_test.c new file +6
-0+#include <linux/completion.h> +#include <linux/delay.h> +#include <linux/kthread.h> +#include <linux/refcount.h> +#include <linux/revocable.h> +
Otherwise looks good, with the above:
Thanks,
Bartosz
Re: [PATCH v10 1/9] revocable: Revocable resource management
In reply to Tzung-Bi ShihQuoted 6 lines · Tzung-Bi Shih
The "revocable" mechanism is a synchronization primitive designed to manage safe access to resources that can be asynchronously removed or invalidated. Its primary purpose is to prevent Use-After-Free (UAF) errors when interacting with resources whose lifetimes are not guaranteed to outlast their consumers.
...
Quoted 211 lines
include/linux/revocable.h new file +204
-1@@ -0,0 +1,204 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright 2026 Google LLC +*/ + +#ifndef __LINUX_REVOCABLE_H +#define __LINUX_REVOCABLE_H + +#include <linux/cleanup.h> +#include <linux/kref.h> +#include <linux/srcu.h> + +/** +* struct revocable - A handle for resource provider. +* @srcu: The SRCU to protect the resource. +* @res: The pointer of resource. It can point to anything. +* @kref: The refcount for this handle. +* @embedded: Indicate if the handle is embedded in another struct. +* +* Note: All members of this structure are intended to be opaque and should +* not be accessed directly by the users. +*/ +struct revocable { +struct srcu_struct srcu; +void __rcu *res; +struct kref kref; +bool embedded; +}; + +/** +* struct revocable_handle - A handle for resource consumer. +* @rev: The pointer of resource provider. +* @idx: The index for the SRCU critical section. +* +* Note: All members of this structure are intended to be opaque and should +* not be accessed directly by the users. +*/ +struct revocable_handle { +struct revocable *rev; +int idx; +}; + +struct revocable *revocable_alloc (void *res); +int revocable_init (struct revocable *rev,void *res); +void revocable_revoke (struct revocable *rev); +void revocable_get (struct revocable *rev); +void revocable_put (struct revocable *rev); + +void revocable_handle_init (struct revocable *rev,struct revocable_handle *rh); +void revocable_handle_deinit (struct revocable_handle *rh); +void *revocable_try_access (struct revocable_handle *rh) + __acquires(&rh->rev->srcu); +void revocable_withdraw_access(struct revocable_handle *rh) + __releases(&rh->rev->srcu); + +DEFINE_FREE(access_rev, struct revocable_handle *, { + revocable_withdraw_access(_T); +revocable_handle_deinit (_T); +}) + +#define _revocable_try_access_with (_rev, _rh, _res) \ + struct revocable_handle _rh; \ + struct revocable_handle *__UNIQUE_ID(name) __free(access_rev) = &_rh; \ + \ + revocable_handle_init(_rev, &_rh); \ + _res = revocable_try_access(&_rh) + +/** +* revocable_try_access_with() - A helper for accessing revocable resource +* @_rev: The pointer of resource provider. +* @_res: A pointer variable that will be assigned the resource. +* +* The macro simplifies the access-release cycle for consumers, ensuring that +* corresponding revocable_withdraw_access() and revocable_handle_deinit() are +* called, even in the case of an early exit. +* +* It creates a local variable in the current scope. @_res is populated with +* the result of revocable_try_access(). Callers **must** check if @_res is +* ``NULL`` before using it. The revocable_withdraw_access() function is +* automatically called when the scope is exited. +* +* Note: It shares the same issue with guard() in cleanup.h. No goto statements +* are allowed before the helper. Otherwise, the compiler fails with +* "jump bypasses initialization of variable with __attribute__((cleanup))". +*/ +#define revocable_try_access_with (_rev, _res) \ + _revocable_try_access_with(_rev, __UNIQUE_ID(name), _res) + +/** +* revocable_try_access_or_return_err() - Variant of revocable_try_access_with() +* @_rev: The pointer of resource provider. +* @_res: A pointer variable that will be assigned the resource. +* @_err: The error code to return if resource is revoked. +* +* Similar to revocable_try_access_with() but returns from the current function +* with @_err if the resource is revoked. Callers don't need to check @_res for +* ``NULL`` as this handles the revocation case by returning early. +*/ +#define revocable_try_access_or_return_err (_rev, _res, _err) \ + _revocable_try_access_with(_rev, __UNIQUE_ID(name), _res); \ + if (!_res) \ + return _err + +/** +* revocable_try_access_or_return() - Variant of revocable_try_access_with() +* @_rev: The pointer of resource provider. +* @_res: A pointer variable that will be assigned the resource. +* +* Similar to revocable_try_access_or_return_err() but returns -ENODEV if the +* resource is revoked. +*/ +#define revocable_try_access_or_return (_rev, _res) \ + revocable_try_access_or_return_err(_rev, _res, -ENODEV) + +/** +* revocable_try_access_or_return_void() - Variant of revocable_try_access_with() +* @_rev: The pointer of resource provider. +* @_res: A pointer variable that will be assigned the resource. +* +* Similar to revocable_try_access_or_return_err() but returns void if the +* resource is revoked. +*/ +#define revocable_try_access_or_return_void (_rev, _res) \ + revocable_try_access_or_return_err(_rev, _res, ) + +#define _revocable_try_access_with_scoped (_rev, _rh, _label, _res) \ + for (struct revocable_handle _rh, \ + *__UNIQUE_ID(name) __free(access_rev) = &_rh; \ + ({ revocable_handle_init(_rev, &_rh); \ + _res = revocable_try_access(&_rh); \ + true; }); \ + ({ goto _label; })) \ + if (0) { \ +_label: \ + break; \ + } else + +/** +* revocable_try_access_with_scoped() - Variant of revocable_try_access_with() +* @_rev: The pointer of resource provider. +* @_res: A pointer variable that will be assigned the resource. +* +* Similar to revocable_try_access_with() but with an explicit scope from a +* temporary ``for`` loop. +*/ +#define revocable_try_access_with_scoped (_rev, _res) \ + _revocable_try_access_with_scoped(_rev, __UNIQUE_ID(name), \ + __UNIQUE_ID(label), _res) + +/** +* revocable_try_access_or_return_err_scoped() - Variant of revocable_try_access_with_scoped() +* @_rev: The pointer of resource provider. +* @_res: A pointer variable that will be assigned the resource. +* @_err: The error code to return if resource is revoked. +* +* Similar to revocable_try_access_with_scoped() but returns from the current +* function with @_err if the resource is revoked. Callers don't need to check +* @_res for ``NULL`` as this handles the revocation case by returning early. +*/ +#define revocable_try_access_or_return_err_scoped (_rev, _res, _err) \ + _revocable_try_access_with_scoped(_rev, __UNIQUE_ID(name), \ + __UNIQUE_ID(label), _res) \ + if (!_res) { \ + return _err; \ + } else + +/** +* revocable_try_access_or_return_scoped() - Variant of revocable_try_access_with_scoped() +* @_rev: The pointer of resource provider. +* @_res: A pointer variable that will be assigned the resource. +* +* Similar to revocable_try_access_or_return_err_scoped() but returns -ENODEV +* if the resource is revoked. +*/ +#define revocable_try_access_or_return_scoped (_rev, _res) \ + revocable_try_access_or_return_err_scoped(_rev, _res, -ENODEV) + +/** +* revocable_try_access_or_return_void_scoped() - Variant of revocable_try_access_with_scoped() +* @_rev: The pointer of resource provider. +* @_res: A pointer variable that will be assigned the resource. +* +* Similar to revocable_try_access_or_return_err_scoped() but returns void +* if the resource is revoked. +*/ +#define revocable_try_access_or_return_void_scoped (_rev, _res) \ + revocable_try_access_or_return_err_scoped(_rev, _res, ) + +/** +* revocable_try_access_or_skip_scoped() - Variant of revocable_try_access_with_scoped() +* @_rev: The pointer of resource provider. +* @_res: A pointer variable that will be assigned the resource. +* +* Similar to revocable_try_access_with_scoped() but skips the following code +* block if the resource is revoked. +*/ +#define revocable_try_access_or_skip_scoped (_rev, _res) \ + _revocable_try_access_with_scoped(_rev, __UNIQUE_ID(name), \ + __UNIQUE_ID(label), _res) \ + if (!_res) { \ + break; \ + } else + +#endif /* __LINUX_REVOCABLE_H */ --
Quoted 3 lines
2.51.0
I have not looked into the implementation details all that much - that can always be ironed out later - but for the API part: I quite like it now. The resulting GPIO code looks cleaner and I think it's worth adding or v7.2.
Thanks for addressing the issues and perseverence.
Bart
Quoted 20 lines · Tzung-Bi Shih
This series introduces the "revocable" mechanism, a synchronization primitive designed to prevent Use-After-Free errors.
- Patch 1 introduces the revocable which is an implementation of ideas from the talk [1].
- Patch 2 adds KUnit test cases.
- Patches 3 to 7 transitions the UAF prevention logic within the GPIO core (gpiolib) to use the "revocable" mechanism.
The existing code aims to prevent UAF issues when the underlying GPIO chip is removed. They replace that custom logic with the generic "revocable" API, which is designed to handle such lifecycle dependencies. There should be no changes in behavior.
- Patches 8 to 9 uses "revocable" mechanism to fix an UAF in
cros_ec_chardevdriver. Alternatively, [2] is a series for fixing the same issue without using "revocable".
Quoted 7 lines
Since v9, there are two ways to manage the resource provider handle.
- Embedded allocation: patches 3 to 7 might be the potential user.
- Dynamic allocation: patches 8 to 9 might be the potential user.[1] https://lpc.events/event/17/contributions/1627/
[2] https://lore.kernel.org/all/[email protected]
All my tests pass with this series on top of v7.1-rc3.
Bartosz Golaszewski
maintainerRe: [PATCH v10 7/9] gpio: Remove unused `chip` and `srcu` in struct gpio_device
In reply to Tzung-Bi ShihQuoted 5 lines · Tzung-Bi Shih
chipandsrcuin structgpio_deviceare unused as their usages are replaced to use revocable. Remove them.Signed-off-by Tzung-Bi Shih <[email protected]>---
I'm thinking that all the GPIO patches could actually be squashed into one. Is there any technical reason for the split or is it just for easier review?
Bart
Bartosz Golaszewski
maintainerRe: [PATCH v10 8/9] platform/chrome: Protect cros_ec_device lifecycle with revocable
In reply to Jason GunthorpeQuoted 6 lines · Jason Gunthorpe
Quoted 5 lines · Tzung-Bi Shih
struct
cros_ec_device*cros_ec_device_alloc(struct device *dev)drivers/platform/chrome/cros_ec.c +0
-0@@ -47,6 +49,15 @@ struct cros_ec_device *cros_ec_device_alloc(struct device *dev)if (!ec_dev)return NULL ;
Quoted 17 lines
drivers/platform/chrome/cros_ec.c struct cros_ec_device *cros_ec_device_alloc(struct device *dev) · line 63
Quoted 12 lines
drivers/platform/chrome/cros_ec.c +9
-0+ ec_dev->its_rev =revocable_alloc (ec_dev); +if (!ec_dev->its_rev) +return NULL ; +/* +* Drop the extra reference for the caller as the caller is the +* resource provider. +*/ +revocable_put (ec_dev->its_rev); + ec_dev->din_size =sizeof (struct ec_host_response ) +sizeof (struct ec_response_get_protocol_info ) +EC_MAX_RESPONSE_OVERHEAD ;FWIW I am still very much against seeing any revokable concept used *between two drivers*. That will turn the kernel's lifetime model into spaghetti code.
I gave my R-b under the API definition and I'm fine with the GPIO changes. Just for the record: I don't have an opinion on using it in this driver and don't know if it's a good idea or not.
Bart
Tzung-Bi Shih
maintainerRe: [PATCH v10 1/9] revocable: Revocable resource management
In reply to Bartosz GolaszewskiQuoted 5 lines · Bartosz Golaszewski
I have not looked into the implementation details all that much - that can always be ironed out later - but for the API part: I quite like it now. The resulting GPIO code looks cleaner and I think it's worth adding or v7.2.
Thanks for addressing the issues and perseverence.
Thank you for taking the time to review the APIs. I'm glad you found the changes to be an improvement!
Tzung-Bi Shih
maintainerRe: [PATCH v10 2/9] revocable: Add KUnit test cases
In reply to Bartosz GolaszewskiQuoted 5 lines · Bartosz Golaszewski
drivers/base/revocable_test.c
Quoted 1 line · Tzung-Bi Shih
drivers/base/revocable_test.c +0
-0Please move this under
drivers/base/tests/where the rest of kunit modules live and name itrevocable-test.cfor consistency with the existing ones.
Ack, I overlooked the folder. Will move the test to drivers/base/test/ and rename it in the next version.
Quoted 3 lines
Quoted 1 line
+#include <
kunit/test.h>Add a newline here as do other kunit modules.
Ack, will fix it in the next version.
Tzung-Bi Shih
maintainerRe: [PATCH v10 7/9] gpio: Remove unused `chip` and `srcu` in struct gpio_device
In reply to Bartosz GolaszewskiQuoted 9 lines · Bartosz Golaszewski
Quoted 5 lines · Tzung-Bi Shih
chipandsrcuin structgpio_deviceare unused as their usages are replaced to use revocable. Remove them.Signed-off-by Tzung-Bi Shih <[email protected]>---
I'm thinking that all the GPIO patches could actually be squashed into one. Is there any technical reason for the split or is it just for easier review?
Correct, they are separated only for easier review. Would you prefer I squash the 5 patches into a single patch?
Re: [PATCH v10 8/9] platform/chrome: Protect cros_ec_device lifecycle with revocable
In reply to Jason GunthorpeQuoted 6 lines · Jason Gunthorpe
Quoted 5 lines · Tzung-Bi Shih
struct
cros_ec_device*cros_ec_device_alloc(struct device *dev)drivers/platform/chrome/cros_ec.c +0
-0@@ -47,6 +49,15 @@ struct cros_ec_device *cros_ec_device_alloc(struct device *dev)if (!ec_dev)return NULL ;
Quoted 16 lines
drivers/platform/chrome/cros_ec.c struct cros_ec_device *cros_ec_device_alloc(struct device *dev) · line 63
Quoted 12 lines
drivers/platform/chrome/cros_ec.c +9
-0+ ec_dev->its_rev =revocable_alloc (ec_dev); +if (!ec_dev->its_rev) +return NULL ; +/* +* Drop the extra reference for the caller as the caller is the +* resource provider. +*/ +revocable_put (ec_dev->its_rev); + ec_dev->din_size =sizeof (struct ec_host_response ) +sizeof (struct ec_response_get_protocol_info ) +EC_MAX_RESPONSE_OVERHEAD ;FWIW I am still very much against seeing any revokable concept used *between two drivers*. That will turn the kernel's lifetime model into spaghetti code.
I agree, I really think it will become a huge mess that we will massively regret.
/me feels like Cassandra
Quoted 12 lines
Your other series where you only have to change
drivers/platform/chrome/cros_ec_chardev.cjust confirms how wrong this approach is.Given you say this is such a bug I think you really should be sending a series that is patches 5 through 7 from the other series and a simple rwsem instead of
misc_deregister_sync()to deal with this bug ASAP. No need to complicate a simple bug fix in a driver with all these core changes.Once the bug is fixed you can continue to try to propose more general solutions.
Signature · 3 lines
Regards, Laurent Pinchart
Bartosz Golaszewski
maintainerRe: [PATCH v10 7/9] gpio: Remove unused `chip` and `srcu` in struct gpio_device
In reply to Tzung-Bi ShihQuoted 6 lines · Tzung-Bi Shih
Quoted 4 lines · Bartosz Golaszewski
Quoted 3 lines · Tzung-Bi Shih
chipandsrcuin structgpio_deviceare unused as their usages are replaced to use revocable. Remove them.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Signed-off-by Tzung-Bi Shih <[email protected]>---
I'm thinking that all the GPIO patches could actually be squashed into one. Is there any technical reason for the split or is it just for easier review?
Correct, they are separated only for easier review. Would you prefer I squash the 5 patches into a single patch?
Yes, I think it's less churn that way.
Bartosz
Tzung-Bi Shih
maintainerRe: [PATCH v10 8/9] platform/chrome: Protect cros_ec_device lifecycle with revocable
In reply to Jason GunthorpeQuoted 27 lines · Jason Gunthorpe
Quoted 7 lines · Tzung-Bi Shih
struct
cros_ec_device*cros_ec_device_alloc(struct device *dev)drivers/platform/chrome/cros_ec.c +2
-0@@ -47,6 +49,15 @@ struct cros_ec_device *cros_ec_device_alloc(struct device *dev)if (!ec_dev)return NULL ; + ec_dev->its_rev =revocable_alloc (ec_dev); +if (!ec_dev->its_rev)drivers/platform/chrome/cros_ec.c struct cros_ec_device *cros_ec_device_alloc(struct device *dev) · line 63
Quoted 10 lines
drivers/platform/chrome/cros_ec.c +7
-0+return NULL ; +/* +* Drop the extra reference for the caller as the caller is the +* resource provider. +*/ +revocable_put (ec_dev->its_rev); + ec_dev->din_size =sizeof (struct ec_host_response ) +sizeof (struct ec_response_get_protocol_info ) +EC_MAX_RESPONSE_OVERHEAD ;FWIW I am still very much against seeing any revokable concept used *between two drivers*. That will turn the kernel's lifetime model into spaghetti code.
Your other series where you only have to change
drivers/platform/chrome/cros_ec_chardev.cjust confirms how wrong this approach is.
Quoted 5 lines
Given you say this is such a bug I think you really should be sending a series that is patches 5 through 7 from the other series and a simple rwsem instead of
misc_deregister_sync()to deal with this bug ASAP. No need to complicate a simple bug fix in a driver with all these core changes.
Apologies for missing this suggestion.
For "patches 5 through 7 from the other series" I guess you're referring:
- https://lore.kernel.org/all/[email protected]
- https://lore.kernel.org/all/[email protected]
- https://lore.kernel.org/all/[email protected]
Could you provide a bit more detail on the rwsem approach? I'm not entirely clear on what data or operations the rwsem would be protecting.
Re: [PATCH v10 8/9] platform/chrome: Protect cros_ec_device lifecycle with revocable
Quoted 6 lines · Tzung-Bi Shih
Quoted 5 lines
Given you say this is such a bug I think you really should be sending a series that is patches 5 through 7 from the other series and a simple rwsem instead of
misc_deregister_sync()to deal with this bug ASAP. No need to complicate a simple bug fix in a driver with all these core changes.
Quoted 6 lines
Apologies for missing this suggestion.
For "patches 5 through 7 from the other series" I guess you're referring:
- https://lore.kernel.org/all/[email protected]
- https://lore.kernel.org/all/[email protected]
- https://lore.kernel.org/all/[email protected]
Yes
Quoted 2 lines
Could you provide a bit more detail on the rwsem approach? I'm not entirely clear on what data or operations the rwsem would be protecting.
Just put a rwsem, or even scru, inside the driver's fops.
You can refactor that out to a misc or revocable later.
Jason
Tzung-Bi Shih
maintainerRe: [PATCH v10 8/9] platform/chrome: Protect cros_ec_device lifecycle with revocable
Quoted 15 lines · Jason Gunthorpe
Quoted 6 lines · Tzung-Bi Shih
Quoted 5 lines
Given you say this is such a bug I think you really should be sending a series that is patches 5 through 7 from the other series and a simple rwsem instead of
misc_deregister_sync()to deal with this bug ASAP. No need to complicate a simple bug fix in a driver with all these core changes.Quoted 6 lines
Apologies for missing this suggestion.
For "patches 5 through 7 from the other series" I guess you're referring:
- https://lore.kernel.org/all/[email protected]
- https://lore.kernel.org/all/[email protected]
- https://lore.kernel.org/all/[email protected]
Quoted 8 lines
Yes
Quoted 2 lines
Could you provide a bit more detail on the rwsem approach? I'm not entirely clear on what data or operations the rwsem would be protecting.
Just put a rwsem, or even scru, inside the driver's fops.
You can refactor that out to a misc or revocable later.
I see. Thank you for your suggestion. I will explore it and send out a new version.
Tzung-Bi Shih
maintainerRe: [PATCH v10 8/9] platform/chrome: Protect cros_ec_device lifecycle with revocable
Quoted 21 lines · Tzung-Bi Shih
Quoted 20 lines · Jason Gunthorpe
Quoted 6 lines · Tzung-Bi Shih
Quoted 5 lines
Given you say this is such a bug I think you really should be sending a series that is patches 5 through 7 from the other series and a simple rwsem instead of
misc_deregister_sync()to deal with this bug ASAP. No need to complicate a simple bug fix in a driver with all these core changes.Quoted 6 lines
Apologies for missing this suggestion.
For "patches 5 through 7 from the other series" I guess you're referring:
- https://lore.kernel.org/all/[email protected]
- https://lore.kernel.org/all/[email protected]
- https://lore.kernel.org/all/[email protected]Yes
Quoted 2 lines
Could you provide a bit more detail on the rwsem approach? I'm not entirely clear on what data or operations the rwsem would be protecting.
Quoted 6 lines
Quoted 3 lines
Just put a rwsem, or even scru, inside the driver's fops.
You can refactor that out to a misc or revocable later.
I see. Thank you for your suggestion. I will explore it and send out a new version.
https://lore.kernel.org/all/[email protected]
is an attempt at it.