Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp13574739rwd; Sat, 24 Jun 2023 02:49:49 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ45puKvmYHLMYjgI85N/gSKTXwHqmN2RYI9mbpA+a+ghQK5r0//vFXblF9X5H3x2n0cBm8O X-Received: by 2002:a05:6808:cd:b0:39e:cb29:572a with SMTP id t13-20020a05680800cd00b0039ecb29572amr18061297oic.17.1687600189667; Sat, 24 Jun 2023 02:49:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687600189; cv=none; d=google.com; s=arc-20160816; b=tqEkCDXt/ffiR0EzYJWAjnrUOlAiAr03GdPYEqpP3n64JEAg046NdR4pic4nzVq8MG /kY1/sH3ovmWYh8+xSM2AHI93R3vqFzTCBMuqfGGDRRO8coBnhmSOFKR5RIzTHbw4dFi T8Vu3ivVxvEi0DopQyioNKSSaQiPTp+vOBzsbgGypP5uvekj+KseokSDTKWH738W/wLe gEJe+6u0jyK/xYz2Jq4wTpiVsPPxdrUlDOuQ+tbDNz4bDis9hp5SdRa8ZA7Ev/N9cSDq LMH84pB2mUbYCR+dSKPt3Ph0uTgy1Whom/fYF4Vfp7LFXnnKnL3RXMFDK0DiXpd2YZxr ucdg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :feedback-id:references:in-reply-to:message-id:subject:cc:from:to :dkim-signature:date; bh=F442EJD8L22HB05UPRQHv+35aOiE4fN5MpCVafEtbng=; fh=Y3hGqDvLhFUfCxW08frpp1lth+cS6Xq25ZUxZll0Zv0=; b=eJj8M2CVBFot0mAHJ0GfUGIqUegs9k2ZVG3t0Eivxuso0VetaMpVjJjmF477uMrIRn MMgJfUKfhoIEzqHwIMoX2zCRk7trprITy2aC1x4V/bUIFvew1vjTm7nosnSDEn3yEUiw uVpWxa6DxsO6IiWDbDKBhJgOmhlUqQVnszlKhQIShCzvlMPUWWCqyd5lNNHv75X6vRKv U0j7PlDvyk8asiQF4jvi5BpHptcOB4ID9bN/PzqOOy+DRVTCnbE27uDIjfaxoek6CJcb X9qJS+j/bub2UHB8usEYieIwwEAhlGjo/YOu1kLpG0kdm+UxCnxalYCPSDLCYk2ZDP07 BWwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=RZe4r4hG; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id na11-20020a17090b4c0b00b00260d9e72ba1si4097669pjb.132.2023.06.24.02.49.36; Sat, 24 Jun 2023 02:49:49 -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=@proton.me header.s=protonmail header.b=RZe4r4hG; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232887AbjFXJZh (ORCPT + 99 others); Sat, 24 Jun 2023 05:25:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52502 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233008AbjFXJZa (ORCPT ); Sat, 24 Jun 2023 05:25:30 -0400 Received: from mail-4322.protonmail.ch (mail-4322.protonmail.ch [185.70.43.22]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E79312128 for ; Sat, 24 Jun 2023 02:25:23 -0700 (PDT) Date: Sat, 24 Jun 2023 09:25:10 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1687598721; x=1687857921; bh=F442EJD8L22HB05UPRQHv+35aOiE4fN5MpCVafEtbng=; 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=RZe4r4hGwC3X2Yf8meMtLPnpHuNbbt3hvvQm5OhFulqj51Wfeh9O2oJymyLFULk3U 0zzW9fXpxc5suiaGFJv2NhGQEqWE5FybAA5U47qT6r46PPw465AADoJE/fQitdHMzS T4XA7V/Va3kcasO3EnMB/N6iiC6llValzVIO+HPOrE4irfHExbaV5RE9JmJ8wi9r7x 9Zr2iQZ4Wns1P+JV44Pc3upL/Z7Z2TrFKiORmrB8sDfgLaixFynMw/c/fGpG3dzC+9 /pTPm10InbpJYHhyZ62yHXRYl92iHZgIl63yH+E2LwToIfkaWyTt95DsuSVMKOm1xs OxF863zALlKCg== To: Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor From: Benno Lossin Cc: Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Andreas Hindborg , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Asahi Lina Subject: [PATCH 3/7] rust: init: make guards in the init macros hygienic Message-ID: <20230624092330.157338-3-benno.lossin@proton.me> In-Reply-To: <20230624092330.157338-1-benno.lossin@proton.me> References: <20230624092330.157338-1-benno.lossin@proton.me> Feedback-ID: 71780778:user:proton MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=no 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 Use hygienic identifiers for the guards instead of the field names. This makes the init macros feel more like normal struct initializers, since assigning identifiers with the name of a field does not create conflicts. Also change the internals of the guards, no need to make the `forget` function `unsafe`, since users cannot access the guards anyways. Now the guards are carried directly on the stack and have no extra `Cell` field that marks if they have been forgotten or not, instead they are just forgotten via `mem::forget`. Suggested-by: Asahi Lina Signed-off-by: Benno Lossin --- rust/kernel/init.rs | 1 - rust/kernel/init/__internal.rs | 25 +++------------ rust/kernel/init/macros.rs | 56 ++++++++++++---------------------- 3 files changed, 23 insertions(+), 59 deletions(-) diff --git a/rust/kernel/init.rs b/rust/kernel/init.rs index d9a91950cba2..ecf6a4bd0ce4 100644 --- a/rust/kernel/init.rs +++ b/rust/kernel/init.rs @@ -206,7 +206,6 @@ use alloc::boxed::Box; use core::{ alloc::AllocError, - cell::Cell, convert::Infallible, marker::PhantomData, mem::MaybeUninit, diff --git a/rust/kernel/init/__internal.rs b/rust/kernel/init/__internal.r= s index 44751fb62b51..7abd1fb65e41 100644 --- a/rust/kernel/init/__internal.rs +++ b/rust/kernel/init/__internal.rs @@ -174,7 +174,6 @@ pub fn init(self: Pin<&mut Self>, init: impl PinInit= ) -> Result { ptr: *mut T, - do_drop: Cell, } impl DropGuard { @@ -190,32 +189,16 @@ impl DropGuard { /// - will not be dropped by any other means. #[inline] pub unsafe fn new(ptr: *mut T) -> Self { - Self { - ptr, - do_drop: Cell::new(true), - } - } - - /// Prevents this guard from dropping the supplied pointer. - /// - /// # Safety - /// - /// This function is unsafe in order to prevent safe code from forgett= ing this guard. It should - /// only be called by the macros in this module. - #[inline] - pub unsafe fn forget(&self) { - self.do_drop.set(false); + Self { ptr } } } impl Drop for DropGuard { #[inline] fn drop(&mut self) { - if self.do_drop.get() { - // SAFETY: A `DropGuard` can only be constructed using the uns= afe `new` function - // ensuring that this operation is safe. - unsafe { ptr::drop_in_place(self.ptr) } - } + // SAFETY: A `DropGuard` can only be constructed using the unsafe = `new` function + // ensuring that this operation is safe. + unsafe { ptr::drop_in_place(self.ptr) } } } diff --git a/rust/kernel/init/macros.rs b/rust/kernel/init/macros.rs index e8165ff53a94..df4281743175 100644 --- a/rust/kernel/init/macros.rs +++ b/rust/kernel/init/macros.rs @@ -992,7 +992,6 @@ impl<$($impl_generics)*> $pin_data<$($ty_generics)*> /// - `init_slot`: recursively creates the code that initializes all field= s in `slot`. /// - `make_initializer`: recursively create the struct initializer that g= uarantees that every /// field has been initialized exactly once. -/// - `forget_guards`: recursively forget the drop guards for every field. #[doc(hidden)] #[macro_export] macro_rules! __init_internal { @@ -1032,6 +1031,7 @@ macro_rules! __init_internal { $crate::__init_internal!(init_slot($($use_data)?): @data(data), @slot(slot), + @guards(), @munch_fields($($fields)*,), ); // We use unreachable code to ensure that all fields h= ave been mentioned exactly @@ -1046,10 +1046,6 @@ macro_rules! __init_internal { @acc(), ); } - // Forget all guards, since initialization was a succe= ss. - $crate::__init_internal!(forget_guards: - @munch_fields($($fields)*,), - ); } Ok(__InitOk) } @@ -1063,13 +1059,17 @@ macro_rules! __init_internal { (init_slot($($use_data:ident)?): @data($data:ident), @slot($slot:ident), + @guards($($guards:ident,)*), @munch_fields($(,)?), ) =3D> { - // Endpoint of munching, no fields are left. + // Endpoint of munching, no fields are left. If execution reaches = this point, all fields + // have been initialized. Therefore we can now dismiss the guards = by forgetting them. + $(::core::mem::forget($guards);)* }; (init_slot($use_data:ident): // use_data is present, so we use the `da= ta` to init fields. @data($data:ident), @slot($slot:ident), + @guards($($guards:ident,)*), // In-place initialization syntax. @munch_fields($field:ident <- $val:expr, $($rest:tt)*), ) =3D> { @@ -1080,24 +1080,26 @@ macro_rules! __init_internal { // return when an error/panic occurs. // We also use the `data` to require the correct trait (`Init` or = `PinInit`) for `$field`. unsafe { $data.$field(::core::ptr::addr_of_mut!((*$slot).$field), = $field)? }; - // Create the drop guard. + // Create the drop guard: // - // We only give access to `&DropGuard`, so it cannot be forgotten = via safe code. + // We rely on macro hygiene to make it impossible for users to acc= ess this local variable. // // SAFETY: We forget the guard later when initialization has succe= eded. - let $field =3D &unsafe { + let guard =3D unsafe { $crate::init::__internal::DropGuard::new(::core::ptr::addr_of_= mut!((*$slot).$field)) }; $crate::__init_internal!(init_slot($use_data): @data($data), @slot($slot), + @guards(guard, $($guards,)*), @munch_fields($($rest)*), ); }; (init_slot(): // no use_data, so we use `Init::__init` directly. @data($data:ident), @slot($slot:ident), + @guards($($guards:ident,)*), // In-place initialization syntax. @munch_fields($field:ident <- $val:expr, $($rest:tt)*), ) =3D> { @@ -1107,24 +1109,26 @@ macro_rules! __init_internal { // SAFETY: `slot` is valid, because we are inside of an initialize= r closure, we // return when an error/panic occurs. unsafe { $crate::init::Init::__init($field, ::core::ptr::addr_of_m= ut!((*$slot).$field))? }; - // Create the drop guard. + // Create the drop guard: // - // We only give access to `&DropGuard`, so it cannot be forgotten = via safe code. + // We rely on macro hygiene to make it impossible for users to acc= ess this local variable. // // SAFETY: We forget the guard later when initialization has succe= eded. - let $field =3D &unsafe { + let guard =3D unsafe { $crate::init::__internal::DropGuard::new(::core::ptr::addr_of_= mut!((*$slot).$field)) }; $crate::__init_internal!(init_slot(): @data($data), @slot($slot), + @guards(guard, $($guards,)*), @munch_fields($($rest)*), ); }; (init_slot($($use_data:ident)?): @data($data:ident), @slot($slot:ident), + @guards($($guards:ident,)*), // Init by-value. @munch_fields($field:ident $(: $val:expr)?, $($rest:tt)*), ) =3D> { @@ -1135,16 +1139,17 @@ macro_rules! __init_internal { unsafe { ::core::ptr::write(::core::ptr::addr_of_mut!((*$slot).$fi= eld), $field) }; // Create the drop guard: // - // We only give access to `&DropGuard`, so it cannot be accidental= ly forgotten. + // We rely on macro hygiene to make it impossible for users to acc= ess this local variable. // // SAFETY: We forget the guard later when initialization has succe= eded. - let $field =3D &unsafe { + let guard =3D unsafe { $crate::init::__internal::DropGuard::new(::core::ptr::addr_of_= mut!((*$slot).$field)) }; $crate::__init_internal!(init_slot($($use_data)?): @data($data), @slot($slot), + @guards(guard, $($guards,)*), @munch_fields($($rest)*), ); }; @@ -1189,29 +1194,6 @@ macro_rules! __init_internal { @acc($($acc)* $field: ::core::panic!(),), ); }; - (forget_guards: - @munch_fields($(,)?), - ) =3D> { - // Munching finished. - }; - (forget_guards: - @munch_fields($field:ident <- $val:expr, $($rest:tt)*), - ) =3D> { - unsafe { $crate::init::__internal::DropGuard::forget($field) }; - - $crate::__init_internal!(forget_guards: - @munch_fields($($rest)*), - ); - }; - (forget_guards: - @munch_fields($field:ident $(: $val:expr)?, $($rest:tt)*), - ) =3D> { - unsafe { $crate::init::__internal::DropGuard::forget($field) }; - - $crate::__init_internal!(forget_guards: - @munch_fields($($rest)*), - ); - }; } #[doc(hidden)] -- 2.41.0