Re: [RFC PATCH 1/5] KVM: arm64: Replace power_off with mp_state in struct kvm_vcpu_arch

2021-07-01 Thread Fuad Tabba
Hi Jean-Philippe,

On Tue, Jun 8, 2021 at 4:54 PM Jean-Philippe Brucker
 wrote:
>
> In order to add a new "suspend" power state, replace power_off with
> mp_state in struct kvm_vcpu_arch. Factor the vcpu_off() function while
> we're here.
>
> No functional change intended.
>
> Signed-off-by: Jean-Philippe Brucker 
> ---
>  arch/arm64/include/asm/kvm_host.h |  6 --
>  arch/arm64/kvm/arm.c  | 29 +++--
>  arch/arm64/kvm/psci.c | 19 ++-
>  3 files changed, 25 insertions(+), 29 deletions(-)
>
> diff --git a/arch/arm64/include/asm/kvm_host.h 
> b/arch/arm64/include/asm/kvm_host.h
> index 7cd7d5c8c4bc..55a04f4d5919 100644
> --- a/arch/arm64/include/asm/kvm_host.h
> +++ b/arch/arm64/include/asm/kvm_host.h
> @@ -340,8 +340,8 @@ struct kvm_vcpu_arch {
> u32 mdscr_el1;
> } guest_debug_preserved;
>
> -   /* vcpu power-off state */
> -   bool power_off;
> +   /* vcpu power state (runnable, stopped, halted) */

Should the comment be, for clarity, something along the lines of
KVM_MP_STATE_(STOPPED, RUNNABLE, HALTED), or maybe "a valid struct
kvm_mp_state", if you think other states might be added in the future?

Thanks,
/fuad


> +   u32 mp_state;
>
> /* Don't run the guest (internal implementation need) */
> bool pause;
> @@ -720,6 +720,8 @@ int kvm_arm_vcpu_arch_get_attr(struct kvm_vcpu *vcpu,
>struct kvm_device_attr *attr);
>  int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu,
>struct kvm_device_attr *attr);
> +void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu);
> +bool kvm_arm_vcpu_is_off(struct kvm_vcpu *vcpu);
>
>  /* Guest/host FPSIMD coordination helpers */
>  int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu);
> diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
> index e720148232a0..bcc24adb9c0a 100644
> --- a/arch/arm64/kvm/arm.c
> +++ b/arch/arm64/kvm/arm.c
> @@ -435,21 +435,22 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
> vcpu->cpu = -1;
>  }
>
> -static void vcpu_power_off(struct kvm_vcpu *vcpu)
> +void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu)
>  {
> -   vcpu->arch.power_off = true;
> +   vcpu->arch.mp_state = KVM_MP_STATE_STOPPED;
> kvm_make_request(KVM_REQ_SLEEP, vcpu);
> kvm_vcpu_kick(vcpu);
>  }
>
> +bool kvm_arm_vcpu_is_off(struct kvm_vcpu *vcpu)
> +{
> +   return vcpu->arch.mp_state == KVM_MP_STATE_STOPPED;
> +}
> +
>  int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
> struct kvm_mp_state *mp_state)
>  {
> -   if (vcpu->arch.power_off)
> -   mp_state->mp_state = KVM_MP_STATE_STOPPED;
> -   else
> -   mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
> -
> +   mp_state->mp_state = vcpu->arch.mp_state;
> return 0;
>  }
>
> @@ -460,10 +461,10 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu 
> *vcpu,
>
> switch (mp_state->mp_state) {
> case KVM_MP_STATE_RUNNABLE:
> -   vcpu->arch.power_off = false;
> +   vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
> break;
> case KVM_MP_STATE_STOPPED:
> -   vcpu_power_off(vcpu);
> +   kvm_arm_vcpu_power_off(vcpu);
> break;
> default:
> ret = -EINVAL;
> @@ -483,7 +484,7 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
>  {
> bool irq_lines = *vcpu_hcr(v) & (HCR_VI | HCR_VF);
> return ((irq_lines || kvm_vgic_vcpu_pending_irq(v))
> -   && !v->arch.power_off && !v->arch.pause);
> +   && !kvm_arm_vcpu_is_off(v) && !v->arch.pause);
>  }
>
>  bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
> @@ -643,10 +644,10 @@ static void vcpu_req_sleep(struct kvm_vcpu *vcpu)
> struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu);
>
> rcuwait_wait_event(wait,
> -  (!vcpu->arch.power_off) &&(!vcpu->arch.pause),
> +  !kvm_arm_vcpu_is_off(vcpu) && !vcpu->arch.pause,
>TASK_INTERRUPTIBLE);
>
> -   if (vcpu->arch.power_off || vcpu->arch.pause) {
> +   if (kvm_arm_vcpu_is_off(vcpu) || vcpu->arch.pause) {
> /* Awaken to handle a signal, request we sleep again later. */
> kvm_make_request(KVM_REQ_SLEEP, vcpu);
> }
> @@ -1087,9 +1088,9 @@ static int kvm_arch_vcpu_ioctl_vcpu_init(struct 
> kvm_vcpu *vcpu,
>  * Handle the "start in power-off" case.
>  */
> if (test_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features))
> -   vcpu_power_off(vcpu);
> +   kvm_arm_vcpu_power_off(vcpu);
> else
> -   vcpu->arch.power_off = false;
> +   vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
>
> return 0;
>  }
> diff --git a/arch/arm64/kvm/psci.c b/arch/arm64/kvm/psci.c
> index db4056ecccfd..24b4a2265

Re: [RFC PATCH 1/5] KVM: arm64: Replace power_off with mp_state in struct kvm_vcpu_arch

