qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Michael S. Tsirkin" <mst@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Peter Maydell" <peter.maydell@linaro.org>,
	=?UTF-8?q?Marc-Andr=C3=A9=20Lureau?=
	<marcandre.lureau@redhat.com>
Subject: [Qemu-devel] [PULL 04/25] util: add memfd helpers
Date: Fri, 9 Oct 2015 00:16:45 +0300	[thread overview]
Message-ID: <1444338957-15293-5-git-send-email-mst@redhat.com> (raw)
In-Reply-To: <1444338957-15293-1-git-send-email-mst@redhat.com>

From: Marc-André Lureau <marcandre.lureau@redhat.com>

Add qemu_memfd_alloc/free() helpers.

The function helps to allocate and seal shared memory.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
 include/qemu/memfd.h |  4 +++
 util/memfd.c         | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 77 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..dd47552 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,76 @@
 #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
+
+/*
+ * 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;
+
+    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;
+    }
+
+    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);
+    }
+}
-- 
MST

  parent reply	other threads:[~2015-10-08 21:16 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-08 21:16 [Qemu-devel] [PULL 00/25] virtio,pc features, fixes Michael S. Tsirkin
2015-10-08 21:16 ` [Qemu-devel] [PULL 01/25] configure: probe for memfd Michael S. Tsirkin
2015-10-08 21:16 ` [Qemu-devel] [PULL 02/25] linux-headers: add unistd.h Michael S. Tsirkin
2015-10-08 21:16 ` [Qemu-devel] [PULL 03/25] util: add linux-only memfd fallback Michael S. Tsirkin
2015-10-08 21:16 ` Michael S. Tsirkin [this message]
2015-10-08 21:16 ` [Qemu-devel] [PULL 05/25] util: add fallback for qemu_memfd_alloc() Michael S. Tsirkin
2015-10-08 21:16 ` [Qemu-devel] [PULL 06/25] vhost: document log resizing Michael S. Tsirkin
2015-10-08 21:16 ` [Qemu-devel] [PULL 07/25] vhost: add vhost_set_log_base op Michael S. Tsirkin
2015-10-08 21:17 ` [Qemu-devel] [PULL 08/25] vhost-user: add vhost_user_requires_shm_log() Michael S. Tsirkin
2015-10-08 21:17 ` [Qemu-devel] [PULL 09/25] vhost: alloc shareable log Michael S. Tsirkin
2015-10-08 21:17 ` [Qemu-devel] [PULL 10/25] vhost-user: send log shm fd along with log_base Michael S. Tsirkin
2015-10-08 21:17 ` [Qemu-devel] [PULL 11/25] vhost-user: add a migration blocker Michael S. Tsirkin
2015-10-08 21:17 ` [Qemu-devel] [PULL 12/25] vhost: use a function for each call Michael S. Tsirkin
2015-10-09  6:40   ` Thibaut Collet
2015-10-09  7:37     ` Michael S. Tsirkin
2015-10-08 21:17 ` [Qemu-devel] [PULL 13/25] vhost-user: document migration log Michael S. Tsirkin
2015-10-08 21:17 ` [Qemu-devel] [PULL 14/25] net: add trace_vhost_user_event Michael S. Tsirkin
2015-10-08 21:17 ` [Qemu-devel] [PULL 15/25] vhost user: add support of live migration Michael S. Tsirkin
2015-10-08 21:17 ` [Qemu-devel] [PULL 16/25] vhost user: add rarp sending after live migration for legacy guest Michael S. Tsirkin
2015-10-08 21:17 ` [Qemu-devel] [PULL 17/25] vhost-user: use an enum helper for features mask Michael S. Tsirkin
2015-10-08 21:17 ` [Qemu-devel] [PULL 18/25] vhost: add migration block if memfd failed Michael S. Tsirkin
2015-10-08 21:18 ` [Qemu-devel] [PULL 19/25] vhost-user-test: move wait_for_fds() out Michael S. Tsirkin
2015-10-08 21:18 ` [Qemu-devel] [PULL 20/25] vhost-user-test: remove useless static check Michael S. Tsirkin
2015-10-08 21:18 ` [Qemu-devel] [PULL 21/25] vhost-user-test: wrap server in TestServer struct Michael S. Tsirkin
2015-10-08 21:18 ` [Qemu-devel] [PULL 22/25] vhost-user-test: learn to tweak various qemu arguments Michael S. Tsirkin
2015-10-08 21:18 ` [Qemu-devel] [PULL 23/25] vhost-user-test: add live-migration test Michael S. Tsirkin
2015-10-08 21:18 ` [Qemu-devel] [PULL 24/25] vhost-user-test: check ownership during migration Michael S. Tsirkin
2015-10-08 21:18 ` [Qemu-devel] [PULL 25/25] intel_iommu: Add support for translation for devices behind bridges Michael S. Tsirkin
2015-10-09 11:05 ` [Qemu-devel] [PULL 00/25] virtio,pc features, fixes Peter Maydell
2015-10-09 11:09   ` Peter Maydell
2015-10-09 13:10     ` Eduardo Otubo
2015-10-09 13:37   ` Peter Maydell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1444338957-15293-5-git-send-email-mst@redhat.com \
    --to=mst@redhat.com \
    --cc=marcandre.lureau@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).