From: "Marc-André Lureau" <marcandre.lureau@redhat.com>
To: qemu-devel@nongnu.org
Cc: pbonzini@redhat.com, ehabkost@redhat.com,
"Marc-André Lureau" <marcandre.lureau@redhat.com>
Subject: [Qemu-devel] [PATCH v5 1/6] memfd: split qemu_memfd_alloc()
Date: Mon, 23 Oct 2017 10:59:05 +0100 [thread overview]
Message-ID: <20171023095910.23202-2-marcandre.lureau@redhat.com> (raw)
In-Reply-To: <20171023095910.23202-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 | 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 4571d1aba8..e9c1a0e700 100644
--- a/util/memfd.c
+++ b/util/memfd.c
@@ -55,6 +55,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
@@ -65,35 +97,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.15.0.rc0.40.gaefcc5f6f
next prev parent reply other threads:[~2017-10-23 9:59 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-10-23 9:59 [Qemu-devel] [PATCH v5 0/6] Add memfd memory backend Marc-André Lureau
2017-10-23 9:59 ` Marc-André Lureau [this message]
2017-10-23 11:02 ` [Qemu-devel] [PATCH v5 1/6] memfd: split qemu_memfd_alloc() Philippe Mathieu-Daudé
2017-10-23 9:59 ` [Qemu-devel] [PATCH v5 2/6] memfd: remove needless include Marc-André Lureau
2017-10-23 10:59 ` Philippe Mathieu-Daudé
2017-10-23 9:59 ` [Qemu-devel] [PATCH v5 3/6] memfd: add error argument, instead of perror() Marc-André Lureau
2017-10-23 9:59 ` [Qemu-devel] [PATCH v5 4/6] memfd: add hugetlb support Marc-André Lureau
2017-10-23 10:23 ` Daniel P. Berrange
2017-10-23 11:27 ` Marc-André Lureau
2017-10-23 11:33 ` Daniel P. Berrange
2017-10-23 9:59 ` [Qemu-devel] [PATCH v5 5/6] Add memfd based hostmem Marc-André Lureau
2017-10-23 11:36 ` Daniel P. Berrange
2017-10-23 11:41 ` Marc-André Lureau
2017-10-23 11:47 ` Daniel P. Berrange
2017-10-23 9:59 ` [Qemu-devel] [PATCH v5 6/6] tests: use memfd in vhost-user-test Marc-André Lureau
2017-10-23 10:26 ` Daniel P. Berrange
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=20171023095910.23202-2-marcandre.lureau@redhat.com \
--to=marcandre.lureau@redhat.com \
--cc=ehabkost@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).