Skip to contentSkip to conversation

backends/hostmem: Make QOM types and properties unconditional

PATCH In review Markus Armbruster 3 participants 26 messages Nov 11 – Nov 12 Arm STM32

lore
Share & tools
lore
#0 lore raw

cover backends/hostmem: Make QOM types and properties unconditional

I recently checked qom introspection for dangling child and link types, and found one from sgx-epc to memory-backend-epc. I reported

    Subject: qemu-system-x86_64 appears to have sgx-epc, but not memory-backend-epc
    Date: Thu, 22 Oct 2026 14:58:16 +0200
    Message-ID: <[email protected]>

Daniel identified why: we register QOM type "memory-backend-epc" conditionally. He called that "rather dubious". We found similar conditional registration of "hostmem-memfd".

This series makes them both unconditional, along with a two class properties. It also improves error reporting.

Markus Armbruster (7):
backends/hostmem: Drop redundant validation of property "size" backends/hostmem-file: Drop useless validation of property "mem-path" backends/hostmem: Improve error reporting when method alloc() fails backends/hostmem-epc: Register QOM type unconditionally backends/hostmem-memfd: Register QOM type unconditionally util/memfd: Improve error message when memfd_create() fails backends/hostmem-memfd: Add huge TLB properties unconditionally

 backends/hostmem-epc.c   | 12 +-----------
 backends/hostmem-file.c  |  9 ---------
 backends/hostmem-memfd.c | 35 +++++++++++++----------------------
 backends/hostmem-ram.c   |  5 -----
 backends/hostmem-shm.c   |  7 +------
 backends/hostmem.c       |  3 +++
 util/memfd.c             |  5 ++++-
 7 files changed, 22 insertions(+), 54 deletions(-)
Signature · 1 line
2.55.0
#1 lore raw

PATCH 1/7 backends/hostmem: Drop redundant validation of property "size"

Property "size" is mandatory, and its setter host_memory_backend_set_size() rejects value zero. Delete the redundant checks for zero in the .alloc() callbacks.

---
 backends/hostmem-epc.c   | 5 -----
 backends/hostmem-file.c  | 4 ----
 backends/hostmem-memfd.c | 5 -----
 backends/hostmem-ram.c   | 5 -----
 backends/hostmem-shm.c   | 5 -----
 5 files changed, 24 deletions(-)
backends/hostmem-epc.c +0 -5
index ab20b182331..e1c784249ff 100644
@@ -24,11 +24,6 @@ sgx_epc_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
     uint32_t ram_flags;
     int fd;
 
