Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp5076139iob; Mon, 9 May 2022 08:09:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJydvZIp0kQnRXjUWjDKiFQpp2TySepoDePYC5Ar2GrTwef8IZ2H8LqxAs2xX9gS7bv7KxwN X-Received: by 2002:a17:902:ecc4:b0:15e:a670:6056 with SMTP id a4-20020a170902ecc400b0015ea6706056mr16363821plh.108.1652108958425; Mon, 09 May 2022 08:09:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652108958; cv=none; d=google.com; s=arc-20160816; b=NruDWjnMGsVUHLjYoobUy7j9NnfWK8UnDB/DvHEXzbsDyi2ZtcdBeMCPk0BjSbFWdM WC4woL3eIrln2fXHuYgzuLTppE13LV74/n5XWKpUIrxoE+v6DwdTy/DAZ5g6sxt2TLFl 8nYAg7ELP9rLL0bCgDdjgo8IrU0Fl8hB2gltdKCQWGmtOOgx5MhOY7lUREM6L7m2XPtQ L33SP+42Wrh7E6MWzQlp/zvtvGIOtYq+K1SPP6NsfIzsl3+K7pGWU1Hmw2mEWs1/cxI2 DYu+ayeKrrL8uVjfzEc7WaZ5YctIgLVer6DN+xvGbR3ps2Ps4NkcSMAetgVvqk3MJy2C y8XQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version; bh=DNn5pADeSTEeIqgaMpExgJfErrD9ld+adA+VZngH5Qk=; b=WcqoE+nYDnItRlzXKGQW3r4yq5luBqZdSNL2LWqLpiL8xagG55cA2jQ5vkjAlznNL0 Mm7ZGCj/4RL5rPHuX15UYhasefvcX+UAR/YfZCxjhxqZtKH+y/ChRL0SezndTcRYONsR 7mxB3FJnSSTE2mo97+KUvjzkSCsiLP/OvTK/2yeM6S0kxq7vtWdAhdq1L/a0EVituS5f y4yUowvP8jLXzpnnbUQpLz7+bM62j9Wyc2EHETS6CTnnZl1F4Fz80CJX64uyoHnE0DnD jatDordE3vmsTsHdIQLNSAQHrgUivXM8+0zO/w0MxZYXei86ebWpO7fQxAoNHqTimgEv huEA== ARC-Authentication-Results: i=1; mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id c7-20020a170902d48700b0015eb15b9b6bsi12744873plg.560.2022.05.09.08.09.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 May 2022 08:09:18 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 6647C24598; Mon, 9 May 2022 08:01:16 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237867AbiEIPE7 (ORCPT + 99 others); Mon, 9 May 2022 11:04:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59444 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237849AbiEIPE4 (ORCPT ); Mon, 9 May 2022 11:04:56 -0400 Received: from mail-yw1-f170.google.com (mail-yw1-f170.google.com [209.85.128.170]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0D32E15710 for ; Mon, 9 May 2022 08:00:59 -0700 (PDT) Received: by mail-yw1-f170.google.com with SMTP id 00721157ae682-2f7b815ac06so147740837b3.3 for ; Mon, 09 May 2022 08:00:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=DNn5pADeSTEeIqgaMpExgJfErrD9ld+adA+VZngH5Qk=; b=EYKTrdCktjaN+OPNJLxCktWADLAsYB1EjQKQFnHfYnjc0/zEx5rcuj0MAnilmGRoKd hOfc5w2J9oLJqoO3uUfaiWP83mVXmI4oGrx0DZ6QPYEjjfgD9wHAtiiw/W4G4d3qywZP Dd9v3SPIOET7TXgnBjRrn85Iq71Op8Dxcsf4fI4sGnasJkpYDGfKlQ34VN3xwqRapyne H1coiRRiHPhR5ubAUyW7UJcs/Dbs+o+h2LdiFDlqRWPN8zW2k0PUi3Agh4I66JtYZa3M dgirn6p8mSYKB+hMu5Q6wYgO5v7hBOxL6zk0X6ymMdNYQl+sTppcJIWY66r0X+cdxBt3 fwcg== X-Gm-Message-State: AOAM530f/uHb3oa4YjqGpDWCKQEOu7Zi83gkJiR7NPBIAMqDB01kV5I2 XtbZX6FDhjav11zgvhd0K+BFcy/tMSyv7NKLiQQ= X-Received: by 2002:a81:34f:0:b0:2f7:bbb1:1576 with SMTP id 76-20020a81034f000000b002f7bbb11576mr14564264ywd.45.1652108459058; Mon, 09 May 2022 08:00:59 -0700 (PDT) MIME-Version: 1.0 References: <20220306171009.1973074-1-mailhol.vincent@wanadoo.fr> <20220508100907.61231-1-mailhol.vincent@wanadoo.fr> In-Reply-To: From: Vincent MAILHOL Date: Tue, 10 May 2022 00:00:48 +0900 Message-ID: Subject: Re: [RESEND PATCH v1] x86/build: add -fno-builtin flag to prevent shadowing To: Nathan Chancellor Cc: Arnd Bergmann , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "the arch/x86 maintainers" , "H . Peter Anvin" , Linux Kernel Mailing List , clang-built-linux , Nick Desaulniers , Tom Rix , Kees Cook Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE 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 On Mon. 9 May 2022 at 08:51, Nathan Chancellor wrote: > On Sun, May 08, 2022 at 09:37:14PM +0900, Vincent MAILHOL wrote: > > Hi Arnd, > > > > +CC: Kees Cook > > > > On Sun. 8 May 2022 at 19:27, Arnd Bergmann wrote: > > > On Sun, May 8, 2022 at 12:09 PM Vincent Mailhol > > > wrote: > > > > > > > > Aside of the __builtin_foo() ones, x86 does not directly rely on any > > > > builtin functions. > > > > > > > > However, such builtin functions are not explicitly deactivated, > > > > creating some collisions, concrete example being ffs() from bitops.h, > > > > c.f.: > > > > > > > > | ./arch/x86/include/asm/bitops.h:283:28: warning: declaration of 'ffs' shadows a built-in function [-Wshadow] > > > > | 283 | static __always_inline int ffs(int x) > > > > > > > > This patch adds -fno-builtin to KBUILD_CFLAGS for the x86 > > > > architectures in order to prevent shadowing of builtin functions. > > > > > > > > Signed-off-by: Vincent Mailhol > > > > --- > > > > FYI, I tested this patch on a "make allyesconfig" for both x86_32 and > > > > x86_64. > > > > > > > > This is a resend. Only difference is that I dropped the RFC flag and > > > > added Arnd in CC because he did a similar patch to fix ffs shadow > > > > warnings in the past: > > > > > > > > https://lore.kernel.org/all/20201026160006.3704027-1-arnd@kernel.org/ > > > > > > I think this is a correct change, but unfortunately it exposes a clang bug > > > with -mregparm=3. Nick should be able to provide more details, I think > > > he has a plan. > > > > Interesting. I admittedly did not do extensive tests on clang > > but I would have expected the Linux kernel bot to have warned me > > on my previous patch. > > > > I did research on mregparm and clang. I found this thread: > > https://lore.kernel.org/r/20220208225350.1331628-9-keescook@chromium.org > > > > and the associated LLVM issue: > > https://github.com/llvm/llvm-project/issues/53645 > > > > Those threads mention that some clang builtins become unusable > > when combining -mregparm=3 and -m32. But I could not find a > > bug reference about -mregparm=3 and -fno-builtin combination. > > > > Could you just double confirm that you indeed saw the issue with > > -fno-builtin? If that the case, I am really curious to get the > > details :) > > -ffreestanding implies -fno-builtin; removing -ffreestanding from > arch/x86/Makefile for 32-bit x86 caused the problem so I don't think > that your patch would cause any issue but I could be missing something. > > However, doesn't -fno-builtin remove the ability for GCC and clang to > perform certain libcall optimizations? I seem to recall this coming up > in previous threads but I am having a hard time finding the exact > language that I was looking for. I was not aware. I did the test with a dummy memset implementation: | void foo(char *s, unsigned int count) | { | while (count--) | *s++ = 0; | } Before this patch (i.e. with builtins), GCC does this: | foo: | testl %esi, %esi # count | je .L7 #, | pushq %rbp # | movl %esi, %edx # count, count | xorl %esi, %esi # | movq %rsp, %rbp #, | call memset # | popq %rbp # | ret | .L7: | ret Here, we can clearly see that the function is optimized to a call to memset. After this patch (i.e. without builtins), the call disappears: | foo: | testl %esi, %esi # count | je .L1 #, | movl %esi, %esi # count, count | leaq (%rdi,%rsi), %rax #, _12 | .L3: | addq $1, %rdi #, s | movb $0, -1(%rdi) #, MEM[(char *)s_8 + -1B] | cmpq %rax, %rdi # _12, s | jne .L3 #, | .L1: | ret So yes, the -fno-builtin will remove the optimizations at least for GCC (not tested for clang). > This thread seems to be the most recent > one that I can remember: > > https://lore.kernel.org/CAHk-=whn91ar+EbcBXQb9UXad00Q5WjU-TCB6UBzVba682a4ew@mail.gmail.com/ What is funny is that the thread you are pointing at mostly complains about the compiler doing optimization in the user's back. There will be some cases in which the compiler will find valid optimizations and some cases it will do some silly one (e.g. transform a very small loop to a function call). The problem is to know whether the clever optimizations outweigh the silly ones or not. I wonder if any benchmark exists on that. If compiler optimizations are indeed worth it, we should then have a look at the other architecture which uses the -fno-builtin flag (or the -ffreestanding). Regarding this patch, I do not think it should be applied anymore unless proven that "optimizations" are detrimental and thus unwanted. Instead, I am thinking of just using -fno-builtin-ffs to remove the annoying -Wshadow warning. Would that make more sense? Thank you. Yours sincerely, Vincent Mailhol