From: sashiko-bot@kernel.org
To: "Lyude Paul" <lyude@redhat.com>
Cc: ojeda@kernel.org, linux-media@vger.kernel.org
Subject: Re: [PATCH v17 5/6] rust: sync: Add SetOnce::reset()
Date: Wed, 03 Jun 2026 20:07:05 +0000 [thread overview]
Message-ID: <20260603200706.18CB21F00893@smtp.kernel.org> (raw)
In-Reply-To: <20260603195210.693856-6-lyude@redhat.com>
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Potential double-drop vulnerability (Use-After-Free) if the inner value's Drop implementation panics.
- [Low] Incorrect use of inline annotations for abstraction functions.
- [Low] The top-level docstring for `SetOnce` is contradicted by the newly added `reset` function.
--
commit 7a51e89f6588dab952d801924de16b34983e224f
Author: Lyude Paul <lyude@redhat.com>
rust: sync: Add SetOnce::reset()
This function simply drops the contents of the SetOnce, given a mutable
reference - since that proves we have exclusive access to the SetOnce.
Additionally, update the invariants for SetOnce to make it clear as to why
this is safe.
> diff --git a/rust/kernel/sync/set_once.rs b/rust/kernel/sync/set_once.rs
> index 139cef05e935f..d6e4fc2695673 100644
> --- a/rust/kernel/sync/set_once.rs
> +++ b/rust/kernel/sync/set_once.rs
> @@ -15,7 +15,7 @@
> ///
> /// # Invariants
> ///
> -/// - `init` may only increase in value.
> +/// - `init` may only increase in value, unless modified through a mutable reference.
> /// - `init` may only assume values in the range `0..=2`.
[Severity: Low]
Since this patch adds the ability to reset and repopulate the container,
does the top-level struct documentation for SetOnce also need updating?
The struct docstring guarantees that the container can be populated at most
once and remains populated by the same object for its lifetime, which
appears to be contradicted by the new reset() method.
[ ... ]
> @@ -110,17 +110,61 @@ pub fn copy(&self) -> Option<T>
> {
> self.as_ref().copied()
> }
> +
> + /// # Safety
> + ///
> + /// If this function returns `true`, `self` must be freed or `init` must be reset to `0`.
> + unsafe fn drop_val(&mut self) -> bool {
[Severity: Low]
This isn't a bug, but according to the Rust subsystem coding guidelines,
should small abstraction functions like drop_val() be annotated with
the #[inline] attribute?
> + if *self.init.get_mut() != 2 {
> + return false;
> + }
> +
> + let value = self.value.get_mut();
> + // SAFETY: Via our type invariants, `init` == 2 means `value` is initialized.
> + unsafe { value.assume_init_drop() };
> +
> + true
> + }
> +
> + /// Unset the [`SetOnce`].
> + ///
> + /// After this function is called, the [`SetOnce`] is empty and uninitialized. This function is
[ ... ]
> + pub fn reset(&mut self) -> bool {
[Severity: Low]
This isn't a bug, but should reset() also have an #[inline] annotation
following the subsystem guidelines for small abstraction functions?
> + // SAFETY: We write `0` to init below if this returns true.
> + let dropped = unsafe { self.drop_val() };
> + if dropped {
> + // INVARIANT:
> + // - We set `init` back to 0 through a mutable reference.
> + // - We dropped `value` above.
> + *self.init.get_mut() = 0;
> + }
> +
> + dropped
> + }
[Severity: High]
Could this code sequence lead to a double-drop use-after-free if the
inner value's drop implementation panics?
If drop_val() is called and value.assume_init_drop() panics, the process
will unwind. Because *self.init.get_mut() = 0 is only executed after
drop_val() returns successfully, init will remain 2.
During the unwind, the SetOnce drop implementation would execute:
SetOnce::drop() -> drop_val()
Since init is still 2, drop_val() would call value.assume_init_drop()
again on the already-dropped memory.
> impl<T> Drop for SetOnce<T> {
> + #[inline(always)]
> fn drop(&mut self) {
[Severity: Low]
This isn't a bug, but the Rust subsystem guidelines note that
#[inline(always)] should only be used for functions utilizing
build_assert!() that depend on function parameters.
Should this just be a standard #[inline] attribute instead?
> - if *self.init.get_mut() == 2 {
> - let value = self.value.get_mut();
> - // SAFETY: By the type invariants of `Self`, `self.init == 2` means that `self.value`
> - // contains a valid value. We have exclusive access, as we hold a `mut` reference to
> - // `self`.
> - unsafe { value.assume_init_drop() };
> - }
> + // SAFETY: We are dropping this value.
> + unsafe { self.drop_val() };
> }
> }
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260603195210.693856-1-lyude@redhat.com?part=5
next prev parent reply other threads:[~2026-06-03 20:07 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-03 19:42 [PATCH v17 0/6] Rust bindings for gem shmem Lyude Paul
2026-06-03 19:42 ` [PATCH v17 1/6] rust: drm: gem: shmem: Fix Default implementation for ObjectConfig Lyude Paul
2026-06-03 20:02 ` sashiko-bot
2026-06-04 11:49 ` Alice Ryhl
2026-06-03 19:42 ` [PATCH v17 2/6] rust: drm: gem: shmem: Add DmaResvGuard helper Lyude Paul
2026-06-03 20:09 ` sashiko-bot
2026-06-03 19:42 ` [PATCH v17 3/6] rust: drm: gem: shmem: Add vmap functions Lyude Paul
2026-06-03 20:11 ` sashiko-bot
2026-06-03 19:42 ` [PATCH v17 4/6] rust: faux: Allow retrieving a bound Device Lyude Paul
2026-06-03 20:08 ` sashiko-bot
2026-06-04 13:25 ` Danilo Krummrich
2026-06-04 18:48 ` lyude
2026-06-03 19:42 ` [PATCH v17 5/6] rust: sync: Add SetOnce::reset() Lyude Paul
2026-06-03 20:07 ` sashiko-bot [this message]
2026-06-04 11:58 ` Alice Ryhl
2026-06-04 18:53 ` lyude
2026-06-03 19:42 ` [PATCH v17 6/6] rust: drm: gem: Introduce shmem::Object::sg_table() Lyude Paul
2026-06-03 20:12 ` sashiko-bot
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=20260603200706.18CB21F00893@smtp.kernel.org \
--to=sashiko-bot@kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=lyude@redhat.com \
--cc=ojeda@kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
/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