Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * kvm_create_max_vcpus
0004  *
0005  * Copyright (C) 2019, Google LLC.
0006  *
0007  * Test for KVM_CAP_MAX_VCPUS and KVM_CAP_MAX_VCPU_ID.
0008  */
0009 
0010 #define _GNU_SOURCE /* for program_invocation_short_name */
0011 #include <fcntl.h>
0012 #include <stdio.h>
0013 #include <stdlib.h>
0014 #include <string.h>
0015 #include <sys/resource.h>
0016 
0017 #include "test_util.h"
0018 
0019 #include "kvm_util.h"
0020 #include "asm/kvm.h"
0021 #include "linux/kvm.h"
0022 
0023 void test_vcpu_creation(int first_vcpu_id, int num_vcpus)
0024 {
0025     struct kvm_vm *vm;
0026     int i;
0027 
0028     pr_info("Testing creating %d vCPUs, with IDs %d...%d.\n",
0029         num_vcpus, first_vcpu_id, first_vcpu_id + num_vcpus - 1);
0030 
0031     vm = vm_create_barebones();
0032 
0033     for (i = first_vcpu_id; i < first_vcpu_id + num_vcpus; i++)
0034         /* This asserts that the vCPU was created. */
0035         __vm_vcpu_add(vm, i);
0036 
0037     kvm_vm_free(vm);
0038 }
0039 
0040 int main(int argc, char *argv[])
0041 {
0042     int kvm_max_vcpu_id = kvm_check_cap(KVM_CAP_MAX_VCPU_ID);
0043     int kvm_max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS);
0044     /*
0045      * Number of file descriptors reqired, KVM_CAP_MAX_VCPUS for vCPU fds +
0046      * an arbitrary number for everything else.
0047      */
0048     int nr_fds_wanted = kvm_max_vcpus + 100;
0049     struct rlimit rl;
0050 
0051     pr_info("KVM_CAP_MAX_VCPU_ID: %d\n", kvm_max_vcpu_id);
0052     pr_info("KVM_CAP_MAX_VCPUS: %d\n", kvm_max_vcpus);
0053 
0054     /*
0055      * Check that we're allowed to open nr_fds_wanted file descriptors and
0056      * try raising the limits if needed.
0057      */
0058     TEST_ASSERT(!getrlimit(RLIMIT_NOFILE, &rl), "getrlimit() failed!");
0059 
0060     if (rl.rlim_cur < nr_fds_wanted) {
0061         rl.rlim_cur = nr_fds_wanted;
0062         if (rl.rlim_max < nr_fds_wanted) {
0063             int old_rlim_max = rl.rlim_max;
0064             rl.rlim_max = nr_fds_wanted;
0065 
0066             int r = setrlimit(RLIMIT_NOFILE, &rl);
0067             __TEST_REQUIRE(r >= 0,
0068                        "RLIMIT_NOFILE hard limit is too low (%d, wanted %d)\n",
0069                        old_rlim_max, nr_fds_wanted);
0070         } else {
0071             TEST_ASSERT(!setrlimit(RLIMIT_NOFILE, &rl), "setrlimit() failed!");
0072         }
0073     }
0074 
0075     /*
0076      * Upstream KVM prior to 4.8 does not support KVM_CAP_MAX_VCPU_ID.
0077      * Userspace is supposed to use KVM_CAP_MAX_VCPUS as the maximum ID
0078      * in this case.
0079      */
0080     if (!kvm_max_vcpu_id)
0081         kvm_max_vcpu_id = kvm_max_vcpus;
0082 
0083     TEST_ASSERT(kvm_max_vcpu_id >= kvm_max_vcpus,
0084             "KVM_MAX_VCPU_IDS (%d) must be at least as large as KVM_MAX_VCPUS (%d).",
0085             kvm_max_vcpu_id, kvm_max_vcpus);
0086 
0087     test_vcpu_creation(0, kvm_max_vcpus);
0088 
0089     if (kvm_max_vcpu_id > kvm_max_vcpus)
0090         test_vcpu_creation(
0091             kvm_max_vcpu_id - kvm_max_vcpus, kvm_max_vcpus);
0092 
0093     return 0;
0094 }