From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59642) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZhI8n-0004eJ-6h for qemu-devel@nongnu.org; Wed, 30 Sep 2015 10:13:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZhI8k-0004Vi-0U for qemu-devel@nongnu.org; Wed, 30 Sep 2015 10:13:53 -0400 Received: from mx6-phx2.redhat.com ([209.132.183.39]:60488) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZhI8j-0004Ur-PH for qemu-devel@nongnu.org; Wed, 30 Sep 2015 10:13:49 -0400 Date: Wed, 30 Sep 2015 10:13:39 -0400 (EDT) From: =?utf-8?Q?Marc-Andr=C3=A9?= Lureau Message-ID: <493260775.20710201.1443622419525.JavaMail.zimbra@redhat.com> In-Reply-To: <20150930090610.GA3141@redhat.com> References: <1443544494-28737-1-git-send-email-marcandre.lureau@redhat.com> <1443544494-28737-6-git-send-email-marcandre.lureau@redhat.com> <20150930090610.GA3141@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH v6 05/24] util: add memfd helpers List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: "Michael S. Tsirkin" Cc: haifeng lin , thibaut collet , jasowang@redhat.com, qemu-devel@nongnu.org, pbonzini@redhat.com, marcandre lureau ----- Original Message ----- > On Tue, Sep 29, 2015 at 06:34:35PM +0200, marcandre.lureau@redhat.com wro= te: > > From: Marc-Andr=C3=A9 Lureau > >=20 > > Add qemu_memfd_alloc/free() helpers. > >=20 > > The function helps to allocate and seal a memfd. > >=20 > > Signed-off-by: Marc-Andr=C3=A9 Lureau > > --- > > include/qemu/memfd.h | 4 ++++ > > util/memfd.c | 59 > > ++++++++++++++++++++++++++++++++++++++++++++++++++-- > > 2 files changed, 61 insertions(+), 2 deletions(-) > >=20 > > 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 sea= ls, > > + 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..3168902 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,60 @@ > > #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 > > + > > +void *qemu_memfd_alloc(const char *name, size_t size, unsigned int sea= ls, > > + int *fd) > > +{ > > + void *ptr; > > + int mfd; > > + > > + *fd =3D -1; > > + mfd =3D memfd_create(name, MFD_ALLOW_SEALING | MFD_CLOEXEC); > > + if (mfd !=3D -1) { > > + if (ftruncate(mfd, size) =3D=3D -1) { > > + perror("ftruncate"); > > + close(mfd); > > + return NULL; > > + } > > + > > + if (fcntl(mfd, F_ADD_SEALS, seals) =3D=3D -1) { > > + perror("fcntl"); > > + close(mfd); > > + return NULL; > > + } >=20 > Why do it here? I note that you don't try to do this with the tmpfs > fallback. The idea of the helper is to use the best practices of using memfd: sealing= , and provide graceful feedback if not available. Without sealing, there is= n't much benefit over traditional open/mmap. If you want to use memfd diffe= rently, for ex to enforce sealing, then the helper isn't of much use. F_ADD= _SEALS doesn't work on regular open/mmap shared memory, it was added with m= emfd afaik. Since some kernel support memfd without sealing, I'll add a fallback for th= at case. >=20 > > + } 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); > > + } > > +} > > -- > > 2.4.3 >=20