Subject: Re: [PATCH 05/10] KVM: arm64: Use guard(hyp_spinlock) in pkvm.c

From: Jonathan Cameron <[email-masked]>
Date: Tue, 17 Mar 2026 17:47:36 +0000

On Mon, 16 Mar 2026 17:35:26 +0000
Fuad Tabba <[email-masked]> wrote:

> Migrate manual hyp_spin_lock() and hyp_spin_unlock() calls managing
> the global vm_table_lock to use the guard(hyp_spinlock) macro.
> 
> This significantly cleans up validation and error paths during VM
> creation and manipulation by eliminating the need for goto labels
> and manual unlock procedures on early returns.
> 
> Change-Id: I894df69b3cfe053a77dd660dfb70c95640c6d70c
> Signed-off-by: Fuad Tabba <[email-masked]>
> ---

>  
>  struct pkvm_hyp_vm *get_np_pkvm_hyp_vm(pkvm_handle_t handle)
> @@ -613,9 +605,8 @@ static int insert_vm_table_entry(pkvm_handle_t handle,
>  {
>  	int ret;
>  
> -	hyp_spin_lock(&vm_table_lock);
> +	guard(hyp_spinlock)(&vm_table_lock);
>  	ret = __insert_vm_table_entry(handle, hyp_vm);
> -	hyp_spin_unlock(&vm_table_lock);
>  
>  	return ret;

	return __insert_vm_table_entry();

>  }

> @@ -815,35 +804,35 @@ int __pkvm_init_vcpu(pkvm_handle_t handle, struct kvm_vcpu *host_vcpu,
>  	if (!hyp_vcpu)
>  		return -ENOMEM;
>  
> -	hyp_spin_lock(&vm_table_lock);
> +	scoped_guard(hyp_spinlock, &vm_table_lock) {
> +		hyp_vm = get_vm_by_handle(handle);
> +		if (!hyp_vm) {
> +			ret = -ENOENT;
> +			goto err_unmap;

As in earlier patch.  I'd not mix gotos and guard()s.

> +		}
>  
> -	hyp_vm = get_vm_by_handle(handle);
> -	if (!hyp_vm) {
> -		ret = -ENOENT;
> -		goto unlock;
> +		ret = init_pkvm_hyp_vcpu(hyp_vcpu, hyp_vm, host_vcpu);
> +		if (ret)
> +			goto err_unmap;
> +
> +		idx = hyp_vcpu->vcpu.vcpu_idx;
> +		if (idx >= hyp_vm->kvm.created_vcpus) {
> +			ret = -EINVAL;
> +			goto err_unmap;
> +		}
> +
> +		if (hyp_vm->vcpus[idx]) {
> +			ret = -EINVAL;
> +			goto err_unmap;
> +		}
> +
> +		hyp_vm->vcpus[idx] = hyp_vcpu;
> +
> +		return 0;
>  	}
>  
> -	ret = init_pkvm_hyp_vcpu(hyp_vcpu, hyp_vm, host_vcpu);
> -	if (ret)
> -		goto unlock;
> -
> -	idx = hyp_vcpu->vcpu.vcpu_idx;
> -	if (idx >= hyp_vm->kvm.created_vcpus) {
> -		ret = -EINVAL;
> -		goto unlock;
> -	}
> -
> -	if (hyp_vm->vcpus[idx]) {
> -		ret = -EINVAL;
> -		goto unlock;
> -	}
> -
> -	hyp_vm->vcpus[idx] = hyp_vcpu;
> -unlock:
> -	hyp_spin_unlock(&vm_table_lock);
> -
> -	if (ret)
> -		unmap_donated_memory(hyp_vcpu, sizeof(*hyp_vcpu));
> +err_unmap:
> +	unmap_donated_memory(hyp_vcpu, sizeof(*hyp_vcpu));
>  	return ret;
>  }
>  
>