qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Philippe Mathieu-Daudé" <philmd@linaro.org>
To: qemu-devel@nongnu.org
Cc: qemu-ppc@nongnu.org, qemu-arm@nongnu.org, qemu-riscv@nongnu.org,
	"Philippe Mathieu-Daudé" <philmd@linaro.org>,
	"Manos Pitsidianakis" <manos.pitsidianakis@linaro.org>,
	"Gavin Shan" <gshan@redhat.com>,
	"David Hildenbrand" <david@redhat.com>,
	"Igor Mammedov" <imammedo@redhat.com>
Subject: [PULL 59/71] backends: Have HostMemoryBackendClass::alloc() handler return a boolean
Date: Fri,  5 Jan 2024 16:42:52 +0100	[thread overview]
Message-ID: <20240105154307.21385-60-philmd@linaro.org> (raw)
In-Reply-To: <20240105154307.21385-1-philmd@linaro.org>

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>
---
 include/sysemu/hostmem.h | 10 +++++++++-
 backends/hostmem-epc.c   | 11 +++++------
 backends/hostmem-file.c  | 19 ++++++++++---------
 backends/hostmem-memfd.c | 10 +++++-----
 backends/hostmem-ram.c   |  9 +++++----
 backends/hostmem.c       |  5 ++---
 6 files changed, 36 insertions(+), 28 deletions(-)

