Skip to contentSkip to conversation

rust: revocable: clarify invariants in safety documentation

PATCH In review Marcelo Moreira 3 participants 4 messages May 1, 2025 – May 2, 2025 Rust

lore
Share & tools
lore
#0 lore raw

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
index 1e5a9d25c21b..2da3e9460c07 100644
@@ -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
#1 lore raw
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

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 data must 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

reviewer
#2 lore raw
Quoted 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

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_available is 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 data must 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?

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

#3 lore raw

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

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_available is 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 data must 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?

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