Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp283657rdh; Sat, 23 Sep 2023 10:07:39 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHh51wFjZgN+4722wQKfEEw7J8My2UYYuYo7TUDdgxZJISsbv7jHJPErMLgaJthqDKHfP/0 X-Received: by 2002:a05:6871:80c:b0:1dc:bccf:b840 with SMTP id q12-20020a056871080c00b001dcbccfb840mr3851033oap.32.1695488858733; Sat, 23 Sep 2023 10:07:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695488858; cv=none; d=google.com; s=arc-20160816; b=h5C9WAIl/cp94rzRqfRbiXtH6OlG9PllRf9RY2a3cexfNi0b3Mi0fEVJF50rNhr0gf KuA4cs3JkCohmVKPe9q8JN/W7+Lg72fKctghogX+k/sbjKq6h7ZeA9thN1qGt8IKuNBP 99Qmb1SqFb2zyUr83r8XZqJOlcb6/VJn/L5WwstomhIpu9ZVAei8+S+7iTLRHI9itpYU C+soEwLnyiUMdf7/0Gj0hoZ2zcDiueEkx8gwZb+yuOkIAwPMLhw2/uayhgDhnN/AbMOG K7Oj7MPlYQCnVaYJyVFsz38QfEYXIgLeh0/45cuG1QxK6AflO8s9M+bCQ02IRdSYxQmE 9nqw== 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=LY3Ph9x6pWQb69i1+N/rSHqRZP4MX+dDBpge1hAwHaQ=; fh=mjCGlFxamdbn+LCVaid2YIAlz8jZZ97ruA2zhZf4EFw=; b=zgyKUNADW5OJOHjVY+B+fTkMAuOnnox5xTAfXWPbKrz0cbwmqku4maWzSMPBNPIoPz VtxXrHy7uF5/sVLt91QxYAN2hSEbDdOFKtaJtoVkrRSkrRHltFucGfRK31kJIdkXM8Ev yw42scXxhVzgsAJ1AYHSHUZwaS2vDTjD4cwEwgawZCMb055WcdBcvZProGeSVXVRY5Fk Xw/cKtor7+xDmJhzMXsZ3BXkBQNAujljOf23Y+uuGM48+tLfAslG+Iak426MWP+xW6+U FvEUpaSqKFu1SCrrOYJ+ydrqO2dlZeLxhDb5x/xF9wQkwsszqsjw4+w/gxAmiUdBXGi+ ymzQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=YYLwik+2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id m124-20020a633f82000000b005774635c69dsi6182979pga.66.2023.09.23.10.07.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Sep 2023 10:07:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=YYLwik+2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (Postfix) with ESMTP id 70EF2835C146; Sat, 23 Sep 2023 07:01:30 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230272AbjIWOB1 (ORCPT + 99 others); Sat, 23 Sep 2023 10:01:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53984 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229616AbjIWOBZ (ORCPT ); Sat, 23 Sep 2023 10:01:25 -0400 Received: from mail-yb1-xb35.google.com (mail-yb1-xb35.google.com [IPv6:2607:f8b0:4864:20::b35]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9C9E819F; Sat, 23 Sep 2023 07:01:18 -0700 (PDT) Received: by mail-yb1-xb35.google.com with SMTP id 3f1490d57ef6-d85800eb234so4118574276.0; Sat, 23 Sep 2023 07:01:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1695477678; x=1696082478; 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=LY3Ph9x6pWQb69i1+N/rSHqRZP4MX+dDBpge1hAwHaQ=; b=YYLwik+2r9cb6AmSOBULWdCYUyAy5PPAWnuHoLRAbF4xaCy9q7Rc5OkNqX+CmYVhBY EC8RlXoBA8MM+pt++0K37lPpFWw81VSZPTdDuPeyvaely/97uZzpj5+D9CNjCikwWxVV ZFpv7ym2fA1RQwnv/IegU7Bw8Vz9q4YrzJiCnckgbHoWTfvsW4qD29zL+7HgHkvbRsgX Fhcdxu3pEnDBKCOcaKs3nduWLz7vWMUAjx5hCKXyk7BvLBh/mRaOjMzh+ndM+03rU2V/ FYXr46KJE8rsG1SepW8DwGlpbcWsCiEtMWRNynhtA7UQHkbz10TwjC69NG9prmbOXJD+ mMiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695477678; x=1696082478; 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=LY3Ph9x6pWQb69i1+N/rSHqRZP4MX+dDBpge1hAwHaQ=; b=bgvWOrV0lSNe9oGVMBsCITSkrdLzJJJLa5sQfvvBQ7ymUNrTJ3lGSfcLiplDOd+1v9 wJluCz2AKGFpVh+M5bqVbeU5diFPG2oN0mahD+1fT0b35OmgePXZfILZVZML4B/SQtOK TkzoC/+n0q7GrW55qKal8sZPzMi2z7KFnHHd6RE0SvRySeLwxbBeB5hX6XTnzolm7flM BTivcOa5VLZEre2sFVDUVZtIvV1T7Mc1vr+KBRx3lphaEkxiGx3xQZBpb3V43S3lcMX2 l3ABGwgh0/mEzw9bJdOprmKcXJvBOYZthTC+PXMxcMOJak2d5reyJrYdzBi04WwdkvI/ kPoQ== X-Gm-Message-State: AOJu0YzDbrCxOKYBmwsdoTgJ0XHoQv0rGa6RStdCOsHFnTJcjCRL9sqM ryfqywPnjcUVQJ4oIRwfVzcYuHujh4AMtHKCphI= X-Received: by 2002:a25:e087:0:b0:d81:65a9:ac6d with SMTP id x129-20020a25e087000000b00d8165a9ac6dmr2541416ybg.37.1695477677564; Sat, 23 Sep 2023 07:01:17 -0700 (PDT) MIME-Version: 1.0 References: <20230921213440.202017-1-wedsonaf@gmail.com> <20230921213440.202017-3-wedsonaf@gmail.com> <63223594-1bbe-c4f2-ea82-9b34c66ba1f6@proton.me> In-Reply-To: <63223594-1bbe-c4f2-ea82-9b34c66ba1f6@proton.me> From: Wedson Almeida Filho Date: Sat, 23 Sep 2023 11:01:05 -0300 Message-ID: Subject: Re: [PATCH 2/2] rust: arc: remove `ArcBorrow` in favour of `WithRef` To: Benno Lossin Cc: rust-for-linux@vger.kernel.org, Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?Q?Bj=C3=B6rn_Roy_Baron?= , 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 pete.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 (pete.vger.email [0.0.0.0]); Sat, 23 Sep 2023 07:01:30 -0700 (PDT) X-Spam-Level: ** On Fri, 22 Sept 2023 at 05:53, Benno Lossin wrote: > > On 21.09.23 23:34, 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 > > I only have a very small comment below, so > > Reviewed-by: Benno Lossin > > > --- > > 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, > > } > > I do not really like the position of this definition in this file. > It should be further down with the other code (like the > `impl From<&WithRef> for Arc`). Yeah, I think it was ok originally because it was just a private standalone auxiliary type. Since this is not the case anymore, it's probably better to move it down. Will do for v2. > > @@ -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`]. > > /// > > - /// 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, > > - } > > - } > > -} > > I think here would be the correct location. Another option would be > to move this code up to the definition. I don't think this is important, > so I can maybe just create an issue and let this be someone's first contribution. > > > +// 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 > > > >