]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
ratelimit: Avoid jiffies=0 special case
authorPaul E. McKenney <paulmck@kernel.org>
Tue, 25 Mar 2025 22:32:54 +0000 (15:32 -0700)
committerPaul E. McKenney <paulmck@kernel.org>
Thu, 8 May 2025 23:13:26 +0000 (16:13 -0700)
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=1000, 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@paulmck-laptop/
Link: https://lore.kernel.org/all/20250423115409.3425-1-spasswolf@web.de/
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
Reviewed-by: Petr Mladek <pmladek@suse.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Kuniyuki Iwashima <kuniyu@amazon.com>
Cc: Mateusz Guzik <mjguzik@gmail.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: John Ogness <john.ogness@linutronix.de>
Cc: Sergey Senozhatsky <senozhatsky@chromium.org>
include/linux/ratelimit.h
include/linux/ratelimit_types.h
lib/ratelimit.c

index c78b92b3e5cd833a5c8570a7657e26a0bd74c384..adfec24061d16f7fddd8a6827e84be2fbc8ad82c 100644 (file)
@@ -43,7 +43,7 @@ static inline void ratelimit_state_reset_interval(struct ratelimit_state *rs, in
 
        raw_spin_lock_irqsave(&rs->lock, flags);
        rs->interval = interval_init;
-       rs->begin = 0;
+       rs->flags &= ~RATELIMIT_INITIALIZED;
        rs->printed = 0;
        ratelimit_state_reset_miss(rs);
        raw_spin_unlock_irqrestore(&rs->lock, flags);
index d21fe82b67f67e67b3386124ca503ae0025a6796..ef6711b6b229f626f5ceef0f1e184a32d3945eec 100644 (file)
@@ -11,6 +11,7 @@
 
 /* issue num suppressed message on exit */
 #define RATELIMIT_MSG_ON_RELEASE       BIT(0)
+#define RATELIMIT_INITIALIZED          BIT(1)
 
 struct ratelimit_state {
        raw_spinlock_t  lock;           /* protect the state */
index 19ad3cdbd1711e42feb62e6a8bdd5b8c5774c1eb..bd6e3b429e333fd388f35cafdfdb8b6dea50fd3a 100644 (file)
@@ -49,8 +49,10 @@ int ___ratelimit(struct ratelimit_state *rs, const char *func)
                return 0;
        }
 
-       if (!rs->begin)
+       if (!(rs->flags & RATELIMIT_INITIALIZED)) {
                rs->begin = jiffies;
+               rs->flags |= RATELIMIT_INITIALIZED;
+       }
 
        if (time_is_before_jiffies(rs->begin + interval)) {
                int m = ratelimit_state_reset_miss(rs);