Skip to content

Commit 81df4fa

Browse files
edumazetkuba-moo
authored andcommitted
tcp: add a fast path in tcp_delack_timer()
delack timer is not stopped from inet_csk_clear_xmit_timer() because we do not define INET_CSK_CLEAR_TIMERS. This is a conscious choice : inet_csk_clear_xmit_timer() is often called from another cpu. Calling del_timer() would cause false sharing and lock contention. This means that very often, tcp_delack_timer() is called at the timer expiration, while there is no ACK to transmit. This can be detected very early, avoiding the socket spinlock. Notes: - test about tp->compressed_ack is racy, but in the unlikely case there is a race, the dedicated compressed_ack_timer hrtimer would close it. - Even if the fast path is not taken, reading icsk->icsk_ack.pending and tp->compressed_ack before acquiring the socket spinlock reduces acquisition time and chances of contention. Signed-off-by: Eric Dumazet <edumazet@google.com> Link: https://patch.msgid.link/20241002173042.917928-4-edumazet@google.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent 3b78429 commit 81df4fa

File tree

5 files changed

+18
-6
lines changed

5 files changed

+18
-6
lines changed

include/net/inet_connection_sock.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ static inline void inet_csk_clear_xmit_timer(struct sock *sk, const int what)
202202
sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
203203
#endif
204204
} else if (what == ICSK_TIME_DACK) {
205-
icsk->icsk_ack.pending = 0;
205+
smp_store_release(&icsk->icsk_ack.pending, 0);
206206
icsk->icsk_ack.retry = 0;
207207
#ifdef INET_CSK_CLEAR_TIMERS
208208
sk_stop_timer(sk, &icsk->icsk_delack_timer);
@@ -233,7 +233,8 @@ static inline void inet_csk_reset_xmit_timer(struct sock *sk, const int 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) {
236-
icsk->icsk_ack.pending |= ICSK_ACK_TIMER;
236+
smp_store_release(&icsk->icsk_ack.pending,
237+
icsk->icsk_ack.pending | ICSK_ACK_TIMER);
237238
icsk->icsk_ack.timeout = jiffies + when;
238239
sk_reset_timer(sk, &icsk->icsk_delack_timer, icsk->icsk_ack.timeout);
239240
} else {

net/ipv4/inet_connection_sock.c

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

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

781781
sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
782782
sk_stop_timer(sk, &icsk->icsk_delack_timer);
@@ -792,7 +792,7 @@ void inet_csk_clear_xmit_timers_sync(struct sock *sk)
792792
sock_not_owned_by_me(sk);
793793

794794
smp_store_release(&icsk->icsk_pending, 0);
795-
icsk->icsk_ack.pending = 0;
795+
smp_store_release(&icsk->icsk_ack.pending, 0);
796796

797797
sk_stop_timer_sync(sk, &icsk->icsk_retransmit_timer);
798798
sk_stop_timer_sync(sk, &icsk->icsk_delack_timer);

net/ipv4/tcp_output.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4224,7 +4224,8 @@ void tcp_send_delayed_ack(struct sock *sk)
42244224
if (!time_before(timeout, icsk->icsk_ack.timeout))
42254225
timeout = icsk->icsk_ack.timeout;
42264226
}
4227-
icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
4227+
smp_store_release(&icsk->icsk_ack.pending,
4228+
icsk->icsk_ack.pending | ICSK_ACK_SCHED | ICSK_ACK_TIMER);
42284229
icsk->icsk_ack.timeout = timeout;
42294230
sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout);
42304231
}

net/ipv4/tcp_timer.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,14 @@ static void tcp_delack_timer(struct timer_list *t)
361361
from_timer(icsk, t, icsk_delack_timer);
362362
struct sock *sk = &icsk->icsk_inet.sk;
363363

364+
/* Avoid taking socket spinlock if there is no ACK to send.
365+
* The compressed_ack check is racy, but a separate hrtimer
366+
* will take care of it eventually.
367+
*/
368+
if (!(smp_load_acquire(&icsk->icsk_ack.pending) & ICSK_ACK_TIMER) &&
369+
!READ_ONCE(tcp_sk(sk)->compressed_ack))
370+
goto out;
371+
364372
bh_lock_sock(sk);
365373
if (!sock_owned_by_user(sk)) {
366374
tcp_delack_timer_handler(sk);
@@ -371,6 +379,7 @@ static void tcp_delack_timer(struct timer_list *t)
371379
sock_hold(sk);
372380
}
373381
bh_unlock_sock(sk);
382+
out:
374383
sock_put(sk);
375384
}
376385

net/mptcp/protocol.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3504,7 +3504,8 @@ static void schedule_3rdack_retransmission(struct sock *ssk)
35043504
timeout += jiffies;
35053505

35063506
WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER);
3507-
icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
3507+
smp_store_release(&icsk->icsk_ack.pending,
3508+
icsk->icsk_ack.pending | ICSK_ACK_SCHED | ICSK_ACK_TIMER);
35083509
icsk->icsk_ack.timeout = timeout;
35093510
sk_reset_timer(ssk, &icsk->icsk_delack_timer, timeout);
35103511
}

0 commit comments

Comments
 (0)