62547e
From 4ca61efe246d62d420eb332655c0c8ead4cc762b Mon Sep 17 00:00:00 2001
62547e
From: Janosch Frank <frankja@linux.ibm.com>
62547e
Date: Wed, 30 Mar 2022 12:35:55 +0000
62547e
Subject: [PATCH 13/42] dump: Use ERRP_GUARD()
62547e
MIME-Version: 1.0
62547e
Content-Type: text/plain; charset=UTF-8
62547e
Content-Transfer-Encoding: 8bit
62547e
62547e
RH-Author: Cédric Le Goater <clg@redhat.com>
62547e
RH-MergeRequest: 226: s390: Enhanced Interpretation for PCI Functions and Secure Execution guest dump
62547e
RH-Bugzilla: 1664378 2043909
62547e
RH-Acked-by: Thomas Huth <thuth@redhat.com>
62547e
RH-Acked-by: Cornelia Huck <cohuck@redhat.com>
62547e
RH-Acked-by: Jon Maloy <jmaloy@redhat.com>
62547e
RH-Commit: [13/41] f735cd1dab0230000cfadd878765fdf4647b239c
62547e
62547e
Let's move to the new way of handling errors before changing the dump
62547e
code. This patch has mostly been generated by the coccinelle script
62547e
scripts/coccinelle/errp-guard.cocci.
62547e
62547e
Signed-off-by: Janosch Frank <frankja@linux.ibm.com>
62547e
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
62547e
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
62547e
Message-Id: <20220330123603.107120-2-frankja@linux.ibm.com>
62547e
(cherry picked from commit 86a518bba4f4d7c9016fc5b104fe1e58b00ad756)
62547e
Signed-off-by: Cédric Le Goater <clg@redhat.com>
62547e
---
62547e
 dump/dump.c | 144 ++++++++++++++++++++++------------------------------
62547e
 1 file changed, 61 insertions(+), 83 deletions(-)
62547e
62547e
diff --git a/dump/dump.c b/dump/dump.c
62547e
index 662d0a62cd..9876123f2e 100644
62547e
--- a/dump/dump.c
62547e
+++ b/dump/dump.c
62547e
@@ -390,23 +390,21 @@ static void write_data(DumpState *s, void *buf, int length, Error **errp)
62547e
 static void write_memory(DumpState *s, GuestPhysBlock *block, ram_addr_t start,
62547e
                          int64_t size, Error **errp)
