Skip to contentSkip to conversation

hyperv fixes and enhancements

PATCH In review Roman Kagan 2 participants 36 messages Jun 21, 2017 – Jul 7, 2017

lore
Share & tools
lore
#0 lore raw

v2 cover hyperv fixes and enhancements

This series applies miscellaneous fixes and enhancements to Hyper-V emulation code in QEMU, and lays out the ground for VMBus devices.

v1 -> v2:
- drop the already merged patch
- split and rework SINTx and SVERSION msrs init
- factor out hyperv vcpu init to a function
- rework vp_index management
- distinguish kvm-only (== legacy) mode for SynIC
- use new capabilities recently submitted to KVM
- add compat logic for SynIC
- drop workaround for KVM zeroing SynIC pages
- minor fixes according to comments

Evgeny Yakovlev (1):
hyperv: set partition-wide MSRs only on first vcpu

Roman Kagan (22):
hyperv: add header with protocol definitions
update-linux-headers: prepare for hyperv.h removal
hyperv: ensure SINTx msrs are reset properly
hyperv: make SynIC version msr constant
[not to commit] add new hyperv-related caps
hyperv: ensure VP index equal to QEMU cpu_index
hyperv_testdev: refactor for readability
hyperv: cosmetic: g_malloc -> g_new
hyperv: synic: only setup ack notifier if there's a callback
hyperv: allow passing arbitrary data to sint ack callback
hyperv: address HvSintRoute by X86CPU pointer
hyperv: make HvSintRoute reference-counted
hyperv: qom-ify SynIC
hyperv: block SynIC use in QEMU in incompatible configurations
hyperv: make overlay pages for SynIC
hyperv: add synic message delivery
hyperv: add synic event flag signaling
hyperv: process SIGNAL_EVENT hypercall
hyperv: process POST_MESSAGE hypercall
hyperv_testdev: add SynIC message and event testmodes
MAINTAINERS: add myself and eyakovlev@ for hyperv*
hyperv: update copyright notices

 include/hw/i386/pc.h            |   5 +
 linux-headers/linux/kvm.h       |   2 +
 target/i386/cpu.h               |  16 +-
 target/i386/hyperv.h            |  40 ++-
 target/i386/hyperv_proto.h      | 257 ++++++++++++++++
 hw/misc/hyperv_testdev.c        | 267 +++++++++++++----
 target/i386/cpu.c               |   4 +-
 target/i386/hyperv.c            | 634 ++++++++++++++++++++++++++++++++++++++--
 target/i386/kvm.c               | 167 +++++++----
 target/i386/machine.c           |  24 +-
 MAINTAINERS                     |   7 +
 scripts/update-linux-headers.sh |   4 +-
 12 files changed, 1256 insertions(+), 171 deletions(-)
 create mode 100644 target/i386/hyperv_proto.h
Signature · 1 line
2.9.4
#1 lore raw

PATCH v2 1/23 hyperv: add header with protocol definitions

The definitions for Hyper-V emulation are currently taken from a header imported from the Linux kernel.

However, as these describe a third-party protocol rather than a kernel API, it probably wasn't a good idea to publish it in the kernel uapi.

This patch introduces a header that provides all the necessary definitions, obsoleting the one coming from the kernel.

The new header supports (temporary) coexistence with the kernel one. The constants explicitly named in the Hyper-V specification (e.g. msr numbers) are defined in a non-conflicting way. Other constants and types have got new names.

While at this, the protocol data structures are defined in a more conventional way, without bitfields, enums, and excessive unions.

The code using this stuff is adjusted, too; it can now be built both with and without the kernel header in the tree.

---
 target/i386/cpu.h          |  10 +-
 target/i386/hyperv_proto.h | 257 +++++++++++++++++++++++++++++++++++++++++++++
 target/i386/cpu.c          |   4 +-
 target/i386/hyperv.c       |   6 +-
 target/i386/kvm.c          |  57 +++++-----
 target/i386/machine.c      |  15 ++-
 6 files changed, 301 insertions(+), 48 deletions(-)
 create mode 100644 target/i386/hyperv_proto.h
target/i386/cpu.h +5 -5
index de0551f..464ed1e 100644
@@ -22,7 +22,7 @@
 
 #include "qemu-common.h"
 #include "cpu-qom.h"
-#include "standard-headers/asm-x86/hyperv.h"
+#include "hyperv_proto.h"
 
 #ifdef TARGET_X86_64
 #define TARGET_LONG_BITS 64
