Skip to contentSkip to conversation

x86: fixes for MTRR/PAT decoupling

PATCH Applied Juergen Gross 3 participants 7 messages Dec 5, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover x86: fixes for MTRR/PAT decoupling

Two small fixes for the decoupling of MTRR and PAT.

Juergen Gross (2):
x86/pat: fix TDX guest PAT initialization x86/mtrr: make message for disabled MTRRs more descriptive

 arch/x86/kernel/cpu/mtrr/mtrr.c | 4 +++-
 arch/x86/mm/pat/memtype.c       | 6 +++++-
 2 files changed, 8 insertions(+), 2 deletions(-)
Signature · 1 line
2.35.3
#1 lore raw

PATCH 1/2 x86/pat: fix TDX guest PAT initialization

With the decoupling of PAT and MTRR initialization, PAT will be used even with MTRRs disabled. This seems to break booting up as TDX guest, as the recommended sequence to set the PAT MSR across CPUs can't work in TDX guests due to disabling caches via setting CR0.CD isn't allowed in TDX mode.

This is an inconsistency in the Intel documentation between the SDM and the TDX specification. For now handle TDX mode the same way as Xen PV guest mode by just accepting the current PAT MSR setting without trying to modify it.

---
 arch/x86/mm/pat/memtype.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
arch/x86/mm/pat/memtype.c +5 -1
index 9aab17d660cd..4e50add760ad 100644
@@ -296,8 +296,12 @@ void __init pat_bp_init(void)
 	/*
 	 * Xen PV doesn't allow to set PAT MSR, but all cache modes are
 	 * supported.
+	 * When running as TDX guest setting the PAT MSR won't work either
+	 * due to the requirement to set CR0.CD when doing so. Rely on
+	 * firmware to have set the PAT MSR correctly.
 	 */
