2017-12-20 07:29:58

by Haozhong Zhang

[permalink] [raw]
Subject: [PATCH v6 0/2] KVM: MMU: fix kvm_is_mmio_pfn()

Some reserved 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 as
MMIO. Because KVM maps MMIO pages with UC memory type, the
performance of guest accesses to those pages would be harmed.
Therefore, we check the host memory type in addition and only treat
UC/UC-/WC pages as MMIO.

Changes in v6:
* Rename the function in patch 1 to pat_immune_to_uc_mtrr().
* Consider WC memory type in patch 1.

Changes in v5:
* Rename pat_pfn_is_uc() into pat_pfn_is_uc_or_uc_minus() to avoid
confusion.
* Drop converters between kvm_pfn_t and pfn_t, because they are not
necessary. pat_pfn_is_uc_or_uc_minus() does not need flags in
pfn_t, so we can only pass a raw unsigned long to it.

Changes in v4:
* Mask pfn_t and kvm_pfn_t specific flags in conversion.

Changes in v3:
* Move cache mode check to pat.c as pat_pfn_is_uc()
* Reintroduce converters between kvm_pfn_t and pfn_t.

Changes in v2:
* Switch to lookup_memtype() to get host memory type.
* Rewrite the comment in KVM MMU patch.
* Remove v1 patch 2, which is not necessary in v2.

Haozhong Zhang (2):
x86/mm: add a function to check if a pfn is UC/UC-/WC
KVM: MMU: consider host cache mode in MMIO page check

arch/x86/include/asm/pat.h | 2 ++
arch/x86/kvm/mmu.c | 13 ++++++++++++-
arch/x86/mm/pat.c | 19 +++++++++++++++++++
3 files changed, 33 insertions(+), 1 deletion(-)

--
2.14.1


2017-12-20 07:30:04

by Haozhong Zhang

[permalink] [raw]
Subject: [PATCH v6 2/2] KVM: MMU: consider host cache mode in MMIO page check

Some reserved 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 as
MMIO. Because KVM maps MMIO pages with UC memory type, the
performance of guest accesses to those pages would be harmed.
Therefore, we check the host memory type in addition and only treat
UC/UC-/WC pages as MMIO.

Signed-off-by: Haozhong Zhang <[email protected]>
Reported-by: Cuevas Escareno, Ivan D <[email protected]>
Reported-by: Kumar, Karthik <[email protected]>
Reviewed-by: Xiao Guangrong <[email protected]>
---
arch/x86/kvm/mmu.c | 13 ++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kvm/mmu.c b/arch/x86/kvm/mmu.c
index 89da688784fa..e3b9998b3355 100644
--- a/arch/x86/kvm/mmu.c
+++ b/arch/x86/kvm/mmu.c
@@ -2708,7 +2708,18 @@ static bool mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
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)) &&
+ /*
+ * Some reserved 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
+ * as MMIO, and results in KVM mapping them with UC
+ * memory type, which would hurt the performance.
+ * Therefore, we check the host memory type in addition
+ * and only treat UC/UC-/WC pages as MMIO.
+ */
+ (!pat_enabled() || pat_immune_to_uc_mtrr(pfn));

return true;
}
--
2.14.1

2017-12-20 07:30:02

by Haozhong Zhang

[permalink] [raw]
Subject: [PATCH v6 1/2] x86/mm: add a function to check if a pfn is UC/UC-/WCee

Check whether the PAT memory type of a pfn cannot be overridden by
MTRR UC memory type, i.e. the PAT memory type is UC, UC- or WC. This
function will be used by KVM to determine whether it needs to map a
host pfn to guest with UC memory type.

Signed-off-by: Haozhong Zhang <[email protected]>
Reviewed-by: Xiao Guangrong <[email protected]>
---
arch/x86/include/asm/pat.h | 2 ++
arch/x86/mm/pat.c | 19 +++++++++++++++++++
2 files changed, 21 insertions(+)

