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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxx>
---
 util/util.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

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);
-- 
2.39.0.rc0.267.gcb52ba06e7-goog




[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux