All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 0/3] KVM: MMU: fix kvm_is_mmio_pfn()
@ 2017-11-03  1:16 Haozhong Zhang
  2017-11-03  1:16 ` [PATCH v3 1/3] x86/mm: add function to check if a pfn is UC/UC- Haozhong Zhang
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Haozhong Zhang @ 2017-11-03  1:16 UTC (permalink / raw)
  To: kvm, x86
  Cc: linux-kernel, Paolo Bonzini, rkrcmar, Xiao Guangrong,
	Dan Williams, ivan.d.cuevas.escareno, karthik.kumar,
	Konrad Rzeszutek Wilk, Olif Chapman, Haozhong Zhang,
	Thomas Gleixner, Ingo Molnar, H. Peter Anvin, Borislav Petkov,
	Mikulas Patocka, Tom Lendacky

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v3 1/3] x86/mm: add function to check if a pfn is UC/UC-
  2017-11-03  1:16 [PATCH v3 0/3] KVM: MMU: fix kvm_is_mmio_pfn() Haozhong Zhang
@ 2017-11-03  1:16 ` Haozhong Zhang
  2017-11-03  1:16 ` [PATCH v3 2/3] KVM: add converters between pfn_t and kvm_pfn_t Haozhong Zhang
  2017-11-03  1:16 ` [PATCH v3 3/3] KVM: MMU: consider host cache mode in MMIO page check Haozhong Zhang
  2 siblings, 0 replies; 6+ messages in thread
From: Haozhong Zhang @ 2017-11-03  1:16 UTC (permalink / raw)
  To: kvm, x86
  Cc: linux-kernel, Paolo Bonzini, rkrcmar, Xiao Guangrong,
	Dan Williams, ivan.d.cuevas.escareno, karthik.kumar,
	Konrad Rzeszutek Wilk, Olif Chapman, Haozhong Zhang,
	Thomas Gleixner, Ingo Molnar, H. Peter Anvin, Borislav Petkov,
	Tom Lendacky, Mikulas Patocka

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 <haozhong.zhang@intel.com>
---
 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

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v3 2/3] KVM: add converters between pfn_t and kvm_pfn_t
  2017-11-03  1:16 [PATCH v3 0/3] KVM: MMU: fix kvm_is_mmio_pfn() Haozhong Zhang
  2017-11-03  1:16 ` [PATCH v3 1/3] x86/mm: add function to check if a pfn is UC/UC- Haozhong Zhang
@ 2017-11-03  1:16 ` Haozhong Zhang
  2017-11-03  2:25   ` Dan Williams
  2017-11-03  1:16 ` [PATCH v3 3/3] KVM: MMU: consider host cache mode in MMIO page check Haozhong Zhang
  2 siblings, 1 reply; 6+ messages in thread
From: Haozhong Zhang @ 2017-11-03  1:16 UTC (permalink / raw)
  To: kvm, x86
  Cc: linux-kernel, Paolo Bonzini, rkrcmar, Xiao Guangrong,
	Dan Williams, ivan.d.cuevas.escareno, karthik.kumar,
	Konrad Rzeszutek Wilk, Olif Chapman, Haozhong Zhang

Signed-off-by: Haozhong Zhang <haozhong.zhang@intel.com>
---
 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

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v3 3/3] KVM: MMU: consider host cache mode in MMIO page check
  2017-11-03  1:16 [PATCH v3 0/3] KVM: MMU: fix kvm_is_mmio_pfn() Haozhong Zhang
  2017-11-03  1:16 ` [PATCH v3 1/3] x86/mm: add function to check if a pfn is UC/UC- Haozhong Zhang
  2017-11-03  1:16 ` [PATCH v3 2/3] KVM: add converters between pfn_t and kvm_pfn_t Haozhong Zhang
@ 2017-11-03  1:16 ` Haozhong Zhang
  2 siblings, 0 replies; 6+ messages in thread
From: Haozhong Zhang @ 2017-11-03  1:16 UTC (permalink / raw)
  To: kvm, x86
  Cc: linux-kernel, Paolo Bonzini, rkrcmar, Xiao Guangrong,
	Dan Williams, ivan.d.cuevas.escareno, karthik.kumar,
	Konrad Rzeszutek Wilk, Olif Chapman, Haozhong Zhang, Ingo Molnar

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 <haozhong.zhang@intel.com>
Reported-by: Cuevas Escareno, Ivan D <ivan.d.cuevas.escareno@intel.com>
Reported-by: Kumar, Karthik <karthik.kumar@intel.com>
---
 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

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v3 2/3] KVM: add converters between pfn_t and kvm_pfn_t
  2017-11-03  1:16 ` [PATCH v3 2/3] KVM: add converters between pfn_t and kvm_pfn_t Haozhong Zhang
@ 2017-11-03  2:25   ` Dan Williams
  2017-11-03  2:39     ` Haozhong Zhang
  0 siblings, 1 reply; 6+ messages in thread
From: Dan Williams @ 2017-11-03  2:25 UTC (permalink / raw)
  To: Haozhong Zhang
  Cc: KVM list, X86 ML, linux-kernel, Paolo Bonzini, rkrcmar,
	Xiao Guangrong, Cuevas Escareno, Ivan D, Kumar, Karthik,
	Konrad Rzeszutek Wilk, Olif Chapman

On Thu, Nov 2, 2017 at 6:16 PM, Haozhong Zhang <haozhong.zhang@intel.com> wrote:
> Signed-off-by: Haozhong Zhang <haozhong.zhang@intel.com>
> ---
>  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))

Shouldn't this mask off the kvm_pfn_t and  pfn_t specific bits when
converting between the two?

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v3 2/3] KVM: add converters between pfn_t and kvm_pfn_t
  2017-11-03  2:25   ` Dan Williams
@ 2017-11-03  2:39     ` Haozhong Zhang
  0 siblings, 0 replies; 6+ messages in thread
From: Haozhong Zhang @ 2017-11-03  2:39 UTC (permalink / raw)
  To: Dan Williams
  Cc: KVM list, X86 ML, linux-kernel, Paolo Bonzini, rkrcmar,
	Xiao Guangrong, Cuevas Escareno, Ivan D, Kumar, Karthik,
	Konrad Rzeszutek Wilk, Olif Chapman

On 11/02/17 19:25 -0700, Dan Williams wrote:
> On Thu, Nov 2, 2017 at 6:16 PM, Haozhong Zhang <haozhong.zhang@intel.com> wrote:
> > Signed-off-by: Haozhong Zhang <haozhong.zhang@intel.com>
> > ---
> >  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))
> 
> Shouldn't this mask off the kvm_pfn_t and  pfn_t specific bits when
> converting between the two?

Yes, I'll fix it.

Thanks,
Haozhong

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2017-11-03  2:39 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-11-03  1:16 [PATCH v3 0/3] KVM: MMU: fix kvm_is_mmio_pfn() Haozhong Zhang
2017-11-03  1:16 ` [PATCH v3 1/3] x86/mm: add function to check if a pfn is UC/UC- Haozhong Zhang
2017-11-03  1:16 ` [PATCH v3 2/3] KVM: add converters between pfn_t and kvm_pfn_t Haozhong Zhang
2017-11-03  2:25   ` Dan Williams
2017-11-03  2:39     ` Haozhong Zhang
2017-11-03  1:16 ` [PATCH v3 3/3] KVM: MMU: consider host cache mode in MMIO page check Haozhong Zhang

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.