Skip to contentSkip to conversation

Allow hotplug of s390 CPUs

PATCH Applied Matthew Rosato 4 participants 12 messages Mar 4, 2016 – Mar 7, 2016

lore
Share & tools
lore
#0 lore raw

v9 cover Allow hotplug of s390 CPUs

Changes from v8->v9:

* Patch 4: fix bug introduced during split, only init smp_cpus (David)
* Patch 6:
* nit change to error_propagate handling in realizefn (David)
* scc->next_cpu_id++ (David)
* s/s390_cpu_set_id/s390x_cpu_set_id/ (David)
* s/s390_new_cpu/s390x_new_cpu/ (David)
* Put id getter back in (David)
* Move linux-user next_cpu_id into s390_cpux_init (David)
* Patch 7: nit change to error_propagate handling (David)

**************

As discussed in the KVM call, we will go ahead with cpu_add for s390x to get cpu hotplug functionality in s390x now, until architectures that require a more robust hotplug interface settle on a design.

To configure a guest with 2 CPUs online at
boot and 4 maximum:

qemu -smp 2,maxcpus=4

Or, when using libvirt:
<domain>

    ...
    <vcpu current="2">4</vcpu>
    ...

</domain>

To subsequently hotplug a CPU:

Issue 'cpu-add <id>' from qemu monitor, or use virsh setvcpus --count <n> <domain>, where <n> is the total number of desired guest CPUs.

At this point, the guest must bring the CPU online for use -- This can be achieved via "echo 1 > /sys/devices/system/cpu/cpuX/online" or via a management tool like cpuplugd.

This patch set is based on work previously done by Jason Herne.

Matthew Rosato (7):
s390x/cpu: Cleanup init in preparation for hotplug s390x/cpu: Set initial CPU state in common routine s390x/cpu: Get rid of side effects when creating a vcpu s390x/cpu: Tolerate max_cpus
s390x/cpu: Add CPU property links
s390x/cpu: Add error handling to cpu creation s390x/cpu: Allow hotplug of CPUs

 hw/s390x/s390-virtio-ccw.c | 46 +++++++++++++++++++++-
 hw/s390x/s390-virtio.c     | 36 +++++++++--------
 hw/s390x/s390-virtio.h     |  2 +-
 target-s390x/cpu-qom.h     |  3 ++
 target-s390x/cpu.c         | 96 +++++++++++++++++++++++++++++++++++++++++++---
 target-s390x/cpu.h         |  2 +
 target-s390x/helper.c      | 41 +++++++++++++++++++-
 7 files changed, 200 insertions(+), 26 deletions(-)
Signature · 1 line
1.9.1
#1 lore raw

PATCH v9 1/7 s390x/cpu: Cleanup init in preparation for hotplug

Ensure a valid cpu_model is set upfront by setting the default value directly into the MachineState when none is specified. This is needed to ensure hotplugged CPUs share the same cpu_model.

---
 hw/s390x/s390-virtio-ccw.c | 2 +-
 hw/s390x/s390-virtio.c     | 8 ++++----
 hw/s390x/s390-virtio.h     | 2 +-
 3 files changed, 6 insertions(+), 6 deletions(-)
hw/s390x/s390-virtio-ccw.c +1 -1
index 89f5d0d..b05ed8b 100644
@@ -136,7 +136,7 @@ static void ccw_init(MachineState *machine)
     virtio_ccw_register_hcalls();
 
     /* init CPUs */
