monitor: increase the refcount of the current CPU
cover monitor: increase the refcount of the current CPU
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
I could verify that this happens with x86 and ppc, but I guess s390x is also impacted.
This series tries to fix the issue by using object_ref() and object_unref() in the monitor code. For this to work on ppc, some preliminary work is needed to let QOM handle the CPU object lifecycle.
Please comment.
--
Greg
---
Greg Kurz (2):
spapr_cpu_core: instantiate CPUs separately
monitor: add proper reference counting of the current CPU
hw/ppc/spapr.c | 10 +++------- hw/ppc/spapr_cpu_core.c | 29 +++++++++-------------------- include/hw/ppc/spapr_cpu_core.h | 2 +- monitor.c | 12 ++++++++++++ 4 files changed, 25 insertions(+), 28 deletions(-)
PATCH 1/2 spapr_cpu_core: instantiate CPUs separately
The current code assumes that only the CPU core object holds a reference on each individual CPU object, and happily frees their allocated memory when the core is unrealized. This is dangerous as some other code can legitimely keep a pointer to a CPU if it calls object_ref(), but it would end up with a dangling pointer.
Let's allocate all CPUs with object_new() and let QOM frees them when their reference count reaches zero.
--- hw/ppc/spapr.c | 10 +++------- hw/ppc/spapr_cpu_core.c | 29 +++++++++-------------------- include/hw/ppc/spapr_cpu_core.h | 2 +- 3 files changed, 13 insertions(+), 28 deletions(-)
hw/ppc/spapr.c +3 -7
@@ -3153,12 +3153,10 @@ void spapr_core_release(DeviceState *dev)if (smc->pre_2_10_has_unused_icps) {sPAPRCPUCore *sc =SPAPR_CPU_CORE (OBJECT (dev)); -sPAPRCPUCoreClass *scc =SPAPR_CPU_CORE_GET_CLASS (OBJECT (cc)); -size_t size =object_type_get_instance_size (scc->cpu_type);int i;for (i =0 ; i < cc->nr_threads; i++) { -CPUState *cs =CPU (sc->threads + i * size); +CPUState *cs =CPU (sc->threads[i]);pre_2_10_vmstate_register_dummy_icp (cs->cpu_index); }
@@ -3204,7 +3202,7 @@ static void spapr_core_plug(HotplugHandler *hotplug_dev, DeviceState *dev,sPAPRMachineClass *smc =SPAPR_MACHINE_CLASS (mc);sPAPRCPUCore *core =SPAPR_CPU_CORE (OBJECT (dev));CPUCore *cc =CPU_CORE (dev); -CPUState *cs =CPU (core->threads); +CPUState *cs =CPU (core->threads[0 ]);sPAPRDRConnector *drc;Error *local_err =NULL ;int smt =kvmppc_smt_threads ();
@@ -3249,13 +3247,11 @@ static void spapr_core_plug(HotplugHandler *hotplug_dev, DeviceState *dev, core_slot->cpu =OBJECT (dev);if (smc->pre_2_10_has_unused_icps) { -sPAPRCPUCoreClass *scc =SPAPR_CPU_CORE_GET_CLASS (OBJECT (cc)); -size_t size =object_type_get_instance_size (scc->cpu_type);int i;for (i =0 ; i < cc->nr_threads; i++) {sPAPRCPUCore *sc =SPAPR_CPU_CORE (dev); -void *obj = sc->threads + i * size; +void *obj = sc->threads[i]; cs =CPU (obj);pre_2_10_vmstate_unregister_dummy_icp (cs->cpu_index);
hw/ppc/spapr_cpu_core.c +9 -20
@@ -79,21 +79,18 @@ const char *spapr_get_cpu_core_type(const char *cpu_type)static void spapr_cpu_core_unrealizefn (DeviceState *dev,Error **errp) {sPAPRCPUCore *sc =SPAPR_CPU_CORE (OBJECT (dev)); -sPAPRCPUCoreClass *scc =SPAPR_CPU_CORE_GET_CLASS (OBJECT (dev)); -size_t size =object_type_get_instance_size (scc->cpu_type);CPUCore *cc =CPU_CORE (dev);int i;for (i =0 ; i < cc->nr_threads; i++) { -void *obj = sc->threads + i * size; -DeviceState *dev =DEVICE (obj); +DeviceState *dev =DEVICE (sc->threads[i]);CPUState *cs =CPU (dev);PowerPCCPU *cpu =POWERPC_CPU (cs);spapr_cpu_destroy (cpu);object_unparent (cpu->intc);cpu_remove_sync (cs); -object_unparent (obj); +object_unparent (sc->threads[i]); }g_free (sc->threads); }
@@ -146,9 +143,7 @@ static void spapr_cpu_core_realize(DeviceState *dev, Error **errp)sPAPRCPUCore *sc =SPAPR_CPU_CORE (OBJECT (dev));sPAPRCPUCoreClass *scc =SPAPR_CPU_CORE_GET_CLASS (OBJECT (dev));CPUCore *cc =CPU_CORE (OBJECT (dev)); -size_t size;Error *local_err =NULL ; -void *obj;int i, j;if (!spapr) {
@@ -156,17 +151,14 @@ static void spapr_cpu_core_realize(DeviceState *dev, Error **errp)return ; } - size =object_type_get_instance_size (scc->cpu_type); - sc->threads =g_malloc0 (size * cc->nr_threads); + sc->threads = g_new(void *, cc->nr_threads);for (i =0 ; i < cc->nr_threads; i++) {char id[32 ];CPUState *cs;PowerPCCPU *cpu; - obj = sc->threads + i * size; - -object_initialize (obj, size, scc->cpu_type); - cs =CPU (obj); + sc->threads[i] =object_new (scc->cpu_type); + cs =CPU (sc->threads[i]); cpu =POWERPC_CPU (cs); cs->cpu_index = cc->core_id + i; cpu->vcpu_id = (cc->core_id * spapr->vsmt / smp_threads) + i;
@@ -184,17 +176,15 @@ static void spapr_cpu_core_realize(DeviceState *dev, Error **errp) cpu->node_id = sc->node_id;snprintf (id,sizeof (id),"thread[%d]" , i); -object_property_add_child (OBJECT (sc), id, obj, &local_err); +object_property_add_child (OBJECT (sc), id, sc->threads[i], &local_err);if (local_err) { goto err; } -object_unref (obj); +object_unref (sc->threads[i]); }for (j =0 ; j < cc->nr_threads; j++) { - obj = sc->threads + j * size; - -spapr_cpu_core_realize_child (obj, spapr, &local_err); +spapr_cpu_core_realize_child (sc->threads[j], spapr, &local_err);if (local_err) { goto err; }
@@ -203,8 +193,7 @@ static void spapr_cpu_core_realize(DeviceState *dev, Error **errp) err:while (--i >=0 ) { - obj = sc->threads + i * size; -object_unparent (obj); +object_unparent (sc->threads[i]); }g_free (sc->threads);error_propagate (errp, local_err);
include/hw/ppc/spapr_cpu_core.h +1 -1
@@ -28,7 +28,7 @@ typedef struct sPAPRCPUCore {CPUCore parent_obj;/*< public >*/ -void *threads; +void **threads;int node_id; } sPAPRCPUCore;
Re: [Qemu-devel] [PATCH 1/2] spapr_cpu_core: instantiate CPUs separately
Quoted 8 lines · On Fri, 13 Oct 2017 10:35:19 +0200 Greg Kurz <[email protected]> wrote:
The current code assumes that only the CPU core object holds a reference on each individual CPU object, and happily frees their allocated memory when the core is unrealized. This is dangerous as some other code can legitimely keep a pointer to a CPU if it calls
object_ref(), but it would end up with a dangling pointer.Let's allocate all CPUs with
object_new()and let QOM frees them when their reference count reaches zero.
I agree with patch as it simplifies code and one doesn't have to fiddle wit size anymore.
See some comments below.
Quoted 89 lines
Signed-off-by Greg Kurz <[email protected]>--- hw/ppc/spapr.c | 10 +++------- hw/ppc/spapr_cpu_core.c | 29 +++++++++-------------------- include/hw/ppc/spapr_cpu_core.h | 2 +- 3 files changed, 13 insertions(+), 28 deletions(-)hw/ppc/spapr.c +3
-7@@ -3153,12 +3153,10 @@ void spapr_core_release(DeviceState *dev)if (smc->pre_2_10_has_unused_icps) {sPAPRCPUCore *sc =SPAPR_CPU_CORE (OBJECT (dev)); -sPAPRCPUCoreClass *scc =SPAPR_CPU_CORE_GET_CLASS (OBJECT (cc)); -size_t size =object_type_get_instance_size (scc->cpu_type);int i;for (i =0 ; i < cc->nr_threads; i++) { -CPUState *cs =CPU (sc->threads + i * size); +CPUState *cs =CPU (sc->threads[i]);pre_2_10_vmstate_register_dummy_icp (cs->cpu_index); }@@ -3204,7 +3202,7 @@ static void spapr_core_plug(HotplugHandler *hotplug_dev, DeviceState *dev,sPAPRMachineClass *smc =SPAPR_MACHINE_CLASS (mc);sPAPRCPUCore *core =SPAPR_CPU_CORE (OBJECT (dev));CPUCore *cc =CPU_CORE (dev); -CPUState *cs =CPU (core->threads); +CPUState *cs =CPU (core->threads[0 ]);sPAPRDRConnector *drc;Error *local_err =NULL ;int smt =kvmppc_smt_threads ();@@ -3249,13 +3247,11 @@ static void spapr_core_plug(HotplugHandler *hotplug_dev, DeviceState *dev, core_slot->cpu =OBJECT (dev);if (smc->pre_2_10_has_unused_icps) { -sPAPRCPUCoreClass *scc =SPAPR_CPU_CORE_GET_CLASS (OBJECT (cc)); -size_t size =object_type_get_instance_size (scc->cpu_type);int i;for (i =0 ; i < cc->nr_threads; i++) {sPAPRCPUCore *sc =SPAPR_CPU_CORE (dev); -void *obj = sc->threads + i * size; +void *obj = sc->threads[i]; cs =CPU (obj);pre_2_10_vmstate_unregister_dummy_icp (cs->cpu_index);hw/ppc/spapr_cpu_core.c +2
-7@@ -79,21 +79,18 @@ const char *spapr_get_cpu_core_type(const char *cpu_type)static void spapr_cpu_core_unrealizefn (DeviceState *dev,Error **errp) {sPAPRCPUCore *sc =SPAPR_CPU_CORE (OBJECT (dev)); -sPAPRCPUCoreClass *scc =SPAPR_CPU_CORE_GET_CLASS (OBJECT (dev)); -size_t size =object_type_get_instance_size (scc->cpu_type);CPUCore *cc =CPU_CORE (dev);int i;for (i =0 ; i < cc->nr_threads; i++) { -void *obj = sc->threads + i * size; -DeviceState *dev =DEVICE (obj); +DeviceState *dev =DEVICE (sc->threads[i]);CPUState *cs =CPU (dev);PowerPCCPU *cpu =POWERPC_CPU (cs);spapr_cpu_destroy (cpu);object_unparent (cpu->intc);cpu_remove_sync (cs); -object_unparent (obj); +object_unparent (sc->threads[i]); }g_free (sc->threads); }@@ -146,9 +143,7 @@ static void spapr_cpu_core_realize(DeviceState *dev, Error **errp)sPAPRCPUCore *sc =SPAPR_CPU_CORE (OBJECT (dev));sPAPRCPUCoreClass *scc =SPAPR_CPU_CORE_GET_CLASS (OBJECT (dev));CPUCore *cc =CPU_CORE (OBJECT (dev)); -size_t size;Error *local_err =NULL ; -void *obj;int i, j;if (!spapr) {
hw/ppc/spapr_cpu_core.c static void spapr_cpu_core_realize(DeviceState *dev, Error **errp) · line 154
Quoted 6 lines
hw/ppc/spapr_cpu_core.c +1
-2@@ -156,17 +151,14 @@ static void spapr_cpu_core_realize(DeviceState *dev, Error **errp)return ; } - size =object_type_get_instance_size (scc->cpu_type); - sc->threads =g_malloc0 (size * cc->nr_threads); + sc->threads = g_new(void *, cc->nr_threads);
^^^ PowerPCCPU*
and maybe g_new0() if incomplete cores are possible.
Quoted 50 lines
hw/ppc/spapr_cpu_core.c +6
-11for (i =0 ; i < cc->nr_threads; i++) {char id[32 ];CPUState *cs;PowerPCCPU *cpu; - obj = sc->threads + i * size; - -object_initialize (obj, size, scc->cpu_type); - cs =CPU (obj); + sc->threads[i] =object_new (scc->cpu_type); + cs =CPU (sc->threads[i]); cpu =POWERPC_CPU (cs); cs->cpu_index = cc->core_id + i; cpu->vcpu_id = (cc->core_id * spapr->vsmt / smp_threads) + i;@@ -184,17 +176,15 @@ static void spapr_cpu_core_realize(DeviceState *dev, Error **errp) cpu->node_id = sc->node_id;snprintf (id,sizeof (id),"thread[%d]" , i); -object_property_add_child (OBJECT (sc), id, obj, &local_err); +object_property_add_child (OBJECT (sc), id, sc->threads[i], &local_err);if (local_err) { goto err; } -object_unref (obj); +object_unref (sc->threads[i]); }for (j =0 ; j < cc->nr_threads; j++) { - obj = sc->threads + j * size; - -spapr_cpu_core_realize_child (obj, spapr, &local_err); +spapr_cpu_core_realize_child (sc->threads[j], spapr, &local_err);if (local_err) { goto err; }@@ -203,8 +193,7 @@ static void spapr_cpu_core_realize(DeviceState *dev, Error **errp) err:while (--i >=0 ) { - obj = sc->threads + i * size; -object_unparent (obj); +object_unparent (sc->threads[i]); }g_free (sc->threads);error_propagate (errp, local_err);include/hw/ppc/spapr_cpu_core.h +0
-0
include/hw/ppc/spapr_cpu_core.h typedef struct sPAPRCPUCore { · line 31
Quoted 5 lines
include/hw/ppc/spapr_cpu_core.h +1
-1@@ -28,7 +28,7 @@ typedef struct sPAPRCPUCore {CPUCore parent_obj;/*< public >*/ -void *threads; +void **threads;
ditto, should be PowerPCCPU
Quoted 5 lines
include/hw/ppc/spapr_cpu_core.h +0
-0int node_id; } sPAPRCPUCore;
Re: [Qemu-devel] [PATCH 1/2] spapr_cpu_core: instantiate CPUs separately
Quoted 9 lines · On Fri, 13 Oct 2017 11:21:20 +0200 Igor Mammedov <[email protected]> wrote:
Quoted 6 lines · On Fri, 13 Oct 2017 10:35:19 +0200 Greg Kurz <[email protected]> wrote:
The current code assumes that only the CPU core object holds a reference on each individual CPU object, and happily frees their allocated memory when the core is unrealized. This is dangerous as some other code can legitimely keep a pointer to a CPU if it calls
object_ref(), but it would end up with a dangling pointer.
Quoted 4 lines
Quoted 2 lines
Let's allocate all CPUs with
object_new()and let QOM frees them when their reference count reaches zero.I agree with patch as it simplifies code and one doesn't have to fiddle wit size anymore.
Yes, I'll mention that in the changelog as well.
Quoted 94 lines
See some comments below.
Quoted 92 lines
Signed-off-by Greg Kurz <[email protected]>--- hw/ppc/spapr.c | 10 +++------- hw/ppc/spapr_cpu_core.c | 29 +++++++++-------------------- include/hw/ppc/spapr_cpu_core.h | 2 +- 3 files changed, 13 insertions(+), 28 deletions(-)hw/ppc/spapr.c +3
-7@@ -3153,12 +3153,10 @@ void spapr_core_release(DeviceState *dev)if (smc->pre_2_10_has_unused_icps) {sPAPRCPUCore *sc =SPAPR_CPU_CORE (OBJECT (dev)); -sPAPRCPUCoreClass *scc =SPAPR_CPU_CORE_GET_CLASS (OBJECT (cc)); -size_t size =object_type_get_instance_size (scc->cpu_type);int i;for (i =0 ; i < cc->nr_threads; i++) { -CPUState *cs =CPU (sc->threads + i * size); +CPUState *cs =CPU (sc->threads[i]);pre_2_10_vmstate_register_dummy_icp (cs->cpu_index); }@@ -3204,7 +3202,7 @@ static void spapr_core_plug(HotplugHandler *hotplug_dev, DeviceState *dev,sPAPRMachineClass *smc =SPAPR_MACHINE_CLASS (mc);sPAPRCPUCore *core =SPAPR_CPU_CORE (OBJECT (dev));CPUCore *cc =CPU_CORE (dev); -CPUState *cs =CPU (core->threads); +CPUState *cs =CPU (core->threads[0 ]);sPAPRDRConnector *drc;Error *local_err =NULL ;int smt =kvmppc_smt_threads ();@@ -3249,13 +3247,11 @@ static void spapr_core_plug(HotplugHandler *hotplug_dev, DeviceState *dev, core_slot->cpu =OBJECT (dev);if (smc->pre_2_10_has_unused_icps) { -sPAPRCPUCoreClass *scc =SPAPR_CPU_CORE_GET_CLASS (OBJECT (cc)); -size_t size =object_type_get_instance_size (scc->cpu_type);int i;for (i =0 ; i < cc->nr_threads; i++) {sPAPRCPUCore *sc =SPAPR_CPU_CORE (dev); -void *obj = sc->threads + i * size; +void *obj = sc->threads[i]; cs =CPU (obj);pre_2_10_vmstate_unregister_dummy_icp (cs->cpu_index);hw/ppc/spapr_cpu_core.c +2
-7@@ -79,21 +79,18 @@ const char *spapr_get_cpu_core_type(const char *cpu_type)static void spapr_cpu_core_unrealizefn (DeviceState *dev,Error **errp) {sPAPRCPUCore *sc =SPAPR_CPU_CORE (OBJECT (dev)); -sPAPRCPUCoreClass *scc =SPAPR_CPU_CORE_GET_CLASS (OBJECT (dev)); -size_t size =object_type_get_instance_size (scc->cpu_type);CPUCore *cc =CPU_CORE (dev);int i;for (i =0 ; i < cc->nr_threads; i++) { -void *obj = sc->threads + i * size; -DeviceState *dev =DEVICE (obj); +DeviceState *dev =DEVICE (sc->threads[i]);CPUState *cs =CPU (dev);PowerPCCPU *cpu =POWERPC_CPU (cs);spapr_cpu_destroy (cpu);object_unparent (cpu->intc);cpu_remove_sync (cs); -object_unparent (obj); +object_unparent (sc->threads[i]); }g_free (sc->threads); }@@ -146,9 +143,7 @@ static void spapr_cpu_core_realize(DeviceState *dev, Error **errp)sPAPRCPUCore *sc =SPAPR_CPU_CORE (OBJECT (dev));sPAPRCPUCoreClass *scc =SPAPR_CPU_CORE_GET_CLASS (OBJECT (dev));CPUCore *cc =CPU_CORE (OBJECT (dev)); -size_t size;Error *local_err =NULL ; -void *obj;int i, j;if (!spapr) {@@ -156,17 +151,14 @@ static void spapr_cpu_core_realize(DeviceState *dev, Error **errp)return ; }
Quoted 6 lines
hw/ppc/spapr_cpu_core.c static void spapr_cpu_core_realize(DeviceState *dev, Error **errp) · line 154
Quoted 3 lines
hw/ppc/spapr_cpu_core.c +1
-2- size =object_type_get_instance_size (scc->cpu_type); - sc->threads =g_malloc0 (size * cc->nr_threads); + sc->threads = g_new(void *, cc->nr_threads);^^^ PowerPCCPU*
Right, I guess void * was appropriate to do the pointer arithmetics but we don't need that anymore.
Quoted 2 lines
and maybe
g_new0()if incomplete cores are possible.
Yes, older machine types (ie, that don't support CPU hotplug) can have incomplete cores, but cc->nr_threads is set to the actual number of threads in this case. See spapr_init_cpus() in hw/ppc/spapr.c.
Quoted 52 lines
Quoted 52 lines
hw/ppc/spapr_cpu_core.c +6
-11for (i =0 ; i < cc->nr_threads; i++) {char id[32 ];CPUState *cs;PowerPCCPU *cpu; - obj = sc->threads + i * size; - -object_initialize (obj, size, scc->cpu_type); - cs =CPU (obj); + sc->threads[i] =object_new (scc->cpu_type); + cs =CPU (sc->threads[i]); cpu =POWERPC_CPU (cs); cs->cpu_index = cc->core_id + i; cpu->vcpu_id = (cc->core_id * spapr->vsmt / smp_threads) + i;@@ -184,17 +176,15 @@ static void spapr_cpu_core_realize(DeviceState *dev, Error **errp) cpu->node_id = sc->node_id;snprintf (id,sizeof (id),"thread[%d]" , i); -object_property_add_child (OBJECT (sc), id, obj, &local_err); +object_property_add_child (OBJECT (sc), id, sc->threads[i], &local_err);if (local_err) { goto err; } -object_unref (obj); +object_unref (sc->threads[i]); }for (j =0 ; j < cc->nr_threads; j++) { - obj = sc->threads + j * size; - -spapr_cpu_core_realize_child (obj, spapr, &local_err); +spapr_cpu_core_realize_child (sc->threads[j], spapr, &local_err);if (local_err) { goto err; }@@ -203,8 +193,7 @@ static void spapr_cpu_core_realize(DeviceState *dev, Error **errp) err:while (--i >=0 ) { - obj = sc->threads + i * size; -object_unparent (obj); +object_unparent (sc->threads[i]); }g_free (sc->threads);error_propagate (errp, local_err);include/hw/ppc/spapr_cpu_core.h +0
-0@@ -28,7 +28,7 @@ typedef struct sPAPRCPUCore {CPUCore parent_obj;
Quoted 5 lines
include/hw/ppc/spapr_cpu_core.h typedef struct sPAPRCPUCore { · line 31
Quoted 3 lines
include/hw/ppc/spapr_cpu_core.h +1
-1/*< public >*/ -void *threads; +void **threads;ditto, should be PowerPCCPU
Ok.
Thanks for the feedback!
Quoted 6 lines
Quoted 5 lines
include/hw/ppc/spapr_cpu_core.h +0
-0int node_id; } sPAPRCPUCore;
PATCH 2/2 monitor: add proper reference counting of the current CPU
In reply to Greg KurzIf 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 use object_ref() to ensure the CPU object doesn't vanish unexpectedly. The reference is dropped either when "cpu" is used to switch to another CPU, or when the selected CPU is unrealized and cpu_list_remove() sets its cpu_index back to UNASSIGNED_CPU_INDEX.
--- monitor.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
monitor.c +12 -0
@@ -579,6 +579,9 @@ static void monitor_data_init(Monitor *mon)static void monitor_data_destroy (Monitor *mon) { +if (mon->mon_cpu) { +object_unref ((Object *) mon->mon_cpu); + }qemu_chr_fe_deinit (&mon->chr, false);if (monitor_is_qmp (mon)) {json_message_parser_destroy (&mon->qmp.parser);
@@ -1047,12 +1050,21 @@ int monitor_set_cpu(int cpu_index)if (cpu ==NULL ) {return -1 ; } +if (cur_mon->mon_cpu) { +object_unref ((Object *) cur_mon->mon_cpu); + } cur_mon->mon_cpu = cpu; +object_ref ((Object *) cpu);return 0 ; }CPUState *mon_get_cpu (void ) { +if (cur_mon->mon_cpu && + cur_mon->mon_cpu->cpu_index ==UNASSIGNED_CPU_INDEX ) { +object_unref ((Object *) cur_mon->mon_cpu); + cur_mon->mon_cpu =NULL ; + }if (!cur_mon->mon_cpu) {if (!first_cpu) {return NULL ;
Re: [Qemu-devel] [PATCH 2/2] monitor: add proper reference counting of the current CPU
Quoted 16 lines · On Fri, 13 Oct 2017 10:35:31 +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 pointer
Quoted 7 lines
This could cause a QEMU crash as well.
This patch switches the monitor to use
object_ref()to ensure the CPU object doesn't vanish unexpectedly. The reference is dropped either when "cpu" is used to switch to another CPU, or when the selected CPU is unrealized andcpu_list_remove()sets itscpu_indexback toUNASSIGNED_CPU_INDEX.
I don't really like an idea of leaving dangling cpu around. Is it possible to store QOM path on set_cpu in monitor and resolving it each to instance each time it's needed?
Quoted 43 lines
Signed-off-by Greg Kurz <[email protected]>--- monitor.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)monitor.c +12
-0@@ -579,6 +579,9 @@ static void monitor_data_init(Monitor *mon)static void monitor_data_destroy (Monitor *mon) { +if (mon->mon_cpu) { +object_unref ((Object *) mon->mon_cpu); + }qemu_chr_fe_deinit (&mon->chr, false);if (monitor_is_qmp (mon)) {json_message_parser_destroy (&mon->qmp.parser);@@ -1047,12 +1050,21 @@ int monitor_set_cpu(int cpu_index)if (cpu ==NULL ) {return -1 ; } +if (cur_mon->mon_cpu) { +object_unref ((Object *) cur_mon->mon_cpu); + } cur_mon->mon_cpu = cpu; +object_ref ((Object *) cpu);return 0 ; }CPUState *mon_get_cpu (void ) { +if (cur_mon->mon_cpu && + cur_mon->mon_cpu->cpu_index ==UNASSIGNED_CPU_INDEX ) { +object_unref ((Object *) cur_mon->mon_cpu); + cur_mon->mon_cpu =NULL ; + }if (!cur_mon->mon_cpu) {if (!first_cpu) {return NULL ;
Re: [Qemu-devel] [PATCH 2/2] monitor: add proper reference counting of the current CPU
Quoted 21 lines · On Fri, 13 Oct 2017 11:24:59 +0200 Igor Mammedov <[email protected]> wrote:
Quoted 18 lines · On Fri, 13 Oct 2017 10:35:31 +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.
Quoted 9 lines
Quoted 5 lines
This patch switches the monitor to use
object_ref()to ensure the CPU object doesn't vanish unexpectedly. The reference is dropped either when "cpu" is used to switch to another CPU, or when the selected CPU is unrealized andcpu_list_remove()sets itscpu_indexback toUNASSIGNED_CPU_INDEX.I don't really like an idea of leaving dangling cpu around. Is it possible to store QOM path on
set_cpuin monitor and resolving it each to instance each time it's needed?
It sounds workable. Also it would allow the fix to not depend on patch 1 for ppc.
Thanks for the suggestion!
Quoted 45 lines
Quoted 44 lines
Signed-off-by Greg Kurz <[email protected]>--- monitor.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)monitor.c +12
-0@@ -579,6 +579,9 @@ static void monitor_data_init(Monitor *mon)static void monitor_data_destroy (Monitor *mon) { +if (mon->mon_cpu) { +object_unref ((Object *) mon->mon_cpu); + }qemu_chr_fe_deinit (&mon->chr, false);if (monitor_is_qmp (mon)) {json_message_parser_destroy (&mon->qmp.parser);@@ -1047,12 +1050,21 @@ int monitor_set_cpu(int cpu_index)if (cpu ==NULL ) {return -1 ; } +if (cur_mon->mon_cpu) { +object_unref ((Object *) cur_mon->mon_cpu); + } cur_mon->mon_cpu = cpu; +object_ref ((Object *) cpu);return 0 ; }CPUState *mon_get_cpu (void ) { +if (cur_mon->mon_cpu && + cur_mon->mon_cpu->cpu_index ==UNASSIGNED_CPU_INDEX ) { +object_unref ((Object *) cur_mon->mon_cpu); + cur_mon->mon_cpu =NULL ; + }if (!cur_mon->mon_cpu) {if (!first_cpu) {return NULL ;
Re: [Qemu-devel] [PATCH 2/2] monitor: add proper reference counting of the current CPU
Quoted 29 lines · On Fri, 13 Oct 2017 12:05:46 +0200 Greg Kurz <[email protected]> wrote:
Quoted 26 lines · On Fri, 13 Oct 2017 11:24:59 +0200 Igor Mammedov <[email protected]> wrote:
Quoted 23 lines · On Fri, 13 Oct 2017 10:35:31 +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 use
object_ref()to ensure the CPU object doesn't vanish unexpectedly. The reference is dropped either when "cpu" is used to switch to another CPU, or when the selected CPU is unrealized andcpu_list_remove()sets itscpu_indexback toUNASSIGNED_CPU_INDEX.
Quoted 7 lines
Quoted 4 lines
I don't really like an idea of leaving dangling cpu around. Is it possible to store QOM path on
set_cpuin monitor and resolving it each to instance each time it's needed?It sounds workable. Also it would allow the fix to not depend on patch 1 for ppc.
yep,
also if you'd touch these cpu_index based monitor commands, it might make sense to convert them to use qmo path directly instead if cpu_index (with command completion it would be easy to use)
Quoted 49 lines
Thanks for the suggestion!
Quoted 45 lines
Quoted 44 lines
Signed-off-by Greg Kurz <[email protected]>--- monitor.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)monitor.c +12
-0@@ -579,6 +579,9 @@ static void monitor_data_init(Monitor *mon)static void monitor_data_destroy (Monitor *mon) { +if (mon->mon_cpu) { +object_unref ((Object *) mon->mon_cpu); + }qemu_chr_fe_deinit (&mon->chr, false);if (monitor_is_qmp (mon)) {json_message_parser_destroy (&mon->qmp.parser);@@ -1047,12 +1050,21 @@ int monitor_set_cpu(int cpu_index)if (cpu ==NULL ) {return -1 ; } +if (cur_mon->mon_cpu) { +object_unref ((Object *) cur_mon->mon_cpu); + } cur_mon->mon_cpu = cpu; +object_ref ((Object *) cpu);return 0 ; }CPUState *mon_get_cpu (void ) { +if (cur_mon->mon_cpu && + cur_mon->mon_cpu->cpu_index ==UNASSIGNED_CPU_INDEX ) { +object_unref ((Object *) cur_mon->mon_cpu); + cur_mon->mon_cpu =NULL ; + }if (!cur_mon->mon_cpu) {if (!first_cpu) {return NULL ;
Re: [Qemu-devel] [PATCH 2/2] monitor: add proper reference counting of the current CPU
Quoted 37 lines · On Mon, 16 Oct 2017 10:07:18 +0200 Igor Mammedov <[email protected]> wrote:
Quoted 34 lines · On Fri, 13 Oct 2017 12:05:46 +0200 Greg Kurz <[email protected]> wrote:
Quoted 30 lines · On Fri, 13 Oct 2017 11:24:59 +0200 Igor Mammedov <[email protected]> wrote:
Quoted 16 lines · On Fri, 13 Oct 2017 10:35:31 +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 pointerQuoted 7 lines
This could cause a QEMU crash as well.
This patch switches the monitor to use
object_ref()to ensure the CPU object doesn't vanish unexpectedly. The reference is dropped either when "cpu" is used to switch to another CPU, or when the selected CPU is unrealized andcpu_list_remove()sets itscpu_indexback toUNASSIGNED_CPU_INDEX.I don't really like an idea of leaving dangling cpu around. Is it possible to store QOM path on
set_cpuin monitor and resolving it each to instance each time it's needed?
Quoted 8 lines
Quoted 2 lines
It sounds workable. Also it would allow the fix to not depend on patch 1 for ppc.
yep,
also if you'd touch thesecpu_indexbased monitor commands, it might make sense to convert them to use qmo path directly instead ifcpu_index(with command completion it would be easy to use)
Ok, I'll look into that but I'm not sure I can work something out before soft freeze (2017-10-31 AFAIK).
Quoted 51 lines
Quoted 50 lines
Thanks for the suggestion!
Quoted 45 lines
Quoted 44 lines
Signed-off-by Greg Kurz <[email protected]>--- monitor.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)monitor.c +12
-0@@ -579,6 +579,9 @@ static void monitor_data_init(Monitor *mon)static void monitor_data_destroy (Monitor *mon) { +if (mon->mon_cpu) { +object_unref ((Object *) mon->mon_cpu); + }qemu_chr_fe_deinit (&mon->chr, false);if (monitor_is_qmp (mon)) {json_message_parser_destroy (&mon->qmp.parser);@@ -1047,12 +1050,21 @@ int monitor_set_cpu(int cpu_index)if (cpu ==NULL ) {return -1 ; } +if (cur_mon->mon_cpu) { +object_unref ((Object *) cur_mon->mon_cpu); + } cur_mon->mon_cpu = cpu; +object_ref ((Object *) cpu);return 0 ; }CPUState *mon_get_cpu (void ) { +if (cur_mon->mon_cpu && + cur_mon->mon_cpu->cpu_index ==UNASSIGNED_CPU_INDEX ) { +object_unref ((Object *) cur_mon->mon_cpu); + cur_mon->mon_cpu =NULL ; + }if (!cur_mon->mon_cpu) {if (!first_cpu) {return NULL ;
Quoted 9 lines · On Fri, 13 Oct 2017 10:35:06 +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 processI could verify that this happens with x86 and ppc, but I guess s390x is also impacted.
Not really, as s390x does not support cpu unplug.
Quoted 23 lines
This series tries to fix the issue by using
object_ref()andobject_unref()in the monitor code. For this to work on ppc, some preliminary work is needed to let QOM handle the CPU object lifecycle.Please comment.
--
Greg---
Greg Kurz (2):
spapr_cpu_core: instantiate CPUs separately
monitor: add proper reference counting of the current CPUhw/ppc/spapr.c | 10 +++------- hw/ppc/spapr_cpu_core.c | 29 +++++++++-------------------- include/hw/ppc/spapr_cpu_core.h | 2 +- monitor.c | 12 ++++++++++++ 4 files changed, 25 insertions(+), 28 deletions(-)
Quoted 6 lines · On Fri, 13 Oct 2017 10:46:22 +0200 Cornelia Huck <[email protected]> wrote:
Quoted 3 lines · On Fri, 13 Oct 2017 10:35:06 +0200 Greg Kurz <[email protected]> wrote:
If a CPU selected with the "cpu" command is hot-unplugged then "info cpus" causes QEMU to exit:
Quoted 9 lines
Quoted 6 lines
(qemu)
device_delcpu1
(qemu) info cpus
qemu:qemu_cpu_kick_thread: No such processI could verify that this happens with x86 and ppc, but I guess s390x is also impacted.
Not really, as s390x does not support cpu unplug.
I must admit I didn't check. Thanks for telling me. :)
Quoted 24 lines
Quoted 23 lines
This series tries to fix the issue by using
object_ref()andobject_unref()in the monitor code. For this to work on ppc, some preliminary work is needed to let QOM handle the CPU object lifecycle.Please comment.
--
Greg---
Greg Kurz (2):
spapr_cpu_core: instantiate CPUs separately
monitor: add proper reference counting of the current CPUhw/ppc/spapr.c | 10 +++------- hw/ppc/spapr_cpu_core.c | 29 +++++++++-------------------- include/hw/ppc/spapr_cpu_core.h | 2 +- monitor.c | 12 ++++++++++++ 4 files changed, 25 insertions(+), 28 deletions(-)