1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * kvm_create_max_vcpus 4 * 5 * Copyright (C) 2019, Google LLC. 6 * 7 * Test for KVM_CAP_MAX_VCPUS and KVM_CAP_MAX_VCPU_ID. 8 */ 9 #include <fcntl.h> 10 #include <stdio.h> 11 #include <stdlib.h> 12 #include <string.h> 13 14 #include "test_util.h" 15 16 #include "kvm_util.h" 17 #include "asm/kvm.h" 18 #include "linux/kvm.h" 19 20 void test_vcpu_creation(int first_vcpu_id, int num_vcpus) 21 { 22 struct kvm_vm *vm; 23 int i; 24 25 pr_info("Testing creating %d vCPUs, with IDs %d...%d.\n", 26 num_vcpus, first_vcpu_id, first_vcpu_id + num_vcpus - 1); 27 28 vm = vm_create_barebones(); 29 30 for (i = first_vcpu_id; i < first_vcpu_id + num_vcpus; i++) 31 /* This asserts that the vCPU was created. */ 32 __vm_vcpu_add(vm, i); 33 34 kvm_vm_free(vm); 35 } 36 37 int main(int argc, char *argv[]) 38 { 39 int kvm_max_vcpu_id = kvm_check_cap(KVM_CAP_MAX_VCPU_ID); 40 int kvm_max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS); 41 42 pr_info("KVM_CAP_MAX_VCPU_ID: %d\n", kvm_max_vcpu_id); 43 pr_info("KVM_CAP_MAX_VCPUS: %d\n", kvm_max_vcpus); 44 45 kvm_set_files_rlimit(kvm_max_vcpus); 46 47 /* 48 * Upstream KVM prior to 4.8 does not support KVM_CAP_MAX_VCPU_ID. 49 * Userspace is supposed to use KVM_CAP_MAX_VCPUS as the maximum ID 50 * in this case. 51 */ 52 if (!kvm_max_vcpu_id) 53 kvm_max_vcpu_id = kvm_max_vcpus; 54 55 TEST_ASSERT(kvm_max_vcpu_id >= kvm_max_vcpus, 56 "KVM_MAX_VCPU_IDS (%d) must be at least as large as KVM_MAX_VCPUS (%d).", 57 kvm_max_vcpu_id, kvm_max_vcpus); 58 59 test_vcpu_creation(0, kvm_max_vcpus); 60 61 if (kvm_max_vcpu_id > kvm_max_vcpus) 62 test_vcpu_creation( 63 kvm_max_vcpu_id - kvm_max_vcpus, kvm_max_vcpus); 64 65 return 0; 66 } 67