From patchwork Fri Jul 12 17:00:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brendan Jackman X-Patchwork-Id: 13731993 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E2A1CC2BD09 for ; Fri, 12 Jul 2024 17:01:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BAA846B00A1; Fri, 12 Jul 2024 13:01:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B358F6B00A2; Fri, 12 Jul 2024 13:01:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 937FE6B00A3; Fri, 12 Jul 2024 13:01:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 676E86B00A1 for ; Fri, 12 Jul 2024 13:01:24 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 1D8BDA3F26 for ; Fri, 12 Jul 2024 17:01:24 +0000 (UTC) X-FDA: 82331716488.12.0484F66 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) by imf16.hostedemail.com (Postfix) with ESMTP id EC53D18002A for ; Fri, 12 Jul 2024 17:01:21 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="Z/ZdD3cF"; spf=pass (imf16.hostedemail.com: domain of 3YGGRZggKCKMMDFNPDQEJRRJOH.FRPOLQXa-PPNYDFN.RUJ@flex--jackmanb.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3YGGRZggKCKMMDFNPDQEJRRJOH.FRPOLQXa-PPNYDFN.RUJ@flex--jackmanb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1720803647; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=jms+qnUoGWR3Ok8D5PSRh1HvZeNFLLNfdQwhqdA9AN8=; b=JcQCh6+SYgkBypsp30omWSUocgwujasfYqNiM2iJqnJ6Gd4+AZH0eFkzrC1aWIb94JttNx JNQ0hLLuJ3TFxL7DWRVQ+vjyQInSpj5Rf0UfzNm2EljC771RPvtT8L3OH/pOT6OApWHubI BZB0/W+2TKD4sUMQeaXkDCr3eoVo4eY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1720803647; a=rsa-sha256; cv=none; b=Y+IlCYV18sOpBzseypmAO/dzz1cHo0izBn+pkahYeIqNeN9bXX6EzjKmqJc/k+PG92Xy7n NC2rdVh5zCza1/PJeS9HUI7UGG/RcoyD3jYIb0OeqISbwrM5WWS7gDKpnkfAku6tVDfVIe IcaVhpqDT3nKt5Kc4g+/AHjSOrihDas= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="Z/ZdD3cF"; spf=pass (imf16.hostedemail.com: domain of 3YGGRZggKCKMMDFNPDQEJRRJOH.FRPOLQXa-PPNYDFN.RUJ@flex--jackmanb.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3YGGRZggKCKMMDFNPDQEJRRJOH.FRPOLQXa-PPNYDFN.RUJ@flex--jackmanb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-4265464ddc9so12814175e9.1 for ; Fri, 12 Jul 2024 10:01:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1720803680; x=1721408480; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=jms+qnUoGWR3Ok8D5PSRh1HvZeNFLLNfdQwhqdA9AN8=; b=Z/ZdD3cFyg+wQ+WLYsPvtVddXtKR6sC5/cipvY4MVDPPdnRUA7fDx8h4s6kN/l6JLA fbHbzT/ZbTwX18lJA5WUeomu1whBa23bW5VSe7SYpx+txuVbgCeLot1YaJKrX2FcHzUF t780esivmslZf9oSfuZCmAp+3yYZFGK63/5MzXVVMfjuOPNcjl/BByw21AV3wlkDrRHM RqYhPA0x5uXRbmgVVqBzEhynMCo6ESJ3VjjdzwkGRLNlQMvkqebnCcwTpgK9CVvYoZv0 8tJmJWdKB2cMPwrJMmi820UkrQpFxf109DpcLS8aPAApNeO94rmOGF4on9f8PXKmRnh8 w4Xg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720803680; x=1721408480; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=jms+qnUoGWR3Ok8D5PSRh1HvZeNFLLNfdQwhqdA9AN8=; b=QpK0PYk3VL6FbpelIWxYvJqmEYgKw9/edoL9IjvyQVR1Y1s26QQMd0cfGI3LaWQRo7 cu93Kl+MLbjJG8gSp3tvN7iWvjmk+NIP+NRxvsTds15hlLswlDLtEj/08yOmoo+cOUmf uuhtxkglgouwbREcYD6/hJ6w7AsM5Ik+SevbO4WJhKUnwevgeqcVINhgoXjlu+KRkPd7 /gseA+xbmwCksTV0HyqEqSdG0gNQMIimUgBe8qoQqzASlguFQ2RmyJwGUK9w/fTFJEjg 1YKgI+S+4p5gnavGsZIoaFA9gmVcyRvkLkPhYqhn1A2H6vfSA9h9T3hX4VmxzwpsOtBq 0ovA== X-Forwarded-Encrypted: i=1; AJvYcCXUynzzLzKIluF/EQ4M54sgcm5d5czGZaWx0COgB/eivDJxKhFMtS9IZdq0zKwVFwwRPDvZaJEYq3iPbAp8VlUbZ2Y= X-Gm-Message-State: AOJu0YwDBUKN/B/ki//KZMvhYi+dxd1zp5UfxPsMTK7+8hUEY5pdaL94 /EQrpg4LapYIiarl361gLf6VF5Hc7/fMB0B9eBl6e1ukwp8uYkNP/yBBC+lLKv50RJgqCZq+RF9 JXkS56nhbRw== X-Google-Smtp-Source: AGHT+IGisRm/zH5xPuc3vZntkMct9zRaY/l9dILlFDVOJcrKy+wGrjnqUwhX4l9eAthBD2p9mDlo+nx2x2+a6A== X-Received: from beeg.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:11db]) (user=jackmanb job=sendgmr) by 2002:a7b:cb11:0:b0:426:670b:20bf with SMTP id 5b1f17b1804b1-427a0a24cb6mr168765e9.0.1720803680269; Fri, 12 Jul 2024 10:01:20 -0700 (PDT) Date: Fri, 12 Jul 2024 17:00:28 +0000 In-Reply-To: <20240712-asi-rfc-24-v1-0-144b319a40d8@google.com> Mime-Version: 1.0 References: <20240712-asi-rfc-24-v1-0-144b319a40d8@google.com> X-Mailer: b4 0.14-dev Message-ID: <20240712-asi-rfc-24-v1-10-144b319a40d8@google.com> Subject: [PATCH 10/26] mm: asi: Avoid warning from NMI userspace accesses in ASI context From: Brendan Jackman To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Sean Christopherson , Paolo Bonzini , Alexandre Chartre , Liran Alon , Jan Setje-Eilers , Catalin Marinas , Will Deacon , Mark Rutland , Andrew Morton , Mel Gorman , Lorenzo Stoakes , David Hildenbrand , Vlastimil Babka , Michal Hocko , Khalid Aziz , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Valentin Schneider , Paul Turner , Reiji Watanabe , Junaid Shahid , Ofir Weisse , Yosry Ahmed , Patrick Bellasi , KP Singh , Alexandra Sandulescu , Matteo Rizzo , Jann Horn Cc: x86@kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, kvm@vger.kernel.org, Brendan Jackman X-Stat-Signature: mbi113szfy5hc6o7rqi1oc6fiabmtj7j X-Rspamd-Queue-Id: EC53D18002A X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1720803681-575358 X-HE-Meta: U2FsdGVkX1/Yc3w/W+eXmj/VlBZR7theP9cGvrvvIE0rVMHEgTwmeSKbn2TxIKeKKlp04KL25W2LaDvWLQBRMyMsDDMMM3/kp8N6P7CwLlb83bce91tQUpIWupc5iZsU860efz1Jg3PwkW5CrRBWR7C0wqj7AIlCCj2ujGOId6eB1u2bP3B//5/nOuaRzSB9h2qE53Mpg2b7l47YHb1MbOTAx3PYp88ktSfoNfCvdNcPI90JbB5pUbgPziVPFDFhN5eo6A50EmeRU3ud2FGQdHFk28/e9ZjknuqEbOzqqHU5Fmq4Q0FTmCCmEN/FfeC7uqTWsET280mnSkqKwJrFp6dQRe5xDAf9Gwo8GaFZjZ+2EAgubbL35vwhz6D6WaGvkO422aKO1puymzP505jxXKP7NSUE/qyJZ/o7Vqx1gRbpI8fSYaPX3s+Dsy+1wix+Ij9fR4UFTxfa3KdAwX2hJjHOpEdy0afmmfTR0t+DtseshnCtIajf/f/QSTCM7DUQC9FuH3xxf1FKiSeyfHOKsXfx3cJj4DxQkF6rPfPwlx9sV1HuSsv0ePRBAIBUZ8/fqzKjKow7T59bgdDtXyLG9V9XwEMU6qpiTbF4Kj6X2V0m2+6Uzx9oV6U5vPHlorrBvPeVt3LGK3ABFeXHcgPffIlXiihDdX9f3Tv3fbOP+gHl9mLhBIc5eRMp0558nDmRKrR/o7iGr/TxrZ8vitljpVTdqzerqOnRWlT9VSJ0k2IANae0Y4qZVOGffsOpsymvcy9E9k5SMbtcFrqYNVaVJQG6OkY5YcKvbikMxn3SUo6QztxmDRkmGng7n2Z1c4jtGduDEuYX3cMMRLLAqGbQlpMuwx/eah9u+jZDr4B4zaKzgfa86ldnqHrLMSYoxufFHFvsOgbCwAhyZ8AkXlMaS9JHrHpvBb91yi0H4Jx+hR0PZgYhwRNCGE7wrOiOaFJX8LZRueZT8Zwqh5Y+xm1 kDPQ93QW YHyNU/IcfDgXAQo+LO/EApYVwm/cXsF8vyXtf6uqS6isnPGDCZF0v+kimeHlynymze791s7ILapHYPpUD+mPNNogJ+B2hbb2PfO7+3nme5b5JMFmlB1ZX33g4FNtavPxt0M9n40gD9VcsAda8i1rYup/MNCeduVTRz9X4RVfkDInDrEClDDMFtOaDmu4kr6I2SLLmlJrHhwsmAJh9OpWwKxdIH6ynwabJnNBUYz5amxzECOweFYGXo/ew5x9qYxFF9kkWOSuVXC0tD8+0c9WddaF1dtQgiDDF8qlZRuVqTBm+4yES5fEK0kUAVv9BMcZmI2q/QB6VtIYi82bhP/nsim7vUI/sY64PI/dUpVQ3Klaz66X3iVW4GBNyLtdeQXz6xv5rxcBHpmavqDtP7zP2NRmdgDYlUsRjclmHCZ6G8fNeXQFzc2roa/7SAAanGGIV1C0zOqoCT3SfAGYJZaXlSIzZ9tQ+y3YwH35Bnjd+B1e9yDp6oQFe9tZBv1VGdaIoSLvtVfa/DjaLlKtcd7Z2N9AEvFkvYp1Fc+D6 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: nmi_uaccess_okay() emits a warning if current CR3 != mm->pgd. Limit the warning to only when ASI is not active. Co-developed-by: Junaid Shahid Signed-off-by: Brendan Jackman --- arch/x86/mm/tlb.c | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c index 02f73a71d4ea..e80cd67a5239 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -1326,6 +1326,24 @@ void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch) put_cpu(); } +static inline bool cr3_matches_current_mm(void) +{ + struct asi *asi = asi_get_current(); + pgd_t *cr3_pgd; + + /* + * Prevent read_cr3_pa -> [NMI, asi_exit] -> asi_get_current, + * otherwise we might find CR3 pointing to the ASI PGD but not + * find a current ASI domain. + */ + barrier(); + cr3_pgd = __va(read_cr3_pa()); + + if (cr3_pgd == current->mm->pgd) + return true; + return asi && (cr3_pgd == asi_pgd(asi)); +} + /* * Blindly accessing user memory from NMI context can be dangerous * if we're in the middle of switching the current user task or @@ -1341,10 +1359,10 @@ bool nmi_uaccess_okay(void) VM_WARN_ON_ONCE(!loaded_mm); /* - * The condition we want to check is - * current_mm->pgd == __va(read_cr3_pa()). This may be slow, though, - * if we're running in a VM with shadow paging, and nmi_uaccess_okay() - * is supposed to be reasonably fast. + * The condition we want to check that CR3 points to either + * current_mm->pgd or an appropriate ASI PGD. Reading CR3 may be slow, + * though, if we're running in a VM with shadow paging, and + * nmi_uaccess_okay() is supposed to be reasonably fast. * * Instead, we check the almost equivalent but somewhat conservative * condition below, and we rely on the fact that switch_mm_irqs_off() @@ -1353,7 +1371,7 @@ bool nmi_uaccess_okay(void) if (loaded_mm != current_mm) return false; - VM_WARN_ON_ONCE(current_mm->pgd != __va(read_cr3_pa())); + VM_WARN_ON_ONCE(!cr3_matches_current_mm()); return true; }