From patchwork Fri Jan 10 18:40:33 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brendan Jackman X-Patchwork-Id: 13935236 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 575B8E77188 for ; Fri, 10 Jan 2025 18:41:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 005ED6B00C3; Fri, 10 Jan 2025 13:41:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id ECBEA6B00C4; Fri, 10 Jan 2025 13:41:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CFA4D6B00C5; Fri, 10 Jan 2025 13:41:05 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id A51836B00C3 for ; Fri, 10 Jan 2025 13:41:05 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 48D904433B for ; Fri, 10 Jan 2025 18:41:05 +0000 (UTC) X-FDA: 82992409290.28.F6EAD72 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) by imf11.hostedemail.com (Postfix) with ESMTP id 5A96340003 for ; Fri, 10 Jan 2025 18:41:03 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Lmir1uJK; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf11.hostedemail.com: domain of 3vWmBZwgKCNgD46EG4H5AIIAF8.6IGFCHOR-GGEP46E.ILA@flex--jackmanb.bounces.google.com designates 209.85.221.74 as permitted sender) smtp.mailfrom=3vWmBZwgKCNgD46EG4H5AIIAF8.6IGFCHOR-GGEP46E.ILA@flex--jackmanb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736534463; 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=/j+RB8/lAgzqGTtPca+XTmsHyqC3ld9qGwXYbAsCbQ0=; b=4qEH/W+LIyWfqiQ1RfNJoQZUJxfbrc3sCTwstKeQ+P7/3QA2x7H7LPNcfAxgog+F8a0gxz YGuCSb2ra6iQxmmizRTovUPnBSY4JOlEzTCjt2ibrR11HghJ8f91qzSXux05iIBRoK5Dk6 tZu7xtMFiQZdKGX8yN/rwp58FsXosQ0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736534463; a=rsa-sha256; cv=none; b=r4A/Vi+T0WjkNEvKpHdxbcjpRnm/QleLqjq669+WGeRqbWCaVNuaG50QnD+G+Wafh+npKn CFnrQXsnVqLeTv7a6XMZgkzFh5ISMvDbM43+dOnwxjUoHw92Je57ZkFi61wJxAqsl+bHcZ LP8fXqjHcgG+4SeRhy6FfHdMXm7kl0M= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Lmir1uJK; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf11.hostedemail.com: domain of 3vWmBZwgKCNgD46EG4H5AIIAF8.6IGFCHOR-GGEP46E.ILA@flex--jackmanb.bounces.google.com designates 209.85.221.74 as permitted sender) smtp.mailfrom=3vWmBZwgKCNgD46EG4H5AIIAF8.6IGFCHOR-GGEP46E.ILA@flex--jackmanb.bounces.google.com Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-385d6ee042eso1539329f8f.0 for ; Fri, 10 Jan 2025 10:41:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736534462; x=1737139262; 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=/j+RB8/lAgzqGTtPca+XTmsHyqC3ld9qGwXYbAsCbQ0=; b=Lmir1uJKq61yPZlt8QUQPk2jHA9a6rHiFJ3p+eV7fZykND84mCINHjdEP9QKgTIEb1 Ilr1gwn4vFeWILRlo5u6VdVy0CZNyMWh5QlnoPC81nO94RTmgjnM8D7BWXFklMP1En+w pDBXAxZEr0qRtY/oXiBuBnwejjMlyDJktNEsbrw1s5b5Ph+baXLVAOd0nkTdBgtDd589 FFT2Gja+bT2pt67qnn4sDDZoXQ1t1Mo9LZ3EyEv5ufB5RTrqYN26kxJLq5LFk9ar6bs7 y+BBjuc37U+Ixhr53vQp5x4MNUO96urGIuEZ2NnMHxbLhVlDLhZ5h9D/d1njwsDmIMf/ ff7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736534462; x=1737139262; 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=/j+RB8/lAgzqGTtPca+XTmsHyqC3ld9qGwXYbAsCbQ0=; b=uf0BnccPkCUhpI9lRqv1AKv/UfWr0pWTBwJkT6HtZv8dmhcFqadYZuwQH+mPpd8V1c 9bvFmsryWqbC5gVSFv9B0egG2RFjbQnjprDB7SI3upEIWJ87gdNKSdBdAlM7lhsmVghV 62BU5ejf9lH63WMurOF4gtxm0NGCrJ5DTJrpJIPCU5a6cd+92R1fRTk7SWuISQ1pTNC5 PavaddKrHtw57oyax67FGk+QvUfymg8C4GCO3OrQc6AcUBOVIDnM0Ep3QxAyO8qfgN2P kizAX9UISKOk5uCD2C7XBuacLJO7dvIHgaRswVtUZR/gl+tLVb8VO3BSyThHMmNUmuQy zkvg== X-Forwarded-Encrypted: i=1; AJvYcCUmX22JiImnPxtwzv7Gfrc+UkWNhZ67xGa83dAUKSg2ohUeXdmTQ+PeniOi/YvA/pf4k23XfAmtyQ==@kvack.org X-Gm-Message-State: AOJu0YwOcitSx0plJsQQpVEtCZSThwfp/TmzU9Pn6MV6vhAwqVDLdkq9 FdRfu70Uxh6rDIyVm3a7Pe5o0VIlnzs3KJ6smb4cxzzyhRuJH5aXxpo9u3lOpiqW3EosLtlf9vk eP5m5J9RFhA== X-Google-Smtp-Source: AGHT+IEaBJ4kVsH9fdlEpqyA1vEXALZVlXyRkre6Nr4itvNL2JHwYHaMyiaIiF1xr8efeWfazalHNzAFq1Ad7A== X-Received: from wrbfi1.prod.google.com ([2002:a05:6000:4401:b0:386:333e:ad16]) (user=jackmanb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:470d:b0:385:dedb:a156 with SMTP id ffacd0b85a97d-38a872cfdffmr10312148f8f.6.1736534461506; Fri, 10 Jan 2025 10:41:01 -0800 (PST) Date: Fri, 10 Jan 2025 18:40:33 +0000 In-Reply-To: <20250110-asi-rfc-v2-v2-0-8419288bc805@google.com> Mime-Version: 1.0 References: <20250110-asi-rfc-v2-v2-0-8419288bc805@google.com> X-Mailer: b4 0.15-dev Message-ID: <20250110-asi-rfc-v2-v2-7-8419288bc805@google.com> Subject: [PATCH RFC v2 07/29] mm: asi: Make __get_current_cr3_fast() ASI-aware From: Brendan Jackman To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Richard Henderson , Matt Turner , Vineet Gupta , Russell King , Catalin Marinas , Will Deacon , Guo Ren , Brian Cain , Huacai Chen , WANG Xuerui , Geert Uytterhoeven , Michal Simek , Thomas Bogendoerfer , Dinh Nguyen , Jonas Bonn , Stefan Kristiansson , Stafford Horne , "James E.J. Bottomley" , Helge Deller , Michael Ellerman , Nicholas Piggin , Christophe Leroy , Naveen N Rao , Madhavan Srinivasan , Paul Walmsley , Palmer Dabbelt , Albert Ou , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , Yoshinori Sato , Rich Felker , John Paul Adrian Glaubitz , "David S. Miller" , Andreas Larsson , Richard Weinberger , Anton Ivanov , Johannes Berg , Chris Zankel , Max Filippov , Arnd Bergmann , Andrew Morton , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider , Uladzislau Rezki , Christoph Hellwig , Masami Hiramatsu , Mathieu Desnoyers , Mike Rapoport , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Dennis Zhou , Tejun Heo , Christoph Lameter , Sean Christopherson , Paolo Bonzini , Ard Biesheuvel , Josh Poimboeuf , Pawan Gupta Cc: x86@kernel.org, linux-kernel@vger.kernel.org, linux-alpha@vger.kernel.org, linux-snps-arc@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-csky@vger.kernel.org, linux-hexagon@vger.kernel.org, loongarch@lists.linux.dev, linux-m68k@lists.linux-m68k.org, linux-mips@vger.kernel.org, linux-openrisc@vger.kernel.org, linux-parisc@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-sh@vger.kernel.org, sparclinux@vger.kernel.org, linux-um@lists.infradead.org, linux-arch@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, Brendan Jackman , Junaid Shahid X-Stat-Signature: 49o17hx3uod69nibncq1gc6w5tby8nqz X-Rspamd-Queue-Id: 5A96340003 X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1736534463-956048 X-HE-Meta: U2FsdGVkX1/9Scc07VYCNrz2TtybRhvAtd1sGXihfH0or/eIz7CwbHj0afnIwhFx3REQo8ux/qM/oH3HJdfxintoq9a9vrr3Zj8aeKznHRRkQm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xZEaWdmAkfgCOnBYcfDlM+rKXrKR2be1trgWSEvPeysnQkliiRDMqfOKA6Q3IbYAlBY33/apxgK289gABlLcJsYN6HbdNLPRWlyRuB/LdMJMuWJBRvzO7FrPOtmxFEQm+fTPYZ0omjeKq13rMd7J+lbaAWoVm15MVAhGfF7jynrPqi8Ym5n8aDD3FNrjcK+iWTLRCuTe/yUjA8Ms+ElMd+xljZXqpctMkVChfH5eYnvGhh7l2j7K6NNl7CvN7PDEg6oZU2pyPvteXWJygOefrO7jj5tOJjDnZyJn//nxRxKMMRtqbY6LYJqV1GRVPMIdgHX401VHsy2aeGQfQCf+a0vNjZdN5u9XEIwCXvM9UKE0y7om8PnFtbKUdnzWxbKhLeh5M6M58/aLGMFrLIk7t6o6IbtKdaKmMChpqFF7LkX07O3SYgfk9Prl7nP8vrnJvdt4ZQE= 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: From: Junaid Shahid When ASI is active, __get_current_cr3_fast() adjusts the returned CR3 value accordingly to reflect the actual ASI CR3. Signed-off-by: Junaid Shahid Signed-off-by: Brendan Jackman --- arch/x86/mm/tlb.c | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c index 2601beed83aef182d88800c09d70e4c5e95e7ed0..b2a13fdab0c6454c1d9d4e3338801f3402da4191 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include "mm_internal.h" @@ -197,8 +198,8 @@ static inline unsigned long build_cr3_noflush(pgd_t *pgd, u16 asid, return build_cr3(pgd, asid, lam) | CR3_NOFLUSH; } -noinstr unsigned long build_cr3_pcid_noinstr(pgd_t *pgd, u16 pcid, - unsigned long lam, bool noflush) +static __always_inline unsigned long build_cr3_pcid(pgd_t *pgd, u16 pcid, + unsigned long lam, bool noflush) { u64 noflush_bit = 0; @@ -210,6 +211,12 @@ noinstr unsigned long build_cr3_pcid_noinstr(pgd_t *pgd, u16 pcid, return __build_cr3(pgd, pcid, lam) | noflush_bit; } +noinstr unsigned long build_cr3_pcid_noinstr(pgd_t *pgd, u16 pcid, + unsigned long lam, bool noflush) +{ + return build_cr3_pcid(pgd, pcid, lam, noflush); +} + /* * We get here when we do something requiring a TLB invalidation * but could not go invalidate all of the contexts. We do the @@ -1133,14 +1140,32 @@ void flush_tlb_kernel_range(unsigned long start, unsigned long end) */ noinstr unsigned long __get_current_cr3_fast(void) { - unsigned long cr3 = - build_cr3(this_cpu_read(cpu_tlbstate.loaded_mm)->pgd, - this_cpu_read(cpu_tlbstate.loaded_mm_asid), - tlbstate_lam_cr3_mask()); + unsigned long cr3; + pgd_t *pgd; + u16 asid = this_cpu_read(cpu_tlbstate.loaded_mm_asid); + struct asi *asi = asi_get_current(); + u16 pcid; + + if (asi) { + pgd = asi_pgd(asi); + pcid = asi_pcid(asi, asid); + } else { + pgd = this_cpu_read(cpu_tlbstate.loaded_mm)->pgd; + pcid = kern_pcid(asid); + } + + cr3 = build_cr3_pcid(pgd, pcid, tlbstate_lam_cr3_mask(), false); /* For now, be very restrictive about when this can be called. */ VM_WARN_ON(in_nmi() || preemptible()); + /* + * Outside of the ASI critical section, an ASI-restricted CR3 is + * unstable because an interrupt (including an inner interrupt, if we're + * already in one) could cause a persistent asi_exit. + */ + VM_WARN_ON_ONCE(asi && asi_in_critical_section()); + VM_BUG_ON(cr3 != __read_cr3()); return cr3; }