Skip to contentSkip to conversation

RFC: Precise TSC migration

PATCH Superseded Maxim Levitsky 1 participant 3 messages Nov 30, 2020 KVM

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover RFC: Precise TSC migration

Note that to use this feature you need the kernel patches which are posted to LKML and [email protected]

Currently the feature is disabled by default, and enabled with x-precise-tsc cpu feature.

Also I changed the TSC and TSC adjust read/write code to go though a special function kvm_get_tsc/kvm_set_tsc regardless of enablement of this feature.

The side effect of this is that now we upload to the kernel the TSC_ADJUST msr only on KVM_PUT_RESET_STATE reset level.
This shouldn't matter as I don't think that qemu changes this msr on its own.

For migration I added new state field 'tsc_nsec_info', where I save the 'nsec since epoch' timestamp, which is the only new thing that was added to the migration state.

First patch in this series is temporary and it just updates the kernel headers to make qemu compile.

When the feature is merged to the kernel, a kernel header sync will bring the same changes to the qemu, making this patch unnecessary.

Best regards,
Maxim Levitsky

Maxim Levitsky (2):
Update the kernel headers for 5.10-rc5 + TSC
Implement support for precise TSC migration

 include/standard-headers/asm-x86/kvm_para.h |   1 +
 linux-headers/asm-x86/kvm.h                 |   2 +
 linux-headers/linux/kvm.h                   |  70 +++++++++-
 target/i386/cpu.c                           |   1 +
 target/i386/cpu.h                           |   4 +
 target/i386/kvm.c                           | 141 ++++++++++++++++----
 target/i386/machine.c                       |  20 +++
 7 files changed, 211 insertions(+), 28 deletions(-)
Signature · 1 line
2.26.2
#1 lore raw

PATCH 1/2 Update the kernel headers for 5.10-rc5 + TSC

---
 include/standard-headers/asm-x86/kvm_para.h |  1 +
 linux-headers/asm-x86/kvm.h                 |  2 +
 linux-headers/linux/kvm.h                   | 70 ++++++++++++++++++++-
 3 files changed, 72 insertions(+), 1 deletion(-)
include/standard-headers/asm-x86/kvm_para.h +1 -0
index 07877d3295..215d01b4ec 100644
@@ -32,6 +32,7 @@
 #define KVM_FEATURE_POLL_CONTROL	12
 #define KVM_FEATURE_PV_SCHED_YIELD	13
 #define KVM_FEATURE_ASYNC_PF_INT	14
+#define KVM_FEATURE_MSI_EXT_DEST_ID	15
 
 #define KVM_HINTS_REALTIME      0
 
linux-headers/asm-x86/kvm.h +2 -0
index 89e5f3d1bb..2a60fc6674 100644
@@ -12,6 +12,7 @@
 
 #define KVM_PIO_PAGE_OFFSET 1
 #define KVM_COALESCED_MMIO_PAGE_OFFSET 2
+#define KVM_DIRTY_LOG_PAGE_OFFSET 64
 
 #define DE_VECTOR 0
 #define DB_VECTOR 1
@@ -403,6 +404,7 @@ struct kvm_sync_regs {
 #define KVM_X86_QUIRK_LAPIC_MMIO_HOLE	   (1 << 2)
 #define KVM_X86_QUIRK_OUT_7E_INC_RIP	   (1 << 3)
 #define KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT (1 << 4)
+#define KVM_X86_QUIRK_TSC_HOST_ACCESS      (1 << 5)
 
 #define KVM_STATE_NESTED_FORMAT_VMX	0
 #define KVM_STATE_NESTED_FORMAT_SVM	1
linux-headers/linux/kvm.h +69 -1

104 lines not on this page · raw message

Signature · 1 line
2.26.2
#2 lore raw

PATCH 2/2 Implement support for precise TSC migration

In reply to Maxim Levitsky

Currently to enable it, you need to set x-precise-tsc=on for each vcpu.

---
 target/i386/cpu.c     |   1 +
 target/i386/cpu.h     |   4 ++
 target/i386/kvm.c     | 141 ++++++++++++++++++++++++++++++++++--------
 target/i386/machine.c |  20 ++++++
 4 files changed, 139 insertions(+), 27 deletions(-)
target/i386/cpu.c +1 -0
index 5a8c96072e..3c82864930 100644
@@ -7231,6 +7231,7 @@ static Property x86_cpu_properties[] = {
                      false),
     DEFINE_PROP_BOOL("x-intel-pt-auto-level", X86CPU, intel_pt_auto_level,
                      true),
+    DEFINE_PROP_BOOL("x-precise-tsc", X86CPU, precise_tsc, false),
     DEFINE_PROP_END_OF_LIST()
 };
 
target/i386/cpu.h +4 -0
index 88e8586f8f..fd355057b8 100644
@@ -1460,6 +1460,7 @@ typedef struct CPUX86State {
     uint64_t tsc_adjust;
     uint64_t tsc_deadline;
     uint64_t tsc_aux;
+    uint64_t tsc_ns_timestamp;
 
     uint64_t xcr0;
 
@@ -1743,6 +1744,9 @@ struct X86CPU {
     /* Number of physical address bits supported */
     uint32_t phys_bits;
 
+    /* Use KVM_GET_TSC_PRECISE/KVM_SET_TSC_PRECISE to access IA32_TSC */
+    bool precise_tsc;
+
     /* in order to simplify APIC support, we leave this pointer to the
        user */
     struct DeviceState *apic_state;
target/i386/kvm.c +114 -27

225 lines not on this page · raw message

target/i386/machine.c +20 -0
index 233e46bb70..4f4296a3e4 100644
@@ -1359,6 +1359,25 @@ static const VMStateDescription vmstate_msr_tsx_ctrl = {
     }
 };
 
+
+static bool tsc_info_needed(void *opaque)
+{
+    X86CPU *cpu = opaque;
+    return cpu->precise_tsc;
+}
+
+static const VMStateDescription vmstate_tsc_info = {
+    .name = "cpu/tsc_nsec_info",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .needed = tsc_info_needed,
+    .fields = (VMStateField[]) {
+        VMSTATE_UINT64(env.tsc_ns_timestamp, X86CPU),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+
 VMStateDescription vmstate_x86_cpu = {
     .name = "cpu",
     .version_id = 12,
@@ -1493,6 +1512,7 @@ VMStateDescription vmstate_x86_cpu = {
 #endif
 #ifdef CONFIG_KVM
         &vmstate_nested_state,
+        &vmstate_tsc_info,
 #endif
         &vmstate_msr_tsx_ctrl,
         NULL
Signature · 1 line
2.26.2