Skip to contentSkip to conversation

hyperv fixes and enhancements

PATCH Superseded Roman Kagan 4 participants 76 messages Jun 6, 2017 – Jun 23, 2017 1 message not in the archive

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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.

To give you a heads up, we currently have an implementation of VMBus SCSI storage controller that works with native drivers in Windows 2008sp2 to 2016 and Linux; when used with modified SeaBIOS it can also install and boot with no extra drivers required. We hope to submit it within 2.10 merge window in a few reasonably-sized self-contained patchsets; this is the first one.

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 msrs are inited properly
hyperv: ensure VP index equal to QEMU cpu_index
hyperv: helper to find vcpu by VP 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: qdev-ify SynIC
kvm-all: make async_safe_run_on_cpu safe on kvm too
hyperv: make overlay pages for SynIC
hyperv: map overlay pages after updating msrs
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

 target/i386/cpu.h               |  18 +-
 target/i386/hyperv.h            |  38 ++-
 target/i386/hyperv_proto.h      | 257 +++++++++++++++++
 hw/misc/hyperv_testdev.c        | 269 ++++++++++++++----
 kvm-all.c                       |   2 +
 target/i386/cpu.c               |   4 +-
 target/i386/hyperv.c            | 612 +++++++++++++++++++++++++++++++++++++---
 target/i386/kvm.c               | 126 ++++++---
 target/i386/machine.c           |  15 +-
 MAINTAINERS                     |   7 +
 scripts/update-linux-headers.sh |   4 +-
 11 files changed, 1189 insertions(+), 163 deletions(-)
 create mode 100644 target/i386/hyperv_proto.h
Signature · 1 line
2.9.4
#1 lore raw

PATCH 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 cfe825f..9335dcc 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 ffb5267..ef46850 100644
@@ -3808,12 +3808,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 3cb2729..eb00b19 100644
@@ -570,7 +570,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;
         }
@@ -584,8 +584,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()
     }
 };
@@ -643,8 +642,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()
     }
 };
@@ -669,10 +667,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 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 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 9335dcc..7af2cce 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 a6debbd..3a80913 100644
@@ -1719,19 +1719,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 4/23 hyperv: ensure msrs are inited properly

In reply to Roman Kagan

Make sure HV_X64_MSR_SVERSION and HV_X64_MSR_SINTx are properly initialized at guest start.

For that, move the field containing SVERSION value into the region in CPUX86State which is preserved across resets, and move the initialization of SINTx to kvm_arch_vcpu_reset().

---
 target/i386/cpu.h |  3 ++-
 target/i386/kvm.c | 12 +++++++-----
 2 files changed, 9 insertions(+), 6 deletions(-)
target/i386/cpu.h +2 -1
index 7af2cce..7c97bce 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];
@@ -1159,6 +1158,8 @@ typedef struct CPUX86State {
     uint64_t mtrr_deftype;
     MTRRVar mtrr_var[MSR_MTRRcap_VCNT];
 
+    uint64_t msr_hv_synic_version;
+
     /* For KVM */
     uint32_t mp_state;
     int32_t exception_injected;
target/i386/kvm.c +7 -5
index 3a80913..251aa95 100644
@@ -646,8 +646,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");
@@ -656,9 +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;
-        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) {
@@ -1038,6 +1033,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 5/23 hyperv: ensure VP index equal to QEMU cpu_index

In reply to Roman Kagan

Hyper-V identifies vcpus by the virtual processor (VP) index which is normally queried by the guest via HV_X64_MSR_VP_INDEX msr.

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

However, the current implementation in KVM doesn't allow to set this msr, and KVM uses its own notion of VP index. Fortunately, the way vcpus are created in QEMU/KVM basically guarantees that the KVM value is equal to QEMU cpu_index.

So, for back and forward compatibility, attempt to set the msr at vcpu init time to cpu_index, but ignore the errors; then query the msr value from KVM and assert that it's equal to cpu_index. On current kernels this will work by luck; future ones will accept the value from userspace.

---
 target/i386/hyperv.h |  2 ++
 target/i386/hyperv.c |  5 +++++
 target/i386/kvm.c    | 26 ++++++++++++++++++++++++++
 3 files changed, 33 insertions(+)
target/i386/hyperv.h +2 -0
index 0c3b562..35da0b1 100644
@@ -39,4 +39,6 @@ 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);
+
 #endif
target/i386/hyperv.c +5 -0
index 227185c..27de5bc 100644
@@ -16,6 +16,11 @@
 #include "hyperv.h"
 #include "hyperv_proto.h"
 
+uint32_t hyperv_vp_index(X86CPU *cpu)
+{
+    return CPU(cpu)->cpu_index;
+}
+
 int kvm_hv_handle_exit(X86CPU *cpu, struct kvm_hyperv_exit *exit)
 {
     CPUX86State *env = &cpu->env;
target/i386/kvm.c +26 -0
index 251aa95..eb9cde4 100644
@@ -610,11 +610,37 @@ static int kvm_arch_set_tsc_khz(CPUState *cs)
     return 0;
 }
 
+static void hyperv_set_vp_index(CPUState *cs)
+{
+    struct {
+        struct kvm_msrs info;
+        struct kvm_msr_entry entries[1];
+    } msr_data;
+    int ret;
+
+    msr_data.info.nmsrs = 1;
+    msr_data.entries[0].index = HV_X64_MSR_VP_INDEX;
+
+    /*
+     * Some kernels don't support setting HV_X64_MSR_VP_INDEX.  However,
+     * they use VP_INDEX equal to cpu_index due to the way vcpus are created.
+     * So ignore the errors from SET_MSRS, but verify that GET_MSRS returns the
+     * expected value.
+     */
+    msr_data.entries[0].data = cs->cpu_index;
+    kvm_vcpu_ioctl(cs, KVM_SET_MSRS, &msr_data);
+    ret = kvm_vcpu_ioctl(cs, KVM_GET_MSRS, &msr_data);
+    assert(ret == 1);
+    assert(msr_data.entries[0].data == cs->cpu_index);
+}
+
 static int hyperv_handle_properties(CPUState *cs)
 {
     X86CPU *cpu = X86_CPU(cs);
     CPUX86State *env = &cpu->env;
 
+    hyperv_set_vp_index(cs);
+
     if (cpu->hyperv_time &&
             kvm_check_extension(cs->kvm_state, KVM_CAP_HYPERV_TIME) <= 0) {
         cpu->hyperv_time = false;
Signature · 1 line
2.9.4
#6 lore raw

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

Quoted 9 lines · Roman Kagan

Hyper-V identifies vcpus by the virtual processor (VP) index which is normally queried by the guest via HV_X64_MSR_VP_INDEX msr.

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

However, the current implementation in KVM doesn't allow to set this msr, and KVM uses its own notion of VP index. Fortunately, the way vcpus are created in QEMU/KVM basically guarantees that the KVM value is equal to QEMU cpu_index.

This might not be true in the future. cpu_index is not a good identifier for CPUs, and we would like to remove it in the future.

But it looks like we have no choice, see extra comments below:

Quoted 30 lines

So, for back and forward compatibility, attempt to set the msr at vcpu init time to cpu_index, but ignore the errors; then query the msr value from KVM and assert that it's equal to cpu_index. On current kernels this will work by luck; future ones will accept the value from userspace.

---
 target/i386/hyperv.h |  2 ++
 target/i386/hyperv.c |  5 +++++
 target/i386/kvm.c    | 26 ++++++++++++++++++++++++++
 3 files changed, 33 insertions(+)
target/i386/hyperv.h +2 -0
index 0c3b562..35da0b1 100644
@@ -39,4 +39,6 @@ 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);
+
 #endif
target/i386/hyperv.c +0 -0
index 227185c..27de5bc 100644

target/i386/hyperv.c line 23

Quoted 8 lines
target/i386/hyperv.c +5 -0
@@ -16,6 +16,11 @@
 #include "hyperv.h"
 #include "hyperv_proto.h"
 
+uint32_t hyperv_vp_index(X86CPU *cpu)
+{
+    return CPU(cpu)->cpu_index;
+}
+

You are introducing a wrapper, but you are using cs->cpu_index directly at hyperv_set_vp_index(). The knowledge that vp_index == cpu_index is duplicated on two places, and the functions risk getting out of sync.

I suggest using the wrapper inside hyperv_set_vp_index() too.

Quoted 18 lines
target/i386/hyperv.c +0 -0
 int kvm_hv_handle_exit(X86CPU *cpu, struct kvm_hyperv_exit *exit)
 {
     CPUX86State *env = &cpu->env;
target/i386/kvm.c +7 -0
index 251aa95..eb9cde4 100644
@@ -610,11 +610,37 @@ static int kvm_arch_set_tsc_khz(CPUState *cs)
     return 0;
 }
 
+static void hyperv_set_vp_index(CPUState *cs)
+{
+    struct {
+        struct kvm_msrs info;
+        struct kvm_msr_entry entries[1];
+    } msr_data;
+    int ret;

target/i386/kvm.c static int kvm_arch_set_tsc_khz(CPUState *cs) · line 629

Quoted 10 lines
target/i386/kvm.c +10 -0
+
+    msr_data.info.nmsrs = 1;
+    msr_data.entries[0].index = HV_X64_MSR_VP_INDEX;
+
+    /*
+     * Some kernels don't support setting HV_X64_MSR_VP_INDEX.  However,
+     * they use VP_INDEX equal to cpu_index due to the way vcpus are created.
+     * So ignore the errors from SET_MSRS, but verify that GET_MSRS returns the
+     * expected value.
+     */

Oh. This sounds like a problem. As reference for others, this is the KVM code in kvm_hv_get_msr():

	case HV_X64_MSR_VP_INDEX: {
		int r;
		struct kvm_vcpu *v;

		kvm_for_each_vcpu(r, v, vcpu->kvm) {
			if (v == vcpu) {
				data = r;
				break;
			}
		}
		break;
	}

The problem with that is that it will break as soon as we create VCPUs in a different order. Unsolvable on hosts that don't allow HV_X64_MSR_VP_INDEX to be set, however.

target/i386/kvm.c static int kvm_arch_set_tsc_khz(CPUState *cs) · line 634

Quoted 5 lines
target/i386/kvm.c +5 -0
+    msr_data.entries[0].data = cs->cpu_index;
+    kvm_vcpu_ioctl(cs, KVM_SET_MSRS, &msr_data);
+    ret = kvm_vcpu_ioctl(cs, KVM_GET_MSRS, &msr_data);
+    assert(ret == 1);
+    assert(msr_data.entries[0].data == cs->cpu_index);

If KVM already initializes the MSR to cpu_index and we will abort if it was not set to anything except cpu_index here, why exactly do we need the KVM_SET_MSRS call?

target/i386/kvm.c static int kvm_arch_set_tsc_khz(CPUState *cs) · line 643

Quoted 9 lines
target/i386/kvm.c +4 -0
+}
+
 static int hyperv_handle_properties(CPUState *cs)
 {
     X86CPU *cpu = X86_CPU(cs);
     CPUX86State *env = &cpu->env;
 
+    hyperv_set_vp_index(cs);
+

The purpose of hyperv_handle_properties() is just to initialize cpu->features[] CPUID data based on the hyperv options. I suggest creating a new hyperv_vcpu_init() or x86_cpu_hyperv_init() function for these initialization steps.

Quoted 6 lines
target/i386/kvm.c +0 -0
     if (cpu->hyperv_time &&
             kvm_check_extension(cs->kvm_state, KVM_CAP_HYPERV_TIME) <= 0) {
         cpu->hyperv_time = false;
Signature · 1 line
2.9.4
Signature · 1 line
Eduardo
#7 lore raw

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

Quoted 51 lines · Eduardo Habkost
Quoted 9 lines · Roman Kagan

Hyper-V identifies vcpus by the virtual processor (VP) index which is normally queried by the guest via HV_X64_MSR_VP_INDEX msr.

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

However, the current implementation in KVM doesn't allow to set this msr, and KVM uses its own notion of VP index. Fortunately, the way vcpus are created in QEMU/KVM basically guarantees that the KVM value is equal to QEMU cpu_index.

This might not be true in the future. cpu_index is not a good identifier for CPUs, and we would like to remove it in the future.

But it looks like we have no choice, see extra comments below:

Quoted 7 lines
target/i386/kvm.c +7 -0
+static void hyperv_set_vp_index(CPUState *cs)
+{
+    struct {
+        struct kvm_msrs info;
+        struct kvm_msr_entry entries[1];
+    } msr_data;
+    int ret;

target/i386/kvm.c static int kvm_arch_set_tsc_khz(CPUState *cs) · line 629

Quoted 10 lines
target/i386/kvm.c +10 -0
+
+    msr_data.info.nmsrs = 1;
+    msr_data.entries[0].index = HV_X64_MSR_VP_INDEX;
+
+    /*
+     * Some kernels don't support setting HV_X64_MSR_VP_INDEX.  However,
+     * they use VP_INDEX equal to cpu_index due to the way vcpus are created.
+     * So ignore the errors from SET_MSRS, but verify that GET_MSRS returns the
+     * expected value.
+     */

Oh. This sounds like a problem. As reference for others, this is the KVM code in kvm_hv_get_msr():

	case HV_X64_MSR_VP_INDEX: {
		int r;
		struct kvm_vcpu *v;

		kvm_for_each_vcpu(r, v, vcpu->kvm) {
			if (v == vcpu) {
				data = r;
				break;
			}
		}
		break;
	}
Quoted 3 lines

The problem with that is that it will break as soon as we create VCPUs in a different order. Unsolvable on hosts that don't allow HV_X64_MSR_VP_INDEX to be set, however.

Right, thanks for putting together a detailed explanation.

This was a thinko back then, not to have HV_X64_MSR_VP_INDEX maintained by QEMU. I'm going to post a patch to KVM fixing that.

Meanwhile QEMU needs a way to maintain its notion of vp_index that is
1) in sync with kernel's notion
2) also with kernels that don't support setting the msr
3) persistent across migrations

cpu_index looked like a perfect candidate.

Quoted 9 lines

target/i386/kvm.c static int kvm_arch_set_tsc_khz(CPUState *cs) · line 634

Quoted 5 lines
target/i386/kvm.c +5 -0
+    msr_data.entries[0].data = cs->cpu_index;
+    kvm_vcpu_ioctl(cs, KVM_SET_MSRS, &msr_data);
+    ret = kvm_vcpu_ioctl(cs, KVM_GET_MSRS, &msr_data);
+    assert(ret == 1);
+    assert(msr_data.entries[0].data == cs->cpu_index);

If KVM already initializes the MSR to cpu_index and we will abort if it was not set to anything except cpu_index here, why exactly do we need the KVM_SET_MSRS call?

The kernel made no obligations to keep initializing its vp_index identically to QEMU's cpu_index. So we'd better check and abort if that got out of sync. Once KVM gains the ability to learn vp_index from QEMU we'll no longer depend on that as we'll do explicit synchronization.

Quoted 7 lines
Quoted 7 lines
target/i386/kvm.c +2 -0
+}
+
 static int hyperv_handle_properties(CPUState *cs)
 {
     X86CPU *cpu = X86_CPU(cs);
     CPUX86State *env = &cpu->env;
Quoted 7 lines

target/i386/kvm.c static int kvm_arch_set_tsc_khz(CPUState *cs) · line 643

Quoted 2 lines
target/i386/kvm.c +2 -0
+    hyperv_set_vp_index(cs);
+

The purpose of hyperv_handle_properties() is just to initialize cpu->features[] CPUID data based on the hyperv options. I suggest creating a new hyperv_vcpu_init() or x86_cpu_hyperv_init() function for these initialization steps.

Sounds like a good idea, thanks.

Roman.

#8 lore raw

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

Quoted 8 lines · Roman Kagan
Quoted 3 lines

The problem with that is that it will break as soon as we create VCPUs in a different order. Unsolvable on hosts that don't allow HV_X64_MSR_VP_INDEX to be set, however.

Right, thanks for putting together a detailed explanation.

This was a thinko back then, not to have HV_X64_MSR_VP_INDEX maintained by QEMU. I'm going to post a patch to KVM fixing that.

Quoted 7 lines

Meanwhile QEMU needs a way to maintain its notion of vp_index that is
1) in sync with kernel's notion
2) also with kernels that don't support setting the msr
3) persistent across migrations

cpu_index looked like a perfect candidate.

What you want is the APIC id, which _is_ cpu_index but may not be in the future. But the APIC id is also the KVM vcpu_id, so there's no need to have VP_INDEX maintained by QEMU.

Paolo

#9 lore raw

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

Quoted 14 lines · On Wed, 14 Jun 2017 13:26:44 +0200 Paolo Bonzini <[email protected]> wrote:
Quoted 13 lines · Roman Kagan
Quoted 3 lines

The problem with that is that it will break as soon as we create VCPUs in a different order. Unsolvable on hosts that don't allow HV_X64_MSR_VP_INDEX to be set, however.

Right, thanks for putting together a detailed explanation.

This was a thinko back then, not to have HV_X64_MSR_VP_INDEX maintained by QEMU. I'm going to post a patch to KVM fixing that.

Meanwhile QEMU needs a way to maintain its notion of vp_index that is
1) in sync with kernel's notion
2) also with kernels that don't support setting the msr
3) persistent across migrations

Quoted 6 lines
Quoted 2 lines

cpu_index looked like a perfect candidate.

What you want is the APIC id,

which _is_ cpu_index but may not be in the

depending on topology cpu_index won't be the same as APIC ID/vcpu_id /AMDs odd core count/.

Quoted 2 lines

future. But the APIC id is also the KVM vcpu_id, so there's no need to have VP_INDEX maintained by QEMU.

agreed it'd be better to reuse vcpu_id/apic id as interface between qemu/kvm/guest instead of adding additional cpu_index concept in ABI

Quoted 2 lines

Paolo

#10 lore raw

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

Quoted 17 lines · Igor Mammedov
Quoted 14 lines · On Wed, 14 Jun 2017 13:26:44 +0200 Paolo Bonzini <[email protected]> wrote:
Quoted 13 lines · Roman Kagan
Quoted 3 lines

The problem with that is that it will break as soon as we create VCPUs in a different order. Unsolvable on hosts that don't allow HV_X64_MSR_VP_INDEX to be set, however.

Right, thanks for putting together a detailed explanation.

This was a thinko back then, not to have HV_X64_MSR_VP_INDEX maintained by QEMU. I'm going to post a patch to KVM fixing that.

Meanwhile QEMU needs a way to maintain its notion of vp_index that is
1) in sync with kernel's notion
2) also with kernels that don't support setting the msr
3) persistent across migrations

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

cpu_index looked like a perfect candidate.

What you want is the APIC id,

which _is_ cpu_index but may not be in the

depending on topology cpu_index won't be the same as APIC ID/vcpu_id /AMDs odd core count/.

So vcpu_id can be sparse?

Quoted 4 lines
Quoted 2 lines

future. But the APIC id is also the KVM vcpu_id, so there's no need to have VP_INDEX maintained by QEMU.

agreed it'd be better to reuse vcpu_id/apic id as interface between qemu/kvm/guest instead of adding additional cpu_index concept in ABI

Having consulted the spec, I'm not so confident any more this is the right move.

Quoted 7 lines

7.8.1 Virtual Processor Index

Virtual processors are identified by using an index (VP index). The maximum number of virtual processors per partition supported by the current implementation of the hypervisor can be obtained through CPUID leaf 0x40000005. A virtual processor index must be less than the maximum number of virtual processors per partition.

This seems to imply that VP index should be dense. As if they use it directly as an index into an array whose length is equal to the max number of vcpus. (BTW the value we report for it is currently hardcoded to 0x40 which probably needs fixing, too.)

I'm starting to drift back to adding SET_MSRS support to HV_X64_MSR_VP_INDEX in KVM to delegate the control to QEMU, and having QEMU use cpu_index as its value... :-/

Roman.

#11 lore raw

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

Quoted 21 lines · Roman Kagan
Quoted 20 lines · Igor Mammedov
Quoted 17 lines · On Wed, 14 Jun 2017 13:26:44 +0200 Paolo Bonzini <[email protected]> wrote:
Quoted 15 lines · Roman Kagan
Quoted 3 lines

The problem with that is that it will break as soon as we create VCPUs in a different order. Unsolvable on hosts that don't allow HV_X64_MSR_VP_INDEX to be set, however.

Right, thanks for putting together a detailed explanation.

This was a thinko back then, not to have HV_X64_MSR_VP_INDEX maintained by QEMU. I'm going to post a patch to KVM fixing that.

Meanwhile QEMU needs a way to maintain its notion of vp_index that is
1) in sync with kernel's notion
2) also with kernels that don't support setting the msr
3) persistent across migrations

cpu_index looked like a perfect candidate.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

What you want is the APIC id,

which _is_ cpu_index but may not be in the

depending on topology cpu_index won't be the same as APIC ID/vcpu_id /AMDs odd core count/.

So vcpu_id can be sparse?

Yes.

Quoted 11 lines

Having consulted the spec, I'm not so confident any more this is the right move.

Quoted 7 lines

7.8.1 Virtual Processor Index

Virtual processors are identified by using an index (VP index). The maximum number of virtual processors per partition supported by the current implementation of the hypervisor can be obtained through CPUID leaf 0x40000005. A virtual processor index must be less than the maximum number of virtual processors per partition.

Quoted 8 lines

This seems to imply that VP index should be dense. As if they use it directly as an index into an array whose length is equal to the max number of vcpus. (BTW the value we report for it is currently hardcoded to 0x40 which probably needs fixing, too.)

I'm starting to drift back to adding SET_MSRS support to HV_X64_MSR_VP_INDEX in KVM to delegate the control to QEMU, and having QEMU use cpu_index as its value... :-/

Yes, definitely.

Paolo

#12 lore raw

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

In reply to Roman Kagan
Quoted 21 lines · On Thu, 15 Jun 2017 15:41:08 +0300 Roman Kagan <[email protected]> wrote:
Quoted 20 lines · Igor Mammedov
Quoted 17 lines · On Wed, 14 Jun 2017 13:26:44 +0200 Paolo Bonzini <[email protected]> wrote:
Quoted 15 lines · Roman Kagan
Quoted 3 lines

The problem with that is that it will break as soon as we create VCPUs in a different order. Unsolvable on hosts that don't allow HV_X64_MSR_VP_INDEX to be set, however.

Right, thanks for putting together a detailed explanation.

This was a thinko back then, not to have HV_X64_MSR_VP_INDEX maintained by QEMU. I'm going to post a patch to KVM fixing that.

Meanwhile QEMU needs a way to maintain its notion of vp_index that is
1) in sync with kernel's notion
2) also with kernels that don't support setting the msr
3) persistent across migrations

cpu_index looked like a perfect candidate.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

What you want is the APIC id,

which _is_ cpu_index but may not be in the

depending on topology cpu_index won't be the same as APIC ID/vcpu_id /AMDs odd core count/.

So vcpu_id can be sparse?

yes

I suppose kernel even has cpu->apic_id map somewhere that you can reuse instead of custom map hv_context.vp_index[]

Quoted 16 lines
Quoted 4 lines
Quoted 2 lines

future. But the APIC id is also the KVM vcpu_id, so there's no need to have VP_INDEX maintained by QEMU.

agreed it'd be better to reuse vcpu_id/apic id as interface between qemu/kvm/guest instead of adding additional cpu_index concept in ABI

Having consulted the spec, I'm not so confident any more this is the right move.

Quoted 7 lines

7.8.1 Virtual Processor Index

Virtual processors are identified by using an index (VP index). The maximum number of virtual processors per partition supported by the current implementation of the hypervisor can be obtained through CPUID leaf 0x40000005. A virtual processor index must be less than the maximum number of virtual processors per partition.

Quoted 4 lines

This seems to imply that VP index should be dense. As if they use it directly as an index into an array whose length is equal to the max number of vcpus. (BTW the value we report for it is currently hardcoded to 0x40 which probably needs fixing, too.)

the question here is where "maximum number of virtual processors" comes from

if it's possible to tell guest value explicitly,
we can use pcms->apic_id_limit for it.

Quoted 5 lines

I'm starting to drift back to adding SET_MSRS support to HV_X64_MSR_VP_INDEX in KVM to delegate the control to QEMU, and having QEMU use cpu_index as its value... :-/

Roman.

#13 lore raw

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

Quoted 26 lines · Igor Mammedov
Quoted 24 lines · On Thu, 15 Jun 2017 15:41:08 +0300 Roman Kagan <[email protected]> wrote:
Quoted 23 lines · Igor Mammedov
Quoted 20 lines · On Wed, 14 Jun 2017 13:26:44 +0200 Paolo Bonzini <[email protected]> wrote:
Quoted 8 lines · Roman Kagan
Quoted 3 lines

The problem with that is that it will break as soon as we create VCPUs in a different order. Unsolvable on hosts that don't allow HV_X64_MSR_VP_INDEX to be set, however.

Right, thanks for putting together a detailed explanation.

This was a thinko back then, not to have HV_X64_MSR_VP_INDEX maintained by QEMU. I'm going to post a patch to KVM fixing that.

Quoted 7 lines

Meanwhile QEMU needs a way to maintain its notion of vp_index that is
1) in sync with kernel's notion
2) also with kernels that don't support setting the msr
3) persistent across migrations

cpu_index looked like a perfect candidate.

What you want is the APIC id,

which _is_ cpu_index but may not be in the

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

depending on topology cpu_index won't be the same as APIC ID/vcpu_id /AMDs odd core count/.

So vcpu_id can be sparse?

yes

I suppose kernel even has cpu->apic_id map somewhere that you can reuse instead of custom map hv_context.vp_index[]

Right, but this is not the problem. Whether apic_id matches the expected vp_index semantics is.

Quoted 16 lines
Quoted 16 lines
Quoted 4 lines
Quoted 2 lines

future. But the APIC id is also the KVM vcpu_id, so there's no need to have VP_INDEX maintained by QEMU.

agreed it'd be better to reuse vcpu_id/apic id as interface between qemu/kvm/guest instead of adding additional cpu_index concept in ABI

Having consulted the spec, I'm not so confident any more this is the right move.

Quoted 7 lines

7.8.1 Virtual Processor Index

Virtual processors are identified by using an index (VP index). The maximum number of virtual processors per partition supported by the current implementation of the hypervisor can be obtained through CPUID leaf 0x40000005. A virtual processor index must be less than the maximum number of virtual processors per partition.

Quoted 5 lines
Quoted 4 lines

This seems to imply that VP index should be dense. As if they use it directly as an index into an array whose length is equal to the max number of vcpus. (BTW the value we report for it is currently hardcoded to 0x40 which probably needs fixing, too.)

the question here is where "maximum number of virtual processors" comes from

We provide it in a corresponding cpuid leaf.

Quoted 2 lines

if it's possible to tell guest value explicitly,
we can use pcms->apic_id_limit for it.

But, depending on the apic_id encoding scheme, this can be arbitrarily bigger than the actual maximum number of vcpus. Which can result in guest confusion or inefficiency.

Quoted 3 lines
Quoted 3 lines

I'm starting to drift back to adding SET_MSRS support to HV_X64_MSR_VP_INDEX in KVM to delegate the control to QEMU, and having QEMU use cpu_index as its value... :-/

Roman.

#14 lore raw

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

Quoted 1 line · Roman Kagan

On Thu, Jun 15, 2017 at 03:27:28PM +0200, Igor Mammedov wrote:

[...]

Quoted 22 lines
Quoted 16 lines
Quoted 16 lines
Quoted 4 lines
Quoted 2 lines

future. But the APIC id is also the KVM vcpu_id, so there's no need to have VP_INDEX maintained by QEMU.

agreed it'd be better to reuse vcpu_id/apic id as interface between qemu/kvm/guest instead of adding additional cpu_index concept in ABI

Having consulted the spec, I'm not so confident any more this is the right move.

Quoted 7 lines

7.8.1 Virtual Processor Index

Virtual processors are identified by using an index (VP index). The maximum number of virtual processors per partition supported by the current implementation of the hypervisor can be obtained through CPUID leaf 0x40000005. A virtual processor index must be less than the maximum number of virtual processors per partition.

Quoted 5 lines
Quoted 4 lines

This seems to imply that VP index should be dense. As if they use it directly as an index into an array whose length is equal to the max number of vcpus. (BTW the value we report for it is currently hardcoded to 0x40 which probably needs fixing, too.)

the question here is where "maximum number of virtual processors" comes from

Quoted 8 lines

We provide it in a corresponding cpuid leaf.

Quoted 2 lines

if it's possible to tell guest value explicitly,
we can use pcms->apic_id_limit for it.

But, depending on the apic_id encoding scheme, this can be arbitrarily bigger than the actual maximum number of vcpus. Which can result in guest confusion or inefficiency.

Note that it will be bigger only if threads-per-core or cores-per-thread are not powers of 2. If your use case don't require configuration of a virtual thread/core topology, the APIC IDs can be always contiguous.

Signature · 1 line
Eduardo
#15 lore raw

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

In reply to Paolo Bonzini
Quoted 16 lines · Paolo Bonzini
Quoted 13 lines · Roman Kagan
Quoted 3 lines

The problem with that is that it will break as soon as we create VCPUs in a different order. Unsolvable on hosts that don't allow HV_X64_MSR_VP_INDEX to be set, however.

Right, thanks for putting together a detailed explanation.

This was a thinko back then, not to have HV_X64_MSR_VP_INDEX maintained by QEMU. I'm going to post a patch to KVM fixing that.

Meanwhile QEMU needs a way to maintain its notion of vp_index that is
1) in sync with kernel's notion
2) also with kernels that don't support setting the msr
3) persistent across migrations

Quoted 6 lines
Quoted 2 lines

cpu_index looked like a perfect candidate.

What you want is the APIC id, which _is_ cpu_index but may not be in the future. But the APIC id is also the KVM vcpu_id, so there's no need to have VP_INDEX maintained by QEMU.

No, KVM really uses the VCPU _index_ for HV_X64_MSR_VP_INDEX:

