[PATCH 0/7] backends/hostmem: Make QOM types and properties unconditional

Markus Armbruster posted 7 patches 1 week, 4 days ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261111122342.634370-1-armbru@redhat.com
Maintainers: David Hildenbrand <david@kernel.org>, Igor Mammedov <imammedo@redhat.com>
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(-)
[PATCH 0/7] backends/hostmem: Make QOM types and properties unconditional
Posted by Markus Armbruster 1 week, 4 days ago
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: <87zew5j3iv.fsf@pond.sub.org>

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(-)

-- 
2.55.0
Use of qemu_memfd_check() in system/physmem.c (was: [PATCH 0/7] backends/hostmem: Make QOM types and properties unconditional)
Posted by Markus Armbruster 1 week, 4 days ago
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.