]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
KVM: selftests: Adjust number of files rlimit for all "standard" VMs
authorSean Christopherson <seanjc@google.com>
Sat, 11 Jan 2025 00:50:47 +0000 (16:50 -0800)
committerSean Christopherson <seanjc@google.com>
Fri, 14 Feb 2025 15:02:12 +0000 (07:02 -0800)
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>
tools/testing/selftests/kvm/include/kvm_util.h
tools/testing/selftests/kvm/kvm_create_max_vcpus.c
tools/testing/selftests/kvm/lib/kvm_util.c

index 9a64bab42f898b181b5387de001ab7209f74020a..d4670b5962ab569961575c42baad55d415403f57 100644 (file)
@@ -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[],
index c78f34699f73018ba5b86e4ba69092ff262f0314..c5310736ed06560a9716ed097ce7760af860cb0d 100644 (file)
@@ -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.
index 8cacb7fd1ad97c405529534d07cc501528c75a6c..faeeb576be909f5da391b9c1a55e7f6f65bc615c 100644 (file)
@@ -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);