kvm_hv_get_msr():

        case HV_X64_MSR_VP_INDEX: {
                int r;
                struct kvm_vcpu *v;

                kvm_for_each_vcpu(r, v, vcpu->kvm) {
                        if (v == vcpu) {
                                data = r;
                                break;
                        }
                }
                break;
        }
Signature · 1 line
Eduardo
#16 lore raw

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

Quoted 17 lines · On Wed, 14 Jun 2017 10:01:49 -0300 Eduardo Habkost <[email protected]> wrote:
Quoted 16 lines · Paolo Bonzini
Quoted 13 lines · Roman Kagan
Quoted 3 lines

The problem with that is that it will break as soon as we create VCPUs in a different order. Unsolvable on hosts that don't allow HV_X64_MSR_VP_INDEX to be set, however.

Right, thanks for putting together a detailed explanation.

This was a thinko back then, not to have HV_X64_MSR_VP_INDEX maintained by QEMU. I'm going to post a patch to KVM fixing that.

Meanwhile QEMU needs a way to maintain its notion of vp_index that is
1) in sync with kernel's notion
2) also with kernels that don't support setting the msr
3) persistent across migrations

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

cpu_index looked like a perfect candidate.

What you want is the APIC id, which _is_ cpu_index but may not be in the future. But the APIC id is also the KVM vcpu_id, so there's no need to have VP_INDEX maintained by QEMU.

No, KVM really uses the VCPU _index_ for HV_X64_MSR_VP_INDEX:

and as you pointed out that works just by luck, as soon as we there would be out of order created CPUs returned value won't match cpu_index.

So instead of spreading this nonsense out to QEMU, is it possible to fix kernel(kvm+guest) to use apic_id instead?

Quoted 16 lines

kvm_hv_get_msr():

        case HV_X64_MSR_VP_INDEX: {
                int r;
                struct kvm_vcpu *v;

                kvm_for_each_vcpu(r, v, vcpu->kvm) {
                        if (v == vcpu) {
                                data = r;
                                break;
                        }
                }
                break;
        }
#17 lore raw

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

Quoted 8 lines · Igor Mammedov
Quoted 1 line

No, KVM really uses the VCPU _index_ for HV_X64_MSR_VP_INDEX:

and as you pointed out that works just by luck, as soon as we there would be out of order created CPUs returned value won't match cpu_index.

So instead of spreading this nonsense out to QEMU, is it possible to fix kernel(kvm+guest) to use apic_id instead?

Yes, definitely. At this point let's add a new "KVM_CAP_HYPERV_SYNIC2" capability and declare the old one broken, that will make things easier.

Paolo

#18 lore raw

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

Quoted 9 lines · Paolo Bonzini
Quoted 6 lines · Igor Mammedov
Quoted 1 line

No, KVM really uses the VCPU _index_ for HV_X64_MSR_VP_INDEX:

and as you pointed out that works just by luck, as soon as we there would be out of order created CPUs returned value won't match cpu_index.

Quoted 5 lines
Quoted 2 lines

So instead of spreading this nonsense out to QEMU, is it possible to fix kernel(kvm+guest) to use apic_id instead?

Yes, definitely. At this point let's add a new "KVM_CAP_HYPERV_SYNIC2" capability and declare the old one broken, that will make things easier.

What do we need a capability for? Can't we just call KVM_SET_MSRS using arch_id (== apic_id) and warn the user if it doesn't work?

Signature · 1 line
Eduardo
#19 lore raw

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

Quoted 5 lines · Eduardo Habkost
Quoted 2 lines

Yes, definitely. At this point let's add a new "KVM_CAP_HYPERV_SYNIC2" capability and declare the old one broken, that will make things easier.

What do we need a capability for? Can't we just call KVM_SET_MSRS using arch_id (== apic_id) and warn the user if it doesn't work?

As mentioned in the review of patch 16/23, we may want to break backwards compatibility (meaning we drop the old capability and introduce a new one) for other reasons.

Paolo

#20 lore raw

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

In reply to Eduardo Habkost
Quoted 13 lines · On Wed, 14 Jun 2017 10:22:16 -0300 Eduardo Habkost <[email protected]> wrote:
Quoted 12 lines · Paolo Bonzini
Quoted 8 lines · Igor Mammedov
Quoted 1 line

No, KVM really uses the VCPU _index_ for HV_X64_MSR_VP_INDEX:

and as you pointed out that works just by luck, as soon as we there would be out of order created CPUs returned value won't match cpu_index.

So instead of spreading this nonsense out to QEMU, is it possible to fix kernel(kvm+guest) to use apic_id instead?

Quoted 7 lines
Quoted 2 lines

Yes, definitely. At this point let's add a new "KVM_CAP_HYPERV_SYNIC2" capability and declare the old one broken, that will make things easier.

What do we need a capability for?
Can't we just call
KVM_SET_MSRS using arch_id (== apic_id) and warn the user if it doesn't work?

warn won't solve issue, and it's called rather late in vcpu creation chain without any error propagation so it's not possible (hard) to gracefully fail cpu hotplug.

with KVM_CAP_HYPERV_SYNIC2 we could easily turn on compat property for SYNC device (needs QOMification) that would turn on broken compat logic if capability is not present/old machine type.

#21 lore raw

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

Quoted 19 lines · Igor Mammedov
Quoted 16 lines · On Wed, 14 Jun 2017 10:22:16 -0300 Eduardo Habkost <[email protected]> wrote:
Quoted 14 lines · Paolo Bonzini
Quoted 8 lines · Igor Mammedov
Quoted 1 line

No, KVM really uses the VCPU _index_ for HV_X64_MSR_VP_INDEX:

and as you pointed out that works just by luck, as soon as we there would be out of order created CPUs returned value won't match cpu_index.

So instead of spreading this nonsense out to QEMU, is it possible to fix kernel(kvm+guest) to use apic_id instead?

Yes, definitely. At this point let's add a new "KVM_CAP_HYPERV_SYNIC2" capability and declare the old one broken, that will make things easier.

Quoted 7 lines
Quoted 4 lines

What do we need a capability for?
Can't we just call
KVM_SET_MSRS using arch_id (== apic_id) and warn the user if it doesn't work?

warn won't solve issue, and it's called rather late in vcpu creation chain without any error propagation so it's not possible (hard) to gracefully fail cpu hotplug.

The issue is unsolvable on old kernels (and I don't think we want to prevent the VM from starting), hence the warning. But the ability to report errors gracefully on CPU hotplug is a very good reason. Good point.

Quoted 4 lines

with KVM_CAP_HYPERV_SYNIC2 we could easily turn on compat property for SYNC device (needs QOMification) that would turn on broken compat logic if capability is not present/old machine type.

I was going to propose enabling the compat logic if KVM_SET_MSRS failed, but you have a good point about KVM_SET_MSRS being called very late.

Signature · 1 line
Eduardo
#22 lore raw

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

Quoted 34 lines · Eduardo Habkost
Quoted 19 lines · Igor Mammedov
Quoted 16 lines · On Wed, 14 Jun 2017 10:22:16 -0300 Eduardo Habkost <[email protected]> wrote:
Quoted 14 lines · Paolo Bonzini
Quoted 8 lines · Igor Mammedov
Quoted 1 line

No, KVM really uses the VCPU _index_ for HV_X64_MSR_VP_INDEX:

and as you pointed out that works just by luck, as soon as we there would be out of order created CPUs returned value won't match cpu_index.

So instead of spreading this nonsense out to QEMU, is it possible to fix kernel(kvm+guest) to use apic_id instead?

Yes, definitely. At this point let's add a new "KVM_CAP_HYPERV_SYNIC2" capability and declare the old one broken, that will make things easier.

Quoted 7 lines
Quoted 4 lines

What do we need a capability for?
Can't we just call
KVM_SET_MSRS using arch_id (== apic_id) and warn the user if it doesn't work?

warn won't solve issue, and it's called rather late in vcpu creation chain without any error propagation so it's not possible (hard) to gracefully fail cpu hotplug.

The issue is unsolvable on old kernels (and I don't think we want to prevent the VM from starting), hence the warning. But the ability to report errors gracefully on CPU hotplug is a very good reason. Good point.

Quoted 1 line
Quoted 7 lines
Quoted 3 lines

with KVM_CAP_HYPERV_SYNIC2 we could easily turn on compat property for SYNC device (needs QOMification) that would turn on broken compat logic if capability is not present/old machine type.

I was going to propose enabling the compat logic if KVM_SET_MSRS failed, but you have a good point about KVM_SET_MSRS being called very late.

Thanks for this discussion, I didn't realize all the implications (including that hotplug issue too).

One more data point is that until now there was no use for vp_index in QEMU, so it didn't care how KVM managed it. In KVM the only vp_index-aware path that the guests could trigger was exactly reading of HV_X64_MSR_VP_INDEX.

So let me try to sum up (to make sure I understand it right);

1) we add KVM_CAP_HYPERV_SYNIC2 to KVM; when QEMU enables it KVM switches to using vcpu_id as vp_index and stops zeroing synic pages

2) new QEMU refuses to start in non-compat mode when KVM_CAP_HYPERV_SYNIC2 is not supported

3) old QEMU or new QEMU in compat mode enables KVM_CAP_HYPERV_SYNIC making KVM keep using internal cpu index as vp_index and zeroing synic pages

4) new QEMU in compat mode refuses vmbus or sint route creation

Is this what is proposed? My only problem here is that KVM will have to somehow guarantee stable numbering in the old synic mode. How can this be ensured? And should it be at all?

Thanks,
Roman.

#23 lore raw

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

Quoted 51 lines · Roman Kagan
Quoted 34 lines · Eduardo Habkost
Quoted 19 lines · Igor Mammedov
Quoted 16 lines · On Wed, 14 Jun 2017 10:22:16 -0300 Eduardo Habkost <[email protected]> wrote:
Quoted 14 lines · Paolo Bonzini
Quoted 8 lines · Igor Mammedov
Quoted 1 line

No, KVM really uses the VCPU _index_ for HV_X64_MSR_VP_INDEX:

and as you pointed out that works just by luck, as soon as we there would be out of order created CPUs returned value won't match cpu_index.

So instead of spreading this nonsense out to QEMU, is it possible to fix kernel(kvm+guest) to use apic_id instead?

Yes, definitely. At this point let's add a new "KVM_CAP_HYPERV_SYNIC2" capability and declare the old one broken, that will make things easier.

Quoted 7 lines
Quoted 4 lines

What do we need a capability for?
Can't we just call
KVM_SET_MSRS using arch_id (== apic_id) and warn the user if it doesn't work?

warn won't solve issue, and it's called rather late in vcpu creation chain without any error propagation so it's not possible (hard) to gracefully fail cpu hotplug.

The issue is unsolvable on old kernels (and I don't think we want to prevent the VM from starting), hence the warning. But the ability to report errors gracefully on CPU hotplug is a very good reason. Good point.

Quoted 1 line
Quoted 7 lines
Quoted 3 lines

with KVM_CAP_HYPERV_SYNIC2 we could easily turn on compat property for SYNC device (needs QOMification) that would turn on broken compat logic if capability is not present/old machine type.

I was going to propose enabling the compat logic if KVM_SET_MSRS failed, but you have a good point about KVM_SET_MSRS being called very late.

Thanks for this discussion, I didn't realize all the implications (including that hotplug issue too).

One more data point is that until now there was no use for vp_index in QEMU, so it didn't care how KVM managed it. In KVM the only vp_index-aware path that the guests could trigger was exactly reading of HV_X64_MSR_VP_INDEX.

Quoted 4 lines

So let me try to sum up (to make sure I understand it right);

1) we add KVM_CAP_HYPERV_SYNIC2 to KVM; when QEMU enables it KVM switches to using vcpu_id as vp_index and stops zeroing synic pages

If we want to keep KVM code simpler, we could make QEMU explicitly initialize vp_index using vcpu_id (== arch_id == apic_id) if KVM_CAP_HYPERV_SYNIC2 is reported as supported.

Quoted 3 lines

2) new QEMU refuses to start in non-compat mode when KVM_CAP_HYPERV_SYNIC2 is not supported

It depends on which cases are considered "in non-compat mode". Getting a VM not runnable just because the machine-type was updated is not desirable, especially considering that on most cases we will create the VCPUs on the same order and things would keep working. Probably the best we can do on this case is to automatically enable compat mode, but print a warning saying future QEMU versions might break if the kernel is not upgraded.

Quoted 4 lines

3) old QEMU or new QEMU in compat mode enables KVM_CAP_HYPERV_SYNIC making KVM keep using internal cpu index as vp_index and zeroing synic pages

Maybe we need a warning on this case too, because QEMU won't guarantee it will always create VCPUs in the same order in the future.

Quoted 2 lines

4) new QEMU in compat mode refuses vmbus or sint route creation

Also: new QEMU in compat mode refuses CPU hotplug.

Quoted 4 lines

Is this what is proposed? My only problem here is that KVM will have to somehow guarantee stable numbering in the old synic mode. How can this be ensured? And should it be at all?

As far as I can see, KVM can only guarantee stable numbering in the old mode if QEMU creates the VCPUs in the same order. QEMU can do a reasonable effort to not break that unnecessarily, but users should be aware that old mode is deprecated and can break.

Signature · 1 line
Eduardo
#24 lore raw

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

Quoted 18 lines · Eduardo Habkost
Quoted 9 lines · Roman Kagan

One more data point is that until now there was no use for vp_index in QEMU, so it didn't care how KVM managed it. In KVM the only vp_index-aware path that the guests could trigger was exactly reading of HV_X64_MSR_VP_INDEX.

So let me try to sum up (to make sure I understand it right);

1) we add KVM_CAP_HYPERV_SYNIC2 to KVM; when QEMU enables it KVM switches to using vcpu_id as vp_index and stops zeroing synic pages

If we want to keep KVM code simpler, we could make QEMU explicitly initialize vp_index using vcpu_id (== arch_id == apic_id) if KVM_CAP_HYPERV_SYNIC2 is reported as supported.

Quoted 2 lines

2) new QEMU refuses to start in non-compat mode when KVM_CAP_HYPERV_SYNIC2 is not supported

Quoted 7 lines

It depends on which cases are considered "in non-compat mode". Getting a VM not runnable just because the machine-type was updated is not desirable, especially considering that on most cases we will create the VCPUs on the same order and things would keep working. Probably the best we can do on this case is to automatically enable compat mode, but print a warning saying future QEMU versions might break if the kernel is not upgraded.

Anything that specifies hv_synic can be broken. There was really no reason to specify it for anything except experimenting.

So QEMU never has to enable KVM_CAP_HYPERV_SYNIC. No compat mode is necessary. In fact, I don't see any reason to _keep_ KVM_CAP_HYPERV_SYNIC in the kernel, either. This means that KVM can also switch unconditionally the vp_index to vcpu_id, because old QEMU + new kernel won't even start.

Paolo

#25 lore raw

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

Quoted 27 lines · Paolo Bonzini
Quoted 18 lines · Eduardo Habkost
Quoted 9 lines · Roman Kagan

One more data point is that until now there was no use for vp_index in QEMU, so it didn't care how KVM managed it. In KVM the only vp_index-aware path that the guests could trigger was exactly reading of HV_X64_MSR_VP_INDEX.

