From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from GBR01-CWL-obe.outbound.protection.outlook.com (mail-cwlgbr01on2096.outbound.protection.outlook.com [40.107.11.96]) (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 B59E11F178 for ; Mon, 5 Jun 2023 14:31:47 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ceTZapZecnYDzmhl8xrXAoH+f97dbeM2Rjpib/6k+wGOi197tjNRL4T2BVRZcqwOrPa27R5ictUHdE711h/ICHg6aOryit6JLQFvIXOsoM/fKi6JovtkH4OTXLzH9IoQoXmBQia0VsvqQceGgjVvKwnDiLYzsTPrpMXVU2LZYRpDTltz9a3ttJvFmSBacBrbd5L4Qmk0HMUxmLS3/FIu2ntS/bzLTUF4yJoMi0eheOk1vyIRvk1EOn0WFf+61gHhrJqkPCBlbqqniIwbhAcwfYkLd/TzCo+1tnSR+pGRg9+9q4cE/hOfIaBMY8IArt9owS93pT0jMWbImwYmC3jrKw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Cog1w8AFtoEz+eixFO5Ja8Qcpiuhzcl8huW5fcmX+v0=; b=is7h0IIKQx8FmbfAjrOYjeQDnAjonfgaKQaKMlQmKSexuAlbyQOGtvm+aNl6fiTX9nX782Rh1NJ0GRgv6kr3GvnLiy824/XEr/5LU1pDe22snSryFc/g/ozSMoEPPjjz3gZJ7MmN5TW0mdXJpNytAcU0Lg0C5r64U3uxdSFehp0FWuoPA/u2DPCRN5v4qO8lrW9vSUjeEPvJ9MH/4hgrDunHJmkJPaIl1B+UhCglBgaLDn47Rmois8aqDSjQY2JoLY2UVMfZi71grEdcEwYbhHMmYHUSYq5awDoDHFAJRcwARwEWfMSGSdu3Gs1GMlvLUz/ZSN97xs13p5y7GziwRg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=garyguo.net; dmarc=pass action=none header.from=garyguo.net; dkim=pass header.d=garyguo.net; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=garyguo.net; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Cog1w8AFtoEz+eixFO5Ja8Qcpiuhzcl8huW5fcmX+v0=; b=MQ6PJce8OKgNP0FdtmieHrsvWmHlcmAQd8ChT2KQ4BEHXQSnuX//sV9K3HxBKCQTtwnQu0DN2/Ms6Fsh36cfGOjp73Me/gv8yql9wia0/gKts36vjhHxUHBt+/SzZy+0vm7cg1/rDICvY94aZEg4K8zC96lZUWAAeMOw+wEbTM0= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=garyguo.net; Received: from LO2P265MB5183.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:253::10) by LOYP265MB2335.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:120::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.19; Mon, 5 Jun 2023 14:31:44 +0000 Received: from LO2P265MB5183.GBRP265.PROD.OUTLOOK.COM ([fe80::2e6d:c339:6006:9013]) by LO2P265MB5183.GBRP265.PROD.OUTLOOK.COM ([fe80::2e6d:c339:6006:9013%5]) with mapi id 15.20.6477.016; Mon, 5 Jun 2023 14:31:44 +0000 Date: Mon, 5 Jun 2023 15:31:42 +0100 From: Gary Guo To: "Andreas Hindborg (Samsung)" Cc: Alice Ryhl , rust-for-linux@vger.kernel.org, Miguel Ojeda , Wedson Almeida Filho , Tejun Heo , Lai Jiangshan , Alex Gaynor , Boqun Feng , =?UTF-8?B?QmrDtnJu?= Roy Baron , Benno Lossin , 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: <20230605153142.28a4093c.gary@garyguo.net> In-Reply-To: <87v8g61119.fsf@metaspace.dk> References: <20230601134946.3887870-1-aliceryhl@google.com> <20230601134946.3887870-4-aliceryhl@google.com> <87v8g61119.fsf@metaspace.dk> X-Mailer: Claws Mail 4.0.0 (GTK+ 3.24.33; x86_64-pc-linux-gnu) Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-ClientProxiedBy: LO6P123CA0039.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:2fe::19) To LO2P265MB5183.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:253::10) Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LO2P265MB5183:EE_|LOYP265MB2335:EE_ X-MS-Office365-Filtering-Correlation-Id: a808b2f4-17f5-4f7f-82a9-08db65d19620 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: K/+0ImnjldYYGGlxoQh3IqWHCEU66spDR5AAyrKZ8QZoIx+xfzDxJeOqc5K8jntMFmrCJe8WPJCe4kp2+rg/yVmnGQHMT1FPPGHs7ecd+WKsVI5GM5ZGKJ/DwBiZBBtUf98f0wEhqWHOE5RG5KK9NLPyAawb239iT2Rwxr4GQAO2QjfAa9CFTwAD1H81WONV6lomxqAxTaxJGRSzxduHfdTaNjcLWrwujUu8pexADQuBu4MThKSS13RKLs2SVEVwvEL6p3t1Gm9OTTsvi4b2F3bgDSLDC7lv7mEgU37P0VbB5V6/43TUbpLcGW0Sh3IVD4uMft2/LQtQIpSGceBkjLYJsiLWfPt6EfqB/FTK2SaR7CMhWn23AoXQFCqz6T3RWcDCbkNFzVktEmWao/aF+voSXJNsrDaVYXIu5HcPOtYFpM6kMdlVl8MPEihSoe24N1g+wd40fa8rzKGkPTWQL2UePI//sLKQJLIeeO8YJdGyOyqElMj4qT++tx89PbYhFqeyFCHDKAPlurBQwkoVbclzVTl2mxq/SYdFr3LdRCcN51vQU5uj/zIaWC+uGy1o/Lnihi/yjsVMKQi3K9kMcPtLc5NtnW+0m1IbAF7VV6c= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LO2P265MB5183.GBRP265.PROD.OUTLOOK.COM;PTR:;CAT:NONE;SFS:(13230028)(39830400003)(136003)(376002)(366004)(396003)(346002)(451199021)(6506007)(26005)(1076003)(6512007)(186003)(2616005)(966005)(7416002)(83380400001)(6486002)(36756003)(2906002)(8676002)(8936002)(54906003)(478600001)(4326008)(5660300002)(38100700002)(6916009)(86362001)(45080400002)(316002)(41300700001)(66556008)(66476007)(66946007)(81973001);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?0n6YMDhg9HjybfuFTt2XuCqCBN8NLXKv8OIWLFr3T8kKg3c9vIm55/EsbY5T?= =?us-ascii?Q?rr/TpBUFx3dfGALAP7qYzhfrcsvX+58O0r61lYvsPmzA+dfq/o/R3ZlxC70Q?= =?us-ascii?Q?Ht9q0Kwaw/2N5sI7gYQAs9u817g+bNtIoZ/p4ce5G86d7Kv//KhBNWQ3QJSu?= =?us-ascii?Q?Gj+VQmJtXnefn5AUTTC4TXbZ6RxzS46qR4ZQABtQbfLdY5jfCpkNm7WD82+f?= =?us-ascii?Q?jRM2hAQW2hAsZ+VvViBVaADK0Gb+ohgMbH53JWgyVIoCVXUwkBwywX2lkM1M?= =?us-ascii?Q?rkSczkgranWUbrGhCeHKbSGnMp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Mirva9eLfBob43fSPlXo6saQ9Zmfx1XYXoTKCLKx62TB7v/6g8w3y6TMzao?= =?us-ascii?Q?CTYIRAIFWSwr3Se5hlF87yhuyt4vO1yo8txFoijsxyxzpQMvd3mfHT58DAR0?= =?us-ascii?Q?cppyRYdySIvC0hGf9PzJAfWNS4kGFpW+T2PxuQA34rpLjtgY3MtBdtUkqB25?= =?us-ascii?Q?5bf7KHuTGRq5XBf8xSrWMA5YF+aTuD0FaxHGTE/ivHi+jxnO+jlsBPUlzUjK?= =?us-ascii?Q?R9N81QQxctA4dF6++6Vg88SJtIKVNca/LBZYiFUiij88knydJ9gr/mFnk0hm?= =?us-ascii?Q?hckItGH6ZGyS3OasPisyZQiL8b+jrhbWMt+i+cQY0WeVeiB5MWuD8OGD0EfP?= =?us-ascii?Q?/AFNkFEQr+LZTsSflkpTsf2aGK/9F6kHxSR4gtrQ?= X-OriginatorOrg: garyguo.net X-MS-Exchange-CrossTenant-Network-Message-Id: a808b2f4-17f5-4f7f-82a9-08db65d19620 X-MS-Exchange-CrossTenant-AuthSource: LO2P265MB5183.GBRP265.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 Jun 2023 14:31:44.5908 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: bbc898ad-b10f-4e10-8552-d9377b823d45 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: hbyLmsMAYsnU8YUS3k1fHLBXAXfXiPSSzvw81huymZ0IEnSJU15WjodYrel61ALSWNe8ebP3QMsJmDJ+x7BFyQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: LOYP265MB2335 On Fri, 02 Jun 2023 12:51:08 +0200 "Andreas Hindborg (Samsung)" wrote: > Alice Ryhl writes: > > > From: Wedson Almeida Filho > > > > 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. > > > > 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. > > > > 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 > > --- > > rust/kernel/lib.rs | 1 + > > rust/kernel/sync/arc.rs | 42 ++++++++++++++++++++++++++++++++++++++++- > > 2 files changed, 42 insertions(+), 1 deletion(-) > > > > 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)] > > > > 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 { > > } > > } > > > > + /// Convert the [`Arc`] into a raw pointer. > > + /// > > + /// The raw pointer has ownership of the refcount that this Arc object owned. > > + pub fn into_raw(self) -> *const T { > > + let ptr = 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_raw`]. > > + pub unsafe fn from_raw(ptr: *const T) -> Self { > > + let refcount_layout = Layout::new::(); > > + // SAFETY: The caller guarantees that the pointer is valid. > > + let val_layout = unsafe { Layout::for_value(&*ptr) }; > > + // SAFETY: We're computing the layout of a real struct that existed when compiling this > > + // binary, so its layout is not so large that it can trigger arithmetic overflow. > > + let val_offset = unsafe { refcount_layout.extend(val_layout).unwrap_unchecked().1 }; > > + > > + // This preserves the metadata in the pointer, if any. > > + // > > + // Note that `*const T` and `*const ArcInner` have the same metadata as documented at > > + // . > > + let metadata = core::ptr::metadata(ptr as *const ArcInner); > > Thanks for updating the comment with the link. I looked into this and I > find that what we are doing here, even though it works, does not feel > right at all. We should be able to do this: > > let metadata = core::ptr::metadata(ptr); > let ptr = (ptr as *mut u8).wrapping_sub(val_offset) as *mut (); > let ptr = core::ptr::from_raw_parts_mut(ptr, metadata); > > but the way `Pointee::Metadata` is defined will not allow this, even > though we know it is valid. I would suggest the following instead: > > let metadata = core::ptr::metadata(ptr); > // Convert ::Metadata to as > // Pointee>::Metadata. We know they have identical representation and thus this is OK. > let metadata: as Pointee>::Metadata = *unsafe { > &*((&metadata as *const ::Metadata as *const ()) > as *const as Pointee>::Metadata) > }; This could just be a `transmute_copy`. > let ptr = (ptr as *mut u8).wrapping_sub(val_offset) as *mut (); > let ptr = core::ptr::from_raw_parts_mut(ptr, metadata); > > Even though it is a bit more complex, it captures what we are trying to > do better. I agree this captures the semantics better. Best, Gary