From: "Marc-André Lureau" <marcandre.lureau@redhat.com>
To: qemu-devel@nongnu.org
Cc: pbonzini@redhat.com, "Marc-André Lureau" <marcandre.lureau@redhat.com>
Subject: [Qemu-devel] [PATCH v2 7/9] memfd: split qemu_memfd_alloc()
Date: Wed, 11 Jan 2017 12:33:45 +0100 [thread overview]
Message-ID: <20170111113347.7577-8-marcandre.lureau@redhat.com> (raw)
In-Reply-To: <20170111113347.7577-1-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>
---
include/qemu/memfd.h | 2 ++
util/memfd.c | 42 +++++++++++++++++++++++++-----------------
2 files changed, 27 insertions(+), 17 deletions(-)
diff --git a/include/qemu/memfd.h b/include/qemu/memfd.h
index 745a8c501e..30c1ab1d91 100644
--- a/include/qemu/memfd.h
+++ b/include/qemu/memfd.h
@@ -16,6 +16,8 @@
#define F_SEAL_WRITE 0x0008 /* prevent writes */
#endif
+int qemu_memfd_create(const char *name, size_t size, unsigned int seals,
+ bool must_seal);
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 4571d1aba8..e6476df083 100644
--- a/util/memfd.c
+++ b/util/memfd.c
@@ -55,45 +55,53 @@ static int memfd_create(const char *name, unsigned int flags)
#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)
+int qemu_memfd_create(const char *name, size_t size, unsigned int seals,
+ bool must_seal)
{
- void *ptr;
int mfd = -1;
- *fd = -1;
-
#ifdef CONFIG_LINUX
if (seals) {
mfd = memfd_create(name, MFD_ALLOW_SEALING | MFD_CLOEXEC);
}
- if (mfd == -1) {
+ if (mfd == -1 && !must_seal) {
/* some systems have memfd without sealing */
mfd = memfd_create(name, MFD_CLOEXEC);
seals = 0;
}
-#endif
- if (mfd != -1) {
+ if (mfd >= 0) {
if (ftruncate(mfd, size) == -1) {
perror("ftruncate");
close(mfd);
- return NULL;
+ return -1;
}
if (seals && fcntl(mfd, F_ADD_SEALS, seals) == -1) {
perror("fcntl");
close(mfd);
- return NULL;
+ return -1;
}
- } else {
+ }
+#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
+ * 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 = qemu_memfd_create(name, size, seals, false);
+
+ if (mfd == -1) {
const char *tmpdir = g_get_tmp_dir();
gchar *fname;
--
2.11.0
next prev parent reply other threads:[~2017-01-11 11:34 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-01-11 11:33 [Qemu-devel] [PATCH v2 0/9] Add memfd memory backend Marc-André Lureau
2017-01-11 11:33 ` [Qemu-devel] [PATCH v2 1/9] exec: check kvm mmu notifiers earlier Marc-André Lureau
2017-01-11 11:33 ` [Qemu-devel] [PATCH v2 2/9] exec: split file_ram_alloc() Marc-André Lureau
2017-01-11 11:33 ` [Qemu-devel] [PATCH v2 3/9] exec: split qemu_ram_alloc_from_file() Marc-André Lureau
2017-01-11 11:33 ` [Qemu-devel] [PATCH v2 4/9] Add memory_region_init_ram_from_fd() Marc-André Lureau
2017-01-11 11:33 ` [Qemu-devel] [PATCH v2 5/9] ivshmem: use ram_from_fd() Marc-André Lureau
2017-01-11 11:33 ` [Qemu-devel] [PATCH v2 6/9] memory: remove memory_region_set_fd Marc-André Lureau
2017-01-11 11:33 ` Marc-André Lureau [this message]
2017-01-11 11:33 ` [Qemu-devel] [PATCH v2 8/9] Add memfd based hostmem Marc-André Lureau
2017-01-11 11:33 ` [Qemu-devel] [PATCH v2 9/9] tests: use memfd in vhost-user-test Marc-André Lureau
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=20170111113347.7577-8-marcandre.lureau@redhat.com \
--to=marcandre.lureau@redhat.com \
--cc=pbonzini@redhat.com \
--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).