From nobody Sun Feb 8 23:41:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 AAEA410A1E for ; Wed, 30 Apr 2025 01:05:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975126; cv=none; b=kJseyjDEspazgN0DztcibvnzhCzfoY+iUDLOebBzN+EDNB2IDOCFavhKWwZTnd0YqV6ab4cOk+stHIsoS9ZqA2FePbTA9L7lCOKfvpYmET/fHHu4ATPiGefldHhd07ZHKbztexJMJobjMcwCarY8ZBVKbeZUshEUGoNvmSUIids= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975126; c=relaxed/simple; bh=U7zgE6IzCLrPpMWVpq2+GUdcrSUMCh/2FsvZ9huMwiU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Yb07tBkMDhg9G5//9t2e4+7XXY5fcd5ikRTxantmx0BuqYiFZvP9UAadiahgjhlvdxEtCHqIQKU1FM/wBasYCjhQh0jqG4wlEg8Nx0G4ZS9vEgCeS049hj1ulu+SeLRwoMfr/aBmkMpKfhSnwR4IU+2gnruFgvNljxjXMJkPpmU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=EWGtGc5C; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="EWGtGc5C" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1D92FC4CEE3; Wed, 30 Apr 2025 01:05:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1745975126; bh=U7zgE6IzCLrPpMWVpq2+GUdcrSUMCh/2FsvZ9huMwiU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EWGtGc5CTuD7OCD0BH1BIMJ4uUH/UutxSxCR9uVXzltMFD3JzqWumduUhzgREaX16 kT2MciZhfHf4bJULImPkHAAl40aPPqtgvmnNJzR7tZgYbwBQVv/WlvWmsNI8e7Ea/2 CrpGDDj7R8nUiu6C06PxqVC6yZip1nFIghuxrH7t+pLhGRKpg1koEOGkQUTT8GYx8W 6kJ3yr94xvGEYvLVNUP+hJ5mp0T6lzt6VDi8qTVGjiMn2HJJipwKvWEO19x0S8tT/o Q4L+U7RbvqP8unmRRjeKRWAo7ZIyKeWCxMTyOu3lyLtWn2YVFWgJI8unOSUxooHfn/ xIw7PIkIY08ng== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 35971CE077B; Tue, 29 Apr 2025 18:05:25 -0700 (PDT) From: "Paul E. McKenney" To: linux-kernel@vger.kernel.org Cc: kernel-team@meta.com, Andrew Morton , Kuniyuki Iwashima , Mateusz Guzik , Petr Mladek , Steven Rostedt , John Ogness , Sergey Senozhatsky , Jon Pan-Doh , Bjorn Helgaas , Karolina Stolarek , "Paul E. McKenney" Subject: [PATCH v4 01/20] lib: Add trivial kunit test for ratelimit Date: Tue, 29 Apr 2025 18:05:05 -0700 Message-Id: <20250430010524.3614408-1-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a simple single-threaded smoke test for lib/ratelimit.c To run on x86: make ARCH=3Dx86_64 mrproper ./tools/testing/kunit/kunit.py run --arch x86_64 --kconfig_add CONFIG_RATE= LIMIT_KUNIT_TEST=3Dy --kconfig_add CONFIG_SMP=3Dy lib_ratelimit This will fail on old ___ratelimit(), and subsequent patches provide the fixes that are required. [ paulmck: Apply timeout and kunit feedback from Petr Mladek. ] Link: https://lore.kernel.org/all/fbe93a52-365e-47fe-93a4-44a44547d601@paul= mck-laptop/ Link: https://lore.kernel.org/all/20250423115409.3425-1-spasswolf@web.de/ Signed-off-by: Paul E. McKenney Reviewed-by: Petr Mladek Cc: Andrew Morton Cc: Kuniyuki Iwashima Cc: Mateusz Guzik Cc: Steven Rostedt Cc: John Ogness Cc: Sergey Senozhatsky Cc: Jon Pan-Doh Cc: Bjorn Helgaas Cc: Karolina Stolarek --- lib/Kconfig.debug | 11 ++++++ lib/tests/Makefile | 1 + lib/tests/test_ratelimit.c | 79 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 lib/tests/test_ratelimit.c diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 9fe4d8dfe5782..c239099218c2b 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -3232,6 +3232,17 @@ config TEST_OBJPOOL =20 If unsure, say N. =20 +config RATELIMIT_KUNIT_TEST + tristate "KUnit Test for correctness and stress of ratelimit" if !KUNIT_A= LL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS + help + This builds the "test_ratelimit" module that should be used + for correctness verification and concurrent testings of rate + limiting. + + If unsure, say N. + config INT_POW_KUNIT_TEST tristate "Integer exponentiation (int_pow) test" if !KUNIT_ALL_TESTS depends on KUNIT diff --git a/lib/tests/Makefile b/lib/tests/Makefile index 5a4794c1826e7..1c7c2d20fe501 100644 --- a/lib/tests/Makefile +++ b/lib/tests/Makefile @@ -45,5 +45,6 @@ obj-$(CONFIG_STRING_KUNIT_TEST) +=3D string_kunit.o obj-$(CONFIG_STRING_HELPERS_KUNIT_TEST) +=3D string_helpers_kunit.o obj-$(CONFIG_USERCOPY_KUNIT_TEST) +=3D usercopy_kunit.o obj-$(CONFIG_UTIL_MACROS_KUNIT) +=3D util_macros_kunit.o +obj-$(CONFIG_RATELIMIT_KUNIT_TEST) +=3D test_ratelimit.o =20 obj-$(CONFIG_TEST_RUNTIME_MODULE) +=3D module/ diff --git a/lib/tests/test_ratelimit.c b/lib/tests/test_ratelimit.c new file mode 100644 index 0000000000000..0374107f5ea89 --- /dev/null +++ b/lib/tests/test_ratelimit.c @@ -0,0 +1,79 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include + +#include +#include + +/* a simple boot-time regression test */ + +#define TESTRL_INTERVAL (5 * HZ) +static DEFINE_RATELIMIT_STATE(testrl, TESTRL_INTERVAL, 3); + +#define test_ratelimited(test, expected) \ + KUNIT_ASSERT_EQ(test, ___ratelimit(&testrl, "test_ratelimit_smoke"), (exp= ected)) + +static void test_ratelimit_smoke(struct kunit *test) +{ + // Check settings. + KUNIT_ASSERT_GE(test, TESTRL_INTERVAL, 100); + + // Test normal operation. + test_ratelimited(test, true); + test_ratelimited(test, true); + test_ratelimited(test, true); + test_ratelimited(test, false); + + schedule_timeout_idle(TESTRL_INTERVAL - 40); + test_ratelimited(test, false); + + schedule_timeout_idle(50); + test_ratelimited(test, true); + + schedule_timeout_idle(2 * TESTRL_INTERVAL); + test_ratelimited(test, true); + test_ratelimited(test, true); + + schedule_timeout_idle(TESTRL_INTERVAL - 40); + test_ratelimited(test, true); + schedule_timeout_idle(50); + test_ratelimited(test, true); + test_ratelimited(test, true); + test_ratelimited(test, true); + test_ratelimited(test, false); + + // Test disabling. + testrl.burst =3D 0; + test_ratelimited(test, false); + testrl.burst =3D 2; + testrl.interval =3D 0; + test_ratelimited(test, true); + test_ratelimited(test, true); + test_ratelimited(test, true); + test_ratelimited(test, true); + test_ratelimited(test, true); + test_ratelimited(test, true); + test_ratelimited(test, true); + + // Testing re-enabling. + testrl.interval =3D TESTRL_INTERVAL; + test_ratelimited(test, true); + test_ratelimited(test, true); + test_ratelimited(test, false); + test_ratelimited(test, false); +} + +static struct kunit_case sort_test_cases[] =3D { + KUNIT_CASE_SLOW(test_ratelimit_smoke), + {} +}; + +static struct kunit_suite ratelimit_test_suite =3D { + .name =3D "lib_ratelimit", + .test_cases =3D sort_test_cases, +}; + +kunit_test_suites(&ratelimit_test_suite); + +MODULE_DESCRIPTION("___ratelimit() KUnit test suite"); +MODULE_LICENSE("GPL"); --=20 2.40.1 From nobody Sun Feb 8 23:41:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 D1C4F11712 for ; Wed, 30 Apr 2025 01:05:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975126; cv=none; b=ng6pyXjh+CwVSiQyZF22a4/7bTbC5BeF4542D0y7eN5DnznBbct8U/RGmzChtyo2UF/FqvCbnpjjc9SeM601FQdlruFXmrLMbq8TvAKoj02hamVv8Z4HtH/aHBN0T3IQqh0yiUsNiqJehvYriLykYhmJNTKbDVqcJ9TcQR48Wrg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975126; c=relaxed/simple; bh=N3RIolT2/wwpsyp3qVL0vety09DWGenJ3HGIlVCg+P0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=tTvEWLAneR1X4VSAHzGVWBymCLFu2hVdpqT2UIE1siVcDy+Jh80QG+E2xhs4mTV87Q1UdfzFi0pWdrKYOWYSruwjzjtbGrxSU7KvMLB0uBBTcOcxp5gFhrbCq6U/kuf0T1RjoCBhMmDFzsNmHDifKErROlchnx+rlvnqQdgnqaw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=OtV0/lFX; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="OtV0/lFX" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 621E7C4CEED; Wed, 30 Apr 2025 01:05:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1745975126; bh=N3RIolT2/wwpsyp3qVL0vety09DWGenJ3HGIlVCg+P0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OtV0/lFX2qmoJ7fHvG5EdLqgeuJ0Sm79t1SeFHcHJ3K0nwFvue7YvDYn+akTnDneC fg/IcTLE3kpWjCg0ccDSP4m3Y3mQEdAKp+4uF5CfQHpxPNt2WUpebhrcvp76jZFe6E eChDq5euQTXGy0ClmmFTLj+fox4AdjJNvmo/3yJFTeWKb+zX5BM+PJjsKcoxOhHJ48 jky+bkPgXoyUmkUiiCeZ7RhdryT3Wlwmrtv/8wh066o40OKtcfITWPyHxu2IHO3BrF y+jKvlaIKs1L1srqX6ZetQjwxLXA2IIunsYUx92Jf7hxy2T6++8siBJB6ICzqf9krE BHnr4XNVsuMig== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 418E6CE084A; Tue, 29 Apr 2025 18:05:25 -0700 (PDT) From: "Paul E. McKenney" To: linux-kernel@vger.kernel.org Cc: kernel-team@meta.com, Andrew Morton , Kuniyuki Iwashima , Mateusz Guzik , Petr Mladek , Steven Rostedt , John Ogness , Sergey Senozhatsky , Jon Pan-Doh , Bjorn Helgaas , Karolina Stolarek , "Paul E. McKenney" Subject: [PATCH v4 02/20] ratelimit: Create functions to handle ratelimit_state internals Date: Tue, 29 Apr 2025 18:05:06 -0700 Message-Id: <20250430010524.3614408-2-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" A number of ratelimit use cases do open-coded access to the ratelimit_state structure's ->missed field. This works, but is a bit messy and makes it more annoying to make changes to this field. Therefore, provide a ratelimit_state_inc_miss() function that increments the ->missed field, a ratelimit_state_get_miss() function that reads out the ->missed field, and a ratelimit_state_reset_miss() function that reads out that field, but that also resets its value to zero. These functions will replace client-code open-coded uses of ->missed. In addition, a new ratelimit_state_reset_interval() function encapsulates what was previously open-coded lock acquisition and direct field updates. [ paulmck: Apply kernel test robot feedback. ] Link: https://lore.kernel.org/all/fbe93a52-365e-47fe-93a4-44a44547d601@paul= mck-laptop/ Link: https://lore.kernel.org/all/20250423115409.3425-1-spasswolf@web.de/ Signed-off-by: Paul E. McKenney Reviewed-by: Petr Mladek Cc: Andrew Morton Cc: Kuniyuki Iwashima Cc: Mateusz Guzik Cc: Steven Rostedt Cc: John Ogness Cc: Sergey Senozhatsky --- include/linux/ratelimit.h | 40 ++++++++++++++++++++++++++++++++++----- lib/ratelimit.c | 8 ++++---- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/include/linux/ratelimit.h b/include/linux/ratelimit.h index b17e0cd0a30cf..8400c5356c187 100644 --- a/include/linux/ratelimit.h +++ b/include/linux/ratelimit.h @@ -22,16 +22,46 @@ static inline void ratelimit_default_init(struct rateli= mit_state *rs) DEFAULT_RATELIMIT_BURST); } =20 +static inline void ratelimit_state_inc_miss(struct ratelimit_state *rs) +{ + rs->missed++; +} + +static inline int ratelimit_state_get_miss(struct ratelimit_state *rs) +{ + return rs->missed; +} + +static inline int ratelimit_state_reset_miss(struct ratelimit_state *rs) +{ + int ret =3D rs->missed; + + rs->missed =3D 0; + return ret; +} + +static inline void ratelimit_state_reset_interval(struct ratelimit_state *= rs, int interval_init) +{ + unsigned long flags; + + raw_spin_lock_irqsave(&rs->lock, flags); + rs->interval =3D interval_init; + rs->begin =3D 0; + rs->printed =3D 0; + ratelimit_state_reset_miss(rs); + raw_spin_unlock_irqrestore(&rs->lock, flags); +} + static inline void ratelimit_state_exit(struct ratelimit_state *rs) { + int m; + if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE)) return; =20 - if (rs->missed) { - pr_warn("%s: %d output lines suppressed due to ratelimiting\n", - current->comm, rs->missed); - rs->missed =3D 0; - } + m =3D ratelimit_state_reset_miss(rs); + if (m) + pr_warn("%s: %d output lines suppressed due to ratelimiting\n", current-= >comm, m); } =20 static inline void diff --git a/lib/ratelimit.c b/lib/ratelimit.c index ce945c17980b9..85e22f00180c5 100644 --- a/lib/ratelimit.c +++ b/lib/ratelimit.c @@ -51,12 +51,12 @@ int ___ratelimit(struct ratelimit_state *rs, const char= *func) rs->begin =3D jiffies; =20 if (time_is_before_jiffies(rs->begin + interval)) { - if (rs->missed) { + int m =3D ratelimit_state_reset_miss(rs); + + if (m) { if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE)) { printk_deferred(KERN_WARNING - "%s: %d callbacks suppressed\n", - func, rs->missed); - rs->missed =3D 0; + "%s: %d callbacks suppressed\n", func, m); } } rs->begin =3D jiffies; --=20 2.40.1 From nobody Sun Feb 8 23:41:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 16CCA13FEE for ; Wed, 30 Apr 2025 01:05:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975127; cv=none; b=ZESAWI7wrBTEG/ZuyuIaA97lBu6IsSPqmFargr3xpdgAUhgMX3UgzAiOuwGEtxvVLF5OqdQB8+91WNGTDvBi3DZiw7+1vPn9hoD0JeoYvflkYYqSgqKsksLefWE3aw7Ldnvt94D81sMpGZ9kwy21LjeFs3HlGJ3wKVwxoovgv0Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975127; c=relaxed/simple; bh=gdsff1ewKdQFrckg4o6Eop2RJTaYEsK2lc1udsPqdD4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=bA2cIOzGgvIDakR1Fk2/JhjEzSV0ZGGIRzW7NUTFL6YALKMHmmo5gTeX/uw1P9ZvNySmdp8sPyiX7lIzWceraS/jMDAplIdaeybiB+GHaKTNi8SxfXolF8Nc8Zhw3KIIP6lBzmf64hKdcqSdSIxXPV5oktuVejZUlEVE16+UUkI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=dGcUCjFq; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="dGcUCjFq" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A39D9C4CEEF; Wed, 30 Apr 2025 01:05:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1745975126; bh=gdsff1ewKdQFrckg4o6Eop2RJTaYEsK2lc1udsPqdD4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dGcUCjFqgqdvL2K23b0Mh9BNt68ZzzgNMwWFEus1JXO57ZaRExAwbZeie4GbN2n+y O+IzQTQmJKn3l408CY0gO/8BOSl2aRaWRKHO4hwuJA9fIJMnVzPy7vzBiwX0skNMvY inVEP/sThwRH2aoEQsETSZFwMbX0xv7CVeQn8GtmrAri3LamoOvHjf5ASMuOPojpUc vreQPJFcE4l5hVboiFegAenRppmil13gOLQoOYEjbCfnaDxIpnkflfyKKXsHq+BZVl 43RGV02rGojLLcJrdOu4bciXgYlrPw3pIajA95MWq6ogSxUI82UClJZpzBTOKRdlUZ qsIWU8u92FyeA== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 4C1DBCE08F4; Tue, 29 Apr 2025 18:05:25 -0700 (PDT) From: "Paul E. McKenney" To: linux-kernel@vger.kernel.org Cc: kernel-team@meta.com, Andrew Morton , Kuniyuki Iwashima , Mateusz Guzik , Petr Mladek , Steven Rostedt , John Ogness , Sergey Senozhatsky , Jon Pan-Doh , Bjorn Helgaas , Karolina Stolarek , "Paul E. McKenney" , "Theodore Ts'o" Subject: [PATCH v4 03/20] random: Avoid open-coded use of ratelimit_state structure's ->missed field Date: Tue, 29 Apr 2025 18:05:07 -0700 Message-Id: <20250430010524.3614408-3-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The _credit_init_bits() function directly accesses the ratelimit_state structure's ->missed field, which works, but which also makes it more difficult to change this field. Therefore, make use of the ratelimit_state_get_miss() and ratelimit_state_inc_miss() functions instead of directly accessing the ->missed field. Link: https://lore.kernel.org/all/fbe93a52-365e-47fe-93a4-44a44547d601@paul= mck-laptop/ Link: https://lore.kernel.org/all/20250423115409.3425-1-spasswolf@web.de/ Signed-off-by: Paul E. McKenney Reviewed-by: Petr Mladek Cc: "Theodore Ts'o" "Jason A. Donenfeld" --- drivers/char/random.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/drivers/char/random.c b/drivers/char/random.c index 38f2fab29c569..416dac0ab565d 100644 --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -726,6 +726,7 @@ static void __cold _credit_init_bits(size_t bits) static DECLARE_WORK(set_ready, crng_set_ready); unsigned int new, orig, add; unsigned long flags; + int m; =20 if (!bits) return; @@ -748,9 +749,9 @@ static void __cold _credit_init_bits(size_t bits) wake_up_interruptible(&crng_init_wait); kill_fasync(&fasync, SIGIO, POLL_IN); pr_notice("crng init done\n"); - if (urandom_warning.missed) - pr_notice("%d urandom warning(s) missed due to ratelimiting\n", - urandom_warning.missed); + m =3D ratelimit_state_get_miss(&urandom_warning); + if (m) + pr_notice("%d urandom warning(s) missed due to ratelimiting\n", m); } else if (orig < POOL_EARLY_BITS && new >=3D POOL_EARLY_BITS) { spin_lock_irqsave(&base_crng.lock, flags); /* Check if crng_init is CRNG_EMPTY, to avoid race with crng_reseed(). */ @@ -1466,7 +1467,7 @@ static ssize_t urandom_read_iter(struct kiocb *kiocb,= struct iov_iter *iter) =20 if (!crng_ready()) { if (!ratelimit_disable && maxwarn <=3D 0) - ++urandom_warning.missed; + ratelimit_state_inc_miss(&urandom_warning); else if (ratelimit_disable || __ratelimit(&urandom_warning)) { --maxwarn; pr_notice("%s: uninitialized urandom read (%zu bytes read)\n", --=20 2.40.1 From nobody Sun Feb 8 23:41:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 3A7F61B7F4 for ; Wed, 30 Apr 2025 01:05:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975127; cv=none; b=o97QE09P5i1ICLS94M57jNiUACFzRLIEOoRhTBpz04Soq+LXVcymKEWDRZKSMurFN66RuAlA+0MJuBbyB8KqBSWqq4NxpUmKWDpExX8FsjhLRQX6vEn/0HwrU/2G4HwHkpfmGI2SkbW30E94u4kawusDe3yR/RlAc4wcchWAO1Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975127; c=relaxed/simple; bh=qrbtE4GJAc5CkkZvuRY2A4Vna57jDG+6dB5+Nw5K9to=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=aNUb0DYkNeo4WTcTX3I90+jsz94y6OAS+tLZR8PFVnkhyR2yRcRHWoLmHXOGHW4o4xkXM18H2nRnKMsLG3zADubXTRT5lEllSHFQUqcgxIz5C5lZhgWbTd6j55VIEujFdgwQOuYlNITs0EZ+sWzf+FXA1eJLfwPYgjCDWfBZ+60= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=J0Cd+GHK; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="J0Cd+GHK" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A9F12C4CEEB; Wed, 30 Apr 2025 01:05:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1745975126; bh=qrbtE4GJAc5CkkZvuRY2A4Vna57jDG+6dB5+Nw5K9to=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=J0Cd+GHK5HI12oOTGq3MCptyKvKob6SIvI4/kwg+GZUml+H6o/pF5KAEiCoDYcO3A tq9qIffs9TIR3cYVIBX+CprXLUa3Pyga7XpjSL45Ooin2zTKEg/+OMjXiqDd9h4foq YIFn9m/S/tMCW6FBo6fNHPC/ZsQnz+eWD/XmTBJRbkR5dz6WknS+M4F/Cbhd8BbAaH M9GLoPKP/vFrqh+tR22Zc9A+W3S65AVdecybTT8UgLiZPdg4ufKXfxv7cUeXvRjFdr GZ5WwRfCsnTvZl0N88HznyyVPfLjZH4egBLQZJ1RUaKASy/33nBPSdGXoISraajtXI PTzEbTIFJx2cA== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 581D5CE0DEC; Tue, 29 Apr 2025 18:05:25 -0700 (PDT) From: "Paul E. McKenney" To: linux-kernel@vger.kernel.org Cc: kernel-team@meta.com, Andrew Morton , Kuniyuki Iwashima , Mateusz Guzik , Petr Mladek , Steven Rostedt , John Ogness , Sergey Senozhatsky , Jon Pan-Doh , Bjorn Helgaas , Karolina Stolarek , "Paul E. McKenney" , Jani Nikula , Joonas Lahtinen , Rodrigo Vivi , Tvrtko Ursulin , David Airlie , Simona Vetter , intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org Subject: [PATCH v4 04/20] drm/i915: Avoid open-coded use of ratelimit_state structure's ->missed field Date: Tue, 29 Apr 2025 18:05:08 -0700 Message-Id: <20250430010524.3614408-4-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The i915_oa_stream_destroy() function directly accesses the ratelimit_state structure's ->missed field, which works, but which also makes it more difficult to change this field. Therefore, make use of the ratelimit_state_get_miss() function instead of directly accessing the ->missed field. Link: https://lore.kernel.org/all/fbe93a52-365e-47fe-93a4-44a44547d601@paul= mck-laptop/ Link: https://lore.kernel.org/all/20250423115409.3425-1-spasswolf@web.de/ Signed-off-by: Paul E. McKenney Reviewed-by: Petr Mladek Acked-by: Jani Nikula Cc: Joonas Lahtinen Cc: Rodrigo Vivi Cc: Tvrtko Ursulin Cc: David Airlie Cc: Simona Vetter Cc: Cc: --- drivers/gpu/drm/i915/i915_perf.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/gpu/drm/i915/i915_perf.c b/drivers/gpu/drm/i915/i915_p= erf.c index de0b413600a15..1658f1246c6fa 100644 --- a/drivers/gpu/drm/i915/i915_perf.c +++ b/drivers/gpu/drm/i915/i915_perf.c @@ -1666,6 +1666,7 @@ static void i915_oa_stream_destroy(struct i915_perf_s= tream *stream) struct i915_perf *perf =3D stream->perf; struct intel_gt *gt =3D stream->engine->gt; struct i915_perf_group *g =3D stream->engine->oa_group; + int m; =20 if (WARN_ON(stream !=3D g->exclusive_stream)) return; @@ -1690,10 +1691,9 @@ static void i915_oa_stream_destroy(struct i915_perf_= stream *stream) free_oa_configs(stream); free_noa_wait(stream); =20 - if (perf->spurious_report_rs.missed) { - gt_notice(gt, "%d spurious OA report notices suppressed due to ratelimit= ing\n", - perf->spurious_report_rs.missed); - } + m =3D ratelimit_state_get_miss(&perf->spurious_report_rs); + if (m) + gt_notice(gt, "%d spurious OA report notices suppressed due to ratelimit= ing\n", m); } =20 static void gen7_init_oa_buffer(struct i915_perf_stream *stream) --=20 2.40.1 From nobody Sun Feb 8 23:41:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 E49D611CBA for ; Wed, 30 Apr 2025 01:05:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975127; cv=none; b=FBn/10TyobxA3yR7IIHOJK3/nDxw5jDXQv5oW3QN5nftguJT9iLOao7anmzg9/TsM7ZksGXFeGaByhTsNQ5GZECUqNH29NMpa1M/KQObgxA5RKG6eKng5DNrDFkQHHnThwWNwcepZZwkQWI4DxHV5rygePvurhf7wwax6iql1n0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975127; c=relaxed/simple; bh=oSI4AdvwmcSLbAbbuCxcWerS/mPOdTcxQRziDu39kWk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=WVC8kfT2FVIUOzOZsOeSJHw0hvLysTodDBv+2vHbHcbV2wr6zF9VVdNTRWJLYYsP7FydEFQuiwdY8H2mBXLJFd7+WUc6qe8nReiVHuO3pOBJdTlrJjJlZGCXjFimJoTix2wiPKBCyR/RBwQ9D+SUe8nYmyXFG9n4jzh0kHmKCbw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=UMfXMPf5; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="UMfXMPf5" Received: by smtp.kernel.org (Postfix) with ESMTPSA id ABC3BC4CEF1; Wed, 30 Apr 2025 01:05:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1745975126; bh=oSI4AdvwmcSLbAbbuCxcWerS/mPOdTcxQRziDu39kWk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UMfXMPf5xvH5ArBjNBTG7Tv8pWiWvDjbeVtolcSJm7iXVCypcUrf3INOHPeiDZxPZ 2Ls/nsp3B/fyPA8dm0MSEpKeTqUKSZAM0sgKzIHyoE3gYjk0oJAsT9VwHJcyd1g7jQ 0Yxkam1PeoGWs83YeRiqxvJYkDkEISG5M+h/YynLeoHLZtquzHCvKAyN/nNaeN+ge+ bgbkPqqyRmPvlua0r0eabHvnsoO1hlponjPv8OLGxGvw8+XASO2Uw34HFvkI94YByH zHN1cUv1R0TUp86Jhf3lPXe/w7eC5cxHumHeiB8ckhBmg9dJzLmZ+/KqE7d2+YYeCL LwLLAgbvXE1Qg== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 6475CCE0E56; Tue, 29 Apr 2025 18:05:25 -0700 (PDT) From: "Paul E. McKenney" To: linux-kernel@vger.kernel.org Cc: kernel-team@meta.com, Andrew Morton , Kuniyuki Iwashima , Mateusz Guzik , Petr Mladek , Steven Rostedt , John Ogness , Sergey Senozhatsky , Jon Pan-Doh , Bjorn Helgaas , Karolina Stolarek , "Paul E. McKenney" , kernel test robot , Alex Deucher , Kenneth Feng , =?UTF-8?q?Christian=20K=C3=B6nig?= , Xinhui Pan , David Airlie , Simona Vetter , amd-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org Subject: [PATCH v4 05/20] drm/amd/pm: Avoid open-coded use of ratelimit_state structure's internals Date: Tue, 29 Apr 2025 18:05:09 -0700 Message-Id: <20250430010524.3614408-5-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable The amdgpu_set_thermal_throttling_logging() function directly accesses the ratelimit_state structure's ->missed field, which works, but which also makes it more difficult to change this field. Therefore, make use of the ratelimit_state_reset_interval() function instead of directly accessing the ->missed field. Nevertheless, open-coded use of ->burst and ->interval is still permitted, for example, for runtime sysfs adjustment of these fields. Link: https://lore.kernel.org/all/fbe93a52-365e-47fe-93a4-44a44547d601@paul= mck-laptop/ Link: https://lore.kernel.org/all/20250423115409.3425-1-spasswolf@web.de/ Reported-by: kernel test robot Closes: https://lore.kernel.org/oe-kbuild-all/202503180826.EiekA1MB-lkp@int= el.com/ Signed-off-by: Paul E. McKenney Acked-by: Alex Deucher Reviewed-by: Petr Mladek Cc: Kenneth Feng Cc: "Christian K=C3=B6nig" Cc: Xinhui Pan Cc: David Airlie Cc: Simona Vetter Cc: Cc: --- drivers/gpu/drm/amd/pm/amdgpu_pm.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/drivers/gpu/drm/amd/pm/amdgpu_pm.c b/drivers/gpu/drm/amd/pm/am= dgpu_pm.c index 922def51685b0..d533c79f7e215 100644 --- a/drivers/gpu/drm/amd/pm/amdgpu_pm.c +++ b/drivers/gpu/drm/amd/pm/amdgpu_pm.c @@ -1606,7 +1606,6 @@ static ssize_t amdgpu_set_thermal_throttling_logging(= struct device *dev, struct drm_device *ddev =3D dev_get_drvdata(dev); struct amdgpu_device *adev =3D drm_to_adev(ddev); long throttling_logging_interval; - unsigned long flags; int ret =3D 0; =20 ret =3D kstrtol(buf, 0, &throttling_logging_interval); @@ -1617,18 +1616,12 @@ static ssize_t amdgpu_set_thermal_throttling_loggin= g(struct device *dev, return -EINVAL; =20 if (throttling_logging_interval > 0) { - raw_spin_lock_irqsave(&adev->throttling_logging_rs.lock, flags); /* * Reset the ratelimit timer internals. * This can effectively restart the timer. */ - adev->throttling_logging_rs.interval =3D - (throttling_logging_interval - 1) * HZ; - adev->throttling_logging_rs.begin =3D 0; - adev->throttling_logging_rs.printed =3D 0; - adev->throttling_logging_rs.missed =3D 0; - raw_spin_unlock_irqrestore(&adev->throttling_logging_rs.lock, flags); - + ratelimit_state_reset_interval(&adev->throttling_logging_rs, + (throttling_logging_interval - 1) * HZ); atomic_set(&adev->throttling_logging_enabled, 1); } else { atomic_set(&adev->throttling_logging_enabled, 0); --=20 2.40.1 From nobody Sun Feb 8 23:41:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 5F3E8746E for ; Wed, 30 Apr 2025 01:05:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975127; cv=none; b=LnizA6pWzRxsu+Q8BaIbokK0EraZTxUgmCSiuZkLRfBkAWZLJbpPze1IwTpj/8SVx9nPRBF73w/G7z6HhB9hXn/IF1LX5b8PToSmmqpWZJF3m3JeY14umrLZ1QWWE0avYNqGTvBOB5TviBmAbpcCPYC6Kg3Vhvz4+2Fp1/w9Zes= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975127; c=relaxed/simple; bh=oqGQLaAZjOiJlIyJmpFkRKe1yhFE6Ac++q3VgQmENoU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=rw+6ro3HemTLGksF7XOzN1UFlC2UoG+Z5vNGz9rbLBy/lr2/vw1XWyqCW8+8+EcwnC35QFlR0daaLqlpG4+7FKVbBA6eJmjcZWgzd7Of/r/iHqJdR47H1iY4w1Lgp/g/lUHxstOb8aBfeLc3NNIl6wnt8Uqz8Xa8/O4sjsnVKdI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=YYnxc77K; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="YYnxc77K" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C6AC6C4CEF3; Wed, 30 Apr 2025 01:05:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1745975126; bh=oqGQLaAZjOiJlIyJmpFkRKe1yhFE6Ac++q3VgQmENoU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YYnxc77KczXhWfNJw8sUDsIgf+ncwZf8BjD0MBgZ3Xnv+79uLjeTCCapab/0X30At rcWRHPo/6zUTibl2ipykQ6Uf6tm++qhDCUgbT8dqXEnT627gnCD6qlGQOxsvIESaH0 UMC95LPNEf4xSFm04WKLXog+sCnUo6VG6Oog40eB5jp1GoqJJ2SHo1+AvnozsC1Hy4 TZid0RmWLjILBcjcAqLcZJoq//McVJ0KkynGU/sUhUYQ7hICE1IVzXXTPwDeNohcNa vNgdErBNmZwYyUUySoE7fHLcV39kF8Ofswfp70nCsv65y1I3wMBmQCcKoxcBNLtDrJ PBBRLWjdgCAXg== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 6FA2CCE0E5C; Tue, 29 Apr 2025 18:05:25 -0700 (PDT) From: "Paul E. McKenney" To: linux-kernel@vger.kernel.org Cc: kernel-team@meta.com, Andrew Morton , Kuniyuki Iwashima , Mateusz Guzik , Petr Mladek , Steven Rostedt , John Ogness , Sergey Senozhatsky , Jon Pan-Doh , Bjorn Helgaas , Karolina Stolarek , "Paul E. McKenney" Subject: [PATCH v4 06/20] ratelimit: Convert the ->missed field to atomic_t Date: Tue, 29 Apr 2025 18:05:10 -0700 Message-Id: <20250430010524.3614408-6-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The ratelimit_state structure's ->missed field is sometimes incremented locklessly, and it would be good to avoid lost counts. This is also needed to count the number of misses due to trylock failure. Therefore, convert the ratelimit_state structure's ->missed field to atomic_t. Link: https://lore.kernel.org/all/fbe93a52-365e-47fe-93a4-44a44547d601@paul= mck-laptop/ Link: https://lore.kernel.org/all/20250423115409.3425-1-spasswolf@web.de/ Signed-off-by: Paul E. McKenney Reviewed-by: Petr Mladek Cc: Andrew Morton Cc: Kuniyuki Iwashima Cc: Mateusz Guzik Cc: Steven Rostedt Cc: John Ogness Cc: Sergey Senozhatsky --- include/linux/ratelimit.h | 9 +++------ include/linux/ratelimit_types.h | 2 +- lib/ratelimit.c | 2 +- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/include/linux/ratelimit.h b/include/linux/ratelimit.h index 8400c5356c187..c78b92b3e5cd8 100644 --- a/include/linux/ratelimit.h +++ b/include/linux/ratelimit.h @@ -24,20 +24,17 @@ static inline void ratelimit_default_init(struct rateli= mit_state *rs) =20 static inline void ratelimit_state_inc_miss(struct ratelimit_state *rs) { - rs->missed++; + atomic_inc(&rs->missed); } =20 static inline int ratelimit_state_get_miss(struct ratelimit_state *rs) { - return rs->missed; + return atomic_read(&rs->missed); } =20 static inline int ratelimit_state_reset_miss(struct ratelimit_state *rs) { - int ret =3D rs->missed; - - rs->missed =3D 0; - return ret; + return atomic_xchg_relaxed(&rs->missed, 0); } =20 static inline void ratelimit_state_reset_interval(struct ratelimit_state *= rs, int interval_init) diff --git a/include/linux/ratelimit_types.h b/include/linux/ratelimit_type= s.h index 765232ce0b5e9..d21fe82b67f67 100644 --- a/include/linux/ratelimit_types.h +++ b/include/linux/ratelimit_types.h @@ -18,7 +18,7 @@ struct ratelimit_state { int interval; int burst; int printed; - int missed; + atomic_t missed; unsigned int flags; unsigned long begin; }; diff --git a/lib/ratelimit.c b/lib/ratelimit.c index 85e22f00180c5..18703f92d73e7 100644 --- a/lib/ratelimit.c +++ b/lib/ratelimit.c @@ -66,7 +66,7 @@ int ___ratelimit(struct ratelimit_state *rs, const char *= func) rs->printed++; ret =3D 1; } else { - rs->missed++; + ratelimit_state_inc_miss(rs); ret =3D 0; } raw_spin_unlock_irqrestore(&rs->lock, flags); --=20 2.40.1 From nobody Sun Feb 8 23:41:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 07ECE134CB for ; Wed, 30 Apr 2025 01:05:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975127; cv=none; b=iMXspUzqlTrL8XuF1SCGbDff2C/mliCHijTm0arSXhAOBRd1IgchB0HzWDAP0nyLgq5eDJfF4It69kky+Qo6DM3m337bvqhXeXBVkq1okeFz+R6Zmk2KhUqizSXgO8fhWE4ome4hFZr//LbZ4/vrwc6hx4rMMiEtXWZ2qINd7QA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975127; c=relaxed/simple; bh=drIhZlz6qjJXl6CfFVToJFT9tX9Y5dyNfzQrE+P3u7o=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=TvymNZ79VCtwKK3JOIAauBJZmCfO2gzJH4SM+H/nDbuC+IqRY2ItGalxGhdNBgI/kM4cW/sVUV9/3j3VjdD+03hvu41D8kR0CX/J/YwVNb8WxxK4sGiAOJ+/hsRzLbTBqlhyU95/GvMKx5mtUAbtVZDUpEVBlzrDhG9C33MX9hM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ZYO3AJC6; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ZYO3AJC6" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CBF39C4CEE3; Wed, 30 Apr 2025 01:05:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1745975126; bh=drIhZlz6qjJXl6CfFVToJFT9tX9Y5dyNfzQrE+P3u7o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZYO3AJC6RXPJeaS+caIxQrzhTeKEi94FbgC1Uc0iLBoCA9Zlwz2uW+qGjfMy23MoH d/FukNP/1PD10TRwKVeS3SJ0FrP0lDcjL/M3fNSZ80DhXg7Amzv3aW8oEi52P7Musl 8vJ1mBpFZ3ZP2gBr/1SE8Gxbc/XEmn0ac4fpCFd3k4YDkYoLUTIrBefLx4xmPGRXHf wwaZ5gVACPV+52BuXVgLfFBeQzJS41F+c5lhjTn/98IUwFJ6uPUbKRdm7baj3vNpcA 4UDh7yJAbIG2288Fz9h2fSgCbDbCAHDzwUYVKHrCQsrHJEcRjrRDyaTHN7shzllQcW qSbbWYc511zPQ== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 7B7DDCE0E61; Tue, 29 Apr 2025 18:05:25 -0700 (PDT) From: "Paul E. McKenney" To: linux-kernel@vger.kernel.org Cc: kernel-team@meta.com, Andrew Morton , Kuniyuki Iwashima , Mateusz Guzik , Petr Mladek , Steven Rostedt , John Ogness , Sergey Senozhatsky , Jon Pan-Doh , Bjorn Helgaas , Karolina Stolarek , "Paul E. McKenney" Subject: [PATCH v4 07/20] ratelimit: Count misses due to lock contention Date: Tue, 29 Apr 2025 18:05:11 -0700 Message-Id: <20250430010524.3614408-7-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The ___ratelimit() function simply returns zero ("do ratelimiting") if the trylock fails, but does not adjust the ->missed field. This means that the resulting dropped printk()s are dropped silently, which could seriously confuse people trying to do console-log-based debugging. Therefore, increment the ->missed field upon trylock failure. Link: https://lore.kernel.org/all/fbe93a52-365e-47fe-93a4-44a44547d601@paul= mck-laptop/ Link: https://lore.kernel.org/all/20250423115409.3425-1-spasswolf@web.de/ Signed-off-by: Paul E. McKenney Reviewed-by: Petr Mladek Cc: Andrew Morton Cc: Kuniyuki Iwashima Cc: Mateusz Guzik Cc: Steven Rostedt Cc: John Ogness Cc: Sergey Senozhatsky --- lib/ratelimit.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/ratelimit.c b/lib/ratelimit.c index 18703f92d73e7..19ad3cdbd1711 100644 --- a/lib/ratelimit.c +++ b/lib/ratelimit.c @@ -44,8 +44,10 @@ int ___ratelimit(struct ratelimit_state *rs, const char = *func) * in addition to the one that will be printed by * the entity that is holding the lock already: */ - if (!raw_spin_trylock_irqsave(&rs->lock, flags)) + if (!raw_spin_trylock_irqsave(&rs->lock, flags)) { + ratelimit_state_inc_miss(rs); return 0; + } =20 if (!rs->begin) rs->begin =3D jiffies; --=20 2.40.1 From nobody Sun Feb 8 23:41:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 5F3A922612 for ; Wed, 30 Apr 2025 01:05:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975127; cv=none; b=BUwBKq7UtApbDVdJjQqj0kB7yQi+/NhUpeC9u6Elo7pXKydJxOjZb2mWWosR30DogutkiiBMWfCryEDZFLTbhH+FuvwPVTF+/2DC5qppyhNrRkFeApRuLyn8ct+/LJcHVQAel9LvWsT2MEuBfD0Lj6C6ZTjgZXeL5R2niHx5cl0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975127; c=relaxed/simple; bh=/+i4NOmtNmiJBCyQ4d45E7T91BkwFjUY+T60jMduKPo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=IXTb3Y0eYZqbdKS/Xh28IOl4OvyQkqFInAhSLCM7DTY23kwcMka71LTR9koGuKj/qzcZ2eLXY6bJXhRhZl5Xel+Ub1RrgYG0qCOW8akOf5Hd24F/R7dzUWndn1xwAJQZhogq9ZBnrCvIR3QxJmBYlsGc0AyUwNsefaUPByTSpro= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=BRuqRK2R; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="BRuqRK2R" Received: by smtp.kernel.org (Postfix) with ESMTPSA id EA500C4CEF4; Wed, 30 Apr 2025 01:05:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1745975127; bh=/+i4NOmtNmiJBCyQ4d45E7T91BkwFjUY+T60jMduKPo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BRuqRK2RVomGeEtSOnh1tFi1dDiseR0kIrpbeMfHXrOW66HJ+s1o7R6OELVGpx61f xvos93VuzHGEWkB99HHZAKQ699eisrlfrynzFYZvH0womii0ikyf9qDXq3niyeui6b DU/95FHywTsIKI24cm3vs12fdRc3BDm/2KazWniYFYnz1R+YkxIIVDYMZDNNpCdFKH tCiAslazyXdpKnVWChAKJUqJ5NBEL07KNQ3uAn4FaQClQpTmfd7Cz53+2NyO9++eNy auMIgCvarf7gtui4DeFqsmwbNAKGhUwO+vX73RDftlm3FoNZg/3irfKOtzfnrN1OXr jRM6OmF6XFexQ== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 87287CE0F4E; Tue, 29 Apr 2025 18:05:25 -0700 (PDT) From: "Paul E. McKenney" To: linux-kernel@vger.kernel.org Cc: kernel-team@meta.com, Andrew Morton , Kuniyuki Iwashima , Mateusz Guzik , Petr Mladek , Steven Rostedt , John Ogness , Sergey Senozhatsky , Jon Pan-Doh , Bjorn Helgaas , Karolina Stolarek , "Paul E. McKenney" Subject: [PATCH v4 08/20] ratelimit: Avoid jiffies=0 special case Date: Tue, 29 Apr 2025 18:05:12 -0700 Message-Id: <20250430010524.3614408-8-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The ___ratelimit() function special-cases the jiffies-counter value of zero as "uninitialized". This works well on 64-bit systems, where the jiffies counter is not going to return to zero for more than half a billion years on systems with HZ=3D1000, but similar 32-bit systems take less than 50 days to wrap the jiffies counter. And although the consequences of wrapping the jiffies counter seem to be limited to minor confusion on the duration of the rate-limiting interval that happens to end at time zero, it is almost no work to avoid this confusion. Therefore, introduce a RATELIMIT_INITIALIZED bit to the ratelimit_state structure's ->flags field so that a ->begin value of zero is no longer special. Link: https://lore.kernel.org/all/fbe93a52-365e-47fe-93a4-44a44547d601@paul= mck-laptop/ Link: https://lore.kernel.org/all/20250423115409.3425-1-spasswolf@web.de/ Signed-off-by: Paul E. McKenney Reviewed-by: Petr Mladek Cc: Andrew Morton Cc: Kuniyuki Iwashima Cc: Mateusz Guzik Cc: Steven Rostedt Cc: John Ogness Cc: Sergey Senozhatsky --- include/linux/ratelimit.h | 2 +- include/linux/ratelimit_types.h | 1 + lib/ratelimit.c | 4 +++- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/include/linux/ratelimit.h b/include/linux/ratelimit.h index c78b92b3e5cd8..adfec24061d16 100644 --- a/include/linux/ratelimit.h +++ b/include/linux/ratelimit.h @@ -43,7 +43,7 @@ static inline void ratelimit_state_reset_interval(struct = ratelimit_state *rs, in =20 raw_spin_lock_irqsave(&rs->lock, flags); rs->interval =3D interval_init; - rs->begin =3D 0; + rs->flags &=3D ~RATELIMIT_INITIALIZED; rs->printed =3D 0; ratelimit_state_reset_miss(rs); raw_spin_unlock_irqrestore(&rs->lock, flags); diff --git a/include/linux/ratelimit_types.h b/include/linux/ratelimit_type= s.h index d21fe82b67f67..ef6711b6b229f 100644 --- a/include/linux/ratelimit_types.h +++ b/include/linux/ratelimit_types.h @@ -11,6 +11,7 @@ =20 /* issue num suppressed message on exit */ #define RATELIMIT_MSG_ON_RELEASE BIT(0) +#define RATELIMIT_INITIALIZED BIT(1) =20 struct ratelimit_state { raw_spinlock_t lock; /* protect the state */ diff --git a/lib/ratelimit.c b/lib/ratelimit.c index 19ad3cdbd1711..bd6e3b429e333 100644 --- a/lib/ratelimit.c +++ b/lib/ratelimit.c @@ -49,8 +49,10 @@ int ___ratelimit(struct ratelimit_state *rs, const char = *func) return 0; } =20 - if (!rs->begin) + if (!(rs->flags & RATELIMIT_INITIALIZED)) { rs->begin =3D jiffies; + rs->flags |=3D RATELIMIT_INITIALIZED; + } =20 if (time_is_before_jiffies(rs->begin + interval)) { int m =3D ratelimit_state_reset_miss(rs); --=20 2.40.1 From nobody Sun Feb 8 23:41:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 30DC717C77 for ; Wed, 30 Apr 2025 01:05:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975127; cv=none; b=ciZpi67MpnCu87m4Iu5X9ZYo4k3wskWp9vVuvripItmTtpsoi3hPK2D+l57ZhSWyc23AGow8VBejtYI9UvDe+c4remJFZ/ANfo0aNUbP82Vmhb2BY6vXo+hbolXpmaiTZepEwuyKgMJfy2UcNFn6B33TUYcaeX/qWep7+Lfn/OI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975127; c=relaxed/simple; bh=ntY2Sw3SfTBZAtAeM3adbzynb7JDNJCcwtct5/GAXqc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Mh4UT1uOhRAsn6PXxvqEVWHrVSigBkATJR7jJtOO1EGSKdjBo/fMyMcHAUfeJPjfcbTCSHZ6pGG/WAWAFRNxl34i5n69q/cfl6F7wcm6B3RATNIInt3LkiW7xhz25WA24SAvpC3gMPl3g8Rc6q2ClK2zTAjIjCc4q0kzMdcQhUo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=XkBGKBlH; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="XkBGKBlH" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 02D77C4CEED; Wed, 30 Apr 2025 01:05:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1745975127; bh=ntY2Sw3SfTBZAtAeM3adbzynb7JDNJCcwtct5/GAXqc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XkBGKBlHwR6pN+H09J8wazqbf5d+JPrS/+9xwhoAAvpL7nY7xPDmZSwD28eiNsgGi aadFusgQHmZuMX1yIi42v+zzoQilitGoSSoxak6lAmWGl1be31uD8TUE5h9ZPKrzF1 ql/55/fWyncn9PvLD7kCHKFOxL1If4u+feMaFqelxzQlCIuoPTDyMCa87qIuhXrun6 0zIJyhPaZgcFGGC26NnsqtwkThJJBtHnni0kaJfNHM8ZlZBrM4QnGd6nn4bBrvdd7M J4KPuC1g1R3Giw9f8nke84tIS1nNUAvyJs/MwKFldYU/MHcGcIq1mVJym9v+Ni1tww d5y2zlvfAd07Q== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 93526CE0F53; Tue, 29 Apr 2025 18:05:25 -0700 (PDT) From: "Paul E. McKenney" To: linux-kernel@vger.kernel.org Cc: kernel-team@meta.com, Andrew Morton , Kuniyuki Iwashima , Mateusz Guzik , Petr Mladek , Steven Rostedt , John Ogness , Sergey Senozhatsky , Jon Pan-Doh , Bjorn Helgaas , Karolina Stolarek , "Paul E . McKenney" Subject: [PATCH v4 09/20] ratelimit: Reduce ___ratelimit() false-positive rate limiting Date: Tue, 29 Apr 2025 18:05:13 -0700 Message-Id: <20250430010524.3614408-9-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Petr Mladek Retain the locked design, but check rate-limiting even when the lock could not be acquired. Link: https://lore.kernel.org/all/Z_VRo63o2UsVoxLG@pathway.suse.cz/ Link: https://lore.kernel.org/all/fbe93a52-365e-47fe-93a4-44a44547d601@paul= mck-laptop/ Link: https://lore.kernel.org/all/20250423115409.3425-1-spasswolf@web.de/ Signed-off-by: Petr Mladek Signed-off-by: Paul E. McKenney Cc: Petr Mladek Cc: Andrew Morton Cc: Kuniyuki Iwashima Cc: Mateusz Guzik Cc: Steven Rostedt Cc: John Ogness Cc: Sergey Senozhatsky --- include/linux/ratelimit.h | 2 +- include/linux/ratelimit_types.h | 2 +- lib/ratelimit.c | 51 ++++++++++++++++++++++++--------- 3 files changed, 40 insertions(+), 15 deletions(-) diff --git a/include/linux/ratelimit.h b/include/linux/ratelimit.h index adfec24061d16..7aaad158ee373 100644 --- a/include/linux/ratelimit.h +++ b/include/linux/ratelimit.h @@ -44,7 +44,7 @@ static inline void ratelimit_state_reset_interval(struct = ratelimit_state *rs, in raw_spin_lock_irqsave(&rs->lock, flags); rs->interval =3D interval_init; rs->flags &=3D ~RATELIMIT_INITIALIZED; - rs->printed =3D 0; + atomic_set(&rs->rs_n_left, rs->burst); ratelimit_state_reset_miss(rs); raw_spin_unlock_irqrestore(&rs->lock, flags); } diff --git a/include/linux/ratelimit_types.h b/include/linux/ratelimit_type= s.h index ef6711b6b229f..b19c4354540ab 100644 --- a/include/linux/ratelimit_types.h +++ b/include/linux/ratelimit_types.h @@ -18,7 +18,7 @@ struct ratelimit_state { =20 int interval; int burst; - int printed; + atomic_t rs_n_left; atomic_t missed; unsigned int flags; unsigned long begin; diff --git a/lib/ratelimit.c b/lib/ratelimit.c index bd6e3b429e333..90c9fe57eb422 100644 --- a/lib/ratelimit.c +++ b/lib/ratelimit.c @@ -39,12 +39,22 @@ int ___ratelimit(struct ratelimit_state *rs, const char= *func) return 1; =20 /* - * If we contend on this state's lock then almost - * by definition we are too busy to print a message, - * in addition to the one that will be printed by - * the entity that is holding the lock already: + * If we contend on this state's lock then just check if + * the current burst is used or not. It might cause + * false positive when we are past the interval and + * the current lock owner is just about to reset it. */ if (!raw_spin_trylock_irqsave(&rs->lock, flags)) { + unsigned int rs_flags =3D READ_ONCE(rs->flags); + + if (rs_flags & RATELIMIT_INITIALIZED && burst) { + int n_left; + + n_left =3D atomic_dec_return(&rs->rs_n_left); + if (n_left >=3D 0) + return 1; + } + ratelimit_state_inc_miss(rs); return 0; } @@ -52,27 +62,42 @@ int ___ratelimit(struct ratelimit_state *rs, const char= *func) if (!(rs->flags & RATELIMIT_INITIALIZED)) { rs->begin =3D jiffies; rs->flags |=3D RATELIMIT_INITIALIZED; + atomic_set(&rs->rs_n_left, rs->burst); } =20 if (time_is_before_jiffies(rs->begin + interval)) { - int m =3D ratelimit_state_reset_miss(rs); + int m; + + /* + * Reset rs_n_left ASAP to reduce false positives + * in parallel calls, see above. + */ + atomic_set(&rs->rs_n_left, rs->burst); + rs->begin =3D jiffies; =20 + m =3D ratelimit_state_reset_miss(rs); if (m) { if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE)) { printk_deferred(KERN_WARNING "%s: %d callbacks suppressed\n", func, m); } } - rs->begin =3D jiffies; - rs->printed =3D 0; } - if (burst && burst > rs->printed) { - rs->printed++; - ret =3D 1; - } else { - ratelimit_state_inc_miss(rs); - ret =3D 0; + if (burst) { + int n_left; + + /* The burst might have been taken by a parallel call. */ + n_left =3D atomic_dec_return(&rs->rs_n_left); + if (n_left >=3D 0) { + ret =3D 1; + goto unlock_ret; + } } + + ratelimit_state_inc_miss(rs); + ret =3D 0; + +unlock_ret: raw_spin_unlock_irqrestore(&rs->lock, flags); =20 return ret; --=20 2.40.1 From nobody Sun Feb 8 23:41:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 A2ECE3F9D2 for ; Wed, 30 Apr 2025 01:05:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975127; cv=none; b=iOXIl2Kz6HRRQgDkfI8SRCrX8N8tUEMYtU0yZR6w2O9FUire91cKvY4ft5ALaJzTDGUVkPxRlhZR+E9VLklYN9y8RCV6/o999V0pT4LMQxSTQuaAtcYI41fiKxOg2qEWtFHQH8uhj8RBSkUYBkY/5pGcCwyfDjvLck0VyTB2VH0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975127; c=relaxed/simple; bh=xVs4QemZgCqU8+2ERmMbebq38vqQbBEp640MidYy624=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=tphGnU4zV2evpqd9Ss9cWP2idVKdum4UbE0fIe15CmZ1rBh16UCd+l2HbZ5H70j9aajFMXy4DXxOl6R8gOtoiOtZbTvV0RL7Ix+aZDBmTpv883Ix4UAbi8mCD1zA9b13iyz8h25eJaiH8kP8Ebj+d16zSlj5kxjSdP+G+6bNEKk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=CtYUznKR; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="CtYUznKR" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3E119C4CEEF; Wed, 30 Apr 2025 01:05:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1745975127; bh=xVs4QemZgCqU8+2ERmMbebq38vqQbBEp640MidYy624=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CtYUznKR250P3dvtYOd90HMm+n+4obD6nB47wOlv3DjPO2cDbTRehpr2bxhAnILB0 y66Fhu/d09d8Z3JEVGGJMDRLqrWTvnqs4xbuo8DrBAi87qyF2CDlL+L8uhHlDZj/5B MUPryFnD6NPnsFIZyNMz3N/7fCURAv+jKvA43sOi1kzK2l/zUf9MPdnSRIs3zxSOte YkvPQqg5VITzEb7IDrdzx4YYPOUAa+rv1VnJBWoRob1wgkGYTxDcxcJPlDkNUqYlUi NyqBvdaJvGE5WetC48BIgV7TdYt2vCLb94rJBVSwNA1XZKL0pNYhqSpJTwsfzqqiP3 oHTB5IL3FpohQ== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id A35BECE0F7D; Tue, 29 Apr 2025 18:05:25 -0700 (PDT) From: "Paul E. McKenney" To: linux-kernel@vger.kernel.org Cc: kernel-team@meta.com, Andrew Morton , Kuniyuki Iwashima , Mateusz Guzik , Petr Mladek , Steven Rostedt , John Ogness , Sergey Senozhatsky , Jon Pan-Doh , Bjorn Helgaas , Karolina Stolarek , "Paul E. McKenney" , Bert Karwatzki , "Aithal, Srikanth" , Mark Brown Subject: [PATCH v4 10/20] ratelimit: Allow zero ->burst to disable ratelimiting Date: Tue, 29 Apr 2025 18:05:14 -0700 Message-Id: <20250430010524.3614408-10-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" If ->interval is zero, then rate-limiting will be disabled. Alternatively, if interval is greater than zero and ->burst is zero, then rate-limiting will be applied unconditionally. The point of this distinction is to handle current users that pass zero-initialized ratelimit_state structures to ___ratelimit(), and in such cases the ->lock field will be uninitialized. Acquiring ->lock in this case is clearly not a strategy to win. Therefore, make this classification be lockless. Note that although negative ->interval and ->burst happen to be treated as if they were zero, this is an accident of the current implementation. The semantics of negative values for these fields is subject to change without notice. Especially given that Bert Karwatzki determined that no current calls to ___ratelimit() ever have negative values for these fields. This commit replaces an earlier buggy versions. Link: https://lore.kernel.org/all/fbe93a52-365e-47fe-93a4-44a44547d601@paul= mck-laptop/ Link: https://lore.kernel.org/all/20250423115409.3425-1-spasswolf@web.de/ Reported-by: Bert Karwatzki Reported-by: "Aithal, Srikanth" Closes: https://lore.kernel.org/all/20250423115409.3425-1-spasswolf@web.de/ Reported-by: Mark Brown Closes: https://lore.kernel.org/all/257c3b91-e30f-48be-9788-d27a4445a416@si= rena.org.uk/ Signed-off-by: Paul E. McKenney Tested-by: "Aithal, Srikanth" Reviewed-by: Petr Mladek Cc: Andrew Morton Cc: Kuniyuki Iwashima Cc: Mateusz Guzik Cc: Steven Rostedt Cc: John Ogness Cc: Sergey Senozhatsky --- lib/ratelimit.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/ratelimit.c b/lib/ratelimit.c index 90c9fe57eb422..7a7ba4835639f 100644 --- a/lib/ratelimit.c +++ b/lib/ratelimit.c @@ -35,8 +35,12 @@ int ___ratelimit(struct ratelimit_state *rs, const char = *func) unsigned long flags; int ret; =20 - if (!interval) - return 1; + if (interval <=3D 0 || burst <=3D 0) { + ret =3D interval =3D=3D 0 || burst > 0; + if (!ret) + ratelimit_state_inc_miss(rs); + return ret; + } =20 /* * If we contend on this state's lock then just check if --=20 2.40.1 From nobody Sun Feb 8 23:41:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 5E3FF4685 for ; Wed, 30 Apr 2025 01:05:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975127; cv=none; b=qzj1vkqg39uy2IzvtcfFyXL2lubCJtZmne4yC57dt97LpRYKoZYDn9cCut2ShC+Tb7iXebgug+OyYURBxoTGaiB1LW7XVKB814lwk8FzCWvQq1yX3b+O+PR0pjTdQo5SoEo5oF5UzFtbU1M7Feg8UCdinVtp7n5qcbxJw7xq9ec= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975127; c=relaxed/simple; bh=eAuW+1XsPwY9rkuRnnnWelC3ErbYzxH9EQx5wYh1v9g=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=bTuT5p+c4TMhOT6srpCaAFV4SWYJ9ICiupJA2ZnAlDEjyqa4Y1B1fUM5HkVui5F6ZIg5s3P2IKQGa6ceDAEN2i2mimHlJwU7EpY8uuPCMlUKAYb/U1LFwYrb+DRwDON15BunjlLPBAyrR0Zfbvj+H9KzJ0I6/aBxhLsr+jnsQ8Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=m6YjAchR; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="m6YjAchR" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3DE6FC4CEE3; Wed, 30 Apr 2025 01:05:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1745975127; bh=eAuW+1XsPwY9rkuRnnnWelC3ErbYzxH9EQx5wYh1v9g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=m6YjAchRtZqVp0QpuGrQ3iIeQ8+iNXWcIIRSPu3ybnzFq9IzTdYSLuLhAT8CKN3sO jRgSpG3qV43dpCf7Zhiv8BX6M0nEhg85M+Yh8DoEPBOMurXHW6cmv/ab8R9TDqbb3T MOntnXqmiUsX6olHGor6fgBqPSqCiCU/BJ6PfpNVv1YVLa4q91KrDlas8BDajepSkx SutAPiSulHipwFl3MKVnXj6nCt1LszSzz2OOGq6OETLIc6M8df4obi83ZMkR8MbgxT HuSIDhaEQORKbR31K/dcIYN0wwwa3LQJH1A3iEYTUZpzaMOlDHO+pKg2qWYxekVhGr FdYZzrVU+ntdw== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id AF0C0CE10DB; Tue, 29 Apr 2025 18:05:25 -0700 (PDT) From: "Paul E. McKenney" To: linux-kernel@vger.kernel.org Cc: kernel-team@meta.com, Andrew Morton , Kuniyuki Iwashima , Mateusz Guzik , Petr Mladek , Steven Rostedt , John Ogness , Sergey Senozhatsky , Jon Pan-Doh , Bjorn Helgaas , Karolina Stolarek , "Paul E. McKenney" Subject: [PATCH v4 11/20] ratelimit: Force re-initialization when rate-limiting re-enabled Date: Tue, 29 Apr 2025 18:05:15 -0700 Message-Id: <20250430010524.3614408-11-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently, if rate limiting is disabled, ___ratelimit() does an immediate early return with no state changes. This can result in false-positive drops when re-enabling rate limiting. Therefore, mark the ratelimit_state structure "uninitialized" when rate limiting is disabled. [ paulmck: Apply Petr Mladek feedback. ] Link: https://lore.kernel.org/all/fbe93a52-365e-47fe-93a4-44a44547d601@paul= mck-laptop/ Link: https://lore.kernel.org/all/20250423115409.3425-1-spasswolf@web.de/ Signed-off-by: Paul E. McKenney Cc: Petr Mladek Cc: Andrew Morton Cc: Kuniyuki Iwashima Cc: Mateusz Guzik Cc: Steven Rostedt Cc: John Ogness Cc: Sergey Senozhatsky --- lib/ratelimit.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/ratelimit.c b/lib/ratelimit.c index 7a7ba4835639f..7d4f4e241213e 100644 --- a/lib/ratelimit.c +++ b/lib/ratelimit.c @@ -35,11 +35,24 @@ int ___ratelimit(struct ratelimit_state *rs, const char= *func) unsigned long flags; int ret; =20 + /* + * Zero interval says never limit, otherwise, non-positive burst + * says always limit. + */ if (interval <=3D 0 || burst <=3D 0) { ret =3D interval =3D=3D 0 || burst > 0; + if (!(READ_ONCE(rs->flags) & RATELIMIT_INITIALIZED) || (!interval && !bu= rst) || + !raw_spin_trylock_irqsave(&rs->lock, flags)) { + if (!ret) + ratelimit_state_inc_miss(rs); + return ret; + } + + /* Force re-initialization once re-enabled. */ + rs->flags &=3D ~RATELIMIT_INITIALIZED; if (!ret) ratelimit_state_inc_miss(rs); - return ret; + goto unlock_ret; } =20 /* --=20 2.40.1 From nobody Sun Feb 8 23:41:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 63CBB262A6 for ; Wed, 30 Apr 2025 01:05:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975127; cv=none; b=d8LbDjZ3Vtj9tgEjbNt6v6O0twntWsFnalEeHtdhMMlEQxvOR5dVLknADjeecR+KgoL0Ifc39SBiUSgPLO6HHtTPe/x9vVvu9Zu+UNJqVTUEsIdLG+pHQn97IsblrHbaA+ix9kouAgNOJjBTSnD+XIHfjmwF2hn0V1CgI3vV0DY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975127; c=relaxed/simple; bh=x6kgo4O9hta9sDmEsHUTl1Ls9HFDrMxc1Cr1vGjF/bo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=dBnpFqVKY9IOUbdGTyUMo/663O9hPBM9DLH4jnZKURvL87og3U/EePIi5B5Rmdg/yVwiLeyWnl4iH4ktjFUzLrZyyOb+JfULG6CTfWeaRtfgJBP7QckjB05STCyOik92v+2W+2fMa2zUPnVj3q9ovRNPZQPJ1dZQkTtp1rjPpGU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=NT2J/2tG; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="NT2J/2tG" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3E885C4CEF1; Wed, 30 Apr 2025 01:05:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1745975127; bh=x6kgo4O9hta9sDmEsHUTl1Ls9HFDrMxc1Cr1vGjF/bo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NT2J/2tGCdqUGnNFOcq7QiBa1f382Z5ZT5ZbxoAAJwcpp8RX2566VuOwZsl5yKuUV hUOlSXKpkJRJ2sObAOiip7fAEwnAl0mqp/E2Me6U7hk/SADxNrZEDSXjMggWCWyDmB YInif51EnfnZXymtBSYWMGpgteZtTQ9LtOd2eVldIAMRGt17X/2mIuW0U2fnZlxLKI R4YkyNzejtwYEGRzt1HYMMCah6vsivkuPlgZtgJEuEsSuWfHsYUx5mwkHlZ2+bce/+ kho5qTPlT6lGdTtQg6LCO8S8kBWssGTnzGh2duFzVDdHEyGVgR4YFEU/i5tsMIJE49 Pu9ynIufxD7Hg== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id BBA5FCE10F4; Tue, 29 Apr 2025 18:05:25 -0700 (PDT) From: "Paul E. McKenney" To: linux-kernel@vger.kernel.org Cc: kernel-team@meta.com, Andrew Morton , Kuniyuki Iwashima , Mateusz Guzik , Petr Mladek , Steven Rostedt , John Ogness , Sergey Senozhatsky , Jon Pan-Doh , Bjorn Helgaas , Karolina Stolarek , "Paul E. McKenney" Subject: [PATCH v4 12/20] ratelimit: Don't flush misses counter if RATELIMIT_MSG_ON_RELEASE Date: Tue, 29 Apr 2025 18:05:16 -0700 Message-Id: <20250430010524.3614408-12-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Restore the previous semantics where the misses counter is unchanged if the RATELIMIT_MSG_ON_RELEASE flag is set. Link: https://lore.kernel.org/all/fbe93a52-365e-47fe-93a4-44a44547d601@paul= mck-laptop/ Link: https://lore.kernel.org/all/20250423115409.3425-1-spasswolf@web.de/ Signed-off-by: Paul E. McKenney Reviewed-by: Petr Mladek Cc: Andrew Morton Cc: Kuniyuki Iwashima Cc: Mateusz Guzik Cc: Steven Rostedt Cc: John Ogness Cc: Sergey Senozhatsky --- lib/ratelimit.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/ratelimit.c b/lib/ratelimit.c index 7d4f4e241213e..4e520d029d28f 100644 --- a/lib/ratelimit.c +++ b/lib/ratelimit.c @@ -92,9 +92,9 @@ int ___ratelimit(struct ratelimit_state *rs, const char *= func) atomic_set(&rs->rs_n_left, rs->burst); rs->begin =3D jiffies; =20 - m =3D ratelimit_state_reset_miss(rs); - if (m) { - if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE)) { + if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE)) { + m =3D ratelimit_state_reset_miss(rs); + if (m) { printk_deferred(KERN_WARNING "%s: %d callbacks suppressed\n", func, m); } --=20 2.40.1 From nobody Sun Feb 8 23:41:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 771A328DB3 for ; Wed, 30 Apr 2025 01:05:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975127; cv=none; b=CTfUIyZqAn1DbD//ZTWYPKhLKTFhYW3khiIogxyJVYuMGUfBKM4FRe204RfKynZGohl/M0QQVynwdDKWR3WtpBoTwMHiv9D0Po8+i7YIyd2D5uP0zA1ZR2EhEk3QDoTdsS1jIhcPZyYDTwRxP4ij2CtCg9ktDz6kSXvXdnt6lSc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975127; c=relaxed/simple; bh=QCSRO+sN3BeKZWvK+OThs6qhu1d5NNPNgRJTZ+bBQSc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=A0rTXvoxfkS66gbSv1q3n27X3OlGEtHTaZDb1zMum06oaoDQlutGlBlnf3i2+i1jKpKUW3BiyrIhB/a9XT6DwvIU6G5UJpNd0Jp7Ki9I+aw6/yBilD2FtWh8Hz7w1Smj8O6Du6qh1Fk2fsYr7ItIdl27yQ0mo4CVhVPlpgTJ0sI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Pdg9wyzo; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Pdg9wyzo" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 44259C4CEF0; Wed, 30 Apr 2025 01:05:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1745975127; bh=QCSRO+sN3BeKZWvK+OThs6qhu1d5NNPNgRJTZ+bBQSc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Pdg9wyzomGrOJ1zK/Yr8rB3YO6DRbT5FUmF9dpvL7VmFzyKaPGcRcPhPjiGpLt/a1 BGaHdO7i7X8K+MZJlFZZU9nTUVhJFXbkrFX117jb0ZDpszk5V75LxuLsqLiKlUI7uU oFTVOEo7BEeqNN3IgZeuWKNllDHZpmZEmO2QnuOI2ElTWGIUjBy6M3qqbQZ1ZXxAxE giCt3OmIdxdHSPiFbDME7n04agpmssmr2Yhb17ySe04eelbHk0i+ais0SnVkr49+FU VT+KqrN8XLeXwGJKVY8nTaLJoS7DQk5HxLB1Lu7AK1aKg0YFNeKQpT+MUEc+z0L5eo r8gEn9Jeu7Qqg== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id C6FD5CE1129; Tue, 29 Apr 2025 18:05:25 -0700 (PDT) From: "Paul E. McKenney" To: linux-kernel@vger.kernel.org Cc: kernel-team@meta.com, Andrew Morton , Kuniyuki Iwashima , Mateusz Guzik , Petr Mladek , Steven Rostedt , John Ogness , Sergey Senozhatsky , Jon Pan-Doh , Bjorn Helgaas , Karolina Stolarek , "Paul E. McKenney" Subject: [PATCH v4 13/20] ratelimit: Avoid atomic decrement if already rate-limited Date: Tue, 29 Apr 2025 18:05:17 -0700 Message-Id: <20250430010524.3614408-13-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently, if the lock could not be acquired, the code unconditionally does an atomic decrement on ->rs_n_left, even if that atomic operation is guaranteed to return a limit-rate verdict. This incurs needless overhead and also raises the spectre of counter wrap. Therefore, do the atomic decrement only if there is some chance that rates won't be limited. Link: https://lore.kernel.org/all/fbe93a52-365e-47fe-93a4-44a44547d601@paul= mck-laptop/ Link: https://lore.kernel.org/all/20250423115409.3425-1-spasswolf@web.de/ Signed-off-by: Paul E. McKenney Reviewed-by: Petr Mladek Cc: Andrew Morton Cc: Kuniyuki Iwashima Cc: Mateusz Guzik Cc: Steven Rostedt Cc: John Ogness Cc: Sergey Senozhatsky --- lib/ratelimit.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/ratelimit.c b/lib/ratelimit.c index 4e520d029d28f..a7aaebb7a7189 100644 --- a/lib/ratelimit.c +++ b/lib/ratelimit.c @@ -65,8 +65,10 @@ int ___ratelimit(struct ratelimit_state *rs, const char = *func) unsigned int rs_flags =3D READ_ONCE(rs->flags); =20 if (rs_flags & RATELIMIT_INITIALIZED && burst) { - int n_left; + int n_left =3D atomic_read(&rs->rs_n_left); =20 + if (n_left <=3D 0) + return 0; n_left =3D atomic_dec_return(&rs->rs_n_left); if (n_left >=3D 0) return 1; --=20 2.40.1 From nobody Sun Feb 8 23:41:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 A2F0241AAC for ; Wed, 30 Apr 2025 01:05:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975127; cv=none; b=cpASn1Qe3/cMOC05fR4/QEhpZWII9CO7r1fNG3CO94o2u4KuGmfvzDLIcH69CrA+votal7vEtNinBPeILYaU3Xb5NBzmb/pTwrtfG4MNigW5G1Gxz4pdFpttMgW+/AeDPAEDAYwN18aWumGEjChv+K5XluLJ8Zg/dGD6I5SHMBw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975127; c=relaxed/simple; bh=NCii0AygsQ7t/hzmWNQsffMOxKXMFGYLlumd3Gn7O9s=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=lpmF6Uuqr/c1Nk00frBKN4LYBx2V/2IAhovjAQT//LOhMm+WPIDUli/fIyZJniXLCJ1ggkXAhB4GNHrgD1RMASB6pr67QRkNuLYgGR2P25sfa521nG2At4ss8JiwWa0X7K2uVAcSy9HH30HY8oB4CAbeDW4S/N0m1KzCXWhrdqg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=q27b/DA2; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="q27b/DA2" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4D876C4CEF2; Wed, 30 Apr 2025 01:05:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1745975127; bh=NCii0AygsQ7t/hzmWNQsffMOxKXMFGYLlumd3Gn7O9s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=q27b/DA2L68ZFp80NHX0Cpuf7QxNXmW6PXrwrGwXEdQ4pReA+bU2TTG9GH4gRtCS3 lozfkTEW+zrY6C4oyPDbsxU5+5khX7JRt85tODrQBP5SXyv17vO7P/jGmrSNWcKzFK Xw2+g1qsnssvOy7RAaD5kQAvJhlck3l41K5TxogSPUI+NwBsaTh+1pRlucmMxLShik dBmMYRQwVuypD4+oun+TswTyHxz6VAnxC9deMUBrxeCK6UWLkp6NIV6M70uMZA7OVf zcSF7zs/aS4BSGKft9O2XvyV1U0l/5H51w1WeLjzxlb5HGd0ILKCUbf5+s/xdh3Qo2 lCSMl1v+SoTqQ== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id CE475CE112C; Tue, 29 Apr 2025 18:05:25 -0700 (PDT) From: "Paul E. McKenney" To: linux-kernel@vger.kernel.org Cc: kernel-team@meta.com, Andrew Morton , Kuniyuki Iwashima , Mateusz Guzik , Petr Mladek , Steven Rostedt , John Ogness , Sergey Senozhatsky , Jon Pan-Doh , Bjorn Helgaas , Karolina Stolarek , "Paul E. McKenney" Subject: [PATCH v4 14/20] ratelimit: Avoid atomic decrement under lock if already rate-limited Date: Tue, 29 Apr 2025 18:05:18 -0700 Message-Id: <20250430010524.3614408-14-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently, if the lock is acquired, the code unconditionally does an atomic decrement on ->rs_n_left, even if that atomic operation is guaranteed to return a limit-rate verdict. A limit-rate verdict will in fact be the common case when something is spewing into a rate limit. This unconditional atomic operation incurs needless overhead and also raises the spectre of counter wrap. Therefore, do the atomic decrement only if there is some chance that rates won't be limited. Link: https://lore.kernel.org/all/fbe93a52-365e-47fe-93a4-44a44547d601@paul= mck-laptop/ Link: https://lore.kernel.org/all/20250423115409.3425-1-spasswolf@web.de/ Signed-off-by: Paul E. McKenney Reviewed-by: Petr Mladek Cc: Andrew Morton Cc: Kuniyuki Iwashima Cc: Mateusz Guzik Cc: Steven Rostedt Cc: John Ogness Cc: Sergey Senozhatsky --- lib/ratelimit.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/ratelimit.c b/lib/ratelimit.c index a7aaebb7a7189..ab8472edeb1d2 100644 --- a/lib/ratelimit.c +++ b/lib/ratelimit.c @@ -103,13 +103,16 @@ int ___ratelimit(struct ratelimit_state *rs, const ch= ar *func) } } if (burst) { - int n_left; + int n_left =3D atomic_read(&rs->rs_n_left); =20 /* The burst might have been taken by a parallel call. */ - n_left =3D atomic_dec_return(&rs->rs_n_left); - if (n_left >=3D 0) { - ret =3D 1; - goto unlock_ret; + + if (n_left > 0) { + n_left =3D atomic_dec_return(&rs->rs_n_left); + if (n_left >=3D 0) { + ret =3D 1; + goto unlock_ret; + } } } =20 --=20 2.40.1 From nobody Sun Feb 8 23:41:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 8C4C6CA5A for ; Wed, 30 Apr 2025 01:05:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975127; cv=none; b=o/9IJ+dUg5ST8R5OXValVCHV0t+RY7TPNeQnb4MvKCmNuR6KA9kXJedUBsGwrLOGmEj5spaF86B74yBM5drICkF3gnzLAD1m3Cy3pCRqFYzxq+SP6fIa+gWbJsXHnJvAWZ5IlLZQE6D4op84H0vlU0PJ+ipG4QeGOKyVLmpFAHI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975127; c=relaxed/simple; bh=pXpLtvkklfQw6NZrHmnRsqa8G0gH1/KimuwtRPQx/mI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=eby6CpvnWqYwyLp+r6BHans9eAT3p0YVPsfjvGw0SvtUmWglbBAgYDpfFpJBR3ffXpEmSzEkOCVM03JO42g6S9afk9UfGVB0slWtxjHLlIxdROmFjmbBfExWKGb/nv0M4CLg/E4yaHnrcNfTNwWl3gg4vLhpH5Bke/yw04j0Oeo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ED+XYwc7; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ED+XYwc7" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5D331C4CEF7; Wed, 30 Apr 2025 01:05:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1745975127; bh=pXpLtvkklfQw6NZrHmnRsqa8G0gH1/KimuwtRPQx/mI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ED+XYwc7kFJjCb1P6TrRUoiJ6MBC6KHV8pv1h+Pp2ZN0JY5lK+YQ7QRFjDObVuReN fjZWBQ3Emxj/bBWD3gu+NaRsSzdsI/UDcyeaycQzbQ8qeMk3eS0kd3AhmGHxHNZ3hr 5nf7enlZOU57a5/M1Jxrb4NCdNcTHBO8cDyn1kPixlRh8iP3H+MgOA7pix5K5UIqO/ 6FIjtXjKWw8mzvcAF3NV0LwaGuoAwbDhYVEqKhASfcqny/GIG0JMkuvioEQ80kV7Q1 ZFTGsQ4wkLDWnapIWyh1jDbe3Bbb+69ZBbJypLOiFO2mCAEOBNV55vWvybmhFmnAN/ Kz70Tpz9HYMAA== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id D11ADCE1160; Tue, 29 Apr 2025 18:05:25 -0700 (PDT) From: "Paul E. McKenney" To: linux-kernel@vger.kernel.org Cc: kernel-team@meta.com, Andrew Morton , Kuniyuki Iwashima , Mateusz Guzik , Petr Mladek , Steven Rostedt , John Ogness , Sergey Senozhatsky , Jon Pan-Doh , Bjorn Helgaas , Karolina Stolarek , "Paul E . McKenney" Subject: [PATCH v4 15/20] ratelimit: Warn if ->interval or ->burst are negative Date: Tue, 29 Apr 2025 18:05:19 -0700 Message-Id: <20250430010524.3614408-15-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Petr Mladek Currently, ___ratelimit() treats a negative ->interval or ->burst as if it was zero, but this is an accident of the current implementation. Therefore, splat in this case, which might have the benefit of detecting use of uninitialized ratelimit_state structures on the one hand or easing addition of new features on the other. Link: https://lore.kernel.org/all/fbe93a52-365e-47fe-93a4-44a44547d601@paul= mck-laptop/ Link: https://lore.kernel.org/all/20250423115409.3425-1-spasswolf@web.de/ Signed-off-by: Petr Mladek Signed-off-by: Paul E. McKenney Cc: Andrew Morton Cc: Kuniyuki Iwashima Cc: Mateusz Guzik Cc: Steven Rostedt Cc: John Ogness Cc: Sergey Senozhatsky --- lib/ratelimit.c | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/ratelimit.c b/lib/ratelimit.c index ab8472edeb1d2..6a5cb05413013 100644 --- a/lib/ratelimit.c +++ b/lib/ratelimit.c @@ -40,6 +40,7 @@ int ___ratelimit(struct ratelimit_state *rs, const char *= func) * says always limit. */ if (interval <=3D 0 || burst <=3D 0) { + WARN_ONCE(interval < 0 || burst < 0, "Negative interval (%d) or burst (%= d): Uninitialized ratelimit_state structure?\n", interval, burst); ret =3D interval =3D=3D 0 || burst > 0; if (!(READ_ONCE(rs->flags) & RATELIMIT_INITIALIZED) || (!interval && !bu= rst) || !raw_spin_trylock_irqsave(&rs->lock, flags)) { --=20 2.40.1 From nobody Sun Feb 8 23:41:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 872002C1A2 for ; Wed, 30 Apr 2025 01:05:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975127; cv=none; b=Xkm33ZS/fUQqnUCJUZleqRDBcHAIQqWkTY8c6k+Y3IarDSV6lVH+r/DYkVUTRAYdYBzJsBcidIJJWX1rQtvpZ43goZGAfks983IDnmmOlsL9pu9AkDdvlX+v7suAY2mivFxEfYbN4tisMbhWaQv4CdtpeXe3y+8WH8s55FdE0Hs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975127; c=relaxed/simple; bh=F8T2xxbMxQXl7LOrVaC3+b+nco6tPLr/P6VQcN8CUaQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=RjFFr6xt4FSDttiLq0VMK+r23W7EwJmdbk6X0dcXujIpXCvcwbVyY4doN6Sa9kpb751PaScv1yvwkbUWvxcMMnS+HLPiZsvYVEErbAcsfTEo50LDCbGAuwgyaTuXL5fZIjeLxhBXp6QcSLeW5mBGNNgFSBpOjCdy8qpS2PPNKTk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=JGfe98lD; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="JGfe98lD" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 61E6FC4CEF8; Wed, 30 Apr 2025 01:05:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1745975127; bh=F8T2xxbMxQXl7LOrVaC3+b+nco6tPLr/P6VQcN8CUaQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JGfe98lD74Q2P4ub0kckHKJcw4S5ECp15JpVhSbVU7sAk/gujtu8Rd9DIE16J1nUe DAgJXMJPdtZUHX7XFHClXFzSQLhZZK2WqAtzMEKchl/5Oona4G/RO3vmYvmNnMpi9E uNbb3xMpIpmrQSGupPG9eOumrgolZVA/go0QgQb+KZMuhpUhvx/ORtEvR1tmeWLpv3 8rkW12SHexULmdjT9p/BbPyNq8y/MfqyE9YGeGmUnA1nHGHeLwYpG2hzaE9uO/3q8R hCK7NXqdzzv23tYvA2tVPGm2iSSNvgYUO2LH08DsfN/8cOMH2Qbfpue/N6hEaJZRs5 qVoQliR2MJtXw== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id D440BCE077B; Tue, 29 Apr 2025 18:05:25 -0700 (PDT) From: "Paul E. McKenney" To: linux-kernel@vger.kernel.org Cc: kernel-team@meta.com, Andrew Morton , Kuniyuki Iwashima , Mateusz Guzik , Petr Mladek , Steven Rostedt , John Ogness , Sergey Senozhatsky , Jon Pan-Doh , Bjorn Helgaas , Karolina Stolarek , "Paul E. McKenney" Subject: [PATCH v4 16/20] ratelimit: Simplify common-case exit path Date: Tue, 29 Apr 2025 18:05:20 -0700 Message-Id: <20250430010524.3614408-16-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" By making "ret" always be initialized, and moving the final call to ratelimit_state_inc_miss() out from under the lock, we save a goto and a couple lines of code. This also saves a couple of lines of code from the unconditional enable/disable slowpath. Link: https://lore.kernel.org/all/fbe93a52-365e-47fe-93a4-44a44547d601@paul= mck-laptop/ Link: https://lore.kernel.org/all/20250423115409.3425-1-spasswolf@web.de/ Signed-off-by: Paul E. McKenney Reviewed-by: Petr Mladek Cc: Andrew Morton Cc: Kuniyuki Iwashima Cc: Mateusz Guzik Cc: Steven Rostedt Cc: John Ogness Cc: Sergey Senozhatsky --- lib/ratelimit.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/lib/ratelimit.c b/lib/ratelimit.c index 6a5cb05413013..7c6e864306db2 100644 --- a/lib/ratelimit.c +++ b/lib/ratelimit.c @@ -33,7 +33,7 @@ int ___ratelimit(struct ratelimit_state *rs, const char *= func) int interval =3D READ_ONCE(rs->interval); int burst =3D READ_ONCE(rs->burst); unsigned long flags; - int ret; + int ret =3D 0; =20 /* * Zero interval says never limit, otherwise, non-positive burst @@ -51,8 +51,6 @@ int ___ratelimit(struct ratelimit_state *rs, const char *= func) =20 /* Force re-initialization once re-enabled. */ rs->flags &=3D ~RATELIMIT_INITIALIZED; - if (!ret) - ratelimit_state_inc_miss(rs); goto unlock_ret; } =20 @@ -110,19 +108,17 @@ int ___ratelimit(struct ratelimit_state *rs, const ch= ar *func) =20 if (n_left > 0) { n_left =3D atomic_dec_return(&rs->rs_n_left); - if (n_left >=3D 0) { + if (n_left >=3D 0) ret =3D 1; - goto unlock_ret; - } } } =20 - ratelimit_state_inc_miss(rs); - ret =3D 0; - unlock_ret: raw_spin_unlock_irqrestore(&rs->lock, flags); =20 + if (!ret) + ratelimit_state_inc_miss(rs); + return ret; } EXPORT_SYMBOL(___ratelimit); --=20 2.40.1 From nobody Sun Feb 8 23:41:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 95E0535961 for ; Wed, 30 Apr 2025 01:05:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975127; cv=none; b=RPtV2nD9Og5q9NZ1fNdERDdmsUy+Zj3P3s4YDvg1paIunFC9MnCHu3N5gpPYNrAnWYvn4CDcL8XUeLAj2Ot7o7iNennVH1wmNrGdX4jj4wWLxdIhQWbktwZWgbiRwZyr6OGOUY0pG6CDcQHKbiR2FDo39zaiM1a+dXF/R4bGLDQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975127; c=relaxed/simple; bh=feyMWlvQ3IMm4wSDJau8dg3L1bsITx2hZGbyD38McFU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=pcfZ8+QgADs1jxlmk6sYQv+HxBjjkjwna6xEDN33yHsBHqypmtQCeXeR6BZzrHDukGW0dzNy3z64aFJtBsRe9w8cBCE6K3RuRa30UEP3483LfzPS8mZl+qEGWDwPLmaFhEUsGDSsuRAcjlQKWMH6Y2k/Pqmqtz5vcTH1AG3XqFs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=pbVXGaSG; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="pbVXGaSG" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 65943C4CEFC; Wed, 30 Apr 2025 01:05:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1745975127; bh=feyMWlvQ3IMm4wSDJau8dg3L1bsITx2hZGbyD38McFU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pbVXGaSGz6Ox4rLxvllqzX7HwSM9A+Cx05Gao5cdkUncYgOcqDP03XkJ3IFCabkZx gefF/lRFGsFj5+oJZImBMYilMl4oy4HVOb24oDczz1quTyLXzm9lRiQRhv8JfAbLTY 1MiENWFzCtIB7GnjscL1kcVrhN4uerVeC9JlAN1ecEer/LB4BAGu1uhWuiqNn17GtX iJyQO5ZrEKJw6dnL8HYQgTAVf2hUrYGIha+9ckzGnj8ns8hVrf63dCzjzQf7bgzY1b qeaqI43HUnlcs3h4N4+25gfXkIWTF0t4pvTiXArrakC/objKkfthSPgDNq7Dw5AA67 kkfqmq7qRsozA== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id D72D8CE11BC; Tue, 29 Apr 2025 18:05:25 -0700 (PDT) From: "Paul E. McKenney" To: linux-kernel@vger.kernel.org Cc: kernel-team@meta.com, Andrew Morton , Kuniyuki Iwashima , Mateusz Guzik , Petr Mladek , Steven Rostedt , John Ogness , Sergey Senozhatsky , Jon Pan-Doh , Bjorn Helgaas , Karolina Stolarek , "Paul E. McKenney" Subject: [PATCH v4 17/20] ratelimit: Use nolock_ret label to save a couple of lines of code Date: Tue, 29 Apr 2025 18:05:21 -0700 Message-Id: <20250430010524.3614408-17-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Create a nolock_ret label in order to start consolidating the unlocked return paths that conditionally invoke ratelimit_state_inc_miss(). Link: https://lore.kernel.org/all/fbe93a52-365e-47fe-93a4-44a44547d601@paul= mck-laptop/ Link: https://lore.kernel.org/all/20250423115409.3425-1-spasswolf@web.de/ Signed-off-by: Paul E. McKenney Reviewed-by: Petr Mladek Cc: Andrew Morton Cc: Kuniyuki Iwashima Cc: Mateusz Guzik Cc: Steven Rostedt Cc: John Ogness Cc: Sergey Senozhatsky --- lib/ratelimit.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/ratelimit.c b/lib/ratelimit.c index 7c6e864306db2..e7101a79c6973 100644 --- a/lib/ratelimit.c +++ b/lib/ratelimit.c @@ -43,11 +43,8 @@ int ___ratelimit(struct ratelimit_state *rs, const char = *func) WARN_ONCE(interval < 0 || burst < 0, "Negative interval (%d) or burst (%= d): Uninitialized ratelimit_state structure?\n", interval, burst); ret =3D interval =3D=3D 0 || burst > 0; if (!(READ_ONCE(rs->flags) & RATELIMIT_INITIALIZED) || (!interval && !bu= rst) || - !raw_spin_trylock_irqsave(&rs->lock, flags)) { - if (!ret) - ratelimit_state_inc_miss(rs); - return ret; - } + !raw_spin_trylock_irqsave(&rs->lock, flags)) + goto nolock_ret; =20 /* Force re-initialization once re-enabled. */ rs->flags &=3D ~RATELIMIT_INITIALIZED; @@ -116,6 +113,7 @@ int ___ratelimit(struct ratelimit_state *rs, const char= *func) unlock_ret: raw_spin_unlock_irqrestore(&rs->lock, flags); =20 +nolock_ret: if (!ret) ratelimit_state_inc_miss(rs); =20 --=20 2.40.1 From nobody Sun Feb 8 23:41:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 8722B2D023 for ; Wed, 30 Apr 2025 01:05:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975127; cv=none; b=h82lifHC1Lx6OYDyHsx73pdI3hC1hhqjFXByeqvxBh7sDKqrFe1g2YlgI59lcCG+kcdyPlWfK9y6ZBUqaF9Jx/A7lXSYCGJlTcYMmiEC+htFFgH9EpoPlquka3bNZIHE1oO2R+MGidCUvNGIIxqUfhcee0xBZyxyrgaGzR865g8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975127; c=relaxed/simple; bh=IXjNK7JRbzJgmjpzWE0gJOfavJOhfRMhhLqkx9ULkFU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=pfzrHl5m5DWvoXQK6ckFNyvpUctiJwCvdecZBpOCMKHwttS9w9s1dEzhCrChXq8tx0irt4CgIkTF2tFGs3T6lHufQ7+WvkAzvMFUQ6Sdde65cUtUQj71j1bItbRgJCp7ApR39Xxp9SuVOpDPGcEexLrnR6ETpaEZ2uprgTWe7Vk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=KIcblsI2; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="KIcblsI2" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 63381C4CEFB; Wed, 30 Apr 2025 01:05:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1745975127; bh=IXjNK7JRbzJgmjpzWE0gJOfavJOhfRMhhLqkx9ULkFU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KIcblsI2Thiw3KwUc4uQV9D+bH31MxD4Q/fy/cyDIjHqUH4UivV8acnosJCs6svyT aQxQjHCLrM9N6BGdUHHazFIy6CD8PcLwdGOO02MCZGRLdsBkN2u3I78nGq/fwl5eKg 4d93AxvDzXmxlBzmYbI4UaekIpvHe8JXtkQMcESfiAV8CifItH5ZilVHRhXpWjX8oC 3wSLgIQ8LYMQQg7y18IKFzM1lB0YwoKVxG47giUBt0+cgCJKQbrxSUkel9XW4Bh09e P4N8ARg1neePR1SNbYtLWLUhyGZvonKUOb14hxVqPNkOwCpBrCakSdtK0BQMxDSS/d +f1DHBJj8ALpA== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id DA53ACE11CD; Tue, 29 Apr 2025 18:05:25 -0700 (PDT) From: "Paul E. McKenney" To: linux-kernel@vger.kernel.org Cc: kernel-team@meta.com, Andrew Morton , Kuniyuki Iwashima , Mateusz Guzik , Petr Mladek , Steven Rostedt , John Ogness , Sergey Senozhatsky , Jon Pan-Doh , Bjorn Helgaas , Karolina Stolarek , "Paul E. McKenney" Subject: [PATCH v4 18/20] ratelimit: Use nolock_ret label to collapse lock-failure code Date: Tue, 29 Apr 2025 18:05:22 -0700 Message-Id: <20250430010524.3614408-18-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now that we have a nolock_ret label that handles ->missed correctly based on the value of ret, we can eliminate a local variable and collapse several "if" statements on the lock-acquisition-failure code path. Link: https://lore.kernel.org/all/fbe93a52-365e-47fe-93a4-44a44547d601@paul= mck-laptop/ Link: https://lore.kernel.org/all/20250423115409.3425-1-spasswolf@web.de/ Signed-off-by: Paul E. McKenney Reviewed-by: Petr Mladek Cc: Andrew Morton Cc: Kuniyuki Iwashima Cc: Mateusz Guzik Cc: Steven Rostedt Cc: John Ogness Cc: Sergey Senozhatsky --- lib/ratelimit.c | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/lib/ratelimit.c b/lib/ratelimit.c index e7101a79c6973..bcda7c61fc6ff 100644 --- a/lib/ratelimit.c +++ b/lib/ratelimit.c @@ -58,20 +58,10 @@ int ___ratelimit(struct ratelimit_state *rs, const char= *func) * the current lock owner is just about to reset it. */ if (!raw_spin_trylock_irqsave(&rs->lock, flags)) { - unsigned int rs_flags =3D READ_ONCE(rs->flags); - - if (rs_flags & RATELIMIT_INITIALIZED && burst) { - int n_left =3D atomic_read(&rs->rs_n_left); - - if (n_left <=3D 0) - return 0; - n_left =3D atomic_dec_return(&rs->rs_n_left); - if (n_left >=3D 0) - return 1; - } - - ratelimit_state_inc_miss(rs); - return 0; + if (READ_ONCE(rs->flags) & RATELIMIT_INITIALIZED && burst && + atomic_read(&rs->rs_n_left) > 0 && atomic_dec_return(&rs->rs_n_left)= >=3D 0) + ret =3D 1; + goto nolock_ret; } =20 if (!(rs->flags & RATELIMIT_INITIALIZED)) { --=20 2.40.1 From nobody Sun Feb 8 23:41:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 9E7613D544 for ; Wed, 30 Apr 2025 01:05:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975127; cv=none; b=XEY6771Jw6wQ9h34HzbUzUCFX3KTq0+HGC6CoPiJw2KO2e//vu1z/J2WzhDfiFNKPCrlZpBtFEOpxJxx7tbewWm710bHUpb9EM3EZytpUOpMckdSQBwxZZBxfxKYuUsTasJGmGRLMVPaKsmaMJ0v2ay1Wc7tFkkMXBXpR1oNRGA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975127; c=relaxed/simple; bh=QuXY/enqInz7nSkzplVvspsi9vNMNEEbLePY/x4Nq0g=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=L30i4SNIcvgXW3gQRcl+UKXXWLqWWLztdGYr2uxS7kkVo8Fwh2MNWzQtcyOYwgL9C1acpCPx2f1F3wLTwnsQrR8W2k4cFwbbPpRnwT2zSA8aWqp+AxlsYER44ILVJFEHh8lskTx9ah1U50AH6vAhCGyl5ifgJdY+NNMihdg9LqU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=lvrHcO7/; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="lvrHcO7/" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 73DB3C4CEF9; Wed, 30 Apr 2025 01:05:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1745975127; bh=QuXY/enqInz7nSkzplVvspsi9vNMNEEbLePY/x4Nq0g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lvrHcO7/TTN5nyU5mDMLJprXQdjbgmn6s7zsEQYoLwSPdbT7wWG7dCHXSdIzFrplJ n7fM7tpae4Ekl+k5+w0ChQl+2qv/bSpb9hYRSSsxQH4t7/B1MM/weQD3VjwqVPWJ8j tPCv5WP8QeOEy42Toyc4QX4oAW757pj8z9aIde/sBlvna1Br0vMVewUkKsqRp3/XnG oztNRiWVoW+TwU1SQirDUQ24PZH46VT/rTLiLxl0pfnD9AhBu5RKSyJkSCudplzUDy N020hTcQLkxQpYmBBoJMqg+IGiJMCxO7dfinx8MWkbcUYqAJCwyVi//blaI2m94aVs u18E1gIlWdiVQ== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id DD2F7CE1216; Tue, 29 Apr 2025 18:05:25 -0700 (PDT) From: "Paul E. McKenney" To: linux-kernel@vger.kernel.org Cc: kernel-team@meta.com, Andrew Morton , Kuniyuki Iwashima , Mateusz Guzik , Petr Mladek , Steven Rostedt , John Ogness , Sergey Senozhatsky , Jon Pan-Doh , Bjorn Helgaas , Karolina Stolarek , "Paul E. McKenney" Subject: [PATCH v4 19/20] ratelimit: Use nolock_ret restructuring to collapse common case code Date: Tue, 29 Apr 2025 18:05:23 -0700 Message-Id: <20250430010524.3614408-19-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now that unlock_ret releases the lock, then falls into nolock_ret, which handles ->missed based on the value of ret, the common-case lock-held code can be collapsed into a single "if" statement with a single-statement "then" clause. Yes, we could go further and just assign the "if" condition to ret, but in the immortal words of MSDOS, "Are you sure?". Link: https://lore.kernel.org/all/fbe93a52-365e-47fe-93a4-44a44547d601@paul= mck-laptop/ Link: https://lore.kernel.org/all/20250423115409.3425-1-spasswolf@web.de/ Signed-off-by: Paul E. McKenney Reviewed-by: Petr Mladek Cc: Andrew Morton Cc: Kuniyuki Iwashima Cc: Mateusz Guzik Cc: Steven Rostedt Cc: John Ogness Cc: Sergey Senozhatsky --- lib/ratelimit.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/lib/ratelimit.c b/lib/ratelimit.c index bcda7c61fc6ff..dcc063af195eb 100644 --- a/lib/ratelimit.c +++ b/lib/ratelimit.c @@ -88,17 +88,10 @@ int ___ratelimit(struct ratelimit_state *rs, const char= *func) } } } - if (burst) { - int n_left =3D atomic_read(&rs->rs_n_left); =20 - /* The burst might have been taken by a parallel call. */ - - if (n_left > 0) { - n_left =3D atomic_dec_return(&rs->rs_n_left); - if (n_left >=3D 0) - ret =3D 1; - } - } + /* Note that the burst might be taken by a parallel call. */ + if (burst && atomic_read(&rs->rs_n_left) > 0 && atomic_dec_return(&rs->rs= _n_left) >=3D 0) + ret =3D 1; =20 unlock_ret: raw_spin_unlock_irqrestore(&rs->lock, flags); --=20 2.40.1 From nobody Sun Feb 8 23:41:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 EB04C1428E7 for ; Wed, 30 Apr 2025 01:05:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975129; cv=none; b=mZkEBZwW+j0UrBmPONKgIJpKRdnfx1UeI4vmApVNB0y6e5+H11hIKEYdLzukD3/RNV7Dn+G+FVYCl06xGQ4yrx/FeINQkKfqJeANygUvkyby2N4qqx+SIKC4i8Je/ObyDCm3QcvLAwsi/YYo5pwyu1mvfvwJC6N9VU/Ee9F1gw8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745975129; c=relaxed/simple; bh=JP4VCGxb2YnnLFOMJL/zztqr+/zmTsrBpCccs3bYkwQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=swQ06uYJZcjyOqIJEH1hKyWeIfdPWqdNduD6YF+TlRcP0z9xo46cfizgbZJGJSvMH1FSuXU1lAzV0WviUIhXXllN7kwbswg0mcMYNpEPaLHwudFGbjqkUptmG0xyDh6PB0K+t6Kuct4p8qLVG2st3YaPZqXw1TTmAKDydCG/ouI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=czv3lorS; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="czv3lorS" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8296EC4AF09; Wed, 30 Apr 2025 01:05:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1745975127; bh=JP4VCGxb2YnnLFOMJL/zztqr+/zmTsrBpCccs3bYkwQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=czv3lorSzyIcEuTPMQ9N19WesE6W4jVrEFhcPB+bgSTQMffEM3H3R2TMJnNPBnox/ W+2qwMfirqyDRLtYuYhmRLrXsDrkPQVScoQGvMDmghz5uTjCinYCR5n7vaDBY9b+sA SiSD51GY+V3kzp8VNW+IvkofW8kHuIRsoKUgjX5MQAy4ORg9/PDQSqaUxVHWzQiV1O HuXb/Ey60YvS0LeS7tam0ON5GeUyb2Ln/uJJ7fEXFk/KuWGMxYmynYd48OWLtmwujW 4RUCnjvQeF8DvHToqgnhjVNZDyCF7VAXrSiviVCLhxPPdejOKv9Nlybvya4+mMyqdS 0M/jbaYwu2GrQ== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id E0505CE122D; Tue, 29 Apr 2025 18:05:25 -0700 (PDT) From: "Paul E. McKenney" To: linux-kernel@vger.kernel.org Cc: kernel-team@meta.com, Andrew Morton , Kuniyuki Iwashima , Mateusz Guzik , Petr Mladek , Steven Rostedt , John Ogness , Sergey Senozhatsky , Jon Pan-Doh , Bjorn Helgaas , Karolina Stolarek , "Paul E. McKenney" Subject: [PATCH v4 20/20] ratelimit: Drop redundant accesses to burst Date: Tue, 29 Apr 2025 18:05:24 -0700 Message-Id: <20250430010524.3614408-20-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now that there is the "burst <=3D 0" fastpath, for all later code, burst must be strictly greater than zero. Therefore, drop the redundant checks of this local variable. Link: https://lore.kernel.org/all/fbe93a52-365e-47fe-93a4-44a44547d601@paul= mck-laptop/ Link: https://lore.kernel.org/all/20250423115409.3425-1-spasswolf@web.de/ Signed-off-by: Paul E. McKenney Cc: Petr Mladek Cc: Andrew Morton Cc: Kuniyuki Iwashima Cc: Mateusz Guzik Cc: Steven Rostedt Cc: John Ogness Cc: Sergey Senozhatsky --- lib/ratelimit.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ratelimit.c b/lib/ratelimit.c index dcc063af195eb..859c251b23ce2 100644 --- a/lib/ratelimit.c +++ b/lib/ratelimit.c @@ -58,7 +58,7 @@ int ___ratelimit(struct ratelimit_state *rs, const char *= func) * the current lock owner is just about to reset it. */ if (!raw_spin_trylock_irqsave(&rs->lock, flags)) { - if (READ_ONCE(rs->flags) & RATELIMIT_INITIALIZED && burst && + if (READ_ONCE(rs->flags) & RATELIMIT_INITIALIZED && atomic_read(&rs->rs_n_left) > 0 && atomic_dec_return(&rs->rs_n_left)= >=3D 0) ret =3D 1; goto nolock_ret; @@ -90,7 +90,7 @@ int ___ratelimit(struct ratelimit_state *rs, const char *= func) } =20 /* Note that the burst might be taken by a parallel call. */ - if (burst && atomic_read(&rs->rs_n_left) > 0 && atomic_dec_return(&rs->rs= _n_left) >=3D 0) + if (atomic_read(&rs->rs_n_left) > 0 && atomic_dec_return(&rs->rs_n_left) = >=3D 0) ret =3D 1; =20 unlock_ret: --=20 2.40.1