rust: upgrade to Rust 1.76.0
[sfrench/cifs-2.6.git] / tools / testing / selftests / kvm / kvm_create_max_vcpus.c
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
10 #define _GNU_SOURCE /* for program_invocation_short_name */
11 #include <fcntl.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/resource.h>
16
17 #include "test_util.h"
18
19 #include "kvm_util.h"
20 #include "asm/kvm.h"
21 #include "linux/kvm.h"
22
23 void test_vcpu_creation(int first_vcpu_id, int num_vcpus)
24 {
25         struct kvm_vm *vm;
26         int i;
27
28         pr_info("Testing creating %d vCPUs, with IDs %d...%d.\n",
29                 num_vcpus, first_vcpu_id, first_vcpu_id + num_vcpus - 1);
30
31         vm = vm_create_barebones();
32
33         for (i = first_vcpu_id; i < first_vcpu_id + num_vcpus; i++)
34                 /* This asserts that the vCPU was created. */
35                 __vm_vcpu_add(vm, i);
36
37         kvm_vm_free(vm);
38 }
39
40 int main(int argc, char *argv[])
41 {
42         int kvm_max_vcpu_id = kvm_check_cap(KVM_CAP_MAX_VCPU_ID);
43         int kvm_max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS);
44         /*
45          * Number of file descriptors reqired, KVM_CAP_MAX_VCPUS for vCPU fds +
46          * an arbitrary number for everything else.
47          */
48         int nr_fds_wanted = kvm_max_vcpus + 100;
49         struct rlimit rl;
50
51         pr_info("KVM_CAP_MAX_VCPU_ID: %d\n", kvm_max_vcpu_id);
52         pr_info("KVM_CAP_MAX_VCPUS: %d\n", kvm_max_vcpus);
53
54         /*
55          * Check that we're allowed to open nr_fds_wanted file descriptors and
56          * try raising the limits if needed.
57          */
58         TEST_ASSERT(!getrlimit(RLIMIT_NOFILE, &rl), "getrlimit() failed!");
59
60         if (rl.rlim_cur < nr_fds_wanted) {
61                 rl.rlim_cur = nr_fds_wanted;
62                 if (rl.rlim_max < nr_fds_wanted) {
63                         int old_rlim_max = rl.rlim_max;
64                         rl.rlim_max = nr_fds_wanted;
65
66                         int r = setrlimit(RLIMIT_NOFILE, &rl);
67                         __TEST_REQUIRE(r >= 0,
68                                        "RLIMIT_NOFILE hard limit is too low (%d, wanted %d)\n",
69                                        old_rlim_max, nr_fds_wanted);
70                 } else {
71                         TEST_ASSERT(!setrlimit(RLIMIT_NOFILE, &rl), "setrlimit() failed!");
72                 }
73         }
74
75         /*
76          * Upstream KVM prior to 4.8 does not support KVM_CAP_MAX_VCPU_ID.
77          * Userspace is supposed to use KVM_CAP_MAX_VCPUS as the maximum ID
78          * in this case.
79          */
80         if (!kvm_max_vcpu_id)
81                 kvm_max_vcpu_id = kvm_max_vcpus;
82
83         TEST_ASSERT(kvm_max_vcpu_id >= kvm_max_vcpus,
84                     "KVM_MAX_VCPU_IDS (%d) must be at least as large as KVM_MAX_VCPUS (%d).",
85                     kvm_max_vcpu_id, kvm_max_vcpus);
86
87         test_vcpu_creation(0, kvm_max_vcpus);
88
89         if (kvm_max_vcpu_id > kvm_max_vcpus)
90                 test_vcpu_creation(
91                         kvm_max_vcpu_id - kvm_max_vcpus, kvm_max_vcpus);
92
93         return 0;
94 }