Skip to contentSkip to conversation

Add support for restricted guest memory in kvmtool

RFC PATCH In review Fuad Tabba 2 participants 37 messages Dec 2, 2022 – Dec 7, 2022

Subsystems (2)
lore
Share & tools
lore

Fuad Tabba

author
#0 lore raw

cover Add support for restricted guest memory in kvmtool

The main goal of this patch series is to add support for the the restricted guest memory proposali (V9) [1] to kvmtool (V10 was released today [2]). This proposal is still being discussed, but it seems to be close to its final form. The intention is that the restricted guest memory would be used in various confidential computing environments, such as TDX and pKVM.

This series is intended to work with the kernel in of the V9 proposal [1], in addition to work to port it to pKVM [3]. It has been tested on qemu/arm64.

The patch series is divided as follows:

Patches 1--4:
General fixes and tidying up

Patches 5--18:
Move kvmtool from allocating guest vm memory using anonymous mmap to using memfd/ftruncate. The main motivation is to support the fd-based kvm guest memory proposal [1, 2]. It also facilitates using ipc memory sharing should that be needed in the future. It also moves kvmtool to using only a file based backend for guest memory allocation, with the file descriptor being the canonical reference to guest memory. The idea is to refer to all allocated guest memory via a file descriptor.

Patches 19--28:
Add architecture-independent framework to support restricted guest memory.

Patches 29--32:
Add pKVM-specific (arm64) support for restricted guest memory.

I had posted a subset of this series earlier covering patches 1--18 [4]. This series incorporates fixes and suggestions from Alex into those patches.

Cheers,
/fuad

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://android-kvm.googlesource.com/kvmtool/+/refs/heads/tabba/fdmem-v9-core
[4] https://lore.kernel.org/all/[email protected]/

Fuad Tabba (31):
Initialize the return value in kvm__for_each_mem_bank() Remove newline from end of die() aborts
Make mmap_hugetlbfs() static
Rename parameter in mmap_anon_or_hugetlbfs()
Add hostmem va to debug print
Factor out getting the hugetlb block size
Use memfd for hugetlbfs when allocating guest ram Make blk_size a parameter and pass it to mmap_hugetlbfs() Use memfd for all guest ram allocations
Allocate pvtime memory with memfd
Allocate vesa memory with memfd
Add a function that allocates aligned memory if specified Use new function to align memory
Remove struct fields and code used for alignment Replace kvm__arch_delete_ram() with kvm__delete_ram() Remove no-longer used macro
Factor out set_user_memory_region code
Pass the memory file descriptor and offset when registering ram Add memfd_restricted system call
Add kvm linux headers and structure extensions for restricted_fd Add option for enabling restricted memory for guests Change guest ram mapping from private to shared Change pvtime mapping from private to shared
Change vesa mapping from private to shared
Allocate guest memory as restricted if needed
Use the new fd-based extended memory region
Track the memfd in the bank
Add functions for mapping/unmapping guest memory
pkvm: Enable exit hypercall capability if supported
pkvm: Handle (un)share hypercalls coming from the guest
pkvm: Unmap all guest memory after initialization

Will Deacon (1):
pkvm: Add option to spawn a protected vm in pkvm

 arm/aarch32/include/kvm/kvm-arch.h |   1 +
 arm/aarch64/include/asm/kvm.h      |   7 +
 arm/aarch64/include/kvm/kvm-arch.h |   1 +
 arm/aarch64/kvm.c                  |  26 +++
 arm/aarch64/pvtime.c               |  20 +-
 arm/fdt.c                          |  18 ++
 arm/include/arm-common/fdt-arch.h  |   2 +-
 arm/include/arm-common/kvm-arch.h  |   7 -
 arm/kvm-cpu.c                      |  58 ++++++
 arm/kvm.c                          |  41 ++--
 arm/pci.c                          |   3 +
 builtin-run.c                      |   7 +
 framebuffer.c                      |   2 +
 hw/cfi_flash.c                     |   4 +-
 hw/vesa.c                          |  17 +-
 include/kvm/framebuffer.h          |   1 +
 include/kvm/kvm-config.h           |   2 +
 include/kvm/kvm.h                  |  25 ++-
 include/kvm/util.h                 |   5 +-
 include/linux/kvm.h                |  19 ++
 kvm.c                              | 301 ++++++++++++++++++++++++++---
 mips/kvm.c                         |  11 +-
 powerpc/kvm.c                      |   7 +-
 riscv/include/kvm/kvm-arch.h       |   7 -
 riscv/kvm.c                        |  26 +--
 util/util.c                        | 131 ++++++++++---
 vfio/core.c                        |   3 +-
 virtio/pci-modern.c                |   3 +
 x86/kvm.c                          |  11 +-
 29 files changed, 606 insertions(+), 160 deletions(-)

base-commit: e17d182ad3f797f01947fc234d95c96c050c534b

Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
author
#1 lore raw

RFC PATCH 1/32 Initialize the return value in kvm__for_each_mem_bank()

If none of the bank types match, the function would return an uninitialized value.

---
 kvm.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kvm.c +3 -2
index 42b8812..765dc71 100644
@@ -381,14 +381,15 @@ u64 host_to_guest_flat(struct kvm *kvm, void *ptr)
  * their type.
  *
  * If one call to @fun returns a non-zero value, stop iterating and return the
- * value. Otherwise, return zero.
+ * value. If none of the bank types match, return -ENODEV. Otherwise, return
+ * zero.
  */
 int kvm__for_each_mem_bank(struct kvm *kvm, enum kvm_mem_type type,
 			   int (*fun)(struct kvm *kvm, struct kvm_mem_bank *bank, void *data),
 			   void *data)
 {
-	int ret;
 	struct kvm_mem_bank *bank;
+	int ret = -ENODEV;
 
 	list_for_each_entry(bank, &kvm->mem_banks, list) {
 		if (type != KVM_MEM_TYPE_ALL && !(bank->type & type))
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
author
#2 lore raw

RFC PATCH 2/32 Remove newline from end of die() aborts

In reply to Fuad Tabba

die() already adds the newline at the end of the string.

---
 util/util.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
util/util.c +5 -5
index 1877105..786dfc5 100644
@@ -90,14 +90,14 @@ void *mmap_hugetlbfs(struct kvm *kvm, const char *htlbfs_path, u64 size)
 	unsigned long blk_size;
 
 	if (statfs(htlbfs_path, &sfs) < 0)
-		die("Can't stat %s\n", htlbfs_path);
+		die("Can't stat %s", htlbfs_path);
 
 	if ((unsigned int)sfs.f_type != HUGETLBFS_MAGIC)
-		die("%s is not hugetlbfs!\n", htlbfs_path);
+		die("%s is not hugetlbfs!", htlbfs_path);
 
 	blk_size = (unsigned long)sfs.f_bsize;
 	if (sfs.f_bsize == 0 || blk_size > size) {
-		die("Can't use hugetlbfs pagesize %ld for mem size %lld\n",
+		die("Can't use hugetlbfs pagesize %ld for mem size %lld",
 			blk_size, (unsigned long long)size);
 	}
 
@@ -106,10 +106,10 @@ void *mmap_hugetlbfs(struct kvm *kvm, const char *htlbfs_path, u64 size)
 	snprintf(mpath, PATH_MAX, "%s/kvmtoolXXXXXX", htlbfs_path);
 	fd = mkstemp(mpath);
 	if (fd < 0)
-		die("Can't open %s for hugetlbfs map\n", mpath);
+		die("Can't open %s for hugetlbfs map", mpath);
 	unlink(mpath);
 	if (ftruncate(fd, size) < 0)
-		die("Can't ftruncate for mem mapping size %lld\n",
+		die("Can't ftruncate for mem mapping size %lld",
 			(unsigned long long)size);
 	addr = mmap(NULL, size, PROT_RW, MAP_PRIVATE, fd, 0);
 	close(fd);
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
author
#3 lore raw

RFC PATCH 3/32 Make mmap_hugetlbfs() static

In reply to Fuad Tabba

This function isn't used outside of util.c.

No functional change intended.

---
 include/kvm/util.h | 1 -
 util/util.c        | 2 +-
 2 files changed, 1 insertion(+), 2 deletions(-)
include/kvm/util.h +0 -1
index b494548..b0c3684 100644
@@ -140,7 +140,6 @@ static inline int pow2_size(unsigned long x)
 }
 
 struct kvm;
-void *mmap_hugetlbfs(struct kvm *kvm, const char *htlbfs_path, u64 size);
 void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size);
 
 #endif /* KVM__UTIL_H */
util/util.c +1 -1
index 786dfc5..7a3fd62 100644
@@ -81,7 +81,7 @@ void die_perror(const char *s)
 	exit(1);
 }
 
-void *mmap_hugetlbfs(struct kvm *kvm, const char *htlbfs_path, u64 size)
+static void *mmap_hugetlbfs(struct kvm *kvm, const char *htlbfs_path, u64 size)
 {
 	char mpath[PATH_MAX];
 	int fd;
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
author
#4 lore raw

RFC PATCH 4/32 Rename parameter in mmap_anon_or_hugetlbfs()

In reply to Fuad Tabba

For consistency with other similar functions in the same file.

No functional change intended.

---
 util/util.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
util/util.c +5 -5
index 7a3fd62..9f9642e 100644
@@ -81,7 +81,7 @@ void die_perror(const char *s)
 	exit(1);
 }
 
-static void *mmap_hugetlbfs(struct kvm *kvm, const char *htlbfs_path, u64 size)
+static void *mmap_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size)
 {
 	char mpath[PATH_MAX];
 	int fd;
@@ -89,11 +89,11 @@ static void *mmap_hugetlbfs(struct kvm *kvm, const char *htlbfs_path, u64 size)
 	void *addr;
 	unsigned long blk_size;
 
-	if (statfs(htlbfs_path, &sfs) < 0)
-		die("Can't stat %s", htlbfs_path);
+	if (statfs(hugetlbfs_path, &sfs) < 0)
+		die("Can't stat %s", hugetlbfs_path);
 
 	if ((unsigned int)sfs.f_type != HUGETLBFS_MAGIC)
-		die("%s is not hugetlbfs!", htlbfs_path);
+		die("%s is not hugetlbfs!", hugetlbfs_path);
 
 	blk_size = (unsigned long)sfs.f_bsize;
 	if (sfs.f_bsize == 0 || blk_size > size) {
@@ -103,7 +103,7 @@ static void *mmap_hugetlbfs(struct kvm *kvm, const char *htlbfs_path, u64 size)
 
 	kvm->ram_pagesize = blk_size;
 
-	snprintf(mpath, PATH_MAX, "%s/kvmtoolXXXXXX", htlbfs_path);
+	snprintf(mpath, PATH_MAX, "%s/kvmtoolXXXXXX", hugetlbfs_path);
 	fd = mkstemp(mpath);
 	if (fd < 0)
 		die("Can't open %s for hugetlbfs map", mpath);
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
author
#5 lore raw

RFC PATCH 5/32 Add hostmem va to debug print

In reply to Fuad Tabba

Useful when debugging.

---
 arm/kvm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arm/kvm.c +2 -2
index d51cc15..c84983e 100644
@@ -66,8 +66,8 @@ void kvm__init_ram(struct kvm *kvm)
 
 	kvm->arch.memory_guest_start = phys_start;
 
-	pr_debug("RAM created at 0x%llx - 0x%llx",
-		 phys_start, phys_start + phys_size - 1);
+	pr_debug("RAM created at 0x%llx - 0x%llx (host_mem 0x%llx)",
+		 phys_start, phys_start + phys_size - 1, (u64)host_mem);
 }
 
 void kvm__arch_delete_ram(struct kvm *kvm)
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
author
#6 lore raw

RFC PATCH 6/32 Factor out getting the hugetlb block size

In reply to Fuad Tabba

This functionality will be needed separately in a future patch.

No functional change intended.

---
 util/util.c | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)
util/util.c +15 -9
index 9f9642e..2c6fcc5 100644
@@ -81,13 +81,9 @@ void die_perror(const char *s)
 	exit(1);
 }
 
-static void *mmap_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size)
+static u64 get_hugepage_blk_size(const char *hugetlbfs_path)
 {
-	char mpath[PATH_MAX];
-	int fd;
 	struct statfs sfs;
-	void *addr;
-	unsigned long blk_size;
 
 	if (statfs(hugetlbfs_path, &sfs) < 0)
 		die("Can't stat %s", hugetlbfs_path);
@@ -95,10 +91,20 @@ static void *mmap_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 siz
 	if ((unsigned int)sfs.f_type != HUGETLBFS_MAGIC)
 		die("%s is not hugetlbfs!", hugetlbfs_path);
 
-	blk_size = (unsigned long)sfs.f_bsize;
-	if (sfs.f_bsize == 0 || blk_size > size) {
-		die("Can't use hugetlbfs pagesize %ld for mem size %lld",
-			blk_size, (unsigned long long)size);
+	return sfs.f_bsize;
+}
+
+static void *mmap_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size)
+{
+	char mpath[PATH_MAX];
+	int fd;
+	void *addr;
+	u64 blk_size;
+
+	blk_size = get_hugepage_blk_size(hugetlbfs_path);
+	if (blk_size == 0 || blk_size > size) {
+		die("Can't use hugetlbfs pagesize %lld for mem size %lld",
+			(unsigned long long)blk_size, (unsigned long long)size);
 	}
 
 	kvm->ram_pagesize = blk_size;
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
author
#7 lore raw

RFC PATCH 7/32 Use memfd for hugetlbfs when allocating guest ram

In reply to Fuad Tabba

This removes the need of using a temporary file for the fd, and is the first step towards using memfd_create() across this series.

---
 util/util.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
util/util.c +11 -5
index 2c6fcc5..b347fa3 100644
@@ -6,6 +6,7 @@
 
 #include <kvm/kvm.h>
 #include <linux/magic.h>	/* For HUGETLBFS_MAGIC */
+#include <linux/memfd.h>
 #include <sys/mman.h>
 #include <sys/stat.h>
 #include <sys/statfs.h>
@@ -96,7 +97,8 @@ static u64 get_hugepage_blk_size(const char *hugetlbfs_path)
 
 static void *mmap_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size)
 {
-	char mpath[PATH_MAX];
+	const char *name = "kvmtool";
+	unsigned int flags = 0;
 	int fd;
 	void *addr;
 	u64 blk_size;
@@ -107,13 +109,17 @@ static void *mmap_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 siz
 			(unsigned long long)blk_size, (unsigned long long)size);
 	}
 
