2017-11-03 01:17:48

by Haozhong Zhang

[permalink] [raw]
Subject: [PATCH v3 0/3] 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 by lookup_memtype() in
addition and only treat UC/UC- pages as MMIO.


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 (3):
x86/mm: add function to check if a pfn is UC/UC-
KVM: add converters between pfn_t and kvm_pfn_t
KVM: MMU: consider host cache mode in MMIO page check

arch/x86/include/asm/pat.h | 2 ++
arch/x86/kvm/mmu.c | 18 +++++++++++++++++-
arch/x86/mm/pat.c | 14 ++++++++++++++
include/linux/kvm_host.h | 3 +++
4 files changed, 36 insertions(+), 1 deletion(-)

--
2.14.1


From 1583002540715856895@xxx Fri Nov 03 00:27:45 +0000 2017
X-GM-THRID: 1582773659541235477
X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread


2017-11-03 01:17:48

by Haozhong Zhang

[permalink] [raw]
Subject: [PATCH v3 3/3] 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 by lookup_memtype() in
addition and only treat UC/UC- pages as MMIO.

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

diff --git a/arch/x86/kvm/mmu.c b/arch/x86/kvm/mmu.c
index 0b481cc9c725..058ff6442638 100644
--- a/arch/x86/kvm/mmu.c
+++ b/arch/x86/kvm/mmu.c
@@ -2708,7 +2708,23 @@ 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.
+ * 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- pages as MMIO.
+ *
+ * pat_pfn_is_uc() works only when PAT is enabled,
+ * so check pat_enabled() as well.
+ */
+ (!pat_enabled() || pat_pfn_is_uc(kvm_pfn_to_pfn(pfn)));

return true;
}
--
2.14.1


From 1583483348372747268@xxx Wed Nov 08 07:49:59 +0000 2017
X-GM-THRID: 1583483348372747268
X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread

2017-11-03 01:18:03

by Haozhong Zhang

[permalink] [raw]
Subject: [PATCH v3 2/3] KVM: add converters between pfn_t and kvm_pfn_t

Signed-off-by: Haozhong Zhang <[email protected]>
---
include/linux/kvm_host.h | 3 +++
1 file changed, 3 insertions(+)

diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index 6882538eda32..759fe498c89e 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -67,6 +67,9 @@
#define KVM_PFN_ERR_HWPOISON (KVM_PFN_ERR_MASK + 1)
#define KVM_PFN_ERR_RO_FAULT (KVM_PFN_ERR_MASK + 2)

+#define kvm_pfn_to_pfn(x) ((pfn_t){ .val = (x)})
+#define pfn_to_kvm_pfn(x) ((kvm_pfn_t)((x).val))
+
/*
* error pfns indicate that the gfn is in slot but faild to
* translate it to pfn on host.
--
2.14.1


From 1583470984007385339@xxx Wed Nov 08 04:33:28 +0000 2017
X-GM-THRID: 1583447418213042380
X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread

2017-11-03 01:17:48

by Haozhong Zhang

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

pat_pfn_is_uc(pfn) is added and will be used by KVM to check whether
the memory type of a host pfn is UC/UC-.

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

diff --git a/arch/x86/include/asm/pat.h b/arch/x86/include/asm/pat.h
index fffb2794dd89..4027d9fb10a8 100644
--- a/arch/x86/include/asm/pat.h
+++ b/arch/x86/include/asm/pat.h
@@ -21,4 +21,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_pfn_is_uc(pfn_t pfn);
+
#endif /* _ASM_X86_PAT_H */
diff --git a/arch/x86/mm/pat.c b/arch/x86/mm/pat.c
index fe7d57a8fb60..71fe9c29727e 100644
--- a/arch/x86/mm/pat.c
+++ b/arch/x86/mm/pat.c
@@ -677,6 +677,20 @@ static enum page_cache_mode lookup_memtype(u64 paddr)
return rettype;
}

+/**
+ * Check whether the memory type of a pfn is UC or UC-. The result is
+ * valid only when PAT is enabled.
+ *
+ * Returns true if it is; otherwise, returns false.
+ */
+bool pat_pfn_is_uc(pfn_t pfn)
+{
+ enum page_cache_mode cm = lookup_memtype(pfn_t_to_phys(pfn));
+
+ return cm == _PAGE_CACHE_MODE_UC || cm == _PAGE_CACHE_MODE_UC_MINUS;
+}
+EXPORT_SYMBOL_GPL(pat_pfn_is_uc);
+
/**
* io_reserve_memtype - Request a memory type mapping for a region of memory
* @start: start (physical address) of the region
--
2.14.1


From 1583331563091358548@xxx Mon Nov 06 15:37:26 +0000 2017
X-GM-THRID: 1583325461642585509
X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread