qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Philippe Mathieu-Daudé" <philmd@linaro.org>
To: qemu-devel@nongnu.org
Cc: "David Hildenbrand" <david@redhat.com>,
	"Peter Maydell" <peter.maydell@linaro.org>,
	"Michael S. Tsirkin" <mst@redhat.com>,
	qemu-arm@nongnu.org,
	"Mark Cave-Ayland" <mark.cave-ayland@ilande.co.uk>,
	"Markus Armbruster" <armbru@redhat.com>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Igor Mammedov" <imammedo@redhat.com>,
	qemu-ppc@nongnu.org, "Cédric Le Goater" <clg@kaod.org>,
	"Philippe Mathieu-Daudé" <philmd@linaro.org>,
	"Peter Xu" <peterx@redhat.com>
Subject: [PATCH-for-9.0 15/25] backends: Simplify host_memory_backend_memory_complete()
Date: Mon, 20 Nov 2023 22:32:49 +0100	[thread overview]
Message-ID: <20231120213301.24349-16-philmd@linaro.org> (raw)
In-Reply-To: <20231120213301.24349-1-philmd@linaro.org>

Return early if bc->alloc is NULL. De-indent the if() ladder.

Note, this avoids a pointless call to error_propagate() with
errp=NULL at the 'out:' label.

Change trivial when reviewed with 'git-diff --ignore-all-space'.

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 backends/hostmem.c | 133 +++++++++++++++++++++++----------------------
 1 file changed, 67 insertions(+), 66 deletions(-)

diff --git a/backends/hostmem.c b/backends/hostmem.c
index 747e7838c0..1723c19165 100644
--- a/backends/hostmem.c
+++ b/backends/hostmem.c
@@ -328,83 +328,84 @@ host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)
     void *ptr;
     uint64_t sz;
 
