[PATCH RESEND] KVM: arm: fix missing free_percpu_irq in kvm_timer_hyp_init()

2020-03-13 Thread linmiaohe
From: Miaohe Lin 

When host_ptimer_irq request irq resource failed, we forget to release the
host_vtimer_irq resource already requested. Fix this missing irq release
and other similar scenario.

Fixes: 9e01dc76be6a ("KVM: arm/arm64: arch_timer: Assign the phys timer on VHE 
systems")
Signed-off-by: Miaohe Lin 
---
 virt/kvm/arm/arch_timer.c | 13 -
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/virt/kvm/arm/arch_timer.c b/virt/kvm/arm/arch_timer.c
index e2bb5bd60227..fe958255ae2b 100644
--- a/virt/kvm/arm/arch_timer.c
+++ b/virt/kvm/arm/arch_timer.c
@@ -935,7 +935,7 @@ int kvm_timer_hyp_init(bool has_gic)
kvm_get_running_vcpus());
if (err) {
kvm_err("kvm_arch_timer: error setting vcpu 
affinity\n");
-   goto out_free_irq;
+   goto out_free_vtime_irq;
}
 
static_branch_enable(&has_gic_active_state);
@@ -960,7 +960,7 @@ int kvm_timer_hyp_init(bool has_gic)
if (err) {
kvm_err("kvm_arch_timer: can't request ptimer interrupt 
%d (%d)\n",
host_ptimer_irq, err);
-   return err;
+   goto out_free_vtime_irq;
}
 
if (has_gic) {
@@ -968,7 +968,7 @@ int kvm_timer_hyp_init(bool has_gic)
kvm_get_running_vcpus());
if (err) {
kvm_err("kvm_arch_timer: error setting vcpu 
affinity\n");
-   goto out_free_irq;
+   goto out_free_ptime_irq;
}
}
 
@@ -977,14 +977,17 @@ int kvm_timer_hyp_init(bool has_gic)
kvm_err("kvm_arch_timer: invalid physical timer IRQ: %d\n",
info->physical_irq);
err = -ENODEV;
-   goto out_free_irq;
+   goto out_free_vtime_irq;
}
 
cpuhp_setup_state(CPUHP_AP_KVM_ARM_TIMER_STARTING,
  "kvm/arm/timer:starting", kvm_timer_starting_cpu,
  kvm_timer_dying_cpu);
return 0;
-out_free_irq:
+
+out_free_ptime_irq:
+   free_percpu_irq(host_ptimer_irq, kvm_get_running_vcpus());
+out_free_vtime_irq:
free_percpu_irq(host_vtimer_irq, kvm_get_running_vcpus());
return err;
 }
-- 
2.19.1

___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm


Re: [PATCH] KVM: arm64: get rid of var ret and out jump label in kvm_arch_vcpu_ioctl_set_guest_debug()

2020-01-15 Thread linmiaohe


Marc Zyngier  wrote:
> 
> I don't think there is anything wrong with the existing code.
> It may not be to your own taste, but is in keeping with a lot of the KVM code.
>
> If you were making changes to this code, I wouldn't object.
> But on its own, this is just churn.
>
> Thanks,
>
>  M.

Oh, I see. Many thanks for your reply and patient explaination.
___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm


Re: [PATCH] KVM: arm/arm64: Fix some obsolete comments

2020-01-15 Thread linmiaohe
friendly ping. :)
> From: Miaohe Lin 
>
> Fix various comments, including comment typo, and obsolete comments no longer 
> make sense.
> Signed-off-by: Miaohe Lin 
> ---
>  virt/kvm/arm/arch_timer.c| 5 ++---
>  virt/kvm/arm/arm.c   | 1 -
>  virt/kvm/arm/vgic/vgic-its.c | 2 +-
>  3 files changed, 3 insertions(+), 5 deletions(-)
>
> diff --git a/virt/kvm/arm/arch_timer.c b/virt/kvm/arm/arch_timer.c index 
> 73867f97040c..d8d2f4bec935 100644
> --- a/virt/kvm/arm/arch_timer.c
> +++ b/virt/kvm/arm/arch_timer.c
> @@ -322,9 +322,8 @@ static void timer_emulate(struct arch_timer_context *ctx)
>   }
>  
>   /*
> -  * If the timer can fire now, we don't need to have a soft timer
> -  * scheduled for the future.  If the timer cannot fire at all,
> -  * then we also don't need a soft timer.
> +  * If the timer cannot fire at all, we don't need to have a
> +  * soft timer scheduled for the future.
>*/
>   if (!kvm_timer_irq_can_fire(ctx)) {
>   soft_timer_cancel(&ctx->hrtimer);
> diff --git a/virt/kvm/arm/arm.c b/virt/kvm/arm/arm.c index 
> 8de4daf25097..7687663ab71b 100644
> --- a/virt/kvm/arm/arm.c
> +++ b/virt/kvm/arm/arm.c
> @@ -525,7 +525,6 @@ static bool need_new_vmid_gen(struct kvm_vmid *vmid)
>  
>  /**
>   * update_vmid - Update the vmid with a valid VMID for the current generation
> - * @kvm: The guest that struct vmid belongs to
>   * @vmid: The stage-2 VMID information struct
>   */
>  static void update_vmid(struct kvm_vmid *vmid) diff --git 
> a/virt/kvm/arm/vgic/vgic-its.c b/virt/kvm/arm/vgic/vgic-its.c index 
> 98c7360d9fb7..d64569b30b5c 100644
> --- a/virt/kvm/arm/vgic/vgic-its.c
> +++ b/virt/kvm/arm/vgic/vgic-its.c
> @@ -2564,7 +2564,7 @@ static int vgic_its_restore_collection_table(struct 
> vgic_its *its)  }
>  
>  /**
> - * vgic_its_save_tables_v0 - Save the ITS tables into guest ARM
> + * vgic_its_save_tables_v0 - Save the ITS tables into guest RAM
>   * according to v0 ABI
>   */
>  static int vgic_its_save_tables_v0(struct vgic_its *its)
> --
> 2.19.1
>
___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm


Re: [PATCH] KVM: arm64: get rid of var ret and out jump label in kvm_arch_vcpu_ioctl_set_guest_debug()

2020-01-13 Thread linmiaohe
Friendly ping :)

