Skip to content

Commit 949ad62

Browse files
edumazetkuba-moo
authored andcommitted
tcp_metrics: annotate data-races around tm->tcpm_stamp
tm->tcpm_stamp can be read or written locklessly. Add needed READ_ONCE()/WRITE_ONCE() to document this. Also constify tcpm_check_stamp() dst argument. Fixes: 51c5d0c ("tcp: Maintain dynamic metrics in local cache.") Signed-off-by: Eric Dumazet <edumazet@google.com> Reviewed-by: David Ahern <dsahern@kernel.org> Reviewed-by: Kuniyuki Iwashima <kuniyu@amazon.com> Link: https://lore.kernel.org/r/20230802131500.1478140-3-edumazet@google.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent e663809 commit 949ad62

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

net/ipv4/tcp_metrics.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ static void tcpm_suck_dst(struct tcp_metrics_block *tm,
9797
u32 msval;
9898
u32 val;
9999

100-
tm->tcpm_stamp = jiffies;
100+
WRITE_ONCE(tm->tcpm_stamp, jiffies);
101101

102102
val = 0;
103103
if (dst_metric_locked(dst, RTAX_RTT))
@@ -131,9 +131,15 @@ static void tcpm_suck_dst(struct tcp_metrics_block *tm,
131131

132132
#define TCP_METRICS_TIMEOUT (60 * 60 * HZ)
133133

134-
static void tcpm_check_stamp(struct tcp_metrics_block *tm, struct dst_entry *dst)
134+
static void tcpm_check_stamp(struct tcp_metrics_block *tm,
135+
const struct dst_entry *dst)
135136
{
136-
if (tm && unlikely(time_after(jiffies, tm->tcpm_stamp + TCP_METRICS_TIMEOUT)))
137+
unsigned long limit;
138+
139+
if (!tm)
140+
return;
141+
limit = READ_ONCE(tm->tcpm_stamp) + TCP_METRICS_TIMEOUT;
142+
if (unlikely(time_after(jiffies, limit)))
137143
tcpm_suck_dst(tm, dst, false);
138144
}
139145

@@ -174,7 +180,8 @@ static struct tcp_metrics_block *tcpm_new(struct dst_entry *dst,
174180
oldest = deref_locked(tcp_metrics_hash[hash].chain);
175181
for (tm = deref_locked(oldest->tcpm_next); tm;
176182
tm = deref_locked(tm->tcpm_next)) {
177-
if (time_before(tm->tcpm_stamp, oldest->tcpm_stamp))
183+
if (time_before(READ_ONCE(tm->tcpm_stamp),
184+
READ_ONCE(oldest->tcpm_stamp)))
178185
oldest = tm;
179186
}
180187
tm = oldest;
@@ -434,7 +441,7 @@ void tcp_update_metrics(struct sock *sk)
434441
tp->reordering);
435442
}
436443
}
437-
tm->tcpm_stamp = jiffies;
444+
WRITE_ONCE(tm->tcpm_stamp, jiffies);
438445
out_unlock:
439446
rcu_read_unlock();
440447
}
@@ -647,7 +654,7 @@ static int tcp_metrics_fill_info(struct sk_buff *msg,
647654
}
648655

649656
if (nla_put_msecs(msg, TCP_METRICS_ATTR_AGE,
650-
jiffies - tm->tcpm_stamp,
657+
jiffies - READ_ONCE(tm->tcpm_stamp),
651658
TCP_METRICS_ATTR_PAD) < 0)
652659
goto nla_put_failure;
653660

0 commit comments

Comments
 (0)