diff mbox

[v6,06/24] memfd: add fallback for memfd

Message ID 1443544494-28737-7-git-send-email-marcandre.lureau@redhat.com
State New
Headers show

Commit Message

Marc-André Lureau Sept. 29, 2015, 4:34 p.m. UTC
From: Marc-André Lureau <marcandre.lureau@redhat.com>

Add an open/unlink/mmap fallback for system that do not support memfd.
This patch may require additional SELinux policies to work for enforced
systems, but should gracefully fail nonetheless.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 util/memfd.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)

Comments

Michael S. Tsirkin Sept. 30, 2015, 9:03 a.m. UTC | #1
On Tue, Sep 29, 2015 at 06:34:36PM +0200, marcandre.lureau@redhat.com wrote:
> From: Marc-André Lureau <marcandre.lureau@redhat.com>
> 
> Add an open/unlink/mmap fallback for system that do not support memfd.
> This patch may require additional SELinux policies to work for enforced
> systems, but should gracefully fail nonetheless.
> 
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>

I'd rather just fail migration.

> ---
>  util/memfd.c | 22 ++++++++++++++++++++--
>  1 file changed, 20 insertions(+), 2 deletions(-)
> 
> diff --git a/util/memfd.c b/util/memfd.c
> index 3168902..970b5b0 100644
> --- a/util/memfd.c
> +++ b/util/memfd.c
> @@ -84,8 +84,26 @@ void *qemu_memfd_alloc(const char *name, size_t size, unsigned int seals,
>              return NULL;
>          }
>      } else {
> -        perror("memfd");
> -        return NULL;
> +        const char *tmpdir = getenv("TMPDIR");
> +        gchar *fname;
> +
> +        tmpdir = tmpdir ? tmpdir : "/tmp";
> +
> +        fname = g_strdup_printf("%s/memfd-XXXXXX", tmpdir);
> +        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);
> -- 
> 2.4.3
Marc-Andre Lureau Sept. 30, 2015, 9:06 a.m. UTC | #2
Hi

----- Original Message -----
> On Tue, Sep 29, 2015 at 06:34:36PM +0200, marcandre.lureau@redhat.com wrote:
> > From: Marc-André Lureau <marcandre.lureau@redhat.com>
> > 
> > Add an open/unlink/mmap fallback for system that do not support memfd.
> > This patch may require additional SELinux policies to work for enforced
> > systems, but should gracefully fail nonetheless.
> > 
> > Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> 
> I'd rather just fail migration.

So we don't provide this compatibility code and migration should fail.

Would it be enough to check if memfd works at early runtime and add a migration blocker for vhost-user? Or is it possible to recover if migration fails when memfd fails to allocate? I would thing the former is better.

> 
> > ---
> >  util/memfd.c | 22 ++++++++++++++++++++--
> >  1 file changed, 20 insertions(+), 2 deletions(-)
> > 
> > diff --git a/util/memfd.c b/util/memfd.c
> > index 3168902..970b5b0 100644
> > --- a/util/memfd.c
> > +++ b/util/memfd.c
> > @@ -84,8 +84,26 @@ void *qemu_memfd_alloc(const char *name, size_t size,
> > unsigned int seals,
> >              return NULL;
> >          }
> >      } else {
> > -        perror("memfd");
> > -        return NULL;
> > +        const char *tmpdir = getenv("TMPDIR");
> > +        gchar *fname;
> > +
> > +        tmpdir = tmpdir ? tmpdir : "/tmp";
> > +
> > +        fname = g_strdup_printf("%s/memfd-XXXXXX", tmpdir);
> > +        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);
> > --
> > 2.4.3
>
Michael S. Tsirkin Sept. 30, 2015, 9:08 a.m. UTC | #3
On Wed, Sep 30, 2015 at 05:06:55AM -0400, Marc-André Lureau wrote:
> Hi
> 
> ----- Original Message -----
> > On Tue, Sep 29, 2015 at 06:34:36PM +0200, marcandre.lureau@redhat.com wrote:
> > > From: Marc-André Lureau <marcandre.lureau@redhat.com>
> > > 
> > > Add an open/unlink/mmap fallback for system that do not support memfd.
> > > This patch may require additional SELinux policies to work for enforced
> > > systems, but should gracefully fail nonetheless.
> > > 
> > > Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> > 
> > I'd rather just fail migration.
> 
> So we don't provide this compatibility code and migration should fail.
> 
> Would it be enough to check if memfd works at early runtime and add a migration blocker for vhost-user? Or is it possible to recover if migration fails when memfd fails to allocate? I would thing the former is better.

Fine with me.

> > 
> > > ---
> > >  util/memfd.c | 22 ++++++++++++++++++++--
> > >  1 file changed, 20 insertions(+), 2 deletions(-)
> > > 
> > > diff --git a/util/memfd.c b/util/memfd.c
> > > index 3168902..970b5b0 100644
> > > --- a/util/memfd.c
> > > +++ b/util/memfd.c
> > > @@ -84,8 +84,26 @@ void *qemu_memfd_alloc(const char *name, size_t size,
> > > unsigned int seals,
> > >              return NULL;
> > >          }
> > >      } else {
> > > -        perror("memfd");
> > > -        return NULL;
> > > +        const char *tmpdir = getenv("TMPDIR");
> > > +        gchar *fname;
> > > +
> > > +        tmpdir = tmpdir ? tmpdir : "/tmp";
> > > +
> > > +        fname = g_strdup_printf("%s/memfd-XXXXXX", tmpdir);
> > > +        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);
> > > --
> > > 2.4.3
> >
diff mbox

Patch

diff --git a/util/memfd.c b/util/memfd.c
index 3168902..970b5b0 100644
--- a/util/memfd.c
+++ b/util/memfd.c
@@ -84,8 +84,26 @@  void *qemu_memfd_alloc(const char *name, size_t size, unsigned int seals,
             return NULL;
         }
     } else {
-        perror("memfd");
-        return NULL;
+        const char *tmpdir = getenv("TMPDIR");
+        gchar *fname;
+
+        tmpdir = tmpdir ? tmpdir : "/tmp";
+
+        fname = g_strdup_printf("%s/memfd-XXXXXX", tmpdir);
+        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);