From nobody Thu Feb 12 17:27:54 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 62A2D7FBDF for ; Mon, 10 Jun 2024 12:46:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718023598; cv=none; b=dWkr91ahKzGp82vB230MeboXuPJCPCo8069HuUA2wiyWNupvU155RIXzL2ggzgIbVg+eW199LY5ZohgYSacriWdonE1VxEO1BqBxQbJMc11VRPIRdpgw0ZlMcHByIfw57yDkynZJVcYPf0naKA5ArbsnJU9OEiQEu+seM6X/W+s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718023598; c=relaxed/simple; bh=oCEqJwcxMYboMVgrrm6y7VEn7VWx+xeGRNPCQEOcPxs=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=riXsu6dU+7InbC/Ymip1W9IRri2kRcSm9gRYqzYrxrKIKRLapSgAx/zoTxvDWIn+QeO4ntLW+8kO93P3+YloCFQrQ2AffttHN/Dja5clBmbfewGsDOl1bExavN10i6HwUcrib2HcBUC4enFbIsxZiYcXzgb/IGhkKv9yRQFj4jw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=yT490YFF; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=I1gphHWH; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="yT490YFF"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="I1gphHWH" Message-ID: <20240610124406.359476013@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1718023595; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=1aKSWv3sviyM2cko0BTe9cpwsTAoHAr5O414DpYJPMk=; b=yT490YFF6PPEGMHbNULktS2FmI18dVp7vUQgcqkZk9Ihkl31ng3C5NwZ75+shwEYv1cbyG 0FcCeKnUzR5UQWA9BgMs3OZDjskAulz3rZGmXDKU3jIKz0hZnmsdHBN4n7eV/SZCQVIHUy 8X5V+jgX3A/cWUsB+jUdCXZFlB7DnqII4yn6ehgGnuqC2r1tH22YYzPOl9ZTWa5m/ZJKg2 cP/o6WQ4Dxm/tiIbTpa3N0cQDH0pYplra7xYB5Asmw/ZiE5WlLvy/23SWJFWgRmLcIZs6p Cuq909lmxmAogdb7PaSYZhfDB5T8pQ1AUshvoAv17tHBHioCHEjbfjMRrxORKw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1718023595; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=1aKSWv3sviyM2cko0BTe9cpwsTAoHAr5O414DpYJPMk=; b=I1gphHWH0KTjYMrhYZnT9EZzhHiGS8b2a78TEoXv0YGJu2WmY7btuax/AD0tza2xFY1EOP j4KCEZePe3SqtkCQ== From: Thomas Gleixner To: LKML Cc: Steven Rostedt , Sam Sun , x86@kernel.org, syzkaller-bugs@googlegroups.com, xrivendell7@gmail.com Subject: [patch 1/4] perf/x86: Serialize set_attr_rdpmc() References: <87bk49xf15.ffs@tglx> <20240610124258.109097511@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 10 Jun 2024 14:46:35 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Yue and Xingwei reported a jump label failure. It's caused by the lack of serialization in set_attr_rdpmc(): CPU0 CPU1 Assume: x86_pmu.attr_rdpmc =3D=3D 0 if (val !=3D x86_pmu.attr_rdpmc) { if (val =3D=3D 0) ... else if (x86_pmu.attr_rdpmc =3D=3D 0) static_branch_dec(&rdpmc_never_available_key); if (val !=3D x86_pmu.attr_rdpmc) { if (val =3D=3D 0) ... else if (x86_pmu.attr_rdpmc =3D=3D 0) FAIL, due to imbalance ---> static_branch_dec(&rdpmc_never_availa= ble_key); The reported BUG() is a consequence of the above and of another bug in the jump label core code. The core code needs a separate fix, but that cannot prevent the imbalance problem caused by set_attr_rdpmc(). Prevent this by serializing set_attr_rdpmc() locally. Fixes: a66734297f78 ("perf/x86: Add /sys/devices/cpu/rdpmc=3D2 to allow rdp= mc for all tasks") Reported-by: Yue Sun Reported-by: Xingwei Lee Signed-off-by: Thomas Gleixner Closes: https://lore.kernel.org/r/CAEkJfYNzfW1vG=3DZTMdz_Weoo=3DRXY1NDunbxn= DaLyj8R4kEoE_w@mail.gmail.com --- arch/x86/events/core.c | 3 +++ 1 file changed, 3 insertions(+) --- a/arch/x86/events/core.c +++ b/arch/x86/events/core.c @@ -2547,6 +2547,7 @@ static ssize_t set_attr_rdpmc(struct dev struct device_attribute *attr, const char *buf, size_t count) { + static DEFINE_MUTEX(rdpmc_mutex); unsigned long val; ssize_t ret; =20 @@ -2560,6 +2561,8 @@ static ssize_t set_attr_rdpmc(struct dev if (x86_pmu.attr_rdpmc_broken) return -ENOTSUPP; =20 + guard(mutex)(&rdpmc_mutex); + if (val !=3D x86_pmu.attr_rdpmc) { /* * Changing into or out of never available or always available, From nobody Thu Feb 12 17:27:54 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C52967FBC3 for ; Mon, 10 Jun 2024 12:46:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718023600; cv=none; b=Swo6klmopnCEeSKtBFgnWMP0Qs89KLpeg02s7ScTvBglGEmZq9LrZrH702/D+BKWMe+L/devwtPKG3IjnLj6ILzhFNqMSCEw4FLADWjv2lLALey14xiXxTwDyyLzHCjzoY8arY1G3M2iCQzUVnM7jq4mqp7ZkLkoZwZmfm1U1lI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718023600; c=relaxed/simple; bh=T0rVGvr8LUnN2I6wnFzkd0Tr5pnWjtPzVfRQIF/iiMw=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=kmu8ApIck/4YD/ga0RKCMzFs5t4FhVTUaaHsOe4xh2y0iqjcO64DmUwfV/YbBZCuEsJ0kmy1KlcLqsl9l0kxaGQZTc9cvgpEJA0lnOt8wXbSbrJspjHv0VIaYyVuYcyV4K6rMvMV7ZLSy2ab9X2wSjFg16kDKgzh2J+uboSVHwY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=1nEESOIn; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=7WWbIfT9; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="1nEESOIn"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="7WWbIfT9" Message-ID: <20240610124406.422897838@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1718023596; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=1pvZGI7LQe2pRVrv1BSniOl5jD1/WESX9RzOVXXK4i8=; b=1nEESOIn4k16C5y3euYo6+jNB3gqgCCamS8J7wyMdiYoKICUmXlb34xDjcvRv4SGdtwcft B6ICy6ADGIBf+NIJpOEibwCgMfK79tvijUl6De/PYvluQ/RwXpwK8/mDOXlZe6LA5arqXe QqbCmz999WKaR07A3dxKAyIZ0xmPPKq9WviUj6QES0LVeT5kVlO6g/EPT/2V/28q3u9pgA BVVRo0+QfoDYcKIm1lQ4DER2vG2bsol3NiKmSQBzwEMXx7+MO8PZHEmrI0n3kxsdoLDbnV K2ZKr6MvI9ZpTEpPfjcrPq5Qzq9Ew7Kq8Rb05w/8eeHubdqubzqGNAI19yvc8w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1718023596; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=1pvZGI7LQe2pRVrv1BSniOl5jD1/WESX9RzOVXXK4i8=; b=7WWbIfT9Is15JgwTfm1VRsuaWLnyII+SrAkwQAPGeorvzLJYQzaobyrqy8XV4RM2Wim0up QEdrQQwukPv6PSDA== From: Thomas Gleixner To: LKML Cc: Steven Rostedt , Sam Sun , x86@kernel.org, syzkaller-bugs@googlegroups.com, xrivendell7@gmail.com Subject: [patch 2/4] jump_label: Fix concurrency issues in static_key_slow_dec() References: <87bk49xf15.ffs@tglx> <20240610124258.109097511@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 10 Jun 2024 14:46:36 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The commit which tried to fix the concurrency issues of concurrent static_key_slow_inc() failed to fix the equivalent issues vs. static_key_slow_dec(): CPU0 CPU1 static_key_slow_dec() static_key_slow_try_dec() key->enabled =3D=3D 1 val =3D atomic_fetch_add_unless(&key->enabled, -1, 1); if (val =3D=3D 1) return false; jump_label_lock(); if (atomic_dec_and_test(&key->enabled)) { --> key->enabled =3D=3D 0 __jump_label_update() static_key_slow_dec() static_key_slow_try_dec() key->enabled =3D=3D 0 val =3D atomic_fetch_add_unless(&key->enabled, -1, 1); --> key->enabled =3D=3D -1 <- FAIL There is another bug in that code, when there is a concurrent static_key_slow_inc() which enables the key as that sets key->enabled to -1 so on the other CPU val =3D atomic_fetch_add_unless(&key->enabled, -1, 1); will succeed and decrement to -2, which is invalid. Cure all of this by replacing the atomic_fetch_add_unless() with a atomic_try_cmpxchg() loop similar to static_key_fast_inc_not_disabled(). Fixes: 4c5ea0a9cd02 ("locking/static_key: Fix concurrent static_key_slow_in= c()") Reported-by: Yue Sun Reported-by: Xingwei Lee Signed-off-by: Thomas Gleixner --- kernel/jump_label.c | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) --- a/kernel/jump_label.c +++ b/kernel/jump_label.c @@ -131,7 +131,7 @@ bool static_key_fast_inc_not_disabled(st STATIC_KEY_CHECK_USE(key); /* * Negative key->enabled has a special meaning: it sends - * static_key_slow_inc() down the slow path, and it is non-zero + * static_key_slow_inc/dec() down the slow path, and it is non-zero * so it counts as "enabled" in jump_label_update(). Note that * atomic_inc_unless_negative() checks >=3D 0, so roll our own. */ @@ -150,7 +150,7 @@ bool static_key_slow_inc_cpuslocked(stru lockdep_assert_cpus_held(); =20 /* - * Careful if we get concurrent static_key_slow_inc() calls; + * Careful if we get concurrent static_key_slow_inc/dec() calls; * later calls must wait for the first one to _finish_ the * jump_label_update() process. At the same time, however, * the jump_label_update() call below wants to see @@ -247,20 +247,25 @@ EXPORT_SYMBOL_GPL(static_key_disable); =20 static bool static_key_slow_try_dec(struct static_key *key) { - int val; - - val =3D atomic_fetch_add_unless(&key->enabled, -1, 1); - if (val =3D=3D 1) - return false; + int v; =20 /* - * The negative count check is valid even when a negative - * key->enabled is in use by static_key_slow_inc(); a - * __static_key_slow_dec() before the first static_key_slow_inc() - * returns is unbalanced, because all other static_key_slow_inc() - * instances block while the update is in progress. + * Go into the slow path if key::enabled is less than or equal than + * one. One is valid to shut down the key, anything less than one + * is an imbalance, which is handled at the call site. + * + * That includes the special case of '-1' which is set in + * static_key_slow_inc_cpuslocked(), but that's harmless as it is + * fully serialized in the slow path below. By the time this task + * acquires the jump label lock the value is back to one and the + * retry under the lock must succeed. */ - WARN(val < 0, "jump label: negative count!\n"); + v =3D atomic_read(&key->enabled); + do { + if (v <=3D 1) + return false; + } while (!likely(atomic_try_cmpxchg(&key->enabled, &v, v - 1))); + return true; } =20 @@ -271,10 +276,11 @@ static void __static_key_slow_dec_cpuslo if (static_key_slow_try_dec(key)) return; =20 - jump_label_lock(); - if (atomic_dec_and_test(&key->enabled)) + guard(mutex)(&jump_label_mutex); + if (atomic_cmpxchg(&key->enabled, 1, 0)) jump_label_update(key); - jump_label_unlock(); + else + WARN_ON_ONCE(!static_key_slow_try_dec(key)); } =20 static void __static_key_slow_dec(struct static_key *key) From nobody Thu Feb 12 17:27:54 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A7C5881211 for ; Mon, 10 Jun 2024 12:46:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718023601; cv=none; b=alivhNN09EQj1PYffUDsN4WZJacBiilX8BhRHJRF4I8lthFepTy0OYzYJQImIz2lMSAn9dhzpCheFvWHs6nBZINz2RUB8cNC4ga076W0cEAb8lGQITHjGBAf0CnauAgReOAFK7401S3Cgd+Gnzxw6hM+b9i7O7IkGL7kBilEhmI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718023601; c=relaxed/simple; bh=3f/P+yKfDD1JJ3JmMZTVY0zvYt/RZf6q4FDQnJb9lfI=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=eXnp8LeyHeyKiqbBmHs7H0/vVUKhEhXRGuq9Skemv98FjSrlgWfHtKJKF3IEahHPcm2RNtsoQWnD+vJZXi3lsjY8feJ7drUIy50PrUnY9BOdKBTTHd9nF9/4jG3pzEmcbsy/ltS6klhG+pZngiKHvQ5fbNyD4Vg6qrAUc9dm6rs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=W6jTctGx; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=pOTQN9Ei; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="W6jTctGx"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="pOTQN9Ei" Message-ID: <20240610124406.484973160@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1718023598; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=VC6lH3dC9sC0pTK2Lu8vCn78JTXMe8396fISayi/2Vc=; b=W6jTctGxznSs+d0Vc0T3v2pyTyX8ZeWi4ZJPm4e3cz2qxlCRGnZITUy+rnTqy7u8XDyHru AT5yh1xlA5MrDfaCaK1cZEpMnrqrdyupPA8JTmk+AjsWYuBsyW6bRBHHouDlRdWeRoIcwF sn1SUxVdUfj2JIPDWEjpWIsF5nvvFHuctnRUfOGJl6czNPWRsl8fujGWgbOMom/Mlf+9MU I+Dq5r71wVKDZe+8ujBURIC3JGBzC47Yfb3eYv16dWJRpu9LLNGSF5/TwuTYXQMc14kJdO kNQViAEoFMl0eVRCFY1DhMjpEg5pYi41nljNGip2aQsezJK1Gr0ITIK7xTdMsg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1718023598; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=VC6lH3dC9sC0pTK2Lu8vCn78JTXMe8396fISayi/2Vc=; b=pOTQN9EifyeiNBTLQTvhWd26e5r9OUy2JFMVMc4M7N9XpACKLeQv/vSC43WmwKZC14Q+xj HBWpnM94Bw7tuJBQ== From: Thomas Gleixner To: LKML Cc: Steven Rostedt , Sam Sun , x86@kernel.org, syzkaller-bugs@googlegroups.com, xrivendell7@gmail.com Subject: [patch 3/4] jump_label: Clarify condition in static_key_fast_inc_not_disabled() References: <87bk49xf15.ffs@tglx> <20240610124258.109097511@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 10 Jun 2024 14:46:37 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The second part of if (v <=3D 0 || (v + 1) < 0) is not immediately obvious that it acts as overflow protection. Check explicitely for v =3D=3D INT_MAX instead and add a proper comment how this is used at the call sites. Signed-off-by: Thomas Gleixner --- kernel/jump_label.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) --- a/kernel/jump_label.c +++ b/kernel/jump_label.c @@ -132,12 +132,15 @@ bool static_key_fast_inc_not_disabled(st /* * Negative key->enabled has a special meaning: it sends * static_key_slow_inc/dec() down the slow path, and it is non-zero - * so it counts as "enabled" in jump_label_update(). Note that - * atomic_inc_unless_negative() checks >=3D 0, so roll our own. + * so it counts as "enabled" in jump_label_update(). + * + * The INT_MAX overflow condition is either used by the networking + * code to reset or detected in the slow path of + * static_key_slow_inc_cpuslocked(). */ v =3D atomic_read(&key->enabled); do { - if (v <=3D 0 || (v + 1) < 0) + if (v <=3D 0 || v =3D=3D INT_MAX) return false; } while (!likely(atomic_try_cmpxchg(&key->enabled, &v, v + 1))); From nobody Thu Feb 12 17:27:54 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2C40A823CE for ; Mon, 10 Jun 2024 12:46:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718023602; cv=none; b=Zr66wlL8crVoLb08oCYvEKSsJxpkk4fOkv0C8dmhurVNJOOVOqo+wCN24WFPhpZx3TIYjwMU8nofBO/X1BH490HdX34hbVY8C5+y+yoNFJddt3ymj0pfO4Mb+3jScC+uj/BfjExON0PCGSV79XLJ6iltBtYV5rtxn0aa6JbF+fw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718023602; c=relaxed/simple; bh=DzfzfEOYrS5hRroqqM63QoncLtcbJosXf0SN2GkmGYw=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=HnNFMKrR4KNkhOf4vsixE8LzykmsvGtlCZ9TgEm+V7i3qlXg2yDJDPzK/l6Us3A0FKaPINywVmaRQZRKpA2ZOVT/IfCtmgxtkPiSaGNd7wo3dIhGz+ykOaKani7zVR52WzChgFrlTFFCfFzt5Vxr4JeePNPW6TE2pDl7zpzVwt4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=1ae8f6d+; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=rdmqu4fD; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="1ae8f6d+"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="rdmqu4fD" Message-ID: <20240610124406.548322963@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1718023599; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=6YG0UjVZ2COICpcGaXkn7bT56v/DQnx1rLutVdszFN8=; b=1ae8f6d+738dLW/DcQCym/msZK5WZIsZijFafDFY4feUxd4pZuUcKa1BSXnKmDRBHjD5Tr GEj5hH48cj/TffXwAAO4JQZ960K9kmMyua9fASlTNhUy+ZcQDEnZriSxohCPVjLNNVh1Zh hUwLZQUs3aQmUqtGoxCjRkJAE+WmU53gwGgyKncnVpWCr/ThLVrwn9y+djrzytF3gOHnLA Z6W1S/CqE30hNkSsB+2fCEh+dThO36lyeUjll6LzStW8a9/0QIOGDQD/ude6jGuzupQgAU mF8h8e4bJqaqtNW8s48dwm1OEa5C7we76cMX94jSPk/azXpHgOasrN8MCZuWMQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1718023599; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=6YG0UjVZ2COICpcGaXkn7bT56v/DQnx1rLutVdszFN8=; b=rdmqu4fDZwdgg9hVq+KvgzLnTfjhJWIvHjiU3TN/Lvq6deQbPWM/rQ0gGTjx9XgtcBOcQN IT7F1TAyJYzaasAg== From: Thomas Gleixner To: LKML Cc: Steven Rostedt , Sam Sun , x86@kernel.org, syzkaller-bugs@googlegroups.com, xrivendell7@gmail.com Subject: [patch 4/4] jump_label: Simplify and clarify static_key_fast_inc_cpus_locked() References: <87bk49xf15.ffs@tglx> <20240610124258.109097511@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 10 Jun 2024 14:46:39 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Make the code more obvious and add proper comments to avoid future head scratching. Signed-off-by: Thomas Gleixner --- kernel/jump_label.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) --- a/kernel/jump_label.c +++ b/kernel/jump_label.c @@ -162,22 +162,24 @@ bool static_key_slow_inc_cpuslocked(stru if (static_key_fast_inc_not_disabled(key)) return true; =20 - jump_label_lock(); - if (atomic_read(&key->enabled) =3D=3D 0) { - atomic_set(&key->enabled, -1); + guard(mutex)(&jump_label_mutex); + /* Try to mark it as 'enabling in progress. */ + if (!atomic_cmpxchg(&key->enabled, 0, -1)) { jump_label_update(key); /* - * Ensure that if the above cmpxchg loop observes our positive - * value, it must also observe all the text changes. + * Ensure that when static_key_fast_inc_not_disabled() or + * static_key_slow_try_dec() observe the positive value, + * they must also observe all the text changes. */ atomic_set_release(&key->enabled, 1); } else { - if (WARN_ON_ONCE(!static_key_fast_inc_not_disabled(key))) { - jump_label_unlock(); + /* + * While holding the mutex this should never observe + * anything else than a value >=3D 1 and succeed + */ + if (WARN_ON_ONCE(!static_key_fast_inc_not_disabled(key))) return false; - } } - jump_label_unlock(); return true; }