Skip to contentSkip to conversation

x86/pat: fix pat_x_mtrr_type() for MTRR disabled case

PATCH Applied Juergen Gross 3 participants 3 messages Jan 10, 2023

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

PATCH 1/1 x86/pat: fix pat_x_mtrr_type() for MTRR disabled case

Since commit 72cbc8f04fe2 ("x86/PAT: Have pat_enabled() properly reflect state when running on Xen") PAT can be enabled without MTRR.

This has resulted in problems e.g. for a SEV-SNP guest running under Hyper-V, when trying to establish a new mapping via memremap() with WB caching mode, as pat_x_mtrr_type() will call mtrr_type_lookup(), which in turn is returning MTRR_TYPE_INVALID due to MTRR being disabled in this configuration. The result is a mapping with UC- caching, leading to severe performance degradation.

Fix that by handling MTRR_TYPE_INVALID the same way as MTRR_TYPE_WRBACK in pat_x_mtrr_type().

Fixes 72cbc8f04fe2 ("x86/PAT: Have pat_enabled() properly reflect state when running on Xen")
---
 arch/x86/mm/pat/memtype.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/mm/pat/memtype.c +2 -1
index 46de9cf5c91d..fb4b1b5e0dea 100644
@@ -387,7 +387,8 @@ static unsigned long pat_x_mtrr_type(u64 start, u64 end,
 		u8 mtrr_type, uniform;
 
 		mtrr_type = mtrr_type_lookup(start, end, &uniform);
-		if (mtrr_type != MTRR_TYPE_WRBACK)
+		if (mtrr_type != MTRR_TYPE_WRBACK &&
+		    mtrr_type != MTRR_TYPE_INVALID)
 			return _PAGE_CACHE_MODE_UC_MINUS;
 
 		return _PAGE_CACHE_MODE_WB;
Signature · 1 line
2.35.3
Reviewed-by Tested-by on v1 1/1
#1 lore raw

From: Juergen Gross <[email protected]> Sent: Monday, January 9, 2023 10:54 PM

Quoted 36 lines

Since commit 72cbc8f04fe2 ("x86/PAT: Have pat_enabled() properly reflect state when running on Xen") PAT can be enabled without MTRR.

This has resulted in problems e.g. for a SEV-SNP guest running under Hyper-V, when trying to establish a new mapping via memremap() with WB caching mode, as pat_x_mtrr_type() will call mtrr_type_lookup(), which in turn is returning MTRR_TYPE_INVALID due to MTRR being disabled in this configuration. The result is a mapping with UC- caching, leading to severe performance degradation.

Fix that by handling MTRR_TYPE_INVALID the same way as MTRR_TYPE_WRBACK in pat_x_mtrr_type().

Fixes 72cbc8f04fe2 ("x86/PAT: Have pat_enabled() properly reflect state when running on Xen")
---
 arch/x86/mm/pat/memtype.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/mm/pat/memtype.c +2 -2
index 46de9cf5c91d..fb4b1b5e0dea 100644
@@ -387,7 +387,8 @@ static unsigned long pat_x_mtrr_type(u64 start, u64 end,
 		u8 mtrr_type, uniform;
 
 		mtrr_type = mtrr_type_lookup(start, end, &uniform);
-		if (mtrr_type != MTRR_TYPE_WRBACK)
+		if (mtrr_type != MTRR_TYPE_WRBACK &&
+		    mtrr_type != MTRR_TYPE_INVALID)
 			return _PAGE_CACHE_MODE_UC_MINUS;
 
 		return _PAGE_CACHE_MODE_WB;
--
Quoted 1 line

2.35.3

This looks good to me. I've tested my specific use case of a SEV-SNP guest running under Hyper-V, where the MTRRs are not visible in the guest but the PAT is functional. Previously, this use case showed many "uncached-minus" entries in /sys/kernel/debug/x86/pat_memtype_list even though the mapping requests were made as WB. With this patch, those entries show "write-back", as expected.

Thanks!

merge bot Applied
#2 lore raw

[tip: x86/urgent] x86/pat: Fix pat_x_mtrr_type() for MTRR disabled case

In reply to Juergen Gross
The following commit has been merged into the x86/urgent branch of tip:

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

Commit-ID:     90b926e68f500844dff16b5bcea178dc55cf580a
Gitweb:        https://git.kernel.org/tip/90b926e68f500844dff16b5bcea178dc55cf580a
Author:        Juergen Gross <[email protected]>
AuthorDate:    Tue, 10 Jan 2023 07:54:27 +01:00
Committer:     Borislav Petkov (AMD) <[email protected]>
CommitterDate: Tue, 10 Jan 2023 17:21:53 +01:00

x86/pat: Fix pat_x_mtrr_type() for MTRR disabled case

Since

72cbc8f04fe2 ("x86/PAT: Have pat_enabled() properly reflect state when running on Xen")

PAT can be enabled without MTRR.

This has resulted in problems e.g. for a SEV-SNP guest running under Hyper-V, when trying to establish a new mapping via memremap() with WB caching mode, as pat_x_mtrr_type() will call mtrr_type_lookup(), which in turn is returning MTRR_TYPE_INVALID due to MTRR being disabled in this configuration.

The result is a mapping with UC- caching, leading to severe performance degradation.

Fix that by handling MTRR_TYPE_INVALID the same way as MTRR_TYPE_WRBACK in pat_x_mtrr_type() because MTRR_TYPE_INVALID means MTRRs are disabled.

[ bp: Massage commit message. ]

---
 arch/x86/mm/pat/memtype.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/mm/pat/memtype.c +2 -1
index 46de9cf..fb4b1b5 100644
@@ -387,7 +387,8 @@ static unsigned long pat_x_mtrr_type(u64 start, u64 end,
 		u8 mtrr_type, uniform;
 
 		mtrr_type = mtrr_type_lookup(start, end, &uniform);
-		if (mtrr_type != MTRR_TYPE_WRBACK)
+		if (mtrr_type != MTRR_TYPE_WRBACK &&
+		    mtrr_type != MTRR_TYPE_INVALID)
 			return _PAGE_CACHE_MODE_UC_MINUS;
 
 		return _PAGE_CACHE_MODE_WB;