Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp1017925rwl; Fri, 31 Mar 2023 05:50:24 -0700 (PDT) X-Google-Smtp-Source: AKy350ZlIUR4RQNFa+Dxc9aBTMb1OOSLVN0hfiMYISWFEgTJKohkeWb/Fw4HgonMVD903rmx/Rg8 X-Received: by 2002:a17:906:18d:b0:92f:39d9:1e50 with SMTP id 13-20020a170906018d00b0092f39d91e50mr4099722ejb.3.1680267023921; Fri, 31 Mar 2023 05:50:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680267023; cv=none; d=google.com; s=arc-20160816; b=nxWiv93mCq9MqBYBP4GD1QtUQdKrCkWK2e4LdyIpvaiN9dBoeSLrLKONCzymmh7OVw lk0VzGYDZ0xePqjWECi1pCQhESl/zEowTwbThfg2DMrv2CRrqYXQcHl2PCs0HgHN4LuV NR8DJOM7nthsQ8zZllDk5uh9Junq7Gh9VJmoqVAlVhKQYJKOmbu5tSU1+V+vEnfDOggf 73zfudGP7X0MoVxQtQaryM9ioXKE48GIDyYkPfAFqMWpAlblzOLPGdlJTJ0FzMs1qdkv wNO2s3rLYvq3EzLDMA0Eai965HZgVgwP8PeSwnkH0F1H84d3wWHGlucFvd/7Obehhb5G gmmw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:in-reply-to:date:subject :cc:to:from:user-agent:references:dkim-signature; bh=Pj2oN2LwZ2WDVYe/kJrJYhEskC0mu3RJkM2yXvZMoo0=; b=1GMTZSBBYLLgYqW+0Ja4oBX12b3bs834TMlKnH3sKq2oTEqJr5UHpd7svitNy8tjmg SeN3Kw7XRuhrHSOPSK6orKzyHgw/d4Nbi3aym++dIkJeNqCu60gJySM0H0BKd3xIH6kf 0V062ijj1vyxv14lfCAH5jbeYyH4ZA5rUmqpZpWmdlck4Kl0FsExckidnqy1FrVXCeuA rdk9pdO2DEGn6fCn7v4y0pykWOWPqV48J1LAt0MXhLIhv1jlIp3fEU1dU59F7f/w9ubF WmnwdQXYHO8FasRQB46ZR5BC8ZFTW0TNxmrzBpZl+VWcwaFMOjlKWRRiuxJY8VyzZ9tG TiRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@metaspace-dk.20210112.gappssmtp.com header.s=20210112 header.b=romFrc+y; 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 lb13-20020a170907784d00b009333d91e361si1974888ejc.592.2023.03.31.05.49.59; Fri, 31 Mar 2023 05:50:23 -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=@metaspace-dk.20210112.gappssmtp.com header.s=20210112 header.b=romFrc+y; 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 S232401AbjCaMok (ORCPT + 99 others); Fri, 31 Mar 2023 08:44:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39878 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232032AbjCaMog (ORCPT ); Fri, 31 Mar 2023 08:44:36 -0400 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 146832063B for ; Fri, 31 Mar 2023 05:44:12 -0700 (PDT) Received: by mail-wr1-x430.google.com with SMTP id l27so22264910wrb.2 for ; Fri, 31 Mar 2023 05:44:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=metaspace-dk.20210112.gappssmtp.com; s=20210112; t=1680266648; h=mime-version:message-id:in-reply-to:date:subject:cc:to:from :user-agent:references:from:to:cc:subject:date:message-id:reply-to; bh=Pj2oN2LwZ2WDVYe/kJrJYhEskC0mu3RJkM2yXvZMoo0=; b=romFrc+yurgbCwzfpHvf55mEkveW/UWgdUQvLlSgn+vNSyDFcUbgBY6A5BD6RLggkS WFOb/znbl01YD+TZx9uMbPqgmpfGbTf9s9+lNnFUnvgbNAJbhVGCd5PKqxY6SfLU+0yT TvIr4GX4qQ/A2nqAmxsCIQSQ69eLj/KN1ybe0we8EHFy5iFRQOOwNUhwl219CA223pis Vj9z1byg7h9oEldI6Gog2U6NVyJYXVTP/3eUm96zH8KWMDzD9t4YtrLdvA7vjrxc4B97 o5Yg/fGOZd/8zHhTqrS3oNdz2D0Jp2YK3Di+n8X1CZwCDcqAvwxh62DxzzkoYk+Wb1yo PPUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680266648; h=mime-version:message-id:in-reply-to:date:subject:cc:to:from :user-agent:references:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=Pj2oN2LwZ2WDVYe/kJrJYhEskC0mu3RJkM2yXvZMoo0=; b=RvoP8kKzprIhrCh3PdLxxXC3ZAFUqlPvE22VCXiaj4CAP5d2aontnP8fEeaVZ+aOci dwdEsnVecqNa6a84U9kaIcocZM/kuToaLr/RcwgYc32HzC5Rwu7a0SyyCs3lFzISidat jnZbp1vrBWDQZysUD7cMdfn7vhN6oQE5KQOz/qHFAQtdvNuEQ8IJJpUjXEm166YHm5w/ UkckDF95oOJun5Wkybr8o6Qy2JPR0ZVSNaEUqCLLJvycxHDGSFnMtDhGyzJq8wjlFRpx sMhHXvOSsBYMaxrN2+IMB7H3MmV2N7oPo22dhLDlbK4k4Xbu5AZLf+VCQ2QgaSTr6811 F1yQ== X-Gm-Message-State: AAQBX9eKO30+6y6i09oCHP3hLvYPLknrExe0w5ZKHm8hkKYM8eQspcR/ pa0CNxwb+yIjfDb3cR8VJu0GhQ== X-Received: by 2002:a5d:44c4:0:b0:2c5:4f2d:d9b4 with SMTP id z4-20020a5d44c4000000b002c54f2dd9b4mr20341258wrr.58.1680266648536; Fri, 31 Mar 2023 05:44:08 -0700 (PDT) Received: from localhost ([165.225.194.194]) by smtp.gmail.com with ESMTPSA id s12-20020adfeccc000000b002da1261aa44sm2155877wro.48.2023.03.31.05.44.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 Mar 2023 05:44:08 -0700 (PDT) References: <20230329223239.138757-1-y86-dev@protonmail.com> <20230329223239.138757-9-y86-dev@protonmail.com> User-agent: mu4e 1.9.18; emacs 28.2.50 From: Andreas Hindborg To: y86-dev@protonmail.com Cc: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn?= Roy Baron , Alice Ryhl , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Andreas Hindborg Subject: Re: [PATCH v3 08/13] rust: init: add `stack_pin_init!` macro Date: Fri, 31 Mar 2023 14:43:33 +0200 In-reply-to: <20230329223239.138757-9-y86-dev@protonmail.com> Message-ID: <87fs9l2iqw.fsf@metaspace.dk> MIME-Version: 1.0 Content-Type: text/plain X-Spam-Status: No, score=0.0 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE 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 y86-dev@protonmail.com writes: > From: Benno Lossin > > The `stack_pin_init!` macro allows pin-initializing a value on the > stack. It accepts a `impl PinInit` to initialize a `T`. It allows > propagating any errors via `?` or handling it normally via `match`. > > Signed-off-by: Benno Lossin > --- Reviewed-by: Andreas Hindborg > rust/kernel/init.rs | 70 +++++++++++++++++++++++++++++++--- > rust/kernel/init/__internal.rs | 46 ++++++++++++++++++++++ > 2 files changed, 110 insertions(+), 6 deletions(-) > > diff --git a/rust/kernel/init.rs b/rust/kernel/init.rs > index 428b5c2ac516..3358f14beffb 100644 > --- a/rust/kernel/init.rs > +++ b/rust/kernel/init.rs > @@ -12,7 +12,8 @@ > //! > //! To initialize a `struct` with an in-place constructor you will need two things: > //! - an in-place constructor, > -//! - a memory location that can hold your `struct`. > +//! - a memory location that can hold your `struct` (this can be the [stack], an [`Arc`], > +//! [`UniqueArc`], [`Box`] or any other smart pointer that implements [`InPlaceInit`]). > //! > //! To get an in-place constructor there are generally three options: > //! - directly creating an in-place constructor using the [`pin_init!`] macro, > @@ -180,6 +181,7 @@ > //! [pinning]: https://doc.rust-lang.org/std/pin/index.html > //! [structurally pinned fields]: > //! https://doc.rust-lang.org/std/pin/index.html#pinning-is-structural-for-field > +//! [stack]: crate::stack_pin_init > //! [`Arc`]: crate::sync::Arc > //! [`impl PinInit`]: PinInit > //! [`impl PinInit`]: PinInit > @@ -199,6 +201,62 @@ pub mod __internal; > #[doc(hidden)] > pub mod macros; > > +/// Initialize and pin a type directly on the stack. > +/// > +/// # Examples > +/// > +/// ```rust > +/// # #![allow(clippy::disallowed_names, clippy::new_ret_no_self)] > +/// # use kernel::{init, pin_init, stack_pin_init, init::*, sync::Mutex, new_mutex}; > +/// # use macros::pin_data; > +/// # use core::pin::Pin; > +/// #[pin_data] > +/// struct Foo { > +/// #[pin] > +/// a: Mutex, > +/// b: Bar, > +/// } > +/// > +/// #[pin_data] > +/// struct Bar { > +/// x: u32, > +/// } > +/// > +/// let a = new_mutex!(42, "Foo::a"); > +/// > +/// stack_pin_init!(let foo =? pin_init!(Foo { > +/// a, > +/// b: Bar { > +/// x: 64, > +/// }, > +/// })); > +/// let foo: Pin<&mut Foo> = foo; > +/// # Ok::<(), core::convert::Infallible>(()) > +/// ``` > +/// > +/// # Syntax > +/// > +/// A normal `let` binding with optional type annotation. The expression is expected to implement > +/// [`PinInit`]. Additionally a `?` can be put after the `=`, this will assign `Pin<&mut T>` to the > +/// variable instead of `Result, E>`. > +#[macro_export] > +macro_rules! stack_pin_init { > + (let $var:ident $(: $t:ty)? = $val:expr) => { > + let mut $var = $crate::init::__internal::StackInit$(::<$t>)?::uninit(); > + let mut $var = { > + let val = $val; > + unsafe { $crate::init::__internal::StackInit::init(&mut $var, val) } > + }; > + }; > + (let $var:ident $(: $t:ty)? =? $val:expr) => { > + let mut $var = $crate::init::__internal::StackInit$(::<$t>)?::uninit(); > + let mut $var = { > + let val = $val; > + unsafe { $crate::init::__internal::StackInit::init(&mut $var, val)? } > + }; > + }; > +} > + > /// Construct an in-place, pinned initializer for `struct`s. > /// > /// This macro defaults the error to [`Infallible`]. If you need [`Error`], then use > @@ -916,8 +974,8 @@ macro_rules! try_init { > /// A pinned initializer for `T`. > /// > /// To use this initializer, you will need a suitable memory location that can hold a `T`. This can > -/// be [`Box`], [`Arc`], [`UniqueArc`]. Use the [`InPlaceInit::pin_init`] function of a > -/// smart pointer like [`Arc`] on this. > +/// be [`Box`], [`Arc`], [`UniqueArc`] or even the stack (see [`stack_pin_init!`]). Use the > +/// [`InPlaceInit::pin_init`] function of a smart pointer like [`Arc`] on this. > /// > /// Also see the [module description](self). > /// > @@ -952,9 +1010,9 @@ pub unsafe trait PinInit: Sized { > /// An initializer for `T`. > /// > /// To use this initializer, you will need a suitable memory location that can hold a `T`. This can > -/// be [`Box`], [`Arc`], [`UniqueArc`]. Use the [`InPlaceInit::init`] function of a smart > -/// pointer like [`Arc`] on this. Because [`PinInit`] is a super trait, you can > -/// use every function that takes it as well. > +/// be [`Box`], [`Arc`], [`UniqueArc`] or even the stack (see [`stack_pin_init!`]). Use the > +/// [`InPlaceInit::init`] function of a smart pointer like [`Arc`] on this. Because > +/// [`PinInit`] is a super trait, you can use every function that takes it as well. > /// > /// Also see the [module description](self). > /// > diff --git a/rust/kernel/init/__internal.rs b/rust/kernel/init/__internal.rs > index 4a3c7bf27a06..bf33c8e96e6d 100644 > --- a/rust/kernel/init/__internal.rs > +++ b/rust/kernel/init/__internal.rs > @@ -89,6 +89,52 @@ unsafe impl HasInitData for T { > } > } > > +/// Stack initializer helper type. Use [`stack_pin_init`] instead of this primitive. > +/// > +/// # Invariants > +/// > +/// If `self.1` is true, then `self.0` is initialized. > +/// > +/// [`stack_pin_init`]: kernel::stack_pin_init > +pub struct StackInit(MaybeUninit, bool); > + > +impl Drop for StackInit { > + #[inline] > + fn drop(&mut self) { > + if self.1 { > + // SAFETY: As we are being dropped, we only call this once. And since `self.1 == true`, > + // `self.0` has to be initialized. > + unsafe { self.0.assume_init_drop() }; > + } > + } > +} > + > +impl StackInit { > + /// Creates a new [`StackInit`] that is uninitialized. Use [`stack_pin_init`] instead of this > + /// primitive. > + /// > + /// [`stack_pin_init`]: kernel::stack_pin_init > + #[inline] > + pub fn uninit() -> Self { > + Self(MaybeUninit::uninit(), false) > + } > + > + /// Initializes the contents and returns the result. > + /// > + /// # Safety > + /// > + /// The caller ensures that `self` is on the stack and not accessible in any other way, if this > + /// function returns `Ok`. > + #[inline] > + pub unsafe fn init(&mut self, init: impl PinInit) -> Result, E> { > + // SAFETY: The memory slot is valid and this type ensures that it will stay pinned. > + unsafe { init.__pinned_init(self.0.as_mut_ptr())? }; > + self.1 = true; > + // SAFETY: The slot is now pinned, since we will never give access to `&mut T`. > + Ok(unsafe { Pin::new_unchecked(self.0.assume_init_mut()) }) > + } > +} > + > /// When a value of this type is dropped, it drops a `T`. > /// > /// Can be forgotton to prevent the drop.