Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp1035064rwl; Wed, 5 Apr 2023 10:53:27 -0700 (PDT) X-Google-Smtp-Source: AKy350ZuIhwYKvil86HfCpZAxk7wB7Do5TfITn2PmBmO9gi+1frfDiwGADKKmGpOsxOs5BhMmeNX X-Received: by 2002:a17:90b:4a46:b0:234:b4a7:2abd with SMTP id lb6-20020a17090b4a4600b00234b4a72abdmr7797284pjb.12.1680717207028; Wed, 05 Apr 2023 10:53:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680717207; cv=none; d=google.com; s=arc-20160816; b=J4qDw8W1ExGPh1AaKhiILRtnduIffWx3DmcpFlbFdR9ED2d857S47/J6ezevZHqOxM CYUfo6ryPPlBU5qM3Js5HIKFxh+pzSJLUhjrncqNu8tFmcHyqJzxYHS8czUXQH+z7wgN IyrAmRgeWTmJuL0xvL0Otp0xLit8TNkYbDen3Vxb/AnL0XjYaTEzdKV10Jw5dlMN77Y0 oPaYL1awKwPZqEeJAa/u8C3/zcyTcjWv3kozp7aNgaaRR2Bi8bItHBUk4quP8F7VPzP5 SscShjkAv+bAJW0DQV9qNU0NKE0WzzGE3cs6lMQFLS0G3KrmC9rQ6X9cpxuREoGSRWA5 LJcA== 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=yDKrcsZh/wP/lITy5QMY3W9uqlt+ywEoUOGYJrXbNMY=; b=J8MtjNVhR8Rv3ely2Ts2z28JSfpLK2DXDEZrmVMc/TWPl89oYLCLIDzcx6x4Oj1OTD 40O3VaQyFc2el8mZA6cC6LJzTvJpCIXKt8O3pwb45HE/MEd0yDPmFJypSrBBnA4K9ouh PzgvRoG/Q3fg1Ls5rwGzaTphFDIQCyyWzHSPCLWlFZZIn/zt/g97MIHo/jfTDMq+QCxK U0pxQlaiXlD2PjD8p5lNIGoXszJ7mI2uLqh8Nt/M7EJHa8uU9P9H2Bon4zvZfrPnkRDY aiYzWblgbcAatZh89xviyERsv+Z0rep+fUIV0txHXg7Az1/I1PshOQxsGkQtTIv9lGe7 TmTQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="B/OTciN2"; 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 pj5-20020a17090b4f4500b00233a5e3826asi1811726pjb.78.2023.04.05.10.53.15; Wed, 05 Apr 2023 10:53:27 -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=@gmail.com header.s=20210112 header.b="B/OTciN2"; 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 S232955AbjDERwt (ORCPT + 99 others); Wed, 5 Apr 2023 13:52:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40050 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232079AbjDERwr (ORCPT ); Wed, 5 Apr 2023 13:52:47 -0400 Received: from mail-oi1-x235.google.com (mail-oi1-x235.google.com [IPv6:2607:f8b0:4864:20::235]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9EE4A729F; Wed, 5 Apr 2023 10:52:19 -0700 (PDT) Received: by mail-oi1-x235.google.com with SMTP id y184so27232254oiy.8; Wed, 05 Apr 2023 10:52:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680717138; 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=yDKrcsZh/wP/lITy5QMY3W9uqlt+ywEoUOGYJrXbNMY=; b=B/OTciN28kbRHCwGguPcjRZzHoK8qkWPrMjQOltw7nWygfMtMu8Fq3RBOlurtP5vIo cD5ZIDTTmI9fUJGEWt1e/oEQuKOFJEuM0RT9GqGRHPrsJhBnxEMsw0ymiqqAo1WKEmb1 BtPyexqr5J6zBpeCCmo2XosylDfOCLH1G7aTdxJ9TjdBfRFrZNJkl7I+1n6zFaRJ437Q wVgX9fwX0Kg5lgJ6sH24X3XaZ4GNNbGcb3wstSU7fbv6woY5MgnOOmgz3i96fPY3IoCL a2TXs3XSh38D3ybYh9rPZi+OX1UG6EjKHfIkdU4vXA9JV4iIurUSS2fDghs/+dNabLmK 0ePw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680717138; 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=yDKrcsZh/wP/lITy5QMY3W9uqlt+ywEoUOGYJrXbNMY=; b=FamiMgBgYvLQoFWSPtFD1QyjnuY0OYo6I1o1bIykIRN2HGJsXKJ+j7JuYt5dopYHuI 8KxZhmr/UD7UHgxgDJWGrfpRsnz3D7DuiMkAkO6Q3e4pNy2lJxHZTzusARtjweUzxt5f zc+GVa74Ab3l/qi846CJp+26qicD8iyOR+lYMRjFJ1nXJBz3dl/CTsZLTV4UaHWMo7Vx eR7D+R5rTxWTmHBn1hFm1/dYR6Ya4JyJfnvBZCw4VbDnzp1rv5CIJhtIh+dZQd4IFQlx K8rU7kToYcm4Tj27WzpnlA6EmzZlVoLjm9Mv7mi5TWoYcO7L9qXulCZWMU/emCoxQ9Ec 721g== X-Gm-Message-State: AAQBX9camDSVcm1mdbMfZfGXbkUKHtY4KBfUxCEoJX4FZqGjs44wSXeY hIAfZrN7DEbl1F+O7Go65jEghX+nFkI= X-Received: by 2002:aca:2210:0:b0:386:9873:d377 with SMTP id b16-20020aca2210000000b003869873d377mr2780365oic.56.1680717137736; Wed, 05 Apr 2023 10:52:17 -0700 (PDT) Received: from wedsonaf-dev.home.lan ([189.124.190.154]) by smtp.googlemail.com with ESMTPSA id w185-20020aca62c2000000b003896e31867esm6604045oib.49.2023.04.05.10.52.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Apr 2023 10:52:17 -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?Bj=C3=B6rn=20Roy=20Baron?= , linux-kernel@vger.kernel.org, Wedson Almeida Filho Subject: [PATCH v2 08/13] rust: introduce `ARef` Date: Wed, 5 Apr 2023 14:51:06 -0300 Message-Id: <20230405175111.5974-8-wedsonaf@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230405175111.5974-1-wedsonaf@gmail.com> References: <20230405175111.5974-1-wedsonaf@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,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 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 --- v1 -> v2: No changes 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`. -- 2.34.1