From: Kuniyuki Iwashima <kuniyu@google.com>
To: Eric Dumazet <edumazet@kernel.org>,
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>
Cc: Simon Horman <horms@kernel.org>,
Kuniyuki Iwashima <kuniyu@google.com>,
Kuniyuki Iwashima <kuni1840@gmail.com>,
netdev@vger.kernel.org, mptcp@lists.linux.dev
Subject: [PATCH v3 net-next 2/7] tcp: Don't collapse high density skb.
Date: Thu, 19 Nov 2026 07:15:54 +0000 [thread overview]
Message-ID: <20261119071649.374106-3-kuniyu@google.com> (raw)
In-Reply-To: <20261119071649.374106-1-kuniyu@google.com>
tcp_collapse() starts collapsing skb to a new skb if either
of the following is true:
1. skb is less dense compared to tp->scaling_ratio
2. skb has read data before @start
However, this could make situation worse.
The new skb size is SKB_TRUESIZE(SKB_MAX_ORDER(0, 0)) at most,
and with PAGE_SIZE being 4K, its scaling_ratio is 222 (86%).
If 4K-MSS GRO skb updates tp->scaling_ratio, it can be larger
than 222.
Then, even the newly collapsed skb could be detected as bloated
by the next tcp_collapse().
Also, even when only a few bytes are read from GRO skb, it is
collapsed by the 2nd rule, making less-efficient skb.
Let's cap tp->scaling_ratio with the collapsed skb's maximum
scaling_ratio and use unread bytes for bloat detection.
Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
Reviewed-by: Eric Dumazet <edumazet@kernel.org>
---
net/ipv4/tcp_input.c | 24 ++++++++++++++++++++----
1 file changed, 20 insertions(+), 4 deletions(-)
diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
index 838d4f470c94..b4e3d623c4a2 100644
--- a/net/ipv4/tcp_input.c
+++ b/net/ipv4/tcp_input.c
@@ -5818,6 +5818,19 @@ static struct sk_buff *tcp_skb_next(struct sk_buff *skb, struct sk_buff_head *li
return skb_rb_next(skb);
}
+#define TCP_COLLAPSE_MAX_SIZE SKB_MAX_ORDER(0, 0)
+#define TCP_COLLAPSE_SCALING_RATIO \
+ ((TCP_COLLAPSE_MAX_SIZE << TCP_RMEM_TO_WIN_SCALE) / \
+ (SKB_TRUESIZE(TCP_COLLAPSE_MAX_SIZE)))
+
+static bool tcp_collapse_skb_bloated(const struct sk_buff *skb,
+ u32 start, u8 scaling_ratio)
+{
+ int unread = TCP_SKB_CB(skb)->end_seq - start;
+
+ return __tcp_win_from_space(scaling_ratio, skb->truesize) > unread;
+}
+
static struct sk_buff *tcp_collapse_one(struct sock *sk, struct sk_buff *skb,
struct sk_buff_head *list,
struct rb_root *root)
@@ -5849,6 +5862,10 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
{
struct sk_buff *skb = head, *n;
struct sk_buff_head tmp;
+ u8 scaling_ratio;
+
+ scaling_ratio = min(tcp_sk(sk)->scaling_ratio,
+ TCP_COLLAPSE_SCALING_RATIO);
/* First, check that queue is collapsible and find
* the point where collapsing can be useful.
@@ -5865,15 +5882,14 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
/* The first skb to collapse is:
* - not SYN/FIN and
- * - bloated or contains data before "start" or
+ * - the unread portion starting from "start" is bloated, or
* overlaps to the next one and mptcp allow collapsing.
*/
if (!skb_frags_readable(skb) ||
TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN))
goto skip_this;
- if (tcp_win_from_space(sk, skb->truesize) > skb->len ||
- before(TCP_SKB_CB(skb)->seq, start))
+ if (tcp_collapse_skb_bloated(skb, start, scaling_ratio))
break;
if (n && n != tail && skb_frags_readable(n) &&
@@ -5892,7 +5908,7 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
__skb_queue_head_init(&tmp);
while (before(start, end)) {
- int copy = min_t(int, SKB_MAX_ORDER(0, 0), end - start);
+ int copy = min_t(int, TCP_COLLAPSE_MAX_SIZE, end - start);
struct sk_buff *nskb;
nskb = alloc_skb(copy, GFP_ATOMIC);
--
2.56.1.708.g77570a6a1d-goog
next prev parent reply other threads:[~2026-11-19 7:16 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-19 7:15 [PATCH v3 net-next 0/7] tcp: Prevent tcp_collapse() from bloating queue Kuniyuki Iwashima
2026-11-19 7:15 ` [PATCH v3 net-next 1/7] tcp: Refactor the first loop in tcp_collapse() Kuniyuki Iwashima
2026-11-19 7:15 ` Kuniyuki Iwashima [this message]
2026-11-19 7:15 ` [PATCH v3 net-next 3/7] tcp: Stop collapsing if not bloated Kuniyuki Iwashima
2026-11-19 7:39 ` Eric Dumazet
2026-11-20 14:58 ` Matthieu Baerts
2026-11-19 7:15 ` [PATCH v3 net-next 4/7] tcp: Skip collapsing if collapsed skb is less-dense Kuniyuki Iwashima
2026-11-19 7:41 ` Eric Dumazet
2026-11-19 7:15 ` [PATCH v3 net-next 5/7] tcp: Allocate all new skb before collapsing Kuniyuki Iwashima
2026-11-19 7:44 ` Eric Dumazet
2026-11-19 20:02 ` Kuniyuki Iwashima
2026-11-19 7:15 ` [PATCH v3 net-next 6/7] tcp: Batch memory accounting in tcp_collapse() Kuniyuki Iwashima
2026-11-19 7:15 ` [PATCH v3 net-next 7/7] selftest: packetdrill: Add test for tcp_collapse() Kuniyuki Iwashima
2026-11-19 7:45 ` Eric Dumazet
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=20261119071649.374106-3-kuniyu@google.com \
--to=kuniyu@google.com \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=kuni1840@gmail.com \
--cc=martineau@kernel.org \
--cc=matttbe@kernel.org \
--cc=mptcp@lists.linux.dev \
--cc=ncardwell@google.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.