From patchwork Wed Jul 19 14:20:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benno Lossin X-Patchwork-Id: 122647 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp2476396vqt; Wed, 19 Jul 2023 07:24:54 -0700 (PDT) X-Google-Smtp-Source: APBJJlHXwKC9mgfINfVqQURn19ZMiyt6R3+7GLyG+GEfOUe2p2KTrUw8P8mJBx4R1dZTKESTBHz/ X-Received: by 2002:a19:8c05:0:b0:4f9:6842:afc with SMTP id o5-20020a198c05000000b004f968420afcmr2568lfd.64.1689776694305; Wed, 19 Jul 2023 07:24:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689776694; cv=none; d=google.com; s=arc-20160816; b=zUuAT9aLGmeBgu+u0fBiqq5WjiQTx6BqbM+7duUiWh+CCcairDD77r3p0YYtGVyxeJ MlpSpOo59zxieGjVN2dpoZf8ewJ/gOmaIMdH9eGTP0vHwurAF9vSaW6BWjFwYUogolq6 AVtNJuIXOpJ3StZnNh5zro17XOVZ4jC+7s9h+l3IFX3z1k60yrv17+v6Ohi+jiDCmu5n Xh5aF3zSVpmbZ3IaXSPbYFHoota5bCflOwoZfpoR7IoANiSfClV++D1Pg7ybGlIwV+zk 8G6C4GuPwtAdCKwIAb9LeX8v2cNxZmlCcxWemFjxOLXiPgiNEy6hjDep7PwLUs7lCzbA 4MNw== 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=DsEfSlKtIhyTB7NkpS5sssxEHNqI+QqAvnsnKYAwLtk=; fh=qm1bOEYzVYZKL4fg86TgWb8D+QJA9mGpUhKFp2A6Xio=; b=SoQ9XxInOOjAcgZnQlnMyYoAxFVQ05lwgPrebFlKGMuLyZ976hH+53Lq87IsZ+30hV l6nZ675jSD53rdELuzJLr3xn5k3Ez30na1p6quYdVBXZKVONmC9u4oDwBab5CPXNBYCY NxkdTMQ4/KM/DXW2ELw1JwMpKUWiYhFQkHz5XH/OsCtpHhCe1eEIFsxkthJD7UcJ3qiF iPfJeeQps/PE15bYuNfPYriHO+XOJzQcK7ujP3RIPH6WvEW4MeA5KCLzzjIci6XvqbyU NMv2JNK5/EHVjJhPbhCSDgkC9vND0Hc9AmQaVPd/BChGjAkCqBV5Q0te7H0v8LUKEh9Y BRkQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=bC5l9XnA; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e25-20020a50fb99000000b0051bec40640bsi3035470edq.603.2023.07.19.07.24.30; Wed, 19 Jul 2023 07:24:54 -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=bC5l9XnA; 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 S231165AbjGSOU5 (ORCPT + 99 others); Wed, 19 Jul 2023 10:20:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50782 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231516AbjGSOUx (ORCPT ); Wed, 19 Jul 2023 10:20:53 -0400 Received: from mail-4322.protonmail.ch (mail-4322.protonmail.ch [185.70.43.22]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0E2651993; Wed, 19 Jul 2023 07:20:44 -0700 (PDT) Date: Wed, 19 Jul 2023 14:20:36 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1689776443; x=1690035643; bh=DsEfSlKtIhyTB7NkpS5sssxEHNqI+QqAvnsnKYAwLtk=; 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=bC5l9XnAlrfdaq2S5N8x5zKK+FHAetJ5Qoe3fWVLWY0MN12Z6bYFJOnh7qF1QhQ8A 7Y8++xHkylHtv06khzindeO9YELBrZiRYIJwXZ5JgQODdCPOSBuf0A5hYME84tJ/0X uWkzgAF9ipSb5S4EmVjrU/M4Di2j4hoWBmmgMubB1zQrWzquDMRDMhdfkimwq/4dTB paPXsBuQL1rrWJr3ePiYKVTpH3bOH6nptIry5TmBDyQu0iSjSl/sPfB4HYBK2Xiq8d 5yCe/yrjOAtNt21S3EGzfW99kSq+/Jp9s4AJb+XEbiVZyHVrburFMl321d96JmnDXB 4hzuS60K6qpeA== 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 Subject: [PATCH v2 04/12] rust: init: wrap type checking struct initializers in a closure Message-ID: <20230719141918.543938-5-benno.lossin@proton.me> In-Reply-To: <20230719141918.543938-1-benno.lossin@proton.me> References: <20230719141918.543938-1-benno.lossin@proton.me> Feedback-ID: 71780778:user:proton MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771859286731113927 X-GMAIL-MSGID: 1771859286731113927 In the implementation of the init macros there is a `if false` statement that type checks the initializer to ensure every field is initialized. Since the next patch has a stack variable to store the struct, the function might allocate too much memory on debug builds. Putting the struct into a closure that is never executed ensures that even in debug builds no stack overflow error is caused. In release builds this was not a problem since the code was optimized away due to the `if false`. Signed-off-by: Benno Lossin Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Alice Ryhl --- v1 -> v2: * do not call the created closure rust/kernel/init/macros.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rust/kernel/init/macros.rs b/rust/kernel/init/macros.rs index c5f977f52d0c..160b95fc03c9 100644 --- a/rust/kernel/init/macros.rs +++ b/rust/kernel/init/macros.rs @@ -1038,14 +1038,14 @@ macro_rules! __init_internal { // once, this struct initializer will still be type-checked and complain with a // very natural error message if a field is forgotten/mentioned more than once. #[allow(unreachable_code, clippy::diverging_sub_expression)] - if false { + let _ = || { $crate::__init_internal!(make_initializer: @slot(slot), @type_name($t), @munch_fields($($fields)*,), @acc(), ); - } + }; } Ok(__InitOk) } @@ -1166,8 +1166,8 @@ macro_rules! __init_internal { @acc($($acc:tt)*), ) => { // Endpoint, nothing more to munch, create the initializer. - // Since we are in the `if false` branch, this will never get executed. We abuse `slot` to - // get the correct type inference here: + // Since we are in the closure that is never called, this will never get executed. + // We abuse `slot` to get the correct type inference here: unsafe { ::core::ptr::write($slot, $t { $($acc)*