Re: [PATCH v5 07/13] KVM/nVMX: Use kvm_vcpu_map when mapping the virtual APIC page

2019-01-25 Thread Raslan, KarimAllah
On Wed, 2019-01-23 at 12:57 -0500, Konrad Rzeszutek Wilk wrote:
> On Wed, Jan 09, 2019 at 10:42:07AM +0100, KarimAllah Ahmed wrote:
> > 
> > Use kvm_vcpu_map when mapping the virtual APIC page since using
> > kvm_vcpu_gpa_to_page() and kmap() will only work for guest memory that has
> > a "struct page".
> > 
> > One additional semantic change is that the virtual host mapping lifecycle
> > has changed a bit. It now has the same lifetime of the pinning of the
> > virtual APIC page on the host side.
> 
> Could you expand a bit on the 'same lifetime .. on the host side'  to be
> obvious for folks what exactly the semantic is?
> 
> And how does this ring with this comment:
> > 
> > 
> > Signed-off-by: KarimAllah Ahmed 
> > ---
> > v4 -> v5:
> > - unmap with dirty flag
> > 
> > v1 -> v2:
> > - Do not change the lifecycle of the mapping (pbonzini)
> 
> .. Where Paolo does not want the semantics of the mapping to be changed?
> 
> Code wise feel free to smack my Reviewed-by on it, but obviously the
> question on the above comment needs to be resolved.

Ah, right. So there was two life-cycle changes:

1- Lazily unmap the mapping and only do it on: a) release b) if the gfn is 
   different. This was done as an optimization (i.e. cache of a single entry). 
   This is the first life-cycle change that Paolo asked me to do seperately and
   suggested to create a PFN cache instead. This has indeed been dropped
   between v1 -> v2 switch.

2- The life-cycle change now is the fact that the kvm_vcpu_map interface does: 
   a) map to a virtual address b) translate a gfn to a pfn.

   The original code was doing the kmap in one location and the gfn_to_page in
   another. Using kvm_vcpu_map means that both kmap+gfn_to_page will be tied 
   together and will not be done seperately. So far no one complained about
   this one so I kept it :D

