From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58659) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZkIYU-0005xY-Rk for qemu-devel@nongnu.org; Thu, 08 Oct 2015 17:16:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZkIYT-000778-Pf for qemu-devel@nongnu.org; Thu, 08 Oct 2015 17:16:50 -0400 Received: from mx1.redhat.com ([209.132.183.28]:51614) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZkIYT-000773-IN for qemu-devel@nongnu.org; Thu, 08 Oct 2015 17:16:49 -0400 Date: Fri, 9 Oct 2015 00:16:45 +0300 From: "Michael S. Tsirkin" Message-ID: <1444338957-15293-5-git-send-email-mst@redhat.com> References: <1444338957-15293-1-git-send-email-mst@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline In-Reply-To: <1444338957-15293-1-git-send-email-mst@redhat.com> Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PULL 04/25] util: add memfd helpers List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Peter Maydell , =?us-ascii?B?PT9VVEYtOD9xP01hcmMtQW5kcj1DMz1BOT0yMEx1cmVhdT89?= From: Marc-Andr=E9 Lureau Add qemu_memfd_alloc/free() helpers. The function helps to allocate and seal shared memory. Signed-off-by: Marc-Andr=E9 Lureau Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- 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 =20 +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 @@ =20 #include "config-host.h" =20 +#include +#include + +#include +#include +#include +#include + #include "qemu/memfd.h" =20 #ifdef CONFIG_MEMFD @@ -44,13 +52,76 @@ #define MFD_ALLOW_SEALING 0x0002U #endif =20 -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 =3D -1; + + *fd =3D -1; + + if (seals) { + mfd =3D memfd_create(name, MFD_ALLOW_SEALING | MFD_CLOEXEC); + } + + if (mfd =3D=3D -1) { + /* some systems have memfd without sealing */ + mfd =3D memfd_create(name, MFD_CLOEXEC); + seals =3D 0; + } + + if (mfd !=3D -1) { + if (ftruncate(mfd, size) =3D=3D -1) { + perror("ftruncate"); + close(mfd); + return NULL; + } + + if (seals && fcntl(mfd, F_ADD_SEALS, seals) =3D=3D -1) { + perror("fcntl"); + close(mfd); + return NULL; + } + } else { + perror("memfd"); + return NULL; + } + + ptr =3D mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0); + if (ptr =3D=3D MAP_FAILED) { + perror("mmap"); + close(mfd); + return NULL; + } + + *fd =3D mfd; + return ptr; +} + +void qemu_memfd_free(void *ptr, size_t size, int fd) +{ + if (ptr) { + munmap(ptr, size); + } + + if (fd !=3D -1) { + close(fd); + } +} --=20 MST