Skip to contentSkip to conversation

target/i386: Revert monitor_puts() in do_inject_x86_mce()

PATCH Applied Tao Su 6 participants 7 messages Mar 20, 2024 – Mar 21, 2024

lore
Share & tools
lore

Tao Su

author
#0 lore raw

PATCH v2 1/1 target/i386: Revert monitor_puts() in do_inject_x86_mce()

monitor_puts() doesn't check the monitor pointer, but do_inject_x86_mce() may have a parameter with NULL monitor pointer. Revert monitor_puts() in do_inject_x86_mce() to fix, then the fact that we send the same message to monitor and log is again more obvious.

Fixes bf0c50d4aa85 (monitor: expose monitor_puts to rest of code)

---
v1 -> v2:
- Instead revert the broken part of commit bf0c50d4aa85
- Add Markus's Reviewed-by

v1:
- https://lore.kernel.org/all/[email protected]/

---
 target/i386/helper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/i386/helper.c +1 -1
index 2070dd0dda..23ccb23a5b 100644
@@ -430,7 +430,7 @@ static void do_inject_x86_mce(CPUState *cs, run_on_cpu_data data)
         if (need_reset) {
             emit_guest_memory_failure(MEMORY_FAILURE_ACTION_RESET, ar,
                                       recursive);
-            monitor_puts(params->mon, msg);
+            monitor_printf(params->mon, "%s", msg);
             qemu_log_mask(CPU_LOG_RESET, "%s\n", msg);
             qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
             return;

base-commit: c62d54d0a8067ffb3d5b909276f7296d7df33fa7

Signature · 1 line
2.34.1
#2 lore raw
In reply to Tao Su
Quoted 4 lines · Tao Su

monitor_puts() doesn't check the monitor pointer, but do_inject_x86_mce() may have a parameter with NULL monitor pointer. Revert monitor_puts() in do_inject_x86_mce() to fix, then the fact that we send the same message to monitor and log is again more obvious.

Yikes, why do we have such a horrible trap-door in our monitor output APIs.

Isn't the right fix here to make 'monitor_puts' check for NULL & be a no-op, in the same way 'monitor_printf' does, so the APIs have consistent behaviour.

Quoted 30 lines
Fixes bf0c50d4aa85 (monitor: expose monitor_puts to rest of code)

---
v1 -> v2:
- Instead revert the broken part of commit bf0c50d4aa85
- Add Markus's Reviewed-by

v1:
- https://lore.kernel.org/all/[email protected]/

---
 target/i386/helper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/i386/helper.c +1 -1
index 2070dd0dda..23ccb23a5b 100644
@@ -430,7 +430,7 @@ static void do_inject_x86_mce(CPUState *cs, run_on_cpu_data data)
         if (need_reset) {
             emit_guest_memory_failure(MEMORY_FAILURE_ACTION_RESET, ar,
                                       recursive);
-            monitor_puts(params->mon, msg);
+            monitor_printf(params->mon, "%s", msg);
             qemu_log_mask(CPU_LOG_RESET, "%s\n", msg);
             qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
             return;
Quoted 5 lines

base-commit: c62d54d0a8067ffb3d5b909276f7296d7df33fa7

Signature · 1 line
2.34.1

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#3 lore raw
Quoted 7 lines · Daniel P. Berrangé
Quoted 4 lines · Tao Su

monitor_puts() doesn't check the monitor pointer, but do_inject_x86_mce() may have a parameter with NULL monitor pointer. Revert monitor_puts() in do_inject_x86_mce() to fix, then the fact that we send the same message to monitor and log is again more obvious.

Quoted 6 lines

Yikes, why do we have such a horrible trap-door in our monitor output APIs.

Isn't the right fix here to make 'monitor_puts' check for NULL & be a no-op, in the same way 'monitor_printf' does, so the APIs have consistent behaviour.

The other difference between monitor_puts(mon, s) and monitor_printf(mon, "%s", s)
is that the latter will return an error if the monitor is QMP, whereas the former will go ahead and print the message anyway. That one is awkward to resolve, because the mechanism the QMP monitor uses to print the JSON in qmp_send_response() is to call monitor_puts()...

-- PMM

#4 lore raw
Quoted 15 lines · Peter Maydell
Quoted 7 lines · Daniel P. Berrangé
Quoted 4 lines · Tao Su

monitor_puts() doesn't check the monitor pointer, but do_inject_x86_mce() may have a parameter with NULL monitor pointer. Revert monitor_puts() in do_inject_x86_mce() to fix, then the fact that we send the same message to monitor and log is again more obvious.

Quoted 6 lines

Yikes, why do we have such a horrible trap-door in our monitor output APIs.

Isn't the right fix here to make 'monitor_puts' check for NULL & be a no-op, in the same way 'monitor_printf' does, so the APIs have consistent behaviour.

Quoted 6 lines

The other difference between monitor_puts(mon, s) and monitor_printf(mon, "%s", s)
is that the latter will return an error if the monitor is QMP, whereas the former will go ahead and print the message anyway. That one is awkward to resolve, because the mechanism the QMP monitor uses to print the JSON in qmp_send_response() is to call monitor_puts()...

We need a low-level function to send to a monitor, be it HMP or QMP:
monitor_puts().

We need a high-level function to format JSON and send it to QMP:
qmp_send_response().

We need a high-level functions to format text and send it to HMP:
monitor_printf(), ...

Trouble is the first and the last one are deceptively named. The names suggest monitor_printf() is to monitor_puts() what printf() is to puts(). Not true.

Naming the functions that expect an HMP monitor hmp_FOO() would make more sense. Renaming them now would be quite some churn, though.

#5 lore raw
Quoted 33 lines · Markus Armbruster
Quoted 15 lines · Peter Maydell
Quoted 7 lines · Daniel P. Berrangé
Quoted 4 lines · Tao Su

monitor_puts() doesn't check the monitor pointer, but do_inject_x86_mce() may have a parameter with NULL monitor pointer. Revert monitor_puts() in do_inject_x86_mce() to fix, then the fact that we send the same message to monitor and log is again more obvious.

Quoted 6 lines

Yikes, why do we have such a horrible trap-door in our monitor output APIs.

Isn't the right fix here to make 'monitor_puts' check for NULL & be a no-op, in the same way 'monitor_printf' does, so the APIs have consistent behaviour.

Quoted 6 lines

The other difference between monitor_puts(mon, s) and monitor_printf(mon, "%s", s)
is that the latter will return an error if the monitor is QMP, whereas the former will go ahead and print the message anyway. That one is awkward to resolve, because the mechanism the QMP monitor uses to print the JSON in qmp_send_response() is to call monitor_puts()...

We need a low-level function to send to a monitor, be it HMP or QMP:
monitor_puts().

We need a high-level function to format JSON and send it to QMP:
qmp_send_response().

We need a high-level functions to format text and send it to HMP:
monitor_printf(), ...

Quoted 6 lines

Trouble is the first and the last one are deceptively named. The names suggest monitor_printf() is to monitor_puts() what printf() is to puts(). Not true.

Naming the functions that expect an HMP monitor hmp_FOO() would make more sense. Renaming them now would be quite some churn, though.

How about a simpler alternative.

* Rename monitor_puts to monitor_puts_internal and it is in monitor-internal.h

* Change low level users (whcih are all inside monitor/) to use monitor_puts_internal

* Introduce a new monitors_puts, which is to monitor_printf what puts() is to printf()

Most (all?) usage outside monitor/ appears to be HMP only.

eg this patch:

include/monitor/monitor.h +0 -1
index 965f5d5450..8e5d0cc71c 100644
@@ -40,7 +40,6 @@ void monitor_flush(Monitor *mon);
 int monitor_set_cpu(Monitor *mon, int cpu_index);
 int monitor_get_cpu_index(Monitor *mon);
 
-int monitor_puts_locked(Monitor *mon, const char *str);
 void monitor_flush_locked(Monitor *mon);
 
 void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, uint64_t size, Error **errp);
monitor/monitor-internal.h +3 -0
index 252de85681..972e7f96d9 100644
@@ -188,4 +188,7 @@ int get_monitor_def(Monitor *mon, int64_t *pval, const char *name);
 void handle_hmp_command(MonitorHMP *mon, const char *cmdline);
 int hmp_compare_cmd(const char *name, const char *list);
 
+int monitor_puts_internal(Monitor *mon, const char *str);
+int monitor_puts_locked(Monitor *mon, const char *str);
+
 #endif
monitor/monitor.c +15 -2
index 01ede1babd..c0ec5bc03e 100644
@@ -206,12 +206,25 @@ int monitor_puts_locked(Monitor *mon, const char *str)
     return i;
 }
 
