From patchwork Sat Jul 29 09:09:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benno Lossin X-Patchwork-Id: 128088 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:918b:0:b0:3e4:2afc:c1 with SMTP id s11csp944300vqg; Sat, 29 Jul 2023 03:01:38 -0700 (PDT) X-Google-Smtp-Source: APBJJlG3HJkGzgyBW9cxYva5i9JlOrbokeJrDw86/ZziN2Tj5wjpMa5CbVPqwHxmCoUJHNy+1+Nq X-Received: by 2002:a05:6a21:3b45:b0:11d:4c79:90ee with SMTP id zy5-20020a056a213b4500b0011d4c7990eemr4370714pzb.25.1690624897725; Sat, 29 Jul 2023 03:01:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690624897; cv=none; d=google.com; s=arc-20160816; b=NVJbxLPi6HYWwVbuaO1pSq76PhtxeGNmwYOa8/F61W8YxG5U8fgXGpkmCpHZVtoU8f 7g3wcYkC2IctPvm3k/fDqncXAuoYSB1g/QIrEPmmZMUWweR2e5idC5VNHlHA48/n73Ev KtR9IMjpl91FImtHPGeywmQCrmQGjU/JKqa+KvZzs9yGHQjDiCSkpn4GSKr3aeVABl3l D/rI0mgn8hOkAmafDJ7+DxksoGWKH43TBwXwkoHzOB7yyxcSPYSwcOKLpr95fmmgVEXj toL7R0sRz3J9r5VZAPSRRQBPZBGp3W9z+xU+nSXjnFok6tsuo/1GQ+v7gwmbb8WkURiN VQaA== 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=pQx6ihcSvIvQbgV/BbP1F61LnlGypfrLDVQD2l3NnSY=; fh=Kxkq5MAR/ee06c4zNa7/ow9QmWBIyeCoT7WYYnSSoIA=; b=0flyQUiNRgmVoYLi6HAxe2gIB856u06gofLda4EYWF6qAGasIA/SXm7FuKrcgqLd7c BLDFDzF21X9u2OkwHFLaaW+cwTSk/CrBxqckuoh1cHWY5rTjIGwW+e1vKCHBCCfd66WS WbdxOjjoUTuZFujFGCSwXHajZgbjcDsO/ybysJZ1iqfdWi3fr3hMm2HU6ER1HVar/Let x4VIynn1lmDjEbW5GjcM1XQ/1VT14W3CfTOYfjqV7XtY6bCNxjTLtF2rnycK17DpTEhN Mm6S0C7tbc7hQGNqMBGfIsJmF5NU6tV9siruWKztSjsQ81HjQcvpjqWLTfm4TkSUcrIq K15A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@proton.me header.s=oeiw4vx7yna6jha6bjbqylksbe.protonmail header.b=DQjd3O6k; 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 cd5-20020a056a00420500b006870b0a14cfsi2763255pfb.134.2023.07.29.03.01.18; Sat, 29 Jul 2023 03:01:37 -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=oeiw4vx7yna6jha6bjbqylksbe.protonmail header.b=DQjd3O6k; 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 S231699AbjG2JKp (ORCPT + 99 others); Sat, 29 Jul 2023 05:10:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41312 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231682AbjG2JKb (ORCPT ); Sat, 29 Jul 2023 05:10:31 -0400 Received: from mail-40134.protonmail.ch (mail-40134.protonmail.ch [185.70.40.134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EC29D49DB for ; Sat, 29 Jul 2023 02:10:03 -0700 (PDT) Date: Sat, 29 Jul 2023 09:09:46 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=oeiw4vx7yna6jha6bjbqylksbe.protonmail; t=1690621802; x=1690881002; bh=pQx6ihcSvIvQbgV/BbP1F61LnlGypfrLDVQD2l3NnSY=; 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=DQjd3O6krWMb6w6+BBC7iqizJ08O0WXNNmNV7ZfY08l4HV8f5wScaDrARRhWDaXWm h/QFvAkYLcmRrc+FCNtwdwvxtg+uzZiFcXPxlktpHnK6eTiVpiGQbAvZyBRUy17lW0 ankNpi1N/xj4O/sdOeQHt3qQxHtWqKHaBCH6ayYy142AeHbnU5VqdO4fNjWfmoog4p KMFFwz9n0D8IhmPN5grUjre/ZkAS8dpzcfcErS0M2leEm2T2bGuXnk5NnsZLdyZysC exFdp21cSBNvFqq4JKGaapbuGq8OzcnZxgZW4d3EHFU8wq1hnCjw7vuOIAnTLxqNJ3 jHK6JTuG3Ehdg== 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, Martin Rodriguez Reboredo Subject: [PATCH v3 05/13] rust: init: wrap type checking struct initializers in a closure Message-ID: <20230729090838.225225-6-benno.lossin@proton.me> In-Reply-To: <20230729090838.225225-1-benno.lossin@proton.me> References: <20230729090838.225225-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_MSPIKE_H5, RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE, URIBL_BLOCKED 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772748692739263381 X-GMAIL-MSGID: 1772748692739263381 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`. Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Alice Ryhl Signed-off-by: Benno Lossin Reviewed-by: Gary Guo --- v2 -> v3: - added Reviewed-by's from Martin and Alice. 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 454f31b8c614..2bad086cda0a 100644 --- a/rust/kernel/init/macros.rs +++ b/rust/kernel/init/macros.rs @@ -1040,14 +1040,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) } @@ -1168,8 +1168,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)*