From: Chris Mason accept() can permanently overcharge the memory cgroup of an MPTCP server, so the server can be OOM killed, if the new connection received data while its MPTCP socket was locked. The sockets of an MPTCP connection have no memory cgroup before accept(), which then charges the sk_forward_alloc and the sk_rmem_alloc of the MPTCP socket (msk) and of each subflow. mptcp_graft_subflows() also charges msk->backlog_unaccounted: the truesize of the skbs that __mptcp_add_backlog() queued while the socket lock of the msk was held and their subflow had no memory cgroup. But the first charge already covers each of these skbs that has not been freed, in the sk_rmem_alloc of its subflow or of the msk, so no later uncharge matches the extra charge. The fix used here is to remove msk->backlog_unaccounted, its charge in mptcp_graft_subflows() and the warning that tests it in mptcp_can_spool_backlog(). An RFC patch [1] removes the same lines, as part of another approach. This issue has been found with the help of an LLM, and reproduced in a VM, with the reproducer shared with the original version of this patch, on a kernel with the previous patch: 100,000 connections to an MPTCP server in a memory cgroup. Without this patch, about 63 MB stayed charged to this memory cgroup once all the connections were closed, and to its parent after its removal: SNAP label=after-100000 rounds=100000 sockets_left_in_srv=0 tcp_mem_pages=0 [...] c_sock=63295488 [...] [...] SNAP label=c-removed rounds=100000 sockets_left_in_srv=0 tcp_mem_pages=0 c=removed [...] p_sock=63295488 [...] And about 12 MB with only 50 connections, each getting 20,000 one-byte segments before accept(), new for TCP but with the same data sequence number, so the msk drops them as duplicates. Bare ACKs with RM_ADDR, sent at the same time, make the MPTCP worker take the socket lock, so that some of these segments go through the backlog: SNAP label=after-50 rounds=50 sockets_left_in_srv=0 tcp_mem_pages=256 [...] c_sock=12881920 [...] Fixes: 6228efe0cc01 ("mptcp: leverage the backlog for RX packet processing") Cc: stable@vger.kernel.org Link: https://lore.kernel.org/all/20260924080219.1036588-4-caixinchen1@huawei.com/ [1] Signed-off-by: Chris Mason [ No comment change: the commit message explains why the backlog is covered. Commit message: refer to the RFC [1] removing the same lines + how the issue was found, and its symptoms. ] Reviewed-by: Mat Martineau Signed-off-by: Matthieu Baerts (NGI0) --- net/mptcp/protocol.c | 35 +---------------------------------- net/mptcp/protocol.h | 1 - 2 files changed, 1 insertion(+), 35 deletions(-) diff --git a/net/mptcp/protocol.c b/net/mptcp/protocol.c index ed8606fb1a90..cef861ea026e 100644 --- a/net/mptcp/protocol.c +++ b/net/mptcp/protocol.c @@ -795,12 +795,6 @@ static void __mptcp_add_backlog(struct sock *sk, account: WRITE_ONCE(msk->backlog_len, msk->backlog_len + delta); - - /* Possibly not accept()ed yet, keep track of memory not CG - * accounted, mptcp_graft_subflows() will handle it. - */ - if (!mem_cgroup_from_sk(ssk)) - msk->backlog_unaccounted += delta; } static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk, @@ -2334,12 +2328,6 @@ static bool mptcp_can_spool_backlog(struct sock *sk, struct list_head *skbs) { struct mptcp_sock *msk = mptcp_sk(sk); - /* After CG initialization, subflows should never add skb before - * gaining the CG themself. - */ - DEBUG_NET_WARN_ON_ONCE(msk->backlog_unaccounted && sk->sk_socket && - mem_cgroup_from_sk(sk)); - if (list_empty(&msk->backlog_list)) return false; @@ -4508,8 +4496,7 @@ static void mptcp_graft_subflows(struct sock *sk) /* Subflows joining after __inet_accept() will get the * mem CG properly initialized at mptcp_finish_join() time, * but subflows pending in join_list need explicit - * initialization before flushing `backlog_unaccounted` - * or MPTCP can later unexpectedly observe unaccounted memory. + * initialization by the loop below. */ mptcp_data_lock(sk); list_splice_init(&msk->join_list, &join_list); @@ -4541,26 +4528,6 @@ static void mptcp_graft_subflows(struct sock *sk) unlock: release_sock(ssk); } - - if (mem_cgroup_sk_enabled(sk)) { - gfp_t gfp = GFP_KERNEL | __GFP_NOFAIL; - int amt; - - /* Account the backlog memory; prior accept() is aware of - * fwd and rmem only. - */ - mptcp_data_lock(sk); - amt = sk_mem_pages(sk->sk_forward_alloc + - msk->backlog_unaccounted + - atomic_read(&sk->sk_rmem_alloc)) - - sk_mem_pages(sk->sk_forward_alloc + - atomic_read(&sk->sk_rmem_alloc)); - msk->backlog_unaccounted = 0; - mptcp_data_unlock(sk); - - if (amt) - mem_cgroup_sk_charge(sk, amt, gfp); - } } static int mptcp_stream_accept(struct socket *sock, struct socket *newsock, diff --git a/net/mptcp/protocol.h b/net/mptcp/protocol.h index 05bf9c3f11b4..73bd9a3008f6 100644 --- a/net/mptcp/protocol.h +++ b/net/mptcp/protocol.h @@ -392,7 +392,6 @@ struct mptcp_sock { struct list_head backlog_list; /* protected by the data lock */ u32 backlog_len; - u32 backlog_unaccounted; }; #define mptcp_data_lock(sk) spin_lock_bh(&(sk)->sk_lock.slock) -- 2.55.0