[PATCH v2 ratelimit 11/14] ratelimit: Force re-initialization when rate-limiting re-enabled
From: Paul E. McKenney
Date: Fri Apr 18 2025 - 13:15:19 EST
Currently, rate limiting being disabled results in 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.
Additionally, interpret non-positive ->burst to unconditionally force
rate limiting. When ->burst is positive, interpret non-positive interval
to unconditionally disable rate limiting.
Signed-off-by: Paul E. McKenney <paulmck@xxxxxxxxxx>
Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Cc: Kuniyuki Iwashima <kuniyu@xxxxxxxxxx>
Cc: Mateusz Guzik <mjguzik@xxxxxxxxx>
Cc: Petr Mladek <pmladek@xxxxxxxx>
Cc: Steven Rostedt <rostedt@xxxxxxxxxxx>
Cc: John Ogness <john.ogness@xxxxxxxxxxxxx>
Cc: Sergey Senozhatsky <senozhatsky@xxxxxxxxxxxx>
---
lib/ratelimit.c | 16 ++++++++++++++--
1 file changed, 14 insertions(+), 2 deletions(-)
diff --git a/lib/ratelimit.c b/lib/ratelimit.c
index 04f16b8e24575..6c639ab6489d5 100644
--- a/lib/ratelimit.c
+++ b/lib/ratelimit.c
@@ -35,8 +35,20 @@ int ___ratelimit(struct ratelimit_state *rs, const char *func)
unsigned long flags;
int ret;
- if (!interval || !burst)
- return 1;
+ /*
+ * Non-positive burst says always limit, otherwise, non-positive
+ * interval says never limit.
+ */
+ if (interval <= 0 || burst <= 0) {
+ ret = burst > 0;
+ if (!(READ_ONCE(rs->flags) & RATELIMIT_INITIALIZED) ||
+ !raw_spin_trylock_irqsave(&rs->lock, flags))
+ return ret;
+
+ /* Force re-initialization once re-enabled. */
+ rs->flags &= ~RATELIMIT_INITIALIZED;
+ goto unlock_ret;
+ }
/*
* If we contend on this state's lock then just check if
--
2.40.1