Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Revert "revocable: Revocable resource management"

This reverts commit 62eb557580eb2177cf16c3fd2b6efadff297b29a.

The revocable implementation uses two separate abstractions, struct
revocable_provider and struct revocable, in order to store the SRCU read
lock index which must be passed unaltered to srcu_read_unlock() in the
same context when a resource is no longer needed.

With the merged revocable API, multiple threads could however share the
same struct revocable and therefore potentially overwrite the SRCU index
of another thread which can cause the SRCU synchronisation in
revocable_provider_revoke() to never complete. [1]

An example revocable conversion of the gpiolib code also turned out to
be fundamentally flawed and could lead to use-after-free. [2]

An attempt to address both issues was quickly put together and merged,
but revocable is still fundamentally broken. [3]

Specifically, the latest design relies on RCU for storing a pointer to
the revocable provider, but since the resource can be shared by value
(e.g. as in the now reverted selftests) this does not work at all and
can also lead to use-after-free:

static void revocable_provider_release(struct kref *kref)
{
struct revocable_provider *rp = container_of(kref,
struct revocable_provider, kref);

cleanup_srcu_struct(&rp->srcu);
kfree_rcu(rp, rcu);
}

void revocable_provider_revoke(struct revocable_provider __rcu **rp_ptr)
{
struct revocable_provider *rp;

rp = rcu_replace_pointer(*rp_ptr, NULL, 1);
...
kref_put(&rp->kref, revocable_provider_release);
}

int revocable_init(struct revocable_provider __rcu *_rp,
struct revocable *rev)
{
struct revocable_provider *rp;

...

scoped_guard(rcu) {
rp = rcu_dereference(_rp);
if (!rp)
return -ENODEV;

if (!kref_get_unless_zero(&rp->kref))
return -ENODEV;
}

...
}

producer:

priv->rp = revocable_provider_alloc(&priv->res);
// pass priv->rp by value to consumer
revocable_provider_revoke(&priv->rp);

consumer:

struct revocable_provider __rcu *rp = filp->private_data;
struct revocable *rev;

revocable_init(rp, &rev);

as _rp would still be non-NULL in revocable_init() regardless of whether
the producer has revoked the resource and set its pointer to NULL.

Essentially revocable still relies on having a pointer to reference
counted driver data which holds the revocable provider, which makes all
the RCU protection unnecessary along with most of the current revocable
design and implementation.

As the above shows, and as has been pointed out repeatedly elsewhere,
these kind of issues are not something that should be addressed
incrementally. [4]

Revert the revocable implementation until a redesign has been proposed
and evaluated properly.

Link: https://lore.kernel.org/all/20260124170535.11756-4-johan@kernel.org/ [1]
Link: https://lore.kernel.org/all/aXT45B6vLf9R3Pbf@hovoldconsulting.com/ [2]
Link: https://lore.kernel.org/all/20260129143733.45618-1-tzungbi@kernel.org/ [3]
Link: https://lore.kernel.org/all/aXobzoeooJqxMkEj@hovoldconsulting.com/ [4]
Signed-off-by: Johan Hovold <johan@kernel.org>
Link: https://patch.msgid.link/20260204142849.22055-4-johan@kernel.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Johan Hovold and committed by
Greg Kroah-Hartman
21bab791 7149ce34

