Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp13616068rwl; Wed, 4 Jan 2023 10:28:48 -0800 (PST) X-Google-Smtp-Source: AMrXdXtn0djya31BMC99OQ6X/PG5Q7js5oNOInkcK1SqGaLqjz6r/DU6ydqvuYaUiWV0N0n+GSGN X-Received: by 2002:a05:6a20:d39a:b0:9d:efbf:6618 with SMTP id iq26-20020a056a20d39a00b0009defbf6618mr61002383pzb.38.1672856928135; Wed, 04 Jan 2023 10:28:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672856928; cv=none; d=google.com; s=arc-20160816; b=wq/mh0cbrvrpPAR/qWE0AaOuuGeLoowBa0LJusIGUuuSGCPNrUCMm2X5639R7iwTIU p6HfCMRCro7n+cqi2+IuSOZSoGwrCgocMR8Z0aY7xOZmYgE+3U0x72J0BnKVnxLwKp83 KidAzL5GIW6C8coIYj8webNrBDubpcweQeULurxevDLxRC+jSdiznePyCPKqOlMzqY8P BnIVThz68ADhOWItcN471JquUcV7T0lklMen7so19BH50Y6TfOGSkIKsBtOWG4q6EiiO +Np3p0gCMMfXE1WEp62o1P1xRGQaZ6RqSX6hQiRIWfuSg1i2WYrLVzQAQVf+OOeqj/Wq 7FVw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=pzRN+ThvK9tCIgke9+TLEirKqYpVwfmOcXrEgIxAajI=; b=yDgwIb2685l/niSuifjWUBbB+6PkkAFf57Pr8tezHDzlAmU5BO/wnCHVzNc3ljb/I6 JZ4sm9KstyFnK5Ire3+ZOfWBOiW3VUBjjDAbhNbvlOUUuBfsGBpSAJWfBJ4nD/3f3M47 Jx7xlU6DW1ZWznIIIba1cgY8KIxIiHXWG7YMjZFBvY1a1FUY9eTzFGWvadAgDqfFFl4y nXNhtZmZWgJB2vJ6pTkcDsgpDxHWb4dCf/baz5oPt0yy9Q2RJgGrCFiVQZzXWBj3I1a5 3BDrpXsJvilFEgMK+nHk/AGGGKJSGu95oLwtUw9pFmlw2+aNV3B0Sp0PzmT/9wNZVGeE V+eg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=Bh+eN9QL; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g5-20020a63fa45000000b0049b36d5a275si24277519pgk.400.2023.01.04.10.28.40; Wed, 04 Jan 2023 10:28:48 -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=@gmail.com header.s=20210112 header.b=Bh+eN9QL; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239988AbjADRxR (ORCPT + 56 others); Wed, 4 Jan 2023 12:53:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49712 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240103AbjADRxA (ORCPT ); Wed, 4 Jan 2023 12:53:00 -0500 Received: from mail-oa1-x35.google.com (mail-oa1-x35.google.com [IPv6:2001:4860:4864:20::35]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7A6A2E0A2; Wed, 4 Jan 2023 09:52:58 -0800 (PST) Received: by mail-oa1-x35.google.com with SMTP id 586e51a60fabf-1442977d77dso40639463fac.6; Wed, 04 Jan 2023 09:52:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=pzRN+ThvK9tCIgke9+TLEirKqYpVwfmOcXrEgIxAajI=; b=Bh+eN9QLMyipT6Z9HuZkAw5U7DOXS0Yx86HMEMI1AQBref5j0cYWdKlY5pro1eo5Ij of6QbL7Spuzk6CPXZGDPJIXYypt377XLxZChKWDpQvyoxzlpyynetvJKRlI3mZ7wytrO IOQGPvzOde80/6rR7tpK9Vn1LpJMMPN7A3Fq+b9WRkUFk+Kn/AMqoqbvFCnIX1ksj7kJ XOOyoaxzgOUSoH0temzpJ7qYs7Apg6I+KLygDD0ffiO8WYc4fuI/UNcrtmEX44KRUPsh eYN3bJqXDU46KyU6P+aYthrIj7UEYBAxG6zGu8LoLDyOg2uKp+mjQY3ZkKPwRj3J2SZF VDsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding: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=pzRN+ThvK9tCIgke9+TLEirKqYpVwfmOcXrEgIxAajI=; b=LYgJTr3QrNhbq2KDGUWVlhFvAP/Ef476BZIx0lw/LZuHnZLutUhkJeASRCqh1CYi0E 9uw1vH0kLZO/brFoGq+fFzYKDdJ2GWLnhDVtMkX+ryUnoCscE3BB+rfHJo+3YY+54vFs DziqxbI1heAHefds2GonQjAbjFAfdA8bpffR5QeDJHjo4Ehjl/MssFmwI7DoUp2pX/3y wEkhnSYaD+iX0fDHTNqy8Oty/tVDps1tHb/SnsYjewQ9csiISPKzK70SbQ9BVOZPGt0f b1ZH2BhPxObW1c0MTZXGkyova4wwgwg0rDwJI685Nq+5G6X9EHOUfUX0crQBz/+Znnv3 xP2w== X-Gm-Message-State: AFqh2kqKM4RQsEIYRpKt3k9iGpBsERGmjVIFZFcOHZnbXjisvNFHCrgR BRCfPqCypFaax8EutmN/3vipQF6JhMq4iN2ggI8= X-Received: by 2002:a05:6870:ac10:b0:144:bf10:eecd with SMTP id kw16-20020a056870ac1000b00144bf10eecdmr3806466oab.204.1672854777368; Wed, 04 Jan 2023 09:52:57 -0800 (PST) MIME-Version: 1.0 References: <20221228060346.352362-1-wedsonaf@gmail.com> <20221228060346.352362-4-wedsonaf@gmail.com> <20221231194352.55cf0a26.gary@garyguo.net> In-Reply-To: From: Wedson Almeida Filho Date: Wed, 4 Jan 2023 17:52:46 +0000 Message-ID: Subject: Re: [PATCH 4/7] rust: sync: introduce `ArcBorrow` To: =?UTF-8?Q?Emilio_Cobos_=C3=81lvarez?= Cc: Gary Guo , rust-for-linux@vger.kernel.org, Miguel Ojeda , Alex Gaynor , Boqun Feng , =?UTF-8?Q?Bj=C3=B6rn_Roy_Baron?= , linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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_NONE,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 On Wed, 4 Jan 2023 at 16:06, Emilio Cobos =C3=81lvarez w= rote: > > Sorry for the drive-by comment, but maybe it saves some work. > > On 1/4/23 16:29, Wedson Almeida Filho wrote: > > On Sat, 31 Dec 2022 at 19:43, Gary Guo wrote: > >> > >> On Wed, 28 Dec 2022 06:03:43 +0000 > >> Wedson Almeida Filho wrote: > >> > >>> This allows us to create references to a ref-counted allocation witho= ut > >>> 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 [`ArcB= orrow`] is free when optimised. > >>> + #[inline] > >>> + pub fn as_arc_borrow(&self) -> ArcBorrow<'_, T> { > >>> + // SAFETY: The constraint that the lifetime of the shared re= ference 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 o= n the allocation, it is simpler > >>> +/// to use just `&T`, which we can trivially get from an `Arc` in= stance. > >>> +/// > >>> +/// However, when one may need to increment the refcount, it is pref= erable to use an `ArcBorrow` > >>> +/// over `&Arc` because the latter results in a double-indirectio= n: a pointer (shared reference) > >>> +/// to a pointer (`Arc`) to the object (`T`). An [`ArcBorrow`] el= iminates this double > >>> +/// indirection while still allowing one to increment the refcount a= nd getting an `Arc` when/if > >>> +/// needed. > >>> +/// > >>> +/// # Invariants > >>> +/// > >>> +/// There are no mutable references to the underlying [`Arc`], and i= t 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 =3D Arc::try_new(Example)?; > >>> +/// let cloned =3D do_something(obj.as_arc_borrow()); > >>> +/// > >>> +/// // Assert that both `obj` and `cloned` point to the same underly= ing 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> {} > >> > >> Couldn't this just be derived `Clone` and `Copy`? > > > > Indeed. I'll send a v2 with this. > > I'm not sure this is true. Deriving will add the T: Copy and T: Clone > bound, which I think is not what you want here. > > i.e., I assume you want an ArcBorrow to be Copy even if the underlying T > is not. > > See for the relevant > (really long-standing) Rust issue. Thanks for the heads up, Emilio! After trying this out, derive doesn't work. The errors brought me back memories of when I first implemented this over a year ago, though I didn't take the time to try to understand why it was failing. So no v2. The series will remain as is. Cheers > > Cheers, > > -- Emilio