Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp313494rdh; Sat, 23 Sep 2023 11:27:41 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHkI3iTg7+IL6l0KdI+9CDVU1AjJQQFva/SfJERbydvN1rwgBj69pKQKjFVsUwS6E7k3zJ3 X-Received: by 2002:a05:6a20:938e:b0:153:8754:8a7e with SMTP id x14-20020a056a20938e00b0015387548a7emr3464240pzh.3.1695493661514; Sat, 23 Sep 2023 11:27:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695493661; cv=none; d=google.com; s=arc-20160816; b=lf1k2GzPU4/OfZEuMTBkJJjZ9uFd4iUz2PrPwA8opwVVf4nwrVKemEZEz266QoazQF I39iRWCBk2Yv2D7VfS+sc2VJV9dWafvWcS4yMTIhSOVK0Xx5kDDxD0msGUr5GqxArI1A OdNcXG1Rso+VotStC4ggd6y0LUiNq/aEaX2aZdxkw7L/2sQneIdWecE8DD4LyhJvmyA8 1KIawdSkJ2tEPU5cGZkjDJfq9vxkgSRyUrvtTYwdcUQJWSoxSgL0zXDkgGeDjWHkzdsI SNtW8J8wFbw8lVpiiKNWTDMR3nFezWV2hA0C65YlFmfgQrvDC041LQTIM6MP6kb8keox k1Fg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=I27QSkJfyuEvUa4n1zGSchXIF6bDq2xv+lKsYDTk2zk=; fh=QkY2SKPdSGBrklGsinoO03N5xC+EX21m/4RoIR1QxBg=; b=qWZes/PH+KQxbIiNPQNNVEyMsuKvxbjldL2ODFrw54yiQ6r1ObzzSKlzvnMSdnXOGo s98MPDxANYT/Z8bs5emLinMroeCG/OIRZF6yRUg7AbeWb0l04rDfJEeniHZP7/MMdqIa 2ShkQB2rLywHUA242UApSrGRiOeTC8ht8MfiL29WiiH7Bp8Jk4+j7a82bCCVUKFDrM8n Ony0xR4tpJHFeIz0dppcONFW9B09f7BP82Wtb9DmtGp72qC8VlFJgNnwdkaQPd4UCZ/F u9imRgWtIzpcRKPcWsPpdverM6yDh4CbjGPCTfm2t8q4X0xE/OjNCNAtPjBb7nR47P9D 46KQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=VRhcd8lM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id m124-20020a633f82000000b005774635c69dsi6291992pga.66.2023.09.23.11.27.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Sep 2023 11:27:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=VRhcd8lM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 5A5B98326383; Sat, 23 Sep 2023 07:13:09 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231638AbjIWONI (ORCPT + 99 others); Sat, 23 Sep 2023 10:13:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34336 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231307AbjIWONH (ORCPT ); Sat, 23 Sep 2023 10:13:07 -0400 Received: from mail-yb1-xb2a.google.com (mail-yb1-xb2a.google.com [IPv6:2607:f8b0:4864:20::b2a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 030059E; Sat, 23 Sep 2023 07:13:00 -0700 (PDT) Received: by mail-yb1-xb2a.google.com with SMTP id 3f1490d57ef6-d81d09d883dso4226392276.0; Sat, 23 Sep 2023 07:12:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1695478379; x=1696083179; darn=vger.kernel.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=I27QSkJfyuEvUa4n1zGSchXIF6bDq2xv+lKsYDTk2zk=; b=VRhcd8lMPjvpl6/P/4Zql1FSa3ViHDbMxyYhfns7H4Bsn2nlMhSdc+w++OEc9yue5X rhSfjdYM87IaYjlBA+xZTBH82AFuWpZUmKBuZNXyUrF8ew8EjgCrPMrx8xzTEdwXX81x lPwTPA1dvRF9G6DgJ54kaXZN9TQ+kM6RtpQzzu/yrUs/acb1zytCC9+oWu8OyJXx8fuv PMu3yBDd5uUd8XosLslY9zkhxG4GBSTvtqucLzJRgeAUOJcLkktDEDy9o/uR1PrFqNf5 Oym6D8Uuo5QT9E52fGS9rKvzNmokRB47E0c+Bh51EkWSexsOt7j7163Ry2UxtnoHpqe+ /wag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695478379; x=1696083179; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=I27QSkJfyuEvUa4n1zGSchXIF6bDq2xv+lKsYDTk2zk=; b=t3GbOdB3NeKWqcCv7Kl27wi6+Xmwj0bhIzCtU5EDJlzP0+HeJflw6A5X2CRMaBqKTU +p4b+2f/c9jK0+/ffGA8jy9HUvzE/oqLQMUlrFuEfW6gxi6QdG8jhcDY3Dx0rlayAjrg Y2RFPr4Ke3SdsNVjsG2PZXyLsl0JWSA+zF7qSGCZDbWYfLeRAjAib62VK2VzerTjsWqg TLmG869KRoAuVpD2DW/2dQm8wXh7H4g22zK5Sj19EViDhlpvB+mRNtHT0wuz40bjsGAP LS3o2AnU+sgpdvQacO9qVk13ZAw4m1oRlbmp5RRYXEqLT165NsgVuW7d/ltaY+Mz34eb EZOw== X-Gm-Message-State: AOJu0Yzs0Cel/3g7FIljixTTgtPHaoHs3dCCWzsw53I5Q66ncbeVmDQp UNFvl9IGFG9X78xH1M0RmWvqt4DaPonqYtbEq3Y= X-Received: by 2002:a25:6881:0:b0:d15:e204:a7be with SMTP id d123-20020a256881000000b00d15e204a7bemr1918154ybc.8.1695478378988; Sat, 23 Sep 2023 07:12:58 -0700 (PDT) MIME-Version: 1.0 References: <20230921213440.202017-1-wedsonaf@gmail.com> <20230921213440.202017-3-wedsonaf@gmail.com> In-Reply-To: From: Wedson Almeida Filho Date: Sat, 23 Sep 2023 11:12:48 -0300 Message-ID: Subject: Re: [PATCH 2/2] rust: arc: remove `ArcBorrow` in favour of `WithRef` To: Jianguo Bao Cc: rust-for-linux@vger.kernel.org, Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?Q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , linux-kernel@vger.kernel.org, Wedson Almeida Filho Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=3.0 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_SBL_CSS, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Sat, 23 Sep 2023 07:13:09 -0700 (PDT) X-Spam-Level: ** On Sat, 23 Sept 2023 at 02:17, Jianguo Bao wrote: > > On Thu, Sep 21, 2023 at 06:34:40PM -0300, Wedson Almeida Filho wrote: > > From: Wedson Almeida Filho > > > > With GATs, we don't need a separate type to represent a borrowed object > > with a refcount, we can just use Rust's regular shared borrowing. In > > this case, we use `&WithRef` instead of `ArcBorrow<'_, T>`. > > > > Co-developed-by: Boqun Feng > > Signed-off-by: Boqun Feng > > Signed-off-by: Wedson Almeida Filho > > --- > > rust/kernel/sync.rs | 2 +- > > rust/kernel/sync/arc.rs | 180 ++++++++++++++-------------------------- > > 2 files changed, 62 insertions(+), 120 deletions(-) > > > > diff --git a/rust/kernel/sync.rs b/rust/kernel/sync.rs > > index d219ee518eff..083494884500 100644 > > --- a/rust/kernel/sync.rs > > +++ b/rust/kernel/sync.rs > > @@ -12,7 +12,7 @@ > > pub mod lock; > > mod locked_by; > > > > -pub use arc::{Arc, ArcBorrow, UniqueArc}; > > +pub use arc::{Arc, UniqueArc, WithRef}; > > pub use condvar::CondVar; > > pub use lock::{mutex::Mutex, spinlock::SpinLock}; > > pub use locked_by::LockedBy; > > diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs > > index 86bff1e0002c..5948e42b9c8f 100644 > > --- a/rust/kernel/sync/arc.rs > > +++ b/rust/kernel/sync/arc.rs > > @@ -105,14 +105,14 @@ > > /// Coercion from `Arc` to `Arc`: > > /// > > /// ``` > > -/// use kernel::sync::{Arc, ArcBorrow}; > > +/// use kernel::sync::{Arc, WithRef}; > > /// > > /// trait MyTrait { > > /// // Trait has a function whose `self` type is `Arc`. > > /// fn example1(self: Arc) {} > > /// > > -/// // Trait has a function whose `self` type is `ArcBorrow<'_, Self>`. > > -/// fn example2(self: ArcBorrow<'_, Self>) {} > > +/// // Trait has a function whose `self` type is `&WithRef`. > > +/// fn example2(self: &WithRef) {} > > /// } > > /// > > /// struct Example; > > @@ -130,9 +130,48 @@ pub struct Arc { > > _p: PhantomData>, > > } > > > > +/// An instance of `T` with an attached reference count. > > +/// > > +/// # Examples > > +/// > > +/// ``` > > +/// use kernel::sync::{Arc, WithRef}; > > +/// > > +/// struct Example; > > +/// > > +/// fn do_something(e: &WithRef) -> Arc { > > +/// e.into() > > +/// } > > +/// > > +/// let obj = Arc::try_new(Example)?; > > +/// let cloned = do_something(obj.as_with_ref()); > > +/// > > +/// // Assert that both `obj` and `cloned` point to the same underlying object. > > +/// assert!(core::ptr::eq(&*obj, &*cloned)); > > +/// ``` > > +/// > > +/// Using `WithRef` as the type of `self`: > > +/// > > +/// ``` > > +/// use kernel::sync::{Arc, WithRef}; > > +/// > > +/// struct Example { > > +/// _a: u32, > > +/// _b: u32, > > +/// } > > +/// > > +/// impl Example { > > +/// fn use_reference(self: &WithRef) { > > +/// // ... > > +/// } > > +/// } > > +/// > > +/// let obj = Arc::try_new(Example { _a: 10, _b: 20 })?; > > +/// obj.as_with_ref().use_reference(); > > +/// ``` > > #[pin_data] > > #[repr(C)] > > -struct WithRef { > > +pub struct WithRef { > > refcount: Opaque, > > data: T, > > } > > @@ -215,16 +254,16 @@ unsafe fn from_inner(inner: NonNull>) -> Self { > > } > > } > > > > - /// Returns an [`ArcBorrow`] from the given [`Arc`]. > > + /// Returns a [`WithRef`] from the given [`Arc`]. > > /// Returns a shared reference of [`WithRef`] from the given [`Arc`]. Thanks Jianguo.. I'll make this change in v2. > > /// > > - /// 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. > > + /// This is useful when the argument of a function call is a [`WithRef`] (e.g., in a method > > + /// receiver), but we have an [`Arc`] instead. Getting a [`WithRef`] is free when optimised. > > #[inline] > > - pub fn as_arc_borrow(&self) -> ArcBorrow<'_, T> { > > + pub fn as_with_ref(&self) -> &WithRef { > > // 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 > > + // the returned `WithRef` ensures that the object remains alive and that no mutable > > // reference can be created. > > - unsafe { ArcBorrow::new(self.ptr) } > > + unsafe { self.ptr.as_ref() } > > } > > > > /// Compare whether two [`Arc`] pointers reference the same underlying object. > > @@ -234,20 +273,17 @@ pub fn ptr_eq(this: &Self, other: &Self) -> bool { > > } > > > > impl ForeignOwnable for Arc { > > - type Borrowed<'a> = ArcBorrow<'a, T>; > > + type Borrowed<'a> = &'a WithRef; > > > > fn into_foreign(self) -> *const core::ffi::c_void { > > ManuallyDrop::new(self).ptr.as_ptr() as _ > > } > > > > - unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> ArcBorrow<'a, T> { > > + unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> &'a WithRef { > > // SAFETY: By the safety requirement of this function, we know that `ptr` came from > > - // a previous call to `Arc::into_foreign`. > > - let inner = NonNull::new(ptr as *mut WithRef).unwrap(); > > - > > - // SAFETY: The safety requirements of `from_foreign` ensure that the object remains alive > > - // for the lifetime of the returned value. > > - unsafe { ArcBorrow::new(inner) } > > + // a previous call to `Arc::into_foreign`. The safety requirements of `from_foreign` ensure > > + // that the object remains alive for the lifetime of the returned value. > > + unsafe { &*(ptr.cast::>()) } > > } > > > > unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self { > > @@ -320,119 +356,25 @@ fn from(item: Pin>) -> Self { > > } > > } > > > > -/// 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 kernel::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)); > > -/// # Ok::<(), Error>(()) > > -/// ``` > > -/// > > -/// Using `ArcBorrow` as the type of `self`: > > -/// > > -/// ``` > > -/// use kernel::sync::{Arc, ArcBorrow}; > > -/// > > -/// struct Example { > > -/// a: u32, > > -/// b: u32, > > -/// } > > -/// > > -/// impl Example { > > -/// fn use_reference(self: ArcBorrow<'_, Self>) { > > -/// // ... > > -/// } > > -/// } > > -/// > > -/// let obj = Arc::try_new(Example { a: 10, b: 20 })?; > > -/// obj.as_arc_borrow().use_reference(); > > -/// # Ok::<(), Error>(()) > > -/// ``` > > -pub struct ArcBorrow<'a, T: ?Sized + 'a> { > > - inner: NonNull>, > > - _p: PhantomData<&'a ()>, > > -} > > - > > -// This is to allow [`ArcBorrow`] (and variants) to be used as the type of `self`. > > -impl core::ops::Receiver for ArcBorrow<'_, T> {} > > - > > -// This is to allow `ArcBorrow` to be dispatched on when `ArcBorrow` can be coerced into > > -// `ArcBorrow`. > > -impl, U: ?Sized> core::ops::DispatchFromDyn> > > - for ArcBorrow<'_, T> > > -{ > > -} > > - > > -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, > > - } > > - } > > -} > > +// This is to allow [`WithRef`] (and variants) to be used as the type of `self`. > > +impl core::ops::Receiver for WithRef {} > > > > -impl From> for Arc { > > - fn from(b: ArcBorrow<'_, T>) -> Self { > > +impl From<&WithRef> for Arc { > > + fn from(b: &WithRef) -> 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) }) > > + ManuallyDrop::new(unsafe { Arc::from_inner(b.into()) }) > > .deref() > > .clone() > > } > > } > > > > -impl Deref for ArcBorrow<'_, T> { > > +impl Deref for WithRef { > > 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 } > > + &self.data > > } > > } > > > > -- > > 2.34.1 > >