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

From: Ackerley Tng
Date: Tue Sep 10 2024 - 19:51:23 EST


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@xxxxxxxxxx>

---
.../testing/selftests/kvm/include/test_util.h | 16 ++++
tools/testing/selftests/kvm/lib/test_util.c | 74 +++++++++++++++++++
2 files changed, 90 insertions(+)

diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testing/selftests/kvm/include/test_util.h
index 3e473058849f..011e757d4e2c 100644
--- a/tools/testing/selftests/kvm/include/test_util.h
+++ b/tools/testing/selftests/kvm/include/test_util.h
@@ -142,6 +142,16 @@ struct vm_mem_backing_src_alias {
uint32_t flag;
};

+enum vm_private_mem_backing_src_type {
+ VM_PRIVATE_MEM_SRC_GUEST_MEM, /* Use default page size */
+ VM_PRIVATE_MEM_SRC_HUGETLB, /* Use kernel default page size for hugetlb pages */
+ VM_PRIVATE_MEM_SRC_HUGETLB_2MB,
+ VM_PRIVATE_MEM_SRC_HUGETLB_1GB,
+ NUM_PRIVATE_MEM_SRC_TYPES,
+};
+
+#define DEFAULT_VM_PRIVATE_MEM_SRC VM_PRIVATE_MEM_SRC_GUEST_MEM
+
#define MIN_RUN_DELAY_NS 200000UL

bool thp_configured(void);
@@ -152,6 +162,12 @@ 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 private_mem_backing_src_help(const char *flag);
+enum vm_private_mem_backing_src_type parse_private_mem_backing_src_type(const char *type_name);
+const struct vm_mem_backing_src_alias *vm_private_mem_backing_src_alias(uint32_t i);
+size_t get_private_mem_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 8ed0b74ae837..d0a9b5ee0c01 100644
--- a/tools/testing/selftests/kvm/lib/test_util.c
+++ b/tools/testing/selftests/kvm/lib/test_util.c
@@ -15,6 +15,7 @@
#include <sys/syscall.h>
#include <linux/mman.h>
#include "linux/kernel.h"
+#include <linux/kvm.h>

#include "test_util.h"

@@ -288,6 +289,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_private_mem_backing_src_alias(uint32_t i)
+{
+ static const struct vm_mem_backing_src_alias aliases[] = {
+ [VM_PRIVATE_MEM_SRC_GUEST_MEM] = {
+ .name = "private_mem_guest_mem",
+ .flag = 0,
+ },
+ [VM_PRIVATE_MEM_SRC_HUGETLB] = {
+ .name = "private_mem_hugetlb",
+ .flag = KVM_GUEST_MEMFD_HUGETLB,
+ },
+ [VM_PRIVATE_MEM_SRC_HUGETLB_2MB] = {
+ .name = "private_mem_hugetlb_2mb",
+ .flag = KVM_GUEST_MEMFD_HUGETLB | KVM_GUEST_MEMFD_HUGE_2MB,
+ },
+ [VM_PRIVATE_MEM_SRC_HUGETLB_1GB] = {
+ .name = "private_mem_hugetlb_1gb",
+ .flag = KVM_GUEST_MEMFD_HUGETLB | KVM_GUEST_MEMFD_HUGE_1GB,
+ },
+ };
+ _Static_assert(ARRAY_SIZE(aliases) == NUM_PRIVATE_MEM_SRC_TYPES,
+ "Missing new backing private mem src types?");
+
+ TEST_ASSERT(i < NUM_PRIVATE_MEM_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)
@@ -308,6 +337,20 @@ size_t get_backing_src_pagesz(uint32_t i)
}
}

+size_t get_private_mem_backing_src_pagesz(uint32_t i)
+{
+ uint32_t flag = vm_private_mem_backing_src_alias(i)->flag;
+
+ switch (i) {
+ case VM_PRIVATE_MEM_SRC_GUEST_MEM:
+ return getpagesize();
+ case VM_PRIVATE_MEM_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);
@@ -344,6 +387,37 @@ enum vm_mem_backing_src_type parse_backing_src_type(const char *type_name)
return -1;
}

+static void print_available_private_mem_backing_src_types(const char *prefix)
+{
+ int i;
+
+ printf("%sAvailable private mem backing src types:\n", prefix);
+
+ for (i = 0; i < NUM_PRIVATE_MEM_SRC_TYPES; i++)
+ printf("%s %s\n", prefix, vm_private_mem_backing_src_alias(i)->name);
+}
+
+void private_mem_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_private_mem_backing_src_alias(DEFAULT_VM_PRIVATE_MEM_SRC)->name);
+ print_available_private_mem_backing_src_types(" ");
+}
+
+enum vm_private_mem_backing_src_type parse_private_mem_backing_src_type(const char *type_name)
+{
+ int i;
+
+ for (i = 0; i < NUM_PRIVATE_MEM_SRC_TYPES; i++)
+ if (!strcmp(type_name, vm_private_mem_backing_src_alias(i)->name))
+ return i;
+
+ print_available_private_mem_backing_src_types("");
+ TEST_FAIL("Unknown private mem backing src type: %s", type_name);
+ return -1;
+}
+
long get_run_delay(void)
{
char path[64];
--
2.46.0.598.g6f2099f65c-goog