From: "Benno Lossin" <lossin@kernel.org>
To: "Marcelo Moreira" <marcelomoreira1905@gmail.com>
Cc: <aliceryhl@google.com>, <dakr@kernel.org>, <ojeda@kernel.org>,
<rust-for-linux@vger.kernel.org>, <skhan@linuxfoundation.org>,
<linux-kernel-mentees@lists.linuxfoundation.org>,
<~lkcamp/patches@lists.sr.ht>
Subject: Re: [PATCH v6 3/3] rust: revocable: Document RevocableGuard invariants and refine Deref safety
Date: Sat, 12 Jul 2025 10:26:04 +0200 [thread overview]
Message-ID: <DB9XM595UG4V.3N51HST2TFVW@kernel.org> (raw)
In-Reply-To: <CAPZ3m_h37mCRO7oZX3qLnc+iFNwUu_wgYqEu_r0g-gtOB0661w@mail.gmail.com>
On Sat Jul 12, 2025 at 1:06 AM CEST, Marcelo Moreira wrote:
> Em ter., 8 de jul. de 2025 às 12:04, Benno Lossin <lossin@kernel.org> escreveu:
>> On Tue Jul 8, 2025 at 2:33 AM CEST, Marcelo Moreira wrote:
>> > Improves the `RevocableGuard` documentation by explicitly stating that
>> > its `data_ref` member is a valid pointer as an invariant. Additionally,
>> > the `Deref` implementation's `SAFETY` comment is refined to justify
>> > the `unsafe` dereference based on this new invariant and the
>> > `_rcu_guard` ensuring data accessibility.
>> >
>> > These changes address feedback regarding the clarity and completeness
>> > of `RevocableGuard`'s safety guarantees.
>> >
>> > Signed-off-by: Marcelo Moreira <marcelomoreira1905@gmail.com>
>> > ---
>> > rust/kernel/revocable.rs | 7 ++++---
>> > 1 file changed, 4 insertions(+), 3 deletions(-)
>> >
>> > diff --git a/rust/kernel/revocable.rs b/rust/kernel/revocable.rs
>> > index 6d8e9237dbdf..64fe44b4f5a3 100644
>> > --- a/rust/kernel/revocable.rs
>> > +++ b/rust/kernel/revocable.rs
>> > @@ -233,7 +233,8 @@ fn drop(self: Pin<&mut Self>) {
>> > ///
>> > /// # Invariants
>> > ///
>> > -/// The RCU read-side lock is held while the guard is alive.
>> > +/// - `data_ref` is a valid pointer to a `T` object for the entire lifetime of this guard.
>>
>> This should be:
>>
>> /// - `data_ref` is a valid pointer for as long as the RCU read-side lock is held.
>>
>> > +/// - The RCU read-side lock is held while the guard is alive.
>>
>> And this invariant is unnecessary.
>>
>> > pub struct RevocableGuard<'a, T> {
>> > // This can't use the `&'a T` type because references that appear in function arguments must
>> > // not become dangling during the execution of the function, which can happen if the
>> > @@ -258,8 +259,8 @@ impl<T> Deref for RevocableGuard<'_, T> {
>> > type Target = T;
>> >
>> > fn deref(&self) -> &Self::Target {
>> > - // SAFETY: By the type invariants, we hold the rcu read-side lock, so the object is
>> > - // guaranteed to remain valid.
>> > + // SAFETY: `self.data_ref` is valid for writes because of `Self`'s type invariants,
>> > + // and `_rcu_guard` ensures the data's accessibility for the lifetime of this guard.
>>
>> This also needs to be adjusted.
>>
>> Also this patch should fix any invariant comments needed to construct
>> `RevocableGuard`.
>
> Regarding this point, "...fix any invariant comments needed to
> construct `RevocableGuard`."
>
> I looked at the function that constructs `RevocableGuard`
> (try_access->RevocableGuard::new) and I think it's correct.
>
> In `try_access`, the comment justifies the validity of self.data.get()
> by mentioning that `self.is_available` is true and that the RCU read
> lock prevents data from being dropped. This aligns well with what we
> already have in try_access_with_guard, for example (from patch 1/3).
The comment seems wrong to me, it isn't even an `INVARIANT` comment. It
also talks about RCU in the wrong way and doesn't mention the type
invariants of `Self`...
> Since there are no other code snippets that construct
> `RevocableGuard`, I thought I'd submit the patch with just the
> adjustments I made previously.
There also is the `new` function of `RevocableGuard` that doesn't use an
invariant comment. It also isn't unsafe despite taking a raw pointer...
So either we make it unsafe, add the correct safety requirements which
should just be the type invariants of the guard or we remove it, since
we only use it once.
---
Cheers,
Benno
next prev parent reply other threads:[~2025-07-12 8:26 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-08 0:33 [PATCH v6 0/3] rust: revocable: Documentation and refactorings for Revocable type Marcelo Moreira
2025-07-08 0:33 ` [PATCH v6 1/3] rust: revocable: Clarify write invariant and update safety comments Marcelo Moreira
2025-07-08 0:33 ` [PATCH v6 2/3] rust: revocable: Refactor revocation mechanism to remove generic revoke_internal Marcelo Moreira
2025-07-08 0:33 ` [PATCH v6 3/3] rust: revocable: Document RevocableGuard invariants and refine Deref safety Marcelo Moreira
2025-07-08 15:04 ` Benno Lossin
2025-07-08 22:24 ` Marcelo Moreira
2025-07-09 8:04 ` Benno Lossin
2025-07-11 23:06 ` Marcelo Moreira
2025-07-12 8:26 ` Benno Lossin [this message]
2025-07-12 18:49 ` Marcelo Moreira
2025-07-12 19:56 ` Benno Lossin
2025-07-16 20:11 ` Marcelo Moreira
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=DB9XM595UG4V.3N51HST2TFVW@kernel.org \
--to=lossin@kernel.org \
--cc=aliceryhl@google.com \
--cc=dakr@kernel.org \
--cc=linux-kernel-mentees@lists.linuxfoundation.org \
--cc=marcelomoreira1905@gmail.com \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=skhan@linuxfoundation.org \
--cc=~lkcamp/patches@lists.sr.ht \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).