From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f50.google.com (mail-wm1-f50.google.com [209.85.128.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 58F372F3E for ; Thu, 30 Mar 2023 13:39:55 +0000 (UTC) Received: by mail-wm1-f50.google.com with SMTP id o24-20020a05600c511800b003ef59905f26so11764972wms.2 for ; Thu, 30 Mar 2023 06:39:55 -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=rwRrYxFsNktb4LvaRk3hpoL9Nq4THAJqlffkaglY3M924YK9W8ziyMHmO3P1c8KMeH Sfamxkqm3aP1rOsEmZ+JbVKS8LILffqH4Ic8BBflsZxW2Yk5sKomMN2aJ/rZbswp8hPU JrmVBIQmFVEqPY6gZV2gzcBOKAF2NigpbzQZwzbUMF7xN0aOo/5fPTjeYiSYHQu0/j/f uolOUVwVghq0kTFLUkoBj72mmCuw8NSXe/8CfkP7LfrqjrJAxmrR8Ek3MU4JdxjvpJNX OAsVmDAzaeaksfev9rWy4khPO7kpoG3rdY3eVL+u4luBNCc5FiZGwENtddaSZpS2Uvt5 n+HA== X-Gm-Message-State: AO0yUKVJwRl+piEfv9GYkqCISCBp4VQNZ9dCqb7Rj4vnltS5rpTyV9Xo PCMeRWE3NsTqOVM0Omh0zxSzrA== X-Google-Smtp-Source: AK7set+k2PrWvan4zfb9OG6ybBURZnKDsRVl/hovKJ4Xp/T6XF5Jk/uqVgLrxo6+urwaOhXX/cPV7Q== 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> Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain 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 {