So let me try to sum up (to make sure I understand it right);

1) we add KVM_CAP_HYPERV_SYNIC2 to KVM; when QEMU enables it KVM switches to using vcpu_id as vp_index and stops zeroing synic pages

If we want to keep KVM code simpler, we could make QEMU explicitly initialize vp_index using vcpu_id (== arch_id == apic_id) if KVM_CAP_HYPERV_SYNIC2 is reported as supported.

Quoted 2 lines

2) new QEMU refuses to start in non-compat mode when KVM_CAP_HYPERV_SYNIC2 is not supported

Quoted 7 lines

It depends on which cases are considered "in non-compat mode". Getting a VM not runnable just because the machine-type was updated is not desirable, especially considering that on most cases we will create the VCPUs on the same order and things would keep working. Probably the best we can do on this case is to automatically enable compat mode, but print a warning saying future QEMU versions might break if the kernel is not upgraded.

Quoted 2 lines

Anything that specifies hv_synic can be broken. There was really no reason to specify it for anything except experimenting.

Hyper-V SynIC timers depend on it, and they work since QEMU 2.6 / KVM 4.5 and even supported by libvirt since 1.3.3.

Quoted 5 lines

So QEMU never has to enable KVM_CAP_HYPERV_SYNIC. No compat mode is necessary. In fact, I don't see any reason to _keep_ KVM_CAP_HYPERV_SYNIC in the kernel, either. This means that KVM can also switch unconditionally the vp_index to vcpu_id, because old QEMU + new kernel won't even start.

I'm afraid this is too harsh on users...

Roman.

#26 lore raw

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

Quoted 28 lines · Roman Kagan
Quoted 27 lines · Paolo Bonzini
Quoted 25 lines · Eduardo Habkost
Quoted 9 lines · Roman Kagan

One more data point is that until now there was no use for vp_index in QEMU, so it didn't care how KVM managed it. In KVM the only vp_index-aware path that the guests could trigger was exactly reading of HV_X64_MSR_VP_INDEX.

So let me try to sum up (to make sure I understand it right);

1) we add KVM_CAP_HYPERV_SYNIC2 to KVM; when QEMU enables it KVM switches to using vcpu_id as vp_index and stops zeroing synic pages

If we want to keep KVM code simpler, we could make QEMU explicitly initialize vp_index using vcpu_id (== arch_id == apic_id) if KVM_CAP_HYPERV_SYNIC2 is reported as supported.

Quoted 2 lines

2) new QEMU refuses to start in non-compat mode when KVM_CAP_HYPERV_SYNIC2 is not supported

It depends on which cases are considered "in non-compat mode". Getting a VM not runnable just because the machine-type was updated is not desirable, especially considering that on most cases we will create the VCPUs on the same order and things would keep working. Probably the best we can do on this case is to automatically enable compat mode, but print a warning saying future QEMU versions might break if the kernel is not upgraded.

Quoted 5 lines
Quoted 2 lines

Anything that specifies hv_synic can be broken. There was really no reason to specify it for anything except experimenting.

Hyper-V SynIC timers depend on it, and they work since QEMU 2.6 / KVM 4.5 and even supported by libvirt since 1.3.3.

But who is using them, and why would they be doing that? What is the advantage of using SynIC timers?

Paolo

Quoted 10 lines
Quoted 5 lines

So QEMU never has to enable KVM_CAP_HYPERV_SYNIC. No compat mode is necessary. In fact, I don't see any reason to _keep_ KVM_CAP_HYPERV_SYNIC in the kernel, either. This means that KVM can also switch unconditionally the vp_index to vcpu_id, because old QEMU + new kernel won't even start.

I'm afraid this is too harsh on users...

Roman.

#27 lore raw

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

Quoted 31 lines · Paolo Bonzini
Quoted 28 lines · Roman Kagan
Quoted 27 lines · Paolo Bonzini
Quoted 25 lines · Eduardo Habkost
Quoted 9 lines · Roman Kagan

One more data point is that until now there was no use for vp_index in QEMU, so it didn't care how KVM managed it. In KVM the only vp_index-aware path that the guests could trigger was exactly reading of HV_X64_MSR_VP_INDEX.

So let me try to sum up (to make sure I understand it right);

1) we add KVM_CAP_HYPERV_SYNIC2 to KVM; when QEMU enables it KVM switches to using vcpu_id as vp_index and stops zeroing synic pages

If we want to keep KVM code simpler, we could make QEMU explicitly initialize vp_index using vcpu_id (== arch_id == apic_id) if KVM_CAP_HYPERV_SYNIC2 is reported as supported.

Quoted 2 lines

2) new QEMU refuses to start in non-compat mode when KVM_CAP_HYPERV_SYNIC2 is not supported

It depends on which cases are considered "in non-compat mode". Getting a VM not runnable just because the machine-type was updated is not desirable, especially considering that on most cases we will create the VCPUs on the same order and things would keep working. Probably the best we can do on this case is to automatically enable compat mode, but print a warning saying future QEMU versions might break if the kernel is not upgraded.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Anything that specifies hv_synic can be broken. There was really no reason to specify it for anything except experimenting.

Hyper-V SynIC timers depend on it, and they work since QEMU 2.6 / KVM 4.5 and even supported by libvirt since 1.3.3.

But who is using them, and why would they be doing that? What is the advantage of using SynIC timers?

I guess because they are lighter-weight than HPET, and Windows used to ignore apic timer so it would choose SynIC timers if available. Dunno...

Roman.

#28 lore raw

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

In reply to Igor Mammedov
Quoted 23 lines · Igor Mammedov
Quoted 20 lines · On Wed, 14 Jun 2017 10:01:49 -0300 Eduardo Habkost <[email protected]> wrote:
Quoted 19 lines · Paolo Bonzini
Quoted 15 lines · Roman Kagan
Quoted 3 lines

The problem with that is that it will break as soon as we create VCPUs in a different order. Unsolvable on hosts that don't allow HV_X64_MSR_VP_INDEX to be set, however.

Right, thanks for putting together a detailed explanation.

This was a thinko back then, not to have HV_X64_MSR_VP_INDEX maintained by QEMU. I'm going to post a patch to KVM fixing that.

Meanwhile QEMU needs a way to maintain its notion of vp_index that is
1) in sync with kernel's notion
2) also with kernels that don't support setting the msr
3) persistent across migrations

cpu_index looked like a perfect candidate.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

What you want is the APIC id, which _is_ cpu_index but may not be in the future. But the APIC id is also the KVM vcpu_id, so there's no need to have VP_INDEX maintained by QEMU.

No, KVM really uses the VCPU _index_ for HV_X64_MSR_VP_INDEX:

and as you pointed out that works just by luck, as soon as we there would be out of order created CPUs returned value won't match cpu_index.

This is true, which makes it even worse.

Quoted 3 lines

So instead of spreading this nonsense out to QEMU, is it possible to fix kernel(kvm+guest) to use apic_id instead?

It is possible to fix the kernel, but if we want to support older kernels, QEMU has no choice but creating the VCPUs always in the same order.

Signature · 1 line
Eduardo
#29 lore raw

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

In reply to Roman Kagan
Quoted 61 lines · Roman Kagan
Quoted 51 lines · Eduardo Habkost
Quoted 9 lines · Roman Kagan

Hyper-V identifies vcpus by the virtual processor (VP) index which is normally queried by the guest via HV_X64_MSR_VP_INDEX msr.

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

However, the current implementation in KVM doesn't allow to set this msr, and KVM uses its own notion of VP index. Fortunately, the way vcpus are created in QEMU/KVM basically guarantees that the KVM value is equal to QEMU cpu_index.

This might not be true in the future. cpu_index is not a good identifier for CPUs, and we would like to remove it in the future.

But it looks like we have no choice, see extra comments below:

Quoted 7 lines
target/i386/kvm.c +7 -0
+static void hyperv_set_vp_index(CPUState *cs)
+{
+    struct {
+        struct kvm_msrs info;
+        struct kvm_msr_entry entries[1];
+    } msr_data;
+    int ret;

target/i386/kvm.c static int kvm_arch_set_tsc_khz(CPUState *cs) · line 629

Quoted 10 lines
target/i386/kvm.c +10 -0
+
+    msr_data.info.nmsrs = 1;
+    msr_data.entries[0].index = HV_X64_MSR_VP_INDEX;
+
+    /*
+     * Some kernels don't support setting HV_X64_MSR_VP_INDEX.  However,
+     * they use VP_INDEX equal to cpu_index due to the way vcpus are created.
+     * So ignore the errors from SET_MSRS, but verify that GET_MSRS returns the
+     * expected value.
+     */

Oh. This sounds like a problem. As reference for others, this is the KVM code in kvm_hv_get_msr():

	case HV_X64_MSR_VP_INDEX: {
		int r;
		struct kvm_vcpu *v;

		kvm_for_each_vcpu(r, v, vcpu->kvm) {
			if (v == vcpu) {
				data = r;
				break;
			}
		}
		break;
	}
Quoted 3 lines

The problem with that is that it will break as soon as we create VCPUs in a different order. Unsolvable on hosts that don't allow HV_X64_MSR_VP_INDEX to be set, however.

Right, thanks for putting together a detailed explanation.

This was a thinko back then, not to have HV_X64_MSR_VP_INDEX maintained by QEMU. I'm going to post a patch to KVM fixing that.

Quoted 6 lines

Meanwhile QEMU needs a way to maintain its notion of vp_index that is
1) in sync with kernel's notion
2) also with kernels that don't support setting the msr
3) persistent across migrations

cpu_index looked like a perfect candidate.

I would like to be able to stop using cpu_index as a persistent VCPU identifier in the future. I would also like to be able to create VCPUs in any order without breaking guest ABI. But it seems to be impossible to do that without breaking vp_index on older kernels.

So cpu_index looks like the only candidate we have.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
target/i386/kvm.c +5 -0
+    msr_data.entries[0].data = cs->cpu_index;
+    kvm_vcpu_ioctl(cs, KVM_SET_MSRS, &msr_data);
+    ret = kvm_vcpu_ioctl(cs, KVM_GET_MSRS, &msr_data);
+    assert(ret == 1);
+    assert(msr_data.entries[0].data == cs->cpu_index);
Quoted 8 lines
Quoted 3 lines

If KVM already initializes the MSR to cpu_index and we will abort if it was not set to anything except cpu_index here, why exactly do we need the KVM_SET_MSRS call?

The kernel made no obligations to keep initializing its vp_index identically to QEMU's cpu_index. So we'd better check and abort if that got out of sync. Once KVM gains the ability to learn vp_index from QEMU we'll no longer depend on that as we'll do explicit synchronization.

I think the kernel now has an obligation to keep initializing HV_X64_MSR_VP_INDEX in a compatible way, or it will break older QEMU versions that don't set the MSR.

But if you don't think we can rely on that, then the KVM_SET_MSRS call won't hurt.

Signature · 1 line
Eduardo
#30 lore raw

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

Quoted 93 lines · On Wed, 14 Jun 2017 10:00:15 -0300 Eduardo Habkost <[email protected]> wrote:
Quoted 61 lines · Roman Kagan
Quoted 51 lines · Eduardo Habkost
Quoted 9 lines · Roman Kagan

Hyper-V identifies vcpus by the virtual processor (VP) index which is normally queried by the guest via HV_X64_MSR_VP_INDEX msr.

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

However, the current implementation in KVM doesn't allow to set this msr, and KVM uses its own notion of VP index. Fortunately, the way vcpus are created in QEMU/KVM basically guarantees that the KVM value is equal to QEMU cpu_index.

This might not be true in the future. cpu_index is not a good identifier for CPUs, and we would like to remove it in the future.

But it looks like we have no choice, see extra comments below:

Quoted 7 lines
target/i386/kvm.c +7 -0
+static void hyperv_set_vp_index(CPUState *cs)
+{
+    struct {
+        struct kvm_msrs info;
+        struct kvm_msr_entry entries[1];
+    } msr_data;
+    int ret;

target/i386/kvm.c static int kvm_arch_set_tsc_khz(CPUState *cs) · line 629

Quoted 10 lines
target/i386/kvm.c +10 -0
+
+    msr_data.info.nmsrs = 1;
+    msr_data.entries[0].index = HV_X64_MSR_VP_INDEX;
+
+    /*
+     * Some kernels don't support setting HV_X64_MSR_VP_INDEX.  However,
+     * they use VP_INDEX equal to cpu_index due to the way vcpus are created.
+     * So ignore the errors from SET_MSRS, but verify that GET_MSRS returns the
+     * expected value.
+     */  

Oh. This sounds like a problem. As reference for others, this is the KVM code in kvm_hv_get_msr():

	case HV_X64_MSR_VP_INDEX: {
		int r;
		struct kvm_vcpu *v;

		kvm_for_each_vcpu(r, v, vcpu->kvm) {
			if (v == vcpu) {
				data = r;
				break;
			}
		}
		break;
	}
Quoted 3 lines

The problem with that is that it will break as soon as we create VCPUs in a different order. Unsolvable on hosts that don't allow HV_X64_MSR_VP_INDEX to be set, however.

Right, thanks for putting together a detailed explanation.

This was a thinko back then, not to have HV_X64_MSR_VP_INDEX maintained by QEMU. I'm going to post a patch to KVM fixing that.

Quoted 6 lines

Meanwhile QEMU needs a way to maintain its notion of vp_index that is
1) in sync with kernel's notion
2) also with kernels that don't support setting the msr
3) persistent across migrations

cpu_index looked like a perfect candidate.

I would like to be able to stop using cpu_index as a persistent VCPU identifier in the future. I would also like to be able to create VCPUs in any order without breaking guest ABI. But it seems to be impossible to do that without breaking vp_index on older kernels.

So cpu_index looks like the only candidate we have.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
target/i386/kvm.c +5 -0
+    msr_data.entries[0].data = cs->cpu_index;
+    kvm_vcpu_ioctl(cs, KVM_SET_MSRS, &msr_data);
+    ret = kvm_vcpu_ioctl(cs, KVM_GET_MSRS, &msr_data);
+    assert(ret == 1);
+    assert(msr_data.entries[0].data == cs->cpu_index);  
Quoted 8 lines
Quoted 3 lines

If KVM already initializes the MSR to cpu_index and we will abort if it was not set to anything except cpu_index here, why exactly do we need the KVM_SET_MSRS call?

The kernel made no obligations to keep initializing its vp_index identically to QEMU's cpu_index. So we'd better check and abort if that got out of sync. Once KVM gains the ability to learn vp_index from QEMU we'll no longer depend on that as we'll do explicit synchronization.

Quoted 6 lines

I think the kernel now has an obligation to keep initializing HV_X64_MSR_VP_INDEX in a compatible way, or it will break older QEMU versions that don't set the MSR.

But if you don't think we can rely on that, then the KVM_SET_MSRS call won't hurt.

if we can tell apart old index based vp_index kernel and new that supports
MSR setting (add cap check perhaps) then we should be able to
- leave out old vp_index broken as it is now (for old kernels/old QEMU and/ new QEMU old machine types) i.e. do not set vp_index MSR in QEMU
- in case of (new QEMU new machine type/new kernel) use APIC ID which would work
with out of order CPU creation

