[PATCH 2/6] KVM: SEV: use mutex guard in sev_mem_enc_ioctl()

Carlos López posted 6 patches 1 month, 3 weeks ago
There is a newer version of this series
[PATCH 2/6] KVM: SEV: use mutex guard in sev_mem_enc_ioctl()
Posted by Carlos López 1 month, 3 weeks ago
Simplify the error paths in sev_mem_enc_ioctl() by using a mutex guard,
allowing early return instead of using gotos.

Signed-off-by: Carlos López <clopez@suse.de>
---
 arch/x86/kvm/svm/sev.c | 25 ++++++++-----------------
 1 file changed, 8 insertions(+), 17 deletions(-)

diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
index 1b325ae61d15..0ee1b77aeec5 100644
--- a/arch/x86/kvm/svm/sev.c
+++ b/arch/x86/kvm/svm/sev.c
@@ -2575,30 +2575,24 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp)
 	if (copy_from_user(&sev_cmd, argp, sizeof(struct kvm_sev_cmd)))
 		return -EFAULT;
 
-	mutex_lock(&kvm->lock);
+	guard(mutex)(&kvm->lock);
 
 	/* Only the enc_context_owner handles some memory enc operations. */
 	if (is_mirroring_enc_context(kvm) &&
-	    !is_cmd_allowed_from_mirror(sev_cmd.id)) {
-		r = -EINVAL;
-		goto out;
-	}
+	    !is_cmd_allowed_from_mirror(sev_cmd.id))
+		return -EINVAL;
 
 	/*
 	 * Once KVM_SEV_INIT2 initializes a KVM instance as an SNP guest, only
 	 * allow the use of SNP-specific commands.
 	 */
-	if (sev_snp_guest(kvm) && sev_cmd.id < KVM_SEV_SNP_LAUNCH_START) {
-		r = -EPERM;
-		goto out;
-	}
+	if (sev_snp_guest(kvm) && sev_cmd.id < KVM_SEV_SNP_LAUNCH_START)
+		return -EPERM;
 
 	switch (sev_cmd.id) {
 	case KVM_SEV_ES_INIT:
-		if (!sev_es_enabled) {
-			r = -ENOTTY;
-			goto out;
-		}
+		if (!sev_es_enabled)
+			return -ENOTTY;
 		fallthrough;
 	case KVM_SEV_INIT:
 		r = sev_guest_init(kvm, &sev_cmd);
@@ -2667,15 +2661,12 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp)
 		r = snp_launch_finish(kvm, &sev_cmd);
 		break;
 	default:
-		r = -EINVAL;
-		goto out;
+		return -EINVAL;
 	}
 
 	if (copy_to_user(argp, &sev_cmd, sizeof(struct kvm_sev_cmd)))
 		r = -EFAULT;
 
-out:
-	mutex_unlock(&kvm->lock);
 	return r;
 }
 
-- 
2.51.0

Re: [PATCH 2/6] KVM: SEV: use mutex guard in sev_mem_enc_ioctl()
Posted by Gupta, Pankaj 1 month, 1 week ago
On 12/19/2025 12:41 PM, Carlos López wrote:
> Simplify the error paths in sev_mem_enc_ioctl() by using a mutex guard,
> allowing early return instead of using gotos.
>
> Signed-off-by: Carlos López <clopez@suse.de>

Reviewed-by: Pankaj Gupta <pankaj.gupta@amd.com>

> ---
>   arch/x86/kvm/svm/sev.c | 25 ++++++++-----------------
>   1 file changed, 8 insertions(+), 17 deletions(-)
>
> diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
> index 1b325ae61d15..0ee1b77aeec5 100644
> --- a/arch/x86/kvm/svm/sev.c
> +++ b/arch/x86/kvm/svm/sev.c
> @@ -2575,30 +2575,24 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp)
>   	if (copy_from_user(&sev_cmd, argp, sizeof(struct kvm_sev_cmd)))
>   		return -EFAULT;
>   
> -	mutex_lock(&kvm->lock);
> +	guard(mutex)(&kvm->lock);
>   
>   	/* Only the enc_context_owner handles some memory enc operations. */
>   	if (is_mirroring_enc_context(kvm) &&
> -	    !is_cmd_allowed_from_mirror(sev_cmd.id)) {
> -		r = -EINVAL;
> -		goto out;
> -	}
> +	    !is_cmd_allowed_from_mirror(sev_cmd.id))
> +		return -EINVAL;
>   
>   	/*
>   	 * Once KVM_SEV_INIT2 initializes a KVM instance as an SNP guest, only
>   	 * allow the use of SNP-specific commands.
>   	 */
> -	if (sev_snp_guest(kvm) && sev_cmd.id < KVM_SEV_SNP_LAUNCH_START) {
> -		r = -EPERM;
> -		goto out;
> -	}
> +	if (sev_snp_guest(kvm) && sev_cmd.id < KVM_SEV_SNP_LAUNCH_START)
> +		return -EPERM;
>   
>   	switch (sev_cmd.id) {
>   	case KVM_SEV_ES_INIT:
> -		if (!sev_es_enabled) {
> -			r = -ENOTTY;
> -			goto out;
> -		}
> +		if (!sev_es_enabled)
> +			return -ENOTTY;
>   		fallthrough;
>   	case KVM_SEV_INIT:
>   		r = sev_guest_init(kvm, &sev_cmd);
> @@ -2667,15 +2661,12 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp)
>   		r = snp_launch_finish(kvm, &sev_cmd);
>   		break;
>   	default:
> -		r = -EINVAL;
> -		goto out;
> +		return -EINVAL;
>   	}
>   
>   	if (copy_to_user(argp, &sev_cmd, sizeof(struct kvm_sev_cmd)))
>   		r = -EFAULT;
>   
> -out:
> -	mutex_unlock(&kvm->lock);
>   	return r;
>   }
>