From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-40134.protonmail.ch (mail-40134.protonmail.ch [185.70.40.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2BBAFC126 for ; Sun, 11 Jun 2023 15:48:42 +0000 (UTC) Date: Sun, 11 Jun 2023 15:48:36 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1686498520; x=1686757720; bh=llThXYR/g/KbQkmoR17RlJfSDl2qAR2F4OAIJumA+eM=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=g76RGSywGB6xjzPyW6VilVGaHlM7ho5EX2WpRqG99kgZWilxwRCfzqVLT2W6o274F hRVuaInH2oI/6DujaUJjG0rCtWBNjKAiXjr/pLtr3oI29QAQsyihS0cyeCxReuQglm VjbecqsZjI0HuPBP/V//q4BKPwUI1DksRhlJzo/48OHPKL9TsYoZFejq+xqjtWTwHr uD7l55KIffo5GLfHE7qCj0Om49ZamNqoqTalUiDWqxDh5bJPZxmB/Moq8ZVLX1YTGH qoXDjZzTfef0nzYthtmsnPo+6dkWGz3yMwN8E3e7sgVW8iXXLjiTq3WKBJDfC5DCnd eEle7bJ7aInHQ== To: Alice Ryhl From: Benno Lossin Cc: rust-for-linux@vger.kernel.org, Miguel Ojeda , Wedson Almeida Filho , Tejun Heo , Lai Jiangshan , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , linux-kernel@vger.kernel.org, patches@lists.linux.dev, Wedson Almeida Filho , Martin Rodriguez Reboredo Subject: Re: [PATCH v2 3/8] rust: sync: add `Arc::{from_raw, into_raw}` Message-ID: In-Reply-To: <20230601134946.3887870-4-aliceryhl@google.com> References: <20230601134946.3887870-1-aliceryhl@google.com> <20230601134946.3887870-4-aliceryhl@google.com> Feedback-ID: 71780778:user:proton Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 01.06.23 15:49, Alice Ryhl wrote: > From: Wedson Almeida Filho >=20 > These methods can be used to turn an `Arc` into a raw pointer and back, > in a way that preserves the metadata for fat pointers. >=20 > This is done using the unstable ptr_metadata feature [1]. However, it > could also be done using the unstable pointer_byte_offsets feature [2], > which is likely to have a shorter path to stabilization than > ptr_metadata. >=20 > Link: https://github.com/rust-lang/rust/issues/81513 [1] > Link: https://github.com/rust-lang/rust/issues/96283 [2] > Signed-off-by: Wedson Almeida Filho > Co-developed-by: Alice Ryhl > Signed-off-by: Alice Ryhl > Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Benno Lossin --=20 Cheers, Benno > --- > rust/kernel/lib.rs | 1 + > rust/kernel/sync/arc.rs | 42 ++++++++++++++++++++++++++++++++++++++++- > 2 files changed, 42 insertions(+), 1 deletion(-) >=20 > diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs > index 7ea777b731e6..ad9142928fb1 100644 > --- a/rust/kernel/lib.rs > +++ b/rust/kernel/lib.rs > @@ -17,6 +17,7 @@ > #![feature(const_refs_to_cell)] > #![feature(dispatch_from_dyn)] > #![feature(new_uninit)] > +#![feature(ptr_metadata)] > #![feature(receiver_trait)] > #![feature(unsize)] >=20 > diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs > index a89843cacaad..684be9f73aca 100644 > --- a/rust/kernel/sync/arc.rs > +++ b/rust/kernel/sync/arc.rs > @@ -24,7 +24,7 @@ > }; > use alloc::boxed::Box; > use core::{ > - alloc::AllocError, > + alloc::{AllocError, Layout}, > fmt, > marker::{PhantomData, Unsize}, > mem::{ManuallyDrop, MaybeUninit}, > @@ -212,6 +212,46 @@ unsafe fn from_inner(inner: NonNull>) ->= Self { > } > } >=20 > + /// Convert the [`Arc`] into a raw pointer. > + /// > + /// The raw pointer has ownership of the refcount that this Arc obje= ct owned. > + pub fn into_raw(self) -> *const T { > + let ptr =3D self.ptr.as_ptr(); > + core::mem::forget(self); > + // SAFETY: The pointer is valid. > + unsafe { core::ptr::addr_of!((*ptr).data) } > + } > + > + /// Recreates an [`Arc`] instance previously deconstructed via [`Arc= ::into_raw`]. > + /// > + /// This code relies on the `repr(C)` layout of structs as described= in > + /// . > + /// > + /// # Safety > + /// > + /// `ptr` must have been returned by a previous call to [`Arc::into_= raw`]. Additionally, it > + /// can only be called once for each previous call to [`Arc::into_ra= w`]. > + pub unsafe fn from_raw(ptr: *const T) -> Self { > + let refcount_layout =3D Layout::new::(); > + // SAFETY: The caller guarantees that the pointer is valid. > + let val_layout =3D unsafe { Layout::for_value(&*ptr) }; > + // SAFETY: We're computing the layout of a real struct that exis= ted when compiling this > + // binary, so its layout is not so large that it can trigger ari= thmetic overflow. > + let val_offset =3D unsafe { refcount_layout.extend(val_layout).u= nwrap_unchecked().1 }; > + > + // This preserves the metadata in the pointer, if any. > + // > + // Note that `*const T` and `*const ArcInner` have the same m= etadata as documented at > + // . > + let metadata =3D core::ptr::metadata(ptr as *const ArcInner); > + let ptr =3D (ptr as *mut u8).wrapping_sub(val_offset) as *mut ()= ; > + let ptr =3D core::ptr::from_raw_parts_mut(ptr, metadata); > + > + // SAFETY: By the safety requirements we know that `ptr` came fr= om `Arc::into_raw`, so the > + // reference count held then will be owned by the new `Arc` obje= ct. > + unsafe { Self::from_inner(NonNull::new_unchecked(ptr)) } > + } > + > /// Returns an [`ArcBorrow`] from the given [`Arc`]. > /// > /// This is useful when the argument of a function call is an [`Arc= Borrow`] (e.g., in a method > -- > 2.41.0.rc0.172.g3f132b7071-goog >=20