Received: by 2002:a05:6a10:a852:0:0:0:0 with SMTP id d18csp3951546pxy; Tue, 4 May 2021 13:54:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwhhXx1HGBbBtmt/puZjTkA2nkInROtW/OHsly+Zl0MwQv9xRcvZwIr6R4dZxoaJl339/IT X-Received: by 2002:a17:907:7216:: with SMTP id dr22mr24254324ejc.185.1620161694583; Tue, 04 May 2021 13:54:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620161694; cv=none; d=google.com; s=arc-20160816; b=QyQyqVYRVGHEtHu7Wl/0EZvsvapMDsK/0Boz+vPh10Yo/N9d5edOmQYAVfqji4gpi+ SzlqqcHu0rX6AFn96TTex8AgBWsgNMRcUVkTtYO+UHYcctQEKISXJYuChqdDwdgEZRBZ M2gDOiC01C2MC1cNQkQlHv/z3T3ghBqe7FPPVK2bfuzh1Cn9XTFuIlhp0oE/mhrOWrjP kFEVpsrhkSlqiQVVAsO/JTatfzGydHEIwerrfZSj8i8c7gDv8Ui38Kya4UlReShmO/c7 6zmxVlGYjuiVIkhuuuAN6HsvJUtxlS/VQDf0frHDjOPxIib1mDMqMClX3WSmkDxTXxH4 zAJA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=aEYlrGT5yKNhRydE3xwgtuGyulB/FRhvfO707ZajoC4=; b=I3UMZosshYUpGlUUrRTwNfrGcwVW6vhZ0cX1wNgWQwlI8XoN9V+sKqhYdWOphlmfL6 KPvJvU8DX1RtHoPix5jeBdMlvRjm+3frq6+WpIkLQPoUA4qA0m67ksiih4rsxET/zRik oAIbG0ej+Bmxw2uOFy/0OzF0IP+lv97S+LqbtyZOwhHuOH/MyajaD9YK0JvyBFZ2GLXF nB0Os9X0GZvqmhwv++qvVUTxD68AkMT7irip6luOO06auZXRMHQWu8Evn03xpx/hCrA1 5gpi43wUKgyocikDgtiYxebuRF3M5IpJIgOp092xo8fOMqzfMaFmvXzz6p0GP9EnkS4N z1Hg== ARC-Authentication-Results: i=1; mx.google.com; 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 a15si14482337edr.346.2021.05.04.13.54.25; Tue, 04 May 2021 13:54:54 -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; 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 S232455AbhEDUwK (ORCPT + 99 others); Tue, 4 May 2021 16:52:10 -0400 Received: from wtarreau.pck.nerim.net ([62.212.114.60]:52895 "EHLO 1wt.eu" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230150AbhEDUwJ (ORCPT ); Tue, 4 May 2021 16:52:09 -0400 Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 144Kp9pY019879; Tue, 4 May 2021 22:51:09 +0200 Date: Tue, 4 May 2021 22:51:09 +0200 From: Willy Tarreau To: Florian Weimer Cc: "Vladislav K. Valtchev" , linux-kernel@vger.kernel.org, linux-gcc@vger.kernel.org, linux-toolchains@vger.kernel.org Subject: Re: GCC, unaligned access and UB in the Linux kernel Message-ID: <20210504205109.GA19716@1wt.eu> References: <877dkekzj8.fsf@mid.deneb.enyo.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <877dkekzj8.fsf@mid.deneb.enyo.de> User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Florian, On Tue, May 04, 2021 at 10:35:39PM +0200, Florian Weimer wrote: > > [1] If aliasing is involved, even with -fno-strict-aliasing, unaligned access > > WILL break some code, today. Check the following example: > > > > int h(int *p, int *q){ > > *p = 1; > > *q = 1; > > return *p; > > } > > > > typedef __attribute__((__may_alias__)) int I; > > > > I k(I *p, I *q){ > > *p = 1; > > *q = 1; > > return *p; > > } > > > > Starting from GCC 8.1, both h() and k() will always return 1, when compiled with > > -O2, even with -fno-strict-aliasing. > > > > [2] Some SIMD instructions have alignment requirements that recent compilers > > might just start to assume to be true, in my current understanding. In general, > > SIMD instructions can be emitted automatically by the compiler because of auto- > > vectorization. But, fortunately, that *cannot* happen in the kernel because we > > build with -fno-mmx, -fno-sse, -fno-avx etc. > > Cc:ing linux-toolchains. > > __attribute__ ((aligned (1))) can be used to reduce alignment, similar > to attribute packed on structs. If that doesn't work for partially > overlapping accesses, that's probably a compiler bug. Indeed, for me it fixes the example above with gcc-8.4: Before: 0000000000000020 : 20: c7 07 01 00 00 00 movl $0x1,(%rdi) 26: b8 01 00 00 00 mov $0x1,%eax 2b: c7 06 01 00 00 00 movl $0x1,(%rsi) 31: c3 retq After: 0000000000000020 : 20: c7 07 01 00 00 00 movl $0x1,(%rdi) 26: c7 06 01 00 00 00 movl $0x1,(%rsi) 2c: 8b 07 mov (%rdi),%eax 2e: c3 retq That's good to know :-) Willy