Received: by 2002:a05:7412:37c9:b0:e2:908c:2ebd with SMTP id jz9csp394107rdb; Mon, 18 Sep 2023 20:30:16 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGE4SQrtpsndswzYJqviParlOCXn9KRCzfZ3b3dtHzo5MPWw20dB5C64CbbQ74JqQBzlb8H X-Received: by 2002:a05:6a20:4423:b0:14d:6309:fc92 with SMTP id ce35-20020a056a20442300b0014d6309fc92mr11774437pzb.46.1695094216542; Mon, 18 Sep 2023 20:30:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695094216; cv=none; d=google.com; s=arc-20160816; b=d4/Q3zYkqRCqc8jLmHFx1LoO+BZSxaCNVtbHxb3lOkYcakJ+EHUwgmklR4Juux1qml kn0ywcQcDMsSftSQVSoxfO3Y1gxAfpAbXmuSs4yBp4jeLjVNMfx+xIqJZ79z7gO6MU+8 PhNzivi0W2CkNP9SS1eESAWq/GdO3nwCdRzRGEpEc4Y/5LOOEnIifcFmyw5lxtmljgAQ 96M5vE9Sg6y2wnPqPEekWTgPb8O48FGpMJv31s/5tj45hLwZVy8nqhfYhSm7rQd05u/s aNdGwP5VNbAgRXlgE5mK04JwQzbeVFsd1ApB/QDi8vVXHQec5lKc+QuL6xum0sA5VpV9 2luw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:subject:cc:to:from:date:references:in-reply-to :message-id:mime-version:user-agent:feedback-id:dkim-signature; bh=Vgd5OE4AVVQsje5MhoJbDTN5YYPZy+l1MVQ3BAJ34rw=; fh=opdTVWqhJxcomjCr1zRf1+f5MH3pjjKeqfUTehFGGGA=; b=FMMA+xHOKvZclZRgRfYLYC7YSegWo1FTtT4RJuMU5WBp0bQPZgjTBJoL6DyNd7tlF4 Ox0fg8c1P8Hkm0YAV3G92DodHUdEGonMnodWp6nUUnX7ECqXRyPcnhzQd+KXvmJn7uMU 3hG4FBbuiaShZaLVlCTKGUJolGY2VV3rJ/KmQcnPFeT07IVDhW2eH4SnDaABk4oq5DwL wPyrah9vhGXRQiIkFGU0I3Jno3OCAzweS68C/eA3x9kTLfIWbbhgz1uZ5C0izArvXTe4 OZo6EZbdgL8oPKA7Zlm+mrK4FAIfIuvvnp1T/ogzL6O/Ce4IK8kdzXu2eY0zM2/kKNyx KQlw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=UdCk2gRc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id v15-20020a056a00148f00b0068ffda29587si9427168pfu.109.2023.09.18.20.30.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Sep 2023 20:30:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=UdCk2gRc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id CA784811ED98; Mon, 18 Sep 2023 20:28:33 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231279AbjISD2d (ORCPT + 99 others); Mon, 18 Sep 2023 23:28:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34604 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231278AbjISD2b (ORCPT ); Mon, 18 Sep 2023 23:28:31 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2236E95 for ; Mon, 18 Sep 2023 20:28:26 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2B327C433CA; Tue, 19 Sep 2023 03:28:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1695094105; bh=pZggQ60AuJITow/YiIvrlV3FwI2hImVOIwYTooLc/kk=; h=In-Reply-To:References:Date:From:To:Cc:Subject:From; b=UdCk2gRciQGMMzDNdwBndu7mJq0IlT3p0jG904SONrzShXfXmz1jm7Jl2ygP8fjhm AbphpoNDbrXF6TBqjO5/VGq9vl8zprZYuoz0kAt5+rrGqpqptqOiUpcKiaTkX/d87B czBM7vreI0fjIKMWHUpA0K2YiR4To9jfyoqjZNPFYnEFCR+doUYgu+Nizg8+Xn7G3T vWV2CC8s9+UN4UdUEi/NVmQ6CvtjuX44s7qjFb3Ad93MNrY7vkuH8+YPbfDm+ZgpEI 07c9BsELkMmFLBsbbMY/r/HhX3r3Ch+gBcQSnEwUD3jmC5fz/mTCvA4wXbOnsJXjmQ xwNKeFtMnz/CA== Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailauth.nyi.internal (Postfix) with ESMTP id 0C89B27C0054; Mon, 18 Sep 2023 23:28:24 -0400 (EDT) Received: from imap48 ([10.202.2.98]) by compute3.internal (MEProxy); Mon, 18 Sep 2023 23:28:24 -0400 X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedviedrudejledgjedvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvfevufgtsehttdertderredtnecuhfhrohhmpedftehn ugihucfnuhhtohhmihhrshhkihdfuceolhhuthhosehkvghrnhgvlhdrohhrgheqnecugg ftrfgrthhtvghrnhepveffgfevhfeiteduueetgeevvdevudevteefveffudeiveefuddt leeitdeludfgnecuffhomhgrihhnpehkvghrnhgvlhdrohhrghenucevlhhushhtvghruf hiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrnhguhidomhgvshhmthhprghu thhhphgvrhhsohhnrghlihhthidqudduiedukeehieefvddqvdeifeduieeitdekqdhluh htoheppehkvghrnhgvlhdrohhrgheslhhinhhugidrlhhuthhordhush X-ME-Proxy: Feedback-ID: ieff94742:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id B0BCF31A0064; Mon, 18 Sep 2023 23:28:23 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.9.0-alpha0-761-gece9e40c48-fm-20230913.001-gece9e40c MIME-Version: 1.0 Message-Id: In-Reply-To: <20230817121513.1382800-1-jackmanb@google.com> References: <20230817121513.1382800-1-jackmanb@google.com> Date: Mon, 18 Sep 2023 20:28:02 -0700 From: "Andy Lutomirski" To: "Brendan Jackman" Cc: "Thomas Gleixner" , "Ingo Molnar" , "Borislav Petkov" , "Dave Hansen" , "the arch/x86 maintainers" , "H. Peter Anvin" , "Linux Kernel Mailing List" , "Lai Jiangshan" , yosryahmed@google.com, reijiw@google.com, oweisse@google.com Subject: Re: [PATCH RESEND] x86/entry: Don't write to CR3 when restoring to kernel CR3 Content-Type: text/plain X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Mon, 18 Sep 2023 20:28:34 -0700 (PDT) On Thu, Aug 17, 2023, at 5:15 AM, Brendan Jackman wrote: > From: Lai Jiangshan > > Skip resuming KERNEL pages since it is already KERNEL CR3 > > Signed-off-by: Lai Jiangshan > Signed-off-by: Brendan Jackman > --- > > While staring at paranoid_exit I was confused about why we had this CR3 > write, avoiding it seems like a free optimisation. The original commit > 21e94459110252 ("x86/mm: Optimize RESTORE_CR3") says "Most NMI/paranoid > exceptions will not in fact change pagetables" but I didn't't understand > what the "most" was referring to. I then discovered this patch on the > mailing list, Andy said[1] that it looks correct so maybe now is the > time to merge it? I did? Looking at the link, I think I was saying that the opposite patch (*always* flush) looked okay. > > Note there's another patch in [1] as well, the benefit of that one is > not obvious to me though. > > We've tested an equivalent patch in our internal kernel. > > [1] https://lore.kernel.org/lkml/20200526043507.51977-3-laijs@linux.alibaba.com/ > -- >8 -- > arch/x86/entry/calling.h | 13 ++++--------- > 1 file changed, 4 insertions(+), 9 deletions(-) > > diff --git a/arch/x86/entry/calling.h b/arch/x86/entry/calling.h > index f6907627172b..b2458685d56e 100644 > --- a/arch/x86/entry/calling.h > +++ b/arch/x86/entry/calling.h > @@ -236,14 +236,13 @@ For 32-bit we have the following conventions - > kernel is built with > .macro RESTORE_CR3 scratch_reg:req save_reg:req > ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI > > - ALTERNATIVE "jmp .Lwrcr3_\@", "", X86_FEATURE_PCID > - > /* > - * KERNEL pages can always resume with NOFLUSH as we do > - * explicit flushes. > + * Skip resuming KERNEL pages since it is already KERNEL CR3. > */ > bt $PTI_USER_PGTABLE_BIT, \save_reg > - jnc .Lnoflush_\@ > + jnc .Lend_\@ I don't get it. How do you know that the actual loaded CR3 is correct? I'm willing to believe that there is some constraint in the way the kernel works such that every paranoid entry will, as part of its execution, switch CR3 to kernel *and leave it like that* *and that this will be the _same_ kernel CR3 that was saved*. But I'm not really convinced that's true. (Can we schedule in a paranoid entry? Probably not. What about the weird NMI paths? What if we do something that switches to init mm? Hmm -- doing that in a paranoid context is probably not a brilliant idea.) Maybe it is true, and maybe a convincing argument could be made. But that seems like a lot of thinking and fragility for an optimization that seems pretty minor. --Andy