#31 lore raw

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

Quoted 103 lines · Igor Mammedov
Quoted 93 lines · On Wed, 14 Jun 2017 10:00:15 -0300 Eduardo Habkost <[email protected]> wrote:
Quoted 61 lines · Roman Kagan
Quoted 51 lines · Eduardo Habkost
Quoted 9 lines · Roman Kagan

Hyper-V identifies vcpus by the virtual processor (VP) index which is normally queried by the guest via HV_X64_MSR_VP_INDEX msr.

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

However, the current implementation in KVM doesn't allow to set this msr, and KVM uses its own notion of VP index. Fortunately, the way vcpus are created in QEMU/KVM basically guarantees that the KVM value is equal to QEMU cpu_index.

This might not be true in the future. cpu_index is not a good identifier for CPUs, and we would like to remove it in the future.

But it looks like we have no choice, see extra comments below:

Quoted 7 lines
target/i386/kvm.c +7 -0
+static void hyperv_set_vp_index(CPUState *cs)
+{
+    struct {
+        struct kvm_msrs info;
+        struct kvm_msr_entry entries[1];
+    } msr_data;
+    int ret;

target/i386/kvm.c static int kvm_arch_set_tsc_khz(CPUState *cs) · line 629

Quoted 10 lines
target/i386/kvm.c +10 -0
+
+    msr_data.info.nmsrs = 1;
+    msr_data.entries[0].index = HV_X64_MSR_VP_INDEX;
+
+    /*
+     * Some kernels don't support setting HV_X64_MSR_VP_INDEX.  However,
+     * they use VP_INDEX equal to cpu_index due to the way vcpus are created.
+     * So ignore the errors from SET_MSRS, but verify that GET_MSRS returns the
+     * expected value.
+     */  

Oh. This sounds like a problem. As reference for others, this is the KVM code in kvm_hv_get_msr():

	case HV_X64_MSR_VP_INDEX: {
		int r;
		struct kvm_vcpu *v;

		kvm_for_each_vcpu(r, v, vcpu->kvm) {
			if (v == vcpu) {
				data = r;
				break;
			}
		}
		break;
	}
Quoted 3 lines

The problem with that is that it will break as soon as we create VCPUs in a different order. Unsolvable on hosts that don't allow HV_X64_MSR_VP_INDEX to be set, however.

Right, thanks for putting together a detailed explanation.

This was a thinko back then, not to have HV_X64_MSR_VP_INDEX maintained by QEMU. I'm going to post a patch to KVM fixing that.

Quoted 6 lines

Meanwhile QEMU needs a way to maintain its notion of vp_index that is
1) in sync with kernel's notion
2) also with kernels that don't support setting the msr
3) persistent across migrations

cpu_index looked like a perfect candidate.

I would like to be able to stop using cpu_index as a persistent VCPU identifier in the future. I would also like to be able to create VCPUs in any order without breaking guest ABI. But it seems to be impossible to do that without breaking vp_index on older kernels.

So cpu_index looks like the only candidate we have.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
target/i386/kvm.c +5 -0
+    msr_data.entries[0].data = cs->cpu_index;
+    kvm_vcpu_ioctl(cs, KVM_SET_MSRS, &msr_data);
+    ret = kvm_vcpu_ioctl(cs, KVM_GET_MSRS, &msr_data);
+    assert(ret == 1);
+    assert(msr_data.entries[0].data == cs->cpu_index);  
Quoted 8 lines
Quoted 3 lines

If KVM already initializes the MSR to cpu_index and we will abort if it was not set to anything except cpu_index here, why exactly do we need the KVM_SET_MSRS call?

The kernel made no obligations to keep initializing its vp_index identically to QEMU's cpu_index. So we'd better check and abort if that got out of sync. Once KVM gains the ability to learn vp_index from QEMU we'll no longer depend on that as we'll do explicit synchronization.

Quoted 6 lines

I think the kernel now has an obligation to keep initializing HV_X64_MSR_VP_INDEX in a compatible way, or it will break older QEMU versions that don't set the MSR.

But if you don't think we can rely on that, then the KVM_SET_MSRS call won't hurt.

Quoted 6 lines

if we can tell apart old index based vp_index kernel and new that supports
MSR setting (add cap check perhaps) then we should be able to
- leave out old vp_index broken as it is now (for old kernels/old QEMU and/ new QEMU old machine types) i.e. do not set vp_index MSR in QEMU
- in case of (new QEMU new machine type/new kernel) use APIC ID which would work
with out of order CPU creation

I agree with the proposal, but I don't see the need for a capability check:

Old machine-types can simply not call KVM_SET_MSRS, as you suggested.

New machine-types can just call KVM_SET_MSRS unconditionally, and warn the user in case the it fails and the APIC ID really doesn't match the index in KVM (which in practice will happen only if the user is also configuring a CPU topology explicitly).

Signature · 1 line
Eduardo
#32 lore raw

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

Quoted 114 lines · On Wed, 14 Jun 2017 10:35:36 -0300 Eduardo Habkost <[email protected]> wrote:
Quoted 103 lines · Igor Mammedov
Quoted 93 lines · On Wed, 14 Jun 2017 10:00:15 -0300 Eduardo Habkost <[email protected]> wrote:
Quoted 61 lines · Roman Kagan
Quoted 51 lines · Eduardo Habkost
Quoted 9 lines · Roman Kagan

Hyper-V identifies vcpus by the virtual processor (VP) index which is normally queried by the guest via HV_X64_MSR_VP_INDEX msr.

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

However, the current implementation in KVM doesn't allow to set this msr, and KVM uses its own notion of VP index. Fortunately, the way vcpus are created in QEMU/KVM basically guarantees that the KVM value is equal to QEMU cpu_index.

This might not be true in the future. cpu_index is not a good identifier for CPUs, and we would like to remove it in the future.

But it looks like we have no choice, see extra comments below:

Quoted 7 lines
target/i386/kvm.c +7 -0
+static void hyperv_set_vp_index(CPUState *cs)
+{
+    struct {
+        struct kvm_msrs info;
+        struct kvm_msr_entry entries[1];
+    } msr_data;
+    int ret;

target/i386/kvm.c static int kvm_arch_set_tsc_khz(CPUState *cs) · line 629

Quoted 10 lines
target/i386/kvm.c +10 -0
+
+    msr_data.info.nmsrs = 1;
+    msr_data.entries[0].index = HV_X64_MSR_VP_INDEX;
+
+    /*
+     * Some kernels don't support setting HV_X64_MSR_VP_INDEX.  However,
+     * they use VP_INDEX equal to cpu_index due to the way vcpus are created.
+     * So ignore the errors from SET_MSRS, but verify that GET_MSRS returns the
+     * expected value.
+     */    

Oh. This sounds like a problem. As reference for others, this is the KVM code in kvm_hv_get_msr():

	case HV_X64_MSR_VP_INDEX: {
		int r;
		struct kvm_vcpu *v;

		kvm_for_each_vcpu(r, v, vcpu->kvm) {
			if (v == vcpu) {
				data = r;
				break;
			}
		}
		break;
	}
Quoted 3 lines

The problem with that is that it will break as soon as we create VCPUs in a different order. Unsolvable on hosts that don't allow HV_X64_MSR_VP_INDEX to be set, however.

Right, thanks for putting together a detailed explanation.

This was a thinko back then, not to have HV_X64_MSR_VP_INDEX maintained by QEMU. I'm going to post a patch to KVM fixing that.

Quoted 6 lines

Meanwhile QEMU needs a way to maintain its notion of vp_index that is
1) in sync with kernel's notion
2) also with kernels that don't support setting the msr
3) persistent across migrations

cpu_index looked like a perfect candidate.

I would like to be able to stop using cpu_index as a persistent VCPU identifier in the future. I would also like to be able to create VCPUs in any order without breaking guest ABI. But it seems to be impossible to do that without breaking vp_index on older kernels.

So cpu_index looks like the only candidate we have.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
target/i386/kvm.c +5 -0
+    msr_data.entries[0].data = cs->cpu_index;
+    kvm_vcpu_ioctl(cs, KVM_SET_MSRS, &msr_data);
+    ret = kvm_vcpu_ioctl(cs, KVM_GET_MSRS, &msr_data);
+    assert(ret == 1);
+    assert(msr_data.entries[0].data == cs->cpu_index);    
Quoted 8 lines
Quoted 3 lines

If KVM already initializes the MSR to cpu_index and we will abort if it was not set to anything except cpu_index here, why exactly do we need the KVM_SET_MSRS call?

The kernel made no obligations to keep initializing its vp_index identically to QEMU's cpu_index. So we'd better check and abort if that got out of sync. Once KVM gains the ability to learn vp_index from QEMU we'll no longer depend on that as we'll do explicit synchronization.

Quoted 6 lines

I think the kernel now has an obligation to keep initializing HV_X64_MSR_VP_INDEX in a compatible way, or it will break older QEMU versions that don't set the MSR.

But if you don't think we can rely on that, then the KVM_SET_MSRS call won't hurt.

Quoted 6 lines

if we can tell apart old index based vp_index kernel and new that supports
MSR setting (add cap check perhaps) then we should be able to
- leave out old vp_index broken as it is now (for old kernels/old QEMU and/ new QEMU old machine types) i.e. do not set vp_index MSR in QEMU
- in case of (new QEMU new machine type/new kernel) use APIC ID which would work
with out of order CPU creation

I agree with the proposal, but I don't see the need for a capability check:

Quoted 7 lines

Old machine-types can simply not call KVM_SET_MSRS, as you suggested.

New machine-types can just call KVM_SET_MSRS unconditionally, and warn the user in case the it fails and the APIC ID really doesn't match the index in KVM (which in practice will happen only if the user is also configuring a CPU topology explicitly).

I don't know anything about hyperv so here goes question:
new machine booted on new kernel (using ACPI ID) and than migrated to new qemu on old kernel.
Will it work or break. If it breaks we need cap check and if not we probably could do without it.

#33 lore raw

PATCH 6/23 hyperv: helper to find vcpu by VP index

In reply to Roman Kagan

Locate the vcpu by its VP index (equal to QEMU cpu_index).

---
 target/i386/hyperv.h | 1 +
 target/i386/hyperv.c | 5 +++++
 2 files changed, 6 insertions(+)
target/i386/hyperv.h +1 -0
index 35da0b1..c5843c9 100644
@@ -40,5 +40,6 @@ 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 vcpu_id);
 
 #endif
target/i386/hyperv.c +5 -0
index 27de5bc..480bdfe 100644
@@ -21,6 +21,11 @@ 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;
Signature · 1 line
2.9.4
#34 lore raw

PATCH 7/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.

---
 hw/misc/hyperv_testdev.c | 113 ++++++++++++++++++++++-------------------------
 1 file changed, 52 insertions(+), 61 deletions(-)
hw/misc/hyperv_testdev.c +52 -61
index dbd7cdd..da87630 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 cpu;
+    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,73 @@ 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 cpu, 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->cpu = cpu;
+    sint_route->sint = sint;
+
+    sint_route->sint_route = kvm_hv_sint_route_create(cpu, 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 cpu, 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->cpu == cpu && 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 cpu, 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, cpu, 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 cpu, uint8_t sint)
+{
+    TestSintRoute *sint_route;
+
+    sint_route = sint_route_find(dev, cpu, 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 vcpu_id = (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, vcpu_id, 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, vcpu_id, sint);
+        break;
+    case HV_TEST_DEV_SINT_ROUTE_SET_SINT:
+        sint_route_set_sint(dev, vcpu_id, sint);
         break;
-    }
     default:
         break;
     }
@@ -139,7 +130,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
#35 lore raw

