kvm: cpuid: fix cpuid nent field
Subsystems (5)
PATCH v2 1/4 kvm: cpuid: adjust the returned nent field of kvm_cpuid2 for KVM_GET_SUPPORTED_CPUID and KVM_GET_EMULATED_CPUID
In reply to Emanuele Giuseppe EspositoCalling the kvm KVM_GET_[SUPPORTED/EMULATED]_CPUID ioctl requires a nent field inside the kvm_cpuid2 struct to be big enough to contain all entries that will be set by kvm.
Therefore if the nent field is too high, kvm will adjust it to the right value. If too low, -E2BIG is returned.
However, when filling the entries do_cpuid_func() requires an additional entry, so if the right nent is known in advance, giving the exact number of entries won't work because it has to be increased by one.
--- arch/x86/kvm/cpuid.c | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-)
arch/x86/kvm/cpuid.c +18 -17
@@ -567,34 +567,34 @@ static struct kvm_cpuid_entry2 *do_host_cpuid(struct kvm_cpuid_array *array,static int __do_cpuid_func_emulated (struct kvm_cpuid_array *array,u32 func) { -struct kvm_cpuid_entry2 *entry; - -if (array->nent >= array->maxnent) -return -E2BIG ; +struct kvm_cpuid_entry2 entry; - entry = &array->entries[array->nent]; - entry->function = func; - entry->index =0 ; - entry->flags =0 ; + entry.function = func; + entry.index =0 ; + entry.flags =0 ;switch (func) {case 0 : - entry->eax =7 ; - ++array->nent; + entry.eax =7 ;break ;case 1 : - entry->ecx =F (MOVBE ); - ++array->nent; + entry.ecx =F (MOVBE );break ;case 7 : - entry->flags |=KVM_CPUID_FLAG_SIGNIFCANT_INDEX ; - entry->eax =0 ; - entry->ecx =F (RDPID ); - ++array->nent; -default : + entry.flags |=KVM_CPUID_FLAG_SIGNIFCANT_INDEX ; + entry.eax =0 ; + entry.ecx =F (RDPID );break ; +default : + goto out; } +if (array->nent >= array->maxnent) +return -E2BIG ; + +memcpy (&array->entries[array->nent++], &entry,sizeof (entry)); + +out:return 0 ; }
@@ -975,6 +975,7 @@ int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,if (cpuid->nent <1 )return -E2BIG ; +if (cpuid->nent >KVM_MAX_CPUID_ENTRIES ) cpuid->nent =KVM_MAX_CPUID_ENTRIES ;
Signature · 1 line
2.30.2
PATCH v2 3/4 selftests: add kvm_get_emulated_cpuid
In reply to Emanuele Giuseppe EspositoAs the similar kvm_get_supported_cpuid, allocates and gets the struct kvm_cpuid2 filled with emulated features.
--- .../selftests/kvm/include/x86_64/processor.h | 1 + .../selftests/kvm/lib/x86_64/processor.c | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+)
tools/testing/selftests/kvm/include/x86_64/processor.h +1 -0
@@ -353,6 +353,7 @@ void vcpu_load_state(struct kvm_vm *vm, uint32_t vcpuid,struct kvm_msr_list *kvm_get_msr_index_list (void );uint64_t kvm_get_feature_msr (uint64_t msr_index);struct kvm_cpuid2 *kvm_get_supported_cpuid (void ); +struct kvm_cpuid2 *kvm_get_emulated_cpuid (void );struct kvm_cpuid2 *vcpu_get_cpuid (struct kvm_vm *vm,uint32_t vcpuid); void vcpu_set_cpuid(struct kvm_vm *vm, uint32_t vcpuid,
tools/testing/selftests/kvm/lib/x86_64/processor.c +33 -0
@@ -669,6 +669,39 @@ struct kvm_cpuid2 *kvm_get_supported_cpuid(void)return cpuid; } +/* +* KVM Emulated CPUID Get +* +* Input Args: None +* +* Output Args: +* +* Return: The emulated KVM CPUID +* +* Get the guest CPUID emulated by KVM. +*/ +struct kvm_cpuid2 *kvm_get_emulated_cpuid (void ) +{ +static struct kvm_cpuid2 *cpuid; +int ret; +int kvm_fd; + +if (cpuid) +return cpuid; + + cpuid =allocate_kvm_cpuid2 (); + kvm_fd =open (KVM_DEV_PATH ,O_RDONLY ); +if (kvm_fd <0 ) +exit (KSFT_SKIP ); + + ret =ioctl (kvm_fd,KVM_GET_EMULATED_CPUID , cpuid); +TEST_ASSERT (ret ==0 ,"KVM_GET_EMULATED_CPUID failed %d %d\n" , + ret, errno); + +close (kvm_fd); +return cpuid; +} +/* * KVM Get MSR *
Signature · 1 line
2.30.2
cover kvm: cpuid: fix cpuid nent field
This series aims to clarify the behavior of KVM_GET_EMULATED_CPUID and KVM_GET_SUPPORTED
ioctls, and fix a corner case where the nent field of the struct kvm_cpuid2 is matching the amount of entries that kvm returns.
Patch 1 proposes the nent field fix to cpuid.c,
patch 2 updates the ioctl documentation accordingly and patches 3 and 4 provide a selftest to check KVM_GET_EMULATED_CPUID accordingly.
---
v2:
- better fix in cpuid.c, perform the nent check after the switch statement
- fix bug in get_emulated_cpuid.c selftest, each entry needs to have at least the padding zeroed otherwise it fails.
Emanuele Giuseppe Esposito (4):
kvm: cpuid: adjust the returned nent field of kvm_cpuid2 for
KVM_GET_SUPPORTED_CPUID and KVM_GET_EMULATED_CPUID
Documentation: kvm: update KVM_GET_EMULATED_CPUID ioctl description
selftests: add kvm_get_emulated_cpuid
selftests: kvm: add get_emulated_cpuid test
Documentation/virt/kvm/api.rst | 10 +- arch/x86/kvm/cpuid.c | 35 ++-- tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/include/x86_64/processor.h | 1 + .../selftests/kvm/lib/x86_64/processor.c | 33 +++ .../selftests/kvm/x86_64/get_emulated_cpuid.c | 198 ++++++++++++++++++ 7 files changed, 256 insertions(+), 23 deletions(-) create mode 100644 tools/testing/selftests/kvm/x86_64/get_emulated_cpuid.c
Signature · 1 line
2.30.2
PATCH v2 2/4 Documentation: kvm: update KVM_GET_EMULATED_CPUID ioctl description
KVM_GET_EMULATED_CPUID returns -E2BIG if the nent field of struct kvm_cpuid2 is smaller than the actual entries, while it adjusts nent if the provided amount is bigger than the actual amount.
Update documentation accordingly. ENOMEM is just returned if the allocation fails, like all other calls.
--- Documentation/virt/kvm/api.rst | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
Documentation/virt/kvm/api.rst +4 -6
@@ -3404,12 +3404,10 @@ which features are emulated by kvm instead of being present natively. Userspace invokes KVM_GET_EMULATED_CPUID by passing a kvm_cpuid2 structure with the 'nent' field indicating the number of entries in -the variable-size array 'entries'. If the number of entries is too low -to describe the cpu capabilities, an error (E2BIG) is returned. If the -number is too high, the 'nent' field is adjusted and an error (ENOMEM) -is returned. If the number is just right, the 'nent' field is adjusted -to the number of valid entries in the 'entries' array, which is then -filled. +the variable-size array 'entries'. +If the number of entries is too low to describe the cpu +capabilities, an error (E2BIG) is returned. If the number is too high, +the 'nent' field is adjusted and the entries array is filled. The entries returned are the set CPUID bits of the respective features which kvm emulates, as returned by the CPUID instruction, with unknown
Signature · 1 line
2.30.2
PATCH v2 4/4 selftests: kvm: add get_emulated_cpuid test
In reply to Emanuele Giuseppe EspositoIntroduce a new selftest for the KVM_GET_EMULATED_CPUID ioctl. Since the behavior and functionality is similar to get_cpuid_test, the test checks:
1) checks for corner case in the nent field of the struct kvm_cpuid2.
2) sets and gets it as cpuid from the guest VM
--- tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/x86_64/get_emulated_cpuid.c | 198 ++++++++++++++++++ 3 files changed, 200 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/get_emulated_cpuid.c
tools/testing/selftests/kvm/.gitignore +1 -0
@@ -8,6 +8,7 @@ /x86_64/debug_regs /x86_64/evmcs_test /x86_64/get_cpuid_test +x86_64/get_emulated_cpuid /x86_64/get_msr_index_features /x86_64/kvm_pv_test /x86_64/hyperv_clock
tools/testing/selftests/kvm/Makefile +1 -0
@@ -40,6 +40,7 @@ LIBKVM_s390x = lib/s390x/processor.c lib/s390x/ucall.c lib/s390x/diag318_test_ha TEST_GEN_PROGS_x86_64 = x86_64/cr4_cpuid_sync_test TEST_GEN_PROGS_x86_64 += x86_64/get_msr_index_features +TEST_GEN_PROGS_x86_64 += x86_64/get_emulated_cpuid TEST_GEN_PROGS_x86_64 += x86_64/evmcs_test TEST_GEN_PROGS_x86_64 += x86_64/get_cpuid_test TEST_GEN_PROGS_x86_64 += x86_64/hyperv_clock
tools/testing/selftests/kvm/x86_64/get_emulated_cpuid.c new file +198 -0
198 lines not on this page · raw message
Signature · 1 line
2.30.2
Sean Christopherson
maintainerRe: [PATCH v2 1/4] kvm: cpuid: adjust the returned nent field of kvm_cpuid2 for KVM_GET_SUPPORTED_CPUID and KVM_GET_EMULATED_CPUID
In reply to Emanuele Giuseppe EspositoQuoted 10 lines · Emanuele Giuseppe Esposito
Calling the kvm KVM_GET_[SUPPORTED/EMULATED]_CPUID ioctl requires a nent field inside the
kvm_cpuid2struct to be big enough to contain all entries that will be set by kvm.
Therefore if the nent field is too high, kvm will adjust it to the right value. If too low,-E2BIGis returned.However, when filling the entries
do_cpuid_func()requires an additional entry, so if the right nent is known in advance, giving the exact number of entries won't work because it has to be increased by one.
I'd strong prefer to reword the shortlog and changelog. It's not immediately obvious what this is changing without the context from the v1 thread. E.g.
KVM: x86: Fix a spurious -E2BIG in KVM_GET_EMULATED_CPUID
When retrieving emulated CPUID entries, check for an insufficient array size if and only if KVM is actually inserting an entry. If userspace has a priori knowledge of the exact array size, KVM_GET_EMULATED_CPUID will incorrectly fail due to effectively requiring an extra, unused entry.
Quoted 16 lines
Signed-off-by Emanuele Giuseppe Esposito <[email protected]>--- arch/x86/kvm/cpuid.c | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-)arch/x86/kvm/cpuid.c +0
-3@@ -567,34 +567,34 @@ static struct kvm_cpuid_entry2 *do_host_cpuid(struct kvm_cpuid_array *array,static int __do_cpuid_func_emulated (struct kvm_cpuid_array *array,u32 func) { -struct kvm_cpuid_entry2 *entry; - -if (array->nent >= array->maxnent)
arch/x86/kvm/cpuid.c static struct kvm_cpuid_entry2 *do_host_cpuid(struct kvm_cpuid_array *array, · line 574
Quoted 10 lines
arch/x86/kvm/cpuid.c +4
-5-return -E2BIG ; +struct kvm_cpuid_entry2 entry; - entry = &array->entries[array->nent]; - entry->function = func; - entry->index =0 ; - entry->flags =0 ; + entry.function = func; + entry.index =0 ; + entry.flags =0 ;
Depending on the leaf, eax/ebx/ecx/edx will be left uninitialized. This wasn't a bug before since @array is zeroed on allocation.
What about pre-checking @func? I don't particular like the duplicate checks, but none of the solutions are particularly elegant. E.g.
arch/x86/kvm/cpuid.c +8 -6
@@ -565,14 +565,18 @@ static struct kvm_cpuid_entry2 *do_host_cpuid(struct kvm_cpuid_array *array,return entry; } -static int __do_cpuid_func_emulated (struct kvm_cpuid_array *array,u32 func) +static noinline int __do_cpuid_func_emulated(struct kvm_cpuid_array *array, u32 func) {struct kvm_cpuid_entry2 *entry; +if (func !=0 && func !=1 && func !=7 ) +return 0 ; +if (array->nent >= array->maxnent)return -E2BIG ; - entry = &array->entries[array->nent]; + entry = &array->entries[array->nent++]; + entry->function = func; entry->index =0 ; entry->flags =0 ;
@@ -580,19 +584,17 @@ static int __do_cpuid_func_emulated(struct kvm_cpuid_array *array, u32 func)switch (func) {case 0 : entry->eax =7 ; - ++array->nent;break ;case 1 : entry->ecx =F (MOVBE ); - ++array->nent;break ;case 7 : entry->flags |=KVM_CPUID_FLAG_SIGNIFCANT_INDEX ; entry->eax =0 ; entry->ecx =F (RDPID ); - ++array->nent; -default :break ; +default : +BUG (); }return 0
Quoted 46 lines
arch/x86/kvm/cpuid.c +14
-9switch (func) {case 0 : - entry->eax =7 ; - ++array->nent; + entry.eax =7 ;break ;case 1 : - entry->ecx =F (MOVBE ); - ++array->nent; + entry.ecx =F (MOVBE );break ;case 7 : - entry->flags |=KVM_CPUID_FLAG_SIGNIFCANT_INDEX ; - entry->eax =0 ; - entry->ecx =F (RDPID ); - ++array->nent; -default : + entry.flags |=KVM_CPUID_FLAG_SIGNIFCANT_INDEX ; + entry.eax =0 ; + entry.ecx =F (RDPID );break ; +default : + goto out; } +if (array->nent >= array->maxnent) +return -E2BIG ; + +memcpy (&array->entries[array->nent++], &entry,sizeof (entry)); + +out:return 0 ; }@@ -975,6 +975,7 @@ int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,if (cpuid->nent <1 )return -E2BIG ; +if (cpuid->nent >KVM_MAX_CPUID_ENTRIES ) cpuid->nent =KVM_MAX_CPUID_ENTRIES ;Signature · 1 line
2.30.2
Re: [PATCH v2 1/4] kvm: cpuid: adjust the returned nent field of kvm_cpuid2 for KVM_GET_SUPPORTED_CPUID and KVM_GET_EMULATED_CPUID
Quoted 15 lines · Sean Christopherson
Quoted 10 lines · Emanuele Giuseppe Esposito
Calling the kvm KVM_GET_[SUPPORTED/EMULATED]_CPUID ioctl requires a nent field inside the
kvm_cpuid2struct to be big enough to contain all entries that will be set by kvm.
Therefore if the nent field is too high, kvm will adjust it to the right value. If too low,-E2BIGis returned.However, when filling the entries
do_cpuid_func()requires an additional entry, so if the right nent is known in advance, giving the exact number of entries won't work because it has to be increased by one.I'd strong prefer to reword the shortlog and changelog. It's not immediately obvious what this is changing without the context from the v1 thread. E.g.
Quoted 6 lines
KVM: x86: Fix a spurious
-E2BIGinKVM_GET_EMULATED_CPUIDWhen retrieving emulated CPUID entries, check for an insufficient array size if and only if KVM is actually inserting an entry. If userspace has a priori knowledge of the exact array size,
KVM_GET_EMULATED_CPUIDwill incorrectly fail due to effectively requiring an extra, unused entry.
I will update it with v3, thanks.
Quoted 31 lines
Quoted 17 lines
Signed-off-by Emanuele Giuseppe Esposito <[email protected]>--- arch/x86/kvm/cpuid.c | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-)arch/x86/kvm/cpuid.c +0
-3@@ -567,34 +567,34 @@ static struct kvm_cpuid_entry2 *do_host_cpuid(struct kvm_cpuid_array *array,static int __do_cpuid_func_emulated (struct kvm_cpuid_array *array,u32 func) { -struct kvm_cpuid_entry2 *entry; - -if (array->nent >= array->maxnent)arch/x86/kvm/cpuid.c static struct kvm_cpuid_entry2 *do_host_cpuid(struct kvm_cpuid_array *array, · line 574
Quoted 10 lines
arch/x86/kvm/cpuid.c +4
-5-return -E2BIG ; +struct kvm_cpuid_entry2 entry; - entry = &array->entries[array->nent]; - entry->function = func; - entry->index =0 ; - entry->flags =0 ; + entry.function = func; + entry.index =0 ; + entry.flags =0 ;Depending on the leaf, eax/ebx/ecx/edx will be left uninitialized. This wasn't a bug before since @array is zeroed on allocation.
Quoted 2 lines
What about pre-checking @func? I don't particular like the duplicate checks, but none of the solutions are particularly elegant. E.g.
You're right, I should have zeroed it. I agree that memsetting and memcopying is not elegant either, but unless I am missing something and it changes the intended behavior, IMHO this avoids duplicate checks and makes it simpler to add a new 'func'.
Emanuele
Quoted 97 lines
arch/x86/kvm/cpuid.c +8
-6@@ -565,14 +565,18 @@ static struct kvm_cpuid_entry2 *do_host_cpuid(struct kvm_cpuid_array *array,return entry; } -static int __do_cpuid_func_emulated (struct kvm_cpuid_array *array,u32 func) +static noinline int __do_cpuid_func_emulated(struct kvm_cpuid_array *array, u32 func) {struct kvm_cpuid_entry2 *entry; +if (func !=0 && func !=1 && func !=7 ) +return 0 ; +if (array->nent >= array->maxnent)return -E2BIG ; - entry = &array->entries[array->nent]; + entry = &array->entries[array->nent++]; + entry->function = func; entry->index =0 ; entry->flags =0 ;@@ -580,19 +584,17 @@ static int __do_cpuid_func_emulated(struct kvm_cpuid_array *array, u32 func)switch (func) {case 0 : entry->eax =7 ; - ++array->nent;break ;case 1 : entry->ecx =F (MOVBE ); - ++array->nent;break ;case 7 : entry->flags |=KVM_CPUID_FLAG_SIGNIFCANT_INDEX ; entry->eax =0 ; entry->ecx =F (RDPID ); - ++array->nent; -default :break ; +default : +BUG (); }return 0 Quoted 48 lines
arch/x86/kvm/cpuid.c +14
-9switch (func) {case 0 : - entry->eax =7 ; - ++array->nent; + entry.eax =7 ;break ;case 1 : - entry->ecx =F (MOVBE ); - ++array->nent; + entry.ecx =F (MOVBE );break ;case 7 : - entry->flags |=KVM_CPUID_FLAG_SIGNIFCANT_INDEX ; - entry->eax =0 ; - entry->ecx =F (RDPID ); - ++array->nent; -default : + entry.flags |=KVM_CPUID_FLAG_SIGNIFCANT_INDEX ; + entry.eax =0 ; + entry.ecx =F (RDPID );break ; +default : + goto out; } +if (array->nent >= array->maxnent) +return -E2BIG ; + +memcpy (&array->entries[array->nent++], &entry,sizeof (entry)); + +out:return 0 ; }@@ -975,6 +975,7 @@ int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,if (cpuid->nent <1 )return -E2BIG ; +if (cpuid->nent >KVM_MAX_CPUID_ENTRIES ) cpuid->nent =KVM_MAX_CPUID_ENTRIES ;Signature · 1 line
2.30.2