Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp2055192pxp; Fri, 18 Mar 2022 02:01:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxl4N6SZCyfaazrcWGsHBCYyaxNIu5swNSSPW6kO45jqgTuQJa6Rtu+XgxXAETWj9N3SU6K X-Received: by 2002:a17:906:1615:b0:6bb:150f:adf8 with SMTP id m21-20020a170906161500b006bb150fadf8mr7924240ejd.272.1647594095886; Fri, 18 Mar 2022 02:01:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647594095; cv=none; d=google.com; s=arc-20160816; b=HHT61HSwGgbITtOzob8jWBMV4cO/E+t4b28e2jpDv0AEVN2RPumOXRTn4HD6vaqIA1 a4VLSKmOIAaax62YyfP5RHLP9S9rxl5M1H5tlXAEOeaCdzfxNFJtHqisg3frXsNUx2p1 bEfUxg2W947KpVinOP9VoazL2yGcT5/tbWQ6FiCiLNHyLp9PSCzc8bSF66JTjpUGnOnR ptVgtwcWUskETI+1c05SzcMMOMzzgoyvZxuT/MoVO2e4qr2P62sd7cbpbbXbNJsqqic2 KnL4haWAL+qB/0AW/kXAoy0LwQ6tknR/QH3iGVv2cgOqGW95uI354nhfBJn1/4ftnXge Fa+A== 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=Afb7XK8RJ7Ye2gs29dRISiXOELfG+qiKmUZO6aB8/lw=; b=pbsWH9K5ba9nDW4hNumOUTYZ3XsMF4p7OBwDdMJWNmEh6PQL0Mfzio8likFnXMNIS6 P8tdWDHdGca7Hf6u14UpM38gWxpfBgyagWtFrsDO46v7Uy6aWDOPH5iuCMCKQ2c2plBY YgQK1ANlpUm1LDa7LJiS+TvJmqwXlJEsVy1cazXGbfhqCHabeCFWyQHWLTHEoPgkEXiW aFl3+Zw0aR+UwChIsUHSAcpXP3dKDgWyNsR7H/345roGM3KazfWLCtwJDjTifr6SrI1G XULExPXR+m40PG17xflSUecJwOjYOHixCW+7A3ce1EoTPcjHPogGmdnfYmuq+3o/ozwQ DtCw== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s2-20020a50ab02000000b00418c2b5bf23si3144785edc.517.2022.03.18.02.01.11; Fri, 18 Mar 2022 02:01:35 -0700 (PDT) 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231193AbiCRAdk (ORCPT + 99 others); Thu, 17 Mar 2022 20:33:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40100 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229499AbiCRAdj (ORCPT ); Thu, 17 Mar 2022 20:33:39 -0400 Received: from gate.crashing.org (gate.crashing.org [63.228.1.57]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 7AB58292DA9; Thu, 17 Mar 2022 17:32:22 -0700 (PDT) Received: from gate.crashing.org (localhost.localdomain [127.0.0.1]) by gate.crashing.org (8.14.1/8.14.1) with ESMTP id 22I0Pv9f001636; Thu, 17 Mar 2022 19:25:57 -0500 Received: (from segher@localhost) by gate.crashing.org (8.14.1/8.14.1/Submit) id 22I0Ptsh001632; Thu, 17 Mar 2022 19:25:55 -0500 X-Authentication-Warning: gate.crashing.org: segher set sender to segher@kernel.crashing.org using -f Date: Thu, 17 Mar 2022 19:25:55 -0500 From: Segher Boessenkool To: Linus Torvalds Cc: Florian Weimer , Nick Desaulniers , "H. Peter Anvin" , Bill Wendling , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)" , Nathan Chancellor , Juergen Gross , Peter Zijlstra , Andy Lutomirski , llvm@lists.linux.dev, LKML , linux-toolchains Subject: Re: [PATCH v5] x86: use builtins to read eflags Message-ID: <20220318002555.GP614@gate.crashing.org> References: <20220210223134.233757-1-morbo@google.com> <20220301201903.4113977-1-morbo@google.com> <878rt8gwxa.fsf@oldenburg.str.redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.4.2.3i X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_PASS, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 Thu, Mar 17, 2022 at 01:36:19PM -0700, Linus Torvalds wrote: > asm volatile("# __raw_save_flags\n\t" > "pushf ; pop %0" > : "=rm" (flags) > : /* no input */ > : "memory"); > And is that "memory" clobber because it modifies the memory location > just below the current stack pointer? > > No, not really - outside the kernel that might be an issue, but we > already have to build the kernel with -mno-red-zone, so if the > compiler uses that memory location, that would be a *HUGE* compiler > bug already. There is the problem though that this might extend the stack, which isn't marked up anywhere, so the static checkers do not see the stack overflow, and it won't be noticed until runtime. Or do the checkers consider such cases? > So the "memory" clobber has absolutely nothing to do with the fact > that 'pushf' updates the stack pointer, writes to that location, and > the popf then undoes it. > > It's literally because we don't want the compiler to move non-spill > memory accesses around it (or other asm statements wiht memory > clobbers), regardless of the fact that the sequence doesn't really > read or write memory in any way that is relevant to the compiler. Well, that, or the write of the code didn't consider this, just went "writes memory, so we clobber". > > GCC doesn't have barriers in the built-ins (if we are talking about > > __builtin_ia32_readeflags_u64 and __builtin_ia32_writeeflags_u64). I > > expect they are actually pretty useless, and were merely added for > > completeness of the intrinsics headers. > > Yeah, without any kinds of ordering guarantees, I think those builtins > are basically only so in name. They might as well return a random > value - they're not *useful*, because they don't have any defined > behavior. No ordering wrt any other code, yes. Which is not anything you can solve in only the builtin -- you need to consider the other code that you order with as well, change that code as well. > I mean, we *could* certainly use "read eflags" in the kernel, and yes, > in theory it would be lovely if we didn't have to encode it as a > "pushf/pop" sequence, and the compiler tracked the stack pointer for > us, and perhaps combined it with other stack pointer changes to the > point where the "popf" would never happen, it would just undo the %rsp > change at function exit time. > > So yes, a builtin can improve code generation. Yes, and they are much easier to write, and can be written correctly by lookenspeepers who just *have* to twist every knob that they can -- more easily than inline asm anyway, which is unforgiving to the extreme. They are also much easier to read usually, make for cleaner code. They certainly have their place. But they should stay there, too :-) Segher