OpenVZ / Virtuozzo kernel development (devel@openvz.org)
 help / color / mirror / Atom feed
From: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
To: khorenko@virtuozzo.com, vladimir.riabchun@virtuozzo.com
Cc: devel@openvz.org
Subject: [Devel] [PATCH hci-8.2 8/9] ms/mptcp: borrow forward memory from subflow
Date: Thu, 19 Nov 2026 06:35:33 +0200	[thread overview]
Message-ID: <20261119044702.1218722-9-eva.kurchatova@virtuozzo.com> (raw)
In-Reply-To: <20261119044702.1218722-1-eva.kurchatova@virtuozzo.com>

From: Paolo Abeni <pabeni@redhat.com>

In the MPTCP receive path, we release the subflow allocated fwd
memory just to allocate it again shortly after for the msk.

That could increases the failures chances, especially when we will
add backlog processing, with other actions could consume the just
released memory before the msk socket has a chance to do the
rcv allocation.

Replace the skb_orphan() call with an open-coded variant that
explicitly borrows, the fwd memory from the subflow socket instead
of releasing it.

The borrowed memory does not have PAGE_SIZE granularity; rounding to
the page size will make the fwd allocated memory higher than what is
strictly required and could make the incoming subflow fwd mem
consistently negative. Instead, keep track of the accumulated frag and
borrow the full page at subflow close time.

This allow removing the last drop in the TCP to MPTCP transition and
the associated, now unused, MIB.

Signed-off-by: Paolo Abeni <pabeni@redhat.com>
Reviewed-by: Mat Martineau <martineau@kernel.org>
Signed-off-by: Matthieu Baerts (NGI0) <matttbe@kernel.org>
Link: https://patch.msgid.link/20251121-net-next-mptcp-memcg-backlog-imp-v1-12-1f34b6c1e0b1@kernel.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
(cherry picked from commit 9db5b3cec4ec1c0cd3239689f5c8653d691a1754)