diff --git a/include/sysemu/hostmem.h b/include/sysemu/hostmem.h
index 39326f1d4f..0e411aaa29 100644
--- a/include/sysemu/hostmem.h
+++ b/include/sysemu/hostmem.h
@@ -47,7 +47,15 @@ OBJECT_DECLARE_TYPE(HostMemoryBackend, HostMemoryBackendClass,
 struct HostMemoryBackendClass {
     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);
 };
 
 /**
diff --git a/backends/hostmem-epc.c b/backends/hostmem-epc.c
index 3ceb079f9e..735e2e1cf8 100644
--- a/backends/hostmem-epc.c
+++ b/backends/hostmem-epc.c
@@ -17,7 +17,7 @@
 #include "sysemu/hostmem.h"
 #include "hw/i386/hostmem-epc.h"
 
-static void
+static bool
 sgx_epc_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
 {
     g_autofree char *name = NULL;
@@ -26,21 +26,20 @@ sgx_epc_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
 
     if (!backend->size) {
         error_setg(errp, "can't create backend with size 0");
-        return;
+        return false;
     }
 
     fd = qemu_open_old("/dev/sgx_vepc", O_RDWR);
     if (fd < 0) {
         error_setg_errno(errp, errno,
                          "failed to open /dev/sgx_vepc to alloc SGX EPC");
-        return;
+        return false;
     }
 
     name = object_get_canonical_path(OBJECT(backend));
     ram_flags = (backend->share ? RAM_SHARED : 0) | RAM_PROTECTED;
-    memory_region_init_ram_from_fd(&backend->mr, OBJECT(backend),
-                                   name, backend->size, ram_flags,
-                                   fd, 0, errp);
+    return memory_region_init_ram_from_fd(&backend->mr, OBJECT(backend), name,
+                                          backend->size, ram_flags, fd, 0, errp);
 }
 
 static void sgx_epc_backend_instance_init(Object *obj)
diff --git a/backends/hostmem-file.c b/backends/hostmem-file.c
index fe8c481f8f..ac3e433cbd 100644
--- a/backends/hostmem-file.c
+++ b/backends/hostmem-file.c
@@ -36,12 +36,13 @@ struct HostMemoryBackendFile {
     OnOffAuto rom;
 };
 
-static void
+static bool
 file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
 {
 #ifndef CONFIG_POSIX
     error_setg(errp, "backend '%s' not supported on this host",
                object_get_typename(OBJECT(backend)));
+    return false;
 #else
     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(backend);
     g_autofree gchar *name = NULL;
@@ -49,11 +50,11 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
 
     if (!backend->size) {
         error_setg(errp, "can't create backend with size 0");
-        return;
+        return false;
     }
     if (!fb->mem_path) {
         error_setg(errp, "mem-path property not set");
-        return;
+        return false;
     }
 
     switch (fb->rom) {
@@ -65,18 +66,18 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
         if (!fb->readonly) {
             error_setg(errp, "property 'rom' = 'on' is not supported with"
                        " 'readonly' = 'off'");
-            return;
+            return false;
         }
         break;
     case ON_OFF_AUTO_OFF:
         if (fb->readonly && backend->share) {
             error_setg(errp, "property 'rom' = 'off' is incompatible with"
                        " 'readonly' = 'on' and 'share' = 'on'");
-            return;
+            return false;
         }
         break;
     default:
-        assert(false);
+        g_assert_not_reached();
     }
 
     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 |= fb->is_pmem ? RAM_PMEM : 0;
     ram_flags |= RAM_NAMED_FILE;
-    memory_region_init_ram_from_file(&backend->mr, OBJECT(backend), name,
-                                     backend->size, fb->align, ram_flags,
-                                     fb->mem_path, fb->offset, errp);
+    return memory_region_init_ram_from_file(&backend->mr, OBJECT(backend), name,
+                                            backend->size, fb->align, ram_flags,
+                                            fb->mem_path, fb->offset, errp);
 #endif
 }
 
diff --git a/backends/hostmem-memfd.c b/backends/hostmem-memfd.c
index db28ab5a56..3923ea9364 100644
--- a/backends/hostmem-memfd.c
+++ b/backends/hostmem-memfd.c
@@ -31,7 +31,7 @@ struct HostMemoryBackendMemfd {
     bool seal;
 };
 
-static void
+static bool
 memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
 {
     HostMemoryBackendMemfd *m = MEMORY_BACKEND_MEMFD(backend);
@@ -41,7 +41,7 @@ memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
 
     if (!backend->size) {
         error_setg(errp, "can't create backend with size 0");
-        return;
+        return false;
     }
 
     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,
                            errp);
     if (fd == -1) {
-        return;
+        return false;
     }
 
     name = host_memory_backend_get_name(backend);
     ram_flags = backend->share ? RAM_SHARED : 0;
     ram_flags |= backend->reserve ? 0 : RAM_NORESERVE;
-    memory_region_init_ram_from_fd(&backend->mr, OBJECT(backend), name,
-                                   backend->size, ram_flags, fd, 0, errp);
+    return memory_region_init_ram_from_fd(&backend->mr, OBJECT(backend), name,
+                                          backend->size, ram_flags, fd, 0, errp);
 }
 
 static bool
diff --git a/backends/hostmem-ram.c b/backends/hostmem-ram.c
index 0a670fc22a..d121249f0f 100644
--- a/backends/hostmem-ram.c
+++ b/backends/hostmem-ram.c
@@ -16,7 +16,7 @@
 #include "qemu/module.h"
 #include "qom/object_interfaces.h"
 
-static void
+static bool
 ram_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
 {
     g_autofree char *name = NULL;
@@ -24,14 +24,15 @@ ram_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
 
     if (!backend->size) {
         error_setg(errp, "can't create backend with size 0");
-        return;
+        return false;
     }
 
     name = host_memory_backend_get_name(backend);
     ram_flags = backend->share ? RAM_SHARED : 0;
     ram_flags |= backend->reserve ? 0 : RAM_NORESERVE;
-    memory_region_init_ram_flags_nomigrate(&backend->mr, OBJECT(backend), name,
-                                           backend->size, ram_flags, errp);
+    return memory_region_init_ram_flags_nomigrate(&backend->mr, OBJECT(backend),
+                                                  name, backend->size,
+                                                  ram_flags, errp);
 }
 
 static void
diff --git a/backends/hostmem.c b/backends/hostmem.c
index 1723c19165..3f8eb936d7 100644
--- a/backends/hostmem.c
+++ b/backends/hostmem.c
@@ -331,9 +331,8 @@ host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)
     if (!bc->alloc) {
         return;
     }
-    bc->alloc(backend, &local_err);
-    if (local_err) {
-        goto out;
+    if (!bc->alloc(backend, errp)) {
+        return;
     }
 
     ptr = memory_region_get_ram_ptr(&backend->mr);
-- 
2.41.0



  parent reply	other threads:[~2024-01-05 15:59 UTC|newest]

Thread overview: 76+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-05 15:41 [PULL 00/71] HW core patches for 2024-01-05 Philippe Mathieu-Daudé
2024-01-05 15:41 ` [PULL 01/71] meson: Allow building binary with no target-specific files in hw/ Philippe Mathieu-Daudé
2024-01-05 15:41 ` [PULL 02/71] target/alpha: Remove fallback to ev67 cpu class Philippe Mathieu-Daudé
2024-01-05 15:41 ` [PULL 03/71] target/hppa: Remove object_class_is_abstract() Philippe Mathieu-Daudé
2024-01-05 15:41 ` [PULL 04/71] cpu: Call object_class_dynamic_cast() once in cpu_class_by_name() Philippe Mathieu-Daudé
2024-01-05 15:41 ` [PULL 05/71] cpu: Add helper cpu_model_from_type() Philippe Mathieu-Daudé
2024-01-05 15:41 ` [PULL 06/71] cpu: Add generic cpu_list() Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 07/71] target/alpha: Use " Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 08/71] target/arm: " Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 09/71] target/avr: " Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 10/71] target/cris: " Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 11/71] target/hexagon: " Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 12/71] target/hppa: " Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 13/71] target/loongarch: " Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 14/71] target/m68k: " Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 15/71] target/mips: " Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 16/71] target/openrisc: " Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 17/71] target/riscv: " Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 18/71] target/rx: " Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 19/71] target/sh4: " Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 20/71] target/tricore: " Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 21/71] target/xtensa: " Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 22/71] target: Use generic cpu_model_from_type() Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 23/71] hw/core: Add machine_class_default_cpu_type() Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 24/71] machine: Use error handling when CPU type is checked Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 25/71] machine: Introduce helper is_cpu_type_supported() Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 26/71] machine: Improve is_cpu_type_supported() Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 27/71] machine: Print CPU model name instead of CPU type Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 28/71] hw/arm/virt: Hide host CPU model for tcg Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 29/71] hw/arm/virt: Check CPU type in machine_run_board_init() Philippe Mathieu-Daudé
2024-01-09 14:33   ` Peter Maydell
2024-01-11  6:02     ` Gavin Shan
2024-01-05 15:42 ` [PULL 30/71] hw/arm/sbsa-ref: " Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 31/71] hw/arm: " Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 32/71] hw/riscv/shakti_c: " Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 33/71] hw/core/cpu: Remove final vestiges of dynamic state tracing Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 34/71] hw/core/cpu: Update description of CPUState::node Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 35/71] hw/cpu/core: Cleanup unused included header in core.c Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 36/71] hw/cpu/cluster: Cleanup unused included header in cluster.c Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 37/71] hw/audio/sb16: Do not migrate qdev properties Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 38/71] hw/arm/bcm2836: Simplify use of 'reset-cbar' property Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 39/71] hw/arm/bcm2836: Use ARM_CPU 'mp-affinity' property Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 40/71] hw/ppc/spapr_cpu_core: Access QDev properties with proper API Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 41/71] hw: Simplify accesses to the CPUState::'start-powered-off' property Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 42/71] hw/ppc/xive2_regs: Remove unnecessary 'cpu.h' inclusion Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 43/71] hw/mips: Inline 'bios.h' definitions Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 44/71] memory: Have memory_region_init_ram_flags_nomigrate() return a boolean Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 45/71] memory: Have memory_region_init_ram_nomigrate() handler " Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 46/71] memory: Have memory_region_init_rom_nomigrate() " Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 47/71] memory: Simplify memory_region_init_rom_nomigrate() calls Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 48/71] memory: Simplify memory_region_init_ram_from_fd() calls Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 49/71] memory: Have memory_region_init_ram() handler return a boolean Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 50/71] memory: Have memory_region_init_rom() " Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 51/71] memory: Have memory_region_init_rom_device_nomigrate() " Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 52/71] memory: Simplify memory_region_init_rom_device_nomigrate() calls Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 53/71] memory: Have memory_region_init_rom_device() handler return a boolean Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 54/71] memory: Have memory_region_init_resizeable_ram() " Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 55/71] memory: Have memory_region_init_ram_from_file() handler " Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 56/71] memory: Have memory_region_init_ram_from_fd() " Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 57/71] backends: Use g_autofree in HostMemoryBackendClass::alloc() handlers Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 58/71] backends: Simplify host_memory_backend_memory_complete() Philippe Mathieu-Daudé
2024-01-05 15:42 ` Philippe Mathieu-Daudé [this message]
2024-01-05 15:42 ` [PULL 60/71] backends: Reduce variable scope in host_memory_backend_memory_complete Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 61/71] util/oslib: Have qemu_prealloc_mem() handler return a boolean Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 62/71] misc: Simplify qemu_prealloc_mem() calls Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 63/71] hw: Simplify memory_region_init_ram() calls Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 64/71] hw/arm: Simplify memory_region_init_rom() calls Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 65/71] hw/sparc: Simplify memory_region_init_ram_nomigrate() calls Philippe Mathieu-Daudé
2024-01-05 15:42 ` [PULL 66/71] hw/misc: Simplify memory_region_init_ram_from_fd() calls Philippe Mathieu-Daudé
2024-01-05 15:43 ` [PULL 67/71] hw/nvram: Simplify memory_region_init_rom_device() calls Philippe Mathieu-Daudé
2024-01-05 15:43 ` [PULL 68/71] hw/pci-host/raven: Propagate error in raven_realize() Philippe Mathieu-Daudé
2024-01-05 15:43 ` [PULL 69/71] hw/m68k/mcf5206: Embed m5206_timer_state in m5206_mbar_state Philippe Mathieu-Daudé
2024-01-05 15:43 ` [PULL 70/71] hw/net/can/sja1000: fix bug for single acceptance filter and standard frame Philippe Mathieu-Daudé
2024-01-05 15:43 ` [PULL 71/71] target/sparc: Simplify qemu_irq_ack Philippe Mathieu-Daudé
2024-01-05 17:05 ` [PULL 00/71] HW core patches for 2024-01-05 Philippe Mathieu-Daudé
2024-01-05 18:43   ` Peter Maydell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240105154307.21385-60-philmd@linaro.org \
    --to=philmd@linaro.org \
    --cc=david@redhat.com \
    --cc=gshan@redhat.com \
    --cc=imammedo@redhat.com \
    --cc=manos.pitsidianakis@linaro.org \
    --cc=qemu-arm@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-ppc@nongnu.org \
    --cc=qemu-riscv@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).