167761Smsmith// SPDX-License-Identifier: GPL-2.0-only
267761Smsmith/*
367761Smsmith * kvm_create_max_vcpus
467761Smsmith *
567761Smsmith * Copyright (C) 2019, Google LLC.
667761Smsmith *
767761Smsmith * Test for KVM_CAP_MAX_VCPUS and KVM_CAP_MAX_VCPU_ID.
867761Smsmith */
967761Smsmith
1067761Smsmith#define _GNU_SOURCE /* for program_invocation_short_name */
1167761Smsmith#include <fcntl.h>
1267761Smsmith#include <stdio.h>
1367761Smsmith#include <stdlib.h>
1467761Smsmith#include <string.h>
1567761Smsmith#include <sys/resource.h>
1667761Smsmith
1767761Smsmith#include "test_util.h"
1867761Smsmith
1967761Smsmith#include "kvm_util.h"
2067761Smsmith#include "asm/kvm.h"
2167761Smsmith#include "linux/kvm.h"
2267761Smsmith
2367761Smsmithvoid test_vcpu_creation(int first_vcpu_id, int num_vcpus)
2467761Smsmith{
2567761Smsmith	struct kvm_vm *vm;
2667761Smsmith	int i;
2767761Smsmith
2867761Smsmith	pr_info("Testing creating %d vCPUs, with IDs %d...%d.\n",
2967761Smsmith		num_vcpus, first_vcpu_id, first_vcpu_id + num_vcpus - 1);
3067761Smsmith
3167761Smsmith	vm = vm_create_barebones();
3267761Smsmith
3367761Smsmith	for (i = first_vcpu_id; i < first_vcpu_id + num_vcpus; i++)
3467761Smsmith		/* This asserts that the vCPU was created. */
3567761Smsmith		__vm_vcpu_add(vm, i);
3667761Smsmith
3767761Smsmith	kvm_vm_free(vm);
3867761Smsmith}
39118783Snjl
4077432Smsmithint main(int argc, char *argv[])
4191123Smsmith{
4269744Smsmith	int kvm_max_vcpu_id = kvm_check_cap(KVM_CAP_MAX_VCPU_ID);
4367761Smsmith	int kvm_max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS);
4467761Smsmith	/*
4567761Smsmith	 * Number of file descriptors reqired, KVM_CAP_MAX_VCPUS for vCPU fds +
46120308Snjl	 * an arbitrary number for everything else.
47118783Snjl	 */
48118783Snjl	int nr_fds_wanted = kvm_max_vcpus + 100;
49120308Snjl	struct rlimit rl;
5067761Smsmith
5167761Smsmith	pr_info("KVM_CAP_MAX_VCPU_ID: %d\n", kvm_max_vcpu_id);
52118783Snjl	pr_info("KVM_CAP_MAX_VCPUS: %d\n", kvm_max_vcpus);
53118783Snjl
54118783Snjl	/*
5567761Smsmith	 * Check that we're allowed to open nr_fds_wanted file descriptors and
5667761Smsmith	 * try raising the limits if needed.
57100497Siwasaki	 */
58100497Siwasaki	TEST_ASSERT(!getrlimit(RLIMIT_NOFILE, &rl), "getrlimit() failed!");
59118783Snjl
60118783Snjl	if (rl.rlim_cur < nr_fds_wanted) {
61120308Snjl		rl.rlim_cur = nr_fds_wanted;
62120308Snjl		if (rl.rlim_max < nr_fds_wanted) {
6367761Smsmith			int old_rlim_max = rl.rlim_max;
6467761Smsmith			rl.rlim_max = nr_fds_wanted;
6567761Smsmith
6667761Smsmith			int r = setrlimit(RLIMIT_NOFILE, &rl);
6767761Smsmith			__TEST_REQUIRE(r >= 0,
6867761Smsmith				       "RLIMIT_NOFILE hard limit is too low (%d, wanted %d)",
6967761Smsmith				       old_rlim_max, nr_fds_wanted);
70100497Siwasaki		} else {
71118049Stakawata			TEST_ASSERT(!setrlimit(RLIMIT_NOFILE, &rl), "setrlimit() failed!");
72100497Siwasaki		}
7367761Smsmith	}
7467761Smsmith
7567761Smsmith	/*
7667761Smsmith	 * Upstream KVM prior to 4.8 does not support KVM_CAP_MAX_VCPU_ID.
7767761Smsmith	 * Userspace is supposed to use KVM_CAP_MAX_VCPUS as the maximum ID
7867761Smsmith	 * in this case.
7967761Smsmith	 */
8067761Smsmith	if (!kvm_max_vcpu_id)
8167761Smsmith		kvm_max_vcpu_id = kvm_max_vcpus;
8267761Smsmith
8389054Smsmith	TEST_ASSERT(kvm_max_vcpu_id >= kvm_max_vcpus,
84118783Snjl		    "KVM_MAX_VCPU_IDS (%d) must be at least as large as KVM_MAX_VCPUS (%d).",
85118783Snjl		    kvm_max_vcpu_id, kvm_max_vcpus);
8667761Smsmith
8767761Smsmith	test_vcpu_creation(0, kvm_max_vcpus);
8867761Smsmith
8967761Smsmith	if (kvm_max_vcpu_id > kvm_max_vcpus)
9067761Smsmith		test_vcpu_creation(
91120308Snjl			kvm_max_vcpu_id - kvm_max_vcpus, kvm_max_vcpus);
9267761Smsmith
9367761Smsmith	return 0;
94120308Snjl}
95120308Snjl