> 
> Thank you.
> 
> > 
> > - Use pfn_to_hpa instead of gfn_to_gpa
> > ---
> >  arch/x86/kvm/vmx/nested.c | 32 +++-
> >  arch/x86/kvm/vmx/vmx.c|  5 ++---
> >  arch/x86/kvm/vmx/vmx.h|  2 +-
> >  3 files changed, 14 insertions(+), 25 deletions(-)
> > 
> > diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
> > index 4127ad9..dcff99d 100644
> > --- a/arch/x86/kvm/vmx/nested.c
> > +++ b/arch/x86/kvm/vmx/nested.c
> > @@ -229,10 +229,7 @@ static void free_nested(struct kvm_vcpu *vcpu)
> > kvm_release_page_dirty(vmx->nested.apic_access_page);
> > vmx->nested.apic_access_page = NULL;
> > }
> > -   if (vmx->nested.virtual_apic_page) {
> > -   kvm_release_page_dirty(vmx->nested.virtual_apic_page);
> > -   vmx->nested.virtual_apic_page = NULL;
> > -   }
> > +   kvm_vcpu_unmap(&vmx->nested.virtual_apic_map, true);
> > if (vmx->nested.pi_desc_page) {
> > kunmap(vmx->nested.pi_desc_page);
> > kvm_release_page_dirty(vmx->nested.pi_desc_page);
> > @@ -2817,6 +2814,7 @@ static void nested_get_vmcs12_pages(struct kvm_vcpu 
> > *vcpu)
> >  {
> > struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
> > struct vcpu_vmx *vmx = to_vmx(vcpu);
> > +   struct kvm_host_map *map;
> > struct page *page;
> > u64 hpa;
> >  
> > @@ -2849,11 +2847,7 @@ static void nested_get_vmcs12_pages(struct kvm_vcpu 
> > *vcpu)
> > }
> >  
> > if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
> > -   if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
> > -   kvm_release_page_dirty(vmx->nested.virtual_apic_page);
> > -   vmx->nested.virtual_apic_page = NULL;
> > -   }
> > -   page = kvm_vcpu_gpa_to_page(vcpu, 
> > vmcs12->virtual_apic_page_addr);
> > +   map = &vmx->nested.virtual_apic_map;
> >  
> > /*
> >  * If translation failed, VM entry will fail because
> > @@ -2868,11 +2862,9 @@ static void nested_get_vmcs12_pages(struct kvm_vcpu 
> > *vcpu)
> >  * control.  But such a configuration is useless, so
> >  * let's keep the code simple.
> >  */
> > -   if (!is_error_page(page)) {
> > -   vmx->nested.virtual_apic_page = page;
> > -   hpa = page_to_phys(vmx->nested.virtual_apic_page);
> > -   vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
> > -   }
> > +   if (!kvm_vcpu_map(vcpu, 
> > gpa_to_gfn(vmcs12->virtual_apic_page_addr), map))
> > +   vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 
> > pfn_to_hpa(map->pfn));
> > +
> > }
> >  
> > if (nested_cpu_has_posted_intr(vmcs12)) {
> > @@ -3279,11 +3271,12 @@ static void 
> > vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
> >  
> > max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
> > if (max_irr != 256) {
> > -   vapic_page = kmap(vmx->nested.virtual_apic_page);
> > +   vapic_page = vmx->nested.virtual_apic_map.hva;
> > +   if (!vapic_page)
> > +   return;
> > +

Re: [PATCH v5 07/13] KVM/nVMX: Use kvm_vcpu_map when mapping the virtual APIC page

2019-01-23 Thread Konrad Rzeszutek Wilk
On Wed, Jan 09, 2019 at 10:42:07AM +0100, KarimAllah Ahmed wrote:
> Use kvm_vcpu_map when mapping the virtual APIC page since using
> kvm_vcpu_gpa_to_page() and kmap() will only work for guest memory that has
> a "struct page".
> 
> One additional semantic change is that the virtual host mapping lifecycle
> has changed a bit. It now has the same lifetime of the pinning of the
> virtual APIC page on the host side.

Could you expand a bit on the 'same lifetime .. on the host side'  to be
obvious for folks what exactly the semantic is?

And how does this ring with this comment:
> 
> Signed-off-by: KarimAllah Ahmed 
> ---
> v4 -> v5:
> - unmap with dirty flag
> 
> v1 -> v2:
> - Do not change the lifecycle of the mapping (pbonzini)

.. Where Paolo does not want the semantics of the mapping to be changed?

Code wise feel free to smack my Reviewed-by on it, but obviously the
question on the above comment needs to be resolved.

Thank you.

> - Use pfn_to_hpa instead of gfn_to_gpa
> ---
>  arch/x86/kvm/vmx/nested.c | 32 +++-
>  arch/x86/kvm/vmx/vmx.c|  5 ++---
>  arch/x86/kvm/vmx/vmx.h|  2 +-
>  3 files changed, 14 insertions(+), 25 deletions(-)
> 
> diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
> index 4127ad9..dcff99d 100644
> --- a/arch/x86/kvm/vmx/nested.c
> +++ b/arch/x86/kvm/vmx/nested.c
> @@ -229,10 +229,7 @@ static void free_nested(struct kvm_vcpu *vcpu)
>   kvm_release_page_dirty(vmx->nested.apic_access_page);
>   vmx->nested.apic_access_page = NULL;
>   }
> - if (vmx->nested.virtual_apic_page) {
> - kvm_release_page_dirty(vmx->nested.virtual_apic_page);
> - vmx->nested.virtual_apic_page = NULL;
> - }
> + kvm_vcpu_unmap(&vmx->nested.virtual_apic_map, true);
>   if (vmx->nested.pi_desc_page) {
>   kunmap(vmx->nested.pi_desc_page);
>   kvm_release_page_dirty(vmx->nested.pi_desc_page);
> @@ -2817,6 +2814,7 @@ static void nested_get_vmcs12_pages(struct kvm_vcpu 
> *vcpu)
>  {
>   struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
>   struct vcpu_vmx *vmx = to_vmx(vcpu);
> + struct kvm_host_map *map;
>   struct page *page;
>   u64 hpa;
>  
> @@ -2849,11 +2847,7 @@ static void nested_get_vmcs12_pages(struct kvm_vcpu 
> *vcpu)
>   }
>  
>   if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
> - if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
> - kvm_release_page_dirty(vmx->nested.virtual_apic_page);
> - vmx->nested.virtual_apic_page = NULL;
> - }
> - page = kvm_vcpu_gpa_to_page(vcpu, 
> vmcs12->virtual_apic_page_addr);
> + map = &vmx->nested.virtual_apic_map;
>  
>   /*
>* If translation failed, VM entry will fail because
> @@ -2868,11 +2862,9 @@ static void nested_get_vmcs12_pages(struct kvm_vcpu 
> *vcpu)
>* control.  But such a configuration is useless, so
>* let's keep the code simple.
>*/
> - if (!is_error_page(page)) {
> - vmx->nested.virtual_apic_page = page;
> - hpa = page_to_phys(vmx->nested.virtual_apic_page);
> - vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
> - }
> + if (!kvm_vcpu_map(vcpu, 
> gpa_to_gfn(vmcs12->virtual_apic_page_addr), map))
> + vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 
> pfn_to_hpa(map->pfn));
> +
>   }
>  
>   if (nested_cpu_has_posted_intr(vmcs12)) {
> @@ -3279,11 +3271,12 @@ static void 
> vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
>  
>   max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
>   if (max_irr != 256) {
> - vapic_page = kmap(vmx->nested.virtual_apic_page);
> + vapic_page = vmx->nested.virtual_apic_map.hva;
> + if (!vapic_page)
> + return;
> +
>   __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
>   vapic_page, &max_irr);
> - kunmap(vmx->nested.virtual_apic_page);
> -
>   status = vmcs_read16(GUEST_INTR_STATUS);
>   if ((u8)max_irr > ((u8)status & 0xff)) {
>   status &= ~0xff;
> @@ -3917,10 +3910,7 @@ void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 
> exit_reason,
>   kvm_release_page_dirty(vmx->nested.apic_access_page);
>   vmx->nested.apic_access_page = NULL;
>   }
> - if (vmx->nested.virtual_apic_page) {
> - kvm_release_page_dirty(vmx->nested.virtual_apic_page);
> - vmx->nested.virtual_apic_page = NULL;
> - }
> + kvm_vcpu_unmap(&vmx->nested.virtual_apic_map, true);
>   if (vmx->nested.pi_desc_page) {
>   kunmap(vmx->nested.pi_desc_page);
>   kvm_release_page_dirty(vmx->nested.pi_desc_page);
> diff --git a/arch/x86/kvm/vmx/vmx.c b/arch

[PATCH v5 07/13] KVM/nVMX: Use kvm_vcpu_map when mapping the virtual APIC page

2019-01-09 Thread KarimAllah Ahmed
Use kvm_vcpu_map when mapping the virtual APIC page since using
kvm_vcpu_gpa_to_page() and kmap() will only work for guest memory that has
a "struct page".

One additional semantic change is that the virtual host mapping lifecycle
has changed a bit. It now has the same lifetime of the pinning of the
virtual APIC page on the host side.

Signed-off-by: KarimAllah Ahmed 
---
v4 -> v5:
- unmap with dirty flag

v1 -> v2:
- Do not change the lifecycle of the mapping (pbonzini)
- Use pfn_to_hpa instead of gfn_to_gpa
---
 arch/x86/kvm/vmx/nested.c | 32 +++-
 arch/x86/kvm/vmx/vmx.c|  5 ++---
 arch/x86/kvm/vmx/vmx.h|  2 +-
 3 files changed, 14 insertions(+), 25 deletions(-)

diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
index 4127ad9..dcff99d 100644
--- a/arch/x86/kvm/vmx/nested.c
+++ b/arch/x86/kvm/vmx/nested.c
@@ -229,10 +229,7 @@ static void free_nested(struct kvm_vcpu *vcpu)
kvm_release_page_dirty(vmx->nested.apic_access_page);
vmx->nested.apic_access_page = NULL;
}
-   if (vmx->nested.virtual_apic_page) {
-   kvm_release_page_dirty(vmx->nested.virtual_apic_page);
-   vmx->nested.virtual_apic_page = NULL;
-   }
+   kvm_vcpu_unmap(&vmx->nested.virtual_apic_map, true);
if (vmx->nested.pi_desc_page) {
kunmap(vmx->nested.pi_desc_page);
kvm_release_page_dirty(vmx->nested.pi_desc_page);
@@ -2817,6 +2814,7 @@ static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
 {
struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
struct vcpu_vmx *vmx = to_vmx(vcpu);
+   struct kvm_host_map *map;
struct page *page;
u64 hpa;
 
@@ -2849,11 +2847,7 @@ static void nested_get_vmcs12_pages(struct kvm_vcpu 
*vcpu)
}
 
if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
-   if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
-   kvm_release_page_dirty(vmx->nested.virtual_apic_page);
-   vmx->nested.virtual_apic_page = NULL;
-   }
-   page = kvm_vcpu_gpa_to_page(vcpu, 
vmcs12->virtual_apic_page_addr);
+   map = &vmx->nested.virtual_apic_map;
 
/*
 * If translation failed, VM entry will fail because
@@ -2868,11 +2862,9 @@ static void nested_get_vmcs12_pages(struct kvm_vcpu 
*vcpu)
 * control.  But such a configuration is useless, so
 * let's keep the code simple.
 */
-   if (!is_error_page(page)) {
-   vmx->nested.virtual_apic_page = page;
-   hpa = page_to_phys(vmx->nested.virtual_apic_page);
-   vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
-   }
+   if (!kvm_vcpu_map(vcpu, 
gpa_to_gfn(vmcs12->virtual_apic_page_addr), map))
+   vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 
pfn_to_hpa(map->pfn));
+
}
 
