Skip to contentSkip to conversation

pc: add CPU hot-add/hot-remove with device_add/device_del

PATCH In review Igor Mammedov 7 participants 40 messages Jul 14, 2016 – Aug 10, 2016

lore
Share & tools
lore
#0 lore raw

v4 cover pc: add CPU hot-add/hot-remove with device_add/device_del

Changelog:
since v3:
* rebase on top of x86-next tree as it's already applied 1-6/19 from v3 (so they are could be omitted from v4)
* set apic-state to NULL after destroying it in unrealize()
* fix places where I forgot to print X86CPUTopoInfo fields as %u
* move "pc: implement query-hotpluggable-cpus callback" after patch that enables device_add cpu
* extract counting of present cpus in possible_cpus into a separate helper
* newly added patches:
* pc: forbid BSP removal
* pc: enforce adding CPUs contiguously and removing them in opposit order
* apic: kvm-apic: fix crash due to access to freed memory region
* update Reviewed-bys
since v2:
* use 0xFFFFFFFF for UNASSIGNED_APIC_ID instead of UINT32_MAX
* add comment why 0xFFFFFFFF could be used for UNASSIGNED_APIC_ID
* print topo ids is unsigned
* print APIC ID as hex
* print topo ids calculated from APIC ID beside it
* add extra patch to fix migration failure due to APIC's instance_id mismatch
since v1:
* s/pc_find_cpu/pc_find_cpu_slot/ + add comment to it
* add more sanity checks for socket-id/core-id/thread-id and 'apic' properties
* include device_del cpu patches and related fixes to x86 CPU/apic

Series enabling usage of -device/device_add for adding CPUs as devices and device_del for removing them. Using -device/device_add in combination with query-hotpluggable-cpus QMP command allows to hotplug CPUs at any not used possition and then safely migrate QEMU instance by specifying hotadded CPUs on target with help of -device CLI option like with any other device.
Having been able to replicate exact topology on taggert with -device CPUs also opens poosibility to hot-remove CPUs, which this series does by enabling to use device_del with x86 CPUs.

git tree for testing:
https://github.com/imammedo/qemu.git dev_del_cpu_v4
for viewing:
https://github.com/imammedo/qemu/commits/dev_del_cpu_v4

Tested with RHEL7.2 guest including ping/pong migration with adding/removing CPUs in between.

Igor Mammedov (16):
pc: set APIC ID based on socket/core/thread ids if it's not been set
yet
pc: delay setting number of boot CPUs to machine_done time
pc: register created initial and hotpluged CPUs in one place

    pc_cpu_plug()
  pc: forbid BSP removal
  pc: enforce adding CPUs contiguously and removing them in opposit
    order
  pc: cpu: allow device_add to be used with x86 cpu
  pc: implement query-hotpluggable-cpus callback
  apic: move MAX_APICS check to 'apic' class
  apic: drop APICCommonState.idx and use APIC ID as index in
    local_apics[]

apic: kvm-apic: fix crash due to access to freed memory region (kvm)apic: add unrealize callbacks
apic: use apic_id as apic's migration instance_id
target-i386: cpu: do not ignore error and fix apic parent
target-i386: fix apic object leak when CPU is deleted
target-i386: add x86_cpu_unrealizefn()
pc: make device_del CPU work for x86 CPUs

 include/hw/i386/apic_internal.h |   5 +-
 include/hw/i386/pc.h            |   5 ++
 hw/i386/kvm/apic.c              |   9 +-
 hw/i386/pc.c                    | 183 ++++++++++++++++++++++++++++++++++------
 hw/intc/apic.c                  |  26 +++++-
 hw/intc/apic_common.c           |  33 ++++++--
 qmp-commands.hx                 |  15 ++++
 target-i386/cpu.c               |  23 ++++-
 8 files changed, 251 insertions(+), 48 deletions(-)
Signature · 1 line
2.7.4
#1 lore raw

PATCH v4 1/16 pc: set APIC ID based on socket/core/thread ids if it's not been set yet

CPU added with device_add help won't have APIC ID set, so set it according to socket/core/thread ids provided with device_add command.

---
v4:
- use %u for printing topo ids in if (!cpu_slot) branch
v3:
- use %u for printing topo ids
v2:
- add validity checks for socket-id/core-id/thread-id values

---
 hw/i386/pc.c | 44 +++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 41 insertions(+), 3 deletions(-)
