tcp: some bug fixes for tcp_read_skb()
Subsystems (6)
v3 cover tcp: some bug fixes for tcp_read_skb()
From: Cong Wang <[email protected]>
This patchset contains 3 bug fixes and 1 minor refactor patch for tcp_read_skb(). V1 only had the first patch, as Eric prefers to fix all of them together, I have to group them together. Please see each patch description for more details.
---
v2: add more patches
v3: simplify empty receive queue case
Cong Wang (4):
tcp: fix sock skb accounting in tcp_read_skb()
tcp: fix tcp_cleanup_rbuf() for tcp_read_skb()
tcp: refactor tcp_read_skb() a bit
tcp: handle pure FIN case correctly
net/core/skmsg.c | 5 +++-- net/ipv4/tcp.c | 49 +++++++++++++++++++++++------------------------- 2 files changed, 26 insertions(+), 28 deletions(-)
Signature · 1 line
2.34.1
PATCH v3 1/4 tcp: fix sock skb accounting in tcp_read_skb()
From: Cong Wang <[email protected]>
Before commit 965b57b469a5 ("net: Introduce a new proto_ops ->read_skb()"), skb was not dequeued from receive queue hence when we close TCP socket skb can be just flushed synchronously.
After this commit, we have to uncharge skb immediately after being dequeued, otherwise it is still charged in the original sock. And we still need to retain skb->sk, as eBPF programs may extract sock information from skb->sk. Therefore, we have to call skb_set_owner_sk_safe() here.
--- net/ipv4/tcp.c | 1 + 1 file changed, 1 insertion(+)
net/ipv4/tcp.c +1 -0
@@ -1760,6 +1760,7 @@ int tcp_read_skb(struct sock *sk, skb_read_actor_t recv_actor)int used;__skb_unlink (skb, &sk->sk_receive_queue); +WARN_ON (!skb_set_owner_sk_safe (skb, sk)); used =recv_actor (sk, skb);if (used <=0 ) {if (!copied)
Signature · 1 line
2.34.1
Jakub Sitnicki
maintainerRe: [Patch net v3 1/4] tcp: fix sock skb accounting in tcp_read_skb()
Quoted 28 lines · Cong Wang
From: Cong Wang <[email protected]>
Before commit
965b57b469a5("net: Introduce a newproto_ops->read_skb()"), skb was not dequeued from receive queue hence when we close TCP socket skb can be just flushed synchronously.After this commit, we have to uncharge skb immediately after being dequeued, otherwise it is still charged in the original sock. And we still need to retain
skb->sk, as eBPF programs may extract sock information fromskb->sk. Therefore, we have to callskb_set_owner_sk_safe()here.Fixes 965b57b469a5 ("net: Introduce a new proto_ops ->read_skb()")Reported-and-tested-by [email protected]Tested-by Stanislav Fomichev <[email protected]>Cc Eric Dumazet <[email protected]>Cc John Fastabend <[email protected]>Cc Jakub Sitnicki <[email protected]>Signed-off-by Cong Wang <[email protected]>--- net/ipv4/tcp.c | 1 + 1 file changed, 1 insertion(+)net/ipv4/tcp.c +0
-0
net/ipv4/tcp.c int tcp_read_skb(struct sock *sk, skb_read_actor_t recv_actor) · line 1766
Quoted 7 lines
net/ipv4/tcp.c +1
-0@@ -1760,6 +1760,7 @@ int tcp_read_skb(struct sock *sk, skb_read_actor_t recv_actor)int used;__skb_unlink (skb, &sk->sk_receive_queue); +WARN_ON (!skb_set_owner_sk_safe (skb, sk)); used =recv_actor (sk, skb);if (used <=0 ) {if (!copied)
That is a frequent operation.
Don't we want WARN_ON_ONCE like in tcp_read_sock?
PATCH 1/1 tcp: Use WARN_ON_ONCE() in tcp_read_skb()
From: Peilin Ye <[email protected]>
Prevent tcp_read_skb() from flooding the syslog.
--- net/ipv4/tcp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/tcp.c +1 -1
@@ -1766,7 +1766,7 @@ int tcp_read_skb(struct sock *sk, skb_read_actor_t recv_actor)return 0 ;__skb_unlink (skb, &sk->sk_receive_queue); -WARN_ON (!skb_set_owner_sk_safe (skb, sk)); +WARN_ON_ONCE (!skb_set_owner_sk_safe (skb, sk)); copied =recv_actor (sk, skb);if (copied >=0 ) { seq += copied;
Signature · 1 line
2.20.1
PATCH v2 1/1 net: Use WARN_ON_ONCE() in {tcp,udp}_read_skb()
From: Peilin Ye <[email protected]>
Prevent tcp_read_skb() and udp_read_skb() from flooding the syslog.
---
change since v1:
- do the same to udp_read_skb() (Cong Wang)
Cong's tcp_read_skb() fix [1] depends on this patch.
[1] https://lore.kernel.org/netdev/[email protected]/
Thanks,
Peilin Ye
net/ipv4/tcp.c | 2 +- net/ipv4/udp.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
net/ipv4/tcp.c +1 -1
@@ -1766,7 +1766,7 @@ int tcp_read_skb(struct sock *sk, skb_read_actor_t recv_actor)return 0 ;__skb_unlink (skb, &sk->sk_receive_queue); -WARN_ON (!skb_set_owner_sk_safe (skb, sk)); +WARN_ON_ONCE (!skb_set_owner_sk_safe (skb, sk)); copied =recv_actor (sk, skb);if (copied >=0 ) { seq += copied;
net/ipv4/udp.c +1 -1
@@ -1821,7 +1821,7 @@ int udp_read_skb(struct sock *sk, skb_read_actor_t recv_actor)continue ; } -WARN_ON (!skb_set_owner_sk_safe (skb, sk)); +WARN_ON_ONCE (!skb_set_owner_sk_safe (skb, sk)); used =recv_actor (sk, skb);if (used <=0 ) {if (!copied)
Signature · 1 line
2.20.1
Re: [PATCH net v2] net: Use WARN_ON_ONCE() in {tcp,udp}_read_skb()
Quoted 2 lines · Peilin Ye
Suggested-by Jakub Sitnicki <[email protected]>Signed-off-by Peilin Ye <[email protected]>
Sorry, I forgot to add Fixes: tag.
Those WARN_ON() come from different commits. I will split this into two in v3 to make it easier.
Thanks,
Peilin Ye
Re: [PATCH net v2] net: Use WARN_ON_ONCE() in {tcp,udp}_read_skb()
Quoted 8 lines · Peilin Ye
Quoted 2 lines · Peilin Ye
Suggested-by Jakub Sitnicki <[email protected]>Signed-off-by Peilin Ye <[email protected]>Sorry, I forgot to add Fixes: tag.
Those
WARN_ON()come from different commits. I will split this into two in v3 to make it easier.
Cong suggested not sending v3 since this is not fixing a bug, and thus no need to add Fixes: tags.
Re: [PATCH net] tcp: Use WARN_ON_ONCE() in tcp_read_skb()
In reply to Peilin YeThis patch was applied to netdev/net.git (master) by David S. Miller <[email protected]>:
Hello:
This patch was applied to netdev/net.git (master)
by David S. Miller <[email protected]>:
Quoted 8 lines · On Thu, 8 Sep 2022 16:15:23 -0700 you wrote:
From: Peilin Ye <[email protected]>
Prevent
tcp_read_skb()from flooding the syslog.Suggested-by Jakub Sitnicki <[email protected]>Signed-off-by Peilin Ye <[email protected]>[...]
Here is the summary with links:
- [net] tcp: Use WARN_ON_ONCE() in tcp_read_skb()
https://git.kernel.org/netdev/net/c/96628951869c
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html
PATCH v3 2/4 tcp: fix tcp_cleanup_rbuf() for tcp_read_skb()
In reply to Cong WangFrom: Cong Wang <[email protected]>
tcp_cleanup_rbuf() retrieves the skb from sk_receive_queue, it assumes the skb is not yet dequeued. This is no longer true for tcp_read_skb() case where we dequeue the skb first.
Fix this by introducing a helper __tcp_cleanup_rbuf() which does not require any skb and calling it in tcp_read_skb().
--- net/ipv4/tcp.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-)
net/ipv4/tcp.c +14 -10
@@ -1567,17 +1567,11 @@ static int tcp_peek_sndq(struct sock *sk, struct msghdr *msg, int len)* calculation of whether or not we must ACK for the sake of * a window update. */ -void tcp_cleanup_rbuf (struct sock *sk,int copied) +static void __tcp_cleanup_rbuf (struct sock *sk,int copied) {struct tcp_sock *tp =tcp_sk (sk);bool time_to_ack = false; -struct sk_buff *skb =skb_peek (&sk->sk_receive_queue); - -WARN (skb && !before (tp->copied_seq,TCP_SKB_CB (skb)->end_seq), -"cleanup rbuf bug: copied %X seq %X rcvnxt %X\n" , - tp->copied_seq,TCP_SKB_CB (skb)->end_seq, tp->rcv_nxt); -if (inet_csk_ack_scheduled (sk)) {const struct inet_connection_sock *icsk =inet_csk (sk);
@@ -1623,6 +1617,17 @@ void tcp_cleanup_rbuf(struct sock *sk, int copied)tcp_send_ack (sk); } +void tcp_cleanup_rbuf (struct sock *sk,int copied) +{ +struct sk_buff *skb =skb_peek (&sk->sk_receive_queue); +struct tcp_sock *tp =tcp_sk (sk); + +WARN (skb && !before (tp->copied_seq,TCP_SKB_CB (skb)->end_seq), +"cleanup rbuf bug: copied %X seq %X rcvnxt %X\n" , + tp->copied_seq,TCP_SKB_CB (skb)->end_seq, tp->rcv_nxt); +__tcp_cleanup_rbuf (sk, copied); +} +static void tcp_eat_recv_skb (struct sock *sk,struct sk_buff *skb) {__skb_unlink (skb, &sk->sk_receive_queue);
@@ -1771,20 +1776,19 @@ int tcp_read_skb(struct sock *sk, skb_read_actor_t recv_actor) copied += used;if (TCP_SKB_CB (skb)->tcp_flags &TCPHDR_FIN ) { -consume_skb (skb); ++seq;break ; } -consume_skb (skb);break ; } +consume_skb (skb);WRITE_ONCE (tp->copied_seq, seq);tcp_rcv_space_adjust (sk);/* Clean up data we have read: This will do ACK frames. */ if (copied >0 ) -tcp_cleanup_rbuf (sk, copied); +__tcp_cleanup_rbuf (sk, copied);return copied; }
Signature · 1 line
2.34.1
Jakub Sitnicki
maintainerRe: [Patch net v3 2/4] tcp: fix tcp_cleanup_rbuf() for tcp_read_skb()
Quoted 72 lines · Cong Wang
From: Cong Wang <[email protected]>
tcp_cleanup_rbuf()retrieves the skb fromsk_receive_queue, it assumes the skb is not yet dequeued. This is no longer true fortcp_read_skb()case where we dequeue the skb first.Fix this by introducing a helper
__tcp_cleanup_rbuf()which does not require any skb and calling it intcp_read_skb().Fixes 04919bed948d ("tcp: Introduce tcp_read_skb()")Cc Eric Dumazet <[email protected]>Cc John Fastabend <[email protected]>Cc Jakub Sitnicki <[email protected]>Signed-off-by Cong Wang <[email protected]>--- net/ipv4/tcp.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-)net/ipv4/tcp.c +13
-9@@ -1567,17 +1567,11 @@ static int tcp_peek_sndq(struct sock *sk, struct msghdr *msg, int len)* calculation of whether or not we must ACK for the sake of * a window update. */ -void tcp_cleanup_rbuf (struct sock *sk,int copied) +static void __tcp_cleanup_rbuf (struct sock *sk,int copied) {struct tcp_sock *tp =tcp_sk (sk);bool time_to_ack = false; -struct sk_buff *skb =skb_peek (&sk->sk_receive_queue); - -WARN (skb && !before (tp->copied_seq,TCP_SKB_CB (skb)->end_seq), -"cleanup rbuf bug: copied %X seq %X rcvnxt %X\n" , - tp->copied_seq,TCP_SKB_CB (skb)->end_seq, tp->rcv_nxt); -if (inet_csk_ack_scheduled (sk)) {const struct inet_connection_sock *icsk =inet_csk (sk);@@ -1623,6 +1617,17 @@ void tcp_cleanup_rbuf(struct sock *sk, int copied)tcp_send_ack (sk); } +void tcp_cleanup_rbuf (struct sock *sk,int copied) +{ +struct sk_buff *skb =skb_peek (&sk->sk_receive_queue); +struct tcp_sock *tp =tcp_sk (sk); + +WARN (skb && !before (tp->copied_seq,TCP_SKB_CB (skb)->end_seq), +"cleanup rbuf bug: copied %X seq %X rcvnxt %X\n" , + tp->copied_seq,TCP_SKB_CB (skb)->end_seq, tp->rcv_nxt); +__tcp_cleanup_rbuf (sk, copied); +} +static void tcp_eat_recv_skb (struct sock *sk,struct sk_buff *skb) {__skb_unlink (skb, &sk->sk_receive_queue);@@ -1771,20 +1776,19 @@ int tcp_read_skb(struct sock *sk, skb_read_actor_t recv_actor) copied += used;if (TCP_SKB_CB (skb)->tcp_flags &TCPHDR_FIN ) { -consume_skb (skb); ++seq;break ; } -consume_skb (skb);break ; } +consume_skb (skb);WRITE_ONCE (tp->copied_seq, seq);
net/ipv4/tcp.c int tcp_read_skb(struct sock *sk, skb_read_actor_t recv_actor) · line 1794
Quoted 10 lines
net/ipv4/tcp.c +1
-1tcp_rcv_space_adjust (sk);/* Clean up data we have read: This will do ACK frames. */ if (copied >0 ) -tcp_cleanup_rbuf (sk, copied); +__tcp_cleanup_rbuf (sk, copied);return copied; }
This seems to be fixing 2 different problems, but the commit description mentions just one.
consume_skb() got pulled out of the `while' body. And thanks to that we are not leaving a dangling skb ref if recv_actor, sk_psock_verdict_recv in this case, returns 0.
PATCH v3 3/4 tcp: refactor tcp_read_skb() a bit
In reply to Cong WangFrom: Cong Wang <[email protected]>
As tcp_read_skb() only reads one skb at a time, the while loop is unnecessary, we can turn it into an if. This also simplifies the code logic.
--- net/ipv4/tcp.c | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-)
net/ipv4/tcp.c +9 -17
@@ -1761,25 +1761,17 @@ int tcp_read_skb(struct sock *sk, skb_read_actor_t recv_actor)if (sk->sk_state ==TCP_LISTEN )return -ENOTCONN ; -while ((skb =tcp_recv_skb (sk, seq, &offset)) !=NULL ) { -int used; - -__skb_unlink (skb, &sk->sk_receive_queue); -WARN_ON (!skb_set_owner_sk_safe (skb, sk)); - used =recv_actor (sk, skb); -if (used <=0 ) { -if (!copied) - copied = used; -break ; - } - seq += used; - copied += used; + skb =tcp_recv_skb (sk, seq, &offset); +if (!skb) +return 0 ; -if (TCP_SKB_CB (skb)->tcp_flags &TCPHDR_FIN ) { +__skb_unlink (skb, &sk->sk_receive_queue); +WARN_ON (!skb_set_owner_sk_safe (skb, sk)); + copied =recv_actor (sk, skb); +if (copied >0 ) { + seq += copied; +if (TCP_SKB_CB (skb)->tcp_flags &TCPHDR_FIN ) ++seq; -break ; - } -break ; }consume_skb (skb);WRITE_ONCE (tp->copied_seq, seq);
Signature · 1 line
2.34.1
PATCH v3 4/4 tcp: handle pure FIN case correctly
In reply to Cong WangFrom: Cong Wang <[email protected]>
When skb->len==0, the recv_actor() returns 0 too, but we also use 0 for error conditions. This patch amends this by propagating the errors to tcp_read_skb() so that we can distinguish skb->len==0 case from error cases.
--- net/core/skmsg.c | 5 +++-- net/ipv4/tcp.c | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-)
net/core/skmsg.c +3 -2
@@ -1194,8 +1194,9 @@ static int sk_psock_verdict_recv(struct sock *sk, struct sk_buff *skb) ret =bpf_prog_run_pin_on_cpu (prog, skb); ret =sk_psock_map_verd (ret,skb_bpf_redirect_fetch (skb)); } -if (sk_psock_verdict_apply (psock, skb, ret) <0 ) - len =0 ; + ret =sk_psock_verdict_apply (psock, skb, ret); +if (ret <0 ) + len = ret; out:rcu_read_unlock ();return len;
net/ipv4/tcp.c +1 -1
@@ -1768,7 +1768,7 @@ int tcp_read_skb(struct sock *sk, skb_read_actor_t recv_actor)__skb_unlink (skb, &sk->sk_receive_queue);WARN_ON (!skb_set_owner_sk_safe (skb, sk)); copied =recv_actor (sk, skb); -if (copied >0 ) { +if (copied >=0 ) { seq += copied;if (TCP_SKB_CB (skb)->tcp_flags &TCPHDR_FIN ) ++seq;
Signature · 1 line
2.34.1
This series was applied to netdev/net.git (master) by Jakub Kicinski <[email protected]>:
Hello:
This series was applied to netdev/net.git (master) by Jakub Kicinski <[email protected]>:
Quoted 8 lines · On Wed, 17 Aug 2022 12:54:41 -0700 you wrote:
From: Cong Wang <[email protected]>
This patchset contains 3 bug fixes and 1 minor refactor patch for
tcp_read_skb(). V1 only had the first patch, as Eric prefers to fix all of them together, I have to group them together. Please see each patch description for more details.[...]
Here is the summary with links:
- [net,v3,1/4] tcp: fix sock skb accounting in tcp_read_skb()
https://git.kernel.org/netdev/net/c/e9c6e7976026 - [net,v3,2/4] tcp: fix tcp_cleanup_rbuf() for tcp_read_skb() https://git.kernel.org/netdev/net/c/c457985aaa92 - [net,v3,3/4] tcp: refactor tcp_read_skb() a bit https://git.kernel.org/netdev/net/c/a8688821f385 - [net,v3,4/4] tcp: handle pure FIN case correctly https://git.kernel.org/netdev/net/c/2e23acd99efa
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html