aboutsummaryrefslogtreecommitdiff
path: root/util/memfd.c
diff options
context:
space:
mode:
Diffstat (limited to 'util/memfd.c')
-rw-r--r--util/memfd.c78
1 files changed, 51 insertions, 27 deletions
diff --git a/util/memfd.c b/util/memfd.c
index dce61f9..07d579e 100644
--- a/util/memfd.c
+++ b/util/memfd.c
@@ -27,7 +27,9 @@
#include "qemu/osdep.h"
+#include "qapi/error.h"
#include "qemu/memfd.h"
+#include "qemu/host-utils.h"
#if defined CONFIG_LINUX && !defined CONFIG_MEMFD
#include <sys/syscall.h>
@@ -51,36 +53,59 @@ static int memfd_create(const char *name, unsigned int flags)
#define MFD_ALLOW_SEALING 0x0002U
#endif
-int qemu_memfd_create(const char *name, size_t size, unsigned int seals)
+#ifndef MFD_HUGETLB
+#define MFD_HUGETLB 0x0004U
+#endif
+
+#ifndef MFD_HUGE_SHIFT
+#define MFD_HUGE_SHIFT 26
+#endif
+
+int qemu_memfd_create(const char *name, size_t size, bool hugetlb,
+ uint64_t hugetlbsize, unsigned int seals, Error **errp)
{
- int mfd = -1;
+ int htsize = hugetlbsize ? ctz64(hugetlbsize) : 0;
+
+ if (htsize && 1 << htsize != hugetlbsize) {
+ error_setg(errp, "Hugepage size must be a power of 2");
+ return -1;
+ }
+
+ htsize = htsize << MFD_HUGE_SHIFT;
#ifdef CONFIG_LINUX
+ int mfd = -1;
unsigned int flags = MFD_CLOEXEC;
if (seals) {
flags |= MFD_ALLOW_SEALING;
}
-
+ if (hugetlb) {
+ flags |= MFD_HUGETLB;
+ flags |= htsize;
+ }
mfd = memfd_create(name, flags);
if (mfd < 0) {
- return -1;
+ goto err;
}
if (ftruncate(mfd, size) == -1) {
- perror("ftruncate");
- close(mfd);
- return -1;
+ goto err;
}
if (seals && fcntl(mfd, F_ADD_SEALS, seals) == -1) {
- perror("fcntl");
- close(mfd);
- return -1;
+ goto err;
}
-#endif
return mfd;
+
+err:
+ if (mfd >= 0) {
+ close(mfd);
+ }
+#endif
+ error_setg_errno(errp, errno, "failed to create memfd");
+ return -1;
}
/*
@@ -90,14 +115,14 @@ int qemu_memfd_create(const char *name, size_t size, unsigned int seals)
* sealing.
*/
void *qemu_memfd_alloc(const char *name, size_t size, unsigned int seals,
- int *fd)
+ int *fd, Error **errp)
{
void *ptr;
- int mfd = qemu_memfd_create(name, size, seals);
+ int mfd = qemu_memfd_create(name, size, false, 0, seals, NULL);
/* some systems have memfd without sealing */
if (mfd == -1) {
- mfd = qemu_memfd_create(name, size, 0);
+ mfd = qemu_memfd_create(name, size, false, 0, 0, NULL);
}
if (mfd == -1) {
@@ -109,27 +134,26 @@ void *qemu_memfd_alloc(const char *name, size_t size, unsigned int seals,
unlink(fname);
g_free(fname);
- if (mfd == -1) {
- perror("mkstemp");
- return NULL;
- }
-
- if (ftruncate(mfd, size) == -1) {
- perror("ftruncate");
- close(mfd);
- return NULL;
+ if (mfd == -1 ||
+ ftruncate(mfd, size) == -1) {
+ goto err;
}
}
ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0);
if (ptr == MAP_FAILED) {
- perror("mmap");
- close(mfd);
- return NULL;
+ goto err;
}
*fd = mfd;
return ptr;
+
+err:
+ error_setg_errno(errp, errno, "failed to allocate shared memory");
+ if (mfd >= 0) {
+ close(mfd);
+ }
+ return NULL;
}
void qemu_memfd_free(void *ptr, size_t size, int fd)
@@ -157,7 +181,7 @@ bool qemu_memfd_check(void)
int fd;
void *ptr;
- ptr = qemu_memfd_alloc("test", 4096, 0, &fd);
+ ptr = qemu_memfd_alloc("test", 4096, 0, &fd, NULL);
memfd_check = ptr ? MEMFD_OK : MEMFD_KO;
qemu_memfd_free(ptr, 4096, fd);
}