Some hosts (like in case of nested guest the hypervisor L0 might have quotas/limited resources for L1 guest) can run out of hypervisor resources before hitting KVM_CAP_MAX_VCPUS.
This patch make use of the helper we introduced in the previous patch (__vm_vcpu_try_add()) to tolerate such scenarios where an ENOMEM can occur. Signed-off-by: Ritesh Harjani (IBM) <[email protected]> --- .../testing/selftests/kvm/include/kvm_util.h | 3 +++ .../selftests/kvm/kvm_create_max_vcpus.c | 21 +++++++++++++++---- tools/testing/selftests/kvm/lib/kvm_util.c | 5 +++++ 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h index aa521b383abe..576bbebe0679 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -1064,6 +1064,9 @@ const char *exit_reason_str(unsigned int exit_reason); bool kvm_arch_needs_naturally_aligned_page_tables(void); +/* True if KVM_CREATE_VM / KVM_CREATE_VCPU may return ENOMEM. */ +bool kvm_arch_vm_create_may_enomem(void); + gpa_t ____vm_phy_pages_alloc(struct kvm_vm *vm, size_t nr_pages, gpa_t min_gpa, u32 memslot, bool protected, bool naturally_aligned); gpa_t __vm_phy_pages_alloc(struct kvm_vm *vm, size_t nr_pages, diff --git a/tools/testing/selftests/kvm/kvm_create_max_vcpus.c b/tools/testing/selftests/kvm/kvm_create_max_vcpus.c index 59ddc3757943..45a9d8b369b5 100644 --- a/tools/testing/selftests/kvm/kvm_create_max_vcpus.c +++ b/tools/testing/selftests/kvm/kvm_create_max_vcpus.c @@ -20,16 +20,29 @@ void test_vcpu_creation(int first_vcpu_id, int num_vcpus) { struct kvm_vm *vm; - int i; + struct kvm_vcpu *vcpu; + int i, created = 0; pr_info("Testing creating %d vCPUs, with IDs %d...%d.\n", num_vcpus, first_vcpu_id, first_vcpu_id + num_vcpus - 1); vm = vm_create_barebones(); - for (i = first_vcpu_id; i < first_vcpu_id + num_vcpus; i++) - /* This asserts that the vCPU was created. */ - __vm_vcpu_add(vm, i); + for (i = first_vcpu_id; i < first_vcpu_id + num_vcpus; i++) { + vcpu = __vm_vcpu_try_add(vm, i); + if (vcpu) { + created++; + continue; + } + + TEST_ASSERT(errno == ENOMEM && kvm_arch_vm_create_may_enomem(), + "KVM_CREATE_VCPU failed for id %d, errno: %d (%s)", + i, errno, strerror(errno)); + TEST_ASSERT(created > 0, + "KVM_CREATE_VCPU failed with ENOMEM before creating any vCPUs"); + pr_info("Created %d vCPUs before ENOMEM at id %d\n", created, i); + break; + } kvm_vm_free(vm); } diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index 7084ed90e4cc..b4d6b8167466 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -2112,6 +2112,11 @@ __weak bool kvm_arch_needs_naturally_aligned_page_tables(void) return false; } +__weak bool kvm_arch_vm_create_may_enomem(void) +{ + return false; +} + gpa_t __vm_phy_pages_alloc(struct kvm_vm *vm, size_t nr_pages, enum kvm_mem_region_type type, bool protected) { -- 2.39.5