-    if (!backend->size) {
-        error_setg(errp, "can't create backend with size 0");
-        return false;
-    }
-
     fd = qemu_open("/dev/sgx_vepc", O_RDWR, errp);
     if (fd < 0) {
         return false;
backends/hostmem-file.c +0 -4
index 6ad9cad0320..08d0382d76f 100644
@@ -48,10 +48,6 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
     g_autofree gchar *name = NULL;
     uint32_t ram_flags;
 
-    if (!backend->size) {
-        error_setg(errp, "can't create backend with size 0");
-        return false;
-    }
     if (!fb->mem_path) {
         error_setg(errp, "mem-path property not set");
         return false;
backends/hostmem-memfd.c +0 -5
index 46489212a23..11d456e3ebd 100644
@@ -49,11 +49,6 @@ memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
     int fd = cpr_find_fd(name, 0);
     uint32_t ram_flags;
 
-    if (!backend->size) {
-        error_setg(errp, "can't create backend with size 0");
-        return false;
-    }
-
     if (fd >= 0) {
         goto have_fd;
     }
backends/hostmem-ram.c +0 -5
index 6a507fad77c..0ae7b4ae759 100644
@@ -22,11 +22,6 @@ ram_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
     g_autofree char *name = NULL;
     uint32_t ram_flags;
 
-    if (!backend->size) {
-        error_setg(errp, "can't create backend with size 0");
-        return false;
-    }
-
     name = host_memory_backend_get_name(backend);
     ram_flags = backend->share ? RAM_SHARED : RAM_PRIVATE;
     ram_flags |= backend->reserve ? 0 : RAM_NORESERVE;
backends/hostmem-shm.c +0 -5
index adf60957c3c..0a01c760551 100644
@@ -30,11 +30,6 @@ shm_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
     uint32_t ram_flags;
     int fd = cpr_find_fd(backend_name, 0);
 
-    if (!backend->size) {
-        error_setg(errp, "can't create shm backend with size 0");
-        return false;
-    }
-
     if (!backend->share) {
         error_setg(errp, "can't create shm backend with `share=off`");
         return false;
Signature · 1 line
2.55.0
Reviewed-by on v1 1/7
#2 lore raw

Re: [PATCH 1/7] backends/hostmem: Drop redundant validation of property "size"

Quoted 4 lines · Markus Armbruster

Property "size" is mandatory, and its setter host_memory_backend_set_size() rejects value zero. Delete the redundant checks for zero in the .alloc() callbacks.

Quoted 8 lines
---
 backends/hostmem-epc.c   | 5 -----
 backends/hostmem-file.c  | 4 ----
 backends/hostmem-memfd.c | 5 -----
 backends/hostmem-ram.c   | 5 -----
 backends/hostmem-shm.c   | 5 -----
 5 files changed, 24 deletions(-)

...it is a shame this was/is dead code, since the actual error message from host_memory_backend_set_size() is (IMHO) worse than that from the unreachable code you're deleting

  $ ./build/qemu-system-x86_64 -object memory-backend-ram,size=0,id=foo
  qemu-system-x86_64: property 'size' of memory-backend-ram doesn't take value '0'

as a separate patch I might suggest

    diff --git a/backends/hostmem.c b/backends/hostmem.c
    index 173d042be35..f523dce6eb0 100644
    --- a/backends/hostmem.c
    +++ b/backends/hostmem.c
    @@ -77,8 +77,8 @@ host_memory_backend_set_size(Object *obj, Visitor *v, const char *name,
         }
         if (!value) {
             error_setg(errp,
    -                   "property '%s' of %s doesn't take value '%" PRIu64 "'",
    -                   name, object_get_typename(obj), value);
    +                   "cannot create '%s' with a size of 0",
    +                   object_get_typename(obj));
             return;
         }
         backend->size = value;

so that we get the nicer message:

    $ ./build/qemu-system-x86_64 -object memory-backend-ram,size=0,id=foo
    qemu-system-x86_64: cannot create 'memory-backend-ram' with a size of 0
Quoted 83 lines
backends/hostmem-epc.c +0 -5
index ab20b182331..e1c784249ff 100644
@@ -24,11 +24,6 @@ sgx_epc_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
     uint32_t ram_flags;
     int fd;
 
-    if (!backend->size) {
-        error_setg(errp, "can't create backend with size 0");
-        return false;
-    }
-
     fd = qemu_open("/dev/sgx_vepc", O_RDWR, errp);
     if (fd < 0) {
         return false;
backends/hostmem-file.c +0 -4
index 6ad9cad0320..08d0382d76f 100644
@@ -48,10 +48,6 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
     g_autofree gchar *name = NULL;
     uint32_t ram_flags;
 
-    if (!backend->size) {
-        error_setg(errp, "can't create backend with size 0");
-        return false;
-    }
     if (!fb->mem_path) {
         error_setg(errp, "mem-path property not set");
         return false;
backends/hostmem-memfd.c +0 -5
index 46489212a23..11d456e3ebd 100644
@@ -49,11 +49,6 @@ memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
     int fd = cpr_find_fd(name, 0);
     uint32_t ram_flags;
 
-    if (!backend->size) {
-        error_setg(errp, "can't create backend with size 0");
-        return false;
-    }
-
     if (fd >= 0) {
         goto have_fd;
     }
backends/hostmem-ram.c +0 -5
index 6a507fad77c..0ae7b4ae759 100644
@@ -22,11 +22,6 @@ ram_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
     g_autofree char *name = NULL;
     uint32_t ram_flags;
 
-    if (!backend->size) {
-        error_setg(errp, "can't create backend with size 0");
-        return false;
-    }
-
     name = host_memory_backend_get_name(backend);
     ram_flags = backend->share ? RAM_SHARED : RAM_PRIVATE;
     ram_flags |= backend->reserve ? 0 : RAM_NORESERVE;
backends/hostmem-shm.c +0 -5
index adf60957c3c..0a01c760551 100644
@@ -30,11 +30,6 @@ shm_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
     uint32_t ram_flags;
     int fd = cpr_find_fd(backend_name, 0);
 
-    if (!backend->size) {
-        error_setg(errp, "can't create shm backend with size 0");
-        return false;
-    }
-
     if (!backend->share) {
         error_setg(errp, "can't create shm backend with `share=off`");
         return false;
Signature · 1 line
2.55.0

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#3 lore raw

Re: [PATCH 1/7] backends/hostmem: Drop redundant validation of property "size"

Quoted 14 lines · Daniel P. Berrangé
Quoted 4 lines · Markus Armbruster

Property "size" is mandatory, and its setter host_memory_backend_set_size() rejects value zero. Delete the redundant checks for zero in the .alloc() callbacks.

Quoted 8 lines
---
 backends/hostmem-epc.c   | 5 -----
 backends/hostmem-file.c  | 4 ----
 backends/hostmem-memfd.c | 5 -----
 backends/hostmem-ram.c   | 5 -----
 backends/hostmem-shm.c   | 5 -----
 5 files changed, 24 deletions(-)
Quoted 8 lines

...it is a shame this was/is dead code, since the actual error message from host_memory_backend_set_size() is (IMHO) worse than that from the unreachable code you're deleting

  $ ./build/qemu-system-x86_64 -object memory-backend-ram,size=0,id=foo
  qemu-system-x86_64: property 'size' of memory-backend-ram doesn't take value '0'

Fair point.

Quoted 7 lines

as a separate patch I might suggest

    diff --git a/backends/hostmem.c b/backends/hostmem.c
    index 173d042be35..f523dce6eb0 100644
    --- a/backends/hostmem.c
    +++ b/backends/hostmem.c
    @@ -77,8 +77,8 @@ host_memory_backend_set_size(Object *obj, Visitor *v, const char *name,
           if (host_memory_backend_mr_inited(backend)) {
               error_setg(errp, "cannot change property %s of %s ", name,
                          object_get_typename(obj));
               return;
           }

           if (!visit_type_size(v, name, &value, errp)) {
               return;
Quoted 11 lines
         }
         if (!value) {
             error_setg(errp,
    -                   "property '%s' of %s doesn't take value '%" PRIu64 "'",
    -                   name, object_get_typename(obj), value);
    +                   "cannot create '%s' with a size of 0",
    +                   object_get_typename(obj));
             return;
         }
         backend->size = value;
Quoted 4 lines

so that we get the nicer message:

    $ ./build/qemu-system-x86_64 -object memory-backend-ram,size=0,id=foo
    qemu-system-x86_64: cannot create 'memory-backend-ram' with a size of 0

Your message assumes that the property is only ever set on behalf of an object creation request. Not true in general, qom-set exists. I believe it's true here due to the host_memory_backend_mr_inited() guard. But that's not entirely obvious, isn't it?

Possible phrasings that don't assume this:

property 'size' of 'memory-backend-ram' cannot be zero cannot set property 'size' of 'memory-backend-ram' to zero

Is one of them good enough? I doubt this error is common.

There are enough "doesn't take value" error messages elsewhere to justify a small series of its own.

#4 lore raw

Re: [PATCH 1/7] backends/hostmem: Drop redundant validation of property "size"

Quoted 63 lines · Markus Armbruster
Quoted 14 lines · Daniel P. Berrangé
Quoted 4 lines · Markus Armbruster

Property "size" is mandatory, and its setter host_memory_backend_set_size() rejects value zero. Delete the redundant checks for zero in the .alloc() callbacks.

Quoted 8 lines
---
 backends/hostmem-epc.c   | 5 -----
 backends/hostmem-file.c  | 4 ----
 backends/hostmem-memfd.c | 5 -----
 backends/hostmem-ram.c   | 5 -----
 backends/hostmem-shm.c   | 5 -----
 5 files changed, 24 deletions(-)
Quoted 8 lines

...it is a shame this was/is dead code, since the actual error message from host_memory_backend_set_size() is (IMHO) worse than that from the unreachable code you're deleting

  $ ./build/qemu-system-x86_64 -object memory-backend-ram,size=0,id=foo
  qemu-system-x86_64: property 'size' of memory-backend-ram doesn't take value '0'

Fair point.

Quoted 7 lines

as a separate patch I might suggest

    diff --git a/backends/hostmem.c b/backends/hostmem.c
    index 173d042be35..f523dce6eb0 100644
    --- a/backends/hostmem.c
    +++ b/backends/hostmem.c
    @@ -77,8 +77,8 @@ host_memory_backend_set_size(Object *obj, Visitor *v, const char *name,
           if (host_memory_backend_mr_inited(backend)) {
               error_setg(errp, "cannot change property %s of %s ", name,
                          object_get_typename(obj));
               return;
           }

           if (!visit_type_size(v, name, &value, errp)) {
               return;
Quoted 11 lines
         }
         if (!value) {
             error_setg(errp,
    -                   "property '%s' of %s doesn't take value '%" PRIu64 "'",
    -                   name, object_get_typename(obj), value);
    +                   "cannot create '%s' with a size of 0",
    +                   object_get_typename(obj));
             return;
         }
         backend->size = value;
Quoted 4 lines

so that we get the nicer message:

    $ ./build/qemu-system-x86_64 -object memory-backend-ram,size=0,id=foo
    qemu-system-x86_64: cannot create 'memory-backend-ram' with a size of 0

Your message assumes that the property is only ever set on behalf of an object creation request. Not true in general, qom-set exists. I believe it's true here due to the host_memory_backend_mr_inited() guard. But that's not entirely obvious, isn't it?

Quoted 6 lines

Possible phrasings that don't assume this:

property 'size' of 'memory-backend-ram' cannot be zero cannot set property 'size' of 'memory-backend-ram' to zero

Is one of them good enough? I doubt this error is common.

I'm fine with either of those, perhaps mildly leaning towards the second.

Quoted 2 lines

There are enough "doesn't take value" error messages elsewhere to justify a small series of its own.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#5 lore raw

Re: [PATCH 1/7] backends/hostmem: Drop redundant validation of property "size"

Quoted 2 lines · Daniel P. Berrangé
Quoted 1 line · Markus Armbruster

Daniel P. Berrangé <[email protected]> writes:

[...]

Quoted 1 line
Quoted 1 line
Quoted 1 line

as a separate patch I might suggest

[...]

Quoted 12 lines
Quoted 12 lines
Quoted 4 lines

so that we get the nicer message:

    $ ./build/qemu-system-x86_64 -object memory-backend-ram,size=0,id=foo
    qemu-system-x86_64: cannot create 'memory-backend-ram' with a size of 0

Your message assumes that the property is only ever set on behalf of an object creation request. Not true in general, qom-set exists. I believe it's true here due to the host_memory_backend_mr_inited() guard. But that's not entirely obvious, isn't it?

Possible phrasings that don't assume this:

Quoted 10 lines
Quoted 4 lines

property 'size' of 'memory-backend-ram' cannot be zero cannot set property 'size' of 'memory-backend-ram' to zero

Is one of them good enough? I doubt this error is common.

I'm fine with either of those, perhaps mildly leaning towards the second.

Quoted 2 lines

There are enough "doesn't take value" error messages elsewhere to justify a small series of its own.

Just posted:

From: Markus Armbruster <[email protected]>
Subject: [PATCH] error: Improve a few error messages on setting property to zero
Date: Thu, 12 Nov 2026 12:47:02 +0100
Message-ID: <[email protected]>

Reviewed-by on v1 1/7
#6 lore raw

Re: [PATCH 1/7] backends/hostmem: Drop redundant validation of property "size"

In reply to Markus Armbruster
Quoted 82 lines · Markus Armbruster

Property "size" is mandatory, and its setter host_memory_backend_set_size() rejects value zero. Delete the redundant checks for zero in the .alloc() callbacks.

---
  backends/hostmem-epc.c   | 5 -----
  backends/hostmem-file.c  | 4 ----
  backends/hostmem-memfd.c | 5 -----
  backends/hostmem-ram.c   | 5 -----
  backends/hostmem-shm.c   | 5 -----
  5 files changed, 24 deletions(-)
backends/hostmem-epc.c +0 -5
index ab20b182331..e1c784249ff 100644
@@ -24,11 +24,6 @@ sgx_epc_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
      uint32_t ram_flags;
      int fd;
 
-    if (!backend->size) {
-        error_setg(errp, "can't create backend with size 0");
-        return false;
-    }
-
      fd = qemu_open("/dev/sgx_vepc", O_RDWR, errp);
      if (fd < 0) {
          return false;
backends/hostmem-file.c +0 -4
index 6ad9cad0320..08d0382d76f 100644
@@ -48,10 +48,6 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
      g_autofree gchar *name = NULL;
      uint32_t ram_flags;
 
-    if (!backend->size) {
-        error_setg(errp, "can't create backend with size 0");
-        return false;
-    }
      if (!fb->mem_path) {
          error_setg(errp, "mem-path property not set");
          return false;
backends/hostmem-memfd.c +0 -5
index 46489212a23..11d456e3ebd 100644
@@ -49,11 +49,6 @@ memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
      int fd = cpr_find_fd(name, 0);
      uint32_t ram_flags;
 
-    if (!backend->size) {
-        error_setg(errp, "can't create backend with size 0");
-        return false;
-    }
-
      if (fd >= 0) {
          goto have_fd;
      }
backends/hostmem-ram.c +0 -5
index 6a507fad77c..0ae7b4ae759 100644
@@ -22,11 +22,6 @@ ram_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
      g_autofree char *name = NULL;
      uint32_t ram_flags;
 
-    if (!backend->size) {
-        error_setg(errp, "can't create backend with size 0");
-        return false;
-    }
-
      name = host_memory_backend_get_name(backend);
      ram_flags = backend->share ? RAM_SHARED : RAM_PRIVATE;
      ram_flags |= backend->reserve ? 0 : RAM_NORESERVE;
backends/hostmem-shm.c +0 -0
index adf60957c3c..0a01c760551 100644
@@ -30,11 +30,6 @@ shm_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
      uint32_t ram_flags;

backends/hostmem-shm.c shm_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) · line 35

Quoted 10 lines
backends/hostmem-shm.c +0 -5
      int fd = cpr_find_fd(backend_name, 0);
 
-    if (!backend->size) {
-        error_setg(errp, "can't create shm backend with size 0");
-        return false;
-    }
-
      if (!backend->share) {
          error_setg(errp, "can't create shm backend with `share=off`");
          return false;

ATB,

Mark.

#7 lore raw

PATCH 2/7 backends/hostmem-file: Drop useless validation of property "mem-path"

In reply to Markus Armbruster

A non-optional string property like "mem-path" cannot be null. Drop the useless check.

---
 backends/hostmem-file.c | 5 -----
 1 file changed, 5 deletions(-)
backends/hostmem-file.c +0 -5
index 08d0382d76f..1e8f8ae6aa8 100644
@@ -48,11 +48,6 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
     g_autofree gchar *name = NULL;
     uint32_t ram_flags;
 
-    if (!fb->mem_path) {
-        error_setg(errp, "mem-path property not set");
-        return false;
-    }
-
     switch (fb->rom) {
     case ON_OFF_AUTO_AUTO:
         /* Traditionally, opening the file readonly always resulted in ROM. */
Signature · 1 line
2.55.0
Reviewed-by on v1 2/7
#8 lore raw

Re: [PATCH 2/7] backends/hostmem-file: Drop useless validation of property "mem-path"

Quoted 7 lines · Markus Armbruster

A non-optional string property like "mem-path" cannot be null. Drop the useless check.

---
 backends/hostmem-file.c | 5 -----
 1 file changed, 5 deletions(-)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
Reviewed-by on v1 2/7
#9 lore raw

Re: [PATCH 2/7] backends/hostmem-file: Drop useless validation of property "mem-path"

In reply to Markus Armbruster
Quoted 14 lines · Markus Armbruster

A non-optional string property like "mem-path" cannot be null. Drop the useless check.

---
  backends/hostmem-file.c | 5 -----
  1 file changed, 5 deletions(-)
backends/hostmem-file.c +0 -0
index 08d0382d76f..1e8f8ae6aa8 100644
@@ -48,11 +48,6 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
      g_autofree gchar *name = NULL;

backends/hostmem-file.c file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) · line 53

Quoted 10 lines
backends/hostmem-file.c +0 -5
      uint32_t ram_flags;
 
-    if (!fb->mem_path) {
-        error_setg(errp, "mem-path property not set");
-        return false;
-    }
-
      switch (fb->rom) {
      case ON_OFF_AUTO_AUTO:
          /* Traditionally, opening the file readonly always resulted in ROM. */

ATB,

Mark.

#10 lore raw

PATCH 3/7 backends/hostmem: Improve error reporting when method alloc() fails

In reply to Markus Armbruster

When HostMemoryBackend method alloc() fails, the error report isn't always clear. E.g. "-object memory-backend-shm,id=mem0,size=1E" fails for me like this:

qemu-system-x86_64: unable to map backing store for guest RAM: Cannot allocate memory

Leaves users guessing which part of the (often lengthy) command line needs fixing.

Let's prepend a bit of context:

qemu-system-x86_64: can't create memory-backend-shm: unable to map backing store for guest RAM: Cannot allocate memory

This makes an error message in shm_backend_memory_alloc() partly redundant:

qemu-system-x86_64: can't create memory-backend-shm: can't create shm backend with share=off

Change it to

qemu-system-x86_64: can't create memory-backend-shm: backend requires 'share=on'

---
 backends/hostmem-shm.c | 2 +-
 backends/hostmem.c     | 3 +++
 2 files changed, 4 insertions(+), 1 deletion(-)
backends/hostmem-shm.c +1 -1
index 0a01c760551..d1cba22cbeb 100644
@@ -31,7 +31,7 @@ shm_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
     int fd = cpr_find_fd(backend_name, 0);
 
     if (!backend->share) {
-        error_setg(errp, "can't create shm backend with `share=off`");
+        error_setg(errp, "backend requires 'share=on'");
         return false;
     }
 
backends/hostmem.c +3 -0
index 173d042be35..1c2a5d42fc5 100644
@@ -335,6 +335,7 @@ size_t host_memory_backend_pagesize(HostMemoryBackend *memdev)
 static void
 host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)
 {
+    ERRP_GUARD();
     HostMemoryBackend *backend = MEMORY_BACKEND(uc);
     HostMemoryBackendClass *bc = MEMORY_BACKEND_GET_CLASS(uc);
     void *ptr;
@@ -346,6 +347,8 @@ host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)
         return;
     }
     if (!bc->alloc(backend, errp)) {
+        error_prepend(errp, "can't create '%s': ",
+                      object_get_typename(OBJECT(uc)));
         return;
     }
 
Signature · 1 line
2.55.0
Reviewed-by on v1 3/7
#11 lore raw

Re: [PATCH 3/7] backends/hostmem: Improve error reporting when method alloc() fails

Quoted 20 lines · Markus Armbruster

When HostMemoryBackend method alloc() fails, the error report isn't always clear. E.g. "-object memory-backend-shm,id=mem0,size=1E" fails for me like this:

qemu-system-x86_64: unable to map backing store for guest RAM: Cannot allocate memory

Leaves users guessing which part of the (often lengthy) command line needs fixing.

Let's prepend a bit of context:

qemu-system-x86_64: can't create memory-backend-shm: unable to map backing store for guest RAM: Cannot allocate memory

This makes an error message in shm_backend_memory_alloc() partly redundant:

qemu-system-x86_64: can't create memory-backend-shm: can't create shm backend with share=off

Change it to

Quoted 7 lines

qemu-system-x86_64: can't create memory-backend-shm: backend requires 'share=on'

---
 backends/hostmem-shm.c | 2 +-
 backends/hostmem.c     | 3 +++
 2 files changed, 4 insertions(+), 1 deletion(-)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
Reviewed-by on v1 3/7
#12 lore raw

Re: [PATCH 3/7] backends/hostmem: Improve error reporting when method alloc() fails

In reply to Markus Armbruster
Quoted 54 lines · Markus Armbruster

When HostMemoryBackend method alloc() fails, the error report isn't always clear. E.g. "-object memory-backend-shm,id=mem0,size=1E" fails for me like this:

qemu-system-x86_64: unable to map backing store for guest RAM: Cannot allocate memory

Leaves users guessing which part of the (often lengthy) command line needs fixing.

Let's prepend a bit of context:

qemu-system-x86_64: can't create memory-backend-shm: unable to map backing store for guest RAM: Cannot allocate memory

This makes an error message in shm_backend_memory_alloc() partly redundant:

qemu-system-x86_64: can't create memory-backend-shm: can't create shm backend with share=off

Change it to

qemu-system-x86_64: can't create memory-backend-shm: backend requires 'share=on'

---
  backends/hostmem-shm.c | 2 +-
  backends/hostmem.c     | 3 +++
  2 files changed, 4 insertions(+), 1 deletion(-)
backends/hostmem-shm.c +1 -1
index 0a01c760551..d1cba22cbeb 100644
@@ -31,7 +31,7 @@ shm_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
      int fd = cpr_find_fd(backend_name, 0);
 
      if (!backend->share) {
-        error_setg(errp, "can't create shm backend with `share=off`");
+        error_setg(errp, "backend requires 'share=on'");
          return false;
      }
 
backends/hostmem.c +1 -0
index 173d042be35..1c2a5d42fc5 100644
@@ -335,6 +335,7 @@ size_t host_memory_backend_pagesize(HostMemoryBackend *memdev)
  static void
  host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)
  {
+    ERRP_GUARD();
      HostMemoryBackend *backend = MEMORY_BACKEND(uc);
      HostMemoryBackendClass *bc = MEMORY_BACKEND_GET_CLASS(uc);
      void *ptr;

backends/hostmem.c host_memory_backend_memory_complete(UserCreatable *uc, Error **errp) · line 353

Quoted 7 lines
backends/hostmem.c +2 -0
@@ -346,6 +347,8 @@ host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)
          return;
      }
      if (!bc->alloc(backend, errp)) {
+        error_prepend(errp, "can't create '%s': ",
+                      object_get_typename(OBJECT(uc)));
          return;
      } 

ATB,

Mark.

#13 lore raw

PATCH 4/7 backends/hostmem-epc: Register QOM type unconditionally

In reply to Markus Armbruster

The example from docs/system/i386/sgx.rst section "Launching a guest" fails for me like this:

    $ qemu-system-x86_64 -cpu host,+sgx-provisionkey -object memory-backend-epc,id=mem1,size=64M,prealloc=on -M sgx-epc.0.memdev=mem1,sgx-epc.0.node=0
    qemu-system-x86_64: invalid object type: memory-backend-epc

Device "sgx-epc" has a link property to a "memory-backend-epc" (SgxEPC member memdev). It's not optional, so we must create a "memory-backend-epc" to be able to create an "sgx-epc". In the test shown above, "sgx-epc" exists, but "memory-backend-epc" does not.

The latter doesn't exist for me, because we register QOM type "memory-backend-epc" only when we can open /dev/sgx_vepc read/write, which fails for me. Such conditional registration is unusual and problematic:

1. Management applications use introspection interfaces to find out what a certain QEMU can do. Conditional type registration makes QOM introspection results depend on the condition, which generally makes them valid only for the process that produced them. Undesirable.

2. For user-creatable objects like "memory-backend-epc", it also makes QAPI introspection unreliable: query-qmp-schema claims the type exists (object-add argument @qom-type accepts "memory-backend-epc") even when it doesn't.

Conditional registration actually serves no purpose here, because we still have to open /dev/sgx_vepc for real when we create the object, and need and do handle errors then.

Register unconditionally. The example now fails to create the object, like this:

qemu-system-x86_64: can't create 'memory-backend-epc': Could not open '/dev/sgx_vepc': No such file or directory

---
 backends/hostmem-epc.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
backends/hostmem-epc.c +1 -6
index e1c784249ff..bf5aecdfd45 100644
@@ -62,12 +62,7 @@ static const TypeInfo sgx_epc_backed_info = {
 
 static void register_types(void)
 {
-    int fd = qemu_open_old("/dev/sgx_vepc", O_RDWR);
-    if (fd >= 0) {
-        close(fd);
-
-        type_register_static(&sgx_epc_backed_info);
-    }
+    type_register_static(&sgx_epc_backed_info);
 }
 
 type_init(register_types);
Signature · 1 line
2.55.0
Reviewed-by on v1 4/7
#14 lore raw

Re: [PATCH 4/7] backends/hostmem-epc: Register QOM type unconditionally

Quoted 26 lines · Markus Armbruster

The example from docs/system/i386/sgx.rst section "Launching a guest" fails for me like this:

    $ qemu-system-x86_64 -cpu host,+sgx-provisionkey -object memory-backend-epc,id=mem1,size=64M,prealloc=on -M sgx-epc.0.memdev=mem1,sgx-epc.0.node=0
    qemu-system-x86_64: invalid object type: memory-backend-epc

Device "sgx-epc" has a link property to a "memory-backend-epc" (SgxEPC member memdev). It's not optional, so we must create a "memory-backend-epc" to be able to create an "sgx-epc". In the test shown above, "sgx-epc" exists, but "memory-backend-epc" does not.

The latter doesn't exist for me, because we register QOM type "memory-backend-epc" only when we can open /dev/sgx_vepc read/write, which fails for me. Such conditional registration is unusual and problematic:

1. Management applications use introspection interfaces to find out what a certain QEMU can do. Conditional type registration makes QOM introspection results depend on the condition, which generally makes them valid only for the process that produced them. Undesirable.

2. For user-creatable objects like "memory-backend-epc", it also makes QAPI introspection unreliable: query-qmp-schema claims the type exists (object-add argument @qom-type accepts "memory-backend-epc") even when it doesn't.

Quoted 8 lines

Conditional registration actually serves no purpose here, because we still have to open /dev/sgx_vepc for real when we create the object, and need and do handle errors then.

Register unconditionally. The example now fails to create the object, like this:

qemu-system-x86_64: can't create 'memory-backend-epc': Could not open '/dev/sgx_vepc': No such file or directory

Bonus points - this error message is orders of magnitude more useful to users than

qemu-system-x86_64: invalid object type: memory-backend-epc

which makes them think their QEMU is built wrong or outdated.

Quoted 5 lines
---
 backends/hostmem-epc.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
Quoted 22 lines
backends/hostmem-epc.c +1 -6
index e1c784249ff..bf5aecdfd45 100644
@@ -62,12 +62,7 @@ static const TypeInfo sgx_epc_backed_info = {
 
 static void register_types(void)
 {
-    int fd = qemu_open_old("/dev/sgx_vepc", O_RDWR);
-    if (fd >= 0) {
-        close(fd);
-
-        type_register_static(&sgx_epc_backed_info);
-    }
+    type_register_static(&sgx_epc_backed_info);
 }
 
 type_init(register_types);
Signature · 1 line
2.55.0

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
Reviewed-by on v1 4/7
#15 lore raw

Re: [PATCH 4/7] backends/hostmem-epc: Register QOM type unconditionally

In reply to Markus Armbruster
Quoted 48 lines · Markus Armbruster

The example from docs/system/i386/sgx.rst section "Launching a guest" fails for me like this:

     $ qemu-system-x86_64 -cpu host,+sgx-provisionkey -object memory-backend-epc,id=mem1,size=64M,prealloc=on -M sgx-epc.0.memdev=mem1,sgx-epc.0.node=0
     qemu-system-x86_64: invalid object type: memory-backend-epc

Device "sgx-epc" has a link property to a "memory-backend-epc" (SgxEPC member memdev). It's not optional, so we must create a "memory-backend-epc" to be able to create an "sgx-epc". In the test shown above, "sgx-epc" exists, but "memory-backend-epc" does not.

The latter doesn't exist for me, because we register QOM type "memory-backend-epc" only when we can open /dev/sgx_vepc read/write, which fails for me. Such conditional registration is unusual and problematic:

1. Management applications use introspection interfaces to find out what a certain QEMU can do. Conditional type registration makes QOM introspection results depend on the condition, which generally makes them valid only for the process that produced them. Undesirable.

2. For user-creatable objects like "memory-backend-epc", it also makes QAPI introspection unreliable: query-qmp-schema claims the type exists (object-add argument @qom-type accepts "memory-backend-epc") even when it doesn't.

Conditional registration actually serves no purpose here, because we still have to open /dev/sgx_vepc for real when we create the object, and need and do handle errors then.

Register unconditionally. The example now fails to create the object, like this:

qemu-system-x86_64: can't create 'memory-backend-epc': Could not open '/dev/sgx_vepc': No such file or directory

---
  backends/hostmem-epc.c | 7 +------
  1 file changed, 1 insertion(+), 6 deletions(-)
backends/hostmem-epc.c +0 -0
index e1c784249ff..bf5aecdfd45 100644
@@ -62,12 +62,7 @@ static const TypeInfo sgx_epc_backed_info = {
 
  static void register_types(void)
  {

backends/hostmem-epc.c static const TypeInfo sgx_epc_backed_info = { · line 68

Quoted 10 lines
backends/hostmem-epc.c +1 -6
-    int fd = qemu_open_old("/dev/sgx_vepc", O_RDWR);
-    if (fd >= 0) {
-        close(fd);
-
-        type_register_static(&sgx_epc_backed_info);
-    }
+    type_register_static(&sgx_epc_backed_info);
  }
 
  type_init(register_types);

ATB,

Mark.

#16 lore raw

PATCH 5/7 backends/hostmem-memfd: Register QOM type unconditionally

In reply to Markus Armbruster

We register QOM type "memory-backend-memfd" only when a test of memfd_create() succeeds. Such conditional registration is unusual and problematic:

1. Management applications use introspection interfaces to find out what a certain QEMU can do. Conditional type registration makes QOM introspection results depend on the condition, which generally makes them valid only for the process that produced them. Undesirable.

2. For user-creatable objects like "memory-backend-memfd", it also makes QAPI introspection unreliable: query-qmp-schema claims the type exists (object-add argument @qom-type accepts "memory-backend-memfd") even when it doesn't.

Conditional registration actually serves no purpose here, because we still have to memfd_create() for real when we create the object, and need and do handle errors then.

Register the type unconditionally.

To test, I hacked the qemu_memfd() call to fail. The error for -object memory-backend-memfd,id=mem0,size=1m changes from

qemu-system-x86_64: invalid object type: memory-backend-memfd

to

    qemu-system-x86_64: can't create 'memory-backend-memfd': failed to create memfd with flags 0x3: Invalid argument
---
 backends/hostmem-memfd.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
backends/hostmem-memfd.c +1 -3
index 11d456e3ebd..54f84ae331a 100644
@@ -223,9 +223,7 @@ static const TypeInfo memfd_backend_info = {
 
 static void register_types(void)
 {
-    if (qemu_memfd_check(MFD_ALLOW_SEALING)) {
-        type_register_static(&memfd_backend_info);
-    }
+    type_register_static(&memfd_backend_info);
 }
 
 type_init(register_types);
Signature · 1 line
2.55.0
Reviewed-by on v1 5/7
#17 lore raw

Re: [PATCH 5/7] backends/hostmem-memfd: Register QOM type unconditionally

Quoted 25 lines · Markus Armbruster

We register QOM type "memory-backend-memfd" only when a test of memfd_create() succeeds. Such conditional registration is unusual and problematic:

1. Management applications use introspection interfaces to find out what a certain QEMU can do. Conditional type registration makes QOM introspection results depend on the condition, which generally makes them valid only for the process that produced them. Undesirable.

2. For user-creatable objects like "memory-backend-memfd", it also makes QAPI introspection unreliable: query-qmp-schema claims the type exists (object-add argument @qom-type accepts "memory-backend-memfd") even when it doesn't.

Conditional registration actually serves no purpose here, because we still have to memfd_create() for real when we create the object, and need and do handle errors then.

Register the type unconditionally.

To test, I hacked the qemu_memfd() call to fail. The error for -object memory-backend-memfd,id=mem0,size=1m changes from

qemu-system-x86_64: invalid object type: memory-backend-memfd

Quoted 8 lines

to

    qemu-system-x86_64: can't create 'memory-backend-memfd': failed to create memfd with flags 0x3: Invalid argument
---
 backends/hostmem-memfd.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
Reviewed-by on v1 5/7
#18 lore raw

Re: [PATCH 5/7] backends/hostmem-memfd: Register QOM type unconditionally

In reply to Markus Armbruster
Quoted 39 lines · Markus Armbruster

We register QOM type "memory-backend-memfd" only when a test of memfd_create() succeeds. Such conditional registration is unusual and problematic:

1. Management applications use introspection interfaces to find out what a certain QEMU can do. Conditional type registration makes QOM introspection results depend on the condition, which generally makes them valid only for the process that produced them. Undesirable.

2. For user-creatable objects like "memory-backend-memfd", it also makes QAPI introspection unreliable: query-qmp-schema claims the type exists (object-add argument @qom-type accepts "memory-backend-memfd") even when it doesn't.

Conditional registration actually serves no purpose here, because we still have to memfd_create() for real when we create the object, and need and do handle errors then.

Register the type unconditionally.

To test, I hacked the qemu_memfd() call to fail. The error for -object memory-backend-memfd,id=mem0,size=1m changes from

qemu-system-x86_64: invalid object type: memory-backend-memfd

to

     qemu-system-x86_64: can't create 'memory-backend-memfd': failed to create memfd with flags 0x3: Invalid argument
---
  backends/hostmem-memfd.c | 4 +---
  1 file changed, 1 insertion(+), 3 deletions(-)
backends/hostmem-memfd.c +0 -0
index 11d456e3ebd..54f84ae331a 100644

backends/hostmem-memfd.c static const TypeInfo memfd_backend_info = { · line 229

Quoted 10 lines
backends/hostmem-memfd.c +1 -3
@@ -223,9 +223,7 @@ static const TypeInfo memfd_backend_info = {
 
  static void register_types(void)
  {
-    if (qemu_memfd_check(MFD_ALLOW_SEALING)) {
-        type_register_static(&memfd_backend_info);
-    }
+    type_register_static(&memfd_backend_info);
  }
 
  type_init(register_types);

ATB,

Mark.

#19 lore raw

PATCH 6/7 util/memfd: Improve error message when memfd_create() fails

In reply to Markus Armbruster

qemu_memfd_create() reports something like

    failed to create memfd with flags 0x3: Invalid argument

when memfd_create() fails. To make sense of this, you have to understand what flags are, and how to decode them.

qemu_memfd_create() computes the flags from @seals and @hugetlb, so show these instead, like this:

failed to create memfd with sealing and without huge pages: Invalid argument

---
 util/memfd.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
util/memfd.c +4 -1
index 07beab174dc..a398b246600 100644
@@ -73,7 +73,10 @@ int qemu_memfd_create(const char *name, size_t size, bool hugetlb,
     mfd = memfd_create(name, flags);
     if (mfd < 0) {
         error_setg_errno(errp, errno,
-                         "failed to create memfd with flags 0x%x", flags);
+                         "failed to create memfd %s sealing"
+                         " and %s huge pages",
+                         seals ? "with" : "without",
+                         hugetlb ? "with" : "without");
         goto err;
     }
 
Signature · 1 line
2.55.0
Reviewed-by on v1 6/7
#20 lore raw

Re: [PATCH 6/7] util/memfd: Improve error message when memfd_create() fails

Quoted 10 lines · Markus Armbruster

qemu_memfd_create() reports something like

    failed to create memfd with flags 0x3: Invalid argument

when memfd_create() fails. To make sense of this, you have to understand what flags are, and how to decode them.

qemu_memfd_create() computes the flags from @seals and @hugetlb, so show these instead, like this:

Quoted 6 lines

failed to create memfd with sealing and without huge pages: Invalid argument

---
 util/memfd.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
Reviewed-by on v1 6/7
#21 lore raw

Re: [PATCH 6/7] util/memfd: Improve error message when memfd_create() fails

In reply to Markus Armbruster
Quoted 22 lines · Markus Armbruster

qemu_memfd_create() reports something like

     failed to create memfd with flags 0x3: Invalid argument

when memfd_create() fails. To make sense of this, you have to understand what flags are, and how to decode them.

qemu_memfd_create() computes the flags from @seals and @hugetlb, so show these instead, like this:

failed to create memfd with sealing and without huge pages: Invalid argument

---
  util/memfd.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
util/memfd.c +0 -0
index 07beab174dc..a398b246600 100644

util/memfd.c int qemu_memfd_create(const char *name, size_t size, bool hugetlb, · line 81

Quoted 10 lines
util/memfd.c +4 -1
@@ -73,7 +73,10 @@ int qemu_memfd_create(const char *name, size_t size, bool hugetlb,
      mfd = memfd_create(name, flags);
      if (mfd < 0) {
          error_setg_errno(errp, errno,
-                         "failed to create memfd with flags 0x%x", flags);
+                         "failed to create memfd %s sealing"
+                         " and %s huge pages",
+                         seals ? "with" : "without",
+                         hugetlb ? "with" : "without");
          goto err;
      }

ATB,

Mark.

#22 lore raw

PATCH 7/7 backends/hostmem-memfd: Add huge TLB properties unconditionally

In reply to Markus Armbruster

We add class properties "hugetlb" and "hugetlbsize" only when a test of memfd_create() succeeds. Such conditional registration is unusual and can be problematic:

1. Management applications use introspection interfaces to find out what a certain QEMU can do. Conditional property addition makes QOM introspection results depend on the condition, which generally makes them valid only for the process that produced them. Undesirable.

2. For user-creatable objects like "memory-backend-memfd", it also makes QAPI introspection unreliable: query-qmp-schema claims the properties exist (object-add takes arguments @hugetlb and @hugetlbsize when argument @qom-type is "memory-backend-memfd") even when it doesn't.

Conditional registration actually serves no purpose here, because we still have to memfd_create() for real when we create the object, and need and do handle errors then.

Moreover, we actually KVM ioctl KVM_CREATE_GUEST_MEMFD instead of memfd_create() there when @guest-memfd is on. If the memfd_create() check can fail when the ioctl could still succeed, the condition is counter-productive.

Add the properties unconditionally.

To test, I hacked the qemu_memfd() call to fail. The error for -object memory-backend-memfd,id=mem0,size=1m,hugetlb=off,hugetlbsize=512,seal=on changes from

qemu-system-x86_64: Property 'memory-backend-memfd.hugetlbsize' not found

to

qemu-system-x86_64: can't create 'memory-backend-memfd': failed to create memfd with sealing and without huge pages: Invalid argument

---
 backends/hostmem-memfd.c | 26 ++++++++++++--------------
 1 file changed, 12 insertions(+), 14 deletions(-)
backends/hostmem-memfd.c +12 -14
index 54f84ae331a..b945fc46021 100644
@@ -184,20 +184,18 @@ memfd_backend_class_init(ObjectClass *oc, const void *data)
 
     bc->alloc = memfd_backend_memory_alloc;
 
-    if (qemu_memfd_check(MFD_HUGETLB)) {
-        object_class_property_add_bool(oc, "hugetlb",
-                                       memfd_backend_get_hugetlb,
-                                       memfd_backend_set_hugetlb);
-        object_class_property_set_description(oc, "hugetlb",
-                                              "Use huge pages");
-        object_class_property_add(oc, "hugetlbsize", "uint64",
-                                  &uint64_type_info,
-                                  memfd_backend_get_hugetlbsize,
-                                  memfd_backend_set_hugetlbsize,
-                                  NULL, NULL);
-        object_class_property_set_description(oc, "hugetlbsize",
-                                              "Huge pages size (ex: 2M, 1G)");
-    }
+    object_class_property_add_bool(oc, "hugetlb",
+                                   memfd_backend_get_hugetlb,
+                                   memfd_backend_set_hugetlb);
+    object_class_property_set_description(oc, "hugetlb",
+                                          "Use huge pages");
+    object_class_property_add(oc, "hugetlbsize", "uint64",
+                              &uint64_type_info,
+                              memfd_backend_get_hugetlbsize,
+                              memfd_backend_set_hugetlbsize,
+                              NULL, NULL);
+    object_class_property_set_description(oc, "hugetlbsize",
+                                          "Huge pages size (ex: 2M, 1G)");
 
     object_class_property_add_enum(oc, "guest-memfd", "OnOffAuto",
                                    &OnOffAuto_type_info,
Signature · 1 line
2.55.0
Reviewed-by on v1 7/7
#23 lore raw

Re: [PATCH 7/7] backends/hostmem-memfd: Add huge TLB properties unconditionally

Quoted 32 lines · Markus Armbruster

We add class properties "hugetlb" and "hugetlbsize" only when a test of memfd_create() succeeds. Such conditional registration is unusual and can be problematic:

1. Management applications use introspection interfaces to find out what a certain QEMU can do. Conditional property addition makes QOM introspection results depend on the condition, which generally makes them valid only for the process that produced them. Undesirable.

2. For user-creatable objects like "memory-backend-memfd", it also makes QAPI introspection unreliable: query-qmp-schema claims the properties exist (object-add takes arguments @hugetlb and @hugetlbsize when argument @qom-type is "memory-backend-memfd") even when it doesn't.

Conditional registration actually serves no purpose here, because we still have to memfd_create() for real when we create the object, and need and do handle errors then.

Moreover, we actually KVM ioctl KVM_CREATE_GUEST_MEMFD instead of memfd_create() there when @guest-memfd is on. If the memfd_create() check can fail when the ioctl could still succeed, the condition is counter-productive.

Add the properties unconditionally.

To test, I hacked the qemu_memfd() call to fail. The error for -object memory-backend-memfd,id=mem0,size=1m,hugetlb=off,hugetlbsize=512,seal=on changes from

qemu-system-x86_64: Property 'memory-backend-memfd.hugetlbsize' not found

Quoted 8 lines

to

qemu-system-x86_64: can't create 'memory-backend-memfd': failed to create memfd with sealing and without huge pages: Invalid argument

---
 backends/hostmem-memfd.c | 26 ++++++++++++--------------
 1 file changed, 12 insertions(+), 14 deletions(-)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
Reviewed-by on v1 7/7
#24 lore raw

Re: [PATCH 7/7] backends/hostmem-memfd: Add huge TLB properties unconditionally

In reply to Markus Armbruster
Quoted 68 lines · Markus Armbruster

We add class properties "hugetlb" and "hugetlbsize" only when a test of memfd_create() succeeds. Such conditional registration is unusual and can be problematic:

1. Management applications use introspection interfaces to find out what a certain QEMU can do. Conditional property addition makes QOM introspection results depend on the condition, which generally makes them valid only for the process that produced them. Undesirable.

2. For user-creatable objects like "memory-backend-memfd", it also makes QAPI introspection unreliable: query-qmp-schema claims the properties exist (object-add takes arguments @hugetlb and @hugetlbsize when argument @qom-type is "memory-backend-memfd") even when it doesn't.

Conditional registration actually serves no purpose here, because we still have to memfd_create() for real when we create the object, and need and do handle errors then.

Moreover, we actually KVM ioctl KVM_CREATE_GUEST_MEMFD instead of memfd_create() there when @guest-memfd is on. If the memfd_create() check can fail when the ioctl could still succeed, the condition is counter-productive.

Add the properties unconditionally.

To test, I hacked the qemu_memfd() call to fail. The error for -object memory-backend-memfd,id=mem0,size=1m,hugetlb=off,hugetlbsize=512,seal=on changes from

qemu-system-x86_64: Property 'memory-backend-memfd.hugetlbsize' not found

to

qemu-system-x86_64: can't create 'memory-backend-memfd': failed to create memfd with sealing and without huge pages: Invalid argument

---
  backends/hostmem-memfd.c | 26 ++++++++++++--------------
  1 file changed, 12 insertions(+), 14 deletions(-)
backends/hostmem-memfd.c +5 -14
index 54f84ae331a..b945fc46021 100644
@@ -184,20 +184,18 @@ memfd_backend_class_init(ObjectClass *oc, const void *data)
 
      bc->alloc = memfd_backend_memory_alloc;
 
-    if (qemu_memfd_check(MFD_HUGETLB)) {
-        object_class_property_add_bool(oc, "hugetlb",
-                                       memfd_backend_get_hugetlb,
-                                       memfd_backend_set_hugetlb);
-        object_class_property_set_description(oc, "hugetlb",
-                                              "Use huge pages");
-        object_class_property_add(oc, "hugetlbsize", "uint64",
-                                  &uint64_type_info,
-                                  memfd_backend_get_hugetlbsize,
-                                  memfd_backend_set_hugetlbsize,
-                                  NULL, NULL);
-        object_class_property_set_description(oc, "hugetlbsize",
-                                              "Huge pages size (ex: 2M, 1G)");
-    }
+    object_class_property_add_bool(oc, "hugetlb",
+                                   memfd_backend_get_hugetlb,
+                                   memfd_backend_set_hugetlb);
+    object_class_property_set_description(oc, "hugetlb",
+                                          "Use huge pages");

backends/hostmem-memfd.c memfd_backend_class_init(ObjectClass *oc, const void *data) · line 201

Quoted 10 lines
backends/hostmem-memfd.c +7 -0
+    object_class_property_add(oc, "hugetlbsize", "uint64",
+                              &uint64_type_info,
+                              memfd_backend_get_hugetlbsize,
+                              memfd_backend_set_hugetlbsize,
+                              NULL, NULL);
+    object_class_property_set_description(oc, "hugetlbsize",
+                                          "Huge pages size (ex: 2M, 1G)");
 
      object_class_property_add_enum(oc, "guest-memfd", "OnOffAuto",
                                     &OnOffAuto_type_info,

ATB,

Mark.

#25 lore raw

Use of qemu_memfd_check() in system/physmem.c (was: [PATCH 0/7] backends/hostmem: Make QOM types and properties unconditional)

In reply to Markus Armbruster

After this series, there is just one use of qemu_memfd_check() outside tests/ left:

    /*
     * Create MAP_SHARED RAMBlocks by mmap'ing a file descriptor, so it can be
     * shared with another process if CPR is being used.  Use memfd if available
     * because it has no size limits, else use POSIX shm.
     */
    static int qemu_ram_get_shared_fd(const char *name, bool *reused, Error **errp)
    {
        int fd = cpr_find_fd(name, 0);

        if (fd >= 0) {
            *reused = true;
            return fd;
        }
--->    if (qemu_memfd_check(0)) {
            fd = qemu_memfd_create(name, 0, 0, 0, 0, errp);
        } else {
            fd = qemu_shm_alloc(0, errp);
        }

        if (fd >= 0 && !cpr_save_fd(name, 0, fd, errp)) {
            close(fd);
            fd = -1;
        }
        *reused = false;
        return fd;
    }

We test whether memfd_create("test", MFD_CLOEXEC) succeeds to decide whether to use memfd_create(name, MFD_CLOEXEC) or POSIX shared memory. This is anti-pattern "predict which method should succed, then try that method". A better way is "try methods one after the other until one succeeds".

Doing that here would require refactoring, because both qemu_memfd_create() and qemu_shm_alloc() do additional work that can also fail: ftruncate().

Doubt: qemu_memfd_create(0) caches its result[*]. Is that a mere optimization, or does something depend on "we use the same method always"?

I don't plan to mess with this myself, I'm merely reporting something I found.

[*] Only when the argument is 0.