Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp838244rdh; Sun, 24 Sep 2023 14:23:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGV4BT6UtBhda+ZPbcYShu9pRl/z9Dfd19w/qXlJZOJbtH8hPxuY9TJJLGNQ3h/zGfk5dln X-Received: by 2002:a05:6a00:150d:b0:690:d2de:14cc with SMTP id q13-20020a056a00150d00b00690d2de14ccmr6841804pfu.33.1695590629973; Sun, 24 Sep 2023 14:23:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695590629; cv=none; d=google.com; s=arc-20160816; b=h6jtmddqahRDbJR8M9uzQeDzGI6RynAgsDklw2q59KuXqNGZ0972lMyyW6x+PLtDcf DRN6TVazpQn891joh/YG+mDwTgcUQUTYMa0tl15e8G8XV3QDp6N+FqAGNEn5E+w5KtQv cy0DY/UXphC4LSTGB3JQb1O+gU6JzIZdh2k6r/2GUDAmRxpcrS9owrqMcGtwkGZp3Ras JtilHn3HW5qatYPPuLv3lC1wy1D+17akwPv+qCyigoyRRapnU9waHteKQ++4PzOOmn8E xdM6bY86zzbCf0Efo4pLOvk1gO6S+0IKR28HPJrZFZDE9Uq1Bpa12HrcHhoA9f74GzDc Awnw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=Vreia5Yjln7K/grcdVqC7UfkRGK1LxKMbc6SYxt0QU0=; fh=32f/2l/eLIvRE5VIosPiA0C9RafUxyvAZXmxKHDM0tI=; b=ZjNh/vNz/2hEJhVh4JCUyKaW5xL/tOZSMaZctqXG9eR4h3a6r01fzAC791p37awHe3 qrrh+8z1nyvhFFZ7ASTLnO9pl3MY/ymqZSuRBz4AtH5WAJdFj0Y8zjsX2BWpfCCUozDP 9XAoFfp+Ez1SAa11CiMf4UySoBdloq6wF38lzi0MyiZsdIwXipRr45BfVWLPZpsjaU4h +f0vmKnEYu6fgLSSfuJARdNSxdzKWugohw6205pZiuaSeQ0iOEIQ0M2Lc4/VNNwkJb8f s5pPkEaCwDnU6NhnYoVdqPVL1JkqQpoibvNMlPEl4a1iAQemW90WU2aIt7SKyQW6vU59 6LRw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=WPvghzGz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 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 howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id x24-20020a056a000bd800b00690d42e3347si7842302pfu.157.2023.09.24.14.23.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 Sep 2023 14:23:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=WPvghzGz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 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 howler.vger.email (Postfix) with ESMTP id E844E806AFCB; Sun, 24 Sep 2023 06:38:32 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230027AbjIXNid (ORCPT + 99 others); Sun, 24 Sep 2023 09:38:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47052 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230293AbjIXNiS (ORCPT ); Sun, 24 Sep 2023 09:38:18 -0400 Received: from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com [IPv6:2607:f8b0:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4F81219B2; Sun, 24 Sep 2023 06:37:42 -0700 (PDT) Received: by mail-pf1-x42e.google.com with SMTP id d2e1a72fcca58-6910ea9cddbso4196290b3a.0; Sun, 24 Sep 2023 06:37:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1695562662; x=1696167462; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=Vreia5Yjln7K/grcdVqC7UfkRGK1LxKMbc6SYxt0QU0=; b=WPvghzGzBdxkcam3dVTY4Vo/6f8EPnqXaLw/kdQ2cY0DWhREXby6q0aR6mHfGFk9WT KbWC3JDqCd9y/ROUQzEvPKNOQ8p/ZPb36vEiRBywSyEX+E4QjMjljcnKIo3hFvsEXqgt 1iQ3qBn/REqJOjH7JANIAL/xUVu24WbIarMZwqGW364TibIMoOO9fwwY54lKBlSuV+dO b/EjSPZ+nncLYvjsgibWdPAwkEahALukBVknOYGyLdOs9fdep0Iy2vUuAyZjGDgYIC5k /XbWqVm+qAnzsH7MAI6fTTuEeh+LFGwqsmi9HwvTuFg/1XVq+5H9FX9pV1ZDPFSofNDL uTNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695562662; x=1696167462; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=Vreia5Yjln7K/grcdVqC7UfkRGK1LxKMbc6SYxt0QU0=; b=dSpU7MOa/5YQYiImtyVZbTXvu69OeiSXuQw2jNCtgm2cCQgs7i3B+xEbSVKwzCsnQn e7/BiyL48xNN+NiCXl2INc89eSyS2a4oZwUrHwugD9gDspOewVmdf1iTcWktQjA9I+2w mMLyl4vGCazoo+xrdZJcnWcuKTya8rrpqJzpGQpPKAEHVnxUfYJcz/qeIaTaof7sKFzS oqAvxClbO3bzWo0ht5rXvCwEvuGtQSb9LEMLb5CaRpjnBdHKk/2dhox6iCo095SxJ83D JUURK0eXFVUSyh3NnXtLFPgt8K53UbkRbJU7RTB47PRZShxKXIW+5oMRPmap0CAu411L gRAQ== X-Gm-Message-State: AOJu0Yz2QhClE11lsYPV5Wde/gjYyNNIj4n1KtkWpTOlKhj82/rQNeCw UxhZDfMn+aOUbe0HH9GNrPg9KT5MJ/fh+g== X-Received: by 2002:a05:6a00:23d3:b0:690:2ecd:a593 with SMTP id g19-20020a056a0023d300b006902ecda593mr5881424pfc.26.1695562661563; Sun, 24 Sep 2023 06:37:41 -0700 (PDT) Received: from localhost ([162.243.44.213]) by smtp.gmail.com with ESMTPSA id bu20-20020a632954000000b0057c29fec795sm5934434pgb.37.2023.09.24.06.37.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 Sep 2023 06:37:41 -0700 (PDT) Date: Sun, 24 Sep 2023 21:36:37 +0800 From: Jianguo Bao To: Wedson Almeida Filho Cc: rust-for-linux@vger.kernel.org, Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?B?QmrDtnJu?= Roy Baron , Benno Lossin , Andreas Hindborg , Alice Ryhl , linux-kernel@vger.kernel.org, Wedson Almeida Filho Subject: Re: [PATCH v2 2/2] rust: arc: remove `ArcBorrow` in favour of `WithRef` Message-ID: References: <20230923144938.219517-1-wedsonaf@gmail.com> <20230923144938.219517-3-wedsonaf@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230923144938.219517-3-wedsonaf@gmail.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Sun, 24 Sep 2023 06:38:33 -0700 (PDT) Hi, This time i just realize that the `Ref` in WithRef is the refcount, not the reference in rust . So can we just change the name WithRefC or other to express this refcount,not reference. If we have defined the Ref only mean the refcount already in somewhere such as rust doc or have some conventions in rust community before, then omit this suggest. On Sat, Sep 23, 2023 at 11:49:38AM -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 | 134 ++++++++++++---------------------------- > 2 files changed, 39 insertions(+), 97 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..a1806e38c37f 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,13 +130,6 @@ pub struct Arc { > _p: PhantomData>, > } > > -#[pin_data] > -#[repr(C)] > -struct WithRef { > - refcount: Opaque, > - data: T, > -} > - > // This is to allow [`Arc`] (and variants) to be used as the type of `self`. > impl core::ops::Receiver for Arc {} > > @@ -215,16 +208,16 @@ unsafe fn from_inner(inner: NonNull>) -> Self { > } > } > > - /// Returns an [`ArcBorrow`] from the given [`Arc`]. > + /// Returns a shared reference to a [`WithRef`] the given [`Arc`]. /// Returns a shared reference to a [`WithRef`] from the given [`Arc`]. or /// Returns a borrowed arc [`&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 +227,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 +310,71 @@ 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. > +/// An instance of `T` with an attached reference count. > /// > -/// 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 > +/// # Examples > /// > /// ``` > -/// use kernel::sync::{Arc, ArcBorrow}; > +/// use kernel::sync::{Arc, WithRef}; > /// > /// struct Example; > /// > -/// fn do_something(e: ArcBorrow<'_, Example>) -> Arc { > +/// fn do_something(e: &WithRef) -> Arc { > /// e.into() > /// } > /// > /// let obj = Arc::try_new(Example)?; > -/// let cloned = do_something(obj.as_arc_borrow()); > +/// 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)); > -/// # Ok::<(), Error>(()) > /// ``` > /// > -/// Using `ArcBorrow` as the type of `self`: > +/// Using `WithRef` as the type of `self`: > /// > /// ``` > -/// use kernel::sync::{Arc, ArcBorrow}; > +/// use kernel::sync::{Arc, WithRef}; > /// > /// struct Example { > -/// a: u32, > -/// b: u32, > +/// _a: u32, > +/// _b: u32, > /// } > /// > /// impl Example { > -/// fn use_reference(self: ArcBorrow<'_, Self>) { > +/// fn use_reference(self: &WithRef) { > /// // ... > /// } > /// } > /// > -/// let obj = Arc::try_new(Example { a: 10, b: 20 })?; > -/// obj.as_arc_borrow().use_reference(); > -/// # Ok::<(), Error>(()) > +/// let obj = Arc::try_new(Example { _a: 10, _b: 20 })?; > +/// obj.as_with_ref().use_reference(); > /// ``` > -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 > - } > +#[pin_data] > +#[repr(C)] > +pub struct WithRef { > + refcount: Opaque, > + data: T, > } > > -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 >