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

KVM: selftests: Print a message if /dev/kvm is missing

If a KVM selftest is run on a machine without /dev/kvm, it will exit
silently. Make it easy to tell what's happening by printing an error
message.

Opportunistically consolidate all codepaths that open /dev/kvm into a
single function so they all print the same message.

This slightly changes the semantics of vm_is_unrestricted_guest() by
changing a TEST_ASSERT() to exit(KSFT_SKIP). However
vm_is_unrestricted_guest() is only called in one place
(x86_64/mmio_warning_test.c) and that is to determine if the test should
be skipped or not.

Signed-off-by: David Matlack <dmatlack@google.com>
Message-Id: <20210511202120.1371800-1-dmatlack@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

authored by

David Matlack and committed by
Paolo Bonzini
2aab4b35 c887d6a1

+39 -32
+1
tools/testing/selftests/kvm/include/kvm_util.h
··· 77 77 }; 78 78 extern const struct vm_guest_mode_params vm_guest_mode_params[]; 79 79 80 + int open_kvm_dev_path_or_exit(void); 80 81 int kvm_check_cap(long cap); 81 82 int vm_enable_cap(struct kvm_vm *vm, struct kvm_enable_cap *cap); 82 83 int vcpu_enable_cap(struct kvm_vm *vm, uint32_t vcpu_id,
+32 -14
tools/testing/selftests/kvm/lib/kvm_util.c
··· 32 32 } 33 33 34 34 /* 35 + * Open KVM_DEV_PATH if available, otherwise exit the entire program. 36 + * 37 + * Input Args: 38 + * flags - The flags to pass when opening KVM_DEV_PATH. 39 + * 40 + * Return: 41 + * The opened file descriptor of /dev/kvm. 42 + */ 43 + static int _open_kvm_dev_path_or_exit(int flags) 44 + { 45 + int fd; 46 + 47 + fd = open(KVM_DEV_PATH, flags); 48 + if (fd < 0) { 49 + print_skip("%s not available, is KVM loaded? (errno: %d)", 50 + KVM_DEV_PATH, errno); 51 + exit(KSFT_SKIP); 52 + } 53 + 54 + return fd; 55 + } 56 + 57 + int open_kvm_dev_path_or_exit(void) 58 + { 59 + return _open_kvm_dev_path_or_exit(O_RDONLY); 60 + } 61 + 62 + /* 35 63 * Capability 36 64 * 37 65 * Input Args: ··· 80 52 int ret; 81 53 int kvm_fd; 82 54 83 - kvm_fd = open(KVM_DEV_PATH, O_RDONLY); 84 - if (kvm_fd < 0) 85 - exit(KSFT_SKIP); 86 - 55 + kvm_fd = open_kvm_dev_path_or_exit(); 87 56 ret = ioctl(kvm_fd, KVM_CHECK_EXTENSION, cap); 88 57 TEST_ASSERT(ret != -1, "KVM_CHECK_EXTENSION IOCTL failed,\n" 89 58 " rc: %i errno: %i", ret, errno); ··· 153 128 154 129 static void vm_open(struct kvm_vm *vm, int perm) 155 130 { 156 - vm->kvm_fd = open(KVM_DEV_PATH, perm); 157 - if (vm->kvm_fd < 0) 158 - exit(KSFT_SKIP); 131 + vm->kvm_fd = _open_kvm_dev_path_or_exit(perm); 159 132 160 133 if (!kvm_check_cap(KVM_CAP_IMMEDIATE_EXIT)) { 161 134 print_skip("immediate_exit not available"); ··· 1019 996 { 1020 997 int dev_fd, ret; 1021 998 1022 - dev_fd = open(KVM_DEV_PATH, O_RDONLY); 1023 - if (dev_fd < 0) 1024 - exit(KSFT_SKIP); 999 + dev_fd = open_kvm_dev_path_or_exit(); 1025 1000 1026 1001 ret = ioctl(dev_fd, KVM_GET_VCPU_MMAP_SIZE, NULL); 1027 1002 TEST_ASSERT(ret >= sizeof(struct kvm_run), ··· 2112 2091 2113 2092 if (vm == NULL) { 2114 2093 /* Ensure that the KVM vendor-specific module is loaded. */ 2115 - f = fopen(KVM_DEV_PATH, "r"); 2116 - TEST_ASSERT(f != NULL, "Error in opening KVM dev file: %d", 2117 - errno); 2118 - fclose(f); 2094 + close(open_kvm_dev_path_or_exit()); 2119 2095 } 2120 2096 2121 2097 f = fopen("/sys/module/kvm_intel/parameters/unrestricted_guest", "r");
+4 -12
tools/testing/selftests/kvm/lib/x86_64/processor.c
··· 657 657 return cpuid; 658 658 659 659 cpuid = allocate_kvm_cpuid2(); 660 - kvm_fd = open(KVM_DEV_PATH, O_RDONLY); 661 - if (kvm_fd < 0) 662 - exit(KSFT_SKIP); 660 + kvm_fd = open_kvm_dev_path_or_exit(); 663 661 664 662 ret = ioctl(kvm_fd, KVM_GET_SUPPORTED_CPUID, cpuid); 665 663 TEST_ASSERT(ret == 0, "KVM_GET_SUPPORTED_CPUID failed %d %d\n", ··· 689 691 690 692 buffer.header.nmsrs = 1; 691 693 buffer.entry.index = msr_index; 692 - kvm_fd = open(KVM_DEV_PATH, O_RDONLY); 693 - if (kvm_fd < 0) 694 - exit(KSFT_SKIP); 694 + kvm_fd = open_kvm_dev_path_or_exit(); 695 695 696 696 r = ioctl(kvm_fd, KVM_GET_MSRS, &buffer.header); 697 697 TEST_ASSERT(r == 1, "KVM_GET_MSRS IOCTL failed,\n" ··· 982 986 struct kvm_msr_list *list; 983 987 int nmsrs, r, kvm_fd; 984 988 985 - kvm_fd = open(KVM_DEV_PATH, O_RDONLY); 986 - if (kvm_fd < 0) 987 - exit(KSFT_SKIP); 989 + kvm_fd = open_kvm_dev_path_or_exit(); 988 990 989 991 nmsrs = kvm_get_num_msrs_fd(kvm_fd); 990 992 list = malloc(sizeof(*list) + nmsrs * sizeof(list->indices[0])); ··· 1306 1312 return cpuid; 1307 1313 1308 1314 cpuid = allocate_kvm_cpuid2(); 1309 - kvm_fd = open(KVM_DEV_PATH, O_RDONLY); 1310 - if (kvm_fd < 0) 1311 - exit(KSFT_SKIP); 1315 + kvm_fd = open_kvm_dev_path_or_exit(); 1312 1316 1313 1317 ret = ioctl(kvm_fd, KVM_GET_SUPPORTED_HV_CPUID, cpuid); 1314 1318 TEST_ASSERT(ret == 0, "KVM_GET_SUPPORTED_HV_CPUID failed %d %d\n",
+2 -6
tools/testing/selftests/kvm/x86_64/get_msr_index_features.c
··· 37 37 int old_res, res, kvm_fd, r; 38 38 struct kvm_msr_list *list; 39 39 40 - kvm_fd = open(KVM_DEV_PATH, O_RDONLY); 41 - if (kvm_fd < 0) 42 - exit(KSFT_SKIP); 40 + kvm_fd = open_kvm_dev_path_or_exit(); 43 41 44 42 old_res = kvm_num_index_msrs(kvm_fd, 0); 45 43 TEST_ASSERT(old_res != 0, "Expecting nmsrs to be > 0"); ··· 99 101 int res, old_res, i, kvm_fd; 100 102 struct kvm_msr_list *feature_list; 101 103 102 - kvm_fd = open(KVM_DEV_PATH, O_RDONLY); 103 - if (kvm_fd < 0) 104 - exit(KSFT_SKIP); 104 + kvm_fd = open_kvm_dev_path_or_exit(); 105 105 106 106 old_res = kvm_num_feature_msrs(kvm_fd, 0); 107 107 TEST_ASSERT(old_res != 0, "Expecting nmsrs to be > 0");