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