diff --git a/arch/x86/include/asm/pat.h b/arch/x86/include/asm/pat.h
index 8a3ee355b422..9a217a18523b 100644
--- a/arch/x86/include/asm/pat.h
+++ b/arch/x86/include/asm/pat.h
@@ -22,4 +22,6 @@ int io_reserve_memtype(resource_size_t start, resource_size_t end,

void io_free_memtype(resource_size_t start, resource_size_t end);

+bool pat_immune_to_uc_mtrr(unsigned long pfn);
+
#endif /* _ASM_X86_PAT_H */
diff --git a/arch/x86/mm/pat.c b/arch/x86/mm/pat.c
index fe7d57a8fb60..2231a84c3d34 100644
--- a/arch/x86/mm/pat.c
+++ b/arch/x86/mm/pat.c
@@ -677,6 +677,25 @@ static enum page_cache_mode lookup_memtype(u64 paddr)
return rettype;
}

+/**
+ * Check whether the PAT memory type of @pfn cannot be overridden by
+ * UC MTRR memory type.
+ *
+ * Only to be called when PAT is enabled.
+ *
+ * Returns true, if the PAT memory type of @pfn is UC, UC-, or WC.
+ * Returns false in other cases.
+ */
+bool pat_immune_to_uc_mtrr(unsigned long pfn)
+{
+ enum page_cache_mode cm = lookup_memtype(PFN_PHYS(pfn));
+
+ return cm == _PAGE_CACHE_MODE_UC ||
+ cm == _PAGE_CACHE_MODE_UC_MINUS ||
+ cm == _PAGE_CACHE_MODE_WC;
+}
+EXPORT_SYMBOL_GPL(pat_immune_to_uc_mtrr);
+
/**
* io_reserve_memtype - Request a memory type mapping for a region of memory
* @start: start (physical address) of the region
--
2.14.1

2017-12-20 10:13:03

by Paolo Bonzini

[permalink] [raw]
Subject: Re: [PATCH v6 0/2] KVM: MMU: fix kvm_is_mmio_pfn()

On 20/12/2017 08:29, Haozhong Zhang wrote:
> Some reserved 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 as
> MMIO. Because KVM maps MMIO pages with UC memory type, the
> performance of guest accesses to those pages would be harmed.
> Therefore, we check the host memory type in addition and only treat
> UC/UC-/WC pages as MMIO.
>
> Changes in v6:
> * Rename the function in patch 1 to pat_immune_to_uc_mtrr().
> * Consider WC memory type in patch 1.
>
> Changes in v5:
> * Rename pat_pfn_is_uc() into pat_pfn_is_uc_or_uc_minus() to avoid
> confusion.
> * Drop converters between kvm_pfn_t and pfn_t, because they are not
> necessary. pat_pfn_is_uc_or_uc_minus() does not need flags in
> pfn_t, so we can only pass a raw unsigned long to it.
>
> Changes in v4:
> * Mask pfn_t and kvm_pfn_t specific flags in conversion.
>
> Changes in v3:
> * Move cache mode check to pat.c as pat_pfn_is_uc()
> * Reintroduce converters between kvm_pfn_t and pfn_t.
>
> Changes in v2:
> * Switch to lookup_memtype() to get host memory type.
> * Rewrite the comment in KVM MMU patch.
> * Remove v1 patch 2, which is not necessary in v2.
>
> Haozhong Zhang (2):
> x86/mm: add a function to check if a pfn is UC/UC-/WC
> KVM: MMU: consider host cache mode in MMIO page check
>
> arch/x86/include/asm/pat.h | 2 ++
> arch/x86/kvm/mmu.c | 13 ++++++++++++-
> arch/x86/mm/pat.c | 19 +++++++++++++++++++
> 3 files changed, 33 insertions(+), 1 deletion(-)
>

Thanks, I applied this series. I only changed the function name to
pat_pfn_immune_to_uc_mtrr.

Thanks,

Paolo