Skip to content

Commit fec8ca6

Browse files
author
Guillaume Nault
committed
ratelimit: Fix data-races in ___ratelimit().
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2160073 Upstream Status: linux.git commit 6bae8ce Author: Kuniyuki Iwashima <kuniyu@amazon.com> Date: Tue Aug 23 10:46:48 2022 -0700 ratelimit: Fix data-races in ___ratelimit(). While reading rs->interval and rs->burst, they can be changed concurrently via sysctl (e.g. net_ratelimit_state). Thus, we need to add READ_ONCE() to their readers. Fixes: 1da177e ("Linux-2.6.12-rc2") Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com> Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Guillaume Nault <gnault@redhat.com>
1 parent 4437c76 commit fec8ca6

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

lib/ratelimit.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,16 @@
2626
*/
2727
int ___ratelimit(struct ratelimit_state *rs, const char *func)
2828
{
29+
/* Paired with WRITE_ONCE() in .proc_handler().
30+
* Changing two values seperately could be inconsistent
31+
* and some message could be lost. (See: net_ratelimit_state).
32+
*/
33+
int interval = READ_ONCE(rs->interval);
34+
int burst = READ_ONCE(rs->burst);
2935
unsigned long flags;
3036
int ret;
3137

32-
if (!rs->interval)
38+
if (!interval)
3339
return 1;
3440

3541
/*
@@ -44,7 +50,7 @@ int ___ratelimit(struct ratelimit_state *rs, const char *func)
4450
if (!rs->begin)
4551
rs->begin = jiffies;
4652

47-
if (time_is_before_jiffies(rs->begin + rs->interval)) {
53+
if (time_is_before_jiffies(rs->begin + interval)) {
4854
if (rs->missed) {
4955
if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE)) {
5056
printk_deferred(KERN_WARNING
@@ -56,7 +62,7 @@ int ___ratelimit(struct ratelimit_state *rs, const char *func)
5662
rs->begin = jiffies;
5763
rs->printed = 0;
5864
}
59-
if (rs->burst && rs->burst > rs->printed) {
65+
if (burst && burst > rs->printed) {
6066
rs->printed++;
6167
ret = 1;
6268
} else {

0 commit comments

Comments
 (0)