[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <20230821194411.2165757-1-ackerleytng@google.com>
Date: Mon, 21 Aug 2023 19:44:11 +0000
From: Ackerley Tng <ackerleytng@...gle.com>
To: pbonzini@...hat.com, seanjc@...gle.com, tglx@...utronix.de,
x86@...nel.org, kvm@...r.kernel.org, linux-kernel@...r.kernel.org,
linux-kselftest@...r.kernel.org
Cc: mingo@...hat.com, bp@...en8.de, dave.hansen@...ux.intel.com,
hpa@...or.com, shuah@...nel.org, andrew.jones@...ux.dev,
ricarkol@...gle.com, chao.p.peng@...ux.intel.com, tabba@...gle.com,
jarkko@...nel.org, yu.c.zhang@...ux.intel.com,
vannapurve@...gle.com, ackerleytng@...gle.com,
erdemaktas@...gle.com, mail@...iej.szmigiero.name, vbabka@...e.cz,
david@...hat.com, qperret@...gle.com, michael.roth@....com,
wei.w.wang@...el.com, liam.merwick@...cle.com,
isaku.yamahata@...il.com, kirill.shutemov@...ux.intel.com
Subject: [PATCH] KVM: selftests: Add tests - invalid inputs for KVM_CREATE_GUEST_MEMFD
Test that invalid inputs for KVM_CREATE_GUEST_MEMFD, such as
non-page-aligned page size and invalid flags, are rejected by the
KVM_CREATE_GUEST_MEMFD with EINVAL
Signed-off-by: Ackerley Tng <ackerleytng@...gle.com>
---
.../testing/selftests/kvm/guest_memfd_test.c | 33 +++++++++++++++++++
.../selftests/kvm/include/kvm_util_base.h | 11 +++++--
2 files changed, 42 insertions(+), 2 deletions(-)
diff --git a/tools/testing/selftests/kvm/guest_memfd_test.c b/tools/testing/selftests/kvm/guest_memfd_test.c
index eb93c608a7e0..a8e37f001297 100644
--- a/tools/testing/selftests/kvm/guest_memfd_test.c
+++ b/tools/testing/selftests/kvm/guest_memfd_test.c
@@ -90,6 +90,37 @@ static void test_fallocate(int fd, size_t page_size, size_t total_size)
TEST_ASSERT(!ret, "fallocate to restore punched hole should succeed");
}
+static void test_create_guest_memfd_invalid(struct kvm_vm *vm, size_t page_size)
+{
+ int fd;
+ uint64_t size;
+ uint64_t flags;
+ uint64_t valid_flags = 0;
+
+ for (size = 1; size < page_size; size++) {
+ fd = __vm_create_guest_memfd(vm, size, 0);
+ TEST_ASSERT(
+ fd == -1,
+ "Creating guest memfds with non-page-aligned page sizes should fail");
+ TEST_ASSERT(errno == EINVAL, "... and errno should be set to EINVAL");
+ }
+
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
+ valid_flags = KVM_GUEST_MEMFD_ALLOW_HUGEPAGE;
+#endif
+
+ for (flags = 1; flags; flags <<= 1) {
+ if (flags & valid_flags)
+ continue;
+
+ fd = __vm_create_guest_memfd(vm, page_size, flags);
+ TEST_ASSERT(
+ fd == -1,
+ "Creating guest memfds with invalid flags should fail");
+ TEST_ASSERT(errno == EINVAL, "... and errno should be set to EINVAL");
+ }
+}
+
int main(int argc, char *argv[])
{
@@ -103,6 +134,8 @@ int main(int argc, char *argv[])
vm = vm_create_barebones();
+ test_create_guest_memfd_invalid(vm, page_size);
+
fd = vm_create_guest_memfd(vm, total_size, 0);
test_file_read_write(fd);
diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h
index 39b38c75b99c..8bdfadd72349 100644
--- a/tools/testing/selftests/kvm/include/kvm_util_base.h
+++ b/tools/testing/selftests/kvm/include/kvm_util_base.h
@@ -474,7 +474,8 @@ static inline uint64_t vm_get_stat(struct kvm_vm *vm, const char *stat_name)
}
void vm_create_irqchip(struct kvm_vm *vm);
-static inline int vm_create_guest_memfd(struct kvm_vm *vm, uint64_t size,
+
+static inline int __vm_create_guest_memfd(struct kvm_vm *vm, uint64_t size,
uint64_t flags)
{
struct kvm_create_guest_memfd gmem = {
@@ -482,7 +483,13 @@ static inline int vm_create_guest_memfd(struct kvm_vm *vm, uint64_t size,
.flags = flags,
};
- int fd = __vm_ioctl(vm, KVM_CREATE_GUEST_MEMFD, &gmem);
+ return __vm_ioctl(vm, KVM_CREATE_GUEST_MEMFD, &gmem);
+}
+
+static inline int vm_create_guest_memfd(struct kvm_vm *vm, uint64_t size,
+ uint64_t flags)
+{
+ int fd = __vm_create_guest_memfd(vm, size, flags);
TEST_ASSERT(fd >= 0, KVM_IOCTL_ERROR(KVM_CREATE_GUEST_MEMFD, fd));
return fd;
--
2.42.0.rc1.204.g551eb34607-goog
Powered by blists - more mailing lists