149fe9a5dSThomas Huth // SPDX-License-Identifier: GPL-2.0-only
249fe9a5dSThomas Huth /*
349fe9a5dSThomas Huth * kvm_create_max_vcpus
449fe9a5dSThomas Huth *
549fe9a5dSThomas Huth * Copyright (C) 2019, Google LLC.
649fe9a5dSThomas Huth *
749fe9a5dSThomas Huth * Test for KVM_CAP_MAX_VCPUS and KVM_CAP_MAX_VCPU_ID.
849fe9a5dSThomas Huth */
949fe9a5dSThomas Huth
1049fe9a5dSThomas Huth #define _GNU_SOURCE /* for program_invocation_short_name */
1149fe9a5dSThomas Huth #include <fcntl.h>
1249fe9a5dSThomas Huth #include <stdio.h>
1349fe9a5dSThomas Huth #include <stdlib.h>
1449fe9a5dSThomas Huth #include <string.h>
15908fa88eSVitaly Kuznetsov #include <sys/resource.h>
1649fe9a5dSThomas Huth
1749fe9a5dSThomas Huth #include "test_util.h"
1849fe9a5dSThomas Huth
1949fe9a5dSThomas Huth #include "kvm_util.h"
2049fe9a5dSThomas Huth #include "asm/kvm.h"
2149fe9a5dSThomas Huth #include "linux/kvm.h"
2249fe9a5dSThomas Huth
test_vcpu_creation(int first_vcpu_id,int num_vcpus)2349fe9a5dSThomas Huth void test_vcpu_creation(int first_vcpu_id, int num_vcpus)
2449fe9a5dSThomas Huth {
2549fe9a5dSThomas Huth struct kvm_vm *vm;
2649fe9a5dSThomas Huth int i;
2749fe9a5dSThomas Huth
28244c6b6dSAndrew Jones pr_info("Testing creating %d vCPUs, with IDs %d...%d.\n",
2949fe9a5dSThomas Huth num_vcpus, first_vcpu_id, first_vcpu_id + num_vcpus - 1);
3049fe9a5dSThomas Huth
3195fb0460SSean Christopherson vm = vm_create_barebones();
3249fe9a5dSThomas Huth
33f245eeadSWainer dos Santos Moschetta for (i = first_vcpu_id; i < first_vcpu_id + num_vcpus; i++)
3449fe9a5dSThomas Huth /* This asserts that the vCPU was created. */
35f742d94fSSean Christopherson __vm_vcpu_add(vm, i);
3649fe9a5dSThomas Huth
3749fe9a5dSThomas Huth kvm_vm_free(vm);
3849fe9a5dSThomas Huth }
3949fe9a5dSThomas Huth
main(int argc,char * argv[])4049fe9a5dSThomas Huth int main(int argc, char *argv[])
4149fe9a5dSThomas Huth {
4249fe9a5dSThomas Huth int kvm_max_vcpu_id = kvm_check_cap(KVM_CAP_MAX_VCPU_ID);
4349fe9a5dSThomas Huth int kvm_max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS);
44908fa88eSVitaly Kuznetsov /*
45908fa88eSVitaly Kuznetsov * Number of file descriptors reqired, KVM_CAP_MAX_VCPUS for vCPU fds +
46908fa88eSVitaly Kuznetsov * an arbitrary number for everything else.
47908fa88eSVitaly Kuznetsov */
48908fa88eSVitaly Kuznetsov int nr_fds_wanted = kvm_max_vcpus + 100;
49908fa88eSVitaly Kuznetsov struct rlimit rl;
5049fe9a5dSThomas Huth
51244c6b6dSAndrew Jones pr_info("KVM_CAP_MAX_VCPU_ID: %d\n", kvm_max_vcpu_id);
52244c6b6dSAndrew Jones pr_info("KVM_CAP_MAX_VCPUS: %d\n", kvm_max_vcpus);
5349fe9a5dSThomas Huth
5449fe9a5dSThomas Huth /*
55908fa88eSVitaly Kuznetsov * Check that we're allowed to open nr_fds_wanted file descriptors and
56908fa88eSVitaly Kuznetsov * try raising the limits if needed.
57908fa88eSVitaly Kuznetsov */
58908fa88eSVitaly Kuznetsov TEST_ASSERT(!getrlimit(RLIMIT_NOFILE, &rl), "getrlimit() failed!");
59908fa88eSVitaly Kuznetsov
60908fa88eSVitaly Kuznetsov if (rl.rlim_cur < nr_fds_wanted) {
61908fa88eSVitaly Kuznetsov rl.rlim_cur = nr_fds_wanted;
62908fa88eSVitaly Kuznetsov if (rl.rlim_max < nr_fds_wanted) {
63908fa88eSVitaly Kuznetsov int old_rlim_max = rl.rlim_max;
64908fa88eSVitaly Kuznetsov rl.rlim_max = nr_fds_wanted;
65908fa88eSVitaly Kuznetsov
66908fa88eSVitaly Kuznetsov int r = setrlimit(RLIMIT_NOFILE, &rl);
67*7ed397d1SSean Christopherson __TEST_REQUIRE(r >= 0,
68*7ed397d1SSean Christopherson "RLIMIT_NOFILE hard limit is too low (%d, wanted %d)\n",
69908fa88eSVitaly Kuznetsov old_rlim_max, nr_fds_wanted);
70908fa88eSVitaly Kuznetsov } else {
71908fa88eSVitaly Kuznetsov TEST_ASSERT(!setrlimit(RLIMIT_NOFILE, &rl), "setrlimit() failed!");
72908fa88eSVitaly Kuznetsov }
73908fa88eSVitaly Kuznetsov }
74908fa88eSVitaly Kuznetsov
75908fa88eSVitaly Kuznetsov /*
7649fe9a5dSThomas Huth * Upstream KVM prior to 4.8 does not support KVM_CAP_MAX_VCPU_ID.
7749fe9a5dSThomas Huth * Userspace is supposed to use KVM_CAP_MAX_VCPUS as the maximum ID
7849fe9a5dSThomas Huth * in this case.
7949fe9a5dSThomas Huth */
8049fe9a5dSThomas Huth if (!kvm_max_vcpu_id)
8149fe9a5dSThomas Huth kvm_max_vcpu_id = kvm_max_vcpus;
8249fe9a5dSThomas Huth
8349fe9a5dSThomas Huth TEST_ASSERT(kvm_max_vcpu_id >= kvm_max_vcpus,
84a1c42ddeSJuergen Gross "KVM_MAX_VCPU_IDS (%d) must be at least as large as KVM_MAX_VCPUS (%d).",
8549fe9a5dSThomas Huth kvm_max_vcpu_id, kvm_max_vcpus);
8649fe9a5dSThomas Huth
8749fe9a5dSThomas Huth test_vcpu_creation(0, kvm_max_vcpus);
8849fe9a5dSThomas Huth
8949fe9a5dSThomas Huth if (kvm_max_vcpu_id > kvm_max_vcpus)
9049fe9a5dSThomas Huth test_vcpu_creation(
9149fe9a5dSThomas Huth kvm_max_vcpu_id - kvm_max_vcpus, kvm_max_vcpus);
9249fe9a5dSThomas Huth
9349fe9a5dSThomas Huth return 0;
9449fe9a5dSThomas Huth }
95