RFC: Precise TSC migration
v2 cover RFC: Precise TSC migration
Note that to use this feature you need the kernel patches which are posted to LKML and [email protected]
The feature is disabled by default, and can be enabled with -accel kvm,x-precise-tsc=on.
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 a new state field 'cpu/tsc_ns_timestamp', where I save the TSC nanosecond 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.
V2:
- switched to -accel for enablement
- sync with updated kernel patches
- minor cleanups, renames, etc
Best regards,
Maxim Levitsky
Maxim Levitsky (2):
Update the kernel headers for 5.10-rc5 + TSC
Implement support for precise TSC migration
accel/kvm/kvm-all.c | 28 ++++ include/standard-headers/asm-x86/kvm_para.h | 1 + include/sysemu/kvm.h | 1 + linux-headers/asm-x86/kvm.h | 2 + linux-headers/linux/kvm.h | 71 +++++++++- target/i386/cpu.h | 1 + target/i386/kvm.c | 140 ++++++++++++++++---- target/i386/machine.c | 19 +++ 8 files changed, 234 insertions(+), 29 deletions(-)
Signature · 1 line
2.26.2
PATCH v2 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 | 71 ++++++++++++++++++++- 3 files changed, 73 insertions(+), 1 deletion(-)
include/standard-headers/asm-x86/kvm_para.h +1 -0
@@ -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
@@ -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 +70 -1
105 lines not on this page · raw message
Signature · 1 line
2.26.2
PATCH v2 2/2 Implement support for precise TSC migration
In reply to Maxim LevitskyTo enable it, you need to set -accel kvm,x-precise-tsc=on, and have a kernel that supports this feature.
--- accel/kvm/kvm-all.c | 28 +++++++++ include/sysemu/kvm.h | 1 + target/i386/cpu.h | 1 + target/i386/kvm.c | 140 +++++++++++++++++++++++++++++++++--------- target/i386/machine.c | 19 ++++++ 5 files changed, 161 insertions(+), 28 deletions(-)
accel/kvm/kvm-all.c +28 -0
@@ -104,6 +104,8 @@ struct KVMStateOnOffAuto kernel_irqchip_split;bool sync_mmu;uint64_t manual_dirty_log_protect; +/* Use KVM_GET_TSC_PRECISE/KVM_SET_TSC_PRECISE to access IA32_TSC */ +bool precise_tsc;/* The man page (and posix) say ioctl numbers are signed int, but * they're not. Linux, glibc and *BSD all treat ioctl numbers as * unsigned, and treating them as signed here can break things */
@@ -3194,6 +3196,24 @@ bool kvm_kernel_irqchip_split(void)return kvm_state->kernel_irqchip_split ==ON_OFF_AUTO_ON ; } +bool kvm_has_precise_tsc (void ) +{ +return kvm_state && kvm_state->precise_tsc; +} + +static void kvm_set_precise_tsc (Object *obj, + bool value, Error **errp G_GNUC_UNUSED) +{ +KVMState *s =KVM_STATE (obj); + s->precise_tsc = value; +} + +static bool kvm_get_precise_tsc(Object *obj, Error **errp G_GNUC_UNUSED) +{ +KVMState *s =KVM_STATE (obj); +return s->precise_tsc; +} +static void kvm_accel_instance_init (Object *obj) {KVMState *s =KVM_STATE (obj);
@@ -3222,6 +3242,14 @@ static void kvm_accel_class_init(ObjectClass *oc, void *data) NULL, NULL);object_class_property_set_description (oc,"kvm-shadow-mem" ,"KVM shadow MMU size" ); + +object_class_property_add_bool (oc,"x-precise-tsc" , + kvm_get_precise_tsc, + kvm_set_precise_tsc); + +object_class_property_set_description (oc,"x-precise-tsc" , +"Use precise tsc kvm API" ); + }static const TypeInfo kvm_accel_type = {
include/sysemu/kvm.h +1 -0
@@ -519,6 +519,7 @@ void kvm_init_irq_routing(KVMState *s);bool kvm_kernel_irqchip_allowed (void );bool kvm_kernel_irqchip_required (void );bool kvm_kernel_irqchip_split (void ); +bool kvm_has_precise_tsc (void );/** * kvm_arch_irqchip_create:
target/i386/cpu.h +1 -0
@@ -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;
target/i386/kvm.c +112 -28
212 lines not on this page · raw message
target/i386/machine.c +19 -0
@@ -1359,6 +1359,24 @@ static const VMStateDescription vmstate_msr_tsx_ctrl = { } }; + +static bool tsc_ns_timestamp_needed (void *opaque) +{ +return kvm_has_precise_tsc (); +} + +static const VMStateDescription vmstate_tsc_ns_timestamp = { + .name ="cpu/tsc_ns_timestamp" , + .version_id =1 , + .minimum_version_id =1 , + .needed = tsc_ns_timestamp_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 +1511,7 @@ VMStateDescription vmstate_x86_cpu = {#endif #ifdef CONFIG_KVM &vmstate_nested_state, + &vmstate_tsc_ns_timestamp, #endif &vmstate_msr_tsx_ctrl,NULL
Signature · 1 line
2.26.2