> From: Miaohe Lin 
>
> The var ret and out jump label is not really needed. Clean them up.
> Signed-off-by: Miaohe Lin 
> ---
>  arch/arm64/kvm/guest.c | 11 +++
>  1 file changed, 3 insertions(+), 8 deletions(-)
>
> diff --git a/arch/arm64/kvm/guest.c b/arch/arm64/kvm/guest.c index 
> 2fff06114a8f..3b836c91609e 100644
> --- a/arch/arm64/kvm/guest.c
> +++ b/arch/arm64/kvm/guest.c
> @@ -834,14 +834,10 @@ int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu 
> *vcpu,  int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
>   struct kvm_guest_debug *dbg)
>  {
> - int ret = 0;
> -
>   trace_kvm_set_guest_debug(vcpu, dbg->control);
>  
> - if (dbg->control & ~KVM_GUESTDBG_VALID_MASK) {
> - ret = -EINVAL;
> - goto out;
> - }
> + if (dbg->control & ~KVM_GUESTDBG_VALID_MASK)
> + return -EINVAL;
>  
>   if (dbg->control & KVM_GUESTDBG_ENABLE) {
>   vcpu->guest_debug = dbg->control;
> @@ -856,8 +852,7 @@ int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu 
> *vcpu,
>   vcpu->guest_debug = 0;
>   }
>  
> -out:
> - return ret;
> + return 0;
>  }
>  
>  int kvm_arm_vcpu_arch_set_attr(struct kvm_vcpu *vcpu,
> --
> 2.19.1
>
___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm


Re: [PATCH] KVM: arm: fix missing free_percpu_irq in kvm_timer_hyp_init()

2019-12-06 Thread linmiaohe
Marc Zyngier  wrote:
>On 2019-11-23 02:30, linmiaohe wrote:
>> From: Miaohe Lin 
>>
>> When host_ptimer_irq request irq resource failed, we forget to release 
>> the host_vtimer_irq resource already requested.
>> Fix this missing irq release and other similar scenario.
>
>That's really not a big deal, as nothing but KVM can use the timers anyway, 
>but I guess it doesn't hurt to be correct.

I think It's a good practice to release the never used resources though it may 
be harmless.

>>
>> -out_free_irq:
>> +
>> +out_free_ptimer_irq:
>> +free_percpu_irq(host_ptimer_irq, kvm_get_running_vcpus());
>> +out_disable_gic_state:
>> +if (has_gic)
>> +static_branch_disable(&has_gic_active_state);
>
>Given that we're failing the init of KVM, this is totally superfluous. Also, 
>this state is still valid, no matter what happens (the GIC is not going away 
>from under our feet).
>

Would you like a v2 patch without out_disable_gic_state cleanup ? If so, I 
would send a new one. But if you
think this patch isn't worth to pick up, I would drop it.

Many thanks for your review.

>> +out_free_vtimer_irq:
>>  free_percpu_irq(host_vtimer_irq, kvm_get_running_vcpus());
>> +
___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm


Re: [PATCH] KVM: arm: fix missing free_percpu_irq in kvm_timer_hyp_init()

2019-11-30 Thread linmiaohe
friendly ping ...
> From: Miaohe Lin 
>
> When host_ptimer_irq request irq resource failed, we forget to release the 
> host_vtimer_irq resource already requested.
> Fix this missing irq release and other similar scenario.
>
> Fixes: 9e01dc76be6a ("KVM: arm/arm64: arch_timer: Assign the phys timer on 
> VHE systems")
> Signed-off-by: Miaohe Lin 
> ---
>  virt/kvm/arm/arch_timer.c | 17 -
>  1 file changed, 12 insertions(+), 5 deletions(-)
>
> diff --git a/virt/kvm/arm/arch_timer.c b/virt/kvm/arm/arch_timer.c index 
> f182b2380345..73867f97040c 100644
> --- a/virt/kvm/arm/arch_timer.c
> +++ b/virt/kvm/arm/arch_timer.c
> @@ -935,7 +935,7 @@ int kvm_timer_hyp_init(bool has_gic)
>   kvm_get_running_vcpus());
>   if (err) {
>   kvm_err("kvm_arch_timer: error setting vcpu 
> affinity\n");
> - goto out_free_irq;
> + goto out_free_vtimer_irq;
>   }
>  
>   static_branch_enable(&has_gic_active_state);
> @@ -960,7 +960,7 @@ int kvm_timer_hyp_init(bool has_gic)
>   if (err) {
>   kvm_err("kvm_arch_timer: can't request ptimer interrupt 
> %d (%d)\n",
>   host_ptimer_irq, err);
> - return err;
> + goto out_disable_gic_state;
>   }
>  
>   if (has_gic) {
> @@ -968,7 +968,7 @@ int kvm_timer_hyp_init(bool has_gic)
>   kvm_get_running_vcpus());
>   if (err) {
>   kvm_err("kvm_arch_timer: error setting vcpu 
> affinity\n");
> - goto out_free_irq;
> + goto out_free_ptimer_irq;
>   }
>   }
>  
> @@ -977,15 +977,22 @@ int kvm_timer_hyp_init(bool has_gic)
>   kvm_err("kvm_arch_timer: invalid physical timer IRQ: %d\n",
>   info->physical_irq);
>   err = -ENODEV;
> - goto out_free_irq;
> + goto out_disable_gic_state;
>   }
>  
>   cpuhp_setup_state(CPUHP_AP_KVM_ARM_TIMER_STARTING,
> "kvm/arm/timer:starting", kvm_timer_starting_cpu,
> kvm_timer_dying_cpu);
>   return 0;
> -out_free_irq:
> +
> +out_free_ptimer_irq:
> + free_percpu_irq(host_ptimer_irq, kvm_get_running_vcpus());
> +out_disable_gic_state:
> + if (has_gic)
> + static_branch_disable(&has_gic_active_state);
> +out_free_vtimer_irq:
>   free_percpu_irq(host_vtimer_irq, kvm_get_running_vcpus());
> +
>   return err;
>  }
>  
> --
> 2.19.1

___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm


Re: [PATCH] KVM: arm: get rid of unused arg in cpu_init_hyp_mode()

