From: "Kumar Kartikeya Dwivedi" <memxor@gmail.com>
To: "Tom Roozen" <rzn.tom@gmail.com>, "Alexei Starovoitov" <ast@kernel.org>
Cc: "Andrii Nakryiko" <andrii@kernel.org>,
"Thomas Gleixner" <tglx@kernel.org>, <x86@kernel.org>,
<bpf@vger.kernel.org>, <linux-kernel@vger.kernel.org>
Subject: Re: [BUG] bpf: hard lockup with ringbuf used from NMI and sched_switch on hpet clocksource
Date: Tue, 01 Dec 2026 04:41:45 +0100 [thread overview]
Message-ID: <DN932NX9YHGW.1S2WTU00TNYIV@gmail.com> (raw)
In-Reply-To: <DN6FH90QBQNL.3TQOK2LID5S8D@gmail.com>
On Sat Nov 28, 2026 at 1:47 AM CET, Kumar Kartikeya Dwivedi wrote:
> 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().
>
>> [...]
Tom, could you try the patch below? It's not supposed to the final thing
intended for submission, but to double check whether it fixes the deadlock you
are currently seeing.
Thanks
diff --git a/arch/x86/include/asm/rqspinlock.h b/arch/x86/include/asm/rqspinlock.h
index 24a885449ee6..12e3421ba7a5 100644
--- a/arch/x86/include/asm/rqspinlock.h
+++ b/arch/x86/include/asm/rqspinlock.h
@@ -28,6 +28,16 @@ static inline int resilient_virt_spin_lock(rqspinlock_t *lock)
#endif /* CONFIG_PARAVIRT */
+#include <asm/tsc.h>
+
+/*
+ * Reading the clocksource may wait for another CPU (see read_hpet()), and that
+ * CPU can be queued behind us for the lock from an NMI. sched_clock() reads the
+ * TSC without going through the clocksource. It is based on jiffies when the
+ * TSC frequency is unknown, so keep the clocksource in that case.
+ */
+#define res_clock_ns() (tsc_khz ? sched_clock() : ktime_get_mono_fast_ns())
+
#include <asm-generic/rqspinlock.h>
#endif /* _ASM_X86_RQSPINLOCK_H */
diff --git a/kernel/bpf/rqspinlock.c b/kernel/bpf/rqspinlock.c
index 14d379833589..ca1ecbec2f23 100644
--- a/kernel/bpf/rqspinlock.c
+++ b/kernel/bpf/rqspinlock.c
@@ -29,6 +29,7 @@
#include <trace/events/lock.h>
#include <asm/rqspinlock.h>
#include <linux/timekeeping.h>
+#include <linux/sched/clock.h>
/*
* Include queued spinlock definitions and statistics code
@@ -86,6 +87,10 @@ struct rqspinlock_timeout {
#define RES_TIMEOUT_VAL 2
+#ifndef res_clock_ns
+#define res_clock_ns() ktime_get_mono_fast_ns()
+#endif
+
DEFINE_PER_CPU_ALIGNED(struct rqspinlock_held, rqspinlock_held_locks);
EXPORT_SYMBOL_GPL(rqspinlock_held_locks);
@@ -205,12 +210,12 @@ static noinline int check_timeout(rqspinlock_t *lock, u32 mask,
if (!ts->timeout_end) {
if (check_deadlock_AA(lock))
return -EDEADLK;
- ts->cur = ktime_get_mono_fast_ns();
+ ts->cur = res_clock_ns();
ts->timeout_end = ts->cur + ts->duration;
return 0;
}
- time = ktime_get_mono_fast_ns();
+ time = res_clock_ns();
if (time > ts->timeout_end)
return -ETIMEDOUT;
prev parent reply other threads:[~2026-12-01 3:41 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-28 0:17 Tom Roozen
2026-11-28 0:47 ` Kumar Kartikeya Dwivedi
2026-12-01 3:41 ` Kumar Kartikeya Dwivedi [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=DN932NX9YHGW.1S2WTU00TNYIV@gmail.com \
--to=memxor@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=rzn.tom@gmail.com \
--cc=tglx@kernel.org \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®