clocksource: hyper-v: Fix hv tsc page based sched_clock for hibernation
Subsystems (4)
PATCH 1/1 clocksource: hyper-v: Fix hv tsc page based sched_clock for hibernation
read_hv_sched_clock_tsc() assumes that the Hyper-V clock counter is bigger than the variable hv_sched_clock_offset, which is cached during early boot, but depending on the timing this assumption may be false when a hibernated VM starts again (the clock counter starts from 0 again) and is resuming back (Note: hv_init_tsc_clocksource() is not called during hibernation/resume); consequently, read_hv_sched_clock_tsc() may return a negative integer (which is interpreted as a huge positive integer since the return type is u64) and new kernel messages are prefixed with huge timestamps before read_hv_sched_clock_tsc() grows big enough (which typically takes several seconds).
Fix the issue by saving the Hyper-V clock counter just before the suspend, and using it to correct the hv_sched_clock_offset in resume. Override x86_platform.save_sched_clock_state and x86_platform.restore_sched_clock_state so that we don't have to touch the common x86 code.
Note: if Invariant TSC is available, the issue doesn't happen because
1) we don't register read_hv_sched_clock_tsc() for sched clock:
See commit e5313f1c5404 ("clocksource/drivers/hyper-v: Rework clocksource and sched clock setup");
2) the common x86 code adjusts TSC similarly: see __restore_processor_state() -> tsc_verify_tsc_adjust(true) and x86_platform.restore_sched_clock_state().
--- drivers/clocksource/hyperv_timer.c | 64 +++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-)
drivers/clocksource/hyperv_timer.c +63 -1
@@ -27,7 +27,10 @@#include <asm/mshyperv.h> static struct clock_event_device __percpu *hv_clock_event; -static u64 hv_sched_clock_offset__ro_after_init ; + +/* Can have negative values, after resume from hibernation, so keep them s64 */ +static s64 hv_sched_clock_offset__read_mostly ; +static s64 hv_sched_clock_offset_saved;/* * If false, we're using the old mechanism for stimer0 interrupts
@@ -51,6 +54,9 @@ static int stimer0_irq = -1;static int stimer0_message_sint; static __maybe_unused DEFINE_PER_CPU(long, stimer0_evt); +static void (*old_save_sched_clock_state)(void ); +static void (*old_restore_sched_clock_state)(void ); +/* * Common code for stimer0 interrupts coming via Direct Mode or * as a VMbus message.
@@ -434,6 +440,39 @@ static u64 noinstr read_hv_sched_clock_tsc(void) (NSEC_PER_SEC /HV_CLOCK_HZ ); } +/* +* Hyper-V clock counter resets during hibernation. Save and restore clock +* offset during suspend/resume, while also considering the time passed +* before suspend. This is to make sure that sched_clock using hv tsc page +* based clocksource, proceeds from where it left off during suspend and +* it shows correct time for the timestamps of kernel messages after resume. +*/ +static void save_hv_clock_tsc_state (void ) +{ + hv_sched_clock_offset_saved =hv_read_reference_counter (); +} + +static void restore_hv_clock_tsc_state (void ) +{ +/* +* Time passed before suspend = hv_sched_clock_offset_saved +* - hv_sched_clock_offset (old) +* +* After Hyper-V clock counter resets, hv_sched_clock_offset needs a correction. +* +* New time = hv_read_reference_counter() (future) - hv_sched_clock_offset (new) +* New time = Time passed before suspend + hv_read_reference_counter() (future) +* - hv_read_reference_counter() (now) +* +* Solving the above two equations gives: +* +* hv_sched_clock_offset (new) = hv_sched_clock_offset (old) +* - hv_sched_clock_offset_saved +* + hv_read_reference_counter() (now)) +*/ + hv_sched_clock_offset -= hv_sched_clock_offset_saved -hv_read_reference_counter (); +} +static void suspend_hv_clock_tsc (struct clocksource *arg) {union hv_reference_tsc_msr tsc_msr;
@@ -456,6 +495,24 @@ static void resume_hv_clock_tsc(struct clocksource *arg)hv_set_msr (HV_MSR_REFERENCE_TSC , tsc_msr.as_uint64); } +/* +* Functions to override save_sched_clock_state and restore_sched_clock_state +* functions of x86_platform. The Hyper-V clock counter is reset during +* suspend-resume and the offset used to measure time needs to be +* corrected, post resume. +*/ +static void hv_save_sched_clock_state (void ) +{ +save_hv_clock_tsc_state (); +old_save_sched_clock_state (); +} + +static void hv_restore_sched_clock_state (void ) +{ +restore_hv_clock_tsc_state (); +old_restore_sched_clock_state (); +} +#ifdef HAVE_VDSO_CLOCKMODE_HVCLOCK static int hv_cs_enable (struct clocksource *cs) {
@@ -539,6 +596,11 @@ static void __init hv_init_tsc_clocksource(void) hv_read_reference_counter = read_hv_clock_tsc; + old_save_sched_clock_state = x86_platform.save_sched_clock_state; + x86_platform.save_sched_clock_state = hv_save_sched_clock_state; + old_restore_sched_clock_state = x86_platform.restore_sched_clock_state; + x86_platform.restore_sched_clock_state = hv_restore_sched_clock_state; +/* * TSC page mapping works differently in root compared to guest. * - In guest partition the guest PFN has to be passed to the
base-commit: da3ea35007d0af457a0afc87e84fddaebc4e0b63
Signature · 1 line
2.25.1
From: Naman Jain <[email protected]> Sent: Sunday, September 8, 2024 10:39 PM
Quoted 132 lines
read_hv_sched_clock_tsc()assumes that the Hyper-V clock counter is bigger than the variablehv_sched_clock_offset, which is cached during early boot, but depending on the timing this assumption may be false when a hibernated VM starts again (the clock counter starts from 0 again) and is resuming back (Note:hv_init_tsc_clocksource()is not called during hibernation/resume); consequently,read_hv_sched_clock_tsc()may return a negative integer (which is interpreted as a huge positive integer since the return type is u64) and new kernel messages are prefixed with huge timestamps beforeread_hv_sched_clock_tsc()grows big enough (which typically takes several seconds).Fix the issue by saving the Hyper-V clock counter just before the suspend, and using it to correct the
hv_sched_clock_offsetin resume. Override x86_platform.save_sched_clock_state and x86_platform.restore_sched_clock_state so that we don't have to touch the common x86 code.Note: if Invariant TSC is available, the issue doesn't happen because
1) we don't registerread_hv_sched_clock_tsc()for sched clock:
See commite5313f1c5404("clocksource/drivers/hyper-v: Rework clocksource and sched clock setup");
2) the common x86 code adjusts TSC similarly: see__restore_processor_state()->tsc_verify_tsc_adjust(true)andx86_platform.restore_sched_clock_state().Fixes 1349401ff1aa ("clocksource/drivers/hyper-v: Suspend/resume Hyper-Vclocksource for hibernation")
Co-developed-by Dexuan Cui <[email protected]>Signed-off-by Dexuan Cui <[email protected]>Signed-off-by Naman Jain <[email protected]>--- drivers/clocksource/hyperv_timer.c | 64 +++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-)drivers/clocksource/hyperv_timer.c +40
-1@@ -27,7 +27,10 @@#include <asm/mshyperv.h> static struct clock_event_device __percpu *hv_clock_event; -static u64 hv_sched_clock_offset__ro_after_init ; + +/* Can have negative values, after resume from hibernation, so keep them s64 */ +static s64 hv_sched_clock_offset__read_mostly ; +static s64 hv_sched_clock_offset_saved;/* * If false, we're using the old mechanism for stimer0 interrupts @@ -51,6 +54,9 @@ static int stimer0_irq = -1;static int stimer0_message_sint; static __maybe_unused DEFINE_PER_CPU(long, stimer0_evt); +static void (*old_save_sched_clock_state)(void ); +static void (*old_restore_sched_clock_state)(void ); +/* * Common code for stimer0 interrupts coming via Direct Mode or * as a VMbus message. @@ -434,6 +440,39 @@ static u64 noinstr read_hv_sched_clock_tsc(void) (NSEC_PER_SEC /HV_CLOCK_HZ ); } +/* +* Hyper-V clock counter resets during hibernation. Save and restore clock +* offset during suspend/resume, while also considering the time passed +* before suspend. This is to make sure that sched_clock using hv tsc page +* based clocksource, proceeds from where it left off during suspend and +* it shows correct time for the timestamps of kernel messages after resume. +*/ +static void save_hv_clock_tsc_state (void ) +{ + hv_sched_clock_offset_saved =hv_read_reference_counter (); +} + +static void restore_hv_clock_tsc_state (void ) +{ +/* +* Time passed before suspend = hv_sched_clock_offset_saved +* - hv_sched_clock_offset (old) +* +* After Hyper-V clock counter resets, hv_sched_clock_offset needs a correction. +* +* New time = hv_read_reference_counter() (future) - hv_sched_clock_offset (new) +* New time = Time passed before suspend + hv_read_reference_counter() (future) +* - hv_read_reference_counter() (now) +* +* Solving the above two equations gives: +* +* hv_sched_clock_offset (new) = hv_sched_clock_offset (old) +* - hv_sched_clock_offset_saved +* + hv_read_reference_counter() (now)) +*/ + hv_sched_clock_offset -= hv_sched_clock_offset_saved -hv_read_reference_counter (); +} +static void
suspend_hv_clock_tsc(struct clocksource *arg){drivers/clocksource/hyperv_timer.c +18
-0union hv_reference_tsc_msr tsc_msr;@@ -456,6 +495,24 @@ static void resume_hv_clock_tsc(struct clocksource *arg)hv_set_msr (HV_MSR_REFERENCE_TSC , tsc_msr.as_uint64); } +/* +* Functions to override save_sched_clock_state and restore_sched_clock_state +* functions of x86_platform. The Hyper-V clock counter is reset during +* suspend-resume and the offset used to measure time needs to be +* corrected, post resume. +*/ +static void hv_save_sched_clock_state (void ) +{ +save_hv_clock_tsc_state (); +old_save_sched_clock_state (); +} + +static void hv_restore_sched_clock_state (void ) +{ +restore_hv_clock_tsc_state (); +old_restore_sched_clock_state (); +} +#ifdef HAVE_VDSO_CLOCKMODE_HVCLOCK static int hv_cs_enable (struct clocksource *cs) {
drivers/clocksource/hyperv_timer.c static void __init hv_init_tsc_clocksource(void) · line 602
Quoted 7 lines
drivers/clocksource/hyperv_timer.c +4
-0@@ -539,6 +596,11 @@ static void __init hv_init_tsc_clocksource(void) hv_read_reference_counter = read_hv_clock_tsc; + old_save_sched_clock_state = x86_platform.save_sched_clock_state; + x86_platform.save_sched_clock_state = hv_save_sched_clock_state; + old_restore_sched_clock_state = x86_platform.restore_sched_clock_state; + x86_platform.restore_sched_clock_state = hv_restore_sched_clock_state;
This Hyper-V clocksource/timer driver has intentionally been kept instruction set architecture independent. See the comment at the top of the source code file. We've also avoided any "#ifdef x86" or similar, though it's OK to have #ifdef's on specific clock-related functionality like GENERIC_SCHED_CLOCK.
The reference to "x86_platform" violates the intended independence. The code to save-on-suspend and update-on-resume can probably stay in this module in generic form, but hooking the functions into the x86_platform function call mechanism should move to x86-specific code.
Michael
Quoted 8 lines
drivers/clocksource/hyperv_timer.c +1
-0+/* * TSC page mapping works differently in root compared to guest. * - In guest partition the guest PFN has to be passed to the base-commit:
da3ea35007d0af457a0afc87e84fddaebc4e0b63--
2.25.1
Quoted 5 lines · Michael Kelley
From: Naman Jain <[email protected]> Sent: Sunday, September 8, 2024 10:39 PM
Quoted 4 lines
read_hv_sched_clock_tsc()assumes that the Hyper-V clock counter is bigger than the variablehv_sched_clock_offset, which is cached during early boot, but depending on the timing this assumption may be false
..
Quoted 11 lines
drivers/clocksource/hyperv_timer.c static void __init hv_init_tsc_clocksource(void) · line 602
Quoted 4 lines
drivers/clocksource/hyperv_timer.c +4
-0+ old_save_sched_clock_state = x86_platform.save_sched_clock_state; + x86_platform.save_sched_clock_state = hv_save_sched_clock_state; + old_restore_sched_clock_state = x86_platform.restore_sched_clock_state; + x86_platform.restore_sched_clock_state = hv_restore_sched_clock_state;This Hyper-V clocksource/timer driver has intentionally been kept instruction set architecture independent. See the comment at the top of the source code file. We've also avoided any "#ifdef x86" or similar, though it's OK to have #ifdef's on specific clock-related functionality like
GENERIC_SCHED_CLOCK.
Quoted 7 lines
The reference to "
x86_platform" violates the intended independence. The code to save-on-suspend and update-on-resume can probably stay in this module in generic form, but hooking the functions into thex86_platformfunction call mechanism should move to x86-specific code.Michael
Thanks for the review and guidance Michael. I'll take care of it in v2.
Regards,
Naman
Quoted 9 lines
Quoted 8 lines
+
/*
* TSC page mapping works differently in root compared to guest.
* - In guest partition the guest PFN has to be passed to thebase-commit:
da3ea35007d0af457a0afc87e84fddaebc4e0b63--
2.25.1