PATCH 8/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 480bdfe..9aa5ec5 100644
@@ -88,7 +88,7 @@ HvSintRoute *kvm_hv_sint_route_create(uint32_t vcpu_id, 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
#36 lore raw

PATCH 9/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 9aa5ec5..3ed5a48 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 vcpu_id, 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 vcpu_id, 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, vcpu_id, sint);
     if (gsi < 0) {
@@ -109,7 +110,7 @@ HvSintRoute *kvm_hv_sint_route_create(uint32_t vcpu_id, 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 vcpu_id, 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
#37 lore raw

PATCH 10/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 c5843c9..bf7f47b 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 vcpu_id, 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 da87630..97ea959 100644
@@ -55,7 +55,7 @@ static void sint_route_create(HypervTestDev *dev, uint8_t cpu, uint8_t sint)
     sint_route->cpu = cpu;
     sint_route->sint = sint;
 
-    sint_route->sint_route = kvm_hv_sint_route_create(cpu, sint, NULL);
+    sint_route->sint_route = kvm_hv_sint_route_create(cpu, sint, NULL, NULL);
     assert(sint_route->sint_route);
 
     QLIST_INSERT_HEAD(&dev->sint_routes, sint_route, le);
target/i386/hyperv.c +14 -2
index 3ed5a48..ac7988f 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 vcpu_id, 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 vcpu_id, 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 = vcpu_id;
     sint_route->sint = sint;
 
Signature · 1 line
2.9.4
#38 lore raw

PATCH 12/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 bc071da..ca5a32d 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(X86CPU *cpu, 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(X86CPU *cpu, 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 1b12295..929bf4f 100644
@@ -55,7 +55,7 @@ static void sint_route_create(HypervTestDev *dev, X86CPU *cpu, uint8_t sint)
     sint_route->cpu = cpu;
     sint_route->sint = sint;
 
-    sint_route->sint_route = kvm_hv_sint_route_create(cpu, sint, NULL, NULL);
+    sint_route->sint_route = hyperv_sint_route_new(cpu, 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, X86CPU *cpu, uint8_t sint)
 
     sint_route = sint_route_find(dev, cpu, 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 a150401..ae67f82 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(X86CPU *cpu, uint32_t sint,
-                                      HvSintAckClb sint_ack_clb,
-                                      void *sint_ack_clb_data)
+HvSintRoute *hyperv_sint_route_new(X86CPU *cpu, uint32_t sint,
+                                   HvSintAckClb sint_ack_clb,
+                                   void *sint_ack_clb_data)
 {
     HvSintRoute *sint_route;
     EventNotifier *ack_notifier;
@@ -130,6 +131,7 @@ HvSintRoute *kvm_hv_sint_route_create(X86CPU *cpu, 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;
 
@@ -148,8 +150,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
#39 lore raw

Re: [Qemu-devel] [PATCH 12/23] hyperv: make HvSintRoute reference-counted

Quoted 5 lines · 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.

Isn't it easier to reuse existing refcounting infrastructure here? Is it overkill to make it a QOM object? (struct Object has 40 bytes)

Signature · 1 line
Eduardo
#40 lore raw

Re: [Qemu-devel] [PATCH 12/23] hyperv: make HvSintRoute reference-counted

Quoted 10 lines · Eduardo Habkost
Quoted 5 lines · 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.

Isn't it easier to reuse existing refcounting infrastructure here? Is it overkill to make it a QOM object? (struct Object has 40 bytes)

Normally the guests use a sint route per cpu or less. So no, the space overhead is not an issue.

I also wanted to reuse regular QOM refcounting so I QOM-ified it at first. However, while hammering out the design, I found no appropriate place in the QOM hierachy where to stick these objects so we dropped the idea.

If I get your proposal right you suggest to leave it unattached instead. That should probably work; however, looking at all the boilerplate code this would entail, including OBJECT casts, I'm not sure it would save anything. Do you think it's worth reworking into QOM?

Thanks,
Roman.

#41 lore raw

Re: [Qemu-devel] [PATCH 12/23] hyperv: make HvSintRoute reference-counted

Quoted 20 lines · Roman Kagan
Quoted 10 lines · Eduardo Habkost
Quoted 5 lines · 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.

Isn't it easier to reuse existing refcounting infrastructure here? Is it overkill to make it a QOM object? (struct Object has 40 bytes)

Normally the guests use a sint route per cpu or less. So no, the space overhead is not an issue.

I also wanted to reuse regular QOM refcounting so I QOM-ified it at first. However, while hammering out the design, I found no appropriate place in the QOM hierachy where to stick these objects so we dropped the idea.

Quoted 4 lines

If I get your proposal right you suggest to leave it unattached instead. That should probably work; however, looking at all the boilerplate code this would entail, including OBJECT casts, I'm not sure it would save anything. Do you think it's worth reworking into QOM?

I just noticed I didn't reply to this, sorry. I'm also not sure it is worth reworking into QOM, so I guess it's up to you.

About placing the object in the QOM hierarchy, I don't think that would be a problem. Objects without a parent are safer, as long as reference counting is properly tracked.

About the OBJECT casts, I guess that's a common issue. I'm considering proposing making object_ref()/object_unref() macros that use OBJECT() automatically.

Signature · 1 line
Eduardo
#42 lore raw

PATCH 13/23 hyperv: qdev-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 exposes a few properties which are maintained in sync with the respecitve msrs of the parent cpu.

---
 target/i386/hyperv.h  |   4 ++
 target/i386/hyperv.c  | 131 +++++++++++++++++++++++++++++++++++++++++++++++++-
 target/i386/kvm.c     |   5 ++
 target/i386/machine.c |   9 ++++
 4 files changed, 147 insertions(+), 2 deletions(-)
target/i386/hyperv.h +4 -0
index ca5a32d..9dd5ca0 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 vcpu_id);
 
+void hyperv_synic_add(X86CPU *cpu);
+void hyperv_synic_reset(X86CPU *cpu);
+void hyperv_synic_update(X86CPU *cpu);
+
 #endif
target/i386/hyperv.c +129 -2

173 lines not on this page · raw message

target/i386/kvm.c +5 -0
index eb9cde4..433c912 100644
@@ -688,6 +688,9 @@ static int hyperv_handle_properties(CPUState *cs)
         }
         env->features[FEAT_HYPERV_EAX] |= HV_SYNTIMERS_AVAILABLE;
     }
+
+    hyperv_synic_add(cpu);
+
     return 0;
 }
 
@@ -1065,6 +1068,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 eb00b19..8022c24 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"
 
@@ -633,11 +634,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
#43 lore raw

Re: [Qemu-devel] [PATCH 13/23] hyperv: qdev-ify SynIC

Quoted 60 lines · 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 exposes a few properties which are maintained in sync with the respecitve msrs of the parent cpu.

---
 target/i386/hyperv.h  |   4 ++
 target/i386/hyperv.c  | 131 +++++++++++++++++++++++++++++++++++++++++++++++++-
 target/i386/kvm.c     |   5 ++
 target/i386/machine.c |   9 ++++
 4 files changed, 147 insertions(+), 2 deletions(-)
target/i386/hyperv.h +4 -0
index ca5a32d..9dd5ca0 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 vcpu_id);
 
+void hyperv_synic_add(X86CPU *cpu);
+void hyperv_synic_reset(X86CPU *cpu);
+void hyperv_synic_update(X86CPU *cpu);
+
 #endif
target/i386/hyperv.c +16 -1
index ae67f82..2d9e9fe 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;

target/i386/hyperv.c X86CPU *hyperv_find_vcpu(uint32_t vp_index) · line 60

Quoted 9 lines
target/i386/hyperv.c +6 -0
@@ -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;

How often this will run? I think a new X86CPU struct field would be acceptable to avoid resolving a QOM path on every hyperv exit.

Do you even need QOM for this?

Quoted 60 lines
target/i386/hyperv.c +30 -1
+}
+
+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,10 +142,13 @@ HvSintRoute *hyperv_sint_route_new(X86CPU *cpu, uint32_t sint,
                                    HvSintAckClb sint_ack_clb,
                                    void *sint_ack_clb_data)
 {
+    SynICState *synic;
     HvSintRoute *sint_route;
     EventNotifier *ack_notifier;
     int r, gsi;
 
+    synic = get_synic(cpu);
+
     sint_route = g_new0(HvSintRoute, 1);
     r = event_notifier_init(&sint_route->sint_set_notifier, false);
     if (r) {
@@ -129,7 +179,7 @@ HvSintRoute *hyperv_sint_route_new(X86CPU *cpu, 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 240

Quoted 8 lines
target/i386/hyperv.c +5 -0
@@ -183,3 +233,80 @@ 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("enabled", SynICState, enabled, false),
+    DEFINE_PROP_UINT64("msg-page-addr", SynICState, msg_page_addr, 0),
+    DEFINE_PROP_UINT64("evt-page-addr", SynICState, evt_page_addr, 0),

What do you need the QOM properties for? Are they supposed to be configurable by the user? Are they supposed to be queried from outside QEMU? On which cases?

Quoted 77 lines
target/i386/hyperv.c +72 -0
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static void synic_realize(DeviceState *dev, Error **errp)
+{
+    Object *obj = OBJECT(dev);
+    SynICState *synic = SYNIC(dev);
+
+    synic->cpu = X86_CPU(obj->parent);
+}
+
+static void synic_reset(DeviceState *dev)
+{
+    SynICState *synic = SYNIC(dev);
+    synic_update(synic);
+}
+
+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)
+{
+    Object *obj;
+
+    if (!cpu->hyperv_synic) {
+        return;
+    }
+
+    obj = object_new(TYPE_SYNIC);
+    object_property_add_child(OBJECT(cpu), "synic", obj, &error_abort);
+    object_unref(obj);
+    object_property_set_bool(obj, true, "realized", &error_abort);
+}
+
+void hyperv_synic_reset(X86CPU *cpu)
+{
+    if (!cpu->hyperv_synic) {
+        return;
+    }
+
+    device_reset(DEVICE(get_synic(cpu)));
+}
+
+void hyperv_synic_update(X86CPU *cpu)
+{
+    if (!cpu->hyperv_synic) {
+        return;
+    }
+
+    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 +0 -0
index eb9cde4..433c912 100644

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

Quoted 6 lines
target/i386/kvm.c +3 -0
@@ -688,6 +688,9 @@ static int hyperv_handle_properties(CPUState *cs)
         }
         env->features[FEAT_HYPERV_EAX] |= HV_SYNTIMERS_AVAILABLE;
     }
+
+    hyperv_synic_add(cpu);
+

The purpose of hyperv_handle_properties() is to just initialize env->features based on hyperv flags, and it might even go away some day. I suggest moving this to x86_cpu_realizefn().

Quoted 47 lines
target/i386/kvm.c +2 -0
     return 0;
 }
 
@@ -1065,6 +1068,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 eb00b19..8022c24 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"
 
@@ -633,11 +634,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
Signature · 1 line
Eduardo
#44 lore raw

Re: [Qemu-devel] [PATCH 13/23] hyperv: qdev-ify SynIC

Quoted 5 lines · Eduardo Habkost
Quoted 4 lines · 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 exposes a few properties which are maintained in sync with the respecitve msrs of the parent cpu.

[...]

Quoted 9 lines

target/i386/hyperv.c X86CPU *hyperv_find_vcpu(uint32_t vp_index) · line 60

Quoted 6 lines
target/i386/hyperv.c +6 -0
+static SynICState *get_synic(X86CPU *cpu)
+{
+    SynICState *synic =
+        SYNIC(object_resolve_path_component(OBJECT(cpu), "synic"));
+    assert(synic);
+    return synic;

How often this will run? I think a new X86CPU struct field would be acceptable to avoid resolving a QOM path on every hyperv exit.

It's only used at the points where synic state is updated: at realize/save/load/unrealize and when the guest configures synics via msrs (i.e. a few times per cpu at guest startup). None of those is performance-critical.

Quoted 1 line

Do you even need QOM for this?

I need to reach the synic from the cpu on slow paths, and IMHO the pointer on X86CPU is not justified here. Besides there are two memory regions associated with every synic (in a followup patch) and I thought it made sense to have a dedicated parent QOM node for them.

Quoted 7 lines

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

Quoted 4 lines
target/i386/hyperv.c +4 -0
+static Property synic_props[] = {
+    DEFINE_PROP_BOOL("enabled", SynICState, enabled, false),
+    DEFINE_PROP_UINT64("msg-page-addr", SynICState, msg_page_addr, 0),
+    DEFINE_PROP_UINT64("evt-page-addr", SynICState, evt_page_addr, 0),

What do you need the QOM properties for? Are they supposed to be configurable by the user?

No. Actually I wanted to define them as read-only, but I failed to find a way to do so.

Quoted 1 line

Are they supposed to be queried from outside QEMU? On which cases?

ATM I only see them as informational fields that may prove useful during debugging.

Roman.

#45 lore raw

Re: [Qemu-devel] [PATCH 13/23] hyperv: qdev-ify SynIC

Quoted 22 lines · Roman Kagan
Quoted 5 lines · Eduardo Habkost
Quoted 4 lines · 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 exposes a few properties which are maintained in sync with the respecitve msrs of the parent cpu.

[...]

Quoted 9 lines

target/i386/hyperv.c X86CPU *hyperv_find_vcpu(uint32_t vp_index) · line 60

Quoted 6 lines
target/i386/hyperv.c +6 -0
+static SynICState *get_synic(X86CPU *cpu)
+{
+    SynICState *synic =
+        SYNIC(object_resolve_path_component(OBJECT(cpu), "synic"));
+    assert(synic);
+    return synic;

How often this will run? I think a new X86CPU struct field would be acceptable to avoid resolving a QOM path on every hyperv exit.

It's only used at the points where synic state is updated: at realize/save/load/unrealize and when the guest configures synics via msrs (i.e. a few times per cpu at guest startup). None of those is performance-critical.

Quoted 6 lines
Quoted 1 line

Do you even need QOM for this?

I need to reach the synic from the cpu on slow paths, and IMHO the pointer on X86CPU is not justified here. Besides there are two memory regions associated with every synic (in a followup patch) and I thought it made sense to have a dedicated parent QOM node for them.

Makes sense.

Quoted 9 lines
Quoted 8 lines

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

Quoted 5 lines
target/i386/hyperv.c +4 -0
+static Property synic_props[] = {
+    DEFINE_PROP_BOOL("enabled", SynICState, enabled, false),
+    DEFINE_PROP_UINT64("msg-page-addr", SynICState, msg_page_addr, 0),
+    DEFINE_PROP_UINT64("evt-page-addr", SynICState, evt_page_addr, 0),

What do you need the QOM properties for? Are they supposed to be configurable by the user?

Quoted 8 lines

No. Actually I wanted to define them as read-only, but I failed to find a way to do so.

Quoted 1 line

Are they supposed to be queried from outside QEMU? On which cases?

ATM I only see them as informational fields that may prove useful during debugging.

You can use object_property_add_uint64_ptr() on instance_init to add read-only properties.

If the enabled/msg_page_addr/evt_page_addr struct fields exist only because of the properties, you can also use object_property_add() and write your own getter that will query the MSRs only when reading the property. I normally don't like custom getters/setters, but it sounds acceptable for a read-only property that's only for debugging.

Either way you choose to implement it, I would add a comment noting that the properties are there just for debugging.

BTW, would you still want to add this amount of boilerplate code just for debugging if we had a generic msr_get HMP command? There's a series in the list (submitted on April) adding msr_get and msr_set HMP commands, but it was never merged.

Signature · 1 line
Eduardo
#46 lore raw

Re: [Qemu-devel] [PATCH 13/23] hyperv: qdev-ify SynIC

Quoted 23 lines · Eduardo Habkost
Quoted 10 lines · Roman Kagan
Quoted 8 lines · Eduardo Habkost

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

Quoted 4 lines · Roman Kagan
target/i386/hyperv.c +4 -0
+static Property synic_props[] = {
+    DEFINE_PROP_BOOL("enabled", SynICState, enabled, false),
+    DEFINE_PROP_UINT64("msg-page-addr", SynICState, msg_page_addr, 0),
+    DEFINE_PROP_UINT64("evt-page-addr", SynICState, evt_page_addr, 0),

What do you need the QOM properties for? Are they supposed to be configurable by the user?

Quoted 8 lines

No. Actually I wanted to define them as read-only, but I failed to find a way to do so.

Quoted 1 line

Are they supposed to be queried from outside QEMU? On which cases?

ATM I only see them as informational fields that may prove useful during debugging.

You can use object_property_add_uint64_ptr() on instance_init to add read-only properties.

Quoted 6 lines

If the enabled/msg_page_addr/evt_page_addr struct fields exist only because of the properties, you can also use object_property_add() and write your own getter that will query the MSRs only when reading the property. I normally don't like custom getters/setters, but it sounds acceptable for a read-only property that's only for debugging.

Actually that was what I did at first, but then I decided it was useful to have the fields directly on SynICState (see followup patches).

Quoted 7 lines

Either way you choose to implement it, I would add a comment noting that the properties are there just for debugging.

BTW, would you still want to add this amount of boilerplate code just for debugging if we had a generic msr_get HMP command? There's a series in the list (submitted on April) adding msr_get and msr_set HMP commands, but it was never merged.

Yes I guess it would do. So I'm now tempted to just drop the properties, and leave the fields invisible through QOM.

Thanks,
Roman.

#47 lore raw

Re: [Qemu-devel] [PATCH 13/23] hyperv: qdev-ify SynIC

Quoted 37 lines · Roman Kagan
Quoted 23 lines · Eduardo Habkost
Quoted 10 lines · Roman Kagan
Quoted 8 lines · Eduardo Habkost

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

Quoted 4 lines · Roman Kagan
target/i386/hyperv.c +4 -0
+static Property synic_props[] = {
+    DEFINE_PROP_BOOL("enabled", SynICState, enabled, false),
+    DEFINE_PROP_UINT64("msg-page-addr", SynICState, msg_page_addr, 0),
+    DEFINE_PROP_UINT64("evt-page-addr", SynICState, evt_page_addr, 0),

What do you need the QOM properties for? Are they supposed to be configurable by the user?

Quoted 8 lines

No. Actually I wanted to define them as read-only, but I failed to find a way to do so.

Quoted 1 line

Are they supposed to be queried from outside QEMU? On which cases?

ATM I only see them as informational fields that may prove useful during debugging.

You can use object_property_add_uint64_ptr() on instance_init to add read-only properties.

Quoted 6 lines

If the enabled/msg_page_addr/evt_page_addr struct fields exist only because of the properties, you can also use object_property_add() and write your own getter that will query the MSRs only when reading the property. I normally don't like custom getters/setters, but it sounds acceptable for a read-only property that's only for debugging.

Actually that was what I did at first, but then I decided it was useful to have the fields directly on SynICState (see followup patches).

Quoted 3 lines

Either way you choose to implement it, I would add a comment noting that the properties are there just for debugging.

Quoted 7 lines
Quoted 4 lines

BTW, would you still want to add this amount of boilerplate code just for debugging if we had a generic msr_get HMP command? There's a series in the list (submitted on April) adding msr_get and msr_set HMP commands, but it was never merged.

Yes I guess it would do. So I'm now tempted to just drop the properties, and leave the fields invisible through QOM.

If you are going to keep the fields on SynICState, then you just need one object_property_add_*_ptr() line for each property, so maybe it's still worth it. It's up to you.

Signature · 1 line
Eduardo
#48 lore raw

PATCH 14/23 kvm-all: make async_safe_run_on_cpu safe on kvm too

In reply to Roman Kagan

Wrap the bulk of kvm_cpu_exec with cpu_exec_start/end, so that kvm version can also enjoy performing certain operations while all vCPUs are quiescent.

---
 kvm-all.c | 2 ++
 1 file changed, 2 insertions(+)
kvm-all.c +2 -0
index 494b925..85668fb 100644
@@ -1974,6 +1974,7 @@ int kvm_cpu_exec(CPUState *cpu)
     }
 
     qemu_mutex_unlock_iothread();
+    cpu_exec_start(cpu);
 
     do {
         MemTxAttrs attrs;
@@ -2103,6 +2104,7 @@ int kvm_cpu_exec(CPUState *cpu)
         }
     } while (ret == 0);
 
+    cpu_exec_end(cpu);
     qemu_mutex_lock_iothread();
 
     if (ret < 0) {
Signature · 1 line
2.9.4
#49 lore raw

Re: [Qemu-devel] [PATCH 14/23] kvm-all: make async_safe_run_on_cpu safe on kvm too

Quoted 23 lines · Roman Kagan

Wrap the bulk of kvm_cpu_exec with cpu_exec_start/end, so that kvm version can also enjoy performing certain operations while all vCPUs are quiescent.

---
 kvm-all.c | 2 ++
 1 file changed, 2 insertions(+)
kvm-all.c +1 -0
index 494b925..85668fb 100644
@@ -1974,6 +1974,7 @@ int kvm_cpu_exec(CPUState *cpu)
     }
 
     qemu_mutex_unlock_iothread();
+    cpu_exec_start(cpu);
 
     do {
         MemTxAttrs attrs;

kvm-all.c int kvm_cpu_exec(CPUState *cpu) · line 2110

Quoted 7 lines
kvm-all.c +1 -0
@@ -2103,6 +2104,7 @@ int kvm_cpu_exec(CPUState *cpu)
         }
     } while (ret == 0);
 
+    cpu_exec_end(cpu);
     qemu_mutex_lock_iothread();
 
     if (ret < 0) {

This patch caught my eye, I'm applying it right now.

Paolo

#50 lore raw

PATCH 15/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. Besides being more correct, it will help to work around certain lifetime issues with the current code (in a followup patch).

---
 target/i386/hyperv.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 61 insertions(+), 6 deletions(-)
target/i386/hyperv.c +61 -6
index 2d9e9fe..165133a 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"
 
@@ -26,6 +29,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"
@@ -65,6 +72,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;
 }
 
@@ -73,6 +91,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;
 }
 
@@ -83,6 +112,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;
@@ -93,11 +131,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;
@@ -111,7 +144,8 @@ int kvm_hv_handle_exit(X86CPU *cpu, struct kvm_hyperv_exit *exit)
         default:
             return -1;
         }
-        synic_update(get_synic(cpu));
+        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;
@@ -245,13 +279,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
#51 lore raw

PATCH 16/23 hyperv: map overlay pages after updating msrs

In reply to Roman Kagan

There is a design flaw in the Hyper-V SynIC implementation in KVM: when message page or event flags page is enabled by setting the corresponding msr, KVM zeroes it out. This violates the spec in general (per spec, the pages have to be overlay ones and only zeroed at cpu reset), but it's non-fatal in normal operation because the user exit happens after the page is zeroed, so it's the underlying guest page which is zeroed out, and sane guests don't depend on its contents to be preserved while it's overlaid.

However, in the case of vmstate load the overlay pages are set up before msrs are set so the contents of those pages get lost.

To work it around, avoid setting up overlay pages in .post_load. Instead, postpone it until after the msrs are pushed to KVM. As a result, KVM just zeroes out the underlying guest pages similar to how it happens during guest-initiated msr writes, which is tolerable.

---
 target/i386/kvm.c     | 8 ++++++++
 target/i386/machine.c | 9 ---------
 2 files changed, 8 insertions(+), 9 deletions(-)
target/i386/kvm.c +8 -0
index 433c912..b0b7595 100644
@@ -2761,6 +2761,14 @@ int kvm_arch_put_registers(CPUState *cpu, int level)
             return ret;
         }
     }
