Skip to content

Commit 4164245

Browse files
edumazetkuba-moo
authored andcommitted
tcp: annotate data-races around tp->keepalive_time
do_tcp_getsockopt() reads tp->keepalive_time while another cpu might change its value. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: Eric Dumazet <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent dd23c9f commit 4164245

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

include/net/tcp.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,9 +1517,12 @@ static inline int keepalive_intvl_when(const struct tcp_sock *tp)
15171517
static inline int keepalive_time_when(const struct tcp_sock *tp)
15181518
{
15191519
struct net *net = sock_net((struct sock *)tp);
1520+
int val;
15201521

1521-
return tp->keepalive_time ? :
1522-
READ_ONCE(net->ipv4.sysctl_tcp_keepalive_time);
1522+
/* Paired with WRITE_ONCE() in tcp_sock_set_keepidle_locked() */
1523+
val = READ_ONCE(tp->keepalive_time);
1524+
1525+
return val ? : READ_ONCE(net->ipv4.sysctl_tcp_keepalive_time);
15231526
}
15241527

15251528
static inline int keepalive_probes(const struct tcp_sock *tp)

net/ipv4/tcp.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3312,7 +3312,8 @@ int tcp_sock_set_keepidle_locked(struct sock *sk, int val)
33123312
if (val < 1 || val > MAX_TCP_KEEPIDLE)
33133313
return -EINVAL;
33143314

3315-
tp->keepalive_time = val * HZ;
3315+
/* Paired with WRITE_ONCE() in keepalive_time_when() */
3316+
WRITE_ONCE(tp->keepalive_time, val * HZ);
33163317
if (sock_flag(sk, SOCK_KEEPOPEN) &&
33173318
!((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))) {
33183319
u32 elapsed = keepalive_time_elapsed(tp);

0 commit comments

Comments
 (0)