Re: [BUG] bpf: hard lockup with ringbuf used from NMI and sched_switch on hpet clocksource

From: Kumar Kartikeya Dwivedi

Date: Fri Nov 27 2026 - 19:47:14 EST


On Sat Nov 28, 2026 at 1:17 AM CET, Tom Roozen wrote:
> Hi,
>
> A BPF ring buffer written from a perf_event program (NMI context) and
> from a tp_btf/sched_switch program hard-locks my machine within about
> a second when the clocksource is hpet. On acpi_pm it doesn't hard-lock.
>
>
> Environment
> -----------
>
> - AMD Ryzen 7 7735HS, Lenovo ThinkBook 14 G7 ARP,
> BIOS P6CN22WW
> - Kernel 7.3.3-1-default (openSUSE Tumbleweed), x86_64,
> PREEMPT_DYNAMIC (full), HZ=1000
> - The TSC is marked unstable at every boot, so the kernel selects
> hpet:
>
> TSC synchronization [CPU#0 -> CPU#2]:
> Measured 5354294254 cycles TSC warp between CPUs, turning off TSC clock.
> tsc: Marking TSC unstable due to check_tsc_sync_source failed
>

This is a known limitation. In general, we rely on ktime_get_mono_fast_ns() to
be safe in NMI context in a bunch of places, rqspinlock is just one instance of
this pattern. In case of hpet, we definitely have this problem of a deadlock.

Maybe the documentation for ktime_get_mono_fast_ns() should be updated to stop
claiming it is NMI safe.

Technically, the lock only needs elapsed time on the local CPU, so I think the
simplest fix may be to stop reading the clocksource for the timeout on x86 and
use sched_clock() instead. It reads the TSC directly, also when the TSC is
marked unstable, never calls into the clocksource, and is already used from NMI
(perf, printk).

That takes read_hpet() out of the waiter path, so the queue head or pending
waiter can no longer block on hpet.lock.

local_clock() does not work for this, since with an unstable TSC it is clamped
to one tick past the last tick, so it stops advancing while IRQs are off. The
clock ticking with IRQs off is mandatory.

Other architectures keep can ktime_get_mono_fast_ns(). x86 also keeps it when
the TSC frequency is unknown, because sched_clock() is then based on jiffies,
and would stop advancing. I don't think we can do better than that, even if that
still falls back to hpet.

Untested pseudocode:

/* arch/x86/include/asm/rqspinlock.h */
#define res_clock_ns() (__use_tsc ? sched_clock() : ktime_get_mono_fast_ns())

/* kernel/bpf/rqspinlock.c */
#ifndef res_clock_ns
#define res_clock_ns() ktime_get_mono_fast_ns()
#endif

with check_timeout() calling res_clock_ns().

> [...]