[PATCH] KVM: x86/mmu: treat WC memory as MMIO

From: francisco_flynn
Date: Mon Mar 11 2024 - 06:49:06 EST


when doing kvm_tdp_mmu_map for WC memory, such as pages
allocated by amdgpu ttm driver for ttm_write_combined
caching mode(e.g. host coherent in vulkan),
the spte would be set to WB, in this case, vcpu write
to these pages would goes to cache first, and never
be write-combined and host-coherent anymore. so
WC memory should be treated as MMIO, and the effective
memory type is depending on guest PAT.

Signed-off-by: francisco_flynn <francisco_flynn@xxxxxxxxxxx>
---
arch/x86/include/asm/memtype.h | 2 ++
arch/x86/kvm/mmu/spte.c | 5 +++--
arch/x86/mm/pat/memtype.c | 8 ++++++++
3 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/arch/x86/include/asm/memtype.h b/arch/x86/include/asm/memtype.h
index 113b2fa51849..b49887d8580e 100644
--- a/arch/x86/include/asm/memtype.h
+++ b/arch/x86/include/asm/memtype.h
@@ -23,6 +23,8 @@ extern void memtype_free_io(resource_size_t start, resource_size_t end);

extern bool pat_pfn_immune_to_uc_mtrr(unsigned long pfn);

+extern bool pat_pfn_is_wc(unsigned long pfn);
+
bool x86_has_pat_wp(void);
enum page_cache_mode pgprot2cachemode(pgprot_t pgprot);

diff --git a/arch/x86/kvm/mmu/spte.c b/arch/x86/kvm/mmu/spte.c
index 4a599130e9c9..7842458dafa8 100644
--- a/arch/x86/kvm/mmu/spte.c
+++ b/arch/x86/kvm/mmu/spte.c
@@ -88,9 +88,10 @@ u64 make_mmio_spte(struct kvm_vcpu *vcpu, u64 gfn, unsigned int access)
static bool kvm_is_mmio_pfn(kvm_pfn_t pfn)
{
if (pfn_valid(pfn))
- return !is_zero_pfn(pfn) && PageReserved(pfn_to_page(pfn)) &&
+ return !is_zero_pfn(pfn) &&
+ (PageReserved(pfn_to_page(pfn)) || pat_pfn_is_wc(pfn)) &&
/*
- * Some reserved pages, such as those from NVDIMM
+ * Some reserved or WC pages, such as those from NVDIMM
* DAX devices, are not for MMIO, and can be mapped
* with cached memory type for better performance.
* However, the above check misconceives those pages
diff --git a/arch/x86/mm/pat/memtype.c b/arch/x86/mm/pat/memtype.c
index 0904d7e8e126..a5dd906cd987 100644
--- a/arch/x86/mm/pat/memtype.c
+++ b/arch/x86/mm/pat/memtype.c
@@ -716,6 +716,14 @@ bool pat_pfn_immune_to_uc_mtrr(unsigned long pfn)
}
EXPORT_SYMBOL_GPL(pat_pfn_immune_to_uc_mtrr);

+bool pat_pfn_is_wc(unsigned long pfn)
+{
+ enum page_cache_mode cm = lookup_memtype(PFN_PHYS(pfn));
+
+ return cm == _PAGE_CACHE_MODE_WC;
+}
+EXPORT_SYMBOL_GPL(pat_pfn_is_wc);
+
/**
* memtype_reserve_io - Request a memory type mapping for a region of memory
* @start: start (physical address) of the region
--
2.25.1