Received: from eggs.gnu.org ([2001:4830:134:3::10]:34903) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zoqc2-0004sY-33 for qemu-devel@nongnu.org; Wed, 21 Oct 2015 06:27:19 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zoqc1-0006CE-6s for qemu-devel@nongnu.org; Wed, 21 Oct 2015 06:27:18 -0400 Received: from mx1.redhat.com ([209.132.183.28]:57775) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zoqc0-0006C7-V9 for qemu-devel@nongnu.org; Wed, 21 Oct 2015 06:27:17 -0400 Date: Wed, 21 Oct 2015 13:27:14 +0300 From: "Michael S. Tsirkin" Message-ID: <1445423133-5119-14-git-send-email-mst@redhat.com> References: <1445423133-5119-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: <1445423133-5119-1-git-send-email-mst@redhat.com> Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PULL 13/38] util: add memfd helpers List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Peter Maydell , Thibaut Collet , =?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 Tested-by: Thibaut Collet --- include/qemu/memfd.h | 4 +++ util/memfd.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++= +++++- 2 files changed, 75 insertions(+), 1 deletion(-) 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 c509692..c119483 100644 --- a/util/memfd.c +++ b/util/memfd.c @@ -27,6 +27,11 @@ =20 #include "qemu/osdep.h" =20 +#include +#include + +#include + #include "qemu/memfd.h" =20 #ifdef CONFIG_MEMFD @@ -35,7 +40,7 @@ #include #include =20 -inline static int memfd_create(const char *name, unsigned int flags) +static int memfd_create(const char *name, unsigned int flags) { #ifdef __NR_memfd_create return syscall(__NR_memfd_create, name, flags); @@ -52,3 +57,68 @@ inline static int memfd_create(const char *name, unsig= ned int flags) #ifndef MFD_ALLOW_SEALING #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) +{ + void *ptr; + int mfd =3D -1; + + *fd =3D -1; + +#ifdef CONFIG_LINUX + 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; + } +#endif + + 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