Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp2147457rwl; Thu, 30 Mar 2023 06:41:57 -0700 (PDT) X-Google-Smtp-Source: AKy350YzfJdn3yMz6sCmkgdZ6xfs1q1nuO4YX38Y98+1oTJ5eA7mAzQZYL83+OEqui51vDtBH6YU X-Received: by 2002:a17:906:15c:b0:8f5:14ab:94bc with SMTP id 28-20020a170906015c00b008f514ab94bcmr20248425ejh.6.1680183717059; Thu, 30 Mar 2023 06:41:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680183717; cv=none; d=google.com; s=arc-20160816; b=tNOHPfhSodtcqblC/8MZSgRb5I1vxCdaYFUsaDYpB4BsxGwiIt+CO0QEX8TIqvt1kR B90JOZZthe5lBKFLRDgVbddLCz7e8BW1REvO1AhT0rlEGcG70bxPPZ6kboaTO23zRkPU Ohu0VdS55xbVzv8pjibizpDYuw8ULOfD9Ez5e1+Lftg0tMl8P1eAeyA8R4VcnsAFLxAm knndzSFEtvjSmVE33YeQ77HYcJ1CZh/L0c6nwYAXYUBtzeXz9HlN70tP6890ocRF1N88 uusClUdBzhUSNMP1CRq1iFxS9H1DwOg+dpYOy6IS6UZcCkRg0/cOqnmpmTEXf63w6kzG iceQ== 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=JM8HKL3778vUQkbWShLT/DOaNAA+BeiO2MSEIcKh1es=; b=SbmDYbIxxskHjvAAowjlt465U5Ul86w7H6VfZjnpLm+NA5otbWeJzXC657UyChR92H O+tUhseIxMcMBKqYMkmIxbO/olGbmp6lOCuo2E8/dhy3RT7Eaw9Rr6WQhgYvtVqlurDg OfmPLFaFTZnWTaRBpWc7D/d6WNQvH7nQOhXMZuLcPmZ4iycxtNQnrc1Rw8r72El784gu 0ySPoxqoqQ0JvOLQwTMzlbEXnI30+cyZkZkhUfPY5+u3ddeDuVnnvkRHC99K+BOPg+NY aP9ux5AXBT2I1gwXs0Qk1s7PpkULWkw4kiy5W1yI3thxZcB+T+FYi7Re7Sd+rn5lkPzR /Png== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@metaspace-dk.20210112.gappssmtp.com header.s=20210112 header.b=lrsXoKtw; 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 sg6-20020a170907a40600b00944a52dd3b9si12231789ejc.452.2023.03.30.06.41.31; Thu, 30 Mar 2023 06:41:57 -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=lrsXoKtw; 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 S231439AbjC3Nj6 (ORCPT + 99 others); Thu, 30 Mar 2023 09:39:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50712 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229694AbjC3Nj5 (ORCPT ); Thu, 30 Mar 2023 09:39:57 -0400 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E32B7FC for ; Thu, 30 Mar 2023 06:39:54 -0700 (PDT) Received: by mail-wm1-x334.google.com with SMTP id j18-20020a05600c1c1200b003ee5157346cso13465907wms.1 for ; Thu, 30 Mar 2023 06:39:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=metaspace-dk.20210112.gappssmtp.com; s=20210112; t=1680183593; 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=JM8HKL3778vUQkbWShLT/DOaNAA+BeiO2MSEIcKh1es=; b=lrsXoKtwc5CT99FxrJAn4K/hioQwQhZrvDjS0r++gn+JrXlTHEc965i/+u0ZLtsxos OFXwAnjnAStfkXedcwa3Mh/buf2gYTn5fRkG4rmF/sIfpVsC8/639Zw4/yDQxqZ+UrXg o89jqWzNZOKHwEljKC4ggty61cWJoXWsOKtOVCe0hEi3I4duZvJ8M6XRqfLGDXGzFqwO tJZKtBs636Jp0tPvvvDtYQeEbdPznRxEZ1ufYCwyVC2EYY16fzYZeClPc7LI3qMSZ1qx lhlbNkLCOJHp3pKiRieRfRiwEZzQnq9Q3++xmfBEE05wTsCKSICFosMtPQfhsuT68p7x /H1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680183593; 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=JM8HKL3778vUQkbWShLT/DOaNAA+BeiO2MSEIcKh1es=; b=ucMjiupRWt5RA3zSpdIpCTGZVdf5Yc4jFyQQgegMZEbJIW/FyU7muRZ6uOW+eWHkFC C9qnw8Wog0pjeDeHdGqcnLeBu2iJbF1AdKYouvL5eQ4G470QAclNzMIlDfH8WgAxieQj pwB2SlmmmiRDo8tJM+xIOyZw2cko0siWv1e37XsNAs1N4anLimqoBeN9aWbjcUhxdy2a vqzS8sPKFvUwJDGjJoNJMSSAz/pOstDqDo3Xqx+r+fEO7QULJeF4qrCJYAUhNOcCbwJt 3VaM3osq75vIaQvcxfADhCgntmuk3az3hsOxN7Q+JgoebfvJ4z2Y0LCdmg+4GhO7Lz9C T7NQ== X-Gm-Message-State: AO0yUKXLKxk/7E1+QOueyvr+ADu1EhVb6bpZkBEO8RZdWmDlo61FW1t1 jDAyr3GzNJ6HEGJvKxo6u5WlOA== X-Received: by 2002:a1c:7516:0:b0:3ef:3ce6:7c69 with SMTP id o22-20020a1c7516000000b003ef3ce67c69mr17784934wmc.8.1680183593259; Thu, 30 Mar 2023 06:39:53 -0700 (PDT) Received: from localhost ([194.62.217.4]) by smtp.gmail.com with ESMTPSA id 7-20020a05600c024700b003ef5db16176sm5877756wmj.32.2023.03.30.06.39.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Mar 2023 06:39:52 -0700 (PDT) References: <20230329223239.138757-1-y86-dev@protonmail.com> <20230329223239.138757-7-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 Subject: Re: [PATCH v3 06/13] rust: init/sync: add `InPlaceInit` trait to pin-initialize smart pointers Date: Thu, 30 Mar 2023 15:39:20 +0200 In-reply-to: <20230329223239.138757-7-y86-dev@protonmail.com> Message-ID: <87cz4q4atz.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 `InPlaceInit` trait that provides two functions, for initializing > using `PinInit` and `Init`. It is implemented by `Arc`, > `UniqueArc` and `Box`. > > Signed-off-by: Benno Lossin > --- Reviewed-by: Andreas Hindborg > rust/kernel/init.rs | 97 +++++++++++++++++++++++++++++++++++------ > rust/kernel/sync/arc.rs | 25 ++++++++++- > 2 files changed, 108 insertions(+), 14 deletions(-) > > diff --git a/rust/kernel/init.rs b/rust/kernel/init.rs > index 85e8d5f41b60..3d89c7e3bdb5 100644 > --- a/rust/kernel/init.rs > +++ b/rust/kernel/init.rs > @@ -114,10 +114,13 @@ > //! [`impl Init`]: Init > //! [`Opaque`]: kernel::types::Opaque > //! [`pin_data`]: ::macros::pin_data > -//! [`UniqueArc`]: kernel::sync::UniqueArc > > +use crate::{ > + error::{self, Error}, > + sync::UniqueArc, > +}; > use alloc::boxed::Box; > -use core::{cell::Cell, convert::Infallible, marker::PhantomData, mem::MaybeUninit, ptr}; > +use core::{cell::Cell, convert::Infallible, marker::PhantomData, mem::MaybeUninit, pin::Pin, ptr}; > > #[doc(hidden)] > pub mod __internal; > @@ -309,7 +312,6 @@ pub mod macros; > /// > /// [`try_pin_init!`]: kernel::try_pin_init > /// [`NonNull`]: core::ptr::NonNull > -/// [`Error`]: kernel::error::Error > // For a detailed example of how this macro works, see the module documentation of the hidden > // module `__internal` inside of `init/__internal.rs`. > #[macro_export] > @@ -366,8 +368,6 @@ macro_rules! pin_init { > /// } > /// } > /// ``` > -/// > -/// [`Error`]: kernel::error::Error > // For a detailed example of how this macro works, see the module documentation of the hidden > // module `__internal` inside of `init/__internal.rs`. > #[macro_export] > @@ -589,8 +589,6 @@ macro_rules! try_pin_init { > /// > /// This initializer is for initializing data in-place that might later be moved. If you want to > /// pin-initialize, use [`pin_init!`]. > -/// > -/// [`Error`]: kernel::error::Error > // For a detailed example of how this macro works, see the module documentation of the hidden > // module `__internal` inside of `init/__internal.rs`. > #[macro_export] > @@ -641,8 +639,6 @@ macro_rules! init { > /// } > /// } > /// ``` > -/// > -/// [`Error`]: kernel::error::Error > // For a detailed example of how this macro works, see the module documentation of the hidden > // module `__internal` inside of `init/__internal.rs`. > #[macro_export] > @@ -848,7 +844,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`]. > +/// be [`Box`], [`Arc`], [`UniqueArc`]. Use the [`InPlaceInit::pin_init`] function of a > +/// smart pointer like [`Arc`] on this. > /// > /// Also see the [module description](self). > /// > @@ -867,7 +864,6 @@ macro_rules! try_init { > /// > /// [`Arc`]: crate::sync::Arc > /// [`Arc::pin_init`]: crate::sync::Arc::pin_init > -/// [`UniqueArc`]: kernel::sync::UniqueArc > #[must_use = "An initializer must be used in order to create its value."] > pub unsafe trait PinInit: Sized { > /// Initializes `slot`. > @@ -884,7 +880,8 @@ 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`]. Because [`PinInit`] is a super trait, you 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. > /// > /// Also see the [module description](self). > @@ -909,7 +906,6 @@ pub unsafe trait PinInit: Sized { > /// move the pointee after initialization. > /// > /// [`Arc`]: crate::sync::Arc > -/// [`UniqueArc`]: kernel::sync::UniqueArc > #[must_use = "An initializer must be used in order to create its value."] > pub unsafe trait Init: PinInit { > /// Initializes `slot`. > @@ -1014,3 +1010,78 @@ unsafe impl Init for T { > Ok(()) > } > } > + > +/// Smart pointer that can initialize memory in-place. > +pub trait InPlaceInit: Sized { > + /// Use the given initializer to in-place initialize a `T`. > + /// > + /// If `T: !Unpin` it will not be able to move afterwards. > + fn pin_init(init: impl PinInit) -> error::Result> > + where > + Error: From; > + > + /// Use the given initializer to in-place initialize a `T`. > + fn init(init: impl Init) -> error::Result > + where > + Error: From; > +} > + > +impl InPlaceInit for Box { > + #[inline] > + fn pin_init(init: impl PinInit) -> error::Result> > + where > + Error: From, > + { > + let mut this = Box::try_new_uninit()?; > + let slot = this.as_mut_ptr(); > + // SAFETY: When init errors/panics, slot will get deallocated but not dropped, > + // slot is valid and will not be moved because of the `Pin::new_unchecked` > + unsafe { init.__pinned_init(slot)? }; > + // SAFETY: All fields have been initialized. > + Ok(unsafe { Pin::new_unchecked(this.assume_init()) }) > + } > + > + #[inline] > + fn init(init: impl Init) -> error::Result > + where > + Error: From, > + { > + let mut this = Box::try_new_uninit()?; > + let slot = this.as_mut_ptr(); > + // SAFETY: When init errors/panics, slot will get deallocated but not dropped, > + // slot is valid > + unsafe { init.__init(slot)? }; > + // SAFETY: All fields have been initialized. > + Ok(unsafe { this.assume_init() }) > + } > +} > + > +impl InPlaceInit for UniqueArc { > + #[inline] > + fn pin_init(init: impl PinInit) -> error::Result> > + where > + Error: From, > + { > + let mut this = UniqueArc::try_new_uninit()?; > + let slot = this.as_mut_ptr(); > + // SAFETY: When init errors/panics, slot will get deallocated but not dropped, > + // slot is valid and will not be moved because of the `Pin::new_unchecked`. > + unsafe { init.__pinned_init(slot)? }; > + // SAFETY: All fields have been initialized. > + Ok(unsafe { Pin::new_unchecked(this.assume_init()) }) > + } > + > + #[inline] > + fn init(init: impl Init) -> error::Result > + where > + Error: From, > + { > + let mut this = UniqueArc::try_new_uninit()?; > + let slot = this.as_mut_ptr(); > + // SAFETY: When init errors/panics, slot will get deallocated but not dropped, > + // slot is valid. > + unsafe { init.__init(slot)? }; > + // SAFETY: All fields have been initialized. > + Ok(unsafe { this.assume_init() }) > + } > +} > diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs > index 16ec174637b2..77a3833cc265 100644 > --- a/rust/kernel/sync/arc.rs > +++ b/rust/kernel/sync/arc.rs > @@ -17,7 +17,8 @@ > > use crate::{ > bindings, > - error::Result, > + error::{Error, Result}, > + init::{InPlaceInit, Init, PinInit}, > types::{ForeignOwnable, Opaque}, > }; > use alloc::boxed::Box; > @@ -163,6 +164,28 @@ impl Arc { > // `Arc` object. > Ok(unsafe { Self::from_inner(Box::leak(inner).into()) }) > } > + > + /// Use the given initializer to in-place initialize a `T`. > + /// > + /// If `T: !Unpin` it will not be able to move afterwards. > + #[inline] > + pub fn pin_init(init: impl PinInit) -> Result > + where > + Error: From, > + { > + UniqueArc::pin_init(init).map(|u| u.into()) > + } > + > + /// Use the given initializer to in-place initialize a `T`. > + /// > + /// This is equivalent to [`pin_init`], since an [`Arc`] is always pinned. > + #[inline] > + pub fn init(init: impl Init) -> Result > + where > + Error: From, > + { > + UniqueArc::init(init).map(|u| u.into()) > + } > } > > impl Arc {