Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp4512350rwd; Sun, 11 Jun 2023 08:59:54 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4a0odofZ2PFnb0jtqvQqi7EzS4qbDzPVFhIcuNqz+mWfMQEVVvLsjuH7tjETpf0WwgM7ZZ X-Received: by 2002:a05:6a20:2590:b0:110:e7bc:b0cb with SMTP id k16-20020a056a20259000b00110e7bcb0cbmr8206891pzd.39.1686499193815; Sun, 11 Jun 2023 08:59:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686499193; cv=none; d=google.com; s=arc-20160816; b=JPOSkslHVrsOTY8OJpGIgTPLq6XQJdu+uPCvJNm7C/cJtHUQzyzyBIYIQIRs3GVtrf eGNuEJWJcK1VyataZmXo2VyaieFdaOkOmmi7s4zfm7LDYyg0Nto0BxMzULSu6k02ANCo GMzt60mFzUeVTWPnFhNgZKwzf3MfA2ZbEJPu/pqDSIgYInTZIQWZvert0RiGxZMh9eQ7 dhrBKfiD1iF/KYoLiuhBQ/XJkowcPRKgcT+heqSSi7QtJvira3RtvkyHrq88Pw2EJYwn 94mR2ojk+egXJjt7DTbgbcS2jl9Ix4mIIpxNab62WePzm8xHqZAZxfKhPZUbR0ezFMhV jWbw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :feedback-id:references:in-reply-to:message-id:subject:cc:from:to :dkim-signature:date; bh=llThXYR/g/KbQkmoR17RlJfSDl2qAR2F4OAIJumA+eM=; b=vB6nlqZkC2JDjKDoKDP5+eLfHWDiFko8QF7OnOTk1BmE89+bS/31cg1UzohEhp9ljr OxwamY5lUVyW3WDGGUfyHP6yLfNZmS6N5f7eCyhB1Qd4go3lAOvqFdY7IE2r8TnuVNt8 s+QUbzlV+po/C8x3BXLe7aAbiUsV+GwHo/z8NdaUfSOOJ0cNjckpGX5TSWgiYw882ndk KUW4FAWZcs0+yblfUU78X7c2NtjGkKFsFVNC8mJgmSXDhhexSOQkdxRLTZeg6fhrXk5u kKQG+ekRhUsfIWFDVcPtSnyexSbDsLx5rlHC01mjNIe/m88njkbjrBVpmOGGNF2W+sI7 1jGg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=g76RGSyw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bt20-20020a632914000000b0051b7b83d6b1si2118149pgb.241.2023.06.11.08.59.38; Sun, 11 Jun 2023 08:59:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=g76RGSyw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231748AbjFKPst (ORCPT + 99 others); Sun, 11 Jun 2023 11:48:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55912 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229624AbjFKPsr (ORCPT ); Sun, 11 Jun 2023 11:48:47 -0400 Received: from mail-40131.protonmail.ch (mail-40131.protonmail.ch [185.70.40.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 54E0113D for ; Sun, 11 Jun 2023 08:48:42 -0700 (PDT) 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 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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