-    if (bc->alloc) {
-        bc->alloc(backend, &local_err);
-        if (local_err) {
-            goto out;
-        }
+    if (!bc->alloc) {
+        return;
+    }
+    bc->alloc(backend, &local_err);
+    if (local_err) {
+        goto out;
+    }
 
-        ptr = memory_region_get_ram_ptr(&backend->mr);
-        sz = memory_region_size(&backend->mr);
+    ptr = memory_region_get_ram_ptr(&backend->mr);
+    sz = memory_region_size(&backend->mr);
 
-        if (backend->merge) {
-            qemu_madvise(ptr, sz, QEMU_MADV_MERGEABLE);
-        }
-        if (!backend->dump) {
-            qemu_madvise(ptr, sz, QEMU_MADV_DONTDUMP);
-        }
+    if (backend->merge) {
+        qemu_madvise(ptr, sz, QEMU_MADV_MERGEABLE);
+    }
+    if (!backend->dump) {
+        qemu_madvise(ptr, sz, QEMU_MADV_DONTDUMP);
+    }
 #ifdef CONFIG_NUMA
-        unsigned long lastbit = find_last_bit(backend->host_nodes, MAX_NODES);
-        /* lastbit == MAX_NODES means maxnode = 0 */
-        unsigned long maxnode = (lastbit + 1) % (MAX_NODES + 1);
-        /* ensure policy won't be ignored in case memory is preallocated
-         * before mbind(). note: MPOL_MF_STRICT is ignored on hugepages so
-         * this doesn't catch hugepage case. */
-        unsigned flags = MPOL_MF_STRICT | MPOL_MF_MOVE;
-        int mode = backend->policy;
+    unsigned long lastbit = find_last_bit(backend->host_nodes, MAX_NODES);
+    /* lastbit == MAX_NODES means maxnode = 0 */
+    unsigned long maxnode = (lastbit + 1) % (MAX_NODES + 1);
+    /* ensure policy won't be ignored in case memory is preallocated
+     * before mbind(). note: MPOL_MF_STRICT is ignored on hugepages so
+     * this doesn't catch hugepage case. */
+    unsigned flags = MPOL_MF_STRICT | MPOL_MF_MOVE;
+    int mode = backend->policy;
 
-        /* check for invalid host-nodes and policies and give more verbose
-         * error messages than mbind(). */
-        if (maxnode && backend->policy == MPOL_DEFAULT) {
-            error_setg(errp, "host-nodes must be empty for policy default,"
-                       " or you should explicitly specify a policy other"
-                       " than default");
-            return;
-        } else if (maxnode == 0 && backend->policy != MPOL_DEFAULT) {
-            error_setg(errp, "host-nodes must be set for policy %s",
-                       HostMemPolicy_str(backend->policy));
-            return;
-        }
+    /* check for invalid host-nodes and policies and give more verbose
+     * error messages than mbind(). */
+    if (maxnode && backend->policy == MPOL_DEFAULT) {
+        error_setg(errp, "host-nodes must be empty for policy default,"
+                   " or you should explicitly specify a policy other"
+                   " than default");
+        return;
+    } else if (maxnode == 0 && backend->policy != MPOL_DEFAULT) {
+        error_setg(errp, "host-nodes must be set for policy %s",
+                   HostMemPolicy_str(backend->policy));
+        return;
+    }
 
-        /* We can have up to MAX_NODES nodes, but we need to pass maxnode+1
-         * as argument to mbind() due to an old Linux bug (feature?) which
-         * cuts off the last specified node. This means backend->host_nodes
-         * must have MAX_NODES+1 bits available.
-         */
-        assert(sizeof(backend->host_nodes) >=
-               BITS_TO_LONGS(MAX_NODES + 1) * sizeof(unsigned long));
-        assert(maxnode <= MAX_NODES);
+    /* We can have up to MAX_NODES nodes, but we need to pass maxnode+1
+     * as argument to mbind() due to an old Linux bug (feature?) which
+     * cuts off the last specified node. This means backend->host_nodes
+     * must have MAX_NODES+1 bits available.
+     */
+    assert(sizeof(backend->host_nodes) >=
+           BITS_TO_LONGS(MAX_NODES + 1) * sizeof(unsigned long));
+    assert(maxnode <= MAX_NODES);
 
 #ifdef HAVE_NUMA_HAS_PREFERRED_MANY
-        if (mode == MPOL_PREFERRED && numa_has_preferred_many() > 0) {
-            /*
-             * Replace with MPOL_PREFERRED_MANY otherwise the mbind() below
-             * silently picks the first node.
-             */
-            mode = MPOL_PREFERRED_MANY;
-        }
+    if (mode == MPOL_PREFERRED && numa_has_preferred_many() > 0) {
+        /*
+         * Replace with MPOL_PREFERRED_MANY otherwise the mbind() below
+         * silently picks the first node.
+         */
+        mode = MPOL_PREFERRED_MANY;
+    }
 #endif
 
-        if (maxnode &&
-            mbind(ptr, sz, mode, backend->host_nodes, maxnode + 1, flags)) {
-            if (backend->policy != MPOL_DEFAULT || errno != ENOSYS) {
-                error_setg_errno(errp, errno,
-                                 "cannot bind memory to host NUMA nodes");
-                return;
-            }
+    if (maxnode &&
+        mbind(ptr, sz, mode, backend->host_nodes, maxnode + 1, flags)) {
+        if (backend->policy != MPOL_DEFAULT || errno != ENOSYS) {
+            error_setg_errno(errp, errno,
+                             "cannot bind memory to host NUMA nodes");
+            return;
         }
+    }
 #endif
-        /* Preallocate memory after the NUMA policy has been instantiated.
-         * This is necessary to guarantee memory is allocated with
-         * specified NUMA policy in place.
-         */
-        if (backend->prealloc) {
-            qemu_prealloc_mem(memory_region_get_fd(&backend->mr), ptr, sz,
-                              backend->prealloc_threads,
-                              backend->prealloc_context, &local_err);
-            if (local_err) {
-                goto out;
-            }
+    /* Preallocate memory after the NUMA policy has been instantiated.
+     * This is necessary to guarantee memory is allocated with
+     * specified NUMA policy in place.
+     */
+    if (backend->prealloc) {
+        qemu_prealloc_mem(memory_region_get_fd(&backend->mr), ptr, sz,
+                          backend->prealloc_threads,
+                          backend->prealloc_context, &local_err);
+        if (local_err) {
+            goto out;
         }
     }
 out:
-- 
2.41.0



  parent reply	other threads:[~2023-11-20 21:36 UTC|newest]

Thread overview: 84+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-20 21:32 [PATCH-for-9.0 00/25] memory: Propagate Error* when possible Philippe Mathieu-Daudé
2023-11-20 21:32 ` [PATCH-for-9.0 01/25] memory: Have memory_region_init_ram_flags_nomigrate() return a boolean Philippe Mathieu-Daudé
2023-11-21 12:03   ` Manos Pitsidianakis
2023-11-21 14:56   ` Peter Xu
2023-12-04  4:42   ` Gavin Shan
2023-11-20 21:32 ` [PATCH-for-9.0 02/25] memory: Have memory_region_init_ram_nomigrate() handler " Philippe Mathieu-Daudé
2023-11-21 11:54   ` Manos Pitsidianakis
2023-12-04  4:44   ` Gavin Shan
2023-11-20 21:32 ` [PATCH-for-9.0 03/25] memory: Have memory_region_init_rom_nomigrate() " Philippe Mathieu-Daudé
2023-11-21 12:10   ` Manos Pitsidianakis
2024-01-05 14:46     ` Philippe Mathieu-Daudé
2024-01-05 14:57       ` Peter Maydell
2024-01-05 17:13         ` Philippe Mathieu-Daudé
2023-12-04  4:45   ` Gavin Shan
2023-11-20 21:32 ` [PATCH-for-9.0 04/25] memory: Simplify memory_region_init_rom_nomigrate() calls Philippe Mathieu-Daudé
2023-11-21 14:57   ` Peter Xu
2023-11-21 18:50   ` Richard Henderson
2023-11-21 18:52     ` Richard Henderson
2023-12-04  4:46   ` Gavin Shan
2023-11-20 21:32 ` [PATCH-for-9.0 05/25] memory: Simplify memory_region_init_ram_from_fd() calls Philippe Mathieu-Daudé
2023-11-21 12:13   ` Manos Pitsidianakis
2023-12-04  4:46   ` Gavin Shan
2023-11-20 21:32 ` [PATCH-for-9.0 06/25] memory: Have memory_region_init_ram() handler return a boolean Philippe Mathieu-Daudé
2023-11-21 14:58   ` Peter Xu
2023-12-04  4:47   ` Gavin Shan
2023-11-20 21:32 ` [PATCH-for-9.0 07/25] memory: Have memory_region_init_rom() " Philippe Mathieu-Daudé
2023-11-21 12:15   ` Manos Pitsidianakis
2023-12-04  4:48   ` Gavin Shan
2023-11-20 21:32 ` [PATCH-for-9.0 08/25] memory: Have memory_region_init_rom_device_nomigrate() " Philippe Mathieu-Daudé
2023-11-21 14:59   ` Peter Xu
2023-12-04  4:49   ` Gavin Shan
2023-11-20 21:32 ` [PATCH-for-9.0 09/25] memory: Simplify memory_region_init_rom_device_nomigrate() calls Philippe Mathieu-Daudé
2023-11-21 15:00   ` Peter Xu
2023-12-04  4:50   ` Gavin Shan
2023-11-20 21:32 ` [PATCH-for-9.0 10/25] memory: Have memory_region_init_rom_device() handler return a boolean Philippe Mathieu-Daudé
2023-11-21 15:00   ` Peter Xu
2023-12-04  4:51   ` Gavin Shan
2023-11-20 21:32 ` [PATCH-for-9.0 11/25] memory: Have memory_region_init_resizeable_ram() " Philippe Mathieu-Daudé
2023-11-21 15:02   ` Peter Xu
2023-12-04  4:52   ` Gavin Shan
2023-11-20 21:32 ` [PATCH-for-9.0 12/25] memory: Have memory_region_init_ram_from_file() handler " Philippe Mathieu-Daudé
2023-11-21 15:03   ` Peter Xu
2023-12-04  4:53   ` Gavin Shan
2023-11-20 21:32 ` [PATCH-for-9.0 13/25] memory: Have memory_region_init_ram_from_fd() " Philippe Mathieu-Daudé
2023-11-21 15:04   ` Peter Xu
2023-12-04  4:54   ` Gavin Shan
2023-11-20 21:32 ` [PATCH-for-9.0 14/25] backends: Use g_autofree in HostMemoryBackendClass::alloc() handlers Philippe Mathieu-Daudé
2023-11-22  7:09   ` Manos Pitsidianakis
2023-12-04  4:55   ` Gavin Shan
2023-11-20 21:32 ` Philippe Mathieu-Daudé [this message]
2023-11-22  7:11   ` [PATCH-for-9.0 15/25] backends: Simplify host_memory_backend_memory_complete() Manos Pitsidianakis
2023-12-04  4:56   ` Gavin Shan
2023-11-20 21:32 ` [PATCH-for-9.0 16/25] backends: Have HostMemoryBackendClass::alloc() handler return a boolean Philippe Mathieu-Daudé
2023-11-22  7:14   ` Manos Pitsidianakis
2023-12-04  4:57   ` Gavin Shan
2023-11-20 21:32 ` [PATCH-for-9.0 17/25] backends: Reduce variable scope in host_memory_backend_memory_complete Philippe Mathieu-Daudé
2023-11-22  7:32   ` Manos Pitsidianakis
2023-12-04  4:58   ` Gavin Shan
2023-11-20 21:32 ` [PATCH-for-9.0 18/25] util/oslib: Have qemu_prealloc_mem() handler return a boolean Philippe Mathieu-Daudé
2023-11-21 15:07   ` Peter Xu
2023-12-04  4:59   ` Gavin Shan
2023-11-20 21:32 ` [PATCH-for-9.0 19/25] misc: Simplify qemu_prealloc_mem() calls Philippe Mathieu-Daudé
2023-11-22  7:38   ` Manos Pitsidianakis
2024-01-05 15:04     ` Philippe Mathieu-Daudé
2023-12-04  5:00   ` Gavin Shan
2023-11-20 21:32 ` [PATCH-for-9.0 20/25] hw: Simplify memory_region_init_ram() calls Philippe Mathieu-Daudé
2023-11-22  7:42   ` Manos Pitsidianakis
2023-11-27  1:01   ` Andrew Jeffery
2023-12-04  5:00   ` Gavin Shan
2023-11-20 21:32 ` [PATCH-for-9.0 21/25] hw/arm: Simplify memory_region_init_rom() calls Philippe Mathieu-Daudé
2023-11-22  7:43   ` Manos Pitsidianakis
2023-12-04  5:01   ` Gavin Shan
2023-11-20 21:32 ` [PATCH-for-9.0 22/25] hw/sparc: Simplify memory_region_init_ram_nomigrate() calls Philippe Mathieu-Daudé
2023-11-21 11:31   ` Philippe Mathieu-Daudé
2023-12-04  5:02   ` Gavin Shan
2023-11-20 21:32 ` [PATCH-for-9.0 23/25] hw/misc: Simplify memory_region_init_ram_from_fd() calls Philippe Mathieu-Daudé
2023-11-22  7:45   ` Manos Pitsidianakis
2023-12-04  5:02   ` Gavin Shan
2023-11-20 21:32 ` [PATCH-for-9.0 24/25] hw/nvram: Simplify memory_region_init_rom_device() calls Philippe Mathieu-Daudé
2023-11-22  7:46   ` Manos Pitsidianakis
2023-12-04  5:03   ` Gavin Shan
2023-11-20 21:32 ` [PATCH-for-9.0 25/25] hw/pci-host/raven: Propagate error in raven_realize() Philippe Mathieu-Daudé
2023-11-22  7:46   ` Manos Pitsidianakis
2023-12-04  5:03   ` Gavin Shan

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=20231120213301.24349-16-philmd@linaro.org \
    --to=philmd@linaro.org \
    --cc=armbru@redhat.com \
    --cc=clg@kaod.org \
    --cc=david@redhat.com \
    --cc=imammedo@redhat.com \
    --cc=mark.cave-ayland@ilande.co.uk \
    --cc=mst@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=peterx@redhat.com \
    --cc=qemu-arm@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-ppc@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).