monitor: fix dangling CPU pointer
PATCH 1/1 monitor: fix dangling CPU pointer
If a CPU selected with the "cpu" command is hot-unplugged then "info cpus" causes QEMU to exit:
(qemu) device_del cpu1
(qemu) info cpus
qemu:qemu_cpu_kick_thread: No such process
This happens because "cpu" stores the pointer to the selected CPU into the monitor structure. When the CPU is hot-unplugged, we end up with a dangling pointer. The "info cpus" command then does:
hmp_info_cpus()
monitor_get_cpu_index()
mon_get_cpu()
cpu_synchronize_state() <--- called with dangling pointer
This could cause a QEMU crash as well.
This patch switches the monitor to store the QOM path instead of a pointer to the current CPU. The path is then resolved when needed. If the resolution fails, we assume that the CPU was removed and the path is resetted to the default (ie, path of first_cpu).
Note that the resolution should really return a CPU object, otherwise we have a bug. This is achieved by relying on object_resolve_path() and CPU() instead of calling object_resolve_path_type(path, TYPE_CPU).
--- monitor.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-)
monitor.c +20 -5
@@ -200,7 +200,7 @@ struct Monitor {ReadLineState *rs;MonitorQMP qmp; -CPUState *mon_cpu; +gchar *mon_cpu_path;BlockCompletionFunc *password_completion_cb;void *password_opaque;mon_cmd_t *cmd_table;
@@ -579,6 +579,7 @@ static void monitor_data_init(Monitor *mon)static void monitor_data_destroy (Monitor *mon) { +g_free (mon->mon_cpu_path);qemu_chr_fe_deinit (&mon->chr, false);if (monitor_is_qmp (mon)) {json_message_parser_destroy (&mon->qmp.parser);
@@ -1047,20 +1048,34 @@ int monitor_set_cpu(int cpu_index)if (cpu ==NULL ) {return -1 ; } - cur_mon->mon_cpu = cpu; +g_free (cur_mon->mon_cpu_path); + cur_mon->mon_cpu_path =object_get_canonical_path (OBJECT (cpu));return 0 ; }CPUState *mon_get_cpu (void ) { -if (!cur_mon->mon_cpu) { +CPUState *cpu; + +if (cur_mon->mon_cpu_path) { +Object *obj =object_resolve_path (cur_mon->mon_cpu_path,NULL ); + +if (!obj) { +g_free (cur_mon->mon_cpu_path); + cur_mon->mon_cpu_path =NULL ; + }else { + cpu =CPU (obj); + } + } +if (!cur_mon->mon_cpu_path) {if (!first_cpu) {return NULL ; }monitor_set_cpu (first_cpu->cpu_index); + cpu = first_cpu; } -cpu_synchronize_state (cur_mon->mon_cpu); -return cur_mon->mon_cpu; +cpu_synchronize_state (cpu); +return cpu; }CPUArchState *mon_get_cpu_env (void )
Quoted 23 lines · On Fri, 13 Oct 2017 13:47:51 +0200 Greg Kurz <[email protected]> wrote:
If a CPU selected with the "cpu" command is hot-unplugged then "info cpus" causes QEMU to exit:
(qemu)
device_delcpu1
(qemu) info cpus
qemu:qemu_cpu_kick_thread: No such processThis happens because "cpu" stores the pointer to the selected CPU into the monitor structure. When the CPU is hot-unplugged, we end up with a dangling pointer. The "info cpus" command then does:
hmp_info_cpus()
monitor_get_cpu_index()
mon_get_cpu()
cpu_synchronize_state()<--- called with dangling pointerThis could cause a QEMU crash as well.
This patch switches the monitor to store the QOM path instead of a pointer to the current CPU. The path is then resolved when needed. If the resolution fails, we assume that the CPU was removed and the path is resetted to the default (ie, path of
first_cpu).
Quoted 7 lines
Note that the resolution should really return a CPU object, otherwise we have a bug. This is achieved by relying on
object_resolve_path()andCPU()instead of callingobject_resolve_path_type(path, TYPE_CPU).Suggested-by Igor Mammedov <[email protected]>Signed-off-by Greg Kurz <[email protected]>---
Whoever merges this patch, please add:
Thanks!
Quoted 65 lines
monitor.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-)monitor.c +20
-5@@ -200,7 +200,7 @@ struct Monitor {ReadLineState *rs;MonitorQMP qmp; -CPUState *mon_cpu; +gchar *mon_cpu_path;BlockCompletionFunc *password_completion_cb;void *password_opaque;mon_cmd_t *cmd_table;@@ -579,6 +579,7 @@ static void monitor_data_init(Monitor *mon)static void monitor_data_destroy (Monitor *mon) { +g_free (mon->mon_cpu_path);qemu_chr_fe_deinit (&mon->chr, false);if (monitor_is_qmp (mon)) {json_message_parser_destroy (&mon->qmp.parser);@@ -1047,20 +1048,34 @@ int monitor_set_cpu(int cpu_index)if (cpu ==NULL ) {return -1 ; } - cur_mon->mon_cpu = cpu; +g_free (cur_mon->mon_cpu_path); + cur_mon->mon_cpu_path =object_get_canonical_path (OBJECT (cpu));return 0 ; }CPUState *mon_get_cpu (void ) { -if (!cur_mon->mon_cpu) { +CPUState *cpu; + +if (cur_mon->mon_cpu_path) { +Object *obj =object_resolve_path (cur_mon->mon_cpu_path,NULL ); + +if (!obj) { +g_free (cur_mon->mon_cpu_path); + cur_mon->mon_cpu_path =NULL ; + }else { + cpu =CPU (obj); + } + } +if (!cur_mon->mon_cpu_path) {if (!first_cpu) {return NULL ; }monitor_set_cpu (first_cpu->cpu_index); + cpu = first_cpu; } -cpu_synchronize_state (cur_mon->mon_cpu); -return cur_mon->mon_cpu; +cpu_synchronize_state (cpu); +return cpu; }CPUArchState *mon_get_cpu_env (void )
On Fri, 13 Oct 2017 13:47:51 +0200
Greg Kurz <[email protected]> wrote:
[...]
Quoted 3 lines
Note that the resolution should really return a CPU object, otherwise we have a bug. This is achieved by relying on
object_resolve_path()andCPU()instead of callingobject_resolve_path_type(path, TYPE_CPU).
I'm not sure what you are trying to say here, could you explain a bit more why CPU(object_resolve_path()) is chosen vs object_resolve_path_type(path, TYPE_CPU)
Quoted 40 lines
Suggested-by Igor Mammedov <[email protected]>Signed-off-by Greg Kurz <[email protected]>--- monitor.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-)monitor.c +4
-3@@ -200,7 +200,7 @@ struct Monitor {ReadLineState *rs;MonitorQMP qmp; -CPUState *mon_cpu; +gchar *mon_cpu_path;BlockCompletionFunc *password_completion_cb;void *password_opaque;mon_cmd_t *cmd_table;@@ -579,6 +579,7 @@ static void monitor_data_init(Monitor *mon)static void monitor_data_destroy (Monitor *mon) { +g_free (mon->mon_cpu_path);qemu_chr_fe_deinit (&mon->chr, false);if (monitor_is_qmp (mon)) {json_message_parser_destroy (&mon->qmp.parser);@@ -1047,20 +1048,34 @@ int monitor_set_cpu(int cpu_index)if (cpu ==NULL ) {return -1 ; } - cur_mon->mon_cpu = cpu; +g_free (cur_mon->mon_cpu_path); + cur_mon->mon_cpu_path =object_get_canonical_path (OBJECT (cpu));return 0 ; }CPUState *mon_get_cpu (void ) { -if (!cur_mon->mon_cpu) {
monitor.c int monitor_set_cpu(int cpu_index) · line 1067
Quoted 10 lines
monitor.c +10
-0+CPUState *cpu; + +if (cur_mon->mon_cpu_path) { +Object *obj =object_resolve_path (cur_mon->mon_cpu_path,NULL ); + +if (!obj) { +g_free (cur_mon->mon_cpu_path); + cur_mon->mon_cpu_path =NULL ; + }else { + cpu =CPU (obj);
this potentially might abort if obj couldn't be cast to TYPE_CPU
Quoted 18 lines
monitor.c +6
-2+ } + } +if (!cur_mon->mon_cpu_path) {if (!first_cpu) {return NULL ; }monitor_set_cpu (first_cpu->cpu_index); + cpu = first_cpu; } -cpu_synchronize_state (cur_mon->mon_cpu); -return cur_mon->mon_cpu; +cpu_synchronize_state (cpu); +return cpu; }CPUArchState *mon_get_cpu_env (void )
Quoted 12 lines · On Mon, 16 Oct 2017 10:41:39 +0200 Igor Mammedov <[email protected]> wrote:
On Fri, 13 Oct 2017 13:47:51 +0200
Greg Kurz <[email protected]> wrote:[...]
Quoted 3 lines
Note that the resolution should really return a CPU object, otherwise we have a bug. This is achieved by relying on
object_resolve_path()andCPU()instead of callingobject_resolve_path_type(path, TYPE_CPU).I'm not sure what you are trying to say here, could you explain a bit more why CPU(object_resolve_path()) is chosen vs
object_resolve_path_type(path, TYPE_CPU)
IIUC, if we use object_resolve_path_type(path, TYPE_CPU) and path doesn't point to a CPU object, it will return NULL (see object_resolve_abs_path()) just like if the CPU got hot-unplugged.
My point is that the path we got from object_get_canonical_path() did point to a CPU: if later on this path resolves to an object that isn't a CPU, then some code somewhere used the same QOM path for some unrelated object. I tend to think this is a bug in QEMU and we shouldn't silently ignore it.
Makes sense ?
Quoted 38 lines
Quoted 38 lines
Suggested-by Igor Mammedov <[email protected]>Signed-off-by Greg Kurz <[email protected]>--- monitor.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-)monitor.c +4
-2@@ -200,7 +200,7 @@ struct Monitor {ReadLineState *rs;MonitorQMP qmp; -CPUState *mon_cpu; +gchar *mon_cpu_path;BlockCompletionFunc *password_completion_cb;void *password_opaque;mon_cmd_t *cmd_table;@@ -579,6 +579,7 @@ static void monitor_data_init(Monitor *mon)static void monitor_data_destroy (Monitor *mon) { +g_free (mon->mon_cpu_path);qemu_chr_fe_deinit (&mon->chr, false);if (monitor_is_qmp (mon)) {json_message_parser_destroy (&mon->qmp.parser);@@ -1047,20 +1048,34 @@ int monitor_set_cpu(int cpu_index)if (cpu ==NULL ) {return -1 ; } - cur_mon->mon_cpu = cpu; +g_free (cur_mon->mon_cpu_path); + cur_mon->mon_cpu_path =object_get_canonical_path (OBJECT (cpu));return 0 ; }
Quoted 15 lines
monitor.c int monitor_set_cpu(int cpu_index) · line 1067
Quoted 13 lines
monitor.c +10
-1CPUState *mon_get_cpu (void ) { -if (!cur_mon->mon_cpu) { +CPUState *cpu; + +if (cur_mon->mon_cpu_path) { +Object *obj =object_resolve_path (cur_mon->mon_cpu_path,NULL ); + +if (!obj) { +g_free (cur_mon->mon_cpu_path); + cur_mon->mon_cpu_path =NULL ; + }else { + cpu =CPU (obj);this potentially might abort if obj couldn't be cast to
TYPE_CPU
This is deliberate... is there a case where cur_mon->mon_cpu_path would legitimately point to something that isn't of type TYPE_CPU ?
Quoted 20 lines
Quoted 19 lines
monitor.c +6
-2+ } + } +if (!cur_mon->mon_cpu_path) {if (!first_cpu) {return NULL ; }monitor_set_cpu (first_cpu->cpu_index); + cpu = first_cpu; } -cpu_synchronize_state (cur_mon->mon_cpu); -return cur_mon->mon_cpu; +cpu_synchronize_state (cpu); +return cpu; }CPUArchState *mon_get_cpu_env (void )
Quoted 20 lines · On Mon, 16 Oct 2017 13:24:20 +0200 Greg Kurz <[email protected]> wrote:
Quoted 12 lines · On Mon, 16 Oct 2017 10:41:39 +0200 Igor Mammedov <[email protected]> wrote:
On Fri, 13 Oct 2017 13:47:51 +0200
Greg Kurz <[email protected]> wrote:[...]
Quoted 3 lines
Note that the resolution should really return a CPU object, otherwise we have a bug. This is achieved by relying on
object_resolve_path()andCPU()instead of callingobject_resolve_path_type(path, TYPE_CPU).I'm not sure what you are trying to say here, could you explain a bit more why CPU(object_resolve_path()) is chosen vs
object_resolve_path_type(path, TYPE_CPU)IIUC, if we use
object_resolve_path_type(path, TYPE_CPU)and path doesn't point to a CPU object, it will return NULL (seeobject_resolve_abs_path()) just like if the CPU got hot-unplugged.
Quoted 5 lines
My point is that the path we got from
object_get_canonical_path()did point to a CPU: if later on this path resolves to an object that isn't a CPU, then some code somewhere used the same QOM path for some unrelated object. I tend to think this is a bug in QEMU and we shouldn't silently ignore it.
sometimes QOM path to a cpu object could be:
1. arbitrary if set explicitly by board
2. /unattached/device[X] or /peripheral-anon/device[X]
potentially might end-up with another device at the same path
in case of hot-remove + migration
3. /peripheral
hot-remove named (i.e. non null 'id') cpu and then plug a non cpu device with the same 'id', which is legitimate thing to do
I'd use object_resolve_path_type(path, TYPE_CPU) and gracefully error out in case it returns NULL.
Quoted 81 lines
Makes sense ?
Quoted 38 lines
Quoted 38 lines
Suggested-by Igor Mammedov <[email protected]>Signed-off-by Greg Kurz <[email protected]>--- monitor.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-)monitor.c +4
-2@@ -200,7 +200,7 @@ struct Monitor {ReadLineState *rs;MonitorQMP qmp; -CPUState *mon_cpu; +gchar *mon_cpu_path;BlockCompletionFunc *password_completion_cb;void *password_opaque;mon_cmd_t *cmd_table;@@ -579,6 +579,7 @@ static void monitor_data_init(Monitor *mon)static void monitor_data_destroy (Monitor *mon) { +g_free (mon->mon_cpu_path);qemu_chr_fe_deinit (&mon->chr, false);if (monitor_is_qmp (mon)) {json_message_parser_destroy (&mon->qmp.parser);@@ -1047,20 +1048,34 @@ int monitor_set_cpu(int cpu_index)if (cpu ==NULL ) {return -1 ; } - cur_mon->mon_cpu = cpu; +g_free (cur_mon->mon_cpu_path); + cur_mon->mon_cpu_path =object_get_canonical_path (OBJECT (cpu));return 0 ; }Quoted 15 lines
monitor.c int monitor_set_cpu(int cpu_index) · line 1067
Quoted 13 lines
monitor.c +10
-1CPUState *mon_get_cpu (void ) { -if (!cur_mon->mon_cpu) { +CPUState *cpu; + +if (cur_mon->mon_cpu_path) { +Object *obj =object_resolve_path (cur_mon->mon_cpu_path,NULL ); + +if (!obj) { +g_free (cur_mon->mon_cpu_path); + cur_mon->mon_cpu_path =NULL ; + }else { + cpu =CPU (obj);this potentially might abort if obj couldn't be cast to
TYPE_CPUThis is deliberate... is there a case where
cur_mon->mon_cpu_pathwould legitimately point to something that isn't of typeTYPE_CPU?Quoted 20 lines
Quoted 19 lines
monitor.c +6
-2+ } + } +if (!cur_mon->mon_cpu_path) {if (!first_cpu) {return NULL ; }monitor_set_cpu (first_cpu->cpu_index); + cpu = first_cpu; } -cpu_synchronize_state (cur_mon->mon_cpu); -return cur_mon->mon_cpu; +cpu_synchronize_state (cpu); +return cpu; }CPUArchState *mon_get_cpu_env (void )
Quoted 28 lines · On Mon, 16 Oct 2017 16:31:07 +0200 Igor Mammedov <[email protected]> wrote:
Quoted 20 lines · On Mon, 16 Oct 2017 13:24:20 +0200 Greg Kurz <[email protected]> wrote:
Quoted 12 lines · On Mon, 16 Oct 2017 10:41:39 +0200 Igor Mammedov <[email protected]> wrote:
On Fri, 13 Oct 2017 13:47:51 +0200
Greg Kurz <[email protected]> wrote:[...]
Quoted 3 lines
Note that the resolution should really return a CPU object, otherwise we have a bug. This is achieved by relying on
object_resolve_path()andCPU()instead of callingobject_resolve_path_type(path, TYPE_CPU).I'm not sure what you are trying to say here, could you explain a bit more why CPU(object_resolve_path()) is chosen vs
object_resolve_path_type(path, TYPE_CPU)IIUC, if we use
object_resolve_path_type(path, TYPE_CPU)and path doesn't point to a CPU object, it will return NULL (seeobject_resolve_abs_path()) just like if the CPU got hot-unplugged.Quoted 5 lines
My point is that the path we got from
object_get_canonical_path()did point to a CPU: if later on this path resolves to an object that isn't a CPU, then some code somewhere used the same QOM path for some unrelated object. I tend to think this is a bug in QEMU and we shouldn't silently ignore it.
Quoted 9 lines
sometimes QOM path to a cpu object could be:
1. arbitrary if set explicitly by board
2. /unattached/device[X] or /peripheral-anon/device[X]
potentially might end-up with another device at the same path
in case of hot-remove + migration
3. /peripheral
hot-remove named (i.e. non null 'id') cpu and then plug a non cpu device with the same 'id', which is legitimate thing to do
Ok, I didn't think of these cases...
Quoted 3 lines
I'd use
object_resolve_path_type(path, TYPE_CPU)and gracefully error out in case it returns NULL.
I'll do that. Thanks for the detailed explanation.
Quoted 82 lines
Quoted 81 lines
Makes sense ?
Quoted 38 lines
Quoted 38 lines
Suggested-by Igor Mammedov <[email protected]>Signed-off-by Greg Kurz <[email protected]>--- monitor.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-)monitor.c +4
-2@@ -200,7 +200,7 @@ struct Monitor {ReadLineState *rs;MonitorQMP qmp; -CPUState *mon_cpu; +gchar *mon_cpu_path;BlockCompletionFunc *password_completion_cb;void *password_opaque;mon_cmd_t *cmd_table;@@ -579,6 +579,7 @@ static void monitor_data_init(Monitor *mon)static void monitor_data_destroy (Monitor *mon) { +g_free (mon->mon_cpu_path);qemu_chr_fe_deinit (&mon->chr, false);if (monitor_is_qmp (mon)) {json_message_parser_destroy (&mon->qmp.parser);@@ -1047,20 +1048,34 @@ int monitor_set_cpu(int cpu_index)if (cpu ==NULL ) {return -1 ; } - cur_mon->mon_cpu = cpu; +g_free (cur_mon->mon_cpu_path); + cur_mon->mon_cpu_path =object_get_canonical_path (OBJECT (cpu));return 0 ; }Quoted 15 lines
monitor.c int monitor_set_cpu(int cpu_index) · line 1067
Quoted 13 lines
monitor.c +10
-1CPUState *mon_get_cpu (void ) { -if (!cur_mon->mon_cpu) { +CPUState *cpu; + +if (cur_mon->mon_cpu_path) { +Object *obj =object_resolve_path (cur_mon->mon_cpu_path,NULL ); + +if (!obj) { +g_free (cur_mon->mon_cpu_path); + cur_mon->mon_cpu_path =NULL ; + }else { + cpu =CPU (obj);this potentially might abort if obj couldn't be cast to
TYPE_CPUThis is deliberate... is there a case where
cur_mon->mon_cpu_pathwould legitimately point to something that isn't of typeTYPE_CPU?Quoted 20 lines
Quoted 19 lines
monitor.c +6
-2+ } + } +if (!cur_mon->mon_cpu_path) {if (!first_cpu) {return NULL ; }monitor_set_cpu (first_cpu->cpu_index); + cpu = first_cpu; } -cpu_synchronize_state (cur_mon->mon_cpu); -return cur_mon->mon_cpu; +cpu_synchronize_state (cpu); +return cpu; }CPUArchState *mon_get_cpu_env (void )