+    /*
+     * to work around buggy KVM which zeroes out the message and event pages in
+     * KVM_SET_MSRS handler, only map the overlay pages after kvm_put_msrs,
+     * making vmstate load work similar to guest-initiated set_msr
+     */
+    if (level >= KVM_PUT_RESET_STATE) {
+        hyperv_synic_update(x86_cpu);
+    }
 
     ret = kvm_put_tscdeadline_msr(x86_cpu);
     if (ret < 0) {
target/i386/machine.c +0 -9
index 8022c24..eb00b19 100644
@@ -7,7 +7,6 @@
 #include "hw/i386/pc.h"
 #include "hw/isa/isa.h"
 #include "migration/cpu.h"
-#include "hyperv.h"
 
 #include "sysemu/kvm.h"
 
@@ -634,19 +633,11 @@ 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
#52 lore raw

Re: [Qemu-devel] [PATCH 16/23] hyperv: map overlay pages after updating msrs

Quoted 9 lines · Roman Kagan

There is a design flaw in the Hyper-V SynIC implementation in KVM: when message page or event flags page is enabled by setting the corresponding msr, KVM zeroes it out. This violates the spec in general (per spec, the pages have to be overlay ones and only zeroed at cpu reset), but it's non-fatal in normal operation because the user exit happens after the page is zeroed, so it's the underlying guest page which is zeroed out, and sane guests don't depend on its contents to be preserved while it's overlaid.

Quoted 7 lines

However, in the case of vmstate load the overlay pages are set up before msrs are set so the contents of those pages get lost.

To work it around, avoid setting up overlay pages in .post_load. Instead, postpone it until after the msrs are pushed to KVM. As a result, KVM just zeroes out the underlying guest pages similar to how it happens during guest-initiated msr writes, which is tolerable.

Why not disable the zeroing for host-initiated MSR writes? This is pretty clearly a KVM bug, we can push it to stable kernels too.

Paolo

#53 lore raw

Re: [Qemu-devel] [PATCH 16/23] hyperv: map overlay pages after updating msrs

Quoted 15 lines · Paolo Bonzini
Quoted 12 lines · Roman Kagan

There is a design flaw in the Hyper-V SynIC implementation in KVM: when message page or event flags page is enabled by setting the corresponding msr, KVM zeroes it out. This violates the spec in general (per spec, the pages have to be overlay ones and only zeroed at cpu reset), but it's non-fatal in normal operation because the user exit happens after the page is zeroed, so it's the underlying guest page which is zeroed out, and sane guests don't depend on its contents to be preserved while it's overlaid.

However, in the case of vmstate load the overlay pages are set up before msrs are set so the contents of those pages get lost.

Quoted 7 lines
Quoted 4 lines

To work it around, avoid setting up overlay pages in .post_load. Instead, postpone it until after the msrs are pushed to KVM. As a result, KVM just zeroes out the underlying guest pages similar to how it happens during guest-initiated msr writes, which is tolerable.

Why not disable the zeroing for host-initiated MSR writes? This is pretty clearly a KVM bug, we can push it to stable kernels too.

The only problem with this is that QEMU will have no reliable way to know if the KVM it runs with has this bug fixed or not. Machines without vmbus work and even migrate fine with the current KVM despite this bug (the only user of those pages currently is synic timers which re-arm themselves and post messages regardless of zeroing). Now updating QEMU to a vmbus-enabled version without updating the kernel will make the migrations cause guest hangs.

If that is tolerable I can happily drop this patch as it complicates code a little. Distros probably won't be affected as they can make sure their kernels have this bug fixed before they roll out a vmbus-capable QEMU.

What do you think?

Thanks,
Roman.

#54 lore raw

Re: [Qemu-devel] [PATCH 16/23] hyperv: map overlay pages after updating msrs

Quoted 10 lines · Roman Kagan
Quoted 2 lines

Why not disable the zeroing for host-initiated MSR writes? This is pretty clearly a KVM bug, we can push it to stable kernels too.

The only problem with this is that QEMU will have no reliable way to know if the KVM it runs with has this bug fixed or not. Machines without vmbus work and even migrate fine with the current KVM despite this bug (the only user of those pages currently is synic timers which re-arm themselves and post messages regardless of zeroing). Now updating QEMU to a vmbus-enabled version without updating the kernel will make the migrations cause guest hangs.

Return 2 from KVM_CHECK_EXTENSION(KVM_CAP_HYPERV_SYNIC)? Then you can make new QEMU refuse to enable synic if a new kernel is not available.

Paolo

#55 lore raw

Re: [Qemu-devel] [PATCH 16/23] hyperv: map overlay pages after updating msrs

Quoted 12 lines · Paolo Bonzini
Quoted 10 lines · Roman Kagan
Quoted 2 lines

Why not disable the zeroing for host-initiated MSR writes? This is pretty clearly a KVM bug, we can push it to stable kernels too.

The only problem with this is that QEMU will have no reliable way to know if the KVM it runs with has this bug fixed or not. Machines without vmbus work and even migrate fine with the current KVM despite this bug (the only user of those pages currently is synic timers which re-arm themselves and post messages regardless of zeroing). Now updating QEMU to a vmbus-enabled version without updating the kernel will make the migrations cause guest hangs.

Quoted 2 lines

Return 2 from KVM_CHECK_EXTENSION(KVM_CAP_HYPERV_SYNIC)? Then you can make new QEMU refuse to enable synic if a new kernel is not available.

Indeed, that's a possibility.

I'll probably make it in both directions then: on KVM_ENABLE_CAP(KVM_CAP_HYPERV_SYNIC, 2) disable zeroing completely, including on guest writes, to better match Hyper-V. Or does it deserve a separate cap number?

Thanks,
Roman.

#56 lore raw

Re: [Qemu-devel] [PATCH 16/23] hyperv: map overlay pages after updating msrs

Quoted 16 lines · Roman Kagan
Quoted 12 lines · Paolo Bonzini
Quoted 10 lines · Roman Kagan
Quoted 2 lines

Why not disable the zeroing for host-initiated MSR writes? This is pretty clearly a KVM bug, we can push it to stable kernels too.

The only problem with this is that QEMU will have no reliable way to know if the KVM it runs with has this bug fixed or not. Machines without vmbus work and even migrate fine with the current KVM despite this bug (the only user of those pages currently is synic timers which re-arm themselves and post messages regardless of zeroing). Now updating QEMU to a vmbus-enabled version without updating the kernel will make the migrations cause guest hangs.

Quoted 2 lines

Return 2 from KVM_CHECK_EXTENSION(KVM_CAP_HYPERV_SYNIC)? Then you can make new QEMU refuse to enable synic if a new kernel is not available.

Quoted 6 lines

Indeed, that's a possibility.

I'll probably make it in both directions then: on KVM_ENABLE_CAP(KVM_CAP_HYPERV_SYNIC, 2) disable zeroing completely, including on guest writes, to better match Hyper-V. Or does it deserve a separate cap number?

Unfortunately kvm_vcpu_ioctl_enable_cap is not checking arguments and refusing nonzero values, so I'm a bit wary of doing that unconditionally---and I'm not sure it deserves a separate capability number.

Maybe a flag KVM_ENABLE_CAP_STRICT_ARG_CHECK in cap->flags, but I don't want you to do too much unrelated work. Keeping the zeroing on guest writes doesn't seem too bad.

Or the nuclear option, introduce KVM_CAP_HYPERV_SYNIC2 and drop the broken KVM_CAP_HYPERV_SYNIC altogether. Has its charm...

Paolo

#57 lore raw

PATCH 17/23 hyperv: add synic message delivery

In reply to Roman Kagan

Add infrastructure to deliver SynIC messages to the guest SynIC message page.

Note that KVM also may want to deliver (SynIC timer) messages to the same message slot.

The problem is that the access to a SynIC message slot is controlled by the value of its .msg_type field which indicates if the slot is being owned by the hypervisor (zero) or by the guest (non-zero).

This leaves no room for synchronizing multiple concurrent producers.

The simplest way to deal with this for both KVM and QEMU is to only deliver messages in the vcpu thread. KVM already does this; this patch makes it for QEMU, too.

Specifically,

- add a function for posting messages, which only copies the message into the staging buffer if its free, and schedules a work on the corresponding vcpu to actually deliver it to the guest slot;

- instead of a sint ack callback, set up the sint route with a message status callback. This function is called in a bh whenever there are updates to the message slot status: either the vcpu made definitive progress delivering the message from the staging buffer (succeeded or failed) or the guest issued EOM; the status is passed as an argument to the callback.

---
 target/i386/hyperv.h |   7 ++--
 target/i386/hyperv.c | 116 ++++++++++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 109 insertions(+), 14 deletions(-)
target/i386/hyperv.h +4 -3
index 9dd5ca0..fa3e988 100644
@@ -19,13 +19,12 @@
 #include "qemu/event_notifier.h"
 
 typedef struct HvSintRoute HvSintRoute;
-typedef void (*HvSintAckClb)(void *data);
+typedef void (*HvSintMsgCb)(void *data, int status);
 
 int kvm_hv_handle_exit(X86CPU *cpu, struct kvm_hyperv_exit *exit);
 
 HvSintRoute *hyperv_sint_route_new(X86CPU *cpu, uint32_t sint,
-                                   HvSintAckClb sint_ack_clb,
-                                   void *sint_ack_clb_data);
+                                   HvSintMsgCb cb, void *cb_data);
 void hyperv_sint_route_ref(HvSintRoute *sint_route);
 void hyperv_sint_route_unref(HvSintRoute *sint_route);
 
@@ -38,4 +37,6 @@ void hyperv_synic_add(X86CPU *cpu);
 void hyperv_synic_reset(X86CPU *cpu);
 void hyperv_synic_update(X86CPU *cpu);
 
+int hyperv_post_msg(HvSintRoute *sint_route, struct hyperv_message *msg);
+
 #endif
target/i386/hyperv.c +105 -11

175 lines not on this page · raw message

Signature · 1 line
2.9.4
#58 lore raw

Re: [Qemu-devel] [PATCH 17/23] hyperv: add synic message delivery

target/i386/hyperv.c static void synic_update(SynICState *synic) · line 164

