rust: revocable: clarify invariants in safety documentation
PATCH 1/1 rust: revocable: clarify invariants in safety documentation
Clarify the safety invariants for Revocable<T>, documenting that data is only valid if is_available is true, and that access requires holding the RCU read-side lock.
This helps developers understand when it is safe to access the wrapped data and the role of RCU in ensuring memory safety.
This is part of issue #1160 ("correct safety comments and documentation in revocable.rs"), reported by Miguel Ojeda and Benno Lossin.
--- rust/kernel/revocable.rs | 9 +++++++++ 1 file changed, 9 insertions(+)
rust/kernel/revocable.rs +9 -0
@@ -61,6 +61,15 @@/// v.revoke(); /// assert_eq!(add_two(&v), None); /// ``` +/// +/// # Invariants +/// +/// - The wrapped object `data` is valid if and only if `is_available` is `true`. +/// - Access to `data` must occur only while holding the RCU read-side lock (e.g., via +/// [`Revocable::try_access`] or [`Revocable::try_access_with_guard`]). +/// - Once `is_available` is set to `false`, further access to `data` is disallowed, +/// and the object is dropped either after an RCU grace period (in [`revoke`]), +/// or immediately (in [`revoke_nosync`]). #[pin_data( PinnedDrop )] pub struct Revocable <T > { is_available:AtomicBool ,
Signature · 1 line
2.49.0
Quoted 6 lines · Marcelo Moreira
This is part of issue #1160 ("correct safety comments and documentation in
revocable.rs"), reported by Miguel Ojeda and Benno Lossin.
I think it was just Benno. :)
Quoted 1 line
Tested-by Marcelo Moreira <[email protected]>
Normally one is supposed to test a patch before submitting it, i.e. it is implicit.
Quoted 1 line
+/// # Invariants
We also place // INVARIANT: ... comments in the places that need to ensure the type invariants hold, e.g. in constructors.
Given the issue, I think Benno may have also wanted that you then refer to the type invariants to justify some of the comments, e.g. in the "Since self.is_available is true, ..." ones.
Quoted 1 line
+/// - Access to
datamust occur only while holding the RCU read-side lock (e.g., vi
The "must occur" wording seems a bit strange for a type invariant, but maybe Benno had something in mind here?
Thanks for the patch!
Cheers,
Miguel
Benno Lossin
reviewerQuoted 16 lines · Miguel Ojeda
Quoted 6 lines · Marcelo Moreira
This is part of issue #1160 ("correct safety comments and documentation in
revocable.rs"), reported by Miguel Ojeda and Benno Lossin.I think it was just Benno. :)
Quoted 1 line
Tested-by Marcelo Moreira <[email protected]>Normally one is supposed to test a patch before submitting it, i.e. it is implicit.
Quoted 8 lines
Quoted 1 line
+/// # Invariants
We also place
// INVARIANT: ...comments in the places that need to ensure the type invariants hold, e.g. in constructors.Given the issue, I think Benno may have also wanted that you then refer to the type invariants to justify some of the comments, e.g. in the "Since
self.is_availableis true, ..." ones.
Indeed, we also need the comments that establish/make use of the invariant. For example in the try_access_with_guard function.
There also is a missing invariant on RevocableGuard that the data_ref pointer is valid.
Quoted 4 lines
Quoted 1 line
+/// - Access to
datamust occur only while holding the RCU read-side lock (e.g., viThe "must occur" wording seems a bit strange for a type invariant, but maybe Benno had something in mind here?
Yeah, I don't think that using RCU is even a hard requirement. One could also call revoke_nosync, since one knows the revocable is only used by a single kernel thread. I also don't think that it's needed for memory correctness, but I could be wrong there.
The most important invariant is that data is valid while is_available is true. That's what's needed by the different functions accessing it.
---
Cheers,
Benno
Hey Miguel, hey Benno,
Thanks a lot for the feedback! =)
I now get what you meant about the invariants: it's not just about documenting them at the top, but also about adding // INVARIANT: ... comments in the code where they actually matter — like in the constructors and in try_access_with_guard.
Benno, thanks for pointing out the missing invariant in RevocableGuard. I’ll add a comment explaining that data_ref is only valid when is_available is true.
And regarding RCU, your comments made things clearer. Instead of saying it's a hard requirement, I’ll reword it to focus on the is_available check as the actual safety condition, and just mention RCU as a typical way to ensure safety — but not the only one.
I’ll get v2 ready with those improvements and send it soon.
Cheers,
Marcelo M.
Em sex., 2 de mai. de 2025 às 04:33, Benno Lossin <[email protected]> escreveu:
Quoted 50 lines · Miguel Ojeda
Quoted 16 lines
Quoted 6 lines · Marcelo Moreira
This is part of issue #1160 ("correct safety comments and documentation in
revocable.rs"), reported by Miguel Ojeda and Benno Lossin.I think it was just Benno. :)
Quoted 1 line
Tested-by Marcelo Moreira <[email protected]>Normally one is supposed to test a patch before submitting it, i.e. it is implicit.
Quoted 8 lines
Quoted 1 line
+/// # Invariants
We also place
// INVARIANT: ...comments in the places that need to ensure the type invariants hold, e.g. in constructors.Given the issue, I think Benno may have also wanted that you then refer to the type invariants to justify some of the comments, e.g. in the "Since
self.is_availableis true, ..." ones.Indeed, we also need the comments that establish/make use of the invariant. For example in the
try_access_with_guardfunction.There also is a missing invariant on
RevocableGuardthat thedata_refpointer is valid.Quoted 4 lines
Quoted 1 line
+/// - Access to
datamust occur only while holding the RCU read-side lock (e.g., viThe "must occur" wording seems a bit strange for a type invariant, but maybe Benno had something in mind here?
Yeah, I don't think that using RCU is even a hard requirement. One could also call
revoke_nosync, since one knows the revocable is only used by a single kernel thread. I also don't think that it's needed for memory correctness, but I could be wrong there.The most important invariant is that
datais valid whileis_availableis true. That's what's needed by the different functions accessing it.---
Cheers,
Benno