Received: by 2002:ab2:5d18:0:b0:1ef:7a0f:c32d with SMTP id j24csp156937lqk; Sat, 9 Mar 2024 04:56:27 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVk+ziwgLrjTRYIBW+gNgqN/qwlA4UE1bv79x9FrnUEJzgSsRCXRts3TbM/eazMPB2uor063hWxrXk9R1hcavvaghuiaZYYskDrF79k0w== X-Google-Smtp-Source: AGHT+IEMoDHh2EauRD6q93MMXz0RqStbnzZIO+uU0YWpedHNnM9TsQrScL71fPKYeAs6/PcfBf8D X-Received: by 2002:a05:6808:1485:b0:3c1:e1b2:7974 with SMTP id e5-20020a056808148500b003c1e1b27974mr2792222oiw.26.1709988987420; Sat, 09 Mar 2024 04:56:27 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709988987; cv=pass; d=google.com; s=arc-20160816; b=wLqvv3bFAFIgU5Yjo9T8HCE0xUfXdeii+mpCxoOk7dmQ4awxX9v1swAQJu+Srr4C91 iZogEOCVUZL95rzSakwytlG1slqCy+fbmJK2oiUyoZQhTUwQ3AQdTje9H33pOACmVDKa B3zsIn+iYBTUKJcFZDVzkppxZU44yVzpYQMt8/qBxEVORwFzDUZsw1nSoBzKzgQrqHhp G7+YkkASM5qBudxBtUVGaFvYJevqqXpQ/nm5lwBafG3Dc4Hd4ad+uFI1TYaqCq8W2tLB KxvWEkomFdTntJoysGQ9VcZ6+6mnBZyOQ+bvGMwW8ou7kjTgWoH2pUFeHwpsEsxy6sMN abuQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:feedback-id:references :in-reply-to:message-id:subject:cc:from:to:date:dkim-signature; bh=QKG7Og4r8P88E0N1YInQ/WJ0SZGm9r7NkKPA5RX/JQg=; fh=nBIU09dkdw1pFNvOl0hziI6raxaJ3qXyfxUzWt0uves=; b=Ys7cEkcl0BflGXqw/6kUgIxuior3jjrdZ81oyGjCnSNZDuVP/he3N0hQiE7aZNFS08 23CGaxUPow/MzE148aE8tjng6C6a/EdsnVzWiUlx/qK8qykPJjwV5TTyrdVo7pLICmvT Wf2US3TDE7Yt0i18hOWQ7g4QbjQ0ni+3ZLOqflltHeFbSwHgtuNyHiMg/Kco2QFCmZOx x+SIC3hfjRilMoF6JXCIpnQn6UxrbGKKvDzOnfZF4Jsln0pf3Q80Lzf9qF5BMDDV1X/c yRoWp86Z/lxo8K9ICdekvNGrt0Ol/r6R60tbOcIA6MCXplUZkcNxeKt69UNX4eWg5KHv Vzww==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=VKslXP3A; arc=pass (i=1 spf=pass spfdomain=proton.me dkim=pass dkdomain=proton.me dmarc=pass fromdomain=proton.me); spf=pass (google.com: domain of linux-kernel+bounces-97891-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-97891-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id t18-20020a639552000000b005dcb4f1a3easi1420633pgn.180.2024.03.09.04.56.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 09 Mar 2024 04:56:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-97891-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=VKslXP3A; arc=pass (i=1 spf=pass spfdomain=proton.me dkim=pass dkdomain=proton.me dmarc=pass fromdomain=proton.me); spf=pass (google.com: domain of linux-kernel+bounces-97891-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-97891-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 2068BB21100 for ; Sat, 9 Mar 2024 12:56:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 95CCE3BBE7; Sat, 9 Mar 2024 12:56:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b="VKslXP3A" Received: from mail-4322.protonmail.ch (mail-4322.protonmail.ch [185.70.43.22]) (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 0FD61364C0; Sat, 9 Mar 2024 12:56:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.43.22 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709988971; cv=none; b=HrEa25f4O7D0wYgfPQGVXyZ9DvCp+7Hh6qDhxt6pVbk2dcr4Dabg2g4hqsLDbjEDakWquoQwDlcUMse6Fd8cVvPIGJxHcP+4FltDvfxF2H2cVGL6QHKlcjI7wUM7wh8ECvLa1CeFt8alNCKqIj7Jgtb3lW7Zknv7kTAlgjnAHUQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709988971; c=relaxed/simple; bh=ElLfEFnJ6xG3gz8E1eLHKtA0poaBTMqxkUvb3LCp40o=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=uWzrMrzLi88SxVQ6cdMZTla0USrrbtAw94HTxGpHTYHY2ADROPav/em4PW7ZfndZAUQEo3eQzAe6RRADzewKRJvyX1YTAJOg8h51mhXqfpsmhe/SGTxd3TeXSTtCoR4s7VmSUtwBgBsCSJPKOy9sZX2JJsT4pvnXC3i02GIRbps= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me; spf=pass smtp.mailfrom=proton.me; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b=VKslXP3A; arc=none smtp.client-ip=185.70.43.22 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=proton.me DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1709988963; x=1710248163; bh=QKG7Og4r8P88E0N1YInQ/WJ0SZGm9r7NkKPA5RX/JQg=; 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=VKslXP3A5rB0GXrXOtmAgqr7iSu5DaPUaZbdRIsukN7+B5A70FJTiY12TndIf1/0U EqOemI7ofG401W9P8rTCnamYpigDpnmdSExQLtB+DqduIQgp+v1/kdTkyYqZDmH2fV NsNxwhAFtKKRQDmk1hX76+KzbCqiGjuoHED2Yn5sjIjQj8HqZgPXefOtNGQTSVnn1/ tCw6oSkV4brRjyiet/DaQPh5yvT6ke8y0avvV3eov3sqKVLMb3fJPge9Rma2gtuAoB ixd6KhPY4PogSNy3jjkbrmQNY1OMNBNPuzgyHljkt+H7mRJVq1JJA8ietmlllGu3ES sRMpixq5h0iCA== Date: Sat, 09 Mar 2024 12:55:35 +0000 To: Alice Ryhl , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Andreas Hindborg From: Benno Lossin Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 1/2] rust: sync: add `ArcBorrow::from_raw` Message-ID: In-Reply-To: <20240228-arc-for-list-v2-1-ae93201426b4@google.com> References: <20240228-arc-for-list-v2-0-ae93201426b4@google.com> <20240228-arc-for-list-v2-1-ae93201426b4@google.com> Feedback-ID: 71780778:user:proton Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 2/28/24 14:00, Alice Ryhl wrote: > Allows access to a value in an `Arc` that is currently held as a raw > pointer due to use of `Arc::into_raw`, without destroying or otherwise > consuming that raw pointer. >=20 > This is a dependency of the linked list that Rust Binder uses. The > linked list uses this method when iterating over the linked list. >=20 > Signed-off-by: Alice Ryhl > --- > rust/kernel/sync/arc.rs | 76 +++++++++++++++++++++++++++++++++++++-----= ------- > 1 file changed, 58 insertions(+), 18 deletions(-) >=20 > diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs > index 7d4c4bf58388..53addb8876c2 100644 > --- a/rust/kernel/sync/arc.rs > +++ b/rust/kernel/sync/arc.rs > @@ -137,6 +137,39 @@ struct ArcInner { > data: T, > } >=20 > +impl ArcInner { > + /// Converts a pointer to the contents of an [`Arc`] into a pointer = to the [`ArcInner`]. > + /// > + /// # Safety > + /// > + /// `ptr` must have been returned by a previous call to [`Arc::into_= raw`], and the `Arc` must > + /// not yet have been destroyed. > + unsafe fn container_of(ptr: *const T) -> NonNull> { > + let refcount_layout =3D Layout::new::(); > + // SAFETY: The caller guarantees that the pointer is valid. > + let val_layout =3D Layout::for_value(unsafe { &*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 }; > + > + // Pointer casts leave the metadata unchanged. This is okay beca= use the metadata of `T` and > + // `ArcInner` is the same since `ArcInner` is a struct with `= T` as its last field. > + // > + // This is documented at: > + // . > + let ptr =3D ptr as *const ArcInner; > + > + // SAFETY: The pointer is in-bounds of an allocation both before= and after offsetting the > + // pointer, since it originates from a previous call to `Arc::in= to_raw` on an `Arc` that is > + // still valid. > + let ptr =3D unsafe { ptr.byte_sub(val_offset) }; > + > + // SAFETY: The pointer can't be null since you can't have an `Ar= cInner` value at the null > + // address. > + unsafe { NonNull::new_unchecked(ptr.cast_mut()) } > + } > +} > + > // This is to allow [`Arc`] (and variants) to be used as the type of `s= elf`. > impl core::ops::Receiver for Arc {} >=20 > @@ -232,27 +265,13 @@ pub fn into_raw(self) -> *const T { > /// `ptr` must have been returned by a previous call to [`Arc::into= _raw`]. Additionally, it > /// must not be called more than once for each previous call to [`A= rc::into_raw`]. > 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 Layout::for_value(unsafe { &*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 }; > - > - // Pointer casts leave the metadata unchanged. This is okay beca= use the metadata of `T` and > - // `ArcInner` is the same since `ArcInner` is a struct with `= T` as its last field. > - // > - // This is documented at: > - // . > - let ptr =3D ptr as *const ArcInner; > - > - // SAFETY: The pointer is in-bounds of an allocation both before= and after offsetting the > - // pointer, since it originates from a previous call to `Arc::in= to_raw` and is still valid. > - let ptr =3D unsafe { ptr.byte_sub(val_offset) }; > + // SAFETY: The caller promises that this pointer originates from= a call to `into_raw` on an > + // `Arc` that is still valid. > + let ptr =3D unsafe { ArcInner::container_of(ptr) }; >=20 > // SAFETY: By the safety requirements we know that `ptr` came f= rom `Arc::into_raw`, so the > // reference count held then will be owned by the new `Arc` obj= ect. > - unsafe { Self::from_inner(NonNull::new_unchecked(ptr.cast_mut())= ) } > + unsafe { Self::from_inner(ptr) } > } >=20 > /// Returns an [`ArcBorrow`] from the given [`Arc`]. > @@ -453,6 +472,27 @@ unsafe fn new(inner: NonNull>) -> Self { > _p: PhantomData, > } > } > + > + /// Creates an [`ArcBorrow`] to an [`Arc`] that has previously been = deconstructed with > + /// [`Arc::into_raw`]. > + /// > + /// # Safety > + /// > + /// * The provided pointer must originate from a call to [`Arc::into= _raw`]. > + /// * For the duration of the lifetime annotated on this `ArcBorrow`= , the reference count must > + /// not hit zero. > + /// * For the duration of the lifetime annotated on this `ArcBorrow`= , there must not be a > + /// [`UniqueArc`] reference to this value. I am a bit confused, this feels to me like it should be guaranteed by `UniqueArc` and not by this function. Currently there is not even a way of getting a `*const T` from a `UniqueArc`. So I think we can remove this requirement and instead have the requirement for creating `UniqueArc` that not only the refcount is exactly 1, but also that no `ArcBorrow` exists. --=20 Cheers, Benno > + pub unsafe fn from_raw(ptr: *const T) -> Self { > + // SAFETY: The caller promises that this pointer originates from= a call to `into_raw` on an > + // `Arc` that is still valid. > + let ptr =3D unsafe { ArcInner::container_of(ptr) }; > + > + // SAFETY: The caller promises that the value remains valid sinc= e the reference count must > + // not hit zero, and no mutable reference will be created since = that would involve a > + // `UniqueArc`. > + unsafe { Self::new(ptr) } > + } > } >=20 > impl From> for Arc { >=20 > -- > 2.44.0.rc1.240.g4c46232300-goog >=20