Commit 9b56532b authored by Sean Christopherson's avatar Sean Christopherson
Browse files

KVM: selftests: Adjust number of files rlimit for all "standard" VMs

Move the max vCPUs test's RLIMIT_NOFILE adjustments to common code, and
use the new helper to adjust the resource limit for non-barebones VMs by
default.  x86's recalc_apic_map_test creates 512 vCPUs, and a future
change will open the binary stats fd for all vCPUs, which will put the
recalc APIC test above some distros' default limit of 1024.

Link: https://lore.kernel.org/r/20250111005049.1247555-8-seanjc@google.com


Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
parent ea7179f9
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -966,6 +966,8 @@ static inline struct kvm_vm *vm_create_shape_with_one_vcpu(struct vm_shape shape

struct kvm_vcpu *vm_recreate_with_one_vcpu(struct kvm_vm *vm);

void kvm_set_files_rlimit(uint32_t nr_vcpus);

void kvm_pin_this_task_to_pcpu(uint32_t pcpu);
void kvm_print_vcpu_pinning_help(void);
void kvm_parse_vcpu_pinning(const char *pcpus_string, uint32_t vcpu_to_pcpu[],
+1 −27
Original line number Diff line number Diff line
@@ -10,7 +10,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>

#include "test_util.h"

@@ -39,36 +38,11 @@ int main(int argc, char *argv[])
{
	int kvm_max_vcpu_id = kvm_check_cap(KVM_CAP_MAX_VCPU_ID);
	int kvm_max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS);
	/*
	 * Number of file descriptors reqired, KVM_CAP_MAX_VCPUS for vCPU fds +
	 * an arbitrary number for everything else.
	 */
	int nr_fds_wanted = kvm_max_vcpus + 100;
	struct rlimit rl;

	pr_info("KVM_CAP_MAX_VCPU_ID: %d\n", kvm_max_vcpu_id);
	pr_info("KVM_CAP_MAX_VCPUS: %d\n", kvm_max_vcpus);

	/*
	 * Check that we're allowed to open nr_fds_wanted file descriptors and
	 * try raising the limits if needed.
	 */
	TEST_ASSERT(!getrlimit(RLIMIT_NOFILE, &rl), "getrlimit() failed!");

	if (rl.rlim_cur < nr_fds_wanted) {
		rl.rlim_cur = nr_fds_wanted;
		if (rl.rlim_max < nr_fds_wanted) {
			int old_rlim_max = rl.rlim_max;
			rl.rlim_max = nr_fds_wanted;

			int r = setrlimit(RLIMIT_NOFILE, &rl);
			__TEST_REQUIRE(r >= 0,
				       "RLIMIT_NOFILE hard limit is too low (%d, wanted %d)",
				       old_rlim_max, nr_fds_wanted);
		} else {
			TEST_ASSERT(!setrlimit(RLIMIT_NOFILE, &rl), "setrlimit() failed!");
		}
	}
	kvm_set_files_rlimit(kvm_max_vcpus);

	/*
	 * Upstream KVM prior to 4.8 does not support KVM_CAP_MAX_VCPU_ID.
+34 −0
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@
#include <assert.h>
#include <sched.h>
#include <sys/mman.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
@@ -411,6 +412,37 @@ static uint64_t vm_nr_pages_required(enum vm_guest_mode mode,
	return vm_adjust_num_guest_pages(mode, nr_pages);
}

void kvm_set_files_rlimit(uint32_t nr_vcpus)
{
	/*
	 * Number of file descriptors required, nr_vpucs vCPU fds + an arbitrary
	 * number for everything else.
	 */
	int nr_fds_wanted = nr_vcpus + 100;
	struct rlimit rl;

	/*
	 * Check that we're allowed to open nr_fds_wanted file descriptors and
	 * try raising the limits if needed.
	 */
	TEST_ASSERT(!getrlimit(RLIMIT_NOFILE, &rl), "getrlimit() failed!");

	if (rl.rlim_cur < nr_fds_wanted) {
		rl.rlim_cur = nr_fds_wanted;
		if (rl.rlim_max < nr_fds_wanted) {
			int old_rlim_max = rl.rlim_max;

			rl.rlim_max = nr_fds_wanted;
			__TEST_REQUIRE(setrlimit(RLIMIT_NOFILE, &rl) >= 0,
				       "RLIMIT_NOFILE hard limit is too low (%d, wanted %d)",
				       old_rlim_max, nr_fds_wanted);
		} else {
			TEST_ASSERT(!setrlimit(RLIMIT_NOFILE, &rl), "setrlimit() failed!");
		}
	}

}

struct kvm_vm *__vm_create(struct vm_shape shape, uint32_t nr_runnable_vcpus,
			   uint64_t nr_extra_pages)
{
@@ -420,6 +452,8 @@ struct kvm_vm *__vm_create(struct vm_shape shape, uint32_t nr_runnable_vcpus,
	struct kvm_vm *vm;
	int i;

	kvm_set_files_rlimit(nr_runnable_vcpus);

	pr_debug("%s: mode='%s' type='%d', pages='%ld'\n", __func__,
		 vm_guest_mode_string(shape.mode), shape.type, nr_pages);