qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Alex Bennée" <alex.bennee@linaro.org>
To: qemu-devel@nongnu.org
Cc: "Peter Maydell" <peter.maydell@linaro.org>,
	qemu-arm@nongnu.org, "Alex Bennée" <alex.bennee@linaro.org>
Subject: [PATCH 2/4] hw/arm: use g_autofree for fdt in arm_load_dtb
Date: Mon,  1 Sep 2025 13:53:02 +0100	[thread overview]
Message-ID: <20250901125304.1047624-3-alex.bennee@linaro.org> (raw)
In-Reply-To: <20250901125304.1047624-1-alex.bennee@linaro.org>

With the fdt being protected by g_autofree we can skip the goto fail
and bail out straight away. The only thing we must take care of is
stealing the pointer in the one case when we do need it to survive.

Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
---
 hw/arm/boot.c | 29 ++++++++++++-----------------
 1 file changed, 12 insertions(+), 17 deletions(-)

diff --git a/hw/arm/boot.c b/hw/arm/boot.c
index 56fd13b9f7c..749f2d08341 100644
--- a/hw/arm/boot.c
+++ b/hw/arm/boot.c
@@ -519,7 +519,7 @@ int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
                  hwaddr addr_limit, AddressSpace *as, MachineState *ms,
                  ARMCPU *cpu)
 {
-    void *fdt = NULL;
+    g_autofree void *fdt = NULL;
     int size, rc, n = 0;
     uint32_t acells, scells;
     unsigned int i;
@@ -538,13 +538,13 @@ int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
         fdt = load_device_tree(filename, &size);
         if (!fdt) {
             fprintf(stderr, "Couldn't open dtb file %s\n", filename);
-            goto fail;
+            return -1;
         }
     } else {
         fdt = binfo->get_dtb(binfo, &size);
         if (!fdt) {
             fprintf(stderr, "Board was unable to create a dtb blob\n");
-            goto fail;
+            return -1;
         }
     }
 
@@ -553,7 +553,6 @@ int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
          * Whether this constitutes failure is up to the caller to decide,
          * so just return 0 as size, i.e., no error.
          */
-        g_free(fdt);
         return 0;
     }
 
@@ -563,7 +562,7 @@ int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
                                    NULL, &error_fatal);
     if (acells == 0 || scells == 0) {
         fprintf(stderr, "dtb file invalid (#address-cells or #size-cells 0)\n");
-        goto fail;
+        return -1;
     }
 
     if (scells < 2 && binfo->ram_size >= 4 * GiB) {
@@ -572,14 +571,14 @@ int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
          */
         fprintf(stderr, "qemu: dtb file not compatible with "
                 "RAM size > 4GB\n");
-        goto fail;
+        return -1;
     }
 
     /* nop all root nodes matching /memory or /memory@unit-address */
     node_path = qemu_fdt_node_unit_path(fdt, "memory", &err);
     if (err) {
         error_report_err(err);
-        goto fail;
+        return -1;
     }
     while (node_path[n]) {
         if (g_str_has_prefix(node_path[n], "/memory")) {
@@ -611,7 +610,7 @@ int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
             if (rc < 0) {
                 fprintf(stderr, "couldn't add /memory@%"PRIx64" node\n",
                         mem_base);
-                goto fail;
+                return -1;
             }
 
             mem_base += mem_len;
@@ -622,7 +621,7 @@ int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
         if (rc < 0) {
             fprintf(stderr, "couldn't add /memory@%"PRIx64" node\n",
                     binfo->loader_start);
-            goto fail;
+            return -1;
         }
     }
 
@@ -636,7 +635,7 @@ int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
                                      ms->kernel_cmdline);
         if (rc < 0) {
             fprintf(stderr, "couldn't set /chosen/bootargs\n");
-            goto fail;
+            return -1;
         }
     }
 
@@ -645,7 +644,7 @@ int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
                                           acells, binfo->initrd_start);
         if (rc < 0) {
             fprintf(stderr, "couldn't set /chosen/linux,initrd-start\n");
-            goto fail;
+            return -1;
         }
 
         rc = qemu_fdt_setprop_sized_cells(fdt, "/chosen", "linux,initrd-end",
@@ -654,7 +653,7 @@ int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
                                           binfo->initrd_size);
         if (rc < 0) {
             fprintf(stderr, "couldn't set /chosen/linux,initrd-end\n");
-            goto fail;
+            return -1;
         }
     }
 
@@ -673,14 +672,10 @@ int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
 
     if (fdt != ms->fdt) {
         g_free(ms->fdt);
-        ms->fdt = fdt;
+        ms->fdt = g_steal_pointer(&fdt);
     }
 
     return size;
-
-fail:
-    g_free(fdt);
-    return -1;
 }
 
 static void do_cpu_reset(void *opaque)
-- 
2.47.2



  parent reply	other threads:[~2025-09-01 12:53 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-01 12:53 [PATCH 0/4] arm_load_dtb cleanups Alex Bennée
2025-09-01 12:53 ` [PATCH 1/4] hw/arm: use g_autofree for filename in arm_load_dtb Alex Bennée
2025-09-01 13:04   ` Manos Pitsidianakis
2025-09-03  3:53   ` Richard Henderson
2025-09-01 12:53 ` Alex Bennée [this message]
2025-09-01 13:01   ` [PATCH 2/4] hw/arm: use g_autofree for fdt " Manos Pitsidianakis
2025-09-02  9:36   ` Peter Maydell
2025-09-03  8:16     ` Alex Bennée
2025-09-03 10:04       ` Peter Maydell
2025-09-03 10:05       ` Alex Bennée
2025-09-01 12:53 ` [PATCH 3/4] hw/arm: use g_auto(GStrv) for node_path " Alex Bennée
2025-09-01 13:05   ` Manos Pitsidianakis
2025-09-03  3:59   ` Richard Henderson
2025-09-01 12:53 ` [PATCH 4/4] hw/arm: expose Error * to arm_load_dtb Alex Bennée
2025-09-01 13:03   ` Manos Pitsidianakis

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=20250901125304.1047624-3-alex.bennee@linaro.org \
    --to=alex.bennee@linaro.org \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-arm@nongnu.org \
    --cc=qemu-devel@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).