-	if (pat_disabled || cpu_feature_enabled(X86_FEATURE_XENPV)) {
+	if (pat_disabled || cpu_feature_enabled(X86_FEATURE_XENPV) ||
+	    cpu_feature_enabled(X86_FEATURE_TDX_GUEST)) {
 		init_cache_modes(pat_msr_val);
 		return;
 	}
Signature · 1 line
2.35.3
merge bot Applied
#2 lore raw

[tip: x86/cpu] x86/pat: Handle TDX guest PAT initialization

The following commit has been merged into the x86/cpu branch of tip:

The following commit has been merged into the x86/cpu branch of tip:

Commit-ID:     c11ca45441d56ed6d353548cad660e04f0b6605d
Gitweb:        https://git.kernel.org/tip/c11ca45441d56ed6d353548cad660e04f0b6605d
Author:        Juergen Gross <[email protected]>
AuthorDate:    Mon, 05 Dec 2022 09:04:32 +01:00
Committer:     Borislav Petkov (AMD) <[email protected]>
CommitterDate: Mon, 05 Dec 2022 11:03:27 +01:00

x86/pat: Handle TDX guest PAT initialization

With the decoupling of PAT and MTRR initialization, PAT will be used even with MTRRs disabled. This seems to break booting up as TDX guest, as the recommended sequence to set the PAT MSR across CPUs can't work in TDX guests due to disabling caches via setting CR0.CD isn't allowed in TDX mode.

This is an inconsistency in the Intel documentation between the SDM and the TDX specification. For now handle TDX mode the same way as Xen PV guest mode by just accepting the current PAT MSR setting without trying to modify it.

[ bp: Align conditions for better readability. ]

---
 arch/x86/mm/pat/memtype.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/x86/mm/pat/memtype.c +6 -1
index 9aab17d..46de9cf 100644
@@ -296,8 +296,13 @@ void __init pat_bp_init(void)
 	/*
 	 * Xen PV doesn't allow to set PAT MSR, but all cache modes are
 	 * supported.
+	 * When running as TDX guest setting the PAT MSR won't work either
+	 * due to the requirement to set CR0.CD when doing so. Rely on
+	 * firmware to have set the PAT MSR correctly.
 	 */
-	if (pat_disabled || cpu_feature_enabled(X86_FEATURE_XENPV)) {
+	if (pat_disabled ||
+	    cpu_feature_enabled(X86_FEATURE_XENPV) ||
+	    cpu_feature_enabled(X86_FEATURE_TDX_GUEST)) {
 		init_cache_modes(pat_msr_val);
 		return;
 	}
Acked-by Kirill A. Shutemov on v1 1/2
#3 lore raw

Re: [PATCH 1/2] x86/pat: fix TDX guest PAT initialization

In reply to Juergen Gross
Quoted 6 lines · Juergen Gross

With the decoupling of PAT and MTRR initialization, PAT will be used even with MTRRs disabled. This seems to break booting up as TDX guest, as the recommended sequence to set the PAT MSR across CPUs can't work in TDX guests due to disabling caches via setting CR0.CD isn't allowed in TDX mode.

Quoted 6 lines

This is an inconsistency in the Intel documentation between the SDM and the TDX specification. For now handle TDX mode the same way as Xen PV guest mode by just accepting the current PAT MSR setting without trying to modify it.

Good enough for now. I will follow up if something comes up from the discussion around the topic.

Quoted 9 lines
---
 arch/x86/mm/pat/memtype.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
arch/x86/mm/pat/memtype.c +0 -0
index 9aab17d660cd..4e50add760ad 100644

arch/x86/mm/pat/memtype.c void __init pat_bp_init(void) · line 301

Quoted 6 lines
arch/x86/mm/pat/memtype.c +3 -0
@@ -296,8 +296,12 @@ void __init pat_bp_init(void)
 	/*
 	 * Xen PV doesn't allow to set PAT MSR, but all cache modes are
 	 * supported.
+	 * When running as TDX guest setting the PAT MSR won't work either
+	 * due to the requirement to set CR0.CD when doing so. Rely on
+	 * firmware to have set the PAT MSR correctly.

s/firmware/TDX module/

Quoted 7 lines
arch/x86/mm/pat/memtype.c +2 -1
 	 */
-	if (pat_disabled || cpu_feature_enabled(X86_FEATURE_XENPV)) {
+	if (pat_disabled || cpu_feature_enabled(X86_FEATURE_XENPV) ||
+	    cpu_feature_enabled(X86_FEATURE_TDX_GUEST)) {
 		init_cache_modes(pat_msr_val);
 		return;
 	}
Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
#4 lore raw

PATCH 2/2 x86/mtrr: make message for disabled MTRRs more descriptive

In reply to Juergen Gross

Instead of just saying "Disabled" when MTRRs are disabled for any reason, tell what is disabled and why.

---
 arch/x86/kernel/cpu/mtrr/mtrr.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/mtrr/mtrr.c +3 -1
index 6432abccbf56..94d5739758ba 100644
@@ -630,6 +630,7 @@ int __initdata changed_by_mtrr_cleanup;
 void __init mtrr_bp_init(void)
 {
 	u32 phys_addr;
+	const char *why = "(not available)";
 
 	phys_addr = 32;
 
@@ -705,12 +706,13 @@ void __init mtrr_bp_init(void)
 				changed_by_mtrr_cleanup = mtrr_cleanup(phys_addr);
 			} else {
 				mtrr_if = NULL;
+				why = "by BIOS";
 			}
 		}
 	}
 
 	if (!mtrr_enabled())
-		pr_info("Disabled\n");
+		pr_info("MTRRs disabled %s\n", why);
 }
 
 /**
Signature · 1 line
2.35.3
merge bot Applied
#5 lore raw

[tip: x86/cpu] x86/mtrr: Make message for disabled MTRRs more descriptive

The following commit has been merged into the x86/cpu branch of tip:

The following commit has been merged into the x86/cpu branch of tip:

Commit-ID:     7882b69eb6cdf6288a89d831d95c8547400a8b4d
Gitweb:        https://git.kernel.org/tip/7882b69eb6cdf6288a89d831d95c8547400a8b4d
Author:        Juergen Gross <[email protected]>
AuthorDate:    Mon, 05 Dec 2022 09:04:33 +01:00
Committer:     Borislav Petkov (AMD) <[email protected]>
CommitterDate: Mon, 05 Dec 2022 11:08:25 +01:00

x86/mtrr: Make message for disabled MTRRs more descriptive

Instead of just saying "Disabled" when MTRRs are disabled for any reason, tell what is disabled and why.

---
 arch/x86/kernel/cpu/mtrr/mtrr.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/mtrr/mtrr.c +3 -1
index 6432abc..783f321 100644
@@ -629,6 +629,7 @@ int __initdata changed_by_mtrr_cleanup;
  */
 void __init mtrr_bp_init(void)
 {
+	const char *why = "(not available)";
 	u32 phys_addr;
 
 	phys_addr = 32;
@@ -705,12 +706,13 @@ void __init mtrr_bp_init(void)
 				changed_by_mtrr_cleanup = mtrr_cleanup(phys_addr);
 			} else {
 				mtrr_if = NULL;
+				why = "by BIOS";
 			}
 		}
 	}
 
 	if (!mtrr_enabled())
-		pr_info("Disabled\n");
+		pr_info("MTRRs disabled %s\n", why);
 }
 
 /**