The MPTCP sockets orphaned by close() were not counted in
tcp_orphan_count, contrary to TCP: tcp_max_orphans did not limit them,
and they were not visible in /proc/net/sockstat.
This is all the more needed since commit "mptcp: keep an orphan while it
can send its data": an orphan can then be kept longer than its close
timeout, e.g., as Sashiko noticed, by a peer acking the data at the TCP
level only, without moving the DATA_ACK.
Count them, and as TCP does, reset an orphan when there are too many
orphans, or when TCP uses too much memory: at close(), and when its
close timeout expires while it is kept to send its data. This is similar
to what tcp_out_of_resources() does from the TCP timers, without the
stricter limit for the stalled orphans. These resets are counted in
TCPAbortOnMemory, not in a new MPTCP counter: the MPTCP orphans now
share the TCP orphan count and limits.
This has been found by a review agent, and validated with a script
closing 8 connections whose peer doesn't read, with tcp_max_orphans set
to 4: the MPTCP orphans were all kept, without being counted, while
other orphans were reset. Now, only 5 or 6 are kept, vs 4 or 5 with TCP,
which also checks the limit from the timers of its orphans.
Fixes: e16163b6e2b7 ("mptcp: refactor shutdown and close")
Cc: stab…@vger.kernel.org
Reviewed-by: Mat Martineau <mart…@kernel.org>
Signed-off-by: Matthieu Baerts (NGI0) <matt…@kernel.org>
---
net/mptcp/protocol.c | 24 ++++++++++++++++++++++--
net/mptcp/protocol.h | 1 +
2 files changed, 23 insertions(+), 2 deletions(-)
diff --git a/net/mptcp/protocol.c b/net/mptcp/protocol.c
index 96b3baef341f..fc8aad7e2fde 100644
--- a/net/mptcp/protocol.c
+++ b/net/mptcp/protocol.c
@@ -2840,6 +2840,16 @@ static bool mptcp_subflow_probe_timed_out(struct sock *ssk)
return ret;
}
+/* As TCP orphans: limited in number and memory */
+static bool mptcp_orphan_oom(struct sock *sk)
+{
+ if (!tcp_check_oom(sk, 0))
+ return false;
+
+ NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONMEMORY);
+ return true;
+}
+
/* Before its DATA_FIN, keep an orphan while a subflow can send its data */
static bool mptcp_orphan_keep(struct sock *sk)
{
@@ -2857,7 +2867,7 @@ static bool mptcp_orphan_keep(struct sock *sk)
tcp_jiffies32 - READ_ONCE(tcp_sk(ssk)->rcv_tstamp) <
2 * TCP_RTO_MAX &&
!mptcp_subflow_probe_timed_out(ssk))
- return true;
+ return !mptcp_orphan_oom(sk);
}
return false;
@@ -3490,6 +3500,9 @@ static void __mptcp_destroy_sock(struct sock *sk)
sk_stream_kill_queues(sk);
xfrm_sk_free_policy(sk);
+ if (test_and_clear_bit(MPTCP_ORPHAN_COUNTED, &msk->flags))
+ tcp_orphan_count_dec();
+
sock_put(sk);
}
@@ -3579,8 +3592,15 @@ bool __mptcp_close(struct sock *sk, long timeout)
/* all the subflows are closed, only timeout can change the msk
* state, let's not keep resources busy for no reasons
*/
- if (subflows_alive == 0)
+ if (subflows_alive == 0) {
mptcp_set_state(sk, TCP_CLOSE);
+ } else if (sk->sk_state != TCP_CLOSE) {
+ /* as TCP orphans: counted, and limited */
+ tcp_orphan_count_inc();
+ set_bit(MPTCP_ORPHAN_COUNTED, &msk->flags);
+ if (mptcp_orphan_oom(sk))
+ mptcp_do_fastclose(sk);
+ }
sock_hold(sk);
pr_debug("msk=%p state=%d\n", sk, sk->sk_state);
diff --git a/net/mptcp/protocol.h b/net/mptcp/protocol.h
index 7644651f01a8..05bf9c3f11b4 100644
--- a/net/mptcp/protocol.h
+++ b/net/mptcp/protocol.h
@@ -117,6 +117,7 @@
#define MPTCP_FALLBACK_DONE 2
#define MPTCP_WORK_CLOSE_SUBFLOW 3
#define MPTCP_RTX_ENABLED 4
+#define MPTCP_ORPHAN_COUNTED 5
/* MPTCP socket release cb flags */
#define MPTCP_PUSH_PENDING 1
--
2.55.0