hw/i386/pc.c +41 -3
index f07d38a..31b502c 100644
@@ -1792,14 +1792,52 @@ static void pc_cpu_pre_plug(HotplugHandler *hotplug_dev,
                             DeviceState *dev, Error **errp)
 {
     int idx;
+    CPUArchId *cpu_slot;
     X86CPUTopoInfo topo;
     X86CPU *cpu = X86_CPU(dev);
     PCMachineState *pcms = PC_MACHINE(hotplug_dev);
-    CPUArchId *cpu_slot = pc_find_cpu_slot(pcms, CPU(dev), &idx);
 
+    /* if APIC ID is not set, set it based on socket/core/thread properties */
+    if (cpu->apic_id == UNASSIGNED_APIC_ID) {
+        int max_socket = (max_cpus - 1) / smp_threads / smp_cores;
+
+        if (cpu->socket_id < 0) {
+            error_setg(errp, "CPU socket-id is not set");
+            return;
+        } else if (cpu->socket_id > max_socket) {
+            error_setg(errp, "Invalid CPU socket-id: %u must be in range 0:%u",
+                       cpu->socket_id, max_socket);
+            return;
+        }
+        if (cpu->core_id < 0) {
+            error_setg(errp, "CPU core-id is not set");
+            return;
+        } else if (cpu->core_id > (smp_cores - 1)) {
+            error_setg(errp, "Invalid CPU core-id: %u must be in range 0:%u",
+                       cpu->core_id, smp_cores - 1);
+            return;
+        }
+        if (cpu->thread_id < 0) {
+            error_setg(errp, "CPU thread-id is not set");
+            return;
+        } else if (cpu->thread_id > (smp_threads - 1)) {
+            error_setg(errp, "Invalid CPU thread-id: %u must be in range 0:%u",
+                       cpu->thread_id, smp_threads - 1);
+            return;
+        }
+
+        topo.pkg_id = cpu->socket_id;
+        topo.core_id = cpu->core_id;
+        topo.smt_id = cpu->thread_id;
+        cpu->apic_id = apicid_from_topo_ids(smp_cores, smp_threads, &topo);
+    }
+
+    cpu_slot = pc_find_cpu_slot(pcms, CPU(dev), &idx);
     if (!cpu_slot) {
-        error_setg(errp, "Invalid CPU index with APIC ID (%" PRIu32
-                   "), valid range 0:%d", cpu->apic_id,
+        x86_topo_ids_from_apicid(cpu->apic_id, smp_cores, smp_threads, &topo);
+        error_setg(errp, "Invalid CPU [socket: %u, core: %u, thread: %u] with"
+                  " APIC ID %" PRIu32 ", valid index range 0:%d",
+                   topo.pkg_id, topo.core_id, topo.smt_id, cpu->apic_id,
                    pcms->possible_cpus->len - 1);
         return;
     }
Signature · 1 line
2.7.4
#2 lore raw

PATCH v4 2/16 pc: delay setting number of boot CPUs to machine_done time

In reply to Igor Mammedov

currently present CPUs counter in CMOS only contains smp_cpus (i.e. initial CPUs specified with -smp X) and doesn't account for CPUs created with -device. If VM is started with additional CPUs added with
-device, it will hang in BIOS waiting for condition
smp_cpus == counted_cpus
forever as counted_cpus will include -device CPUs as well and be more than smp_cpus.

make present CPUs counter in CMOS to count all CPUs (initial and coldplugged with -device) by delaying it to machine done time when it possible to count CPUs added with -device.

---
v4:
- extract present cpus count into a separate function pc_present_cpus_count()

---
 hw/i386/pc.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
hw/i386/pc.c +14 -3
index 31b502c..d83fde4 100644
@@ -471,9 +471,6 @@ void pc_cmos_init(PCMachineState *pcms,
     rtc_set_memory(s, 0x5c, val >> 8);
     rtc_set_memory(s, 0x5d, val >> 16);
 
-    /* set the number of CPU */
-    rtc_set_memory(s, 0x5f, smp_cpus - 1);
-
     object_property_add_link(OBJECT(pcms), "rtc_state",
                              TYPE_ISA_DEVICE,
                              (Object **)&pcms->rtc,
@@ -1039,6 +1036,17 @@ void pc_acpi_smi_interrupt(void *opaque, int irq, int level)
     }
 }
 
+static int pc_present_cpus_count(PCMachineState *pcms)
+{
+    int i, boot_cpus = 0;
+    for (i = 0; i < pcms->possible_cpus->len; i++) {
+        if (pcms->possible_cpus->cpus[i].cpu) {
+            boot_cpus++;
+        }
+    }
+    return boot_cpus;
+}
+
 static X86CPU *pc_new_cpu(const char *typename, int64_t apic_id,
                           Error **errp)
 {
@@ -1189,6 +1197,9 @@ void pc_machine_done(Notifier *notifier, void *data)
                                         PCMachineState, machine_done);
     PCIBus *bus = pcms->bus;
 
+    /* set the number of CPUs */
+    rtc_set_memory(pcms->rtc, 0x5f, pc_present_cpus_count(pcms) - 1);
+
     if (bus) {
         int extra_hosts = 0;
 
Signature · 1 line
2.7.4
#4 lore raw

PATCH v4 3/16 pc: register created initial and hotpluged CPUs in one place pc_cpu_plug()

In reply to Igor Mammedov

consolidate possible_cpus array management in pc_cpu_plug() for smp_cpus, coldplugged with -device and hotplugged with device_add.

---
 hw/i386/pc.c | 25 +++++++++----------------
 1 file changed, 9 insertions(+), 16 deletions(-)
hw/i386/pc.c +9 -16
index d83fde4..5a67f15 100644
@@ -1153,7 +1153,6 @@ void pc_cpus_init(PCMachineState *pcms)
         if (i < smp_cpus) {
             cpu = pc_new_cpu(typename, x86_cpu_apic_id_from_index(i),
                              &error_fatal);
-            pcms->possible_cpus->cpus[i].cpu = CPU(cpu);
             object_unref(OBJECT(cpu));
         }
     }
@@ -1731,25 +1730,19 @@ static void pc_cpu_plug(HotplugHandler *hotplug_dev,
     Error *local_err = NULL;
     PCMachineState *pcms = PC_MACHINE(hotplug_dev);
 
-    if (!dev->hotplugged) {
-        goto out;
-    }
-
-    if (!pcms->acpi_dev) {
-        error_setg(&local_err,
-                   "cpu hotplug is not enabled: missing acpi device");
-        goto out;
+    if (pcms->acpi_dev) {
+        hhc = HOTPLUG_HANDLER_GET_CLASS(pcms->acpi_dev);
+        hhc->plug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
+        if (local_err) {
+            goto out;
+        }
     }
 
-    hhc = HOTPLUG_HANDLER_GET_CLASS(pcms->acpi_dev);
-    hhc->plug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
-    if (local_err) {
-        goto out;
+    if (dev->hotplugged) {
+        /* increment the number of CPUs */
+        rtc_set_memory(pcms->rtc, 0x5f, rtc_get_memory(pcms->rtc, 0x5f) + 1);
     }
 
-    /* increment the number of CPUs */
-    rtc_set_memory(pcms->rtc, 0x5f, rtc_get_memory(pcms->rtc, 0x5f) + 1);
-
     found_cpu = pc_find_cpu_slot(pcms, CPU(dev), NULL);
     found_cpu->cpu = CPU(dev);
 out:
Signature · 1 line
2.7.4
#5 lore raw

PATCH v4 4/16 pc: forbid BSP removal

In reply to Igor Mammedov

BSP is assumed to always present in QEMU code, so untile that assumptions are gone, deny removal request. In another words QEMU won't support BSP hot-unplug.

---
 hw/i386/pc.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/i386/pc.c +7 -0
index 5a67f15..33c5f97 100644
@@ -1751,10 +1751,17 @@ out:
 static void pc_cpu_unplug_request_cb(HotplugHandler *hotplug_dev,
                                      DeviceState *dev, Error **errp)
 {
+    int idx;
     HotplugHandlerClass *hhc;
     Error *local_err = NULL;
     PCMachineState *pcms = PC_MACHINE(hotplug_dev);
 
+    pc_find_cpu_slot(pcms, CPU(dev), &idx);
+    if (idx == 0) {
+        error_setg(&local_err, "1st CPU (BSP) is unpluggable");
+        goto out;
+    }
+
     hhc = HOTPLUG_HANDLER_GET_CLASS(pcms->acpi_dev);
     hhc->unplug_request(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
 
Signature · 1 line
2.7.4
#14 lore raw

PATCH v4 5/16 pc: enforce adding CPUs contiguously and removing them in opposit order

In reply to Igor Mammedov

it will still allow us to use cpu_index as migration instance_id since when CPUs are added contiguously (from the first to the last) and removed in opposite order, cpu_index stays stable and it's reproducable on destination side.

---
While there is work in progress to support migration when there are holes in cpu_index range resulting from out-of-order plug or unplug, this patch is intended as a last resort if no easy, risk-free and elegant solution emerges before 2.7 dev cycle ends.

---
 hw/i386/pc.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
hw/i386/pc.c +34 -0
index 33c5f97..75a92d0 100644
@@ -1762,6 +1762,23 @@ static void pc_cpu_unplug_request_cb(HotplugHandler *hotplug_dev,
         goto out;
     }
 
+    if (idx < pcms->possible_cpus->len - 1 &&
+        pcms->possible_cpus->cpus[idx + 1].cpu != NULL) {
+        X86CPU *cpu;
+
+        for (idx = pcms->possible_cpus->len - 1;
+             pcms->possible_cpus->cpus[idx].cpu == NULL; idx--) {
+            ;;
+        }
+
+        cpu = X86_CPU(pcms->possible_cpus->cpus[idx].cpu);
+        error_setg(&local_err, "CPU [socket-id: %u, core-id: %u,"
+                   " thread-id: %u] should be removed first",
+                   cpu->socket_id, cpu->core_id, cpu->thread_id);
+        goto out;
+
+    }
+
     hhc = HOTPLUG_HANDLER_GET_CLASS(pcms->acpi_dev);
     hhc->unplug_request(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
 
@@ -1860,6 +1877,23 @@ static void pc_cpu_pre_plug(HotplugHandler *hotplug_dev,
         return;
     }
 
+    if (idx != 0 && pcms->possible_cpus->cpus[idx - 1].cpu == NULL) {
+        PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
+
+        for (idx = 1; pcms->possible_cpus->cpus[idx].cpu != NULL; idx++) {
+            ;;
+        }
+
+        x86_topo_ids_from_apicid(pcms->possible_cpus->cpus[idx].arch_id,
+                                 smp_cores, smp_threads, &topo);
+
+        if (!pcmc->legacy_cpu_hotplug) {
+            error_setg(errp, "CPU [socket: %u, core: %u, thread: %u] should be"
+                       " added first", topo.pkg_id, topo.core_id, topo.smt_id);
+            return;
+        }
+    }
+
     /* if 'address' properties socket-id/core-id/thread-id are not set, set them
      * so that query_hotpluggable_cpus would show correct values
      */
Signature · 1 line
2.7.4
#22 lore raw

PATCH v4 6/16 pc: cpu: allow device_add to be used with x86 cpu

In reply to Igor Mammedov
---
 target-i386/cpu.c | 1 +
 1 file changed, 1 insertion(+)
target-i386/cpu.c +1 -0
index 91396e8..c6d8704 100644
@@ -3397,6 +3397,7 @@ static void x86_cpu_common_class_init(ObjectClass *oc, void *data)
     cc->cpu_exec_enter = x86_cpu_exec_enter;
     cc->cpu_exec_exit = x86_cpu_exec_exit;
 
+    dc->cannot_instantiate_with_device_add_yet = false;
     /*
      * Reason: x86_cpu_initfn() calls cpu_exec_init(), which saves the
      * object in cpus -> dangling pointer after final object_unref().
Signature · 1 line
2.7.4
#23 lore raw

PATCH v4 7/16 pc: implement query-hotpluggable-cpus callback

In reply to Igor Mammedov

it returns a list of present/possible to hotplug CPU objects with a list of properties to use with device_add.

in PC case returned list would looks like:
-> { "execute": "query-hotpluggable-cpus" }
<- {"return": [

     {
        "type": "qemu64-x86_64-cpu", "vcpus-count": 1,
        "props": {"core-id": 0, "socket-id": 1, "thread-id": 0}
     },
     {
        "qom-path": "/machine/unattached/device[0]",
        "type": "qemu64-x86_64-cpu", "vcpus-count": 1,
        "props": {"core-id": 0, "socket-id": 0, "thread-id": 0}
     }

]}

---
v2:
- add -id suffix to socket/core/thread properties to match fixed schema

---
 hw/i386/pc.c    | 45 +++++++++++++++++++++++++++++++++++++++++++++
 qmp-commands.hx | 15 +++++++++++++++
 2 files changed, 60 insertions(+)
hw/i386/pc.c +45 -0
index 75a92d0..feb71f9 100644
@@ -2184,6 +2184,50 @@ static CPUArchIdList *pc_possible_cpu_arch_ids(MachineState *machine)
     return list;
 }
 
+static HotpluggableCPUList *pc_query_hotpluggable_cpus(MachineState *machine)
+{
+    int i;
+    CPUState *cpu;
+    HotpluggableCPUList *head = NULL;
+    PCMachineState *pcms = PC_MACHINE(machine);
+    const char *cpu_type;
+
+    cpu = pcms->possible_cpus->cpus[0].cpu;
+    assert(cpu); /* BSP is always present */
+    cpu_type = object_class_get_name(OBJECT_CLASS(CPU_GET_CLASS(cpu)));
+
+    for (i = 0; i < pcms->possible_cpus->len; i++) {
+        X86CPUTopoInfo topo;
+        HotpluggableCPUList *list_item = g_new0(typeof(*list_item), 1);
+        HotpluggableCPU *cpu_item = g_new0(typeof(*cpu_item), 1);
+        CpuInstanceProperties *cpu_props = g_new0(typeof(*cpu_props), 1);
+        const uint32_t apic_id = pcms->possible_cpus->cpus[i].arch_id;
+
+        x86_topo_ids_from_apicid(apic_id, smp_cores, smp_threads, &topo);
+
+        cpu_item->type = g_strdup(cpu_type);
+        cpu_item->vcpus_count = 1;
+        cpu_props->has_socket_id = true;
+        cpu_props->socket_id = topo.pkg_id;
+        cpu_props->has_core_id = true;
+        cpu_props->core_id = topo.core_id;
+        cpu_props->has_thread_id = true;
+        cpu_props->thread_id = topo.smt_id;
+        cpu_item->props = cpu_props;
+
+        cpu = pcms->possible_cpus->cpus[i].cpu;
+        if (cpu) {
+            cpu_item->has_qom_path = true;
+            cpu_item->qom_path = object_get_canonical_path(OBJECT(cpu));
+        }
+
+        list_item->value = cpu_item;
+        list_item->next = head;
+        head = list_item;
+    }
+    return head;
+}
+
 static void x86_nmi(NMIState *n, int cpu_index, Error **errp)
 {
     /* cpu index isn't used */
@@ -2224,6 +2268,7 @@ static void pc_machine_class_init(ObjectClass *oc, void *data)
     mc->get_hotplug_handler = pc_get_hotpug_handler;
     mc->cpu_index_to_socket_id = pc_cpu_index_to_socket_id;
     mc->possible_cpu_arch_ids = pc_possible_cpu_arch_ids;
+    mc->query_hotpluggable_cpus = pc_query_hotpluggable_cpus;
     mc->default_boot_order = "cad";
     mc->hot_add_cpu = pc_hot_add_cpu;
     mc->max_cpus = 255;
qmp-commands.hx +15 -0
index 6937e83..3afe066 100644
@@ -4983,3 +4983,18 @@ Example for pseries machine type started with
      { "props": { "core-id": 0 }, "type": "POWER8-spapr-cpu-core",
        "vcpus-count": 1, "qom-path": "/machine/unattached/device[0]"}
    ]}'
+
+Example for pc machine type started with
+-smp 1,maxcpus=2:
+    -> { "execute": "query-hotpluggable-cpus" }
+    <- {"return": [
+         {
+            "type": "qemu64-x86_64-cpu", "vcpus-count": 1,
+            "props": {"core-id": 0, "socket-id": 1, "thread-id": 0}
+         },
+         {
+            "qom-path": "/machine/unattached/device[0]",
+            "type": "qemu64-x86_64-cpu", "vcpus-count": 1,
+            "props": {"core-id": 0, "socket-id": 0, "thread-id": 0}
+         }
+       ]}
Signature · 1 line
2.7.4
#27 lore raw

PATCH v4 9/16 apic: drop APICCommonState.idx and use APIC ID as index in local_apics[]

In reply to Igor Mammedov

local_apics[] is sized to contain all APIC ID supported in xAPIC mode, so use APIC ID as index in it instead of constantly increasing counter idx.

Fixes error "apic initialization failed" when a CPU hotplugged and unplugged more times than there are free slots in local_apics[].

---
CC: Radim Krčmář <[email protected]>
CC: [email protected]

---
 include/hw/i386/apic_internal.h |  1 -
 hw/intc/apic.c                  | 16 +++++++---------
 2 files changed, 7 insertions(+), 10 deletions(-)
include/hw/i386/apic_internal.h +0 -1
index 67348e9..8330592 100644
@@ -174,7 +174,6 @@ struct APICCommonState {
     uint32_t initial_count;
     int64_t initial_count_load_time;
     int64_t next_time;
-    int idx; /* not actually common, used only by 'apic' derived class */
     QEMUTimer *timer;
     int64_t timer_expiry;
     int sipi_vector;
hw/intc/apic.c +7 -9
index b0d237b..f473572 100644
@@ -421,7 +421,7 @@ static int apic_find_dest(uint8_t dest)
     int i;
 
     if (apic && apic->id == dest)
-        return dest;  /* shortcut in case apic->id == apic->idx */
+        return dest;  /* shortcut in case apic->id == local_apics[dest]->id */
 
     for (i = 0; i < MAX_APICS; i++) {
         apic = local_apics[i];
@@ -504,14 +504,14 @@ static void apic_deliver(DeviceState *dev, uint8_t dest, uint8_t dest_mode,
         break;
     case 1:
         memset(deliver_bitmask, 0x00, sizeof(deliver_bitmask));
-        apic_set_bit(deliver_bitmask, s->idx);
+        apic_set_bit(deliver_bitmask, s->id);
         break;
     case 2:
         memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask));
         break;
     case 3:
         memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask));
-        apic_reset_bit(deliver_bitmask, s->idx);
+        apic_reset_bit(deliver_bitmask, s->id);
         break;
     }
 
@@ -871,20 +871,18 @@ static const MemoryRegionOps apic_io_ops = {
 static void apic_realize(DeviceState *dev, Error **errp)
 {
     APICCommonState *s = APIC_COMMON(dev);
-    static int apic_no;
 
-    if (apic_no >= MAX_APICS) {
-        error_setg(errp, "%s initialization failed.",
-                   object_get_typename(OBJECT(dev)));
+    if (s->id >= MAX_APICS) {
+        error_setg(errp, "%s initialization failed. APIC ID %d is invalid",
+                   object_get_typename(OBJECT(dev)), s->id);
         return;
     }
-    s->idx = apic_no++;
 
     memory_region_init_io(&s->io_memory, OBJECT(s), &apic_io_ops, s, "apic-msi",
                           APIC_SPACE_SIZE);
 
     s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, apic_timer, s);
-    local_apics[s->idx] = s;
+    local_apics[s->id] = s;
 
     msi_nonbroken = true;
 }
Signature · 1 line
2.7.4
#25 lore raw

PATCH v4 8/16 apic: move MAX_APICS check to 'apic' class

In reply to Igor Mammedov

MAX_APICS is only used by child 'apic' class and not by its parent TYPE_APIC_COMMON or any other derived class.
Move check into end user 'apic' class so it won't get in the way of other APIC implementations if they support more then MAX_APICS.

---
CC: Radim Krčmář <[email protected]>
CC: [email protected]

---
 include/hw/i386/apic_internal.h |  4 +---
 hw/intc/apic.c                  | 10 ++++++++++
 hw/intc/apic_common.c           |  8 --------
 3 files changed, 11 insertions(+), 11 deletions(-)
include/hw/i386/apic_internal.h +1 -3
index 73ce716..67348e9 100644
@@ -121,8 +121,6 @@
 #define VAPIC_ENABLE_BIT                0
 #define VAPIC_ENABLE_MASK               (1 << VAPIC_ENABLE_BIT)
 
-#define MAX_APICS 255
-
 typedef struct APICCommonState APICCommonState;
 
 #define TYPE_APIC_COMMON "apic-common"
@@ -176,7 +174,7 @@ struct APICCommonState {
     uint32_t initial_count;
     int64_t initial_count_load_time;
     int64_t next_time;
-    int idx;
+    int idx; /* not actually common, used only by 'apic' derived class */
     QEMUTimer *timer;
     int64_t timer_expiry;
     int sipi_vector;
hw/intc/apic.c +10 -0
index e1ab935..b0d237b 100644
@@ -28,7 +28,9 @@
 #include "trace.h"
 #include "hw/i386/pc.h"
 #include "hw/i386/apic-msidef.h"
+#include "qapi/error.h"
 
+#define MAX_APICS 255
 #define MAX_APIC_WORDS 8
 
 #define SYNC_FROM_VAPIC                 0x1
@@ -869,6 +871,14 @@ static const MemoryRegionOps apic_io_ops = {
 static void apic_realize(DeviceState *dev, Error **errp)
 {
     APICCommonState *s = APIC_COMMON(dev);
+    static int apic_no;
+
+    if (apic_no >= MAX_APICS) {
+        error_setg(errp, "%s initialization failed.",
+                   object_get_typename(OBJECT(dev)));
+        return;
+    }
+    s->idx = apic_no++;
 
     memory_region_init_io(&s->io_memory, OBJECT(s), &apic_io_ops, s, "apic-msi",
                           APIC_SPACE_SIZE);
hw/intc/apic_common.c +0 -8
index e6eb694..fd425d1 100644
@@ -299,14 +299,6 @@ static void apic_common_realize(DeviceState *dev, Error **errp)
     APICCommonState *s = APIC_COMMON(dev);
     APICCommonClass *info;
     static DeviceState *vapic;
-    static int apic_no;
-
-    if (apic_no >= MAX_APICS) {
-        error_setg(errp, "%s initialization failed.",
-                   object_get_typename(OBJECT(dev)));
-        return;
-    }
-    s->idx = apic_no++;
 
     info = APIC_COMMON_GET_CLASS(s);
     info->realize(dev, errp);
Signature · 1 line
2.7.4
#29 lore raw

PATCH v4 10/16 apic: kvm-apic: fix crash due to access to freed memory region

In reply to Igor Mammedov

kvm-apic.io_memory memory region had its parent set to NULL at memory_region_init_io() time, so it ended up as a child in
/unattached contaner.
As result when kvm-apic instance was deleted, the child property
/unattached/kvm-apic-msi[XXX] contained a reference to
kvm-apic.io_memory address which was freed as part of kvm-apic.

Do the same as 'apic' and make kvm-apic instance the owner of the memory region so that it won't end up in /unattached and gets cleanly released along with related kvm-apic instance.

---
CC: Radim Krčmář <[email protected]>
CC: [email protected]

---
 hw/i386/kvm/apic.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/i386/kvm/apic.c +2 -2
index c5983c7..1f87e73 100644
@@ -184,8 +184,8 @@ static void kvm_apic_realize(DeviceState *dev, Error **errp)
 {
     APICCommonState *s = APIC_COMMON(dev);
 
-    memory_region_init_io(&s->io_memory, NULL, &kvm_apic_io_ops, s, "kvm-apic-msi",
-                          APIC_SPACE_SIZE);
+    memory_region_init_io(&s->io_memory, OBJECT(s), &kvm_apic_io_ops, s,
+                          "kvm-apic-msi", APIC_SPACE_SIZE);
 
     if (kvm_has_gsi_routing()) {
         msi_nonbroken = true;
Signature · 1 line
2.7.4
#30 lore raw

PATCH v4 11/16 (kvm)apic: add unrealize callbacks

In reply to Igor Mammedov

callbacks will do necessary cleanups before APIC device is deleted

---
CC: [email protected]

---
 include/hw/i386/apic_internal.h |  1 +
 hw/i386/kvm/apic.c              |  5 +++++
 hw/intc/apic.c                  | 10 ++++++++++
 hw/intc/apic_common.c           | 13 +++++++++++++
 4 files changed, 29 insertions(+)
include/hw/i386/apic_internal.h +1 -0
index 8330592..e549a62 100644
@@ -136,6 +136,7 @@ typedef struct APICCommonClass
     DeviceClass parent_class;
 
     DeviceRealize realize;
+    DeviceUnrealize unrealize;
     void (*set_base)(APICCommonState *s, uint64_t val);
     void (*set_tpr)(APICCommonState *s, uint8_t val);
     uint8_t (*get_tpr)(APICCommonState *s);
hw/i386/kvm/apic.c +5 -0
index 1f87e73..2bd0de8 100644
@@ -192,11 +192,16 @@ static void kvm_apic_realize(DeviceState *dev, Error **errp)
     }
 }
 
+static void kvm_apic_unrealize(DeviceState *dev, Error **errp)
+{
+}
+
 static void kvm_apic_class_init(ObjectClass *klass, void *data)
 {
     APICCommonClass *k = APIC_COMMON_CLASS(klass);
 
     k->realize = kvm_apic_realize;
+    k->unrealize = kvm_apic_unrealize;
     k->reset = kvm_apic_reset;
     k->set_base = kvm_apic_set_base;
     k->set_tpr = kvm_apic_set_tpr;
hw/intc/apic.c +10 -0
index f473572..45887d9 100644
@@ -887,11 +887,21 @@ static void apic_realize(DeviceState *dev, Error **errp)
     msi_nonbroken = true;
 }
 
+static void apic_unrealize(DeviceState *dev, Error **errp)
+{
+    APICCommonState *s = APIC_COMMON(dev);
+
+    timer_del(s->timer);
+    timer_free(s->timer);
+    local_apics[s->id] = NULL;
+}
+
 static void apic_class_init(ObjectClass *klass, void *data)
 {
     APICCommonClass *k = APIC_COMMON_CLASS(klass);
 
     k->realize = apic_realize;
+    k->unrealize = apic_unrealize;
     k->set_base = apic_set_base;
     k->set_tpr = apic_set_tpr;
     k->get_tpr = apic_get_tpr;
hw/intc/apic_common.c +13 -0
index fd425d1..0bb9ad5 100644
@@ -315,6 +315,18 @@ static void apic_common_realize(DeviceState *dev, Error **errp)
 
 }
 
+static void apic_common_unrealize(DeviceState *dev, Error **errp)
+{
+    APICCommonState *s = APIC_COMMON(dev);
+    APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
+
+    info->unrealize(dev, errp);
+
+    if (apic_report_tpr_access && info->enable_tpr_reporting) {
+        info->enable_tpr_reporting(s, false);
+    }
+}
+
 static int apic_pre_load(void *opaque)
 {
     APICCommonState *s = APIC_COMMON(opaque);
@@ -421,6 +433,7 @@ static void apic_common_class_init(ObjectClass *klass, void *data)
     dc->reset = apic_reset_common;
     dc->props = apic_properties_common;
     dc->realize = apic_common_realize;
+    dc->unrealize = apic_common_unrealize;
     /*
      * Reason: APIC and CPU need to be wired up by
      * x86_cpu_apic_create()
Signature · 1 line
2.7.4
#31 lore raw

PATCH v4 12/16 apic: use apic_id as apic's migration instance_id

In reply to Igor Mammedov

instance_id is generated by last_used_id + 1 for a given device type so for QEMU with 3 CPUs instance_id for APICs is a seti of [0, 1, 2] When CPU in the middle is hot-removed and migration started APICs with instance_ids 0 and 2 are transferred in migration stream. However target starts with 2 CPUs and APICs' instance_ids are generated from scratch [0, 1] hence migration fails with error
Unknown savevm section or instance 'apic' 2

Fix issue by manually registering APIC's vmsd with apic_id as instance_id, in this case instance_id on target will always match instance_id on source as apic_id is the same for a given cpu instance.

---
CC: [email protected]

---
 include/hw/i386/apic_internal.h |  1 +
 include/hw/i386/pc.h            |  5 +++++
 hw/intc/apic_common.c           | 12 +++++++++++-
 3 files changed, 17 insertions(+), 1 deletion(-)
include/hw/i386/apic_internal.h +1 -0
index e549a62..06c4e9f 100644
@@ -183,6 +183,7 @@ struct APICCommonState {
     uint32_t vapic_control;
     DeviceState *vapic;
     hwaddr vapic_paddr; /* note: persistence via kvmvapic */
+    bool legacy_instance_id;
 };
 
 typedef struct VAPICState {
include/hw/i386/pc.h +5 -0
index 7d9d03c..dc82e29 100644
@@ -378,6 +378,11 @@ bool e820_get_entry(int, uint32_t, uint64_t *, uint64_t *);
         .driver = TYPE_X86_CPU,\
         .property = "fill-mtrr-mask",\
         .value = "off",\
+    },\
+    {\
+        .driver   = "apic",\
+        .property = "legacy-instance-id",\
+        .value    = "on",\
     },
 
 #define PC_COMPAT_2_5 \
hw/intc/apic_common.c +11 -1
index 0bb9ad5..14ac43c 100644
@@ -294,11 +294,14 @@ static int apic_load_old(QEMUFile *f, void *opaque, int version_id)
     return 0;
 }
 
+static const VMStateDescription vmstate_apic_common;
+
 static void apic_common_realize(DeviceState *dev, Error **errp)
 {
     APICCommonState *s = APIC_COMMON(dev);
     APICCommonClass *info;
     static DeviceState *vapic;
+    int instance_id = s->id;
 
     info = APIC_COMMON_GET_CLASS(s);
     info->realize(dev, errp);
@@ -313,6 +316,11 @@ static void apic_common_realize(DeviceState *dev, Error **errp)
         info->enable_tpr_reporting(s, true);
     }
 
+    if (s->legacy_instance_id) {
+        instance_id = -1;
+    }
+    vmstate_register_with_alias_id(NULL, instance_id, &vmstate_apic_common,
+                                   s, -1, 0);
 }
 
 static void apic_common_unrealize(DeviceState *dev, Error **errp)
@@ -320,6 +328,7 @@ static void apic_common_unrealize(DeviceState *dev, Error **errp)
     APICCommonState *s = APIC_COMMON(dev);
     APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
 
+    vmstate_unregister(NULL, &vmstate_apic_common, s);
     info->unrealize(dev, errp);
 
     if (apic_report_tpr_access && info->enable_tpr_reporting) {
@@ -422,6 +431,8 @@ static Property apic_properties_common[] = {
     DEFINE_PROP_UINT8("version", APICCommonState, version, 0x14),
     DEFINE_PROP_BIT("vapic", APICCommonState, vapic_control, VAPIC_ENABLE_BIT,
                     true),
+    DEFINE_PROP_BOOL("legacy-instance-id", APICCommonState, legacy_instance_id,
+                     false),
     DEFINE_PROP_END_OF_LIST(),
 };
 
@@ -429,7 +440,6 @@ static void apic_common_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(klass);
 
-    dc->vmsd = &vmstate_apic_common;
     dc->reset = apic_reset_common;
     dc->props = apic_properties_common;
     dc->realize = apic_common_realize;
Signature · 1 line
2.7.4
#32 lore raw

PATCH v4 13/16 target-i386: cpu: do not ignore error and fix apic parent

In reply to Igor Mammedov

object_property_add_child() silently fails with error that it can't create duplicate propery 'apic' as we already have 'apic' property registered for 'apic' feature. As result generic device_realize puts apic into unattached container.

As it's programming error, abort if name collision happens in future and fix property name for apic_state to 'lapic', this way apic is a child of cpu instance.

---
 target-i386/cpu.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
target-i386/cpu.c +3 -2
index c6d8704..fb528de 100644
@@ -2829,8 +2829,9 @@ static void x86_cpu_apic_create(X86CPU *cpu, Error **errp)
 
     cpu->apic_state = DEVICE(object_new(apic_type));
 
-    object_property_add_child(OBJECT(cpu), "apic",
-                              OBJECT(cpu->apic_state), NULL);
+    object_property_add_child(OBJECT(cpu), "lapic",
+                              OBJECT(cpu->apic_state), &error_abort);
+
     qdev_prop_set_uint8(cpu->apic_state, "id", cpu->apic_id);
     /* TODO: convert to link<> */
     apic = APIC_COMMON(cpu->apic_state);
Signature · 1 line
2.7.4
#33 lore raw

PATCH v4 14/16 target-i386: fix apic object leak when CPU is deleted

In reply to Igor Mammedov
---
 target-i386/cpu.c | 1 +
 1 file changed, 1 insertion(+)
target-i386/cpu.c +1 -0
index fb528de..73ed447 100644
@@ -2831,6 +2831,7 @@ static void x86_cpu_apic_create(X86CPU *cpu, Error **errp)
 
     object_property_add_child(OBJECT(cpu), "lapic",
                               OBJECT(cpu->apic_state), &error_abort);
+    object_unref(OBJECT(cpu->apic_state));
 
     qdev_prop_set_uint8(cpu->apic_state, "id", cpu->apic_id);
     /* TODO: convert to link<> */
Signature · 1 line
2.7.4
#34 lore raw

PATCH v4 15/16 target-i386: add x86_cpu_unrealizefn()

In reply to Igor Mammedov

first remove VCPU from exec loop and only then remove lapic.

---
 target-i386/cpu.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
target-i386/cpu.c +16 -0
index 73ed447..f904671 100644
@@ -3117,6 +3117,21 @@ out:
     }
 }
 
+static void x86_cpu_unrealizefn(DeviceState *dev, Error **errp)
+{
+    X86CPU *cpu = X86_CPU(dev);
+
+#ifndef CONFIG_USER_ONLY
+    cpu_remove_sync(CPU(dev));
+    qemu_unregister_reset(x86_cpu_machine_reset_cb, dev);
+#endif
+
+    if (cpu->apic_state) {
+        object_unparent(OBJECT(cpu->apic_state));
+        cpu->apic_state = NULL;
+    }
+}
+
 typedef struct BitProperty {
     uint32_t *ptr;
     uint32_t mask;
@@ -3363,6 +3378,7 @@ static void x86_cpu_common_class_init(ObjectClass *oc, void *data)
 
     xcc->parent_realize = dc->realize;
     dc->realize = x86_cpu_realizefn;
+    dc->unrealize = x86_cpu_unrealizefn;
     dc->props = x86_cpu_properties;
 
     xcc->parent_reset = cc->reset;
Signature · 1 line
2.7.4
#36 lore raw

PATCH v4 16/16 pc: make device_del CPU work for x86 CPUs

In reply to Igor Mammedov

ACPI subsystem already has all logic in place the only thing left to eject CPU is destroy it and ammend present CPUs counter in CMOS, do so.

---
 hw/i386/pc.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
hw/i386/pc.c +5 -6
index feb71f9..dec74c9 100644
@@ -1794,6 +1794,7 @@ static void pc_cpu_unplug_request_cb(HotplugHandler *hotplug_dev,
 static void pc_cpu_unplug_cb(HotplugHandler *hotplug_dev,
                              DeviceState *dev, Error **errp)
 {
+    CPUArchId *found_cpu;
     HotplugHandlerClass *hhc;
     Error *local_err = NULL;
     PCMachineState *pcms = PC_MACHINE(hotplug_dev);
@@ -1805,13 +1806,11 @@ static void pc_cpu_unplug_cb(HotplugHandler *hotplug_dev,
         goto out;
     }
 
-    /*
-     * TODO: enable unplug once generic CPU remove bits land
-     * for now guest will be able to eject CPU ACPI wise but
-     * it will come back again on machine reset.
-     */
-    /*  object_unparent(OBJECT(dev)); */
+    found_cpu = pc_find_cpu_slot(pcms, CPU(dev), NULL);
+    found_cpu->cpu = NULL;
+    object_unparent(OBJECT(dev));
 
+    rtc_set_memory(pcms->rtc, 0x5f, rtc_get_memory(pcms->rtc, 0x5f) - 1);
  out:
     error_propagate(errp, local_err);
 }
Signature · 1 line
2.7.4
Reviewed-by on v4 2/16
#3 lore raw

Re: [Qemu-devel] [PATCH v4 02/16] pc: delay setting number of boot CPUs to machine_done time

In reply to Igor Mammedov
Quoted 9 lines · Igor Mammedov

currently present CPUs counter in CMOS only contains smp_cpus (i.e. initial CPUs specified with -smp X) and doesn't account for CPUs created with -device. If VM is started with additional CPUs added with
-device, it will hang in BIOS waiting for condition
smp_cpus == counted_cpus
forever as counted_cpus will include -device CPUs as well and be more than smp_cpus.

Quoted 6 lines

make present CPUs counter in CMOS to count all CPUs (initial and coldplugged with -device) by delaying it to machine done time when it possible to count CPUs added with -device.

Signature · 1 line
Eduardo
#6 lore raw

Re: [Qemu-devel] [PATCH v4 04/16] pc: forbid BSP removal

In reply to Igor Mammedov
Quoted 17 lines · Igor Mammedov

BSP is assumed to always present in QEMU code, so untile that assumptions are gone, deny removal request. In another words QEMU won't support BSP hot-unplug.

---
 hw/i386/pc.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/i386/pc.c +0 -0
index 5a67f15..33c5f97 100644
@@ -1751,10 +1751,17 @@ out:
 static void pc_cpu_unplug_request_cb(HotplugHandler *hotplug_dev,
                                      DeviceState *dev, Error **errp)
 {

hw/i386/pc.c out: · line 1763

Quoted 10 lines
hw/i386/pc.c +6 -0
+    int idx;
     HotplugHandlerClass *hhc;
     Error *local_err = NULL;
     PCMachineState *pcms = PC_MACHINE(hotplug_dev);
 
+    pc_find_cpu_slot(pcms, CPU(dev), &idx);
+    if (idx == 0) {
+        error_setg(&local_err, "1st CPU (BSP) is unpluggable");
+        goto out;
+    }

Nit: Boot CPU or simply Bootstrap Processor sounds better IMO.

Quoted 2 lines
hw/i386/pc.c +0 -0
     hhc = HOTPLUG_HANDLER_GET_CLASS(pcms->acpi_dev);
     hhc->unplug_request(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
#7 lore raw

Re: [Qemu-devel] [PATCH v4 04/16] pc: forbid BSP removal

In reply to Igor Mammedov
Quoted 14 lines · Igor Mammedov

BSP is assumed to always present in QEMU code, so untile that assumptions are gone, deny removal request. In another words QEMU won't support BSP hot-unplug.

---
 hw/i386/pc.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/i386/pc.c +0 -0
index 5a67f15..33c5f97 100644

hw/i386/pc.c out: · line 1759

Quoted 9 lines
hw/i386/pc.c +2 -0
@@ -1751,10 +1751,17 @@ out:
 static void pc_cpu_unplug_request_cb(HotplugHandler *hotplug_dev,
                                      DeviceState *dev, Error **errp)
 {
+    int idx;
     HotplugHandlerClass *hhc;
     Error *local_err = NULL;
     PCMachineState *pcms = PC_MACHINE(hotplug_dev);
 
+    pc_find_cpu_slot(pcms, CPU(dev), &idx);

Looks fragile: if one day we create any TYPE_CPU object that is not in possible_cpus array, idx is undefined. I suggest initializing idx to -1 above.

I can change it when committing, if that's OK for you.

Quoted 11 lines
hw/i386/pc.c +5 -0
+    if (idx == 0) {
+        error_setg(&local_err, "1st CPU (BSP) is unpluggable");
+        goto out;
+    }
+
     hhc = HOTPLUG_HANDLER_GET_CLASS(pcms->acpi_dev);
     hhc->unplug_request(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
 
Signature · 1 line
2.7.4
Signature · 1 line
Eduardo
#15 lore raw

Re: [Qemu-devel] [PATCH v4 05/16] pc: enforce adding CPUs contiguously and removing them in opposit order

In reply to Igor Mammedov
Quoted 11 lines · Igor Mammedov

it will still allow us to use cpu_index as migration instance_id since when CPUs are added contiguously (from the first to the last) and removed in opposite order, cpu_index stays stable and it's reproducable on destination side.

---
While there is work in progress to support migration when there are holes in cpu_index range resulting from out-of-order plug or unplug, this patch is intended as a last resort if no easy, risk-free and elegant solution emerges before 2.7 dev cycle ends.

I think this (or a modified version) is appropriate comment material to accompany the changes. Ok if you are sure this code is short-lived, but if it stays longer, a comment is definitely helpful. Maybe a bit of reasoning added to the error message is fine too.

Quoted 56 lines
---
 hw/i386/pc.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
hw/i386/pc.c +34 -0
index 33c5f97..75a92d0 100644
@@ -1762,6 +1762,23 @@ static void pc_cpu_unplug_request_cb(HotplugHandler *hotplug_dev,
         goto out;
     }
 
+    if (idx < pcms->possible_cpus->len - 1 &&
+        pcms->possible_cpus->cpus[idx + 1].cpu != NULL) {
+        X86CPU *cpu;
+
+        for (idx = pcms->possible_cpus->len - 1;
+             pcms->possible_cpus->cpus[idx].cpu == NULL; idx--) {
+            ;;
+        }
+
+        cpu = X86_CPU(pcms->possible_cpus->cpus[idx].cpu);
+        error_setg(&local_err, "CPU [socket-id: %u, core-id: %u,"
+                   " thread-id: %u] should be removed first",
+                   cpu->socket_id, cpu->core_id, cpu->thread_id);
+        goto out;
+
+    }
+
     hhc = HOTPLUG_HANDLER_GET_CLASS(pcms->acpi_dev);
     hhc->unplug_request(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
 
@@ -1860,6 +1877,23 @@ static void pc_cpu_pre_plug(HotplugHandler *hotplug_dev,
         return;
     }
 
+    if (idx != 0 && pcms->possible_cpus->cpus[idx - 1].cpu == NULL) {
+        PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
+
+        for (idx = 1; pcms->possible_cpus->cpus[idx].cpu != NULL; idx++) {
+            ;;
+        }
+
+        x86_topo_ids_from_apicid(pcms->possible_cpus->cpus[idx].arch_id,
+                                 smp_cores, smp_threads, &topo);
+
+        if (!pcmc->legacy_cpu_hotplug) {
+            error_setg(errp, "CPU [socket: %u, core: %u, thread: %u] should be"
+                       " added first", topo.pkg_id, topo.core_id, topo.smt_id);
+            return;
+        }
+    }
+
     /* if 'address' properties socket-id/core-id/thread-id are not set, set them
      * so that query_hotpluggable_cpus would show correct values
      */
#8 lore raw

Re: [Qemu-devel] [PATCH v4 04/16] pc: forbid BSP removal

In reply to Eduardo Habkost
Quoted 23 lines · Eduardo Habkost
Quoted 22 lines · Igor Mammedov

BSP is assumed to always present in QEMU code, so untile that assumptions are gone, deny removal request. In another words QEMU won't support BSP hot-unplug.

---
 hw/i386/pc.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/i386/pc.c +1 -0
index 5a67f15..33c5f97 100644
@@ -1751,10 +1751,17 @@ out:
 static void pc_cpu_unplug_request_cb(HotplugHandler *hotplug_dev,
                                      DeviceState *dev, Error **errp)
 {
+    int idx;
     HotplugHandlerClass *hhc;
     Error *local_err = NULL;
     PCMachineState *pcms = PC_MACHINE(hotplug_dev);
 
Quoted 5 lines

hw/i386/pc.c out: · line 1759

Quoted 1 line
hw/i386/pc.c +1 -0
+    pc_find_cpu_slot(pcms, CPU(dev), &idx);

Looks fragile: if one day we create any TYPE_CPU object that is not in possible_cpus array, idx is undefined. I suggest initializing idx to -1 above.

Or just let pc_find_cpu_slot universally set it to -1 since this series assumes that -1 means index isn't valid.

Quoted 14 lines

I can change it when committing, if that's OK for you.

Quoted 11 lines
hw/i386/pc.c +5 -0
+    if (idx == 0) {
+        error_setg(&local_err, "1st CPU (BSP) is unpluggable");
+        goto out;
+    }
+
     hhc = HOTPLUG_HANDLER_GET_CLASS(pcms->acpi_dev);
     hhc->unplug_request(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
 
Signature · 1 line
2.7.4
#9 lore raw

Re: [Qemu-devel] [PATCH v4 04/16] pc: forbid BSP removal

Quoted 25 lines · Bandan Das
Quoted 23 lines · Eduardo Habkost
Quoted 22 lines · Igor Mammedov

BSP is assumed to always present in QEMU code, so untile that assumptions are gone, deny removal request. In another words QEMU won't support BSP hot-unplug.

---
 hw/i386/pc.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/i386/pc.c +1 -0
index 5a67f15..33c5f97 100644
@@ -1751,10 +1751,17 @@ out:
 static void pc_cpu_unplug_request_cb(HotplugHandler *hotplug_dev,
                                      DeviceState *dev, Error **errp)
 {
+    int idx;
     HotplugHandlerClass *hhc;
     Error *local_err = NULL;
     PCMachineState *pcms = PC_MACHINE(hotplug_dev);
 
Quoted 8 lines
Quoted 5 lines

hw/i386/pc.c out: · line 1759

Quoted 1 line
hw/i386/pc.c +1 -0
+    pc_find_cpu_slot(pcms, CPU(dev), &idx);

Looks fragile: if one day we create any TYPE_CPU object that is not in possible_cpus array, idx is undefined. I suggest initializing idx to -1 above.

Or just let pc_find_cpu_slot universally set it to -1 since this series assumes that -1 means index isn't valid.

I think it would be more intuitive if pc_find_cpu_slot() didn't touch *idx if no slot is found. But both ways sound good to me.

Signature · 1 line
Eduardo
#10 lore raw

Re: [Qemu-devel] [PATCH v4 04/16] pc: forbid BSP removal

Eduardo Habkost <[email protected]> writes:
...

Quoted 13 lines
Quoted 13 lines
Quoted 12 lines

hw/i386/pc.c out: · line 1759

Quoted 8 lines

DeviceState *dev, Error **errp)
{

hw/i386/pc.c +2 -0
+    int idx;
     HotplugHandlerClass *hhc;
     Error *local_err = NULL;
     PCMachineState *pcms = PC_MACHINE(hotplug_dev);
 
+    pc_find_cpu_slot(pcms, CPU(dev), &idx);

Looks fragile: if one day we create any TYPE_CPU object that is not in possible_cpus array, idx is undefined. I suggest initializing idx to -1 above.

Quoted 5 lines
Quoted 2 lines

Or just let pc_find_cpu_slot universally set it to -1 since this series assumes that -1 means index isn't valid.

I think it would be more intuitive if pc_find_cpu_slot() didn't touch *idx if no slot is found. But both ways sound good to me.

The caller then has to always take care of making sure there is no bogus value in idx. Maybe, always calling if (pc_find_cpu_slot()) is better.

#11 lore raw

Re: [Qemu-devel] [PATCH v4 04/16] pc: forbid BSP removal

In reply to Eduardo Habkost
Quoted 23 lines · On Thu, 14 Jul 2016 14:54:53 -0300 Eduardo Habkost <[email protected]> wrote:
Quoted 22 lines · Igor Mammedov

BSP is assumed to always present in QEMU code, so untile that assumptions are gone, deny removal request. In another words QEMU won't support BSP hot-unplug.

---
 hw/i386/pc.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/i386/pc.c +1 -0
index 5a67f15..33c5f97 100644
@@ -1751,10 +1751,17 @@ out:
 static void pc_cpu_unplug_request_cb(HotplugHandler *hotplug_dev,
                                      DeviceState *dev, Error **errp)
 {
+    int idx;
     HotplugHandlerClass *hhc;
     Error *local_err = NULL;
     PCMachineState *pcms = PC_MACHINE(hotplug_dev);
 
Quoted 5 lines

hw/i386/pc.c out: · line 1759

Quoted 1 line
hw/i386/pc.c +1 -0
+    pc_find_cpu_slot(pcms, CPU(dev), &idx);  

Looks fragile: if one day we create any TYPE_CPU object that is not in possible_cpus array, idx is undefined. I suggest initializing idx to -1 above.

not that we expect that ever happen, but it won't hurt to initialize it to -1.

Quoted 2 lines

I can change it when committing, if that's OK for you.

sounds good to me, thanks.

Quoted 14 lines
Quoted 13 lines
hw/i386/pc.c +5 -0
+    if (idx == 0) {
+        error_setg(&local_err, "1st CPU (BSP) is unpluggable");
+        goto out;
+    }
+
     hhc = HOTPLUG_HANDLER_GET_CLASS(pcms->acpi_dev);
     hhc->unplug_request(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
 
Signature · 1 line
2.7.4
#16 lore raw

Re: [Qemu-devel] [PATCH v4 05/16] pc: enforce adding CPUs contiguously and removing them in opposit order

In reply to Bandan Das
Quoted 14 lines · On Thu, 14 Jul 2016 14:10:24 -0400 Bandan Das <[email protected]> wrote:
Quoted 11 lines · Igor Mammedov

it will still allow us to use cpu_index as migration instance_id since when CPUs are added contiguously (from the first to the last) and removed in opposite order, cpu_index stays stable and it's reproducable on destination side.

---
While there is work in progress to support migration when there are holes in cpu_index range resulting from out-of-order plug or unplug, this patch is intended as a last resort if no easy, risk-free and elegant solution emerges before 2.7 dev cycle ends.

Quoted 5 lines

I think this (or a modified version) is appropriate comment material to accompany the changes. Ok if you are sure this code is short-lived, but if it stays longer, a comment is definitely helpful. Maybe a bit of reasoning added to the error message is fine too.

dwg is looking at cpu_index refactoring but that's not 2.7 material, this patch is doing what the similar spapr patch did (which David applied to his ppc queue).

Perhaps moving comment under --- to commit message itself would be better as to leave trace of future refactoring plans.

Quoted 56 lines
Quoted 56 lines
---
 hw/i386/pc.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
hw/i386/pc.c +34 -0
index 33c5f97..75a92d0 100644
@@ -1762,6 +1762,23 @@ static void pc_cpu_unplug_request_cb(HotplugHandler *hotplug_dev,
         goto out;
     }
 
+    if (idx < pcms->possible_cpus->len - 1 &&
+        pcms->possible_cpus->cpus[idx + 1].cpu != NULL) {
+        X86CPU *cpu;
+
+        for (idx = pcms->possible_cpus->len - 1;
+             pcms->possible_cpus->cpus[idx].cpu == NULL; idx--) {
+            ;;
+        }
+
+        cpu = X86_CPU(pcms->possible_cpus->cpus[idx].cpu);
+        error_setg(&local_err, "CPU [socket-id: %u, core-id: %u,"
+                   " thread-id: %u] should be removed first",
+                   cpu->socket_id, cpu->core_id, cpu->thread_id);
+        goto out;
+
+    }
+
     hhc = HOTPLUG_HANDLER_GET_CLASS(pcms->acpi_dev);
     hhc->unplug_request(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
 
@@ -1860,6 +1877,23 @@ static void pc_cpu_pre_plug(HotplugHandler *hotplug_dev,
         return;
     }
 
+    if (idx != 0 && pcms->possible_cpus->cpus[idx - 1].cpu == NULL) {
+        PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
+
+        for (idx = 1; pcms->possible_cpus->cpus[idx].cpu != NULL; idx++) {
+            ;;
+        }
+
+        x86_topo_ids_from_apicid(pcms->possible_cpus->cpus[idx].arch_id,
+                                 smp_cores, smp_threads, &topo);
+
+        if (!pcmc->legacy_cpu_hotplug) {
+            error_setg(errp, "CPU [socket: %u, core: %u, thread: %u] should be"
+                       " added first", topo.pkg_id, topo.core_id, topo.smt_id);
+            return;
+        }
+    }
+
     /* if 'address' properties socket-id/core-id/thread-id are not set, set them
      * so that query_hotpluggable_cpus would show correct values
      */  
#17 lore raw

Re: [Qemu-devel] [PATCH v4 05/16] pc: enforce adding CPUs contiguously and removing them in opposit order

Quoted 22 lines · Igor Mammedov
Quoted 14 lines · On Thu, 14 Jul 2016 14:10:24 -0400 Bandan Das <[email protected]> wrote:
Quoted 11 lines · Igor Mammedov

it will still allow us to use cpu_index as migration instance_id since when CPUs are added contiguously (from the first to the last) and removed in opposite order, cpu_index stays stable and it's reproducable on destination side.

---
While there is work in progress to support migration when there are holes in cpu_index range resulting from out-of-order plug or unplug, this patch is intended as a last resort if no easy, risk-free and elegant solution emerges before 2.7 dev cycle ends.

Quoted 5 lines

I think this (or a modified version) is appropriate comment material to accompany the changes. Ok if you are sure this code is short-lived, but if it stays longer, a comment is definitely helpful. Maybe a bit of reasoning added to the error message is fine too.

Quoted 6 lines

dwg is looking at cpu_index refactoring but that's not 2.7 material, this patch is doing what the similar spapr patch did (which David applied to his ppc queue).

Perhaps moving comment under --- to commit message itself would be better as to leave trace of future refactoring plans.

Right, sounds good.

Quoted 56 lines
Quoted 56 lines
Quoted 56 lines
---
 hw/i386/pc.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
hw/i386/pc.c +34 -0
index 33c5f97..75a92d0 100644
@@ -1762,6 +1762,23 @@ static void pc_cpu_unplug_request_cb(HotplugHandler *hotplug_dev,
         goto out;
     }
 
+    if (idx < pcms->possible_cpus->len - 1 &&
+        pcms->possible_cpus->cpus[idx + 1].cpu != NULL) {
+        X86CPU *cpu;
+
+        for (idx = pcms->possible_cpus->len - 1;
+             pcms->possible_cpus->cpus[idx].cpu == NULL; idx--) {
+            ;;
+        }
+
+        cpu = X86_CPU(pcms->possible_cpus->cpus[idx].cpu);
+        error_setg(&local_err, "CPU [socket-id: %u, core-id: %u,"
+                   " thread-id: %u] should be removed first",
+                   cpu->socket_id, cpu->core_id, cpu->thread_id);
+        goto out;
+
+    }
+
     hhc = HOTPLUG_HANDLER_GET_CLASS(pcms->acpi_dev);
     hhc->unplug_request(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
 
@@ -1860,6 +1877,23 @@ static void pc_cpu_pre_plug(HotplugHandler *hotplug_dev,
         return;
     }
 
+    if (idx != 0 && pcms->possible_cpus->cpus[idx - 1].cpu == NULL) {
+        PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
+
+        for (idx = 1; pcms->possible_cpus->cpus[idx].cpu != NULL; idx++) {
+            ;;
+        }
+
+        x86_topo_ids_from_apicid(pcms->possible_cpus->cpus[idx].arch_id,
+                                 smp_cores, smp_threads, &topo);
+
+        if (!pcmc->legacy_cpu_hotplug) {
+            error_setg(errp, "CPU [socket: %u, core: %u, thread: %u] should be"
+                       " added first", topo.pkg_id, topo.core_id, topo.smt_id);
+            return;
+        }
+    }
+
     /* if 'address' properties socket-id/core-id/thread-id are not set, set them
      * so that query_hotpluggable_cpus would show correct values
      */  
#12 lore raw

PATCH v5 4/16 pc: forbid BSP removal

In reply to Igor Mammedov

Boot CPU is assumed to always present in QEMU code, so untile that assumptions are gone, deny removal request, In another words QEMU won't support BSP hot-unplug.

---
v5:
- s/1st CPU (BSP)/Boot CPU/
Eduardo Habkost <[email protected]>
- intialize idx to -1 and assert on it,
Eduardo Habkost <[email protected]>
(note: that should nexer happen in current code as we don't
have stray CPUs and most likely never will, but it doesn't hurt to cautios)

---
 hw/i386/pc.c | 8 ++++++++
 1 file changed, 8 insertions(+)
hw/i386/pc.c +8 -0
index 110f1bf..e15fcc1 100644
@@ -1757,10 +1757,18 @@ out:
 static void pc_cpu_unplug_request_cb(HotplugHandler *hotplug_dev,
                                      DeviceState *dev, Error **errp)
 {
+    int idx = -1;
     HotplugHandlerClass *hhc;
     Error *local_err = NULL;
     PCMachineState *pcms = PC_MACHINE(hotplug_dev);
 
+    pc_find_cpu_slot(pcms, CPU(dev), &idx);
+    assert(idx != -1);
+    if (idx == 0) {
+        error_setg(&local_err, "Boot CPU is unpluggable");
+        goto out;
+    }
+
     hhc = HOTPLUG_HANDLER_GET_CLASS(pcms->acpi_dev);
     hhc->unplug_request(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
 
Signature · 1 line
2.7.4
#18 lore raw

PATCH v5 5/16 pc: enforce adding CPUs contiguously and removing them in opposit order

In reply to Igor Mammedov

it will still allow us to use cpu_index as migration instance_id since when CPUs are added contiguously (from the first to the last) and removed in opposite order, cpu_index stays stable and it's reproducable on destination side.

While there is work in progress to support migration when there are holes in cpu_index range resulting from out-of-order plug or unplug, this patch is intended as an interim solution until cpu_index usage is cleaned up.

As result of this patch it would be possible to plug/unplug CPUs, but in limited order that doesn't break migration.

---
v5:
- move note from under --- to commit message itself so it won't be discarded along the way try to improve cmmit message Bandan Das <[email protected]>

---
 hw/i386/pc.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
hw/i386/pc.c +34 -0
index e15fcc1..dda91da 100644
@@ -1769,6 +1769,23 @@ static void pc_cpu_unplug_request_cb(HotplugHandler *hotplug_dev,
         goto out;
     }
 
+    if (idx < pcms->possible_cpus->len - 1 &&
+        pcms->possible_cpus->cpus[idx + 1].cpu != NULL) {
+        X86CPU *cpu;
+
+        for (idx = pcms->possible_cpus->len - 1;
+             pcms->possible_cpus->cpus[idx].cpu == NULL; idx--) {
+            ;;
+        }
+
+        cpu = X86_CPU(pcms->possible_cpus->cpus[idx].cpu);
+        error_setg(&local_err, "CPU [socket-id: %u, core-id: %u,"
+                   " thread-id: %u] should be removed first",
+                   cpu->socket_id, cpu->core_id, cpu->thread_id);
+        goto out;
+
+    }
+
     hhc = HOTPLUG_HANDLER_GET_CLASS(pcms->acpi_dev);
     hhc->unplug_request(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
 
@@ -1867,6 +1884,23 @@ static void pc_cpu_pre_plug(HotplugHandler *hotplug_dev,
         return;
     }
 
+    if (idx != 0 && pcms->possible_cpus->cpus[idx - 1].cpu == NULL) {
+        PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
+
+        for (idx = 1; pcms->possible_cpus->cpus[idx].cpu != NULL; idx++) {
+            ;;
+        }
+
+        x86_topo_ids_from_apicid(pcms->possible_cpus->cpus[idx].arch_id,
+                                 smp_cores, smp_threads, &topo);
+
+        if (!pcmc->legacy_cpu_hotplug) {
+            error_setg(errp, "CPU [socket: %u, core: %u, thread: %u] should be"
+                       " added first", topo.pkg_id, topo.core_id, topo.smt_id);
+            return;
+        }
+    }
+
     /* if 'address' properties socket-id/core-id/thread-id are not set, set them
      * so that query_hotpluggable_cpus would show correct values
      */
Signature · 1 line
2.7.4
Reviewed-by on v4 8/16
#26 lore raw

Re: [Qemu-devel] [PATCH v4 08/16] apic: move MAX_APICS check to 'apic' class

In reply to Igor Mammedov

2016-07-14 18:54+0200, Igor Mammedov:

Quoted 9 lines

MAX_APICS is only used by child 'apic' class and not by its parent TYPE_APIC_COMMON or any other derived class.
Move check into end user 'apic' class so it won't get in the way of other APIC implementations if they support more then MAX_APICS.

---

The other two APIC implementations, for KVM a Xen, don't seem to need this extra check.

Quoted 81 lines
CC Radim Krčmář <[email protected]>
---
 include/hw/i386/apic_internal.h |  4 +---
 hw/intc/apic.c                  | 10 ++++++++++
 hw/intc/apic_common.c           |  8 --------
 3 files changed, 11 insertions(+), 11 deletions(-)
include/hw/i386/apic_internal.h +1 -3
index 73ce716..67348e9 100644
@@ -121,8 +121,6 @@
 #define VAPIC_ENABLE_BIT                0
 #define VAPIC_ENABLE_MASK               (1 << VAPIC_ENABLE_BIT)
 
-#define MAX_APICS 255
-
 typedef struct APICCommonState APICCommonState;
 
 #define TYPE_APIC_COMMON "apic-common"
@@ -176,7 +174,7 @@ struct APICCommonState {
     uint32_t initial_count;
     int64_t initial_count_load_time;
     int64_t next_time;
-    int idx;
+    int idx; /* not actually common, used only by 'apic' derived class */
     QEMUTimer *timer;
     int64_t timer_expiry;
     int sipi_vector;
hw/intc/apic.c +10 -0
index e1ab935..b0d237b 100644
@@ -28,7 +28,9 @@
 #include "trace.h"
 #include "hw/i386/pc.h"
 #include "hw/i386/apic-msidef.h"
+#include "qapi/error.h"
 
+#define MAX_APICS 255
 #define MAX_APIC_WORDS 8
 
 #define SYNC_FROM_VAPIC                 0x1
@@ -869,6 +871,14 @@ static const MemoryRegionOps apic_io_ops = {
 static void apic_realize(DeviceState *dev, Error **errp)
 {
     APICCommonState *s = APIC_COMMON(dev);
+    static int apic_no;
+
+    if (apic_no >= MAX_APICS) {
+        error_setg(errp, "%s initialization failed.",
+                   object_get_typename(OBJECT(dev)));
+        return;
+    }
+    s->idx = apic_no++;
 
     memory_region_init_io(&s->io_memory, OBJECT(s), &apic_io_ops, s, "apic-msi",
                           APIC_SPACE_SIZE);
hw/intc/apic_common.c +0 -8
index e6eb694..fd425d1 100644
@@ -299,14 +299,6 @@ static void apic_common_realize(DeviceState *dev, Error **errp)
     APICCommonState *s = APIC_COMMON(dev);
     APICCommonClass *info;
     static DeviceState *vapic;
-    static int apic_no;
-
-    if (apic_no >= MAX_APICS) {
-        error_setg(errp, "%s initialization failed.",
-                   object_get_typename(OBJECT(dev)));
-        return;
-    }
-    s->idx = apic_no++;
 
     info = APIC_COMMON_GET_CLASS(s);
     info->realize(dev, errp);
Signature · 1 line
2.7.4
Reviewed-by on v4 9/16
#28 lore raw

Re: [Qemu-devel] [PATCH v4 09/16] apic: drop APICCommonState.idx and use APIC ID as index in local_apics[]

In reply to Igor Mammedov

2016-07-14 18:54+0200, Igor Mammedov:

Quoted 8 lines

local_apics[] is sized to contain all APIC ID supported in xAPIC mode, so use APIC ID as index in it instead of constantly increasing counter idx.

Fixes error "apic initialization failed" when a CPU hotplugged and unplugged more times than there are free slots in local_apics[].

---

The new method handles dynamic APIC ID changes made by the guest and id is set to the initial apic_id when realize is called, so it's even better than the previous one as we have higher chance of hitting the correct apic in local_apics[].

Quoted 24 lines
CC Radim Krčmář <[email protected]>
---
 include/hw/i386/apic_internal.h |  1 -
 hw/intc/apic.c                  | 16 +++++++---------
 2 files changed, 7 insertions(+), 10 deletions(-)
include/hw/i386/apic_internal.h +0 -1
index 67348e9..8330592 100644
@@ -174,7 +174,6 @@ struct APICCommonState {
     uint32_t initial_count;
     int64_t initial_count_load_time;
     int64_t next_time;
-    int idx; /* not actually common, used only by 'apic' derived class */
     QEMUTimer *timer;
     int64_t timer_expiry;
     int sipi_vector;
hw/intc/apic.c +0 -0
index b0d237b..f473572 100644

hw/intc/apic.c static int apic_find_dest(uint8_t dest) · line 427

Quoted 8 lines
hw/intc/apic.c +1 -1
@@ -421,7 +421,7 @@ static int apic_find_dest(uint8_t dest)
     int i;
 
     if (apic && apic->id == dest)
-        return dest;  /* shortcut in case apic->id == apic->idx */
+        return dest;  /* shortcut in case apic->id == local_apics[dest]->id */
 
     for (i = 0; i < MAX_APICS; i++) {
         apic = local_apics[i];

(We could also update local_apics[] when APIC ID changes and drop the
loop, but it's safer this way.)

Quoted 45 lines
hw/intc/apic.c +6 -8
@@ -504,14 +504,14 @@ static void apic_deliver(DeviceState *dev, uint8_t dest, uint8_t dest_mode,
         break;
     case 1:
         memset(deliver_bitmask, 0x00, sizeof(deliver_bitmask));
-        apic_set_bit(deliver_bitmask, s->idx);
+        apic_set_bit(deliver_bitmask, s->id);
         break;
     case 2:
         memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask));
         break;
     case 3:
         memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask));
-        apic_reset_bit(deliver_bitmask, s->idx);
+        apic_reset_bit(deliver_bitmask, s->id);
         break;
     }
 
@@ -871,20 +871,18 @@ static const MemoryRegionOps apic_io_ops = {
 static void apic_realize(DeviceState *dev, Error **errp)
 {
     APICCommonState *s = APIC_COMMON(dev);
-    static int apic_no;
 
-    if (apic_no >= MAX_APICS) {
-        error_setg(errp, "%s initialization failed.",
-                   object_get_typename(OBJECT(dev)));
+    if (s->id >= MAX_APICS) {
+        error_setg(errp, "%s initialization failed. APIC ID %d is invalid",
+                   object_get_typename(OBJECT(dev)), s->id);
         return;
     }
-    s->idx = apic_no++;
 
     memory_region_init_io(&s->io_memory, OBJECT(s), &apic_io_ops, s, "apic-msi",
                           APIC_SPACE_SIZE);
 
     s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, apic_timer, s);
-    local_apics[s->idx] = s;
+    local_apics[s->id] = s;
 
     msi_nonbroken = true;
 }
Signature · 1 line
2.7.4
#24 lore raw

Re: [Qemu-devel] [PATCH v4 07/16] pc: implement query-hotpluggable-cpus callback

In reply to Igor Mammedov
Quoted 16 lines · Igor Mammedov

it returns a list of present/possible to hotplug CPU objects with a list of properties to use with device_add.

in PC case returned list would looks like:
-> { "execute": "query-hotpluggable-cpus" }
<- {"return": [

     {
        "type": "qemu64-x86_64-cpu", "vcpus-count": 1,
        "props": {"core-id": 0, "socket-id": 1, "thread-id": 0}
     },
     {
        "qom-path": "/machine/unattached/device[0]",
        "type": "qemu64-x86_64-cpu", "vcpus-count": 1,
        "props": {"core-id": 0, "socket-id": 0, "thread-id": 0}
     }
Quoted 4 lines

]}

Shouldn't QMP maintainer be Cc'd?

Quoted 96 lines

---
v2:
- add -id suffix to socket/core/thread properties to match fixed schema

---
 hw/i386/pc.c    | 45 +++++++++++++++++++++++++++++++++++++++++++++
 qmp-commands.hx | 15 +++++++++++++++
 2 files changed, 60 insertions(+)
hw/i386/pc.c +45 -0
index 75a92d0..feb71f9 100644
@@ -2184,6 +2184,50 @@ static CPUArchIdList *pc_possible_cpu_arch_ids(MachineState *machine)
     return list;
 }
 
+static HotpluggableCPUList *pc_query_hotpluggable_cpus(MachineState *machine)
+{
+    int i;
+    CPUState *cpu;
+    HotpluggableCPUList *head = NULL;
+    PCMachineState *pcms = PC_MACHINE(machine);
+    const char *cpu_type;
+
+    cpu = pcms->possible_cpus->cpus[0].cpu;
+    assert(cpu); /* BSP is always present */
+    cpu_type = object_class_get_name(OBJECT_CLASS(CPU_GET_CLASS(cpu)));
+
+    for (i = 0; i < pcms->possible_cpus->len; i++) {
+        X86CPUTopoInfo topo;
+        HotpluggableCPUList *list_item = g_new0(typeof(*list_item), 1);
+        HotpluggableCPU *cpu_item = g_new0(typeof(*cpu_item), 1);
+        CpuInstanceProperties *cpu_props = g_new0(typeof(*cpu_props), 1);
+        const uint32_t apic_id = pcms->possible_cpus->cpus[i].arch_id;
+
+        x86_topo_ids_from_apicid(apic_id, smp_cores, smp_threads, &topo);
+
+        cpu_item->type = g_strdup(cpu_type);
+        cpu_item->vcpus_count = 1;
+        cpu_props->has_socket_id = true;
+        cpu_props->socket_id = topo.pkg_id;
+        cpu_props->has_core_id = true;
+        cpu_props->core_id = topo.core_id;
+        cpu_props->has_thread_id = true;
+        cpu_props->thread_id = topo.smt_id;
+        cpu_item->props = cpu_props;
+
+        cpu = pcms->possible_cpus->cpus[i].cpu;
+        if (cpu) {
+            cpu_item->has_qom_path = true;
+            cpu_item->qom_path = object_get_canonical_path(OBJECT(cpu));
+        }
+
+        list_item->value = cpu_item;
+        list_item->next = head;
+        head = list_item;
+    }
+    return head;
+}
+
 static void x86_nmi(NMIState *n, int cpu_index, Error **errp)
 {
     /* cpu index isn't used */
@@ -2224,6 +2268,7 @@ static void pc_machine_class_init(ObjectClass *oc, void *data)
     mc->get_hotplug_handler = pc_get_hotpug_handler;
     mc->cpu_index_to_socket_id = pc_cpu_index_to_socket_id;
     mc->possible_cpu_arch_ids = pc_possible_cpu_arch_ids;
+    mc->query_hotpluggable_cpus = pc_query_hotpluggable_cpus;
     mc->default_boot_order = "cad";
     mc->hot_add_cpu = pc_hot_add_cpu;
     mc->max_cpus = 255;
qmp-commands.hx +15 -0
index 6937e83..3afe066 100644
@@ -4983,3 +4983,18 @@ Example for pseries machine type started with
      { "props": { "core-id": 0 }, "type": "POWER8-spapr-cpu-core",
        "vcpus-count": 1, "qom-path": "/machine/unattached/device[0]"}
    ]}'
+
+Example for pc machine type started with
+-smp 1,maxcpus=2:
+    -> { "execute": "query-hotpluggable-cpus" }
+    <- {"return": [
+         {
+            "type": "qemu64-x86_64-cpu", "vcpus-count": 1,
+            "props": {"core-id": 0, "socket-id": 1, "thread-id": 0}
+         },
+         {
+            "qom-path": "/machine/unattached/device[0]",
+            "type": "qemu64-x86_64-cpu", "vcpus-count": 1,
+            "props": {"core-id": 0, "socket-id": 0, "thread-id": 0}
+         }
+       ]}
Signature · 1 line
2.7.4
#19 lore raw

Re: [Qemu-devel] [PATCH v4 05/16] pc: enforce adding CPUs contiguously and removing them in opposit order

In reply to Igor Mammedov

On 07/14/2016 10:54 AM, Igor Mammedov wrote:

s/opposit/opposite/ in the subject line, but it's already long. I wonder if you can go shorter, with:

pc: enforce CPU add/remove in contiguous order

Quoted 4 lines

it will still allow us to use cpu_index as migration instance_id since when CPUs are added contiguously (from the first to the last) and removed in opposite order, cpu_index stays stable and it's reproducable on destination side.

s/reproducable/reproducible/

Quoted 7 lines

---
While there is work in progress to support migration when there are holes in cpu_index range resulting from out-of-order plug or unplug, this patch is intended as a last resort if no easy, risk-free and elegant solution emerges before 2.7 dev cycle ends.

I'm not too worried about succeeding only on contiguous ids, as that has been something libvirt has already had to deal with. But I am a bit worried about whether it is easy to introspect whether 2.8 adds a way to hot-plug (or hot-remove) cpus in arbitrary order, with gaps rather than contiguous ids, especially if the interface does not gain any obvious parameters.

If we are going to enhance the interface in the future, do we have any plans how to make it easily detectable which version of the interface we are working with (contiguous-only, or full power)?

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Reviewed-by on the v4 series
#37 lore raw
In reply to Igor Mammedov
Quoted 1 line · Igor Mammedov

Changelog:

So for pc and apic bits:

Since Eduardo picked up first part of this, feel free to merge the rest too.

Quoted 85 lines

since v3:
* rebase on top of x86-next tree as it's already applied 1-6/19 from v3 (so they are could be omitted from v4)
* set apic-state to NULL after destroying it in unrealize()
* fix places where I forgot to print X86CPUTopoInfo fields as %u
* move "pc: implement query-hotpluggable-cpus callback" after patch that enables device_add cpu
* extract counting of present cpus in possible_cpus into a separate helper
* newly added patches:
* pc: forbid BSP removal
* pc: enforce adding CPUs contiguously and removing them in opposit order
* apic: kvm-apic: fix crash due to access to freed memory region
* update Reviewed-bys
since v2:
* use 0xFFFFFFFF for UNASSIGNED_APIC_ID instead of UINT32_MAX
* add comment why 0xFFFFFFFF could be used for UNASSIGNED_APIC_ID
* print topo ids is unsigned
* print APIC ID as hex
* print topo ids calculated from APIC ID beside it
* add extra patch to fix migration failure due to APIC's instance_id mismatch
since v1:
* s/pc_find_cpu/pc_find_cpu_slot/ + add comment to it
* add more sanity checks for socket-id/core-id/thread-id and 'apic' properties
* include device_del cpu patches and related fixes to x86 CPU/apic

Series enabling usage of -device/device_add for adding CPUs as devices and device_del for removing them. Using -device/device_add in combination with query-hotpluggable-cpus QMP command allows to hotplug CPUs at any not used possition and then safely migrate QEMU instance by specifying hotadded CPUs on target with help of -device CLI option like with any other device.
Having been able to replicate exact topology on taggert with -device CPUs also opens poosibility to hot-remove CPUs, which this series does by enabling to use device_del with x86 CPUs.

git tree for testing:
https://github.com/imammedo/qemu.git dev_del_cpu_v4
for viewing:
https://github.com/imammedo/qemu/commits/dev_del_cpu_v4

Tested with RHEL7.2 guest including ping/pong migration with adding/removing CPUs in between.

Igor Mammedov (16):
pc: set APIC ID based on socket/core/thread ids if it's not been set
yet
pc: delay setting number of boot CPUs to machine_done time
pc: register created initial and hotpluged CPUs in one place

    pc_cpu_plug()
  pc: forbid BSP removal
  pc: enforce adding CPUs contiguously and removing them in opposit
    order
  pc: cpu: allow device_add to be used with x86 cpu
  pc: implement query-hotpluggable-cpus callback
  apic: move MAX_APICS check to 'apic' class
  apic: drop APICCommonState.idx and use APIC ID as index in
    local_apics[]

apic: kvm-apic: fix crash due to access to freed memory region (kvm)apic: add unrealize callbacks
apic: use apic_id as apic's migration instance_id
target-i386: cpu: do not ignore error and fix apic parent
target-i386: fix apic object leak when CPU is deleted
target-i386: add x86_cpu_unrealizefn()
pc: make device_del CPU work for x86 CPUs

 include/hw/i386/apic_internal.h |   5 +-
 include/hw/i386/pc.h            |   5 ++
 hw/i386/kvm/apic.c              |   9 +-
 hw/i386/pc.c                    | 183 ++++++++++++++++++++++++++++++++++------
 hw/intc/apic.c                  |  26 +++++-
 hw/intc/apic_common.c           |  33 ++++++--
 qmp-commands.hx                 |  15 ++++
 target-i386/cpu.c               |  23 ++++-
 8 files changed, 251 insertions(+), 48 deletions(-)
Signature · 1 line
2.7.4
#20 lore raw

Re: [Qemu-devel] [PATCH v4 05/16] pc: enforce adding CPUs contiguously and removing them in opposit order

In reply to Eric Blake
Quoted 5 lines · On Mon, 18 Jul 2016 15:05:37 -0600 Eric Blake <[email protected]> wrote:

On 07/14/2016 10:54 AM, Igor Mammedov wrote:

s/opposit/opposite/ in the subject line, but it's already long. I wonder if you can go shorter, with:

Quoted 8 lines

pc: enforce CPU add/remove in contiguous order

Quoted 4 lines

it will still allow us to use cpu_index as migration instance_id since when CPUs are added contiguously (from the first to the last) and removed in opposite order, cpu_index stays stable and it's reproducable on destination side.

s/reproducable/reproducible/

Eduardo could you fix it up when applying or should I respin it?

Quoted 15 lines
Quoted 7 lines

---
While there is work in progress to support migration when there are holes in cpu_index range resulting from out-of-order plug or unplug, this patch is intended as a last resort if no easy, risk-free and elegant solution emerges before 2.7 dev cycle ends.

I'm not too worried about succeeding only on contiguous ids, as that has been something libvirt has already had to deal with. But I am a bit worried about whether it is easy to introspect whether 2.8 adds a way to hot-plug (or hot-remove) cpus in arbitrary order, with gaps rather than contiguous ids, especially if the interface does not gain any obvious parameters.

Quoted 3 lines

If we are going to enhance the interface in the future, do we have any plans how to make it easily detectable which version of the interface we are working with (contiguous-only, or full power)?

We are discussing on list if we should keep limitation for now and later add means to detect full vs limited interface or go other route and revert limitation patch exposing full interface. Lets wait for feedback from David.

Anyway this patch should be applied to be symmetric with what spapr does and if we decide to lift limitation then we should revert it for both spapr and x86.

#21 lore raw

Re: [Qemu-devel] [PATCH v4 05/16] pc: enforce adding CPUs contiguously and removing them in opposit order

Quoted 10 lines · Igor Mammedov
Quoted 7 lines · On Mon, 18 Jul 2016 15:05:37 -0600 Eric Blake <[email protected]> wrote:

On 07/14/2016 10:54 AM, Igor Mammedov wrote:

s/opposit/opposite/ in the subject line, but it's already long. I wonder if you can go shorter, with:

pc: enforce CPU add/remove in contiguous order

Quoted 7 lines
Quoted 6 lines
Quoted 4 lines

it will still allow us to use cpu_index as migration instance_id since when CPUs are added contiguously (from the first to the last) and removed in opposite order, cpu_index stays stable and it's reproducable on destination side.

s/reproducable/reproducible/

Eduardo could you fix it up when applying or should I respin it?

I can fix it.

Signature · 1 line
Eduardo
Reviewed-by on v5 4/16
#13 lore raw

Re: [Qemu-devel] [PATCH v5 04/16] pc: forbid BSP removal

In reply to Igor Mammedov
Quoted 5 lines · Igor Mammedov

Boot CPU is assumed to always present in QEMU code, so untile that assumptions are gone, deny removal request, In another words QEMU won't support BSP hot-unplug.

Quoted 39 lines

---
v5:
- s/1st CPU (BSP)/Boot CPU/
Eduardo Habkost <[email protected]>
- intialize idx to -1 and assert on it,
Eduardo Habkost <[email protected]>
(note: that should nexer happen in current code as we don't
have stray CPUs and most likely never will, but it doesn't hurt to cautios)

---
 hw/i386/pc.c | 8 ++++++++
 1 file changed, 8 insertions(+)
hw/i386/pc.c +8 -0
index 110f1bf..e15fcc1 100644
@@ -1757,10 +1757,18 @@ out:
 static void pc_cpu_unplug_request_cb(HotplugHandler *hotplug_dev,
                                      DeviceState *dev, Error **errp)
 {
+    int idx = -1;
     HotplugHandlerClass *hhc;
     Error *local_err = NULL;
     PCMachineState *pcms = PC_MACHINE(hotplug_dev);
 
+    pc_find_cpu_slot(pcms, CPU(dev), &idx);
+    assert(idx != -1);
+    if (idx == 0) {
+        error_setg(&local_err, "Boot CPU is unpluggable");
+        goto out;
+    }
+
     hhc = HOTPLUG_HANDLER_GET_CLASS(pcms->acpi_dev);
     hhc->unplug_request(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
 
Signature · 1 line
2.7.4
Signature · 1 line
Eduardo
Reviewed-by on v4 15/16
#35 lore raw

Re: [Qemu-devel] [PATCH v4 15/16] target-i386: add x86_cpu_unrealizefn()

In reply to Igor Mammedov
Quoted 6 lines · Igor Mammedov

first remove VCPU from exec loop and only then remove lapic.

Quoted 42 lines
---
 target-i386/cpu.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
target-i386/cpu.c +16 -0
index 73ed447..f904671 100644
@@ -3117,6 +3117,21 @@ out:
     }
 }
 
+static void x86_cpu_unrealizefn(DeviceState *dev, Error **errp)
+{
+    X86CPU *cpu = X86_CPU(dev);
+
+#ifndef CONFIG_USER_ONLY
+    cpu_remove_sync(CPU(dev));
+    qemu_unregister_reset(x86_cpu_machine_reset_cb, dev);
+#endif
+
+    if (cpu->apic_state) {
+        object_unparent(OBJECT(cpu->apic_state));
+        cpu->apic_state = NULL;
+    }
+}
+
 typedef struct BitProperty {
     uint32_t *ptr;
     uint32_t mask;
@@ -3363,6 +3378,7 @@ static void x86_cpu_common_class_init(ObjectClass *oc, void *data)
 
     xcc->parent_realize = dc->realize;
     dc->realize = x86_cpu_realizefn;
+    dc->unrealize = x86_cpu_unrealizefn;
     dc->props = x86_cpu_properties;
 
     xcc->parent_reset = cc->reset;
Signature · 1 line
2.7.4
Signature · 1 line
Eduardo
#38 lore raw
In reply to Michael S. Tsirkin
Quoted 9 lines · Michael S. Tsirkin
Quoted 1 line · Igor Mammedov

Changelog:

So for pc and apic bits:

Since Eduardo picked up first part of this, feel free to merge the rest too.

Hi Igor,

I was a little curios about the state of this feature. Is this going to make it to 2.7?

Best regards,

Quoted 88 lines
Quoted 88 lines

since v3:
* rebase on top of x86-next tree as it's already applied 1-6/19 from v3 (so they are could be omitted from v4)
* set apic-state to NULL after destroying it in unrealize()
* fix places where I forgot to print X86CPUTopoInfo fields as %u
* move "pc: implement query-hotpluggable-cpus callback" after patch that enables device_add cpu
* extract counting of present cpus in possible_cpus into a separate helper
* newly added patches:
* pc: forbid BSP removal
* pc: enforce adding CPUs contiguously and removing them in opposit order
* apic: kvm-apic: fix crash due to access to freed memory region
* update Reviewed-bys
since v2:
* use 0xFFFFFFFF for UNASSIGNED_APIC_ID instead of UINT32_MAX
* add comment why 0xFFFFFFFF could be used for UNASSIGNED_APIC_ID
* print topo ids is unsigned
* print APIC ID as hex
* print topo ids calculated from APIC ID beside it
* add extra patch to fix migration failure due to APIC's instance_id mismatch
since v1:
* s/pc_find_cpu/pc_find_cpu_slot/ + add comment to it
* add more sanity checks for socket-id/core-id/thread-id and 'apic' properties
* include device_del cpu patches and related fixes to x86 CPU/apic

Series enabling usage of -device/device_add for adding CPUs as devices and device_del for removing them. Using -device/device_add in combination with query-hotpluggable-cpus QMP command allows to hotplug CPUs at any not used possition and then safely migrate QEMU instance by specifying hotadded CPUs on target with help of -device CLI option like with any other device.
Having been able to replicate exact topology on taggert with -device CPUs also opens poosibility to hot-remove CPUs, which this series does by enabling to use device_del with x86 CPUs.

git tree for testing:
https://github.com/imammedo/qemu.git dev_del_cpu_v4
for viewing:
https://github.com/imammedo/qemu/commits/dev_del_cpu_v4

Tested with RHEL7.2 guest including ping/pong migration with adding/removing CPUs in between.

Igor Mammedov (16):
pc: set APIC ID based on socket/core/thread ids if it's not been set
yet
pc: delay setting number of boot CPUs to machine_done time
pc: register created initial and hotpluged CPUs in one place

    pc_cpu_plug()
  pc: forbid BSP removal
  pc: enforce adding CPUs contiguously and removing them in opposit
    order
  pc: cpu: allow device_add to be used with x86 cpu
  pc: implement query-hotpluggable-cpus callback
  apic: move MAX_APICS check to 'apic' class
  apic: drop APICCommonState.idx and use APIC ID as index in
    local_apics[]

apic: kvm-apic: fix crash due to access to freed memory region (kvm)apic: add unrealize callbacks
apic: use apic_id as apic's migration instance_id
target-i386: cpu: do not ignore error and fix apic parent
target-i386: fix apic object leak when CPU is deleted
target-i386: add x86_cpu_unrealizefn()
pc: make device_del CPU work for x86 CPUs

 include/hw/i386/apic_internal.h |   5 +-
 include/hw/i386/pc.h            |   5 ++
 hw/i386/kvm/apic.c              |   9 +-
 hw/i386/pc.c                    | 183 ++++++++++++++++++++++++++++++++++------
 hw/intc/apic.c                  |  26 +++++-
 hw/intc/apic_common.c           |  33 ++++++--
 qmp-commands.hx                 |  15 ++++
 target-i386/cpu.c               |  23 ++++-
 8 files changed, 251 insertions(+), 48 deletions(-)
Signature · 1 line
2.7.4
Signature · 2 lines
Eduardo Otubo
ProfitBricks GmbH
#39 lore raw
Quoted 8 lines · On Wed, 10 Aug 2016 15:56:24 +0200 Eduardo Otubo…
Quoted 7 lines · Michael S. Tsirkin
Quoted 1 line · Igor Mammedov

Changelog:

So for pc and apic bits:

Quoted 7 lines
Quoted 2 lines

Since Eduardo picked up first part of this, feel free to merge the rest too.

Hi Igor,

I was a little curios about the state of this feature. Is this going to make it to 2.7?

it's merged

Quoted 92 lines

Best regards,

Quoted 88 lines
Quoted 88 lines

since v3:
* rebase on top of x86-next tree as it's already applied 1-6/19 from v3 (so they are could be omitted from v4)
* set apic-state to NULL after destroying it in unrealize()
* fix places where I forgot to print X86CPUTopoInfo fields as %u
* move "pc: implement query-hotpluggable-cpus callback" after patch that enables device_add cpu
* extract counting of present cpus in possible_cpus into a separate helper
* newly added patches:
* pc: forbid BSP removal
* pc: enforce adding CPUs contiguously and removing them in opposit order
* apic: kvm-apic: fix crash due to access to freed memory region
* update Reviewed-bys
since v2:
* use 0xFFFFFFFF for UNASSIGNED_APIC_ID instead of UINT32_MAX
* add comment why 0xFFFFFFFF could be used for UNASSIGNED_APIC_ID
* print topo ids is unsigned
* print APIC ID as hex
* print topo ids calculated from APIC ID beside it
* add extra patch to fix migration failure due to APIC's instance_id mismatch
since v1:
* s/pc_find_cpu/pc_find_cpu_slot/ + add comment to it
* add more sanity checks for socket-id/core-id/thread-id and 'apic' properties
* include device_del cpu patches and related fixes to x86 CPU/apic

Series enabling usage of -device/device_add for adding CPUs as devices and device_del for removing them. Using -device/device_add in combination with query-hotpluggable-cpus QMP command allows to hotplug CPUs at any not used possition and then safely migrate QEMU instance by specifying hotadded CPUs on target with help of -device CLI option like with any other device.
Having been able to replicate exact topology on taggert with -device CPUs also opens poosibility to hot-remove CPUs, which this series does by enabling to use device_del with x86 CPUs.

git tree for testing:
https://github.com/imammedo/qemu.git dev_del_cpu_v4
for viewing:
https://github.com/imammedo/qemu/commits/dev_del_cpu_v4

Tested with RHEL7.2 guest including ping/pong migration with adding/removing CPUs in between.

Igor Mammedov (16):
pc: set APIC ID based on socket/core/thread ids if it's not been set
yet
pc: delay setting number of boot CPUs to machine_done time
pc: register created initial and hotpluged CPUs in one place

    pc_cpu_plug()
  pc: forbid BSP removal
  pc: enforce adding CPUs contiguously and removing them in opposit
    order
  pc: cpu: allow device_add to be used with x86 cpu
  pc: implement query-hotpluggable-cpus callback
  apic: move MAX_APICS check to 'apic' class
  apic: drop APICCommonState.idx and use APIC ID as index in
    local_apics[]

apic: kvm-apic: fix crash due to access to freed memory region (kvm)apic: add unrealize callbacks
apic: use apic_id as apic's migration instance_id
target-i386: cpu: do not ignore error and fix apic parent
target-i386: fix apic object leak when CPU is deleted
target-i386: add x86_cpu_unrealizefn()
pc: make device_del CPU work for x86 CPUs

 include/hw/i386/apic_internal.h |   5 +-
 include/hw/i386/pc.h            |   5 ++
 hw/i386/kvm/apic.c              |   9 +-
 hw/i386/pc.c                    | 183 ++++++++++++++++++++++++++++++++++------
 hw/intc/apic.c                  |  26 +++++-
 hw/intc/apic_common.c           |  33 ++++++--
 qmp-commands.hx                 |  15 ++++
 target-i386/cpu.c               |  23 ++++-
 8 files changed, 251 insertions(+), 48 deletions(-)
Signature · 1 line
2.7.4