qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Eduardo Habkost <ehabkost@redhat.com>
To: Peter Maydell <peter.maydell@linaro.org>, qemu-devel@nongnu.org
Cc: "Marcel Apfelbaum" <marcel@redhat.com>,
	"Marc-André Lureau" <marcandre.lureau@redhat.com>
Subject: [Qemu-devel] [PULL 01/19] memfd: split qemu_memfd_alloc()
Date: Thu, 18 Jan 2018 00:09:42 -0200	[thread overview]
Message-ID: <20180118021000.27203-2-ehabkost@redhat.com> (raw)
In-Reply-To: <20180118021000.27203-1-ehabkost@redhat.com>

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

Add a function to only create a memfd, without mmap. The function is
used in the following memory backend.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Message-Id: <20171023141815.17709-2-marcandre.lureau@redhat.com>
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 include/qemu/memfd.h |  1 +
 util/memfd.c         | 61 +++++++++++++++++++++++++++++++---------------------
 2 files changed, 37 insertions(+), 25 deletions(-)

diff --git a/include/qemu/memfd.h b/include/qemu/memfd.h
index 745a8c501e..41c24d807c 100644
--- a/include/qemu/memfd.h
+++ b/include/qemu/memfd.h
@@ -16,6 +16,7 @@
 #define F_SEAL_WRITE    0x0008  /* prevent writes */
 #endif
 
+int qemu_memfd_create(const char *name, size_t size, unsigned int seals);
 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);
diff --git a/util/memfd.c b/util/memfd.c
index 412e94a405..3a82505f8d 100644
--- a/util/memfd.c
+++ b/util/memfd.c
@@ -53,6 +53,38 @@ 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)
+{
+    int mfd = -1;
+
+#ifdef CONFIG_LINUX
+    unsigned int flags = MFD_CLOEXEC;
+
+    if (seals) {
+        flags |= MFD_ALLOW_SEALING;
+    }
+
+    mfd = memfd_create(name, flags);
+    if (mfd < 0) {
+        return -1;
+    }
+
+    if (ftruncate(mfd, size) == -1) {
+        perror("ftruncate");
+        close(mfd);
+        return -1;
+    }
+
+    if (seals && fcntl(mfd, F_ADD_SEALS, seals) == -1) {
+        perror("fcntl");
+        close(mfd);
+        return -1;
+    }
+#endif
+
+    return mfd;
+}
+
 /*
  * This is a best-effort helper for shared memory allocation, with
  * optional sealing. The helper will do his best to allocate using
@@ -63,35 +95,14 @@ 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);
-    }
+    int mfd = qemu_memfd_create(name, size, seals);
 
+    /* some systems have memfd without sealing */
     if (mfd == -1) {
-        /* some systems have memfd without sealing */
-        mfd = memfd_create(name, MFD_CLOEXEC);
-        seals = 0;
+        mfd = qemu_memfd_create(name, size, 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 {
+    if (mfd == -1) {
         const char *tmpdir = g_get_tmp_dir();
         gchar *fname;
 
-- 
2.14.3

  reply	other threads:[~2018-01-18  2:10 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-18  2:09 [Qemu-devel] [PULL 00/19] machine queue, 2018-01-18 Eduardo Habkost
2018-01-18  2:09 ` Eduardo Habkost [this message]
2018-01-18  2:09 ` [Qemu-devel] [PULL 02/19] memfd: remove needless include Eduardo Habkost
2018-01-18  2:09 ` [Qemu-devel] [PULL 03/19] qemu-options: document missing memory-backend-file options Eduardo Habkost
2018-01-18  2:09 ` [Qemu-devel] [PULL 04/19] qemu-options: document memory-backend-ram Eduardo Habkost
2018-01-18  2:09 ` [Qemu-devel] [PULL 05/19] numa: fix missing '-numa cpu' in '-help' output Eduardo Habkost
2018-01-18  2:09 ` [Qemu-devel] [PULL 06/19] machine: Replace has_dynamic_sysbus with list of allowed devices Eduardo Habkost
2018-01-18  2:09 ` [Qemu-devel] [PULL 07/19] hw/arm/virt: Allow only supported dynamic sysbus devices Eduardo Habkost
2018-01-18  2:09 ` [Qemu-devel] [PULL 08/19] ppc: e500: " Eduardo Habkost
2018-01-18  2:09 ` [Qemu-devel] [PULL 09/19] spapr: " Eduardo Habkost
2018-01-18  2:09 ` [Qemu-devel] [PULL 10/19] xen: Add only xen-sysdev to dynamic sysbus device list Eduardo Habkost
2018-01-18  2:09 ` [Qemu-devel] [PULL 11/19] q35: Allow only supported dynamic sysbus devices Eduardo Habkost
2018-01-18  2:09 ` [Qemu-devel] [PULL 12/19] qdev_monitor: Simplify error handling in qdev_device_add() Eduardo Habkost
2018-01-18  2:09 ` [Qemu-devel] [PULL 13/19] qdev: Check for the availability of a hotplug controller before adding a device Eduardo Habkost
2018-01-18  2:09 ` [Qemu-devel] [PULL 14/19] scripts: Remove fixed entries from the device-crash-test Eduardo Habkost
2018-01-18  2:09 ` [Qemu-devel] [PULL 15/19] hostmem-file: add "align" option Eduardo Habkost
2018-01-18  2:09 ` [Qemu-devel] [PULL 16/19] nvdimm: add a macro for property "label-size" Eduardo Habkost
2018-01-18  2:09 ` [Qemu-devel] [PULL 17/19] nvdimm: add 'unarmed' option Eduardo Habkost
2018-01-18  2:09 ` [Qemu-devel] [PULL 18/19] possible_cpus: add CPUArchId::type field Eduardo Habkost
2018-01-18  2:10 ` [Qemu-devel] [PULL 19/19] fw_cfg: fix memory corruption when all fw_cfg slots are used Eduardo Habkost
2018-01-18 15:22 ` [Qemu-devel] [PULL 00/19] machine queue, 2018-01-18 Peter Maydell
2018-01-18 15:36   ` Philippe Mathieu-Daudé
2018-01-18 19:59     ` Eduardo Habkost
2018-01-18 20:14     ` Eduardo Habkost
2018-01-18 21:20   ` Eduardo Habkost
2018-01-19  0:47     ` Haozhong Zhang
2018-01-19 13:15       ` Eduardo Habkost

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=20180118021000.27203-2-ehabkost@redhat.com \
    --to=ehabkost@redhat.com \
    --cc=marcandre.lureau@redhat.com \
    --cc=marcel@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).