Skip to contentSkip to conversation

KVM: selftests: Check API version when creating VM

PATCH New Ivan Orlov 1 participant 1 message May 9, 2024

Subsystems (2)
lore
Share & tools
lore

Ivan Orlov

author
#0 lore raw

PATCH 1/1 KVM: selftests: Check API version when creating VM

As it is said in the docs, we should always check the KVM API version before running the KVM-based applications. Add the function which queries the current KVM API version through ioctl to the kvm_util.c file.

Add a new TEST_REQUIRE statement to the vm_open function in order to verify the version of the KVM API before creating a VM.

---
 .../testing/selftests/kvm/include/kvm_util_base.h  |  2 ++
 tools/testing/selftests/kvm/lib/kvm_util.c         | 14 ++++++++++++++
 2 files changed, 16 insertions(+)
tools/testing/selftests/kvm/include/kvm_util_base.h +2 -0
index 3e0db283a46a..d7a83387ae33 100644
@@ -38,6 +38,7 @@
 #define kvm_static_assert(expr, ...) __kvm_static_assert(expr, ##__VA_ARGS__, #expr)
 
 #define KVM_DEV_PATH "/dev/kvm"
+#define KVM_DEFAULT_API_VERSION 12
 #define KVM_MAX_VCPUS 512
 
 #define NSEC_PER_SEC 1000000000L
@@ -275,6 +276,7 @@ int get_kvm_intel_param_integer(const char *param);
 int get_kvm_amd_param_integer(const char *param);
 
 unsigned int kvm_check_cap(long cap);
+int kvm_get_api_version(void);
 
 static inline bool kvm_has_cap(long cap)
 {
tools/testing/selftests/kvm/lib/kvm_util.c +14 -0
index b2262b5fad9e..58a5deccb388 100644
@@ -176,6 +176,19 @@ unsigned int kvm_check_cap(long cap)
 	return (unsigned int)ret;
 }
 
+int kvm_get_api_version(void)
+{
+	int ret;
+	int kvm_fd;
+
+	kvm_fd = open_kvm_dev_path_or_exit();
+	ret = __kvm_ioctl(kvm_fd, KVM_GET_API_VERSION, NULL);
+
+	close(kvm_fd);
+
+	return ret;
+}
+
 void vm_enable_dirty_ring(struct kvm_vm *vm, uint32_t ring_size)
 {
 	if (vm_check_cap(vm, KVM_CAP_DIRTY_LOG_RING_ACQ_REL))
@@ -190,6 +203,7 @@ static void vm_open(struct kvm_vm *vm)
 	vm->kvm_fd = _open_kvm_dev_path_or_exit(O_RDWR);
 
 	TEST_REQUIRE(kvm_has_cap(KVM_CAP_IMMEDIATE_EXIT));
+	TEST_REQUIRE(kvm_get_api_version() == KVM_DEFAULT_API_VERSION);
 
 	vm->fd = __kvm_ioctl(vm->kvm_fd, KVM_CREATE_VM, (void *)vm->type);
 	TEST_ASSERT(vm->fd >= 0, KVM_IOCTL_ERROR(KVM_CREATE_VM, vm->fd));
Signature · 1 line
2.34.1