+	if (!is_power_of_two(blk_size))
+		die("Hugepage size must be a power of 2");
+
+	flags |= MFD_HUGETLB;
+	flags |= blk_size << MFD_HUGE_SHIFT;
+
 	kvm->ram_pagesize = blk_size;
 
-	snprintf(mpath, PATH_MAX, "%s/kvmtoolXXXXXX", hugetlbfs_path);
-	fd = mkstemp(mpath);
+	fd = memfd_create(name, flags);
 	if (fd < 0)
-		die("Can't open %s for hugetlbfs map", mpath);
-	unlink(mpath);
+		die_perror("Can't memfd_create for hugetlbfs map");
 	if (ftruncate(fd, size) < 0)
 		die("Can't ftruncate for mem mapping size %lld",
 			(unsigned long long)size);
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
author
#8 lore raw

RFC PATCH 8/32 Make blk_size a parameter and pass it to mmap_hugetlbfs()

In reply to Fuad Tabba

This is the first step of making this function more generic.

The main purpose of this patch is to make it easier to review the next one.

No functional change intended.

---
 util/util.c | 35 ++++++++++++++++++-----------------
 1 file changed, 18 insertions(+), 17 deletions(-)
util/util.c +18 -17
index b347fa3..93f895a 100644
@@ -95,19 +95,12 @@ static u64 get_hugepage_blk_size(const char *hugetlbfs_path)
 	return sfs.f_bsize;
 }
 
-static void *mmap_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size)
+static void *mmap_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size, u64 blk_size)
 {
 	const char *name = "kvmtool";
 	unsigned int flags = 0;
 	int fd;
 	void *addr;
-	u64 blk_size;
-
-	blk_size = get_hugepage_blk_size(hugetlbfs_path);
-	if (blk_size == 0 || blk_size > size) {
-		die("Can't use hugetlbfs pagesize %lld for mem size %lld",
-			(unsigned long long)blk_size, (unsigned long long)size);
-	}
 
 	if (!is_power_of_two(blk_size))
 		die("Hugepage size must be a power of 2");
@@ -115,8 +108,6 @@ static void *mmap_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 siz
 	flags |= MFD_HUGETLB;
 	flags |= blk_size << MFD_HUGE_SHIFT;
 
-	kvm->ram_pagesize = blk_size;
-
 	fd = memfd_create(name, flags);
 	if (fd < 0)
 		die_perror("Can't memfd_create for hugetlbfs map");
@@ -132,13 +123,23 @@ static void *mmap_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 siz
 /* This function wraps the decision between hugetlbfs map (if requested) or normal mmap */
 void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size)
 {
-	if (hugetlbfs_path)
-		/*
-		 * We don't /need/ to map guest RAM from hugetlbfs, but we do so
-		 * if the user specifies a hugetlbfs path.
-		 */
-		return mmap_hugetlbfs(kvm, hugetlbfs_path, size);
-	else {
+	u64 blk_size = 0;
+
+	/*
+	 * We don't /need/ to map guest RAM from hugetlbfs, but we do so
+	 * if the user specifies a hugetlbfs path.
+	 */
+	if (hugetlbfs_path) {
+		blk_size = get_hugepage_blk_size(hugetlbfs_path);
+
+		if (blk_size == 0 || blk_size > size) {
+			die("Can't use hugetlbfs pagesize %lld for mem size %lld\n",
+				(unsigned long long)blk_size, (unsigned long long)size);
+		}
+
+		kvm->ram_pagesize = blk_size;
+		return mmap_hugetlbfs(kvm, hugetlbfs_path, size, blk_size);
+	} else {
 		kvm->ram_pagesize = getpagesize();
 		return mmap(NULL, size, PROT_RW, MAP_ANON_NORESERVE, -1, 0);
 	}
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
author
#9 lore raw

RFC PATCH 9/32 Use memfd for all guest ram allocations

In reply to Fuad Tabba

Allocate all guest ram backed by memfd/ftruncate instead of anonymous mmap. This will make it easier to use kvm with fd-based kvm guest memory proposals [*]. It also would make it easier to use ipc memory sharing should that be needed in the future.

[*] https://lore.kernel.org/all/[email protected]/

---
 include/kvm/kvm.h  |  1 +
 include/kvm/util.h |  3 +++
 kvm.c              |  4 ++++
 util/util.c        | 30 ++++++++++++++++++------------
 4 files changed, 26 insertions(+), 12 deletions(-)
include/kvm/kvm.h +1 -0
index 3872dc6..d0d519b 100644
@@ -87,6 +87,7 @@ struct kvm {
 	struct kvm_config	cfg;
 	int			sys_fd;		/* For system ioctls(), i.e. /dev/kvm */
 	int			vm_fd;		/* For VM ioctls() */
+	int			ram_fd;		/* For guest memory. */
 	timer_t			timerid;	/* Posix timer for interrupts */
 
 	int			nrcpus;		/* Number of cpus to run */
include/kvm/util.h +3 -0
index b0c3684..74713d9 100644
@@ -140,6 +140,9 @@ static inline int pow2_size(unsigned long x)
 }
 
 struct kvm;
+int memfd_alloc(u64 size, bool hugetlb, u64 blk_size);
+void *mmap_anon_or_hugetlbfs_align(struct kvm *kvm, const char *hugetlbfs_path,
+				   u64 size, u64 align);
 void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size);
 
 #endif /* KVM__UTIL_H */
kvm.c +4 -0
index 765dc71..84a8675 100644
@@ -160,6 +160,7 @@ struct kvm *kvm__new(void)
 	mutex_init(&kvm->mem_banks_lock);
 	kvm->sys_fd = -1;
 	kvm->vm_fd = -1;
+	kvm->ram_fd = -1;
 
 #ifdef KVM_BRLOCK_DEBUG
 	kvm->brlock_sem = (pthread_rwlock_t) PTHREAD_RWLOCK_INITIALIZER;
@@ -174,6 +175,9 @@ int kvm__exit(struct kvm *kvm)
 
 	kvm__arch_delete_ram(kvm);
 
+	if (kvm->ram_fd >= 0)
+		close(kvm->ram_fd);
+
 	list_for_each_entry_safe(bank, tmp, &kvm->mem_banks, list) {
 		list_del(&bank->list);
 		free(bank);
util/util.c +18 -12
index 93f895a..54c2b26 100644
@@ -95,35 +95,36 @@ static u64 get_hugepage_blk_size(const char *hugetlbfs_path)
 	return sfs.f_bsize;
 }
 
-static void *mmap_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size, u64 blk_size)
+int memfd_alloc(u64 size, bool hugetlb, u64 blk_size)
 {
 	const char *name = "kvmtool";
 	unsigned int flags = 0;
 	int fd;
-	void *addr;
 
-	if (!is_power_of_two(blk_size))
-		die("Hugepage size must be a power of 2");
+	if (hugetlb) {
+		if (!is_power_of_two(blk_size))
+			die("Hugepage size must be a power of 2");
 
-	flags |= MFD_HUGETLB;
-	flags |= blk_size << MFD_HUGE_SHIFT;
+		flags |= MFD_HUGETLB;
+		flags |= blk_size << MFD_HUGE_SHIFT;
+	}
 
 	fd = memfd_create(name, flags);
 	if (fd < 0)
-		die_perror("Can't memfd_create for hugetlbfs map");
+		die_perror("Can't memfd_create for memory map");
+
 	if (ftruncate(fd, size) < 0)
 		die("Can't ftruncate for mem mapping size %lld",
 			(unsigned long long)size);
-	addr = mmap(NULL, size, PROT_RW, MAP_PRIVATE, fd, 0);
-	close(fd);
 
-	return addr;
+	return fd;
 }
 
 /* This function wraps the decision between hugetlbfs map (if requested) or normal mmap */
 void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size)
 {
 	u64 blk_size = 0;
+	int fd;
 
 	/*
 	 * We don't /need/ to map guest RAM from hugetlbfs, but we do so
@@ -138,9 +139,14 @@ void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 si
 		}
 
 		kvm->ram_pagesize = blk_size;
-		return mmap_hugetlbfs(kvm, hugetlbfs_path, size, blk_size);
 	} else {
 		kvm->ram_pagesize = getpagesize();
-		return mmap(NULL, size, PROT_RW, MAP_ANON_NORESERVE, -1, 0);
 	}
+
+	fd = memfd_alloc(size, hugetlbfs_path, blk_size);
+	if (fd < 0)
+		return MAP_FAILED;
+
+	kvm->ram_fd = fd;
+	return mmap(NULL, size, PROT_RW, MAP_PRIVATE, kvm->ram_fd, 0);
 }
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
author
#10 lore raw

RFC PATCH 10/32 Allocate pvtime memory with memfd

In reply to Fuad Tabba

Ensure that all guest memory is fd-based.

---
 arm/aarch64/pvtime.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
arm/aarch64/pvtime.c +15 -3
index 2933ac7..a7ba03e 100644
@@ -8,25 +8,35 @@
 #define ARM_PVTIME_STRUCT_SIZE		(64)
 
 static void *usr_mem;
+int user_mem_fd = -1;
 
 static int pvtime__alloc_region(struct kvm *kvm)
 {
 	char *mem;
+	int mem_fd;
 	int ret = 0;
 
-	mem = mmap(NULL, ARM_PVTIME_SIZE, PROT_RW,
-		   MAP_ANON_NORESERVE, -1, 0);
-	if (mem == MAP_FAILED)
+	mem_fd = memfd_alloc(ARM_PVTIME_SIZE, false, 0);
+	if (mem_fd < 0)
 		return -errno;
 
+	mem = mmap(NULL, ARM_PVTIME_SIZE, PROT_RW, MAP_PRIVATE, mem_fd, 0);
+	if (mem == MAP_FAILED) {
+		ret = -errno;
+		close(mem_fd);
+		return ret;
+	}
+
 	ret = kvm__register_ram(kvm, ARM_PVTIME_BASE,
 				ARM_PVTIME_SIZE, mem);
 	if (ret) {
 		munmap(mem, ARM_PVTIME_SIZE);
+		close(mem_fd);
 		return ret;
 	}
 
 	usr_mem = mem;
+	user_mem_fd = mem_fd;
 	return ret;
 }
 
@@ -38,7 +48,9 @@ static int pvtime__teardown_region(struct kvm *kvm)
 	kvm__destroy_mem(kvm, ARM_PVTIME_BASE,
 			 ARM_PVTIME_SIZE, usr_mem);
 	munmap(usr_mem, ARM_PVTIME_SIZE);
+	close(user_mem_fd);
 	usr_mem = NULL;
+	user_mem_fd = -1;
 	return 0;
 }
 
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
author
#11 lore raw

RFC PATCH 11/32 Allocate vesa memory with memfd

In reply to Fuad Tabba

Ensure that all guest memory is fd-based.

---
 framebuffer.c             |  2 ++
 hw/vesa.c                 | 15 +++++++++++++--
 include/kvm/framebuffer.h |  1 +
 3 files changed, 16 insertions(+), 2 deletions(-)
framebuffer.c +2 -0
index fb8f51d..a025293 100644
@@ -73,6 +73,8 @@ int fb__exit(struct kvm *kvm)
 				fb->targets[i]->stop(fb);
 
 		munmap(fb->mem, fb->mem_size);
+		if (fb->mem_fd >= 0)
+			close(fb->mem_fd);
 	}
 
 	return 0;
hw/vesa.c +13 -2
index 7f82cdb..522ffa3 100644
@@ -41,6 +41,7 @@ static struct framebuffer vesafb = {
 	.depth		= VESA_BPP,
 	.mem_addr	= VESA_MEM_ADDR,
 	.mem_size	= VESA_MEM_SIZE,
+	.mem_fd		= -1,
 };
 
 static void vesa_pci_io(struct kvm_cpu *vcpu, u64 addr, u8 *data, u32 len,
@@ -66,6 +67,7 @@ struct framebuffer *vesa__init(struct kvm *kvm)
 {
 	u16 vesa_base_addr;
 	char *mem;
+	int mem_fd;
 	int r;
 
 	BUILD_BUG_ON(!is_power_of_two(VESA_MEM_SIZE));
@@ -88,22 +90,31 @@ struct framebuffer *vesa__init(struct kvm *kvm)
 	if (r < 0)
 		goto unregister_ioport;
 
-	mem = mmap(NULL, VESA_MEM_SIZE, PROT_RW, MAP_ANON_NORESERVE, -1, 0);
-	if (mem == MAP_FAILED) {
+	mem_fd = memfd_alloc(ARM_PVTIME_SIZE, false, 0, 0);
+	if (mem_fd < 0) {
 		r = -errno;
 		goto unregister_device;
 	}
 
+	mem = mmap(NULL, VESA_MEM_SIZE, PROT_RW, MAP_PRIVATE, mem_fd, 0);
+	if (mem == MAP_FAILED) {
+		r = -errno;
+		goto close_memfd;
+	}
+
 	r = kvm__register_dev_mem(kvm, VESA_MEM_ADDR, VESA_MEM_SIZE, mem);
 	if (r < 0)
 		goto unmap_dev;
 
 	vesafb.mem = mem;
+	vesafb.mem_fd = mem_fd;
 	vesafb.kvm = kvm;
 	return fb__register(&vesafb);
 
 unmap_dev:
 	munmap(mem, VESA_MEM_SIZE);
+close_memfd:
+	close(mem_fd);
 unregister_device:
 	device__unregister(&vesa_device);
 unregister_ioport:
include/kvm/framebuffer.h +1 -0
index e3200e5..c340273 100644
@@ -22,6 +22,7 @@ struct framebuffer {
 	char				*mem;
 	u64				mem_addr;
 	u64				mem_size;
+	int				mem_fd;
 	struct kvm			*kvm;
 
 	unsigned long			nr_targets;
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
author
#12 lore raw

RFC PATCH 12/32 Add a function that allocates aligned memory if specified

In reply to Fuad Tabba

Add a variant of mmap_anon_or_hugetlbfs() that allocates memory aligned as specified. This function doesn't map or allocate more memory than the requested amount.

---
 util/util.c | 41 ++++++++++++++++++++++++++++++++++++++---
 1 file changed, 38 insertions(+), 3 deletions(-)
util/util.c +38 -3
index 54c2b26..9f83d70 100644
@@ -120,10 +120,17 @@ int memfd_alloc(u64 size, bool hugetlb, u64 blk_size)
 	return fd;
 }
 
-/* This function wraps the decision between hugetlbfs map (if requested) or normal mmap */
-void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size)
+/*
+ * This function allocates memory aligned to align_sz.
+ * It also wraps the decision between hugetlbfs (if requested) or normal mmap.
+ */
+void *mmap_anon_or_hugetlbfs_align(struct kvm *kvm, const char *hugetlbfs_path,
+				   u64 size, u64 align_sz)
 {
 	u64 blk_size = 0;
+	u64 total_map = size + align_sz;
+	u64 start_off, end_off;
+	void *addr_map, *addr_align;
 	int fd;
 
 	/*
@@ -143,10 +150,38 @@ void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 si
 		kvm->ram_pagesize = getpagesize();
 	}
 
+	/* Create a mapping with room for alignment without allocating. */
+	addr_map = mmap(NULL, total_map, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
+			-1, 0);
+	if (addr_map == MAP_FAILED)
+		return MAP_FAILED;
+
 	fd = memfd_alloc(size, hugetlbfs_path, blk_size);
 	if (fd < 0)
 		return MAP_FAILED;
 
+	/* Map the allocated memory in the fd to the specified alignment. */
+	addr_align = (void *)ALIGN((u64)addr_map, align_sz);
+	if (mmap(addr_align, size, PROT_RW, MAP_PRIVATE | MAP_FIXED, fd, 0) ==
+	    MAP_FAILED) {
+		close(fd);
+		return MAP_FAILED;
+	}
+
+	/* Remove the mapping for unused address ranges. */
+	start_off = addr_align - addr_map;
+	if (start_off)
+		munmap(addr_map, start_off);
+
+	end_off = align_sz - start_off;
+	if (end_off)
+		munmap((void *)((u64)addr_align + size), end_off);
+
 	kvm->ram_fd = fd;
-	return mmap(NULL, size, PROT_RW, MAP_PRIVATE, kvm->ram_fd, 0);
+	return addr_align;
+}
+
+void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size)
+{
+	return mmap_anon_or_hugetlbfs_align(kvm, hugetlbfs_path, size, 0);
 }
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
author
#13 lore raw

RFC PATCH 13/32 Use new function to align memory

In reply to Fuad Tabba

Use the new mmap_anon_or_hugetlbfs_align() to allocate memory aligned as needed instead of doing it at the caller while allocating and mapping more than needed.

---
 arm/kvm.c   | 10 +++++-----
 riscv/kvm.c | 10 +++++-----
 2 files changed, 10 insertions(+), 10 deletions(-)
arm/kvm.c +5 -5
index c84983e..0e5bfad 100644
@@ -37,17 +37,17 @@ void kvm__init_ram(struct kvm *kvm)
 	 * 2M trumps 64K, so let's go with that.
 	 */
 	kvm->ram_size = kvm->cfg.ram_size;
-	kvm->arch.ram_alloc_size = kvm->ram_size + SZ_2M;
-	kvm->arch.ram_alloc_start = mmap_anon_or_hugetlbfs(kvm,
+	kvm->arch.ram_alloc_size = kvm->ram_size;
+	kvm->arch.ram_alloc_start = mmap_anon_or_hugetlbfs_align(kvm,
 						kvm->cfg.hugetlbfs_path,
-						kvm->arch.ram_alloc_size);
+						kvm->arch.ram_alloc_size,
+						SZ_2M);
 
 	if (kvm->arch.ram_alloc_start == MAP_FAILED)
 		die("Failed to map %lld bytes for guest memory (%d)",
 		    kvm->arch.ram_alloc_size, errno);
 
-	kvm->ram_start = (void *)ALIGN((unsigned long)kvm->arch.ram_alloc_start,
-					SZ_2M);
+	kvm->ram_start = kvm->arch.ram_alloc_start;
 
 	madvise(kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size,
 		MADV_MERGEABLE);
riscv/kvm.c +5 -5
index 4d6f5cb..e26b4f0 100644
@@ -70,17 +70,17 @@ void kvm__arch_init(struct kvm *kvm)
 	 * 2M trumps 64K, so let's go with that.
 	 */
 	kvm->ram_size = min(kvm->cfg.ram_size, (u64)RISCV_MAX_MEMORY(kvm));
-	kvm->arch.ram_alloc_size = kvm->ram_size + SZ_2M;
-	kvm->arch.ram_alloc_start = mmap_anon_or_hugetlbfs(kvm,
+	kvm->arch.ram_alloc_size = kvm->ram_size;
+	kvm->arch.ram_alloc_start = mmap_anon_or_hugetlbfs_align(kvm,
 						kvm->cfg.hugetlbfs_path,
-						kvm->arch.ram_alloc_size);
+						kvm->arch.ram_alloc_size,
+						SZ_2M);
 
 	if (kvm->arch.ram_alloc_start == MAP_FAILED)
 		die("Failed to map %lld bytes for guest memory (%d)",
 		    kvm->arch.ram_alloc_size, errno);
 
-	kvm->ram_start = (void *)ALIGN((unsigned long)kvm->arch.ram_alloc_start,
-					SZ_2M);
+	kvm->ram_start = kvm->arch.ram_alloc_start;
 
 	madvise(kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size,
 		MADV_MERGEABLE);
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
author
#14 lore raw

RFC PATCH 14/32 Remove struct fields and code used for alignment

In reply to Fuad Tabba

Now that the allocator allocates aligned memory, remove arch-specific code and struct fields used for alignment.

No functional change intended.

---
 arm/include/arm-common/kvm-arch.h |  7 -------
 arm/kvm.c                         | 31 +++++++++++--------------------
 riscv/include/kvm/kvm-arch.h      |  7 -------
 riscv/kvm.c                       | 21 +++++++--------------
 4 files changed, 18 insertions(+), 48 deletions(-)
arm/include/arm-common/kvm-arch.h +0 -7
index b2ae373..654abc9 100644
@@ -96,13 +96,6 @@ static inline bool arm_addr_in_ioport_region(u64 phys_addr)
 }
 
 struct kvm_arch {
-	/*
-	 * We may have to align the guest memory for virtio, so keep the
-	 * original pointers here for munmap.
-	 */
-	void	*ram_alloc_start;
-	u64	ram_alloc_size;
-
 	/*
 	 * Guest addresses for memory layout.
 	 */
arm/kvm.c +11 -20
index 0e5bfad..770075e 100644
@@ -27,7 +27,6 @@ bool kvm__arch_cpu_supports_vm(void)
 void kvm__init_ram(struct kvm *kvm)
 {
 	u64 phys_start, phys_size;
-	void *host_mem;
 	int err;
 
 	/*
@@ -37,42 +36,34 @@ void kvm__init_ram(struct kvm *kvm)
 	 * 2M trumps 64K, so let's go with that.
 	 */
 	kvm->ram_size = kvm->cfg.ram_size;
-	kvm->arch.ram_alloc_size = kvm->ram_size;
-	kvm->arch.ram_alloc_start = mmap_anon_or_hugetlbfs_align(kvm,
-						kvm->cfg.hugetlbfs_path,
-						kvm->arch.ram_alloc_size,
-						SZ_2M);
+	kvm->ram_start = mmap_anon_or_hugetlbfs_align(kvm,
+						      kvm->cfg.hugetlbfs_path,
+						      kvm->ram_size, SZ_2M);
 
-	if (kvm->arch.ram_alloc_start == MAP_FAILED)
+	if (kvm->ram_start == MAP_FAILED)
 		die("Failed to map %lld bytes for guest memory (%d)",
-		    kvm->arch.ram_alloc_size, errno);
+		    kvm->ram_size, errno);
 
-	kvm->ram_start = kvm->arch.ram_alloc_start;
-
-	madvise(kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size,
-		MADV_MERGEABLE);
-
-	madvise(kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size,
-		MADV_HUGEPAGE);
+	madvise(kvm->ram_start, kvm->ram_size, MADV_MERGEABLE);
+	madvise(kvm->ram_start, kvm->ram_size, MADV_HUGEPAGE);
 
 	phys_start	= kvm->cfg.ram_addr;
 	phys_size	= kvm->ram_size;
-	host_mem	= kvm->ram_start;
 
-	err = kvm__register_ram(kvm, phys_start, phys_size, host_mem);
+	err = kvm__register_ram(kvm, phys_start, phys_size, kvm->ram_start);
 	if (err)
 		die("Failed to register %lld bytes of memory at physical "
 		    "address 0x%llx [err %d]", phys_size, phys_start, err);
 
 	kvm->arch.memory_guest_start = phys_start;
 
-	pr_debug("RAM created at 0x%llx - 0x%llx (host_mem 0x%llx)",
-		 phys_start, phys_start + phys_size - 1, (u64)host_mem);
+	pr_debug("RAM created at 0x%llx - 0x%llx (host ram_start 0x%llx)",
+		 phys_start, phys_start + phys_size - 1, (u64)kvm->ram_start);
 }
 
 void kvm__arch_delete_ram(struct kvm *kvm)
 {
-	munmap(kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size);
+	munmap(kvm->ram_start, kvm->ram_size);
 }
 
 void kvm__arch_read_term(struct kvm *kvm)
riscv/include/kvm/kvm-arch.h +0 -7
index 1e130f5..5bb7eee 100644
@@ -56,13 +56,6 @@
 struct kvm;
 
 struct kvm_arch {
-	/*
-	 * We may have to align the guest memory for virtio, so keep the
-	 * original pointers here for munmap.
-	 */
-	void	*ram_alloc_start;
-	u64	ram_alloc_size;
-
 	/*
 	 * Guest addresses for memory layout.
 	 */
riscv/kvm.c +7 -14
index e26b4f0..d05b8e4 100644
@@ -48,7 +48,7 @@ void kvm__init_ram(struct kvm *kvm)
 
 void kvm__arch_delete_ram(struct kvm *kvm)
 {
-	munmap(kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size);
+	munmap(kvm->ram_start, kvm->ram_size);
 }
 
 void kvm__arch_read_term(struct kvm *kvm)
@@ -70,23 +70,16 @@ void kvm__arch_init(struct kvm *kvm)
 	 * 2M trumps 64K, so let's go with that.
 	 */
 	kvm->ram_size = min(kvm->cfg.ram_size, (u64)RISCV_MAX_MEMORY(kvm));
-	kvm->arch.ram_alloc_size = kvm->ram_size;
-	kvm->arch.ram_alloc_start = mmap_anon_or_hugetlbfs_align(kvm,
-						kvm->cfg.hugetlbfs_path,
-						kvm->arch.ram_alloc_size,
-						SZ_2M);
+	kvm->ram_start = mmap_anon_or_hugetlbfs_align(kvm,
+						      kvm->cfg.hugetlbfs_path,
+						      kvm->ram_size, SZ_2M);
 
-	if (kvm->arch.ram_alloc_start == MAP_FAILED)
+	if (kvm->ram_start == MAP_FAILED)
 		die("Failed to map %lld bytes for guest memory (%d)",
 		    kvm->arch.ram_alloc_size, errno);
 
-	kvm->ram_start = kvm->arch.ram_alloc_start;
-
-	madvise(kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size,
-		MADV_MERGEABLE);
-
-	madvise(kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size,
-		MADV_HUGEPAGE);
+	madvise(kvm->ram_start, kvm->ram_size, MADV_MERGEABLE);
+	madvise(kvm->ram_start, kvm->ram_size, MADV_HUGEPAGE);
 }
 
 #define FDT_ALIGN	SZ_4M
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
author
#15 lore raw

RFC PATCH 15/32 Replace kvm__arch_delete_ram() with kvm__delete_ram()

In reply to Fuad Tabba

Now that deleting ram is the same across all architectures, no need for arch-specific deletion of ram.

No functional change intended.

---
 arm/kvm.c         |  5 -----
 include/kvm/kvm.h |  1 -
 kvm.c             | 13 +++++++++----
 mips/kvm.c        |  5 -----
 powerpc/kvm.c     |  5 -----
 riscv/kvm.c       |  5 -----
 x86/kvm.c         |  5 -----
 7 files changed, 9 insertions(+), 30 deletions(-)
arm/kvm.c +0 -5
index 770075e..5cceef8 100644
@@ -61,11 +61,6 @@ void kvm__init_ram(struct kvm *kvm)
 		 phys_start, phys_start + phys_size - 1, (u64)kvm->ram_start);
 }
 
-void kvm__arch_delete_ram(struct kvm *kvm)
-{
-	munmap(kvm->ram_start, kvm->ram_size);
-}
-
 void kvm__arch_read_term(struct kvm *kvm)
 {
 	serial8250__update_consoles(kvm);
include/kvm/kvm.h +0 -1
index d0d519b..f0be524 100644
@@ -198,7 +198,6 @@ void kvm__arch_validate_cfg(struct kvm *kvm);
 void kvm__arch_set_cmdline(char *cmdline, bool video);
 void kvm__arch_init(struct kvm *kvm);
 u64 kvm__arch_default_ram_address(void);
-void kvm__arch_delete_ram(struct kvm *kvm);
 int kvm__arch_setup_firmware(struct kvm *kvm);
 int kvm__arch_free_firmware(struct kvm *kvm);
 bool kvm__arch_cpu_supports_vm(void);
kvm.c +9 -4
index 84a8675..3a3383a 100644
@@ -169,14 +169,19 @@ struct kvm *kvm__new(void)
 	return kvm;
 }
 
-int kvm__exit(struct kvm *kvm)
+static void kvm__delete_ram(struct kvm *kvm)
 {
-	struct kvm_mem_bank *bank, *tmp;
-
-	kvm__arch_delete_ram(kvm);
+	munmap(kvm->ram_start, kvm->ram_size);
 
 	if (kvm->ram_fd >= 0)
 		close(kvm->ram_fd);
+}
+
+int kvm__exit(struct kvm *kvm)
+{
+	struct kvm_mem_bank *bank, *tmp;
+
+	kvm__delete_ram(kvm);
 
 	list_for_each_entry_safe(bank, tmp, &kvm->mem_banks, list) {
 		list_del(&bank->list);
mips/kvm.c +0 -5
index 0faa03a..0a0d025 100644
@@ -56,11 +56,6 @@ void kvm__init_ram(struct kvm *kvm)
 	}
 }
 
-void kvm__arch_delete_ram(struct kvm *kvm)
-{
-	munmap(kvm->ram_start, kvm->ram_size);
-}
-
 void kvm__arch_set_cmdline(char *cmdline, bool video)
 {
 
powerpc/kvm.c +0 -5
index 7b0d066..8d467e9 100644
@@ -148,11 +148,6 @@ void kvm__arch_init(struct kvm *kvm)
 			 SPAPR_PCI_IO_WIN_SIZE);
 }
 
-void kvm__arch_delete_ram(struct kvm *kvm)
-{
-	munmap(kvm->ram_start, kvm->ram_size);
-}
-
 void kvm__irq_trigger(struct kvm *kvm, int irq)
 {
 	kvm__irq_line(kvm, irq, 1);
riscv/kvm.c +0 -5
index d05b8e4..4a2a3df 100644
@@ -46,11 +46,6 @@ void kvm__init_ram(struct kvm *kvm)
 	kvm->arch.memory_guest_start = phys_start;
 }
 
-void kvm__arch_delete_ram(struct kvm *kvm)
-{
-	munmap(kvm->ram_start, kvm->ram_size);
-}
-
 void kvm__arch_read_term(struct kvm *kvm)
 {
 	serial8250__update_consoles(kvm);
x86/kvm.c +0 -5
index 328fa75..8d29904 100644
@@ -177,11 +177,6 @@ void kvm__arch_init(struct kvm *kvm)
 		die_perror("KVM_CREATE_IRQCHIP ioctl");
 }
 
-void kvm__arch_delete_ram(struct kvm *kvm)
-{
-	munmap(kvm->ram_start, kvm->ram_size);
-}
-
 void kvm__irq_line(struct kvm *kvm, int irq, int level)
 {
 	struct kvm_irq_level irq_level;
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
author
#16 lore raw

RFC PATCH 16/32 Remove no-longer used macro

In reply to Fuad Tabba

Not needed anymore since we're not doing anonymous allocation.

No functional change intended.

---
 include/kvm/util.h | 1 -
 1 file changed, 1 deletion(-)
include/kvm/util.h +0 -1
index 74713d9..79275ed 100644
@@ -35,7 +35,6 @@
 extern bool do_debug_print;
 
 #define PROT_RW (PROT_READ|PROT_WRITE)
-#define MAP_ANON_NORESERVE (MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE)
 
 extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
 extern void die_perror(const char *s) NORETURN;
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
author
#17 lore raw

RFC PATCH 17/32 Factor out set_user_memory_region code

In reply to Fuad Tabba

This is common code, and will be reused in the future when setting memory regions using file descriptors.

No functional change intended.

---
 kvm.c       | 53 ++++++++++++++++++++++++++++-------------------------
 util/util.c |  2 +-
 2 files changed, 29 insertions(+), 26 deletions(-)
kvm.c +28 -25
index 3a3383a..c71646f 100644
@@ -193,10 +193,30 @@ int kvm__exit(struct kvm *kvm)
 }
 core_exit(kvm__exit);
 
+
+static int set_user_memory_region(int vm_fd, u32 slot, u32 flags,
+				  u64 guest_phys, u64 size,
+				  u64 userspace_addr)
+{
+	int ret = 0;
+	struct kvm_userspace_memory_region mem = {
+		.slot			= slot,
+		.flags			= flags,
+		.guest_phys_addr	= guest_phys,
+		.memory_size		= size,
+		.userspace_addr		= (unsigned long)userspace_addr,
+	};
+
+	ret = ioctl(vm_fd, KVM_SET_USER_MEMORY_REGION, &mem);
+	if (ret < 0)
+		ret = -errno;
+
+	return ret;
+}
+
 int kvm__destroy_mem(struct kvm *kvm, u64 guest_phys, u64 size,
 		     void *userspace_addr)
 {
-	struct kvm_userspace_memory_region mem;
 	struct kvm_mem_bank *bank;
 	int ret;
 
@@ -220,18 +240,10 @@ int kvm__destroy_mem(struct kvm *kvm, u64 guest_phys, u64 size,
 		goto out;
 	}
 
-	mem = (struct kvm_userspace_memory_region) {
-		.slot			= bank->slot,
-		.guest_phys_addr	= guest_phys,
-		.memory_size		= 0,
-		.userspace_addr		= (unsigned long)userspace_addr,
-	};
-
-	ret = ioctl(kvm->vm_fd, KVM_SET_USER_MEMORY_REGION, &mem);
-	if (ret < 0) {
-		ret = -errno;
+	ret = set_user_memory_region(kvm->vm_fd, bank->slot, 0, guest_phys, 0,
+				     (u64) userspace_addr);
+	if (ret < 0)
 		goto out;
-	}
 
 	list_del(&bank->list);
 	free(bank);
@@ -246,7 +258,6 @@ out:
 int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size,
 		      void *userspace_addr, enum kvm_mem_type type)
 {
-	struct kvm_userspace_memory_region mem;
 	struct kvm_mem_bank *merged = NULL;
 	struct kvm_mem_bank *bank;
 	struct list_head *prev_entry;
@@ -327,19 +338,11 @@ int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size,
 		flags |= KVM_MEM_READONLY;
 
 	if (type != KVM_MEM_TYPE_RESERVED) {
-		mem = (struct kvm_userspace_memory_region) {
-			.slot			= slot,
-			.flags			= flags,
-			.guest_phys_addr	= guest_phys,
-			.memory_size		= size,
-			.userspace_addr		= (unsigned long)userspace_addr,
-		};
-
-		ret = ioctl(kvm->vm_fd, KVM_SET_USER_MEMORY_REGION, &mem);
-		if (ret < 0) {
-			ret = -errno;
+		ret = set_user_memory_region(kvm->vm_fd, slot, flags,
+					     guest_phys, size,
+					     (u64) userspace_addr);
+		if (ret < 0)
 			goto out;
-		}
 	}
 
 	list_add(&bank->list, prev_entry);
util/util.c +1 -1
index 9f83d70..1f2e1a6 100644
@@ -141,7 +141,7 @@ void *mmap_anon_or_hugetlbfs_align(struct kvm *kvm, const char *hugetlbfs_path,
 		blk_size = get_hugepage_blk_size(hugetlbfs_path);
 
 		if (blk_size == 0 || blk_size > size) {
-			die("Can't use hugetlbfs pagesize %lld for mem size %lld\n",
+			die("Can't use hugetlbfs pagesize %lld for mem size %lld",
 				(unsigned long long)blk_size, (unsigned long long)size);
 		}
 
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
author
#18 lore raw

RFC PATCH 18/32 Pass the memory file descriptor and offset when registering ram

In reply to Fuad Tabba

Since the memory file descriptor is the canonical reference to guest memory, pass that and the offset when registering guest memory. Future fd-based kvm proposals might even not require a userspace address [*].

No functional change intended.

[*] https://lore.kernel.org/all/[email protected]/

---
 arm/aarch64/pvtime.c |  2 +-
 arm/kvm.c            |  3 ++-
 hw/cfi_flash.c       |  4 +++-
 hw/vesa.c            |  2 +-
 include/kvm/kvm.h    | 17 +++++++++--------
 kvm.c                |  3 ++-
 mips/kvm.c           |  6 +++---
 powerpc/kvm.c        |  2 +-
 riscv/kvm.c          |  2 +-
 vfio/core.c          |  3 ++-
 x86/kvm.c            |  6 +++---
 11 files changed, 28 insertions(+), 22 deletions(-)
arm/aarch64/pvtime.c +1 -1
index a7ba03e..9b06ee4 100644
@@ -28,7 +28,7 @@ static int pvtime__alloc_region(struct kvm *kvm)
 	}
 
 	ret = kvm__register_ram(kvm, ARM_PVTIME_BASE,
-				ARM_PVTIME_SIZE, mem);
+				ARM_PVTIME_SIZE, mem, mem_fd, 0);
 	if (ret) {
 		munmap(mem, ARM_PVTIME_SIZE);
 		close(mem_fd);
arm/kvm.c +2 -1
index 5cceef8..8772a55 100644
@@ -50,7 +50,8 @@ void kvm__init_ram(struct kvm *kvm)
 	phys_start	= kvm->cfg.ram_addr;
 	phys_size	= kvm->ram_size;
 
-	err = kvm__register_ram(kvm, phys_start, phys_size, kvm->ram_start);
+	err = kvm__register_ram(kvm, phys_start, phys_size, kvm->ram_start,
+		kvm->ram_fd, 0);
 	if (err)
 		die("Failed to register %lld bytes of memory at physical "
 		    "address 0x%llx [err %d]", phys_size, phys_start, err);
hw/cfi_flash.c +3 -1
index 7faecdf..92a6567 100644
@@ -131,6 +131,7 @@ struct cfi_flash_device {
 	u32			size;
 
 	void			*flash_memory;
+	int			flash_fd;
 	u8			program_buffer[PROGRAM_BUFF_SIZE];
 	unsigned long		*lock_bm;
 	u64			block_address;
@@ -451,7 +452,7 @@ static int map_flash_memory(struct kvm *kvm, struct cfi_flash_device *sfdev)
 	int ret;
 
 	ret = kvm__register_mem(kvm, sfdev->base_addr, sfdev->size,
-				sfdev->flash_memory,
+				sfdev->flash_memory, sfdev->flash_fd, 0,
 				KVM_MEM_TYPE_RAM | KVM_MEM_TYPE_READONLY);
 	if (!ret)
 		sfdev->is_mapped = true;
@@ -583,6 +584,7 @@ static struct cfi_flash_device *create_flash_device_file(struct kvm *kvm,
 		ret = -errno;
 		goto out_free;
 	}
+	sfdev->flash_fd = fd;
 	sfdev->base_addr = KVM_FLASH_MMIO_BASE;
 	sfdev->state = READY;
 	sfdev->read_mode = READ_ARRAY;
hw/vesa.c +1 -1
index 522ffa3..277d638 100644
@@ -102,7 +102,7 @@ struct framebuffer *vesa__init(struct kvm *kvm)
 		goto close_memfd;
 	}
 
-	r = kvm__register_dev_mem(kvm, VESA_MEM_ADDR, VESA_MEM_SIZE, mem);
+	r = kvm__register_dev_mem(kvm, VESA_MEM_ADDR, VESA_MEM_SIZE, mem, mem_fd, 0);
 	if (r < 0)
 		goto unmap_dev;
 
include/kvm/kvm.h +9 -8
index f0be524..33cae9d 100644
@@ -135,24 +135,25 @@ bool kvm__emulate_io(struct kvm_cpu *vcpu, u16 port, void *data, int direction,
 bool kvm__emulate_mmio(struct kvm_cpu *vcpu, u64 phys_addr, u8 *data, u32 len, u8 is_write);
 int kvm__destroy_mem(struct kvm *kvm, u64 guest_phys, u64 size, void *userspace_addr);
 int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size, void *userspace_addr,
-		      enum kvm_mem_type type);
+		      int memfd, u64 offset, enum kvm_mem_type type);
 static inline int kvm__register_ram(struct kvm *kvm, u64 guest_phys, u64 size,
-				    void *userspace_addr)
+				    void *userspace_addr, int memfd, u64 offset)
 {
-	return kvm__register_mem(kvm, guest_phys, size, userspace_addr,
-				 KVM_MEM_TYPE_RAM);
+	return kvm__register_mem(kvm, guest_phys, size, userspace_addr, memfd,
+				 offset, KVM_MEM_TYPE_RAM);
 }
 
 static inline int kvm__register_dev_mem(struct kvm *kvm, u64 guest_phys,
-					u64 size, void *userspace_addr)
+					u64 size, void *userspace_addr,
+					int memfd, u64 offset)
 {
-	return kvm__register_mem(kvm, guest_phys, size, userspace_addr,
-				 KVM_MEM_TYPE_DEVICE);
+	return kvm__register_mem(kvm, guest_phys, size, userspace_addr, memfd,
+				 offset, KVM_MEM_TYPE_DEVICE);
 }
 
 static inline int kvm__reserve_mem(struct kvm *kvm, u64 guest_phys, u64 size)
 {
-	return kvm__register_mem(kvm, guest_phys, size, NULL,
+	return kvm__register_mem(kvm, guest_phys, size, NULL, -1, 0,
 				 KVM_MEM_TYPE_RESERVED);
 }
 
kvm.c +2 -1
index c71646f..fc0bfc4 100644
@@ -256,7 +256,8 @@ out:
 }
 
 int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size,
-		      void *userspace_addr, enum kvm_mem_type type)
+		      void *userspace_addr, int memfd, u64 offset,
+		      enum kvm_mem_type type)
 {
 	struct kvm_mem_bank *merged = NULL;
 	struct kvm_mem_bank *bank;
mips/kvm.c +3 -3
index 0a0d025..ebb2b19 100644
@@ -38,21 +38,21 @@ void kvm__init_ram(struct kvm *kvm)
 		phys_size  = kvm->ram_size;
 		host_mem   = kvm->ram_start;
 
-		kvm__register_ram(kvm, phys_start, phys_size, host_mem);
+		kvm__register_ram(kvm, phys_start, phys_size, host_mem, kvm->ram_fd, 0);
 	} else {
 		/* one region for memory that fits below MMIO range */
 		phys_start = 0;
 		phys_size  = KVM_MMIO_START;
 		host_mem   = kvm->ram_start;
 
-		kvm__register_ram(kvm, phys_start, phys_size, host_mem);
+		kvm__register_ram(kvm, phys_start, phys_size, host_mem, kvm->ram_fd, 0);
 
 		/* one region for rest of memory */
 		phys_start = KVM_MMIO_START + KVM_MMIO_SIZE;
 		phys_size  = kvm->ram_size - KVM_MMIO_START;
 		host_mem   = kvm->ram_start + KVM_MMIO_START;
 
-		kvm__register_ram(kvm, phys_start, phys_size, host_mem);
+		kvm__register_ram(kvm, phys_start, phys_size, host_mem, kvm->ram_fd, 0);
 	}
 }
 
powerpc/kvm.c +1 -1
index 8d467e9..c36c497 100644
@@ -88,7 +88,7 @@ void kvm__init_ram(struct kvm *kvm)
 		    "overlaps MMIO!\n",
 		    phys_size);
 
-	kvm__register_ram(kvm, phys_start, phys_size, host_mem);
+	kvm__register_ram(kvm, phys_start, phys_size, host_mem, kvm->ram_fd, 0);
 }
 
 void kvm__arch_set_cmdline(char *cmdline, bool video)
riscv/kvm.c +1 -1
index 4a2a3df..bb79c5d 100644
@@ -38,7 +38,7 @@ void kvm__init_ram(struct kvm *kvm)
 	phys_size	= kvm->ram_size;
 	host_mem	= kvm->ram_start;
 
-	err = kvm__register_ram(kvm, phys_start, phys_size, host_mem);
+	err = kvm__register_ram(kvm, phys_start, phys_size, host_mem, kvm->ram_fd, 0);
 	if (err)
 		die("Failed to register %lld bytes of memory at physical "
 		    "address 0x%llx [err %d]", phys_size, phys_start, err);
vfio/core.c +2 -1
index 3ff2c0b..ea189a0 100644
@@ -255,7 +255,8 @@ int vfio_map_region(struct kvm *kvm, struct vfio_device *vdev,
 	region->host_addr = base;
 
 	ret = kvm__register_dev_mem(kvm, region->guest_phys_addr, map_size,
-				    region->host_addr);
+				    region->host_addr, vdev->fd,
+				    region->info.offset);
 	if (ret) {
 		vfio_dev_err(vdev, "failed to register region with KVM");
 		return ret;
x86/kvm.c +3 -3
index 8d29904..cee82d3 100644
@@ -107,7 +107,7 @@ void kvm__init_ram(struct kvm *kvm)
 		phys_size  = kvm->ram_size;
 		host_mem   = kvm->ram_start;
 
-		kvm__register_ram(kvm, phys_start, phys_size, host_mem);
+		kvm__register_ram(kvm, phys_start, phys_size, host_mem, kvm->ram_fd, 0);
 	} else {
 		/* First RAM range from zero to the PCI gap: */
 
@@ -115,7 +115,7 @@ void kvm__init_ram(struct kvm *kvm)
 		phys_size  = KVM_32BIT_GAP_START;
 		host_mem   = kvm->ram_start;
 
-		kvm__register_ram(kvm, phys_start, phys_size, host_mem);
+		kvm__register_ram(kvm, phys_start, phys_size, host_mem, kvm->ram_fd, 0);
 
 		/* Second RAM range from 4GB to the end of RAM: */
 
@@ -123,7 +123,7 @@ void kvm__init_ram(struct kvm *kvm)
 		phys_size  = kvm->ram_size - phys_start;
 		host_mem   = kvm->ram_start + phys_start;
 
-		kvm__register_ram(kvm, phys_start, phys_size, host_mem);
+		kvm__register_ram(kvm, phys_start, phys_size, host_mem, kvm->ram_fd, 0);
 	}
 }
 
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
author
#19 lore raw

RFC PATCH 19/32 Add memfd_restricted system call

In reply to Fuad Tabba

This enables building kvmtool on a host that doesn't have the kernel with the new extensions yet.

No functional change intended.

---
 util/util.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
util/util.c +11 -0
index 1f2e1a6..1424815 100644
@@ -10,6 +10,17 @@
 #include <sys/mman.h>
 #include <sys/stat.h>
 #include <sys/statfs.h>
+#include <sys/syscall.h>
+
+#ifndef __NR_memfd_restricted
+#define __NR_memfd_restricted 451
+__SYSCALL(__NR_memfd_restricted, sys_memfd_restricted)
+#endif
+
+static inline int memfd_restricted(unsigned int flags)
+{
+	return syscall(__NR_memfd_restricted, flags);
+}
 
 static void report(const char *prefix, const char *err, va_list params)
 {
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
author
#20 lore raw

RFC PATCH 20/32 Add kvm linux headers and structure extensions for restricted_fd

In reply to Fuad Tabba

This enables building kvmtool on a host that doesn't have the kernel with the new extensions yet.

No functional change intended.

---
 include/linux/kvm.h | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
include/linux/kvm.h +19 -0
index 0d5d441..1d35153 100644
@@ -103,6 +103,14 @@ struct kvm_userspace_memory_region {
 	__u64 userspace_addr; /* start of the userspace allocated memory */
 };
 
+struct kvm_userspace_memory_region_ext {
+	struct kvm_userspace_memory_region region;
+	__u64 restricted_offset;
+	__u32 restricted_fd;
+	__u32 pad1;
+	__u64 pad2[14];
+};
+
 /*
  * The bit 0 ~ bit 15 of kvm_memory_region::flags are visible for userspace,
  * other bits are reserved for kvm internal use which are defined in
@@ -110,6 +118,7 @@ struct kvm_userspace_memory_region {
  */
 #define KVM_MEM_LOG_DIRTY_PAGES	(1UL << 0)
 #define KVM_MEM_READONLY	(1UL << 1)
+#define KVM_MEM_PRIVATE		(1UL << 2)
 
 /* for KVM_IRQ_LINE */
 struct kvm_irq_level {
@@ -272,6 +281,7 @@ struct kvm_xen_exit {
 #define KVM_EXIT_RISCV_SBI        35
 #define KVM_EXIT_RISCV_CSR        36
 #define KVM_EXIT_NOTIFY           37
+#define KVM_EXIT_MEMORY_FAULT     38
 
 /* For KVM_EXIT_INTERNAL_ERROR */
 /* Emulate instruction failed. */
@@ -510,6 +520,14 @@ struct kvm_run {
 #define KVM_NOTIFY_CONTEXT_INVALID	(1 << 0)
 			__u32 flags;
 		} notify;
+		/* KVM_EXIT_MEMORY_FAULT */
+		struct {
+#define KVM_MEMORY_EXIT_FLAG_PRIVATE	(1 << 0)
+			__u32 flags;
+			__u32 padding;
+			__u64 gpa;
+			__u64 size;
+		} memory;
 		/* Fix the size of the union. */
 		char padding[256];
 	};
@@ -1178,6 +1196,7 @@ struct kvm_ppc_resize_hpt {
 #define KVM_CAP_S390_ZPCI_OP 221
 #define KVM_CAP_S390_CPU_TOPOLOGY 222
 #define KVM_CAP_DIRTY_LOG_RING_ACQ_REL 223
+#define KVM_CAP_PRIVATE_MEM 224
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
author
#21 lore raw

RFC PATCH 21/32 Add option for enabling restricted memory for guests

In reply to Fuad Tabba

Currently this way for testing only.

When the option restricted_mem is set, the guest will use the new restricted memory extensions.

This is done this way for now to enable testing and debugging. In the future, pKVM will require that all its guest use restricted memory, so instead of a flag, the intention is for the final version of this patch series to rely on KVM_CAP_PRIVATE_MEM and fail if that capability isn't supported.

---
 arm/kvm.c                | 5 +++++
 builtin-run.c            | 2 ++
 include/kvm/kvm-config.h | 1 +
 3 files changed, 8 insertions(+)
arm/kvm.c +5 -0
index 8772a55..094fbe4 100644
@@ -74,6 +74,11 @@ void kvm__arch_set_cmdline(char *cmdline, bool video)
 
 void kvm__arch_init(struct kvm *kvm)
 {
+	if (kvm->cfg.restricted_mem &&
+	    !kvm__supports_extension(kvm, KVM_CAP_PRIVATE_MEM)) {
+		die("Guest restricted memory capability not supported.");
+	}
+
 	/* Create the virtual GIC. */
 	if (gic__create(kvm, kvm->cfg.arch.irqchip))
 		die("Failed to create virtual GIC");
builtin-run.c +2 -0
index bb7e6e8..4642bc4 100644
@@ -202,6 +202,8 @@ static int mem_parser(const struct option *opt, const char *arg, int unset)
 			"Hugetlbfs path"),				\
 	OPT_BOOLEAN('\0', "virtio-legacy", &(cfg)->virtio_legacy,	\
 		    "Use legacy virtio transport"),			\
+	OPT_BOOLEAN('\0', "restricted_mem", &(cfg)->restricted_mem,	\
+		    "Use restricted memory for guests"),		\
 									\
 	OPT_GROUP("Kernel options:"),					\
 	OPT_STRING('k', "kernel", &(cfg)->kernel_filename, "kernel",	\
include/kvm/kvm-config.h +1 -0
index 368e6c7..ea5f3ea 100644
@@ -65,6 +65,7 @@ struct kvm_config {
 	bool ioport_debug;
 	bool mmio_debug;
 	bool virtio_legacy;
+	bool restricted_mem;
 };
 
 #endif
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
author
#22 lore raw

RFC PATCH 22/32 Change guest ram mapping from private to shared

In reply to Fuad Tabba

Private mappings don't work with restricted memory since it might be COWed.

---
 util/util.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/util.c +1 -1
index 1424815..107f34d 100644
@@ -173,7 +173,7 @@ void *mmap_anon_or_hugetlbfs_align(struct kvm *kvm, const char *hugetlbfs_path,
 
 	/* Map the allocated memory in the fd to the specified alignment. */
 	addr_align = (void *)ALIGN((u64)addr_map, align_sz);
-	if (mmap(addr_align, size, PROT_RW, MAP_PRIVATE | MAP_FIXED, fd, 0) ==
+	if (mmap(addr_align, size, PROT_RW, MAP_SHARED | MAP_FIXED, fd, 0) ==
 	    MAP_FAILED) {
 		close(fd);
 		return MAP_FAILED;
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
author
#23 lore raw

RFC PATCH 23/32 Change pvtime mapping from private to shared

In reply to Fuad Tabba

Private mappings don't work with restricted memory since it might be COWed.

---
 arm/aarch64/pvtime.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arm/aarch64/pvtime.c +1 -1
index 9b06ee4..a452938 100644
@@ -20,7 +20,7 @@ static int pvtime__alloc_region(struct kvm *kvm)
 	if (mem_fd < 0)
 		return -errno;
 
-	mem = mmap(NULL, ARM_PVTIME_SIZE, PROT_RW, MAP_PRIVATE, mem_fd, 0);
+	mem = mmap(NULL, ARM_PVTIME_SIZE, PROT_RW, MAP_SHARED, mem_fd, 0);
 	if (mem == MAP_FAILED) {
 		ret = -errno;
 		close(mem_fd);
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
author
#24 lore raw

RFC PATCH 24/32 Change vesa mapping from private to shared

In reply to Fuad Tabba

Private mappings don't work with restricted memory since it might be COWed.

---
 hw/vesa.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/vesa.c +1 -1
index 277d638..3233794 100644
@@ -96,7 +96,7 @@ struct framebuffer *vesa__init(struct kvm *kvm)
 		goto unregister_device;
 	}
 
-	mem = mmap(NULL, VESA_MEM_SIZE, PROT_RW, MAP_PRIVATE, mem_fd, 0);
+	mem = mmap(NULL, VESA_MEM_SIZE, PROT_RW, MAP_SHARED, mem_fd, 0);
 	if (mem == MAP_FAILED) {
 		r = -errno;
 		goto close_memfd;
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
author
#25 lore raw

RFC PATCH 25/32 Allocate guest memory as restricted if needed

In reply to Fuad Tabba

If specified by the option and supported by KVM, allocate guest memory as restricted with the new system call.

---
 arm/aarch64/pvtime.c |  2 +-
 hw/vesa.c            |  2 +-
 include/kvm/util.h   |  2 +-
 util/util.c          | 12 ++++++++----
 4 files changed, 11 insertions(+), 7 deletions(-)
arm/aarch64/pvtime.c +1 -1
index a452938..8247c52 100644
@@ -16,7 +16,7 @@ static int pvtime__alloc_region(struct kvm *kvm)
 	int mem_fd;
 	int ret = 0;
 
-	mem_fd = memfd_alloc(ARM_PVTIME_SIZE, false, 0);
+	mem_fd = memfd_alloc(kvm, ARM_PVTIME_SIZE, false, 0);
 	if (mem_fd < 0)
 		return -errno;
 
hw/vesa.c +1 -1
index 3233794..6c5287a 100644
@@ -90,7 +90,7 @@ struct framebuffer *vesa__init(struct kvm *kvm)
 	if (r < 0)
 		goto unregister_ioport;
 
-	mem_fd = memfd_alloc(ARM_PVTIME_SIZE, false, 0, 0);
+	mem_fd = memfd_alloc(kvm, ARM_PVTIME_SIZE, false, 0, 0);
 	if (mem_fd < 0) {
 		r = -errno;
 		goto unregister_device;
include/kvm/util.h +1 -1
index 79275ed..5a98d4a 100644
@@ -139,7 +139,7 @@ static inline int pow2_size(unsigned long x)
 }
 
 struct kvm;
-int memfd_alloc(u64 size, bool hugetlb, u64 blk_size);
+int memfd_alloc(struct kvm *kvm, size_t size, bool hugetlb, u64 hugepage_size);
 void *mmap_anon_or_hugetlbfs_align(struct kvm *kvm, const char *hugetlbfs_path,
 				   u64 size, u64 align);
 void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size);
util/util.c +8 -4
index 107f34d..13b3e82 100644
@@ -17,7 +17,7 @@
 __SYSCALL(__NR_memfd_restricted, sys_memfd_restricted)
 #endif
 
-static inline int memfd_restricted(unsigned int flags)
+static int memfd_restricted(unsigned int flags)
 {
 	return syscall(__NR_memfd_restricted, flags);
 }
@@ -106,7 +106,7 @@ static u64 get_hugepage_blk_size(const char *hugetlbfs_path)
 	return sfs.f_bsize;
 }
 
-int memfd_alloc(u64 size, bool hugetlb, u64 blk_size)
+int memfd_alloc(struct kvm *kvm, size_t size, bool hugetlb, u64 blk_size)
 {
 	const char *name = "kvmtool";
 	unsigned int flags = 0;
@@ -120,7 +120,11 @@ int memfd_alloc(u64 size, bool hugetlb, u64 blk_size)
 		flags |= blk_size << MFD_HUGE_SHIFT;
 	}
 
-	fd = memfd_create(name, flags);
+	if (kvm->cfg.restricted_mem)
+		fd = memfd_restricted(flags);
+	else
+		fd = memfd_create(name, flags);
+
 	if (fd < 0)
 		die_perror("Can't memfd_create for memory map");
 
@@ -167,7 +171,7 @@ void *mmap_anon_or_hugetlbfs_align(struct kvm *kvm, const char *hugetlbfs_path,
 	if (addr_map == MAP_FAILED)
 		return MAP_FAILED;
 
-	fd = memfd_alloc(size, hugetlbfs_path, blk_size);
+	fd = memfd_alloc(kvm, size, hugetlbfs_path, blk_size);
 	if (fd < 0)
 		return MAP_FAILED;
 
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
#26 lore raw

Re: [RFC PATCH kvmtool v1 25/32] Allocate guest memory as restricted if needed

Quoted 86 lines · Fuad Tabba

If specified by the option and supported by KVM, allocate guest memory as restricted with the new system call.

---
 arm/aarch64/pvtime.c |  2 +-
 hw/vesa.c            |  2 +-
 include/kvm/util.h   |  2 +-
 util/util.c          | 12 ++++++++----
 4 files changed, 11 insertions(+), 7 deletions(-)
arm/aarch64/pvtime.c +1 -1
index a452938..8247c52 100644
@@ -16,7 +16,7 @@ static int pvtime__alloc_region(struct kvm *kvm)
 	int mem_fd;
 	int ret = 0;
 
-	mem_fd = memfd_alloc(ARM_PVTIME_SIZE, false, 0);
+	mem_fd = memfd_alloc(kvm, ARM_PVTIME_SIZE, false, 0);
 	if (mem_fd < 0)
 		return -errno;
 
hw/vesa.c +1 -1
index 3233794..6c5287a 100644
@@ -90,7 +90,7 @@ struct framebuffer *vesa__init(struct kvm *kvm)
 	if (r < 0)
 		goto unregister_ioport;
 
-	mem_fd = memfd_alloc(ARM_PVTIME_SIZE, false, 0, 0);
+	mem_fd = memfd_alloc(kvm, ARM_PVTIME_SIZE, false, 0, 0);
 	if (mem_fd < 0) {
 		r = -errno;
 		goto unregister_device;
include/kvm/util.h +1 -1
index 79275ed..5a98d4a 100644
@@ -139,7 +139,7 @@ static inline int pow2_size(unsigned long x)
 }
 
 struct kvm;
-int memfd_alloc(u64 size, bool hugetlb, u64 blk_size);
+int memfd_alloc(struct kvm *kvm, size_t size, bool hugetlb, u64 hugepage_size);
 void *mmap_anon_or_hugetlbfs_align(struct kvm *kvm, const char *hugetlbfs_path,
 				   u64 size, u64 align);
 void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size);
util/util.c +7 -3
index 107f34d..13b3e82 100644
@@ -17,7 +17,7 @@
 __SYSCALL(__NR_memfd_restricted, sys_memfd_restricted)
 #endif
 
-static inline int memfd_restricted(unsigned int flags)
+static int memfd_restricted(unsigned int flags)
 {
 	return syscall(__NR_memfd_restricted, flags);
 }
@@ -106,7 +106,7 @@ static u64 get_hugepage_blk_size(const char *hugetlbfs_path)
 	return sfs.f_bsize;
 }
 
-int memfd_alloc(u64 size, bool hugetlb, u64 blk_size)
+int memfd_alloc(struct kvm *kvm, size_t size, bool hugetlb, u64 blk_size)
 {
 	const char *name = "kvmtool";
 	unsigned int flags = 0;
@@ -120,7 +120,11 @@ int memfd_alloc(u64 size, bool hugetlb, u64 blk_size)
 		flags |= blk_size << MFD_HUGE_SHIFT;
 	}
 
-	fd = memfd_create(name, flags);
+	if (kvm->cfg.restricted_mem)
+		fd = memfd_restricted(flags);
+	else
+		fd = memfd_create(name, flags);
+
 	if (fd < 0)
 		die_perror("Can't memfd_create for memory map");
 

util/util.c void *mmap_anon_or_hugetlbfs_align(struct kvm *kvm, const char *hugetlbfs_path, · line 177

Quoted 8 lines
util/util.c +1 -1
@@ -167,7 +171,7 @@ void *mmap_anon_or_hugetlbfs_align(struct kvm *kvm, const char *hugetlbfs_path,
 	if (addr_map == MAP_FAILED)
 		return MAP_FAILED;
 
-	fd = memfd_alloc(size, hugetlbfs_path, blk_size);
+	fd = memfd_alloc(kvm, size, hugetlbfs_path, blk_size);
 	if (fd < 0)
 		return MAP_FAILED;
 

Extra context:

Quoted 7 lines
	/* Map the allocated memory in the fd to the specified alignment. */
	addr_align = (void *)ALIGN((u64)addr_map, align_sz);
	if (mmap(addr_align, size, PROT_RW, MAP_SHARED | MAP_FIXED, fd, 0) ==
	    MAP_FAILED) {
		close(fd);
		return MAP_FAILED;
	}

So I don't understand how this works. My understanding is that memfd_restricted() returns a file descriptor that cannot be mapped in user space. So surely this mmap() will always fail (when kvm->cfg.restricted_mem)?

What am I missing?

Thanks,

Steve

author
#27 lore raw

Re: [RFC PATCH kvmtool v1 25/32] Allocate guest memory as restricted if needed

Hi,

Quoted 105 lines · Steven Price
Quoted 86 lines · Fuad Tabba

If specified by the option and supported by KVM, allocate guest memory as restricted with the new system call.

---
 arm/aarch64/pvtime.c |  2 +-
 hw/vesa.c            |  2 +-
 include/kvm/util.h   |  2 +-
 util/util.c          | 12 ++++++++----
 4 files changed, 11 insertions(+), 7 deletions(-)
arm/aarch64/pvtime.c +1 -1
index a452938..8247c52 100644
@@ -16,7 +16,7 @@ static int pvtime__alloc_region(struct kvm *kvm)
      int mem_fd;
      int ret = 0;
 
-     mem_fd = memfd_alloc(ARM_PVTIME_SIZE, false, 0);
+     mem_fd = memfd_alloc(kvm, ARM_PVTIME_SIZE, false, 0);
      if (mem_fd < 0)
              return -errno;
 
hw/vesa.c +1 -1
index 3233794..6c5287a 100644
@@ -90,7 +90,7 @@ struct framebuffer *vesa__init(struct kvm *kvm)
      if (r < 0)
              goto unregister_ioport;
 
-     mem_fd = memfd_alloc(ARM_PVTIME_SIZE, false, 0, 0);
+     mem_fd = memfd_alloc(kvm, ARM_PVTIME_SIZE, false, 0, 0);
      if (mem_fd < 0) {
              r = -errno;
              goto unregister_device;
include/kvm/util.h +1 -1
index 79275ed..5a98d4a 100644
@@ -139,7 +139,7 @@ static inline int pow2_size(unsigned long x)
 }
 
 struct kvm;
-int memfd_alloc(u64 size, bool hugetlb, u64 blk_size);
+int memfd_alloc(struct kvm *kvm, size_t size, bool hugetlb, u64 hugepage_size);
 void *mmap_anon_or_hugetlbfs_align(struct kvm *kvm, const char *hugetlbfs_path,
                                 u64 size, u64 align);
 void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size);
util/util.c +7 -3
index 107f34d..13b3e82 100644
@@ -17,7 +17,7 @@
 __SYSCALL(__NR_memfd_restricted, sys_memfd_restricted)
 #endif
 
-static inline int memfd_restricted(unsigned int flags)
+static int memfd_restricted(unsigned int flags)
 {
      return syscall(__NR_memfd_restricted, flags);
 }
@@ -106,7 +106,7 @@ static u64 get_hugepage_blk_size(const char *hugetlbfs_path)
      return sfs.f_bsize;
 }
 
-int memfd_alloc(u64 size, bool hugetlb, u64 blk_size)
+int memfd_alloc(struct kvm *kvm, size_t size, bool hugetlb, u64 blk_size)
 {
      const char *name = "kvmtool";
      unsigned int flags = 0;
@@ -120,7 +120,11 @@ int memfd_alloc(u64 size, bool hugetlb, u64 blk_size)
              flags |= blk_size << MFD_HUGE_SHIFT;
      }
 
-     fd = memfd_create(name, flags);
+     if (kvm->cfg.restricted_mem)
+             fd = memfd_restricted(flags);
+     else
+             fd = memfd_create(name, flags);
+
      if (fd < 0)
              die_perror("Can't memfd_create for memory map");
 

util/util.c void *mmap_anon_or_hugetlbfs_align(struct kvm *kvm, const char *hugetlbfs_path, · line 177

Quoted 8 lines
util/util.c +1 -1
@@ -167,7 +171,7 @@ void *mmap_anon_or_hugetlbfs_align(struct kvm *kvm, const char *hugetlbfs_path,
      if (addr_map == MAP_FAILED)
              return MAP_FAILED;
 
-     fd = memfd_alloc(size, hugetlbfs_path, blk_size);
+     fd = memfd_alloc(kvm, size, hugetlbfs_path, blk_size);
      if (fd < 0)
              return MAP_FAILED;
 

Extra context:

Quoted 7 lines
      /* Map the allocated memory in the fd to the specified alignment. */
      addr_align = (void *)ALIGN((u64)addr_map, align_sz);
      if (mmap(addr_align, size, PROT_RW, MAP_SHARED | MAP_FIXED, fd, 0) ==
          MAP_FAILED) {
              close(fd);
              return MAP_FAILED;
      }
Quoted 6 lines

So I don't understand how this works. My understanding is that memfd_restricted() returns a file descriptor that cannot be mapped in user space. So surely this mmap() will always fail (when kvm->cfg.restricted_mem)?

What am I missing?

You're right for the current memfd_restricted() proposal as it is now. However, in our discussions with the folks working on it (e.g., [1, 2]), we pointed out that for pkvm/arm64 and for Android we need to be able to mmap shared memory for a couple of reasons (e.g., sharing in place without copying, guest initialization). So in the pkvm/arm64 port of the memfd_restricted (which we haven't yet sent out since everything is still in flux, but you can have a look at it here [3]), we add the ability to mmap restricted memory but with a few restrictions, one of them being that the memory must be shared.

Of course, we plan on submitting these patches as soon as the memfd_restricted is in.

I hope this answers your question.

Cheers,
/fuad

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://android-kvm.googlesource.com/linux/+/refs/heads/tabba/fdmem-v9-core

Quoted 4 lines

Thanks,

Steve

#28 lore raw

Re: [RFC PATCH kvmtool v1 25/32] Allocate guest memory as restricted if needed

Quoted 115 lines · Fuad Tabba

Hi,

Quoted 105 lines · Steven Price
Quoted 86 lines · Fuad Tabba

If specified by the option and supported by KVM, allocate guest memory as restricted with the new system call.

---
 arm/aarch64/pvtime.c |  2 +-
 hw/vesa.c            |  2 +-
 include/kvm/util.h   |  2 +-
 util/util.c          | 12 ++++++++----
 4 files changed, 11 insertions(+), 7 deletions(-)
arm/aarch64/pvtime.c +1 -1
index a452938..8247c52 100644
@@ -16,7 +16,7 @@ static int pvtime__alloc_region(struct kvm *kvm)
      int mem_fd;
      int ret = 0;
 
-     mem_fd = memfd_alloc(ARM_PVTIME_SIZE, false, 0);
+     mem_fd = memfd_alloc(kvm, ARM_PVTIME_SIZE, false, 0);
      if (mem_fd < 0)
              return -errno;
 
hw/vesa.c +1 -1
index 3233794..6c5287a 100644
@@ -90,7 +90,7 @@ struct framebuffer *vesa__init(struct kvm *kvm)
      if (r < 0)
              goto unregister_ioport;
 
-     mem_fd = memfd_alloc(ARM_PVTIME_SIZE, false, 0, 0);
+     mem_fd = memfd_alloc(kvm, ARM_PVTIME_SIZE, false, 0, 0);
      if (mem_fd < 0) {
              r = -errno;
              goto unregister_device;
include/kvm/util.h +1 -1
index 79275ed..5a98d4a 100644
@@ -139,7 +139,7 @@ static inline int pow2_size(unsigned long x)
 }
 
 struct kvm;
-int memfd_alloc(u64 size, bool hugetlb, u64 blk_size);
+int memfd_alloc(struct kvm *kvm, size_t size, bool hugetlb, u64 hugepage_size);
 void *mmap_anon_or_hugetlbfs_align(struct kvm *kvm, const char *hugetlbfs_path,
                                 u64 size, u64 align);
 void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size);
util/util.c +7 -3
index 107f34d..13b3e82 100644
@@ -17,7 +17,7 @@
 __SYSCALL(__NR_memfd_restricted, sys_memfd_restricted)
 #endif
 
-static inline int memfd_restricted(unsigned int flags)
+static int memfd_restricted(unsigned int flags)
 {
      return syscall(__NR_memfd_restricted, flags);
 }
@@ -106,7 +106,7 @@ static u64 get_hugepage_blk_size(const char *hugetlbfs_path)
      return sfs.f_bsize;
 }
 
-int memfd_alloc(u64 size, bool hugetlb, u64 blk_size)
+int memfd_alloc(struct kvm *kvm, size_t size, bool hugetlb, u64 blk_size)
 {
      const char *name = "kvmtool";
      unsigned int flags = 0;
@@ -120,7 +120,11 @@ int memfd_alloc(u64 size, bool hugetlb, u64 blk_size)
              flags |= blk_size << MFD_HUGE_SHIFT;
      }
 
-     fd = memfd_create(name, flags);
+     if (kvm->cfg.restricted_mem)
+             fd = memfd_restricted(flags);
+     else
+             fd = memfd_create(name, flags);
+
      if (fd < 0)
              die_perror("Can't memfd_create for memory map");
 

util/util.c void *mmap_anon_or_hugetlbfs_align(struct kvm *kvm, const char *hugetlbfs_path, · line 177

Quoted 8 lines
util/util.c +1 -1
@@ -167,7 +171,7 @@ void *mmap_anon_or_hugetlbfs_align(struct kvm *kvm, const char *hugetlbfs_path,
      if (addr_map == MAP_FAILED)
              return MAP_FAILED;
 
-     fd = memfd_alloc(size, hugetlbfs_path, blk_size);
+     fd = memfd_alloc(kvm, size, hugetlbfs_path, blk_size);
      if (fd < 0)
              return MAP_FAILED;
 

Extra context:

Quoted 7 lines
      /* Map the allocated memory in the fd to the specified alignment. */
      addr_align = (void *)ALIGN((u64)addr_map, align_sz);
      if (mmap(addr_align, size, PROT_RW, MAP_SHARED | MAP_FIXED, fd, 0) ==
          MAP_FAILED) {
              close(fd);
              return MAP_FAILED;
      }
Quoted 6 lines

So I don't understand how this works. My understanding is that memfd_restricted() returns a file descriptor that cannot be mapped in user space. So surely this mmap() will always fail (when kvm->cfg.restricted_mem)?

What am I missing?

Quoted 9 lines

You're right for the current memfd_restricted() proposal as it is now. However, in our discussions with the folks working on it (e.g., [1, 2]), we pointed out that for pkvm/arm64 and for Android we need to be able to mmap shared memory for a couple of reasons (e.g., sharing in place without copying, guest initialization). So in the pkvm/arm64 port of the memfd_restricted (which we haven't yet sent out since everything is still in flux, but you can have a look at it here [3]), we add the ability to mmap restricted memory but with a few restrictions, one of them being that the memory must be shared.

Ah, ok. I'm not sure if that works for TDX or not, my understanding was they couldn't have a user space mapping, but I'll let others familiar with TDX comment on that.

For Arm CCA we need to ensure that the kernel doesn't create mappings:
your tree seems to include changes to block GUP so that should work. Accesses from the VMM are not permitted but can be handled 'gracefully' by killing off the VMM - so the mappings are not necessarily a problem, although they do provide a significant "foot gun" to the VMM.

We still have open questions about our UABI so it would be good to have a discussion about how to align pKVM and Arm CCA.

Quoted 4 lines

Of course, we plan on submitting these patches as soon as the memfd_restricted is in.

I hope this answers your question.

Yes thanks, I hadn't realised from your cover letter you had changes to memfd_restricted() on top of Chao's series.

Thanks,

Steve

Quoted 11 lines

Cheers,
/fuad

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://android-kvm.googlesource.com/linux/+/refs/heads/tabba/fdmem-v9-core

Quoted 4 lines

Thanks,

Steve

author
#29 lore raw

Re: [RFC PATCH kvmtool v1 25/32] Allocate guest memory as restricted if needed

Hi,

Quoted 127 lines · Steven Price
Quoted 115 lines · Fuad Tabba

Hi,

Quoted 105 lines · Steven Price
Quoted 86 lines · Fuad Tabba

If specified by the option and supported by KVM, allocate guest memory as restricted with the new system call.

---
 arm/aarch64/pvtime.c |  2 +-
 hw/vesa.c            |  2 +-
 include/kvm/util.h   |  2 +-
 util/util.c          | 12 ++++++++----
 4 files changed, 11 insertions(+), 7 deletions(-)
arm/aarch64/pvtime.c +1 -1
index a452938..8247c52 100644
@@ -16,7 +16,7 @@ static int pvtime__alloc_region(struct kvm *kvm)
      int mem_fd;
      int ret = 0;
 
-     mem_fd = memfd_alloc(ARM_PVTIME_SIZE, false, 0);
+     mem_fd = memfd_alloc(kvm, ARM_PVTIME_SIZE, false, 0);
      if (mem_fd < 0)
              return -errno;
 
hw/vesa.c +1 -1
index 3233794..6c5287a 100644
@@ -90,7 +90,7 @@ struct framebuffer *vesa__init(struct kvm *kvm)
      if (r < 0)
              goto unregister_ioport;
 
-     mem_fd = memfd_alloc(ARM_PVTIME_SIZE, false, 0, 0);
+     mem_fd = memfd_alloc(kvm, ARM_PVTIME_SIZE, false, 0, 0);
      if (mem_fd < 0) {
              r = -errno;
              goto unregister_device;
include/kvm/util.h +1 -1
index 79275ed..5a98d4a 100644
@@ -139,7 +139,7 @@ static inline int pow2_size(unsigned long x)
 }
 
 struct kvm;
-int memfd_alloc(u64 size, bool hugetlb, u64 blk_size);
+int memfd_alloc(struct kvm *kvm, size_t size, bool hugetlb, u64 hugepage_size);
 void *mmap_anon_or_hugetlbfs_align(struct kvm *kvm, const char *hugetlbfs_path,
                                 u64 size, u64 align);
 void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size);
util/util.c +7 -3
index 107f34d..13b3e82 100644
@@ -17,7 +17,7 @@
 __SYSCALL(__NR_memfd_restricted, sys_memfd_restricted)
 #endif
 
-static inline int memfd_restricted(unsigned int flags)
+static int memfd_restricted(unsigned int flags)
 {
      return syscall(__NR_memfd_restricted, flags);
 }
@@ -106,7 +106,7 @@ static u64 get_hugepage_blk_size(const char *hugetlbfs_path)
      return sfs.f_bsize;
 }
 
-int memfd_alloc(u64 size, bool hugetlb, u64 blk_size)
+int memfd_alloc(struct kvm *kvm, size_t size, bool hugetlb, u64 blk_size)
 {
      const char *name = "kvmtool";
      unsigned int flags = 0;
@@ -120,7 +120,11 @@ int memfd_alloc(u64 size, bool hugetlb, u64 blk_size)
              flags |= blk_size << MFD_HUGE_SHIFT;
      }
 
-     fd = memfd_create(name, flags);
+     if (kvm->cfg.restricted_mem)
+             fd = memfd_restricted(flags);
+     else
+             fd = memfd_create(name, flags);
+
      if (fd < 0)
              die_perror("Can't memfd_create for memory map");
 

util/util.c void *mmap_anon_or_hugetlbfs_align(struct kvm *kvm, const char *hugetlbfs_path, · line 177

Quoted 8 lines
util/util.c +1 -1
@@ -167,7 +171,7 @@ void *mmap_anon_or_hugetlbfs_align(struct kvm *kvm, const char *hugetlbfs_path,
      if (addr_map == MAP_FAILED)
              return MAP_FAILED;
 
-     fd = memfd_alloc(size, hugetlbfs_path, blk_size);
+     fd = memfd_alloc(kvm, size, hugetlbfs_path, blk_size);
      if (fd < 0)
              return MAP_FAILED;
 

Extra context:

Quoted 7 lines
      /* Map the allocated memory in the fd to the specified alignment. */
      addr_align = (void *)ALIGN((u64)addr_map, align_sz);
      if (mmap(addr_align, size, PROT_RW, MAP_SHARED | MAP_FIXED, fd, 0) ==
          MAP_FAILED) {
              close(fd);
              return MAP_FAILED;
      }
Quoted 6 lines

So I don't understand how this works. My understanding is that memfd_restricted() returns a file descriptor that cannot be mapped in user space. So surely this mmap() will always fail (when kvm->cfg.restricted_mem)?

What am I missing?

Quoted 9 lines

You're right for the current memfd_restricted() proposal as it is now. However, in our discussions with the folks working on it (e.g., [1, 2]), we pointed out that for pkvm/arm64 and for Android we need to be able to mmap shared memory for a couple of reasons (e.g., sharing in place without copying, guest initialization). So in the pkvm/arm64 port of the memfd_restricted (which we haven't yet sent out since everything is still in flux, but you can have a look at it here [3]), we add the ability to mmap restricted memory but with a few restrictions, one of them being that the memory must be shared.

Quoted 3 lines

Ah, ok. I'm not sure if that works for TDX or not, my understanding was they couldn't have a user space mapping, but I'll let others familiar with TDX comment on that.

My understanding of TDX is quite limited, and that's one of the reasons I didn't even try to implement the x86 arch-specific part in this series.

Quoted 5 lines

For Arm CCA we need to ensure that the kernel doesn't create mappings:
your tree seems to include changes to block GUP so that should work. Accesses from the VMM are not permitted but can be handled 'gracefully' by killing off the VMM - so the mappings are not necessarily a problem, although they do provide a significant "foot gun" to the VMM.

You're right that it includes changes to block gup, since the same problems you've mentioned apply to pKVM.

Quoted 2 lines

We still have open questions about our UABI so it would be good to have a discussion about how to align pKVM and Arm CCA.

Sounds good.

Quoted 7 lines
Quoted 4 lines

Of course, we plan on submitting these patches as soon as the memfd_restricted is in.

I hope this answers your question.

Yes thanks, I hadn't realised from your cover letter you had changes to memfd_restricted() on top of Chao's series.

I just realized that that's my fault. I'd posted the wrong link in the cover letter.

Cheers,
/fuad

Quoted 16 lines

Thanks,

Steve

Quoted 11 lines

Cheers,
/fuad

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://android-kvm.googlesource.com/linux/+/refs/heads/tabba/fdmem-v9-core

Quoted 4 lines

Thanks,

Steve

author
#30 lore raw

RFC PATCH 26/32 Use the new fd-based extended memory region

In reply to Fuad Tabba

If restricted guest memory is enabled, use the new extended guest memory region interface to pass the guest memory to kvm using the restricted file descriptor.

In the future, pKVM will require this, and the successor of patch will instead force the use of restricted guest memory for pKVM (and other potential environments) that require restricted memory.

---
 kvm.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 49 insertions(+), 5 deletions(-)
kvm.c +49 -5
index fc0bfc4..bde6708 100644
@@ -214,6 +214,41 @@ static int set_user_memory_region(int vm_fd, u32 slot, u32 flags,
 	return ret;
 }
 
+static int set_user_memory_region_ext(int vm_fd, u32 slot, u32 flags,
+				  u64 guest_phys, u64 size,
+				  u64 userspace_addr, u32 fd, u64 offset)
+{
+	int ret = 0;
+	struct kvm_enc_region range;
+	struct kvm_userspace_memory_region_ext mem = {
+		.region = {
+			.slot			= slot,
+			.flags			= flags,
+			.guest_phys_addr	= guest_phys,
+			.memory_size		= size,
+			.userspace_addr		= 0,
+		},
+		.restricted_fd = fd,
+		.restricted_offset = offset,
+	};
+
+	ret = ioctl(vm_fd, KVM_SET_USER_MEMORY_REGION, &mem);
+	if (ret < 0) {
+		ret = -errno;
+		goto out;
+	}
+
+	/* Inform KVM that the region is protected. */
+	range.addr = guest_phys;
+	range.size = size;
+	ret = ioctl(vm_fd, KVM_MEMORY_ENCRYPT_REG_REGION, &range);
+	if (ret)
+		ret = -errno;
+
+out:
+	return ret;
+}
+
 int kvm__destroy_mem(struct kvm *kvm, u64 guest_phys, u64 size,
 		     void *userspace_addr)
 {
@@ -240,8 +275,13 @@ int kvm__destroy_mem(struct kvm *kvm, u64 guest_phys, u64 size,
 		goto out;
 	}
 
-	ret = set_user_memory_region(kvm->vm_fd, bank->slot, 0, guest_phys, 0,
-				     (u64) userspace_addr);
+	if (kvm->cfg.restricted_mem)
+		ret = set_user_memory_region_ext(kvm->vm_fd, bank->slot,
+			KVM_MEM_PRIVATE, guest_phys, 0, (u64) userspace_addr,
+			0, 0);
+	else
+		ret = set_user_memory_region(kvm->vm_fd, bank->slot, 0,
+			guest_phys, 0, (u64) userspace_addr);
 	if (ret < 0)
 		goto out;
 
@@ -339,9 +379,13 @@ int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size,
 		flags |= KVM_MEM_READONLY;
 
 	if (type != KVM_MEM_TYPE_RESERVED) {
-		ret = set_user_memory_region(kvm->vm_fd, slot, flags,
-					     guest_phys, size,
-					     (u64) userspace_addr);
+		if (kvm->cfg.restricted_mem)
+			ret = set_user_memory_region_ext(kvm->vm_fd, slot,
+				flags | KVM_MEM_PRIVATE, guest_phys, size,
+				(u64) userspace_addr, memfd, offset);
+		else
+			ret = set_user_memory_region(kvm->vm_fd, slot, flags,
+				guest_phys, size, (u64) userspace_addr);
 		if (ret < 0)
 			goto out;
 	}
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
author
#31 lore raw

RFC PATCH 27/32 Track the memfd in the bank

In reply to Fuad Tabba

Guest memory is canonically represented by the fd, so use it to refer to guest memory in the kvmtool guest memory banks.

---
 include/kvm/kvm.h | 2 ++
 kvm.c             | 2 ++
 2 files changed, 4 insertions(+)
include/kvm/kvm.h +2 -0
index 33cae9d..6192f6c 100644
@@ -80,6 +80,8 @@ struct kvm_mem_bank {
 	u64			size;
 	enum kvm_mem_type	type;
 	u32			slot;
+	int			memfd;
+	u64			memfd_offset;
 };
 
 struct kvm {
kvm.c +2 -0
index bde6708..a0bddf4 100644
@@ -374,6 +374,8 @@ int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size,
 	bank->size			= size;
 	bank->type			= type;
 	bank->slot			= slot;
+	bank->memfd			= memfd;
+	bank->memfd_offset		= offset;
 
 	if (type & KVM_MEM_TYPE_READONLY)
 		flags |= KVM_MEM_READONLY;
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
author
#32 lore raw

RFC PATCH 28/32 Add functions for mapping/unmapping guest memory

In reply to Fuad Tabba

Host shouldn't map guest memory not shared with it. These functions allow kvmtool in a future patch to toggle the mapping depending on the sharing status, even if it changes.

---
 include/kvm/kvm.h |   4 +
 kvm.c             | 183 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 187 insertions(+)
include/kvm/kvm.h +4 -0
index 6192f6c..66b5664 100644
@@ -220,6 +220,10 @@ static inline bool kvm__arch_has_cfg_ram_address(void)
 
 void *guest_flat_to_host(struct kvm *kvm, u64 offset);
 u64 host_to_guest_flat(struct kvm *kvm, void *ptr);
+void map_guest_range(struct kvm *kvm, u64 gpa, u64 size);
+void unmap_guest_range(struct kvm *kvm, u64 gpa, u64 size);
+void map_guest(struct kvm *kvm);
+void unmap_guest(struct kvm *kvm);
 
 bool kvm__arch_load_kernel_image(struct kvm *kvm, int fd_kernel, int fd_initrd,
 				 const char *kernel_cmdline);
kvm.c +183 -0

189 lines not on this page · raw message

Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
author
#33 lore raw

RFC PATCH 29/32 pkvm: Add option to spawn a protected vm in pkvm

In reply to Fuad Tabba

From: Will Deacon <[email protected]>

For Testing

Even when pkvm is enabled, guests are not protected by default. This allows the creation of protected guests with kvmtool.

This is based on the current pKVM proposal [1, 2], and is likely to be different in the final version.

[1] https://lore.kernel.org/kvmarm/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/

---
 arm/aarch64/kvm.c                 |  3 +++
 arm/fdt.c                         | 18 ++++++++++++++++++
 arm/include/arm-common/fdt-arch.h |  2 +-
 arm/pci.c                         |  3 +++
 builtin-run.c                     |  2 ++
 include/kvm/kvm-config.h          |  1 +
 virtio/pci-modern.c               |  3 +++
 7 files changed, 31 insertions(+), 1 deletion(-)
arm/aarch64/kvm.c +3 -0
index 54200c9..f65c9c1 100644
@@ -131,6 +131,9 @@ int kvm__get_vm_type(struct kvm *kvm)
 	if (ipa_bits > max_ipa_bits)
 		die("Memory too large for this system (needs %d bits, %d available)", ipa_bits, max_ipa_bits);
 
+	if (kvm->cfg.pkvm)
+		return KVM_VM_TYPE_ARM_IPA_SIZE(ipa_bits) | (1U << 8);
+
 	return KVM_VM_TYPE_ARM_IPA_SIZE(ipa_bits);
 }
 
arm/fdt.c +18 -0
index 286ccad..0049bef 100644
@@ -116,6 +116,7 @@ static int setup_fdt(struct kvm *kvm)
 					void (*)(void *, u8, enum irq_type));
 	void (*generate_cpu_peripheral_fdt_nodes)(void *, struct kvm *)
 					= kvm->cpus[0]->generate_fdt_nodes;
+	u64 resv_mem_prop;
 
 	/* Create new tree without a reserve map */
 	_FDT(fdt_create(fdt, FDT_MAX_SIZE));
@@ -163,6 +164,23 @@ static int setup_fdt(struct kvm *kvm)
 	_FDT(fdt_property(fdt, "reg", mem_reg_prop, sizeof(mem_reg_prop)));
 	_FDT(fdt_end_node(fdt));
 
+	if (kvm->cfg.pkvm) {
+		/* Reserved memory (restricted DMA) */
+		_FDT(fdt_begin_node(fdt, "reserved-memory"));
+		_FDT(fdt_property_cell(fdt, "#address-cells", 0x2));
+		_FDT(fdt_property_cell(fdt, "#size-cells", 0x2));
+		_FDT(fdt_property(fdt, "ranges", NULL, 0));
+
+		_FDT(fdt_begin_node(fdt, "restricted_dma_reserved"));
+		_FDT(fdt_property_string(fdt, "compatible", "restricted-dma-pool"));
+		resv_mem_prop = cpu_to_fdt64(SZ_8M);
+		_FDT(fdt_property(fdt, "size", &resv_mem_prop, sizeof(resv_mem_prop)));
+		_FDT(fdt_property_cell(fdt, "phandle", PHANDLE_DMA));
+		_FDT(fdt_end_node(fdt));
+
+		_FDT(fdt_end_node(fdt));
+	}
+
 	/* CPU and peripherals (interrupt controller, timers, etc) */
 	generate_cpu_nodes(fdt, kvm);
 	if (generate_cpu_peripheral_fdt_nodes)
arm/include/arm-common/fdt-arch.h +1 -1
index 60c2d40..81df744 100644
@@ -1,6 +1,6 @@
 #ifndef ARM__FDT_H
 #define ARM__FDT_H
 
-enum phandles {PHANDLE_RESERVED = 0, PHANDLE_GIC, PHANDLE_MSI, PHANDLES_MAX};
+enum phandles {PHANDLE_RESERVED = 0, PHANDLE_GIC, PHANDLE_MSI, PHANDLE_DMA, PHANDLES_MAX};
 
 #endif /* ARM__FDT_H */
arm/pci.c +3 -0
index 5bd82d4..d183177 100644
@@ -74,6 +74,9 @@ void pci__generate_fdt_nodes(void *fdt, struct kvm *kvm)
 	if (irqchip == IRQCHIP_GICV2M || irqchip == IRQCHIP_GICV3_ITS)
 		_FDT(fdt_property_cell(fdt, "msi-parent", PHANDLE_MSI));
 
+	if (kvm->cfg.pkvm)
+		_FDT(fdt_property_cell(fdt, "memory-region", PHANDLE_DMA));
+
 	/* Generate the interrupt map ... */
 	dev_hdr = device__first_dev(DEVICE_BUS_PCI);
 	while (dev_hdr && nentries < ARRAY_SIZE(irq_map)) {
builtin-run.c +2 -0
index 4642bc4..9ec5701 100644
@@ -204,6 +204,8 @@ static int mem_parser(const struct option *opt, const char *arg, int unset)
 		    "Use legacy virtio transport"),			\
 	OPT_BOOLEAN('\0', "restricted_mem", &(cfg)->restricted_mem,	\
 		    "Use restricted memory for guests"),		\
+	OPT_BOOLEAN('\0', "pkvm", &(cfg)->pkvm,				\
+		    "Spawn a protected VM (pkvm)"),			\
 									\
 	OPT_GROUP("Kernel options:"),					\
 	OPT_STRING('k', "kernel", &(cfg)->kernel_filename, "kernel",	\
include/kvm/kvm-config.h +1 -0
index ea5f3ea..a18b8a3 100644
@@ -66,6 +66,7 @@ struct kvm_config {
 	bool mmio_debug;
 	bool virtio_legacy;
 	bool restricted_mem;
+	bool pkvm;
 };
 
 #endif
virtio/pci-modern.c +3 -0
index c5b4bc5..84af042 100644
@@ -150,6 +150,9 @@ static bool virtio_pci__common_read(struct virtio_device *vdev,
 	struct virtio_pci *vpci = vdev->virtio;
 	u64 features = 1ULL << VIRTIO_F_VERSION_1;
 
+	if (vpci->kvm->cfg.pkvm)
+		features |= 1ULL << VIRTIO_F_ACCESS_PLATFORM;
+
 	switch (offset - VPCI_CFG_COMMON_START) {
 	case VIRTIO_PCI_COMMON_DFSELECT:
 		val = vpci->device_features_sel;
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
author
#34 lore raw

RFC PATCH 30/32 pkvm: Enable exit hypercall capability if supported

In reply to Fuad Tabba

This hypercall allows the guest to communicate with the host via the new exit type. It will be used in future patches to communicate guest-triggered change of memory sharing status with the host/vmm (kvmtool).

---
 arm/aarch32/include/kvm/kvm-arch.h |  1 +
 arm/aarch64/include/kvm/kvm-arch.h |  1 +
 arm/aarch64/kvm.c                  | 23 +++++++++++++++++++++++
 arm/kvm-cpu.c                      | 10 ++++++++++
 arm/kvm.c                          |  1 +
 5 files changed, 36 insertions(+)
arm/aarch32/include/kvm/kvm-arch.h +1 -0
index 467fb09..5666f2f 100644
@@ -7,6 +7,7 @@
 
 struct kvm;
 static inline void kvm__arch_enable_mte(struct kvm *kvm) {}
+static inline void kvm__arch_enable_exit_hypcall(struct kvm *kvm) {}
 
 #define MAX_PAGE_SIZE	SZ_4K
 
arm/aarch64/include/kvm/kvm-arch.h +1 -0
index 02d09a4..9af002b 100644
@@ -7,6 +7,7 @@ struct kvm;
 unsigned long long kvm__arch_get_kern_offset(struct kvm *kvm, int fd);
 int kvm__arch_get_ipa_limit(struct kvm *kvm);
 void kvm__arch_enable_mte(struct kvm *kvm);
+void kvm__arch_enable_exit_hypcall(struct kvm *kvm);
 
 #define MAX_PAGE_SIZE	SZ_64K
 
arm/aarch64/kvm.c +23 -0
index f65c9c1..604a5e8 100644
@@ -163,3 +163,26 @@ void kvm__arch_enable_mte(struct kvm *kvm)
 
 	pr_debug("MTE capability enabled");
 }
+
+void kvm__arch_enable_exit_hypcall(struct kvm *kvm)
+{
+	struct kvm_enable_cap cap = {
+		.cap = KVM_CAP_EXIT_HYPERCALL,
+		.args[0] = KVM_EXIT_HYPERCALL_VALID_MASK,
+	};
+
+	if (kvm->cfg.arch.aarch32_guest) {
+		pr_debug("EXIT HYPERCALL is incompatible with AArch32");
+		return;
+	}
+
+	if (!kvm__supports_extension(kvm, KVM_CAP_EXIT_HYPERCALL)) {
+		pr_debug("EXIT HYPERCALL capability not available");
+		return;
+	}
+
+	if (ioctl(kvm->vm_fd, KVM_ENABLE_CAP, &cap))
+		die_perror("KVM_ENABLE_CAP(KVM_CAP_EXIT_HYPERCALL)");
+
+	pr_debug("EXIT capability enabled");
+}
arm/kvm-cpu.c +10 -0
index 98bc5fd..cb5a92a 100644
@@ -146,6 +146,16 @@ void kvm_cpu__delete(struct kvm_cpu *vcpu)
 
 bool kvm_cpu__handle_exit(struct kvm_cpu *vcpu)
 {
+	switch (vcpu->kvm_run->exit_reason) {
+	case KVM_EXIT_HYPERCALL:
+		pr_warning("Unhandled exit hypercall: 0x%llx, 0x%llx, 0x%llx, 0x%llx",
+			   vcpu->kvm_run->hypercall.nr,
+			   vcpu->kvm_run->hypercall.ret,
+			   vcpu->kvm_run->hypercall.args[0],
+			   vcpu->kvm_run->hypercall.args[1]);
+		return true;
+	}
+
 	return false;
 }
 
arm/kvm.c +1 -0
index 094fbe4..f4b0247 100644
@@ -84,6 +84,7 @@ void kvm__arch_init(struct kvm *kvm)
 		die("Failed to create virtual GIC");
 
 	kvm__arch_enable_mte(kvm);
+	kvm__arch_enable_exit_hypcall(kvm);
 }
 
 #define FDT_ALIGN	SZ_2M
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
author
#35 lore raw

RFC PATCH 31/32 pkvm: Handle (un)share hypercalls coming from the guest

In reply to Fuad Tabba

(Un)map guest memory if it's sharing status changes.

---
 arm/aarch64/include/asm/kvm.h |  7 +++++
 arm/kvm-cpu.c                 | 58 ++++++++++++++++++++++++++++++++---
 2 files changed, 60 insertions(+), 5 deletions(-)
arm/aarch64/include/asm/kvm.h +7 -0
index 316917b..662c178 100644
@@ -467,6 +467,13 @@ enum {
 /* run->fail_entry.hardware_entry_failure_reason codes. */
 #define KVM_EXIT_FAIL_ENTRY_CPU_UNSUPPORTED	(1ULL << 0)
 
+#define ARM_SMCCC_KVM_FUNC_MEM_SHARE		3
+#define ARM_SMCCC_KVM_FUNC_MEM_UNSHARE		4
+
+#define KVM_EXIT_HYPERCALL_VALID_MASK	((1ULL << ARM_SMCCC_KVM_FUNC_MEM_SHARE) |	\
+					 (1ULL << ARM_SMCCC_KVM_FUNC_MEM_UNSHARE))
+
+
 #endif
 
 #endif /* __ARM_KVM_H__ */
arm/kvm-cpu.c +53 -5

69 lines not on this page · raw message

Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
author
#36 lore raw

RFC PATCH 32/32 pkvm: Unmap all guest memory after initialization

In reply to Fuad Tabba

In pkvm, assume that all guest memory is private and unmap it by default. If it's shared and needed, the host can map it later.

---
 builtin-run.c | 3 +++
 1 file changed, 3 insertions(+)
builtin-run.c +3 -0
index 9ec5701..23a4f8b 100644
@@ -780,6 +780,9 @@ static struct kvm *kvm_cmd_run_init(int argc, const char **argv)
 	if (init_list__init(kvm) < 0)
 		die ("Initialisation failed");
 
+	if (kvm->cfg.pkvm)
+		unmap_guest(kvm);
+
 	return kvm;
 }
 
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog