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: 13935570 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C2C72E7719C for ; Fri, 10 Jan 2025 23:19:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=uOzmdj1eujIQ55wchVUOYMEBS42M6vi2gbrkLZyA9HI=; b=Kxl6vNuDpaR10IA4AgnZDrgxbQ vN7aMVZZq5SPAIB5l1G4qBvj4curHa2RrGpbjkw21KtAecoJVWsW0/EZfQiRLZvLVAmB9QeklLbsv X1vFJp3Wm7sU8w8qcE1gISta9HwVLiKKNBKp+PKgH8z4gV6svZXrtjkqaLQYfNGtj1ILylLE4Ayqo TRtg5maN6CRrTR8T9kNQsXlW2OD2wviPrm+yS6qMyDqZvH6gdJ/7OG5/J/tvMzXRNL6Ju7VflbMPk hGwLgxpq8gX/SwimpdqarjVp2k0sJk/6xdxAgVQAuBq2ToBeniLtP3zrBcpuJ1cN6ANzQbdWu6WqL JpJMUlmg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tWOIB-0000000HEV2-0NjY; Fri, 10 Jan 2025 23:19:51 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tWJwU-0000000GbPp-1ZNb for linux-riscv@bombadil.infradead.org; Fri, 10 Jan 2025 18:41:11 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Type:Cc:To:From:Subject: Message-ID:References:Mime-Version:In-Reply-To:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=/j+RB8/lAgzqGTtPca+XTmsHyqC3ld9qGwXYbAsCbQ0=; b=Uzg/paeyiFHSouf/QKk8XwHwF3 bbgdFYcmr8cLEd1s5VeVef1PCL0fT5ERwSi0PmWMVo2bv1ZAM4c9ObzAL5qJg6Ss1BP2F7mz7szTT PN9JAN3kWYmthyYzC3gY4M6EeduVEEvcyuZz93jz0IV9yiNsQYFIUo6Dr8Df9A5n0/aY8welrwAWw JYPDkwZjT9/OL6xy8JnwUSsTHOaD3ph7AB5RlaUTZtqGJJ6lYibkgEPYCcb0yFWnT4q4itctRn0LX k7dFruk7mmUGhMXSoQA0kwAoZH/kBfwF3zv9p7nVjdiGimBtVN1VnDvQNVGJrJXJbb07rc6EHxJde NHIGUTWw==; Received: from mail-wr1-x449.google.com ([2a00:1450:4864:20::449]) by desiato.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tWJwQ-00000009sNa-2Rmv for linux-riscv@lists.infradead.org; Fri, 10 Jan 2025 18:41:09 +0000 Received: by mail-wr1-x449.google.com with SMTP id ffacd0b85a97d-38a9118c486so1465376f8f.1 for ; Fri, 10 Jan 2025 10:41:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736534462; x=1737139262; darn=lists.infradead.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=bs/6TkwdSdvhkOXOrFUPjxi46IU/OI1oRkXNPg5zhsh4RwOQBAYfN1/6hCyNhDiB72 xbDjKd2XyxJiVAGoUMRGVuanB1PwvRFmySlOetd4eMV2z7R5+Vou0UkGMTJsReGLheWr zSbV7pVqGm2OCQiXiehTrDBiYYKYmsIJoqV2XhpCuSLxo1c42k8ivisdy4ft0zqFAuZP auE3Fih8B9ryEqNnmETEvIXZL1ARumah2DJiiFAOSWFwri39iL/1FrQEjJuNaAf22E/Z Ahkjf3TfFXBW9Zea/kE7ioS1R3MpeuNrsZhERRohkEMUcXS4+9CbGlJ9R/JgpfqQPpYk 1bPg== 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=GeOo351oMfoOWmXyFOi6e4TDhlk+W68FoLl/pn3agfVvSUJiQyPLLLq0bC2C3eDa3a Aro3L1LphEsB+9MP8BzjfGmMmA6/TKSeOadIYhgTGE4fcRJD2/LIF0wGKdVNDRMcCNXV 8K/i7ln44f84vlISMBJV/lfpssq0y759qp9UEj9bq3FfkCTUlomqfqVnDbpw/nshd5/L GlZ7WlBJGGjnIr54AtDcgSU2jbJBVIgwNMxlPYcSBgOosFLNSAfvwKXBvTvAx6nL7E+k LcqYZaF8ju9JbPwd8aGQ5ITbeEzZuYg+SOAGGzwinzgrIpSKPLriUz1tfICGUDOBcQnb VNfg== X-Forwarded-Encrypted: i=1; AJvYcCW5Z6SAh2Tt1tTXJ7yQx1779ei4YTonDY5PnfKJMblADGG40ssA4aN5X2KlYF+R6ItWkfzBuZtwrTluJw==@lists.infradead.org X-Gm-Message-State: AOJu0YyRa7IW7/ChOtxoqbqR3AMz9I+S59XgWopRyOeWSBQ3N54zFThs Q9vM7xqeBnsqd2Uu4pK/yAxcoO7kp51wRMhvCO6z0SqauvcndFQn/u6YxZzqRFERQMtmjAtiho5 MyestJTAYrQ== 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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250110_184106_758766_2A20B521 X-CRM114-Status: GOOD ( 19.24 ) X-Mailman-Approved-At: Fri, 10 Jan 2025 15:19:44 -0800 X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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; }