tcp: tsq: avoid using a tasklet if possible
Subsystems (3)
PATCH 1/1 tcp: tsq: avoid using a tasklet if possible
Instead of always defer TCP Small Queue handling to a tasklet we can try to lock the socket and immediately call tcp_tsq_handler()
In my experiments on a 100Gbit NIC with FQ qdisc, number of times tcp_tasklet_func() is fired per second goes from ~70,000 to ~1,000.
This reduces number of ksoftirqd wakeups and thus extra cpu costs and latencies.
--- net/ipv4/tcp_output.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)
net/ipv4/tcp_output.c +14 -2
@@ -1023,13 +1023,18 @@ static void tcp_tsq_write(struct sock *sk) } } -static void tcp_tsq_handler (struct sock *sk) +static void tcp_tsq_handler_locked (struct sock *sk) { -bh_lock_sock (sk);if (!sock_owned_by_user (sk))tcp_tsq_write (sk);else if (!test_and_set_bit (TCP_TSQ_DEFERRED , &sk->sk_tsq_flags))sock_hold (sk); +} + +static void tcp_tsq_handler (struct sock *sk) +{ +bh_lock_sock (sk); +tcp_tsq_handler_locked (sk);bh_unlock_sock (sk); }/*
@@ -1165,6 +1170,13 @@ void tcp_wfree(struct sk_buff *skb) nval = (oval & ~TSQF_THROTTLED ) |TSQF_QUEUED ; }while (!try_cmpxchg (&sk->sk_tsq_flags, &oval, nval)); +/* attempt to grab socket lock. */ +if (spin_trylock_bh (&sk->sk_lock.slock)) { +clear_bit (TSQ_QUEUED , &sk->sk_tsq_flags); +tcp_tsq_handler_locked (sk); +spin_unlock_bh (&sk->sk_lock.slock); + goto out; + }/* queue this socket to tasklet queue */ local_irq_save (flags); tsq =this_cpu_ptr (&tsq_tasklet);
Signature · 1 line
2.40.0.348.gf938b09366-goog
Jakub Kicinski
maintainerQuoted 8 lines · On Thu, 30 Mar 2023 19:25:34 +0000 Eric Dumazet wrote:
Instead of always defer TCP Small Queue handling to a tasklet we can try to lock the socket and immediately call
tcp_tsq_handler()In my experiments on a 100Gbit NIC with FQ qdisc, number of times
tcp_tasklet_func()is fired per second goes from ~70,000 to ~1,000.This reduces number of ksoftirqd wakeups and thus extra cpu costs and latencies.
Oh, you posted already. LMK what you think about other locks, I reckon we should apply this patch... just maybe around -rc1 to give ourselves plenty of time to find the broken drivers?
Quoted 8 lines · Jakub Kicinski
Quoted 6 lines · On Thu, 30 Mar 2023 19:25:34 +0000 Eric Dumazet wrote:
Instead of always defer TCP Small Queue handling to a tasklet we can try to lock the socket and immediately call
tcp_tsq_handler()In my experiments on a 100Gbit NIC with FQ qdisc, number of times
tcp_tasklet_func()is fired per second goes from ~70,000 to ~1,000.
Quoted 6 lines
Quoted 2 lines
This reduces number of ksoftirqd wakeups and thus extra cpu costs and latencies.
Oh, you posted already. LMK what you think about other locks, I reckon we should apply this patch... just maybe around -rc1 to give ourselves plenty of time to find the broken drivers?
Agreed, this can wait.
I will post a v2 RFC with the additional logic, then I will audit napi_consume_skb() callers.
(BTW this logic removes one indirect call to tcp_wfree())
Thanks
include/net/tcp.h +0 -0
100644
include/net/tcp.h +0 -0
@@ -339,6 +339,7 @@ int tcp_send_mss(struct sock *sk, int *size_goal,
int flags);
void tcp_push(struct sock *sk, int flags, int mss_now, int nonagle,
int size_goal);
void tcp_release_cb(struct sock *sk);
+void __tcp_wfree(struct sk_buff *skb, bool trylock);
void tcp_wfree(struct sk_buff *skb);
void tcp_write_timer_handler(struct sock *sk); void tcp_delack_timer_handler(struct sock *sk);
net/core/skbuff.c +0 -0
100644
net/core/skbuff.c +7 -0
@@ -1242,6 +1242,9 @@ void napi_skb_free_stolen_head(struct sk_buff *skb)napi_skb_cache_put (skb); } +/* +* Note: callers must not lock the TX queue. +*/ void napi_consume_skb (struct sk_buff *skb,int budget) {/* Zero budget indicate non-NAPI context called us, like netpoll */
@@ -1260,6 +1263,10 @@ void napi_consume_skb(struct sk_buff *skb, int budget)/* if SKB is a clone, don't handle this case */ if (skb->fclone !=SKB_FCLONE_UNAVAILABLE ) { +if (skb->destructor == tcp_wfree) { +__tcp_wfree (skb, true); + skb->destructor =NULL ; + }__kfree_skb (skb);return ; }
net/ipv4/tcp_output.c +0 -0
100644
net/ipv4/tcp_output.c +7 -2
@@ -1139,7 +1139,7 @@ void __init tcp_tasklet_init(void)* We can't xmit new skbs from this context, as we might already * hold qdisc lock. */ -void tcp_wfree (struct sk_buff *skb) +void __tcp_wfree (struct sk_buff *skb,bool trylock) {struct sock *sk = skb->sk;struct tcp_sock *tp =tcp_sk (sk);
@@ -1171,7 +1171,7 @@ void tcp_wfree(struct sk_buff *skb) }while (!try_cmpxchg (&sk->sk_tsq_flags, &oval, nval));/* attempt to grab socket lock. */ -if (spin_trylock_bh (&sk->sk_lock.slock)) { +if (trylock &&spin_trylock_bh (&sk->sk_lock.slock)) {clear_bit (TSQ_QUEUED , &sk->sk_tsq_flags);tcp_tsq_handler_locked (sk);spin_unlock_bh (&sk->sk_lock.slock);
@@ -1190,6 +1190,11 @@ void tcp_wfree(struct sk_buff *skb)sk_free (sk); } +void tcp_wfree (struct sk_buff *skb) +{ +__tcp_wfree (skb, false); +} +/* Note: Called under soft irq. * We can call TCP stack right away, unless socket is owned by user. */