Article View: linux.kernel.netdev
Article #19032Re: [PATCH v3 net-next 5/7] tcp: Allocate all new skb before collapsing.
From: Eric Dumazet
Date: Thu, 19 Nov 2026 07:44
Date: Thu, 19 Nov 2026 07:44
136 lines
14420 bytes
14420 bytes
Le jeu. 19 nov. 2026 ร 08:17, 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 capped at TCP_COLLAPSE_MAX_TOTAL_SIZE
> (16 skbs), except for the first single skb.
>
> Once the size exceeds the limit, the remaining skbs are
> re-evaluated from restart:.
>
> Legitimate overlaps (retransmits with a different segmentation)
> only involve a few skbs, so if an overlapping OOO chain reaches
> the limit, we simply skip it.
>
> 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>
> ---
> v3: Make TCP_COLLAPSE_MAX_TOTAL_SIZE hard-limit for multiple skbs
> ---
> net/ipv4/tcp_input.c | 41 ++++++++++++++++++++++++++++++++---------
> 1 file changed, 32 insertions(+), 9 deletions(-)
>
> diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
> index 27ed9ba11ec9..990e34896373 100644
> --- a/net/ipv4/tcp_input.c
> +++ b/net/ipv4/tcp_input.c
> @@ -5822,6 +5822,11 @@ 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)))
> +/* Cap pool preallocation per sub-range to at most 16 skbs in seq
> + * space. A single first skb exceeding this is still collapsed,
> + * whereas an exceeding overlapping ofo range is skipped.
> + */
> +#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)
> @@ -5862,8 +5867,9 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
> {
> unsigned 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;
>
> @@ -5871,6 +5877,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.
> @@ -5926,8 +5933,9 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
> }
>
> if (!overlap &&
> - !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;
>
> sub_end = TCP_SKB_CB(n)->end_seq;
> @@ -5935,25 +5943,38 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
> n = tcp_skb_next(n, list);
> }
>
> + if (sub_end != TCP_SKB_CB(skb)->end_seq &&
> + sub_end - start >= TCP_COLLAPSE_MAX_TOTAL_SIZE)
> + goto skip;
This is fine for now, but note that a single 64K GRO skb overlapping
a bloated skb is enough to reach the limit, and the bloated skb is
then never collapsed. The range is walked again at every
tcp_collapse_ofo_queue() call, for no gain.
A possible followup would be to cut the range instead of skipping it.
When @n overlaps and we do not want (or can not) merge it, we know
that @n already contains [TCP_SKB_CB(n)->seq, sub_end), because
skbs with end_seq <= sub_end were removed earlier in the loop.
So we could do :
sub_end = TCP_SKB_CB(n)->seq;
and collapse [start, sub_end) only, as long as sub_end is after start.
All new skbs would then have a seq smaller than TCP_SKB_CB(n)->seq,
so the rbtree ordering is preserved, and we would not need to drag
@n into the range.
After the copy, the skbs of the range that were not fully consumed
would have to be freed, since the rest of their payload is in @n.
The same trick would also work for the unmergeable overlapping skb
case added in patch 3.
I have not tested this, and this can be done later.
Reviewed-by: Eric Dumazet <edumazet@kernel.org>
Technical Details
Message-ID:
<CAL4WiioNduj1AybypLPDs6HuW2F9Y9zw3B2ycLSogRMnfzBk9g@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-366828-linux-kernel-netdev=dewy.ruReferences:
<20261119071649.374106-1-kuniyu@google.com> <20261119071649.374106-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-366828-linux-kernel-netdev=dewy.ru
From: edumazet@kernel.org (Eric Dumazet)
Newsgroups: linux.kernel.netdev
Subject: Re: [PATCH v3 net-next 5/7] tcp: Allocate all new skb before collapsing.
Date: Thu, 19 Nov 2026 08:44:54 +0100
Organization: Usenet Network
Message-ID: <CAL4WiioNduj1AybypLPDs6HuW2F9Y9zw3B2ycLSogRMnfzBk9g@mail.gmail.com>
References: <20261119071649.374106-1-kuniyu@google.com> <20261119071649.374106-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="2820437"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:bGKwXOcAR4gdxTm/Xe/Z3cBpe8Y= sha256:5Z3qU7iwZXEVFQE1dB/hQrgN1nDRdJRXG6EJUEQztXc=
sha1:JY06jpsqqlQLkJ8CF4C800sag+Q= sha256:8lZRLm/nj9t9ZKNwL9znjNZwvJLfJNM14aKoQ5Yj8h0=
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 1140A115553; Thu, 19 Nov 2026 10:45:31 +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 5ItYKgzEukYx;
Thu, 19 Nov 2026 10:45:23 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=netdev+bounces-366828-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 tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114])
by mail.dewy.ru (Postfix) with ESMTPS id 567E2115505; Thu, 19 Nov 2026 10:45:15 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
by tor.lore.kernel.org (Postfix) with ESMTP id F372D1E0B6C; Thu, 19 Nov 2026 07:45:11 +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=1795074308;
b=dOsCTUAFqVCfzKTfnbVgVC2+VKZwi/VEOP2lSUUh4A5svLSzmUPFFVkNd7b70v3yKL1g
NJnfLmt+ckMO/XEmiONDTU+J+gm6z65tg8+RLxeiwPLQK8WiSDFc3O22sNf9+h0yB2cVQ
vucg9brwIRW9lJh0OqB0fVEFKZzZm/hdt1LwgfMkXBsTzi9ox19T5id9N31D/1zD7YXWK
K81jtWg+p0a2cWoIPnTpnytFRQ2b/NQZdfPoITS1kzXwZvARqj+25qlddFTEGYJ+Q+NSf
NKdyuwUSSK0o9PEc3Tbj3+PsmVHrnEEyIdBpsFFfVhTXhSQBVc0poYCUuLfMq1GFRXw==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1795074308;
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=d1+ergkQMZDPtjbp7or2Wdfi9HoMSYSR/NYEudC0R5I=;
b=AE0VBeduAs9hlELqg7pgIl4XJDuoRRiVQ9ZJRqyceDArfwu1/CzGoi6mj2XKpMWimIs6
VQDUCawEYaSCY2cFpoFdD3ZOsRLP15IxYz2saICOGBB73Uceev1SWdn1PyeLwwPtd7JtQ
1b3xUpGwaWf3NcCiihrYslyM8u0qvIcP/7Koy4oTTMbc3lckvAWakTDpqey3vIeQQ2wLo
RSBuYz5cM8NodTSFm5gh4NJJYb1wTpgD4V6OysnzLN93QxNWdLOhXGucP131oFKyfexNE
0WM2ZdQjQBpiHtEYlwl0AHfUTd21+lFUGzS6P6hVt21DUGhXWmKgzuND4JHkcCbGVng==
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=CVLqd8F9;
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 0570C60A2604;
Thu, 19 Nov 2026 07:45:08 +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=CVLqd8F9
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=1795074307;
b=nZS6pppfmwlEJCdyAdXp3wfdHGjtbqkTpbnfbJOHWp6BP3gVgTkodhijb9djBy0TdYqM
uXmfIM6ktMtZQwHbvg2ZoODTxBUMl2uDRGJMrEVzGp95ETILt8y7jwWHc8/Jm+S8PpzWq
jWf5ndZeDwyDhzahTTH7MwJmmNdcZX5uX/pAnI2oY6JgnVPtbLOXCRWmrf6TiGBASX7US
+Ran9kfcp2YQn4pJHGYqgwsZPqQEbNpDn2wZby6tUJrCErJRIgQBbMuEdSYz5wrSU2Fxs
nDcHCePP1FQbkOG6KT94xuhX1f13yz55GAZIeZvPZ1RT5SVVXOoE+2mjue/gPtBwwSA==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1795074307;
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=d1+ergkQMZDPtjbp7or2Wdfi9HoMSYSR/NYEudC0R5I=;
b=lN8EulXv+pgFN2PbSFyn/YSV+i7q2Mt4zoZ4gICOiUXBY3ycNUpuJXKQEpbEeHU+eLwP
hEn9RTUM6TEybePZfJlz3G908Ic5OezeRNGPlmM3C0qMga5sYxTFF7vIlyh2OLTHiyEUs
VpiykEg9T0WCY58grie32MNGmt0jeoabTvfJcVYa1gNWM+W92LsoWAf3ymiHJQrZdteYx
djm7WYhaRtXJZHFzqupHqNuDdkcH66pSj97LK7KljaBu+ax+7+VoRpN6vxYKfKlDEAmUe
FFQmJv3mLOXos5fRWgic0bsG8ZpGTm19j0b+FVnNGu9KzDrGrU27FWY1u9BNb4k1NRQ==
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=CVLqd8F9;
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 25BB8606A32C
for <netdev@vger.kernel.org>; Thu, 19 Nov 2026 07:45:06 +0000 (UTC)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 55A041F000CC
for <netdev@vger.kernel.org>; Thu, 19 Nov 2026 07:45:06 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
s=k20260515; t=1795074306;
bh=d1+ergkQMZDPtjbp7or2Wdfi9HoMSYSR/NYEudC0R5I=;
h=References:In-Reply-To:From:Date:Subject:To:Cc;
b=CVLqd8F9URZM2NNWOGdHwBZLlNwhMdUqm06DulF4xLJrlTEfhFi38LHJpEDckyq/o
CZfkVJ/4arf4+RPaIH8A5sl+qoEKiCrZ7NO84f07ax3+PHoIPWNjg/FOaQnMQdo53A
RqtK19MJEqqFQAvrLkcbezbu4JSvEDumYItVi/rIZocKk0/SRodfmA8kVojJ/7zjpY
ReUbnV7nadwErHaXOttN40EgYn+0EOaf4prBnTH8SxO9QEfpwohcJDNIBD4RXW6kE9
BlInvkLMRvmF75pqbkSR8nefksnw0+SAA9eiTiXel/PCZxpn75lYXPwfhMYg6TE8e9
F2LGOJsizr2bQ==
X-Received: by mail-oa1-f54.google.com with SMTP id 586e51a60fabf-451a49abd8aso1696296fac.3
for <netdev@vger.kernel.org>; Wed, 18 Nov 2026 23:45:06 -0800 (PST)
X-Forwarded-Encrypted: i=1; AL+hErEV/4Igv5kmtcDRCOhlmv9kNuq/PaupH/3L7B826eNAp8nwqb4IF7QGFTlmjbrtVwJjfRIoDfk=@vger.kernel.org
X-Gm-Message-State: APuU4Z8Q5h7uo6pDNvnBLgFiVVNuMzYtlc7eRlh7+/lJ0tx7N0fFCcL4
inCLYs3lgA+Y81ci8CXuv+LjFGHJFqtGBNjv/DthnieYJav1/vX0bja/WI+w54NL2POc0q6a4VQ
HwJiupT4i76FK+rOERli3T4C8DM7YwyA=
X-Received: by 2002:a05:6870:1429:b0:48f:e0e5:a202 with SMTP id
586e51a60fabf-4b75fe11713mr5053314fac.54.1795074305588; Wed, 18 Nov 2026
23:45:05 -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: <20261119071649.374106-6-kuniyu@google.com>
X-Gmail-Original-Message-ID: <CAL4WiioNduj1AybypLPDs6HuW2F9Y9zw3B2ycLSogRMnfzBk9g@mail.gmail.com>
X-Gm-Features: AclHuK_R3C_eYGkkpq9fmYj3F1h9Oq6I8L_OOXLPiSpYJTlCKcskIw-XEfvh2KY
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