From: "Marc-André Lureau" <mlureau@redhat.com>
To: "Michael S. Tsirkin" <mst@redhat.com>
Cc: haifeng lin <haifeng.lin@huawei.com>,
thibaut collet <thibaut.collet@6wind.com>,
jasowang@redhat.com, qemu-devel@nongnu.org, pbonzini@redhat.com,
marcandre lureau <marcandre.lureau@redhat.com>
Subject: Re: [Qemu-devel] [PATCH v6 05/24] util: add memfd helpers
Date: Wed, 30 Sep 2015 10:13:39 -0400 (EDT) [thread overview]
Message-ID: <493260775.20710201.1443622419525.JavaMail.zimbra@redhat.com> (raw)
In-Reply-To: <20150930090610.GA3141@redhat.com>
----- Original Message -----
> On Tue, Sep 29, 2015 at 06:34:35PM +0200, marcandre.lureau@redhat.com wrote:
> > From: Marc-André Lureau <marcandre.lureau@redhat.com>
> >
> > Add qemu_memfd_alloc/free() helpers.
> >
> > The function helps to allocate and seal a memfd.
> >
> > Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> > ---
> > include/qemu/memfd.h | 4 ++++
> > util/memfd.c | 59
> > ++++++++++++++++++++++++++++++++++++++++++++++++++--
> > 2 files changed, 61 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..3168902 100644
> > --- a/util/memfd.c
> > +++ b/util/memfd.c
> > @@ -27,6 +27,14 @@
> >
> > #include "config-host.h"
> >
> > +#include <glib.h>
> > +#include <glib/gprintf.h>
> > +
> > +#include <stdio.h>
> > +#include <stdlib.h>
> > +#include <fcntl.h>
> > +#include <sys/mman.h>
> > +
> > #include "qemu/memfd.h"
> >
> > #ifdef CONFIG_MEMFD
> > @@ -44,13 +52,60 @@
> > #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;
> > +
> > + *fd = -1;
> > + 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;
> > + }
>
> 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 isn't much benefit over traditional open/mmap. If you want to use memfd differently, 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 memfd afaik.
Since some kernel support memfd without sealing, I'll add a fallback for that case.
>
> > + } else {
> > + perror("memfd");
> > + 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);
> > + }
> > +
> > + if (fd != -1) {
> > + close(fd);
> > + }
> > +}
> > --
> > 2.4.3
>
next prev parent reply other threads:[~2015-09-30 14:13 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-09-29 16:34 [Qemu-devel] [PATCH v6 00/24] vhost-user: add migration support marcandre.lureau
2015-09-29 16:34 ` [Qemu-devel] [PATCH v6 01/24] vhost-user: unit test for new messages marcandre.lureau
2015-09-29 16:34 ` [Qemu-devel] [PATCH v6 02/24] configure: probe for memfd marcandre.lureau
2015-09-29 16:34 ` [Qemu-devel] [PATCH v6 03/24] linux-headers: add unistd.h marcandre.lureau
2015-09-29 16:34 ` [Qemu-devel] [PATCH v6 04/24] util: add linux-only memfd fallback marcandre.lureau
2015-09-30 8:42 ` Michael S. Tsirkin
2015-10-01 17:20 ` Marc-André Lureau
2015-09-29 16:34 ` [Qemu-devel] [PATCH v6 05/24] util: add memfd helpers marcandre.lureau
2015-09-30 9:06 ` Michael S. Tsirkin
2015-09-30 14:13 ` Marc-André Lureau [this message]
2015-09-29 16:34 ` [Qemu-devel] [PATCH v6 06/24] memfd: add fallback for memfd marcandre.lureau
2015-09-30 9:03 ` Michael S. Tsirkin
2015-09-30 9:06 ` Marc-André Lureau
2015-09-30 9:08 ` Michael S. Tsirkin
2015-09-29 16:34 ` [Qemu-devel] [PATCH v6 07/24] vhost: document log resizing marcandre.lureau
2015-09-29 16:34 ` [Qemu-devel] [PATCH v6 08/24] vhost: add vhost_set_log_base op marcandre.lureau
2015-09-29 16:34 ` [Qemu-devel] [PATCH v6 09/24] vhost-user: add vhost_user_requires_shm_log() marcandre.lureau
2015-09-29 16:34 ` [Qemu-devel] [PATCH v6 10/24] vhost: alloc shareable log marcandre.lureau
2015-09-29 16:34 ` [Qemu-devel] [PATCH v6 11/24] vhost-user: send log shm fd along with log_base marcandre.lureau
2015-09-29 16:34 ` [Qemu-devel] [PATCH v6 12/24] vhost-user: add a migration blocker marcandre.lureau
2015-09-29 16:34 ` [Qemu-devel] [PATCH v6 13/24] vhost: use a function for each call marcandre.lureau
2015-09-29 16:34 ` [Qemu-devel] [PATCH v6 14/24] vhost-user: document migration log marcandre.lureau
2015-09-29 16:34 ` [Qemu-devel] [PATCH v6 15/24] net: add trace_vhost_user_event marcandre.lureau
2015-09-29 16:34 ` [Qemu-devel] [PATCH v6 16/24] vhost user: add support of live migration marcandre.lureau
2015-09-29 16:34 ` [Qemu-devel] [PATCH v6 17/24] vhost user: add rarp sending after live migration for legacy guest marcandre.lureau
2015-09-29 16:34 ` [Qemu-devel] [PATCH v6 18/24] vhost-user-test: move wait_for_fds() out marcandre.lureau
2015-09-29 16:34 ` [Qemu-devel] [PATCH v6 19/24] vhost-user-test: remove useless static check marcandre.lureau
2015-09-29 16:34 ` [Qemu-devel] [PATCH v6 20/24] vhost-user-test: wrap server in TestServer struct marcandre.lureau
2015-09-29 16:34 ` [Qemu-devel] [PATCH v6 21/24] vhost-user-test: learn to tweak various qemu arguments marcandre.lureau
2015-09-29 16:34 ` [Qemu-devel] [PATCH v6 22/24] vhost-user-test: add live-migration test marcandre.lureau
2015-09-29 16:34 ` [Qemu-devel] [PATCH v6 23/24] vhost-user-test: check ownership during migration marcandre.lureau
2015-09-29 16:34 ` [Qemu-devel] [PATCH v6 24/24] vhost-user: use an enum helper for features mask marcandre.lureau
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=493260775.20710201.1443622419525.JavaMail.zimbra@redhat.com \
--to=mlureau@redhat.com \
--cc=haifeng.lin@huawei.com \
--cc=jasowang@redhat.com \
--cc=marcandre.lureau@redhat.com \
--cc=mst@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=thibaut.collet@6wind.com \
/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).