[v4,1/4] KVM: SVM: Set sev->asid in sev_asid_new() instead of overloading the return

Message ID 20240131235609.4161407-2-seanjc@google.com
State New
Headers
Series Add support for allowing zero SEV ASIDs |

Commit Message

Sean Christopherson Jan. 31, 2024, 11:56 p.m. UTC
  Explicitly set sev->asid in sev_asid_new() when a new ASID is successfully
allocated, and return '0' to indicate success instead of overloading the
return value to multiplex the ASID with error codes.  There is exactly one
caller of sev_asid_new(), and sev_asid_free() already consumes sev->asid,
i.e. returning the ASID isn't necessary for flexibility, nor does it
provide symmetry between related APIs.

Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 arch/x86/kvm/svm/sev.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
  

Comments

Tom Lendacky Feb. 1, 2024, 4:20 p.m. UTC | #1
On 1/31/24 17:56, Sean Christopherson wrote:
> Explicitly set sev->asid in sev_asid_new() when a new ASID is successfully
> allocated, and return '0' to indicate success instead of overloading the
> return value to multiplex the ASID with error codes.  There is exactly one
> caller of sev_asid_new(), and sev_asid_free() already consumes sev->asid,
> i.e. returning the ASID isn't necessary for flexibility, nor does it
> provide symmetry between related APIs.
> 
> Signed-off-by: Sean Christopherson <seanjc@google.com>

Reviewed-by: Tom Lendacky <thomas.lendacky@amd.com>

> ---
>   arch/x86/kvm/svm/sev.c | 10 +++++-----
>   1 file changed, 5 insertions(+), 5 deletions(-)
> 
> diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
> index f760106c31f8..7c000088bca6 100644
> --- a/arch/x86/kvm/svm/sev.c
> +++ b/arch/x86/kvm/svm/sev.c
> @@ -179,7 +179,8 @@ static int sev_asid_new(struct kvm_sev_info *sev)
>   
>   	mutex_unlock(&sev_bitmap_lock);
>   
> -	return asid;
> +	sev->asid = asid;
> +	return 0;
>   e_uncharge:
>   	sev_misc_cg_uncharge(sev);
>   	put_misc_cg(sev->misc_cg);
> @@ -246,7 +247,7 @@ static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)
>   static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
>   {
>   	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
> -	int asid, ret;
> +	int ret;
>   
>   	if (kvm->created_vcpus)
>   		return -EINVAL;
> @@ -257,10 +258,9 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
>   
>   	sev->active = true;
>   	sev->es_active = argp->id == KVM_SEV_ES_INIT;
> -	asid = sev_asid_new(sev);
> -	if (asid < 0)
> +	ret = sev_asid_new(sev);
> +	if (ret)
>   		goto e_no_asid;
> -	sev->asid = asid;
>   
>   	ret = sev_platform_init(&argp->error);
>   	if (ret)
  

Patch

diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
index f760106c31f8..7c000088bca6 100644
--- a/arch/x86/kvm/svm/sev.c
+++ b/arch/x86/kvm/svm/sev.c
@@ -179,7 +179,8 @@  static int sev_asid_new(struct kvm_sev_info *sev)
 
 	mutex_unlock(&sev_bitmap_lock);
 
-	return asid;
+	sev->asid = asid;
+	return 0;
 e_uncharge:
 	sev_misc_cg_uncharge(sev);
 	put_misc_cg(sev->misc_cg);
@@ -246,7 +247,7 @@  static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)
 static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
 {
 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
-	int asid, ret;
+	int ret;
 
 	if (kvm->created_vcpus)
 		return -EINVAL;
@@ -257,10 +258,9 @@  static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
 
 	sev->active = true;
 	sev->es_active = argp->id == KVM_SEV_ES_INIT;
-	asid = sev_asid_new(sev);
-	if (asid < 0)
+	ret = sev_asid_new(sev);
+	if (ret)
 		goto e_no_asid;
-	sev->asid = asid;
 
 	ret = sev_platform_init(&argp->error);
 	if (ret)