From: Ivan Orlov <ivan.orlov@codethink.co.uk>
To: pbonzini@redhat.com, shuah@kernel.org
Cc: Ivan Orlov <ivan.orlov@codethink.co.uk>,
kvm@vger.kernel.org, linux-kselftest@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: [PATCH] KVM: selftests: Check API version when creating VM
Date: Thu, 9 May 2024 16:37:57 +0100 [thread overview]
Message-ID: <20240509153757.42032-1-ivan.orlov@codethink.co.uk> (raw)
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 --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));
--
2.34.1
reply other threads:[~2024-05-09 15:38 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240509153757.42032-1-ivan.orlov@codethink.co.uk \
--to=ivan.orlov@codethink.co.uk \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=shuah@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).