Skip to contentSkip to conversation

tcp: add recv_should_stop helper

PATCH In review Geliang Tang 3 participants 4 messages Mar 31 – Apr 1

Subsystems (3)
lore
Share & tools
lore

Geliang Tang

author reviewer
#0 lore raw

PATCH v3 1/1 tcp: add recv_should_stop helper

From: Geliang Tang <[email protected]>

Factor out a new helper tcp_recv_should_stop() from tcp_recvmsg_locked() and tcp_splice_read() to check whether to stop receiving. And use this helper in mptcp_recvmsg() and mptcp_splice_read() to reduce redundant code.

---
v3:
- drop timeo parameter of tcp_recv_should_stop as Matt suggested.
- drop patch 1 "mptcp: check desc->count in read_sock" in v2 as Matt suggested. I changed the state of it as "Rejected".

v2:
- v1 changed the original behavior when replacing tcp_recv_should_stop.
This version preserves the behavior unchanged.
- add a cleanup patch for read_sock into this set.
- Link: https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v1:
- Link: https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

---
 include/net/tcp.h    |  8 ++++++++
 net/ipv4/tcp.c       |  9 ++-------
 net/mptcp/protocol.c | 11 +++--------
 3 files changed, 13 insertions(+), 15 deletions(-)
include/net/tcp.h +8 -0
index 565943c34b7e..6156d1d068e1 100644
@@ -3077,4 +3077,12 @@ enum skb_drop_reason tcp_inbound_hash(struct sock *sk,
 		const void *saddr, const void *daddr,
 		int family, int dif, int sdif);
 
+static inline int tcp_recv_should_stop(struct sock *sk)
+{
+	return sk->sk_err ||
+	       sk->sk_state == TCP_CLOSE ||
+	       (sk->sk_shutdown & RCV_SHUTDOWN) ||
+	       signal_pending(current);
+}
+
 #endif	/* _TCP_H */
net/ipv4/tcp.c +2 -7
index 2a69441568b3..f2d2884652ea 100644
@@ -888,9 +888,7 @@ ssize_t tcp_splice_read(struct socket *sock, loff_t *ppos,
 		release_sock(sk);
 		lock_sock(sk);
 
-		if (sk->sk_err || sk->sk_state == TCP_CLOSE ||
-		    (sk->sk_shutdown & RCV_SHUTDOWN) ||
-		    signal_pending(current))
+		if (tcp_recv_should_stop(sk))
 			break;
 	}
 
@@ -2755,10 +2753,7 @@ static int tcp_recvmsg_locked(struct sock *sk, struct msghdr *msg, size_t len,
 
 		if (copied) {
 			if (!timeo ||
-			    sk->sk_err ||
-			    sk->sk_state == TCP_CLOSE ||
-			    (sk->sk_shutdown & RCV_SHUTDOWN) ||
-			    signal_pending(current))
+			    tcp_recv_should_stop(sk))
 				break;
 		} else {
 			if (sock_flag(sk, SOCK_DONE))
net/mptcp/protocol.c +3 -8
index 98a6caf9af97..2f0d7f9f7c3c 100644
@@ -2322,11 +2322,8 @@ static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
 			break;
 
 		if (copied) {
-			if (sk->sk_err ||
-			    sk->sk_state == TCP_CLOSE ||
-			    (sk->sk_shutdown & RCV_SHUTDOWN) ||
-			    !timeo ||
-			    signal_pending(current))
+			if (tcp_recv_should_stop(sk) ||
+			    !timeo)
 				break;
 		} else {
 			if (sk->sk_err) {
@@ -4511,9 +4508,7 @@ static ssize_t mptcp_splice_read(struct socket *sock, loff_t *ppos,
 		release_sock(sk);
 		lock_sock(sk);
 
-		if (sk->sk_err || sk->sk_state == TCP_CLOSE ||
-		    (sk->sk_shutdown & RCV_SHUTDOWN) ||
-		    signal_pending(current))
+		if (tcp_recv_should_stop(sk))
 			break;
 	}
 
Signature · 1 line
2.51.0
#1 lore raw

Hi Geliang,

Thank you for your modifications, that's great!

Our CI did some validations and here is its report:

- KVM Validation: normal (except selftest_mptcp_join): Success! ✅
- KVM Validation: normal (only selftest_mptcp_join): Success! ✅
- KVM Validation: debug (except selftest_mptcp_join): Success! ✅
- KVM Validation: debug (only selftest_mptcp_join): Success! ✅
- KVM Validation: btf-normal (only bpftest_all): Success! ✅
- KVM Validation: btf-debug (only bpftest_all): Success! ✅
- Task: https://github.com/multipath-tcp/mptcp_net-next/actions/runs/23789110560

Initiator: Patchew Applier
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/94bd185c6c6f
Patchwork: https://patchwork.kernel.org/project/mptcp/list/?series=1075047

If there are some issues, you can reproduce them using the same environment as the one used by the CI thanks to a docker image, e.g.:

    $ cd [kernel source code]
    $ docker run -v "${PWD}:${PWD}:rw" -w "${PWD}" --privileged --rm -it \
        --pull always mptcp/mptcp-upstream-virtme-docker:latest \
        auto-normal

For more details:

https://github.com/multipath-tcp/mptcp-upstream-virtme-docker

Please note that despite all the efforts that have been already done to have a stable tests suite when executed on a public CI like here, it is possible some reported issues are not due to your modifications. Still, do not hesitate to help us improve that ;-)

Cheers,
MPTCP GH Action bot
Bot operated by Matthieu Baerts (NGI0 Core)

maintainer Reviewed-by on v3 1/1
#2 lore raw
In reply to Geliang Tang

Hi Geliang,

Quoted 6 lines · Geliang Tang

From: Geliang Tang <[email protected]>

Factor out a new helper tcp_recv_should_stop() from tcp_recvmsg_locked() and tcp_splice_read() to check whether to stop receiving. And use this helper in mptcp_recvmsg() and mptcp_splice_read() to reduce redundant code.

Quoted 8 lines

---
v3:
- drop timeo parameter of tcp_recv_should_stop as Matt suggested.
- drop patch 1 "mptcp: check desc->count in read_sock" in v2 as Matt suggested. I changed the state of it as "Rejected".

Thank you! This v3 looks good to me:

Cheers,
Matt

Signature · 1 line
Sponsored by the NGI0 Core fund.
maintainer
#3 lore raw
In reply to Geliang Tang

Hi Geliang,

Quoted 5 lines · Geliang Tang

From: Geliang Tang <[email protected]>

Factor out a new helper tcp_recv_should_stop() from tcp_recvmsg_locked() and tcp_splice_read() to check whether to stop receiving. And use this helper in mptcp_recvmsg() and mptcp_splice_read() to reduce redundant code.

Now in our tree:

New patches for t/upstream:
- 9c3c48dcf714: tcp: add recv_should_stop helper
- Results: 64764461d86c..cbbf0a1eaf50 (export)

Tests are now in progress:

- export:
https://github.com/multipath-tcp/mptcp_net-next/commit/2181bea59443fcf1527a72c6e0f99a01c245314e/checks

Cheers,
Matt

Signature · 1 line
Sponsored by the NGI0 Core fund.