x86/hyperv: Initialize clockevents earlier in CPU onlining
Subsystems (5)
PATCH 1/1 x86/hyperv: Initialize clockevents earlier in CPU onlining
Hyper-V has historically initialized stimer-based clockevents late in the process of onlining a CPU because clockevents depend on stimer interrupts. In the original Hyper-V design, stimer interrupts generate a VMbus message, so the VMbus machinery must be running first, and VMbus can't be initialized until relatively late. On x86/64, LAPIC timer based clockevents are used during early initialization before VMbus and stimer-based clockevents are ready, and again during CPU offlining after the stimer clockevents have been shut down.
Unfortunately, this design creates problems when offling CPUs for hibernation or other purposes. stimer-based clockevents are shut down relatively early in the offlining process, so clockevents_unbind_device() must be used to fallback to the LAPIC-based clockevents for the remainder of the offlining process. Furthermore, the late initialization and early shutdown of stimer-based clockevents doesn't work well on ARM64 since there is no other timer like the LAPIC to fallback to. So CPU onlining and offlining doesn't work properly.
Fix this by recognizing that stimer Direct Mode is the normal path for newer versions of Hyper-V on x86/64, and the only path on other architectures. With stimer Direct Mode, stimer interrupts don't require any VMbus machinery. stimer clockevents can be initialized and shut down consistent with how it is done for other clockevent devices. While the old VMbus-based stimer interrupts must still be supported for backward compatibility on x86/64, that mode of operation can be treated as legacy.
So add a new Hyper-V stimer entry in the CPU hotplug state list, and use that new state when in Direct Mode. Update the Hyper-V clocksource driver to allocate and initialize stimer clockevents earlier during boot. Update Hyper-V initialization and the VMbus driver to use this new design. As a result, the LAPIC timer is no longer used during boot or CPU onlining/offlining and clockevents_unbind_device() is not called. But retain the old design as a legacy implementation for older versions of Hyper-V that don't support Direct Mode.
--- arch/x86/hyperv/hv_init.c | 7 ++ drivers/clocksource/hyperv_timer.c | 127 ++++++++++++++++++++++++++++++------- drivers/hv/hv.c | 4 +- drivers/hv/vmbus_drv.c | 31 ++++----- include/clocksource/hyperv_timer.h | 6 +- include/linux/cpuhotplug.h | 1 + 6 files changed, 131 insertions(+), 45 deletions(-)
arch/x86/hyperv/hv_init.c +7 -0
@@ -323,8 +323,15 @@ void __init hyperv_init(void) x86_init.pci.arch_init = hv_pci_init; +if (hv_stimer_alloc ()) + goto remove_hypercall_page; +return ; +remove_hypercall_page: + hypercall_msr.as_uint64 =0 ; +wrmsrl (HV_X64_MSR_HYPERCALL , hypercall_msr.as_uint64); + hv_hypercall_pg =NULL ; remove_cpuhp_state:cpuhp_remove_state (cpuhp); free_vp_assist_page:
drivers/clocksource/hyperv_timer.c +104 -23
202 lines not on this page · raw message
drivers/hv/hv.c +2 -2
@@ -202,7 +202,7 @@ int hv_synic_init(unsigned int cpu) {hv_synic_enable_regs (cpu); -hv_stimer_init (cpu); +hv_stimer_legacy_init (cpu,VMBUS_MESSAGE_SINT );return 0 ; }
@@ -277,7 +277,7 @@ int hv_synic_cleanup(unsigned int cpu)if (channel_found && vmbus_connection.conn_state ==CONNECTED )return -EBUSY ; -hv_stimer_cleanup (cpu); +hv_stimer_legacy_cleanup (cpu);hv_synic_disable_regs (cpu);
drivers/hv/vmbus_drv.c +14 -17
@@ -1342,10 +1342,6 @@ static int vmbus_bus_init(void)if (ret) goto err_alloc; - ret =hv_stimer_alloc (VMBUS_MESSAGE_SINT ); -if (ret <0 ) - goto err_alloc; -/* * Initialize the per-cpu interrupt state and stimer state. * Then connect to the host.
@@ -1402,9 +1398,8 @@ static int vmbus_bus_init(void) err_connect:cpuhp_remove_state (hyperv_cpuhp_online); err_cpuhp: -hv_stimer_free (); -err_alloc:hv_synic_free (); +err_alloc:hv_remove_vmbus_irq ();bus_unregister (&hv_bus);
@@ -2310,7 +2305,6 @@ static void hv_crash_handler(struct pt_regs *regs)*/ vmbus_connection.conn_state =DISCONNECTED ; cpu =smp_processor_id (); -hv_stimer_cleanup (cpu);hv_synic_cleanup (cpu);hyperv_cleanup (); };
@@ -2318,20 +2312,23 @@ static void hv_crash_handler(struct pt_regs *regs)static int hv_synic_suspend (void ) {/* -* When we reach here, all the non-boot CPUs have been offlined, and -* the stimers on them have been unbound in hv_synic_cleanup() -> +* When we reach here, all the non-boot CPUs have been offlined. +* If we're in a legacy configuration where stimer Direct Mode is +* not enabled, the stimers on the non-boot CPUs have been unbound +* in hv_synic_cleanup() -> hv_stimer_legacy_cleanup() -> * hv_stimer_cleanup() -> clockevents_unbind_device(). * -* hv_synic_suspend() only runs on CPU0 with interrupts disabled. Here -* we do not unbind the stimer on CPU0 because: 1) it's unnecessary -* because the interrupts remain disabled between syscore_suspend() -* and syscore_resume(): see create_image() and resume_target_kernel(); +* hv_synic_suspend() only runs on CPU0 with interrupts disabled. +* Here we do not call hv_stimer_legacy_cleanup() on CPU0 because: +* 1) it's unnecessary as interrupts remain disabled between +* syscore_suspend() and syscore_resume(): see create_image() and +* resume_target_kernel() * 2) the stimer on CPU0 is automatically disabled later by * syscore_suspend() -> timekeeping_suspend() -> tick_suspend() -> ... -* -> clockevents_shutdown() -> ... -> hv_ce_shutdown(); 3) a warning -* would be triggered if we call clockevents_unbind_device(), which -* may sleep, in an interrupts-disabled context. So, we intentionally -* don't call hv_stimer_cleanup(0) here. +* -> clockevents_shutdown() -> ... -> hv_ce_shutdown() +* 3) a warning would be triggered if we call +* clockevents_unbind_device(), which may sleep, in an +* interrupts-disabled context. */ hv_synic_disable_regs (0 );
include/clocksource/hyperv_timer.h +3 -3
@@ -21,10 +21,10 @@#define HV_MIN_DELTA_TICKS 1/* Routines called by the VMbus driver */ -extern int hv_stimer_alloc (int sint); +extern int hv_stimer_alloc (void );extern void hv_stimer_free (void ); -extern void hv_stimer_init (unsigned int cpu); -extern void hv_stimer_cleanup (unsigned int cpu); +extern void hv_stimer_legacy_init (unsigned int cpu,int sint); +extern void hv_stimer_legacy_cleanup (unsigned int cpu);extern void hv_stimer_global_cleanup (void );extern void hv_stimer0_isr (void );
include/linux/cpuhotplug.h +1 -0
@@ -129,6 +129,7 @@ enum cpuhp_state {CPUHP_AP_ARC_TIMER_STARTING ,CPUHP_AP_RISCV_TIMER_STARTING ,CPUHP_AP_CSKY_TIMER_STARTING , +CPUHP_AP_HYPERV_TIMER_STARTING ,CPUHP_AP_KVM_STARTING ,CPUHP_AP_KVM_ARM_VGIC_INIT_STARTING , CPUHP_AP_KVM_ARM_VGIC_STARTING,
Signature · 1 line
1.8.3.1
Dexuan Cui
maintainerQuoted 4 lines
From: Michael Kelley <[email protected]>
Sent: Sunday, October 27, 2019 7:10 PM
...Signed-off-by Michael Kelley <[email protected]>
Should we add the 2 lines:
fd1fea6834d0() removes the clockevents_unbind_device() call and this patch adds it back.
arch/x86/hyperv/hv_init.c void __init hyperv_init(void) · line 328
Quoted 9 lines
arch/x86/hyperv/hv_init.c +3
-0@@ -323,8 +323,15 @@ void __init hyperv_init(void) x86_init.pci.arch_init = hv_pci_init; +if (hv_stimer_alloc ()) + goto remove_hypercall_page; +
The error handling is imperfect here: when I force hv_stimer_alloc() to return -ENOMEM, I get a panic in hv_apic_eoi_write(). It looks it is because we have cleared the pointer 'hv_vp_assist_page' to NULL, but hv_apic_eoi_write() is still in-use.
In case hv_stimer_alloc() fails, can we set 'direct_mode_enabled' to false and go on with the legacy Hyper-V timer or LAPIC timer? If not, maybe we can use a BUG_ON() to explicitly panic?
Quoted 23 lines
arch/x86/hyperv/hv_init.c +10
-1return ; +remove_hypercall_page: + hypercall_msr.as_uint64 =0 ; +wrmsrl (HV_X64_MSR_HYPERCALL , hypercall_msr.as_uint64); + hv_hypercall_pg =NULL ; remove_cpuhp_state:cpuhp_remove_state (cpuhp); free_vp_assist_page: -void hv_stimer_cleanup(unsigned int cpu) +static int hv_stimer_cleanup(unsigned int cpu) {struct clock_event_device *ce;/* Turn off clockevent device */ if (ms_hyperv.features &HV_MSR_SYNTIMER_AVAILABLE ) { ce =per_cpu_ptr (hv_clock_event, cpu); + +/* +* In the legacy case where Direct Mode is not enabled +* (which can only be on x86/64), stimer cleanup happens +* relatively early in the CPU offlining process. We
arch/x86/hyperv/hv_init.c void __init hyperv_init(void)
Quoted 10 lines
arch/x86/hyperv/hv_init.c +9
-0+* must unbind the stimer-based clockevent device so +* that the LAPIC timer can take over until clockevents +* are no longer needed in the offlining process. The +* unbind should not be done when Direct Mode is enabled +* because we may be on an architecture where there are +* no other clockevents devices to fallback to. +*/ +if (!direct_mode_enabled) +clockevents_unbind_device (ce, cpu);hv_ce_shutdown (ce);
In the legacy stimer0 mode, IMO this hv_ce_shutdown() is unnecessary, because "clockevents_unbind_device(ce, cpu)" automatically calls ce->set_state_shutdown(), if ce is active:clockevents_unbind
__clockevents_unbind
clockevents_replace
tick_install_replacement
clockevents_exchange_device
clockevents_switch_state(old, CLOCK_EVT_STATE_DETACHED)
__clockevents_switch_state
And, in both modes (legacy mode and direct mode), it looks incorrect to call hv_ce_shutdown() if the current processid id != 'cpu', because hv_ce_shutdown() -> hv_init_timer() can only access the current CPU's MSR. Maybe we should use an IPI to run hv_ce_shutdown() on the target CPU in direct mode?
arch/x86/hyperv/hv_init.c
Quoted 11 lines
arch/x86/hyperv/hv_init.c +8
-1-int hv_stimer_alloc(int sint) +int hv_stimer_alloc (void ) + ret =cpuhp_setup_state (CPUHP_AP_HYPERV_TIMER_STARTING , +"clockevents/hyperv/stimer:starting" , + hv_stimer_init, hv_stimer_cleanup); +if (ret <0 ) + goto free_stimer0_irq; + stimer0_cpuhp = ret; } +return ret;
stimer0_cpuhp is 0 when the call is successful, so IMO the logic in hv_stimer_free() is incorrect. Please see below.
Quoted 5 lines
void
hv_stimer_free(void)
{arch/x86/hyperv/hv_init.c +0
-3-if (direct_mode_enabled && (stimer0_irq !=0 )) { -hv_remove_stimer0_irq (stimer0_irq); - stimer0_irq =0 ;
arch/x86/hyperv/hv_init.c
Quoted 10 lines
arch/x86/hyperv/hv_init.c +9
-0+if (direct_mode_enabled) { +if (stimer0_cpuhp) { +cpuhp_remove_state (stimer0_cpuhp); + stimer0_cpuhp =0 ; + } +if (stimer0_irq) { +hv_remove_stimer0_irq (stimer0_irq); + stimer0_irq =0 ; + } }
IMO this should be
if (direct_mode_enabled) {
if (stimer0_cpuhp == 0)
cpuhp_remove_state(CPUHP_AP_HYPERV_TIMER_STARTING);
if (stimer0_irq) {
hv_remove_stimer0_irq(stimer0_irq);
stimer0_irq = 0;
}
}BTW, the default value of 'stimer0_cpuhp' is 0, which means success. Should we change the default value to a non-zero value, e.g. -1 ?
Quoted 5 lines
arch/x86/hyperv/hv_init.c +0
-0free_percpu (hv_clock_event); hv_clock_event =NULL ;@@ -190,14 +274,11 @@ void hv_stimer_free(void)void hv_stimer_global_cleanup (void ) {
arch/x86/hyperv/hv_init.c void hv_stimer_free(void) · line 279
Quoted 10 lines
arch/x86/hyperv/hv_init.c +2
-6int cpu; -struct clock_event_device *ce; -if (ms_hyperv.features &HV_MSR_SYNTIMER_AVAILABLE ) { -for_each_present_cpu (cpu) { - ce =per_cpu_ptr (hv_clock_event, cpu); -clockevents_unbind_device (ce, cpu); - } +for_each_present_cpu (cpu) { +hv_stimer_cleanup (cpu);
hv_stimer_cleanup() -> hv_ce_shutdown() -> hv_init_timer() can not access a remote CPU's MSR.
arch/x86/hyperv/hv_init.c static void hv_crash_handler(struct pt_regs *regs) · line 2309
Quoted 7 lines
arch/x86/hyperv/hv_init.c +0
-1@@ -2310,7 +2305,6 @@ static void hv_crash_handler(struct pt_regs *regs)*/ vmbus_connection.conn_state =DISCONNECTED ; cpu =smp_processor_id (); -hv_stimer_cleanup (cpu);hv_synic_cleanup (cpu);hyperv_cleanup ();
Why should we remove the line hv_stimer_cleanup() in the crash handler? In the crash handler, IMO we'd better disable the timer before we proceed.
Thanks,
-- Dexuan
From: Dexuan Cui <[email protected]> Sent: Tuesday, October 29, 2019 1:34 PM
Quoted 7 lines
Should we add the 2 lines:
Fixes fd1fea6834d0 ("clocksource/drivers: Make Hyper-V clocksource ISA agnostic")
fd1fea6834d0()removes theclockevents_unbind_device()call and this patch adds it back.
I thought about this, but the changes in this patch seem more extensive than we might want to take as a stable fix. The previous removal of clockevents_unbind_device() does not have any negative effects except on the in-progress hibernation work. We really want to have this patch go with the hibernation patches, so backporting these changes to stable isn't necessary for hibernation support. But whether to backport to stable is a judgment call, and I'm open to arguments in favor.
Quoted 7 lines
Quoted 7 lines
arch/x86/hyperv/hv_init.c +0
-0@@ -323,8 +323,15 @@ void __init hyperv_init(void) x86_init.pci.arch_init = hv_pci_init;
Quoted 7 lines
arch/x86/hyperv/hv_init.c void __init hyperv_init(void) · line 328
Quoted 3 lines
arch/x86/hyperv/hv_init.c +3
-0+if (hv_stimer_alloc ()) + goto remove_hypercall_page; +The error handling is imperfect here: when I force
hv_stimer_alloc()to return-ENOMEM, I get a panic inhv_apic_eoi_write(). It looks it is because we have cleared the pointer 'hv_vp_assist_page' to NULL, buthv_apic_eoi_write()is still in-use.
This can be fixed by doing hv_stimer_alloc() before the call to hv_apic_init().
Quoted 4 lines
In case
hv_stimer_alloc()fails, can we set 'direct_mode_enabled' to false and go on with the legacy Hyper-V timer or LAPIC timer? If not, maybe we can use aBUG_ON()to explicitly panic?
I'll look into what can be done here.
Quoted 37 lines
Quoted 24 lines
arch/x86/hyperv/hv_init.c +10
-1return ; +remove_hypercall_page: + hypercall_msr.as_uint64 =0 ; +wrmsrl (HV_X64_MSR_HYPERCALL , hypercall_msr.as_uint64); + hv_hypercall_pg =NULL ; remove_cpuhp_state:cpuhp_remove_state (cpuhp); free_vp_assist_page: -void hv_stimer_cleanup(unsigned int cpu) +static int hv_stimer_cleanup(unsigned int cpu) {struct clock_event_device *ce;/* Turn off clockevent device */ if (ms_hyperv.features &HV_MSR_SYNTIMER_AVAILABLE ) { ce =per_cpu_ptr (hv_clock_event, cpu); + +/* +* In the legacy case where Direct Mode is not enabled +* (which can only be on x86/64), stimer cleanup happens +* relatively early in the CPU offlining process. We arch/x86/hyperv/hv_init.c void __init hyperv_init(void)
Quoted 10 lines
arch/x86/hyperv/hv_init.c +9
-0+* must unbind the stimer-based clockevent device so +* that the LAPIC timer can take over until clockevents +* are no longer needed in the offlining process. The +* unbind should not be done when Direct Mode is enabled +* because we may be on an architecture where there are +* no other clockevents devices to fallback to. +*/ +if (!direct_mode_enabled) +clockevents_unbind_device (ce, cpu);hv_ce_shutdown (ce);In the legacy stimer0 mode, IMO this
hv_ce_shutdown()is unnecessary, because "clockevents_unbind_device(ce, cpu)" automatically calls
Quoted 8 lines
ce->set_state_shutdown(), if ce is active:clockevents_unbind
__clockevents_unbind
clockevents_replace
tick_install_replacement
clockevents_exchange_device
clockevents_switch_state(old, CLOCK_EVT_STATE_DETACHED)
__clockevents_switch_state
Agreed. This is a carryover from even before reorganizing the code into the separate Hyper-V clocksource driver, where this code was present in hv_synic_cleanup():
/* Turn off clockevent device */
if (ms_hyperv.features & HV_MSR_SYNTIMER_AVAILABLE) {
struct hv_per_cpu_context *hv_cpu
= this_cpu_ptr(hv_context.cpu_context);
clockevents_unbind_device(hv_cpu->clk_evt, cpu);
hv_ce_shutdown(hv_cpu->clk_evt);
}But I'll fix it.
Quoted 6 lines
And, in both modes (legacy mode and direct mode), it looks incorrect to call
hv_ce_shutdown()if the current processid id != 'cpu', becausehv_ce_shutdown()->hv_init_timer()can only access the current CPU's MSR. Maybe we should use an IPI to runhv_ce_shutdown()on the target CPU in direct mode?
Agreed. I'll figure out a fix.
Quoted 43 lines
arch/x86/hyperv/hv_init.c
Quoted 12 lines
arch/x86/hyperv/hv_init.c +8
-1-int hv_stimer_alloc(int sint) +int hv_stimer_alloc (void ) + ret =cpuhp_setup_state (CPUHP_AP_HYPERV_TIMER_STARTING , +"clockevents/hyperv/stimer:starting" , + hv_stimer_init, hv_stimer_cleanup); +if (ret <0 ) + goto free_stimer0_irq; + stimer0_cpuhp = ret; } +return ret;
stimer0_cpuhpis 0 when the call is successful, so IMO the logic inhv_stimer_free()is incorrect. Please see below.Quoted 5 lines
void
hv_stimer_free(void)
{arch/x86/hyperv/hv_init.c +0
-3-if (direct_mode_enabled && (stimer0_irq !=0 )) { -hv_remove_stimer0_irq (stimer0_irq); - stimer0_irq =0 ;arch/x86/hyperv/hv_init.c
Quoted 10 lines
arch/x86/hyperv/hv_init.c +9
-0+if (direct_mode_enabled) { +if (stimer0_cpuhp) { +cpuhp_remove_state (stimer0_cpuhp); + stimer0_cpuhp =0 ; + } +if (stimer0_irq) { +hv_remove_stimer0_irq (stimer0_irq); + stimer0_irq =0 ; + } }IMO this should be
if (direct_mode_enabled) { if (stimer0_cpuhp == 0) cpuhp_remove_state(CPUHP_AP_HYPERV_TIMER_STARTING); if (stimer0_irq) { hv_remove_stimer0_irq(stimer0_irq); stimer0_irq = 0; } }
Quoted 2 lines
BTW, the default value of '
stimer0_cpuhp' is 0, which means success. Should we change the default value to a non-zero value, e.g. -1 ?
Indeed, you are correct. I was treating the return value from cphup_setup_state() like the case when the state is CPUHP_AP_ONLINE_DYN. I'll fix it.
Quoted 9 lines
Quoted 9 lines
arch/x86/hyperv/hv_init.c +0
-1free_percpu (hv_clock_event); hv_clock_event =NULL ;@@ -190,14 +274,11 @@ void hv_stimer_free(void)void hv_stimer_global_cleanup (void ) {int cpu; -struct clock_event_device *ce;
Quoted 10 lines
arch/x86/hyperv/hv_init.c void hv_stimer_free(void) · line 279
Quoted 7 lines
arch/x86/hyperv/hv_init.c +2
-5-if (ms_hyperv.features &HV_MSR_SYNTIMER_AVAILABLE ) { -for_each_present_cpu (cpu) { - ce =per_cpu_ptr (hv_clock_event, cpu); -clockevents_unbind_device (ce, cpu); - } +for_each_present_cpu (cpu) { +hv_stimer_cleanup (cpu);
hv_stimer_cleanup()->hv_ce_shutdown()->hv_init_timer()can not access a remote CPU's MSR.
Agreed, per above.
Quoted 11 lines
arch/x86/hyperv/hv_init.c static void hv_crash_handler(struct pt_regs *regs) · line 2309
Quoted 8 lines
arch/x86/hyperv/hv_init.c +0
-1@@ -2310,7 +2305,6 @@ static void hv_crash_handler(struct pt_regs *regs)*/ vmbus_connection.conn_state =DISCONNECTED ; cpu =smp_processor_id (); -hv_stimer_cleanup (cpu);hv_synic_cleanup (cpu);hyperv_cleanup ();Why should we remove the line
hv_stimer_cleanup()in the crash handler? In the crash handler, IMO we'd better disable the timer before we proceed.
With the stimer in legacy mode, the cleanup should happen in hv_synic_cleanup(). But that actually highlights a previously unrecognized problem in that hv_synic_cleanup() usually doesn't do anything because it returns -EBUSY if there is a channel interrupt assigned to "cpu". So even in the older code before the clock/timer reorg, none of the timers or other synic functionality actually got cleaned up.
With the stimer in DirectMode, the cleanup should probably happen in hyperv_cleanup(), to mirror the initialization in hyperv_init(). I'll make that change. Fixing the legacy path is probably a separate patch because we'll need fix the synic cleanup problem by distinguishing between a normal cleanup due to CPU offlining or hibernation, and the crash path.
Thanks for the careful review!
Michael