backends: Have HostMemoryBackendClass::alloc() handler return a boolean

Following the example documented since commit e3fe3988d7 ("error:
Document Error API usage rules"), have HostMemoryBackendClass::alloc
return a boolean indicating whether an error is set or not.

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
Reviewed-by: Gavin Shan <gshan@redhat.com>
Message-Id: <20231120213301.24349-17-philmd@linaro.org>
This commit is contained in:
Philippe Mathieu-Daudé 2023-11-20 13:50:52 +01:00
parent e199f7ad4d
commit fdb63cf3b5
6 changed files with 36 additions and 28 deletions

View File

@ -17,7 +17,7 @@
#include "sysemu/hostmem.h" #include "sysemu/hostmem.h"
#include "hw/i386/hostmem-epc.h" #include "hw/i386/hostmem-epc.h"
static void static bool
sgx_epc_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) sgx_epc_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
{ {
g_autofree char *name = NULL; g_autofree char *name = NULL;
@ -26,21 +26,20 @@ sgx_epc_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
if (!backend->size) { if (!backend->size) {
error_setg(errp, "can't create backend with size 0"); error_setg(errp, "can't create backend with size 0");
return; return false;
} }
fd = qemu_open_old("/dev/sgx_vepc", O_RDWR); fd = qemu_open_old("/dev/sgx_vepc", O_RDWR);
if (fd < 0) { if (fd < 0) {
error_setg_errno(errp, errno, error_setg_errno(errp, errno,
"failed to open /dev/sgx_vepc to alloc SGX EPC"); "failed to open /dev/sgx_vepc to alloc SGX EPC");
return; return false;
} }
name = object_get_canonical_path(OBJECT(backend)); name = object_get_canonical_path(OBJECT(backend));
ram_flags = (backend->share ? RAM_SHARED : 0) | RAM_PROTECTED; ram_flags = (backend->share ? RAM_SHARED : 0) | RAM_PROTECTED;
memory_region_init_ram_from_fd(&backend->mr, OBJECT(backend), return memory_region_init_ram_from_fd(&backend->mr, OBJECT(backend), name,
name, backend->size, ram_flags, backend->size, ram_flags, fd, 0, errp);
fd, 0, errp);
} }
static void sgx_epc_backend_instance_init(Object *obj) static void sgx_epc_backend_instance_init(Object *obj)

View File

@ -36,12 +36,13 @@ struct HostMemoryBackendFile {
OnOffAuto rom; OnOffAuto rom;
}; };
static void static bool
file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
{ {
#ifndef CONFIG_POSIX #ifndef CONFIG_POSIX
error_setg(errp, "backend '%s' not supported on this host", error_setg(errp, "backend '%s' not supported on this host",
object_get_typename(OBJECT(backend))); object_get_typename(OBJECT(backend)));
return false;
#else #else
HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(backend); HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(backend);
g_autofree gchar *name = NULL; g_autofree gchar *name = NULL;
@ -49,11 +50,11 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
if (!backend->size) { if (!backend->size) {
error_setg(errp, "can't create backend with size 0"); error_setg(errp, "can't create backend with size 0");
return; return false;
} }
if (!fb->mem_path) { if (!fb->mem_path) {
error_setg(errp, "mem-path property not set"); error_setg(errp, "mem-path property not set");
return; return false;
} }
switch (fb->rom) { switch (fb->rom) {
@ -65,18 +66,18 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
if (!fb->readonly) { if (!fb->readonly) {
error_setg(errp, "property 'rom' = 'on' is not supported with" error_setg(errp, "property 'rom' = 'on' is not supported with"
" 'readonly' = 'off'"); " 'readonly' = 'off'");
return; return false;
} }
break; break;
case ON_OFF_AUTO_OFF: case ON_OFF_AUTO_OFF:
if (fb->readonly && backend->share) { if (fb->readonly && backend->share) {
error_setg(errp, "property 'rom' = 'off' is incompatible with" error_setg(errp, "property 'rom' = 'off' is incompatible with"
" 'readonly' = 'on' and 'share' = 'on'"); " 'readonly' = 'on' and 'share' = 'on'");
return; return false;
} }
break; break;
default: default:
assert(false); g_assert_not_reached();
} }
name = host_memory_backend_get_name(backend); name = host_memory_backend_get_name(backend);
@ -86,9 +87,9 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
ram_flags |= backend->reserve ? 0 : RAM_NORESERVE; ram_flags |= backend->reserve ? 0 : RAM_NORESERVE;
ram_flags |= fb->is_pmem ? RAM_PMEM : 0; ram_flags |= fb->is_pmem ? RAM_PMEM : 0;
ram_flags |= RAM_NAMED_FILE; ram_flags |= RAM_NAMED_FILE;
memory_region_init_ram_from_file(&backend->mr, OBJECT(backend), name, return memory_region_init_ram_from_file(&backend->mr, OBJECT(backend), name,
backend->size, fb->align, ram_flags, backend->size, fb->align, ram_flags,
fb->mem_path, fb->offset, errp); fb->mem_path, fb->offset, errp);
#endif #endif
} }

