[PATCH 07/10] KVM: arm64: Use scoped resource management in arm.c

Jonathan Cameron jonathan.cameron at huawei.com
Tue Mar 17 10:53:26 PDT 2026


On Mon, 16 Mar 2026 17:35:28 +0000
Fuad Tabba <tabba at google.com> wrote:

> Migrate manual spin_lock() calls managing mp_state_lock and manual
> mutex_lock() calls managing kvm->arch.config_lock to use the
> guard(spinlock) and guard(mutex) macros.
> 
> This eliminates manual unlock calls on early return paths and
> simplifies the vCPU suspend/resume control flow.
> 
> Change-Id: Ifcd8455d08afa5d00fc200daaa3fb13f6736e6ed
> Signed-off-by: Fuad Tabba <tabba at google.com>
Trivial stuff.
> ---
>  arch/arm64/kvm/arm.c | 53 ++++++++++++++++++++--------------------------------
>  1 file changed, 20 insertions(+), 33 deletions(-)
> 
> diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
> index 410ffd41fd73..017f5bfabe19 100644
> --- a/arch/arm64/kvm/arm.c
> +++ b/arch/arm64/kvm/arm.c

>  
>  /**
> @@ -738,9 +736,8 @@ static void __kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu)
>  
>  void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu)
>  {
> -	spin_lock(&vcpu->arch.mp_state_lock);
> +	guard(spinlock)(&vcpu->arch.mp_state_lock);
>  	__kvm_arm_vcpu_power_off(vcpu);
> -	spin_unlock(&vcpu->arch.mp_state_lock);
>  }
>  
>  bool kvm_arm_vcpu_stopped(struct kvm_vcpu *vcpu)
> @@ -773,7 +770,7 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
>  {
>  	int ret = 0;
>  
> -	spin_lock(&vcpu->arch.mp_state_lock);
> +	guard(spinlock)(&vcpu->arch.mp_state_lock);
>  
>  	switch (mp_state->mp_state) {
>  	case KVM_MP_STATE_RUNNABLE:
> @@ -789,8 +786,6 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
>  		ret = -EINVAL;
		return -EINVAL;
	}

	return 0;

Or even return in the switch legs instead of breaking out.

>  	}
>  
> -	spin_unlock(&vcpu->arch.mp_state_lock);
> -
>  	return ret;
>  }

>  /*
> @@ -944,9 +937,8 @@ int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)
>  			return ret;
>  	}
>  
> -	mutex_lock(&kvm->arch.config_lock);
> +	guard(mutex)(&kvm->arch.config_lock);
>  	set_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &kvm->arch.flags);
> -	mutex_unlock(&kvm->arch.config_lock);
>  
>  	return ret;

Personally I'd make this return 0 whilst you are here so it's obvious this
path only ever returns success.

>  }





More information about the linux-arm-kernel mailing list