From: francisco_flynn <francisco_flynn@foxmail.com>
To: dave.hansen@linux.intel.com, luto@kernel.org,
peterz@infradead.org, tglx@linutronix.de, mingo@redhat.com,
bp@alien8.de, x86@kernel.org
Cc: hpa@zytor.com, rdunlap@infradead.org,
francisco_flynn@foxmail.com, akpm@linux-foundation.org,
bhelgaas@google.com, mawupeng1@huawei.com,
linux-kernel@vger.kernel.org
Subject: [PATCH] KVM: x86/mmu: treat WC memory as MMIO
Date: Mon, 11 Mar 2024 18:47:49 +0800 [thread overview]
Message-ID: <tencent_4B50D08D2E6211E4F9B867F0531F2C05BA0A@qq.com> (raw)
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@foxmail.com>
---
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
next reply other threads:[~2024-03-11 10:48 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-11 10:47 francisco_flynn [this message]
2024-03-11 16:20 ` Sean Christopherson
2024-03-12 8:42 ` francisco flynn
2024-03-12 15:21 ` Sean Christopherson
2024-03-13 2:01 ` francisco flynn
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=tencent_4B50D08D2E6211E4F9B867F0531F2C05BA0A@qq.com \
--to=francisco_flynn@foxmail.com \
--cc=akpm@linux-foundation.org \
--cc=bhelgaas@google.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=hpa@zytor.com \
--cc=linux-kernel@vger.kernel.org \
--cc=luto@kernel.org \
--cc=mawupeng1@huawei.com \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=rdunlap@infradead.org \
--cc=tglx@linutronix.de \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®