[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH v8 06/27] util: add memfd helpers
From: |
marcandre . lureau |
Subject: |
[Qemu-devel] [PATCH v8 06/27] util: add memfd helpers |
Date: |
Fri, 9 Oct 2015 17:17:20 +0200 |
From: Marc-André Lureau <address@hidden>
Add qemu_memfd_alloc/free() helpers.
The function helps to allocate and seal shared memory.
Signed-off-by: Marc-André Lureau <address@hidden>
---
include/qemu/memfd.h | 4 +++
util/memfd.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 75 insertions(+), 1 deletion(-)
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 c509692..c119483 100644
--- a/util/memfd.c
+++ b/util/memfd.c
@@ -27,6 +27,11 @@
#include "qemu/osdep.h"
+#include <glib.h>
+#include <glib/gprintf.h>
+
+#include <sys/mman.h>
+
#include "qemu/memfd.h"
#ifdef CONFIG_MEMFD
@@ -35,7 +40,7 @@
#include <sys/syscall.h>
#include <asm/unistd.h>
-inline static int memfd_create(const char *name, unsigned int flags)
+static int memfd_create(const char *name, unsigned int flags)
{
#ifdef __NR_memfd_create
return syscall(__NR_memfd_create, name, flags);
@@ -52,3 +57,68 @@ inline static int memfd_create(const char *name, unsigned
int flags)
#ifndef MFD_ALLOW_SEALING
#define MFD_ALLOW_SEALING 0x0002U
#endif
+
+/*
+ * This is a best-effort helper for shared memory allocation, with
+ * optional sealing. The helper will do his best to allocate using
+ * memfd with sealing, but may fallback on other methods without
+ * sealing.
+ */
+void *qemu_memfd_alloc(const char *name, size_t size, unsigned int seals,
+ int *fd)
+{
+ void *ptr;
+ int mfd = -1;
+
+ *fd = -1;
+
+#ifdef CONFIG_LINUX
+ if (seals) {
+ mfd = memfd_create(name, MFD_ALLOW_SEALING | MFD_CLOEXEC);
+ }
+
+ if (mfd == -1) {
+ /* some systems have memfd without sealing */
+ mfd = memfd_create(name, MFD_CLOEXEC);
+ seals = 0;
+ }
+#endif
+
+ if (mfd != -1) {
+ if (ftruncate(mfd, size) == -1) {
+ perror("ftruncate");
+ close(mfd);
+ return NULL;
+ }
+
+ if (seals && 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 v8 00/27] vhost-user: add migration support, marcandre . lureau, 2015/10/10
- [Qemu-devel] [PATCH v8 02/27] configure: probe for memfd, marcandre . lureau, 2015/10/09
- [Qemu-devel] [PATCH v8 06/27] util: add memfd helpers,
marcandre . lureau <=
- [Qemu-devel] [PATCH v8 05/27] util: add linux-only memfd fallback, marcandre . lureau, 2015/10/09
- [Qemu-devel] [PATCH v8 14/27] vhost: use a function for each call, marcandre . lureau, 2015/10/09
- [Qemu-devel] [PATCH v8 07/27] util: add fallback for qemu_memfd_alloc(), marcandre . lureau, 2015/10/09
- [Qemu-devel] [PATCH v8 12/27] vhost-user: send log shm fd along with log_base, marcandre . lureau, 2015/10/09
- [Qemu-devel] [PATCH v8 16/27] net: add trace_vhost_user_event, marcandre . lureau, 2015/10/09
- [Qemu-devel] [PATCH v8 18/27] vhost user: add rarp sending after live migration for legacy guest, marcandre . lureau, 2015/10/09
- [Qemu-devel] [PATCH v8 17/27] vhost user: add support of live migration, marcandre . lureau, 2015/10/09
- [Qemu-devel] [PATCH v8 26/27] vhost-user-test: check ownership during migration, marcandre . lureau, 2015/10/09
- [Qemu-devel] [PATCH v8 21/27] vhost-user-test: move wait_for_fds() out, marcandre . lureau, 2015/10/09
- [Qemu-devel] [PATCH v8 22/27] vhost-user-test: remove useless static check, marcandre . lureau, 2015/10/09