[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <9f9641de-72ea-4aa1-827c-3bc6c97938b0@proton.me>
Date: Mon, 17 Feb 2025 00:21:09 +0000
From: Benno Lossin <benno.lossin@...ton.me>
To: Andreas Hindborg <a.hindborg@...nel.org>, Danilo Krummrich <dakr@...nel.org>, Miguel Ojeda <ojeda@...nel.org>, Alex Gaynor <alex.gaynor@...il.com>, Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>, Björn Roy Baron <bjorn3_gh@...tonmail.com>, Alice Ryhl <aliceryhl@...gle.com>, Trevor Gross <tmgross@...ch.edu>, Joel Becker <jlbec@...lplan.org>, Christoph Hellwig <hch@....de>, Peter Zijlstra <peterz@...radead.org>, Ingo Molnar <mingo@...hat.com>, Will Deacon <will@...nel.org>, Waiman Long <longman@...hat.com>, Fiona Behrens <me@...enk.dev>, Charalampos Mitrodimas <charmitro@...teo.net>
Cc: rust-for-linux@...r.kernel.org, linux-kernel@...r.kernel.org
Subject: Re: [PATCH v2 1/3] rust: sync: change `<Arc<T> as ForeignOwnable>::PointedTo` to `T`
On 07.02.25 15:41, Andreas Hindborg wrote:
> Using `ArcInner` as `PoinedTo` in the `ForeignOwnable` implementation for
> `Arc` is a bit unfortunate. Using `T` as `PointedTo` does not remove any
> functionality, but allows `ArcInner` to be private. Further, it allows
> downstream users to write code that is generic over `Box` and `Arc`, when
> downstream users need access to `T` after calling `into_foreign`.
>
> Reviewed-by: Fiona Behrens <me@...enk.dev>
> Signed-off-by: Andreas Hindborg <a.hindborg@...nel.org>
Two nits below, but this looks good regardless, so:
Reviewed-by: Benno Lossin <benno.lossin@...ton.me>
> ---
>
> This patch is a dependency for Rust `configfs` abstractions. It allows both
> `Box` and `Arc` to be used as pointer types in the `configfs` hierarchy.
> ---
> rust/kernel/sync/arc.rs | 21 ++++++++++++++++-----
> 1 file changed, 16 insertions(+), 5 deletions(-)
>
> diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
> index dfe4abf82c25c..503e318b4c4ef 100644
> --- a/rust/kernel/sync/arc.rs
> +++ b/rust/kernel/sync/arc.rs
> @@ -143,7 +143,7 @@ pub struct Arc<T: ?Sized> {
> #[doc(hidden)]
> #[pin_data]
> #[repr(C)]
> -pub struct ArcInner<T: ?Sized> {
> +struct ArcInner<T: ?Sized> {
> refcount: Opaque<bindings::refcount_t>,
> data: T,
> }
> @@ -345,18 +345,25 @@ pub fn into_unique_or_drop(self) -> Option<Pin<UniqueArc<T>>> {
>
> // SAFETY: The `into_foreign` function returns a pointer that is well-aligned.
> unsafe impl<T: 'static> ForeignOwnable for Arc<T> {
> - type PointedTo = ArcInner<T>;
> + type PointedTo = T;
> type Borrowed<'a> = ArcBorrow<'a, T>;
> type BorrowedMut<'a> = Self::Borrowed<'a>;
>
> fn into_foreign(self) -> *mut Self::PointedTo {
> - ManuallyDrop::new(self).ptr.as_ptr()
> + let x = ManuallyDrop::new(self).ptr.as_ptr();
I would normally name such a variable `this`, but it's fine as-is.
> + // SAFETY: `x` is a valid pointer to `Self` so the projection below is
> + // in bounds of the allocation.
> + unsafe { core::ptr::addr_of_mut!((*x).data) }
> }
>
> unsafe fn from_foreign(ptr: *mut Self::PointedTo) -> Self {
> + // SAFETY: We did the reverse offset calculation in `into_foreign`, so
> + // the offset calculation below is in bounds of the allocation.
> + let inner_ptr = unsafe { kernel::container_of!(ptr, ArcInner<T>, data).cast_mut() };
> +
> // SAFETY: The safety requirements of this function ensure that `ptr` comes from a previous
> // call to `Self::into_foreign`.
> - let inner = unsafe { NonNull::new_unchecked(ptr) };
> + let inner = unsafe { NonNull::new_unchecked(inner_ptr) };
>
> // SAFETY: By the safety requirement of this function, we know that `ptr` came from
> // a previous call to `Arc::into_foreign`, which guarantees that `ptr` is valid and
> @@ -365,9 +372,13 @@ unsafe fn from_foreign(ptr: *mut Self::PointedTo) -> Self {
> }
>
> unsafe fn borrow<'a>(ptr: *mut Self::PointedTo) -> ArcBorrow<'a, T> {
> + // SAFETY: We did the reverse offset calculation in `into_foreign`, so
> + // the offset calculation below is in bounds of the allocation.
> + let inner_ptr = unsafe { kernel::container_of!(ptr, ArcInner<T>, data).cast_mut() };
You could call this variable `ptr` and avoid the change below, but
either way is fine.
---
Cheers,
Benno
> +
> // SAFETY: The safety requirements of this function ensure that `ptr` comes from a previous
> // call to `Self::into_foreign`.
> - let inner = unsafe { NonNull::new_unchecked(ptr) };
> + let inner = unsafe { NonNull::new_unchecked(inner_ptr) };
>
> // SAFETY: The safety requirements of `from_foreign` ensure that the object remains alive
> // for the lifetime of the returned value.
>
> --
> 2.47.0
>
>
Powered by blists - more mailing lists