Guest memfd has very special semantics, which by default doesn't have a path at all, meanwhile it won't proactively allocate anonymous memory. Currently: - memory-backend-file: it is about creating a memory object based on a path in the file system. It doesn't apply to gmemfd. - memory-backend-ram: it is about (mostly) trying to allocate anonymous memories from the system (private or shared). It also doesn't apply to gmemfd. Forbid the two types of memory backends to gmemfd, but only allow memory-backend-memfd for it as of now. Signed-off-by: Peter Xu <peterx@xxxxxxxxxx> --- backends/hostmem-file.c | 8 +++++++- backends/hostmem-ram.c | 7 ++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/backends/hostmem-file.c b/backends/hostmem-file.c index 46321fda84..c94cf8441b 100644 --- a/backends/hostmem-file.c +++ b/backends/hostmem-file.c @@ -52,11 +52,18 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) 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; } + if (backend->guest_memfd) { + error_setg(errp, "File backends do not support guest memfd. " + "Please use memfd backend"); + return false; + } + switch (fb->rom) { case ON_OFF_AUTO_AUTO: /* Traditionally, opening the file readonly always resulted in ROM. */ @@ -86,7 +93,6 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) ram_flags |= fb->readonly ? RAM_READONLY_FD : 0; ram_flags |= fb->rom == ON_OFF_AUTO_ON ? RAM_READONLY : 0; ram_flags |= backend->reserve ? 0 : RAM_NORESERVE; - ram_flags |= backend->guest_memfd ? RAM_GUEST_MEMFD : 0; ram_flags |= fb->is_pmem ? RAM_PMEM : 0; ram_flags |= RAM_NAMED_FILE; return memory_region_init_ram_from_file(&backend->mr, OBJECT(backend), name, diff --git a/backends/hostmem-ram.c b/backends/hostmem-ram.c index 39aac6bf35..8125be217c 100644 --- a/backends/hostmem-ram.c +++ b/backends/hostmem-ram.c @@ -27,10 +27,15 @@ ram_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) return false; } + if (backend->guest_memfd) { + error_setg(errp, "File backends do not support guest memfd. " + "Please use memfd backend"); + return false; + } + name = host_memory_backend_get_name(backend); ram_flags = backend->share ? RAM_SHARED : 0; ram_flags |= backend->reserve ? 0 : RAM_NORESERVE; - ram_flags |= backend->guest_memfd ? RAM_GUEST_MEMFD : 0; return memory_region_init_ram_flags_nomigrate(&backend->mr, OBJECT(backend), name, backend->size, ram_flags, errp); -- 2.47.0 ===8<=== Thanks, -- Peter Xu