Skip to content

Commit 123a1d9

Browse files
committed
ratelimit: Avoid atomic decrement if already rate-limited
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@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 21ac6e5 commit 123a1d9

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

lib/ratelimit.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@ int ___ratelimit(struct ratelimit_state *rs, const char *func)
6565
unsigned int rs_flags = READ_ONCE(rs->flags);
6666

6767
if (rs_flags & RATELIMIT_INITIALIZED && burst) {
68-
int n_left;
68+
int n_left = atomic_read(&rs->rs_n_left);
6969

70+
if (n_left <= 0)
71+
return 0;
7072
n_left = atomic_dec_return(&rs->rs_n_left);
7173
if (n_left >= 0)
7274
return 1;

0 commit comments

Comments
 (0)