Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp13455164rwl; Wed, 4 Jan 2023 08:22:46 -0800 (PST) X-Google-Smtp-Source: AMrXdXsOmRRCW+ehg0Ba8VERzCC3N9sImx5ubqFH6i0VRuarF4hzjMtlEXK4ErkSG5Y5lz5/lF9V X-Received: by 2002:a05:6402:33a:b0:47b:2524:5cf6 with SMTP id q26-20020a056402033a00b0047b25245cf6mr41699182edw.40.1672849366525; Wed, 04 Jan 2023 08:22:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672849366; cv=none; d=google.com; s=arc-20160816; b=rLHraIak5h2do3/dKbWYK+NxDw6I2hJoTSRH1YkOC3uJTALoblvmsz/irLtcqEu38k zFEd+pcI74zCDhcJerlbRImv1Nqcf4k9SEQCJBjvaPUZ8HIu9cDHETivsRawxyA6UAkJ 9g3bH0OCPG+k4w1E7QN4ttV6PU/KdTDwDcLF5f65ytgq0OYaN9GiusBmRPmW6uTgnfkd 2RUua/P//nMFgxUS7yBRszyaMVAzmNNKdBpzL8MY9RdlGNqE0gIuYI5MlWfeWX8IbRp/ DcaABOztnrk1JMj5eR+wYy+9rYVDQG2daqi/uv2LAVga7ES4a4RVWP9J7O2xLmUPsf/7 b7Zw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:references:to:from:subject:cc :message-id:date:content-transfer-encoding:mime-version :dkim-signature; bh=S78DmMftxVSawcOYgDZLsYS9BKbUVx/H9YpxdwVwKvY=; b=hUUo4uznbIYxtT4C4NacvopRX6oFHJ2sRp4uz2K3rl9pTl+SNpuHAhzxQ3iHqVj9Fe nY12jbH+403SeJf01YbZBcd4GyXaMcE5/PxzoARaVJdAzEM1lVOpaLySU0C0jK0rKR+i H3afM1kI+Ki70Io1fQDnqU9/20/OWZMkT48tQlkuiX2UNvcm8GCfA5QMlhdW1fh5dUQK SgUln9gf6YvHxTMJj4mHQHEPSVuo8NSekNJM/HiiOZaSYovasS71vyxhUHPy3hNJsHfO O3Mm6cAbohUEudeV7XCwtO5ld7kJ08YxU0R0OfykUuOregiitvXJbtDtFPSVwQRH/XJ+ lrQg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=PfSH0e+A; 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 d26-20020aa7ce1a000000b00457f31c1a10si27581724edv.584.2023.01.04.08.22.31; Wed, 04 Jan 2023 08:22:46 -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=PfSH0e+A; 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 S239844AbjADQJK (ORCPT + 59 others); Wed, 4 Jan 2023 11:09:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59436 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239841AbjADQIs (ORCPT ); Wed, 4 Jan 2023 11:08:48 -0500 Received: from mail-ej1-x62b.google.com (mail-ej1-x62b.google.com [IPv6:2a00:1450:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3F6243C380; Wed, 4 Jan 2023 08:08:45 -0800 (PST) Received: by mail-ej1-x62b.google.com with SMTP id qk9so83556127ejc.3; Wed, 04 Jan 2023 08:08:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:references:to:from:subject:cc:message-id:date :content-transfer-encoding:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=S78DmMftxVSawcOYgDZLsYS9BKbUVx/H9YpxdwVwKvY=; b=PfSH0e+A+qifLqlDaJDU9D8DmWHiqnjEqP+S9nwIMMKYc/uE9/JhCAA7N7v7mIeu+S 9s5E/BKFEZY2xYQ/m6Ns94ro6m+Jnhb5g1aCPKNEXKOMRLpFtIy92z0PMJNpvGntU8do BBU2gFpS4Ly1XD7OgXrGd7oNgCnQHHuNnRroJ3osy4SK4YDOmCQUQqhjSN477DRcE5MV 8ehkBxB0/j6XlJGRZz0uy+p0WzC5cotqj1sWaI4ScVtGImMgN+jT1RnPOwo5OAG1gu2x Xwh+P+5s/NQVewBd5btkb521/DmMf5cMxZshMV2/r+UcL7qzc3coY+drmtEa7Gwd4KZl piRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:references:to:from:subject:cc:message-id:date :content-transfer-encoding:mime-version:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=S78DmMftxVSawcOYgDZLsYS9BKbUVx/H9YpxdwVwKvY=; b=E076gpYUSsxbRSyG+QqV0EH8aTCFNa8cRj+qT2gZuZ9teYqS73bosRFW2VFt/fBK7T LNmLmtakl22BR1jCAKIhA3sYY/uMGKCeZj/xyP2OC5KOomu3TX14v63DojEbW72PEPec Yx8qYl2n3PHAeuyVYcrd44Zv5Sfc2Br497G/0cLxJfd2JUVpx0Y9viuC8X56eDPhu6fc tkZfx8dlXrCdOslpT9Zk+HTKTb8afhMysHTqwLE7yzq8ZriM4LldA73csA3G+UGy5BVk a8hht7DRKGUPJg9daFJMA+SIy2Kd11aUkNN6JVKhHXX4sIvChQPb+qChE+iySgUsxU/A GYRA== X-Gm-Message-State: AFqh2kodv2AaxlKOgDDZbmGJHBTJdWzy6w7BUfw5KDs/e0ylgx/DfBD7 LeS0KDVt7eX7AJPQmBFIYjM= X-Received: by 2002:a17:907:c70c:b0:7c0:a247:2f3c with SMTP id ty12-20020a170907c70c00b007c0a2472f3cmr43485561ejc.1.1672848523824; Wed, 04 Jan 2023 08:08:43 -0800 (PST) Received: from localhost (host-82-60-200-213.retail.telecomitalia.it. [82.60.200.213]) by smtp.gmail.com with ESMTPSA id b2-20020a1709063ca200b007bd1ef2cccasm15247811ejh.48.2023.01.04.08.08.42 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 04 Jan 2023 08:08:43 -0800 (PST) Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Wed, 04 Jan 2023 17:08:41 +0100 Message-Id: Cc: "Miguel Ojeda" , "Alex Gaynor" , "Boqun Feng" , "Gary Guo" , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , , "Will Deacon" , "Peter Zijlstra" , "Mark Rutland" Subject: Re: [PATCH 1/7] rust: sync: add `Arc` for ref-counted allocations From: "Vincenzo" To: "Wedson Almeida Filho" , X-Mailer: aerc 0.8.2 References: <20221228060346.352362-1-wedsonaf@gmail.com> In-Reply-To: <20221228060346.352362-1-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_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 Dec 28, 2022 at 7:03 AM CET, Wedson Almeida Filho wrote: Reviewed-by: Vincenzo Palazzo > This is a basic implementation of `Arc` backed by C's `refcount_t`. It > allows Rust code to idiomatically allocate memory that is ref-counted. > > Cc: Will Deacon > Cc: Peter Zijlstra > Cc: Boqun Feng > Cc: Mark Rutland > Signed-off-by: Wedson Almeida Filho > --- > rust/bindings/bindings_helper.h | 1 + > rust/bindings/lib.rs | 1 + > rust/helpers.c | 19 ++++ > rust/kernel/lib.rs | 1 + > rust/kernel/sync.rs | 10 ++ > rust/kernel/sync/arc.rs | 157 ++++++++++++++++++++++++++++++++ > 6 files changed, 189 insertions(+) > create mode 100644 rust/kernel/sync.rs > create mode 100644 rust/kernel/sync/arc.rs > > diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_hel= per.h > index c48bc284214a..75d85bd6c592 100644 > --- a/rust/bindings/bindings_helper.h > +++ b/rust/bindings/bindings_helper.h > @@ -7,6 +7,7 @@ > */ > =20 > #include > +#include > =20 > /* `bindgen` gets confused at certain things. */ > const gfp_t BINDINGS_GFP_KERNEL =3D GFP_KERNEL; > diff --git a/rust/bindings/lib.rs b/rust/bindings/lib.rs > index 6c50ee62c56b..7b246454e009 100644 > --- a/rust/bindings/lib.rs > +++ b/rust/bindings/lib.rs > @@ -41,6 +41,7 @@ mod bindings_raw { > #[allow(dead_code)] > mod bindings_helper { > // Import the generated bindings for types. > + use super::bindings_raw::*; > include!(concat!( > env!("OBJTREE"), > "/rust/bindings/bindings_helpers_generated.rs" > diff --git a/rust/helpers.c b/rust/helpers.c > index b4f15eee2ffd..09a4d93f9d62 100644 > --- a/rust/helpers.c > +++ b/rust/helpers.c > @@ -20,6 +20,7 @@ > =20 > #include > #include > +#include > =20 > __noreturn void rust_helper_BUG(void) > { > @@ -27,6 +28,24 @@ __noreturn void rust_helper_BUG(void) > } > EXPORT_SYMBOL_GPL(rust_helper_BUG); > =20 > +refcount_t rust_helper_REFCOUNT_INIT(int n) > +{ > + return (refcount_t)REFCOUNT_INIT(n); > +} > +EXPORT_SYMBOL_GPL(rust_helper_REFCOUNT_INIT); > + > +void rust_helper_refcount_inc(refcount_t *r) > +{ > + refcount_inc(r); > +} > +EXPORT_SYMBOL_GPL(rust_helper_refcount_inc); > + > +bool rust_helper_refcount_dec_and_test(refcount_t *r) > +{ > + return refcount_dec_and_test(r); > +} > +EXPORT_SYMBOL_GPL(rust_helper_refcount_dec_and_test); > + > /* > * We use `bindgen`'s `--size_t-is-usize` option to bind the C `size_t` = type > * as the Rust `usize` type, so we can use it in contexts where Rust > diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs > index 53040fa9e897..ace064a3702a 100644 > --- a/rust/kernel/lib.rs > +++ b/rust/kernel/lib.rs > @@ -31,6 +31,7 @@ mod static_assert; > #[doc(hidden)] > pub mod std_vendor; > pub mod str; > +pub mod sync; > pub mod types; > =20 > #[doc(hidden)] > diff --git a/rust/kernel/sync.rs b/rust/kernel/sync.rs > new file mode 100644 > index 000000000000..39b379dd548f > --- /dev/null > +++ b/rust/kernel/sync.rs > @@ -0,0 +1,10 @@ > +// SPDX-License-Identifier: GPL-2.0 > + > +//! Synchronisation primitives. > +//! > +//! This module contains the kernel APIs related to synchronisation that= have been ported or > +//! wrapped for usage by Rust code in the kernel. > + > +mod arc; > + > +pub use arc::Arc; > diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs > new file mode 100644 > index 000000000000..22290eb5ab9b > --- /dev/null > +++ b/rust/kernel/sync/arc.rs > @@ -0,0 +1,157 @@ > +// SPDX-License-Identifier: GPL-2.0 > + > +//! A reference-counted pointer. > +//! > +//! This module implements a way for users to create reference-counted o= bjects and pointers to > +//! them. Such a pointer automatically increments and decrements the cou= nt, and drops the > +//! underlying object when it reaches zero. It is also safe to use concu= rrently from multiple > +//! threads. > +//! > +//! It is different from the standard library's [`Arc`] in a few ways: > +//! 1. It is backed by the kernel's `refcount_t` type. > +//! 2. It does not support weak references, which allows it to be half t= he size. > +//! 3. It saturates the reference count instead of aborting when it goes= over a threshold. > +//! 4. It does not provide a `get_mut` method, so the ref counted object= is pinned. > +//! > +//! [`Arc`]: https://doc.rust-lang.org/std/sync/struct.Arc.html > + > +use crate::{bindings, error::Result, types::Opaque}; > +use alloc::boxed::Box; > +use core::{marker::PhantomData, ops::Deref, ptr::NonNull}; > + > +/// A reference-counted pointer to an instance of `T`. > +/// > +/// The reference count is incremented when new instances of [`Arc`] are= created, and decremented > +/// when they are dropped. When the count reaches zero, the underlying `= T` is also dropped. > +/// > +/// # Invariants > +/// > +/// The reference count on an instance of [`Arc`] is always non-zero. > +/// The object pointed to by [`Arc`] is always pinned. > +/// > +/// # Examples > +/// > +/// ``` > +/// use kernel::sync::Arc; > +/// > +/// struct Example { > +/// a: u32, > +/// b: u32, > +/// } > +/// > +/// // Create a ref-counted instance of `Example`. > +/// let obj =3D Arc::try_new(Example { a: 10, b: 20 })?; > +/// > +/// // Get a new pointer to `obj` and increment the refcount. > +/// let cloned =3D obj.clone(); > +/// > +/// // Assert that both `obj` and `cloned` point to the same underlying = object. > +/// assert!(core::ptr::eq(&*obj, &*cloned)); > +/// > +/// // Destroy `obj` and decrement its refcount. > +/// drop(obj); > +/// > +/// // Check that the values are still accessible through `cloned`. > +/// assert_eq!(cloned.a, 10); > +/// assert_eq!(cloned.b, 20); > +/// > +/// // The refcount drops to zero when `cloned` goes out of scope, and t= he memory is freed. > +/// ``` > +pub struct Arc { > + ptr: NonNull>, > + _p: PhantomData>, > +} > + > +#[repr(C)] > +struct ArcInner { > + refcount: Opaque, > + data: T, > +} > + > +// SAFETY: It is safe to send `Arc` to another thread when the underl= ying `T` is `Sync` because > +// it effectively means sharing `&T` (which is safe because `T` is `Sync= `); additionally, it needs > +// `T` to be `Send` because any thread that has an `Arc` may ultimate= ly access `T` directly, for > +// example, when the reference count reaches zero and `T` is dropped. > +unsafe impl Send for Arc {} > + > +// SAFETY: It is safe to send `&Arc` to another thread when the under= lying `T` is `Sync` for the > +// same reason as above. `T` needs to be `Send` as well because a thread= can clone an `&Arc` > +// into an `Arc`, which may lead to `T` being accessed by the same re= asoning as above. > +unsafe impl Sync for Arc {} > + > +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 =3D ArcInner { > + // SAFETY: There are no safety requirements for this FFI cal= l. > + refcount: Opaque::new(unsafe { bindings::REFCOUNT_INIT(1) })= , > + data: contents, > + }; > + > + let inner =3D Box::try_new(value)?; > + > + // SAFETY: We just created `inner` with a reference count of 1, = which is owned by the new > + // `Arc` object. > + Ok(unsafe { Self::from_inner(Box::leak(inner).into()) }) > + } > +} > + > +impl Arc { > + /// Constructs a new [`Arc`] from an existing [`ArcInner`]. > + /// > + /// # Safety > + /// > + /// The caller must ensure that `inner` points to a valid location a= nd has a non-zero reference > + /// count, one of which will be owned by the new [`Arc`] instance. > + unsafe fn from_inner(inner: NonNull>) -> Self { > + // INVARIANT: By the safety requirements, the invariants hold. > + Arc { > + ptr: inner, > + _p: PhantomData, > + } > + } > +} > + > +impl Deref for Arc { > + type Target =3D T; > + > + fn deref(&self) -> &Self::Target { > + // SAFETY: By the type invariant, there is necessarily a referen= ce to the object, so it is > + // safe to dereference it. > + unsafe { &self.ptr.as_ref().data } > + } > +} > + > +impl Clone for Arc { > + fn clone(&self) -> Self { > + // INVARIANT: C `refcount_inc` saturates the refcount, so it can= not overflow to zero. > + // SAFETY: By the type invariant, there is necessarily a referen= ce to the object, so it is > + // safe to increment the refcount. > + unsafe { bindings::refcount_inc(self.ptr.as_ref().refcount.get()= ) }; > + > + // SAFETY: We just incremented the refcount. This increment is n= ow owned by the new `Arc`. > + unsafe { Self::from_inner(self.ptr) } > + } > +} > + > +impl Drop for Arc { > + fn drop(&mut self) { > + // SAFETY: By the type invariant, there is necessarily a referen= ce to the object. We cannot > + // touch `refcount` after it's decremented to a non-zero value b= ecause another thread/CPU > + // may concurrently decrement it to zero and free it. It is ok t= o have a raw pointer to > + // freed/invalid memory as long as it is never dereferenced. > + let refcount =3D unsafe { self.ptr.as_ref() }.refcount.get(); > + > + // INVARIANT: If the refcount reaches zero, there are no other i= nstances of `Arc`, and > + // this instance is being dropped, so the broken invariant is no= t observable. > + // SAFETY: Also by the type invariant, we are allowed to decreme= nt the refcount. > + let is_zero =3D unsafe { bindings::refcount_dec_and_test(refcoun= t) }; > + if is_zero { > + // The count reached zero, we must free the memory. > + // > + // SAFETY: The pointer was initialised from the result of `B= ox::leak`. > + unsafe { Box::from_raw(self.ptr.as_ptr()) }; > + } > + } > +} > --=20 > 2.34.1