View File

@ -31,7 +31,7 @@ struct HostMemoryBackendMemfd {
bool seal; bool seal;
}; };
static void static bool
memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
{ {
HostMemoryBackendMemfd *m = MEMORY_BACKEND_MEMFD(backend); HostMemoryBackendMemfd *m = MEMORY_BACKEND_MEMFD(backend);
@ -41,7 +41,7 @@ memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
if (!backend->size) { if (!backend->size) {
error_setg(errp, "can't create backend with size 0"); error_setg(errp, "can't create backend with size 0");
return; return false;
} }
fd = qemu_memfd_create(TYPE_MEMORY_BACKEND_MEMFD, backend->size, fd = qemu_memfd_create(TYPE_MEMORY_BACKEND_MEMFD, backend->size,
@ -49,14 +49,14 @@ memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL : 0, F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL : 0,
errp); errp);
if (fd == -1) { if (fd == -1) {
return; return false;
} }
name = host_memory_backend_get_name(backend); name = host_memory_backend_get_name(backend);
ram_flags = backend->share ? RAM_SHARED : 0; ram_flags = backend->share ? RAM_SHARED : 0;
ram_flags |= backend->reserve ? 0 : RAM_NORESERVE; ram_flags |= backend->reserve ? 0 : RAM_NORESERVE;
memory_region_init_ram_from_fd(&backend->mr, OBJECT(backend), name, return memory_region_init_ram_from_fd(&backend->mr, OBJECT(backend), name,
backend->size, ram_flags, fd, 0, errp); backend->size, ram_flags, fd, 0, errp);
} }
static bool static bool

View File

@ -16,7 +16,7 @@
#include "qemu/module.h" #include "qemu/module.h"
#include "qom/object_interfaces.h" #include "qom/object_interfaces.h"
static void static bool
ram_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) ram_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
{ {
g_autofree char *name = NULL; g_autofree char *name = NULL;
@ -24,14 +24,15 @@ ram_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
if (!backend->size) { if (!backend->size) {
error_setg(errp, "can't create backend with size 0"); error_setg(errp, "can't create backend with size 0");
return; return false;
} }
name = host_memory_backend_get_name(backend); name = host_memory_backend_get_name(backend);
ram_flags = backend->share ? RAM_SHARED : 0; ram_flags = backend->share ? RAM_SHARED : 0;
ram_flags |= backend->reserve ? 0 : RAM_NORESERVE; ram_flags |= backend->reserve ? 0 : RAM_NORESERVE;
memory_region_init_ram_flags_nomigrate(&backend->mr, OBJECT(backend), name, return memory_region_init_ram_flags_nomigrate(&backend->mr, OBJECT(backend),
backend->size, ram_flags, errp); name, backend->size,
ram_flags, errp);
} }
static void static void

View File

@ -331,9 +331,8 @@ host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)
if (!bc->alloc) { if (!bc->alloc) {
return; return;
} }
bc->alloc(backend, &local_err); if (!bc->alloc(backend, errp)) {
if (local_err) { return;
goto out;
} }
ptr = memory_region_get_ram_ptr(&backend->mr); ptr = memory_region_get_ram_ptr(&backend->mr);

View File

@ -47,7 +47,15 @@ OBJECT_DECLARE_TYPE(HostMemoryBackend, HostMemoryBackendClass,
struct HostMemoryBackendClass { struct HostMemoryBackendClass {
ObjectClass parent_class; ObjectClass parent_class;
void (*alloc)(HostMemoryBackend *backend, Error **errp); /**
* alloc: Allocate memory from backend.
*
* @backend: the #HostMemoryBackend.
* @errp: pointer to Error*, to store an error if it happens.
*
* Return: true on success, else false setting @errp with error.
*/
bool (*alloc)(HostMemoryBackend *backend, Error **errp);
}; };
/** /**