From: Gary Guo <gary@garyguo.net>
To: Tamir Duberstein <tamird@gmail.com>
Cc: "Danilo Krummrich" <dakr@kernel.org>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <benno.lossin@proton.me>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Matthew Wilcox" <willy@infradead.org>,
"Bjorn Helgaas" <bhelgaas@google.com>,
"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
"Rafael J. Wysocki" <rafael@kernel.org>,
"FUJITA Tomonori" <fujita.tomonori@gmail.com>,
"Rob Herring (Arm)" <robh@kernel.org>,
"Maíra Canal" <mcanal@igalia.com>,
"Asahi Lina" <lina@asahilina.net>,
rust-for-linux@vger.kernel.org, linux-fsdevel@vger.kernel.org,
linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org
Subject: Re: [PATCH v19 1/3] rust: types: add `ForeignOwnable::PointedTo`
Date: Wed, 30 Apr 2025 19:31:12 +0100 [thread overview]
Message-ID: <20250430193112.4faaff3d.gary@garyguo.net> (raw)
In-Reply-To: <20250423-rust-xarray-bindings-v19-1-83cdcf11c114@gmail.com>
On Wed, 23 Apr 2025 09:54:37 -0400
Tamir Duberstein <tamird@gmail.com> wrote:
> Allow implementors to specify the foreign pointer type; this exposes
> information about the pointed-to type such as its alignment.
>
> This requires the trait to be `unsafe` since it is now possible for
> implementors to break soundness by returning a misaligned pointer.
>
> Encoding the pointer type in the trait (and avoiding pointer casts)
> allows the compiler to check that implementors return the correct
> pointer type. This is preferable to directly encoding the alignment in
> the trait using a constant as the compiler would be unable to check it.
>
> Acked-by: Danilo Krummrich <dakr@kernel.org>
> Signed-off-by: Tamir Duberstein <tamird@gmail.com>
> ---
> rust/kernel/alloc/kbox.rs | 38 ++++++++++++++++++++------------------
> rust/kernel/miscdevice.rs | 10 +++++-----
> rust/kernel/pci.rs | 2 +-
> rust/kernel/platform.rs | 2 +-
> rust/kernel/sync/arc.rs | 21 ++++++++++++---------
> rust/kernel/types.rs | 46 +++++++++++++++++++++++++++++++---------------
> 6 files changed, 70 insertions(+), 49 deletions(-)
>
> diff --git a/rust/kernel/alloc/kbox.rs b/rust/kernel/alloc/kbox.rs
> index b77d32f3a58b..6aa88b01e84d 100644
> --- a/rust/kernel/alloc/kbox.rs
> +++ b/rust/kernel/alloc/kbox.rs
> @@ -360,68 +360,70 @@ fn try_init<E>(init: impl Init<T, E>, flags: Flags) -> Result<Self, E>
> }
> }
>
> -impl<T: 'static, A> ForeignOwnable for Box<T, A>
> +// SAFETY: The `into_foreign` function returns a pointer that is well-aligned.
> +unsafe impl<T: 'static, A> ForeignOwnable for Box<T, A>
> where
> A: Allocator,
> {
> + type PointedTo = T;
I don't think this is the correct solution for this. The returned
pointer is supposed to opaque, and exposing this type may encourage
this is to be wrongly used.
IIUC, the only reason for this to be exposed is for XArray to be able
to check alignment. However `align_of::<PointedTo>()` is not the
minimum guaranteed alignment.
For example, if the type is allocated via kernel allocator then it can
always guarantee to be at least usize-aligned. ZST can be arbitrarily
aligned so ForeignOwnable` implementation could return a
validly-aligned pointer for XArray. Actually, I think all current
ForeignOwnable implementation can be modified to always give 4-byte
aligned pointers.
Having a const associated item indicating the minimum guaranteed
alignment for *that specific container* is the correct way IMO, not to
reason about the pointee type!
Best,
Gary
> type Borrowed<'a> = &'a T;
> type BorrowedMut<'a> = &'a mut T;
>
> - fn into_foreign(self) -> *mut crate::ffi::c_void {
> - Box::into_raw(self).cast()
> + fn into_foreign(self) -> *mut Self::PointedTo {
> + Box::into_raw(self)
> }
>
> - unsafe fn from_foreign(ptr: *mut crate::ffi::c_void) -> Self {
> + unsafe fn from_foreign(ptr: *mut Self::PointedTo) -> Self {
> // SAFETY: The safety requirements of this function ensure that `ptr` comes from a previous
> // call to `Self::into_foreign`.
> - unsafe { Box::from_raw(ptr.cast()) }
> + unsafe { Box::from_raw(ptr) }
> }
>
> - unsafe fn borrow<'a>(ptr: *mut crate::ffi::c_void) -> &'a T {
> + unsafe fn borrow<'a>(ptr: *mut Self::PointedTo) -> &'a T {
> // SAFETY: The safety requirements of this method ensure that the object remains alive and
> // immutable for the duration of 'a.
> - unsafe { &*ptr.cast() }
> + unsafe { &*ptr }
> }
>
> - unsafe fn borrow_mut<'a>(ptr: *mut crate::ffi::c_void) -> &'a mut T {
> - let ptr = ptr.cast();
> + unsafe fn borrow_mut<'a>(ptr: *mut Self::PointedTo) -> &'a mut T {
> // SAFETY: The safety requirements of this method ensure that the pointer is valid and that
> // nothing else will access the value for the duration of 'a.
> unsafe { &mut *ptr }
> }
> }
next prev parent reply other threads:[~2025-04-30 18:31 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-23 13:54 [PATCH v19 0/3] rust: xarray: Add a minimal abstraction for XArray Tamir Duberstein
2025-04-23 13:54 ` [PATCH v19 1/3] rust: types: add `ForeignOwnable::PointedTo` Tamir Duberstein
2025-04-30 18:31 ` Gary Guo [this message]
2025-04-30 18:57 ` Tamir Duberstein
2025-05-01 7:17 ` Alice Ryhl
2025-05-01 8:07 ` Andreas Hindborg
2025-04-23 13:54 ` [PATCH v19 2/3] rust: xarray: Add an abstraction for XArray Tamir Duberstein
2025-04-23 13:54 ` [PATCH v19 3/3] MAINTAINERS: add entry for Rust XArray API Tamir Duberstein
2025-04-26 19:48 ` [PATCH v19 0/3] rust: xarray: Add a minimal abstraction for XArray Andreas Hindborg
2025-04-27 14:39 ` Danilo Krummrich
2025-04-27 15:57 ` Andreas Hindborg
2025-04-28 7:14 ` 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=20250430193112.4faaff3d.gary@garyguo.net \
--to=gary@garyguo.net \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=benno.lossin@proton.me \
--cc=bhelgaas@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=fujita.tomonori@gmail.com \
--cc=gregkh@linuxfoundation.org \
--cc=lina@asahilina.net \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=mcanal@igalia.com \
--cc=ojeda@kernel.org \
--cc=rafael@kernel.org \
--cc=robh@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tamird@gmail.com \
--cc=tmgross@umich.edu \
--cc=willy@infradead.org \
/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