Skip to content

Commit 5ecf9d4

Browse files
edumazetkuba-moo
authored andcommitted
tcp: annotate data-races around tp->keepalive_intvl
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]>
1 parent 4164245 commit 5ecf9d4

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
@@ -1509,9 +1509,14 @@ void tcp_leave_memory_pressure(struct sock *sk);
15091509
static inline int keepalive_intvl_when(const struct tcp_sock *tp)
15101510
{
15111511
struct net *net = sock_net((struct sock *)tp);
1512+
int val;
1513+
1514+
/* Paired with WRITE_ONCE() in tcp_sock_set_keepintvl()
1515+
* and do_tcp_setsockopt().
1516+
*/
1517+
val = READ_ONCE(tp->keepalive_intvl);
15121518

1513-
return tp->keepalive_intvl ? :
1514-
READ_ONCE(net->ipv4.sysctl_tcp_keepalive_intvl);
1519+
return val ? : READ_ONCE(net->ipv4.sysctl_tcp_keepalive_intvl);
15151520
}
15161521

15171522
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
@@ -3345,7 +3345,7 @@ int tcp_sock_set_keepintvl(struct sock *sk, int val)
33453345
return -EINVAL;
33463346

33473347
lock_sock(sk);
3348-
tcp_sk(sk)->keepalive_intvl = val * HZ;
3348+
WRITE_ONCE(tcp_sk(sk)->keepalive_intvl, val * HZ);
33493349
release_sock(sk);
33503350
return 0;
33513351
}
@@ -3559,7 +3559,7 @@ int do_tcp_setsockopt(struct sock *sk, int level, int optname,
35593559
if (val < 1 || val > MAX_TCP_KEEPINTVL)
35603560
err = -EINVAL;
35613561
else
3562-
tp->keepalive_intvl = val * HZ;
3562+
WRITE_ONCE(tp->keepalive_intvl, val * HZ);
35633563
break;
35643564
case TCP_KEEPCNT:
35653565
if (val < 1 || val > MAX_TCP_KEEPCNT)

0 commit comments

Comments
 (0)