[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH v6 05/24] util: add memfd helpers
From: |
marcandre . lureau |
Subject: |
[Qemu-devel] [PATCH v6 05/24] util: add memfd helpers |
Date: |
Tue, 29 Sep 2015 18:34:35 +0200 |
From: Marc-André Lureau <address@hidden>
Add qemu_memfd_alloc/free() helpers.
The function helps to allocate and seal a memfd.
Signed-off-by: Marc-André Lureau <address@hidden>
---
include/qemu/memfd.h | 4 ++++
util/memfd.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++--
2 files changed, 61 insertions(+), 2 deletions(-)
diff --git a/include/qemu/memfd.h b/include/qemu/memfd.h
index 8b1fe6a..950fb88 100644
--- a/include/qemu/memfd.h
+++ b/include/qemu/memfd.h
@@ -17,4 +17,8 @@
#define F_SEAL_WRITE 0x0008 /* prevent writes */
#endif
+void *qemu_memfd_alloc(const char *name, size_t size, unsigned int seals,
+ int *fd);
+void qemu_memfd_free(void *ptr, size_t size, int fd);
+
#endif /* QEMU_MEMFD_H */
diff --git a/util/memfd.c b/util/memfd.c
index a98d57e..3168902 100644
--- a/util/memfd.c
+++ b/util/memfd.c
@@ -27,6 +27,14 @@
#include "config-host.h"
+#include <glib.h>
+#include <glib/gprintf.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <sys/mman.h>
+
#include "qemu/memfd.h"
#ifdef CONFIG_MEMFD
@@ -44,13 +52,60 @@
#define MFD_ALLOW_SEALING 0x0002U
#endif
-static inline int memfd_create(const char *name, unsigned int flags)
+static int memfd_create(const char *name, unsigned int flags)
{
return syscall(__NR_memfd_create, name, flags);
}
#else /* !LINUX */
-static inline int memfd_create(const char *name, unsigned int flags)
+static int memfd_create(const char *name, unsigned int flags)
{
return -1;
}
#endif
+
+void *qemu_memfd_alloc(const char *name, size_t size, unsigned int seals,
+ int *fd)
+{
+ void *ptr;
+ int mfd;
+
+ *fd = -1;
+ mfd = memfd_create(name, MFD_ALLOW_SEALING | MFD_CLOEXEC);
+ if (mfd != -1) {
+ if (ftruncate(mfd, size) == -1) {
+ perror("ftruncate");
+ close(mfd);
+ return NULL;
+ }
+
+ if (fcntl(mfd, F_ADD_SEALS, seals) == -1) {
+ perror("fcntl");
+ close(mfd);
+ return NULL;
+ }
+ } else {
+ perror("memfd");
+ return NULL;
+ }
+
+ ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0);
+ if (ptr == MAP_FAILED) {
+ perror("mmap");
+ close(mfd);
+ return NULL;
+ }
+
+ *fd = mfd;
+ return ptr;
+}
+
+void qemu_memfd_free(void *ptr, size_t size, int fd)
+{
+ if (ptr) {
+ munmap(ptr, size);
+ }
+
+ if (fd != -1) {
+ close(fd);
+ }
+}
--
2.4.3
- [Qemu-devel] [PATCH v6 01/24] vhost-user: unit test for new messages, (continued)
- [Qemu-devel] [PATCH v6 01/24] vhost-user: unit test for new messages, marcandre . lureau, 2015/10/08
- [Qemu-devel] [PATCH v6 04/24] util: add linux-only memfd fallback, marcandre . lureau, 2015/10/08
- [Qemu-devel] [PATCH v6 07/24] vhost: document log resizing, marcandre . lureau, 2015/10/08
- [Qemu-devel] [PATCH v6 12/24] vhost-user: add a migration blocker, marcandre . lureau, 2015/10/08
- [Qemu-devel] [PATCH v6 03/24] linux-headers: add unistd.h, marcandre . lureau, 2015/10/08
- [Qemu-devel] [PATCH v6 17/24] vhost user: add rarp sending after live migration for legacy guest, marcandre . lureau, 2015/10/08
- [Qemu-devel] [PATCH v6 20/24] vhost-user-test: wrap server in TestServer struct, marcandre . lureau, 2015/10/08
- [Qemu-devel] [PATCH v6 02/24] configure: probe for memfd, marcandre . lureau, 2015/10/08
- [Qemu-devel] [PATCH v6 05/24] util: add memfd helpers,
marcandre . lureau <=
- [Qemu-devel] [PATCH v6 10/24] vhost: alloc shareable log, marcandre . lureau, 2015/10/08
- [Qemu-devel] [PATCH v6 18/24] vhost-user-test: move wait_for_fds() out, marcandre . lureau, 2015/10/08
- [Qemu-devel] [PATCH v6 19/24] vhost-user-test: remove useless static check, marcandre . lureau, 2015/10/08
- [Qemu-devel] [PATCH v6 21/24] vhost-user-test: learn to tweak various qemu arguments, marcandre . lureau, 2015/10/08