Linux PCI subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Andreas Hindborg" <a.hindborg@kernel.org>
Cc: ojeda@kernel.org, dri-devel@lists.freedesktop.org,
	gary@garyguo.net, linux-pci@vger.kernel.org
Subject: Re: [PATCH v21 5/9] rust: Add missing SAFETY documentation for `ARef` example
Date: Thu, 10 Sep 2026 09:07:44 +0000	[thread overview]
Message-ID: <20260910090744.E91C91F000FF@smtp.kernel.org> (raw)
In-Reply-To: <20260910-unique-ref-v21-5-e83257373062@kernel.org>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The newly added SAFETY comments justify an unsound implementation of `RefCounted` by pushing the trait's liveness invariants onto the caller.
--

commit 368d99cfa43beab2cba1151e1e06c503881e1ab5
Author: Oliver Mangold <oliver.mangold@pm.me>

rust: Add missing SAFETY documentation for `ARef` example

This commit replaces a TODO comment with a detailed SAFETY comment in the
rustdoc example for ARef, explaining why the dummy RefCounted implementation
and subsequent ARef creation are considered safe.

Link: https://github.com/Rust-for-Linux/linux/issues/351

> diff --git a/rust/kernel/sync/aref.rs b/rust/kernel/sync/aref.rs
> index 215e7eb139fac..ae76bd9b6c1c9 100644
> --- a/rust/kernel/sync/aref.rs
> +++ b/rust/kernel/sync/aref.rs
> @@ -147,7 +147,10 @@ pub unsafe fn from_raw(ptr: NonNull<T>) -> Self {
>      ///
>      /// struct Empty {}
>      ///
> -    /// # // SAFETY: TODO.
> +    /// // SAFETY: The `RefCounted` implementation for `Empty` does not count references, and
> +    /// // `dec_ref` never frees the underlying object, so a decrement cannot invalidate it. The
> +    /// // object instead lives as long as the `Empty` value itself, so creators of `ARef<Empty>`
> +    /// // must guarantee that the value outlives every `ARef` derived from it (as done below).

[Severity: Medium]
Does this pattern violate the safety contract of the RefCounted trait?

The documentation teaches an approach where creators of ARef<Empty> must
guarantee that the value outlives every ARef derived from it. Since ARef<T>
provides a safe Clone method and does not carry a lifetime parameter, it is
impossible for the creator to enforce this guarantee if the ARef is cloned
and sent to another thread.

If a developer copies this reasoning to implement RefCounted for a type
where inc_ref does not actually maintain liveness, could a cloned ARef
outlive the underlying object and cause a use-after-free vulnerability in
safe Rust?

>      /// unsafe impl RefCounted for Empty {
>      ///     fn inc_ref(&self) {}
>      ///     unsafe fn dec_ref(_obj: NonNull<Self>) {}
> @@ -155,7 +158,9 @@ pub unsafe fn from_raw(ptr: NonNull<T>) -> Self {
>      ///
>      /// let mut data = Empty {};
>      /// let ptr = NonNull::<Empty>::new(&mut data).unwrap();
> -    /// # // SAFETY: TODO.
> +    /// // SAFETY: As the refcount operations of `Empty` are no-ops, we can treat `ptr` as owning
> +    /// // an increment on the refcount. `data` outlives the created `ARef`, upholding the
> +    /// // liveness guarantee required by the `RefCounted` implementation above.

[Severity: Medium]
Since the liveness guarantee mentioned in the RefCounted implementation above
cannot be statically enforced by the caller during safe ARef clones, does
this initialization rely on an unsound guarantee?

>      /// let data_ref: ARef<Empty> = unsafe { ARef::from_raw(ptr) };
>      /// let raw_ptr: NonNull<Empty> = ARef::into_raw(data_ref);
>      ///

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260910-unique-ref-v21-0-e83257373062@kernel.org?part=5

  reply	other threads:[~2026-09-10  9:07 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-10  9:00 [PATCH v21 0/9] rust: add `Ownable` trait and `Owned` type Andreas Hindborg
2026-09-10  9:00 ` [PATCH v21 1/9] rust: alloc: add `KBox::into_non_null` Andreas Hindborg
2026-09-10  9:05   ` sashiko-bot
2026-09-10  9:00 ` [PATCH v21 2/9] rust: types: Add Ownable/Owned types Andreas Hindborg
2026-09-10  9:06   ` sashiko-bot
2026-09-10  9:00 ` [PATCH v21 3/9] rust: implement `ForeignOwnable` for `Owned` Andreas Hindborg
2026-09-10  9:08   ` sashiko-bot
2026-09-10  9:00 ` [PATCH v21 4/9] rust: rename `AlwaysRefCounted` to `RefCounted` Andreas Hindborg
2026-09-10  9:14   ` sashiko-bot
2026-09-11  9:08   ` Alice Ryhl
2026-09-11 12:29     ` Gary Guo
2026-09-13  9:49       ` Andreas Hindborg
2026-09-10  9:00 ` [PATCH v21 5/9] rust: Add missing SAFETY documentation for `ARef` example Andreas Hindborg
2026-09-10  9:07   ` sashiko-bot [this message]
2026-09-11  9:09   ` Alice Ryhl
2026-09-10  9:00 ` [PATCH v21 6/9] rust: Add `OwnableRefCounted` Andreas Hindborg
2026-09-10  9:09   ` sashiko-bot
2026-09-11  9:19   ` Alice Ryhl
2026-09-11 13:25     ` Andreas Hindborg
2026-09-10  9:00 ` [PATCH v21 7/9] rust: page: convert to `AlwaysRefCounted` Andreas Hindborg
2026-09-10  9:18   ` sashiko-bot
2026-09-11 13:43   ` Alice Ryhl
2026-09-10  9:00 ` [PATCH v21 8/9] rust: page: add `from_raw()` Andreas Hindborg
2026-09-10  9:11   ` sashiko-bot
2026-09-10  9:00 ` [PATCH v21 9/9] rust: page: add `ExclusivePage` for race-free page access Andreas Hindborg
2026-09-10  9:14   ` sashiko-bot
2026-09-13  3:11   ` Matthew Wilcox
2026-09-13  9:36     ` Andreas Hindborg

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=20260910090744.E91C91F000FF@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=a.hindborg@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=gary@garyguo.net \
    --cc=linux-pci@vger.kernel.org \
    --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