Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp273702rdb; Mon, 22 Jan 2024 21:34:29 -0800 (PST) X-Google-Smtp-Source: AGHT+IFVTKjUtGcZoc3aj+Hv2Yb4xkG0gWh7jqhY25AVjanuBoqCk40nO/wM3/h6fx5Q8qKcO8QR X-Received: by 2002:a17:902:e54e:b0:1d7:6f1e:d19 with SMTP id n14-20020a170902e54e00b001d76f1e0d19mr335167plf.4.1705988069276; Mon, 22 Jan 2024 21:34:29 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705988069; cv=pass; d=google.com; s=arc-20160816; b=SyNjb7b9UJYKyPzjrpPwagmiBKbDxtUq1nfIxwsCHJOJqmOkSQI5ZKlHy9gyue0bzv 1ISv4M2zj1LF4/9FWfN4Cti3GWN13rLAvGa7aYV6xXllnuBLkyC42JEFmZ1gHcat0sBC RthS7LfH2UCfE2settNbOkvMwwJD5dCu385RGpX0LmUiECxhzzj6u5nGxamr7KmhsXaG zucGsBvbAVQq3cap5tbp46tNddJvxFghQhbCLMg78/z3Bzsj/zB3HHxo0Dc8CJDoVBMs wrj5h1TmEF0QJih2oJ6caeXblV9SLdCl4tm6qj075nNzeFo++O8602qkWpdl58dwfk+Q Cc3Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=UE+KZeEYVb1K/Fr/C+bxoTCDGKC1AQGgVq4rfstEFAY=; fh=/JHxWjMVzbHOKr8RJ5QUbzQ/+lKXmCL3hSNIJ33dACY=; b=npN4lfYpmOX8Hg6Aza+k63Q4C3reCe0kv3XahdxcR3srKhiyqaHGiC5T0bIMbTfjbA oKSke+7JqvY3IK9ltfMhXwVNuSLNxPTq83brTlsHBsirHBbwZnq8ZWpc9WpxrlJ0J7g+ MKnkGHYxH9Fn8TDRK8nBe74PhNQr+V7i+rq4Cf/0Gcy9WfLMsNLLuIONlvnbDMdJ/KYI DtIFtjj+vmPe9kRznWYAZQ/0dHedhOlZZYQzJCEnyxAf5kQYv3j6PmZ/57QgR5/Iu3B3 R4L2D2xCIVg1s8prLV0pmCToebj+15Z6nEAZpwE/wJpTPT3NDYKsmbIqmlniN4jm7oXK eNjg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Mkl2eeVG; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-34725-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-34725-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id l9-20020a170902d34900b001d766749e8csi1331896plk.375.2024.01.22.21.34.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jan 2024 21:34:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-34725-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Mkl2eeVG; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-34725-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-34725-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id AF3EC28BB01 for ; Tue, 23 Jan 2024 05:34:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E1481524B; Tue, 23 Jan 2024 05:34:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Mkl2eeVG" Received: from mail-ej1-f42.google.com (mail-ej1-f42.google.com [209.85.218.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 61DCF5220 for ; Tue, 23 Jan 2024 05:34:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.42 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705988062; cv=none; b=K/AjPE+DNeglmILOgr0m6UKKKcVJWEpELoUoHUqNPt8Sxhoz9tMQJ1ej5S1uRqLAk63CYIKhxsRQARnfGPYTaQfY7m25PMdlq/osnWiq42fhFTCzf+DBwuNn/efjfGNi6VFcaOzMTp1smtt1GJSMRY1r07HLbqzEetndsfK9h2E= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705988062; c=relaxed/simple; bh=05iOvp2t2GTVQ0v6IEkxDMRSaKcObKhdziy74COWLtg=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=d1exNPILxR6DPFsHJJcF0zTeYPzVvEVPg7n9LraOTNIWT/cOC2LebYTQuSUtWQrKDixBwSafLYlX86qCborQgtzNea/xxZD7Lvw2qSZUyTxgPnDw6G5DwXvUx53C3DDkYQTP8Ds8zBI4XagQfau4HDqaKsBeBp/rzCEhWiTBYdo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Mkl2eeVG; arc=none smtp.client-ip=209.85.218.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-ej1-f42.google.com with SMTP id a640c23a62f3a-a277339dcf4so425124966b.2 for ; Mon, 22 Jan 2024 21:34:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1705988058; x=1706592858; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=UE+KZeEYVb1K/Fr/C+bxoTCDGKC1AQGgVq4rfstEFAY=; b=Mkl2eeVGY49kpN7Fz4wt8/epkrCXMBq3S+zz+AVL4RBBS4HbeiwJF8b4n59M14wg8j NW6/3y3DovbEre4r2Uob4RNHt0hDK06jVtHeTFjwrV211xjodBN1VqihsHSK1lwKCdKh lv6BYc7DN97enW82QBDMKIIpPHxIEpJoVAA6HqH5VLh6dhh6l7u03qvA1Ai5Bs16jb8b tXgwq1HP+MJjtnmGjx/uLnSzJMac5iwuhaIU2vfmt9ew+sFa1/hTMAmr4wXCotTZNXGa VZdZmG/sd9jQ/YYvvgr6+OPprdzPbpIYjsAziub9KEPPOvtzTyg1/qd+w7YP+vxIAlSi MRQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705988058; x=1706592858; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=UE+KZeEYVb1K/Fr/C+bxoTCDGKC1AQGgVq4rfstEFAY=; b=Aw4/ekcS3LJWQSK183Xoc/sTUysHx4DU2dx+beKf0DGnleF99J8QO7LRd2Ymmb81pc 1Q/wtCiDdl1qFrqanpImNA3A1ak0KTZXnXpNQGAsSJX4I2yy1a4MhteSrAHf89xPHqti LUm9+fZ+9nA0SmFRSjVF16C7M8NqzHVPnaOMM9pnrbCyE/Pecjte/Yh0R+6KCB1tscDr k3d383PudhQ8KoAHRopnii7ATZDGZS8r2edrSiRSB4ZPhbW2c0ZCyftjWSiJZUPwSbG8 NqtOvLLodBJcjafud8qWzmrKRiTvrPMnTND2jBdvbFcp4YydXY6peziFIg9K6k2c/1R1 Rmjw== X-Gm-Message-State: AOJu0YwVxEbwE3jItrXYw6CgSJxxWJwoJQCUI1XSpClKvjA5cekwHUf8 5hWXHvoMF/hiYbdRhAu9yJmKGQ2eHUB3iqkB2L3uBEnxdIoU7pKdNbXpMvGTAYUY7Wir5PC378G dcXVjld1/msCdUTbVKCpK6+XfJw4thVYXYPr0 X-Received: by 2002:a17:907:31c5:b0:a28:ce1f:6995 with SMTP id xf5-20020a17090731c500b00a28ce1f6995mr3259459ejb.36.1705988058321; Mon, 22 Jan 2024 21:34:18 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240108113950.360438-1-jackmanb@google.com> In-Reply-To: <20240108113950.360438-1-jackmanb@google.com> From: Yosry Ahmed Date: Mon, 22 Jan 2024 21:33:42 -0800 Message-ID: Subject: Re: [PATCH v3 RESEND] x86/entry: Avoid redundant CR3 write on paranoid returns To: Brendan Jackman Cc: luto@kernel.org, tglx@linutronix.de, peterz@infradead.org, dave.hansen@linux.intel.com, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, linux-kernel@vger.kernel.org, laijs@linux.alibaba.com, reijiw@google.com, oweisse@google.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Jan 8, 2024 at 3:39=E2=80=AFAM Brendan Jackman wrote: > > From: Lai Jiangshan > > This path gets used called from: > > 1. #NMI return. > 2. paranoid_exit (i.e. #MCE, #VC, #DB and #DF return) > > Contrary to the implication in commit 21e94459110252 ("x86/mm: Optimize > RESTORE_CR3"), the kernel never modifies CR3 in any of these exceptions, > except for switching from user to kernel pagetables under PTI. That > means that most of the time when returning from an exception that > interrupted the kernel no CR3 restore is necessary. Writing CR3 is > expensive on some machines, so this commit avoids redundant writes. > > I said "most of the time" because the interrupt might have come during > kernel entry before the user->kernel CR3 switch or the during exit after > the kernel->user switch. In the former case skipping the restore might > actually be be fine, but definitely not the latter. So we do still need > to check the saved CR3 and restore it if it's a user CR3. > > Note this code is ONLY used for returning _to kernel code_. So the only > times where the CR3 write is necessary are in those rather special cases > mentioned above where we are in kernel _code_ but a userspace CR3. > > While changing this logic the macro is given a new name to clarify its > usage, and a comment that was describing its behaviour at the call site > is removed. We can also simplify the code around the SET_NOFLUSH_BIT > invocation as we no longer need to branch to it from above. > > Signed-off-by: Lai Jiangshan > [Rewrote commit message; responded to review comments] > Signed-off-by: Brendan Jackman > Change-Id: I6e56978c4753fb943a7897ff101f519514fa0827 The Change-Id line here needs to be deleted. Otherwise, it seems like this patch keeps falling through the cracks :) Is there anything that needs to be done here? > --- > > Notes: > v1: https://lore.kernel.org/lkml/20230817121513.1382800-1-jackmanb@go= ogle.com/ > > v1->v2: Rewrote some comments, added a proper commit message, cleaned= up > the code per tglx's suggestion. > > I've kept Lai as the Author. If you prefer for the blame to > record the last person that touched it then that's also fine > though, I can credit Lai as Co-developed-by. > > v2: https://lore.kernel.org/lkml/20230920150443.1789000-1-jackmanb@go= ogle.com/ > > v2->v3: Clarified the commit message per Dave's suggestion and rename= d the > macro. I did not carry PeterZ's ack since I have made some change= s. > > original v3 (no responses): > https://lore.kernel.org/lkml/20231108171656.3444702-1-jackmanb@go= ogle.com/