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

KVM: selftests: Make sure kvm_create_max_vcpus test won't hit RLIMIT_NOFILE

With the elevated 'KVM_CAP_MAX_VCPUS' value kvm_create_max_vcpus test
may hit RLIMIT_NOFILE limits:

# ./kvm_create_max_vcpus
KVM_CAP_MAX_VCPU_ID: 4096
KVM_CAP_MAX_VCPUS: 1024
Testing creating 1024 vCPUs, with IDs 0...1023.
/dev/kvm not available (errno: 24), skipping test

Adjust RLIMIT_NOFILE limits to make sure KVM_CAP_MAX_VCPUS fds can be
opened. Note, raising hard limit ('rlim_max') requires CAP_SYS_RESOURCE
capability which is generally not needed to run kvm selftests (but without
raising the limit the test is doomed to fail anyway).

Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
Message-Id: <20211123135953.667434-1-vkuznets@redhat.com>
[Skip the test if the hard limit can be raised. - Paolo]
Reviewed-by: Sean Christopherson <seanjc@google.com>
Tested-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

authored by

Vitaly Kuznetsov and committed by
Paolo Bonzini
908fa88e feb627e8

+30
+30
tools/testing/selftests/kvm/kvm_create_max_vcpus.c
··· 12 12 #include <stdio.h> 13 13 #include <stdlib.h> 14 14 #include <string.h> 15 + #include <sys/resource.h> 15 16 16 17 #include "test_util.h" 17 18 ··· 41 40 { 42 41 int kvm_max_vcpu_id = kvm_check_cap(KVM_CAP_MAX_VCPU_ID); 43 42 int kvm_max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS); 43 + /* 44 + * Number of file descriptors reqired, KVM_CAP_MAX_VCPUS for vCPU fds + 45 + * an arbitrary number for everything else. 46 + */ 47 + int nr_fds_wanted = kvm_max_vcpus + 100; 48 + struct rlimit rl; 44 49 45 50 pr_info("KVM_CAP_MAX_VCPU_ID: %d\n", kvm_max_vcpu_id); 46 51 pr_info("KVM_CAP_MAX_VCPUS: %d\n", kvm_max_vcpus); 52 + 53 + /* 54 + * Check that we're allowed to open nr_fds_wanted file descriptors and 55 + * try raising the limits if needed. 56 + */ 57 + TEST_ASSERT(!getrlimit(RLIMIT_NOFILE, &rl), "getrlimit() failed!"); 58 + 59 + if (rl.rlim_cur < nr_fds_wanted) { 60 + rl.rlim_cur = nr_fds_wanted; 61 + if (rl.rlim_max < nr_fds_wanted) { 62 + int old_rlim_max = rl.rlim_max; 63 + rl.rlim_max = nr_fds_wanted; 64 + 65 + int r = setrlimit(RLIMIT_NOFILE, &rl); 66 + if (r < 0) { 67 + printf("RLIMIT_NOFILE hard limit is too low (%d, wanted %d)\n", 68 + old_rlim_max, nr_fds_wanted); 69 + exit(KSFT_SKIP); 70 + } 71 + } else { 72 + TEST_ASSERT(!setrlimit(RLIMIT_NOFILE, &rl), "setrlimit() failed!"); 73 + } 74 + } 47 75 48 76 /* 49 77 * Upstream KVM prior to 4.8 does not support KVM_CAP_MAX_VCPU_ID.