-    s390_init_cpus(machine->cpu_model);
+    s390_init_cpus(machine);
 
     if (kvm_enabled()) {
         kvm_s390_enable_css_support(s390_cpu_addr2state(0));
hw/s390x/s390-virtio.c +4 -4
index 8e533ae..d40d0dc 100644
@@ -93,12 +93,12 @@ void s390_init_ipl_dev(const char *kernel_filename,
     qdev_init_nofail(dev);
 }
 
-void s390_init_cpus(const char *cpu_model)
+void s390_init_cpus(MachineState *machine)
 {
     int i;
 
-    if (cpu_model == NULL) {
-        cpu_model = "host";
+    if (machine->cpu_model == NULL) {
+        machine->cpu_model = "host";
     }
 
     ipi_states = g_malloc(sizeof(S390CPU *) * smp_cpus);
@@ -107,7 +107,7 @@ void s390_init_cpus(const char *cpu_model)
         S390CPU *cpu;
         CPUState *cs;
 
-        cpu = cpu_s390x_init(cpu_model);
+        cpu = cpu_s390x_init(machine->cpu_model);
         cs = CPU(cpu);
 
         ipi_states[i] = cpu;
hw/s390x/s390-virtio.h +1 -1
index eebce8e..ffd014c 100644
@@ -19,7 +19,7 @@
 typedef int (*s390_virtio_fn)(const uint64_t *args);
 void s390_register_virtio_hypercall(uint64_t code, s390_virtio_fn fn);
 
-void s390_init_cpus(const char *cpu_model);
+void s390_init_cpus(MachineState *machine);
 void s390_init_ipl_dev(const char *kernel_filename,
                        const char *kernel_cmdline,
                        const char *initrd_filename,
Signature · 1 line
1.9.1
#2 lore raw

PATCH v9 2/7 s390x/cpu: Set initial CPU state in common routine

In reply to Matthew Rosato

Both initial and hotplugged CPUs need to set the same initial state.

---
 hw/s390x/s390-virtio.c | 4 ----
 target-s390x/cpu.c     | 2 ++
 2 files changed, 2 insertions(+), 4 deletions(-)
hw/s390x/s390-virtio.c +0 -4
index d40d0dc..c501a48 100644
@@ -105,14 +105,10 @@ void s390_init_cpus(MachineState *machine)
 
     for (i = 0; i < smp_cpus; i++) {
         S390CPU *cpu;
-        CPUState *cs;
 
         cpu = cpu_s390x_init(machine->cpu_model);
-        cs = CPU(cpu);
 
         ipi_states[i] = cpu;
-        cs->halted = 1;
-        cs->exception_index = EXCP_HLT;
     }
 }
 
target-s390x/cpu.c +2 -0
index 73a910d..603c2a1 100644
@@ -219,6 +219,8 @@ static void s390_cpu_initfn(Object *obj)
 #endif
 
     cs->env_ptr = env;
+    cs->halted = 1;
+    cs->exception_index = EXCP_HLT;
     cpu_exec_init(cs, &error_abort);
 #if !defined(CONFIG_USER_ONLY)
     qemu_register_reset(s390_cpu_machine_reset_cb, cpu);
Signature · 1 line
1.9.1
#3 lore raw

PATCH v9 3/7 s390x/cpu: Get rid of side effects when creating a vcpu

In reply to Matthew Rosato

In preparation for hotplug, defer some CPU initialization until the device is actually being realized, including cpu_exec_init.

---
 target-s390x/cpu-qom.h |  2 ++
 target-s390x/cpu.c     | 20 +++++++++++++++-----
 2 files changed, 17 insertions(+), 5 deletions(-)
target-s390x/cpu-qom.h +2 -0
index 029a44a..56d82f2 100644
@@ -47,6 +47,8 @@ typedef struct S390CPUClass {
     CPUClass parent_class;
     /*< public >*/
 
+    int64_t next_cpu_id;
+
     DeviceRealize parent_realize;
     void (*parent_reset)(CPUState *cpu);
     void (*load_normal)(CPUState *cpu);
target-s390x/cpu.c +15 -5
index 603c2a1..76c8eaf 100644
@@ -195,7 +195,20 @@ static void s390_cpu_realizefn(DeviceState *dev, Error **errp)
 {
     CPUState *cs = CPU(dev);
     S390CPUClass *scc = S390_CPU_GET_CLASS(dev);
+    S390CPU *cpu = S390_CPU(dev);
+    CPUS390XState *env = &cpu->env;
+    Error *err = NULL;
+
+    cpu_exec_init(cs, &err);
+    if (err != NULL) {
+        error_propagate(errp, err);
+        return;
+    }
 
+#if !defined(CONFIG_USER_ONLY)
+    qemu_register_reset(s390_cpu_machine_reset_cb, cpu);
+#endif
+    env->cpu_num = scc->next_cpu_id++;
     s390_cpu_gdb_init(cs);
     qemu_init_vcpu(cs);
 #if !defined(CONFIG_USER_ONLY)
@@ -213,7 +226,6 @@ static void s390_cpu_initfn(Object *obj)
     S390CPU *cpu = S390_CPU(obj);
     CPUS390XState *env = &cpu->env;
     static bool inited;
-    static int cpu_num = 0;
 #if !defined(CONFIG_USER_ONLY)
     struct tm tm;
 #endif
@@ -221,9 +233,7 @@ static void s390_cpu_initfn(Object *obj)
     cs->env_ptr = env;
     cs->halted = 1;
     cs->exception_index = EXCP_HLT;
-    cpu_exec_init(cs, &error_abort);
 #if !defined(CONFIG_USER_ONLY)
-    qemu_register_reset(s390_cpu_machine_reset_cb, cpu);
     qemu_get_timedate(&tm, 0);
     env->tod_offset = TOD_UNIX_EPOCH +
                       (time2tod(mktimegm(&tm)) * 1000000000ULL);
@@ -232,7 +242,6 @@ static void s390_cpu_initfn(Object *obj)
     env->cpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_cpu_timer, cpu);
     s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
 #endif
-    env->cpu_num = cpu_num++;
 
     if (tcg_enabled() && !inited) {
         inited = true;
@@ -339,6 +348,7 @@ static void s390_cpu_class_init(ObjectClass *oc, void *data)
     CPUClass *cc = CPU_CLASS(scc);
     DeviceClass *dc = DEVICE_CLASS(oc);
 
+    scc->next_cpu_id = 0;
     scc->parent_realize = dc->realize;
     dc->realize = s390_cpu_realizefn;
 
@@ -371,7 +381,7 @@ static void s390_cpu_class_init(ObjectClass *oc, void *data)
     cc->gdb_arch_name = s390_gdb_arch_name;
 
     /*
-     * Reason: s390_cpu_initfn() calls cpu_exec_init(), which saves
+     * Reason: s390_cpu_realizefn() calls cpu_exec_init(), which saves
      * the object in cpus -> dangling pointer after final
      * object_unref().
      */
Signature · 1 line
1.9.1
#4 lore raw

PATCH v9 4/7 s390x/cpu: Tolerate max_cpus

In reply to Matthew Rosato

Once hotplug is enabled, interrupts may come in for CPUs with an address > smp_cpus. Allocate for this and allow search routines to look beyond smp_cpus.

---
 hw/s390x/s390-virtio.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
hw/s390x/s390-virtio.c +6 -5
index c501a48..57c3c88 100644
@@ -58,15 +58,16 @@
 #define S390_TOD_CLOCK_VALUE_MISSING    0x00
 #define S390_TOD_CLOCK_VALUE_PRESENT    0x01
 
-static S390CPU **ipi_states;
+static S390CPU **cpu_states;
 
 S390CPU *s390_cpu_addr2state(uint16_t cpu_addr)
 {
-    if (cpu_addr >= smp_cpus) {
+    if (cpu_addr >= max_cpus) {
         return NULL;
     }
 
-    return ipi_states[cpu_addr];
+    /* Fast lookup via CPU ID */
+    return cpu_states[cpu_addr];
 }
 
 void s390_init_ipl_dev(const char *kernel_filename,
@@ -101,14 +102,14 @@ void s390_init_cpus(MachineState *machine)
         machine->cpu_model = "host";
     }
 
-    ipi_states = g_malloc(sizeof(S390CPU *) * smp_cpus);
+    cpu_states = g_malloc0(sizeof(S390CPU *) * max_cpus);
 
     for (i = 0; i < smp_cpus; i++) {
         S390CPU *cpu;
 
         cpu = cpu_s390x_init(machine->cpu_model);
 
-        ipi_states[i] = cpu;
+        cpu_states[i] = cpu;
     }
 }
 
Signature · 1 line
1.9.1
#7 lore raw

PATCH v9 6/7 s390x/cpu: Add error handling to cpu creation

In reply to Matthew Rosato

Check for and propogate errors during s390 cpu creation.

---
 hw/s390x/s390-virtio.c |  2 +-
 target-s390x/cpu-qom.h |  1 +
 target-s390x/cpu.c     | 73 +++++++++++++++++++++++++++++++++++++++++++++++---
 target-s390x/cpu.h     |  2 ++
 target-s390x/helper.c  | 41 ++++++++++++++++++++++++++--
 5 files changed, 112 insertions(+), 7 deletions(-)
hw/s390x/s390-virtio.c +1 -1
index f00d6b4..4ea9040 100644
@@ -116,7 +116,7 @@ void s390_init_cpus(MachineState *machine)
     }
 
     for (i = 0; i < smp_cpus; i++) {
-        cpu_s390x_init(machine->cpu_model);
+        s390x_new_cpu(machine->cpu_model, i, &error_fatal);
     }
 }
 
target-s390x/cpu-qom.h +1 -0
index 56d82f2..1c90933 100644
@@ -68,6 +68,7 @@ typedef struct S390CPU {
     /*< public >*/
 
     CPUS390XState env;
+    int64_t id;
     /* needed for live migration */
     void *irqstate;
     uint32_t irqstate_saved_size;
target-s390x/cpu.c +69 -4

106 lines not on this page · raw message

target-s390x/cpu.h +2 -0
index 49c8415..6d97c08 100644
@@ -413,6 +413,8 @@ void trigger_pgm_exception(CPUS390XState *env, uint32_t code, uint32_t ilen);
 #endif
 
 S390CPU *cpu_s390x_init(const char *cpu_model);
+S390CPU *s390x_new_cpu(const char *cpu_model, int64_t id, Error **errp);
+S390CPU *cpu_s390x_create(const char *cpu_model, Error **errp);
 void s390x_translate_init(void);
 int cpu_s390x_exec(CPUState *cpu);
 
target-s390x/helper.c +39 -2

53 lines not on this page · raw message

Signature · 1 line
1.9.1
#6 lore raw

PATCH v9 5/7 s390x/cpu: Add CPU property links

In reply to Matthew Rosato

Link each CPUState as property machine/cpu[n] during initialization. Add a hotplug handler to s390-virtio-ccw machine and set the state during plug.

---
 hw/s390x/s390-virtio-ccw.c | 34 ++++++++++++++++++++++++++++++++++
 hw/s390x/s390-virtio.c     | 17 ++++++++++++-----
 2 files changed, 46 insertions(+), 5 deletions(-)
hw/s390x/s390-virtio-ccw.c +34 -0
index b05ed8b..7fc1879 100644
@@ -156,10 +156,41 @@ static void ccw_init(MachineState *machine)
                     gtod_save, gtod_load, kvm_state);
 }
 
+static void s390_cpu_plug(HotplugHandler *hotplug_dev,
+                        DeviceState *dev, Error **errp)
+{
+    gchar *name;
+    S390CPU *cpu = S390_CPU(dev);
+    CPUState *cs = CPU(dev);
+
+    name = g_strdup_printf("cpu[%i]", cpu->env.cpu_num);
+    object_property_set_link(OBJECT(hotplug_dev), OBJECT(cs), name,
+                             errp);
+    g_free(name);
+}
+
+static void s390_machine_device_plug(HotplugHandler *hotplug_dev,
+                                     DeviceState *dev, Error **errp)
+{
+    if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
+        s390_cpu_plug(hotplug_dev, dev, errp);
+    }
+}
+
+static HotplugHandler *s390_get_hotplug_handler(MachineState *machine,
+                                                DeviceState *dev)
+{
+    if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
+        return HOTPLUG_HANDLER(machine);
+    }
+    return NULL;
+}
+
 static void ccw_machine_class_init(ObjectClass *oc, void *data)
 {
     MachineClass *mc = MACHINE_CLASS(oc);
     NMIClass *nc = NMI_CLASS(oc);
+    HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
 
     mc->init = ccw_init;
     mc->reset = s390_machine_reset;
@@ -171,6 +202,8 @@ static void ccw_machine_class_init(ObjectClass *oc, void *data)
     mc->no_sdcard = 1;
     mc->use_sclp = 1;
     mc->max_cpus = 255;
+    mc->get_hotplug_handler = s390_get_hotplug_handler;
+    hc->plug = s390_machine_device_plug;
     nc->nmi_monitor_handler = s390_nmi;
 }
 