2019-11-29 Thread linmiaohe
>From: Miaohe Lin 
>
>As arg dummy is not really needed, there's no need to pass NULL when calling 
>cpu_init_hyp_mode(). So clean it up.
>
>Signed-off-by: Miaohe Lin 
>---
> virt/kvm/arm/arm.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
>diff --git a/virt/kvm/arm/arm.c b/virt/kvm/arm/arm.c index 
>86c6aa1cb58e..a5470f1b1a19 100644
>--- a/virt/kvm/arm/arm.c
>+++ b/virt/kvm/arm/arm.c
>@@ -1315,7 +1315,7 @@ long kvm_arch_vm_ioctl(struct file *filp,
>   }
> }
>
>-static void cpu_init_hyp_mode(void *dummy)
>+static void cpu_init_hyp_mode(void)
> {
>   phys_addr_t pgd_ptr;
>   unsigned long hyp_stack_ptr;
>@@ -1349,7 +1349,7 @@ static void cpu_hyp_reinit(void)
>   if (is_kernel_in_hyp_mode())
>   kvm_timer_init_vhe();
>   else
>-  cpu_init_hyp_mode(NULL);
>+  cpu_init_hyp_mode();
> 
>   kvm_arm_init_debug();
> 
friendly ping ...
___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm


[PATCH v3] KVM: vgic: Use wrapper function to lock/unlock all vcpus in kvm_vgic_create()

2019-11-29 Thread linmiaohe
From: Miaohe Lin 

Use wrapper function lock_all_vcpus()/unlock_all_vcpus()
in kvm_vgic_create() to remove duplicated code dealing
with locking and unlocking all vcpus in a vm.

Reviewed-by: Eric Auger 
Reviewed-by: Steven Price 
Signed-off-by: Miaohe Lin 
---
-v2:
Fix some spelling mistake in patch title and commit log.
-v3:
Remove the comment that no longer makes sense.
---
 virt/kvm/arm/vgic/vgic-init.c | 19 ---
 1 file changed, 4 insertions(+), 15 deletions(-)

diff --git a/virt/kvm/arm/vgic/vgic-init.c b/virt/kvm/arm/vgic/vgic-init.c
index b3c5de48064c..22ff73ecac80 100644
--- a/virt/kvm/arm/vgic/vgic-init.c
+++ b/virt/kvm/arm/vgic/vgic-init.c
@@ -70,7 +70,7 @@ void kvm_vgic_early_init(struct kvm *kvm)
  */
 int kvm_vgic_create(struct kvm *kvm, u32 type)
 {
-   int i, vcpu_lock_idx = -1, ret;
+   int i, ret;
struct kvm_vcpu *vcpu;
 
if (irqchip_in_kernel(kvm))
@@ -86,17 +86,9 @@ int kvm_vgic_create(struct kvm *kvm, u32 type)
!kvm_vgic_global_state.can_emulate_gicv2)
return -ENODEV;
 
-   /*
-* Any time a vcpu is run, vcpu_load is called which tries to grab the
-* vcpu->mutex.  By grabbing the vcpu->mutex of all VCPUs we ensure
-* that no other VCPUs are run while we create the vgic.
-*/
ret = -EBUSY;
-   kvm_for_each_vcpu(i, vcpu, kvm) {
-   if (!mutex_trylock(&vcpu->mutex))
-   goto out_unlock;
-   vcpu_lock_idx = i;
-   }
+   if (!lock_all_vcpus(kvm))
+   return ret;
 
kvm_for_each_vcpu(i, vcpu, kvm) {
if (vcpu->arch.has_run_once)
@@ -125,10 +117,7 @@ int kvm_vgic_create(struct kvm *kvm, u32 type)
INIT_LIST_HEAD(&kvm->arch.vgic.rd_regions);
 
 out_unlock:
-   for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) {
-   vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx);
-   mutex_unlock(&vcpu->mutex);
-   }
+   unlock_all_vcpus(kvm);
return ret;
 }
 
-- 
2.19.1

___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm


[PATCH] KVM: arm/arm64: Fix some obsolete comments

2019-11-28 Thread linmiaohe
From: Miaohe Lin 

Fix various comments, including comment typo, and obsolete comments
no longer make sense.
Signed-off-by: Miaohe Lin 
---
 virt/kvm/arm/arch_timer.c| 5 ++---
 virt/kvm/arm/arm.c   | 1 -
 virt/kvm/arm/vgic/vgic-its.c | 2 +-
 3 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/virt/kvm/arm/arch_timer.c b/virt/kvm/arm/arch_timer.c