2021-06-10 Thread Jonathan Cameron
On Tue,  8 Jun 2021 17:48:02 +0200
Jean-Philippe Brucker  wrote:

> In order to add a new "suspend" power state, replace power_off with
> mp_state in struct kvm_vcpu_arch. Factor the vcpu_off() function while
> we're here.

Hi Jean-Phillipe,

2 changes, so if you do end up doing a v2 I'd prefer the
factor out of kvm_arm_vcpu_power_off() + possibly introduced
kvm_arm_vcpu_is_off() using the old boolean.
Then the change in how you track the state will be a bit easier to
pick out.

> 
> No functional change intended.
> 
> Signed-off-by: Jean-Philippe Brucker 
> ---
>  arch/arm64/include/asm/kvm_host.h |  6 --
>  arch/arm64/kvm/arm.c  | 29 +++--
>  arch/arm64/kvm/psci.c | 19 ++-
>  3 files changed, 25 insertions(+), 29 deletions(-)
> 
> diff --git a/arch/arm64/include/asm/kvm_host.h 
> b/arch/arm64/include/asm/kvm_host.h
> index 7cd7d5c8c4bc..55a04f4d5919 100644
> --- a/arch/arm64/include/asm/kvm_host.h
> +++ b/arch/arm64/include/asm/kvm_host.h
> @@ -340,8 +340,8 @@ struct kvm_vcpu_arch {
>   u32 mdscr_el1;
>   } guest_debug_preserved;
>  
> - /* vcpu power-off state */
> - bool power_off;
> + /* vcpu power state (runnable, stopped, halted) */
> + u32 mp_state;
>  
>   /* Don't run the guest (internal implementation need) */
>   bool pause;
> @@ -720,6 +720,8 @@ int kvm_arm_vcpu_arch_get_attr(struct kvm_vcpu *vcpu,
>  struct kvm_device_attr *attr);
>  int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu,
>  struct kvm_device_attr *attr);
> +void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu);
> +bool kvm_arm_vcpu_is_off(struct kvm_vcpu *vcpu);
>  
>  /* Guest/host FPSIMD coordination helpers */
>  int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu);
> diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
> index e720148232a0..bcc24adb9c0a 100644
> --- a/arch/arm64/kvm/arm.c
> +++ b/arch/arm64/kvm/arm.c
> @@ -435,21 +435,22 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
>   vcpu->cpu = -1;
>  }
>  
> -static void vcpu_power_off(struct kvm_vcpu *vcpu)
> +void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu)
>  {
> - vcpu->arch.power_off = true;
> + vcpu->arch.mp_state = KVM_MP_STATE_STOPPED;
>   kvm_make_request(KVM_REQ_SLEEP, vcpu);
>   kvm_vcpu_kick(vcpu);
>  }
>  
> +bool kvm_arm_vcpu_is_off(struct kvm_vcpu *vcpu)
> +{
> + return vcpu->arch.mp_state == KVM_MP_STATE_STOPPED;
> +}
> +
>  int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
>   struct kvm_mp_state *mp_state)
>  {
> - if (vcpu->arch.power_off)
> - mp_state->mp_state = KVM_MP_STATE_STOPPED;
> - else
> - mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
> -
> + mp_state->mp_state = vcpu->arch.mp_state;

Nice to have a blank line here.

>   return 0;
>  }
>  
> @@ -460,10 +461,10 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu 
> *vcpu,
>  
>   switch (mp_state->mp_state) {
>   case KVM_MP_STATE_RUNNABLE:
> - vcpu->arch.power_off = false;
> + vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
>   break;
>   case KVM_MP_STATE_STOPPED:
> - vcpu_power_off(vcpu);
> + kvm_arm_vcpu_power_off(vcpu);
>   break;
>   default:
>   ret = -EINVAL;
> @@ -483,7 +484,7 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
>  {
>   bool irq_lines = *vcpu_hcr(v) & (HCR_VI | HCR_VF);
>   return ((irq_lines || kvm_vgic_vcpu_pending_irq(v))
> - && !v->arch.power_off && !v->arch.pause);
> + && !kvm_arm_vcpu_is_off(v) && !v->arch.pause);
>  }
>  
>  bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
> @@ -643,10 +644,10 @@ static void vcpu_req_sleep(struct kvm_vcpu *vcpu)
>   struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu);
>  
>   rcuwait_wait_event(wait,
> -(!vcpu->arch.power_off) &&(!vcpu->arch.pause),
> +!kvm_arm_vcpu_is_off(vcpu) && !vcpu->arch.pause,
>  TASK_INTERRUPTIBLE);
>  
> - if (vcpu->arch.power_off || vcpu->arch.pause) {
> + if (kvm_arm_vcpu_is_off(vcpu) || vcpu->arch.pause) {
>   /* Awaken to handle a signal, request we sleep again later. */
>   kvm_make_request(KVM_REQ_SLEEP, vcpu);
>   }
> @@ -1087,9 +1088,9 @@ static int kvm_arch_vcpu_ioctl_vcpu_init(struct 
> kvm_vcpu *vcpu,
>* Handle the "start in power-off" case.
>*/
>   if (test_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features))
> - vcpu_power_off(vcpu);
> + kvm_arm_vcpu_power_off(vcpu);
>   else
> - vcpu->arch.power_off = false;
> + vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
>  
>   return 0;
>  }
> diff --git a/arch/arm64/kvm/psci.c b/arch/arm64/kvm/psci.c
> index db4056ecccfd..24b4a2265dbd 100644
> --- a/arch/arm64/k