From: Andreas Hindborg <a.hindborg@kernel.org>
To: "Alice Ryhl" <aliceryhl@google.com>
Cc: "Oliver Mangold" <oliver.mangold@pm.me>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <benno.lossin@proton.me>,
"Trevor Gross" <tmgross@umich.edu>,
"Asahi Lina" <lina@asahilina.net>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v10 1/5] rust: types: Add Ownable/Owned types
Date: Tue, 06 May 2025 13:20:04 +0200 [thread overview]
Message-ID: <87ecx2q93v.fsf@kernel.org> (raw)
In-Reply-To: <aBSsrLGSJgLGTViT@google.com> (Alice Ryhl's message of "Fri, 02 May 2025 11:29:48 +0000")
"Alice Ryhl" <aliceryhl@google.com> writes:
> On Fri, May 02, 2025 at 09:02:29AM +0000, Oliver Mangold wrote:
>> From: Asahi Lina <lina@asahilina.net>
>>
>> By analogy to AlwaysRefCounted and ARef, an Ownable type is a (typically
>> C FFI) type that *may* be owned by Rust, but need not be. Unlike
>> AlwaysRefCounted, this mechanism expects the reference to be unique
>> within Rust, and does not allow cloning.
>>
>> Conceptually, this is similar to a KBox<T>, except that it delegates
>> resource management to the T instead of using a generic allocator.
>>
>> Link: https://lore.kernel.org/all/20250202-rust-page-v1-1-e3170d7fe55e@asahilina.net/
>> Signed-off-by: Asahi Lina <lina@asahilina.net>
>> [ om:
>> - split code into separate file and `pub use` it from types.rs
>> - make from_raw() and into_raw() public
>> - fixes to documentation
>> ]
>> Signed-off-by: Oliver Mangold <oliver.mangold@pm.me>
>> Reviewed-by: Boqun Feng <boqun.feng@gmail.com>
>> ---
>> rust/kernel/types.rs | 3 ++
>> rust/kernel/types/ownable.rs | 117 +++++++++++++++++++++++++++++++++++++++++++
>> 2 files changed, 120 insertions(+)
>>
>> diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
>> index 9d0471afc9648f2973235488b441eb109069adb1..5d8a99dcba4bf733107635bf3f0c15840ec33e4c 100644
>> --- a/rust/kernel/types.rs
>> +++ b/rust/kernel/types.rs
>> @@ -11,6 +11,9 @@
>> };
>> use pin_init::{PinInit, Zeroable};
>>
>> +pub mod ownable;
>> +pub use ownable::{Ownable, OwnableMut, Owned};
>> +
>> /// Used to transfer ownership to and from foreign (non-Rust) languages.
>> ///
>> /// Ownership is transferred from Rust to a foreign language by calling [`Self::into_foreign`] and
>> diff --git a/rust/kernel/types/ownable.rs b/rust/kernel/types/ownable.rs
>> new file mode 100644
>> index 0000000000000000000000000000000000000000..52e7a69019f1e2bbbe3cf715651b67a5a5c7c13d
>> --- /dev/null
>> +++ b/rust/kernel/types/ownable.rs
>> @@ -0,0 +1,117 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +
>> +//! Owned reference types.
>> +
>> +use core::{
>> + marker::PhantomData,
>> + mem::ManuallyDrop,
>> + ops::{Deref, DerefMut},
>> + ptr::NonNull,
>> +};
>> +
>> +/// Types that may be owned by Rust code or borrowed, but have a lifetime managed by C code.
>> +///
>> +/// It allows such types to define their own custom destructor function to be called when
>> +/// a Rust-owned reference is dropped.
>> +///
>> +/// This is usually implemented by wrappers to existing structures on the C side of the code.
>> +///
>> +/// # Safety
>> +///
>> +/// Implementers must ensure that:
>> +/// - Any objects owned by Rust as [`Owned<T>`] stay alive while that owned reference exists (i.e.
>> +/// until the [`release()`](Ownable::release) trait method is called).
>> +/// - That the C code follows the usual mutable reference requirements. That is, the kernel will
>> +/// never mutate the [`Ownable`] (excluding internal mutability that follows the usual rules)
>> +/// while Rust owns it.
>
> This seems too strong? Or does the exception mean to say that this does
> not apply to anything containing `Opaque`? By far most structs using
> this will use Opaque, so maybe directly mention Opaque instead?
`Opaque` is covered by "(excluding internal mutability that follows the usual rules)".
>
> That C code follows the usual aliasing rules. That is, unless the value
> is wrapped in `Opaque` (or `UnsafeCell`), then the value must not be
> modified in any way while Rust owns it, unless that modification happens
> inside a `&mut T` method on the value.
>
>> +pub unsafe trait Ownable {
>> + /// Releases the object (frees it or returns it to foreign ownership).
>> + ///
>> + /// # Safety
>> + ///
>> + /// Callers must ensure that the object is no longer referenced after this call.
>> + unsafe fn release(this: NonNull<Self>);
>> +}
>> +
>> +/// A subtrait of Ownable that asserts that an [`Owned<T>`] or `&mut Owned<T>` Rust reference
>> +/// may be dereferenced into a `&mut T`.
>> +///
>> +/// # Safety
>> +///
>> +/// Implementers must ensure that access to a `&mut T` is safe, implying that it is okay to call
>> +/// [`core::mem::swap`] on the `Ownable`. This excludes pinned types (meaning: most kernel types).
>> +pub unsafe trait OwnableMut: Ownable {}
>> +
>> +/// An owned reference to an ownable kernel object.
>> +///
>> +/// The object is automatically freed or released when an instance of [`Owned`] is
>> +/// dropped.
>> +///
>> +/// # Invariants
>> +///
>> +/// The pointer stored in `ptr` is valid for the lifetime of the [`Owned`] instance.
>
> This should probably talk about ownership.
How about
The pointee of `ptr` can be considered owned by the [`Owned`] instance.
Best regards,
Andreas Hindborg
next prev parent reply other threads:[~2025-05-06 11:20 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-02 9:02 [PATCH v10 0/5] New trait OwnableRefCounted for ARef<->Owned conversion Oliver Mangold
2025-05-02 9:02 ` [PATCH v10 1/5] rust: types: Add Ownable/Owned types Oliver Mangold
2025-05-02 9:57 ` Andreas Hindborg
2025-06-16 11:43 ` Oliver Mangold
2025-06-17 11:42 ` Andreas Hindborg
2025-05-02 11:29 ` Alice Ryhl
2025-05-06 11:20 ` Andreas Hindborg [this message]
2025-05-07 6:20 ` Alice Ryhl
2025-05-08 12:24 ` Andreas Hindborg
2025-05-14 9:32 ` Benno Lossin
2025-06-17 9:58 ` Oliver Mangold
2025-06-18 21:22 ` Benno Lossin
2025-06-20 7:01 ` Oliver Mangold
2025-06-20 8:09 ` Benno Lossin
2025-06-18 9:34 ` Oliver Mangold
2025-06-18 21:19 ` Benno Lossin
2025-06-19 9:33 ` Andreas Hindborg
2025-06-19 12:18 ` Benno Lossin
2025-05-02 9:02 ` [PATCH v10 2/5] rust: Rename AlwaysRefCounted to RefCounted Oliver Mangold
2025-05-02 10:10 ` Andreas Hindborg
2025-05-02 11:32 ` Alice Ryhl
2025-06-16 11:56 ` Oliver Mangold
2025-06-16 12:13 ` Alice Ryhl
2025-05-02 9:02 ` [PATCH v10 3/5] rust: Add missing SAFETY documentation for ARef example Oliver Mangold
2025-05-02 10:41 ` Andreas Hindborg
2025-05-02 11:12 ` Oliver Mangold
2025-05-02 12:01 ` Andreas Hindborg
2025-05-02 9:02 ` [PATCH v10 4/5] rust: kbuild: provide `RUSTC_HAS_DO_NOT_RECOMMEND` symbol Oliver Mangold
2025-05-02 9:03 ` [PATCH v10 5/5] rust: Add OwnableRefCounted and SimpleOwnableRefCounted Oliver Mangold
2025-05-02 11:43 ` Alice Ryhl
2025-05-06 11:42 ` Oliver Mangold
2025-05-07 6:19 ` Alice Ryhl
2025-06-13 13:10 ` [PATCH v10 0/5] New trait OwnableRefCounted for ARef<->Owned conversion Andreas Hindborg
2025-06-13 13:27 ` Oliver Mangold
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=87ecx2q93v.fsf@kernel.org \
--to=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=gary@garyguo.net \
--cc=lina@asahilina.net \
--cc=linux-kernel@vger.kernel.org \
--cc=ojeda@kernel.org \
--cc=oliver.mangold@pm.me \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
/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).