diff mbox series

[RFC,18/19] KVM: selftests: Support various types of backing sources for private memory

Message ID 5f0d27ce06c03761974264bd8a890614ea7ecb32.1686077275.git.ackerleytng@google.com
State Superseded
Headers show
Series hugetlb support for KVM guest_mem | expand

Commit Message

Ackerley Tng June 6, 2023, 7:04 p.m. UTC
Adds support for various type of backing sources for private
memory (in the sense of confidential computing), similar to the
backing sources available for shared memory.

Signed-off-by: Ackerley Tng <ackerleytng@google.com>
---
 .../testing/selftests/kvm/include/test_util.h | 14 ++++
 tools/testing/selftests/kvm/lib/test_util.c   | 74 +++++++++++++++++++
 2 files changed, 88 insertions(+)
diff mbox series

Patch

diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testing/selftests/kvm/include/test_util.h
index a6e9f215ce70..899ea15ca8a9 100644
--- a/tools/testing/selftests/kvm/include/test_util.h
+++ b/tools/testing/selftests/kvm/include/test_util.h
@@ -122,6 +122,16 @@  struct vm_mem_backing_src_alias {
 	uint32_t flag;
 };
 
+enum vm_pmem_backing_src_type {
+	VM_PMEM_SRC_GMEM,
+	VM_PMEM_SRC_HUGETLB,  /* Use kernel default page size for hugetlb pages */
+	VM_PMEM_SRC_HUGETLB_2MB,
+	VM_PMEM_SRC_HUGETLB_1GB,
+	NUM_PMEM_SRC_TYPES,
+};
+
+#define DEFAULT_VM_PMEM_SRC VM_PMEM_SRC_GMEM
+
 #define MIN_RUN_DELAY_NS	200000UL
 
 bool thp_configured(void);
@@ -132,6 +142,10 @@  size_t get_backing_src_pagesz(uint32_t i);
 bool is_backing_src_hugetlb(uint32_t i);
 void backing_src_help(const char *flag);
 enum vm_mem_backing_src_type parse_backing_src_type(const char *type_name);
+void pmem_backing_src_help(const char *flag);
+enum vm_pmem_backing_src_type parse_pmem_backing_src_type(const char *type_name);
+const struct vm_mem_backing_src_alias *vm_pmem_backing_src_alias(uint32_t i);
+size_t get_pmem_backing_src_pagesz(uint32_t i);
 long get_run_delay(void);
 
 /*
diff --git a/tools/testing/selftests/kvm/lib/test_util.c b/tools/testing/selftests/kvm/lib/test_util.c
index b772193f6c18..62efb7b8ba51 100644
--- a/tools/testing/selftests/kvm/lib/test_util.c
+++ b/tools/testing/selftests/kvm/lib/test_util.c
@@ -8,6 +8,7 @@ 
 #include <assert.h>
 #include <ctype.h>
 #include <limits.h>
+#include <linux/kvm.h>
 #include <stdlib.h>
 #include <time.h>
 #include <sys/stat.h>
@@ -287,6 +288,34 @@  const struct vm_mem_backing_src_alias *vm_mem_backing_src_alias(uint32_t i)
 	return &aliases[i];
 }
 
+const struct vm_mem_backing_src_alias *vm_pmem_backing_src_alias(uint32_t i)
+{
+	static const struct vm_mem_backing_src_alias aliases[] = {
+		[VM_PMEM_SRC_GMEM] = {
+			.name = "pmem_gmem",
+			.flag = 0,
+		},
+		[VM_PMEM_SRC_HUGETLB] = {
+			.name = "pmem_hugetlb",
+			.flag = KVM_GUEST_MEMFD_HUGETLB,
+		},
+		[VM_PMEM_SRC_HUGETLB_2MB] = {
+			.name = "pmem_hugetlb_2mb",
+			.flag = KVM_GUEST_MEMFD_HUGETLB | KVM_GUEST_MEMFD_HUGE_2MB,
+		},
+		[VM_PMEM_SRC_HUGETLB_1GB] = {
+			.name = "pmem_hugetlb_1gb",
+			.flag = KVM_GUEST_MEMFD_HUGETLB | KVM_GUEST_MEMFD_HUGE_1GB,
+		},
+	};
+	_Static_assert(ARRAY_SIZE(aliases) == NUM_PMEM_SRC_TYPES,
+		       "Missing new backing private mem src types?");
+
+	TEST_ASSERT(i < NUM_PMEM_SRC_TYPES, "Private mem backing src type ID %d too big", i);
+
+	return &aliases[i];
+}
+
 #define MAP_HUGE_PAGE_SIZE(x) (1ULL << ((x >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK))
 
 size_t get_backing_src_pagesz(uint32_t i)
@@ -307,6 +336,20 @@  size_t get_backing_src_pagesz(uint32_t i)
 	}
 }
 
+size_t get_pmem_backing_src_pagesz(uint32_t i)
+{
+	uint32_t flag = vm_pmem_backing_src_alias(i)->flag;
+
+	switch (i) {
+	case VM_PMEM_SRC_GMEM:
+		return getpagesize();
+	case VM_PMEM_SRC_HUGETLB:
+		return get_def_hugetlb_pagesz();
+	default:
+		return MAP_HUGE_PAGE_SIZE(flag);
+	}
+}
+
 bool is_backing_src_hugetlb(uint32_t i)
 {
 	return !!(vm_mem_backing_src_alias(i)->flag & MAP_HUGETLB);
@@ -343,6 +386,37 @@  enum vm_mem_backing_src_type parse_backing_src_type(const char *type_name)
 	return -1;
 }
 
+static void print_available_pmem_backing_src_types(const char *prefix)
+{
+	int i;
+
+	printf("%sAvailable private mem backing src types:\n", prefix);
+
+	for (i = 0; i < NUM_PMEM_SRC_TYPES; i++)
+		printf("%s    %s\n", prefix, vm_pmem_backing_src_alias(i)->name);
+}
+
+void pmem_backing_src_help(const char *flag)
+{
+	printf(" %s: specify the type of memory that should be used to\n"
+	       "     back guest private memory. (default: %s)\n",
+	       flag, vm_pmem_backing_src_alias(DEFAULT_VM_MEM_SRC)->name);
+	print_available_pmem_backing_src_types("     ");
+}
+
+enum vm_pmem_backing_src_type parse_pmem_backing_src_type(const char *type_name)
+{
+	int i;
+
+	for (i = 0; i < NUM_SRC_TYPES; i++)
+		if (!strcmp(type_name, vm_pmem_backing_src_alias(i)->name))
+			return i;
+
+	print_available_pmem_backing_src_types("");
+	TEST_FAIL("Unknown private mem backing src type: %s", type_name);
+	return -1;
+}
+
 long get_run_delay(void)
 {
 	char path[64];