Re: CPU Lockups in KVM with deferred hrtimer rearming

Binbin Wu
2026-04-21T04:51:46Z

On 4/20/2026 11:00 PM, Thomas Gleixner wrote:
> On Thu, Apr 16 2026 at 20:50, Vishal L. Verma wrote:
>> I tried out AI assisted and patch (below) which does happen to solve
>> it, but I'm not familiar in this area, and not sure if this is the
>> right fix.
>>
>> diff --git a/include/linux/entry-virt.h b/include/linux/entry-virt.h
>> index bfa767702d9a..c4856c252412 100644
>> --- a/include/linux/entry-virt.h
>> +++ b/include/linux/entry-virt.h
>> @@ -4,6 +4,7 @@
>>
>> #include <linux/static_call_types.h>
>> #include <linux/resume_user_mode.h>
>> +#include <linux/hrtimer_rearm.h>
>> #include <linux/syscalls.h>
>> #include <linux/seccomp.h>
>> #include <linux/sched.h>
>> @@ -58,6 +59,7 @@ int xfer_to_guest_mode_handle_work(void);
>> static inline void xfer_to_guest_mode_prepare(void)
>> {
>> lockdep_assert_irqs_disabled();
>> + hrtimer_rearm_deferred();
>> tick_nohz_user_enter_prepare();
>
>
> This code should never be reached with a rearm pending. Something else
> went wrong earlier. So while the patch "works" it papers over the
> underlying problem.

IIUC, the problem might be:

HRTimer -> VMExit:
[IRQ is disabled]
kvm_x86_call(handle_exit_irqoff)(vcpu)
vmx_handle_exit_irqoff
handle_external_interrupt_irqoff
sysvec_apic_timer_interrupt
irqentry_enter
...
irqentry_exit
irqentry_exit_to_kernel_mode
if (!regs_irqs_disabled(regs)) //<-- This is false, hrtimer
hrtimer_rearm_deferred() rearm is skipped!

This issue is triggered on TDX since TDX can't use preemption timer while normal
VMX VM uses preemption timer by default.

>
> Can you please do the following:
>
> 1) Apply the patch below
>
> 2) Enable function tracing and the hrtimer* trace events
>
> 3) Enable tracing if it has been disabled already
>
> echo 1 >/sys/kernel/tracing/tracing_on
>
> 4) Run the tests and wait for /sys/kernel/tracing/tracing_on to
> become 0, which means the problem triggered.
>
> 5) Retrieve the trace from /sys/kernel/tracing/trace and provide it
> somewhere to download from or send it to me compressed offlist.
>
> Thanks,
>
> tglx
> ---
>
> diff --git a/include/linux/entry-virt.h b/include/linux/entry-virt.h
> index bfa767702d9a..ab73963a7496 100644
> --- a/include/linux/entry-virt.h
> +++ b/include/linux/entry-virt.h
> @@ -58,6 +58,10 @@ int xfer_to_guest_mode_handle_work(void);
> static inline void xfer_to_guest_mode_prepare(void)
> {
> lockdep_assert_irqs_disabled();
> + if (test_thread_flag(TIF_HRTIMER_REARM)) {
> + tracing_off();
> + hrtimer_rearm_deferred();
> + }
> tick_nohz_user_enter_prepare();
> }
>
>