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

From: Tom Roozen

Date: Fri Nov 27 2026 - 19:18:56 EST


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

Reproducer
----------

Source at the end of this mail. One BPF_MAP_TYPE_RINGBUF (16 MiB)
and two programs:

- on_sample: SEC("perf_event"), attached to a
PERF_COUNT_HW_CPU_CYCLES event on every CPU with freq=1,
sample_freq=8000
- on_switch: SEC("tp_btf/sched_switch")

Each reserves 64 bytes with bpf_ringbuf_reserve() and submits them.
It reads bpf_ktime_get_ns() before and after the reserve and keeps
the longest reserve time in a global variable. User space consumes
the ring buffer, runs 4 pairs of threads passing a byte over pipes,
and prints the number of events and the longest reserve times once
per second.

./hpet_rb 8000 60


Results
-------

Clocksource switched at runtime through
/sys/devices/system/clocksource/clocksource0/current_clocksource:

1. acpi_pm: ran the full 60 s, ~148500 events/s. Longest reserve
at the end of the run:

148638 events/s, longest reserve: NMI 0.112 ms, sched_switch 0.159 ms

2. hpet: produced a single log, then hung the machine:

693 events/s, longest reserve: NMI 500.810 ms, sched_switch 1001.174 ms


Additional test: read_hpet() replaced
-------------------------------------

To check whether the HPET read path is involved, I also used a
livepatch that replaces read_hpet() with a plain readl() of the HPET
main counter.

With the livepatch enabled, the reproducer ran the full 60 s:

537229 events/s, longest reserve: NMI 0.034 ms, sched_switch 0.036 ms

After disabling and unloading the livepatch, running the reproducer hung
the machine instantly.

My reading of the code: a CPU can be inside read_hpet() holding
hpet.lock when a perf NMI arrives. The NMI program then queues on
rb->spinlock behind a CPU that is in on_switch. If that
CPU is the queue head or pending waiter, its check_timeout() calls
read_hpet(), which waits for hpet.lock. hpet.lock is held by the
interrupted CPU, whose NMI is waiting in the queue without a timeout
of its own.

Thanks,
Tom Roozen


---- hpet_rb.bpf.c ----

#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>

struct {
__uint(type, BPF_MAP_TYPE_RINGBUF);
__uint(max_entries, 16 << 20);
} rb SEC(".maps");

__u64 max_ns[2]; /* longest bpf_ringbuf_reserve(): [0] NMI, [1] sched_switch */

static __always_inline void emit(int i)
{
__u64 t = bpf_ktime_get_ns();
__u64 *e = bpf_ringbuf_reserve(&rb, 64, 0);
__u64 d = bpf_ktime_get_ns() - t;

if (d > max_ns[i])
max_ns[i] = d;
if (e) {
*e = t;
bpf_ringbuf_submit(e, 0);
}
}

SEC("perf_event")
int on_sample(void *ctx)
{
emit(0);
return 0;
}

SEC("tp_btf/sched_switch")
int on_switch(void *ctx)
{
emit(1);
return 0;
}

char LICENSE[] SEC("license") = "GPL";

---- hpet_rb.c ----

/* Usage ./hpet_rb [sample_hz=8000] [seconds=60] */
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/perf_event.h>
#include <sys/syscall.h>
#include <bpf/libbpf.h>
#include "hpet_rb.skel.h"

static long events;

static int count(void *ctx, void *data, size_t len)
{
events++;
return 0;
}

static void *consume(void *rb)
{
while (ring_buffer__poll(rb, 100) >= 0)
;
return NULL;
}

/* Two threads passing a byte back and forth to cause many context switches. */
static void *pingpong(void *arg)
{
int *fd = arg;
char c = 0;

while (write(fd[1], &c, 1) == 1 && read(fd[0], &c, 1) == 1)
;
return NULL;
}

int main(int argc, char **argv)
{
int freq = argc > 1 ? atoi(argv[1]) : 8000;
int secs = argc > 2 ? atoi(argv[2]) : 60;
static int fds[4][2][2];
struct hpet_rb_bpf *skel;
struct ring_buffer *rb;
pthread_t t;

skel = hpet_rb_bpf__open_and_load();
if (!skel || hpet_rb_bpf__attach(skel))
return 1;

for (int cpu = 0; cpu < sysconf(_SC_NPROCESSORS_ONLN); cpu++) {
struct perf_event_attr attr = {
.type = PERF_TYPE_HARDWARE,
.config = PERF_COUNT_HW_CPU_CYCLES,
.size = sizeof(attr),
.freq = 1,
.sample_freq = freq,
};
int fd = syscall(__NR_perf_event_open, &attr, -1, cpu, -1, 0);

if (fd < 0 || !bpf_program__attach_perf_event(skel->progs.on_sample, fd)) {
perror("perf_event");
return 1;
}
}

rb = ring_buffer__new(bpf_map__fd(skel->maps.rb), count, NULL, NULL);
pthread_create(&t, NULL, consume, rb);

for (int i = 0; i < 4; i++) {
int a[2], b[2];

if (pipe(a) || pipe(b))
return 1;
fds[i][0][0] = a[0], fds[i][0][1] = b[1];
fds[i][1][0] = b[0], fds[i][1][1] = a[1];
pthread_create(&t, NULL, pingpong, fds[i][0]);
pthread_create(&t, NULL, pingpong, fds[i][1]);
}

for (int s = 0; s < secs; s++) {
sleep(1);
printf("%ld events/s, longest reserve: NMI %.3f ms, sched_switch %.3f ms\n",
events, skel->bss->max_ns[0] / 1e6, skel->bss->max_ns[1] / 1e6);
fflush(stdout);
fsync(STDOUT_FILENO);
events = 0;
}
return 0;
}

---- building the reproducer ----

clang -O2 -g -target bpf -c hpet_rb.bpf.c -o hpet_rb.bpf.o
bpftool gen skeleton hpet_rb.bpf.o > hpet_rb.skel.h
cc -O2 -Wall -o hpet_rb hpet_rb.c -lbpf