Skip to content

Commit 6e5e1de

Browse files
edumazetkuba-moo
authored andcommitted
tcp: annotate data-races around tp->keepalive_probes
do_tcp_getsockopt() reads tp->keepalive_probes 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 5ecf9d4 commit 6e5e1de

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

include/net/tcp.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,9 +1533,14 @@ static inline int keepalive_time_when(const struct tcp_sock *tp)
15331533
static inline int keepalive_probes(const struct tcp_sock *tp)
15341534
{
15351535
struct net *net = sock_net((struct sock *)tp);
1536+
int val;
1537+
1538+
/* Paired with WRITE_ONCE() in tcp_sock_set_keepcnt()
1539+
* and do_tcp_setsockopt().
1540+
*/
1541+
val = READ_ONCE(tp->keepalive_probes);
15361542

1537-
return tp->keepalive_probes ? :
1538-
READ_ONCE(net->ipv4.sysctl_tcp_keepalive_probes);
1543+
return val ? : READ_ONCE(net->ipv4.sysctl_tcp_keepalive_probes);
15391544
}
15401545

15411546
static inline u32 keepalive_time_elapsed(const struct tcp_sock *tp)

net/ipv4/tcp.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3357,7 +3357,8 @@ int tcp_sock_set_keepcnt(struct sock *sk, int val)
33573357
return -EINVAL;
33583358

33593359
lock_sock(sk);
3360-
tcp_sk(sk)->keepalive_probes = val;
3360+
/* Paired with READ_ONCE() in keepalive_probes() */
3361+
WRITE_ONCE(tcp_sk(sk)->keepalive_probes, val);
33613362
release_sock(sk);
33623363
return 0;
33633364
}
@@ -3565,7 +3566,7 @@ int do_tcp_setsockopt(struct sock *sk, int level, int optname,
35653566
if (val < 1 || val > MAX_TCP_KEEPCNT)
35663567
err = -EINVAL;
35673568
else
3568-
tp->keepalive_probes = val;
3569+
WRITE_ONCE(tp->keepalive_probes, val);
35693570
break;
35703571
case TCP_SYNCNT:
35713572
if (val < 1 || val > MAX_TCP_SYNCNT)

0 commit comments

Comments
 (0)