@@ -1093,15 +1093,15 @@ typedef struct CPUX86State {
     uint64_t msr_hv_guest_os_id;
     uint64_t msr_hv_vapic;
     uint64_t msr_hv_tsc;
-    uint64_t msr_hv_crash_params[HV_X64_MSR_CRASH_PARAMS];
+    uint64_t msr_hv_crash_params[HV_CRASH_PARAMS];
     uint64_t msr_hv_runtime;
     uint64_t msr_hv_synic_control;
     uint64_t msr_hv_synic_version;
     uint64_t msr_hv_synic_evt_page;
     uint64_t msr_hv_synic_msg_page;
-    uint64_t msr_hv_synic_sint[HV_SYNIC_SINT_COUNT];
-    uint64_t msr_hv_stimer_config[HV_SYNIC_STIMER_COUNT];
-    uint64_t msr_hv_stimer_count[HV_SYNIC_STIMER_COUNT];
+    uint64_t msr_hv_synic_sint[HV_SINT_COUNT];
+    uint64_t msr_hv_stimer_config[HV_STIMER_COUNT];
+    uint64_t msr_hv_stimer_count[HV_STIMER_COUNT];
 
     /* exception/interrupt handling */
     int error_code;
target/i386/hyperv_proto.h new file +257 -0

257 lines not on this page · raw message

target/i386/cpu.c +2 -2
index b2b1d20..631c14d 100644
@@ -3812,12 +3812,12 @@ static GuestPanicInformation *x86_cpu_get_crash_info(CPUState *cs)
     CPUX86State *env = &cpu->env;
     GuestPanicInformation *panic_info = NULL;
 
-    if (env->features[FEAT_HYPERV_EDX] & HV_X64_GUEST_CRASH_MSR_AVAILABLE) {
+    if (env->features[FEAT_HYPERV_EDX] & HV_GUEST_CRASH_MSR_AVAILABLE) {
         panic_info = g_malloc0(sizeof(GuestPanicInformation));
 
         panic_info->type = GUEST_PANIC_INFORMATION_TYPE_HYPER_V;
 
-        assert(HV_X64_MSR_CRASH_PARAMS >= 5);
+        assert(HV_CRASH_PARAMS >= 5);
         panic_info->u.hyper_v.arg1 = env->msr_hv_crash_params[0];
         panic_info->u.hyper_v.arg2 = env->msr_hv_crash_params[1];
         panic_info->u.hyper_v.arg3 = env->msr_hv_crash_params[2];
target/i386/hyperv.c +3 -3
index 8545574..227185c 100644
@@ -14,7 +14,7 @@
 #include "qemu/osdep.h"
 #include "qemu/main-loop.h"
 #include "hyperv.h"
-#include "standard-headers/asm-x86/hyperv.h"
+#include "hyperv_proto.h"
 
 int kvm_hv_handle_exit(X86CPU *cpu, struct kvm_hyperv_exit *exit)
 {
@@ -50,8 +50,8 @@ int kvm_hv_handle_exit(X86CPU *cpu, struct kvm_hyperv_exit *exit)
 
         code  = exit->u.hcall.input & 0xffff;
         switch (code) {
-        case HVCALL_POST_MESSAGE:
-        case HVCALL_SIGNAL_EVENT:
+        case HV_POST_MESSAGE:
+        case HV_SIGNAL_EVENT:
         default:
             exit->u.hcall.result = HV_STATUS_INVALID_HYPERCALL_CODE;
             return 0;
target/i386/kvm.c +28 -29

155 lines not on this page · raw message

target/i386/machine.c +6 -9
index 8c7a822..ded5e34 100644
@@ -566,7 +566,7 @@ static bool hyperv_crash_enable_needed(void *opaque)
     CPUX86State *env = &cpu->env;
     int i;
 
-    for (i = 0; i < HV_X64_MSR_CRASH_PARAMS; i++) {
+    for (i = 0; i < HV_CRASH_PARAMS; i++) {
         if (env->msr_hv_crash_params[i]) {
             return true;
         }
@@ -580,8 +580,7 @@ static const VMStateDescription vmstate_msr_hyperv_crash = {
     .minimum_version_id = 1,
     .needed = hyperv_crash_enable_needed,
     .fields = (VMStateField[]) {
-        VMSTATE_UINT64_ARRAY(env.msr_hv_crash_params,
-                             X86CPU, HV_X64_MSR_CRASH_PARAMS),
+        VMSTATE_UINT64_ARRAY(env.msr_hv_crash_params, X86CPU, HV_CRASH_PARAMS),
         VMSTATE_END_OF_LIST()
     }
 };
@@ -639,8 +638,7 @@ static const VMStateDescription vmstate_msr_hyperv_synic = {
         VMSTATE_UINT64(env.msr_hv_synic_control, X86CPU),
         VMSTATE_UINT64(env.msr_hv_synic_evt_page, X86CPU),
         VMSTATE_UINT64(env.msr_hv_synic_msg_page, X86CPU),
-        VMSTATE_UINT64_ARRAY(env.msr_hv_synic_sint, X86CPU,
-                             HV_SYNIC_SINT_COUNT),
+        VMSTATE_UINT64_ARRAY(env.msr_hv_synic_sint, X86CPU, HV_SINT_COUNT),
         VMSTATE_END_OF_LIST()
     }
 };
@@ -665,10 +663,9 @@ static const VMStateDescription vmstate_msr_hyperv_stimer = {
     .minimum_version_id = 1,
     .needed = hyperv_stimer_enable_needed,
     .fields = (VMStateField[]) {
-        VMSTATE_UINT64_ARRAY(env.msr_hv_stimer_config,
-                             X86CPU, HV_SYNIC_STIMER_COUNT),
-        VMSTATE_UINT64_ARRAY(env.msr_hv_stimer_count,
-                             X86CPU, HV_SYNIC_STIMER_COUNT),
+        VMSTATE_UINT64_ARRAY(env.msr_hv_stimer_config, X86CPU,
+                             HV_STIMER_COUNT),
+        VMSTATE_UINT64_ARRAY(env.msr_hv_stimer_count, X86CPU, HV_STIMER_COUNT),
         VMSTATE_END_OF_LIST()
     }
 };
Signature · 1 line
2.9.4
#2 lore raw

PATCH v2 2/23 update-linux-headers: prepare for hyperv.h removal

In reply to Roman Kagan

All definitions related to Hyper-V emulation are now taken from the QEMU own header, so the one imported from the kernel is no longer needed.

Unfortunately it's included by kvm_para.h.

So, until this is fixed in the kernel, teach the header harvesting script to substitute kernel's hyperv.h with a dummy.

---
 scripts/update-linux-headers.sh | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
scripts/update-linux-headers.sh +3 -1
index 2f906c4..ad80fe3 100755
@@ -104,7 +104,9 @@ for arch in $ARCHLIST; do
         cp "$tmpdir/include/asm/unistd-common.h" "$output/linux-headers/asm-arm/"
     fi
     if [ $arch = x86 ]; then
-        cp_portable "$tmpdir/include/asm/hyperv.h" "$output/include/standard-headers/asm-x86/"
+        cat <<-EOF >"$output/include/standard-headers/asm-x86/hyperv.h"
+        /* this is a temporary placeholder until kvm_para.h stops including it */
+        EOF
         cp "$tmpdir/include/asm/unistd_32.h" "$output/linux-headers/asm-x86/"
         cp "$tmpdir/include/asm/unistd_x32.h" "$output/linux-headers/asm-x86/"
         cp "$tmpdir/include/asm/unistd_64.h" "$output/linux-headers/asm-x86/"
Signature · 1 line
2.9.4
#3 lore raw

PATCH v2 3/23 hyperv: set partition-wide MSRs only on first vcpu

In reply to Roman Kagan

From: Evgeny Yakovlev <[email protected]>

Hyper-V has a notion of partition-wide MSRs. Those MSRs are read and written as usual on each VCPU, however the hypervisor maintains a single global value for all VCPUs. Thus writing such an MSR from any single VCPU affects the global value that is read by all other VCPUs.

This leads to an issue during VCPU hotplug: the zero-initialzied values of those MSRs get synced into KVM and override the global values as has already been set by the guest.

This change makes the partition-wide MSRs only be synchronized on the first vcpu.

---
 target/i386/cpu.h |  5 ++++-
 target/i386/kvm.c | 20 ++++++++++++--------
 2 files changed, 16 insertions(+), 9 deletions(-)
target/i386/cpu.h +4 -1
index 464ed1e..ad8600d 100644
@@ -1089,10 +1089,13 @@ typedef struct CPUX86State {
     uint64_t async_pf_en_msr;
     uint64_t pv_eoi_en_msr;
 
+    /* Partition-wide HV MSRs, will be updated only on the first vcpu */
     uint64_t msr_hv_hypercall;
     uint64_t msr_hv_guest_os_id;
-    uint64_t msr_hv_vapic;
     uint64_t msr_hv_tsc;
+
+    /* Per-VCPU HV MSRs */
+    uint64_t msr_hv_vapic;
     uint64_t msr_hv_crash_params[HV_CRASH_PARAMS];
     uint64_t msr_hv_runtime;
     uint64_t msr_hv_synic_control;
target/i386/kvm.c +12 -8
index 1c619dc..183a85b 100644
@@ -1718,19 +1718,23 @@ static int kvm_put_msrs(X86CPU *cpu, int level)
             kvm_msr_entry_add(cpu, MSR_CORE_PERF_GLOBAL_CTRL,
                               env->msr_global_ctrl);
         }
-        if (has_msr_hv_hypercall) {
-            kvm_msr_entry_add(cpu, HV_X64_MSR_GUEST_OS_ID,
-                              env->msr_hv_guest_os_id);
-            kvm_msr_entry_add(cpu, HV_X64_MSR_HYPERCALL,
-                              env->msr_hv_hypercall);
+        /* Sync partition-wide MSRs only on first VCPU to avoid races */
+        if (current_cpu == first_cpu) {
+            if (has_msr_hv_hypercall) {
+                kvm_msr_entry_add(cpu, HV_X64_MSR_GUEST_OS_ID,
+                                  env->msr_hv_guest_os_id);
+                kvm_msr_entry_add(cpu, HV_X64_MSR_HYPERCALL,
+                                  env->msr_hv_hypercall);
+            }
+            if (cpu->hyperv_time) {
+                kvm_msr_entry_add(cpu, HV_X64_MSR_REFERENCE_TSC,
+                                  env->msr_hv_tsc);
+            }
         }
         if (cpu->hyperv_vapic) {
             kvm_msr_entry_add(cpu, HV_X64_MSR_APIC_ASSIST_PAGE,
                               env->msr_hv_vapic);
         }
-        if (cpu->hyperv_time) {
-            kvm_msr_entry_add(cpu, HV_X64_MSR_REFERENCE_TSC, env->msr_hv_tsc);
-        }
         if (has_msr_hv_crash) {
             int j;
 
Signature · 1 line
2.9.4
#4 lore raw

PATCH v2 4/23 hyperv: ensure SINTx msrs are reset properly

In reply to Roman Kagan

Initially SINTx msrs should be in "masked" state. To ensure that happens on *every* reset, move setting their values to kvm_arch_vcpu_reset.

---
v1 -> v2:
- split out of v1 patch 4

 target/i386/kvm.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
target/i386/kvm.c +7 -5
index 183a85b..27404dd 100644
@@ -647,8 +647,6 @@ static int hyperv_handle_properties(CPUState *cs)
         env->features[FEAT_HYPERV_EAX] |= HV_VP_RUNTIME_AVAILABLE;
     }
     if (cpu->hyperv_synic) {
-        int sint;
-
         if (!has_msr_hv_synic ||
             kvm_vcpu_enable_cap(cs, KVM_CAP_HYPERV_SYNIC, 0)) {
             fprintf(stderr, "Hyper-V SynIC is not supported by kernel\n");
@@ -657,9 +655,6 @@ static int hyperv_handle_properties(CPUState *cs)
 
         env->features[FEAT_HYPERV_EAX] |= HV_SYNIC_AVAILABLE;
         env->msr_hv_synic_version = HV_SYNIC_VERSION;
-        for (sint = 0; sint < ARRAY_SIZE(env->msr_hv_synic_sint); sint++) {
-            env->msr_hv_synic_sint[sint] = HV_SINT_MASKED;
-        }
     }
     if (cpu->hyperv_stimer) {
         if (!has_msr_hv_stimer) {
@@ -1039,6 +1034,13 @@ void kvm_arch_reset_vcpu(X86CPU *cpu)
     } else {
         env->mp_state = KVM_MP_STATE_RUNNABLE;
     }
+
+    if (cpu->hyperv_synic) {
+        int i;
+        for (i = 0; i < ARRAY_SIZE(env->msr_hv_synic_sint); i++) {
+            env->msr_hv_synic_sint[i] = HV_SINT_MASKED;
+        }
+    }
 }
 
 void kvm_arch_do_init_vcpu(X86CPU *cpu)
Signature · 1 line
2.9.4
#5 lore raw

PATCH v2 5/23 hyperv: make SynIC version msr constant

In reply to Roman Kagan

The value of HV_X64_MSR_SVERSION is initialized once at vcpu init, and is reset to zero on vcpu reset, which is wrong.

It is supposed to be a constant, so drop the field from X86CPU, set the msr with the constant value, and don't bother getting it.

---
v1 -> v2:
- split out of v1 patch 4
- make the value constant instead of keeping it on X86CPU

 target/i386/cpu.h | 1 -
 target/i386/kvm.c | 9 ++-------
 2 files changed, 2 insertions(+), 8 deletions(-)
target/i386/cpu.h +0 -1
index ad8600d..917e3c4 100644
@@ -1099,7 +1099,6 @@ typedef struct CPUX86State {
     uint64_t msr_hv_crash_params[HV_CRASH_PARAMS];
     uint64_t msr_hv_runtime;
     uint64_t msr_hv_synic_control;
-    uint64_t msr_hv_synic_version;
     uint64_t msr_hv_synic_evt_page;
     uint64_t msr_hv_synic_msg_page;
     uint64_t msr_hv_synic_sint[HV_SINT_COUNT];
target/i386/kvm.c +2 -7
index 27404dd..2795b63 100644
@@ -654,7 +654,6 @@ static int hyperv_handle_properties(CPUState *cs)
         }
 
         env->features[FEAT_HYPERV_EAX] |= HV_SYNIC_AVAILABLE;
-        env->msr_hv_synic_version = HV_SYNIC_VERSION;
     }
     if (cpu->hyperv_stimer) {
         if (!has_msr_hv_stimer) {
@@ -1752,10 +1751,10 @@ static int kvm_put_msrs(X86CPU *cpu, int level)
         if (cpu->hyperv_synic) {
             int j;
 
+            kvm_msr_entry_add(cpu, HV_X64_MSR_SVERSION, HV_SYNIC_VERSION);
+
             kvm_msr_entry_add(cpu, HV_X64_MSR_SCONTROL,
                               env->msr_hv_synic_control);
-            kvm_msr_entry_add(cpu, HV_X64_MSR_SVERSION,
-                              env->msr_hv_synic_version);
             kvm_msr_entry_add(cpu, HV_X64_MSR_SIEFP,
                               env->msr_hv_synic_evt_page);
             kvm_msr_entry_add(cpu, HV_X64_MSR_SIMP,
@@ -2160,7 +2159,6 @@ static int kvm_get_msrs(X86CPU *cpu)
         uint32_t msr;
 
         kvm_msr_entry_add(cpu, HV_X64_MSR_SCONTROL, 0);
-        kvm_msr_entry_add(cpu, HV_X64_MSR_SVERSION, 0);
         kvm_msr_entry_add(cpu, HV_X64_MSR_SIEFP, 0);
         kvm_msr_entry_add(cpu, HV_X64_MSR_SIMP, 0);
         for (msr = HV_X64_MSR_SINT0; msr <= HV_X64_MSR_SINT15; msr++) {
@@ -2364,9 +2362,6 @@ static int kvm_get_msrs(X86CPU *cpu)
         case HV_X64_MSR_SCONTROL:
             env->msr_hv_synic_control = msrs[i].data;
             break;
-        case HV_X64_MSR_SVERSION:
-            env->msr_hv_synic_version = msrs[i].data;
-            break;
         case HV_X64_MSR_SIEFP:
             env->msr_hv_synic_evt_page = msrs[i].data;
             break;
Signature · 1 line
2.9.4
#6 lore raw

PATCH v2 6/23 add new hyperv-related caps

In reply to Roman Kagan

Add two hyperv-related caps
[they should arrive through the usual kernel header harvesting; this patch allows to build QEMU before that happens].

---
 linux-headers/linux/kvm.h | 2 ++
 1 file changed, 2 insertions(+)
linux-headers/linux/kvm.h +2 -0
index d2892da..d7f532f 100644
@@ -895,6 +895,8 @@ struct kvm_ppc_resize_hpt {
 #define KVM_CAP_SPAPR_TCE_VFIO 142
 #define KVM_CAP_X86_GUEST_MWAIT 143
 #define KVM_CAP_ARM_USER_IRQ 144
+#define KVM_CAP_HYPERV_SYNIC2 145
+#define KVM_CAP_HYPERV_VP_INDEX 146
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
Signature · 1 line
2.9.4
#7 lore raw

PATCH v2 7/23 hyperv: ensure VP index equal to QEMU cpu_index

In reply to Roman Kagan

Hyper-V identifies vCPUs by Virtual Processor (VP) index which can be queried by the guest via HV_X64_MSR_VP_INDEX msr. It is defined by the spec as a sequential number which can't exceed the maximum number of vCPUs per VM.

It has to be owned by QEMU in order to preserve it across migration.

However, the initial implementation in KVM didn't allow to set this msr, and KVM used its own notion of VP index. Fortunately, the way vCPUs are created in QEMU/KVM makes it likely that the KVM value is equal to QEMU cpu_index.

So choose cpu_index as the value for vp_index, and push that to KVM on kernels that support setting the msr. On older ones that don't, query the kernel value and assert that it's in sync with QEMU.

Besides, since handling errors from vCPU init at hotplug time is impossible, disable vCPU hotplug.

This patch also introduces accessor functions to wrap the mapping between a vCPU and its vp_index. Besides, a few variables are renamed to avoid confusion of vp_index with vcpu_id (== apic_id).

---
v1 -> v2:
- were patches 5, 6 in v1
- move vp_index initialization to hyperv_init_vcpu
- check capability before trying to set the msr
- set the msr on the usual kvm_put_msrs path
- disable cpu hotplug if msr is not settable

 target/i386/hyperv.h |  5 ++++-
 target/i386/hyperv.c | 16 +++++++++++++---
 target/i386/kvm.c    | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 68 insertions(+), 4 deletions(-)
target/i386/hyperv.h +4 -1
index 0c3b562..82f4757 100644
@@ -32,11 +32,14 @@ struct HvSintRoute {
 
 int kvm_hv_handle_exit(X86CPU *cpu, struct kvm_hyperv_exit *exit);
 
-HvSintRoute *kvm_hv_sint_route_create(uint32_t vcpu_id, uint32_t sint,
+HvSintRoute *kvm_hv_sint_route_create(uint32_t vp_index, uint32_t sint,
                                       HvSintAckClb sint_ack_clb);
 
 void kvm_hv_sint_route_destroy(HvSintRoute *sint_route);
 
 int kvm_hv_sint_route_set_sint(HvSintRoute *sint_route);
 
+uint32_t hyperv_vp_index(X86CPU *cpu);
+X86CPU *hyperv_find_vcpu(uint32_t vp_index);
+
 #endif
target/i386/hyperv.c +13 -3
index 227185c..4f57447 100644
@@ -16,6 +16,16 @@
 #include "hyperv.h"
 #include "hyperv_proto.h"
 
+uint32_t hyperv_vp_index(X86CPU *cpu)
+{
+    return CPU(cpu)->cpu_index;
+}
+
+X86CPU *hyperv_find_vcpu(uint32_t vp_index)
+{
+    return X86_CPU(qemu_get_cpu(vp_index));
+}
+
 int kvm_hv_handle_exit(X86CPU *cpu, struct kvm_hyperv_exit *exit)
 {
     CPUX86State *env = &cpu->env;
@@ -72,7 +82,7 @@ static void kvm_hv_sint_ack_handler(EventNotifier *notifier)
     }
 }
 
-HvSintRoute *kvm_hv_sint_route_create(uint32_t vcpu_id, uint32_t sint,
+HvSintRoute *kvm_hv_sint_route_create(uint32_t vp_index, uint32_t sint,
                                       HvSintAckClb sint_ack_clb)
 {
     HvSintRoute *sint_route;
@@ -92,7 +102,7 @@ HvSintRoute *kvm_hv_sint_route_create(uint32_t vcpu_id, uint32_t sint,
     event_notifier_set_handler(&sint_route->sint_ack_notifier,
                                kvm_hv_sint_ack_handler);
 
-    gsi = kvm_irqchip_add_hv_sint_route(kvm_state, vcpu_id, sint);
+    gsi = kvm_irqchip_add_hv_sint_route(kvm_state, vp_index, sint);
     if (gsi < 0) {
         goto err_gsi;
     }
@@ -105,7 +115,7 @@ HvSintRoute *kvm_hv_sint_route_create(uint32_t vcpu_id, uint32_t sint,
     }
     sint_route->gsi = gsi;
     sint_route->sint_ack_clb = sint_ack_clb;
-    sint_route->vcpu_id = vcpu_id;
+    sint_route->vcpu_id = vp_index;
     sint_route->sint = sint;
 
     return sint_route;
target/i386/kvm.c +51 -0

81 lines not on this page · raw message

Signature · 1 line
2.9.4
#8 lore raw

Re: [Qemu-devel] [PATCH v2 07/23] hyperv: ensure VP index equal to QEMU cpu_index

Quoted 12 lines · On Wed, 21 Jun 2017 19:24:08 +0300 Roman Kagan <[email protected]> wrote:

Hyper-V identifies vCPUs by Virtual Processor (VP) index which can be queried by the guest via HV_X64_MSR_VP_INDEX msr. It is defined by the spec as a sequential number which can't exceed the maximum number of vCPUs per VM.

It has to be owned by QEMU in order to preserve it across migration.

However, the initial implementation in KVM didn't allow to set this msr, and KVM used its own notion of VP index. Fortunately, the way vCPUs are created in QEMU/KVM makes it likely that the KVM value is equal to QEMU cpu_index.

Quoted 6 lines

So choose cpu_index as the value for vp_index, and push that to KVM on kernels that support setting the msr. On older ones that don't, query the kernel value and assert that it's in sync with QEMU.

Besides, since handling errors from vCPU init at hotplug time is impossible, disable vCPU hotplug.

proper place to check if cpu might be created is at pc_cpu_pre_plug() where you can gracefully abort cpu creation process.

Also it's possible to create cold-plugged CPUs in out of order sequence using
-device cpu-foo on CLI
will be hyperv kvm/guest side ok with it?

Quoted 46 lines

This patch also introduces accessor functions to wrap the mapping between a vCPU and its vp_index. Besides, a few variables are renamed to avoid confusion of vp_index with vcpu_id (== apic_id).

---
v1 -> v2:
- were patches 5, 6 in v1
- move vp_index initialization to hyperv_init_vcpu
- check capability before trying to set the msr
- set the msr on the usual kvm_put_msrs path
- disable cpu hotplug if msr is not settable

 target/i386/hyperv.h |  5 ++++-
 target/i386/hyperv.c | 16 +++++++++++++---
 target/i386/kvm.c    | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 68 insertions(+), 4 deletions(-)
target/i386/hyperv.h +4 -1
index 0c3b562..82f4757 100644
@@ -32,11 +32,14 @@ struct HvSintRoute {
 
 int kvm_hv_handle_exit(X86CPU *cpu, struct kvm_hyperv_exit *exit);
 
-HvSintRoute *kvm_hv_sint_route_create(uint32_t vcpu_id, uint32_t sint,
+HvSintRoute *kvm_hv_sint_route_create(uint32_t vp_index, uint32_t sint,
                                       HvSintAckClb sint_ack_clb);
 
 void kvm_hv_sint_route_destroy(HvSintRoute *sint_route);
 
 int kvm_hv_sint_route_set_sint(HvSintRoute *sint_route);
 
+uint32_t hyperv_vp_index(X86CPU *cpu);
+X86CPU *hyperv_find_vcpu(uint32_t vp_index);
+
 #endif
target/i386/hyperv.c +0 -0
index 227185c..4f57447 100644
@@ -16,6 +16,16 @@
 #include "hyperv.h"
 #include "hyperv_proto.h"
 

target/i386/hyperv.c line 28

Quoted 10 lines
target/i386/hyperv.c +8 -0
+uint32_t hyperv_vp_index(X86CPU *cpu)
+{
+    return CPU(cpu)->cpu_index;
+}
 
 
+X86CPU *hyperv_find_vcpu(uint32_t vp_index)
+{
+    return X86_CPU(qemu_get_cpu(vp_index));
+}

this helper isn't used in this patch, add it in the patch that would actually use it

also if qemu_get_cpu() were called from each CPU init, it would incur O(N^2) complexity, could you do without it?

Quoted 23 lines
target/i386/hyperv.c +3 -2
+
 int kvm_hv_handle_exit(X86CPU *cpu, struct kvm_hyperv_exit *exit)
 {
     CPUX86State *env = &cpu->env;
@@ -72,7 +82,7 @@ static void kvm_hv_sint_ack_handler(EventNotifier *notifier)
     }
 }
 
-HvSintRoute *kvm_hv_sint_route_create(uint32_t vcpu_id, uint32_t sint,
+HvSintRoute *kvm_hv_sint_route_create(uint32_t vp_index, uint32_t sint,
                                       HvSintAckClb sint_ack_clb)
 {
     HvSintRoute *sint_route;
@@ -92,7 +102,7 @@ HvSintRoute *kvm_hv_sint_route_create(uint32_t vcpu_id, uint32_t sint,
     event_notifier_set_handler(&sint_route->sint_ack_notifier,
                                kvm_hv_sint_ack_handler);
 
-    gsi = kvm_irqchip_add_hv_sint_route(kvm_state, vcpu_id, sint);
+    gsi = kvm_irqchip_add_hv_sint_route(kvm_state, vp_index, sint);
     if (gsi < 0) {
         goto err_gsi;
     }

target/i386/hyperv.c HvSintRoute *kvm_hv_sint_route_create(uint32_t vcpu_id, uint32_t sint, · line 118

Quoted 5 lines
target/i386/hyperv.c +1 -1
@@ -105,7 +115,7 @@ HvSintRoute *kvm_hv_sint_route_create(uint32_t vcpu_id, uint32_t sint,
     }
     sint_route->gsi = gsi;
     sint_route->sint_ack_clb = sint_ack_clb;
-    sint_route->vcpu_id = vcpu_id;
+    sint_route->vcpu_id = vp_index;

^^^ - shouldn't it also be re-named?

maybe split all renaming into separate patch ...

Quoted 30 lines
target/i386/hyperv.c +0 -0
     sint_route->sint = sint;
 
     return sint_route;
target/i386/kvm.c +12 -0
index 2795b63..9bf7f08 100644
@@ -86,6 +86,7 @@ static bool has_msr_hv_hypercall;
 static bool has_msr_hv_crash;
 static bool has_msr_hv_reset;
 static bool has_msr_hv_vpindex;
+static bool is_hv_vpindex_settable;
 static bool has_msr_hv_runtime;
 static bool has_msr_hv_synic;
 static bool has_msr_hv_stimer;
@@ -665,6 +666,43 @@ static int hyperv_handle_properties(CPUState *cs)
     return 0;
 }
 
+static int hyperv_init_vcpu(X86CPU *cpu)
+{
+    if (cpu->hyperv_vpindex && !is_hv_vpindex_settable) {
+        /*
+         * the kernel doesn't support setting vp_index; assert that its value
+         * is in sync
+         */
+        int ret;
+        struct {
+            struct kvm_msrs info;
+            struct kvm_msr_entry entries[1];

target/i386/kvm.c static int hyperv_handle_properties(CPUState *cs) · line 689

Quoted 10 lines
target/i386/kvm.c +10 -0
+        } msr_data = {
+            .info.nmsrs = 1,
+            .entries[0].index = HV_X64_MSR_VP_INDEX,
+        };
+
+        /*
+         * handling errors from vcpu init at hotplug time is impossible, so
+         * disallow cpu hotplug
+         */
+        MACHINE_GET_CLASS(current_machine)->hot_add_cpu = NULL;

one shouldn't alter machine this way nor
it would disable cpu hotplug, it would disable only cpu-add interface but device_add() would still work.

target/i386/kvm.c static int hyperv_handle_properties(CPUState *cs) · line 692

Quoted 2 lines
target/i386/kvm.c +2 -0
+        ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_MSRS, &msr_data);
+        if (ret < 0) {

when this can fail?

target/i386/kvm.c static int hyperv_handle_properties(CPUState *cs) · line 698

Quoted 6 lines
target/i386/kvm.c +6 -0
+            return ret;
+        }
+        assert(ret == 1);
+
+        if (msr_data.entries[0].data != hyperv_vp_index(cpu)) {
+            fprintf(stderr, "kernel's vp_index != QEMU's vp_index\n");

error_report()

Quoted 44 lines
target/i386/kvm.c +20 -0
+            return -ENXIO;
+        }
+    }
+
+    return 0;
+}
+
 static Error *invtsc_mig_blocker;
 
 #define KVM_MAX_CPUID_ENTRIES  100
@@ -1013,6 +1051,13 @@ int kvm_arch_init_vcpu(CPUState *cs)
         has_msr_tsc_aux = false;
     }
 
+    if (hyperv_enabled(cpu)) {
+        r = hyperv_init_vcpu(cpu);
+        if (r) {
+            goto fail;
+        }
+    }
+
     return 0;
 
  fail:
@@ -1204,6 +1249,8 @@ int kvm_arch_init(MachineState *ms, KVMState *s)
     has_pit_state2 = kvm_check_extension(s, KVM_CAP_PIT_STATE2);
 #endif
 
+    is_hv_vpindex_settable = kvm_check_extension(s, KVM_CAP_HYPERV_VP_INDEX);
+
     ret = kvm_get_supported_msrs(s);
     if (ret < 0) {
         return ret;
@@ -1748,6 +1795,10 @@ static int kvm_put_msrs(X86CPU *cpu, int level)
         if (has_msr_hv_runtime) {
             kvm_msr_entry_add(cpu, HV_X64_MSR_VP_RUNTIME, env->msr_hv_runtime);
         }
+        if (cpu->hyperv_vpindex && has_msr_hv_vpindex &&
+            is_hv_vpindex_settable) {
+            kvm_msr_entry_add(cpu, HV_X64_MSR_VP_INDEX, hyperv_vp_index(cpu));
+        }
         if (cpu->hyperv_synic) {
             int j;
 
#9 lore raw

Re: [Qemu-devel] [PATCH v2 07/23] hyperv: ensure VP index equal to QEMU cpu_index

Quoted 15 lines · Igor Mammedov
Quoted 12 lines · On Wed, 21 Jun 2017 19:24:08 +0300 Roman Kagan <[email protected]> wrote:

Hyper-V identifies vCPUs by Virtual Processor (VP) index which can be queried by the guest via HV_X64_MSR_VP_INDEX msr. It is defined by the spec as a sequential number which can't exceed the maximum number of vCPUs per VM.

It has to be owned by QEMU in order to preserve it across migration.

However, the initial implementation in KVM didn't allow to set this msr, and KVM used its own notion of VP index. Fortunately, the way vCPUs are created in QEMU/KVM makes it likely that the KVM value is equal to QEMU cpu_index.

Quoted 8 lines
Quoted 6 lines

So choose cpu_index as the value for vp_index, and push that to KVM on kernels that support setting the msr. On older ones that don't, query the kernel value and assert that it's in sync with QEMU.

Besides, since handling errors from vCPU init at hotplug time is impossible, disable vCPU hotplug.

proper place to check if cpu might be created is at pc_cpu_pre_plug() where you can gracefully abort cpu creation process.

Thanks for the suggestion, I'll rework it this way.

Quoted 4 lines

Also it's possible to create cold-plugged CPUs in out of order sequence using
-device cpu-foo on CLI
will be hyperv kvm/guest side ok with it?

On kernels that support setting HV_X64_MSR_VP_INDEX QEMU will synchronize all sides. On kernels that don't, if out-of-order creation results in vp_index mismatch between the kernel and QEMU, vcpu creation will fail.

Quoted 52 lines
Quoted 50 lines

This patch also introduces accessor functions to wrap the mapping between a vCPU and its vp_index. Besides, a few variables are renamed to avoid confusion of vp_index with vcpu_id (== apic_id).

---
v1 -> v2:
- were patches 5, 6 in v1
- move vp_index initialization to hyperv_init_vcpu
- check capability before trying to set the msr
- set the msr on the usual kvm_put_msrs path
- disable cpu hotplug if msr is not settable

 target/i386/hyperv.h |  5 ++++-
 target/i386/hyperv.c | 16 +++++++++++++---
 target/i386/kvm.c    | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 68 insertions(+), 4 deletions(-)
target/i386/hyperv.h +4 -1
index 0c3b562..82f4757 100644
@@ -32,11 +32,14 @@ struct HvSintRoute {
 
 int kvm_hv_handle_exit(X86CPU *cpu, struct kvm_hyperv_exit *exit);
 
-HvSintRoute *kvm_hv_sint_route_create(uint32_t vcpu_id, uint32_t sint,
+HvSintRoute *kvm_hv_sint_route_create(uint32_t vp_index, uint32_t sint,
                                       HvSintAckClb sint_ack_clb);
 
 void kvm_hv_sint_route_destroy(HvSintRoute *sint_route);
 
 int kvm_hv_sint_route_set_sint(HvSintRoute *sint_route);
 
+uint32_t hyperv_vp_index(X86CPU *cpu);
+X86CPU *hyperv_find_vcpu(uint32_t vp_index);
+
 #endif
target/i386/hyperv.c +4 -0
index 227185c..4f57447 100644
@@ -16,6 +16,16 @@
 #include "hyperv.h"
 #include "hyperv_proto.h"
 
+uint32_t hyperv_vp_index(X86CPU *cpu)
+{
+    return CPU(cpu)->cpu_index;
+}
Quoted 5 lines

target/i386/hyperv.c line 27

Quoted 4 lines
target/i386/hyperv.c +4 -0
+X86CPU *hyperv_find_vcpu(uint32_t vp_index)
+{
+    return X86_CPU(qemu_get_cpu(vp_index));
+}

this helper isn't used in this patch, add it in the patch that would actually use it

I thought I would put the only two functions that encapsulate the knowledge of how vp_index is realted to cpu_index, in a single patch.

I'm now thinking of open-coding the iteration over cpus here and directly look for cpu whose hyperv_vp_index() matches. Then that knowledge will become encapsulated in a single place, and indeed, this helper can go into another patch where it's used.

Quoted 2 lines

also if qemu_get_cpu() were called from each CPU init, it would incur O(N^2) complexity, could you do without it?

It isn't called on hot paths (ATM it's called only when SINT routes are created, which is at most one per cpu). I don't see a problem here.

Quoted 7 lines

target/i386/hyperv.c HvSintRoute *kvm_hv_sint_route_create(uint32_t vcpu_id, uint32_t sint, · line 118

Quoted 6 lines
target/i386/hyperv.c +1 -1
@@ -105,7 +115,7 @@ HvSintRoute *kvm_hv_sint_route_create(uint32_t vcpu_id, uint32_t sint,
     }
     sint_route->gsi = gsi;
     sint_route->sint_ack_clb = sint_ack_clb;
-    sint_route->vcpu_id = vcpu_id;
+    sint_route->vcpu_id = vp_index;

^^^ - shouldn't it also be re-named?

Right, but vcpu_id on sint_route is replaced with X86CPU pointer in a followup patch, so I wasn't sure if it was worth while to add more churn...

Quoted 2 lines

maybe split all renaming into separate patch ...

Part of the renaming will disappear eventually in the followup patches, so I'm sure it's a good idea... Opinions?

Quoted 16 lines
Quoted 16 lines
target/i386/hyperv.c +16 -0
+static int hyperv_init_vcpu(X86CPU *cpu)
+{
+    if (cpu->hyperv_vpindex && !is_hv_vpindex_settable) {
+        /*
+         * the kernel doesn't support setting vp_index; assert that its value
+         * is in sync
+         */
+        int ret;
+        struct {
+            struct kvm_msrs info;
+            struct kvm_msr_entry entries[1];
+        } msr_data = {
+            .info.nmsrs = 1,
+            .entries[0].index = HV_X64_MSR_VP_INDEX,
+        };
+
Quoted 8 lines

target/i386/hyperv.c

Quoted 5 lines
target/i386/hyperv.c +5 -0
+        /*
+         * handling errors from vcpu init at hotplug time is impossible, so
+         * disallow cpu hotplug
+         */
+        MACHINE_GET_CLASS(current_machine)->hot_add_cpu = NULL;

one shouldn't alter machine this way nor
it would disable cpu hotplug, it would disable only cpu-add interface but device_add() would still work.

Understood, will rework as you suggest above.

Quoted 3 lines

target/i386/hyperv.c

Quoted 2 lines
target/i386/hyperv.c +2 -0
+        ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_MSRS, &msr_data);
+        if (ret < 0) {

when this can fail?

Dunno, but not checking for errors doesn't look good.

Quoted 7 lines

target/i386/hyperv.c

Quoted 6 lines
target/i386/hyperv.c +6 -0
+            return ret;
+        }
+        assert(ret == 1);
+
+        if (msr_data.entries[0].data != hyperv_vp_index(cpu)) {
+            fprintf(stderr, "kernel's vp_index != QEMU's vp_index\n");

error_report()

OK (target/i386/kvm.c is already a mix of all possible styles of error reporting, I must've followed a wrong one).

Thanks,
Roman.

#10 lore raw

Re: [Qemu-devel] [PATCH v2 07/23] hyperv: ensure VP index equal to QEMU cpu_index

Quoted 32 lines · On Thu, 29 Jun 2017 12:53:27 +0300 Roman Kagan <[email protected]> wrote:
Quoted 15 lines · Igor Mammedov
Quoted 12 lines · On Wed, 21 Jun 2017 19:24:08 +0300 Roman Kagan <[email protected]> wrote:

Hyper-V identifies vCPUs by Virtual Processor (VP) index which can be queried by the guest via HV_X64_MSR_VP_INDEX msr. It is defined by the spec as a sequential number which can't exceed the maximum number of vCPUs per VM.

It has to be owned by QEMU in order to preserve it across migration.

However, the initial implementation in KVM didn't allow to set this msr, and KVM used its own notion of VP index. Fortunately, the way vCPUs are created in QEMU/KVM makes it likely that the KVM value is equal to QEMU cpu_index.

Quoted 8 lines
Quoted 6 lines

So choose cpu_index as the value for vp_index, and push that to KVM on kernels that support setting the msr. On older ones that don't, query the kernel value and assert that it's in sync with QEMU.

Besides, since handling errors from vCPU init at hotplug time is impossible, disable vCPU hotplug.

proper place to check if cpu might be created is at pc_cpu_pre_plug() where you can gracefully abort cpu creation process.

Thanks for the suggestion, I'll rework it this way.

Quoted 4 lines

Also it's possible to create cold-plugged CPUs in out of order sequence using
-device cpu-foo on CLI
will be hyperv kvm/guest side ok with it?

Quoted 4 lines

On kernels that support setting HV_X64_MSR_VP_INDEX QEMU will synchronize all sides. On kernels that don't, if out-of-order creation results in vp_index mismatch between the kernel and QEMU, vcpu creation will fail.

And additional question,
what would happen if VM is started on host supporting VP index setting and then migrated to a host without it?

Quoted 67 lines
Quoted 53 lines
Quoted 51 lines

This patch also introduces accessor functions to wrap the mapping between a vCPU and its vp_index. Besides, a few variables are renamed to avoid confusion of vp_index with vcpu_id (== apic_id).

---
v1 -> v2:
- were patches 5, 6 in v1
- move vp_index initialization to hyperv_init_vcpu
- check capability before trying to set the msr
- set the msr on the usual kvm_put_msrs path
- disable cpu hotplug if msr is not settable

 target/i386/hyperv.h |  5 ++++-
 target/i386/hyperv.c | 16 +++++++++++++---
 target/i386/kvm.c    | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 68 insertions(+), 4 deletions(-)
target/i386/hyperv.h +4 -1
index 0c3b562..82f4757 100644
@@ -32,11 +32,14 @@ struct HvSintRoute {
 
 int kvm_hv_handle_exit(X86CPU *cpu, struct kvm_hyperv_exit *exit);
 
-HvSintRoute *kvm_hv_sint_route_create(uint32_t vcpu_id, uint32_t sint,
+HvSintRoute *kvm_hv_sint_route_create(uint32_t vp_index, uint32_t sint,
                                       HvSintAckClb sint_ack_clb);
 
 void kvm_hv_sint_route_destroy(HvSintRoute *sint_route);
 
 int kvm_hv_sint_route_set_sint(HvSintRoute *sint_route);
 
+uint32_t hyperv_vp_index(X86CPU *cpu);
+X86CPU *hyperv_find_vcpu(uint32_t vp_index);
+
 #endif
target/i386/hyperv.c +4 -0
index 227185c..4f57447 100644
@@ -16,6 +16,16 @@
 #include "hyperv.h"
 #include "hyperv_proto.h"
 
+uint32_t hyperv_vp_index(X86CPU *cpu)
+{
+    return CPU(cpu)->cpu_index;
+}  
Quoted 5 lines

target/i386/hyperv.c line 27

Quoted 4 lines
target/i386/hyperv.c +4 -0
+X86CPU *hyperv_find_vcpu(uint32_t vp_index)
+{
+    return X86_CPU(qemu_get_cpu(vp_index));
+}  

this helper isn't used in this patch, add it in the patch that would actually use it

I thought I would put the only two functions that encapsulate the knowledge of how vp_index is realted to cpu_index, in a single patch.

I'm now thinking of open-coding the iteration over cpus here and directly look for cpu whose hyperv_vp_index() matches. Then that knowledge will become encapsulated in a single place, and indeed, this helper can go into another patch where it's used.

Quoted 5 lines
Quoted 2 lines

also if qemu_get_cpu() were called from each CPU init, it would incur O(N^2) complexity, could you do without it?

It isn't called on hot paths (ATM it's called only when SINT routes are created, which is at most one per cpu). I don't see a problem here.

For what/where do you need this lookup?

Quoted 14 lines
Quoted 8 lines

target/i386/hyperv.c HvSintRoute *kvm_hv_sint_route_create(uint32_t vcpu_id, uint32_t sint, · line 118

Quoted 7 lines
target/i386/hyperv.c +1 -1
@@ -105,7 +115,7 @@ HvSintRoute *kvm_hv_sint_route_create(uint32_t vcpu_id, uint32_t sint,
     }
     sint_route->gsi = gsi;
     sint_route->sint_ack_clb = sint_ack_clb;
-    sint_route->vcpu_id = vcpu_id;
+    sint_route->vcpu_id = vp_index;  

^^^ - shouldn't it also be re-named?

Right, but vcpu_id on sint_route is replaced with X86CPU pointer in a followup patch, so I wasn't sure if it was worth while to add more churn...

Quoted 1 line
Quoted 4 lines
Quoted 1 line

maybe split all renaming into separate patch ...

Part of the renaming will disappear eventually in the followup patches, so I'm sure it's a good idea... Opinions?

I'd still spit. not to distract reviewers from what this patch is actually tries to accomplish at least

Quoted 26 lines
Quoted 17 lines
Quoted 17 lines
target/i386/hyperv.c +16 -0
+static int hyperv_init_vcpu(X86CPU *cpu)
+{
+    if (cpu->hyperv_vpindex && !is_hv_vpindex_settable) {
+        /*
+         * the kernel doesn't support setting vp_index; assert that its value
+         * is in sync
+         */
+        int ret;
+        struct {
+            struct kvm_msrs info;
+            struct kvm_msr_entry entries[1];
+        } msr_data = {
+            .info.nmsrs = 1,
+            .entries[0].index = HV_X64_MSR_VP_INDEX,
+        };
+
Quoted 8 lines

target/i386/hyperv.c

Quoted 5 lines
target/i386/hyperv.c +5 -0
+        /*
+         * handling errors from vcpu init at hotplug time is impossible, so
+         * disallow cpu hotplug
+         */
+        MACHINE_GET_CLASS(current_machine)->hot_add_cpu = NULL;  

one shouldn't alter machine this way nor
it would disable cpu hotplug, it would disable only cpu-add interface but device_add() would still work.

Quoted 7 lines

Understood, will rework as you suggest above.

Quoted 3 lines

target/i386/hyperv.c

Quoted 2 lines
target/i386/hyperv.c +2 -0
+        ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_MSRS, &msr_data);
+        if (ret < 0) {  

when this can fail?

Dunno, but not checking for errors doesn't look good.

Point of asking is to confirm that call shouldn't fail normally and if it fails it's we can't recover and should die.

Looking at condition above !is_hv_vpindex_settable it looks like call wouldn't fail. (i.e. it makes sure that qemu is running on supported kernel)

Quoted 14 lines
Quoted 8 lines

target/i386/hyperv.c

Quoted 7 lines
target/i386/hyperv.c +6 -0
+            return ret;
+        }
+        assert(ret == 1);
+
+        if (msr_data.entries[0].data != hyperv_vp_index(cpu)) {
+            fprintf(stderr, "kernel's vp_index != QEMU's vp_index\n");  

error_report()

OK (target/i386/kvm.c is already a mix of all possible styles of error reporting, I must've followed a wrong one).

Thanks,
Roman.

#11 lore raw

Re: [Qemu-devel] [PATCH v2 07/23] hyperv: ensure VP index equal to QEMU cpu_index

Quoted 35 lines · Igor Mammedov
Quoted 32 lines · On Thu, 29 Jun 2017 12:53:27 +0300 Roman Kagan <[email protected]> wrote:
Quoted 15 lines · Igor Mammedov
Quoted 12 lines · On Wed, 21 Jun 2017 19:24:08 +0300 Roman Kagan <[email protected]> wrote:

Hyper-V identifies vCPUs by Virtual Processor (VP) index which can be queried by the guest via HV_X64_MSR_VP_INDEX msr. It is defined by the spec as a sequential number which can't exceed the maximum number of vCPUs per VM.

It has to be owned by QEMU in order to preserve it across migration.

However, the initial implementation in KVM didn't allow to set this msr, and KVM used its own notion of VP index. Fortunately, the way vCPUs are created in QEMU/KVM makes it likely that the KVM value is equal to QEMU cpu_index.

Quoted 8 lines
Quoted 6 lines

So choose cpu_index as the value for vp_index, and push that to KVM on kernels that support setting the msr. On older ones that don't, query the kernel value and assert that it's in sync with QEMU.

Besides, since handling errors from vCPU init at hotplug time is impossible, disable vCPU hotplug.

proper place to check if cpu might be created is at pc_cpu_pre_plug() where you can gracefully abort cpu creation process.

Thanks for the suggestion, I'll rework it this way.

Quoted 4 lines

Also it's possible to create cold-plugged CPUs in out of order sequence using
-device cpu-foo on CLI
will be hyperv kvm/guest side ok with it?

Quoted 8 lines
Quoted 4 lines

On kernels that support setting HV_X64_MSR_VP_INDEX QEMU will synchronize all sides. On kernels that don't, if out-of-order creation results in vp_index mismatch between the kernel and QEMU, vcpu creation will fail.

And additional question,
what would happen if VM is started on host supporting VP index setting and then migrated to a host without it?

The destination QEMU will attempt to initialize vCPUs, and if that fails (e.g. due to vp_index mismatch), the migration will be aborted and the source VM will continue running.

If the destination QEMU is old, too, there's a chance that vp_index will change. Then we just keep the fingers crossed that the guest doesn't notice (this is the behavior we have now).

Quoted 14 lines
Quoted 14 lines
Quoted 5 lines

target/i386/hyperv.c line 27

Quoted 4 lines
target/i386/hyperv.c +4 -0
+X86CPU *hyperv_find_vcpu(uint32_t vp_index)
+{
+    return X86_CPU(qemu_get_cpu(vp_index));
+}  

this helper isn't used in this patch, add it in the patch that would actually use it

I thought I would put the only two functions that encapsulate the knowledge of how vp_index is realted to cpu_index, in a single patch.

I'm now thinking of open-coding the iteration over cpus here and directly look for cpu whose hyperv_vp_index() matches. Then that knowledge will become encapsulated in a single place, and indeed, this helper can go into another patch where it's used.

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

also if qemu_get_cpu() were called from each CPU init, it would incur O(N^2) complexity, could you do without it?

It isn't called on hot paths (ATM it's called only when SINT routes are created, which is at most one per cpu). I don't see a problem here.

For what/where do you need this lookup?

The guest configures devices to signal their events with synthetic interrupts on specific cpus, identified by vp_index. When we receive such a request we look up the cpu and set up a SINT route to be able to deliver interrupts to its synic.

Or did I misunderstand the question perhaps?

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

maybe split all renaming into separate patch ...

Part of the renaming will disappear eventually in the followup patches, so I'm sure it's a good idea... Opinions?

I'd still spit. not to distract reviewers from what this patch is actually tries to accomplish at least

OK, makes sense.

Quoted 11 lines
Quoted 5 lines
Quoted 3 lines

target/i386/kvm.c static int hyperv_handle_properties(CPUState *cs) · line 692

Quoted 2 lines
target/i386/kvm.c +2 -0
+        ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_MSRS, &msr_data);
+        if (ret < 0) {  

when this can fail?

Dunno, but not checking for errors doesn't look good.

Point of asking is to confirm that call shouldn't fail normally and if it fails it's we can't recover and should die.

Looking at condition above !is_hv_vpindex_settable it looks like call wouldn't fail. (i.e. it makes sure that qemu is running on supported kernel)

No, I do KVM_GET_MSRS here, which is supported since this msr was introduced.

But actually you've spot a bug which I meant to fix but forgot: the code in hyperv_handle_properties should fail if a particular hyperv feature is requested but the corresponding msr isn't found among reported via KVM_GET_MSR_INDEX_LIST. ATM this is only done for some features but not all.

I'll try to fix that; once done, cpu->hyperv_vpindex here will make sure this msr is gettable.

Thanks,
Roman.

#12 lore raw

Re: [Qemu-devel] [PATCH v2 07/23] hyperv: ensure VP index equal to QEMU cpu_index

Quoted 45 lines · On Thu, 29 Jun 2017 16:10:20 +0300 Roman Kagan <[email protected]> wrote:
Quoted 35 lines · Igor Mammedov
Quoted 32 lines · On Thu, 29 Jun 2017 12:53:27 +0300 Roman Kagan <[email protected]> wrote:
Quoted 15 lines · Igor Mammedov
Quoted 12 lines · On Wed, 21 Jun 2017 19:24:08 +0300 Roman Kagan <[email protected]> wrote:

Hyper-V identifies vCPUs by Virtual Processor (VP) index which can be queried by the guest via HV_X64_MSR_VP_INDEX msr. It is defined by the spec as a sequential number which can't exceed the maximum number of vCPUs per VM.

It has to be owned by QEMU in order to preserve it across migration.

However, the initial implementation in KVM didn't allow to set this msr, and KVM used its own notion of VP index. Fortunately, the way vCPUs are created in QEMU/KVM makes it likely that the KVM value is equal to QEMU cpu_index.

Quoted 8 lines
Quoted 6 lines

So choose cpu_index as the value for vp_index, and push that to KVM on kernels that support setting the msr. On older ones that don't, query the kernel value and assert that it's in sync with QEMU.

Besides, since handling errors from vCPU init at hotplug time is impossible, disable vCPU hotplug.

proper place to check if cpu might be created is at pc_cpu_pre_plug() where you can gracefully abort cpu creation process.

Thanks for the suggestion, I'll rework it this way.

Quoted 4 lines

Also it's possible to create cold-plugged CPUs in out of order sequence using
-device cpu-foo on CLI
will be hyperv kvm/guest side ok with it?

Quoted 8 lines
Quoted 4 lines

On kernels that support setting HV_X64_MSR_VP_INDEX QEMU will synchronize all sides. On kernels that don't, if out-of-order creation results in vp_index mismatch between the kernel and QEMU, vcpu creation will fail.

And additional question,
what would happen if VM is started on host supporting VP index setting and then migrated to a host without it?

Quoted 7 lines

The destination QEMU will attempt to initialize vCPUs, and if that fails (e.g. due to vp_index mismatch), the migration will be aborted and the source VM will continue running.

If the destination QEMU is old, too, there's a chance that vp_index will change. Then we just keep the fingers crossed that the guest doesn't notice (this is the behavior we have now).

on source, putting in migration stream a flag that setting HV_X64_MSR_VP_INDEX is in use, should prevent migration to the old destination or new destination but without kernel support.
It also might make sense to disable feature for old machine types
so new->old migration would work as it used to be even if destination kernel supports feature.

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
Quoted 5 lines

target/i386/hyperv.c line 27

Quoted 4 lines
target/i386/hyperv.c +4 -0
+X86CPU *hyperv_find_vcpu(uint32_t vp_index)
+{
+    return X86_CPU(qemu_get_cpu(vp_index));
+}    

this helper isn't used in this patch, add it in the patch that would actually use it

I thought I would put the only two functions that encapsulate the knowledge of how vp_index is realted to cpu_index, in a single patch.

I'm now thinking of open-coding the iteration over cpus here and directly look for cpu whose hyperv_vp_index() matches. Then that knowledge will become encapsulated in a single place, and indeed, this helper can go into another patch where it's used.

Quoted 2 lines

also if qemu_get_cpu() were called from each CPU init, it would incur O(N^2) complexity, could you do without it?

It isn't called on hot paths (ATM it's called only when SINT routes are created, which is at most one per cpu). I don't see a problem here.

Quoted 8 lines
Quoted 1 line

For what/where do you need this lookup?

The guest configures devices to signal their events with synthetic interrupts on specific cpus, identified by vp_index. When we receive such a request we look up the cpu and set up a SINT route to be able to deliver interrupts to its synic.

Or did I misunderstand the question perhaps?

since there is 1:1 mapping between synic:vp_index and vp_index is dense interval of [0..maxcpus),
I'd suggest to maintain internal synic map where vp_index could be used as index in array to fetch addressed synic.

look for local_apics as example.

[...]

#13 lore raw

Re: [Qemu-devel] [PATCH v2 07/23] hyperv: ensure VP index equal to QEMU cpu_index

Quoted 52 lines · Igor Mammedov
Quoted 49 lines · On Thu, 29 Jun 2017 16:10:20 +0300 Roman Kagan <[email protected]> wrote:
Quoted 35 lines · Igor Mammedov
Quoted 32 lines · On Thu, 29 Jun 2017 12:53:27 +0300 Roman Kagan <[email protected]> wrote:
Quoted 15 lines · Igor Mammedov
Quoted 12 lines · On Wed, 21 Jun 2017 19:24:08 +0300 Roman Kagan <[email protected]> wrote:

Hyper-V identifies vCPUs by Virtual Processor (VP) index which can be queried by the guest via HV_X64_MSR_VP_INDEX msr. It is defined by the spec as a sequential number which can't exceed the maximum number of vCPUs per VM.

It has to be owned by QEMU in order to preserve it across migration.

However, the initial implementation in KVM didn't allow to set this msr, and KVM used its own notion of VP index. Fortunately, the way vCPUs are created in QEMU/KVM makes it likely that the KVM value is equal to QEMU cpu_index.

Quoted 8 lines
Quoted 6 lines

So choose cpu_index as the value for vp_index, and push that to KVM on kernels that support setting the msr. On older ones that don't, query the kernel value and assert that it's in sync with QEMU.

Besides, since handling errors from vCPU init at hotplug time is impossible, disable vCPU hotplug.

proper place to check if cpu might be created is at pc_cpu_pre_plug() where you can gracefully abort cpu creation process.

Thanks for the suggestion, I'll rework it this way.

Quoted 4 lines

Also it's possible to create cold-plugged CPUs in out of order sequence using
-device cpu-foo on CLI
will be hyperv kvm/guest side ok with it?

Quoted 8 lines
Quoted 4 lines

On kernels that support setting HV_X64_MSR_VP_INDEX QEMU will synchronize all sides. On kernels that don't, if out-of-order creation results in vp_index mismatch between the kernel and QEMU, vcpu creation will fail.

And additional question,
what would happen if VM is started on host supporting VP index setting and then migrated to a host without it?

The destination QEMU will attempt to initialize vCPUs, and if that fails (e.g. due to vp_index mismatch), the migration will be aborted and the source VM will continue running.

Quoted 6 lines
Quoted 3 lines

If the destination QEMU is old, too, there's a chance that vp_index will change. Then we just keep the fingers crossed that the guest doesn't notice (this is the behavior we have now).

on source, putting in migration stream a flag that setting HV_X64_MSR_VP_INDEX is in use, should prevent migration to the old destination or new destination but without kernel support.

These are different cases. New destination QEMU can verify that all vcpus have the desired vp_index even if it can't set it, so in this case vp_index migration is even reliable.

Old QEMU didn't bother so it potentially can confuse the guest. But we're unaware of this ever happening in the past, probably because the existing users of synic are only in-kvm synic timers which don't depend on vp_index.

Quoted 3 lines

It also might make sense to disable feature for old machine types so new->old migration would work as it used to be even if destination kernel supports feature.

I'm not sure it's worth the effort, especially since other patches in the series introduce "in-kvm-only" flag in SynIC, which is "on" for old machine types. So eventually the migration to/from an old QEMU will only be possible for configurations with only in-kvm synic users, where we hope vp_index not to matter.

Quoted 26 lines
Quoted 26 lines
Quoted 14 lines
Quoted 14 lines
Quoted 5 lines

target/i386/hyperv.c line 27

Quoted 4 lines
target/i386/hyperv.c +4 -0
+X86CPU *hyperv_find_vcpu(uint32_t vp_index)
+{
+    return X86_CPU(qemu_get_cpu(vp_index));
+}    

this helper isn't used in this patch, add it in the patch that would actually use it

I thought I would put the only two functions that encapsulate the knowledge of how vp_index is realted to cpu_index, in a single patch.

I'm now thinking of open-coding the iteration over cpus here and directly look for cpu whose hyperv_vp_index() matches. Then that knowledge will become encapsulated in a single place, and indeed, this helper can go into another patch where it's used.

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

also if qemu_get_cpu() were called from each CPU init, it would incur O(N^2) complexity, could you do without it?

It isn't called on hot paths (ATM it's called only when SINT routes are created, which is at most one per cpu). I don't see a problem here.

For what/where do you need this lookup?

The guest configures devices to signal their events with synthetic interrupts on specific cpus, identified by vp_index. When we receive such a request we look up the cpu and set up a SINT route to be able to deliver interrupts to its synic.

Quoted 5 lines
Quoted 1 line

Or did I misunderstand the question perhaps?

since there is 1:1 mapping between synic:vp_index and vp_index is dense interval of [0..maxcpus),
I'd suggest to maintain internal synic map where vp_index could be used as index in array to fetch addressed synic.

Ah, I see. I wonder why qemu_get_cpu() itself isn't implemented this way?

Roman.

#14 lore raw

PATCH v2 8/23 hyperv_testdev: refactor for readability

In reply to Roman Kagan

Make hyperv_testdev slightly easier to follow and enhance in future. For that, put the hyperv sint routes (wrapped in a helper structure) on a linked list rather than a fixed-size array.

---
v1 -> v2:
- was patch 7 in v1
- renamed the variables to distinguish vp_index from vcpu_id

 hw/misc/hyperv_testdev.c | 114 ++++++++++++++++++++++-------------------------
 1 file changed, 53 insertions(+), 61 deletions(-)
hw/misc/hyperv_testdev.c +53 -61
index dbd7cdd..b47af47 100644
@@ -12,6 +12,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/queue.h"
 #include <linux/kvm.h>
 #include "hw/hw.h"
 #include "hw/qdev.h"
@@ -20,12 +21,17 @@
 #include "target/i386/hyperv.h"
 #include "kvm_i386.h"
 
-#define HV_TEST_DEV_MAX_SINT_ROUTES 64
+typedef struct TestSintRoute {
+    QLIST_ENTRY(TestSintRoute) le;
+    uint8_t vpidx;
+    uint8_t sint;
+    HvSintRoute *sint_route;
+} TestSintRoute;
 
 struct HypervTestDev {
     ISADevice parent_obj;
     MemoryRegion sint_control;
-    HvSintRoute *sint_route[HV_TEST_DEV_MAX_SINT_ROUTES];
+    QLIST_HEAD(, TestSintRoute) sint_routes;
 };
 typedef struct HypervTestDev HypervTestDev;
 
@@ -39,88 +45,74 @@ enum {
     HV_TEST_DEV_SINT_ROUTE_SET_SINT
 };
 
-static int alloc_sint_route_index(HypervTestDev *dev)
+static void sint_route_create(HypervTestDev *dev, uint8_t vpidx, uint8_t sint)
 {
-    int i;
+    TestSintRoute *sint_route;
 
-    for (i = 0; i < ARRAY_SIZE(dev->sint_route); i++) {
-        if (dev->sint_route[i] == NULL) {
-            return i;
-        }
-    }
-    return -1;
-}
+    sint_route = g_new0(TestSintRoute, 1);
+    assert(sint_route);
 
-static void free_sint_route_index(HypervTestDev *dev, int i)
-{
-    assert(i >= 0 && i < ARRAY_SIZE(dev->sint_route));
-    dev->sint_route[i] = NULL;
+    sint_route->vpidx = vpidx;
+    sint_route->sint = sint;
+
+    sint_route->sint_route = kvm_hv_sint_route_create(vpidx, sint, NULL);
+    assert(sint_route->sint_route);
+
+    QLIST_INSERT_HEAD(&dev->sint_routes, sint_route, le);
 }
 
-static int find_sint_route_index(HypervTestDev *dev, uint32_t vcpu_id,
-                                 uint32_t sint)
+static TestSintRoute *sint_route_find(HypervTestDev *dev,
+                                      uint8_t vpidx, uint8_t sint)
 {
-    HvSintRoute *sint_route;
-    int i;
+    TestSintRoute *sint_route;
 
-    for (i = 0; i < ARRAY_SIZE(dev->sint_route); i++) {
-        sint_route = dev->sint_route[i];
-        if (sint_route && sint_route->vcpu_id == vcpu_id &&
-            sint_route->sint == sint) {
-            return i;
+    QLIST_FOREACH(sint_route, &dev->sint_routes, le) {
+        if (sint_route->vpidx == vpidx && sint_route->sint == sint) {
+            return sint_route;
         }
     }
-    return -1;
+    assert(false);
+    return NULL;
 }
 
-static void hv_synic_test_dev_control(HypervTestDev *dev, uint32_t ctl,
-                                      uint32_t vcpu_id, uint32_t sint)
+static void sint_route_destroy(HypervTestDev *dev, uint8_t vpidx, uint8_t sint)
 {
-    int i;
-    HvSintRoute *sint_route;
+    TestSintRoute *sint_route;
 
-    switch (ctl) {
-    case HV_TEST_DEV_SINT_ROUTE_CREATE:
-        i = alloc_sint_route_index(dev);
-        assert(i >= 0);
-        sint_route = kvm_hv_sint_route_create(vcpu_id, sint, NULL);
-        assert(sint_route);
-        dev->sint_route[i] = sint_route;
-        break;
-    case HV_TEST_DEV_SINT_ROUTE_DESTROY:
-        i = find_sint_route_index(dev, vcpu_id, sint);
-        assert(i >= 0);
-        sint_route = dev->sint_route[i];
-        kvm_hv_sint_route_destroy(sint_route);
-        free_sint_route_index(dev, i);
-        break;
-    case HV_TEST_DEV_SINT_ROUTE_SET_SINT:
-        i = find_sint_route_index(dev, vcpu_id, sint);
-        assert(i >= 0);
-        sint_route = dev->sint_route[i];
-        kvm_hv_sint_route_set_sint(sint_route);
-        break;
-    default:
-        break;
-    }
+    sint_route = sint_route_find(dev, vpidx, sint);
+    QLIST_REMOVE(sint_route, le);
+    kvm_hv_sint_route_destroy(sint_route->sint_route);
+    g_free(sint_route);
+}
+
+static void sint_route_set_sint(HypervTestDev *dev,
+                                uint8_t vpidx, uint8_t sint)
+{
+    TestSintRoute *sint_route;
+
+    sint_route = sint_route_find(dev, vpidx, sint);
+
+    kvm_hv_sint_route_set_sint(sint_route->sint_route);
 }
 
 static void hv_test_dev_control(void *opaque, hwaddr addr, uint64_t data,
                                 uint32_t len)
 {
     HypervTestDev *dev = HYPERV_TEST_DEV(opaque);
-    uint8_t ctl;
+    uint8_t sint = data & 0xFF;
+    uint8_t vpidx = (data >> 8ULL) & 0xFF;
+    uint8_t ctl = (data >> 16ULL) & 0xFF;
 
-    ctl = (data >> 16ULL) & 0xFF;
     switch (ctl) {
     case HV_TEST_DEV_SINT_ROUTE_CREATE:
+        sint_route_create(dev, vpidx, sint);
+        break;
     case HV_TEST_DEV_SINT_ROUTE_DESTROY:
-    case HV_TEST_DEV_SINT_ROUTE_SET_SINT: {
-        uint8_t sint = data & 0xFF;
-        uint8_t vcpu_id = (data >> 8ULL) & 0xFF;
-        hv_synic_test_dev_control(dev, ctl, vcpu_id, sint);
+        sint_route_destroy(dev, vpidx, sint);
+        break;
+    case HV_TEST_DEV_SINT_ROUTE_SET_SINT:
+        sint_route_set_sint(dev, vpidx, sint);
         break;
-    }
     default:
         break;
     }
@@ -139,7 +131,7 @@ static void hv_test_dev_realizefn(DeviceState *d, Error **errp)
     HypervTestDev *dev = HYPERV_TEST_DEV(d);
     MemoryRegion *io = isa_address_space_io(isa);
 
-    memset(dev->sint_route, 0, sizeof(dev->sint_route));
+    QLIST_INIT(&dev->sint_routes);
     memory_region_init_io(&dev->sint_control, OBJECT(dev),
                           &synic_test_sint_ops, dev,
                           "hyperv-testdev-ctl", 4);
Signature · 1 line
2.9.4
#15 lore raw

PATCH v2 9/23 hyperv: cosmetic: g_malloc -> g_new

In reply to Roman Kagan
---
 target/i386/hyperv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/i386/hyperv.c +1 -1
index 4f57447..2121d8f 100644
@@ -88,7 +88,7 @@ HvSintRoute *kvm_hv_sint_route_create(uint32_t vp_index, uint32_t sint,
     HvSintRoute *sint_route;
     int r, gsi;
 
-    sint_route = g_malloc0(sizeof(*sint_route));
+    sint_route = g_new0(HvSintRoute, 1);
     r = event_notifier_init(&sint_route->sint_set_notifier, false);
     if (r) {
         goto err;
Signature · 1 line
2.9.4
#16 lore raw

PATCH v2 10/23 hyperv: synic: only setup ack notifier if there's a callback

In reply to Roman Kagan

There's no point setting up an sint ack notifier if no callback is specified.

---
 target/i386/hyperv.c | 33 +++++++++++++++++++--------------
 1 file changed, 19 insertions(+), 14 deletions(-)
target/i386/hyperv.c +19 -14
index 2121d8f..3594bd0 100644
@@ -77,15 +77,14 @@ static void kvm_hv_sint_ack_handler(EventNotifier *notifier)
     HvSintRoute *sint_route = container_of(notifier, HvSintRoute,
                                            sint_ack_notifier);
     event_notifier_test_and_clear(notifier);
-    if (sint_route->sint_ack_clb) {
-        sint_route->sint_ack_clb(sint_route);
-    }
+    sint_route->sint_ack_clb(sint_route);
 }
 
 HvSintRoute *kvm_hv_sint_route_create(uint32_t vp_index, uint32_t sint,
                                       HvSintAckClb sint_ack_clb)
 {
     HvSintRoute *sint_route;
+    EventNotifier *ack_notifier;
     int r, gsi;
 
     sint_route = g_new0(HvSintRoute, 1);
@@ -94,13 +93,15 @@ HvSintRoute *kvm_hv_sint_route_create(uint32_t vp_index, uint32_t sint,
         goto err;
     }
 
-    r = event_notifier_init(&sint_route->sint_ack_notifier, false);
-    if (r) {
-        goto err_sint_set_notifier;
-    }
+    ack_notifier = sint_ack_clb ? &sint_route->sint_ack_notifier : NULL;
+    if (ack_notifier) {
+        r = event_notifier_init(ack_notifier, false);
+        if (r) {
+            goto err_sint_set_notifier;
+        }
 
-    event_notifier_set_handler(&sint_route->sint_ack_notifier,
-                               kvm_hv_sint_ack_handler);
+        event_notifier_set_handler(ack_notifier, kvm_hv_sint_ack_handler);
+    }
 
     gsi = kvm_irqchip_add_hv_sint_route(kvm_state, vp_index, sint);
     if (gsi < 0) {
@@ -109,7 +110,7 @@ HvSintRoute *kvm_hv_sint_route_create(uint32_t vp_index, uint32_t sint,
 
     r = kvm_irqchip_add_irqfd_notifier_gsi(kvm_state,
                                            &sint_route->sint_set_notifier,
-                                           &sint_route->sint_ack_notifier, gsi);
+                                           ack_notifier, gsi);
     if (r) {
         goto err_irqfd;
     }
@@ -123,8 +124,10 @@ HvSintRoute *kvm_hv_sint_route_create(uint32_t vp_index, uint32_t sint,
 err_irqfd:
     kvm_irqchip_release_virq(kvm_state, gsi);
 err_gsi:
-    event_notifier_set_handler(&sint_route->sint_ack_notifier, NULL);
-    event_notifier_cleanup(&sint_route->sint_ack_notifier);
+    if (ack_notifier) {
+        event_notifier_set_handler(ack_notifier, NULL);
+        event_notifier_cleanup(ack_notifier);
+    }
 err_sint_set_notifier:
     event_notifier_cleanup(&sint_route->sint_set_notifier);
 err:
@@ -139,8 +142,10 @@ void kvm_hv_sint_route_destroy(HvSintRoute *sint_route)
                                           &sint_route->sint_set_notifier,
                                           sint_route->gsi);
     kvm_irqchip_release_virq(kvm_state, sint_route->gsi);
-    event_notifier_set_handler(&sint_route->sint_ack_notifier, NULL);
-    event_notifier_cleanup(&sint_route->sint_ack_notifier);
+    if (sint_route->sint_ack_clb) {
+        event_notifier_set_handler(&sint_route->sint_ack_notifier, NULL);
+        event_notifier_cleanup(&sint_route->sint_ack_notifier);
+    }
     event_notifier_cleanup(&sint_route->sint_set_notifier);
     g_free(sint_route);
 }
Signature · 1 line
2.9.4
#17 lore raw

PATCH v2 11/23 hyperv: allow passing arbitrary data to sint ack callback

In reply to Roman Kagan

Make sint ack callback accept an opaque pointer, that is stored on sint_route at creation time.

This allows for more convenient interaction with the callback.

Besides, nothing outside hyperv.c should need to know the layout of HvSintRoute fields any more so its declaration can be removed from the header.

---
 target/i386/hyperv.h     | 14 +++-----------
 hw/misc/hyperv_testdev.c |  2 +-
 target/i386/hyperv.c     | 16 ++++++++++++++--
 3 files changed, 18 insertions(+), 14 deletions(-)
target/i386/hyperv.h +3 -11
index 82f4757..93f7300 100644
@@ -19,21 +19,13 @@
 #include "qemu/event_notifier.h"
 
 typedef struct HvSintRoute HvSintRoute;
-typedef void (*HvSintAckClb)(HvSintRoute *sint_route);
-
-struct HvSintRoute {
-    uint32_t sint;
-    uint32_t vcpu_id;
-    int gsi;
-    EventNotifier sint_set_notifier;
-    EventNotifier sint_ack_notifier;
-    HvSintAckClb sint_ack_clb;
-};
+typedef void (*HvSintAckClb)(void *data);
 
 int kvm_hv_handle_exit(X86CPU *cpu, struct kvm_hyperv_exit *exit);
 
 HvSintRoute *kvm_hv_sint_route_create(uint32_t vp_index, uint32_t sint,
-                                      HvSintAckClb sint_ack_clb);
+                                      HvSintAckClb sint_ack_clb,
+                                      void *sint_ack_clb_data);
 
 void kvm_hv_sint_route_destroy(HvSintRoute *sint_route);
 
hw/misc/hyperv_testdev.c +1 -1
index b47af47..827a8b1 100644
@@ -55,7 +55,7 @@ static void sint_route_create(HypervTestDev *dev, uint8_t vpidx, uint8_t sint)
     sint_route->vpidx = vpidx;
     sint_route->sint = sint;
 
-    sint_route->sint_route = kvm_hv_sint_route_create(vpidx, sint, NULL);
+    sint_route->sint_route = kvm_hv_sint_route_create(vpidx, sint, NULL, NULL);
     assert(sint_route->sint_route);
 
     QLIST_INSERT_HEAD(&dev->sint_routes, sint_route, le);
target/i386/hyperv.c +14 -2
index 3594bd0..84ea228 100644
@@ -16,6 +16,16 @@
 #include "hyperv.h"
 #include "hyperv_proto.h"
 
+struct HvSintRoute {
+    uint32_t sint;
+    uint32_t vcpu_id;
+    int gsi;
+    EventNotifier sint_set_notifier;
+    EventNotifier sint_ack_notifier;
+    HvSintAckClb sint_ack_clb;
+    void *sint_ack_clb_data;
+};
+
 uint32_t hyperv_vp_index(X86CPU *cpu)
 {
     return CPU(cpu)->cpu_index;
@@ -77,11 +87,12 @@ static void kvm_hv_sint_ack_handler(EventNotifier *notifier)
     HvSintRoute *sint_route = container_of(notifier, HvSintRoute,
                                            sint_ack_notifier);
     event_notifier_test_and_clear(notifier);
-    sint_route->sint_ack_clb(sint_route);
+    sint_route->sint_ack_clb(sint_route->sint_ack_clb_data);
 }
 
 HvSintRoute *kvm_hv_sint_route_create(uint32_t vp_index, uint32_t sint,
-                                      HvSintAckClb sint_ack_clb)
+                                      HvSintAckClb sint_ack_clb,
+                                      void *sint_ack_clb_data)
 {
     HvSintRoute *sint_route;
     EventNotifier *ack_notifier;
@@ -116,6 +127,7 @@ HvSintRoute *kvm_hv_sint_route_create(uint32_t vp_index, uint32_t sint,
     }
     sint_route->gsi = gsi;
     sint_route->sint_ack_clb = sint_ack_clb;
+    sint_route->sint_ack_clb_data = sint_ack_clb_data;
     sint_route->vcpu_id = vp_index;
     sint_route->sint = sint;
 
Signature · 1 line
2.9.4
#18 lore raw

PATCH v2 12/23 hyperv: address HvSintRoute by X86CPU pointer

In reply to Roman Kagan

Use X86CPU pointer to refer to the respective HvSintRoute instead of vp_index. This is more convenient and also paves the way for future enhancements.

---
v1 -> v2:
- was patch 11 in v1
- pass vp_index to sint_route_create, and lookup X86CPU * inside

 target/i386/hyperv.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
target/i386/hyperv.c +8 -2
index 84ea228..b5831bf 100644
@@ -18,7 +18,7 @@
 
 struct HvSintRoute {
     uint32_t sint;
-    uint32_t vcpu_id;
+    X86CPU *cpu;
     int gsi;
     EventNotifier sint_set_notifier;
     EventNotifier sint_ack_notifier;
@@ -97,6 +97,12 @@ HvSintRoute *kvm_hv_sint_route_create(uint32_t vp_index, uint32_t sint,
     HvSintRoute *sint_route;
     EventNotifier *ack_notifier;
     int r, gsi;
+    X86CPU *cpu;
+
+    cpu = hyperv_find_vcpu(vp_index);
+    if (!cpu) {
+        return NULL;
+    }
 
     sint_route = g_new0(HvSintRoute, 1);
     r = event_notifier_init(&sint_route->sint_set_notifier, false);
@@ -128,7 +134,7 @@ HvSintRoute *kvm_hv_sint_route_create(uint32_t vp_index, uint32_t sint,
     sint_route->gsi = gsi;
     sint_route->sint_ack_clb = sint_ack_clb;
     sint_route->sint_ack_clb_data = sint_ack_clb_data;
-    sint_route->vcpu_id = vp_index;
+    sint_route->cpu = cpu;
     sint_route->sint = sint;
 
     return sint_route;
Signature · 1 line
2.9.4
#19 lore raw

PATCH v2 13/23 hyperv: make HvSintRoute reference-counted

In reply to Roman Kagan

Multiple entities (e.g. VMBus devices) can use the same SINT route. To make their lives easier in maintaining SINT route ownership, make it reference-counted. Adjust the respective API names accordingly.

---
 target/i386/hyperv.h     | 10 +++++-----
 hw/misc/hyperv_testdev.c |  4 ++--
 target/i386/hyperv.c     | 25 +++++++++++++++++++++----
 3 files changed, 28 insertions(+), 11 deletions(-)
target/i386/hyperv.h +5 -5
index 93f7300..af5fc05 100644
@@ -23,11 +23,11 @@ typedef void (*HvSintAckClb)(void *data);
 
 int kvm_hv_handle_exit(X86CPU *cpu, struct kvm_hyperv_exit *exit);
 
-HvSintRoute *kvm_hv_sint_route_create(uint32_t vp_index, uint32_t sint,
-                                      HvSintAckClb sint_ack_clb,
-                                      void *sint_ack_clb_data);
-
-void kvm_hv_sint_route_destroy(HvSintRoute *sint_route);
+HvSintRoute *hyperv_sint_route_new(uint32_t vp_index, uint32_t sint,
+                                   HvSintAckClb sint_ack_clb,
+                                   void *sint_ack_clb_data);
+void hyperv_sint_route_ref(HvSintRoute *sint_route);
+void hyperv_sint_route_unref(HvSintRoute *sint_route);
 
 int kvm_hv_sint_route_set_sint(HvSintRoute *sint_route);
 
hw/misc/hyperv_testdev.c +2 -2
index 827a8b1..fa435ab 100644
@@ -55,7 +55,7 @@ static void sint_route_create(HypervTestDev *dev, uint8_t vpidx, uint8_t sint)
     sint_route->vpidx = vpidx;
     sint_route->sint = sint;
 
-    sint_route->sint_route = kvm_hv_sint_route_create(vpidx, sint, NULL, NULL);
+    sint_route->sint_route = hyperv_sint_route_new(vpidx, sint, NULL, NULL);
     assert(sint_route->sint_route);
 
     QLIST_INSERT_HEAD(&dev->sint_routes, sint_route, le);
@@ -81,7 +81,7 @@ static void sint_route_destroy(HypervTestDev *dev, uint8_t vpidx, uint8_t sint)
 
     sint_route = sint_route_find(dev, vpidx, sint);
     QLIST_REMOVE(sint_route, le);
-    kvm_hv_sint_route_destroy(sint_route->sint_route);
+    hyperv_sint_route_unref(sint_route->sint_route);
     g_free(sint_route);
 }
 
target/i386/hyperv.c +21 -4
index b5831bf..012c79d 100644
@@ -24,6 +24,7 @@ struct HvSintRoute {
     EventNotifier sint_ack_notifier;
     HvSintAckClb sint_ack_clb;
     void *sint_ack_clb_data;
+    unsigned refcount;
 };
 
 uint32_t hyperv_vp_index(X86CPU *cpu)
@@ -90,9 +91,9 @@ static void kvm_hv_sint_ack_handler(EventNotifier *notifier)
     sint_route->sint_ack_clb(sint_route->sint_ack_clb_data);
 }
 
-HvSintRoute *kvm_hv_sint_route_create(uint32_t vp_index, uint32_t sint,
-                                      HvSintAckClb sint_ack_clb,
-                                      void *sint_ack_clb_data)
+HvSintRoute *hyperv_sint_route_new(uint32_t vp_index, uint32_t sint,
+                                   HvSintAckClb sint_ack_clb,
+                                   void *sint_ack_clb_data)
 {
     HvSintRoute *sint_route;
     EventNotifier *ack_notifier;
@@ -136,6 +137,7 @@ HvSintRoute *kvm_hv_sint_route_create(uint32_t vp_index, uint32_t sint,
     sint_route->sint_ack_clb_data = sint_ack_clb_data;
     sint_route->cpu = cpu;
     sint_route->sint = sint;
+    sint_route->refcount = 1;
 
     return sint_route;
 
@@ -154,8 +156,23 @@ err:
     return NULL;
 }
 
-void kvm_hv_sint_route_destroy(HvSintRoute *sint_route)
+void hyperv_sint_route_ref(HvSintRoute *sint_route)
 {
+    sint_route->refcount++;
+}
+
+void hyperv_sint_route_unref(HvSintRoute *sint_route)
+{
+    if (!sint_route) {
+        return;
+    }
+
+    assert(sint_route->refcount > 0);
+
+    if (--sint_route->refcount) {
+        return;
+    }
+
     kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state,
                                           &sint_route->sint_set_notifier,
                                           sint_route->gsi);
Signature · 1 line
2.9.4
#20 lore raw

PATCH v2 14/23 hyperv: qom-ify SynIC

In reply to Roman Kagan

Make Hyper-V SynIC a device which is attached as a child to X86CPU. For now it only makes SynIC visibile in the qom hierarchy, and maintains its internal fields in sync with the respecitve msrs of the parent cpu (the fields will be used in followup patches).

---
v1 -> v2:
- was patch 13 in v1
- drop unnecessary QOM properties (but keep the fields)
- move KVM_CAP_HYPERV_SYNIC setting and SynIC creation to hyperv_init_vcpu

 target/i386/hyperv.h  |   4 ++
 target/i386/hyperv.c  | 111 +++++++++++++++++++++++++++++++++++++++++++++++++-
 target/i386/kvm.c     |  14 ++++++-
 target/i386/machine.c |   9 ++++
 4 files changed, 134 insertions(+), 4 deletions(-)
target/i386/hyperv.h +4 -0
index af5fc05..20bbd7b 100644
@@ -34,4 +34,8 @@ int kvm_hv_sint_route_set_sint(HvSintRoute *sint_route);
 uint32_t hyperv_vp_index(X86CPU *cpu);
 X86CPU *hyperv_find_vcpu(uint32_t vp_index);
 
+void hyperv_synic_add(X86CPU *cpu);
+void hyperv_synic_reset(X86CPU *cpu);
+void hyperv_synic_update(X86CPU *cpu);
+
 #endif
target/i386/hyperv.c +109 -2

155 lines not on this page · raw message

target/i386/kvm.c +12 -2
index 9bf7f08..eaa2df3 100644
@@ -648,8 +648,7 @@ static int hyperv_handle_properties(CPUState *cs)
         env->features[FEAT_HYPERV_EAX] |= HV_VP_RUNTIME_AVAILABLE;
     }
     if (cpu->hyperv_synic) {
-        if (!has_msr_hv_synic ||
-            kvm_vcpu_enable_cap(cs, KVM_CAP_HYPERV_SYNIC, 0)) {
+        if (!has_msr_hv_synic) {
             fprintf(stderr, "Hyper-V SynIC is not supported by kernel\n");
             return -ENOSYS;
         }
@@ -700,6 +699,15 @@ static int hyperv_init_vcpu(X86CPU *cpu)
         }
     }
 
+    if (cpu->hyperv_synic) {
+        if (kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_HYPERV_SYNIC, 0)) {
+            fprintf(stderr, "failed to enable Hyper-V SynIC\n");
+            return -ENOSYS;
+        }
+
+        hyperv_synic_add(cpu);
+    }
+
     return 0;
 }
 
@@ -1084,6 +1092,8 @@ void kvm_arch_reset_vcpu(X86CPU *cpu)
         for (i = 0; i < ARRAY_SIZE(env->msr_hv_synic_sint); i++) {
             env->msr_hv_synic_sint[i] = HV_SINT_MASKED;
         }
+
+        hyperv_synic_reset(cpu);
     }
 }
 
target/i386/machine.c +9 -0
index ded5e34..90cc3a9 100644
@@ -7,6 +7,7 @@
 #include "hw/i386/pc.h"
 #include "hw/isa/isa.h"
 #include "migration/cpu.h"
+#include "hyperv.h"
 
 #include "sysemu/kvm.h"
 
@@ -629,11 +630,19 @@ static bool hyperv_synic_enable_needed(void *opaque)
     return false;
 }
 
+static int hyperv_synic_post_load(void *opaque, int version_id)
+{
+    X86CPU *cpu = opaque;
+    hyperv_synic_update(cpu);
+    return 0;
+}
+
 static const VMStateDescription vmstate_msr_hyperv_synic = {
     .name = "cpu/msr_hyperv_synic",
     .version_id = 1,
     .minimum_version_id = 1,
     .needed = hyperv_synic_enable_needed,
+    .post_load = hyperv_synic_post_load,
     .fields = (VMStateField[]) {
         VMSTATE_UINT64(env.msr_hv_synic_control, X86CPU),
         VMSTATE_UINT64(env.msr_hv_synic_evt_page, X86CPU),
Signature · 1 line
2.9.4
#21 lore raw

Re: [Qemu-devel] [PATCH v2 14/23] hyperv: qom-ify SynIC

Quoted 138 lines · On Wed, 21 Jun 2017 19:24:15 +0300 Roman Kagan <[email protected]> wrote:

Make Hyper-V SynIC a device which is attached as a child to X86CPU. For now it only makes SynIC visibile in the qom hierarchy, and maintains its internal fields in sync with the respecitve msrs of the parent cpu (the fields will be used in followup patches).

---
v1 -> v2:
- was patch 13 in v1
- drop unnecessary QOM properties (but keep the fields)
- move KVM_CAP_HYPERV_SYNIC setting and SynIC creation to hyperv_init_vcpu

 target/i386/hyperv.h  |   4 ++
 target/i386/hyperv.c  | 111 +++++++++++++++++++++++++++++++++++++++++++++++++-
 target/i386/kvm.c     |  14 ++++++-
 target/i386/machine.c |   9 ++++
 4 files changed, 134 insertions(+), 4 deletions(-)
target/i386/hyperv.h +4 -0
index af5fc05..20bbd7b 100644
@@ -34,4 +34,8 @@ int kvm_hv_sint_route_set_sint(HvSintRoute *sint_route);
 uint32_t hyperv_vp_index(X86CPU *cpu);
 X86CPU *hyperv_find_vcpu(uint32_t vp_index);
 
+void hyperv_synic_add(X86CPU *cpu);
+void hyperv_synic_reset(X86CPU *cpu);
+void hyperv_synic_update(X86CPU *cpu);
+
 #endif
target/i386/hyperv.c +52 -2
index 012c79d..eff612c 100644
@@ -13,12 +13,27 @@
 
 #include "qemu/osdep.h"
 #include "qemu/main-loop.h"
+#include "qapi/error.h"
+#include "hw/qdev-properties.h"
 #include "hyperv.h"
 #include "hyperv_proto.h"
 
+typedef struct SynICState {
+    DeviceState parent_obj;
+
+    X86CPU *cpu;
+
+    bool enabled;
+    hwaddr msg_page_addr;
+    hwaddr evt_page_addr;
+} SynICState;
+
+#define TYPE_SYNIC "hyperv-synic"
+#define SYNIC(obj) OBJECT_CHECK(SynICState, (obj), TYPE_SYNIC)
+
 struct HvSintRoute {
     uint32_t sint;
-    X86CPU *cpu;
+    SynICState *synic;
     int gsi;
     EventNotifier sint_set_notifier;
     EventNotifier sint_ack_notifier;
@@ -37,6 +52,37 @@ X86CPU *hyperv_find_vcpu(uint32_t vp_index)
     return X86_CPU(qemu_get_cpu(vp_index));
 }
 
+static SynICState *get_synic(X86CPU *cpu)
+{
+    SynICState *synic =
+        SYNIC(object_resolve_path_component(OBJECT(cpu), "synic"));
+    assert(synic);
+    return synic;
+}
+
+static void synic_update_msg_page_addr(SynICState *synic)
+{
+    uint64_t msr = synic->cpu->env.msr_hv_synic_msg_page;
+    hwaddr new_addr = (msr & HV_SIMP_ENABLE) ? (msr & TARGET_PAGE_MASK) : 0;
+
+    synic->msg_page_addr = new_addr;
+}
+
+static void synic_update_evt_page_addr(SynICState *synic)
+{
+    uint64_t msr = synic->cpu->env.msr_hv_synic_evt_page;
+    hwaddr new_addr = (msr & HV_SIEFP_ENABLE) ? (msr & TARGET_PAGE_MASK) : 0;
+
+    synic->evt_page_addr = new_addr;
+}
+
+static void synic_update(SynICState *synic)
+{
+    synic->enabled = synic->cpu->env.msr_hv_synic_control & HV_SYNIC_ENABLE;
+    synic_update_msg_page_addr(synic);
+    synic_update_evt_page_addr(synic);
+}
+
 int kvm_hv_handle_exit(X86CPU *cpu, struct kvm_hyperv_exit *exit)
 {
     CPUX86State *env = &cpu->env;
@@ -65,6 +111,7 @@ int kvm_hv_handle_exit(X86CPU *cpu, struct kvm_hyperv_exit *exit)
         default:
             return -1;
         }
+        synic_update(get_synic(cpu));
         return 0;
     case KVM_EXIT_HYPERV_HCALL: {
         uint16_t code;
@@ -95,6 +142,7 @@ HvSintRoute *hyperv_sint_route_new(uint32_t vp_index, uint32_t sint,
                                    HvSintAckClb sint_ack_clb,
                                    void *sint_ack_clb_data)
 {
+    SynICState *synic;
     HvSintRoute *sint_route;
     EventNotifier *ack_notifier;
     int r, gsi;
@@ -105,6 +153,8 @@ HvSintRoute *hyperv_sint_route_new(uint32_t vp_index, uint32_t sint,
         return NULL;
     }
 
+    synic = get_synic(cpu);
+
     sint_route = g_new0(HvSintRoute, 1);
     r = event_notifier_init(&sint_route->sint_set_notifier, false);
     if (r) {
@@ -135,7 +185,7 @@ HvSintRoute *hyperv_sint_route_new(uint32_t vp_index, uint32_t sint,
     sint_route->gsi = gsi;
     sint_route->sint_ack_clb = sint_ack_clb;
     sint_route->sint_ack_clb_data = sint_ack_clb_data;
-    sint_route->cpu = cpu;
+    sint_route->synic = synic;
     sint_route->sint = sint;
     sint_route->refcount = 1;
 

target/i386/hyperv.c int kvm_hv_sint_route_set_sint(HvSintRoute *sint_route) · line 248

Quoted 10 lines
target/i386/hyperv.c +7 -0
@@ -189,3 +239,60 @@ int kvm_hv_sint_route_set_sint(HvSintRoute *sint_route)
 {
     return event_notifier_set(&sint_route->sint_set_notifier);
 }
+
+static void synic_realize(DeviceState *dev, Error **errp)
+{
+    Object *obj = OBJECT(dev);
+    SynICState *synic = SYNIC(dev);
+
+    synic->cpu = X86_CPU(obj->parent);

usually devices shouldn't access parent this way

[...]

target/i386/hyperv.c int kvm_hv_sint_route_set_sint(HvSintRoute *sint_route) · line 266

Quoted 1 line
target/i386/hyperv.c +1 -0
+void hyperv_synic_add(X86CPU *cpu)

this helper is called by/from parent so something like below should do

target/i386/hyperv.c int kvm_hv_sint_route_set_sint(HvSintRoute *sint_route) · line 272

Quoted 6 lines
target/i386/hyperv.c +6 -0
+{
+    Object *obj;
+
+    obj = object_new(TYPE_SYNIC);
+    object_property_add_child(OBJECT(cpu), "synic", obj, &error_abort);
+    object_unref(obj);

SynICState *synic = SYNIC(obj)
synic->cpu = cpu;
or even make synic->cpu a link (object_property_add_link) and set it here, benefit will be when synic is introspected via QOM it will show that it refers back/uses the cpu + proper reference counting of CPU object.

Quoted 41 lines
target/i386/hyperv.c +26 -0
+    object_property_set_bool(obj, true, "realized", &error_abort);
+}
+
+void hyperv_synic_reset(X86CPU *cpu)
+{
+    device_reset(DEVICE(get_synic(cpu)));
+}
+
+void hyperv_synic_update(X86CPU *cpu)
+{
+    synic_update(get_synic(cpu));
+}
+
+static const TypeInfo synic_type_info = {
+    .name = TYPE_SYNIC,
+    .parent = TYPE_DEVICE,
+    .instance_size = sizeof(SynICState),
+    .class_init = synic_class_init,
+};
+
+static void synic_register_types(void)
+{
+    type_register_static(&synic_type_info);
+}
+
+type_init(synic_register_types)
target/i386/kvm.c +1 -2
index 9bf7f08..eaa2df3 100644
@@ -648,8 +648,7 @@ static int hyperv_handle_properties(CPUState *cs)
         env->features[FEAT_HYPERV_EAX] |= HV_VP_RUNTIME_AVAILABLE;
     }
     if (cpu->hyperv_synic) {
-        if (!has_msr_hv_synic ||
-            kvm_vcpu_enable_cap(cs, KVM_CAP_HYPERV_SYNIC, 0)) {
+        if (!has_msr_hv_synic) {
             fprintf(stderr, "Hyper-V SynIC is not supported by kernel\n");
             return -ENOSYS;
         }

target/i386/kvm.c static int hyperv_init_vcpu(X86CPU *cpu) · line 708

Quoted 10 lines
target/i386/kvm.c +7 -0
@@ -700,6 +699,15 @@ static int hyperv_init_vcpu(X86CPU *cpu)
         }
     }
 
+    if (cpu->hyperv_synic) {
+        if (kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_HYPERV_SYNIC, 0)) {
+            fprintf(stderr, "failed to enable Hyper-V SynIC\n");
+            return -ENOSYS;
+        }
+
+        hyperv_synic_add(cpu);

is synic KVM specific or may it work with TCG accel?
in either case, looks like hyperv_synic_add() should be called from x86_cpu_realizefn(), the same like we do with APIC creating it depending feature being enabled.

target/i386/kvm.c void kvm_arch_reset_vcpu(X86CPU *cpu) · line 1096

Quoted 11 lines
target/i386/kvm.c +4 -0
+    }
+
     return 0;
 }
 
@@ -1084,6 +1092,8 @@ void kvm_arch_reset_vcpu(X86CPU *cpu)
         for (i = 0; i < ARRAY_SIZE(env->msr_hv_synic_sint); i++) {
             env->msr_hv_synic_sint[i] = HV_SINT_MASKED;
         }
+
+        hyperv_synic_reset(cpu);

ditto, could go to x86_cpu_machine_reset_cb()
also calling it unconditionally will crash QEMU if get_synic() returns NULL (i.e. when feature is not enabled).

Quoted 35 lines
target/i386/kvm.c +0 -0
     }
 }
 
target/i386/machine.c +9 -0
index ded5e34..90cc3a9 100644
@@ -7,6 +7,7 @@
 #include "hw/i386/pc.h"
 #include "hw/isa/isa.h"
 #include "migration/cpu.h"
+#include "hyperv.h"
 
 #include "sysemu/kvm.h"
 
@@ -629,11 +630,19 @@ static bool hyperv_synic_enable_needed(void *opaque)
     return false;
 }
 
+static int hyperv_synic_post_load(void *opaque, int version_id)
+{
+    X86CPU *cpu = opaque;
+    hyperv_synic_update(cpu);
+    return 0;
+}
+
 static const VMStateDescription vmstate_msr_hyperv_synic = {
     .name = "cpu/msr_hyperv_synic",
     .version_id = 1,
     .minimum_version_id = 1,
     .needed = hyperv_synic_enable_needed,
+    .post_load = hyperv_synic_post_load,
     .fields = (VMStateField[]) {
         VMSTATE_UINT64(env.msr_hv_synic_control, X86CPU),
         VMSTATE_UINT64(env.msr_hv_synic_evt_page, X86CPU),
#22 lore raw

Re: [Qemu-devel] [PATCH v2 14/23] hyperv: qom-ify SynIC

Quoted 14 lines · Igor Mammedov

target/i386/hyperv.c int kvm_hv_sint_route_set_sint(HvSintRoute *sint_route) · line 248

Quoted 6 lines · On Wed, 21 Jun 2017 19:24:15 +0300 Roman Kagan <[email protected]> wrote:
target/i386/hyperv.c +6 -0
+static void synic_realize(DeviceState *dev, Error **errp)
+{
+    Object *obj = OBJECT(dev);
+    SynICState *synic = SYNIC(dev);
+
+    synic->cpu = X86_CPU(obj->parent);

usually devices shouldn't access parent this way

[...]

Quoted 1 line

+void hyperv_synic_add(X86CPU *cpu)

this helper is called by/from parent so something like below should do

Quoted 9 lines

target/i386/hyperv.c int kvm_hv_sint_route_set_sint(HvSintRoute *sint_route) · line 272

Quoted 6 lines
target/i386/hyperv.c +6 -0
+{
+    Object *obj;
+
+    obj = object_new(TYPE_SYNIC);
+    object_property_add_child(OBJECT(cpu), "synic", obj, &error_abort);
+    object_unref(obj);

SynICState *synic = SYNIC(obj)
synic->cpu = cpu;

Indeed, this is better.

Quoted 3 lines

or even make synic->cpu a link (object_property_add_link) and set it here, benefit will be when synic is introspected via QOM it will show that it refers back/uses the cpu + proper reference counting of CPU object.

Good point, will look into it, thanks.

Quoted 8 lines

target/i386/kvm.c static int hyperv_init_vcpu(X86CPU *cpu) · line 708

Quoted 7 lines
target/i386/kvm.c +7 -0
+    if (cpu->hyperv_synic) {
+        if (kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_HYPERV_SYNIC, 0)) {
+            fprintf(stderr, "failed to enable Hyper-V SynIC\n");
+            return -ENOSYS;
+        }
+
+        hyperv_synic_add(cpu);

is synic KVM specific or may it work with TCG accel?

No, it's exclusively KVM. Actually most of it sits in the kernel.

Quoted 3 lines

in either case, looks like hyperv_synic_add() should be called from x86_cpu_realizefn(), the same like we do with APIC creating it depending feature being enabled.

I'm not sure I understand the reason, in view of it being exclusively KVM.

Quoted 5 lines
Quoted 5 lines
target/i386/kvm.c +2 -0
+    }
+
     return 0;
 }
Quoted 9 lines

target/i386/kvm.c void kvm_arch_reset_vcpu(X86CPU *cpu) · line 1096

Quoted 6 lines
target/i386/kvm.c +2 -0
@@ -1084,6 +1092,8 @@ void kvm_arch_reset_vcpu(X86CPU *cpu)
         for (i = 0; i < ARRAY_SIZE(env->msr_hv_synic_sint); i++) {
             env->msr_hv_synic_sint[i] = HV_SINT_MASKED;
         }
+
+        hyperv_synic_reset(cpu);

ditto, could go to x86_cpu_machine_reset_cb()
also calling it unconditionally will crash QEMU if get_synic() returns NULL (i.e. when feature is not enabled).

The context is not visibile in the patch, but this is actually called inside

    if (cpu->hyperv_synic) {
    ...
    }

so no, it won't return NULL.

Thanks,
Roman.

#23 lore raw

Re: [Qemu-devel] [PATCH v2 14/23] hyperv: qom-ify SynIC

Quoted 3 lines · On Thu, 29 Jun 2017 20:51:01 +0300 Roman Kagan <[email protected]> wrote:
Quoted 2 lines · Igor Mammedov

On Wed, 21 Jun 2017 19:24:15 +0300
Roman Kagan <[email protected]> wrote:

[...]

Quoted 10 lines
Quoted 9 lines

target/i386/kvm.c static int hyperv_init_vcpu(X86CPU *cpu) · line 708

Quoted 8 lines
target/i386/kvm.c +7 -0
+    if (cpu->hyperv_synic) {
+        if (kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_HYPERV_SYNIC, 0)) {
+            fprintf(stderr, "failed to enable Hyper-V SynIC\n");
+            return -ENOSYS;
+        }
+
+        hyperv_synic_add(cpu);  

is synic KVM specific or may it work with TCG accel?

Quoted 8 lines

No, it's exclusively KVM. Actually most of it sits in the kernel.

Quoted 3 lines

in either case, looks like hyperv_synic_add() should be called from x86_cpu_realizefn(), the same like we do with APIC creating it depending feature being enabled.

I'm not sure I understand the reason, in view of it being exclusively KVM.

couldn't synic be emulated (if missing QEMU part is written) in TCG mode or it relies on specific KVM features?

[...]

#24 lore raw

Re: [Qemu-devel] [PATCH v2 14/23] hyperv: qom-ify SynIC

Quoted 19 lines · Igor Mammedov
Quoted 3 lines · On Thu, 29 Jun 2017 20:51:01 +0300 Roman Kagan <[email protected]> wrote:
Quoted 2 lines · Igor Mammedov

On Wed, 21 Jun 2017 19:24:15 +0300
Roman Kagan <[email protected]> wrote:

[...]

Quoted 12 lines
Quoted 9 lines

target/i386/kvm.c static int hyperv_init_vcpu(X86CPU *cpu) · line 708

Quoted 8 lines
target/i386/kvm.c +7 -0
+    if (cpu->hyperv_synic) {
+        if (kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_HYPERV_SYNIC, 0)) {
+            fprintf(stderr, "failed to enable Hyper-V SynIC\n");
+            return -ENOSYS;
+        }
+
+        hyperv_synic_add(cpu);  

is synic KVM specific or may it work with TCG accel?

No, it's exclusively KVM. Actually most of it sits in the kernel.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

in either case, looks like hyperv_synic_add() should be called from x86_cpu_realizefn(), the same like we do with APIC creating it depending feature being enabled.

I'm not sure I understand the reason, in view of it being exclusively KVM.

couldn't synic be emulated (if missing QEMU part is written) in TCG mode or it relies on specific KVM features?

At the moment it depends on KVM and, in particular, relies on lapic being in KVM. I guess with certain effort it can be done in TCG too, but we didn't even consider this so far, and have no plans for it.

Roman.

#25 lore raw

Re: [Qemu-devel] [PATCH v2 14/23] hyperv: qom-ify SynIC

Quoted 24 lines · On Fri, 7 Jul 2017 15:47:19 +0300 Roman Kagan <[email protected]> wrote:
Quoted 23 lines · Igor Mammedov
Quoted 3 lines · On Thu, 29 Jun 2017 20:51:01 +0300 Roman Kagan <[email protected]> wrote:
Quoted 2 lines · Igor Mammedov

On Wed, 21 Jun 2017 19:24:15 +0300
Roman Kagan <[email protected]> wrote:

[...]

Quoted 16 lines
Quoted 9 lines

target/i386/kvm.c static int hyperv_init_vcpu(X86CPU *cpu) · line 708

Quoted 8 lines
target/i386/kvm.c +7 -0
+    if (cpu->hyperv_synic) {
+        if (kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_HYPERV_SYNIC, 0)) {
+            fprintf(stderr, "failed to enable Hyper-V SynIC\n");
+            return -ENOSYS;
+        }
+
+        hyperv_synic_add(cpu);    

is synic KVM specific or may it work with TCG accel?

No, it's exclusively KVM. Actually most of it sits in the kernel.

Quoted 3 lines

in either case, looks like hyperv_synic_add() should be called from x86_cpu_realizefn(), the same like we do with APIC creating it depending feature being enabled.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

I'm not sure I understand the reason, in view of it being exclusively KVM.

couldn't synic be emulated (if missing QEMU part is written) in TCG mode or it relies on specific KVM features?

At the moment it depends on KVM and, in particular, relies on lapic being in KVM. I guess with certain effort it can be done in TCG too, but we didn't even consider this so far, and have no plans for it.

we would avoid rewriting synic code if you can keep synic parts that could be reused by TCG outside of kvm specific files.

Refactoring to qom/device and moving that code in separate file
is a good time to start doing it and keeping kvm related parts isolated/minimal should help in future when TCG synic model could be written.
(but I don't insist on it).

#26 lore raw

PATCH v2 15/23 hyperv: block SynIC use in QEMU in incompatible configurations

In reply to Roman Kagan

Certain configurations do not allow SynIC to be used in QEMU. In particular,

- when hyperv_vpindex is off, SINT routes can't be used as they refer to the destination vCPU by vp_index

- older KVM (which doesn't expose KVM_CAP_HYPERV_SYNIC2) zeroes out SynIC message and event pages on every msr load, breaking migration

OTOH in-KVM users of SynIC -- SynIC timers -- do work in those configurations, and we shouldn't stop the guest from using them.

Instead, introduce a SynIC property that disallows to use the SynIC within QEMU but not in KVM. The property is set during vCPU init and via compat logic (as older QEMU had no users for SynIC beyond the test device).

Also a function is added that allows the devices to query the status of SynIC support across vCPUs.

---
v1 -> v2:
- new patch

 include/hw/i386/pc.h |  5 +++++
 target/i386/hyperv.h |  4 +++-
 target/i386/hyperv.c | 39 ++++++++++++++++++++++++++++++++++++++-
 target/i386/kvm.c    | 12 ++++++++++--
 4 files changed, 56 insertions(+), 4 deletions(-)
include/hw/i386/pc.h +5 -0
index 233216a..72b5c62 100644
@@ -389,6 +389,11 @@ bool e820_get_entry(int, uint32_t, uint64_t *, uint64_t *);
         .property = "extended-tseg-mbytes",\
         .value    = stringify(0),\
     },\
+    {\
+        .driver   = "hyperv-synic",\
+        .property = "in-kvm-only",\
+        .value    = "on",\
+    },\
 
 #define PC_COMPAT_2_8 \
     HW_COMPAT_2_8 \
target/i386/hyperv.h +3 -1
index 20bbd7b..7d8753e 100644
@@ -34,8 +34,10 @@ int kvm_hv_sint_route_set_sint(HvSintRoute *sint_route);
 uint32_t hyperv_vp_index(X86CPU *cpu);
 X86CPU *hyperv_find_vcpu(uint32_t vp_index);
 
-void hyperv_synic_add(X86CPU *cpu);
+void hyperv_synic_add(X86CPU *cpu, bool in_kvm_only);
 void hyperv_synic_reset(X86CPU *cpu);
 void hyperv_synic_update(X86CPU *cpu);
 
+bool hyperv_synic_usable(void);
+
 #endif
target/i386/hyperv.c +38 -1
index eff612c..e183638 100644
@@ -23,6 +23,8 @@ typedef struct SynICState {
 
     X86CPU *cpu;
 
+    bool in_kvm_only;
+
     bool enabled;
     hwaddr msg_page_addr;
     hwaddr evt_page_addr;
@@ -78,6 +80,10 @@ static void synic_update_evt_page_addr(SynICState *synic)
 
 static void synic_update(SynICState *synic)
 {
+    if (synic->in_kvm_only) {
+        return;
+    }
+
     synic->enabled = synic->cpu->env.msr_hv_synic_control & HV_SYNIC_ENABLE;
     synic_update_msg_page_addr(synic);
     synic_update_evt_page_addr(synic);
@@ -154,6 +160,7 @@ HvSintRoute *hyperv_sint_route_new(uint32_t vp_index, uint32_t sint,
     }
 
     synic = get_synic(cpu);
+    assert(!synic->in_kvm_only);
 
     sint_route = g_new0(HvSintRoute, 1);
     r = event_notifier_init(&sint_route->sint_set_notifier, false);
@@ -240,6 +247,11 @@ int kvm_hv_sint_route_set_sint(HvSintRoute *sint_route)
     return event_notifier_set(&sint_route->sint_set_notifier);
 }
 
+static Property synic_props[] = {
+    DEFINE_PROP_BOOL("in-kvm-only", SynICState, in_kvm_only, false),
+    DEFINE_PROP_END_OF_LIST(),
+};
+
 static void synic_realize(DeviceState *dev, Error **errp)
 {
     Object *obj = OBJECT(dev);
@@ -258,18 +270,24 @@ static void synic_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(klass);
 
+    dc->props = synic_props;
     dc->realize = synic_realize;
     dc->reset = synic_reset;
     dc->user_creatable = false;
 }
 
-void hyperv_synic_add(X86CPU *cpu)
+void hyperv_synic_add(X86CPU *cpu, bool in_kvm_only)
 {
     Object *obj;
+    SynICState *synic;
 
     obj = object_new(TYPE_SYNIC);
     object_property_add_child(OBJECT(cpu), "synic", obj, &error_abort);
     object_unref(obj);
+
+    synic = SYNIC(obj);
+    synic->in_kvm_only = synic->in_kvm_only || in_kvm_only;
+
     object_property_set_bool(obj, true, "realized", &error_abort);
 }
 
@@ -283,6 +301,25 @@ void hyperv_synic_update(X86CPU *cpu)
     synic_update(get_synic(cpu));
 }
 
+bool hyperv_synic_usable(void)
+{
+    CPUState *cs;
+
+    CPU_FOREACH(cs) {
+        X86CPU *cpu = X86_CPU(cs);
+
+        if (!cpu->hyperv_synic) {
+            return false;
+        }
+
+        if (get_synic(cpu)->in_kvm_only) {
+            return false;
+        }
+    }
+
+    return true;
+}
+
 static const TypeInfo synic_type_info = {
     .name = TYPE_SYNIC,
     .parent = TYPE_DEVICE,
target/i386/kvm.c +10 -2
index eaa2df3..8d1d232 100644
@@ -700,12 +700,20 @@ static int hyperv_init_vcpu(X86CPU *cpu)
     }
 
     if (cpu->hyperv_synic) {
-        if (kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_HYPERV_SYNIC, 0)) {
+        bool in_kvm_only = !cpu->hyperv_vpindex;
+
+        if (!in_kvm_only &&
+            kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_HYPERV_SYNIC2, 0)) {
+            in_kvm_only = true;
+        }
+
+        if (in_kvm_only &&
+            kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_HYPERV_SYNIC, 0)) {
             fprintf(stderr, "failed to enable Hyper-V SynIC\n");
             return -ENOSYS;
         }
 
-        hyperv_synic_add(cpu);
+        hyperv_synic_add(cpu, in_kvm_only);
     }
 
     return 0;
Signature · 1 line
2.9.4
#27 lore raw

PATCH v2 16/23 hyperv: make overlay pages for SynIC

In reply to Roman Kagan

Per Hyper-V spec, SynIC message and event flag pages are to be implemented as so called overlay pages. That is, they are owned by the hypervisor and, when mapped into the guest physical address space, overlay the guest physical pages such that

1) the overlaid guest page becomes invisible to the guest CPUs until the overlay page is turned off
2) the contents of the overlay page is preserved when it's turned off and back on, even at a different address; it's only zeroed at vcpu reset

This particular nature of SynIC message and event flag pages is ignored in the current code, and guest physical pages are used directly instead. This (mostly) works because the actual guests seem not to depend on the features listed above.

This patch implements those pages as the spec mandates.

Since the extra RAM regions, which introduce migration incompatibility, are only added when in_kvm_only == false, no extra compat logic is necessary.

---
v1 -> v2:
- was patch 15 in v1
- add comment on using async_safe_run_on_cpu

 target/i386/hyperv.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 66 insertions(+), 6 deletions(-)
target/i386/hyperv.c +66 -6
index e183638..dca85de 100644
@@ -15,6 +15,9 @@
 #include "qemu/main-loop.h"
 #include "qapi/error.h"
 #include "hw/qdev-properties.h"
+#include "exec/address-spaces.h"
+#include "sysemu/cpus.h"
+#include "migration/vmstate.h"
 #include "hyperv.h"
 #include "hyperv_proto.h"
 
@@ -28,6 +31,10 @@ typedef struct SynICState {
     bool enabled;
     hwaddr msg_page_addr;
     hwaddr evt_page_addr;
+    MemoryRegion msg_page_mr;
+    MemoryRegion evt_page_mr;
+    struct hyperv_message_page *msg_page;
+    struct hyperv_event_flags_page *evt_page;
 } SynICState;
 
 #define TYPE_SYNIC "hyperv-synic"
@@ -67,6 +74,17 @@ static void synic_update_msg_page_addr(SynICState *synic)
     uint64_t msr = synic->cpu->env.msr_hv_synic_msg_page;
     hwaddr new_addr = (msr & HV_SIMP_ENABLE) ? (msr & TARGET_PAGE_MASK) : 0;
 
+    if (new_addr == synic->msg_page_addr) {
+        return;
+    }
+
+    if (synic->msg_page_addr) {
+        memory_region_del_subregion(get_system_memory(), &synic->msg_page_mr);
+    }
+    if (new_addr) {
+        memory_region_add_subregion(get_system_memory(), new_addr,
+                                    &synic->msg_page_mr);
+    }
     synic->msg_page_addr = new_addr;
 }
 
@@ -75,6 +93,17 @@ static void synic_update_evt_page_addr(SynICState *synic)
     uint64_t msr = synic->cpu->env.msr_hv_synic_evt_page;
     hwaddr new_addr = (msr & HV_SIEFP_ENABLE) ? (msr & TARGET_PAGE_MASK) : 0;
 
+    if (new_addr == synic->evt_page_addr) {
+        return;
+    }
+
+    if (synic->evt_page_addr) {
+        memory_region_del_subregion(get_system_memory(), &synic->evt_page_mr);
+    }
+    if (new_addr) {
+        memory_region_add_subregion(get_system_memory(), new_addr,
+                                    &synic->evt_page_mr);
+    }
     synic->evt_page_addr = new_addr;
 }
 
@@ -89,6 +118,15 @@ static void synic_update(SynICState *synic)
     synic_update_evt_page_addr(synic);
 }
 
+
+static void async_synic_update(CPUState *cs, run_on_cpu_data data)
+{
+    SynICState *synic = data.host_ptr;
+    qemu_mutex_lock_iothread();
+    synic_update(synic);
+    qemu_mutex_unlock_iothread();
+}
+
 int kvm_hv_handle_exit(X86CPU *cpu, struct kvm_hyperv_exit *exit)
 {
     CPUX86State *env = &cpu->env;
@@ -99,11 +137,6 @@ int kvm_hv_handle_exit(X86CPU *cpu, struct kvm_hyperv_exit *exit)
             return -1;
         }
 
-        /*
-         * For now just track changes in SynIC control and msg/evt pages msr's.
-         * When SynIC messaging/events processing will be added in future
-         * here we will do messages queues flushing and pages remapping.
-         */
         switch (exit->u.synic.msr) {
         case HV_X64_MSR_SCONTROL:
             env->msr_hv_synic_control = exit->u.synic.control;
@@ -117,7 +150,13 @@ int kvm_hv_handle_exit(X86CPU *cpu, struct kvm_hyperv_exit *exit)
         default:
             return -1;
         }
-        synic_update(get_synic(cpu));
+        /*
+         * this will run in this cpu thread before it returns to KVM, but in a
+         * safe environment (i.e. when all cpus are quiescent) -- this is
+         * necessary because we're changing memory hierarchy
+         */
+        async_safe_run_on_cpu(CPU(cpu), async_synic_update,
+                              RUN_ON_CPU_HOST_PTR(get_synic(cpu)));
         return 0;
     case KVM_EXIT_HYPERV_HCALL: {
         uint16_t code;
@@ -256,13 +295,34 @@ static void synic_realize(DeviceState *dev, Error **errp)
 {
     Object *obj = OBJECT(dev);
     SynICState *synic = SYNIC(dev);
+    char *msgp_name, *evtp_name;
+    uint32_t vp_index;
 
     synic->cpu = X86_CPU(obj->parent);
+
+    /* memory region names have to be globally unique */
+    vp_index = hyperv_vp_index(synic->cpu);
+    msgp_name = g_strdup_printf("synic-%u-msg-page", vp_index);
+    evtp_name = g_strdup_printf("synic-%u-evt-page", vp_index);
+
+    memory_region_init_ram(&synic->msg_page_mr, obj, msgp_name,
+                           sizeof(*synic->msg_page), &error_abort);
+    memory_region_init_ram(&synic->evt_page_mr, obj, evtp_name,
+                           sizeof(*synic->evt_page), &error_abort);
+    vmstate_register_ram(&synic->msg_page_mr, dev);
+    vmstate_register_ram(&synic->evt_page_mr, dev);
+    synic->msg_page = memory_region_get_ram_ptr(&synic->msg_page_mr);
+    synic->evt_page = memory_region_get_ram_ptr(&synic->evt_page_mr);
+
+    g_free(msgp_name);
+    g_free(evtp_name);
 }
 
 static void synic_reset(DeviceState *dev)
 {
     SynICState *synic = SYNIC(dev);
+    memset(synic->msg_page, 0, sizeof(*synic->msg_page));
+    memset(synic->evt_page, 0, sizeof(*synic->evt_page));
     synic_update(synic);
 }
 
Signature · 1 line
2.9.4
#28 lore raw

PATCH v2 18/23 hyperv: add synic event flag signaling

In reply to Roman Kagan

Add infrastructure to signal SynIC event flags by atomically setting the corresponding bit in the event flags page and firing a SINT if necessary.

---
v1 -> v2:
- swapped kvm_hv_sint_route_set_sint and memory_region_set_dirty

 target/i386/hyperv.h |  2 ++
 target/i386/hyperv.c | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+)
target/i386/hyperv.h +2 -0
index cc08ebb..f171f7b 100644
@@ -41,4 +41,6 @@ bool hyperv_synic_usable(void);
 
 int hyperv_post_msg(HvSintRoute *sint_route, struct hyperv_message *msg);
 
+int hyperv_set_evt_flag(HvSintRoute *sint_route, unsigned evtno);
+
 #endif
target/i386/hyperv.c +32 -0
index 768dc2b..a96b03b 100644
@@ -17,6 +17,7 @@
 #include "hw/qdev-properties.h"
 #include "exec/address-spaces.h"
 #include "sysemu/cpus.h"
+#include "qemu/bitops.h"
 #include "migration/vmstate.h"
 #include "hyperv.h"
 #include "hyperv_proto.h"
@@ -203,6 +204,37 @@ int hyperv_post_msg(HvSintRoute *sint_route, struct hyperv_message *src_msg)
     return 0;
 }
 
+/*
+ * Set given event flag for a given sint on a given vcpu, and signal the sint.
+ */
+int hyperv_set_evt_flag(HvSintRoute *sint_route, unsigned evtno)
+{
+    int ret;
+    SynICState *synic = sint_route->synic;
+    unsigned long *flags, set_mask;
+    unsigned set_idx;
+
+    if (evtno > HV_EVENT_FLAGS_COUNT) {
+        return -EINVAL;
+    }
+    if (!synic->enabled || !synic->evt_page_addr) {
+        return -ENXIO;
+    }
+
+    set_idx = BIT_WORD(evtno);
+    set_mask = BIT_MASK(evtno);
+    flags = synic->evt_page->slot[sint_route->sint].flags;
+
+    if ((atomic_fetch_or(&flags[set_idx], set_mask) & set_mask) != set_mask) {
+        memory_region_set_dirty(&synic->evt_page_mr, 0,
+                                sizeof(*synic->evt_page));
+        ret = kvm_hv_sint_route_set_sint(sint_route);
+    } else {
+        ret = 0;
+    }
+    return ret;
+}
+
 static void async_synic_update(CPUState *cs, run_on_cpu_data data)
 {
     SynICState *synic = data.host_ptr;
Signature · 1 line
2.9.4
#29 lore raw

PATCH v2 19/23 hyperv: process SIGNAL_EVENT hypercall

In reply to Roman Kagan

Add handling of SIGNAL_EVENT hypercall. For that, provide an interface to associate an EventNotifier with an event connection number, so that it's signaled when the SIGNAL_EVENT hypercall with the matching parameters is called by the guest.

TODO: we should be able to move this to KVM and avoid expensive user exit just to look up an eventfd by connection number and signal it.

---
v1 -> v2:
- use single mutex for evt and msg handler lists

 target/i386/hyperv.h |   2 +
 target/i386/hyperv.c | 113 +++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 111 insertions(+), 4 deletions(-)
target/i386/hyperv.h +2 -0
index f171f7b..46c6836 100644
@@ -43,4 +43,6 @@ int hyperv_post_msg(HvSintRoute *sint_route, struct hyperv_message *msg);
 
 int hyperv_set_evt_flag(HvSintRoute *sint_route, unsigned evtno);
 
+int hyperv_set_evt_notifier(uint32_t conn_id, EventNotifier *notifier);
+
 #endif
target/i386/hyperv.c +109 -4

137 lines not on this page · raw message

Signature · 1 line
2.9.4
#30 lore raw

PATCH v2 20/23 hyperv: process POST_MESSAGE hypercall

In reply to Roman Kagan

Add handling of POST_MESSAGE hypercall. For that, add an interface to regsiter a handler for the messages arrived from the guest on a particular connection id (IOW set up a message connection in Hyper-V speak).

---
v1 -> v2:
- use single mutex for evt and msg handler lists

 target/i386/hyperv.h |  5 +++
 target/i386/hyperv.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 92 insertions(+)
target/i386/hyperv.h +5 -0
index 46c6836..f101144 100644
@@ -43,6 +43,11 @@ int hyperv_post_msg(HvSintRoute *sint_route, struct hyperv_message *msg);
 
 int hyperv_set_evt_flag(HvSintRoute *sint_route, unsigned evtno);
 
+struct hyperv_post_message_input;
+typedef uint64_t (*HvMsgHandler)(const struct hyperv_post_message_input *msg,
+                                 void *data);
+int hyperv_set_msg_handler(uint32_t conn_id, HvMsgHandler handler, void *data);
+
 int hyperv_set_evt_notifier(uint32_t conn_id, EventNotifier *notifier);
 
 #endif
target/i386/hyperv.c +87 -0

120 lines not on this page · raw message

Signature · 1 line
2.9.4
#31 lore raw

PATCH v2 21/23 hyperv_testdev: add SynIC message and event testmodes

In reply to Roman Kagan

Add testmodes for SynIC messages and events. The message or event connection setup / teardown is initiated by the guest via new control codes written to the test device port. Then the test connections bounce the respective operations back to the guest, i.e. the incoming messages are posted or the incoming events are signaled on the configured vCPUs.

---
 hw/misc/hyperv_testdev.c | 162 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 161 insertions(+), 1 deletion(-)
hw/misc/hyperv_testdev.c +161 -1
index fa435ab..4fc7787 100644
@@ -13,12 +13,15 @@
 
 #include "qemu/osdep.h"
 #include "qemu/queue.h"
+#include "qemu/event_notifier.h"
+#include "qemu/main-loop.h"
 #include <linux/kvm.h>
 #include "hw/hw.h"
 #include "hw/qdev.h"
 #include "hw/isa/isa.h"
 #include "sysemu/kvm.h"
 #include "target/i386/hyperv.h"
+#include "target/i386/hyperv_proto.h"
 #include "kvm_i386.h"
 
 typedef struct TestSintRoute {
@@ -28,10 +31,26 @@ typedef struct TestSintRoute {
     HvSintRoute *sint_route;
 } TestSintRoute;
 
+typedef struct TestMsgConn {
+    QLIST_ENTRY(TestMsgConn) le;
+    uint8_t conn_id;
+    HvSintRoute *sint_route;
+    struct hyperv_message msg;
+} TestMsgConn;
+
+typedef struct TestEvtConn {
+    QLIST_ENTRY(TestEvtConn) le;
+    uint8_t conn_id;
+    HvSintRoute *sint_route;
+    EventNotifier notifier;
+} TestEvtConn;
+
 struct HypervTestDev {
     ISADevice parent_obj;
     MemoryRegion sint_control;
     QLIST_HEAD(, TestSintRoute) sint_routes;
+    QLIST_HEAD(, TestMsgConn) msg_conns;
+    QLIST_HEAD(, TestEvtConn) evt_conns;
 };
 typedef struct HypervTestDev HypervTestDev;
 
@@ -42,7 +61,11 @@ typedef struct HypervTestDev HypervTestDev;
 enum {
     HV_TEST_DEV_SINT_ROUTE_CREATE = 1,
     HV_TEST_DEV_SINT_ROUTE_DESTROY,
-    HV_TEST_DEV_SINT_ROUTE_SET_SINT
+    HV_TEST_DEV_SINT_ROUTE_SET_SINT,
+    HV_TEST_DEV_MSG_CONN_CREATE,
+    HV_TEST_DEV_MSG_CONN_DESTROY,
+    HV_TEST_DEV_EVT_CONN_CREATE,
+    HV_TEST_DEV_EVT_CONN_DESTROY,
 };
 
 static void sint_route_create(HypervTestDev *dev, uint8_t vpidx, uint8_t sint)
@@ -95,6 +118,128 @@ static void sint_route_set_sint(HypervTestDev *dev,
     kvm_hv_sint_route_set_sint(sint_route->sint_route);
 }
 
+static void msg_cb(void *data, int status)
+{
+    TestMsgConn *conn = data;
+
+    assert(status == 0 || status == -EAGAIN);
+
+    if (!status) {
+        return;
+    }
+
+    /* no concurrent posting is expected so this should succeed */
+    assert(!hyperv_post_msg(conn->sint_route, &conn->msg));
+}
+
+static uint64_t msg_handler(const struct hyperv_post_message_input *msg,
+                            void *data)
+{
+    int ret;
+    TestMsgConn *conn = data;
+
+    /* post the same message we've got */
+    conn->msg.header.message_type = msg->message_type;
+    assert(msg->payload_size < sizeof(conn->msg.payload));
+    conn->msg.header.payload_size = msg->payload_size;
+    memcpy(&conn->msg.payload, msg->payload, msg->payload_size);
+
+    ret = hyperv_post_msg(conn->sint_route, &conn->msg);
+
+    switch (ret) {
+    case 0:
+        return HV_STATUS_SUCCESS;
+    case -EAGAIN:
+        return HV_STATUS_INSUFFICIENT_BUFFERS;
+    default:
+        return HV_STATUS_INVALID_HYPERCALL_INPUT;
+    }
+}
+
+static void msg_conn_create(HypervTestDev *dev, uint8_t vpidx,
+                            uint8_t sint, uint8_t conn_id)
+{
+    TestMsgConn *conn;
+
+    conn = g_new0(TestMsgConn, 1);
+    assert(conn);
+
+    conn->conn_id = conn_id;
+
+    conn->sint_route = hyperv_sint_route_new(vpidx, sint, msg_cb, conn);
+    assert(conn->sint_route);
+
+    assert(!hyperv_set_msg_handler(conn->conn_id, msg_handler, conn));
+
+    QLIST_INSERT_HEAD(&dev->msg_conns, conn, le);
+}
+
+static void msg_conn_destroy(HypervTestDev *dev, uint8_t conn_id)
+{
+    TestMsgConn *conn;
+
+    QLIST_FOREACH(conn, &dev->msg_conns, le) {
+        if (conn->conn_id == conn_id) {
+            QLIST_REMOVE(conn, le);
+            hyperv_set_msg_handler(conn->conn_id, NULL, NULL);
+            hyperv_sint_route_unref(conn->sint_route);
+            g_free(conn);
+            return;
+        }
+    }
+    assert(false);
+}
+
+static void evt_conn_handler(EventNotifier *notifier)
+{
+    TestEvtConn *conn = container_of(notifier, TestEvtConn, notifier);
+
+    event_notifier_test_and_clear(notifier);
+
+    /* signal the same event flag we've got */
+    assert(!hyperv_set_evt_flag(conn->sint_route, conn->conn_id));
+}
+
+static void evt_conn_create(HypervTestDev *dev, uint8_t vpidx,
+                            uint8_t sint, uint8_t conn_id)
+{
+    TestEvtConn *conn;
+
+    conn = g_new0(TestEvtConn, 1);
+    assert(conn);
+
+    conn->conn_id = conn_id;
+
+    conn->sint_route = hyperv_sint_route_new(vpidx, sint, NULL, NULL);
+    assert(conn->sint_route);
+
+    assert(!event_notifier_init(&conn->notifier, false));
+
+    event_notifier_set_handler(&conn->notifier, evt_conn_handler);
+
+    assert(!hyperv_set_evt_notifier(conn_id, &conn->notifier));
+
+    QLIST_INSERT_HEAD(&dev->evt_conns, conn, le);
+}
+
+static void evt_conn_destroy(HypervTestDev *dev, uint8_t conn_id)
+{
+    TestEvtConn *conn;
+
+    QLIST_FOREACH(conn, &dev->evt_conns, le) {
+        if (conn->conn_id == conn_id) {
+            QLIST_REMOVE(conn, le);
+            hyperv_set_evt_notifier(conn->conn_id, NULL);
+            event_notifier_set_handler(&conn->notifier, NULL);
+            event_notifier_cleanup(&conn->notifier);
+            hyperv_sint_route_unref(conn->sint_route);
+            g_free(conn);
+            return;
+        }
+    }
+    assert(false);
+}
+
 static void hv_test_dev_control(void *opaque, hwaddr addr, uint64_t data,
                                 uint32_t len)
 {
@@ -102,6 +247,7 @@ static void hv_test_dev_control(void *opaque, hwaddr addr, uint64_t data,
     uint8_t sint = data & 0xFF;
     uint8_t vpidx = (data >> 8ULL) & 0xFF;
     uint8_t ctl = (data >> 16ULL) & 0xFF;
+    uint8_t conn_id = (data >> 24ULL) & 0xFF;
 
     switch (ctl) {
     case HV_TEST_DEV_SINT_ROUTE_CREATE:
@@ -113,6 +259,18 @@ static void hv_test_dev_control(void *opaque, hwaddr addr, uint64_t data,
     case HV_TEST_DEV_SINT_ROUTE_SET_SINT:
         sint_route_set_sint(dev, vpidx, sint);
         break;
+    case HV_TEST_DEV_MSG_CONN_CREATE:
+        msg_conn_create(dev, vpidx, sint, conn_id);
+        break;
+    case HV_TEST_DEV_MSG_CONN_DESTROY:
+        msg_conn_destroy(dev, conn_id);
+        break;
+    case HV_TEST_DEV_EVT_CONN_CREATE:
+        evt_conn_create(dev, vpidx, sint, conn_id);
+        break;
+    case HV_TEST_DEV_EVT_CONN_DESTROY:
+        evt_conn_destroy(dev, conn_id);
+        break;
     default:
         break;
     }
@@ -132,6 +290,8 @@ static void hv_test_dev_realizefn(DeviceState *d, Error **errp)
     MemoryRegion *io = isa_address_space_io(isa);
 
     QLIST_INIT(&dev->sint_routes);
+    QLIST_INIT(&dev->msg_conns);
+    QLIST_INIT(&dev->evt_conns);
     memory_region_init_io(&dev->sint_control, OBJECT(dev),
                           &synic_test_sint_ops, dev,
                           "hyperv-testdev-ctl", 4);
Signature · 1 line
2.9.4
#32 lore raw

PATCH v2 22/23 MAINTAINERS: add myself and eyakovlev@ for hyperv*

In reply to Roman Kagan
---
 MAINTAINERS | 7 +++++++
 1 file changed, 7 insertions(+)
MAINTAINERS +7 -0
index 120788d..16c5422 100644
@@ -852,6 +852,13 @@ F: hw/core/machine.c
 F: hw/core/null-machine.c
 F: include/hw/boards.h
 
+Hyper-V emulation core
+M: Roman Kagan <[email protected]>
+M: Evgeny Yakovlev <[email protected]>
+S: Supported
+F: target/i386/hyperv*
+F: hw/misc/hyperv*
+
 Xtensa Machines
 ---------------
 sim
Signature · 1 line
2.9.4
#33 lore raw

PATCH v2 23/23 hyperv: update copyright notices

In reply to Roman Kagan
---
 target/i386/hyperv.h     | 1 +
 hw/misc/hyperv_testdev.c | 1 +
 target/i386/hyperv.c     | 1 +
 3 files changed, 3 insertions(+)
target/i386/hyperv.h +1 -0
index f101144..cba6cbf 100644
@@ -2,6 +2,7 @@
  * QEMU KVM Hyper-V support
  *
  * Copyright (C) 2015 Andrey Smetanin <[email protected]>
+ * Copyright (C) 2017 Parallels International GmbH
  *
  * Authors:
  *  Andrey Smetanin <[email protected]>
hw/misc/hyperv_testdev.c +1 -0
index 4fc7787..57ee42d 100644
@@ -2,6 +2,7 @@
  * QEMU KVM Hyper-V test device to support Hyper-V kvm-unit-tests
  *
  * Copyright (C) 2015 Andrey Smetanin <[email protected]>
+ * Copyright (C) 2017 Parallels International GmbH
  *
  * Authors:
  *  Andrey Smetanin <[email protected]>
target/i386/hyperv.c +1 -0
index f779977..32c64af 100644
@@ -2,6 +2,7 @@
  * QEMU KVM Hyper-V support
  *
  * Copyright (C) 2015 Andrey Smetanin <[email protected]>
+ * Copyright (C) 2017 Parallels International GmbH
  *
  * Authors:
  *  Andrey Smetanin <[email protected]>
Signature · 1 line
2.9.4
#34 lore raw
In reply to Roman Kagan
Quoted 2 lines · On Wed, 21 Jun 2017 19:24:01 +0300 Roman Kagan <[email protected]> wrote:

This series applies miscellaneous fixes and enhancements to Hyper-V emulation code in QEMU, and lays out the ground for VMBus devices.

Series is a big random mix of cleanup/refactoring/new features which is hard to review due to mixed content of above within patches as well inter them.

I suggest to split series in at least in 2
1: make cleanups/refactoring first
and on top of this
2: add new features

* hyperv_find_vcpu() with expensive lookup and related renaming might be not needed at all if local_apics[12/23] is used.

*pls add in cover letter a link to public git repo where
it would be possible to pull from for testing/review.

PS:
if renaming, do it consistently.
ex:
vcpu_id -> vp_index touches only hyperv.c
but leaves old naming in user of kvm_hv_sint_route_create(): hyperv_testdev.c

Quoted 53 lines

v1 -> v2:
- drop the already merged patch
- split and rework SINTx and SVERSION msrs init
- factor out hyperv vcpu init to a function
- rework vp_index management
- distinguish kvm-only (== legacy) mode for SynIC
- use new capabilities recently submitted to KVM
- add compat logic for SynIC
- drop workaround for KVM zeroing SynIC pages
- minor fixes according to comments

Evgeny Yakovlev (1):
hyperv: set partition-wide MSRs only on first vcpu

Roman Kagan (22):
hyperv: add header with protocol definitions
update-linux-headers: prepare for hyperv.h removal
hyperv: ensure SINTx msrs are reset properly
hyperv: make SynIC version msr constant
[not to commit] add new hyperv-related caps
hyperv: ensure VP index equal to QEMU cpu_index
hyperv_testdev: refactor for readability
hyperv: cosmetic: g_malloc -> g_new
hyperv: synic: only setup ack notifier if there's a callback
hyperv: allow passing arbitrary data to sint ack callback
hyperv: address HvSintRoute by X86CPU pointer
hyperv: make HvSintRoute reference-counted
hyperv: qom-ify SynIC
hyperv: block SynIC use in QEMU in incompatible configurations
hyperv: make overlay pages for SynIC
hyperv: add synic message delivery
hyperv: add synic event flag signaling
hyperv: process SIGNAL_EVENT hypercall
hyperv: process POST_MESSAGE hypercall
hyperv_testdev: add SynIC message and event testmodes
MAINTAINERS: add myself and eyakovlev@ for hyperv*
hyperv: update copyright notices

 include/hw/i386/pc.h            |   5 +
 linux-headers/linux/kvm.h       |   2 +
 target/i386/cpu.h               |  16 +-
 target/i386/hyperv.h            |  40 ++-
 target/i386/hyperv_proto.h      | 257 ++++++++++++++++
 hw/misc/hyperv_testdev.c        | 267 +++++++++++++----
 target/i386/cpu.c               |   4 +-
 target/i386/hyperv.c            | 634 ++++++++++++++++++++++++++++++++++++++--
 target/i386/kvm.c               | 167 +++++++----
 target/i386/machine.c           |  24 +-
 MAINTAINERS                     |   7 +
 scripts/update-linux-headers.sh |   4 +-
 12 files changed, 1256 insertions(+), 171 deletions(-)
 create mode 100644 target/i386/hyperv_proto.h
#35 lore raw
Quoted 6 lines · Igor Mammedov
Quoted 2 lines · On Wed, 21 Jun 2017 19:24:01 +0300 Roman Kagan <[email protected]> wrote:

This series applies miscellaneous fixes and enhancements to Hyper-V emulation code in QEMU, and lays out the ground for VMBus devices.

Quoted 8 lines

Series is a big random mix of cleanup/refactoring/new features which is hard to review due to mixed content of above within patches as well inter them.

I suggest to split series in at least in 2
1: make cleanups/refactoring first
and on top of this
2: add new features

Indeed, I started to realize this. I'll try to re-split better when respinning.

Quoted 5 lines

* hyperv_find_vcpu() with expensive lookup and related renaming might be not needed at all if local_apics[12/23] is used.

*pls add in cover letter a link to public git repo where
it would be possible to pull from for testing/review.

I'm in the process of setting it up, will include it next time.

Quoted 5 lines

PS:
if renaming, do it consistently.
ex:
vcpu_id -> vp_index touches only hyperv.c
but leaves old naming in user of kvm_hv_sint_route_create(): hyperv_testdev.c

I strived for eventual consistency: no vcpu_id is left after the last patch in the series. But it's probably harder to review patch by patch. So I'll redo the way you suggest.

Thanks,
Roman.