🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #25977

[PATCH net 8/9] mptcp: don't charge a shared page twice at accept()

#25977
From: "Matthieu Baerts (NGI0)"
Date: Wed, 02 Dec 2026 21:29
100 lines
12083 bytes
From: Chris Mason <mason@kernel.org>

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 <mason@kernel.org>
[ Shorter comment. Commit message: how the issue was found, and its
  symptoms. ]
Reviewed-by: Mat Martineau <martineau@kernel.org>
Signed-off-by: Matthieu Baerts (NGI0) <matttbe@kernel.org>
---
 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
Technical Details
Message-ID: <20261202-net-mptcp-misc-fixes-7-4-rc7-v1-8-9f03efcb1a17@kernel.org>
Path: mbox-mo1.pugleaf.net!news-wdc.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-376068-linux-kernel-netdev=dewy.ru
References: <20261202-net-mptcp-misc-fixes-7-4-rc7-v1-0-9f03efcb1a17@kernel.org>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-wdc.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-376068-linux-kernel-netdev=dewy.ru
From: matttbe@kernel.org ("Matthieu Baerts (NGI0)")
Newsgroups: linux.kernel.netdev
Subject: [PATCH net 8/9] mptcp: don't charge a shared page twice at
 accept()
Date: Wed, 02 Dec 2026 22:29:38 +0100
Organization: Usenet Network
Message-ID: <20261202-net-mptcp-misc-fixes-7-4-rc7-v1-8-9f03efcb1a17@kernel.org>
References: <20261202-net-mptcp-misc-fixes-7-4-rc7-v1-0-9f03efcb1a17@kernel.org>
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit
Injection-Info: usenet.network;
	logging-data="2066106"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:IuV5fBxL3g8v9eNqOYOK6MYjGuw= sha256:WZfLdIC1JtCAmdKj2sN2w7hUR7RyMVvXn/gkd+n3dLI=
	sha1:mryH0MQTh49Ix63GF3uhDJcYWYs= sha256:ZU/WOqOJDg6I1L9k+wo0C09fXRhyRdf5NoHfw2xNJb4=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id BBB1C11773C; Thu,  3 Dec 2026 00:32:42 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.401
X-Spam-Status: No, score=-3.401 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1,
	DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=kernel.org
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id iXG9mAZCwNi5;
	Thu,  3 Dec 2026 00:32:40 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=104.64.211.4; helo=sin.lore.kernel.org; envelope-from=netdev+bounces-376068-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org
X-Received: from sin.lore.kernel.org (sin.lore.kernel.org [104.64.211.4])
	by mail.dewy.ru (Postfix) with ESMTPS id B34C2117739; Thu,  3 Dec 2026 00:32:31 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sin.lore.kernel.org (Postfix) with ESMTP id B5FB424F72; Wed,  2 Dec 2026 21:32:26 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1796247135;
	b=PTvt4LB84H613EG2gbXtUJ91sJqHhc3Bk1wtd60QTYY/QK3UKAdFzbjg0s1ZxsbW7dwa
	 mERJj+Em44lYrx9fV7Ncibh48L2nZSQLkvJ6ut4bu5HyTd8Qm8ONDwBvPyy8+cogeZjNZ
	 jsfqWGH/L6azDloVjUNojNp/BxyvydhbW+E6vHA0YKO4zOoJILdUFywKsSgN9qXDv2OVF
	 xudNsBt/l+4Wwe4sKflwOjBVWBiEL/B8e1tABYv5/vhUTsdnQBlojj6XKjQ6CI6oLLHh6
	 tpQDV4KaVq6gZKU71XlLk+ENDzTTlOksKnVfyYf3EDMbkAw6cxowbqMIVqdaxMjURRw==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796247135;
	h=X-Original-To:Received:Received:DKIM-Signature:From:Date:Subject:
	 X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe:
	 List-Unsubscribe:List-Owner:Precedence:MIME-Version:Content-Type:
	 Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc:
	 X-Mailer:X-Developer-Signature:X-Developer-Key;
	bh=jzoTuHpyXpH7w9LCyGAsCgwyehvWEnN/AROLokXnnFY=;
	b=FAomUgEUQJbHlbWcfRCrD39/4RwF2S2qCcx/r+0cOi7btxdqZETIUkI6Eii0v+Cs2Z++
	 oMCkf7+XYmvLXoVCndQmzsQvkvCea9/d0mmzYKZnRZn0YQRViBMcoC6MWvCxFb7jHhMtt
	 ll0E2m4gchuBufGo9oyvAa7phNbEXeuEOFvUHzW5VHOqvjbrD74y1T5ZinqhL/b+0JKV9
	 gNlKvZ04p9bGcyD6tpeHIuSnINOcW5d+l8zejDhXk+CATcdkRudS+pLmw8UtSNuBX/EG8
	 66GD/pzK3kHp96I9gwp9TBD1nKy+P5RpDibmWkNZhXupx4fReRK7l1I7d8rr2ZzNqOQ==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=ciRZxaYj;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 1DAD2606A330;
	Wed,  2 Dec 2026 21:32:15 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=ciRZxaYj
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1796247134;
	b=IE+jBbPDrZk65zaSlE3UXeeaSRki9bBkrUXCpuv9f9H3P3+0EyLXO4pN89XuYodBD5wD
	 Vdi9ZTPDJDL0sStcOzFDwl+mWU/0PWT0LYBXyi+mUhluRBhPtllVdBLPG6BiV5voChICH
	 QwJDtnb9DMwxmzkQuanop8coPZPHGtC8BJF6f9GTmBcZVKgTeu9K2lBw7WLlfZuij4aGb
	 QJD8mHIvGKq/rywZRPEATaQSKD8OCFjFHVPEwOR4+fBn31y0vTpgdGbxHd2ZG/w0ZjnHX
	 w1ZLU8TPzQTR/sD8SHEEOAOK9ctI8zqO4Ap9NaaR4k04JoukjKNYJ0Ho/MDWey7xFpg==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796247134;
	h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type:
	 Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc:
	 X-Mailer:X-Developer-Signature:X-Developer-Key;
	bh=jzoTuHpyXpH7w9LCyGAsCgwyehvWEnN/AROLokXnnFY=;
	b=hniyrsRhr2K64V0JMGMoDeRmYvLasAQ5B6danVTAPgDItBQ40sVIxoPjVTmMcYUzIAm8
	 SI47YFPJNX0uc8K1gY5fLMvqtDCaw0Qeg8hJUKzLOYnvbMSarUOKs/+OrnznY6Q2ou91c
	 wrzUcIdrxX8qYJxh/5/zWMPYtktlkWf85z7exHSAgvxB3ac4BbUON45MEpTMun5vZ0Ya4
	 aGat+DBU4eCw2YjZZuy10UO26uls2AKD/2VFnqTlJwVZL0sUAJAWbcag04JbR0U4PBcDT
	 fQnBZuY7aKJoYBRe2I/AAxOjLrsipnaqPm+Ghaz9F3ss6+JddXekI5CMus54awIDD1w==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=ciRZxaYj;
	arc=none smtp.remote-ip=100.103.45.18
X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1))
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3DEFD606A696;
	Wed,  2 Dec 2026 21:32:14 +0000 (UTC)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9CA0C1F00A2A;
	Wed,  2 Dec 2026 21:32:11 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
	s=k20260515; t=1796247133;
	bh=jzoTuHpyXpH7w9LCyGAsCgwyehvWEnN/AROLokXnnFY=;
	h=From:Date:Subject:References:In-Reply-To:To:Cc;
	b=ciRZxaYjnns9SbIunYl6flJVlIme2b++VGG9GlJMiqGXg10w9ZxkjQcXyT5Zin2h2
	 GVkjDyBjqZFRxYIM92400KBdE1m+3IwlN9u2C3Nv0WwmWvfpG3aBPvZupnOHjWvtr2
	 4e84Y4ulaCNGTSv/OFHUxoKqB8ywmgGyMAsAUyA7yZ4J7kXOrvT/mu7se6wAm0MhH0
	 haW9ldq6SIEHreU1T0xRNw830lzq8Ko2bBc9XWAGhnNHzp5xYhDB6ap/O+kqDLNv2K
	 6AGvWaGNbU+P7NglChWUVVYASbbiUfpDEJsTy96/h0bG3ek2PVfXNCnM41oobS55PF
	 1ANqc6dqHr1Sg==
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list
X-In-Reply-To: <20261202-net-mptcp-misc-fixes-7-4-rc7-v1-0-9f03efcb1a17@kernel.org>
X-Cc: netdev@vger.kernel.org, mptcp@lists.linux.dev, 
 "Matthieu Baerts (NGI0)" <matttbe@kernel.org>, 
 Chris Mason <mason@kernel.org>, stable@vger.kernel.org
X-Mailer: b4 0.16.0
X-Developer-Signature: v=1; a=openpgp-sha256; l=3802; i=matttbe@kernel.org;
 h=from:subject:message-id; bh=ibmov4lEhKdjtKx4YquZTyZjLYoUjR6/vGd2l2PMOiw=;
 b=owGbwMvMwCVWo/Th0Gd3rumMp9WSGLIF+pzMA/JvqFksYN1tdP6A1vnDU0Qypjzdu/X9lZSzv
 YZ/K1zVOkpZGMS4GGTFFFmk2yLzZz6v4i3x8rOAmcPKBDKEgYtTACaSHsjwv8L7d13LVNZ6SbYQ
 wdcHHQ036uVkGP9/GFOh6/ypboOuCMM/u5jlB7/nqZbHXkgPn9+0bXWB10aDwPTbGTct3FbWMzF
 zAgA=
X-Developer-Key: i=matttbe@kernel.org; a=openpgp;
 fpr=E8CB85F76877057A6E27F77AF6B7824F4269A073