From patchwork Mon Aug 14 08:46:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benno Lossin X-Patchwork-Id: 135324 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp2651496vqi; Mon, 14 Aug 2023 03:37:56 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGVrJ9W2XCuhJsSFplHP5MExGZ6ypxzsLeYHTSo41JvPABy7tmE0KN7YEfB/CwmpoGcehvM X-Received: by 2002:aca:100b:0:b0:3a7:8e6:c946 with SMTP id 11-20020aca100b000000b003a708e6c946mr8825669oiq.41.1692009476469; Mon, 14 Aug 2023 03:37:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692009476; cv=none; d=google.com; s=arc-20160816; b=ByC8gEqJ1v7U8uTBb3RsaCp9SLMATZewuu3FuYhIu7xJLlNiVWHiuehaPWOfNJkd5c NCt9tiPzUINTqhFitgYmGCGNA8hGfgBB6/Y/TJWWKUPuxh8oMoYDVVdFCHDNcPz10ZNr 6RdbesfmMirjvTN5RnW1kle8AL37yPQPeJ3+yy1pLNVgpeIOpMgQ9lmhfBKlXDeTeEEN AJ5bSfM5YyqvwtHxd6LQAQYG+rpOwFGSib30GEHa0bXPqpcRaoXJGO4MQUva/Y32Qfe9 qnFnNVkeieQF10T+8mzQ3gpr8vOVQBRfFK0rPe49VCIPSrPQnUm+08Mto11/N9itb0DZ jeZQ== 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=+y/4hwjE9xTPBuUzHSo6g+e55bg8L+V4bqZtkGtraCM=; fh=TE4onW3Du2FQ1EsedDK8TzgRHOYndfJXYpOtlseWrM0=; b=Knbsy/rr7tHDprIWFv99mOmXyPFhMn6e33CnDFsdZ1lcDKNW3TwUHgtxZVOQ6j6vpA FRr/H0SNDOgq/lyo3MBTbmNg6/33dceD6SMv7buUp9znYtTZHgCSN6V5TB3837LIcp8w beLyQAl6Xq+Pwe2+7f8mPxwR5dhpFZ6por1L4eQ6JxG7lQJH0h3ilvjd5tfrkxYX4aAq j+ZtYRn0u96I1NPHI3CVLLzL3FTI86xHuJd8ap//YPkvv6pCtB1gkmC0isaX5brj5HjB NUuYCJEg7OPbCscRgfN4LUD45xEWWF3KF2BWYx+2HjEEzzEDD2LIhj/ICTeNipJCBlyu zhPg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@proton.me header.s=6tf7jz7u4rgs3bvq3n5hlsdnem.protonmail header.b=VBx8s9iE; 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 132-20020a63028a000000b0055798b90375si8208444pgc.709.2023.08.14.03.37.41; Mon, 14 Aug 2023 03:37:56 -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=6tf7jz7u4rgs3bvq3n5hlsdnem.protonmail header.b=VBx8s9iE; 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 S235049AbjHNIrZ (ORCPT + 99 others); Mon, 14 Aug 2023 04:47:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38722 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235064AbjHNIrG (ORCPT ); Mon, 14 Aug 2023 04:47:06 -0400 Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D702010C for ; Mon, 14 Aug 2023 01:47:05 -0700 (PDT) Date: Mon, 14 Aug 2023 08:46:55 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=6tf7jz7u4rgs3bvq3n5hlsdnem.protonmail; t=1692002824; x=1692262024; bh=+y/4hwjE9xTPBuUzHSo6g+e55bg8L+V4bqZtkGtraCM=; 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=VBx8s9iEtWxkKgrYspZcruM+PDspV9IubBxy3NR9+IXX26WLTxFQ2GyudmdxCRqP6 gQjLu1hc6mE244ph3QYqkgfBY+rC00A1VjSkfQ0gSOF0a1q5FtrRl1FRDsVYVb2QRI 79cryyyphfK4zDylndAV50fVdxY6XjACv9AU3B5puoVz7kMyZBITZOBIUuocYrR6px CLJ3JO6HMpcGxlmmV09Uo0+vB5uRk1AIqz1WTS/6eGPiKCwZEmq+hMBf24CKTznQ/I gpcTdxefkHMfli3MCLsN9b8QkFtF9Ng+B1WRAFnWD+SBQbsqxGqUMD3orQbHwAFSJq W60adFkHjvzow== 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, Martin Rodriguez Reboredo Subject: [PATCH v4 05/13] rust: init: wrap type checking struct initializers in a closure Message-ID: <20230814084602.25699-6-benno.lossin@proton.me> In-Reply-To: <20230814084602.25699-1-benno.lossin@proton.me> References: <20230814084602.25699-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 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: 1774200528570582712 X-GMAIL-MSGID: 1774200528570582712 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 Reviewed-by: Gary Guo Signed-off-by: Benno Lossin --- v3 -> v4: - added Reviewed-by from Gary. 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)*