index 73867f97040c..d8d2f4bec935 100644
--- a/virt/kvm/arm/arch_timer.c
+++ b/virt/kvm/arm/arch_timer.c
@@ -322,9 +322,8 @@ static void timer_emulate(struct arch_timer_context *ctx)
}
 
/*
-* If the timer can fire now, we don't need to have a soft timer
-* scheduled for the future.  If the timer cannot fire at all,
-* then we also don't need a soft timer.
+* If the timer cannot fire at all, we don't need to have a
+* soft timer scheduled for the future.
 */
if (!kvm_timer_irq_can_fire(ctx)) {
soft_timer_cancel(&ctx->hrtimer);
diff --git a/virt/kvm/arm/arm.c b/virt/kvm/arm/arm.c
index 8de4daf25097..7687663ab71b 100644
--- a/virt/kvm/arm/arm.c
+++ b/virt/kvm/arm/arm.c
@@ -525,7 +525,6 @@ static bool need_new_vmid_gen(struct kvm_vmid *vmid)
 
 /**
  * update_vmid - Update the vmid with a valid VMID for the current generation
- * @kvm: The guest that struct vmid belongs to
  * @vmid: The stage-2 VMID information struct
  */
 static void update_vmid(struct kvm_vmid *vmid)
diff --git a/virt/kvm/arm/vgic/vgic-its.c b/virt/kvm/arm/vgic/vgic-its.c
index 98c7360d9fb7..d64569b30b5c 100644
--- a/virt/kvm/arm/vgic/vgic-its.c
+++ b/virt/kvm/arm/vgic/vgic-its.c
@@ -2564,7 +2564,7 @@ static int vgic_its_restore_collection_table(struct 
vgic_its *its)
 }
 
 /**
- * vgic_its_save_tables_v0 - Save the ITS tables into guest ARM
+ * vgic_its_save_tables_v0 - Save the ITS tables into guest RAM
  * according to v0 ABI
  */
 static int vgic_its_save_tables_v0(struct vgic_its *its)
-- 
2.19.1

___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm


Re: [PATCH v2] KVM: vgic: Use wrapper function to lock/unlock all vcpus in kvm_vgic_create()

2019-11-28 Thread linmiaohe
Steven Price wrote:
>>   
>>  if (irqchip_in_kernel(kvm))
>> @@ -92,11 +92,8 @@ int kvm_vgic_create(struct kvm *kvm, u32 type)
>Extra context:
>
>   /*
>* Any time a vcpu is run, vcpu_load is called which tries to grab the
>* vcpu->mutex.  By grabbing the vcpu->mutex of all VCPUs we ensure
>>   * that no other VCPUs are run while we create the vgic.
>>   */
>
>That comment no longer makes sense here - there's a very similar one already 
>in lock_all_vcpus(). With that removed:
>
>Reviewed-by: Steven Price 
>
Many thanks for your review. That comment no longer makes sense as you figured 
out. I will
remove that. Thanks again.

>>  ret = -EBUSY;
>> -kvm_for_each_vcpu(i, vcpu, kvm) {
>> -if (!mutex_trylock(&vcpu->mutex))
>> -goto out_unlock;
>> -vcpu_lock_idx = i;
___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm


[PATCH v2] KVM: vgic: Use wrapper function to lock/unlock all vcpus in kvm_vgic_create()

2019-11-28 Thread linmiaohe
From: Miaohe Lin 

Use wrapper function lock_all_vcpus()/unlock_all_vcpus()
in kvm_vgic_create() to remove duplicated code dealing
with locking and unlocking all vcpus in a vm.

Signed-off-by: Miaohe Lin 
---
-v2:
Fix some spelling mistake in patch title and commit log.
---
 virt/kvm/arm/vgic/vgic-init.c | 14 --
 1 file changed, 4 insertions(+), 10 deletions(-)

diff --git a/virt/kvm/arm/vgic/vgic-init.c b/virt/kvm/arm/vgic/vgic-init.c
index b3c5de48064c..53e3969dfb52 100644
--- a/virt/kvm/arm/vgic/vgic-init.c
+++ b/virt/kvm/arm/vgic/vgic-init.c
@@ -70,7 +70,7 @@ void kvm_vgic_early_init(struct kvm *kvm)
  */
 int kvm_vgic_create(struct kvm *kvm, u32 type)
 {
-   int i, vcpu_lock_idx = -1, ret;
+   int i, ret;
struct kvm_vcpu *vcpu;
 
if (irqchip_in_kernel(kvm))
@@ -92,11 +92,8 @@ int kvm_vgic_create(struct kvm *kvm, u32 type)
 * that no other VCPUs are run while we create the vgic.
 */
ret = -EBUSY;
-   kvm_for_each_vcpu(i, vcpu, kvm) {
-   if (!mutex_trylock(&vcpu->mutex))
-   goto out_unlock;
-   vcpu_lock_idx = i;
-   }
+   if (!lock_all_vcpus(kvm))
+   return ret;
 
kvm_for_each_vcpu(i, vcpu, kvm) {
if (vcpu->arch.has_run_once)
@@ -125,10 +122,7 @@ int kvm_vgic_create(struct kvm *kvm, u32 type)
INIT_LIST_HEAD(&kvm->arch.vgic.rd_regions);
 
 out_unlock:
-   for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) {
-   vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx);
-   mutex_unlock(&vcpu->mutex);
-   }
+   unlock_all_vcpus(kvm);
return ret;
 }
 