-int monitor_puts(Monitor *mon, const char *str)
+int monitor_puts_internal(Monitor *mon, const char *str)
 {
     QEMU_LOCK_GUARD(&mon->mon_lock);
     return monitor_puts_locked(mon, str);
 }
 
+int monitor_puts(Monitor *mon, const char *str)
+{
+    if (!mon) {
+        return -1;
+    }
+
+    if (monitor_is_qmp(mon)) {
+        return -1;
+    }
+
+    return monitor_puts_internal(mon, str);
+}
+
 int monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
 {
     char *buf;
@@ -226,7 +239,7 @@ int monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
     }
 
     buf = g_strdup_vprintf(fmt, ap);
-    n = monitor_puts(mon, buf);
+    n = monitor_puts_internal(mon, buf);
     g_free(buf);
     return n;
 }
monitor/qmp.c +1 -1
index a239945e8d..4d848ee91c 100644
@@ -139,7 +139,7 @@ void qmp_send_response(MonitorQMP *mon, const QDict *rsp)
     trace_monitor_qmp_respond(mon, json->str);
 
     g_string_append_c(json, '\n');
-    monitor_puts(&mon->common, json->str);
+    monitor_puts_internal(&mon->common, json->str);
 
     g_string_free(json, true);
 }

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#6 lore raw
In reply to Tao Su

20.03.2024 11:36, Tao Su :

Quoted 9 lines

monitor_puts() doesn't check the monitor pointer, but do_inject_x86_mce() may have a parameter with NULL monitor pointer. Revert monitor_puts() in do_inject_x86_mce() to fix, then the fact that we send the same message to monitor and log is again more obvious.

Fixes bf0c50d4aa85 (monitor: expose monitor_puts to rest of code)

Smells like a -stable material, is it not?

Thanks,

/mjt