Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3385753pxb; Mon, 17 Jan 2022 19:16:45 -0800 (PST) X-Google-Smtp-Source: ABdhPJxQpdHlJgMen6gYcgo4H86IZqubcGdq8x1t3Xs6F5kzTeD4nDiKiMcSQ4Lh3VZDECiiddC/ X-Received: by 2002:a63:87c8:: with SMTP id i191mr14937814pge.465.1642475805359; Mon, 17 Jan 2022 19:16:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642475805; cv=none; d=google.com; s=arc-20160816; b=NfonbDf+1wzSUmaLWWDcMXa3vO6hy1B8y8Y44XTMfQOi8ECZgurdRPK/1FXSD2pABC ZZZNrfd4h+mCK4qo9h/DgFCXMLJaFfV0Q/INWLYn7TWxvm0Vk9tEaeu7urenlnb49Gp3 pH7o2toE8kpm7VV+9XondqPPNkVXVwOOzuqHrWQS4/LjBEM4i+Lqs3XaEs/d+vtW2ECX jGGvsy5XMZPEBAYaEBoGECPb+jTACsfdBuQgZlq2qtENYb+BLLHs7jFwcuEoIpJL9jOi DvsAg5RXiH3RYXa+OyjY+V/vmZMxfD6JGXXsXabhOYpLZ0S0W820pjbQJQdT5+ZSOOd5 9Jfw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=ZGKjW0SlT4JaYNDVZEvM9zXNJaXJzo450vQ/ILiuX5U=; b=wt7zBS7ZkKodXmBwSyXVuSyjME8pLnpaMgF9gSc/ZX6JzNmVL6aQ2wm6s1/fpJh1hr LzS8ULY1igPCtc1HX8/IXXo7kjz5CIUnEOyNMYEsxVpAmTPTs8LIrP7bqxVw8M02taeE 29+e4Uq7AkYq8NC+R/SQNGlWKj0JS2Nfwz5YyM3KwbtoAcYIoO4s9jFVcvapZXWw+dV9 GatYVX8bbyCSmTqbN6UhzPxyS4NK0NhyrlRgafFOrruIQjiY6sN24j9mwRzqQIgCEGm5 eJYJ8dah2ztxHA3Ox1XF8+8qUV66mGkHt3g7Psh/E5urtxk1SCuc8rb6So0IVkHvt4V3 n2cg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=YgMzVon5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t13si16686477pgu.296.2022.01.17.19.16.33; Mon, 17 Jan 2022 19:16:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=YgMzVon5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245630AbiARC0s (ORCPT + 99 others); Mon, 17 Jan 2022 21:26:48 -0500 Received: from ams.source.kernel.org ([145.40.68.75]:38684 "EHLO ams.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245545AbiARCYf (ORCPT ); Mon, 17 Jan 2022 21:24:35 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 9AAC3B8124E; Tue, 18 Jan 2022 02:24:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BE33CC36AE3; Tue, 18 Jan 2022 02:24:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1642472673; bh=8viDxICTwBj3X3U5m2jJ0FeAwvjTGlqTSlf2o/uWmFo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YgMzVon5S4Cyy3WWCvCy+8IVnSKcfEn8Ch49qhnT85Ma9CFOajiCyHV32JLGv6GBu PkoqHmTk3n9i7hVREa3S2/5E5eVvFyXWT6e0Jj10vMbNma1iI6CwKpiiQ2RwuNukcx ktUGPSAvos8rGiryxxJTTd/sgfXN5cgqJIWBD5gQqkmdBMPiFNJe+lTw8Fqbc3eWrB eeqkUFHxVzesT9Wmr4GnQkBrgWz61rQAYtmJFxu/5pQi5DQrC7KLq1NvW1Cmotzog4 4O19pLIN8k6MgfToIRIwRAPSn5klcnbbf1t/IveDMMzDP7oPRlB6PFwlp8vDKxvH5u w/2/czAQTj9Fg== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Joerg Roedel , Borislav Petkov , Sasha Levin , tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, seanjc@google.com, hpa@zytor.com, paul.gortmaker@windriver.com, rppt@kernel.org, thomas.lendacky@amd.com, mick@ics.forth.gr, brijesh.singh@amd.com Subject: [PATCH AUTOSEL 5.16 094/217] x86/mm: Flush global TLB when switching to trampoline page-table Date: Mon, 17 Jan 2022 21:17:37 -0500 Message-Id: <20220118021940.1942199-94-sashal@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220118021940.1942199-1-sashal@kernel.org> References: <20220118021940.1942199-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Joerg Roedel [ Upstream commit 71d5049b053876afbde6c3273250b76935494ab2 ] Move the switching code into a function so that it can be re-used and add a global TLB flush. This makes sure that usage of memory which is not mapped in the trampoline page-table is reliably caught. Also move the clearing of CR4.PCIDE before the CR3 switch because the cr4_clear_bits() function will access data not mapped into the trampoline page-table. Signed-off-by: Joerg Roedel Signed-off-by: Borislav Petkov Link: https://lore.kernel.org/r/20211202153226.22946-4-joro@8bytes.org Signed-off-by: Sasha Levin --- arch/x86/include/asm/realmode.h | 1 + arch/x86/kernel/reboot.c | 12 ++---------- arch/x86/realmode/init.c | 26 ++++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/arch/x86/include/asm/realmode.h b/arch/x86/include/asm/realmode.h index 5db5d083c8732..331474b150f16 100644 --- a/arch/x86/include/asm/realmode.h +++ b/arch/x86/include/asm/realmode.h @@ -89,6 +89,7 @@ static inline void set_real_mode_mem(phys_addr_t mem) } void reserve_real_mode(void); +void load_trampoline_pgtable(void); #endif /* __ASSEMBLY__ */ diff --git a/arch/x86/kernel/reboot.c b/arch/x86/kernel/reboot.c index 0a40df66a40de..fa700b46588e0 100644 --- a/arch/x86/kernel/reboot.c +++ b/arch/x86/kernel/reboot.c @@ -113,17 +113,9 @@ void __noreturn machine_real_restart(unsigned int type) spin_unlock(&rtc_lock); /* - * Switch back to the initial page table. + * Switch to the trampoline page table. */ -#ifdef CONFIG_X86_32 - load_cr3(initial_page_table); -#else - write_cr3(real_mode_header->trampoline_pgd); - - /* Exiting long mode will fail if CR4.PCIDE is set. */ - if (boot_cpu_has(X86_FEATURE_PCID)) - cr4_clear_bits(X86_CR4_PCIDE); -#endif + load_trampoline_pgtable(); /* Jump to the identity-mapped low memory code */ #ifdef CONFIG_X86_32 diff --git a/arch/x86/realmode/init.c b/arch/x86/realmode/init.c index 38d24d2ab38b3..c5e29db02a469 100644 --- a/arch/x86/realmode/init.c +++ b/arch/x86/realmode/init.c @@ -17,6 +17,32 @@ u32 *trampoline_cr4_features; /* Hold the pgd entry used on booting additional CPUs */ pgd_t trampoline_pgd_entry; +void load_trampoline_pgtable(void) +{ +#ifdef CONFIG_X86_32 + load_cr3(initial_page_table); +#else + /* + * This function is called before exiting to real-mode and that will + * fail with CR4.PCIDE still set. + */ + if (boot_cpu_has(X86_FEATURE_PCID)) + cr4_clear_bits(X86_CR4_PCIDE); + + write_cr3(real_mode_header->trampoline_pgd); +#endif + + /* + * The CR3 write above will not flush global TLB entries. + * Stale, global entries from previous page tables may still be + * present. Flush those stale entries. + * + * This ensures that memory accessed while running with + * trampoline_pgd is *actually* mapped into trampoline_pgd. + */ + __flush_tlb_all(); +} + void __init reserve_real_mode(void) { phys_addr_t mem; -- 2.34.1