Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp1671292pxp; Thu, 17 Mar 2022 14:07:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyZFeOJwRsi3tQzH/yAlkv+I1W746eka4n6ScY4/EueCz86tRmFWkT9zHTdKhmgJIeS5NMZ X-Received: by 2002:aa7:968b:0:b0:4f6:fa51:c239 with SMTP id f11-20020aa7968b000000b004f6fa51c239mr6367171pfk.48.1647551270200; Thu, 17 Mar 2022 14:07:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647551270; cv=none; d=google.com; s=arc-20160816; b=XoXzalI3xo6L/XzdQC1rAoFHBIlKJVc6inArjIDu+LToUuTqdPHWaqHZFYenEeMijk MkL70ujGX5WOxiVrsNkaQivJbc2ZcHjgdLDeLesPuajUdjugY/tCfZ1gPEhd0j4B2qZY I801h3aP5XppiRkskckSrFy3GO/igWsIa13jAR8JsJON42P9iWtf9vsv5K0PU968tuIK nTeErO3cGwpAasZaFdWau54WuOo3gFQAmyUKxe4RsGagSh0vWjgYT1uVpq9ph8jlujNA 28/MgKRY9A+E0olV0qUvFuWXRaGogmIOmaggHOyu9oFWAgQPHFEY6TD2m8jZIafK8l00 c+9A== 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:dkim-signature; bh=yvCBQhvuhOwtfCqiEm+hthJDaVDRziXPjtYnTX7Px84=; b=cHM+Hy8rpBmDYDlI1b/faIb7HjySdjx2m/vuTNTOiIYIEjOFepzFxAf1Qos7cYPXSg 7QjHqrCB4+v8afdZ5f/g031YwDG4ZoitxEBrJmVfSOufKuVth4La6QNn24WCTrtjsMox MoazBLZfI65ATTc5L6+b03yAg6Zs86T9tK0VH+72bU04lHdeHXbv0KZ6JBw7t4PBALqW 6e+8PmPcTQ6ql5mKDt9mLQWhsti1KI55+Dd3pFNRu7N96oqBG/TpQTeGVNcGNA6JGBLE sZj387ZPHAXvsrc1L7hp9mRciY+YfHj58aEDaoPa4OOQkHRkj+eHtzk7KSdKSPdTExTl FcuQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=Pb7vw1v8; 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 i125-20020a636d83000000b003816043ef0fsi3351336pgc.260.2022.03.17.14.07.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Mar 2022 14:07:50 -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; dkim=pass header.i=@linux-foundation.org header.s=google header.b=Pb7vw1v8; 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 6E6C919613E; Thu, 17 Mar 2022 13:36:47 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229795AbiCQUiA (ORCPT + 99 others); Thu, 17 Mar 2022 16:38:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45510 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229788AbiCQUh6 (ORCPT ); Thu, 17 Mar 2022 16:37:58 -0400 Received: from mail-lf1-x12a.google.com (mail-lf1-x12a.google.com [IPv6:2a00:1450:4864:20::12a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E054319611F for ; Thu, 17 Mar 2022 13:36:39 -0700 (PDT) Received: by mail-lf1-x12a.google.com with SMTP id bt26so10981021lfb.3 for ; Thu, 17 Mar 2022 13:36:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=yvCBQhvuhOwtfCqiEm+hthJDaVDRziXPjtYnTX7Px84=; b=Pb7vw1v8KqV7vMdCXksuJhYR/Z0TATOJk8MxAWV0eo+m1M/QQOaITAqAhXgsEaNHAn OVdl7RDl9aHsjXGHFD1lfodi4HhuuO7ryga88W9mXDwu5HKSYy+CK2HiGm/nVFQxpDaZ tIovYJ38do4gLiUn0qYtQGldgNe/vrnYRv+dc= 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=yvCBQhvuhOwtfCqiEm+hthJDaVDRziXPjtYnTX7Px84=; b=X+YaYK7Dn6rkVLsgR60XVPVxFAZN1qxK+u7nVdzZ+g8vpFA0cj5g47QBjKNeKVri6w JXONMjx0V6CNAJlOYLBQRmcFmG9+vEnwZ40jQ9R71m9pZpmkkMTftbTDHd2aD0pBWKrJ 78PqkeIDF0snW/x/5M49e8rmOV7sBjooUygWn9aZzdYG/oo8gQviq29T5sG6+aWXuC9W 6aOWn8ZsqHl56gOv7TrxW3FZibbQ+kzl2m9Mlav/AcVrFABbO2BPpIjONG9USuQyi//o ze+z06IhKl/TGOcj430kH1iZk9cKti50qkhjUo5kJ0YT9kCIT3HspKhnXq+T5oLfy5f6 hhwQ== X-Gm-Message-State: AOAM5302myxOMTV3/tvU+PQ5L7LQcfxivHNXlQ+l1NyBY3jy+nJn5w/L Ho/cYVE6QFpr8+/fEcXwHrByUV1pI8UuJFv/dKg= X-Received: by 2002:a05:6512:2256:b0:449:f79a:e762 with SMTP id i22-20020a056512225600b00449f79ae762mr2885016lfu.261.1647549397852; Thu, 17 Mar 2022 13:36:37 -0700 (PDT) Received: from mail-lj1-f169.google.com (mail-lj1-f169.google.com. [209.85.208.169]) by smtp.gmail.com with ESMTPSA id v2-20020ac258e2000000b004483faeb6f6sm525494lfo.280.2022.03.17.13.36.36 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 17 Mar 2022 13:36:36 -0700 (PDT) Received: by mail-lj1-f169.google.com with SMTP id r22so8823114ljd.4 for ; Thu, 17 Mar 2022 13:36:36 -0700 (PDT) X-Received: by 2002:a2e:a78f:0:b0:249:21ce:6d53 with SMTP id c15-20020a2ea78f000000b0024921ce6d53mr4013908ljf.164.1647549396155; Thu, 17 Mar 2022 13:36:36 -0700 (PDT) MIME-Version: 1.0 References: <20220210223134.233757-1-morbo@google.com> <20220301201903.4113977-1-morbo@google.com> <878rt8gwxa.fsf@oldenburg.str.redhat.com> In-Reply-To: <878rt8gwxa.fsf@oldenburg.str.redhat.com> From: Linus Torvalds Date: Thu, 17 Mar 2022 13:36:19 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v5] x86: use builtins to read eflags To: Florian Weimer Cc: 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 Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,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 Thu, Mar 17, 2022 at 1:13 PM Florian Weimer wrote: > > * Linus Torvalds: > > > You can actually operate on EFLAGS at multiple granularities. > > > > - normal pushf/popf. Don't do it unless you are doing system software. > > There's one exception: PUSHF/twiddle/POPF/PUSHF/compare is the > recommended sequence to detect CPUID support on i386 (in userspace and > elsewhere). Yeah. I do think that hand-crafted sequences using pushf/popf work. But I think they should be in one single inline asm statement. Obviously the kernel use of asm volatile("# __raw_save_flags\n\t" "pushf ; pop %0" : "=rm" (flags) : /* no input */ : "memory"); is exactly that (and yes, I can well believe that we should make "=rm" be "=r"), or at least show that the "m" case is much more expensive some way). Is it optimal that we put the push/pop right next to each other? No. But it avoids a *lot* of problems. 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. 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. > > - you can use lahf/sahc to load/store only the arithmetic flags > > into/from AH. Deprecated, and going away, but historically supported. > > And these instructions were missing from the original long mode, but > they were added back. They're also actively being deprecated, so the "adding back" ends up being (maybe) temporary. > 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. 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. But if there is no documented barrier guarantees, how do we know that the read of the eflags the compiler does doesn't migrate around the code that sets IF or whatever? So then we'd need compiler intrinsics for 'sti' and 'cli' too. And then we'd need to have a way to describe the ordering requirements for *those* wrt all the other things we do (ie locks or whatever other code that need to be protected from interrupts). None of which exists. And even if it ends up existing in newer compiler versions, we'd have to wait for the better part of a decade (or more) for that to have percolated everywhere. And even _then_, the builtin syntax is often easily clumsier and less flexible than what we get with inline asm (I'm thinking of the fundamentally mis-designed memory ordering intrinsics). Linus