Add a selftest to verify the basic functionality of guest_memfd():

+ file descriptor created with the guest_memfd() ioctl does not allow
  read/write/mmap operations
+ file size and block size as returned from fstat are as expected
+ fallocate on the fd checks that offset/length on
  fallocate(FALLOC_FL_PUNCH_HOLE) should be page aligned

Signed-off-by: Chao Peng <chao.p.p...@linux.intel.com>
Co-developed-by: Ackerley Tng <ackerley...@google.com>
Signed-off-by: Ackerley Tng <ackerley...@google.com>
Signed-off-by: Sean Christopherson <sea...@google.com>
---
 tools/testing/selftests/kvm/Makefile          |   1 +
 .../testing/selftests/kvm/guest_memfd_test.c  | 114 ++++++++++++++++++
 2 files changed, 115 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/guest_memfd_test.c

diff --git a/tools/testing/selftests/kvm/Makefile 
b/tools/testing/selftests/kvm/Makefile
index fdc7dff8d6ae..18c43336ede3 100644
--- a/tools/testing/selftests/kvm/Makefile
+++ b/tools/testing/selftests/kvm/Makefile
@@ -123,6 +123,7 @@ TEST_GEN_PROGS_x86_64 += access_tracking_perf_test
 TEST_GEN_PROGS_x86_64 += demand_paging_test
 TEST_GEN_PROGS_x86_64 += dirty_log_test
 TEST_GEN_PROGS_x86_64 += dirty_log_perf_test
+TEST_GEN_PROGS_x86_64 += guest_memfd_test
 TEST_GEN_PROGS_x86_64 += hardware_disable_test
 TEST_GEN_PROGS_x86_64 += kvm_create_max_vcpus
 TEST_GEN_PROGS_x86_64 += kvm_page_table_test
diff --git a/tools/testing/selftests/kvm/guest_memfd_test.c 
b/tools/testing/selftests/kvm/guest_memfd_test.c
new file mode 100644
index 000000000000..d698f9fde987
--- /dev/null
+++ b/tools/testing/selftests/kvm/guest_memfd_test.c
@@ -0,0 +1,114 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright Intel Corporation, 2023
+ *
+ * Author: Chao Peng <chao.p.p...@linux.intel.com>
+ */
+
+#define _GNU_SOURCE
+#include "test_util.h"
+#include "kvm_util_base.h"
+#include <linux/falloc.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+#include <stdio.h>
+#include <fcntl.h>
+
+static void test_file_read_write(int fd)
+{
+       char buf[64];
+
+       TEST_ASSERT(read(fd, buf, sizeof(buf)) < 0,
+                   "read on a guest_mem fd should fail");
+       TEST_ASSERT(write(fd, buf, sizeof(buf)) < 0,
+                   "write on a guest_mem fd should fail");
+       TEST_ASSERT(pread(fd, buf, sizeof(buf), 0) < 0,
+                   "pread on a guest_mem fd should fail");
+       TEST_ASSERT(pwrite(fd, buf, sizeof(buf), 0) < 0,
+                   "pwrite on a guest_mem fd should fail");
+}
+
+static void test_mmap(int fd, size_t page_size)
+{
+       char *mem;
+
+       mem = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+       ASSERT_EQ(mem, MAP_FAILED);
+}
+
+static void test_file_size(int fd, size_t page_size, size_t total_size)
+{
+       struct stat sb;
+       int ret;
+
+       ret = fstat(fd, &sb);
+       TEST_ASSERT(!ret, "fstat should succeed");
+       ASSERT_EQ(sb.st_size, total_size);
+       ASSERT_EQ(sb.st_blksize, page_size);
+}
+
+static void test_fallocate(int fd, size_t page_size, size_t total_size)
+{
+       int ret;
+
+       ret = fallocate(fd, FALLOC_FL_KEEP_SIZE, 0, total_size);
+       TEST_ASSERT(!ret, "fallocate with aligned offset and size should 
succeed");
+
+       ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE,
+                       page_size - 1, page_size);
+       TEST_ASSERT(ret, "fallocate with unaligned offset should fail");
+
+       ret = fallocate(fd, FALLOC_FL_KEEP_SIZE, total_size, page_size);
+       TEST_ASSERT(ret, "fallocate beginning at total_size should fail");
+
+       ret = fallocate(fd, FALLOC_FL_KEEP_SIZE, total_size + page_size, 
page_size);
+       TEST_ASSERT(ret, "fallocate beginning at total_size should fail");
+
+       ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE,
+                       total_size, page_size);
+       TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) at total_size should succeed");
+
+       ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE,
+                       total_size + page_size, page_size);
+       TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) after total_size should 
succeed");
+
+       ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE,
+                       page_size, page_size - 1);
+       TEST_ASSERT(ret, "fallocate with unaligned size should fail");
+
+       ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE,
+                       page_size, page_size);
+       TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) with aligned offset and size 
should succeed");
+
+       ret = fallocate(fd, FALLOC_FL_KEEP_SIZE, page_size, page_size);
+       TEST_ASSERT(!ret, "fallocate to restore punched hole should succeed");
+}
+
+
+int main(int argc, char *argv[])
+{
+       size_t page_size;
+       size_t total_size;
+       int fd;
+       struct kvm_vm *vm;
+
+       page_size = getpagesize();
+       total_size = page_size * 4;
+
+       vm = vm_create_barebones();
+
+       fd = vm_create_guest_memfd(vm, total_size, 0);
+
+       test_file_read_write(fd);
+       test_mmap(fd, page_size);
+       test_file_size(fd, page_size, total_size);
+       test_fallocate(fd, page_size, total_size);
+
+       close(fd);
+}
-- 
2.41.0.255.g8b1d071c50-goog

Reply via email to