tcp: fix zerocopy and notsent_lowat issues
Subsystems (4)
PATCH v2 1/1 tcp: fix zerocopy and notsent_lowat issues
My recent patch had at least three problems :
1) TX zerocopy wants notification when skb is acknowledged, thus we need to call skb_zcopy_clear() if the skb is cached into sk->sk_tx_skb_cache
2) Some applications might expect precise EPOLLOUT notifications, so we need to update sk->sk_wmem_queued and call sk_mem_uncharge() from sk_wmem_free_skb() in all cases. The SOCK_QUEUE_SHRUNK flag must also be set.
3) Reuse of saved skb should have used skb_cloned() instead of simply checking if the fast clone has been freed.
--- include/net/sock.h | 9 +++++---- net/ipv4/tcp.c | 13 +++---------- 2 files changed, 8 insertions(+), 14 deletions(-)
include/net/sock.h +5 -4
@@ -1465,13 +1465,14 @@ static inline void sk_mem_uncharge(struct sock *sk, int size)static inline void sk_wmem_free_skb (struct sock *sk,struct sk_buff *skb) { -if (!sk->sk_tx_skb_cache) { - sk->sk_tx_skb_cache = skb; -return ; - }sock_set_flag (sk,SOCK_QUEUE_SHRUNK ); sk->sk_wmem_queued -= skb->truesize;sk_mem_uncharge (sk, skb->truesize); +if (!sk->sk_tx_skb_cache) { +skb_zcopy_clear (skb, true); + sk->sk_tx_skb_cache = skb; +return ; + }__kfree_skb (skb); }
net/ipv4/tcp.c +3 -10
@@ -865,14 +865,9 @@ struct sk_buff *sk_stream_alloc_skb(struct sock *sk, int size, gfp_t gfp, {struct sk_buff *skb; - skb = sk->sk_tx_skb_cache; -if (skb && !size) { -const struct sk_buff_fclones *fclones; - - fclones = container_of(skb, struct sk_buff_fclones, skb1); -if (refcount_read (&fclones->fclone_ref) ==1 ) { - sk->sk_wmem_queued -= skb->truesize; -sk_mem_uncharge (sk, skb->truesize); +if (likely (!size)) { + skb = sk->sk_tx_skb_cache; +if (skb && !skb_cloned (skb)) { skb->truesize -= skb->data_len; sk->sk_tx_skb_cache =NULL ;pskb_trim (skb,0 );
@@ -2543,8 +2538,6 @@ void tcp_write_queue_purge(struct sock *sk)tcp_rtx_queue_purge (sk); skb = sk->sk_tx_skb_cache;if (skb) { - sk->sk_wmem_queued -= skb->truesize; -sk_mem_uncharge (sk, skb->truesize);__kfree_skb (skb); sk->sk_tx_skb_cache =NULL ; }
Signature · 1 line
2.21.0.392.gf8f6787159e-goog
Quoted 12 lines · Eric Dumazet
My recent patch had at least three problems :
1) TX zerocopy wants notification when skb is acknowledged, thus we need to call
skb_zcopy_clear()if the skb is cached intosk->sk_tx_skb_cache2) Some applications might expect precise EPOLLOUT notifications, so we need to update
sk->sk_wmem_queuedand callsk_mem_uncharge()fromsk_wmem_free_skb()in all cases. TheSOCK_QUEUE_SHRUNKflag must also be set.
Quoted 7 lines
3) Reuse of saved skb should have used
skb_cloned()instead of simply checking if the fast clone has been freed.Fixes 472c2e07eef0 ("tcp: add one skb cache for tx")Signed-off-by Eric Dumazet <[email protected]>Cc Willem de Bruijn <[email protected]>Cc Soheil Hassas Yeganeh <[email protected]>
I can't think of other corner cases. Thanks!
Quoted 62 lines
--- include/net/sock.h | 9 +++++---- net/ipv4/tcp.c | 13 +++---------- 2 files changed, 8 insertions(+), 14 deletions(-)include/net/sock.h +5
-4@@ -1465,13 +1465,14 @@ static inline void sk_mem_uncharge(struct sock *sk, int size)static inline void sk_wmem_free_skb (struct sock *sk,struct sk_buff *skb) { -if (!sk->sk_tx_skb_cache) { - sk->sk_tx_skb_cache = skb; -return ; - }sock_set_flag (sk,SOCK_QUEUE_SHRUNK ); sk->sk_wmem_queued -= skb->truesize;sk_mem_uncharge (sk, skb->truesize); +if (!sk->sk_tx_skb_cache) { +skb_zcopy_clear (skb, true); + sk->sk_tx_skb_cache = skb; +return ; + }__kfree_skb (skb); }net/ipv4/tcp.c +3
-11@@ -865,14 +865,9 @@ struct sk_buff *sk_stream_alloc_skb(struct sock *sk, int size, gfp_t gfp, {struct sk_buff *skb; - skb = sk->sk_tx_skb_cache; -if (skb && !size) { -const struct sk_buff_fclones *fclones; - - fclones = container_of(skb, struct sk_buff_fclones, skb1); -if (refcount_read (&fclones->fclone_ref) ==1 ) { - sk->sk_wmem_queued -= skb->truesize; -sk_mem_uncharge (sk, skb->truesize); +if (likely (!size)) { + skb = sk->sk_tx_skb_cache; +if (skb && !skb_cloned (skb)) { skb->truesize -= skb->data_len; sk->sk_tx_skb_cache =NULL ;pskb_trim (skb,0 );@@ -2543,8 +2538,6 @@ void tcp_write_queue_purge(struct sock *sk)tcp_rtx_queue_purge (sk); skb = sk->sk_tx_skb_cache;if (skb) { - sk->sk_wmem_queued -= skb->truesize; -sk_mem_uncharge (sk, skb->truesize);__kfree_skb (skb); sk->sk_tx_skb_cache =NULL ; } --2.21.0.392.gf8f6787159e-goog
Quoted 11 lines · Eric Dumazet
My recent patch had at least three problems :
1) TX zerocopy wants notification when skb is acknowledged, thus we need to call
skb_zcopy_clear()if the skb is cached intosk->sk_tx_skb_cache2) Some applications might expect precise EPOLLOUT notifications, so we need to update
sk->sk_wmem_queuedand callsk_mem_uncharge()fromsk_wmem_free_skb()in all cases. TheSOCK_QUEUE_SHRUNKflag must also be set.
Quoted 7 lines
3) Reuse of saved skb should have used
skb_cloned()instead of simply checking if the fast clone has been freed.Fixes 472c2e07eef0 ("tcp: add one skb cache for tx")Signed-off-by Eric Dumazet <[email protected]>Cc Willem de Bruijn <[email protected]>Cc Soheil Hassas Yeganeh <[email protected]>
The v1 of this patch caused a lot of oopsies, but this one works fine. So:
cheers,
Holger
From: Eric Dumazet <[email protected]>
Date: Tue, 26 Mar 2019 08:34:55 -0700
Quoted 11 lines
My recent patch had at least three problems :
1) TX zerocopy wants notification when skb is acknowledged, thus we need to call
skb_zcopy_clear()if the skb is cached intosk->sk_tx_skb_cache2) Some applications might expect precise EPOLLOUT notifications, so we need to update
sk->sk_wmem_queuedand callsk_mem_uncharge()fromsk_wmem_free_skb()in all cases. TheSOCK_QUEUE_SHRUNKflag must also be set.
Quoted 5 lines
3) Reuse of saved skb should have used
skb_cloned()instead of simply checking if the fast clone has been freed.Fixes 472c2e07eef0 ("tcp: add one skb cache for tx")Signed-off-by Eric Dumazet <[email protected]>
Applied, thanks Eric.