Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

+37 -27
+2
tools/testing/selftests/kvm/include/kvm_util.h
··· 966 966 967 967 struct kvm_vcpu *vm_recreate_with_one_vcpu(struct kvm_vm *vm); 968 968 969 + void kvm_set_files_rlimit(uint32_t nr_vcpus); 970 + 969 971 void kvm_pin_this_task_to_pcpu(uint32_t pcpu); 970 972 void kvm_print_vcpu_pinning_help(void); 971 973 void kvm_parse_vcpu_pinning(const char *pcpus_string, uint32_t vcpu_to_pcpu[],
+1 -27
tools/testing/selftests/kvm/kvm_create_max_vcpus.c
··· 10 10 #include <stdio.h> 11 11 #include <stdlib.h> 12 12 #include <string.h> 13 - #include <sys/resource.h> 14 13 15 14 #include "test_util.h" 16 15 ··· 38 39 { 39 40 int kvm_max_vcpu_id = kvm_check_cap(KVM_CAP_MAX_VCPU_ID); 40 41 int kvm_max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS); 41 - /* 42 - * Number of file descriptors reqired, KVM_CAP_MAX_VCPUS for vCPU fds + 43 - * an arbitrary number for everything else. 44 - */ 45 - int nr_fds_wanted = kvm_max_vcpus + 100; 46 - struct rlimit rl; 47 42 48 43 pr_info("KVM_CAP_MAX_VCPU_ID: %d\n", kvm_max_vcpu_id); 49 44 pr_info("KVM_CAP_MAX_VCPUS: %d\n", kvm_max_vcpus); 50 45 51 - /* 52 - * Check that we're allowed to open nr_fds_wanted file descriptors and 53 - * try raising the limits if needed. 54 - */ 55 - TEST_ASSERT(!getrlimit(RLIMIT_NOFILE, &rl), "getrlimit() failed!"); 56 - 57 - if (rl.rlim_cur < nr_fds_wanted) { 58 - rl.rlim_cur = nr_fds_wanted; 59 - if (rl.rlim_max < nr_fds_wanted) { 60 - int old_rlim_max = rl.rlim_max; 61 - rl.rlim_max = nr_fds_wanted; 62 - 63 - int r = setrlimit(RLIMIT_NOFILE, &rl); 64 - __TEST_REQUIRE(r >= 0, 65 - "RLIMIT_NOFILE hard limit is too low (%d, wanted %d)", 66 - old_rlim_max, nr_fds_wanted); 67 - } else { 68 - TEST_ASSERT(!setrlimit(RLIMIT_NOFILE, &rl), "setrlimit() failed!"); 69 - } 70 - } 46 + kvm_set_files_rlimit(kvm_max_vcpus); 71 47 72 48 /* 73 49 * Upstream KVM prior to 4.8 does not support KVM_CAP_MAX_VCPU_ID.
+34
tools/testing/selftests/kvm/lib/kvm_util.c
··· 12 12 #include <assert.h> 13 13 #include <sched.h> 14 14 #include <sys/mman.h> 15 + #include <sys/resource.h> 15 16 #include <sys/types.h> 16 17 #include <sys/stat.h> 17 18 #include <unistd.h> ··· 412 411 return vm_adjust_num_guest_pages(mode, nr_pages); 413 412 } 414 413 414 + void kvm_set_files_rlimit(uint32_t nr_vcpus) 415 + { 416 + /* 417 + * Number of file descriptors required, nr_vpucs vCPU fds + an arbitrary 418 + * number for everything else. 419 + */ 420 + int nr_fds_wanted = nr_vcpus + 100; 421 + struct rlimit rl; 422 + 423 + /* 424 + * Check that we're allowed to open nr_fds_wanted file descriptors and 425 + * try raising the limits if needed. 426 + */ 427 + TEST_ASSERT(!getrlimit(RLIMIT_NOFILE, &rl), "getrlimit() failed!"); 428 + 429 + if (rl.rlim_cur < nr_fds_wanted) { 430 + rl.rlim_cur = nr_fds_wanted; 431 + if (rl.rlim_max < nr_fds_wanted) { 432 + int old_rlim_max = rl.rlim_max; 433 + 434 + rl.rlim_max = nr_fds_wanted; 435 + __TEST_REQUIRE(setrlimit(RLIMIT_NOFILE, &rl) >= 0, 436 + "RLIMIT_NOFILE hard limit is too low (%d, wanted %d)", 437 + old_rlim_max, nr_fds_wanted); 438 + } else { 439 + TEST_ASSERT(!setrlimit(RLIMIT_NOFILE, &rl), "setrlimit() failed!"); 440 + } 441 + } 442 + 443 + } 444 + 415 445 struct kvm_vm *__vm_create(struct vm_shape shape, uint32_t nr_runnable_vcpus, 416 446 uint64_t nr_extra_pages) 417 447 { ··· 451 419 struct userspace_mem_region *slot0; 452 420 struct kvm_vm *vm; 453 421 int i; 422 + 423 + kvm_set_files_rlimit(nr_runnable_vcpus); 454 424 455 425 pr_debug("%s: mode='%s' type='%d', pages='%ld'\n", __func__, 456 426 vm_guest_mode_string(shape.mode), shape.type, nr_pages);