Quoted 7 lines · Roman Kagan
target/i386/hyperv.c +7 -0
+    sint_route->msg_status = ret;
+    /* notify the msg originator of the progress made; if the slot was busy we
+     * set msg_pending flag in it so it will be the guest who will do EOM and
+     * trigger the notification from KVM via sint_ack_notifier */
+    if (ret != -EAGAIN) {
+        qemu_bh_schedule(sint_route->msg_bh);
+    }

It may be faster to use aio_bh_schedule_oneshot, depending on the number of devices.

Paolo

#59 lore raw

Re: [Qemu-devel] [PATCH 17/23] hyperv: add synic message delivery

Quoted 11 lines · Paolo Bonzini

target/i386/hyperv.c static void synic_update(SynICState *synic) · line 164

Quoted 7 lines · Roman Kagan
target/i386/hyperv.c +7 -0
+    sint_route->msg_status = ret;
+    /* notify the msg originator of the progress made; if the slot was busy we
+     * set msg_pending flag in it so it will be the guest who will do EOM and
+     * trigger the notification from KVM via sint_ack_notifier */
+    if (ret != -EAGAIN) {
+        qemu_bh_schedule(sint_route->msg_bh);
+    }
Quoted 2 lines

It may be faster to use aio_bh_schedule_oneshot, depending on the number of devices.

Messages aren't used on fast paths, they are only exchanged at device setup/teardown. So I cared more about readability than speed here.

Thanks,
Roman.

#60 lore raw

Re: [Qemu-devel] [PATCH 17/23] hyperv: add synic message delivery

Quoted 12 lines · Roman Kagan
Quoted 11 lines · Paolo Bonzini
Quoted 7 lines · Roman Kagan
target/i386/hyperv.c +7 -0
+    sint_route->msg_status = ret;
+    /* notify the msg originator of the progress made; if the slot was busy we
+     * set msg_pending flag in it so it will be the guest who will do EOM and
+     * trigger the notification from KVM via sint_ack_notifier */
+    if (ret != -EAGAIN) {
+        qemu_bh_schedule(sint_route->msg_bh);
+    }
Quoted 5 lines
Quoted 2 lines

It may be faster to use aio_bh_schedule_oneshot, depending on the number of devices.

Messages aren't used on fast paths, they are only exchanged at device setup/teardown. So I cared more about readability than speed here.

Then you really want to use aio_bh_schedule_oneshot, because bottom halves incur a (small) cost even when you don't use them: each iteration of the event loop visits the list of bottom halves.

Persistent bottom halves, thus, are only a good idea if they are expected to trigger very often on a busy VM. If this bottom half is only triggered at setup/teardown, it shouldn't use them.

Paolo

#61 lore raw

Re: [Qemu-devel] [PATCH 17/23] hyperv: add synic message delivery

Quoted 18 lines · Paolo Bonzini
Quoted 15 lines · Roman Kagan
Quoted 13 lines · Paolo Bonzini

target/i386/hyperv.c static void synic_update(SynICState *synic) · line 164

Quoted 7 lines · Roman Kagan
target/i386/hyperv.c +7 -0
+    sint_route->msg_status = ret;
+    /* notify the msg originator of the progress made; if the slot was busy we
+     * set msg_pending flag in it so it will be the guest who will do EOM and
+     * trigger the notification from KVM via sint_ack_notifier */
+    if (ret != -EAGAIN) {
+        qemu_bh_schedule(sint_route->msg_bh);
+    }

It may be faster to use aio_bh_schedule_oneshot, depending on the number of devices.

Quoted 6 lines
Quoted 2 lines

Messages aren't used on fast paths, they are only exchanged at device setup/teardown. So I cared more about readability than speed here.

Then you really want to use aio_bh_schedule_oneshot, because bottom halves incur a (small) cost even when you don't use them: each iteration of the event loop visits the list of bottom halves.

I didn't realize that (yes that's easy to see in the code but the API didn't suggest I needed to ;).

Quoted 3 lines

Persistent bottom halves, thus, are only a good idea if they are expected to trigger very often on a busy VM. If this bottom half is only triggered at setup/teardown, it shouldn't use them.

Thanks for pointing that out; we'll have to adjust our vmbus code too as we used a persistent bottom half for message interactions there too.

Roman.

#62 lore raw

PATCH 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.

---
 target/i386/hyperv.h |  2 ++
 target/i386/hyperv.c | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+)
target/i386/hyperv.h +2 -0
index fa3e988..a2d4304 100644
@@ -39,4 +39,6 @@ void hyperv_synic_update(X86CPU *cpu);
 
 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 0a7f9b1..dcf49e4 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"
@@ -189,6 +190,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) {
+        ret = kvm_hv_sint_route_set_sint(sint_route);
+        memory_region_set_dirty(&synic->evt_page_mr, 0,
+                                sizeof(*synic->evt_page));
+    } 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
#63 lore raw

Re: [Qemu-devel] [PATCH 18/23] hyperv: add synic event flag signaling

target/i386/hyperv.c int hyperv_post_msg(HvSintRoute *sint_route, struct hyperv_message *src_msg) · line 217

Quoted 5 lines · Roman Kagan
target/i386/hyperv.c +5 -0
+
+    if ((atomic_fetch_or(&flags[set_idx], set_mask) & set_mask) != set_mask) {
+        ret = kvm_hv_sint_route_set_sint(sint_route);
+        memory_region_set_dirty(&synic->evt_page_mr, 0,
+                                sizeof(*synic->evt_page));

I would swap the kvm_hv_sint_route_set_sint and memory_region_set_dirty lines.

Paolo

Quoted 3 lines
target/i386/hyperv.c +3 -0
+    } else {
+        ret = 0;
+    }
#64 lore raw

PATCH 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.

---
 target/i386/hyperv.h |   2 +
 target/i386/hyperv.c | 113 +++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 111 insertions(+), 4 deletions(-)
target/i386/hyperv.h +2 -0
index a2d4304..d2630ac 100644
@@ -41,4 +41,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
#65 lore raw

PATCH 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).

---
 target/i386/hyperv.h |  5 +++
 target/i386/hyperv.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 95 insertions(+)
target/i386/hyperv.h +5 -0
index d2630ac..f82c770 100644
@@ -41,6 +41,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 +90 -0

118 lines not on this page · raw message

Signature · 1 line
2.9.4
#66 lore raw

Re: [Qemu-devel] [PATCH 20/23] hyperv: process POST_MESSAGE hypercall

target/i386/hyperv.c static void async_synic_update(CPUState *cs, run_on_cpu_data data) · line 244

Quoted 10 lines · Roman Kagan
target/i386/hyperv.c +10 -0
+typedef struct MsgHandler {
+    struct rcu_head rcu;
+    QLIST_ENTRY(MsgHandler) le;
+    uint32_t conn_id;
+    HvMsgHandler handler;
+    void *data;
+} MsgHandler;
+
+static QLIST_HEAD(, MsgHandler) msg_handlers;
+static QemuMutex msg_handlers_mutex;

Maybe use the same mutex for event and message handlers?

Paolo

#67 lore raw

Re: [Qemu-devel] [PATCH 20/23] hyperv: process POST_MESSAGE hypercall

Quoted 13 lines · Paolo Bonzini

target/i386/hyperv.c static void async_synic_update(CPUState *cs, run_on_cpu_data data) · line 244

Quoted 10 lines · Roman Kagan
target/i386/hyperv.c +10 -0
+typedef struct MsgHandler {
+    struct rcu_head rcu;
+    QLIST_ENTRY(MsgHandler) le;
+    uint32_t conn_id;
+    HvMsgHandler handler;
+    void *data;
+} MsgHandler;
+
+static QLIST_HEAD(, MsgHandler) msg_handlers;
+static QemuMutex msg_handlers_mutex;

Maybe use the same mutex for event and message handlers?

Are there other benefits in it beside saving 40 bytes?

Roman.

#68 lore raw

Re: [Qemu-devel] [PATCH 20/23] hyperv: process POST_MESSAGE hypercall

Quoted 15 lines · Roman Kagan
Quoted 13 lines · Paolo Bonzini

target/i386/hyperv.c static void async_synic_update(CPUState *cs, run_on_cpu_data data) · line 244

Quoted 10 lines · Roman Kagan
target/i386/hyperv.c +10 -0
+typedef struct MsgHandler {
+    struct rcu_head rcu;
+    QLIST_ENTRY(MsgHandler) le;
+    uint32_t conn_id;
+    HvMsgHandler handler;
+    void *data;
+} MsgHandler;
+
+static QLIST_HEAD(, MsgHandler) msg_handlers;
+static QemuMutex msg_handlers_mutex;

Maybe use the same mutex for event and message handlers?

Quoted 1 line

Are there other benefits in it beside saving 40 bytes?

It's generally simpler if one module only uses one mutex, you don't have to think of the interactions. Since everything is RCU-protected on the read-side, it should not matter for performance.

Paolo

#69 lore raw

PATCH 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 | 163 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 162 insertions(+), 1 deletion(-)
hw/misc/hyperv_testdev.c +162 -1
index 929bf4f..99d6d8a 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, X86CPU *cpu, uint8_t sint)
@@ -94,6 +117,128 @@ static void sint_route_set_sint(HypervTestDev *dev, X86CPU *cpu, uint8_t sint)
     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, X86CPU *cpu,
+                            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(cpu, 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, X86CPU *cpu,
+                            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(cpu, 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)
 {
@@ -101,7 +246,9 @@ static void hv_test_dev_control(void *opaque, hwaddr addr, uint64_t data,
     uint8_t sint = data & 0xFF;
     uint8_t vcpu_id = (data >> 8ULL) & 0xFF;
     uint8_t ctl = (data >> 16ULL) & 0xFF;
+    uint8_t conn_id = (data >> 24ULL) & 0xFF;
     X86CPU *cpu = hyperv_find_vcpu(vcpu_id);
+
     assert(cpu);
 
     switch (ctl) {
@@ -114,6 +261,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, cpu, sint);
         break;
+    case HV_TEST_DEV_MSG_CONN_CREATE:
+        msg_conn_create(dev, cpu, 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, cpu, sint, conn_id);
+        break;
+    case HV_TEST_DEV_EVT_CONN_DESTROY:
+        evt_conn_destroy(dev, conn_id);
+        break;
     default:
         break;
     }
@@ -133,6 +292,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
#70 lore raw

PATCH 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
#71 lore raw

PATCH 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 f82c770..cca4723 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 99d6d8a..b5611f0 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 50386a2..ed3087b 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

A message that is not in the archive (sent off-list or not yet received): [email protected]

#73 lore raw

Re: [Qemu-devel] [PATCH 11/23] hyperv: address HvSintRoute by X86CPU pointer

Quoted 5 lines · 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.

[...]

Review static void hv_test_dev_control(void *opaque, hwaddr addr, uint64_t data, · line 102

Quoted 3 lines
(quoted diff) +0 -0
@@ -101,16 +101,18 @@ static void hv_test_dev_control(void *opaque, hwaddr addr, uint64_t data,
     uint8_t sint = data & 0xFF;
     uint8_t vcpu_id = (data >> 8ULL) & 0xFF;

vcpu_id risks being confused KVM's vcpu_id (which is the CPU APIC ID in x86). If you are already touching this code, this could be renamed to vp_index to avoid confusion.

Quoted 19 lines
(quoted diff) +5 -3
     uint8_t ctl = (data >> 16ULL) & 0xFF;
+    X86CPU *cpu = hyperv_find_vcpu(vcpu_id);
+    assert(cpu);
 
     switch (ctl) {
     case HV_TEST_DEV_SINT_ROUTE_CREATE:
-        sint_route_create(dev, vcpu_id, sint);
+        sint_route_create(dev, cpu, sint);
         break;
     case HV_TEST_DEV_SINT_ROUTE_DESTROY:
-        sint_route_destroy(dev, vcpu_id, sint);
+        sint_route_destroy(dev, cpu, sint);
         break;
     case HV_TEST_DEV_SINT_ROUTE_SET_SINT:
-        sint_route_set_sint(dev, vcpu_id, sint);
+        sint_route_set_sint(dev, cpu, sint);
         break;
     default:
         break;
Signature · 1 line
Eduardo
#74 lore raw

Re: [Qemu-devel] [PATCH 11/23] hyperv: address HvSintRoute by X86CPU pointer

Quoted 7 lines · Eduardo Habkost

Review static void hv_test_dev_control(void *opaque, hwaddr addr, uint64_t data, · line 102

Quoted 3 lines
(quoted diff) +0 -0
@@ -101,16 +101,18 @@ static void hv_test_dev_control(void *opaque, hwaddr addr, uint64_t data,
     uint8_t sint = data & 0xFF;
     uint8_t vcpu_id = (data >> 8ULL) & 0xFF;

vcpu_id risks being confused KVM's vcpu_id (which is the CPU APIC ID in x86). If you are already touching this code, this could be renamed to vp_index to avoid confusion.

Actually the VP_INDEX is _also_ the vcpu_id. Should we just document that KVM makes the VP_INDEX equal to the CPU APIC id, and adjust patch 5 accordingly?

Paolo

#75 lore raw

Re: [Qemu-devel] [PATCH 11/23] hyperv: address HvSintRoute by X86CPU pointer

Quoted 11 lines · Paolo Bonzini
Quoted 7 lines · Eduardo Habkost

Review static void hv_test_dev_control(void *opaque, hwaddr addr, uint64_t data, · line 102

Quoted 3 lines
(quoted diff) +0 -0
@@ -101,16 +101,18 @@ static void hv_test_dev_control(void *opaque, hwaddr addr, uint64_t data,
     uint8_t sint = data & 0xFF;
     uint8_t vcpu_id = (data >> 8ULL) & 0xFF;

vcpu_id risks being confused KVM's vcpu_id (which is the CPU APIC ID in x86). If you are already touching this code, this could be renamed to vp_index to avoid confusion.

Quoted 3 lines

Actually the VP_INDEX is _also_ the vcpu_id. Should we just document that KVM makes the VP_INDEX equal to the CPU APIC id, and adjust patch 5 accordingly?

I think here it makes sense to follow Eduardo's suggestion because this code doesn't need to know how VP_INDEX is defined.

The real matter is discussed in patch 5, yes.

Thanks,
Roman.

#76 lore raw

Re: [Qemu-devel] [PATCH 11/23] hyperv: address HvSintRoute by X86CPU pointer

Quoted 8 lines · Roman Kagan
Quoted 6 lines
Quoted 3 lines

vcpu_id risks being confused KVM's vcpu_id (which is the CPU APIC ID in x86). If you are already touching this code, this could be renamed to vp_index to avoid confusion.

Actually the VP_INDEX is _also_ the vcpu_id. Should we just document that KVM makes the VP_INDEX equal to the CPU APIC id, and adjust patch 5 accordingly?

I think here it makes sense to follow Eduardo's suggestion because this code doesn't need to know how VP_INDEX is defined.

Yes, I agree. Sorry if that wasn't clear---Eduardo's comment just made me notice this possible solution to the issue of patch 5.

Paolo

Quoted 1 line

The real matter is discussed in patch 5, yes.