62547e
 {
62547e
+    ERRP_GUARD();
62547e
     int64_t i;
62547e
-    Error *local_err = NULL;
62547e
 
62547e
     for (i = 0; i < size / s->dump_info.page_size; i++) {
62547e
         write_data(s, block->host_addr + start + i * s->dump_info.page_size,
62547e
-                   s->dump_info.page_size, &local_err);
62547e
-        if (local_err) {
62547e
-            error_propagate(errp, local_err);
62547e
+                   s->dump_info.page_size, errp);
62547e
+        if (*errp) {
62547e
             return;
62547e
         }
62547e
     }
62547e
 
62547e
     if ((size % s->dump_info.page_size) != 0) {
62547e
         write_data(s, block->host_addr + start + i * s->dump_info.page_size,
62547e
-                   size % s->dump_info.page_size, &local_err);
62547e
-        if (local_err) {
62547e
-            error_propagate(errp, local_err);
62547e
+                   size % s->dump_info.page_size, errp);
62547e
+        if (*errp) {
62547e
             return;
62547e
         }
62547e
     }
62547e
@@ -476,11 +474,11 @@ static void get_offset_range(hwaddr phys_addr,
62547e
 
62547e
 static void write_elf_loads(DumpState *s, Error **errp)
62547e
 {
62547e
+    ERRP_GUARD();
62547e
     hwaddr offset, filesz;
62547e
     MemoryMapping *memory_mapping;
62547e
     uint32_t phdr_index = 1;
62547e
     uint32_t max_index;
62547e
-    Error *local_err = NULL;
62547e
 
62547e
     if (s->have_section) {
62547e
         max_index = s->sh_info;
62547e
@@ -494,14 +492,13 @@ static void write_elf_loads(DumpState *s, Error **errp)
62547e
                          s, &offset, &filesz);
62547e
         if (s->dump_info.d_class == ELFCLASS64) {
62547e
             write_elf64_load(s, memory_mapping, phdr_index++, offset,
62547e
-                             filesz, &local_err);
62547e
+                             filesz, errp);
62547e
         } else {
62547e
             write_elf32_load(s, memory_mapping, phdr_index++, offset,
62547e
-                             filesz, &local_err);
62547e
+                             filesz, errp);
62547e
         }
62547e
 
62547e
-        if (local_err) {
62547e
-            error_propagate(errp, local_err);
62547e
+        if (*errp) {
62547e
             return;
62547e
         }
62547e
 
62547e
@@ -514,7 +511,7 @@ static void write_elf_loads(DumpState *s, Error **errp)
62547e
 /* write elf header, PT_NOTE and elf note to vmcore. */
62547e
 static void dump_begin(DumpState *s, Error **errp)
62547e
 {
62547e
-    Error *local_err = NULL;
62547e
+    ERRP_GUARD();
62547e
 
62547e
     /*
62547e
      * the vmcore's format is:
62547e
@@ -542,73 +539,64 @@ static void dump_begin(DumpState *s, Error **errp)
62547e
 
62547e
     /* write elf header to vmcore */
62547e
     if (s->dump_info.d_class == ELFCLASS64) {
62547e
-        write_elf64_header(s, &local_err);
62547e
+        write_elf64_header(s, errp);
62547e
     } else {
62547e
-        write_elf32_header(s, &local_err);
62547e
+        write_elf32_header(s, errp);
62547e
     }
62547e
-    if (local_err) {
62547e
-        error_propagate(errp, local_err);
62547e
+    if (*errp) {
62547e
         return;
62547e
     }
62547e
 
62547e
     if (s->dump_info.d_class == ELFCLASS64) {
62547e
         /* write PT_NOTE to vmcore */
62547e
-        write_elf64_note(s, &local_err);
62547e
-        if (local_err) {
62547e
-            error_propagate(errp, local_err);
62547e
+        write_elf64_note(s, errp);
62547e
+        if (*errp) {
62547e
             return;
62547e
         }
62547e
 
62547e
         /* write all PT_LOAD to vmcore */
62547e
-        write_elf_loads(s, &local_err);
62547e
-        if (local_err) {
62547e
-            error_propagate(errp, local_err);
62547e
+        write_elf_loads(s, errp);
62547e
+        if (*errp) {
62547e
             return;
62547e
         }
62547e
 
62547e
         /* write section to vmcore */
62547e
         if (s->have_section) {
62547e
-            write_elf_section(s, 1, &local_err);
62547e
-            if (local_err) {
62547e
-                error_propagate(errp, local_err);
62547e
+            write_elf_section(s, 1, errp);
62547e
+            if (*errp) {
62547e
                 return;
62547e
             }
62547e
         }
62547e
 
62547e
         /* write notes to vmcore */
62547e
-        write_elf64_notes(fd_write_vmcore, s, &local_err);
62547e
-        if (local_err) {
62547e
-            error_propagate(errp, local_err);
62547e
+        write_elf64_notes(fd_write_vmcore, s, errp);
62547e
+        if (*errp) {
62547e
             return;
62547e
         }
62547e
     } else {
62547e
         /* write PT_NOTE to vmcore */
62547e
-        write_elf32_note(s, &local_err);
62547e
-        if (local_err) {
62547e
-            error_propagate(errp, local_err);
62547e
+        write_elf32_note(s, errp);
62547e
+        if (*errp) {
62547e
             return;
62547e
         }
62547e
 
62547e
         /* write all PT_LOAD to vmcore */
62547e
-        write_elf_loads(s, &local_err);
62547e
-        if (local_err) {
62547e
-            error_propagate(errp, local_err);
62547e
+        write_elf_loads(s, errp);
62547e
+        if (*errp) {
62547e
             return;
62547e
         }
62547e
 
62547e
         /* write section to vmcore */
62547e
         if (s->have_section) {
62547e
-            write_elf_section(s, 0, &local_err);
62547e
-            if (local_err) {
62547e
-                error_propagate(errp, local_err);
62547e
+            write_elf_section(s, 0, errp);
62547e
+            if (*errp) {
62547e
                 return;
62547e
             }
62547e
         }
62547e
 
62547e
         /* write notes to vmcore */
62547e
-        write_elf32_notes(fd_write_vmcore, s, &local_err);
62547e
-        if (local_err) {
62547e
-            error_propagate(errp, local_err);
62547e
+        write_elf32_notes(fd_write_vmcore, s, errp);
62547e
+        if (*errp) {
62547e
             return;
62547e
         }
62547e
     }
62547e
@@ -644,9 +632,9 @@ static int get_next_block(DumpState *s, GuestPhysBlock *block)
62547e
 /* write all memory to vmcore */
62547e
 static void dump_iterate(DumpState *s, Error **errp)
62547e
 {
62547e
+    ERRP_GUARD();
62547e
     GuestPhysBlock *block;
62547e
     int64_t size;
62547e
-    Error *local_err = NULL;
62547e
 
62547e
     do {
62547e
         block = s->next_block;
62547e
@@ -658,9 +646,8 @@ static void dump_iterate(DumpState *s, Error **errp)
62547e
                 size -= block->target_end - (s->begin + s->length);
62547e
             }
62547e
         }
62547e
-        write_memory(s, block, s->start, size, &local_err);
62547e
-        if (local_err) {
62547e
-            error_propagate(errp, local_err);
62547e
+        write_memory(s, block, s->start, size, errp);
62547e
+        if (*errp) {
62547e
             return;
62547e
         }
62547e
 
62547e
@@ -669,11 +656,10 @@ static void dump_iterate(DumpState *s, Error **errp)
62547e
 
62547e
 static void create_vmcore(DumpState *s, Error **errp)
62547e
 {
62547e
-    Error *local_err = NULL;
62547e
+    ERRP_GUARD();
62547e
 
62547e
-    dump_begin(s, &local_err);
62547e
-    if (local_err) {
62547e
-        error_propagate(errp, local_err);
62547e
+    dump_begin(s, errp);
62547e
+    if (*errp) {
62547e
         return;
62547e
     }
62547e
 
62547e
@@ -810,6 +796,7 @@ static bool note_name_equal(DumpState *s,
62547e
 /* write common header, sub header and elf note to vmcore */
62547e
 static void create_header32(DumpState *s, Error **errp)
62547e
 {
62547e
+    ERRP_GUARD();
62547e
     DiskDumpHeader32 *dh = NULL;
62547e
     KdumpSubHeader32 *kh = NULL;
62547e
     size_t size;
62547e
@@ -818,7 +805,6 @@ static void create_header32(DumpState *s, Error **errp)
62547e
     uint32_t bitmap_blocks;
62547e
     uint32_t status = 0;
62547e
     uint64_t offset_note;
62547e
-    Error *local_err = NULL;
62547e
 
62547e
     /* write common header, the version of kdump-compressed format is 6th */
62547e
     size = sizeof(DiskDumpHeader32);
62547e
@@ -894,9 +880,8 @@ static void create_header32(DumpState *s, Error **errp)
62547e
     s->note_buf_offset = 0;
62547e
 
62547e
     /* use s->note_buf to store notes temporarily */
62547e
-    write_elf32_notes(buf_write_note, s, &local_err);
62547e
-    if (local_err) {
62547e
-        error_propagate(errp, local_err);
62547e
+    write_elf32_notes(buf_write_note, s, errp);
62547e
+    if (*errp) {
62547e
         goto out;
62547e
     }
62547e
     if (write_buffer(s->fd, offset_note, s->note_buf,
62547e
@@ -922,6 +907,7 @@ out:
62547e
 /* write common header, sub header and elf note to vmcore */
62547e
 static void create_header64(DumpState *s, Error **errp)
62547e
 {
62547e
+    ERRP_GUARD();
62547e
     DiskDumpHeader64 *dh = NULL;
62547e
     KdumpSubHeader64 *kh = NULL;
62547e
     size_t size;
62547e
@@ -930,7 +916,6 @@ static void create_header64(DumpState *s, Error **errp)
62547e
     uint32_t bitmap_blocks;
62547e
     uint32_t status = 0;
62547e
     uint64_t offset_note;
62547e
-    Error *local_err = NULL;
62547e
 
62547e
     /* write common header, the version of kdump-compressed format is 6th */
62547e
     size = sizeof(DiskDumpHeader64);
62547e
@@ -1006,9 +991,8 @@ static void create_header64(DumpState *s, Error **errp)
62547e
     s->note_buf_offset = 0;
62547e
 
62547e
     /* use s->note_buf to store notes temporarily */
62547e
-    write_elf64_notes(buf_write_note, s, &local_err);
62547e
-    if (local_err) {
62547e
-        error_propagate(errp, local_err);
62547e
+    write_elf64_notes(buf_write_note, s, errp);
62547e
+    if (*errp) {
62547e
         goto out;
62547e
     }
62547e
 
62547e
@@ -1472,8 +1456,8 @@ out:
62547e
 
62547e
 static void create_kdump_vmcore(DumpState *s, Error **errp)
62547e
 {
62547e
+    ERRP_GUARD();
62547e
     int ret;
62547e
-    Error *local_err = NULL;
62547e
 
62547e
     /*
62547e
      * the kdump-compressed format is:
62547e
@@ -1503,21 +1487,18 @@ static void create_kdump_vmcore(DumpState *s, Error **errp)
62547e
         return;
62547e
     }
62547e
 
62547e
-    write_dump_header(s, &local_err);
62547e
-    if (local_err) {
62547e
-        error_propagate(errp, local_err);
62547e
+    write_dump_header(s, errp);
62547e
+    if (*errp) {
62547e
         return;
62547e
     }
62547e
 
62547e
-    write_dump_bitmap(s, &local_err);
62547e
-    if (local_err) {
62547e
-        error_propagate(errp, local_err);
62547e
+    write_dump_bitmap(s, errp);
62547e
+    if (*errp) {
62547e
         return;
62547e
     }
62547e
 
62547e
-    write_dump_pages(s, &local_err);
62547e
-    if (local_err) {
62547e
-        error_propagate(errp, local_err);
62547e
+    write_dump_pages(s, errp);
62547e
+    if (*errp) {
62547e
         return;
62547e
     }
62547e
 
62547e
@@ -1647,10 +1628,10 @@ static void dump_init(DumpState *s, int fd, bool has_format,
62547e
                       DumpGuestMemoryFormat format, bool paging, bool has_filter,
62547e
                       int64_t begin, int64_t length, Error **errp)
62547e
 {
62547e
+    ERRP_GUARD();
62547e
     VMCoreInfoState *vmci = vmcoreinfo_find();
62547e
     CPUState *cpu;
62547e
     int nr_cpus;
62547e
-    Error *err = NULL;
62547e
     int ret;
62547e
 
62547e
     s->has_format = has_format;
62547e
@@ -1769,9 +1750,8 @@ static void dump_init(DumpState *s, int fd, bool has_format,
62547e
 
62547e
     /* get memory mapping */
62547e
     if (paging) {
62547e
-        qemu_get_guest_memory_mapping(&s->list, &s->guest_phys_blocks, &err;;
62547e
-        if (err != NULL) {
62547e
-            error_propagate(errp, err);
62547e
+        qemu_get_guest_memory_mapping(&s->list, &s->guest_phys_blocks, errp);
62547e
+        if (*errp) {
62547e
             goto cleanup;
62547e
         }
62547e
     } else {
62547e
@@ -1870,33 +1850,32 @@ cleanup:
62547e
 /* this operation might be time consuming. */
62547e
 static void dump_process(DumpState *s, Error **errp)
62547e
 {
62547e
-    Error *local_err = NULL;
62547e
+    ERRP_GUARD();
62547e
     DumpQueryResult *result = NULL;
62547e
 
62547e
     if (s->has_format && s->format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP) {
62547e
 #ifdef TARGET_X86_64
62547e
-        create_win_dump(s, &local_err);
62547e
+        create_win_dump(s, errp);
62547e
 #endif
62547e
     } else if (s->has_format && s->format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
62547e
-        create_kdump_vmcore(s, &local_err);
62547e
+        create_kdump_vmcore(s, errp);
62547e
     } else {
62547e
-        create_vmcore(s, &local_err);
62547e
+        create_vmcore(s, errp);
62547e
     }
62547e
 
62547e
     /* make sure status is written after written_size updates */
62547e
     smp_wmb();
62547e
     qatomic_set(&s->status,
62547e
-               (local_err ? DUMP_STATUS_FAILED : DUMP_STATUS_COMPLETED));
62547e
+               (*errp ? DUMP_STATUS_FAILED : DUMP_STATUS_COMPLETED));
62547e
 
62547e
     /* send DUMP_COMPLETED message (unconditionally) */
62547e
     result = qmp_query_dump(NULL);
62547e
     /* should never fail */
62547e
     assert(result);
62547e
-    qapi_event_send_dump_completed(result, !!local_err, (local_err ?
62547e
-                                   error_get_pretty(local_err) : NULL));
62547e
+    qapi_event_send_dump_completed(result, !!*errp, (*errp ?
62547e
+                                                     error_get_pretty(*errp) : NULL));
62547e
     qapi_free_DumpQueryResult(result);
62547e
 
62547e
-    error_propagate(errp, local_err);
62547e
     dump_cleanup(s);
62547e
 }
62547e
 
62547e
@@ -1925,10 +1904,10 @@ void qmp_dump_guest_memory(bool paging, const char *file,
62547e
                            int64_t length, bool has_format,
62547e
                            DumpGuestMemoryFormat format, Error **errp)
62547e
 {
62547e
+    ERRP_GUARD();
62547e
     const char *p;
62547e
     int fd = -1;
62547e
     DumpState *s;
62547e
-    Error *local_err = NULL;
62547e
     bool detach_p = false;
62547e
 
62547e
     if (runstate_check(RUN_STATE_INMIGRATE)) {
62547e
@@ -2028,9 +2007,8 @@ void qmp_dump_guest_memory(bool paging, const char *file,
62547e
     dump_state_prepare(s);
62547e
 
62547e
     dump_init(s, fd, has_format, format, paging, has_begin,
62547e
-              begin, length, &local_err);
62547e
-    if (local_err) {
62547e
-        error_propagate(errp, local_err);
62547e
+              begin, length, errp);
62547e
+    if (*errp) {
62547e
         qatomic_set(&s->status, DUMP_STATUS_FAILED);
62547e
         return;
62547e
     }
62547e
-- 
62547e
2.37.3
62547e