All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Andreas Hindborg" <a.hindborg@kernel.org>
Cc: dri-devel@lists.freedesktop.org, ojeda@kernel.org,
	linux-pci@vger.kernel.org
Subject: Re: [PATCH v19 6/8] rust: Add missing SAFETY documentation for `ARef` example
Date: Fri, 26 Jun 2026 12:02:38 +0000	[thread overview]
Message-ID: <20260626120238.A799F1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260626-unique-ref-v19-6-2607ca88dfdf@kernel.org>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The newly added SAFETY comments in the rustdoc example incorrectly justify an unsound trait implementation that violates the safety invariants of `RefCounted`.
--

commit 8c6c2bf519778f423165d5e3c916d3965bebb093
Author: Oliver Mangold <oliver.mangold@pm.me>

rust: Add missing SAFETY documentation for ARef example

This commit replaces TODO safety comments in the rustdoc example for
ARef::into_raw with explanations of why the unsafe operations are permitted.

> diff --git a/rust/kernel/sync/aref.rs b/rust/kernel/sync/aref.rs
> index ea5a16b8163a6..f26ca39b84d0d 100644
> --- a/rust/kernel/sync/aref.rs
> +++ b/rust/kernel/sync/aref.rs
[ ... ]
> @@ -142,7 +142,9 @@ pub unsafe fn from_raw(ptr: NonNull<T>) -> Self {
>      ///
>      /// struct Empty {}
>      ///
> -    /// # // SAFETY: TODO.
> +    /// // SAFETY: The `RefCounted` implementation for `Empty` does not count references and never
> +    /// // frees the underlying object. Thus we can act as owning an increment on the refcount for
> +    /// // the object that we pass to the newly created `ARef`.
>      /// unsafe impl RefCounted for Empty {
>      ///     fn inc_ref(&self) {}
>      ///     unsafe fn dec_ref(_obj: NonNull<Self>) {}

[Severity: Medium]
Does this explanation violate the safety invariants of the RefCounted trait?

The RefCounted trait strictly requires that increments to the reference count
keep the object alive, and that all instances are reference-counted.

By using a no-op inc_ref and dec_ref, this example demonstrates an
unsound implementation of the trait, which might lead developers to
introduce use-after-free bugs if they copy this pattern into production
code.

>      /// }
>      ///
>      /// let mut data = Empty {};
>      /// let ptr = NonNull::<Empty>::new(&mut data).unwrap();
> -    /// # // SAFETY: TODO.
> +    /// // SAFETY: We keep `data` around longer than the `ARef`.
>      /// let data_ref: ARef<Empty> = unsafe { ARef::from_raw(ptr) };

[Severity: Medium]
Is it safe to rely on stack-variable lifetimes here to bypass the normal
reference counting expectations?

ARef::from_raw and the RefCounted trait expect the object's lifetime to be
handled inherently via reference counting. Relying on keeping a stack
variable around longer than the ARef bypasses ARef::from_raw safety
preconditions.

Could this documentation lead others to incorrectly apply ARef to stack
variables?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260626-unique-ref-v19-0-2607ca88dfdf@kernel.org?part=6

  reply	other threads:[~2026-06-26 12:02 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-26 11:53 [PATCH v19 0/8] rust: add `Ownable` trait and `Owned` type Andreas Hindborg
2026-06-26 11:53 ` [PATCH v19 1/8] rust: alloc: add `KBox::into_non_null` Andreas Hindborg
2026-06-26 12:01   ` sashiko-bot
2026-06-26 11:53 ` [PATCH v19 2/8] rust: types: Add Ownable/Owned types Andreas Hindborg
2026-06-26 12:06   ` sashiko-bot
2026-06-26 11:54 ` [PATCH v19 3/8] rust: implement `ForeignOwnable` for `Owned` Andreas Hindborg
2026-06-26 12:07   ` sashiko-bot
2026-06-26 11:54 ` [PATCH v19 4/8] rust: page: convert to `Ownable` Andreas Hindborg
2026-06-26 12:05   ` sashiko-bot
2026-06-26 11:54 ` [PATCH v19 5/8] rust: rename `AlwaysRefCounted` to `RefCounted` Andreas Hindborg
2026-06-26 12:06   ` sashiko-bot
2026-06-26 11:54 ` [PATCH v19 6/8] rust: Add missing SAFETY documentation for `ARef` example Andreas Hindborg
2026-06-26 12:02   ` sashiko-bot [this message]
2026-06-26 11:54 ` [PATCH v19 7/8] rust: Add `OwnableRefCounted` Andreas Hindborg
2026-06-26 12:06   ` sashiko-bot
2026-06-26 11:54 ` [PATCH v19 8/8] rust: page: add `from_raw()` Andreas Hindborg
2026-06-26 12:06   ` 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=20260626120238.A799F1F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=a.hindborg@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.