Skip to contentSkip to conversation

Small fixes for qga

PATCH In review Daniel Xu 3 participants 6 messages Oct 1, 2023 – Oct 13, 2023

lore
Share & tools
lore

Daniel Xu

author
#0 lore raw

cover Small fixes for qga

These are two small fixes that fell out of [0]. Since we are not moving forward with that patchset, I thought it would be good to at least send the fixes that came out of it.

See commits for more details.

[0]: https://lore.kernel.org/qemu-devel/[email protected]/

Daniel Xu (2):
qga: Fix memory leak when output stream is unused
qapi: qga: Clarify when out-data and err-data are populated

 qga/commands.c       | 4 ++--
 qga/qapi-schema.json | 8 +++++---
 2 files changed, 7 insertions(+), 5 deletions(-)
Signature · 1 line
2.42.0
author
#1 lore raw

PATCH 1/2 qga: Fix memory leak when output stream is unused

If capture-output is requested but one of the channels goes unused (eg. we attempt to capture stderr but the command never writes to stderr), we can leak memory.

guest_exec_output_watch() is (from what I understand) unconditionally called for both streams if output capture is requested. The first call will always pass the p->size == p->length check b/c both values are
0. Then GUEST_EXEC_IO_SIZE bytes will be allocated for the stream.

But when we reap the exited process there's a gei->err.length > 0 check to actually free the buffer. Which does not get run if the command doesn't write to the stream.

Fix by making free() unconditional.

---
 qga/commands.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
qga/commands.c +2 -2
index 09c683e263..ce172edd2d 100644
@@ -206,15 +206,15 @@ GuestExecStatus *qmp_guest_exec_status(int64_t pid, Error **errp)
 #endif
         if (gei->out.length > 0) {
             ges->out_data = g_base64_encode(gei->out.data, gei->out.length);
-            g_free(gei->out.data);
             ges->has_out_truncated = gei->out.truncated;
         }
+        g_free(gei->out.data);
 
         if (gei->err.length > 0) {
             ges->err_data = g_base64_encode(gei->err.data, gei->err.length);
-            g_free(gei->err.data);
             ges->has_err_truncated = gei->err.truncated;
         }
+        g_free(gei->err.data);
 
         QTAILQ_REMOVE(&guest_exec_state.processes, gei, next);
         g_free(gei);
Signature · 1 line
2.42.0
author
#2 lore raw

PATCH 2/2 qapi: qga: Clarify when out-data and err-data are populated

In reply to Daniel Xu

If output is being captured for a guest-exec invocation, the out-data and err-data fields of guest-exec-status are only populated after the process is reaped. This is somewhat counter intuitive and too late to change. Thus, it would be good to document the behavior.

---
 qga/qapi-schema.json | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
qga/qapi-schema.json +5 -3
index b720dd4379..876e2a8ea8 100644
@@ -1220,11 +1220,13 @@
 # @signal: signal number (linux) or unhandled exception code (windows)
 #     if the process was abnormally terminated.
 #
-# @out-data: base64-encoded stdout of the process
+# @out-data: base64-encoded stdout of the process. This field will only
+#     be populated after the process exits.
 #
-# @err-data: base64-encoded stderr of the process Note: @out-data and
+# @err-data: base64-encoded stderr of the process. Note: @out-data and
 #     @err-data are present only if 'capture-output' was specified for
-#     'guest-exec'
+#     'guest-exec'. This field will only be populated after the process
+#     exits.
 #
 # @out-truncated: true if stdout was not fully captured due to size
 #     limitation.
Signature · 1 line
2.42.0
Reviewed-by on v1 2/2
#3 lore raw

Re: [PATCH 2/2] qapi: qga: Clarify when out-data and err-data are populated

Quoted 35 lines · Daniel Xu

If output is being captured for a guest-exec invocation, the out-data and err-data fields of guest-exec-status are only populated after the process is reaped. This is somewhat counter intuitive and too late to change. Thus, it would be good to document the behavior.

---
 qga/qapi-schema.json | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
qga/qapi-schema.json +5 -4
index b720dd4379..876e2a8ea8 100644
@@ -1220,11 +1220,13 @@
 # @signal: signal number (linux) or unhandled exception code (windows)
 #     if the process was abnormally terminated.
 #
-# @out-data: base64-encoded stdout of the process
+# @out-data: base64-encoded stdout of the process. This field will only
+#     be populated after the process exits.
 #
-# @err-data: base64-encoded stderr of the process Note: @out-data and
+# @err-data: base64-encoded stderr of the process. Note: @out-data and
 #     @err-data are present only if 'capture-output' was specified for
-#     'guest-exec'
+#     'guest-exec'. This field will only be populated after the process
+#     exits.
 #
 # @out-truncated: true if stdout was not fully captured due to size
 #     limitation.
--

2.42.0

Acked-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] qapi: qga: Clarify when out-data and err-data are populated

In reply to Daniel Xu
Quoted 21 lines · Daniel Xu

If output is being captured for a guest-exec invocation, the out-data and err-data fields of guest-exec-status are only populated after the process is reaped. This is somewhat counter intuitive and too late to change. Thus, it would be good to document the behavior.

---
 qga/qapi-schema.json | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
qga/qapi-schema.json +2 -1
index b720dd4379..876e2a8ea8 100644
@@ -1220,11 +1220,13 @@
 # @signal: signal number (linux) or unhandled exception code (windows)
 #     if the process was abnormally terminated.
 #
-# @out-data: base64-encoded stdout of the process
+# @out-data: base64-encoded stdout of the process. This field will only
+#     be populated after the process exits.

qga/qapi-schema.json line 1232

Quoted 10 lines
qga/qapi-schema.json +3 -2
 #
-# @err-data: base64-encoded stderr of the process Note: @out-data and
+# @err-data: base64-encoded stderr of the process. Note: @out-data and
 #     @err-data are present only if 'capture-output' was specified for
-#     'guest-exec'
+#     'guest-exec'. This field will only be populated after the process
+#     exits.
 #
 # @out-truncated: true if stdout was not fully captured due to size
 #     limitation.

Two spaces between sentences for consistency, please.

With that:

#5 lore raw
In reply to Daniel Xu

merged

Quoted 21 lines · Daniel Xu

These are two small fixes that fell out of [0]. Since we are not moving forward with that patchset, I thought it would be good to at least send the fixes that came out of it.

See commits for more details.

[0]:
https://lore.kernel.org/qemu-devel/[email protected]/

Daniel Xu (2):
qga: Fix memory leak when output stream is unused
qapi: qga: Clarify when out-data and err-data are populated

 qga/commands.c       | 4 ++--
 qga/qapi-schema.json | 8 +++++---
 2 files changed, 7 insertions(+), 5 deletions(-)

--
2.42.0