Article View: linux.kernel.netdev
Article #18446Re: [PATCH v2 net-next 5/7] tcp: Allocate all new skb before collapsing.
From: Eric Dumazet
Date: Wed, 18 Nov 2026 10:28
Date: Wed, 18 Nov 2026 10:28
188 lines
16581 bytes
16581 bytes
Le mar. 17 nov. 2026 ร 00:06, Kuniyuki Iwashima <kuniyu@google.com> a รฉcrit :
>
> At the final stage, tcp_collapse() iterates over collapsible
> candidate skbs and repacks them to new skbs.
>
> In each iteration, a new skb is allocated and inserted to the
> receive queue or a temporary list for RB tree.
>
> If alloc_skb() fails while splitting a large bloated skb across
> multiple new skbs, the partially-copied skb is not removed and
> new skbs remain in the queue as duplicates, consuming more memory.
>
> It is only when all candidates are properly collapsed that
> we can save memory.
>
> To avoid complicated rollback, let's preallocate skbs before
> collapsing.
>
> Now all skb in [start, sub_end) must be allocated in a batch,
> so the section size is normally capped at TCP_COLLAPSE_MAX_TOTAL_SIZE
> (16 skbs), except when a single bloated skb or overlapping OOO skbs
> must be collapsed together to free the original skb or keep the
> rbtree monotonic.
>
> Once the size exceeds the limit, the remaining skbs are re-evaluated
> from restart:.
>
> Note that the next skb can be skipped if its density already meets
> tp->scaling_ratio, and each cut adds at most one remainder skb
> (~3.4% worst-case truesize overhead on 4 KiB pages), which is a
> small tradeoff to bound the temporary allocation.
>
> Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
> Reviewed-by: Eric Dumazet <edumazet@kernel.org>
> ---
> net/ipv4/tcp_input.c | 29 +++++++++++++++++++++--------
> 1 file changed, 21 insertions(+), 8 deletions(-)
>
> diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
> index 0815e1891fba..ca4cdb93373d 100644
> --- a/net/ipv4/tcp_input.c
> +++ b/net/ipv4/tcp_input.c
> @@ -5812,6 +5812,7 @@ static struct sk_buff *tcp_skb_next(struct sk_buff *skb, struct sk_buff_head *li
> #define TCP_COLLAPSE_SCALING_RATIO \
> ((TCP_COLLAPSE_MAX_SIZE << TCP_RMEM_TO_WIN_SCALE) / \
> (SKB_TRUESIZE(TCP_COLLAPSE_MAX_SIZE)))
> +#define TCP_COLLAPSE_MAX_TOTAL_SIZE (TCP_COLLAPSE_MAX_SIZE * 16)
>
> static bool tcp_collapse_skb_bloated(const struct sk_buff *skb,
> u32 start, u8 scaling_ratio)
> @@ -5852,8 +5853,9 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
> {
> int old_truesize, new_truesize;
> struct sk_buff *skb = head, *n;
> - struct sk_buff_head tmp;
> + struct sk_buff_head tmp, pool;
> int remainder, nskbs;
> + struct sk_buff *nskb;
> u8 scaling_ratio;
> u32 sub_end;
>
> @@ -5861,6 +5863,7 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
> TCP_COLLAPSE_SCALING_RATIO);
>
> __skb_queue_head_init(&tmp);
> + __skb_queue_head_init(&pool);
>
> /* First, check that queue is collapsible and find
> * the point where collapsing can be useful.
> @@ -5908,8 +5911,9 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
>
> /* seq and end_seq must increase monotonically for rbtree. */
> if ((list || !before(TCP_SKB_CB(n)->seq, sub_end)) &&
> - !tcp_collapse_skb_bloated(n, sub_end,
> - TCP_COLLAPSE_SCALING_RATIO))
> + (TCP_SKB_CB(n)->end_seq - start >= TCP_COLLAPSE_MAX_TOTAL_SIZE ||
> + !tcp_collapse_skb_bloated(n, sub_end,
> + TCP_COLLAPSE_SCALING_RATIO)))
> break;
Sashiko has a point about the "overlapping OOO skbs" exception [1]:
it is fully under control of the peer.
For the ofo queue, as long as n->seq is before sub_end, neither the
cap nor the density test is evaluated. tcp_data_queue_ofo() keeps
partially overlapping segments, so a peer sending small segments,
each of them overlapping the previous one by one byte, builds a chain
that tcp_collapse() sees as a single section covering the whole
ofo queue.
>
> sub_end = TCP_SKB_CB(n)->end_seq;
> @@ -5924,6 +5928,8 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
> if (remainder) {
> nskbs++;
> new_truesize += compute_skb_truesize(remainder, GFP_ATOMIC);
> + } else {
> + remainder = TCP_COLLAPSE_MAX_SIZE;
> }
>
> if (old_truesize <= new_truesize) {
> @@ -5932,14 +5938,19 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
> goto restart;
> }
>
> - while (before(start, sub_end)) {
> - int copy = min_t(int, TCP_COLLAPSE_MAX_SIZE, sub_end - start);
> - struct sk_buff *nskb;
> -
> - nskb = alloc_skb(copy, GFP_ATOMIC);
> + while (nskbs--) {
> + nskb = alloc_skb(nskbs ? TCP_COLLAPSE_MAX_SIZE : remainder,
> + GFP_ATOMIC);
> if (!nskb)
> goto end;
>
> + __skb_queue_tail(&pool, nskb);
> + }
Then this loop is only bounded by sk_rcvbuf.
With a big tcp_rmem[2], this can be megabytes of GFP_ATOMIC memory
allocated at once, not charged to the socket, while all the
original skbs are still in the queue. Before this patch the
overshoot was about one skb, because originals were freed while
being copied.
Also, a single alloc_skb() failure now discards the whole batch,
so such a queue might never be collapsed, precisely when we are
under memory pressure.
I think TCP_COLLAPSE_MAX_TOTAL_SIZE must be a hard limit for anything
but the single (first) skb case.
Legitimate overlaps (retransmits with a different segmentation) only
involve a few skbs. A long chain of overlapping skbs is not something
we need to optimize for, so my preference would be to simply not
collapse it:
Keep walking the chain in the second loop so that sub_end is
correct, remember that the cap has been exceeded, then skip the
section the same way patch 4 does:
start = sub_end;
skb = n;
goto restart;
tcp_prune_ofo_queue() will take care of these skbs if we are still
over the limit.
The alternative would be to cut the chain at the cap, but then the
next section starts with an skb whose seq is before @start, and this
skb would have to be collapsed unconditionally (even if not bloated,
even if patch 4 says the result is not smaller) to keep the rbtree
ordered. This also means @released could be negative in patch 6.
This looks more fragile to me.
Related to this: in patch 4, when the second loop breaks on
!tcp_skb_can_collapse_rx() (or SYN/FIN, unreadable frags) with an
overlapping @n in the rbtree, @n can stay in the tree and the last
nskb of the previous section is inserted after it, with a smaller
end_seq. To be fair, the current code has the same issue (goto end
in the copy loop), but since you are making the ordering explicit
in patch 3, it would be nice to cover it: the same "skip the
section" logic could be used when the loop stops on an overlapping
skb that can not be collapsed.
Whatever option you choose, please add a comment next to
TCP_COLLAPSE_MAX_TOTAL_SIZE describing what it bounds and its
exceptions.
[1] https://lore.kernel.org/netdev/179495722211.3136.15241045445062201797@kernel.org/
Technical Details
Message-ID:
<CAL4WiipeiomdeRaSOpRn6Unu4-ohwrdkWCZ9mRn1ELfrW24+XQ@mail.gmail.com>Path:
mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-366038-linux-kernel-netdev=dewy.ruReferences:
<20261116230555.2305867-1-kuniyu@google.com> <20261116230555.2305867-6-kuniyu@google.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-366038-linux-kernel-netdev=dewy.ru
From: edumazet@kernel.org (Eric Dumazet)
Newsgroups: linux.kernel.netdev
Subject: Re: [PATCH v2 net-next 5/7] tcp: Allocate all new skb before collapsing.
Date: Wed, 18 Nov 2026 11:28:44 +0100
Organization: Usenet Network
Message-ID: <CAL4WiipeiomdeRaSOpRn6Unu4-ohwrdkWCZ9mRn1ELfrW24+XQ@mail.gmail.com>
References: <20261116230555.2305867-1-kuniyu@google.com> <20261116230555.2305867-6-kuniyu@google.com>
MIME-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Injection-Info: usenet.network;
logging-data="2568833"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:Wd1gNPGGKvBhFhDGeuCV0G4Sx0E= sha256:J8iBgG3unKqp/thWYNjEQUJV0ubg8vPTTAm2793rO6E=
sha1:04yoa0sotEZVFg6Q1TxCbcENWOU= sha256:DKhtTS6VlVeV7sFYkLZ6G0fE2qm4c44X7zNzTlgvcOM=
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 346DD11524A; Wed, 18 Nov 2026 13:29:19 +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 mf_KPXPAQBqT;
Wed, 18 Nov 2026 13:29:10 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-366038-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 sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
by mail.dewy.ru (Postfix) with ESMTPS id 732F6115246; Wed, 18 Nov 2026 13:29:02 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
by sea.lore.kernel.org (Postfix) with ESMTP id 1D5D6353FB; Wed, 18 Nov 2026 10:29:00 +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=1794997739;
b=HCaVayd1aXT9jygc2zmli5acYDVFHbu+D/uVq79xwPpiXeGDpHF6AC/3/C3Pa0Q30T9J
SMhb2AXfHoCUsYvRWKP/mRy++ec+96kDsm3VBGt4CsV2NOP9MUuRpjzNsr8ms5K5KBiTS
PC9d166KCXkgkg8XZbmxKMXyK0od1tWFscd40mcQ0dDiIutAb4C8utOZsU3TlHWMOTdXl
mPwzaa+C4u7baWwpxogCf9cRAt5Di/kVSLPjltUq6N3hC5dTVkGVONJikVpzvh043qKJ3
mqn99RbkYf3GHXTT2vjx6CINqE5jqFDWMT5Z8D1U5V8JHBE9e9GINQ3SizNdafFkRpQ==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1794997739;
h=X-Original-To:Received:Received:DKIM-Signature:Received:
X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:X-Mailing-List:
List-Id:List-Post:List-Help:List-Subscribe:List-Unsubscribe:
List-Owner:Precedence:MIME-Version:References:In-Reply-To:From:Date:
X-Gmail-Original-Message-ID:X-Gm-Features:Message-ID:Subject:To:Cc:
Content-Type:Content-Transfer-Encoding;
bh=hyYrCKddYun6Ci2oVE4KqeDabN1R54MsEm3bclQ1m+Y=;
b=PphsZ07rSdmYSY9ESGDAS6ZcRjK4oDz0TCmtY2u6a/zDQSJ9gptNYIM2DxayupPU3Ywe
CBO4liRRkv1s6TjXkJPuI8TQeCPqkmR09W/33KqCPF4pPtJqJ26zCCrpZZ54BA47u0r/G
UdtRObLY3SpZsnvD/jCBNOLZt8FIHCZqctvcp1OvVYw/9WJh1fAf79lpf9bN+ljV3O671
7z1uHBtFenaqbKIP4rHrgAiyoz1NkPB8iKVaVNJ8ZKtvLc+1EWrTkH1N3Gn+ztN0oZnVb
cS0pd6Wjugs9G2ESyYCBzgKl4qVqmdr5OaKxl1pA9KvjIW7DFxNWhdOlJlSR5ZjIMVg==
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=Hqk+8mEJ;
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 BBD3E606A331;
Wed, 18 Nov 2026 10:28:59 +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=Hqk+8mEJ
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=1794997738;
b=QOQKG+1QXGXiPocJ5/r8V5jMI6f7hb6o5TU2zos7pOMpLB09eKubq8LJH+ZQjfTZvYE1
0eIzV8aAvWTXgNWUgLY3L1r0HlMHqOlOIOrjUVtwVVuhf+GPfKcQ+NzB6JV2mNmj/jsHO
n+hKWae9wiUZd0KL3ggsnaYj/75u3Y13F9prNn8sQvgt0rFtOZYsBZ3fB7pUOqm6gQYLH
3y4Yw3G7T4APRmYfN29+PoUABWgZzqpb4dKIKwoyElhbgngMDkwGQqBjknGm3L/IgSYZN
f4moj2hVELP8M1MyImgKIUO9ECW14aUwA0PGAC/r67LzFoTAW31vbQxNXkzD4/oMovg==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1794997738;
h=Received:DKIM-Signature:Received:X-Forwarded-Encrypted:
X-Gm-Message-State:X-Received:MIME-Version:References:In-Reply-To:
From:Date:X-Gmail-Original-Message-ID:X-Gm-Features:Message-ID:
Subject:To:Cc:Content-Type:Content-Transfer-Encoding;
bh=hyYrCKddYun6Ci2oVE4KqeDabN1R54MsEm3bclQ1m+Y=;
b=Yywa8r7jRBr2ygwE/9oK9S+0N2iVeS7qVSYXRKsg6yGYjZkrN8qBISUWLwm6d/+p0zze
X8dn7Vh/L4ExhEr+fSeeQh5yIHaml9cieoKukmA4HBVy8KPAIa1nIAGWu9v7k9wzG1nkF
w8ubdWAonw9kCozp9ljJ1LQSJLiI6cId3Uj8IpMmZ5Dzr0998Ey0sEcOgGx70kfYiSyVe
xnQcSm7pnAT95tcQ6dcJI2Fp9bFXr8P06/M5c7wlGUnlV/XygAKz+DUCVBWP3n3Hs5uDV
wTDyEfJuOw8mUm9kFSSy49OimdHWK74wm3LC/oJ3zrS79mO21sBtgP+3Bo+LaTv6qBg==
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=Hqk+8mEJ;
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 D6DE6606A320
for <netdev@vger.kernel.org>; Wed, 18 Nov 2026 10:28:58 +0000 (UTC)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id B4B0E1F000C9
for <netdev@vger.kernel.org>; Wed, 18 Nov 2026 10:28:58 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
s=k20260515; t=1794997738;
bh=hyYrCKddYun6Ci2oVE4KqeDabN1R54MsEm3bclQ1m+Y=;
h=References:In-Reply-To:From:Date:Subject:To:Cc;
b=Hqk+8mEJ7vtA7W/h5nUCLhMl0lGcipzVhs33vxhvi3a0NnuG6PbosFnmWHshDD7hA
P5z3cktyEIzrNXNBEHc80Td5YNge9iBCtRB4h/qtbGhSCrLuB5rfU/Hc595FQminIn
ycxMu/z5Reo3vH1bL/lVCp4C6Q4Wgq9PEpFjfkykCaTkMvnnwIsKYOFYWLdS3047Du
+ciOYqk1bx5B0/Qy7wGqo4aVI8e767y8lQ3YXV6ORWeWeh9lrViIyMJJEgBvJjVsA7
j2LvQVSL6SbZ7l2AkC4mQoqzfjMoZ7R1vMS+NKaB1luiH+AfrcM6YR5u8aS21vciBT
tvvCeANkwjlRQ==
X-Received: by mail-oo1-f51.google.com with SMTP id 006d021491bc7-716d052dc23so413460eaf.1
for <netdev@vger.kernel.org>; Wed, 18 Nov 2026 02:28:58 -0800 (PST)
X-Forwarded-Encrypted: i=1; AL+hErHbObgbyDslUztBIvFoJnJDkxa2O66I4WIsQtgTj7wNRHQ8Suiq7xI2px8Lill80k6JBoRbMIg=@vger.kernel.org
X-Gm-Message-State: APuU4Z8GPOa1KA+JqXMR4cYy/8bSuSnxW36Nphz1e3SYL2YnPBTeRlbA
hxHSltHer/PqEWVuUq00pQ34Ol1hYBux9ubqVcbA3pWTF9pEnulQ37yqNHa0YqAMDXafEN+X17+
rHevaDoAhHz8mVIeLccOOBJEBUqy0pgo=
X-Received: by 2002:a05:6820:1c84:b0:6d7:a13d:fd7b with SMTP id
006d021491bc7-71b18483801mr1460952eaf.28.1794997737802; Wed, 18 Nov 2026
02:28:57 -0800 (PST)
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: <20261116230555.2305867-6-kuniyu@google.com>
X-Gmail-Original-Message-ID: <CAL4WiipeiomdeRaSOpRn6Unu4-ohwrdkWCZ9mRn1ELfrW24+XQ@mail.gmail.com>
X-Gm-Features: AclHuK-sNpS4rMULS10qwTKTJag47_6QqpDqDc6vL03tNxIhG20bc3PJelJq6jE
X-Cc: Neal Cardwell <ncardwell@google.com>, "David S . Miller" <davem@davemloft.net>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Matthieu Baerts <matttbe@kernel.org>, Mat Martineau <martineau@kernel.org>,
Simon Horman <horms@kernel.org>, Kuniyuki Iwashima <kuni1840@gmail.com>, netdev@vger.kernel.org,
mptcp@lists.linux.dev