From nobody Mon Feb 9 12:11:22 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 335E7220F29 for ; Thu, 8 May 2025 23:33:36 +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=1746747217; cv=none; b=Lc5GT/ee+lAIw9h72PmvIixEP3i3kjs9YEVWDAKjHkq/GlkaYVJCjlSSbu/FJvqu7StMtsR/AYOFRT8ZV+4Peeb5TqzhfQaPFH8SO7NHnN0kYv2W/anRQ1j/szJ8PTeA/M1w4BeIak9XiSCinguM89AVOX8iExz5EO6/pNd9mxs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746747217; c=relaxed/simple; bh=N3RIolT2/wwpsyp3qVL0vety09DWGenJ3HGIlVCg+P0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=uDwv1GIMhJz/FjxHHvUf8++2VSFuQExVeMtiP2MP5l2PrOB/pdI+dwCOlPz/6HT0KFnWS9Lsk70uej14dka7xX++rDs1nvXaAnGx7V/+si0/geiwslQsu1HtGABXuzpjEezENByr6w/T8EbjuBESUvOyisuOVrssEWNqiDj81f8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=WlB6mIKG; 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="WlB6mIKG" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A941FC4CEE7; Thu, 8 May 2025 23:33:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746747216; bh=N3RIolT2/wwpsyp3qVL0vety09DWGenJ3HGIlVCg+P0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WlB6mIKGvzzqxItE/Qxc+Zc/o/qLP12ui7/TpPLRyCC2VzuquNl1ZSzM+A+d4J7/8 oVOOSgB1dy5Bbkjwb2rokj9obna2LaA7J/B6Igw5gupqs5BNrS0NAUHbyOoFLddOQH Xd78cioIVT64UekTNtSwtndQpOCzIqrO7F/T+C5SvuvX2obh3f8uHgxmHdZU38uJYD LSTkMzE7PJrmeA7GKGy8WhjW5ZasSUuZlFPhOiskvdf+kXr4iok9ZIs6T5ipN4ngdu pO5n7shSPZxtOBc+jzVd4aZtAjCmcRxP3u5k2G7iGejtc0q+g6qtdSsWMEZXsso/mJ 0fQEY+u+TiAwA== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 5BC1BCE11A4; Thu, 8 May 2025 16:33:36 -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 v5 01/21] ratelimit: Create functions to handle ratelimit_state internals Date: Thu, 8 May 2025 16:33:15 -0700 Message-Id: <20250508233335.1996059-1-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> References: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> 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 Mon Feb 9 12:11:22 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 334BE1DC98B for ; Thu, 8 May 2025 23:33:36 +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=1746747217; cv=none; b=PyGQchCSF+kvWoQU4oXDX2y2sx6pioGQpLIdGuzOUEfuNjqfHFc3xR5TF1aP4NYp3RjH9LWMY1eaGaFE8FjP5OaprwBJ7GFHTy35q6lD8G4JjhLmc/G03jgFBA1E72Sdkrpivqlgc2rFofm5JtNWWS41NxpJF8Ij4Wd3jplCI8I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746747217; c=relaxed/simple; bh=gdsff1ewKdQFrckg4o6Eop2RJTaYEsK2lc1udsPqdD4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=F9LtmRibzMzQsGJ9oBcS11G52Q6yDgH8yTsiUvgo45Fzf1bLGm4vbAqRngBM+AKcdZPgcshXiKBZfw3fTzM3ygphkLWnyag4/33syBqvvY0xR1rW3/e0Jb3etuN+Ft+ILUGSAJ3Rv1PqK5oXncoOb3rOYUnO2my0n1ttQFqcRl0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=bRkyaPPj; 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="bRkyaPPj" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B9BBBC4CEED; Thu, 8 May 2025 23:33:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746747216; bh=gdsff1ewKdQFrckg4o6Eop2RJTaYEsK2lc1udsPqdD4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bRkyaPPjA6ZYbic6lbmh/qPS8SadLyGMFuIvmTXnP6Cn6CbB1eN9oC3RX1tyDqkjn l058vCGU4gj2wp5rxE78gyTofgpeduw890mVjY/Ex1Tce1XWj/vjy8bDbCabqm/n2W +uA0jA6RfmS4Jhwr5f22RdxNYahAmbvIyFTMPCk9oCZA9e3tGFS/En6wxb9EkCJbTN 6Lsl7Tqtd0sKTJjFYrDbbd0GxNlWnq2JD5Tg/eUYXucsxsNHw5GxDvOSYTTMqPQ1p+ sXyeQhYLzOl4Ll+t6dtEDyyfhvMzYXzYRA4U5c+rAUxughU284eqN/WkRJccbYKCaF 94SSE/NgYeWng== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 5F347CE11D3; Thu, 8 May 2025 16:33:36 -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 v5 02/21] random: Avoid open-coded use of ratelimit_state structure's ->missed field Date: Thu, 8 May 2025 16:33:16 -0700 Message-Id: <20250508233335.1996059-2-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> References: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> 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 Mon Feb 9 12:11:22 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 1326CAD5E for ; Thu, 8 May 2025 23:33:37 +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=1746747217; cv=none; b=p4iO1nHjl/jiY8R9KskBSvUqXy++bEmbgfNd4OhpTD6LH5kwbunUni3T38nKGngxqlKHF0fpzcKqqVfDqspqJhlPDdYj0gGQ1P5L2OAraKa9tAVB56whZ+1GjdYQndIOgd3iLxdTzL2C8NbQWWwoVTtGa1kJ9gSvsWPYGQsLZ+k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746747217; c=relaxed/simple; bh=qrbtE4GJAc5CkkZvuRY2A4Vna57jDG+6dB5+Nw5K9to=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=tD3F5+tKm6bJ1WOi/W6z6jyd3HDbgSDGpxz46EGDN4dVgN7xE0dwcPFM7Euh9g6n9DLLnDwdC2DY/F7QEh73tbaaQP/DwHj68EpmDGYXMlVNaZrL9kric+uxM46EWwMIWA/Sue8m/otzMvCB2/sdMLOMosFnc6Nd3P1g1iiw4rs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=qp/+cd0a; 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="qp/+cd0a" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C5428C4CEF0; Thu, 8 May 2025 23:33:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746747216; bh=qrbtE4GJAc5CkkZvuRY2A4Vna57jDG+6dB5+Nw5K9to=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qp/+cd0aLdjY1AeppBROmJzDS1w0IVP2u2f2XHj9own9KkNr7DK5VW0NKgXXsPoaK B/CA+Z74yAfX6G/l8l20eG6PuYkkPlDJNRd6JYbtVLw49138zf6KlE0tvlMYzBGg1O edvUw6YmBtZD2UEu99Gug1fEjOwN6uEX9qR8UCTF1ld2S2Tx0WXYVLjI3R1Rcwq07c sEior2X/RNuB+RQiKzlE9LUxAOHv5Cvoz/nYgAnFYAl/C4rjqUviBheNuDrBbYD0uy rERZVJk2KKAr7tphqTCnUj2b2rbSft9z0VSm6PwdQXo1UGK0piSs9IMCMJbPPVzCDE /qKV4eVHKXUNw== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 628A6CE11D4; Thu, 8 May 2025 16:33:36 -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 v5 03/21] drm/i915: Avoid open-coded use of ratelimit_state structure's ->missed field Date: Thu, 8 May 2025 16:33:17 -0700 Message-Id: <20250508233335.1996059-3-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> References: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> 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 Mon Feb 9 12:11:22 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 3351E21D3F0 for ; Thu, 8 May 2025 23:33:36 +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=1746747217; cv=none; b=ak0dlXTQRgEnWWhiNJWGUuSWDt6Qx9NOGycZ6OOaIGY8jFtYb3VS2G8lvsU4t7dpHbTtrzZE5vJJQETlDNb50EE7UoY4yPyAVOwitw/gG5UAjMXvzjfARnmFQ71HZojMgkvJzIJcYWEfL2gdqf/NDm0sq8SWw8hQ6umUbkfEaco= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746747217; c=relaxed/simple; bh=oSI4AdvwmcSLbAbbuCxcWerS/mPOdTcxQRziDu39kWk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=jfSszawrAePWuChbcRxM1f/OoH3ZZBVWgCM+j8tB0R+sgQXry8an3XsSsiovQTmgc1VX435TkI+1uf2rrftd1JFw2Mu4jCZDwCEnzGJJ8AEC7dPq+0RYQQPllAjNYb0pZawwExvYKlp8EKpqea4Y0E6hu8qjnSnUvDwiaExfULY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Ao4yOyV3; 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="Ao4yOyV3" Received: by smtp.kernel.org (Postfix) with ESMTPSA id BDB11C4CEEF; Thu, 8 May 2025 23:33:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746747216; bh=oSI4AdvwmcSLbAbbuCxcWerS/mPOdTcxQRziDu39kWk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ao4yOyV309QJqgvD0J6pZSDfn7PX/Sk94PVY6wDwhdmjl1VIBygrgblIKi6BsKnXK ORKXHZI1zKuT6K9yU2wfX6rFUtHc/8H5HVc2l6nEnWr5YA15Jpj7S1QC1HytNCGnSs 8BM2MlCkJxh7P4LdBjpkkHdgSsXaxoj/Deo5okICE9KP8T/BnXjLYPgO36bBfFr6h0 nXaryJJM5lLpvpSr2Oc1Aj3SW7tA7PGm/nzT5Z9PcZoxjsOYpTcMIeakgwE0xE1A52 vOm3hzR3Izo78oZ/TwKWx8bx4GQvADvP0zpR05RFAWYMPZtAz0dCLtQfHEAXmYUT6B hLL7OVlKMJXhg== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 65C08CE11F1; Thu, 8 May 2025 16:33:36 -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 v5 04/21] drm/amd/pm: Avoid open-coded use of ratelimit_state structure's internals Date: Thu, 8 May 2025 16:33:18 -0700 Message-Id: <20250508233335.1996059-4-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> References: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> 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 Mon Feb 9 12:11:22 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 3358B21FF2C for ; Thu, 8 May 2025 23:33:37 +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=1746747217; cv=none; b=Z0029Ibmpn1+lc2hMVWdFJ9RYbJ0yqg6Mb7Na3qN0Yxmp7s2dCM9rCfVIHLiZVgYae/H0zLUf7xFxe2c2zIlSoT0Ci/Km9iOHzsl4vR6IlNCoxSgl6QCEAHEca6vtSPze12WCI/DEle4tHt+LiSu+fG51CgE5RZGs1YV+9hYnpc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746747217; c=relaxed/simple; bh=oqGQLaAZjOiJlIyJmpFkRKe1yhFE6Ac++q3VgQmENoU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=KnjB6kSWqmD3Vosem0jcitqkIQZuGHv9ovjdEXTvT6/2VKFsUNu9btlyKf97TINfwfvH3WedBt19GpJSdim+3z98IOO3NgR8Puij4fiStMcAW2k1ktqiLBQdujK8/lmE4qAvmUnDpz/kydDUr3H+XZCdesZvnAhSYA7iu8qjLtc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Hkb+QpFo; 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="Hkb+QpFo" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D3056C4CEEE; Thu, 8 May 2025 23:33:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746747216; bh=oqGQLaAZjOiJlIyJmpFkRKe1yhFE6Ac++q3VgQmENoU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Hkb+QpFodxpTBhVQFXoHfsb2N9sd9thqmGh0oXpcAE6huLUJdj6DIWk6GDbIWAEg7 AqQdStKpiLBxlYJlMroKNJ0ZGIxtPMzMdCaqCXbATivXcTVm+7z5r1GXqZxPv+A3s5 v/LsmlVYGOTHmWiM8EVyNCvPfYFo+CMFHz5Gz3LngxBpMxgc/V7xWuxeYxLQNITF/i eNMpZGJZMgkEGNXuTi2px2Ge2lwpskdEcpUzxRwTLfaYz8JYTg70UY00RUx1prrlgP VPTYVAJMrLrGNMTFpZzyod50yzg0HWc7WvU9odoEunZ3iDUYbcoWev8wDqRSp+Znp/ 4x96IXUZXll/Q== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 68F04CE122F; Thu, 8 May 2025 16:33:36 -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 v5 05/21] ratelimit: Convert the ->missed field to atomic_t Date: Thu, 8 May 2025 16:33:19 -0700 Message-Id: <20250508233335.1996059-5-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> References: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> 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 Mon Feb 9 12:11:22 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 5FDBE22FF4C for ; Thu, 8 May 2025 23:33:37 +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=1746747217; cv=none; b=CNceAwUFbGkHM1FjU/7Uf7tSc19svfUfueVrLhPnd20ZeYaWqjWGprSd5cKARaOJFiEvTvBRT7zAHoAAZSBZTj5lf9rmefWqWEhRp380wQYdVmbZkMlp0lVot0DyLaXUbQVTFDTSLWTBhT+0hnVFu51NdFd5RzewqEDdbYXsgxs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746747217; c=relaxed/simple; bh=drIhZlz6qjJXl6CfFVToJFT9tX9Y5dyNfzQrE+P3u7o=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=WZOi2BrVBew8kZlwav9R2QP14j0SFmGAEB2Y3QAcs82gS8w2f8um8Ik51c4c/L6X1o/Mh9kXutaHvrEgA44RRqUoCS+y731iFvTy6YwUIK12C+ZevKuUp9+9j0MJ125dYD48X4sPxYShegxQ3aoLKe+/WrzByBG8WDeFIVT0Vk8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rLMpsOoU; 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="rLMpsOoU" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1BC12C4AF09; Thu, 8 May 2025 23:33:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746747217; bh=drIhZlz6qjJXl6CfFVToJFT9tX9Y5dyNfzQrE+P3u7o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rLMpsOoUnVHJIDm1kqQzIFgOosNuV5bLl6Cq2DrQKvgCR2oWbblylQ8pHauqQNCA2 W4PRsrpWeAeLO/bsvX8+1Rc0Qb1kS4PyCU5vDaaJK0j47GrsqwuRDYZwDSaH234vos qkp+gCaeB8DrSG7wXBDDX2HkSCzq5wqLkwX9pH8clYC0WG5nhfwWyApeShG57RQjfn qaefop8/a1/X7DNxMaif+NJfjU5vvPYnrF2IT1tI3XB4Iz9oXLXHnhR1QagbR23lih 7n+cOICeLfIPm0BkRPm8UR/3weuoWAZ9dbjnJb0/YhO7nrw+FN9/bG8juCbNWmh491 8h040afHDZbOw== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 6BF2BCE127E; Thu, 8 May 2025 16:33:36 -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 v5 06/21] ratelimit: Count misses due to lock contention Date: Thu, 8 May 2025 16:33:20 -0700 Message-Id: <20250508233335.1996059-6-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> References: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> 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 Mon Feb 9 12:11:22 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 5FA4122DF84 for ; Thu, 8 May 2025 23:33:37 +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=1746747217; cv=none; b=HKf5c9SXKVBBcYycqDfiTPFH8Cf4aYat4Qm4S33A2u7dCK15etQpqmtQg8i2liur7usmJMuj5cHQe1JGPAIurjzrGowFGqdLuAYcVeRdH2pZ4XUMk9MvnbI+lt6FBg7dcUm4TaIQT6jfS/Mhs9waFho06yd/yg6vHo1mC6jrvSs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746747217; c=relaxed/simple; bh=/+i4NOmtNmiJBCyQ4d45E7T91BkwFjUY+T60jMduKPo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=VzcJwJqd2F+PSJOtak5mkBipFtj7rwFrvyZXYJIdWNPSjIhJ6tXjJFswviM0RhADqu1ScfUs9chPR2ERPQMcIaQXjMpYwz/fVoJmt2OwWMKlWhCRRhvirXcZfKlgRL9Wbx6vMtK5ycLmXROjps/+c88X2d/kxdlMiQyAHg0g/9g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=G6WEkkw7; 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="G6WEkkw7" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1B40EC4CEF4; Thu, 8 May 2025 23:33:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746747217; bh=/+i4NOmtNmiJBCyQ4d45E7T91BkwFjUY+T60jMduKPo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=G6WEkkw7BA8evc1W2mq2ur18opVPk8sCZiZgAK0qj597cAWEsk0F3QplYYWcFHz01 GjQgP0PJtuLp49oY8tUcyw6Lff5abAjvbiqHtgv9icdiShkjjCSCRijtS9a/uoH/At N6NifAXOQ0oL8ecyKtGhAoSFnVD5uaxVcyu+YMBOYw616MZb5QeFRYSUPj3lEHtHAg LIJhRMnYCSEsj1Ku6Kg9HmazsdoPEfFz1o4nej/9p64g1VWvf5HTDJ1vgn/A/qPMZ7 7prrbYGXMxLOT0g+sy+SngaEW0jzPvM/X89/Z40zLTjSvXlne5tPClzZb5GMvMroru h+9b6+eJEUCLg== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 6EF2DCE12A5; Thu, 8 May 2025 16:33:36 -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 v5 07/21] ratelimit: Avoid jiffies=0 special case Date: Thu, 8 May 2025 16:33:21 -0700 Message-Id: <20250508233335.1996059-7-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> References: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> 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 Mon Feb 9 12:11:22 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 5F9C72153ED for ; Thu, 8 May 2025 23:33:37 +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=1746747217; cv=none; b=NnvevZbXHNH4cDLLu3VCCmGnVY+uYm+y7uuXqQlpc3yAUOek+DWeoBeeLH84mGmGHDCGn0NkyyvuDw1vNxvxXQ+5vbzcsfx4NrpE7hBwlqsAvvYEjzElTIx/EnaoBfH40v5v0zXWSg3iqn/z6zQp4aj1QBNhV+ZwUBe5zZRy3nI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746747217; c=relaxed/simple; bh=ntY2Sw3SfTBZAtAeM3adbzynb7JDNJCcwtct5/GAXqc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=C44J0UiqGJCF0XgXAeIFsGPU1FrSFEII7fgQxVD6ko+YrRsGj+jMsRx7TIO3tjoMxhATHdjl55J+ubb+HWCjIoTGKPxt1JRSZReXrURE9MKkEWUtasOMxUemfyYRwzYPcr7/g/Bvntuiw5kWqRMBvj5FhH6lAy5UJ0F9+zOf7Tk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=TIqmAPuO; 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="TIqmAPuO" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1B5AAC4CEF5; Thu, 8 May 2025 23:33:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746747217; bh=ntY2Sw3SfTBZAtAeM3adbzynb7JDNJCcwtct5/GAXqc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TIqmAPuOGY5ijLqa8wv4PirHncJn45vXFlOTP2x4xA0WYF38LRQC/tSOhA280zvxr z/IJyafDxJuOejRkZvQ3klQVjKGKM2sQvZHMOP9/sz3ap7E9xOnKhq8O45KfNpbUYe dfQEFCAUYnt4gXONCZDynsxNcnju8yosUWkMBCJ0/T8JmbElTbRZ2V4pb1/8ys+9y5 kAZMltQdO4lX/yLiCBzGNkBO4792XZoOxLbk4gPeN1M7YTAEwtZ7CP5c5/k42o6/Ex k2OcwR+VkroBWyJSHeq58KlBJGGsBeSBUZSIL/RXY4n7OhS2t0o1lfG2eXzvYVj77+ n60H9Vcl91EZA== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 71E6BCE12A6; Thu, 8 May 2025 16:33:36 -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 v5 08/21] ratelimit: Reduce ___ratelimit() false-positive rate limiting Date: Thu, 8 May 2025 16:33:22 -0700 Message-Id: <20250508233335.1996059-8-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> References: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> 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 Mon Feb 9 12:11:22 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 77D45230BD9 for ; Thu, 8 May 2025 23:33:37 +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=1746747217; cv=none; b=cTd5en1cCNjlMX0+GBsI59E0XwBa08HPyowVza5KHc63AgYTVFrBn7s6ASJDkVTMfEDU9FI+W1O/kSGNuxER8Fhj2z2i7+dIT8ETmaTJEl8iRInk/w+dJOs+VhAHVNU7s9RCsyBT/O6EFLrPHynpS7FI3Txt8+Gv8L8Sy9tbs9o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746747217; c=relaxed/simple; bh=xVs4QemZgCqU8+2ERmMbebq38vqQbBEp640MidYy624=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=qKTkXjVIXdBNnu279smdwqQO/ynw0BRIpY8SxeRhlqX2Qk0s9VufUXcaFQURE4sE06jZtnHY5jD/fazh/o9G7KPX6Bld9EUqwxgfl9yMh29TjsuocjGX/5BUPPJWynAep1giVDCX4Ze+1kAilJAwLY3lEJaxujT5O+/UZUDuUvE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=V9zpcKZD; 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="V9zpcKZD" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 276EFC4CEF1; Thu, 8 May 2025 23:33:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746747217; bh=xVs4QemZgCqU8+2ERmMbebq38vqQbBEp640MidYy624=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=V9zpcKZD0TJEm5y2u9JHNyO3f1q92s7Bv2EMx91UW655NUBBllttnLL2/PY757ihO UUno1wXb1ljnZyCrEDqC4h7OW/mjTkUYbWJTLROQdHyzxyCmjSGChLr3p4YtSuFahK lZw9M9Yxhz+Yr87YdSSY7el+eNgQoyum/oVTSwt15tAy44sjB7SHQuQnOVUJzGo3B7 uxh/VBpvlyK6rr7ek5e5nVaVpCW2U1V2nfhT0lvcbKxICubexyaCCFFnFlN5CKj1BK QQ/H5n8DXCyXdELSY2gPRvOX6xKwaXThXe/IdMhJmws+r4E3OjL9vzBemw7z/XM8Rp 0QPVgbEWrr5PA== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 750B4CE12BF; Thu, 8 May 2025 16:33:36 -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 v5 09/21] ratelimit: Allow zero ->burst to disable ratelimiting Date: Thu, 8 May 2025 16:33:23 -0700 Message-Id: <20250508233335.1996059-9-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> References: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> 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 Mon Feb 9 12:11:22 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 77DB7230D14 for ; Thu, 8 May 2025 23:33:37 +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=1746747217; cv=none; b=adB9wXJdDetBaBDub0RDbzt93tYmbGiHYkPM/C0/f1fpvQJ4n9r3WymV34mt3DD4XDejosfxdyHmPfLqPhiyICCF2T2DTzcBVTYRRozTMgvF2ZbgawMlVp6+3+39fz6kCwnEPX4JfZFsaJ0LzAWvXtaJmBz0yK5ndXWWNKId7GY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746747217; c=relaxed/simple; bh=jzDjoHtGux5M6uZMIriVC2B2N8Be8eXXJtqGq6UGj3E=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ptsUqs60G4wffXYSNWg6RFdNvok50O14NtZhgvZhytv29pQO9ooKKDuArXjY0q6IrsW6N7wOQ1C9h/m9wu2ocwHZ/2N4WtIOm/dqIBEEkYXqg1BzHnEsbCz/w8gY7SHxQ/DHXo4c1pIos+PMPkKORqaQzITr4uyXVq5WEb1yur4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Hub5LdvX; 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="Hub5LdvX" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3074DC4CEF6; Thu, 8 May 2025 23:33:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746747217; bh=jzDjoHtGux5M6uZMIriVC2B2N8Be8eXXJtqGq6UGj3E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Hub5LdvXIykLunoGjodidtxd+/ESsmnK0SAS+KqdmQWvNneA1NOyhB9ywN0lTIzMy J4leEsZV8P26ou4Y5YC7tBCDhC4EFEWoQszuw5zngqhj5pxZuamKLxWjxDhXWDavIo Ca/WlBxXyRS4XLEckj8B3DZ6a60Q7QyID70RZCI2eV9HbAF8Hs/+PpakoUVSF+L313 /PDboHq5W0FOQz7WfkX+aYO7UXjH7aF5nc6TnXIiuZL0EJqLrt8Prbjt5HpiNftq99 zQ+0/02L9wVxVwSy9+yTmA8y9/lLoEw0Pgp02byWJdnh7LVLOJmAgwXYqb4cusrmN5 XYj8kqxGlnRhQ== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 780D4CE12D2; Thu, 8 May 2025 16:33:36 -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 v5 10/21] ratelimit: Force re-initialization when rate-limiting re-enabled Date: Thu, 8 May 2025 16:33:24 -0700 Message-Id: <20250508233335.1996059-10-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> References: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> 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 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 | 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 Mon Feb 9 12:11:22 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 5FE16230272 for ; Thu, 8 May 2025 23:33:37 +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=1746747217; cv=none; b=E6zU1kET8yTv9CJi/6DQAmJLc8QNf14btRFRUbaN8eNI6vzO/R/XfHtxlDfcebR0bAJYMi6wGkBwIlSvb9KFdHT1YwWvgg5/EUIlZL++ubDkVT2NFAL+ntqhQ6COfNJwcuvk5cWBO8zivu6xCQQb3R6fn882e2pfQwVRRiqS4yg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746747217; c=relaxed/simple; bh=x6kgo4O9hta9sDmEsHUTl1Ls9HFDrMxc1Cr1vGjF/bo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=RzGoDNGSWDwsLwCrX1Vz7IOJtjYZb2YJjIi1EZ2RrjbJImJrc7KHZH+yWL13a3G2r1gPUBQWX9mL+ukIGf+9hAzZFoN77eZ4B/Ln1HlGZps/zgJxePyCDegOxhYrWtNe3+HfXrzQxjmATENx8p91rCZSlIld2BJY9ZvgGJVpNVU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Ctcw4eHz; 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="Ctcw4eHz" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 384FBC4CEF0; Thu, 8 May 2025 23:33:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746747217; bh=x6kgo4O9hta9sDmEsHUTl1Ls9HFDrMxc1Cr1vGjF/bo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ctcw4eHz3yC8z4dT0dBFf8dozjkSA1RYzWyYRMLqdtD+NUodKLEf81guFtr1epPlZ lAUpnVZl0bkQur7InS6ah/+ICj00eT5KFK/HS6qWYrdBDpcC8DdPpLmQvsjpLK1KN7 ockfg5LCW8SG3dfd5wxYHrYDouKkWtDdiWFbbGA++EbpZnqCwcCaCSCt6MqDQZGUk/ 8iEDlRYOanXU7/48QZKD+gGVScj0GDKGhiEXBHDj8v7Au62360Md6lQ1ox4r/FfDFR WW4P/nL3S6IpZBYuhNqln1uw69UT3BJVWwjY8mEp87dsnMblrchZbwXJ1OprlowtOl QEz1ov++o8UtQ== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 7AFB7CE130E; Thu, 8 May 2025 16:33:36 -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 v5 11/21] ratelimit: Don't flush misses counter if RATELIMIT_MSG_ON_RELEASE Date: Thu, 8 May 2025 16:33:25 -0700 Message-Id: <20250508233335.1996059-11-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> References: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> 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 Mon Feb 9 12:11:22 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 9433F231C9C for ; Thu, 8 May 2025 23:33:37 +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=1746747217; cv=none; b=RdT8vb7gLG4tq2t6DmqSnOwUAfDhMeEeelOBn5LEgAUd7q/d0BVpTAZWTPYjNcJI5pH+1EBCBNycYopW3UTPQMjmRwKniYm9X4nLyaB++TXEq5zUUYCHPG3lVg/oL0sxDPCw8Eo3VwRCmXZ7mQxcgBMMHIgRr8ULs+7pxvazcc4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746747217; c=relaxed/simple; bh=QCSRO+sN3BeKZWvK+OThs6qhu1d5NNPNgRJTZ+bBQSc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ggIOaKICaL2UOsXRjFa6fvse2a7uVmS18DTtwBczJh9pdLQ0LM5O7G1H7nTtc5h+YZDF4XCiq/OKjr6a7gBVewvMmOa6SByGwE459mR4i4NuKSs2xgco3N4xRHUcMy8U8rbFjFkMeLhJ6lGcXTCznm9Y1oBqzvnzCup3hRlmPzc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=nXjo64Uj; 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="nXjo64Uj" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 46037C4CEF8; Thu, 8 May 2025 23:33:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746747217; bh=QCSRO+sN3BeKZWvK+OThs6qhu1d5NNPNgRJTZ+bBQSc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nXjo64UjKkmd6b/RYf6D7M34C5NytrQDMZL71m6g2AJncbPJ1iYd0PRNyRkhyD/3a SHu8xdTAELzJI7XjssHsR2erY2KyCGe7qpuLVyZF/kDxe/zV2zve41qPHpvCe84JYa tYK7ILoocyEwugGmvBI80kvClm9B7un+9yIOD25D6vo6qZ4MTxA5WA2PFagDy6mg1f XZYn50eODcZVnfZIy8TjIO+P4awiy6xER0/355g50FdVpze4DfW9IdCK0lX/+f7Frn m8+7bIrMpzRta4FPREYh/WczU7yyGHdJQIPNLtKRGJq0LWN9CSQD7jvel7uah6Z5M2 sgJlAg6A/ky3A== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 7E2B3CE1389; Thu, 8 May 2025 16:33:36 -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 v5 12/21] ratelimit: Avoid atomic decrement if already rate-limited Date: Thu, 8 May 2025 16:33:26 -0700 Message-Id: <20250508233335.1996059-12-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> References: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> 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 Mon Feb 9 12:11:22 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 942DC231C8D for ; Thu, 8 May 2025 23:33:37 +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=1746747217; cv=none; b=sGAyWWiRz/k5x6txcA6Jc4GrIcIGelj1McJxf4AuiMzPHA74cfMlUzpMHyrsE6CXeaO1xI56SXaZhODvedPiTliVA+5+AO7Qqn+JmHxJKycBGicij2Jz27G1fbCqOg63GlWXUfONMVU86YMWeBCQMcRkTMJ+yuCrSAM/ZQ2x9pA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746747217; c=relaxed/simple; bh=NCii0AygsQ7t/hzmWNQsffMOxKXMFGYLlumd3Gn7O9s=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JHX/OXGt6OAgVsatTvsYO4YQV+SW7+DoaEpQp+sxA1o0J1pHoX4/n/RKBLgzzzFd3VlZrBc6xwRnME4THyvTb8cfsswoabqoAPyLSrj6wEvynUsNvAzgblNYRw/1MuEl1/HEgtDPflpSKkfbye+njM4a/nLD6Oz87b+2derdIxI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=qxSkAWVE; 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="qxSkAWVE" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4B1B5C4CEFA; Thu, 8 May 2025 23:33:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746747217; bh=NCii0AygsQ7t/hzmWNQsffMOxKXMFGYLlumd3Gn7O9s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qxSkAWVEy5g66fnp1h0GrnYMF26QiEwV8l2E35tRCZsPWbMh2fFmMCMC14Ff5vlmh dDBZtTY+9IKax9yw7C6bc0rkP5wzA+BA0YX8jEb9h7NoWcdxpRKooaPlmhirRkuXr4 MsO6bhtX5eTL7aHCN/2kaigMW+Gdnk+7yCPjLVj0+TdAvWluXcimIls3rfQDiFBdye RL7baUK3w1fJA+QLh0MDLdROVphBFYhTOva7X8CrivtRMcymWVbCZTBAuyXGKjcMuy IFN6PL5uV2p0RGlM25EQtaeIv/8It58lO1GROb/X46BIsFOcT3LtJhEMGXEXueyqPl 0VI8bhODEZh5g== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 811A4CE13B4; Thu, 8 May 2025 16:33:36 -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 v5 13/21] ratelimit: Avoid atomic decrement under lock if already rate-limited Date: Thu, 8 May 2025 16:33:27 -0700 Message-Id: <20250508233335.1996059-13-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> References: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> 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 Mon Feb 9 12:11:22 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 95D91233129 for ; Thu, 8 May 2025 23:33:37 +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=1746747217; cv=none; b=mxnlyACVWVLEmz8iL5XmRoUSthgCfslVdHteLO1K92t0vhs3f/leh7GFCKtW6j2LU8VyaB9H7NrSagjK0iQzguVdXa5B9c/7UVGg10OiYZhYK49J4wf7MNwa0r0x/+lhJTg1A024ACmsUAfZopTEvK6TUj3iZCAb4FmyI1jAyAM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746747217; c=relaxed/simple; bh=pXpLtvkklfQw6NZrHmnRsqa8G0gH1/KimuwtRPQx/mI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=T2+eBVb6m2a8tpqX6KWIjZe5/sTeKBMsf7+WKlb08IODkvhTSNTjNoZO+NYJsuK8pqmTmwJET4mKhB2aWzKuPuxTEGJU3JE13+CQIDGrhOtFY8oAP63xjYGziL4egi3/+M+VKKbF8+45SWBKEGgQQteHzxPyw5dq1suU3FZdDVM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=YsPvAK9i; 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="YsPvAK9i" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5459DC4CEFD; Thu, 8 May 2025 23:33:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746747217; bh=pXpLtvkklfQw6NZrHmnRsqa8G0gH1/KimuwtRPQx/mI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YsPvAK9iRkOD1S12R1g2HzUxdXewdcXng89ZMLPoHeMHjn8N3VUZWzcPklcR19Thq PWNDWUbPZbGPjGc/AheDESYW3hCYOHfimzM7N5ooV93uRI0utIUmqoBFUzrgcFjtP8 JDif6BH+5hRVYgzURE9q3nrm6HAxyiDK/pf1bEcGMoMWNkQoKFbftd2jZSjIVMWotj MP1h+hzOyLkgh3bzdabnBS2LJhnQhlL63RcMnkOJ9l5PV9jl6cWPhiA8ynmwTAT/xI OqUiXBiUWkwL3wq2OyZ7tdEI6G6AZbOrQn5NwDyxHzLwGAgMBmb/vy1yDFzso2LRqj fqAPVASrIcYZw== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 845D0CE13C2; Thu, 8 May 2025 16:33:36 -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 v5 14/21] ratelimit: Warn if ->interval or ->burst are negative Date: Thu, 8 May 2025 16:33:28 -0700 Message-Id: <20250508233335.1996059-14-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> References: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> 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 Mon Feb 9 12:11:22 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 8BCE723185B for ; Thu, 8 May 2025 23:33:37 +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=1746747217; cv=none; b=RUWVi5bqPOx7nMomlqmr6R76VWSeAiF0s83wfx14e/VuDkBjBXQD3L3exrFPVd2IXNnxbwD2HtMdZ/Gd5yUsdiaWVPeTUZrvZujr1+3bl5dkkTSwdIPwTx5RQvc57SLyj/7bG1SGNzVnE9GM/q72fNsvj800I/jKnTHFa3s4+eY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746747217; c=relaxed/simple; bh=F8T2xxbMxQXl7LOrVaC3+b+nco6tPLr/P6VQcN8CUaQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=YGTWgT9OYN3JuyGlhVHo+dA1uh7ZMGsAO9xyFLT5QJnjlvX2twSDD9xZdiWUfhj/RtjdckOj38/sTXsGFLCRJ4M818gsQLZnydX2qh5bOpWUTbZcHUVi9iOXIBW6zQcWHbhrnmivp0HT389c0oYjDNDYvckApot7Y2Yg6mWRF1Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=o1k4bXHL; 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="o1k4bXHL" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 644B1C4CEE7; Thu, 8 May 2025 23:33:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746747217; bh=F8T2xxbMxQXl7LOrVaC3+b+nco6tPLr/P6VQcN8CUaQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=o1k4bXHLdsGiEDYCbOugw4t3HlUtxJbb+gLDFgjtD9XYnY3HJyU29NtFfKs8qg9Ui GQA/7Jk2blh1+PMJbDwzVE6XOyvduoy5ZxcfPZmO839Yk+Rk5o09j215E4gRwj6MZp ENVQ9D2cflyIec9lxQC/bqyXOXApZLoGtxOho+ZX9+YPG+QVAckfJwFfIsj+cwCbF/ XUUiIA1c6LaWcXqf8b7nBMaefJyHaaEeSViFql0AA9dmNPrR29cy+MHMtqeCXn8OrR nwSzcONWNAKIdQ2TgtzENfN8V92RGc2GLARe8Q4/pSBTKJ7YbRPxg7N5sJOqw9H+7Z Zd3c8MIyPUO6A== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 87689CE13C4; Thu, 8 May 2025 16:33:36 -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 v5 15/21] ratelimit: Simplify common-case exit path Date: Thu, 8 May 2025 16:33:29 -0700 Message-Id: <20250508233335.1996059-15-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> References: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> 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 Mon Feb 9 12:11:22 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 90CE2231C87 for ; Thu, 8 May 2025 23:33:37 +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=1746747217; cv=none; b=loCyZdN0eT5uKJTBIiiTRjeMzxB5ljg1QSs5guwAH6LJ37IUgsxx1RndAGwXbkiBBKKx2A5dcQ5rQWsBtuHlu6nJyZSq7jSp5FEgnEhya1wXjj0eU4dWxlZc6PGbnIZLJDCqP5kVH1uoEGPTBnSkPNLendS5UFB4p6zSt2xM4sQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746747217; c=relaxed/simple; bh=feyMWlvQ3IMm4wSDJau8dg3L1bsITx2hZGbyD38McFU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=O7/HHT3BC4CjR4ml1RBXohAOGgcHoNE/tWQ3tFUbNDYpVE3WwdR7CzpwmFQWcofVCHzGIB25SxxmQ1CU7OiEnKhC/D7jq6EdRXBh/00N8Sb+YuJbsMz8LR+4JFQo/enzzrTgfNxCpbaWpudv8qm549HBK8C/Hyw6d4gZkhbMWmk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=jdhlkYsU; 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="jdhlkYsU" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 75DACC4CEED; Thu, 8 May 2025 23:33:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746747217; bh=feyMWlvQ3IMm4wSDJau8dg3L1bsITx2hZGbyD38McFU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jdhlkYsUcbzgkgtyNuejBiUiIVqPihovc97rpKzmJUbjBBYrTXD1FO0q0bicHtIuV SZEu8jTeVDP/1NwvNkX6m2K66m26QSLgpnMuI4PByXYo3qoCMPxKBikF9RbUcLDEoI yxzGYOkftCJKqF0Pz3uo4pl1IApGHmN28IipKmp0BMV73oiveXUeejyhFGajVOT25D 79bb3/aF+MyzkmKMAuOD2Aea6z6JOhPRZN+nVF4nA6qy1nzBTiFpG2Oj7329if7Us7 myjrwMe2mNo9O6CCVXwomzOSfk+wJbsYYqEO8YXnhyi+3D2jrGDRYhksc4oJYN7N+U jYK0q5EP+O6dQ== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 8A58ECE13CE; Thu, 8 May 2025 16:33:36 -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 v5 16/21] ratelimit: Use nolock_ret label to save a couple of lines of code Date: Thu, 8 May 2025 16:33:30 -0700 Message-Id: <20250508233335.1996059-16-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> References: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> 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 Mon Feb 9 12:11:22 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 90C70231A3F for ; Thu, 8 May 2025 23:33:37 +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=1746747217; cv=none; b=gVY/yY/LBe/mLW2BZo5EaYwG7XBcI/okKqWZLewm73RGqGUwZCdRbAXi9fRKzjYVtOgZuKaGEsV7QzSNy8czO86uT4QhZnI0aC+0d9lH80r4w4c6RJ5GkpV9xExt8L2hzZlyqEiEdzONiCIgkW/QkLl/tLMPv8Ff3mR+Yx+NxvI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746747217; c=relaxed/simple; bh=IXjNK7JRbzJgmjpzWE0gJOfavJOhfRMhhLqkx9ULkFU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=iHu9h8jsvhVwH1kT/8AI3/6quMbwWZQ+o0ucN6S4TmQVhdgydVfiZYTTqfRmJjbN1DTN2F2aS8seGt/DD1Cd1O9nhfcYYkf1fkaQj4XmuOf9br+YFHiMjIfAWFbRe+++V9muqXrWaJjYVGZXTjeSfi1nu+Ncm+aLbQzZ3/oXPJY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=h0PTbo3P; 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="h0PTbo3P" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 731F7C4CEF2; Thu, 8 May 2025 23:33:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746747217; bh=IXjNK7JRbzJgmjpzWE0gJOfavJOhfRMhhLqkx9ULkFU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=h0PTbo3P754zYfPGdV516KM07CqLlhVtOmlQqNhzlYMQJs84iLuudF+Bz7ePqxNEk Ml+xBk5KWhx24TFb4YAx0BaWrotIwTrV1jf1WNl99LJpSCNyecUCNAT8cDk4d+WAOQ DpbIMyD7c9A1MbfbxRNyyoEJFyW83Vbbra8Xlezsrb668BU5kZuoVXlMy7qi1hCw5S lTI4Vc86JI/drW8QvABUJJGyHDRj8xZa21m5EHJ9b7kNPOIgYca0Fzr0TUu1pSYk90 AgzZ7B5/TN2tWY90N3KmLTq93Cdh/BLh8wJzot2nl5WB68uzKEygqOUVUNuACHiOIv OiltNyk+MhdnA== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 8D4C2CE13DF; Thu, 8 May 2025 16:33:36 -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 v5 17/21] ratelimit: Use nolock_ret label to collapse lock-failure code Date: Thu, 8 May 2025 16:33:31 -0700 Message-Id: <20250508233335.1996059-17-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> References: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> 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 Mon Feb 9 12:11:22 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 9FD36233158 for ; Thu, 8 May 2025 23:33:37 +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=1746747217; cv=none; b=AxT7AEHxcbihFnPUXDmvll/NQ6cHjHPs/vYpydAAgaGkyaAfVs3N08RarN97uI8niG12WBnbmornkVS2NFK40kHDEDcuD7eKshQPL3TER/dXlMiGHlL7ClNfGnpHSsfXtQipy3AvddTakOmMGI0iBOURrnVRzV05b+eKGLQ/+EM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746747217; c=relaxed/simple; bh=QuXY/enqInz7nSkzplVvspsi9vNMNEEbLePY/x4Nq0g=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=esbqA+RE925Q8z+dx6fGvmS8QvMpOewblxStUK38Vk8A5zhLXWuXQsfKaS6nAw6X+zEdZDchqhUyA5dXMFY5cxJG24GWmMX9XkWHxKKlhfnH9lTDh6aOSpoX0rkuYgY+SFc3AcBR6yifx8xPbTj+XLwMoUP7U03JG2Rr7RleY6Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=J+HkB5XS; 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="J+HkB5XS" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7E263C4CEF9; Thu, 8 May 2025 23:33:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746747217; bh=QuXY/enqInz7nSkzplVvspsi9vNMNEEbLePY/x4Nq0g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=J+HkB5XSP3ZsLFie8L/79AzZaN7ErcgWppHIrYhFdTMrt1bKtvu8+0ldWo48wmakV iBzkUVoSMGdyWtvBurI54PdGgQzn2lMb795+AJ8gKB3rM1ema5ZPOOIIZca/r2oWzu 4w66/7q/HNWDCMRHvWxnM6S3Xnll/xZrwhiuMv6jqVSKoF2l96sktcwC9S3+9Id8Qd k/9Rd+W2FzZ80F4/OfNnD/YlZw7EStEZPfaIJh/PTNqY+a8orOgFjxuKMBJBMqMW7b 1/YS4aRf5GU15dJObVmKXfN9nPxjQlL6NhyI47LqzIo39l+Hs9Byzoac6z+JoOJj8a df9mjktEn8xlw== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 9037ECE13FF; Thu, 8 May 2025 16:33:36 -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 v5 18/21] ratelimit: Use nolock_ret restructuring to collapse common case code Date: Thu, 8 May 2025 16:33:32 -0700 Message-Id: <20250508233335.1996059-18-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> References: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> 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 Mon Feb 9 12:11:22 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 9FCD9233151 for ; Thu, 8 May 2025 23:33:37 +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=1746747217; cv=none; b=B4OSn8y8XBAJODlSmj9NlI41YepGm86/w9vEd85InIBFabjsntvh1QFDXco8BQQRhwR789kxVSoM9EWWLISxFFRoalB+3gn8qZGfJqAB8+uHrH+PJ4FBh2L75dAdyWUArNJJBCIXVVUel/fKKBy+m5delLazfOVDz2UmntuVHfc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746747217; c=relaxed/simple; bh=YB1sT29rmVOlTPoKn0b58m/k1XM/JrnzIA5s1bpM7OE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=MQotR/blEVVlPuhWT/JvR7Lz+PRPPyPCaY0J1xkcZDJLdweD1AW5PnSRajCJf/GZ3QmpFJF7uSrTFYb0OUtWT9+/xa2TP0oU8W1Tc9CUf05AXEz2SMRG9IKYysuyfSi4MgpwHJl62cgyCgwkY3d5DBhl5NPNZ4woobZHhYWTWY8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=YXF9XZlK; 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="YXF9XZlK" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7E1BBC4CEF7; Thu, 8 May 2025 23:33:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746747217; bh=YB1sT29rmVOlTPoKn0b58m/k1XM/JrnzIA5s1bpM7OE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YXF9XZlKf91ukGJZqS2AMyxlIgCipDyPibNs9ccP9xuqCU7HS6MGOWTteVU/1rU/b IAMRVqHkah3xrpEjlASSJVvFcjXuYJ5saq08p5s1R+A6wYNakM5F3U8sYnnRm6EWRD NMMRAQU0zPjfi8+srUrCFThV0KfQexwsvI4S96WLeD5em2EaLCP9+Y/0H00tlQmCfP 89+7fEsEwg7PiOTb1yK9HO+CyLTVnd/bPHbzlND65HrVehVzBg25EPsHU52yjbnn2T TKW+wypjXK8wqYPOVIEUQWYhiFlCjwq4OdeCzrG/aU7oXth0vF18ZlfO4rCjGal9ye buaz/6X8+7vFQ== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 9394DCE1427; Thu, 8 May 2025 16:33:36 -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 v5 19/21] ratelimit: Drop redundant accesses to burst Date: Thu, 8 May 2025 16:33:33 -0700 Message-Id: <20250508233335.1996059-19-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> References: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> 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 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, 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 From nobody Mon Feb 9 12:11:22 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 A5A1C233701 for ; Thu, 8 May 2025 23:33:37 +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=1746747217; cv=none; b=KL8elFHo/GuP8Ni4zynRjC98gvjGHOlmwpIFMKtAm91/C4Mf/QA5kRvygQCsaVMcI4EjHd5u3svL1KcEg2Y96AqOGg7yvXgGtkWCyTvxV90gGzApzVfK7pgCWMOZdVTs54vEaDZLLVYud//8kbsqZscToyIweSp4qWir47qSVVE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746747217; c=relaxed/simple; bh=U7zgE6IzCLrPpMWVpq2+GUdcrSUMCh/2FsvZ9huMwiU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=loahuvOk+7E0O6tHKdUzNju9K7vHt2/fi95vcDDRpWU2wF8pjAr8RoXQv7EFQP8tACDciTBiB5vEhXzBvMRzOzWsg7z1K809BpO5RkRNepgTgUOIpX6XnTOkhwjuwhTfKCLIUP3a7xV5MPTjqTpLTYj2FJkpeG4q62dKXAtTh5Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=MGSIrDM8; 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="MGSIrDM8" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 86F1BC4CEFB; Thu, 8 May 2025 23:33:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746747217; bh=U7zgE6IzCLrPpMWVpq2+GUdcrSUMCh/2FsvZ9huMwiU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MGSIrDM8OOsNfk8o7cQbfOhtCHTXe3lVBuo2ciQbEBz0UAol+TDpwUY1OgVvza6T/ 1xRRWr5r1P4zieSS47IzoNQa79a4c9o1TCCe380WYa6YK/427ib/EVju+PavWlGILE rpTdTLKRyKwx7QYfO/c+SjBMziIDQukHp3YVjNbJT4JFC+SZaRw13O8JdX/WAwYoY3 lVBA0AVySc59GwMLyHytX3qo8H76b5P0gyoChsP7TdEaQrhV+KrN+PTVooe/3xhiwN bgQsHFEjYW1etL9nM1w9X+gJAJ/FVqq/rQyjjkipa67Bg5IAohjpqG+NE78vtzLaTX 3HCURgF425Reg== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 96C17CE1432; Thu, 8 May 2025 16:33:36 -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 v5 20/21] lib: Add trivial kunit test for ratelimit Date: Thu, 8 May 2025 16:33:34 -0700 Message-Id: <20250508233335.1996059-20-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> References: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> 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 Mon Feb 9 12:11:22 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 B3FBA233713 for ; Thu, 8 May 2025 23:33:37 +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=1746747217; cv=none; b=K+tpraLBZwC+ekc2pCtcu6tM5zY+iKKmy3CixNXBLS5Jq3XQ+mGP25ymaslheeT5mNp6pfdMxJwc/iinLLdQcCDKKoxKAZwgbnssmkQzBjYZPaOfxcSho5fWfy7SoVkBb8Bt+7sTqwL88py6UMD06egEd2zqARU9ouXJoOJBIIo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746747217; c=relaxed/simple; bh=SvYRsTTTQICOYz8yThrRfPXwRKZWBL4GRgzZV2m3sC0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=LrfXsMXZzbiwMSDkFzgtJPOvYHlCDh0fp3EkJ7xF39MQeMFoR+gPr7CiqYb5jCXAOFPHooxfGbyr8i9txdAwdlIRZ3EUgi/KetODOE4CGGlC+iypM8HOw2Ey+OEPDRYX10NbBev4HpcyE9e/lpQinY7yCoZZlsEbHk/VlJX/fPs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=LF72BmdJ; 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="LF72BmdJ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8AC15C4CEF3; Thu, 8 May 2025 23:33:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746747217; bh=SvYRsTTTQICOYz8yThrRfPXwRKZWBL4GRgzZV2m3sC0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LF72BmdJGbNou9K1lp3p/FmR4ok/z457cnXRiKfm+TqNXcj67IxKOwNWP2hdUDSxk l/16bdHy56jztGXuUn0OQbWjpB4SKfNyqdHUH7wFGE6sPudbSzIP1SgKGcSbe3A+8U JZ2B2+0xdW5a+CqBQbcWez/85WHmiRORI9U9wuzy/rTOTjvPpwGRiXyovs6rBY9gyQ +jmHUEeQ/RJTxlxQU0vQ8ys+69Kpr/k2kLKZwwlv3c4I0iOuzXK4TDxdTF4f3H6Dcg qOfWQRJDqCIHkNkvUnL56OLaelLyvKzOMBEH7WE6RnFbtVW2o8FxX726dQDDq86hga rPxkTr6ijwxFg== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 99F6CCE1440; Thu, 8 May 2025 16:33:36 -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 v5 21/21] lib: Add stress test for ratelimit Date: Thu, 8 May 2025 16:33:35 -0700 Message-Id: <20250508233335.1996059-21-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> References: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> 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 stress test for lib/ratelimit.c To run on x86: ./tools/testing/kunit/kunit.py run --arch x86_64 --kconfig_add CONFIG_RATE= LIMIT_KUNIT_TEST=3Dy --kconfig_add CONFIG_SMP=3Dy --qemu_args "-smp 4" lib_= ratelimit On a 16-CPU system, the "4" in "-smp 4" can be varied between 1 and 8. Larger numbers have higher probabilities of introducing delays that break the smoke test. In the extreme case, increasing the number to larger than the number of CPUs in the underlying system is an excellent way to get a test 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 Cc: 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/tests/test_ratelimit.c | 69 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 2 deletions(-) diff --git a/lib/tests/test_ratelimit.c b/lib/tests/test_ratelimit.c index 0374107f5ea89..bce80d9dd21bf 100644 --- a/lib/tests/test_ratelimit.c +++ b/lib/tests/test_ratelimit.c @@ -4,6 +4,8 @@ =20 #include #include +#include +#include =20 /* a simple boot-time regression test */ =20 @@ -63,14 +65,77 @@ static void test_ratelimit_smoke(struct kunit *test) test_ratelimited(test, false); } =20 -static struct kunit_case sort_test_cases[] =3D { +static struct ratelimit_state stressrl =3D RATELIMIT_STATE_INIT_FLAGS("str= essrl", HZ / 10, 3, + RATELIMIT_MSG_ON_RELEASE); + +static int doneflag; +static const int stress_duration =3D 2 * HZ; + +struct stress_kthread { + unsigned long nattempts; + unsigned long nunlimited; + unsigned long nlimited; + unsigned long nmissed; + struct task_struct *tp; +}; + +static int test_ratelimit_stress_child(void *arg) +{ + struct stress_kthread *sktp =3D arg; + + set_user_nice(current, MAX_NICE); + WARN_ON_ONCE(!sktp->tp); + + while (!READ_ONCE(doneflag)) { + sktp->nattempts++; + if (___ratelimit(&stressrl, __func__)) + sktp->nunlimited++; + else + sktp->nlimited++; + cond_resched(); + } + + sktp->nmissed =3D ratelimit_state_reset_miss(&stressrl); + return 0; +} + +static void test_ratelimit_stress(struct kunit *test) +{ + int i; + const int n_stress_kthread =3D cpumask_weight(cpu_online_mask); + struct stress_kthread skt =3D { 0 }; + struct stress_kthread *sktp =3D kcalloc(n_stress_kthread, sizeof(*sktp), = GFP_KERNEL); + + KUNIT_EXPECT_NOT_NULL_MSG(test, sktp, "Memory allocation failure"); + for (i =3D 0; i < n_stress_kthread; i++) { + sktp[i].tp =3D kthread_run(test_ratelimit_stress_child, &sktp[i], "%s/%i= ", + "test_ratelimit_stress_child", i); + KUNIT_EXPECT_NOT_NULL_MSG(test, sktp, "kthread creation failure"); + pr_alert("Spawned test_ratelimit_stress_child %d\n", i); + } + schedule_timeout_idle(stress_duration); + WRITE_ONCE(doneflag, 1); + for (i =3D 0; i < n_stress_kthread; i++) { + kthread_stop(sktp[i].tp); + skt.nattempts +=3D sktp[i].nattempts; + skt.nunlimited +=3D sktp[i].nunlimited; + skt.nlimited +=3D sktp[i].nlimited; + skt.nmissed +=3D sktp[i].nmissed; + } + KUNIT_ASSERT_EQ_MSG(test, skt.nunlimited + skt.nlimited, skt.nattempts, + "Outcomes not equal to attempts"); + KUNIT_ASSERT_EQ_MSG(test, skt.nlimited, skt.nmissed, "Misses not equal to= limits"); +} + +static struct kunit_case ratelimit_test_cases[] =3D { KUNIT_CASE_SLOW(test_ratelimit_smoke), + KUNIT_CASE_SLOW(test_ratelimit_stress), {} }; =20 static struct kunit_suite ratelimit_test_suite =3D { .name =3D "lib_ratelimit", - .test_cases =3D sort_test_cases, + .test_cases =3D ratelimit_test_cases, }; =20 kunit_test_suites(&ratelimit_test_suite); --=20 2.40.1