Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp2203133rwl; Thu, 30 Mar 2023 07:21:11 -0700 (PDT) X-Google-Smtp-Source: AKy350bxOAV0G/MNgB00Xlub6ebIZHzBCQcJrwTfxkE6+DlMgpyCl+EJMOCJLhHS60ML3L7mSGR8 X-Received: by 2002:a17:902:7d89:b0:19c:f005:92de with SMTP id a9-20020a1709027d8900b0019cf00592demr2423266plm.4.1680186071314; Thu, 30 Mar 2023 07:21:11 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1680186071; cv=pass; d=google.com; s=arc-20160816; b=Ft7fgEsPMvTAziuru28mh6l1lYXeqbfmcAD2blAFls2yM+RZjeQFmkLetrf3cn8GZL QThFSbdH9V97/QWfDRsGSovZKhnRFOf1nZsKKJEw85kFQXBL/lc4oyyN79DaZ8QKPSYH yaJukrWKy7gNSrSjWNorUWDavuyhRp21UOElUVHK9eOyTOkViYuQlhQ9P2z96zVot8u4 WFqx0OPHJns/bRzFSsejXKupct+MUHTQ/e3dx9P7S+zZgYnJK8XjiUbEerpoUMH7FMiL Q1Ydcz44fk8ssJ6npCOoWbey2OjtL4P6jQgi7LAj2bSski2kHQC8jJ/eYmV6DRhsx7Vs D/RQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=DQztMGI/Big+PDfTjOMsWQbqSuvcXJiht2yCGruKEI4=; b=1AKOL47lpsT+UPwlEiUBR0SEUXyXKxcQB7oVq3hVMMjimR/WIHiUsLiC9olakjUDwi otBKnCw8+aNfB7wi34WvfiDHVacm5GoDqvpZVjTOoyfaLcrBqrD9Nnh5usp7z8UV5Ouj odbboCpJNMLqETmC0/RsLXs9rWBDnSdWjt/TMzgCu9olff7woT0wqxiIEE6K6s2SQt/7 9x9CGGxSxKzOLl+eDG7hB9IBSJCF181kRCISBqaxmfQN2dfGFQ36/l81F6ICzaipI0C/ GUR8tOyae+HHJpYK2zpVDIayMwJyDnQIRJV47kONIzQ/0kETJAmsPwh8DVWk8onCKeUr SJBA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@garyguo.net header.s=selector1 header.b=LgDRpZcx; arc=pass (i=1 spf=pass spfdomain=garyguo.net dkim=pass dkdomain=garyguo.net dmarc=pass fromdomain=garyguo.net); 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id lf13-20020a170902fb4d00b001a249d5fb71si9120569plb.222.2023.03.30.07.20.58; Thu, 30 Mar 2023 07:21:11 -0700 (PDT) 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=@garyguo.net header.s=selector1 header.b=LgDRpZcx; arc=pass (i=1 spf=pass spfdomain=garyguo.net dkim=pass dkdomain=garyguo.net dmarc=pass fromdomain=garyguo.net); 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232248AbjC3OSu (ORCPT + 99 others); Thu, 30 Mar 2023 10:18:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42210 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232357AbjC3OSi (ORCPT ); Thu, 30 Mar 2023 10:18:38 -0400 Received: from GBR01-CWL-obe.outbound.protection.outlook.com (mail-cwlgbr01on2117.outbound.protection.outlook.com [40.107.11.117]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 89431CC2C; Thu, 30 Mar 2023 07:18:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=iJA0rw38LOZ0HIY1jSLwJY3i3a4KEvs6VYr3ju3QTUYd/ZFxTNZAITaqbCT4g1aNoqQr7kzRJsGPolCrsmD6KqPznq+ss9kXh4/ARy/ZCavSDEKhKdWGJ1Vkge59LRckQVvwdhoDBNni4uPaOx7FetnvwkMN7mUbNi7bIrv3ZzoYf/vSfDttdZhchjJ5Guu7F/iHvHQOvIEsWx5+AGNkpoQrhSAWvcN6CSpwlA9OytYTGOURZPEfP5O+5y74c9ksLixSel2CaRHn+N4hQ6OJp/JTYzKoNyjAn/UGl0FVhoNRmEMqG3QMBcE/NegPOeXpF8EGbZKu2ajBUyhlkbR8cQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=DQztMGI/Big+PDfTjOMsWQbqSuvcXJiht2yCGruKEI4=; b=Q4zrC5cDVh6Ib+ygMB1Uf/jmbx1aVwR/3EVN/Xf5zPR0Il527bfrQnqJ3IzRAhUnV7fOhbuEbvc7vMrGN9Tih7Fm99cnHNp7fWkyzOIiXXyl83MTuLd2cy0iNGjDcqpdQAR75MwXGdA5aJCIawtqCbzsWxpkuhLeWn16ZFQtuC61qAMRZHW2VBNxGCp436Cw7QggeI/dxKBcGmwk/sma4ZcifjVEXYg78HhOjCcnIy8qs5H7yb9n3Jom9Ns2vVtMgpH4pTYTz1s8cZtbWP8xF+JhQWypb/hMb0uvz/+0obb6oJzwXWUX0kz22TkSAqivYxVc/hzSRAKSlHCVQlKa2Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=garyguo.net; dmarc=pass action=none header.from=garyguo.net; dkim=pass header.d=garyguo.net; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=garyguo.net; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=DQztMGI/Big+PDfTjOMsWQbqSuvcXJiht2yCGruKEI4=; b=LgDRpZcxRaJtEtJKvgaAOSeHUMUvitzmYTt0GkjbHA4wUUT6Xt1cGkpUmhKp+QjNyd3boYGf1a5hlJQ/8+0s4Z+bWgAJ9OUfWRcZtITgHNZFsjJIUT5FzOgPnZSoINiOP15HYy9esMT0FtcASaRubxkxaBAoh+IzDKuazIuxPS8= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=garyguo.net; Received: from LO2P265MB5183.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:253::10) by LOYP265MB2256.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:11a::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6254.21; Thu, 30 Mar 2023 14:17:29 +0000 Received: from LO2P265MB5183.GBRP265.PROD.OUTLOOK.COM ([fe80::2f2a:55d4:ea1d:dece]) by LO2P265MB5183.GBRP265.PROD.OUTLOOK.COM ([fe80::2f2a:55d4:ea1d:dece%4]) with mapi id 15.20.6222.035; Thu, 30 Mar 2023 14:17:29 +0000 Date: Thu, 30 Mar 2023 15:17:28 +0100 From: Gary Guo To: Wedson Almeida Filho Cc: rust-for-linux@vger.kernel.org, Miguel Ojeda , Alex Gaynor , Boqun Feng , =?UTF-8?B?QmrDtnJu?= Roy Baron , linux-kernel@vger.kernel.org, Wedson Almeida Filho Subject: Re: [PATCH 08/13] rust: introduce `ARef` Message-ID: <20230330151728.5f84a692.gary@garyguo.net> In-Reply-To: <20230330043954.562237-8-wedsonaf@gmail.com> References: <20230330043954.562237-1-wedsonaf@gmail.com> <20230330043954.562237-8-wedsonaf@gmail.com> X-Mailer: Claws Mail 4.0.0 (GTK+ 3.24.33; x86_64-pc-linux-gnu) Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-ClientProxiedBy: LO6P123CA0035.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:2fe::8) To LO2P265MB5183.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:253::10) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LO2P265MB5183:EE_|LOYP265MB2256:EE_ X-MS-Office365-Filtering-Correlation-Id: 09877d05-75ee-4ffd-6e63-08db31297ef6 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: gLEX1F1oHlx7QtwBccZPxJlhl0VowH2RYQIMVTTjRP6hdw4IxHzvlxjH/ojX0NG8XHVMa+PLQwVbBcg0V8FFj5rnOfUqLIRip9uslECGHBA43UdzfpBa3WeY/RjV1tMJldJvVUCK00g9DqCvHLG5ZGFnMhq3QUIQoa6P+712tM1UkNWkuAKeoBbW9XqWQQqfTwSKhAz23aJn7LSu7rnS4AvMMm6JOb6FabVynWAQyAwA7LXpKphwGF7Qg9V7S9xtx3JsaZu9VH1V1e+7RnBrJevp0aGCLKMKsKzTFdONTIWBQmzAnR8nOR2F4UWaVLwsfT2csAqi0SV4yGDqNocLKEdkxPeFskyW5uYPPMKeKg/NTmONPmiBLZIPDkAheUN9dqvruiTEqncufCrLyi42XsqiZ60kjKGE16u2vofFZ7GyOKpVEvArqtcvKqcHrr0jnVQPfc00zSgrlDVRtJBsYWMtqUMRFBn6LVe+RYIScvrukowdWax7M7brIVzRS5MTzd7/aUgkI4rO3BgT7WfkFv2CEURN7ERRBR/gHcN2QJGM81y6KEHW0gJ3yc2TakvX2gwUxB7sr0uP1gwL0LaXfOLApCcFPp4dYbcnonNGXjw= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LO2P265MB5183.GBRP265.PROD.OUTLOOK.COM;PTR:;CAT:NONE;SFS:(13230028)(39840400004)(136003)(366004)(396003)(346002)(376002)(451199021)(38100700002)(6916009)(8936002)(5660300002)(66946007)(36756003)(66476007)(8676002)(41300700001)(66556008)(4326008)(6486002)(2616005)(54906003)(26005)(1076003)(86362001)(6506007)(2906002)(6512007)(186003)(478600001)(45080400002)(316002)(81973001);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?OgVTariO+ho7mcreQlWE/EWEtWvZmW5H1PFU3vNBWXOi1D0cIh/UsG3HAWjN?= =?us-ascii?Q?IPHWWx56Rio+c6em33+3SG771YzttJdS0Ylr+W/bj/+u2oO8w5hNn2Jyi88D?= =?us-ascii?Q?qmZZ30/hjAPkgOnXVdXdJRTwjMXqWWq8MrBauS13L+hsUtTQPO1kK/KOYiTS?= =?us-ascii?Q?spJvlg9IaHvxSXMtyBzuHmkeFPEOMUjuiakxtqVX3FAYA70qW7MLccMhsNQH?= =?us-ascii?Q?RnbXKBsZ/eNHc2gisXXiX4b0DZtHMjLOUXcM8EZZd7muPu0ewwWrZtR6EVoC?= =?us-ascii?Q?SN1lxE1Tbqu/d5Un0PNPlOC4gD6/fTOBUJBwkEluw4AiX1mimZBW99AZpyb7?= =?us-ascii?Q?yNmwJ4jqhR33jUQh+BwvQXbOOIv1j0fyC9EKYNakA8NpJqcxSYNRsSzPzr7Y?= =?us-ascii?Q?wQr3lVyL5DVJw2w0wiobLLlik51UmgQh4qEQnb91A8gIrBUcwsmLKVFVGaeu?= =?us-ascii?Q?zmAVh8Oywsdy54jexHZ7EiS+o0QbEa2K21zs3FKZjdbl8FnBHCcw4pYTz5Ft?= =?us-ascii?Q?92Di4bQkuhz3x+fPXtnuYoFskbd2aeuY266MtLuOd5mV8YCWYcBJfTXi7L1X?= =?us-ascii?Q?4Y2mcpYhdD9PVOmh1PoALpI5s7yA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?Dc+cIv8r5wZvtVYhxlkvMIxetybzmj9lIFECgjkr1BReU+xg1c/dyVkH+L8J?= =?us-ascii?Q?JbQPUSTTDxsKC0nZUmA28lxJmsFI/gXtZsSkMijQ?= X-OriginatorOrg: garyguo.net X-MS-Exchange-CrossTenant-Network-Message-Id: 09877d05-75ee-4ffd-6e63-08db31297ef6 X-MS-Exchange-CrossTenant-AuthSource: LO2P265MB5183.GBRP265.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Mar 2023 14:17:29.7938 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: bbc898ad-b10f-4e10-8552-d9377b823d45 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: pPDbq3Z/uHNeyDF9GxTxV8dp7e5KC9HUWPlT9viQjt8HdTzR/dz/KHZLCNCFjsk2YSSm4wJkpZsQvo9XLMU0zA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: LOYP265MB2256 X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2, SPF_HELO_PASS,SPF_PASS autolearn=unavailable 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 Thu, 30 Mar 2023 01:39:49 -0300 Wedson Almeida Filho wrote: > From: Wedson Almeida Filho > > This is an owned reference to an object that is always ref-counted. This > is meant to be used in wrappers for C types that have their own ref > counting functions, for example, tasks, files, inodes, dentries, etc. > > Signed-off-by: Wedson Almeida Filho Reviewed-by: Gary Guo > --- > rust/kernel/types.rs | 107 +++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 107 insertions(+) > > diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs > index dbfae9bb97ce..b071730253c7 100644 > --- a/rust/kernel/types.rs > +++ b/rust/kernel/types.rs > @@ -6,8 +6,10 @@ use crate::init::{self, PinInit}; > use alloc::boxed::Box; > use core::{ > cell::UnsafeCell, > + marker::PhantomData, > mem::MaybeUninit, > ops::{Deref, DerefMut}, > + ptr::NonNull, > }; > > /// Used to transfer ownership to and from foreign (non-Rust) languages. > @@ -295,6 +297,111 @@ opaque_init_funcs! { > "Rust" manual_init4(arg1: A1, arg2: A2, arg3: A3, arg4: A4); > } > > +/// Types that are _always_ reference counted. > +/// > +/// It allows such types to define their own custom ref increment and decrement functions. > +/// Additionally, it allows users to convert from a shared reference `&T` to an owned reference > +/// [`ARef`]. > +/// > +/// This is usually implemented by wrappers to existing structures on the C side of the code. For > +/// Rust code, the recommendation is to use [`Arc`](crate::sync::Arc) to create reference-counted > +/// instances of a type. > +/// > +/// # Safety > +/// > +/// Implementers must ensure that increments to the reference count keep the object alive in memory > +/// at least until matching decrements are performed. > +/// > +/// Implementers must also ensure that all instances are reference-counted. (Otherwise they > +/// won't be able to honour the requirement that [`AlwaysRefCounted::inc_ref`] keep the object > +/// alive.) > +pub unsafe trait AlwaysRefCounted { > + /// Increments the reference count on the object. > + fn inc_ref(&self); > + > + /// Decrements the reference count on the object. > + /// > + /// Frees the object when the count reaches zero. > + /// > + /// # Safety > + /// > + /// Callers must ensure that there was a previous matching increment to the reference count, > + /// and that the object is no longer used after its reference count is decremented (as it may > + /// result in the object being freed), unless the caller owns another increment on the refcount > + /// (e.g., it calls [`AlwaysRefCounted::inc_ref`] twice, then calls > + /// [`AlwaysRefCounted::dec_ref`] once). > + unsafe fn dec_ref(obj: NonNull); > +} > + > +/// An owned reference to an always-reference-counted object. > +/// > +/// The object's reference count is automatically decremented when an instance of [`ARef`] is > +/// dropped. It is also automatically incremented when a new instance is created via > +/// [`ARef::clone`]. > +/// > +/// # Invariants > +/// > +/// The pointer stored in `ptr` is non-null and valid for the lifetime of the [`ARef`] instance. In > +/// particular, the [`ARef`] instance owns an increment on the underlying object's reference count. > +pub struct ARef { > + ptr: NonNull, > + _p: PhantomData, > +} > + > +impl ARef { > + /// Creates a new instance of [`ARef`]. > + /// > + /// It takes over an increment of the reference count on the underlying object. > + /// > + /// # Safety > + /// > + /// Callers must ensure that the reference count was incremented at least once, and that they > + /// are properly relinquishing one increment. That is, if there is only one increment, callers > + /// must not use the underlying object anymore -- it is only safe to do so via the newly > + /// created [`ARef`]. > + pub unsafe fn from_raw(ptr: NonNull) -> Self { > + // INVARIANT: The safety requirements guarantee that the new instance now owns the > + // increment on the refcount. > + Self { > + ptr, > + _p: PhantomData, > + } > + } > +} > + > +impl Clone for ARef { > + fn clone(&self) -> Self { > + self.inc_ref(); > + // SAFETY: We just incremented the refcount above. > + unsafe { Self::from_raw(self.ptr) } > + } > +} > + > +impl Deref for ARef { > + type Target = T; > + > + fn deref(&self) -> &Self::Target { > + // SAFETY: The type invariants guarantee that the object is valid. > + unsafe { self.ptr.as_ref() } > + } > +} > + > +impl From<&T> for ARef { > + fn from(b: &T) -> Self { > + b.inc_ref(); > + // SAFETY: We just incremented the refcount above. > + unsafe { Self::from_raw(NonNull::from(b)) } > + } > +} > + > +impl Drop for ARef { > + fn drop(&mut self) { > + // SAFETY: The type invariants guarantee that the `ARef` owns the reference we're about to > + // decrement. > + unsafe { T::dec_ref(self.ptr) }; > + } > +} > + > /// A sum type that always holds either a value of type `L` or `R`. > pub enum Either { > /// Constructs an instance of [`Either`] containing a value of type `L`.