-- 
2.19.1

___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm


Re: [PATCH] KVM: vgic: Use warpper function to lock/unlock all vcpus in kvm_vgic_create()

2019-11-28 Thread linmiaohe
Eric wrote:
>> From: Miaohe Lin 
>> 
>> Use warpper function lock_all_vcpus()/unlock_all_vcpus()
> s/warpper/wrapper and also in the title.

Hi, Eric:
Many thanks for your review. I would fix this and send a patch v2.
Thanks again.
___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm


[PATCH] KVM: vgic: Fix potential double free dist->spis in __kvm_vgic_destroy()

2019-11-27 Thread linmiaohe
From: Miaohe Lin 

In kvm_vgic_dist_init() called from kvm_vgic_map_resources(), if
dist->vgic_model is invalid, dist->spis will be freed without set
dist->spis = NULL. And in vgicv2 resources clean up path,
__kvm_vgic_destroy() will be called to free allocated resources.
And dist->spis will be freed again in clean up chain because we
forget to set dist->spis = NULL in kvm_vgic_dist_init() failed
path. So double free would happen.

Signed-off-by: Miaohe Lin 
---
 virt/kvm/arm/vgic/vgic-init.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/virt/kvm/arm/vgic/vgic-init.c b/virt/kvm/arm/vgic/vgic-init.c
index 53e3969dfb52..c17c29beeb72 100644
--- a/virt/kvm/arm/vgic/vgic-init.c
+++ b/virt/kvm/arm/vgic/vgic-init.c
@@ -171,6 +171,7 @@ static int kvm_vgic_dist_init(struct kvm *kvm, unsigned int 
nr_spis)
break;
default:
kfree(dist->spis);
+   dist->spis = NULL;
return -EINVAL;
}
}
-- 
2.19.1

___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm


[PATCH] KVM: arm64: eliminate unnecessary var err and jump label in set_core_reg()

2019-11-27 Thread linmiaohe
From: Miaohe Lin 

The var err and jump label out isn't really needed in
set_core_reg(). Clean them up.
Signed-off-by: Miaohe Lin 
---
 arch/arm64/kvm/guest.c | 13 -
 1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/arch/arm64/kvm/guest.c b/arch/arm64/kvm/guest.c
index 3b836c91609e..88eb6e5399ed 100644
--- a/arch/arm64/kvm/guest.c
+++ b/arch/arm64/kvm/guest.c
@@ -159,7 +159,6 @@ static int set_core_reg(struct kvm_vcpu *vcpu, const struct 
kvm_one_reg *reg)
__uint128_t tmp;
void *valp = &tmp;
u64 off;
-   int err = 0;
 
/* Our ID is an index into the kvm_regs struct. */
off = core_reg_offset_from_id(reg->id);
@@ -173,10 +172,8 @@ static int set_core_reg(struct kvm_vcpu *vcpu, const 
struct kvm_one_reg *reg)
if (KVM_REG_SIZE(reg->id) > sizeof(tmp))
return -EINVAL;
 
-   if (copy_from_user(valp, uaddr, KVM_REG_SIZE(reg->id))) {
-   err = -EFAULT;
-   goto out;
-   }
+   if (copy_from_user(valp, uaddr, KVM_REG_SIZE(reg->id)))
+   return -EFAULT;
 
