diff mbox series

KVM: selftests: Check API version when creating VM

Message ID 20240509153757.42032-1-ivan.orlov@codethink.co.uk (mailing list archive)
State New
Headers show
Series KVM: selftests: Check API version when creating VM | expand

Commit Message

Ivan Orlov May 9, 2024, 3:37 p.m. UTC
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.

Signed-off-by: Ivan Orlov <ivan.orlov@codethink.co.uk>
---
 .../testing/selftests/kvm/include/kvm_util_base.h  |  2 ++
 tools/testing/selftests/kvm/lib/kvm_util.c         | 14 ++++++++++++++
 2 files changed, 16 insertions(+)
diff mbox series

Patch

diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h
index 3e0db283a46a..d7a83387ae33 100644
--- a/tools/testing/selftests/kvm/include/kvm_util_base.h
+++ b/tools/testing/selftests/kvm/include/kvm_util_base.h
@@ -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)
 {
diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
index b2262b5fad9e..58a5deccb388 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -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));