From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.231.128 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796096510; b=XR+HSOaQ0mtZ3wjT6E+WAahyfnN+OpoZdad1eyi79Z7VGpgmG5laZO34t68EJpMjJbxo ZMzBThSln4mbzAfnKQWGEUfkfEaQPjL1aZVF2T8MwfiMlc0IohplL2GDf495DKwz2zzDj HFIEQ6/+GxOUucAoQgL769nBKJBXTyWYJ4LhAAUHuiCE21+3twWgWjqIqYu9XEpGYjod1 Ic537XCWRw0YXbyqqQAGj2KydcXpiWMFFtPRga9TCxCMAWtHdyK6xbr5kM7yG2l95OHJK FUt5qMAY9gnnrMmulbnmCngzDovyiQat3xTm+26UyPTqcDqCWyAaZ/qzxdDmNKb3KFg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796096510; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Mime-Version:Content-Transfer-Encoding:Content-Type:Date:Message-Id: Cc:Subject:From:To:X-Mailer:References:In-Reply-To; bh=ToTBuwarG5a0QjVq4/eVkq+geSMRG42NR7N4EgkINCI=; b=Qxvd8Rhp2Ji0FXbgUzbG4yWMkzZjykDw1xR5FIv/dT9E+Rl3s1UzF7jZW9NfPb9KbRBq yiRiIXHn1YBeMBEIxFtjurENmbJB72EEbonLmPr+HuPsG/QLyHUyabPDSlEz2X9ZFC5Mf dBtIVFxobv+OGGZ2cFm+wyXMCIS34/gOPdG1cauZdBrK5LKVmefPDJkjsWZEwjxwJMYsX Qdygb8hwb5fkhTrHazOjWqzCALpcvGo9NX7AH9wbyJSFVxaG5//almmEfA1srMW9h6hvd iYePIs6HJvFYRiSRuy32QwrARcwmGrnzeECquR5QVTQirrwq1VVNGiqBKprX0MpxoUg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=iZpIfLb4; arc=none smtp.remote-ip=74.125.231.128 Received: from mail-oo2-f0.google.com (mail-oo2-f0.google.com [74.125.231.128]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2500F606A320 for ; Tue, 1 Dec 2026 03:41:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2500F606A320 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=iZpIfLb4 Received: by mail-oo2-f0.google.com with SMTP id 006d021491bc7-71b2566b798so1030231eaf.1 for ; Mon, 30 Nov 2026 19:41:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796096509; x=1796701309; darn=vger.kernel.org; h=in-reply-to:references:to:from:subject:cc:message-id:date :content-type:content-transfer-encoding:mime-version:from:to:cc :subject:date:message-id:reply-to:content-type; bh=ToTBuwarG5a0QjVq4/eVkq+geSMRG42NR7N4EgkINCI=; b=iZpIfLb41svIw22k9Iu9rdSSldzxWTkqFTzVGo19EjZxWF14SbU75kItQNmgVTNl6v kLWghX3lLLUtnzS5pJJ9UF0n2Hp67hnihWHYvYCeYM9cCFmlawMDQVpML6PGSYz2+6oA ujZcFewD1mftCtewjssUUXGP+4G36A6WYi0dmaJ+vUr3Ldtw+DmfstmoGjdMkoD8xwB2 WuDuUPsKdeaPyQGCxA2A3Uv90SO52ixgizpleYNqHF12llCPh1FLushXJpvPehSVGq4Y saNGUwvwvlcEw/rplFgglZgCk7fWdHuMCHOdND6RQtS5x+Y8xaYix7dQ8JwPf+vyohd3 WYFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796096509; x=1796701309; h=in-reply-to:references:to:from:subject:cc:message-id:date :content-type:content-transfer-encoding:mime-version:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=ToTBuwarG5a0QjVq4/eVkq+geSMRG42NR7N4EgkINCI=; b=UQCnyjOduqlfW8BoFWM6Ws26h2yLYqxU+Zi83iojUNTaV1ZM31Hx4jSNuH42Qf61Id 4+JRxE2TeiDlSJuHZjFVykq4nDDeeF3UokVNw0rTYpdvvXi6UK4u3sLzIfDus1l+nF8U 6QFHQJzmDYtow8FILAWN2H837kdycaqv+nOUYfv24NbKlRQPTDkcU0m58eHFsRZMY1Ja akiT+QZ5tFTVnThcVgsEuVPLcT7cB927CJ4B6sdA3GS5UwEY6I6NVk2MvZV9/duQ32lp 1affba9gEwjmWRoiFWSfclZfwqcr1NFVjBFzMyvKpBp64LlIfunnMX0jxfhaiLjXP8SR ZO8g== X-Forwarded-Encrypted: i=1; AL+hErGYDlB1kwhm0dw2DbBGyPvYLZ3m2i3jPp9Zv2egtB7uRfvQCM0Gk96FczqNShJmXVoxXbL3YC0ghapimbc=@vger.kernel.org X-Gm-Message-State: APuU4Z8OTp7s5uvqmzFfDJ8zGnXf4Jq+clagEer5m6ow+Ef+BlCRfAXj arRIMsk/V++9YhVk8u7SHNLb8lvM5UHE+p79AOtz9ydqO5x142cqgnOe X-Gm-Gg: AZFdTZdBDxJ+AIZrbVA091RobFIhELRfBLLT2cHQ5tA0oB7NOY45df1pSi9j1K89gXg EaXtB8uvdDQXLDyL4t/N1OEB06Owg7l5uqW4A6f9rnMwkX74xLD2sqyzOWS7FRpjWHk9BzBtxZl hV1wuT7mWAG+ljR1IWmoYS3nlqdEhjK817lZf6tX3ztWGEmj9QtMzPQzUAgso/DPrS6ssCOpri5 SRIBaSx3lYEF4X3N6Hh1gJ6/DU5XVMGDkYIS65j4n8wVioObd+N2MLH2OnHGSW+jIHaqI3ZJRUd DfOO/wt1kbzfZhmJ+1FanOoV12CqGB99IgyINfkkgVaooNIn6RWDEft/RjgDfikFwaUm33vBIVU 9VltCVMpAO8n1C0+xO6w243bJXybIIbmXH400ituN/wrrQ27LEAnXvs1qZVcPsQ/OwDIbNNzuip IHyM3vvM8vGy0QiNi2CH9MvRpdnZ9V54cx5GG5PkVWojOCSXlTof1fpUrFBpfgYa0IkkBSVqcLp jGvpu3XkU1y61euDlYo99T/1wxIZmaa3z2zMTVLeJ9zqgyoCAQrJJihXf0kb6lR7tYKm7Y4JULo CciSU6cXZieVsLop32YXJekVG8U6Ysh8Z1UDGlQANaz7ut6+LiO2Trk7KDzQZHwbhgRmoWKSGyC R6GvpKRX/KVuwqjvCWedYv1sERheVI41qAe5/6kFzyUDhRZBnYEkDs74G5Jw= X-Received: by 2002:a05:6820:a749:10b0:6dd:bc51:888d with SMTP id 006d021491bc7-71bc246721fmr1932857eaf.87.1796096508904; Mon, 30 Nov 2026 19:41:48 -0800 (PST) Received: from localhost ([2a03:2880:10ff:5a::]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-8631682200esm24519798a34.2.2026.11.30.19.41.46 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 30 Nov 2026 19:41:47 -0800 (PST) X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Tue, 01 Dec 2026 04:41:45 +0100 Message-Id: Cc: "Andrii Nakryiko" , "Thomas Gleixner" , , , Subject: Re: [BUG] bpf: hard lockup with ringbuf used from NMI and sched_switch on hpet clocksource From: "Kumar Kartikeya Dwivedi" To: "Tom Roozen" , "Alexei Starovoitov" X-Mailer: aerc 0.22.0 References: <20261128001825.38905-1-rzn.tom@gmail.com> In-Reply-To: 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=3D1000 >> - 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 cl= ock. >> 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 insta= nce of > this pattern. In case of hpet, we definitely have this problem of a deadl= ock. > > 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 thin= k the > simplest fix may be to stop reading the clocksource for the timeout on x8= 6 and > use sched_clock() instead. It reads the TSC directly, also when the TSC i= s > marked unstable, never calls into the clocksource, and is already used fr= om NMI > (perf, printk). > > That takes read_hpet() out of the waiter path, so the queue head or pendi= ng > waiter can no longer block on hpet.lock. > > local_clock() does not work for this, since with an unstable TSC it is cl= amped > 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 jiff= ies, > 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_n= s()) > > /* 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/rqspi= nlock.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 + +/* + * 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() rea= ds 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 #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 #include #include +#include /* * 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 =3D ktime_get_mono_fast_ns(); + ts->cur =3D res_clock_ns(); ts->timeout_end =3D ts->cur + ts->duration; return 0; } - time =3D ktime_get_mono_fast_ns(); + time =3D res_clock_ns(); if (time > ts->timeout_end) return -ETIMEDOUT;