Skip to content

Commit 7f828d5

Browse files
dcuiliuw
authored andcommitted
clocksource: hyper-v: Use lapic timer in a TDX VM without paravisor
In a TDX VM without paravisor, currently the default timer is the Hyper-V timer, which depends on the slow VM Reference Counter MSR: the Hyper-V TSC page is not enabled in such a VM because the VM uses Invariant TSC as a better clocksource and it's challenging to mark the Hyper-V TSC page shared in very early boot. Lower the rating of the Hyper-V timer so the local APIC timer becomes the the default timer in such a VM, and print a warning in case Invariant TSC is unavailable in such a VM. This change should cause no perceivable performance difference. Cc: [email protected] # 6.6+ Reviewed-by: Roman Kisel <[email protected]> Signed-off-by: Dexuan Cui <[email protected]> Reviewed-by: Michael Kelley <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Wei Liu <[email protected]> Message-ID: <[email protected]>
1 parent 90ac806 commit 7f828d5

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

arch/x86/kernel/cpu/mshyperv.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,9 +449,23 @@ static void __init ms_hyperv_init_platform(void)
449449
ms_hyperv.hints &= ~HV_X64_APIC_ACCESS_RECOMMENDED;
450450

451451
if (!ms_hyperv.paravisor_present) {
452-
/* To be supported: more work is required. */
452+
/*
453+
* Mark the Hyper-V TSC page feature as disabled
454+
* in a TDX VM without paravisor so that the
455+
* Invariant TSC, which is a better clocksource
456+
* anyway, is used instead.
457+
*/
453458
ms_hyperv.features &= ~HV_MSR_REFERENCE_TSC_AVAILABLE;
454459

460+
/*
461+
* The Invariant TSC is expected to be available
462+
* in a TDX VM without paravisor, but if not,
463+
* print a warning message. The slower Hyper-V MSR-based
464+
* Ref Counter should end up being the clocksource.
465+
*/
466+
if (!(ms_hyperv.features & HV_ACCESS_TSC_INVARIANT))
467+
pr_warn("Hyper-V: Invariant TSC is unavailable\n");
468+
455469
/* HV_MSR_CRASH_CTL is unsupported. */
456470
ms_hyperv.misc_features &= ~HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE;
457471

drivers/clocksource/hyperv_timer.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,21 @@ static int hv_stimer_init(unsigned int cpu)
137137
ce->name = "Hyper-V clockevent";
138138
ce->features = CLOCK_EVT_FEAT_ONESHOT;
139139
ce->cpumask = cpumask_of(cpu);
140-
ce->rating = 1000;
140+
141+
/*
142+
* Lower the rating of the Hyper-V timer in a TDX VM without paravisor,
143+
* so the local APIC timer (lapic_clockevent) is the default timer in
144+
* such a VM. The Hyper-V timer is not preferred in such a VM because
145+
* it depends on the slow VM Reference Counter MSR (the Hyper-V TSC
146+
* page is not enbled in such a VM because the VM uses Invariant TSC
147+
* as a better clocksource and it's challenging to mark the Hyper-V
148+
* TSC page shared in very early boot).
149+
*/
150+
if (!ms_hyperv.paravisor_present && hv_isolation_type_tdx())
151+
ce->rating = 90;
152+
else
153+
ce->rating = 1000;
154+
141155
ce->set_state_shutdown = hv_ce_shutdown;
142156
ce->set_state_oneshot = hv_ce_set_oneshot;
143157
ce->set_next_event = hv_ce_set_next_event;

0 commit comments

Comments
 (0)