Re: [PATCH] revocable: rust: document why &T is not used in RevocableGuard

From: Benno Lossin
Date: Thu Jun 12 2025 - 08:16:36 EST


On Thu Jun 12, 2025 at 1:17 PM CEST, Alice Ryhl wrote:
> When a reference appears in a function argument, the reference is
> assumed to be valid for the entire duration of that function call; this
> is called a stack protector [1]. Because of that, custom pointer types
> whose destructor may invalidate the pointee (i.e. they are more similar
> to Box<T> than &T) cannot internally use a reference, and must instead
> use a raw pointer.
>
> This issue is something that is often missed during unsafe review. For
> examples, see [2] and [3]. To ensure that people don't try to simplify
> RevocableGuard by changing the raw pointer to a reference, add a comment
> to that effect.
>
> Link: https://perso.crans.org/vanille/treebor/protectors.html [1]
> Link: https://users.rust-lang.org/t/unsafe-code-review-semi-owning-weak-rwlock-t-guard/95706 [2]
> Link: https://lore.kernel.org/all/aEqdur4JTFa1V20U@xxxxxxxxxx/ [3]
> Signed-off-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>

Thanks!

Reviewed-by: Benno Lossin <lossin@xxxxxxxxxx>

---
Cheers,
Benno

> ---
> rust/kernel/revocable.rs | 4 ++++
> 1 file changed, 4 insertions(+)