Skip to contentSkip to conversation

dump: Minor fixes & improvements

PATCH Superseded Markus Armbruster 4 participants 15 messages Oct 30, 2023 – Oct 31, 2023

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover dump: Minor fixes & improvements

Markus Armbruster (5):
dump: Rename qmp_dump_guest_memory() parameter to match QAPI schema
dump: Fix g_array_unref(NULL) in dump-guest-memory
dump: Recognize "fd:" protocols on Windows hosts
dump: Improve some dump-guest-memory error messages
dump: Drop redundant check for empty dump

 dump/dump.c | 59 +++++++++++++++--------------------------------------
 1 file changed, 16 insertions(+), 43 deletions(-)
Signature · 1 line
2.41.0
#1 lore raw

PATCH 1/5 dump: Rename qmp_dump_guest_memory() parameter to match QAPI schema

The name of the second parameter differs between QAPI schema and C
implementation: it's @protocol in the former and @file in the latter. Potentially confusing. Change the C implementation to match the QAPI schema.

---
 dump/dump.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
dump/dump.c +7 -6
index d355ada62e..a1fad17f9c 100644
@@ -2061,11 +2061,12 @@ DumpQueryResult *qmp_query_dump(Error **errp)
     return result;
 }
 
-void qmp_dump_guest_memory(bool paging, const char *file,
+void qmp_dump_guest_memory(bool paging, const char *protocol,
                            bool has_detach, bool detach,
-                           bool has_begin, int64_t begin, bool has_length,
-                           int64_t length, bool has_format,
-                           DumpGuestMemoryFormat format, Error **errp)
+                           bool has_begin, int64_t begin,
+                           bool has_length, int64_t length,
+                           bool has_format, DumpGuestMemoryFormat format,
+                           Error **errp)
 {
     ERRP_GUARD();
     const char *p;
@@ -2128,7 +2129,7 @@ void qmp_dump_guest_memory(bool paging, const char *file,
     }
 
 #if !defined(WIN32)
-    if (strstart(file, "fd:", &p)) {
+    if (strstart(protocol, "fd:", &p)) {
         fd = monitor_get_fd(monitor_cur(), p, errp);
         if (fd == -1) {
             return;
@@ -2136,7 +2137,7 @@ void qmp_dump_guest_memory(bool paging, const char *file,
     }
 #endif
 
-    if  (strstart(file, "file:", &p)) {
+    if  (strstart(protocol, "file:", &p)) {
         fd = qemu_open_old(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR);
         if (fd < 0) {
             error_setg_file_open(errp, errno, p);
Signature · 1 line
2.41.0
Reviewed-by on v1 1/5
#2 lore raw

Re: [PATCH 1/5] dump: Rename qmp_dump_guest_memory() parameter to match QAPI schema

Quoted 7 lines · Markus Armbruster

The name of the second parameter differs between QAPI schema and C
implementation: it's @protocol in the former and @file in the latter. Potentially confusing. Change the C implementation to match the QAPI schema.

Quoted 46 lines
---
 dump/dump.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
dump/dump.c +7 -7
index d355ada62e..a1fad17f9c 100644
@@ -2061,11 +2061,12 @@ DumpQueryResult *qmp_query_dump(Error **errp)
     return result;
 }
 
-void qmp_dump_guest_memory(bool paging, const char *file,
+void qmp_dump_guest_memory(bool paging, const char *protocol,
                            bool has_detach, bool detach,
-                           bool has_begin, int64_t begin, bool has_length,
-                           int64_t length, bool has_format,
-                           DumpGuestMemoryFormat format, Error **errp)
+                           bool has_begin, int64_t begin,
+                           bool has_length, int64_t length,
+                           bool has_format, DumpGuestMemoryFormat format,
+                           Error **errp)
 {
     ERRP_GUARD();
     const char *p;
@@ -2128,7 +2129,7 @@ void qmp_dump_guest_memory(bool paging, const char *file,
     }
 
 #if !defined(WIN32)
-    if (strstart(file, "fd:", &p)) {
+    if (strstart(protocol, "fd:", &p)) {
         fd = monitor_get_fd(monitor_cur(), p, errp);
         if (fd == -1) {
             return;
@@ -2136,7 +2137,7 @@ void qmp_dump_guest_memory(bool paging, const char *file,
     }
 #endif
 
-    if  (strstart(file, "file:", &p)) {
+    if  (strstart(protocol, "file:", &p)) {
         fd = qemu_open_old(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR);
         if (fd < 0) {
             error_setg_file_open(errp, errno, p);
--

2.41.0

#3 lore raw

PATCH 2/5 dump: Fix g_array_unref(NULL) in dump-guest-memory

In reply to Markus Armbruster

When dump_init()'s check for non-zero @length fails, dump_cleanup() passes null s->string_table_buf to g_array_unref(), which spews "GLib:
g_array_unref: assertion 'array' failed" to stderr.

Guard the g_array_unref().

---
 dump/dump.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
dump/dump.c +3 -1
index a1fad17f9c..d8ea364af2 100644
@@ -100,7 +100,9 @@ static int dump_cleanup(DumpState *s)
     memory_mapping_list_free(&s->list);
     close(s->fd);
     g_free(s->guest_note);
-    g_array_unref(s->string_table_buf);
+    if (s->string_table_buf) {
+        g_array_unref(s->string_table_buf);
+    }
     s->guest_note = NULL;
     if (s->resume) {
         if (s->detached) {
Signature · 1 line
2.41.0
Reviewed-by on v1 2/5
#4 lore raw

Re: [PATCH 2/5] dump: Fix g_array_unref(NULL) in dump-guest-memory

Hi

Quoted 8 lines · Markus Armbruster

When dump_init()'s check for non-zero @length fails, dump_cleanup() passes null s->string_table_buf to g_array_unref(), which spews "GLib:
g_array_unref: assertion 'array' failed" to stderr.

Guard the g_array_unref().

Quoted 9 lines
---
 dump/dump.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
dump/dump.c +0 -0
index a1fad17f9c..d8ea364af2 100644

dump/dump.c static int dump_cleanup(DumpState *s) · line 105

Quoted 7 lines
dump/dump.c +3 -1
@@ -100,7 +100,9 @@ static int dump_cleanup(DumpState *s)
     memory_mapping_list_free(&s->list);
     close(s->fd);
     g_free(s->guest_note);
-    g_array_unref(s->string_table_buf);
+    if (s->string_table_buf) {
+        g_array_unref(s->string_table_buf);
+    }

or:
g_clear_pointer(&s->string_table_buf, g_array_unref)

Quoted 6 lines
dump/dump.c +0 -1
     s->guest_note = NULL;
     if (s->resume) {
         if (s->detached) {
--

2.41.0

#5 lore raw

Re: [PATCH 2/5] dump: Fix g_array_unref(NULL) in dump-guest-memory

Quoted 18 lines · Marc-André Lureau

Hi

Quoted 8 lines · Markus Armbruster

When dump_init()'s check for non-zero @length fails, dump_cleanup() passes null s->string_table_buf to g_array_unref(), which spews "GLib:
g_array_unref: assertion 'array' failed" to stderr.

Guard the g_array_unref().

Quoted 4 lines
---
 dump/dump.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 15 lines

dump/dump.c static int dump_cleanup(DumpState *s) · line 105

Quoted 12 lines
dump/dump.c +3 -1
index a1fad17f9c..d8ea364af2 100644
@@ -100,7 +100,9 @@ static int dump_cleanup(DumpState *s)
     memory_mapping_list_free(&s->list);
     close(s->fd);
     g_free(s->guest_note);
-    g_array_unref(s->string_table_buf);
+    if (s->string_table_buf) {
+        g_array_unref(s->string_table_buf);
+    }

or:
g_clear_pointer(&s->string_table_buf, g_array_unref)

Since dump_cleanup() doesn't clear any of the other members of @s, I'll stick to g_array_unref() for consistency and simplicity.

Quoted 6 lines
Quoted 6 lines
dump/dump.c +0 -1
     s->guest_note = NULL;
     if (s->resume) {
         if (s->detached) {
--

2.41.0

Thanks!

#6 lore raw

Re: [PATCH 2/5] dump: Fix g_array_unref(NULL) in dump-guest-memory

Quoted 36 lines · Markus Armbruster
Quoted 18 lines · Marc-André Lureau

Hi

Quoted 8 lines · Markus Armbruster

When dump_init()'s check for non-zero @length fails, dump_cleanup() passes null s->string_table_buf to g_array_unref(), which spews "GLib:
g_array_unref: assertion 'array' failed" to stderr.

Guard the g_array_unref().

Quoted 4 lines
---
 dump/dump.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 15 lines

dump/dump.c static int dump_cleanup(DumpState *s) · line 105

Quoted 12 lines
dump/dump.c +3 -1
index a1fad17f9c..d8ea364af2 100644
@@ -100,7 +100,9 @@ static int dump_cleanup(DumpState *s)
     memory_mapping_list_free(&s->list);
     close(s->fd);
     g_free(s->guest_note);
-    g_array_unref(s->string_table_buf);
+    if (s->string_table_buf) {
+        g_array_unref(s->string_table_buf);
+    }

or:
g_clear_pointer(&s->string_table_buf, g_array_unref)

Quoted 2 lines

Since dump_cleanup() doesn't clear any of the other members of @s, I'll stick to g_array_unref() for consistency and simplicity.

Wait! You suggest *unconditional*

         g_clear_pointer(&s->string_table_buf, g_array_unref)         

don't you?

Got a preference?

Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
dump/dump.c +0 -1
     s->guest_note = NULL;
     if (s->resume) {
         if (s->detached) {
--

2.41.0

Thanks!

#7 lore raw

Re: [PATCH 2/5] dump: Fix g_array_unref(NULL) in dump-guest-memory

Hi

Quoted 42 lines · Markus Armbruster
Quoted 36 lines · Markus Armbruster
Quoted 18 lines · Marc-André Lureau

Hi

Quoted 8 lines · Markus Armbruster

When dump_init()'s check for non-zero @length fails, dump_cleanup() passes null s->string_table_buf to g_array_unref(), which spews "GLib:
g_array_unref: assertion 'array' failed" to stderr.

Guard the g_array_unref().

Quoted 4 lines
---
 dump/dump.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 15 lines

dump/dump.c static int dump_cleanup(DumpState *s) · line 105

Quoted 12 lines
dump/dump.c +3 -1
index a1fad17f9c..d8ea364af2 100644
@@ -100,7 +100,9 @@ static int dump_cleanup(DumpState *s)
     memory_mapping_list_free(&s->list);
     close(s->fd);
     g_free(s->guest_note);
-    g_array_unref(s->string_table_buf);
+    if (s->string_table_buf) {
+        g_array_unref(s->string_table_buf);
+    }

or:
g_clear_pointer(&s->string_table_buf, g_array_unref)

Quoted 2 lines

Since dump_cleanup() doesn't clear any of the other members of @s, I'll stick to g_array_unref() for consistency and simplicity.

Quoted 7 lines

Wait! You suggest *unconditional*

         g_clear_pointer(&s->string_table_buf, g_array_unref)

don't you?

Got a preference?

Yes, I think it's safe and more future proof in general. Up to you if you respin.

thanks

Quoted 10 lines
Quoted 9 lines
Quoted 7 lines
Quoted 7 lines
dump/dump.c +0 -1
     s->guest_note = NULL;
     if (s->resume) {
         if (s->detached) {
--

2.41.0

Thanks!

#8 lore raw

PATCH 3/5 dump: Recognize "fd:" protocols on Windows hosts

In reply to Markus Armbruster

A few QMP command can work with named file descriptors.

The only way to create a named file descriptor used to be QMP command getfd, which only works on POSIX hosts. Thus, named file descriptors were actually usable only there.

They became usable on Windows hosts when we added QMP command get-win32-socket (commit 4cda177c601 "qmp: add 'get-win32-socket'").

Except in dump-guest-memory, because qmp_dump_guest_memory() compiles its named file descriptor code only #if !defined(WIN32).

Compile it unconditionally, like we do for the other commands supporting them.

---
 dump/dump.c | 2 --
 1 file changed, 2 deletions(-)
dump/dump.c +0 -2
index d8ea364af2..a5e9a06ef1 100644
@@ -2130,14 +2130,12 @@ void qmp_dump_guest_memory(bool paging, const char *protocol,
         return;
     }
 
-#if !defined(WIN32)
     if (strstart(protocol, "fd:", &p)) {
         fd = monitor_get_fd(monitor_cur(), p, errp);
         if (fd == -1) {
             return;
         }
     }
-#endif
 
     if  (strstart(protocol, "file:", &p)) {
         fd = qemu_open_old(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR);
Signature · 1 line
2.41.0
Reviewed-by on v1 3/5
#9 lore raw

Re: [PATCH 3/5] dump: Recognize "fd:" protocols on Windows hosts

Quoted 10 lines · Markus Armbruster

A few QMP command can work with named file descriptors.

The only way to create a named file descriptor used to be QMP command getfd, which only works on POSIX hosts. Thus, named file descriptors were actually usable only there.

They became usable on Windows hosts when we added QMP command get-win32-socket (commit 4cda177c601 "qmp: add 'get-win32-socket'").

Quoted 7 lines

Except in dump-guest-memory, because qmp_dump_guest_memory() compiles its named file descriptor code only #if !defined(WIN32).

Compile it unconditionally, like we do for the other commands supporting them.

Quoted 26 lines
---
 dump/dump.c | 2 --
 1 file changed, 2 deletions(-)
dump/dump.c +0 -3
index d8ea364af2..a5e9a06ef1 100644
@@ -2130,14 +2130,12 @@ void qmp_dump_guest_memory(bool paging, const char *protocol,
         return;
     }
 
-#if !defined(WIN32)
     if (strstart(protocol, "fd:", &p)) {
         fd = monitor_get_fd(monitor_cur(), p, errp);
         if (fd == -1) {
             return;
         }
     }
-#endif
 
     if  (strstart(protocol, "file:", &p)) {
         fd = qemu_open_old(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR);
--

2.41.0

#10 lore raw

PATCH 4/5 dump: Improve some dump-guest-memory error messages

In reply to Markus Armbruster

Zero @length is rejected with "Invalid parameter 'length'". Improve to "Parameter 'length' expects a non-zero length".

@protocol values not starting with "fd:" or "file:" are rejected with "Invalid parameter 'protocol'". Improve to "parameter 'protocol' must start with 'file:' or 'fd:'".

While there, make the conditional checking @protocol a little more obvious.

---
 dump/dump.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
dump/dump.c +7 -9
index a5e9a06ef1..d888e4bd3c 100644
@@ -1812,7 +1812,8 @@ static void dump_init(DumpState *s, int fd, bool has_format,
 
     s->fd = fd;
     if (has_filter && !length) {
-        error_setg(errp, QERR_INVALID_PARAMETER, "length");
+        error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "length",
+                   "a non-zero size");
         goto cleanup;
     }
     s->filter_area_begin = begin;
@@ -2072,7 +2073,7 @@ void qmp_dump_guest_memory(bool paging, const char *protocol,
 {
     ERRP_GUARD();
     const char *p;
-    int fd = -1;
+    int fd;
     DumpState *s;
     bool detach_p = false;
 
@@ -2135,18 +2136,15 @@ void qmp_dump_guest_memory(bool paging, const char *protocol,
         if (fd == -1) {
             return;
         }
-    }
-
-    if  (strstart(protocol, "file:", &p)) {
+    } else if  (strstart(protocol, "file:", &p)) {
         fd = qemu_open_old(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR);
         if (fd < 0) {
             error_setg_file_open(errp, errno, p);
             return;
         }
-    }
-
-    if (fd == -1) {
-        error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
+    } else {
+        error_setg(errp,
+                   "parameter 'protocol' must start with 'file:' or 'fd:'");
         return;
     }
 
Signature · 1 line
2.41.0
#11 lore raw

Re: [PATCH 4/5] dump: Improve some dump-guest-memory error messages

Hi Markus,

Quoted 20 lines · Markus Armbruster

Zero @length is rejected with "Invalid parameter 'length'". Improve to "Parameter 'length' expects a non-zero length".

@protocol values not starting with "fd:" or "file:" are rejected with "Invalid parameter 'protocol'". Improve to "parameter 'protocol' must start with 'file:' or 'fd:'".

While there, make the conditional checking @protocol a little more obvious.

---
  dump/dump.c | 16 +++++++---------
  1 file changed, 7 insertions(+), 9 deletions(-)
dump/dump.c +0 -0
index a5e9a06ef1..d888e4bd3c 100644

dump/dump.c static void dump_init(DumpState *s, int fd, bool has_format, · line 1815

Quoted 5 lines
dump/dump.c +1 -1
@@ -1812,7 +1812,8 @@ static void dump_init(DumpState *s, int fd, bool has_format,
 
      s->fd = fd;
      if (has_filter && !length) {
-        error_setg(errp, QERR_INVALID_PARAMETER, "length");
+        error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "length",

Per commit 4629ed1e98 ("qerror: Finally unused, clean up", 2015):

/*
* These macros will go away, please don't use in new code, ...

Instead we can use:

error_setg(errp, "Parameter '%s' expects %s", "length",

Quoted 3 lines
dump/dump.c +1 -0
+                   "a non-zero size");
          goto cleanup;
      }
#12 lore raw

Re: [PATCH 4/5] dump: Improve some dump-guest-memory error messages

Quoted 29 lines · Philippe Mathieu-Daudé

Hi Markus,

Quoted 19 lines · Markus Armbruster

Zero @length is rejected with "Invalid parameter 'length'". Improve to "Parameter 'length' expects a non-zero length".

@protocol values not starting with "fd:" or "file:" are rejected with "Invalid parameter 'protocol'". Improve to "parameter 'protocol' must start with 'file:' or 'fd:'".

While there, make the conditional checking @protocol a little more obvious.

---
  dump/dump.c | 16 +++++++---------
  1 file changed, 7 insertions(+), 9 deletions(-)
dump/dump.c +0 -0
index a5e9a06ef1..d888e4bd3c 100644

dump/dump.c static void dump_init(DumpState *s, int fd, bool has_format, · line 1814

Quoted 4 lines
dump/dump.c +1 -1
@@ -1812,7 +1812,8 @@ static void dump_init(DumpState *s, int fd, bool has_format,
        s->fd = fd;
      if (has_filter && !length) {
-        error_setg(errp, QERR_INVALID_PARAMETER, "length");
+        error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "length",

Per commit 4629ed1e98 ("qerror: Finally unused, clean up", 2015):

Quoted 6 lines

/*
* These macros will go away, please don't use in new code, ...

Instead we can use:

error_setg(errp, "Parameter '%s' expects %s", "length",

I left this to the next version of your "qapi: Kill 'qapi/qmp/qerror.h' for good" out of laziness. Since you prefer the deed to be done right away, I will in v2.

Quoted 3 lines
Quoted 3 lines
dump/dump.c +1 -0
+                   "a non-zero size");
          goto cleanup;
      }
#13 lore raw

PATCH 5/5 dump: Drop redundant check for empty dump

In reply to Markus Armbruster

dump_init() first computes the size of the dump, taking the filter area into account, and fails if its zero. It then looks for memory in the filter area, and fails if there is none.

This is redundant: if the size of the dump is zero, there is no memory, and vice versa. Delete this check.

---
 dump/dump.c | 26 --------------------------
 1 file changed, 26 deletions(-)
dump/dump.c +0 -26
index d888e4bd3c..03627a4c17 100644
@@ -1674,26 +1674,6 @@ static void create_kdump_vmcore(DumpState *s, Error **errp)
     }
 }
 
-static int validate_start_block(DumpState *s)
-{
-    GuestPhysBlock *block;
-
-    if (!dump_has_filter(s)) {
-        return 0;
-    }
-
-    QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
-        /* This block is out of the range */
-        if (block->target_start >= s->filter_area_begin + s->filter_area_length ||
-            block->target_end <= s->filter_area_begin) {
-            continue;
-        }
-        return 0;
-   }
-
-    return -1;
-}
-
 static void get_max_mapnr(DumpState *s)
 {
     GuestPhysBlock *last_block;
@@ -1842,12 +1822,6 @@ static void dump_init(DumpState *s, int fd, bool has_format,
         goto cleanup;
     }
 
-    /* Is the filter filtering everything? */
-    if (validate_start_block(s) == -1) {
-        error_setg(errp, QERR_INVALID_PARAMETER, "begin");
-        goto cleanup;
-    }
-
     /* get dump info: endian, class and architecture.
      * If the target architecture is not supported, cpu_get_dump_info() will
      * return -1.
Signature · 1 line
2.41.0
Reviewed-by on v1 5/5
#14 lore raw

Re: [PATCH 5/5] dump: Drop redundant check for empty dump

Quoted 9 lines · Markus Armbruster

dump_init() first computes the size of the dump, taking the filter area into account, and fails if its zero. It then looks for memory in the filter area, and fails if there is none.

This is redundant: if the size of the dump is zero, there is no memory, and vice versa. Delete this check.

Quoted 52 lines
---
 dump/dump.c | 26 --------------------------
 1 file changed, 26 deletions(-)
dump/dump.c +0 -27
index d888e4bd3c..03627a4c17 100644
@@ -1674,26 +1674,6 @@ static void create_kdump_vmcore(DumpState *s, Error **errp)
     }
 }
 
-static int validate_start_block(DumpState *s)
-{
-    GuestPhysBlock *block;
-
-    if (!dump_has_filter(s)) {
-        return 0;
-    }
-
-    QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
-        /* This block is out of the range */
-        if (block->target_start >= s->filter_area_begin + s->filter_area_length ||
-            block->target_end <= s->filter_area_begin) {
-            continue;
-        }
-        return 0;
-   }
-
-    return -1;
-}
-
 static void get_max_mapnr(DumpState *s)
 {
     GuestPhysBlock *last_block;
@@ -1842,12 +1822,6 @@ static void dump_init(DumpState *s, int fd, bool has_format,
         goto cleanup;
     }
 
-    /* Is the filter filtering everything? */
-    if (validate_start_block(s) == -1) {
-        error_setg(errp, QERR_INVALID_PARAMETER, "begin");
-        goto cleanup;
-    }
-
     /* get dump info: endian, class and architecture.
      * If the target architecture is not supported, cpu_get_dump_info() will
      * return -1.
--

2.41.0

Signature · 1 line
Marc-André Lureau