[PATCH 4/6] KVM: SEV: use mutex guard in sev_mem_enc_unregister_region()

Carlos López posted 6 patches 1 month, 3 weeks ago
There is a newer version of this series
[PATCH 4/6] KVM: SEV: use mutex guard in sev_mem_enc_unregister_region()
Posted by Carlos López 1 month, 3 weeks ago
Simplify the error paths in sev_mem_enc_unregister_region() 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 | 20 +++++---------------
 1 file changed, 5 insertions(+), 15 deletions(-)

diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
index 253f2ae24bfc..47ff5267ab01 100644
--- a/arch/x86/kvm/svm/sev.c
+++ b/arch/x86/kvm/svm/sev.c
@@ -2746,35 +2746,25 @@ int sev_mem_enc_unregister_region(struct kvm *kvm,
 				  struct kvm_enc_region *range)
 {
 	struct enc_region *region;
-	int ret;
 
 	/* If kvm is mirroring encryption context it isn't responsible for it */
 	if (is_mirroring_enc_context(kvm))
 		return -EINVAL;
 
-	mutex_lock(&kvm->lock);
+	guard(mutex)(&kvm->lock);
 
-	if (!sev_guest(kvm)) {
-		ret = -ENOTTY;
-		goto failed;
-	}
+	if (!sev_guest(kvm))
+		return -ENOTTY;
 
 	region = find_enc_region(kvm, range);
-	if (!region) {
-		ret = -EINVAL;
-		goto failed;
-	}
+	if (!region)
+		return -EINVAL;
 
 	sev_writeback_caches(kvm);
 
 	__unregister_enc_region_locked(kvm, region);
 
-	mutex_unlock(&kvm->lock);
 	return 0;
-
-failed:
-	mutex_unlock(&kvm->lock);
-	return ret;
 }
 
 int sev_vm_copy_enc_context_from(struct kvm *kvm, unsigned int source_fd)
-- 
2.51.0

Re: [PATCH 4/6] KVM: SEV: use mutex guard in sev_mem_enc_unregister_region()
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_unregister_region() 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 | 20 +++++---------------
>   1 file changed, 5 insertions(+), 15 deletions(-)
>
> diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
> index 253f2ae24bfc..47ff5267ab01 100644
> --- a/arch/x86/kvm/svm/sev.c
> +++ b/arch/x86/kvm/svm/sev.c
> @@ -2746,35 +2746,25 @@ int sev_mem_enc_unregister_region(struct kvm *kvm,
>   				  struct kvm_enc_region *range)
>   {
>   	struct enc_region *region;
> -	int ret;
>   
>   	/* If kvm is mirroring encryption context it isn't responsible for it */
>   	if (is_mirroring_enc_context(kvm))
>   		return -EINVAL;
>   
> -	mutex_lock(&kvm->lock);
> +	guard(mutex)(&kvm->lock);
>   
> -	if (!sev_guest(kvm)) {
> -		ret = -ENOTTY;
> -		goto failed;
> -	}
> +	if (!sev_guest(kvm))
> +		return -ENOTTY;
>   
>   	region = find_enc_region(kvm, range);
> -	if (!region) {
> -		ret = -EINVAL;
> -		goto failed;
> -	}
> +	if (!region)
> +		return -EINVAL;
>   
>   	sev_writeback_caches(kvm);
>   
>   	__unregister_enc_region_locked(kvm, region);
>   
> -	mutex_unlock(&kvm->lock);
>   	return 0;
> -
> -failed:
> -	mutex_unlock(&kvm->lock);
> -	return ret;
>   }
>   
>   int sev_vm_copy_enc_context_from(struct kvm *kvm, unsigned int source_fd)