2017-11-03 05:54:35

by Haozhong Zhang

[permalink] [raw]
Subject: [PATCH v4 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 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 (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 | 19 ++++++++++++++++++-
arch/x86/mm/pat.c | 14 ++++++++++++++
include/linux/kvm_host.h | 4 ++++
4 files changed, 38 insertions(+), 1 deletion(-)

--
2.14.1


From 1583005689587814014@xxx Fri Nov 03 01:17:48 +0000 2017
X-GM-THRID: 1582773659541235477
X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread


2017-11-03 05:55:11

by Haozhong Zhang

[permalink] [raw]
Subject: [PATCH v4 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 | 19 ++++++++++++++++++-
1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kvm/mmu.c b/arch/x86/kvm/mmu.c
index 0b481cc9c725..e9ed0e666a83 100644
--- a/arch/x86/kvm/mmu.c
+++ b/arch/x86/kvm/mmu.c
@@ -2708,7 +2708,24 @@ 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_t_to_pfn_t(pfn)));

return true;
}
--
2.14.1


From 1583005689428392726@xxx Fri Nov 03 01:17:48 +0000 2017
X-GM-THRID: 1583005689428392726
X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread

2017-11-03 05:55:06

by Haozhong Zhang

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

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

diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index 6882538eda32..caf6f7a6bdb2 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -27,6 +27,7 @@
#include <linux/irqbypass.h>
#include <linux/swait.h>
#include <linux/refcount.h>
+#include <linux/pfn_t.h>
#include <asm/signal.h>

#include <linux/kvm.h>
@@ -67,6 +68,9 @@
#define KVM_PFN_ERR_HWPOISON (KVM_PFN_ERR_MASK + 1)
#define KVM_PFN_ERR_RO_FAULT (KVM_PFN_ERR_MASK + 2)

+#define pfn_t_to_kvm_pfn_t(x) pfn_t_to_pfn(x)
+#define kvm_pfn_t_to_pfn_t(x) pfn_to_pfn_t((x) & ~KVM_PFN_ERR_NOSLOT_MASK)
+
/*
* error pfns indicate that the gfn is in slot but faild to
* translate it to pfn on host.
--
2.14.1


From 1583010866870781763@xxx Fri Nov 03 02:40:06 +0000 2017
X-GM-THRID: 1583005705143932866
X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread

2017-11-03 05:54:58

by Haozhong Zhang

[permalink] [raw]
Subject: [PATCH v4 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 1583005689258050510@xxx Fri Nov 03 01:17:48 +0000 2017
X-GM-THRID: 1583005689258050510
X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread