From: Chris Mason accept() of an MPTCP connection that has already received data can leak one page of memory cgroup charge, and the leaked charges can get the server OOM killed. At accept(), __inet_accept() charges the MPTCP socket (msk) for the memory that it holds, rounded up to whole pages, and mptcp_graft_subflows() does the same for each subflow. A page is uncharged only once, when the socket that holds it releases it, so rounding up each socket on its own is only correct while each page belongs to one socket. But for each skb that the msk takes over from a subflow, it borrows skb->truesize bytes from the pages that the subflow reserved, so the two can share a page: reserved by the subflow 4096 bytes 1 page now held by the msk 896 bytes charged for 1 page still held by the subflow 3200 bytes charged for 1 page The fix used here is to keep a running sum of what the msk and its subflows hold, and to let the new mptcp_charge_subflow() charge each subflow only for the pages that it adds to the rounded-up sum. 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: an MPTCP server in a memory cgroup, the first byte of the client arriving before accept(). Without this patch, 4096 bytes stayed charged to the memory cgroup for each connection, after its end: SNAP label=after-1000 rounds=1000 sockets_left_in_srv=0 [...] c_sock=4096000 [...] SNAP label=after-5000 rounds=5000 sockets_left_in_srv=0 [...] c_sock=20480000 [...] With a memory limit, the original version of this patch reported that the server was killed by the OOM killer after about 14,400 connections. Fixes: 9db5b3cec4ec ("mptcp: borrow forward memory from subflow") Cc: stable@vger.kernel.org Signed-off-by: Chris Mason [ Shorter comment. Commit message: how the issue was found, and its symptoms. ] Reviewed-by: Mat Martineau Signed-off-by: Matthieu Baerts (NGI0) --- net/mptcp/protocol.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/net/mptcp/protocol.c b/net/mptcp/protocol.c index 8649e739ae50..ed8606fb1a90 100644 --- a/net/mptcp/protocol.c +++ b/net/mptcp/protocol.c @@ -4481,10 +4481,26 @@ static int mptcp_listen(struct socket *sock, int backlog) return err; } +/* Similar to __sk_charge(), but the msk and its subflows share pages */ +static int mptcp_charge_subflow(struct sock *sk, struct sock *ssk, int mem) +{ + int new_mem = mem + ssk->sk_forward_alloc + + atomic_read(&ssk->sk_rmem_alloc); + int amt = sk_mem_pages(new_mem) - sk_mem_pages(mem); + gfp_t gfp = GFP_KERNEL | __GFP_NOFAIL; + + mem_cgroup_sk_inherit(sk, ssk); + if (amt > 0) + mem_cgroup_sk_charge(ssk, amt, gfp); + kmem_cache_charge(ssk, gfp); + return new_mem; +} + static void mptcp_graft_subflows(struct sock *sk) { struct mptcp_subflow_context *subflow; struct mptcp_sock *msk = mptcp_sk(sk); + int mem; if (mem_cgroup_sockets_enabled) { LIST_HEAD(join_list); @@ -4502,6 +4518,9 @@ static void mptcp_graft_subflows(struct sock *sk) __mptcp_flush_join_list(sk, &join_list); } + /* What __inet_accept() just charged for the msk, before rounding up. */ + mem = sk->sk_forward_alloc + atomic_read(&sk->sk_rmem_alloc); + mptcp_for_each_subflow(msk, subflow) { struct sock *ssk = mptcp_subflow_tcp_sock(subflow); @@ -4517,7 +4536,7 @@ static void mptcp_graft_subflows(struct sock *sk) goto unlock; __mptcp_inherit_cgrp_data(sk, ssk); - __mptcp_inherit_memcg(sk, ssk, GFP_KERNEL); + mem = mptcp_charge_subflow(sk, ssk, mem); unlock: release_sock(ssk); -- 2.55.0