@@ -232,6 +265,7 @@ static const TypeInfo ccw_machine_info = {
     .class_init    = ccw_machine_class_init,
     .interfaces = (InterfaceInfo[]) {
         { TYPE_NMI },
+        { TYPE_HOTPLUG_HANDLER},
         { }
     },
 };
hw/s390x/s390-virtio.c +12 -5
index 57c3c88..f00d6b4 100644
@@ -97,6 +97,7 @@ void s390_init_ipl_dev(const char *kernel_filename,
 void s390_init_cpus(MachineState *machine)
 {
     int i;
+    gchar *name;
 
     if (machine->cpu_model == NULL) {
         machine->cpu_model = "host";
@@ -104,12 +105,18 @@ void s390_init_cpus(MachineState *machine)
 
     cpu_states = g_malloc0(sizeof(S390CPU *) * max_cpus);
 
-    for (i = 0; i < smp_cpus; i++) {
-        S390CPU *cpu;
-
-        cpu = cpu_s390x_init(machine->cpu_model);
+    for (i = 0; i < max_cpus; i++) {
+        name = g_strdup_printf("cpu[%i]", i);
+        object_property_add_link(OBJECT(machine), name, TYPE_S390_CPU,
+                                 (Object **) &cpu_states[i],
+                                 object_property_allow_set_link,
+                                 OBJ_PROP_LINK_UNREF_ON_RELEASE,
+                                 &error_abort);
+        g_free(name);
+    }
 
-        cpu_states[i] = cpu;
+    for (i = 0; i < smp_cpus; i++) {
+        cpu_s390x_init(machine->cpu_model);
     }
 }
 
Signature · 1 line
1.9.1
#9 lore raw

PATCH v9 7/7 s390x/cpu: Allow hotplug of CPUs

In reply to Matthew Rosato

Implement cpu hotplug routine and add the machine hook.

---
 hw/s390x/s390-virtio-ccw.c | 10 ++++++++++
 target-s390x/cpu.c         |  7 +++++++
 2 files changed, 17 insertions(+)
hw/s390x/s390-virtio-ccw.c +10 -0
index 7fc1879..a84375b 100644
@@ -186,6 +186,15 @@ static HotplugHandler *s390_get_hotplug_handler(MachineState *machine,
     return NULL;
 }
 
+static void s390_hot_add_cpu(const int64_t id, Error **errp)
+{
+    MachineState *machine = MACHINE(qdev_get_machine());
+    Error *err = NULL;
+
+    s390x_new_cpu(machine->cpu_model, id, &err);
+    error_propagate(errp, err);
+}
+
 static void ccw_machine_class_init(ObjectClass *oc, void *data)
 {
     MachineClass *mc = MACHINE_CLASS(oc);
@@ -194,6 +203,7 @@ static void ccw_machine_class_init(ObjectClass *oc, void *data)
 
     mc->init = ccw_init;
     mc->reset = s390_machine_reset;
+    mc->hot_add_cpu = s390_hot_add_cpu;
     mc->block_default_type = IF_VIRTIO;
     mc->no_cdrom = 1;
     mc->no_floppy = 1;
target-s390x/cpu.c +7 -0
index 3ae38fa..1cbf703 100644
@@ -34,6 +34,7 @@
 #ifndef CONFIG_USER_ONLY
 #include "sysemu/arch_init.h"
 #include "sysemu/sysemu.h"
+#include "hw/s390x/sclp.h"
 #endif
 
 #define CR0_RESET       0xE0UL
@@ -240,6 +241,12 @@ static void s390_cpu_realizefn(DeviceState *dev, Error **errp)
 
     scc->parent_realize(dev, &err);
 
+#if !defined(CONFIG_USER_ONLY)
+    if (dev->hotplugged) {
+        raise_irq_cpu_hotplug();
+    }
+#endif
+
 out:
     error_propagate(errp, err);
 }
Signature · 1 line
1.9.1
Reviewed-by on v9 4/7
#5 lore raw

Re: [Qemu-devel] [PATCH v9 4/7] s390x/cpu: Tolerate max_cpus

In reply to Matthew Rosato
Quoted 5 lines

Once hotplug is enabled, interrupts may come in for CPUs with an address > smp_cpus. Allocate for this and allow search routines to look beyond smp_cpus.

Quoted 35 lines
---
 hw/s390x/s390-virtio.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
hw/s390x/s390-virtio.c +5 -4
index c501a48..57c3c88 100644
@@ -58,15 +58,16 @@
 #define S390_TOD_CLOCK_VALUE_MISSING    0x00
 #define S390_TOD_CLOCK_VALUE_PRESENT    0x01
 
-static S390CPU **ipi_states;
+static S390CPU **cpu_states;
 
 S390CPU *s390_cpu_addr2state(uint16_t cpu_addr)
 {
-    if (cpu_addr >= smp_cpus) {
+    if (cpu_addr >= max_cpus) {
         return NULL;
     }
 
-    return ipi_states[cpu_addr];
+    /* Fast lookup via CPU ID */
+    return cpu_states[cpu_addr];
 }
 
 void s390_init_ipl_dev(const char *kernel_filename,
@@ -101,14 +102,14 @@ void s390_init_cpus(MachineState *machine)
         machine->cpu_model = "host";
     }
 
-    ipi_states = g_malloc(sizeof(S390CPU *) * smp_cpus);
+    cpu_states = g_malloc0(sizeof(S390CPU *) * max_cpus);
 

hw/s390x/s390-virtio.c void s390_init_cpus(MachineState *machine) · line 115

Quoted 10 lines
hw/s390x/s390-virtio.c +1 -1
     for (i = 0; i < smp_cpus; i++) {
         S390CPU *cpu;
 
         cpu = cpu_s390x_init(machine->cpu_model);
 
-        ipi_states[i] = cpu;
+        cpu_states[i] = cpu;
     }
 }
 

David

#11 lore raw
In reply to Matthew Rosato
Quoted 38 lines · On Fri, 4 Mar 2016 12:34:28 -0500 Matthew Rosato…

As discussed in the KVM call, we will go ahead with cpu_add for s390x to get cpu hotplug functionality in s390x now, until architectures that require a more robust hotplug interface settle on a design.

To configure a guest with 2 CPUs online at
boot and 4 maximum:

qemu -smp 2,maxcpus=4

Or, when using libvirt:
<domain>

    ...
    <vcpu current="2">4</vcpu>
    ...

</domain>

To subsequently hotplug a CPU:

Issue 'cpu-add <id>' from qemu monitor, or use virsh setvcpus --count <n> <domain>, where <n> is the total number of desired guest CPUs.

At this point, the guest must bring the CPU online for use -- This can be achieved via "echo 1 > /sys/devices/system/cpu/cpuX/online" or via a management tool like cpuplugd.

This patch set is based on work previously done by Jason Herne.

Matthew Rosato (7):
s390x/cpu: Cleanup init in preparation for hotplug s390x/cpu: Set initial CPU state in common routine s390x/cpu: Get rid of side effects when creating a vcpu s390x/cpu: Tolerate max_cpus
s390x/cpu: Add CPU property links
s390x/cpu: Add error handling to cpu creation s390x/cpu: Allow hotplug of CPUs

Quoted 8 lines
 hw/s390x/s390-virtio-ccw.c | 46 +++++++++++++++++++++-
 hw/s390x/s390-virtio.c     | 36 +++++++++--------
 hw/s390x/s390-virtio.h     |  2 +-
 target-s390x/cpu-qom.h     |  3 ++
 target-s390x/cpu.c         | 96 +++++++++++++++++++++++++++++++++++++++++++---
 target-s390x/cpu.h         |  2 +
 target-s390x/helper.c      | 41 +++++++++++++++++++-
 7 files changed, 200 insertions(+), 26 deletions(-)

Thanks, applied to my s390-next branch.