Skip to content

Commit cf8cfa8

Browse files
pmladekpaulmckrcu
authored andcommitted
ratelimit: Reduce ___ratelimit() false-positive rate limiting
Retain the locked design, but check rate-limiting even when the lock could not be acquired. Link: https://lore.kernel.org/all/[email protected]/ Link: https://lore.kernel.org/all/fbe93a52-365e-47fe-93a4-44a44547d601@paulmck-laptop/ Link: https://lore.kernel.org/all/[email protected]/ Signed-off-by: Petr Mladek <[email protected]> Signed-off-by: Paul E. McKenney <[email protected]> Cc: 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 e64a348 commit cf8cfa8

File tree

3 files changed

+40
-15
lines changed

3 files changed

+40
-15
lines changed

include/linux/ratelimit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ static inline void ratelimit_state_reset_interval(struct ratelimit_state *rs, in
4444
raw_spin_lock_irqsave(&rs->lock, flags);
4545
rs->interval = interval_init;
4646
rs->flags &= ~RATELIMIT_INITIALIZED;
47-
rs->printed = 0;
47+
atomic_set(&rs->rs_n_left, rs->burst);
4848
ratelimit_state_reset_miss(rs);
4949
raw_spin_unlock_irqrestore(&rs->lock, flags);
5050
}

include/linux/ratelimit_types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ struct ratelimit_state {
1818

1919
int interval;
2020
int burst;
21-
int printed;
21+
atomic_t rs_n_left;
2222
atomic_t missed;
2323
unsigned int flags;
2424
unsigned long begin;

lib/ratelimit.c

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,40 +39,65 @@ int ___ratelimit(struct ratelimit_state *rs, const char *func)
3939
return 1;
4040

4141
/*
42-
* If we contend on this state's lock then almost
43-
* by definition we are too busy to print a message,
44-
* in addition to the one that will be printed by
45-
* the entity that is holding the lock already:
42+
* If we contend on this state's lock then just check if
43+
* the current burst is used or not. It might cause
44+
* false positive when we are past the interval and
45+
* the current lock owner is just about to reset it.
4646
*/
4747
if (!raw_spin_trylock_irqsave(&rs->lock, flags)) {
48+
unsigned int rs_flags = READ_ONCE(rs->flags);
49+
50+
if (rs_flags & RATELIMIT_INITIALIZED && burst) {
51+
int n_left;
52+
53+
n_left = atomic_dec_return(&rs->rs_n_left);
54+
if (n_left >= 0)
55+
return 1;
56+
}
57+
4858
ratelimit_state_inc_miss(rs);
4959
return 0;
5060
}
5161

5262
if (!(rs->flags & RATELIMIT_INITIALIZED)) {
5363
rs->begin = jiffies;
5464
rs->flags |= RATELIMIT_INITIALIZED;
65+
atomic_set(&rs->rs_n_left, rs->burst);
5566
}
5667

5768
if (time_is_before_jiffies(rs->begin + interval)) {
58-
int m = ratelimit_state_reset_miss(rs);
69+
int m;
70+
71+
/*
72+
* Reset rs_n_left ASAP to reduce false positives
73+
* in parallel calls, see above.
74+
*/
75+
atomic_set(&rs->rs_n_left, rs->burst);
76+
rs->begin = jiffies;
5977

78+
m = ratelimit_state_reset_miss(rs);
6079
if (m) {
6180
if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE)) {
6281
printk_deferred(KERN_WARNING
6382
"%s: %d callbacks suppressed\n", func, m);
6483
}
6584
}
66-
rs->begin = jiffies;
67-
rs->printed = 0;
6885
}
69-
if (burst && burst > rs->printed) {
70-
rs->printed++;
71-
ret = 1;
72-
} else {
73-
ratelimit_state_inc_miss(rs);
74-
ret = 0;
86+
if (burst) {
87+
int n_left;
88+
89+
/* The burst might have been taken by a parallel call. */
90+
n_left = atomic_dec_return(&rs->rs_n_left);
91+
if (n_left >= 0) {
92+
ret = 1;
93+
goto unlock_ret;
94+
}
7595
}
96+
97+
ratelimit_state_inc_miss(rs);
98+
ret = 0;
99+
100+
unlock_ret:
76101
raw_spin_unlock_irqrestore(&rs->lock, flags);
77102

78103
return ret;

0 commit comments

Comments
 (0)