Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp1694136pxp; Thu, 17 Mar 2022 14:44:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwrNet0VgWIae5wsBn86qzrYtImxs2ZDxJxs0Ym6349GhAXxAaXfMvRHy5FZ7RLbx7F6wSs X-Received: by 2002:a17:90a:7305:b0:1bd:6972:faf5 with SMTP id m5-20020a17090a730500b001bd6972faf5mr7701177pjk.131.1647553456619; Thu, 17 Mar 2022 14:44:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647553456; cv=none; d=google.com; s=arc-20160816; b=cAbhWtDYUQ48X+QOXjNCoREs5Hhj6KkHqWBHFrHExflNU29jmxkzCMwcrhVT8mX39f oqmbSlvWsk8PqzKkdDnfpb2/Pw7khWmk/1wadlO17PBfL3/KYCjllMg/bbpeoPwms+Dv JeVuZ8OhhRZG4NkcchXJWnFGM9L3QXFahQL2BHPNA7DlJMieM0RvdUX6LpniLkDfUoQI 9jlOCp1My8psVv0qBudV6bAE5E27U2i1nFsEk8h8Rsd6K39zZmLGb6PRxP/rshqLcI9W 3HhHzTR9DfhWpzM6/kFEdCogwCuLQ8QQf9CfGli6MZGPTKgNSs0W6exvp7erw34c/H7q YEXA== 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=5Plv3qFJV4Lb71g1Cd3cAsDGLjYDWIwI/2QmHRRATbM=; b=J84rsrjR2vrMX6WXZEHOrl1qrts8p2mutxLOsaARzD7sArYMU73gcF9hI4gg0JxdjU tOagfQ+CCbRhTRr6ahovCQ07SEtpZ2BlY3KGfj5lBJxoBvHqoGnS5Qn0Kfb500g3oojB 1JpWCAgf+GLvlrCQnSlvlhKzNwUOoSBRrxWzlyXpPxVWZuVlsfdwYJm1mTykaHkyY+Sb YBXwwPLymDDrZMW25UP2xXrpzLWH7gSrZ6EvijdFYAKRQ3tLeVWcaokYPU6T+9W2tkR9 FzySRR3Y6b476LB2F4pBEhjQumWUkfue6jp4QlVW0eVYAPYfQ28HyIOGYloeOCHSkHtQ GCmg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=GsA2phHo; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id s20-20020a170902b19400b00153b2d1657dsi249738plr.389.2022.03.17.14.44.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Mar 2022 14:44:16 -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=@google.com header.s=20210112 header.b=GsA2phHo; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 9CC25113D05; Thu, 17 Mar 2022 14:10:45 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230125AbiCQVL5 (ORCPT + 99 others); Thu, 17 Mar 2022 17:11:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34724 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230117AbiCQVL4 (ORCPT ); Thu, 17 Mar 2022 17:11:56 -0400 Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 27D6E111DEF for ; Thu, 17 Mar 2022 14:10:39 -0700 (PDT) Received: by mail-pj1-x102e.google.com with SMTP id l4-20020a17090a49c400b001c6840df4a3so2816941pjm.0 for ; Thu, 17 Mar 2022 14:10:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=5Plv3qFJV4Lb71g1Cd3cAsDGLjYDWIwI/2QmHRRATbM=; b=GsA2phHoKS3U+n8Q+EKkao1i2kLYoXBiTwVxlNflDcHfrWF1kVZ3ya+EF0m7ANPm7/ uSkG6ZPYThP1KALJnZonIggc4+Qbc3BLrX67TnXQ/tcFBU9yv8f60/ejL5Znzp4dl/fI TDLSSvtK3NpSB3G9m0WtnXqAaifVcl+W6YiUXbEw6ascIF9cRDcgkGxZaf2vBtMPr5ON Rof96we0l6PJZPace6iXgeQL8ewmyusPrAvS7iQbcQedc2ol0MWDD4+Tq9h2mABOJvrp l24xn1xvaELgFSBYZiCtunufIPdl1x/nfZiwpFGYgeGaaisFepwT/fPc2ssFOLCkLQk1 hbuA== 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=5Plv3qFJV4Lb71g1Cd3cAsDGLjYDWIwI/2QmHRRATbM=; b=0U8BM37OtbwrMAIk0PqeMA6YyV1p7Yh3Zoek2mqAaCpjWHnoPzxTeaPxzP+gGKP/cE Awc5cpR2kHuZGUeKpKaCmbgRH6AlQi/hxq0O1/MAwY/8J0burwO3qyOHRFdiJOsxsjlT ozDvlWV3/hj1ep5idyXnTc4k3moJoPS9g9g2tAc3BiEn7HaFernzVZnMuziZbtWeLhz9 K1Sz3Wr3KCCTcOvtKRK9Nv2TVSNI2NcEf8/gnH5K3Wt17WLSxKnyLSxbHyDqJvtqqU/w t650rBpVwtlyVH+NvoQP7Ad0Rw2QR8JlJ6f0AnDbfmglbaDCBq6Awm4QEDyK7Nnrtt8w wjkQ== X-Gm-Message-State: AOAM5329P1ipFfuQTigLbsbaXEBQU303RjPU3jo4NvWw3aSy3Ix/Mp1Y X8bqA4L1jupj/a4sa6kmL+zf7HFjxJJdvgESf0fm X-Received: by 2002:a17:90b:1d83:b0:1bf:39d7:207d with SMTP id pf3-20020a17090b1d8300b001bf39d7207dmr18277640pjb.130.1647551438363; Thu, 17 Mar 2022 14:10:38 -0700 (PDT) MIME-Version: 1.0 References: <20220210223134.233757-1-morbo@google.com> <20220301201903.4113977-1-morbo@google.com> In-Reply-To: From: Bill Wendling Date: Thu, 17 Mar 2022 14:10:27 -0700 Message-ID: Subject: Re: [PATCH v5] x86: use builtins to read eflags To: Linus Torvalds Cc: Nick Desaulniers , "H. Peter Anvin" , 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=-9.5 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL 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:14 PM Linus Torvalds wrote: > > On Thu, Mar 17, 2022 at 12:45 PM Bill Wendling wrote: > > > > On Thu, Mar 17, 2022 at 11:52 AM Linus Torvalds > > wrote: > > > > > > But the whole "you can't move _other_ things that you don't even > > > understand around this either" is equally important. A "disable > > > interrupts" could easily be protecting a "read and modify a CPU MSR > > > value" too - no real "memory" access necessarily involved, but > > > "memory" is the only way we can tell you "don't move this". > > > > > And yet that's not guaranteed. From > > https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html: > > That's my point exactly. > > The 'volatile' part of 'asm volatile' is almost meaningless. > > As a result, we mark pretty much all system instructions as being > memory clobbers, because that actually works. > For now. > Whether they actually clobber memory or not is immaterial, and is not > why we do it. > I understand that. My point is that it's not a guarantee that the compiler won't change in the future. > > Note that the solution _isn't_ to add a "memory" clobber, because it's > > not guaranteed to work, as it's explicitly defined to be a read/write > > _memory_ barrier, despite what kernel writers wish it would do. > > The solution you quote *ALSO* doesn't work, because they used a > pointless example that was made-up in order to get to that solution. > > Nobody cares about an operation being ordered wrt an addition. > It's a short example to demonstrate what they meant. I doubt it was meant to be definitive. And I'm not suggesting that it's a general solution to the "we want the ordering to be this and don't change it!" issue. I'm just pointing out that even the current code isn't guaranteed to remain ordered. > Mostly kernel people care about an operation being ordered wrt > something that the compiler DOES NOT KNOW ABOUT, and there is no way > to actually tell the compiler, exactly because the compiler has no > effin idea about it. > > But the other thing kernel people care about is ordering those > operations wrt externally visible things - like modifying memory. So > an MSR write (or a write to a register like %CR0) may not itself > directly read or modify memory at all, but there are other reasons why > it might need to be ordered with any memory operations around it > anyway, because some of those memory operations may be indirectly > relevant (ie maybe they are page table writes and you just changed the > page table pointer in %CR0, and now - even if you don't access the > particular memory location, speculation may cause TLB fills to happen > at any time). > > You can't tell the compiler "this eflags operation must be ordered wrt > this MSR write" - because even if the compiler knows about eflags, it > doesn't know about things like page table contents or specific MSR > bits, or whatever. > > In a perfect world, we could actually enumerate resources we cared > about somehow. But that world is not the world we live in. > > So we end up basically having exactly *ONE* resource we can use as a > "the compiler knows about this, and lets us use it as a > synchronization point". > > That one resource is "memory". You may not like it, but you have > absolutely zero realistic alternatives, do you? > > > Your assertion that compilers don't know about control registers isn't > > exactly true. In the case of "pushf/popf", those instructions know > > about the eflags registers. All subsequent instructions that read or > > modify eflags also know about it. In essence, the compiler can > > determine its own clobber list, which include MSRs. > > Nope. > > I think you are thinking of the arm64 MSR's. As far as I know, no > compiler out there - and certainly not the complete set of compilers > we support - know a thing about x86 msr registers. It's all inline > asm. > > And honestly, no sane person would _want_ a compiler worrying about x86 MSR's. > It's true that a compiler won't take a seemingly unrelated resource (MSR) into account ("unrelated" only in that the ordering is important to the programmer, but the compiler doesn't see an interaction and thus may move it). There was a discussion on that topic here: https://lore.kernel.org/lkml/CAMj1kXFnUuWLyy5q-fAV1jwZobTCNHqhKSN3mF98frsJ4ai4Ow@mail.gmail.com/T/#md85de238571dfe2bb4e4a822e6c983fb7a5ecf60 I don't think a conclusion was reached about preventing a reordering. -bw