Received: by 2002:a89:48b:0:b0:1f5:f2ab:c469 with SMTP id a11csp1399045lqd; Thu, 25 Apr 2024 14:35:29 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUCE+wauOkPJKf2/bw+usuKZ33DmkrFtifkuMhE/l+sK5W8ZQW7ZUdsjvgIGow3xJIss2ijQVdrWOZlUzxW/Pbz28V4uyBNViL1sIRQ2g== X-Google-Smtp-Source: AGHT+IFmWf1jDmoQwpnvoSp5TdUMwUp1U53lXZYD4bBnvgXBFQSB+hmJC6ec/sUQX4MyBKMgunB2 X-Received: by 2002:a17:90a:bd0c:b0:2ac:7a71:9a26 with SMTP id y12-20020a17090abd0c00b002ac7a719a26mr806998pjr.47.1714080929057; Thu, 25 Apr 2024 14:35:29 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714080929; cv=pass; d=google.com; s=arc-20160816; b=wn3PnZKiyDjQokCjKA4GG0XDSXdnJ4WzqrcKkiojxkZ6CRbAHWMIoloe7VbZHYMYAQ bK8GKtw8W0V6zWE7LsBF/xEmWODEm+XFqSSr1Fq+MW1ftllNUxLJTbhpq2mPEyXUpdmh cWjSZ6YNBXrPyWz2DmvHXIiCwyoDUe/9GvVhGCkdbfCGWLtybuSEJvwrCVUt43rXPqfc AfnXSLw87LGo6N/9IRRhTUgYbahi0n/Uob09275ITFB5qezpFq0MZS7y4qDAYBE0UNWF uOljMI3tgqsyGQl8UIrzgLAzfNK1oat7qcpSpW5BsMsjfzOaGV5XgQv+VulJYVi2QHAG rxDQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:feedback-id:references :in-reply-to:message-id:subject:cc:from:to:date:dkim-signature; bh=UJd86kWm35t/O3VTlkvSGLGEC+zSkf49elPIO6KdbFY=; fh=nEaETDv4M2nasq5JfLeaiASHNvZWcEwQ2im83YteH9I=; b=AnxYn02Euj0aaI0n7IsPeV0CexAM9rqMcGWTE/Xf0WzSnR+xKgtyy7HTPkM1jvLjBw JlyVSFXAzmYKfhckGw/R4+0+uABdtGa8RRmVecfEkF2fh5cMGJ9s4WIDDMwbCHlidXe+ iXP1qpQGZPZcuEL31++Q9lhRPtdS+CDptAAKGgw+ZI9e9tMsg8TQA5WpTFJH03+hR8fj KgnqE/1MHdheJMrY9QT2uTeJRN0PKzTTvaPqasHebVvuhHHSrcDFL4qIcWJEYIAuvcfc n29xoVDM0bb2IfLHHdAXag/UogV558YqXltnPCY0g0BPjyhPBSOiz63Mj1XRnhRU7cn8 SHJw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=gmH+hgnV; arc=pass (i=1 spf=pass spfdomain=proton.me dkim=pass dkdomain=proton.me dmarc=pass fromdomain=proton.me); spf=pass (google.com: domain of linux-kernel+bounces-159212-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-159212-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id cq17-20020a17090af99100b002ad87fce3a9si25789pjb.0.2024.04.25.14.35.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Apr 2024 14:35:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-159212-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=gmH+hgnV; arc=pass (i=1 spf=pass spfdomain=proton.me dkim=pass dkdomain=proton.me dmarc=pass fromdomain=proton.me); spf=pass (google.com: domain of linux-kernel+bounces-159212-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-159212-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id A8391B20FF4 for ; Thu, 25 Apr 2024 21:35:13 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1523F155751; Thu, 25 Apr 2024 21:34:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b="gmH+hgnV" Received: from mail-40134.protonmail.ch (mail-40134.protonmail.ch [185.70.40.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5E5FA153812; Thu, 25 Apr 2024 21:34:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.40.134 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714080898; cv=none; b=TwjicXxRGIHGalQm6zYL81FOKC4BQL298m/HJkxbKg+ixPGQbJenH4nvQ9a7gOMDXLT5pj7CFNG3YfcGCbljfjIKh2qqp6WGWQHYMP7hjmwP/KA8sL30c4iY9Gz4hk03lat/tQPiXPWO3JOliFdGV7WQdxBX53mORnrV7FQEduI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714080898; c=relaxed/simple; bh=YtT+9N11G/oagDxFwRs8nvK/R4iQrquRafZwkitYN7k=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=DJS3WdUpT8vq4u4vIbCa9ZtELWnFW9HggqSnKxrEPt2+zcOIiWYYiVuxfezKb4WTwE4xGMWcvTh/CIgpfdO/LQ+DE8LykRNtciNOpPOkTKM8Idoo8zOmetN/6UP2tMXol0/xTfcN8e2siuatnu8wkD2p7V2Ovw7+T6uZqY3+ofc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me; spf=pass smtp.mailfrom=proton.me; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b=gmH+hgnV; arc=none smtp.client-ip=185.70.40.134 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=proton.me DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1714080887; x=1714340087; bh=UJd86kWm35t/O3VTlkvSGLGEC+zSkf49elPIO6KdbFY=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=gmH+hgnVw8qfTX37fl+0eCO6I/Bt3A4JMWO6ab7NggAjrSkyEDrWXQJrsFMbWnVm0 YG5IznlBMmt2imUFw033mRKJY9Pm1VMmNgEUwfNmvOlHdYkle0+bfY5p8fYiwg+gsO nM/kUgoTuhQvi6s6aXDbs8WOZse0NU2XlEPjeZ0G7E5O35UIPXHJVBc98hhICRnqUG uovJko74t5sHFjX/Rcor62H4rwyYkfR9ZD7GhWZxoC0iikOIWkqKn8/GzBKvZgUlNb 3gbBp/TI10NbdfMWpX4yttZ+Au0FvKc++6kFvKvPokn23FTxYdTNhKjSMvy1WndmoY FuozYaKBrN8cw== Date: Thu, 25 Apr 2024 21:34:44 +0000 To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl From: Benno Lossin Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/2] rust: init: add re-initialization functions Message-ID: <20240425213419.3904105-2-benno.lossin@proton.me> In-Reply-To: <20240425213419.3904105-1-benno.lossin@proton.me> References: <20240425213419.3904105-1-benno.lossin@proton.me> Feedback-ID: 71780778:user:proton X-Pm-Message-ID: fd954416d514fda6cdb8e68aded583cacd674889 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Sometimes it is necessary to split allocation and initialization into two steps. One such situation is when reusing existing allocations obtained via `Box::drop_contents`. See [1] for an example. In order to support this use case add `re_[pin_]init` functions to the pin-init API. These functions operate on already allocated smart pointers that contain `MaybeUninit`. Signed-off-by: Benno Lossin Link: https://lore.kernel.org/rust-for-linux/f026532f-8594-4f18-9aa5-57ad3f= 5bc592@proton.me/ [1] --- rust/kernel/init.rs | 88 ++++++++++++++++++++++++++++++------------ rust/kernel/prelude.rs | 2 +- 2 files changed, 65 insertions(+), 25 deletions(-) diff --git a/rust/kernel/init.rs b/rust/kernel/init.rs index 9608f2bd2211..b37b23f07bf7 100644 --- a/rust/kernel/init.rs +++ b/rust/kernel/init.rs @@ -1159,13 +1159,8 @@ fn try_pin_init(init: impl PinInit, flags: = Flags) -> Result, where E: From, { - let mut this =3D as BoxExt<_>>::new_uninit(flags)?; - let slot =3D 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 we pin it later. - unsafe { init.__pinned_init(slot)? }; - // SAFETY: All fields have been initialized. - Ok(unsafe { this.assume_init() }.into()) + let this =3D as BoxExt<_>>::new_uninit(flags)?; + this.re_pin_init(init) } =20 #[inline] @@ -1173,13 +1168,8 @@ fn try_init(init: impl Init, flags: Flags) = -> Result where E: From, { - let mut this =3D as BoxExt<_>>::new_uninit(flags)?; - let slot =3D 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() }) + let this =3D as BoxExt<_>>::new_uninit(flags)?; + this.re_init(init) } } =20 @@ -1189,13 +1179,8 @@ fn try_pin_init(init: impl PinInit, flags: = Flags) -> Result, where E: From, { - let mut this =3D UniqueArc::new_uninit(flags)?; - let slot =3D 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 we pin it later. - unsafe { init.__pinned_init(slot)? }; - // SAFETY: All fields have been initialized. - Ok(unsafe { this.assume_init() }.into()) + let this =3D UniqueArc::new_uninit(flags)?; + this.re_pin_init(init) } =20 #[inline] @@ -1203,13 +1188,68 @@ fn try_init(init: impl Init, flags: Flags)= -> Result where E: From, { - let mut this =3D UniqueArc::new_uninit(flags)?; - let slot =3D this.as_mut_ptr(); + let this =3D UniqueArc::new_uninit(flags)?; + this.re_init(init) + } +} + +/// Smart pointer that can re-initialize its content. +pub trait InPlaceReInit { + /// The type `Self` turns into when re-initialized. + type Initialized; + + /// Re-initializes `self` with the given initializer. + /// + /// Does not drop the current value and considers it as uninitialized = memory. + fn re_init(self, init: impl Init) -> Result; + + /// Re-initializes `self` with the given initializer. + /// + /// Does not drop the current value and considers it as uninitialized = memory. + fn re_pin_init(self, init: impl PinInit) -> Result, E>; +} + +impl InPlaceReInit for Box> { + type Initialized =3D Box; + + fn re_init(mut self, init: impl Init) -> Result { + let slot =3D self.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() }) + Ok(unsafe { self.assume_init() }) + } + + fn re_pin_init(mut self, init: impl PinInit) -> Result, E> { + let slot =3D self.as_mut_ptr(); + // SAFETY: When init errors/panics, slot will get deallocated but = not dropped, + // slot is valid and will not be moved, because we pin it later. + unsafe { init.__pinned_init(slot)? }; + // SAFETY: All fields have been initialized. + Ok(unsafe { self.assume_init() }.into()) + } +} + +impl InPlaceReInit for UniqueArc> { + type Initialized =3D UniqueArc; + + fn re_init(mut self, init: impl Init) -> Result { + let slot =3D self.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 { self.assume_init() }) + } + + fn re_pin_init(mut self, init: impl PinInit) -> Result, E> { + let slot =3D self.as_mut_ptr(); + // SAFETY: When init errors/panics, slot will get deallocated but = not dropped, + // slot is valid and will not be moved, because we pin it later. + unsafe { init.__pinned_init(slot)? }; + // SAFETY: All fields have been initialized. + Ok(unsafe { self.assume_init() }.into()) } } =20 diff --git a/rust/kernel/prelude.rs b/rust/kernel/prelude.rs index b37a0b3180fb..078b2b1d84ae 100644 --- a/rust/kernel/prelude.rs +++ b/rust/kernel/prelude.rs @@ -37,6 +37,6 @@ =20 pub use super::{str::CStr, ThisModule}; =20 -pub use super::init::{InPlaceInit, Init, PinInit}; +pub use super::init::{InPlaceInit, InPlaceReInit, Init, PinInit}; =20 pub use super::current; --=20 2.44.0