if (off == KVM_REG_ARM_CORE_REG(regs.pstate)) {
u64 mode = (*(u64 *)valp) & PSR_AA32_MODE_MASK;
@@ -200,14 +197,12 @@ static int set_core_reg(struct kvm_vcpu *vcpu, const 
struct kvm_one_reg *reg)
return -EINVAL;
break;
default:
-   err = -EINVAL;
-   goto out;
+   return -EINVAL;
}
}
 
memcpy((u32 *)regs + off, valp, KVM_REG_SIZE(reg->id));
-out:
-   return err;
+   return 0;
 }
 
 #define vq_word(vq) (((vq) - SVE_VQ_MIN) / 64)
-- 
2.19.1

___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm


[PATCH] KVM: arm64: get rid of var ret and out jump label in kvm_arch_vcpu_ioctl_set_guest_debug()

2019-11-27 Thread linmiaohe
From: Miaohe Lin 

The var ret and out jump label is not really needed. Clean them
up.
Signed-off-by: Miaohe Lin 
---
 arch/arm64/kvm/guest.c | 11 +++
 1 file changed, 3 insertions(+), 8 deletions(-)

diff --git a/arch/arm64/kvm/guest.c b/arch/arm64/kvm/guest.c
index 2fff06114a8f..3b836c91609e 100644
--- a/arch/arm64/kvm/guest.c
+++ b/arch/arm64/kvm/guest.c
@@ -834,14 +834,10 @@ int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
struct kvm_guest_debug *dbg)
 {
-   int ret = 0;
-
trace_kvm_set_guest_debug(vcpu, dbg->control);
 
-   if (dbg->control & ~KVM_GUESTDBG_VALID_MASK) {
-   ret = -EINVAL;
-   goto out;
-   }
+   if (dbg->control & ~KVM_GUESTDBG_VALID_MASK)
+   return -EINVAL;
 
if (dbg->control & KVM_GUESTDBG_ENABLE) {
vcpu->guest_debug = dbg->control;
@@ -856,8 +852,7 @@ int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu 
*vcpu,
vcpu->guest_debug = 0;
}
 
-out:
-   return ret;
+   return 0;
 }
 
 int kvm_arm_vcpu_arch_set_attr(struct kvm_vcpu *vcpu,
-- 
2.19.1

___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm


[PATCH] KVM: vgic: Use warpper function to lock/unlock all vcpus in kvm_vgic_create()

2019-11-27 Thread linmiaohe
From: Miaohe Lin 

Use warpper function lock_all_vcpus()/unlock_all_vcpus()
in kvm_vgic_create() to remove duplicated code dealing
with locking and unlocking all vcpus in a vm.

Signed-off-by: Miaohe Lin 
---
 virt/kvm/arm/vgic/vgic-init.c | 14 --
 1 file changed, 4 insertions(+), 10 deletions(-)

diff --git a/virt/kvm/arm/vgic/vgic-init.c b/virt/kvm/arm/vgic/vgic-init.c
index b3c5de48064c..53e3969dfb52 100644
--- a/virt/kvm/arm/vgic/vgic-init.c
+++ b/virt/kvm/arm/vgic/vgic-init.c
@@ -70,7 +70,7 @@ void kvm_vgic_early_init(struct kvm *kvm)
  */
 int kvm_vgic_create(struct kvm *kvm, u32 type)
 {
-   int i, vcpu_lock_idx = -1, ret;
+   int i, ret;
struct kvm_vcpu *vcpu;
 
if (irqchip_in_kernel(kvm))
@@ -92,11 +92,8 @@ int kvm_vgic_create(struct kvm *kvm, u32 type)
 * that no other VCPUs are run while we create the vgic.
 */
ret = -EBUSY;
-   kvm_for_each_vcpu(i, vcpu, kvm) {
-   if (!mutex_trylock(&vcpu->mutex))
-   goto out_unlock;
-   vcpu_lock_idx = i;
-   }
+   if (!lock_all_vcpus(kvm))
+   return ret;
 
kvm_for_each_vcpu(i, vcpu, kvm) {
if (vcpu->arch.has_run_once)
@@ -125,10 +122,7 @@ int kvm_vgic_create(struct kvm *kvm, u32 type)
INIT_LIST_HEAD(&kvm->arch.vgic.rd_regions);
 
 out_unlock:
-   for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) {
-   vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx);
-   mutex_unlock(&vcpu->mutex);
-   }
+   unlock_all_vcpus(kvm);
return ret;
 }
 
