[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20240507062009.20336-1-yan.y.zhao@intel.com>
Date: Tue, 7 May 2024 14:20:09 +0800
From: Yan Zhao <yan.y.zhao@...el.com>
To: kvm@...r.kernel.org,
linux-kernel@...r.kernel.org,
x86@...nel.org,
alex.williamson@...hat.com,
jgg@...dia.com,
kevin.tian@...el.com
Cc: iommu@...ts.linux.dev,
pbonzini@...hat.com,
seanjc@...gle.com,
dave.hansen@...ux.intel.com,
luto@...nel.org,
peterz@...radead.org,
tglx@...utronix.de,
mingo@...hat.com,
bp@...en8.de,
hpa@...or.com,
corbet@....net,
joro@...tes.org,
will@...nel.org,
robin.murphy@....com,
baolu.lu@...ux.intel.com,
yi.l.liu@...el.com,
Yan Zhao <yan.y.zhao@...el.com>
Subject: [PATCH 2/5] KVM: x86/mmu: Fine-grained check of whether a invalid & RAM PFN is MMIO
Fine-grained check to decide whether a PFN, which is !pfn_valid() and
identified within the raw e820 table as RAM, should be treated as MMIO by
KVM in order to prevent guest cachable access.
Previously, a PFN that is !pfn_valid() and identified within the raw e820
table as RAM was not considered as MMIO. This is for the scenerio when
"mem=" was passed to the kernel, resulting in certain valid pages lacking
an associated struct page. See commit 0c55671f84ff ("kvm, x86: Properly
check whether a pfn is an MMIO or not").
However, that approach is only based on guest performance perspective
and may cause cacheable access to potential MMIO PFNs if
pat_pfn_immune_to_uc_mtrr() identifies the PFN as having a PAT type of
UC/WC/UC-. Therefore, do a fine-graned check for PAT in primary MMU so that
KVM would map the PFN as UC in EPT to prevent cachable access from guest.
For the rare case when PAT is not enabled, default the PFN to MMIO to avoid
further checking MTRR (since functions for MTRR related checking are not
exported now).
Cc: Kevin Tian <kevin.tian@...el.com>
Signed-off-by: Yan Zhao <yan.y.zhao@...el.com>
---
arch/x86/kvm/mmu/spte.c | 14 +++++++++++++-
1 file changed, 13 insertions(+), 1 deletion(-)
diff --git a/arch/x86/kvm/mmu/spte.c b/arch/x86/kvm/mmu/spte.c
index 4a599130e9c9..5db0fb7b74f5 100644
--- a/arch/x86/kvm/mmu/spte.c
+++ b/arch/x86/kvm/mmu/spte.c
@@ -101,9 +101,21 @@ static bool kvm_is_mmio_pfn(kvm_pfn_t pfn)
*/
(!pat_enabled() || pat_pfn_immune_to_uc_mtrr(pfn));
+ /*
+ * If the PFN is invalid and not RAM in raw e820 table, keep treating it
+ * as MMIO.
+ *
+ * If the PFN is invalid and is RAM in raw e820 table,
+ * - if PAT is not enabled, always treat the PFN as MMIO to avoid futher
+ * checking of MTRRs.
+ * - if PAT is enabled, treat the PFN as MMIO if its PAT is UC/WC/UC- in
+ * primary MMU.
+ * to prevent guest cacheable access to MMIO PFNs.
+ */
return !e820__mapped_raw_any(pfn_to_hpa(pfn),
pfn_to_hpa(pfn + 1) - 1,
- E820_TYPE_RAM);
+ E820_TYPE_RAM) ? true :
+ (!pat_enabled() || pat_pfn_immune_to_uc_mtrr(pfn));
}
/*
--
2.17.1
Powered by blists - more mailing lists