From: sashiko-bot@kernel.org
To: "Andreas Hindborg" <a.hindborg@kernel.org>
Cc: dri-devel@lists.freedesktop.org, linux-pci@vger.kernel.org,
ojeda@kernel.org
Subject: Re: [PATCH v18 2/8] rust: types: Add Ownable/Owned types
Date: Thu, 25 Jun 2026 10:24:49 +0000 [thread overview]
Message-ID: <20260625102450.7611D1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260625-unique-ref-v18-2-4e06b5896d47@kernel.org>
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] `Owned<T>` unconditionally structurally pins `T` by exposing `Pin<&mut T>`, but its API lacks the necessary documentation and safety bounds to enforce the pinning invariants. Specifically, `Owned::from_raw(ptr)` takes ownership without requiring the caller to promise the memory is or can be pinned; `Owned::into_raw(me)` safely extracts the pointer without documenting that the returned pointer's referent is still pinned; and `Ownable::release(this)` doesn't forbid the implementer from moving the pinned `Self` before dropping it. This omission allows an `Ownable` implementer or a user correctly following the explicit safety contracts to accidentally move a pinned `!Unpin` type, resulting in undefined behavior.
--
commit b7329ea2c0525ffa6a63171ede0921ad2e6a23e5
Author: Asahi Lina <lina+kernel@asahilina.net>
rust: types: Add Ownable/Owned types
This commit introduces Ownable and Owned types for unique owned pointers
with custom drop logic. These types allow C-allocated objects owned by Rust
to be freed through the C API. It also updates documentation and adds usage
examples.
Link: https://lore.kernel.org/all/20250202-rust-page-v1-1-e3170d7fe55e@asahilina.net/
> diff --git a/rust/kernel/owned.rs b/rust/kernel/owned.rs
> new file mode 100644
> index 0000000000000..7fe9ec3e55126
> --- /dev/null
> +++ b/rust/kernel/owned.rs
[ ... ]
> +pub trait Ownable {
> + /// Tear down this `Ownable`.
> + ///
> + /// Implementers of `Ownable` can use this function to clean up the use of `Self`. This can
> + /// include freeing the underlying object.
> + ///
> + /// # Safety
> + ///
> + /// Callers must ensure that they have exclusive ownership of the `Self` pointed to by `this`,
> + /// and that this ownership is transferred to the `release` method. `this` must not be used
> + /// after calling this method, as the underlying object may have been freed.
> + unsafe fn release(this: NonNull<Self>);
[Severity: High]
Since Owned<T> treats T as pinned (exposing Pin<&mut T> via as_pin_mut()),
should the safety contract here also require that implementers treat `this`
as pointing to pinned memory?
If an implementer moves the value during cleanup (for example, by reading it
onto the stack), it could violate the pinning guarantees for !Unpin types and
cause memory corruption due to self-reference invalidation.
> +}
[ ... ]
> +impl<T: Ownable> Owned<T> {
> + /// Creates a new instance of [`Owned`].
> + ///
> + /// This function takes over ownership of the underlying object.
> + ///
> + /// # Safety
> + ///
> + /// Callers must ensure that:
> + /// - `ptr` points to a valid instance of `T`.
> + /// - Until `T::release` is called, the returned `Owned<T>` exclusively owns the underlying `T`.
> + #[inline]
> + pub unsafe fn from_raw(ptr: NonNull<T>) -> Self {
[Severity: High]
Does this safety contract need to require that the pointer is permanently
pinned?
Because Owned<T> can expose a pinned mutable reference, callers should be
aware that they cannot assume the memory remains unpinned after calling this
function.
> + // INVARIANT: By function safety requirement we satisfy the first invariant of `Self`.
> + // We treat `T` as pinned from now on.
> + Self { ptr }
> + }
> +
> + /// Consumes the [`Owned`], returning a raw pointer.
> + ///
> + /// This function does not drop the underlying `T`. When this function returns, ownership of the
> + /// underlying `T` is with the caller.
> + #[inline]
> + pub fn into_raw(me: Self) -> NonNull<T> {
[Severity: High]
Since into_raw() safely strips the Owned wrapper, should there be
documentation stating that the caller remains bound by the pin invariant
established by Owned?
Without this, a caller might extract the pointer and safely move the
previously pinned object, resulting in undefined behavior.
> + ManuallyDrop::new(me).ptr
> + }
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260625-unique-ref-v18-0-4e06b5896d47@kernel.org?part=2
next prev parent reply other threads:[~2026-06-25 10:24 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-25 10:15 [PATCH v18 0/8] rust: add `Ownable` trait and `Owned` type Andreas Hindborg
2026-06-25 10:15 ` [PATCH v18 1/8] rust: alloc: add `KBox::into_non_null` Andreas Hindborg
2026-06-25 10:22 ` sashiko-bot
2026-06-25 10:15 ` [PATCH v18 2/8] rust: types: Add Ownable/Owned types Andreas Hindborg
2026-06-25 10:24 ` sashiko-bot [this message]
2026-06-25 12:24 ` Andreas Hindborg
2026-06-25 10:15 ` [PATCH v18 3/8] rust: implement `ForeignOwnable` for `Owned` Andreas Hindborg
2026-06-25 10:23 ` sashiko-bot
2026-06-25 13:29 ` Gary Guo
2026-06-25 19:47 ` Andreas Hindborg
2026-06-25 10:15 ` [PATCH v18 4/8] rust: page: convert to `Ownable` Andreas Hindborg
2026-06-25 10:25 ` sashiko-bot
2026-06-25 13:32 ` Gary Guo
2026-06-25 10:15 ` [PATCH v18 5/8] rust: rename `AlwaysRefCounted` to `RefCounted` Andreas Hindborg
2026-06-25 10:28 ` sashiko-bot
2026-06-25 12:26 ` Andreas Hindborg
2026-06-25 10:15 ` [PATCH v18 6/8] rust: Add missing SAFETY documentation for `ARef` example Andreas Hindborg
2026-06-25 10:21 ` sashiko-bot
2026-06-25 10:15 ` [PATCH v18 7/8] rust: Add `OwnableRefCounted` Andreas Hindborg
2026-06-25 10:26 ` sashiko-bot
2026-06-25 12:37 ` Andreas Hindborg
2026-06-25 10:15 ` [PATCH v18 8/8] rust: page: add `from_raw()` Andreas Hindborg
2026-06-25 10:25 ` sashiko-bot
2026-06-25 13:02 ` 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=20260625102450.7611D1F000E9@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox