diff mbox series

[RFC,kvmtool,v1,07/32] Use memfd for hugetlbfs when allocating guest ram

Message ID 20221202174417.1310826-8-tabba@google.com (mailing list archive)
State New, archived
Headers show
Series Add support for restricted guest memory in kvmtool | expand

Commit Message

Fuad Tabba Dec. 2, 2022, 5:43 p.m. UTC
This removes the need of using a temporary file for the fd, and
is the first step towards using memfd_create() across this
series.

Signed-off-by: Fuad Tabba <tabba@google.com>
---
 util/util.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
diff mbox series

Patch

diff --git a/util/util.c b/util/util.c
index 2c6fcc5..b347fa3 100644
--- a/util/util.c
+++ b/util/util.c
@@ -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);