From patchwork Fri Nov 18 00:55:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yingchi Long X-Patchwork-Id: 22000 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp709933wrr; Thu, 17 Nov 2022 17:00:44 -0800 (PST) X-Google-Smtp-Source: AA0mqf63T3yav6Bd3Xhf/nfwlA2H49Vwjc2DDDLC2Kqi0Qm4RttMDkOxjZ27kMFIODgzCJ4q26mU X-Received: by 2002:a05:6402:205c:b0:461:92bd:21cf with SMTP id bc28-20020a056402205c00b0046192bd21cfmr4115931edb.405.1668733244083; Thu, 17 Nov 2022 17:00:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668733244; cv=none; d=google.com; s=arc-20160816; b=nWcbJlMopDFbLGBbCWsZKUsWwbYnKK0JV1gg9Mk/2suVCkc5Q2X97m+FUMcblAfiQN qstIv3aPV/aglgO7eqOd3sw1kIiHfW5tcGJTHh4+yNoGydoIiHmlZFGK0gjTrjuN9QHR p1PP+7IkF5FH7sJqorZCfKK8UxZxWAGqYnjSlvIfqneWAVSUHjcZMfeQJgwvD2Zn5YQy bUdVHAwxYCMIB3PyPl7/kKyiKOyt5+g+IH2mr0y7SLjTb34FjrmYEpCMkfxXPOEmcMa6 MbzuHYD0KTrCtXgqFUR3feNfDf7cpTVGmYE5TtadXFrSEAhRz5CTh4a8163ZA1bcJ91z fEww== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=Bq+KaqLyxxaj2vVFMDXtbzHivvlwwUkWjG2pi4rDXhE=; b=s2a6SJZd/RdyUfnspR/1mf4MkgESYT6CJgqFo4qZAeZAv5k3GM8Vs16DT9iyrWmiSY 3V2exPCt8o0z3g7ZOIYV4NISO+5pzwp6L81Stlx9WOfTVjzZj3SY6O8R+19RP6TWMq/I fmeC9N2KBAnccyqr9Wxtxq+mh3XDmS/+dODV6yFaT4U0DPAD0NtlRg/K6r8ahnFRs7Da Be81DG+xWfn1okZQmgwhjvqS2H6WH+755weZ6mTKqNjtLgFcMsCsmbmV+QLR6r9wzSzO voAxsTd7AuEqHNR703+y2SNULIzkLwpHMGQDD8r0FgCb5Uf2BGIqQcdzkMoh3m63vKFG A6lg== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=inclyc.cn Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cw7-20020a170906478700b007806964b2fcsi1917471ejc.447.2022.11.17.17.00.19; Thu, 17 Nov 2022 17:00:44 -0800 (PST) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=inclyc.cn Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240770AbiKRA4M (ORCPT + 99 others); Thu, 17 Nov 2022 19:56:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54532 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233894AbiKRA4G (ORCPT ); Thu, 17 Nov 2022 19:56:06 -0500 Received: from mail-m118205.qiye.163.com (mail-m118205.qiye.163.com [115.236.118.205]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 48C4786A44 for ; Thu, 17 Nov 2022 16:56:02 -0800 (PST) Received: from lyc-workstation.. (unknown [221.212.176.31]) by mail-m118205.qiye.163.com (HMail) with ESMTPA id 28B8F2C32E0; Fri, 18 Nov 2022 08:55:59 +0800 (CST) From: Yingchi Long To: bp@alien8.de Cc: chang.seok.bae@intel.com, dave.hansen@linux.intel.com, david.laight@aculab.com, hpa@zytor.com, linux-kernel@vger.kernel.org, me@inclyc.cn, mingo@redhat.com, ndesaulniers@google.com, pbonzini@redhat.com, tglx@linutronix.de, x86@kernel.org Subject: [PATCH RESEND v4] x86/fpu: use _Alignof to avoid UB in TYPE_ALIGN Date: Fri, 18 Nov 2022 08:55:35 +0800 Message-Id: <20221118005535.1120026-1-me@inclyc.cn> X-Mailer: git-send-email 2.37.4 In-Reply-To: References: MIME-Version: 1.0 X-HM-Spam-Status: e1kfGhgUHx5ZQUpXWQgPGg8OCBgUHx5ZQUlOS1dZFg8aDwILHllBWSg2Ly tZV1koWUFPN1dZLVlBSVdZDwkaFQgSH1lBWRofGU9WTEsdSB1JGUsYGE8ZVQIWExYaEhckFA4PWV dZGBILWUFZSUlKVUlKSVVKTE1VSEpZV1kWGg8SFR0UWUFZT0tIVUpKS0hKQ1VKS0tVS1kG X-HM-Sender-Digest: e1kMHhlZQR0aFwgeV1kSHx4VD1lBWUc6OU06MRw6KTlIPyoRPw40NC9D KB1PCRlVSlVKTU1DTEhJQk1LSUhLVTMWGhIXVRYeOxIVGBcCGFUYFUVZV1kSC1lBWUlJSlVJSklV SkxNVUhKWVdZCAFZQUhMTUM3Bg++ X-HM-Tid: 0a84883c3c0a2d27kusn28b8f2c32e0 X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SORTED_RECIPS,SPF_HELO_NONE,SPF_PASS 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1746985074092761822?= X-GMAIL-MSGID: =?utf-8?q?1749793630225312711?= From: YingChi Long Link: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2350.htm WG14 N2350 made very clear that it is an undefined behavior having type definitions with in "offsetof". Replace the macro "TYPE_ALIGN" to builtin "_Alignof" to avoid undefined behavior. I've grepped all source files to find any type definitions within "offsetof". offsetof\(struct .*\{ .*, This implementation of macro "TYPE_ALIGN" seemes to be the only case of type definitions within offsetof in the kernel codebase. Link: https://reviews.llvm.org/D133574 A clang patch has been made that rejects any definitions within __builtin_offsetof (usually #defined with "offsetof"), and tested compiling the kernel using clang, there are no error if this patch applied. Link: https://gcc.gnu.org/onlinedocs/gcc/Alignment.html Link: https://godbolt.org/z/sPs1GEhbT ISO C11 _Alignof is subtly different from the GNU C extension __alignof__. __alignof__ is the preferred alignment and _Alignof the minimal alignment. For 'long long' on x86 these are 8 and 4 respectively. The macro TYPE_ALIGN being replacing has behavior that matches _Alignof rather than __alignof__. Signed-off-by: YingChi Long Reviewed-by: Nick Desaulniers --- v4: - commit message changes suggested by Borislav Petkov v3: - commit message changes suggested by Nick and David v3: https://lore.kernel.org/all/20221006141442.2475978-1-me@inclyc.cn/ v2: https://lore.kernel.org/all/20220927153338.4177854-1-me@inclyc.cn/ Signed-off-by: Yingchi Long --- arch/x86/kernel/fpu/init.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) -- 2.37.4 diff --git a/arch/x86/kernel/fpu/init.c b/arch/x86/kernel/fpu/init.c index 8946f89761cc..851eb13edc01 100644 --- a/arch/x86/kernel/fpu/init.c +++ b/arch/x86/kernel/fpu/init.c @@ -133,9 +133,6 @@ static void __init fpu__init_system_generic(void) fpu__init_system_mxcsr(); } -/* Get alignment of the TYPE. */ -#define TYPE_ALIGN(TYPE) offsetof(struct { char x; TYPE test; }, test) - /* * Enforce that 'MEMBER' is the last field of 'TYPE'. * @@ -143,8 +140,8 @@ static void __init fpu__init_system_generic(void) * because that's how C aligns structs. */ #define CHECK_MEMBER_AT_END_OF(TYPE, MEMBER) \ - BUILD_BUG_ON(sizeof(TYPE) != ALIGN(offsetofend(TYPE, MEMBER), \ - TYPE_ALIGN(TYPE))) + BUILD_BUG_ON(sizeof(TYPE) != \ + ALIGN(offsetofend(TYPE, MEMBER), _Alignof(TYPE))) /* * We append the 'struct fpu' to the task_struct: