From patchwork Sat Oct 29 12:25:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yingchi Long X-Patchwork-Id: 12698 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1330394wru; Sat, 29 Oct 2022 05:45:39 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5YGW4rk7fc5K0Oe+L8UR3WHsKgf7NMLHZCgAXVhoNeJsR+FRR9kdfcDXN8KY5TWit18wtX X-Received: by 2002:a05:6402:4301:b0:461:96bb:b238 with SMTP id m1-20020a056402430100b0046196bbb238mr4133156edc.328.1667047539516; Sat, 29 Oct 2022 05:45:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667047539; cv=none; d=google.com; s=arc-20160816; b=FvgC0IH70DXfmppHx/3ePb/v2eyzCOB0JagYFAP8bsEDX+LMhTo9dY4pIQ8HwSFW9t ZLhlou/iSplN09uW5xYIxxF/EV1FJktxedPbFOZdbab90KM3Wngn/XZayBM63fpPSq54 bEPfD3W2dWuvHURd/SlHG38nKm1A2zvZvj8lv4TYSV7VmhmPnyPpVYuPB95eMaNXp4cy nV2j1fhlxwH7VGXFVLR2FU+JbZLNegatlxE48LbOMhsj5Q2SBlpKPY72MzcJk5sjx6VR f+HevWfwjUnEBrx2JYN0/fQN7duu1P869uGzUybBost0DBFFU1Vy/bpSicbKWPg1wWCT SQxg== 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=3/oFhysJZr1k7COMZAgBa0WiHXdhhOME8z3lm49b8io=; b=D77VqX0RGVtra3prNf65quIBfw4NMwDPVjA/lYKaFY3cw5Xk3xKhgdoj5HoSEYadXO IAGDAyeRg5WD5LILDTh+Lv9edAC3yyqdVLPUqoo0aDW9t8ynh0/FFojJCnGCDu85Hbo5 H4ULyW7RqOapEzf6xGZsBqVbzcNOfxon5UDFpFuqa8NI+NkR954c7hKIELzSwic8Rge4 SfrQlbGF/pHrJpQ7pOmZC4/KFL0zMMo3lzbNUPc1clDidrTlo/2J07MZGo3jdhtweitn x7GEQwujYTZnSwrJy5m+etVe6BcAB0Boy52+BJ968c93djc8Bcd6RkBdqcz8PgCs3uAa csiA== 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 nb17-20020a1709071c9100b0078def5c29e3si1846681ejc.596.2022.10.29.05.45.15; Sat, 29 Oct 2022 05:45:39 -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; 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 S229587AbiJ2M0f (ORCPT + 99 others); Sat, 29 Oct 2022 08:26:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33628 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229473AbiJ2M0e (ORCPT ); Sat, 29 Oct 2022 08:26:34 -0400 Received: from mail-m118205.qiye.163.com (mail-m118205.qiye.163.com [115.236.118.205]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E9629645D9 for ; Sat, 29 Oct 2022 05:26:30 -0700 (PDT) Received: from localhost.localdomain (unknown [221.212.176.48]) by mail-m118205.qiye.163.com (HMail) with ESMTPA id 081482C15D6; Sat, 29 Oct 2022 20:26:28 +0800 (CST) From: YingChi Long To: me@inclyc.cn Cc: bp@alien8.de, chang.seok.bae@intel.com, dave.hansen@linux.intel.com, david.laight@aculab.com, hpa@zytor.com, linux-kernel@vger.kernel.org, mingo@redhat.com, ndesaulniers@google.com, pbonzini@redhat.com, tglx@linutronix.de, x86@kernel.org Subject: [PATCH RESEND v3] x86/fpu: use _Alignof to avoid UB in TYPE_ALIGN Date: Sat, 29 Oct 2022 20:25:52 +0800 Message-Id: <20221029122552.2855941-1-me@inclyc.cn> X-Mailer: git-send-email 2.37.4 In-Reply-To: <20221006141442.2475978-1-me@inclyc.cn> References: <20221006141442.2475978-1-me@inclyc.cn> MIME-Version: 1.0 X-HM-Spam-Status: e1kfGhgUHx5ZQUpXWQgPGg8OCBgUHx5ZQUlOS1dZFg8aDwILHllBWSg2Ly tZV1koWUFPN1dZLVlBSVdZDwkaFQgSH1lBWUIdQh1WQx9OGEkYQx9DTUtDVQIWExYaEhckFA4PWV dZGBILWUFZSUlKVUlKSVVKTE1VT0NZV1kWGg8SFR0UWUFZT0tIVUpJS0NOTVVKS0tVS1kG X-HM-Sender-Digest: e1kMHhlZQR0aFwgeV1kSHx4VD1lBWUc6Kyo6Hyo*LDlOKzQRMxIYHxQz KVYwCxRVSlVKTU1MS09NSEJLS0pJVTMWGhIXVRYeOxIVGBcCGFUYFUVZV1kSC1lBWUlJSlVJSklV SkxNVU9DWVdZCAFZQUhOQ083Bg++ X-HM-Tid: 0a8423b5374e2d27kusn081482c15d6 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,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: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1746985074092761822?= X-GMAIL-MSGID: =?utf-8?q?1748026040598872823?= WG14 N2350 made very clear that it is an UB having type definitions with in "offsetof". This patch change the implementation of 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. I've made a clang patch that rejects any definitions within __builtin_offsetof (usually #defined with "offsetof"), and tested compiling with this patch, there are no error if this patch applied. 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 we're replacing has behavior that matches _Alignof rather than __alignof__. Signed-off-by: YingChi Long Link: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2350.htm Link: https://godbolt.org/z/sPs1GEhbT Link: https://gcc.gnu.org/onlinedocs/gcc/Alignment.html Link: https://reviews.llvm.org/D133574 Reviewed-by: Nick Desaulniers --- v3: - commit message changes suggested by Nick and David 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: