Skip to content

Commit 161b069

Browse files
edumazetgregkh
authored andcommitted
tcp: annotate data-races around tp->keepalive_intvl
[ Upstream commit 5ecf9d4 ] do_tcp_getsockopt() reads tp->keepalive_intvl 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 87b8466 commit 161b069

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

include/net/tcp.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,9 +1511,14 @@ void tcp_leave_memory_pressure(struct sock *sk);
15111511
static inline int keepalive_intvl_when(const struct tcp_sock *tp)
15121512
{
15131513
struct net *net = sock_net((struct sock *)tp);
1514+
int val;
1515+
1516+
/* Paired with WRITE_ONCE() in tcp_sock_set_keepintvl()
1517+
* and do_tcp_setsockopt().
1518+
*/
1519+
val = READ_ONCE(tp->keepalive_intvl);
15141520

1515-
return tp->keepalive_intvl ? :
1516-
READ_ONCE(net->ipv4.sysctl_tcp_keepalive_intvl);
1521+
return val ? : READ_ONCE(net->ipv4.sysctl_tcp_keepalive_intvl);
15171522
}
15181523

15191524
static inline int keepalive_time_when(const struct tcp_sock *tp)

net/ipv4/tcp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3451,7 +3451,7 @@ int tcp_sock_set_keepintvl(struct sock *sk, int val)
34513451
return -EINVAL;
34523452

34533453
lock_sock(sk);
3454-
tcp_sk(sk)->keepalive_intvl = val * HZ;
3454+
WRITE_ONCE(tcp_sk(sk)->keepalive_intvl, val * HZ);
34553455
release_sock(sk);
34563456
return 0;
34573457
}
@@ -3665,7 +3665,7 @@ int do_tcp_setsockopt(struct sock *sk, int level, int optname,
36653665
if (val < 1 || val > MAX_TCP_KEEPINTVL)
36663666
err = -EINVAL;
36673667
else
3668-
tp->keepalive_intvl = val * HZ;
3668+
WRITE_ONCE(tp->keepalive_intvl, val * HZ);
36693669
break;
36703670
case TCP_KEEPCNT:
36713671
if (val < 1 || val > MAX_TCP_KEEPCNT)

0 commit comments

Comments
 (0)