Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3595866pxb; Mon, 24 Jan 2022 13:07:11 -0800 (PST) X-Google-Smtp-Source: ABdhPJze3XZ60fQ2KV9IVcfDalj6CaU5TIG7OS5Lauw2cYFMt20ZkRB2Bg6hL/uSro4/sVWTc2Cq X-Received: by 2002:a62:14d5:0:b0:4c7:43cb:863 with SMTP id 204-20020a6214d5000000b004c743cb0863mr15431971pfu.23.1643058431367; Mon, 24 Jan 2022 13:07:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643058431; cv=none; d=google.com; s=arc-20160816; b=rxVN0Yq7JDyOBce6sM7ehedY6XqG30JKf+ovxQN9X1C3D/t40ndLlIdRRdxwcMni0v LoEhsLFMWjwRexcMXiUoy54oL4WKQYdMlSf7Uv+eXDEzIf1tIA/3252KQEL/1B2F82ED uy28pMTVz5C/JjbI5eL9fdRqt08vjhQHiHu+gdlCBBhjbKagaQYyvJeesDooBeomzEsd MU7kXOzBec8w5s4tqhRGnMGT3O5BgmOCJcggckEqu3hFTtGAuMhVrtZMnHyVvIpCRfoC G1gHqiqqA11yKeq5Bax9VHNx3G5XXV/M68yZjpsepn7K4A5YaRshXMrqKlm8pJ4qwKMv FfrQ== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=NR0yRJrWAppsuBv3wtt3gYpcfcPkp5xE1VpexicjzXw=; b=F+z7GxzFECce9cZok7CVgStlA3gWdl2kXPI3gbeKglH7hGa+CpXH7K4A/La+idySzA C1K/aZjYRnHJf7+jwdZyXcwyGLCqqr57HHGDYfJjr/be3+iDLmfNAJaxQ0LNxcygk8wX rB2mBfpkNZhLNUW9kdBPhYQXQgGnOcKEzVwTfWxXm+8qnGgMNvtXqYyHoLJHdaRpvRLm z2vnmO44FkgrftIqtxPRpTvH+v7LbAqDE52OR++MLGWoRI+W8NSib0i8rTba+H+yfL2M 5hR2wp/tqA8Fzw3xJMkz5VclsVLvHZ0a9Kjrmi5knZP0C5vgxPtoOQXBhEUucUYQsKPz +1rQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=iB5PKUAy; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o6si353408pjt.142.2022.01.24.13.06.57; Mon, 24 Jan 2022 13:07:11 -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=@linuxfoundation.org header.s=korg header.b=iB5PKUAy; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1385307AbiAXUcA (ORCPT + 99 others); Mon, 24 Jan 2022 15:32:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33860 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355388AbiAXUNh (ORCPT ); Mon, 24 Jan 2022 15:13:37 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 41619C06175A; Mon, 24 Jan 2022 11:34:54 -0800 (PST) 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 F3945B811F9; Mon, 24 Jan 2022 19:34:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 58496C340E5; Mon, 24 Jan 2022 19:34:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643052891; bh=QAoycasiljDcdSm6faYBKX2CXMKw6Rx++/AvlQpD6PI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iB5PKUAy98OwqndIR+5L3aq5aQjwOe6O3XW1IRp+6rcXSU7LunMdjgfFetk117KRy yvuzmij82p3aAPvKASeiy+ZiIAKiAFpTnHRQfyxLC0pBJIJtAbnxuIBofWENHKiMZ3 nQ8D5D6DgKMjHYb8Vk6b61yY4JZLUf60DIL+m9aY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Joerg Roedel , Borislav Petkov , Sasha Levin Subject: [PATCH 5.4 175/320] x86/mm: Flush global TLB when switching to trampoline page-table Date: Mon, 24 Jan 2022 19:42:39 +0100 Message-Id: <20220124183959.610833735@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124183953.750177707@linuxfoundation.org> References: <20220124183953.750177707@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 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 09ecc32f65248..52d7512ea91ab 100644 --- a/arch/x86/include/asm/realmode.h +++ b/arch/x86/include/asm/realmode.h @@ -82,6 +82,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 d65d1afb27161..fdef27a84d713 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 de371e52cfa85..fac50ebb122b5 100644 --- a/arch/x86/realmode/init.c +++ b/arch/x86/realmode/init.c @@ -16,6 +16,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