Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2832610rwb; Thu, 17 Nov 2022 17:14:05 -0800 (PST) X-Google-Smtp-Source: AA0mqf7ag+83+NTMS9Oxo/FpnYSoNibnbC0+kW/x6NGPvmqAJ3jFFqtHnLHN9bSxKfSxGOO9XHRv X-Received: by 2002:a63:c63:0:b0:476:c4e1:958b with SMTP id 35-20020a630c63000000b00476c4e1958bmr4553680pgm.256.1668734045486; Thu, 17 Nov 2022 17:14:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668734045; cv=none; d=google.com; s=arc-20160816; b=ZzBhdf9BWTeeZOCck7jULclgDJq01+FxN2Oey814GIKxDT1Bbh43jFB8wT9dZFt6NM Vmx8Emd3Qp4xFGMIX+8EmhtoN/pyE7SuoKrm0DgoxK0on6k3ius59L4QXPDaheiK88g8 manu6XaCgLvQkypXYb3SWrQcKdYm6vG7H10m9/h3d5LPUA+Xi1hiuQ6I7lW24viwT7dJ OPQlz/MxRJINpqt+5WcUp+lrMtSF7OSiKSQDKuYqvmnACHOYeLfXiwgF8ujFmo8PLSD7 5UH7NfqnGhcwLCcqevkKnMIyi1RbLIQLnkMKik3T4wKsXauR6VVPOonHJj1NryuFgG9E EnGw== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d18-20020a056a0024d200b0056782f3bdf1si2703646pfv.19.2022.11.17.17.13.53; Thu, 17 Nov 2022 17:14:05 -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 + 91 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 Content-Transfer-Encoding: 8bit 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 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(-) 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: -- 2.37.4