Skip to content

Commit e64a348

Browse files
committed
ratelimit: Avoid jiffies=0 special case
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/[email protected]/ Signed-off-by: Paul E. McKenney <[email protected]> Reviewed-by: Petr Mladek <[email protected]> Cc: Andrew Morton <[email protected]> Cc: Kuniyuki Iwashima <[email protected]> Cc: Mateusz Guzik <[email protected]> Cc: Steven Rostedt <[email protected]> Cc: John Ogness <[email protected]> Cc: Sergey Senozhatsky <[email protected]>
1 parent d343732 commit e64a348

File tree

3 files changed

+5
-2
lines changed

3 files changed

+5
-2
lines changed

include/linux/ratelimit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ static inline void ratelimit_state_reset_interval(struct ratelimit_state *rs, in
4343

4444
raw_spin_lock_irqsave(&rs->lock, flags);
4545
rs->interval = interval_init;
46-
rs->begin = 0;
46+
rs->flags &= ~RATELIMIT_INITIALIZED;
4747
rs->printed = 0;
4848
ratelimit_state_reset_miss(rs);
4949
raw_spin_unlock_irqrestore(&rs->lock, flags);

include/linux/ratelimit_types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
/* issue num suppressed message on exit */
1313
#define RATELIMIT_MSG_ON_RELEASE BIT(0)
14+
#define RATELIMIT_INITIALIZED BIT(1)
1415

1516
struct ratelimit_state {
1617
raw_spinlock_t lock; /* protect the state */

lib/ratelimit.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ int ___ratelimit(struct ratelimit_state *rs, const char *func)
4949
return 0;
5050
}
5151

52-
if (!rs->begin)
52+
if (!(rs->flags & RATELIMIT_INITIALIZED)) {
5353
rs->begin = jiffies;
54+
rs->flags |= RATELIMIT_INITIALIZED;
55+
}
5456

5557
if (time_is_before_jiffies(rs->begin + interval)) {
5658
int m = ratelimit_state_reset_miss(rs);

0 commit comments

Comments
 (0)