-- 
2.19.1

___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm


[PATCH] KVM: arm: fix missing free_percpu_irq in kvm_timer_hyp_init()

2019-11-22 Thread linmiaohe
From: Miaohe Lin 

When host_ptimer_irq request irq resource failed, we forget
to release the host_vtimer_irq resource already requested.
Fix this missing irq release and other similar scenario.

Fixes: 9e01dc76be6a ("KVM: arm/arm64: arch_timer: Assign the phys timer on VHE 
systems")
Signed-off-by: Miaohe Lin 
---
 virt/kvm/arm/arch_timer.c | 17 -
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/virt/kvm/arm/arch_timer.c b/virt/kvm/arm/arch_timer.c
index f182b2380345..73867f97040c 100644
--- a/virt/kvm/arm/arch_timer.c
+++ b/virt/kvm/arm/arch_timer.c
@@ -935,7 +935,7 @@ int kvm_timer_hyp_init(bool has_gic)
kvm_get_running_vcpus());
if (err) {
kvm_err("kvm_arch_timer: error setting vcpu 
affinity\n");
-   goto out_free_irq;
+   goto out_free_vtimer_irq;
}
 
static_branch_enable(&has_gic_active_state);
@@ -960,7 +960,7 @@ int kvm_timer_hyp_init(bool has_gic)
if (err) {
kvm_err("kvm_arch_timer: can't request ptimer interrupt 
%d (%d)\n",
host_ptimer_irq, err);
-   return err;
+   goto out_disable_gic_state;
}
 
if (has_gic) {
@@ -968,7 +968,7 @@ int kvm_timer_hyp_init(bool has_gic)
kvm_get_running_vcpus());
if (err) {
kvm_err("kvm_arch_timer: error setting vcpu 
affinity\n");
-   goto out_free_irq;
+   goto out_free_ptimer_irq;
}
}
 
@@ -977,15 +977,22 @@ int kvm_timer_hyp_init(bool has_gic)
kvm_err("kvm_arch_timer: invalid physical timer IRQ: %d\n",
info->physical_irq);
err = -ENODEV;
-   goto out_free_irq;
+   goto out_disable_gic_state;
}
 
cpuhp_setup_state(CPUHP_AP_KVM_ARM_TIMER_STARTING,
  "kvm/arm/timer:starting", kvm_timer_starting_cpu,
  kvm_timer_dying_cpu);
return 0;
-out_free_irq:
+
+out_free_ptimer_irq:
+   free_percpu_irq(host_ptimer_irq, kvm_get_running_vcpus());
+out_disable_gic_state:
+   if (has_gic)
+   static_branch_disable(&has_gic_active_state);
+out_free_vtimer_irq:
free_percpu_irq(host_vtimer_irq, kvm_get_running_vcpus());
+
return err;
 }
 
-- 
2.19.1

___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm


[PATCH] KVM: arm: get rid of unused arg in cpu_init_hyp_mode()

2019-11-20 Thread linmiaohe
From: Miaohe Lin 

As arg dummy is not really needed, there's no need to pass
NULL when calling cpu_init_hyp_mode(). So clean it up.

Signed-off-by: Miaohe Lin 
---
 virt/kvm/arm/arm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/virt/kvm/arm/arm.c b/virt/kvm/arm/arm.c
index 86c6aa1cb58e..a5470f1b1a19 100644
--- a/virt/kvm/arm/arm.c
+++ b/virt/kvm/arm/arm.c
@@ -1315,7 +1315,7 @@ long kvm_arch_vm_ioctl(struct file *filp,
}
 }
 
-static void cpu_init_hyp_mode(void *dummy)
+static void cpu_init_hyp_mode(void)
 {
phys_addr_t pgd_ptr;
unsigned long hyp_stack_ptr;
@@ -1349,7 +1349,7 @@ static void cpu_hyp_reinit(void)
if (is_kernel_in_hyp_mode())
kvm_timer_init_vhe();
else
-   cpu_init_hyp_mode(NULL);
+   cpu_init_hyp_mode();
 
kvm_arm_init_debug();
 
-- 
2.19.1

___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm