Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp1659549pxp; Thu, 17 Mar 2022 13:51:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxN26j/+m0E+6gI+bM8eF861LynUyT08XOubzHCCEh3oZ08/hfuBelt9agm904HcvHz0z7l X-Received: by 2002:a63:ea53:0:b0:341:a28e:16b9 with SMTP id l19-20020a63ea53000000b00341a28e16b9mr5282926pgk.259.1647550277400; Thu, 17 Mar 2022 13:51:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647550277; cv=none; d=google.com; s=arc-20160816; b=Wc/9X6+DzLN9tfCcRjEz2WG1eOoVxR0nCv17m4r6y30olVC9h0qM9xZpvh9coaonk+ MHApX98aRsq/wEwIRukJhKTIQaYYOPQMpatrNKODJjWfcz7gr+JM4aALTlEzLFqrSrt4 qpLIgVguTKmK7N3RAbyr1Q0PmgnXT56N9NUBshbLXDwkh2TEvRHRvBTZgaf3lJrUnCcd rMbOf52Zeoe18oRQlr2VQK8YEFiFKXQ8Z7pzuCIfxMF5hVqlmu6qR4CJzUR8pOqROElI rlSeDYr1Gslt78V6IaJqHJ/MBIzh2QBmi0TRj15IdCnnqq+v8RmT1HuvTgJK3M7kSBf3 QGgg== 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=1u5q2Fm4MKNSsi+YVIn0z3cE8RvugcBBiZ6jMBBsc+o=; b=TBzyeCGwJ3BW7kjil2CLD0/5JUnPWnsfWw3Kgfm5xI+Qv7+ue6sIeLMlx5v6+G6BvI B4X5eOykmqa63Xv7lTUQy+EBie4tMKMdvEhafH/IwlAro4eWFYiZKUpbvv3z+AQVZDjs lE9V67AidEcaphU48yEyfd0RSyJeSzMCIZN7mmtYn8E1nf3ZzAQQ3wV9bPSEB9KqjhYo gqYdr9yMWUKmVv2e48BzxIp4rw1ofqPg3ka3ho6oh2oDljEhEbe5WBPOVkt72JAiaK1O 02WiXgG61vbiLlT7xGNJiP/2M8tm6R8cHwDUS3dSVTvJsnA/hbUaD1T8qPdBD6gxEJAt O2bA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=Tv2P+Gkd; 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 w6-20020a170902a70600b00153b2d1659esi124045plq.422.2022.03.17.13.51.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Mar 2022 13:51:17 -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=Tv2P+Gkd; 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 1A7ACEAB; Thu, 17 Mar 2022 13:15:01 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229536AbiCQUPf (ORCPT + 99 others); Thu, 17 Mar 2022 16:15:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52848 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229463AbiCQUPb (ORCPT ); Thu, 17 Mar 2022 16:15:31 -0400 Received: from mail-lf1-x136.google.com (mail-lf1-x136.google.com [IPv6:2a00:1450:4864:20::136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C0B0B173F4F for ; Thu, 17 Mar 2022 13:14:13 -0700 (PDT) Received: by mail-lf1-x136.google.com with SMTP id w12so10857529lfr.9 for ; Thu, 17 Mar 2022 13:14:13 -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=1u5q2Fm4MKNSsi+YVIn0z3cE8RvugcBBiZ6jMBBsc+o=; b=Tv2P+GkdHQ/LpT1WXU2lG9YK+/viOPEc9xA3aFbRpn5HE8wBlRYZRSMfrlMRLReqn8 FfQMBXciwuNxWmd88d70YuCLr0zVBwq3S5RTSCGUfYfmNs8iXpjiPwyWiiDO5PjwFKOb +/jwQ63ou7dGVUxmBColYJjRnqJvo+Ct4FjJk= 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=1u5q2Fm4MKNSsi+YVIn0z3cE8RvugcBBiZ6jMBBsc+o=; b=Hpe+0MdTRz5SfX5tRzRZNyeuXOaUYdYhoN6kG9A2stRJnh3ggnwD+YBUZ7795KMSkQ ZdTehPmmA6/+i/sE9hH94JFfMn/BKoKcGr4beF9VCwLse5BfM8JvJaNu2urJ+aA9zxm/ PxAATk0s5ARZpceicInJ1D1YUR0fOI8MIlHav5v4tk4UmTDWZs5JTl6mFmdMbiBz6UK7 yqbt4AAtiNaR4SB3pA46sBv8r3nA1cw2EFTeMb2ZnIJQiY3+u/GlCjx3GOM4WZyL6mj+ kkPZZHRv2O1xc6DKD3Gj5g4H1vnWM6VktJ9EDeJiHxJGoz+cVlHhUtHTwJMx/ihY/3OI 8Jgg== X-Gm-Message-State: AOAM5310LvbyLy+HdImrT+EK9vLx9A/sGZ3U4lWbI6NKfx/Hb8NtJkk+ acRhZTKmYz0vixNo52Pwsgc68DLYrESzPS6Bne0= X-Received: by 2002:a05:6512:2245:b0:44a:727:5fb with SMTP id i5-20020a056512224500b0044a072705fbmr646643lfu.665.1647548051646; Thu, 17 Mar 2022 13:14:11 -0700 (PDT) Received: from mail-lj1-f170.google.com (mail-lj1-f170.google.com. [209.85.208.170]) by smtp.gmail.com with ESMTPSA id m24-20020a197118000000b00448bb0df9ffsm460447lfc.140.2022.03.17.13.14.10 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 17 Mar 2022 13:14:10 -0700 (PDT) Received: by mail-lj1-f170.google.com with SMTP id q5so8692405ljb.11 for ; Thu, 17 Mar 2022 13:14:10 -0700 (PDT) X-Received: by 2002:a05:651c:1213:b0:247:e2d9:cdda with SMTP id i19-20020a05651c121300b00247e2d9cddamr3952516lja.443.1647548049927; Thu, 17 Mar 2022 13:14:09 -0700 (PDT) MIME-Version: 1.0 References: <20220210223134.233757-1-morbo@google.com> <20220301201903.4113977-1-morbo@google.com> In-Reply-To: From: Linus Torvalds Date: Thu, 17 Mar 2022 13:13:53 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v5] x86: use builtins to read eflags To: Bill Wendling 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=-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 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. Whether they actually clobber memory or not is immaterial, and is not why we do it. > 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. 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. A compiler should do a good job at the basic instruction set, adn then give us the escapes for the unusual cases. Stop believing that a compiler can deal with every part of system programming and that everything should be intrinsics. Because you don't have all the intrinsics we need anyway. Linus