From patchwork Sat Sep 23 14:49:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Wedson Almeida Filho X-Patchwork-Id: 143960 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp289668vqu; Sat, 23 Sep 2023 10:16:42 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGdH8864Ojvu7KlrZBYIzhwX4s+Q+11+lcyHxcMMRAXrgdggM1M+6sFFvwW8lVA6kbjP6Gz X-Received: by 2002:a05:6e02:20ef:b0:348:ac7f:69a2 with SMTP id q15-20020a056e0220ef00b00348ac7f69a2mr3881996ilv.15.1695489401857; Sat, 23 Sep 2023 10:16:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695489401; cv=none; d=google.com; s=arc-20160816; b=L8dsdiDZX7B9VsBgOQsPoc9CLrYGDlYvI7Vwa1R30/qimu5y77LMM9WFVdhXPhV+FH qQXLcrCoB3vlauf8zTnHgMjsFY5RTWERcn20F+LzvOM5HZ5dC888QChT98dWLRXyY+5S nei/9VKiKqbLAZs4yjk09artMy4A+VD4pVHEq9x6JrtJuD5PgK5P0D+OfCNheDYYCMX/ mMtKzgJtfWHqLHRyjL7zZY/KW7PDeV7SSLwtQ8YO80czIV1zTVzAP2AS+CiyXhwIBwTw 5uuZhai5oOnimS4vFEdEI4B86HzfBMKvax35PTbmAuRYDRsEb2nZLCqOzYL5Vfzzlb79 LRhA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=WM0ZLVHvV+NZ5kwdSJi8dtI3pr22k4r3qhjNO/EWTSA=; fh=BpeH/FUAN0wxLMH3ClwVqRp0rua8Kbn4MIwmp4atozc=; b=Zq8a8h5cgYSJmyLDFyto/ierlHLkli8PLnA9VwPwKJhm1OHWwRqvtGewI0GknmfpDa nw4OC0JGvjx6XjviMr/+YVbYxFZbp7g+VB0011VMBLJs3Sw9MYqCeUOBdiAHfBfJJ/MX KYAlNFRIZZXwT7kCdTnpYAOMUx/vAoPYVTyFfxgwWaZT1VvnKF3no1OWIrlzCaljq4QE LMxxLMT5bFnFyF/kYPxyQELto1BzA2GpKNBecl4H6M/Y8keHYG1unwERg04PmplHLHNJ fKxVXirmH4grTWwhip69fETEiAZ1eh50fHrwIrY3oi5PRAH+QVZYF9t4bsSa9KBE+NLd 3ZxA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=dmFulhyA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id bx22-20020a056a02051600b00565eccbc52fsi7088101pgb.303.2023.09.23.10.16.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Sep 2023 10:16:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=dmFulhyA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (Postfix) with ESMTP id AE761803200B; Sat, 23 Sep 2023 07:50:06 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231740AbjIWOuD (ORCPT + 29 others); Sat, 23 Sep 2023 10:50:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60276 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231330AbjIWOuB (ORCPT ); Sat, 23 Sep 2023 10:50:01 -0400 Received: from mail-pg1-x52a.google.com (mail-pg1-x52a.google.com [IPv6:2607:f8b0:4864:20::52a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 741C2180; Sat, 23 Sep 2023 07:49:55 -0700 (PDT) Received: by mail-pg1-x52a.google.com with SMTP id 41be03b00d2f7-578d78ffdbcso2383592a12.2; Sat, 23 Sep 2023 07:49:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1695480595; x=1696085395; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=WM0ZLVHvV+NZ5kwdSJi8dtI3pr22k4r3qhjNO/EWTSA=; b=dmFulhyAqpyhfQ5adNZuxE6scb3rZTL3Nn5EAET3YmwhbF/xz2q9wehAb8pjhGxqoE Eh/JjYzcNMcXgTovgtLhhw/+9oQf1mA5dNm3OY4JX4m3ae9eO/ynicA/yD+q9YMrnuwF T7ampmiI3BZ9lRES/buWylqHAwriB5w1c7yF4GB3Pu3kYFsNotKCDm3hom79RDm4CwJE aGKn0IZkIjYVCYMuLQPKsXOnJvHXo5pwdwIXs9X6GAgys942wYx+x/rLCduaxcM+VgSW 1sLw6Z+xwwFmibglsN39UENu07FV0gLMB8pmazF8K7fBFSTwlFMiMZB6TRfAxI3/rlQX L9Rg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695480595; x=1696085395; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=WM0ZLVHvV+NZ5kwdSJi8dtI3pr22k4r3qhjNO/EWTSA=; b=W3HyuCDsnmhrlEkkvkax0VwLDrQ1z3F1plOvbTtro2TnI8mf/Naz34TeSQ86YAW01i dmKm1DWM/Xi19/dG3TfRVXOl6B/aTyEeA4dw7s4N2On6Uh5TRR5s+MtutgJRHWyzZhMq 6nyLExhaJ5bhMTGuIA45VAoKVA64lePG2ow9uLfrR5QySsEmbpoMwiB8qfsKbV/UJJEp zZZXwt8Ku0B8xKtZ7SfxR5OItffaqQvkX2AI/M0C9ta7fYRay82EME3DdWsYJEzkv6QE XYC84uzxtGHTIhk1U/ycL4NWYs0mBQeMiIUDmNd9YWgJf5CEpORzdVbQh+4qJg/CaWGM pPQw== X-Gm-Message-State: AOJu0Yz/l8FsYvN8o949jjCfIL6psa4Yn81FeK1muf1pLp7M86iAPJhE 80OsujksPMOWLfbN996uUNTRI55GTDc= X-Received: by 2002:a05:6a20:5482:b0:13a:fa9e:787b with SMTP id i2-20020a056a20548200b0013afa9e787bmr2611143pzk.12.1695480594833; Sat, 23 Sep 2023 07:49:54 -0700 (PDT) Received: from wedsonaf-dev.home.lan ([189.124.190.154]) by smtp.googlemail.com with ESMTPSA id s3-20020a170902ea0300b001c42d4b3675sm5392806plg.309.2023.09.23.07.49.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Sep 2023 07:49:54 -0700 (PDT) From: Wedson Almeida Filho To: rust-for-linux@vger.kernel.org Cc: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?B?= =?utf-8?q?j=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , linux-kernel@vger.kernel.org, Wedson Almeida Filho Subject: [PATCH v2 1/2] rust: arc: rename `ArcInner` to `WithRef` Date: Sat, 23 Sep 2023 11:49:37 -0300 Message-Id: <20230923144938.219517-2-wedsonaf@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230923144938.219517-1-wedsonaf@gmail.com> References: <20230923144938.219517-1-wedsonaf@gmail.com> MIME-Version: 1.0 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 (snail.vger.email [0.0.0.0]); Sat, 23 Sep 2023 07:50:06 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777849494844481660 X-GMAIL-MSGID: 1777849494844481660 From: Wedson Almeida Filho This is in preparation for removing `ArcBorrow` and making `WithRef` public. This is a pure name change with no functional changes intended. Suggested-by: Björn Roy Baron Signed-off-by: Wedson Almeida Filho Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Benno Lossin Reviewed-by: Jianguo Bao Reviewed-by: Alice Ryhl --- rust/kernel/sync/arc.rs | 20 ++++++++++---------- rust/kernel/sync/arc/std_vendor.rs | 4 ++-- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs index 3d496391a9bd..86bff1e0002c 100644 --- a/rust/kernel/sync/arc.rs +++ b/rust/kernel/sync/arc.rs @@ -126,13 +126,13 @@ /// # Ok::<(), Error>(()) /// ``` pub struct Arc { - ptr: NonNull>, - _p: PhantomData>, + ptr: NonNull>, + _p: PhantomData>, } #[pin_data] #[repr(C)] -struct ArcInner { +struct WithRef { refcount: Opaque, data: T, } @@ -164,7 +164,7 @@ impl Arc { /// Constructs a new reference counted instance of `T`. pub fn try_new(contents: T) -> Result { // INVARIANT: The refcount is initialised to a non-zero value. - let value = ArcInner { + let value = WithRef { // SAFETY: There are no safety requirements for this FFI call. refcount: Opaque::new(unsafe { bindings::REFCOUNT_INIT(1) }), data: contents, @@ -201,13 +201,13 @@ pub fn init(init: impl Init) -> error::Result } impl Arc { - /// Constructs a new [`Arc`] from an existing [`ArcInner`]. + /// Constructs a new [`Arc`] from an existing [`WithRef`]. /// /// # Safety /// /// The caller must ensure that `inner` points to a valid location and has a non-zero reference /// count, one of which will be owned by the new [`Arc`] instance. - unsafe fn from_inner(inner: NonNull>) -> Self { + unsafe fn from_inner(inner: NonNull>) -> Self { // INVARIANT: By the safety requirements, the invariants hold. Arc { ptr: inner, @@ -243,7 +243,7 @@ fn into_foreign(self) -> *const core::ffi::c_void { unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> ArcBorrow<'a, T> { // 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 ArcInner).unwrap(); + 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. @@ -376,7 +376,7 @@ fn from(item: Pin>) -> Self { /// # Ok::<(), Error>(()) /// ``` pub struct ArcBorrow<'a, T: ?Sized + 'a> { - inner: NonNull>, + inner: NonNull>, _p: PhantomData<&'a ()>, } @@ -406,7 +406,7 @@ impl ArcBorrow<'_, T> { /// 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 { + unsafe fn new(inner: NonNull>) -> Self { // INVARIANT: The safety requirements guarantee the invariants. Self { inner, @@ -526,7 +526,7 @@ pub fn try_new(value: T) -> Result { /// Tries to allocate a new [`UniqueArc`] instance whose contents are not initialised yet. pub fn try_new_uninit() -> Result>, AllocError> { // INVARIANT: The refcount is initialised to a non-zero value. - let inner = Box::try_init::(try_init!(ArcInner { + let inner = Box::try_init::(try_init!(WithRef { // SAFETY: There are no safety requirements for this FFI call. refcount: Opaque::new(unsafe { bindings::REFCOUNT_INIT(1) }), data <- init::uninit::(), diff --git a/rust/kernel/sync/arc/std_vendor.rs b/rust/kernel/sync/arc/std_vendor.rs index a66a0c2831b3..4b30e5597ba5 100644 --- a/rust/kernel/sync/arc/std_vendor.rs +++ b/rust/kernel/sync/arc/std_vendor.rs @@ -5,7 +5,7 @@ //! "Apache-2.0 OR MIT" and adapted for kernel use. For copyright details, //! see . -use crate::sync::{arc::ArcInner, Arc}; +use crate::sync::{arc::WithRef, Arc}; use core::any::Any; impl Arc { @@ -17,7 +17,7 @@ pub fn downcast(self) -> core::result::Result, Self> if (*self).is::() { // SAFETY: We have just checked that the type is correct, so we can cast the pointer. unsafe { - let ptr = self.ptr.cast::>(); + let ptr = self.ptr.cast::>(); core::mem::forget(self); Ok(Arc::from_inner(ptr)) } From patchwork Sat Sep 23 14:49:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wedson Almeida Filho X-Patchwork-Id: 143983 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp331112vqu; Sat, 23 Sep 2023 12:07:03 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHNY7KUomLv+r0rx4kDdS+Vwujd4/5tAIm9kUwDgXXXRSmW85Y1CTWZaJPGzPzrIhw2a1KO X-Received: by 2002:a05:6830:1e96:b0:6b5:ee8f:73af with SMTP id n22-20020a0568301e9600b006b5ee8f73afmr3801902otr.5.1695496023188; Sat, 23 Sep 2023 12:07:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695496023; cv=none; d=google.com; s=arc-20160816; b=BfOroKKgy8/ulWWQhO/BjF+gJ4ZrGIqUcvjUtqlGqSUZRnWjjQuh997e5j19VSNGqJ ZcZ3f5bBZCjti7xVydsXSjHKfkiWebR0yql2CE+Q3A1xDdbvjJHu6CTrn05zT1Ytl3Zp F5Z2PTm0EXxIZkMfYv8/Y2M9HxJqjdw8+SGl7l/lelh+UF4kBhwt7tHPWZMwVB68XAes GqW8YCiSUUZLwE9VZEDfduQsti0SHMMElbC/ci0lwtnXCaYahjJs5oWl7vrjb6iptiCe WT27rnQ3g9uTVQsbN4cyK+6NTisitUILMydSud0BwULR/iachL2HpZyN1cd12fzeQe6O mkFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=nsfSTO1+ET/8ylN5Kt9YqyfDUerwnIWDWqcWtfepBcc=; fh=BpeH/FUAN0wxLMH3ClwVqRp0rua8Kbn4MIwmp4atozc=; b=hJNLbMOyVaFXhRpKqm43Vdz47T2iBe8/oQmuwS29TtLZUMLeGSoYqEEquX1kxuItxy cjtkBoibfHYVJW/my0ripypFrGyvTLxlpy1D7B4x8Pf9qCyx65o5Ci0g8zHxU1EIch2l Rh7hVyCVJeMkYoaIEwmUcXmBMze0AKC7LsenHPJiTbchrEAwdUJXXJ6TKLUaqDrscfcK HbPMOI0jFD5nWF58Aa9BgoS7i0W+mckuWEFz3KbNqp5c4cjo+NYh4/0QbBiMAgWVK7Bg 4oWwiDr4taE+jbHUKGv+DD77r3+Gw310cVaJvouRP3TVOYTFKDjqA4UFMKOXIpnYY5QP zb4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Jy1XWIru; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id bv8-20020a056a00414800b00690cc6f6f38si6211021pfb.76.2023.09.23.12.07.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Sep 2023 12:07:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Jy1XWIru; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (Postfix) with ESMTP id E54718032030; Sat, 23 Sep 2023 07:50:16 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231739AbjIWOuL (ORCPT + 29 others); Sat, 23 Sep 2023 10:50:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60400 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231831AbjIWOuI (ORCPT ); Sat, 23 Sep 2023 10:50:08 -0400 Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 54CB31B2; Sat, 23 Sep 2023 07:49:59 -0700 (PDT) Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-1c451541f23so29926235ad.2; Sat, 23 Sep 2023 07:49:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1695480599; x=1696085399; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=nsfSTO1+ET/8ylN5Kt9YqyfDUerwnIWDWqcWtfepBcc=; b=Jy1XWIruO0qByZarEt84oOuEeatgnpyCEQ/aNZBKWfzybdurVzhnQKbx1Z78ODRy4C nrc/n06tkFNsPC6YOCdGzpoQ0z/lWeglJUcyzUpSq5PP58jKcsORwEsFkXeAn+03EBLp K60k8C4SSr6sLDMiARBCW+h/175LxcGw75ZIPGNDI4/cEA0NXQnEoivpHO4S+NvgMEdd mB/6TNc4CqlgcEDohmhCvREzBRAs5zdMJu9aGYNs6qgsg/gkPFJwodLmseYnsE8/RU6L 6+CyB2ahJDg/KD56fN6APdpFc+Bj5cX5WdlQCqDEcoryU+efqrVFHaQoQ8XgINZzP3xT eHBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695480599; x=1696085399; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=nsfSTO1+ET/8ylN5Kt9YqyfDUerwnIWDWqcWtfepBcc=; b=mEU4lxHFeb7tP/h/1aCoEqRF1pbEQ+qsXa981kWoAKdrIpvAcHWUialEQTvW0Ywjwq WCIMj3iuEIKcY82+J1Y639izmVom7a/C0Z5sMjuDF+fquiWlTX/ze2NRN9A5igS6zWww YvZnu5ASTcmyzgHcL9ZH2Ts0Zpwxz4DMSJ5MbYE4OFppo+Est4sAUa1I/KJBXdV6E9hw lLiywzTCaezMd8kYBAYEdxxf+1C+JDxexAPz3nRGAhr4b8eBIHRgog3fru90IS/cDYgY XK+bKv/OT47TxrgGfriT0O9A4UhR6RLIWhaNM7yUDKcI8dhSvMlKuPkr3FMAIrBm3sY+ E/ww== X-Gm-Message-State: AOJu0Yy+xnyv7BWzdg4kADU807stYHCwxFXXTkKDnLXsZtcXHR57JnM3 2R8OR4kFvZNmVpOHje1D2zjFvo6bPBM= X-Received: by 2002:a17:902:ed8f:b0:1c5:7470:5658 with SMTP id e15-20020a170902ed8f00b001c574705658mr1502288plj.13.1695480598631; Sat, 23 Sep 2023 07:49:58 -0700 (PDT) Received: from wedsonaf-dev.home.lan ([189.124.190.154]) by smtp.googlemail.com with ESMTPSA id s3-20020a170902ea0300b001c42d4b3675sm5392806plg.309.2023.09.23.07.49.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Sep 2023 07:49:58 -0700 (PDT) From: Wedson Almeida Filho To: rust-for-linux@vger.kernel.org Cc: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?B?= =?utf-8?q?j=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , linux-kernel@vger.kernel.org, Wedson Almeida Filho Subject: [PATCH v2 2/2] rust: arc: remove `ArcBorrow` in favour of `WithRef` Date: Sat, 23 Sep 2023 11:49:38 -0300 Message-Id: <20230923144938.219517-3-wedsonaf@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230923144938.219517-1-wedsonaf@gmail.com> References: <20230923144938.219517-1-wedsonaf@gmail.com> MIME-Version: 1.0 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 (snail.vger.email [0.0.0.0]); Sat, 23 Sep 2023 07:50:16 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777856438133137371 X-GMAIL-MSGID: 1777856438133137371 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 Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Benno Lossin --- 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`]. /// - /// 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 } }