Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp123487pxf; Wed, 24 Mar 2021 00:27:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzHrkNfZkbNk2gfu5rh17UXGbFCyjgFj6IeNLgg1OUv81hiLV8vWzK1MXjjIgc1GBGI/3Yv X-Received: by 2002:a17:907:d1f:: with SMTP id gn31mr2168655ejc.536.1616570877238; Wed, 24 Mar 2021 00:27:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616570877; cv=none; d=google.com; s=arc-20160816; b=B+BIQ0wZY6QvuWeFBhc36auycpmq9vANQAQ+uQMzzfxrcCysIISdXZoQ5Fx29t7Gq8 pjc9xDv36zqhDcCbGx/Ute+sLedUABxImzC/8ZB11uxYLhPbOX425zyKLLIna0sxD7/9 Bh0K63yGyJY7dcXeBacR+DRCgYwZoYw50NnvDU2oyfVVAH1YzgVtJ8+JOwAtwu6fdVVK rfr/g6AKqAKQSRnTK6Jb8wL+gxO61f73f7+Pc6VKijC3cAGmcxU9lmqPegPhUikNxq9F DNdScoMHv96ocFZN51RV16JhmPkreYIhVH7TH+/TZEPfzMBzBh63jVNZJkjlv0TUJetI ZY7g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=sIlhwezzW3BH4ibh6p5EOcG1PRMUgx4NenwqaMmx55s=; b=RBlylE2mrviZnarpwF5fM90MBszQhi5r8CbCzTtihe/t1rwMBBmgvuuuRX47rmJeji Uc6kX4rPZAScPLbhi8JG7BhwpuwVCIgntxYdWYXYYIsBIO615+6ZHdb2Aro2vROdeIee jC0FY2ed8kKG1Sv86mBJ4ppT1rfg5ipvfRstZPuuy1KsEr8MMLV7LssOu4hZD6V4cGgK 1je9vR6cwI+Fne4dX5/TIPu7egu5dKtuaOX1nGfwv3FjCKFQ7fB1S2cMP0Z9rqiPhdjQ uQvD9rtS9EtoYbqbyHtWuD1amPITIce8v+tT17K49kqTninKMP7w/d0MDAKOCeeiYsSj tMdA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=NhEf2QJO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p15si1245046ejr.236.2021.03.24.00.27.34; Wed, 24 Mar 2021 00:27:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=NhEf2QJO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233354AbhCWUz3 (ORCPT + 99 others); Tue, 23 Mar 2021 16:55:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48242 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233424AbhCWUzB (ORCPT ); Tue, 23 Mar 2021 16:55:01 -0400 Received: from mail-ed1-x52a.google.com (mail-ed1-x52a.google.com [IPv6:2a00:1450:4864:20::52a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E537FC061574 for ; Tue, 23 Mar 2021 13:55:00 -0700 (PDT) Received: by mail-ed1-x52a.google.com with SMTP id h13so25088081eds.5 for ; Tue, 23 Mar 2021 13:55:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=sIlhwezzW3BH4ibh6p5EOcG1PRMUgx4NenwqaMmx55s=; b=NhEf2QJOcj3iOZD88Yu0ZLce0jFAF2oJ0a4zOpB89R+20Dnm1wJxmHvStKsHMcXIEU u669BILvepA3CMjYHpxucpAG7ZNB3shoKU8iAzkFtfrzwfxVQrb0NnRf13IytDNKcwR6 9/7irFGnst5FsQFkUYhi/hyZwUIkwPDGAQ5nw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=sIlhwezzW3BH4ibh6p5EOcG1PRMUgx4NenwqaMmx55s=; b=XVDQiCrXzoyRo442QpKXBgK+VE20Yce3oHUsqSySMGL7Yjobz3vbuoAs5C0wYdR8IR O4FwRUH8BcNh21TruGdZSY8Tm9BGLxk3e1nls70RmW0PAYLsMTRYBUQUqLv3vVwDYbt1 T6LZs2hzHzBrPO4QPymhnZojQK2wmEehmb6Lt6AZaW8/VEvKXWuKM0p1e2n1uRTDiqWd pNG/BI+vZxLV5xLTJGshZAMMqK4uirDvH71Z8LtkSBd/LxR+3nS4cPStbN/3JdN2rmHa xcPXOwktlpccG5qGqZfLqZ8k0iAxZhtMCsQ8yrBw9b618+6ptmVv5uQ4CfSk9L9aCmAs XWIw== X-Gm-Message-State: AOAM533lqEPEc0TNFZBmev8GFuwzGmmyXels8otmVCLlBWbs2+vpDjC9 72GHb9l76Artl9/Y3WO8KwPDGw== X-Received: by 2002:a05:6402:2695:: with SMTP id w21mr6430957edd.99.1616532899600; Tue, 23 Mar 2021 13:54:59 -0700 (PDT) Received: from [192.168.1.149] ([80.208.71.248]) by smtp.gmail.com with ESMTPSA id ym4sm3844663ejb.100.2021.03.23.13.54.58 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 23 Mar 2021 13:54:59 -0700 (PDT) Subject: Re: [kbuild-all] Re: include/linux/compiler_types.h:315:38: error: call to '__compiletime_assert_536' declared with attribute error: BUILD_BUG_ON failed: offsetof(struct can_frame, len) != offsetof(struct canfd_frame, len) || offsetof(struct can_frame, data) != offsetof(struc... To: Oliver Hartkopp , Marc Kleine-Budde , Rong Chen , Patrick Menschel Cc: kernel test robot , kbuild-all@lists.01.org, linux-kernel@vger.kernel.org, linux-can References: <202103210435.I0fiBGAC-lkp@intel.com> <7f4f7e1c-194b-a903-d474-e3b742556a55@intel.com> <20210323073437.yo63wreqnubbeqby@pengutronix.de> <7ff6bfd3-6b4b-045a-abb7-485927909587@hartkopp.net> <080d9e5c-fe1f-4a64-2938-8ca6d8a98d78@hartkopp.net> <0a8e8e95-c1a2-ede6-9f87-1ab7a0a155e3@rasmusvillemoes.dk> <212c8bc3-89f9-9c33-ed1b-b50ac04e7532@hartkopp.net> From: Rasmus Villemoes Message-ID: <1a6dd272-8bc2-57dc-5592-47a08493193a@rasmusvillemoes.dk> Date: Tue, 23 Mar 2021 21:54:58 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.1 MIME-Version: 1.0 In-Reply-To: <212c8bc3-89f9-9c33-ed1b-b50ac04e7532@hartkopp.net> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 23/03/2021 19.59, Oliver Hartkopp wrote: > > > On 23.03.21 15:00, Rasmus Villemoes wrote: >> Now what CONFIG_* knobs are responsible for putting -mabi=apcs-gnu in >> CFLAGS is left as an exercise for the reader. Regardless, it is not a >> bug in the compiler. The error is the assumption that this language >> >> "Aggregates and Unions >> >> Structures and unions assume the alignment of their most strictly >> aligned component. > > (parse error in sentence) It was a direct quote, but I can try to paraphrase with an example. If you have a struct foo { T1 m1; T2 m2; T3 m3; }, then alignof(struct foo) = max(alignof(T1), alignof(T2), alignof(T3)). Same for a "union foo". But this is specifically for x86-64; for (some flavors of) ARM, other rules apply - namely, alignof(T) is 4 unless T is char or short (or (un)signed variants), ignoring bitfields which have their own rules. Note that while union u {char a; char b;} has alignment 4 on ARM and 1 on x86-64, other types are less strictly aligned on ARM; e.g. s64 aka long long is 8-byte aligned on x86-64 but (still) just 4-byte aligned on ARM. And again, this is just for specific -mabi= options. >> Each member is assigned to the lowest available offset with the >> appropriate >> alignment. The size of any object is always a multiple of the object‘s >> alignment." >> >> from the x86-64 ABI applies on all other architectures/ABIs. >> >>> I'm not a compiler expert but this does not seem to be consistent. >>> >>> Especially as we only have byte sizes (inside and outside of the union) >>> and "A field with a char type is aligned to the next available byte." >> >> Yes, and that's exactly what you got before the anon union was >> introduced. > > Before(!) the union there is nothing to pad. Just to be clear, my "before" was in the temporal sense, i.e. "prior to commit ea7800565a128", all the u8s in struct can_frame were placed one after the other. But after that commit, struct can_frame has a new member replacing can_dlc which happens to occupy 4 bytes (for some ABIs), pushing the subsequent members __pad, __res0 and len8_dlc (formerly known as __res1) ahead. >>> The union is indeed aligned to the word boundary - but the following >>> byte is not aligned to the next available byte. >> >> Yes it is, because the union occupies 4 bytes. The first byte is shared >> by the two char members, the remaining three bytes are padding. > > But why is the union 4 bytes long here and adds a padding of three bytes > at the end? Essentially, because arrays. It's true for _any_ type T that sizeof(T) must be a multiple of alignof(T). Take an array "T x[9]". If x[0] is 4-byte aligned, then in order for x[1] to be 4-byte aligned as well, x[0] must occupy a multiple of 4 bytes. It doesn't matter at all that this happens to be an anonymous union. Layout-wise, you could as well have a definition union uuu { __u8 len; __u8 can_dlc; } and made struct can_frame struct can_frame { canid_t can_id; union uuu u; __u8 __pad; ... }; (you lose the anonymity trick so you'd have to do frame->u.can_dlc instead of just frame->can_dlc). You have a member with alignof()==4 and sizeof()==4; that sizeof() cannot magically become 1 just because that particular instance of the type is not part of an array. Imagine what would happen if the compiler pulled subsequent char members into trailing padding of a previous compound member. E.g. consider struct a { int x; char y; } // alignof==4, sizeof==8, offsetof(y)==4 struct b { struct a a; char z; } If I have a "struct b *b", I'm allowed to do "&b->a" and get a "pointer to struct a". Then I can do memset(&b->a, 0, sizeof(struct a)). Clearly, z must not have been placed inside the trailing padding of struct a. Rasmus