From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51439) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZK8oG-0004r0-Nb for qemu-devel@nongnu.org; Tue, 28 Jul 2015 13:37:01 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZK8oF-0000xB-LD for qemu-devel@nongnu.org; Tue, 28 Jul 2015 13:37:00 -0400 Received: from mail-qk0-x22a.google.com ([2607:f8b0:400d:c09::22a]:36228) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZK8oF-0000x7-HY for qemu-devel@nongnu.org; Tue, 28 Jul 2015 13:36:59 -0400 Received: by qkdv3 with SMTP id v3so53885456qkd.3 for ; Tue, 28 Jul 2015 10:36:59 -0700 (PDT) Sender: =?UTF-8?B?TWFyYy1BbmRyw6kgTHVyZWF1?= From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Tue, 28 Jul 2015 19:36:37 +0200 Message-Id: <1438105003-29501-4-git-send-email-marcandre.lureau@redhat.com> In-Reply-To: <1438105003-29501-1-git-send-email-marcandre.lureau@redhat.com> References: <1438105003-29501-1-git-send-email-marcandre.lureau@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Subject: [Qemu-devel] [PATCH v2 3/9] osdep: add memfd helpers List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: haifeng.lin@huawei.com, mst@redhat.com, thibaut.collet@6wind.com, jasowang@redhat.com, pbonzini@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Add qemu_memfd_alloc/free() helpers. The function helps to allocate and seal a memfd, and implements an open/unlink/mmap fallback for system that do not support memfd. Signed-off-by: Marc-André Lureau --- include/qemu/memfd.h | 4 +++ util/memfd.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 76 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..8b2b785 100644 --- a/util/memfd.c +++ b/util/memfd.c @@ -27,6 +27,14 @@ #include "config-host.h" +#include +#include + +#include +#include +#include +#include + #include "qemu/memfd.h" #ifdef CONFIG_MEMFD @@ -44,13 +52,75 @@ #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 + +void *qemu_memfd_alloc(const char *name, size_t size, unsigned int seals, + int *fd) +{ + void *ptr; + int mfd; + + mfd = memfd_create(name, MFD_ALLOW_SEALING|MFD_CLOEXEC); + if (mfd != -1) { + if (ftruncate(mfd, size) == -1) { + perror("ftruncate"); + close(mfd); + return NULL; + } + + if (fcntl(mfd, F_ADD_SEALS, seals) == -1) { + perror("fcntl"); + close(mfd); + return NULL; + } + } else { + const char *tmpdir = getenv("TMPDIR"); + gchar *fname; + + tmpdir = tmpdir ? tmpdir : "/tmp"; + + fname = g_strdup_printf("%s/memfd-XXXXXX", tmpdir); + mfd = mkstemp(fname); + unlink(fname); + g_free(fname); + + if (mfd == -1) { + perror("mkstemp"); + return NULL; + } + + if (ftruncate(mfd, size) == -1) { + perror("ftruncate"); + close(mfd); + 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); + } + + close(fd); +} -- 2.4.3