Skip to content

Commit 87b8466

Browse files
edumazetgregkh
authored andcommitted
tcp: annotate data-races around tp->keepalive_time
[ Upstream commit 4164245 ] do_tcp_getsockopt() reads tp->keepalive_time while another cpu might change its value. Fixes: 1da177e ("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]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 2c84a3d commit 87b8466

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
@@ -1519,9 +1519,12 @@ static inline int keepalive_intvl_when(const struct tcp_sock *tp)
15191519
static inline int keepalive_time_when(const struct tcp_sock *tp)
15201520
{
15211521
struct net *net = sock_net((struct sock *)tp);
1522+
int val;
15221523

1523-
return tp->keepalive_time ? :
1524-
READ_ONCE(net->ipv4.sysctl_tcp_keepalive_time);
1524+
/* Paired with WRITE_ONCE() in tcp_sock_set_keepidle_locked() */
1525+
val = READ_ONCE(tp->keepalive_time);
1526+
1527+
return val ? : READ_ONCE(net->ipv4.sysctl_tcp_keepalive_time);
15251528
}
15261529

15271530
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
@@ -3418,7 +3418,8 @@ int tcp_sock_set_keepidle_locked(struct sock *sk, int val)
34183418
if (val < 1 || val > MAX_TCP_KEEPIDLE)
34193419
return -EINVAL;
34203420

3421-
tp->keepalive_time = val * HZ;
3421+
/* Paired with WRITE_ONCE() in keepalive_time_when() */
3422+
WRITE_ONCE(tp->keepalive_time, val * HZ);
34223423
if (sock_flag(sk, SOCK_KEEPOPEN) &&
34233424
!((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))) {
34243425
u32 elapsed = keepalive_time_elapsed(tp);

0 commit comments

Comments
 (0)