-471
-1
Documentation/driver-api/driver-model/index.rst
··· 14 14 overview 15 15 platform 16 16 porting 17 - revocable 18 17 19 18 .. only:: subproject and html 20 19
-149
Documentation/driver-api/driver-model/revocable.rst
··· 1 - .. SPDX-License-Identifier: GPL-2.0 2 - 3 - ============================== 4 - Revocable Resource Management 5 - ============================== 6 - 7 - Overview 8 - ======== 9 - 10 - .. kernel-doc:: drivers/base/revocable.c 11 - :doc: Overview 12 - 13 - Revocable vs. Devres (devm) 14 - =========================== 15 - 16 - It's important to understand the distinct roles of the Revocable and Devres, 17 - and how they can complement each other. They address different problems in 18 - resource management: 19 - 20 - * **Devres:** Primarily address **resource leaks**. The lifetime of the 21 - resources is tied to the lifetime of the device. The resource is 22 - automatically freed when the device is unbound. This cleanup happens 23 - irrespective of any potential active users. 24 - 25 - * **Revocable:** Primarily addresses **invalid memory access**, 26 - such as Use-After-Free (UAF). It's an independent synchronization 27 - primitive that decouples consumer access from the resource's actual 28 - presence. Consumers interact with a "revocable object" (an intermediary), 29 - not the underlying resource directly. This revocable object persists as 30 - long as there are active references to it from consumer handles. 31 - 32 - **Key Distinctions & How They Complement Each Other:** 33 - 34 - 1. **Reference Target:** Consumers of a resource managed by the Revocable 35 - mechanism hold a reference to the *revocable object*, not the 36 - encapsulated resource itself. 37 - 38 - 2. **Resource Lifetime vs. Access:** The underlying resource's lifetime is 39 - independent of the number of references to the revocable object. The 40 - resource can be freed at any point. A common scenario is the resource 41 - being freed by `devres` when the providing device is unbound. 42 - 43 - 3. **Safe Access:** Revocable provides a safe way to attempt access. Before 44 - using the resource, a consumer uses the Revocable API (e.g., 45 - revocable_try_access()). This function checks if the resource is still 46 - valid. It returns a pointer to the resource only if it hasn't been 47 - revoked; otherwise, it returns NULL. This prevents UAF by providing a 48 - clear signal that the resource is gone. 49 - 50 - 4. **Complementary Usage:** `devres` and Revocable work well together. 51 - `devres` can handle the automatic allocation and deallocation of a 52 - resource tied to a device. The Revocable mechanism can be layered on top 53 - to provide safe access for consumers whose lifetimes might extend beyond 54 - the provider device's lifetime. For instance, a userspace program might 55 - keep a character device file open even after the physical device has been 56 - removed. In this case: 57 - 58 - * `devres` frees the device-specific resource upon unbinding. 59 - * The Revocable mechanism ensures that any subsequent operations on the 60 - open file handle, which attempt to access the now-freed resource, 61 - will fail gracefully (e.g., revocable_try_access() returns NULL) 62 - instead of causing a UAF. 63 - 64 - In summary, `devres` ensures resources are *released* to prevent leaks, while 65 - the Revocable mechanism ensures that attempts to *access* these resources are 66 - done safely, even if the resource has been released. 67 - 68 - API and Usage 69 - ============= 70 - 71 - For Resource Providers 72 - ---------------------- 73 - .. kernel-doc:: drivers/base/revocable.c 74 - :identifiers: revocable_provider 75 - 76 - .. kernel-doc:: drivers/base/revocable.c 77 - :identifiers: revocable_provider_alloc 78 - 79 - .. kernel-doc:: drivers/base/revocable.c 80 - :identifiers: revocable_provider_revoke 81 - 82 - For Resource Consumers 83 - ---------------------- 84 - .. kernel-doc:: include/linux/revocable.h 85 - :identifiers: revocable 86 - 87 - .. kernel-doc:: drivers/base/revocable.c 88 - :identifiers: revocable_init 89 - 90 - .. kernel-doc:: drivers/base/revocable.c 91 - :identifiers: revocable_deinit 92 - 93 - .. kernel-doc:: drivers/base/revocable.c 94 - :identifiers: revocable_try_access 95 - 96 - .. kernel-doc:: drivers/base/revocable.c 97 - :identifiers: revocable_withdraw_access 98 - 99 - .. kernel-doc:: include/linux/revocable.h 100 - :identifiers: REVOCABLE_TRY_ACCESS_WITH 101 - 102 - Example Usage 103 - ~~~~~~~~~~~~~ 104 - 105 - .. code-block:: c 106 - 107 - void consumer_use_resource(struct revocable_provider *rp) 108 - { 109 - struct foo_resource *res; 110 - 111 - REVOCABLE_TRY_ACCESS_WITH(rp, res); 112 - // Always check if the resource is valid. 113 - if (!res) { 114 - pr_warn("Resource is not available\n"); 115 - return; 116 - } 117 - 118 - // At this point, 'res' is guaranteed to be valid until 119 - // this block exits. 120 - do_something_with(res); 121 - 122 - } // revocable_withdraw_access() is automatically called here. 123 - 124 - .. kernel-doc:: include/linux/revocable.h 125 - :identifiers: REVOCABLE_TRY_ACCESS_SCOPED 126 - 127 - Example Usage 128 - ~~~~~~~~~~~~~ 129 - 130 - .. code-block:: c 131 - 132 - void consumer_use_resource(struct revocable_provider *rp) 133 - { 134 - struct foo_resource *res; 135 - 136 - REVOCABLE_TRY_ACCESS_SCOPED(rp, res) { 137 - // Always check if the resource is valid. 138 - if (!res) { 139 - pr_warn("Resource is not available\n"); 140 - return; 141 - } 142 - 143 - // At this point, 'res' is guaranteed to be valid until 144 - // this block exits. 145 - do_something_with(res); 146 - } 147 - 148 - // revocable_withdraw_access() is automatically called here. 149 - }
-7
MAINTAINERS
··· 22385 22385 F: kernel/rseq.c 22386 22386 F: tools/testing/selftests/rseq/ 22387 22387 22388 - REVOCABLE RESOURCE MANAGEMENT 22389 - M: Tzung-Bi Shih <tzungbi@kernel.org> 22390 - L: linux-kernel@vger.kernel.org 22391 - S: Maintained 22392 - F: drivers/base/revocable.c 22393 - F: include/linux/revocable.h 22394 - 22395 22388 RFKILL 22396 22389 M: Johannes Berg <johannes@sipsolutions.net> 22397 22390 L: linux-wireless@vger.kernel.org
-225
drivers/base/revocable.c
··· 1 - // SPDX-License-Identifier: GPL-2.0 2 - /* 3 - * Copyright 2026 Google LLC 4 - * 5 - * Revocable resource management 6 - */ 7 - 8 - #include <linux/device.h> 9 - #include <linux/kref.h> 10 - #include <linux/revocable.h> 11 - #include <linux/slab.h> 12 - #include <linux/srcu.h> 13 - 14 - /** 15 - * DOC: Overview 16 - * 17 - * The "revocable" mechanism is a synchronization primitive designed to manage 18 - * safe access to resources that can be asynchronously removed or invalidated. 19 - * Its primary purpose is to prevent Use-After-Free (UAF) errors when 20 - * interacting with resources whose lifetimes are not guaranteed to outlast 21 - * their consumers. 22 - * 23 - * This is particularly useful in systems where resources can disappear 24 - * unexpectedly, such as those provided by hot-pluggable devices like USB. 25 - * When a consumer holds a reference to such a resource, the underlying device 26 - * might be removed, causing the resource's memory to be freed. Subsequent 27 - * access attempts by the consumer would then lead to UAF errors. 28 - * 29 - * Revocable addresses this by providing a form of "weak reference" and a 30 - * controlled access method. It allows a resource consumer to safely attempt to 31 - * access the resource. The mechanism guarantees that any access granted is 32 - * valid for the duration of its use. If the resource has already been 33 - * revoked (i.e., freed), the access attempt will fail safely, typically by 34 - * returning NULL, instead of causing a crash. 35 - * 36 - * The implementation uses a provider/consumer model built on Sleepable 37 - * RCU (SRCU) to guarantee safe memory access: 38 - * 39 - * - A resource provider, such as a driver for a hot-pluggable device, 40 - * allocates a struct revocable_provider and initializes it with a pointer 41 - * to the resource. 42 - * 43 - * - A resource consumer that wants to access the resource allocates a 44 - * struct revocable which acts as a handle containing a reference to the 45 - * provider. 46 - * 47 - * - To access the resource, the consumer uses revocable_try_access(). 48 - * This function enters an SRCU read-side critical section and returns 49 - * the pointer to the resource. If the provider has already freed the 50 - * resource, it returns NULL. After use, the consumer calls 51 - * revocable_withdraw_access() to exit the SRCU critical section. The 52 - * REVOCABLE_TRY_ACCESS_WITH() and REVOCABLE_TRY_ACCESS_SCOPED() are 53 - * convenient helpers for doing that. 54 - * 55 - * - When the provider needs to remove the resource, it calls 56 - * revocable_provider_revoke(). This function sets the internal resource 57 - * pointer to NULL and then calls synchronize_srcu() to wait for all 58 - * current readers to finish before the resource can be completely torn 59 - * down. 60 - */ 61 - 62 - /** 63 - * struct revocable_provider - A handle for resource provider. 64 - * @srcu: The SRCU to protect the resource. 65 - * @res: The pointer of resource. It can point to anything. 66 - * @kref: The refcount for this handle. 67 - * @rcu: The RCU to protect pointer to itself. 68 - */ 69 - struct revocable_provider { 70 - struct srcu_struct srcu; 71 - void __rcu *res; 72 - struct kref kref; 73 - struct rcu_head rcu; 74 - }; 75 - 76 - /** 77 - * revocable_provider_alloc() - Allocate struct revocable_provider. 78 - * @res: The pointer of resource. 79 - * 80 - * This holds an initial refcount to the struct. 81 - * 82 - * Return: The pointer of struct revocable_provider. NULL on errors. 83 - * It enforces the caller handles the returned pointer in RCU ways. 84 - */ 85 - struct revocable_provider __rcu *revocable_provider_alloc(void *res) 86 - { 87 - struct revocable_provider *rp; 88 - 89 - rp = kzalloc(sizeof(*rp), GFP_KERNEL); 90 - if (!rp) 91 - return NULL; 92 - 93 - init_srcu_struct(&rp->srcu); 94 - RCU_INIT_POINTER(rp->res, res); 95 - kref_init(&rp->kref); 96 - 97 - return (struct revocable_provider __rcu *)rp; 98 - } 99 - EXPORT_SYMBOL_GPL(revocable_provider_alloc); 100 - 101 - static void revocable_provider_release(struct kref *kref) 102 - { 103 - struct revocable_provider *rp = container_of(kref, 104 - struct revocable_provider, kref); 105 - 106 - cleanup_srcu_struct(&rp->srcu); 107 - kfree_rcu(rp, rcu); 108 - } 109 - 110 - /** 111 - * revocable_provider_revoke() - Revoke the managed resource. 112 - * @rp_ptr: The pointer of pointer of resource provider. 113 - * 114 - * This sets the resource `(struct revocable_provider *)->res` to NULL to 115 - * indicate the resource has gone. 116 - * 117 - * This drops the refcount to the resource provider. If it is the final 118 - * reference, revocable_provider_release() will be called to free the struct. 119 - * 120 - * It enforces the caller to pass a pointer of pointer of resource provider so 121 - * that it sets \*rp_ptr to NULL to prevent from keeping a dangling pointer. 122 - */ 123 - void revocable_provider_revoke(struct revocable_provider __rcu **rp_ptr) 124 - { 125 - struct revocable_provider *rp; 126 - 127 - rp = rcu_replace_pointer(*rp_ptr, NULL, 1); 128 - if (!rp) 129 - return; 130 - 131 - rcu_assign_pointer(rp->res, NULL); 132 - synchronize_srcu(&rp->srcu); 133 - kref_put(&rp->kref, revocable_provider_release); 134 - } 135 - EXPORT_SYMBOL_GPL(revocable_provider_revoke); 136 - 137 - /** 138 - * revocable_init() - Initialize struct revocable. 139 - * @_rp: The pointer of resource provider. 140 - * @rev: The pointer of resource consumer. 141 - * 142 - * This holds a refcount to the resource provider. 143 - * 144 - * Return: 0 on success, -errno otherwise. 145 - */ 146 - int revocable_init(struct revocable_provider __rcu *_rp, struct revocable *rev) 147 - { 148 - struct revocable_provider *rp; 149 - 150 - if (!_rp) 151 - return -ENODEV; 152 - 153 - /* 154 - * Enter a read-side critical section. 155 - * 156 - * This prevents kfree_rcu() from freeing the struct revocable_provider 157 - * memory, for the duration of this scope. 158 - */ 159 - scoped_guard(rcu) { 160 - rp = rcu_dereference(_rp); 161 - if (!rp) 162 - /* The revocable provider has been revoked. */ 163 - return -ENODEV; 164 - 165 - if (!kref_get_unless_zero(&rp->kref)) 166 - /* 167 - * The revocable provider is releasing (i.e., 168 - * revocable_provider_release() has been called). 169 - */ 170 - return -ENODEV; 171 - } 172 - /* At this point, `rp` is safe to access as holding a kref of it */ 173 - 174 - rev->rp = rp; 175 - return 0; 176 - } 177 - EXPORT_SYMBOL_GPL(revocable_init); 178 - 179 - /** 180 - * revocable_deinit() - Deinitialize struct revocable. 181 - * @rev: The pointer of struct revocable. 182 - * 183 - * This drops a refcount to the resource provider. If it is the final 184 - * reference, revocable_provider_release() will be called to free the struct. 185 - */ 186 - void revocable_deinit(struct revocable *rev) 187 - { 188 - struct revocable_provider *rp = rev->rp; 189 - 190 - kref_put(&rp->kref, revocable_provider_release); 191 - } 192 - EXPORT_SYMBOL_GPL(revocable_deinit); 193 - 194 - /** 195 - * revocable_try_access() - Try to access the resource. 196 - * @rev: The pointer of struct revocable. 197 - * 198 - * This tries to de-reference to the resource and enters a RCU critical 199 - * section. 200 - * 201 - * Return: The pointer to the resource. NULL if the resource has gone. 202 - */ 203 - void *revocable_try_access(struct revocable *rev) __acquires(&rev->rp->srcu) 204 - { 205 - struct revocable_provider *rp = rev->rp; 206 - 207 - rev->idx = srcu_read_lock(&rp->srcu); 208 - return srcu_dereference(rp->res, &rp->srcu); 209 - } 210 - EXPORT_SYMBOL_GPL(revocable_try_access); 211 - 212 - /** 213 - * revocable_withdraw_access() - Stop accessing to the resource. 214 - * @rev: The pointer of struct revocable. 215 - * 216 - * Call this function to indicate the resource is no longer used. It exits 217 - * the RCU critical section. 218 - */ 219 - void revocable_withdraw_access(struct revocable *rev) __releases(&rev->rp->srcu) 220 - { 221 - struct revocable_provider *rp = rev->rp; 222 - 223 - srcu_read_unlock(&rp->srcu, rev->idx); 224 - } 225 - EXPORT_SYMBOL_GPL(revocable_withdraw_access);
-89
include/linux/revocable.h
··· 1 - /* SPDX-License-Identifier: GPL-2.0 */ 2 - /* 3 - * Copyright 2026 Google LLC 4 - */ 5 - 6 - #ifndef __LINUX_REVOCABLE_H 7 - #define __LINUX_REVOCABLE_H 8 - 9 - #include <linux/compiler.h> 10 - #include <linux/cleanup.h> 11 - 12 - struct device; 13 - struct revocable_provider; 14 - 15 - /** 16 - * struct revocable - A handle for resource consumer. 17 - * @rp: The pointer of resource provider. 18 - * @idx: The index for the RCU critical section. 19 - */ 20 - struct revocable { 21 - struct revocable_provider *rp; 22 - int idx; 23 - }; 24 - 25 - struct revocable_provider __rcu *revocable_provider_alloc(void *res); 26 - void revocable_provider_revoke(struct revocable_provider __rcu **rp); 27 - 28 - int revocable_init(struct revocable_provider __rcu *rp, struct revocable *rev); 29 - void revocable_deinit(struct revocable *rev); 30 - void *revocable_try_access(struct revocable *rev) __acquires(&rev->rp->srcu); 31 - void revocable_withdraw_access(struct revocable *rev) __releases(&rev->rp->srcu); 32 - 33 - DEFINE_FREE(access_rev, struct revocable *, { 34 - if ((_T)->idx != -1) 35 - revocable_withdraw_access(_T); 36 - if ((_T)->rp) 37 - revocable_deinit(_T); 38 - }) 39 - 40 - #define _REVOCABLE_TRY_ACCESS_WITH(_rp, _rev, _res) \ 41 - struct revocable _rev = {.rp = NULL, .idx = -1}; \ 42 - struct revocable *__UNIQUE_ID(name) __free(access_rev) = &_rev; \ 43 - _res = revocable_init(_rp, &_rev) ? NULL : revocable_try_access(&_rev) 44 - 45 - /** 46 - * REVOCABLE_TRY_ACCESS_WITH() - A helper for accessing revocable resource 47 - * @_rp: The provider's ``struct revocable_provider *`` handle. 48 - * @_res: A pointer variable that will be assigned the resource. 49 - * 50 - * The macro simplifies the access-release cycle for consumers, ensuring that 51 - * corresponding revocable_withdraw_access() and revocable_deinit() are called, 52 - * even in the case of an early exit. 53 - * 54 - * It creates a local variable in the current scope. @_res is populated with 55 - * the result of revocable_try_access(). The consumer code **must** check if 56 - * @_res is ``NULL`` before using it. The revocable_withdraw_access() function 57 - * is automatically called when the scope is exited. 58 - * 59 - * Note: It shares the same issue with guard() in cleanup.h. No goto statements 60 - * are allowed before the helper. Otherwise, the compiler fails with 61 - * "jump bypasses initialization of variable with __attribute__((cleanup))". 62 - */ 63 - #define REVOCABLE_TRY_ACCESS_WITH(_rp, _res) \ 64 - _REVOCABLE_TRY_ACCESS_WITH(_rp, __UNIQUE_ID(name), _res) 65 - 66 - #define _REVOCABLE_TRY_ACCESS_SCOPED(_rp, _rev, _label, _res) \ 67 - for (struct revocable _rev = {.rp = NULL, .idx = -1}, \ 68 - *__UNIQUE_ID(name) __free(access_rev) = &_rev; \ 69 - (_res = revocable_init(_rp, &_rev) ? NULL : \ 70 - revocable_try_access(&_rev)) || true; \ 71 - ({ goto _label; })) \ 72 - if (0) { \ 73 - _label: \ 74 - break; \ 75 - } else 76 - 77 - /** 78 - * REVOCABLE_TRY_ACCESS_SCOPED() - A helper for accessing revocable resource 79 - * @_rp: The provider's ``struct revocable_provider *`` handle. 80 - * @_res: A pointer variable that will be assigned the resource. 81 - * 82 - * Similar to REVOCABLE_TRY_ACCESS_WITH() but with an explicit scope from a 83 - * temporary ``for`` loop. 84 - */ 85 - #define REVOCABLE_TRY_ACCESS_SCOPED(_rp, _res) \ 86 - _REVOCABLE_TRY_ACCESS_SCOPED(_rp, __UNIQUE_ID(name), \ 87 - __UNIQUE_ID(label), _res) 88 - 89 - #endif /* __LINUX_REVOCABLE_H */