if (nested_cpu_has_posted_intr(vmcs12)) {
@@ -3279,11 +3271,12 @@ static void vmx_complete_nested_posted_interrupt(struct 
kvm_vcpu *vcpu)
 
max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
if (max_irr != 256) {
-   vapic_page = kmap(vmx->nested.virtual_apic_page);
+   vapic_page = vmx->nested.virtual_apic_map.hva;
+   if (!vapic_page)
+   return;
+
__kvm_apic_update_irr(vmx->nested.pi_desc->pir,
vapic_page, &max_irr);
-   kunmap(vmx->nested.virtual_apic_page);
-
status = vmcs_read16(GUEST_INTR_STATUS);
if ((u8)max_irr > ((u8)status & 0xff)) {
status &= ~0xff;
@@ -3917,10 +3910,7 @@ void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 
exit_reason,
kvm_release_page_dirty(vmx->nested.apic_access_page);
vmx->nested.apic_access_page = NULL;
}
-   if (vmx->nested.virtual_apic_page) {
-   kvm_release_page_dirty(vmx->nested.virtual_apic_page);
-   vmx->nested.virtual_apic_page = NULL;
-   }
+   kvm_vcpu_unmap(&vmx->nested.virtual_apic_map, true);
if (vmx->nested.pi_desc_page) {
kunmap(vmx->nested.pi_desc_page);
kvm_release_page_dirty(vmx->nested.pi_desc_page);
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index 71d88df..e13308e 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -3627,14 +3627,13 @@ static bool vmx_guest_apic_has_interrupt(struct 
kvm_vcpu *vcpu)
 
if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
!nested_cpu_has_vid(get_vmcs12(vcpu)) ||
-   WARN_ON_ONCE(!vmx->nested.virtual_apic_page))
+   WARN_ON_ONCE(!vmx->nested.virtual_apic_map.gfn))
return false;
 
rvi = vmx_get_rvi();
 
-   vapic_page = kmap(vmx->nested.virtual_