Skip to content

Commit 5a9071a

Browse files
edumazetkuba-moo
authored andcommitted
tcp: annotate data-races around icsk->icsk_pending
icsk->icsk_pending can be read locklessly already. Following patch in the series will add another lockless read. Add smp_load_acquire() and smp_store_release() annotations because following patch will add a test in tcp_write_timer(), and READ_ONCE()/WRITE_ONCE() alone would possibly lead to races. Signed-off-by: Eric Dumazet <edumazet@google.com> Link: https://patch.msgid.link/20241002173042.917928-2-edumazet@google.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent d454184 commit 5a9071a

File tree

7 files changed

+28
-20
lines changed

7 files changed

+28
-20
lines changed

include/net/inet_connection_sock.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ static inline void inet_csk_clear_xmit_timer(struct sock *sk, const int what)
197197
struct inet_connection_sock *icsk = inet_csk(sk);
198198

199199
if (what == ICSK_TIME_RETRANS || what == ICSK_TIME_PROBE0) {
200-
icsk->icsk_pending = 0;
200+
smp_store_release(&icsk->icsk_pending, 0);
201201
#ifdef INET_CSK_CLEAR_TIMERS
202202
sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
203203
#endif
@@ -229,7 +229,7 @@ static inline void inet_csk_reset_xmit_timer(struct sock *sk, const int what,
229229

230230
if (what == ICSK_TIME_RETRANS || what == ICSK_TIME_PROBE0 ||
231231
what == ICSK_TIME_LOSS_PROBE || what == ICSK_TIME_REO_TIMEOUT) {
232-
icsk->icsk_pending = what;
232+
smp_store_release(&icsk->icsk_pending, what);
233233
icsk->icsk_timeout = jiffies + when;
234234
sk_reset_timer(sk, &icsk->icsk_retransmit_timer, icsk->icsk_timeout);
235235
} else if (what == ICSK_TIME_DACK) {

net/ipv4/inet_connection_sock.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,8 @@ void inet_csk_clear_xmit_timers(struct sock *sk)
775775
{
776776
struct inet_connection_sock *icsk = inet_csk(sk);
777777

778-
icsk->icsk_pending = icsk->icsk_ack.pending = 0;
778+
smp_store_release(&icsk->icsk_pending, 0);
779+
icsk->icsk_ack.pending = 0;
779780

780781
sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
781782
sk_stop_timer(sk, &icsk->icsk_delack_timer);
@@ -790,7 +791,8 @@ void inet_csk_clear_xmit_timers_sync(struct sock *sk)
790791
/* ongoing timer handlers need to acquire socket lock. */
791792
sock_not_owned_by_me(sk);
792793

793-
icsk->icsk_pending = icsk->icsk_ack.pending = 0;
794+
smp_store_release(&icsk->icsk_pending, 0);
795+
icsk->icsk_ack.pending = 0;
794796

795797
sk_stop_timer_sync(sk, &icsk->icsk_retransmit_timer);
796798
sk_stop_timer_sync(sk, &icsk->icsk_delack_timer);

net/ipv4/inet_diag.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
247247
struct nlmsghdr *nlh;
248248
struct nlattr *attr;
249249
void *info = NULL;
250+
u8 icsk_pending;
250251
int protocol;
251252

252253
cb_data = cb->data;
@@ -307,14 +308,15 @@ int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
307308
goto out;
308309
}
309310

310-
if (icsk->icsk_pending == ICSK_TIME_RETRANS ||
311-
icsk->icsk_pending == ICSK_TIME_REO_TIMEOUT ||
312-
icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
311+
icsk_pending = smp_load_acquire(&icsk->icsk_pending);
312+
if (icsk_pending == ICSK_TIME_RETRANS ||
313+
icsk_pending == ICSK_TIME_REO_TIMEOUT ||
314+
icsk_pending == ICSK_TIME_LOSS_PROBE) {
313315
r->idiag_timer = 1;
314316
r->idiag_retrans = icsk->icsk_retransmits;
315317
r->idiag_expires =
316318
jiffies_delta_to_msecs(icsk->icsk_timeout - jiffies);
317-
} else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
319+
} else if (icsk_pending == ICSK_TIME_PROBE0) {
318320
r->idiag_timer = 4;
319321
r->idiag_retrans = icsk->icsk_probes_out;
320322
r->idiag_expires =

net/ipv4/tcp_ipv4.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2900,15 +2900,17 @@ static void get_tcp4_sock(struct sock *sk, struct seq_file *f, int i)
29002900
__be32 src = inet->inet_rcv_saddr;
29012901
__u16 destp = ntohs(inet->inet_dport);
29022902
__u16 srcp = ntohs(inet->inet_sport);
2903+
u8 icsk_pending;
29032904
int rx_queue;
29042905
int state;
29052906

2906-
if (icsk->icsk_pending == ICSK_TIME_RETRANS ||
2907-
icsk->icsk_pending == ICSK_TIME_REO_TIMEOUT ||
2908-
icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
2907+
icsk_pending = smp_load_acquire(&icsk->icsk_pending);
2908+
if (icsk_pending == ICSK_TIME_RETRANS ||
2909+
icsk_pending == ICSK_TIME_REO_TIMEOUT ||
2910+
icsk_pending == ICSK_TIME_LOSS_PROBE) {
29092911
timer_active = 1;
29102912
timer_expires = icsk->icsk_timeout;
2911-
} else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
2913+
} else if (icsk_pending == ICSK_TIME_PROBE0) {
29122914
timer_active = 4;
29132915
timer_expires = icsk->icsk_timeout;
29142916
} else if (timer_pending(&sk->sk_timer)) {

net/ipv4/tcp_output.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2960,7 +2960,7 @@ void tcp_send_loss_probe(struct sock *sk)
29602960
WARN_ONCE(tp->packets_out,
29612961
"invalid inflight: %u state %u cwnd %u mss %d\n",
29622962
tp->packets_out, sk->sk_state, tcp_snd_cwnd(tp), mss);
2963-
inet_csk(sk)->icsk_pending = 0;
2963+
smp_store_release(&inet_csk(sk)->icsk_pending, 0);
29642964
return;
29652965
}
29662966

@@ -2993,7 +2993,7 @@ void tcp_send_loss_probe(struct sock *sk)
29932993

29942994
NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPLOSSPROBES);
29952995
/* Reset s.t. tcp_rearm_rto will restart timer from now */
2996-
inet_csk(sk)->icsk_pending = 0;
2996+
smp_store_release(&inet_csk(sk)->icsk_pending, 0);
29972997
rearm_timer:
29982998
tcp_rearm_rto(sk);
29992999
}

net/ipv4/tcp_timer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -701,11 +701,11 @@ void tcp_write_timer_handler(struct sock *sk)
701701
tcp_send_loss_probe(sk);
702702
break;
703703
case ICSK_TIME_RETRANS:
704-
icsk->icsk_pending = 0;
704+
smp_store_release(&icsk->icsk_pending, 0);
705705
tcp_retransmit_timer(sk);
706706
break;
707707
case ICSK_TIME_PROBE0:
708-
icsk->icsk_pending = 0;
708+
smp_store_release(&icsk->icsk_pending, 0);
709709
tcp_probe_timer(sk);
710710
break;
711711
}

net/ipv6/tcp_ipv6.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2177,6 +2177,7 @@ static void get_tcp6_sock(struct seq_file *seq, struct sock *sp, int i)
21772177
const struct tcp_sock *tp = tcp_sk(sp);
21782178
const struct inet_connection_sock *icsk = inet_csk(sp);
21792179
const struct fastopen_queue *fastopenq = &icsk->icsk_accept_queue.fastopenq;
2180+
u8 icsk_pending;
21802181
int rx_queue;
21812182
int state;
21822183

@@ -2185,12 +2186,13 @@ static void get_tcp6_sock(struct seq_file *seq, struct sock *sp, int i)
21852186
destp = ntohs(inet->inet_dport);
21862187
srcp = ntohs(inet->inet_sport);
21872188

2188-
if (icsk->icsk_pending == ICSK_TIME_RETRANS ||
2189-
icsk->icsk_pending == ICSK_TIME_REO_TIMEOUT ||
2190-
icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
2189+
icsk_pending = smp_load_acquire(&icsk->icsk_pending);
2190+
if (icsk_pending == ICSK_TIME_RETRANS ||
2191+
icsk_pending == ICSK_TIME_REO_TIMEOUT ||
2192+
icsk_pending == ICSK_TIME_LOSS_PROBE) {
21912193
timer_active = 1;
21922194
timer_expires = icsk->icsk_timeout;
2193-
} else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
2195+
} else if (icsk_pending == ICSK_TIME_PROBE0) {
21942196
timer_active = 4;
21952197
timer_expires = icsk->icsk_timeout;
21962198
} else if (timer_pending(&sp->sk_timer)) {

0 commit comments

Comments
 (0)