From: "Michael S. Tsirkin" <mst@redhat.com>
To: "Marc-André Lureau" <mlureau@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 v3 03/16] util: add memfd helpers
Date: Tue, 29 Sep 2015 18:41:33 +0300 [thread overview]
Message-ID: <20150929183958-mutt-send-email-mst@redhat.com> (raw)
In-Reply-To: <1449381360.19918275.1443540304770.JavaMail.zimbra@redhat.com>
On Tue, Sep 29, 2015 at 11:25:04AM -0400, Marc-André Lureau wrote:
>
>
> ----- Original Message -----
> > On Thu, Aug 06, 2015 at 02:40:39PM +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, and implements an
> > > open/unlink/mmap fallback for system that do not support memfd.
> > >
> > > Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> > > ---
> > > 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 <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,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);
> >
> >
> > Hmm. Does this interact correctly with the -mem-prealloc flag?
>
> It's unrelated imho. It's helper here.
>
> In the rest of the series, it's used at runtime when migrating with variable size (today code doesn't prealloc that either)
Yes but I think this means it will fault on access even if user
requested -realtime mlock=on.
> >
> > > + if (mfd != -1) {
> > > + if (ftruncate(mfd, size) == -1) {
> >
> > Any limitations on size?
>
> not that I know (reading memfd_create)
>
> >
> > > + 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);
> >
> > This means there's now work to be done to set up selinux
> > to allow QEMU creating memfd under /tmp.
>
> doesn't sound unreasonable to me
>
> >
> > Maybe it's better to just fail gracefully for now.
>
> it's a fallback, but sure we can remove it and add it back later if needed
>
> > > + 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);
> >
> > Pls add space around | here and elsewhere.
> >
>
> ok
> >
> > > + 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);
> >
> > I notice you close fd unconditionally, but it's only returned
> > on success above. So this will close an uninitialized one?
>
> Ok, I'll add a -1 check
Will only work well if you set *fd to -1 on error above ...
--
MST
next prev parent reply other threads:[~2015-09-29 15:41 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-08-06 12:40 [Qemu-devel] [PATCH v3 00/16] vhost-user: add migration log support (for 2.5) marcandre.lureau
2015-08-06 12:40 ` [Qemu-devel] [PATCH v3 01/16] configure: probe for memfd marcandre.lureau
2015-08-06 12:40 ` [Qemu-devel] [PATCH v3 02/16] util: add linux-only memfd fallback marcandre.lureau
2015-08-06 12:40 ` [Qemu-devel] [PATCH v3 03/16] util: add memfd helpers marcandre.lureau
2015-09-29 14:57 ` Michael S. Tsirkin
2015-09-29 15:25 ` Marc-André Lureau
2015-09-29 15:41 ` Michael S. Tsirkin [this message]
2015-08-06 12:40 ` [Qemu-devel] [PATCH v3 04/16] vhost: alloc shareable log marcandre.lureau
2015-09-16 14:06 ` Michael S. Tsirkin
2015-09-19 9:01 ` Marc-André Lureau
2015-08-06 12:40 ` [Qemu-devel] [PATCH v3 05/16] vhost: document log resizing marcandre.lureau
2015-08-06 12:40 ` [Qemu-devel] [PATCH v3 06/16] vhost: use variable arguments for vhost_call() marcandre.lureau
2015-09-16 14:01 ` Michael S. Tsirkin
2015-09-19 8:58 ` Marc-André Lureau
2015-08-06 12:40 ` [Qemu-devel] [PATCH v3 07/16] vhost-user: start and end the va_list marcandre.lureau
2015-08-06 12:40 ` [Qemu-devel] [PATCH v3 08/16] vhost-user: send log shm fd along with log_base marcandre.lureau
2015-09-16 14:08 ` Michael S. Tsirkin
2015-09-19 8:59 ` Marc-André Lureau
2015-08-06 12:40 ` [Qemu-devel] [PATCH v3 09/16] vhost-user: document migration log marcandre.lureau
2015-08-06 12:40 ` [Qemu-devel] [PATCH v3 10/16] net: add trace_vhost_user_event marcandre.lureau
2015-08-06 12:40 ` [Qemu-devel] [PATCH v3 11/16] vhost-user-test: move wait_for_fds() out marcandre.lureau
2015-08-06 12:40 ` [Qemu-devel] [PATCH v3 12/16] vhost-user-test: remove useless static check marcandre.lureau
2015-08-06 12:40 ` [Qemu-devel] [PATCH v3 13/16] vhost-user-test: wrap server in TestServer struct marcandre.lureau
2015-08-06 12:40 ` [Qemu-devel] [PATCH v3 14/16] vhost-user-test: learn to tweak various qemu arguments marcandre.lureau
2015-08-06 12:40 ` [Qemu-devel] [PATCH v3 15/16] vhost-user-test: add live-migration test marcandre.lureau
2015-08-06 12:40 ` [Qemu-devel] [PATCH v3 16/16] vhost-user-test: check ownership during migration marcandre.lureau
2015-09-16 14:02 ` [Qemu-devel] [PATCH v3 00/16] vhost-user: add migration log support (for 2.5) Michael S. Tsirkin
2015-09-16 14:46 ` Michael S. Tsirkin
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=20150929183958-mutt-send-email-mst@redhat.com \
--to=mst@redhat.com \
--cc=haifeng.lin@huawei.com \
--cc=jasowang@redhat.com \
--cc=marcandre.lureau@redhat.com \
--cc=mlureau@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).