Rebase note: mptcp_init_skb() is not split out of __mptcp_move_skb() in
this tree, so the subflow lends and the msk borrows the forward memory
there, in place of skb_orphan() and the sk_rmem_schedule() check.
lent_mem_frag goes after the close_event_done bool.
Here the patch comes after "ms/mptcp: implemented OoO queue pruning",
which it precedes in mainline, so RcvPruned and the drop label stay
for the in-sequence check of that patch. Mainline removed the counter
here and brought it back with e0e4d56b0505 ("mptcp: explicitly drop
over memory limits").

https://virtuozzo.atlassian.net/browse/VSTOR-155321
Feature: fix ms/net

Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
---
 net/mptcp/fastopen.c |  4 +++-
 net/mptcp/protocol.c | 21 ++++++++++++++-------
 net/mptcp/protocol.h | 28 ++++++++++++++++++++++++++++
 3 files changed, 45 insertions(+), 8 deletions(-)

diff --git a/net/mptcp/fastopen.c b/net/mptcp/fastopen.c
index b9e451197902..82ec15bcfd7f 100644
--- a/net/mptcp/fastopen.c
+++ b/net/mptcp/fastopen.c
@@ -32,7 +32,8 @@ void mptcp_fastopen_subflow_synack_set_params(struct mptcp_subflow_context *subf
 	/* dequeue the skb from sk receive queue */
 	__skb_unlink(skb, &ssk->sk_receive_queue);
 	skb_ext_reset(skb);
-	skb_orphan(skb);
+
+	mptcp_subflow_lend_fwdmem(subflow, skb);
 
 	/* We copy the fastopen data, but that don't belong to the mptcp sequence
 	 * space, need to offset it in the subflow sequence, see mptcp_subflow_get_map_offset()
@@ -50,6 +51,7 @@ void mptcp_fastopen_subflow_synack_set_params(struct mptcp_subflow_context *subf
 	mptcp_data_lock(sk);
 	DEBUG_NET_WARN_ON_ONCE(sock_owned_by_user_nocheck(sk));
 
+	mptcp_borrow_fwdmem(sk, skb);
 	skb_set_owner_r(skb, sk);
 	__skb_queue_tail(&sk->sk_receive_queue, skb);
 	mptcp_sk(sk)->bytes_received += skb->len;
diff --git a/net/mptcp/protocol.c b/net/mptcp/protocol.c
index e278f199a200..ff0a56db0876 100644
--- a/net/mptcp/protocol.c
+++ b/net/mptcp/protocol.c
@@ -349,13 +349,8 @@ static bool __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk,
 	__skb_unlink(skb, &ssk->sk_receive_queue);
 
 	skb_ext_reset(skb);
-	skb_orphan(skb);
-
-	/* try to fetch required memory from subflow */
-	if (!sk_rmem_schedule(sk, skb, skb->truesize)) {
-		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RCVPRUNED);
-		goto drop;
-	}
+	mptcp_subflow_lend_fwdmem(subflow, skb);
+	mptcp_borrow_fwdmem(sk, skb);
 
 	has_rxtstamp = TCP_SKB_CB(skb)->has_rxtstamp;
 
@@ -2448,6 +2443,7 @@ static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk,
 {
 	struct mptcp_sock *msk = mptcp_sk(sk);
 	bool dispose_it, need_push = false;
+	int fwd_remaining;
 
 	/* Do not pass RX data to the msk, even if the subflow socket is not
 	 * going to be freed (i.e. even for the first subflow on graceful
@@ -2456,6 +2452,17 @@ static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk,
 	lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
 	subflow->closing = 1;
 
+	/* Borrow the fwd allocated page left-over; fwd memory for the subflow
+	 * could be negative at this point, but will be reach zero soon - when
+	 * the data allocated using such fragment will be freed.
+	 */
+	if (subflow->lent_mem_frag) {
+		fwd_remaining = PAGE_SIZE - subflow->lent_mem_frag;
+		sk_forward_alloc_add(sk, fwd_remaining);
+		sk_forward_alloc_add(ssk, -fwd_remaining);
+		subflow->lent_mem_frag = 0;
+	}
+
 	/* If the first subflow moved to a close state before accept, e.g. due
 	 * to an incoming reset or listener shutdown, the subflow socket is
 	 * already deleted by inet_child_forget() and the mptcp socket can't
diff --git a/net/mptcp/protocol.h b/net/mptcp/protocol.h
index 282176fe111d..51a8a3357e2d 100644
--- a/net/mptcp/protocol.h
+++ b/net/mptcp/protocol.h
@@ -543,6 +543,7 @@ struct mptcp_subflow_context {
 	bool	close_event_done;   /* has done the post-closed part,
 				     * protected by msk socket lock
 				     */
+	u32	lent_mem_frag;
 	u32	remote_nonce;
 	u64	thmac;
 	u32	local_nonce;
@@ -646,6 +647,33 @@ mptcp_send_active_reset_reason(struct sock *sk)
 	tcp_send_active_reset(sk, GFP_ATOMIC, reason);
 }
 
+/* Made the fwd mem carried by the given skb available to the msk,
+ * To be paired with a previous mptcp_subflow_lend_fwdmem() before freeing
+ * the skb or setting the skb ownership.
+ */
+static inline void mptcp_borrow_fwdmem(struct sock *sk, struct sk_buff *skb)
+{
+	struct sock *ssk = skb->sk;
+
+	/* The subflow just lend the skb fwd memory, and we know that the skb
+	 * is only accounted on the incoming subflow rcvbuf.
+	 */
+	DEBUG_NET_WARN_ON_ONCE(skb->destructor);
+	skb->sk = NULL;
+	sk_forward_alloc_add(sk, skb->truesize);
+	atomic_sub(skb->truesize, &ssk->sk_rmem_alloc);
+}
+
+static inline void
+mptcp_subflow_lend_fwdmem(struct mptcp_subflow_context *subflow,
+			  struct sk_buff *skb)
+{
+	int frag = (subflow->lent_mem_frag + skb->truesize) & (PAGE_SIZE - 1);
+
+	skb->destructor = NULL;
+	subflow->lent_mem_frag = frag;
+}
+
 static inline u64
 mptcp_subflow_get_map_offset(const struct mptcp_subflow_context *subflow)
 {
-- 
2.56.1

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

  parent reply	other threads:[~2026-11-19  4:49 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19  4:35 [Devel] [PATCH hci-8.2 0/9] ms/mptcp: backport fixes for dropped stream segments Eva Kurchatova
2026-11-19  4:35 ` [Devel] [PATCH hci-8.2 1/9] ms/tcp: Save lock_sock() for memcg in inet_csk_accept() Eva Kurchatova
2026-11-19  4:53   ` no-reply
2026-11-19 17:08   ` Konstantin Khorenko
2026-11-19  4:35 ` [Devel] [PATCH hci-8.2 2/9] ms/mptcp: Fix up subflow's memcg when CONFIG_SOCK_CGROUP_DATA=n Eva Kurchatova
2026-11-19  4:52   ` no-reply
2026-11-19 17:08   ` Konstantin Khorenko
2026-11-19  4:35 ` [Devel] [PATCH hci-8.2 3/9] ms/net: factor-out _sk_charge() helper Eva Kurchatova
2026-11-19  4:53   ` no-reply
2026-11-19 17:08   ` Konstantin Khorenko
2026-11-19  4:35 ` [Devel] [PATCH hci-8.2 4/9] ms/mptcp: factor-out cgroup data inherit helper Eva Kurchatova
2026-11-19  4:54   ` no-reply
2026-11-19 17:08   ` Konstantin Khorenko
2026-11-19  4:35 ` [Devel] [PATCH hci-8.2 5/9] ms/mptcp: grafting MPJ subflow earlier Eva Kurchatova
2026-11-19  4:59   ` no-reply
2026-11-19 17:08   ` Konstantin Khorenko
2026-11-19  4:35 ` [Devel] [PATCH hci-8.2 6/9] ms/mptcp: fix memcg accounting for passive sockets Eva Kurchatova
2026-11-19  4:58   ` no-reply
2026-11-19 17:08   ` Konstantin Khorenko
2026-11-19  4:35 ` [Devel] [PATCH hci-8.2 7/9] ms/mptcp: handle first subflow closing consistently Eva Kurchatova
2026-11-19  4:59   ` no-reply
2026-11-19 17:08   ` Konstantin Khorenko
2026-11-19  4:35 ` Eva Kurchatova [this message]
2026-11-19  5:03   ` [Devel] [PATCH hci-8.2 8/9] ms/mptcp: borrow forward memory from subflow no-reply
2026-11-19 17:08   ` Konstantin Khorenko
2026-11-19  4:35 ` [Devel] [PATCH hci-8.2 9/9] ms/mptcp: reclaim forward-allocated memory on RX path errors Eva Kurchatova
2026-11-19  5:01   ` no-reply
2026-11-19 17:08   ` Konstantin Khorenko
2026-11-19 17:08 ` [Devel] [PATCH hci-8.2 0/9] ms/mptcp: backport fixes for dropped stream segments Konstantin Khorenko
2026-11-19 17:08 ` Konstantin Khorenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261119044702.1218722-9-eva.kurchatova@virtuozzo.com \
    --to=eva.kurchatova@virtuozzo.com \
    --cc=devel@openvz.org \
    --cc=khorenko@virtuozzo.com \
    --cc=vladimir.riabchun@virtuozzo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox