From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.231.136 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796096510; b=K4r0o3W+Vc2EYs+ZRUwE7bcyyHE2zllXlXwl5iPUjJzBlxpooRv7GKSGJfxAo6Bkj1PJ u4gyoclmtnTbzK1RGn66zNx1cPQnYzBGAR8pHKmsgjRuLuorFCYPq8a3OXgl8oaRwdlSc mqbm2N96tW/ZT31AfyCS/cOYtXVF7xFYGhmpWbm3M+XN4FqY2f6zoYACCcuDONrfQfH+c 4Ilk1n6WqYut7FIAcwIH2v4kCCleaBKbOCbDoAgJ9xUcWpZR4Z1MKGO0WP49XVUjFj7xG iFOWPzom24jXe9PetlbroqaOfKzZy/e9/AhawJuekmnrgQ6aH3lSV2unqf0kvJTwBVA== 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=T65zn108j9oxOB/RveV4FQiouWEABLb3MSQ5NinUSOO91cW2zdo+fA2KE0Ntu3Z8bwj0 gAs3VJdJaZ2cUpApTRz7AAPdHGZ1XH/kFGODUJdvCtnXzAlsFXQJgktu+614CMzzZX0af xQQ6lFF9f+UepQS+SvzJ6Y1lnnIepTyWyxqrDD+dYKwBKm8RpvXhYlEoGrmALz8oYWLBc CduaOEvA+JqDnRK27I6ClznY4aiGe1teAR0uAPfNX8KT6u5zNIRngwG9H/uBPGOj4dzyd QyDwQGSkEgiLJcMvas66wGn4M1f/IJcbi8hpXg+0r9fid2LfUcMVtuyLjiUr18b/XBw== 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.136 Received: from mail-oo2-f8.google.com (mail-oo2-f8.google.com [74.125.231.136]) (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 0DD8F606A31A for ; Tue, 1 Dec 2026 03:41:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0DD8F606A31A 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-f8.google.com with SMTP id 006d021491bc7-718facffeffso1045911eaf.0 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=Fa+0WRdYnu394OYjZ8jTwdtT0CseuQ2sd7IqwNXfee5IwVuBI0ysIRzhP88Te2j3EA gEuiKIKkCKOW+PV/KBNbyjaOLnYTZtgLfsftE+ctnjHSrSo3QLlGkpQkAwmcXvFGzP8A rjJ29iMC3y3ygAHfqmSFrlOLgCzWtfM3L8NNTtgcgINMHt/CqfW7RtAr5jSR9212NCRh 8CbcsK8tuU8fdOrU+vrB/JYdkR+ynTnqC4Q473Al+7KPw7Ry7lDcNG6UC3PQW+uviGTf 2DRAO9Kf0VfUI6Tf6eAH/5DeyNPaSuoskAFmlRr2Qdk/FLyFy09TO7PhIaBDQpOw+h2f YEOg== X-Forwarded-Encrypted: i=1; AL+hErEhYU4hF8if3ui+qrrxJ9OXjiNDuL9zvKLOgcV3TSnp5VDVM9Rac7sT/Zq93p4echHekrU=@vger.kernel.org X-Gm-Message-State: APuU4Z+9PpydHHK9K/orNElj4hOpmUBLW4GPUbY3TZOy7yCHVs/XnQGY CgDHLzxqGYtkCweI2OG2kg3CAV97TVnqUDAbPeboGi1GMEGDHpfDwkvOespbQalOZiU= X-Gm-Gg: AZFdTZcrYhoi1HGqaobpDecaJqx2jVEQtUK7HbV5DAWa2AisrJgJ2yOGiTukvGlKy2d cL8E+OsQb4g5bR12H5e6BHmA8IA2JtRw0b13YLsct185mZxK8bWJ70L2F6mwvu0YorqowRs39k2 /lrykXTrGcDPTY7q8JmCf2OofBw0qtCLCR/M3/eRmcz93keX8ZeZuEp9P99d6gxbKtaXktKtUsx mU0lpZ/J6gTArqFYZNAfLe/tFVygp3FitEBuHhcvAZm9WyKw48gfIEgPicOfogsFukJadilaO9W pOwL4tbWinZJXCZAs47l4BHwiLSusnkuBzx46z77IAbvRccnUS9PAot+jupkqz15Ct1BLz2P6Wi 1bdwMnVDC5QpDyqXUPBaq0afKdYM7OCA5m2Fsni4zmhfVrl2ruDWY2QMNpCae+Ya9elp8tPYNDG OwbBpQzFcC5mBXkBH17hmCLnpgpTnjJliw+jbWVXp8Xow8dXqVsaJS18XIjqTeqFNfglEUgR+nY mea0n5k634NFprj4YTegIdD+bTraqLJeNi5K+1RYrHY14x3CkdEzFsTSb+cDxl0UF6GfJmmACpn z3EJ58QOpcT7Gshutv8dIJDp/6VFGRDGvQJJ2wiEzUPpRvEhQb+lYuh0usoO0WrP7arsc5UOXtx 6lTntNt5ZL8T4tJTQdEzrN/IknGKUhoEBWkfqU3J8Cg0k192NwyMq4ycUyJE= 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: bpf@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;