Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp4327348rwl; Wed, 28 Dec 2022 02:29:38 -0800 (PST) X-Google-Smtp-Source: AMrXdXu5qDuiudKSG6MwzAB+KMK9EipWl4pzLbwSHWpDPr5fFruiXp+V6mcIzJRfkLxoxy52TteR X-Received: by 2002:a05:6402:4518:b0:45c:835c:eab7 with SMTP id ez24-20020a056402451800b0045c835ceab7mr20421964edb.37.1672223378237; Wed, 28 Dec 2022 02:29:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672223378; cv=none; d=google.com; s=arc-20160816; b=sqSBx3uifjo9yFu8QpxH3eweRL0hBOBtnT+QGQCkGQe7Lk9eK5th+Fh37+M2IsEN4p y09Tm13/mlxHHTIkwFxeQDXtPnKjjUB0VjwE5pC4CXLz0P8uG6QQ6ujvycppdakNwKV+ ImouSW0nu6OYLktS6v97PXX4HE22TRveTK6u4lhCQa96wb+o9/QuZlSnjW+L7Bq+nwr/ fOVGG/hm5tK1BfnKyI0Fyk6LHWx8iUSiNAHcJs+H0zQ0liFimd02JUrHmH19nhHnD8Ux ARHpWfuCtYRfySrqCB62TSZpOjSeScZCexImu3JnCsNXzHli7uzSgP5imZPzOxxPF0f4 F0ng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :content-language:references:cc:to:subject:from:user-agent :mime-version:date:message-id:feedback-id:dkim-signature :dkim-signature; bh=MZcrtkpn45hSZzHoYj+0v76Q/VMqCGY1GazRzbBpn54=; b=0zmRnESyu+RyRdSKgkKrdO/fglkvIG7+P/viBx6QS3xG7/fB3OTcj6jS/sey6u28bI AtFJUUEiTWZ6AHB8fAgy/wRK9fBeHxsbedDP0cA0n3XV/YWVqiR9Ab5ZbMn7mYTk9rCr 6MevIQV/lj9DhAoBGOOxT1wZvnsdMLZs10LoXPqJlffdNEcC0Owjese6khcwIb7OhaYi XOtLBqNbyA0HzXUeGlG7ab/rAopkDad9PBRsaWdsckSY/Wtm+CEUfvf1hq0uNWiBYnPU y95mpACHYyRVTUB5rhu9NADu+4Ed0A88jx5MJSMGq9QOXwD7flEtBsFjqI4I5f+2zqzY S7Aw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ryhl.io header.s=fm2 header.b=JiBFMf95; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=Qrp8ZhQ5; 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=NONE sp=NONE dis=NONE) header.from=ryhl.io Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y13-20020a056402270d00b004690009024csi13879145edd.503.2022.12.28.02.29.22; Wed, 28 Dec 2022 02:29:38 -0800 (PST) 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=@ryhl.io header.s=fm2 header.b=JiBFMf95; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=Qrp8ZhQ5; 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=NONE sp=NONE dis=NONE) header.from=ryhl.io Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232680AbiL1KCs (ORCPT + 64 others); Wed, 28 Dec 2022 05:02:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53060 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232767AbiL1KC3 (ORCPT ); Wed, 28 Dec 2022 05:02:29 -0500 Received: from out2-smtp.messagingengine.com (out2-smtp.messagingengine.com [66.111.4.26]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D1498101C8; Wed, 28 Dec 2022 02:02:22 -0800 (PST) Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailout.nyi.internal (Postfix) with ESMTP id 1D8455C0158; Wed, 28 Dec 2022 05:02:21 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Wed, 28 Dec 2022 05:02:21 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ryhl.io; h=cc:cc :content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm2; t=1672221741; x= 1672308141; bh=MZcrtkpn45hSZzHoYj+0v76Q/VMqCGY1GazRzbBpn54=; b=J iBFMf95/cnvdReHC+AtNTdVyIUiJ3oUK4LSNd2QgwA2gITVceh0yE6bRREeAlflv fALA14ePw7rEPvaH1+GHhNKj3i3MX+KxiJZTKYMQ486sSHipg9hh0CEvWOGo6dCG yy7BaVmb/93rvwH6vX/NRDDCeuqUbyeaMwmab32HF3G/tK2NcFDgpQc11vUsDxak 9oxS5Kmne3qIJmc7+KDJSA4rwWeFME7oeZ5LIxjJnxPtyWLyxSNx258TmTkRfHi5 me49o8FME+Ijo5xUtgysDzLuAa1ueO0l/Ia1HCb1ZYAFIpcRAHNKLjCqDCBwwyVb +RMnbrnHE0jtGpJbjhobg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1672221741; x= 1672308141; bh=MZcrtkpn45hSZzHoYj+0v76Q/VMqCGY1GazRzbBpn54=; b=Q rp8ZhQ51iPKrtKhUUc5TfhLgC2dXbpDiHU1luE6V8AQCZVFMllsWAPiWpIgzGl/5 Q21OJfNd2NFmb5IBRxPCcjItA5XIkvdIgzdpgZw0N6GUKvwfwEV1bcfSoBbcsMA3 xdCtxh64V9/ETL8IhLtJW19JdmgZNoo5d4LO1R1IofVH9WAt8x29A8pxuXduV0h+ Wje76bTvbw0p17OTy8FML4flvhCWG/aCRHsEJy2RSD471m+VD2sECNZt89wjFIGU /2xygPq70G3lUh4okA4VwKMhwUrkzChkvGTjxA7M/OmT0XVJEdSCCH0F8DG30gdJ RKE2Vzhufv0UptCzzYhdA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedriedvgdduudcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefkffggfgfhuffvvehfjggtgfesthejredttdefjeenucfhrhhomheptehlihgt vgcutfihhhhluceorghlihgtvgesrhihhhhlrdhioheqnecuggftrfgrthhtvghrnhepvd evhfegtddtledvudfgfeegjeevudefjefhieekieevtdfhgfffudekudfhudeknecuffho mhgrihhnpehinhhnvghrrdgrshenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmh epmhgrihhlfhhrohhmpegrlhhitggvsehrhihhlhdrihho X-ME-Proxy: Feedback-ID: i56684263:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 28 Dec 2022 05:02:18 -0500 (EST) Message-ID: <254be618-67f3-f17c-e01b-e0389c8008bc@ryhl.io> Date: Wed, 28 Dec 2022 11:03:54 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.5.1 From: Alice Ryhl Subject: Re: [PATCH 4/7] rust: sync: introduce `ArcBorrow` To: Wedson Almeida Filho Cc: rust-for-linux@vger.kernel.org, Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?Q?Bj=c3=b6rn_Roy_Baron?= , linux-kernel@vger.kernel.org References: <20221228060346.352362-1-wedsonaf@gmail.com> <20221228060346.352362-4-wedsonaf@gmail.com> Content-Language: en-US In-Reply-To: <20221228060346.352362-4-wedsonaf@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS 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 Reviewed-by: Alice Ryhl On 12/28/22 07:03, Wedson Almeida Filho wrote: > This allows us to create references to a ref-counted allocation without > double-indirection and that still allow us to increment the refcount to > a new `Arc`. > > Signed-off-by: Wedson Almeida Filho > --- > rust/kernel/sync.rs | 2 +- > rust/kernel/sync/arc.rs | 97 +++++++++++++++++++++++++++++++++++++++++ > 2 files changed, 98 insertions(+), 1 deletion(-) > > diff --git a/rust/kernel/sync.rs b/rust/kernel/sync.rs > index 39b379dd548f..5de03ea83ea1 100644 > --- a/rust/kernel/sync.rs > +++ b/rust/kernel/sync.rs > @@ -7,4 +7,4 @@ > > mod arc; > > -pub use arc::Arc; > +pub use arc::{Arc, ArcBorrow}; > diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs > index dbc7596cc3ce..f68bfc02c81a 100644 > --- a/rust/kernel/sync/arc.rs > +++ b/rust/kernel/sync/arc.rs > @@ -19,6 +19,7 @@ use crate::{bindings, error::Result, types::Opaque}; > use alloc::boxed::Box; > use core::{ > marker::{PhantomData, Unsize}, > + mem::ManuallyDrop, > ops::Deref, > ptr::NonNull, > }; > @@ -164,6 +165,18 @@ impl Arc { > _p: PhantomData, > } > } > + > + /// Returns an [`ArcBorrow`] from the given [`Arc`]. > + /// > + /// This is useful when the argument of a function call is an [`ArcBorrow`] (e.g., in a method > + /// receiver), but we have an [`Arc`] instead. Getting an [`ArcBorrow`] is free when optimised. > + #[inline] > + pub fn as_arc_borrow(&self) -> ArcBorrow<'_, T> { > + // SAFETY: The constraint that the lifetime of the shared reference must outlive that of > + // the returned `ArcBorrow` ensures that the object remains alive and that no mutable > + // reference can be created. > + unsafe { ArcBorrow::new(self.ptr) } > + } > } > > impl Deref for Arc { > @@ -208,3 +221,87 @@ impl Drop for Arc { > } > } > } > + > +/// A borrowed reference to an [`Arc`] instance. > +/// > +/// For cases when one doesn't ever need to increment the refcount on the allocation, it is simpler > +/// to use just `&T`, which we can trivially get from an `Arc` instance. > +/// > +/// However, when one may need to increment the refcount, it is preferable to use an `ArcBorrow` > +/// over `&Arc` because the latter results in a double-indirection: a pointer (shared reference) > +/// to a pointer (`Arc`) to the object (`T`). An [`ArcBorrow`] eliminates this double > +/// indirection while still allowing one to increment the refcount and getting an `Arc` when/if > +/// needed. > +/// > +/// # Invariants > +/// > +/// There are no mutable references to the underlying [`Arc`], and it remains valid for the > +/// lifetime of the [`ArcBorrow`] instance. > +/// > +/// # Example > +/// > +/// ``` > +/// use crate::sync::{Arc, ArcBorrow}; > +/// > +/// struct Example; > +/// > +/// fn do_something(e: ArcBorrow<'_, Example>) -> Arc { > +/// e.into() > +/// } > +/// > +/// let obj = Arc::try_new(Example)?; > +/// let cloned = do_something(obj.as_arc_borrow()); > +/// > +/// // Assert that both `obj` and `cloned` point to the same underlying object. > +/// assert!(core::ptr::eq(&*obj, &*cloned)); > +/// ``` > +pub struct ArcBorrow<'a, T: ?Sized + 'a> { > + inner: NonNull>, > + _p: PhantomData<&'a ()>, > +} > + > +impl Clone for ArcBorrow<'_, T> { > + fn clone(&self) -> Self { > + *self > + } > +} > + > +impl Copy for ArcBorrow<'_, T> {} > + > +impl ArcBorrow<'_, T> { > + /// Creates a new [`ArcBorrow`] instance. > + /// > + /// # Safety > + /// > + /// Callers must ensure the following for the lifetime of the returned [`ArcBorrow`] instance: > + /// 1. That `inner` remains valid; > + /// 2. That no mutable references to `inner` are created. > + unsafe fn new(inner: NonNull>) -> Self { > + // INVARIANT: The safety requirements guarantee the invariants. > + Self { > + inner, > + _p: PhantomData, > + } > + } > +} > + > +impl From> for Arc { > + fn from(b: ArcBorrow<'_, T>) -> Self { > + // SAFETY: The existence of `b` guarantees that the refcount is non-zero. `ManuallyDrop` > + // guarantees that `drop` isn't called, so it's ok that the temporary `Arc` doesn't own the > + // increment. > + ManuallyDrop::new(unsafe { Arc::from_inner(b.inner) }) > + .deref() > + .clone() > + } > +} > + > +impl Deref for ArcBorrow<'_, T> { > + type Target = T; > + > + fn deref(&self) -> &Self::Target { > + // SAFETY: By the type invariant, the underlying object is still alive with no mutable > + // references to it, so it is safe to create a shared reference. > + unsafe { &self.inner.as_ref().data } > + } > +}