Skip to content

Commit 9b56532

Browse files
committed
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: Sean Christopherson <seanjc@google.com>
1 parent ea7179f commit 9b56532

File tree

3 files changed

+37
-27
lines changed

3 files changed

+37
-27
lines changed

tools/testing/selftests/kvm/include/kvm_util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,8 @@ static inline struct kvm_vm *vm_create_shape_with_one_vcpu(struct vm_shape shape
966966

967967
struct kvm_vcpu *vm_recreate_with_one_vcpu(struct kvm_vm *vm);
968968

969+
void kvm_set_files_rlimit(uint32_t nr_vcpus);
970+
969971
void kvm_pin_this_task_to_pcpu(uint32_t pcpu);
970972
void kvm_print_vcpu_pinning_help(void);
971973
void kvm_parse_vcpu_pinning(const char *pcpus_string, uint32_t vcpu_to_pcpu[],

tools/testing/selftests/kvm/kvm_create_max_vcpus.c

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include <stdio.h>
1111
#include <stdlib.h>
1212
#include <string.h>
13-
#include <sys/resource.h>
1413

1514
#include "test_util.h"
1615

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

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

52-
/*
53-
* Check that we're allowed to open nr_fds_wanted file descriptors and
54-
* try raising the limits if needed.
55-
*/
56-
TEST_ASSERT(!getrlimit(RLIMIT_NOFILE, &rl), "getrlimit() failed!");
57-
58-
if (rl.rlim_cur < nr_fds_wanted) {
59-
rl.rlim_cur = nr_fds_wanted;
60-
if (rl.rlim_max < nr_fds_wanted) {
61-
int old_rlim_max = rl.rlim_max;
62-
rl.rlim_max = nr_fds_wanted;
63-
64-
int r = setrlimit(RLIMIT_NOFILE, &rl);
65-
__TEST_REQUIRE(r >= 0,
66-
"RLIMIT_NOFILE hard limit is too low (%d, wanted %d)",
67-
old_rlim_max, nr_fds_wanted);
68-
} else {
69-
TEST_ASSERT(!setrlimit(RLIMIT_NOFILE, &rl), "setrlimit() failed!");
70-
}
71-
}
45+
kvm_set_files_rlimit(kvm_max_vcpus);
7246

7347
/*
7448
* Upstream KVM prior to 4.8 does not support KVM_CAP_MAX_VCPU_ID.

tools/testing/selftests/kvm/lib/kvm_util.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <assert.h>
1313
#include <sched.h>
1414
#include <sys/mman.h>
15+
#include <sys/resource.h>
1516
#include <sys/types.h>
1617
#include <sys/stat.h>
1718
#include <unistd.h>
@@ -411,6 +412,37 @@ static uint64_t vm_nr_pages_required(enum vm_guest_mode mode,
411412
return vm_adjust_num_guest_pages(mode, nr_pages);
412413
}
413414

415+
void kvm_set_files_rlimit(uint32_t nr_vcpus)
416+
{
417+
/*
418+
* Number of file descriptors required, nr_vpucs vCPU fds + an arbitrary
419+
* number for everything else.
420+
*/
421+
int nr_fds_wanted = nr_vcpus + 100;
422+
struct rlimit rl;
423+
424+
/*
425+
* Check that we're allowed to open nr_fds_wanted file descriptors and
426+
* try raising the limits if needed.
427+
*/
428+
TEST_ASSERT(!getrlimit(RLIMIT_NOFILE, &rl), "getrlimit() failed!");
429+
430+
if (rl.rlim_cur < nr_fds_wanted) {
431+
rl.rlim_cur = nr_fds_wanted;
432+
if (rl.rlim_max < nr_fds_wanted) {
433+
int old_rlim_max = rl.rlim_max;
434+
435+
rl.rlim_max = nr_fds_wanted;
436+
__TEST_REQUIRE(setrlimit(RLIMIT_NOFILE, &rl) >= 0,
437+
"RLIMIT_NOFILE hard limit is too low (%d, wanted %d)",
438+
old_rlim_max, nr_fds_wanted);
439+
} else {
440+
TEST_ASSERT(!setrlimit(RLIMIT_NOFILE, &rl), "setrlimit() failed!");
441+
}
442+
}
443+
444+
}
445+
414446
struct kvm_vm *__vm_create(struct vm_shape shape, uint32_t nr_runnable_vcpus,
415447
uint64_t nr_extra_pages)
416448
{
@@ -420,6 +452,8 @@ struct kvm_vm *__vm_create(struct vm_shape shape, uint32_t nr_runnable_vcpus,
420452
struct kvm_vm *vm;
421453
int i;
422454

455+
kvm_set_files_rlimit(nr_runnable_vcpus);
456+
423457
pr_debug("%s: mode='%s' type='%d', pages='%ld'\n", __func__,
424458
vm_guest_mode_string(shape.mode), shape.type, nr_pages);
425459

0 commit comments

Comments
 (0)