Add support for allowing zero SEV ASIDs
Sean Christopherson
maintainerv4 cover Add support for allowing zero SEV ASIDs
Play nice with systems where SEV and SEV-ES are enabled, but all ASIDs have been carved out for SEV-eS, i.e. where actually running SEV guests is impossible.
v4:
- Convert all ASID usage to unsigned integers.
- Clean up sev_asid_new() so that it doesn't needlessly overload its return value.
- Split out the -EBUSY=>-EINVAL change to a separate patch.
v3: https://lore.kernel.org/all/[email protected]
Ashish Kalra (1):
KVM: SVM: Add support for allowing zero SEV ASIDs
Sean Christopherson (3):
KVM: SVM: Set sev->asid in sev_asid_new() instead of overloading the
return
KVM: SVM: Use unsigned integers when dealing with ASIDs
KVM: SVM: Return -EINVAL instead of -EBUSY on attempt to re-init
SEV/SEV-ES
arch/x86/kvm/svm/sev.c | 58 +++++++++++++++++++++++++----------------- arch/x86/kvm/trace.h | 10 ++++---- 2 files changed, 39 insertions(+), 29 deletions(-)
base-commit: 41bccc98fb7931d63d03f326a746ac4d429c1dd3
Signature · 1 line
2.43.0.429.g432eaa2c6b-goog
Sean Christopherson
maintainerPATCH v4 1/4 KVM: SVM: Set sev->asid in sev_asid_new() instead of overloading the return
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.
--- arch/x86/kvm/svm/sev.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
arch/x86/kvm/svm/sev.c +5 -5
@@ -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)
Signature · 1 line
2.43.0.429.g432eaa2c6b-goog
Re: [PATCH v4 1/4] KVM: SVM: Set sev->asid in sev_asid_new() instead of overloading the return
Quoted 8 lines · Sean Christopherson
Explicitly set
sev->asidinsev_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 ofsev_asid_new(), andsev_asid_free()already consumessev->asid, i.e. returning the ASID isn't necessary for flexibility, nor does it provide symmetry between related APIs.Signed-off-by Sean Christopherson <[email protected]>
Quoted 40 lines
--- arch/x86/kvm/svm/sev.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)arch/x86/kvm/svm/sev.c +5
-5@@ -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)
Sean Christopherson
maintainerPATCH v4 2/4 KVM: SVM: Use unsigned integers when dealing with ASIDs
In reply to Sean ChristophersonConvert all local ASID variables and parameters throughout the SEV code from signed integers to unsigned integers. As ASIDs are fundamentally unsigned values, and the global min/max variables are appropriately unsigned integers, too.
Functionally, this is a glorified nop as KVM guarantees min_sev_asid is non-zero, and no CPU supports -1u as the _only_ asid, i.e. the signed vs. unsigned goof won't cause problems in practice.
--- arch/x86/kvm/svm/sev.c | 18 ++++++++++-------- arch/x86/kvm/trace.h | 10 +++++----- 2 files changed, 15 insertions(+), 13 deletions(-)
arch/x86/kvm/svm/sev.c +10 -8
@@ -84,9 +84,10 @@ struct enc_region { };/* Called with the sev_bitmap_lock held, or on shutdown */ -static int sev_flush_asids (int min_asid,int max_asid) +static int sev_flush_asids (unsigned int min_asid,unsigned int max_asid) { -int ret, asid, error =0 ; +int ret, error =0 ; +unsigned int asid;/* Check if there are any ASIDs to reclaim before performing a flush */ asid =find_next_bit (sev_reclaim_asid_bitmap, nr_asids, min_asid);
@@ -116,7 +117,7 @@ static inline bool is_mirroring_enc_context(struct kvm *kvm) }/* Must be called with the sev_bitmap_lock held */ -static bool __sev_recycle_asids (int min_asid,int max_asid) +static bool __sev_recycle_asids (unsigned int min_asid,unsigned int max_asid) {if (sev_flush_asids (min_asid, max_asid))return false;
@@ -143,8 +144,9 @@ static void sev_misc_cg_uncharge(struct kvm_sev_info *sev)static int sev_asid_new (struct kvm_sev_info *sev) { -int asid, min_asid, max_asid, ret; +unsigned int asid, min_asid, max_asid;bool retry = true; +int ret;WARN_ON (sev->misc_cg); sev->misc_cg =get_current_misc_cg ();
@@ -188,7 +190,7 @@ static int sev_asid_new(struct kvm_sev_info *sev)return ret; } -static int sev_get_asid (struct kvm *kvm) +static unsigned int sev_get_asid (struct kvm *kvm) {struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info;
@@ -284,8 +286,8 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)static int sev_bind_asid (struct kvm *kvm,unsigned int handle,int *error) { +unsigned int asid =sev_get_asid (kvm);struct sev_data_activate activate; -int asid =sev_get_asid (kvm);int ret;/* activate ASID on the given handle */
@@ -2312,7 +2314,7 @@ int sev_cpu_init(struct svm_cpu_data *sd)*/ static void sev_flush_encrypted_page (struct kvm_vcpu *vcpu,void *va) { -int asid =to_kvm_svm (vcpu->kvm)->sev_info.asid; +unsigned int asid =to_kvm_svm (vcpu->kvm)->sev_info.asid;/* * Note! The address must be a kernel address, as regular page walk
@@ -2630,7 +2632,7 @@ void sev_es_unmap_ghcb(struct vcpu_svm *svm)void pre_sev_run (struct vcpu_svm *svm,int cpu) {struct svm_cpu_data *sd =per_cpu_ptr (&svm_data, cpu); -int asid =sev_get_asid (svm->vcpu.kvm); +unsigned int asid =sev_get_asid (svm->vcpu.kvm);/* Assign the asid allocated with this SEV guest */ svm->asid = asid;
arch/x86/kvm/trace.h +5 -5
@@ -732,13 +732,13 @@ TRACE_EVENT(kvm_nested_intr_vmexit,* Tracepoint for nested #vmexit because of interrupt pending */ TRACE_EVENT(kvm_invlpga, - TP_PROTO(__u64 rip, int asid, u64 address), + TP_PROTO(__u64 rip, unsigned int asid, u64 address), TP_ARGS(rip, asid, address), TP_STRUCT__entry( - __field( __u64, rip ) - __field( int, asid ) - __field( __u64, address ) + __field( __u64, rip ) + __field( unsigned int, asid ) + __field( __u64, address ) ), TP_fast_assign(
@@ -747,7 +747,7 @@ TRACE_EVENT(kvm_invlpga, __entry->address = address; ), -TP_printk ("rip: 0x%016llx asid: %d address: 0x%016llx" , +TP_printk ("rip: 0x%016llx asid: %u address: 0x%016llx" , __entry->rip, __entry->asid, __entry->address) );
Signature · 1 line
2.43.0.429.g432eaa2c6b-goog
Re: [PATCH v4 2/4] KVM: SVM: Use unsigned integers when dealing with ASIDs
Quoted 10 lines · Sean Christopherson
Convert all local ASID variables and parameters throughout the SEV code from signed integers to unsigned integers. As ASIDs are fundamentally unsigned values, and the global min/max variables are appropriately unsigned integers, too.
Functionally, this is a glorified nop as KVM guarantees
min_sev_asidis non-zero, and no CPU supports -1u as the _only_ asid, i.e. the signed vs. unsigned goof won't cause problems in practice.Signed-off-by Sean Christopherson <[email protected]>
Just one minor comment below, but either way...
Quoted 62 lines
--- arch/x86/kvm/svm/sev.c | 18 ++++++++++-------- arch/x86/kvm/trace.h | 10 +++++----- 2 files changed, 15 insertions(+), 13 deletions(-)arch/x86/kvm/svm/sev.c +8
-6@@ -84,9 +84,10 @@ struct enc_region { };/* Called with the sev_bitmap_lock held, or on shutdown */ -static int sev_flush_asids (int min_asid,int max_asid) +static int sev_flush_asids (unsigned int min_asid,unsigned int max_asid) { -int ret, asid, error =0 ; +int ret, error =0 ; +unsigned int asid;/* Check if there are any ASIDs to reclaim before performing a flush */ asid =find_next_bit (sev_reclaim_asid_bitmap, nr_asids, min_asid);@@ -116,7 +117,7 @@ static inline bool is_mirroring_enc_context(struct kvm *kvm) }/* Must be called with the sev_bitmap_lock held */ -static bool __sev_recycle_asids (int min_asid,int max_asid) +static bool __sev_recycle_asids (unsigned int min_asid,unsigned int max_asid) {if (sev_flush_asids (min_asid, max_asid))return false;@@ -143,8 +144,9 @@ static void sev_misc_cg_uncharge(struct kvm_sev_info *sev)static int sev_asid_new (struct kvm_sev_info *sev) { -int asid, min_asid, max_asid, ret; +unsigned int asid, min_asid, max_asid;bool retry = true; +int ret;WARN_ON (sev->misc_cg); sev->misc_cg =get_current_misc_cg ();@@ -188,7 +190,7 @@ static int sev_asid_new(struct kvm_sev_info *sev)return ret; } -static int sev_get_asid (struct kvm *kvm) +static unsigned int sev_get_asid (struct kvm *kvm) {struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info;@@ -284,8 +286,8 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)static int sev_bind_asid (struct kvm *kvm,unsigned int handle,int *error) { +unsigned int asid =sev_get_asid (kvm);struct sev_data_activate activate; -int asid =sev_get_asid (kvm);int ret;/* activate ASID on the given handle */
arch/x86/kvm/svm/sev.c int sev_cpu_init(struct svm_cpu_data *sd) · line 2317
Quoted 5 lines
arch/x86/kvm/svm/sev.c +1
-1@@ -2312,7 +2314,7 @@ int sev_cpu_init(struct svm_cpu_data *sd)*/ static void sev_flush_encrypted_page (struct kvm_vcpu *vcpu,void *va) { -int asid =to_kvm_svm (vcpu->kvm)->sev_info.asid; +unsigned int asid =to_kvm_svm (vcpu->kvm)->sev_info.asid;
Since you're touching this, you could switch this to:
unsigned int asid = sev_get_asid(vcpu->kvm);
Quoted 43 lines
arch/x86/kvm/svm/sev.c +1
-1/* * Note! The address must be a kernel address, as regular page walk @@ -2630,7 +2632,7 @@ void sev_es_unmap_ghcb(struct vcpu_svm *svm)void pre_sev_run (struct vcpu_svm *svm,int cpu) {struct svm_cpu_data *sd =per_cpu_ptr (&svm_data, cpu); -int asid =sev_get_asid (svm->vcpu.kvm); +unsigned int asid =sev_get_asid (svm->vcpu.kvm);/* Assign the asid allocated with this SEV guest */ svm->asid = asid;arch/x86/kvm/trace.h +5
-5@@ -732,13 +732,13 @@ TRACE_EVENT(kvm_nested_intr_vmexit,* Tracepoint for nested #vmexit because of interrupt pending */ TRACE_EVENT(kvm_invlpga, - TP_PROTO(__u64 rip, int asid, u64 address), + TP_PROTO(__u64 rip, unsigned int asid, u64 address), TP_ARGS(rip, asid, address), TP_STRUCT__entry( - __field( __u64, rip ) - __field( int, asid ) - __field( __u64, address ) + __field( __u64, rip ) + __field( unsigned int, asid ) + __field( __u64, address ) ), TP_fast_assign(@@ -747,7 +747,7 @@ TRACE_EVENT(kvm_invlpga, __entry->address = address; ), -TP_printk ("rip: 0x%016llx asid: %d address: 0x%016llx" , +TP_printk ("rip: 0x%016llx asid: %u address: 0x%016llx" , __entry->rip, __entry->asid, __entry->address) );
Sean Christopherson
maintainerRe: [PATCH v4 2/4] KVM: SVM: Use unsigned integers when dealing with ASIDs
Quoted 11 lines · Tom Lendacky
arch/x86/kvm/svm/sev.c int sev_cpu_init(struct svm_cpu_data *sd) · line 2317
Quoted 6 lines · Sean Christopherson
arch/x86/kvm/svm/sev.c +1
-1@@ -2312,7 +2314,7 @@ int sev_cpu_init(struct svm_cpu_data *sd)*/ static void sev_flush_encrypted_page (struct kvm_vcpu *vcpu,void *va) { -int asid =to_kvm_svm (vcpu->kvm)->sev_info.asid; +unsigned int asid =to_kvm_svm (vcpu->kvm)->sev_info.asid;Since you're touching this, you could switch this to:
unsigned int asid = sev_get_asid(vcpu->kvm);
Ah, good call, will do when applying.
Thanks!
Sean Christopherson
maintainerPATCH v4 3/4 KVM: SVM: Add support for allowing zero SEV ASIDs
In reply to Sean ChristophersonFrom: Ashish Kalra <[email protected]>
Some BIOSes allow the end user to set the minimum SEV ASID value (CPUID 0x8000001F_EDX) to be greater than the maximum number of encrypted guests, or maximum SEV ASID value (CPUID 0x8000001F_ECX) in order to dedicate all the SEV ASIDs to SEV-ES or SEV-SNP.
The SEV support, as coded, does not handle the case where the minimum SEV ASID value can be greater than the maximum SEV ASID value. As a result, the following confusing message is issued:
[ 30.715724] kvm_amd: SEV enabled (ASIDs 1007 - 1006)
Fix the support to properly handle this case.
--- arch/x86/kvm/svm/sev.c | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-)
arch/x86/kvm/svm/sev.c +19 -10
@@ -144,10 +144,21 @@ static void sev_misc_cg_uncharge(struct kvm_sev_info *sev)static int sev_asid_new (struct kvm_sev_info *sev) { -unsigned int asid, min_asid, max_asid; +/* +* SEV-enabled guests must use asid from min_sev_asid to max_sev_asid. +* SEV-ES-enabled guest can use from 1 to min_sev_asid - 1. +* Note: min ASID can end up larger than the max if basic SEV support is +* effectively disabled by disallowing use of ASIDs for SEV guests. +*/ +unsigned int min_asid = sev->es_active ?1 : min_sev_asid; +unsigned int max_asid = sev->es_active ? min_sev_asid -1 : max_sev_asid; +unsigned int asid;bool retry = true;int ret; +if (min_asid > max_asid) +return -ENOTTY ; +WARN_ON (sev->misc_cg); sev->misc_cg =get_current_misc_cg (); ret =sev_misc_cg_try_charge (sev);
@@ -159,12 +170,6 @@ static int sev_asid_new(struct kvm_sev_info *sev)mutex_lock (&sev_bitmap_lock); -/* -* SEV-enabled guests must use asid from min_sev_asid to max_sev_asid. -* SEV-ES-enabled guest can use from 1 to min_sev_asid - 1. -*/ - min_asid = sev->es_active ?1 : min_sev_asid; - max_asid = sev->es_active ? min_sev_asid -1 : max_sev_asid; again: asid =find_next_zero_bit (sev_asid_bitmap, max_asid +1 , min_asid);if (asid > max_asid) {
@@ -2234,8 +2239,10 @@ void __init sev_hardware_setup(void) goto out; } - sev_asid_count = max_sev_asid - min_sev_asid +1 ; -WARN_ON_ONCE (misc_cg_set_capacity (MISC_CG_RES_SEV , sev_asid_count)); +if (min_sev_asid <= max_sev_asid) { + sev_asid_count = max_sev_asid - min_sev_asid +1 ; +WARN_ON_ONCE (misc_cg_set_capacity (MISC_CG_RES_SEV , sev_asid_count)); + } sev_supported = true;/* SEV-ES support requested? */
@@ -2266,7 +2273,9 @@ void __init sev_hardware_setup(void) out:if (boot_cpu_has (X86_FEATURE_SEV ))pr_info ("SEV %s (ASIDs %u - %u)\n" , - sev_supported ?"enabled" :"disabled" , + sev_supported ? min_sev_asid <= max_sev_asid ?"enabled" : +"unusable" : +"disabled" , min_sev_asid, max_sev_asid);if (boot_cpu_has (X86_FEATURE_SEV_ES )) pr_info("SEV-ES %s (ASIDs %u - %u)\n",
Signature · 1 line
2.43.0.429.g432eaa2c6b-goog
Sean Christopherson
maintainerPATCH v4 4/4 KVM: SVM: Return -EINVAL instead of -EBUSY on attempt to re-init SEV/SEV-ES
In reply to Sean ChristophersonReturn -EINVAL instead of -EBUSY if userspace attempts KVM_SEV{,ES}_INIT on a VM that already has SEV active. Returning -EBUSY is nonsencial as it's impossible to deactivate SEV without destroying the VM, i.e. the VM isn't "busy" in any sane sense of the word, and the odds of any userspace wanting exactly -EBUSY on a userspace bug are minuscule.
--- arch/x86/kvm/svm/sev.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
arch/x86/kvm/svm/sev.c +1 -2
@@ -259,9 +259,8 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)if (kvm->created_vcpus)return -EINVAL ; - ret = -EBUSY ;if (unlikely (sev->active)) -return ret; +return -EINVAL ; sev->active = true; sev->es_active = argp->id ==KVM_SEV_ES_INIT ;
Signature · 1 line
2.43.0.429.g432eaa2c6b-goog
Re: [PATCH v4 4/4] KVM: SVM: Return -EINVAL instead of -EBUSY on attempt to re-init SEV/SEV-ES
Quoted 7 lines · Sean Christopherson
Return
-EINVALinstead of-EBUSYif userspace attempts KVM_SEV{,ES}_INIT on a VM that already has SEV active. Returning-EBUSYis nonsencial as it's impossible to deactivate SEV without destroying the VM, i.e. the VM isn't "busy" in any sane sense of the word, and the odds of any userspace wanting exactly-EBUSYon a userspace bug are minuscule.Signed-off-by Sean Christopherson <[email protected]>
Quoted 19 lines
--- arch/x86/kvm/svm/sev.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)arch/x86/kvm/svm/sev.c +1
-2@@ -259,9 +259,8 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)if (kvm->created_vcpus)return -EINVAL ; - ret = -EBUSY ;if (unlikely (sev->active)) -return ret; +return -EINVAL ; sev->active = true; sev->es_active = argp->id ==KVM_SEV_ES_INIT ;
Quoted 11 lines · Sean Christopherson
Play nice with systems where SEV and SEV-ES are enabled, but all ASIDs have been carved out for SEV-eS, i.e. where actually running SEV guests is impossible.
v4:
- Convert all ASID usage to unsigned integers.
- Clean upsev_asid_new()so that it doesn't needlessly overload its return value.
- Split out the -EBUSY=>-EINVAL change to a separate patch.[...]
Applied to kvm-x86 svm, thanks!
[1/4] KVM: SVM: Set sev->asid in sev_asid_new() instead of overloading the return
https://github.com/kvm-x86/linux/commit/cc4ce37bed85 [2/4] KVM: SVM: Use unsigned integers when dealing with ASIDs https://github.com/kvm-x86/linux/commit/466eec4a22a7 [3/4] KVM: SVM: Add support for allowing zero SEV ASIDs https://github.com/kvm-x86/linux/commit/0aa6b90ef9d7 [4/4] KVM: SVM: Return -EINVAL instead of -EBUSY on attempt to re-init SEV/SEV-ES https://github.com/kvm-x86/linux/commit/fdd58834d132