Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp2679097pxa; Mon, 17 Aug 2020 16:24:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyxBNq1CTsh+eZW2HgTcwqZKcrmpOAgAUHPyNsyqpgOiBnzAB04/lE9aPaRi3ZVZafcCBPp X-Received: by 2002:a17:906:d7b1:: with SMTP id pk17mr17151494ejb.554.1597706675731; Mon, 17 Aug 2020 16:24:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597706675; cv=none; d=google.com; s=arc-20160816; b=HByCOu2mPO37Ry6AmUN6BtMwo6dtzDEd4Y89pJlFQw06Lol9Fgkk7GMT8WYtZnLgrR H/hRyvXjFo9SUtkcIgqzk2fiOeJQ2xLStNYaVPnZ2hKIwbXFRY8h9Rq8Yevl1VMzvPSD /85GF52DUQefpPFQxcE9m59R0SQZu8rgDrEqMq7RhQUq9j8jrNWGCSkrHcugfjWkx7rN WGRt9wDuj0oFVVQnOKKICBq7/J2pQ0dHzuErE16h2zxHS0e/r3vS06ClR0Onoco6L9cC wwydaq+RCmBcp0ak98GAiWGyA/X57bqycZgV2SmZNzo9N0RGeyPWMvTahfz+k+qrZZMF L1Pg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature:dkim-filter; bh=UcsjupZ0JA2RpPNCd/O00y25ozkP4PEzUv+SeMit95Y=; b=rE0CPgl362dg/6h4kObugKmJk3obbZtpm0riK0HH1tppBgbBsWP2wF99Ck445atx85 0Mq+sdhXFEzRIH3BHgn1FDFPCiaCDAuyZfRJ5vUcTQ3wgHCMJ7mM56PWseCKQnBTW60E roMjSK0UWjXVBKscqHVh2VkF3oNcB0fYrW54LdeJ/soMOIloMQOircJrjc7zMUe/P1cA N4f+aoUc7Hn1A4DvvkuMMqVt9ukaRFm+1Lhyufy82ohV6gaTzQSzcXZiP8v0LYR1FRZE bv1aHlzkod7SeaegqYN0Q57EQ8wHGMCOcST08COq9HtSA7MKIOOk+xh8qjLfnz0nlHP8 Fo9g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@zytor.com header.s=2020072401 header.b=fFw7iaFI; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zytor.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id mm19si13016868ejb.501.2020.08.17.16.24.12; Mon, 17 Aug 2020 16:24:35 -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=@zytor.com header.s=2020072401 header.b=fFw7iaFI; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zytor.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730825AbgHQWpO (ORCPT + 99 others); Mon, 17 Aug 2020 18:45:14 -0400 Received: from terminus.zytor.com ([198.137.202.136]:48771 "EHLO mail.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730732AbgHQWpK (ORCPT ); Mon, 17 Aug 2020 18:45:10 -0400 Received: from hanvin-mobl2.amr.corp.intel.com (jfdmzpr03-ext.jf.intel.com [134.134.139.72]) (authenticated bits=0) by mail.zytor.com (8.15.2/8.15.2) with ESMTPSA id 07HMiW422418579 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NO); Mon, 17 Aug 2020 15:44:32 -0700 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.zytor.com 07HMiW422418579 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zytor.com; s=2020072401; t=1597704275; bh=UcsjupZ0JA2RpPNCd/O00y25ozkP4PEzUv+SeMit95Y=; h=Subject:To:Cc:References:From:Date:In-Reply-To:From; b=fFw7iaFIjOTNkyvMiTi6MGZ1QKRqloYm27Hjeqslbjuje3JHcveUFWxZ13s0xYxGL Pq1ExKfPYkv2sexFEJB1hIocLvmRNjVJtwloT35gujDTd+OXSRHE1cnLSD9IYBA9Vu fzJXyAfOqRLkIZ/xT9bMfg9qbVD/DSfoEC4Z7LDmT07itHKLK5rdn0/7/vG+vwrjtz DFO/Zq97GNALaq2XrP26/hUvUSLd9XpcnOAClKJ13Cd15ifmv/qAT1F7rtvPbSx53E zHhqVOWOP6IWH5Hvg+pP/m4sJM3HmN83qsYrwv0pLaMkD+Ei3kbgruX0KTVvmZxPcG iyP2A+4BGi7gg== Subject: Re: [PATCH 0/4] -ffreestanding/-fno-builtin-* patches To: Nick Desaulniers , Masahiro Yamada , Andrew Morton , Thomas Gleixner , Ingo Molnar , Borislav Petkov Cc: Michal Marek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, Kees Cook , Tony Luck , Dmitry Vyukov , Michael Ellerman , Joe Perches , Joel Fernandes , Daniel Axtens , Arvind Sankar , Andy Shevchenko , Alexandru Ardelean , Yury Norov , x86@kernel.org, Ard Biesheuvel , "Paul E . McKenney" , Daniel Kiper , Bruce Ashfield , Marco Elver , Vamshi K Sthambamkadi , Andi Kleen , Linus Torvalds , =?UTF-8?B?RMOhdmlkIEJvbHZhbnNrw70=?= , Eli Friedman References: <20200817220212.338670-1-ndesaulniers@google.com> From: "H. Peter Anvin" Message-ID: Date: Mon, 17 Aug 2020 15:44:31 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.9.0 MIME-Version: 1.0 In-Reply-To: <20200817220212.338670-1-ndesaulniers@google.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2020-08-17 15:02, Nick Desaulniers wrote: > -ffreestanding typically inhibits "libcall optimizations" where calls to > certain library functions can be replaced by the compiler in certain > cases to calls to other library functions that may be more efficient. > This can be problematic for embedded targets that don't provide full > libc implementations. > > -ffreestanding inhibits all such optimizations, which is the safe > choice, but generally we want the optimizations that are performed. The > Linux kernel does implement a fair amount of libc routines. Instead of > -ffreestanding (which makes more sense in smaller images like kexec's > purgatory image), prefer -fno-builtin-* flags to disable the compiler > from emitting calls to functions which may not be defined. > > If you see a linkage failure due to a missing symbol that's typically > defined in a libc, and not explicitly called from the source code, then > the compiler may have done such a transform. You can either implement > such a function (ie. in lib/string.c) or disable the transform outright > via -fno-builtin-* flag (where * is the name of the library routine, ie. > -fno-builtin-bcmp). > This is arguably exactly the wrong way around. The way this *should* be done is by opt-in, not opt-out, which by almost definition ends up being a game of whack-a-mole, like in this case stpcpy(). Furthermore, it is unlikely that people will remember what options to flip when and if stpcpy() happens to be implemented in the kernel. The problem here is twofold: 1. The user would be expected to know what kind of the optimizations the compiler can do on what function, which is private knowledge to the compiler. 2. The only way to override -fno-builtin is by a header file with macros overriding the function names with __builtin, but that doesn't tell the compiler proper anything about the execution environment. So the Right Thing is for the compiler authors to change the way -ffreestanding works. -ffreestanding means, by definition, that there are no library calls (other than libgcc or whatever else is supplied with the compiler) that the compiler can call. That is currently an all-or-nothing choice, or at least one choice per C standard implemented. Instead, a compile job with -ffreestanding should be able to provide a list of standard C functions that the compiler may call, and thus the compiler actually can do the right thing depending on which exact functions it would consider calling. This list is probably most easily supplied in the form of a header file with #pragma directives. -hpa