* [PATCH v2 net-next 1/7] tcp: Refactor the first loop in tcp_collapse().
2026-11-16 23:05 [PATCH v2 net-next 0/7] tcp: Prevent tcp_collapse() from bloating queue Kuniyuki Iwashima
@ 2026-11-16 23:05 ` Kuniyuki Iwashima
2026-11-16 23:05 ` [PATCH v2 net-next 2/7] tcp: Don't collapse high density skb Kuniyuki Iwashima
` (5 subsequent siblings)
6 siblings, 0 replies; 13+ messages in thread
From: Kuniyuki Iwashima @ 2026-11-16 23:05 UTC (permalink / raw)
To: Eric Dumazet, Neal Cardwell, David S . Miller, Jakub Kicinski,
Paolo Abeni, Matthieu Baerts, Mat Martineau
Cc: Simon Horman, Kuniyuki Iwashima, Kuniyuki Iwashima, netdev, mptcp
tcp_collapse() detects skb to start collapsing from in the
first loop.
If the last skb has SYN or FIN, the condition is tested
again after the loop.
skb_frags_readable() is placed inside and outside of the
loop as well.
If we goto skip_this in both cases, we can remove the second
tests and simply replace end_of_skbs with the inverted loop
condition.
Then, we do not need a test for tcp_collapse_one() too.
Also, skb_frags_readable() should be tested after the fully
overlapping check.
Let's refactor the loop.
Note that previously, when a SYN/FIN skb overlapped with the next
skb, the loop broke with end_of_skbs set to false and the check
after the loop returned without collapsing anything further.
Now such a skb is skipped instead, allowing the following skbs to
be collapsed.
Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
Reviewed-by: Eric Dumazet <edumazet@kernel.org>
---
v2: Document SYN/FIN behaviour change
---
net/ipv4/tcp_input.c | 28 ++++++++++------------------
1 file changed, 10 insertions(+), 18 deletions(-)
diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
index a2074a1af422..32e63d089db8 100644
--- a/net/ipv4/tcp_input.c
+++ b/net/ipv4/tcp_input.c
@@ -5839,23 +5839,17 @@ 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;
- bool end_of_skbs;
/* First, check that queue is collapsible and find
* the point where collapsing can be useful.
*/
restart:
- for (end_of_skbs = true; skb != NULL && skb != tail; skb = n) {
+ for (; skb && skb != tail; skb = n) {
n = tcp_skb_next(skb, list);
- if (!skb_frags_readable(skb))
- goto skip_this;
-
/* No new bits? It is possible on ofo queue. */
if (!before(start, TCP_SKB_CB(skb)->end_seq)) {
skb = tcp_collapse_one(sk, skb, list, root);
- if (!skb)
- break;
goto restart;
}
@@ -5864,27 +5858,25 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
* - bloated or contains data before "start" or
* overlaps to the next one and mptcp allow collapsing.
*/
- if (!(TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)) &&
- (tcp_win_from_space(sk, skb->truesize) > skb->len ||
- before(TCP_SKB_CB(skb)->seq, start))) {
- end_of_skbs = false;
+ 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))
break;
- }
if (n && n != tail && skb_frags_readable(n) &&
tcp_skb_can_collapse_rx(skb, n) &&
- TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(n)->seq) {
- end_of_skbs = false;
+ TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(n)->seq)
break;
- }
skip_this:
/* Decided to skip this, advance start seq. */
start = TCP_SKB_CB(skb)->end_seq;
}
- if (end_of_skbs ||
- (TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)) ||
- !skb_frags_readable(skb))
+
+ if (!skb || skb == tail)
return;
__skb_queue_head_init(&tmp);
--
2.56.0.679.gc527c65306-goog
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH v2 net-next 2/7] tcp: Don't collapse high density skb.
2026-11-16 23:05 [PATCH v2 net-next 0/7] tcp: Prevent tcp_collapse() from bloating queue Kuniyuki Iwashima
2026-11-16 23:05 ` [PATCH v2 net-next 1/7] tcp: Refactor the first loop in tcp_collapse() Kuniyuki Iwashima
@ 2026-11-16 23:05 ` Kuniyuki Iwashima
2026-11-17 23:13 ` netdev-bot+sashiko
2026-11-16 23:05 ` [PATCH v2 net-next 3/7] tcp: Stop collapsing if not bloated Kuniyuki Iwashima
` (4 subsequent siblings)
6 siblings, 1 reply; 13+ messages in thread
From: Kuniyuki Iwashima @ 2026-11-16 23:05 UTC (permalink / raw)
To: Eric Dumazet, Neal Cardwell, David S . Miller, Jakub Kicinski,
Paolo Abeni, Matthieu Baerts, Mat Martineau
Cc: Simon Horman, Kuniyuki Iwashima, Kuniyuki Iwashima, netdev, mptcp
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 32e63d089db8..e07f43347409 100644
--- a/net/ipv4/tcp_input.c
+++ b/net/ipv4/tcp_input.c
@@ -5808,6 +5808,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)
@@ -5839,6 +5852,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.
@@ -5855,15 +5872,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) &&
@@ -5882,7 +5898,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.0.679.gc527c65306-goog
^ permalink raw reply related [flat|nested] 13+ messages in thread* Re: [PATCH v2 net-next 2/7] tcp: Don't collapse high density skb.
2026-11-16 23:05 ` [PATCH v2 net-next 2/7] tcp: Don't collapse high density skb Kuniyuki Iwashima
@ 2026-11-17 23:13 ` netdev-bot+sashiko
0 siblings, 0 replies; 13+ messages in thread
From: netdev-bot+sashiko @ 2026-11-17 23:13 UTC (permalink / raw)
To: kuniyu
Cc: edumazet, ncardwell, davem, kuba, pabeni, matttbe, martineau,
horms, kuni1840, netdev, mptcp
Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The commit message describes a real regression, but the patch has
no Fixes: tag.
--
tcp: Don't collapse high density skb.
This patch caps the scaling_ratio that tcp_collapse() uses at the highest
ratio a collapsed skb can reach (222 with 4K pages). It also changes the
bloat check so it only looks at the bytes left unread after start. With
this, tcp_collapse() no longer collapses an skb it just produced, and it
no longer collapses a dense GRO skb just because a few bytes of it were
read.
> 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().
[Severity: Low]
This describes a regression in tcp_collapse(). Should the patch carry a
Fixes: tag?
The re-collapse seems to have become possible with commit dfa2f0483360
("tcp: get rid of sysctl_tcp_adv_win_scale"). That commit made
tcp_win_from_space() use the per-socket scaling_ratio. For dense skbs,
tcp_measure_rcv_mss() can raise that ratio as high as 255:
net/ipv4/tcp_input.c:tcp_measure_rcv_mss() {
...
u64 val = (u64)skb->len << TCP_RMEM_TO_WIN_SCALE;
...
do_div(val, skb->truesize);
tcp_sk(sk)->scaling_ratio = val ? val : 1;
...
}
Take a full collapsed skb (len 3776, truesize 4352) and a ratio of 230.
The old check in tcp_collapse() gives 4352 * 230 >> 8 = 3910, which is
greater than 3776. So the skb counts as bloated and is copied again on
every prune.
Before dfa2f0483360, the default adv_win_scale=1 reduced the check to
truesize / 2 > len. That gives 2176 > 3776, which is false, so collapsed
skbs never matched.
Documentation/process/maintainer-netdev.rst says the Fixes: tag is
required for fixes, regardless of the tree. Would adding the following
help with tracking?
Fixes: dfa2f0483360 ("tcp: get rid of sysctl_tcp_adv_win_scale")
If the patch is meant only as a heuristic improvement for net-next, could
the commit message say that?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261116230555.2305867-1-kuniyu%40google.com
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH v2 net-next 3/7] tcp: Stop collapsing if not bloated.
2026-11-16 23:05 [PATCH v2 net-next 0/7] tcp: Prevent tcp_collapse() from bloating queue Kuniyuki Iwashima
2026-11-16 23:05 ` [PATCH v2 net-next 1/7] tcp: Refactor the first loop in tcp_collapse() Kuniyuki Iwashima
2026-11-16 23:05 ` [PATCH v2 net-next 2/7] tcp: Don't collapse high density skb Kuniyuki Iwashima
@ 2026-11-16 23:05 ` Kuniyuki Iwashima
2026-11-17 23:13 ` netdev-bot+sashiko
2026-11-16 23:05 ` [PATCH v2 net-next 4/7] tcp: Skip collapsing if collapsed skb is less-dense Kuniyuki Iwashima
` (3 subsequent siblings)
6 siblings, 1 reply; 13+ messages in thread
From: Kuniyuki Iwashima @ 2026-11-16 23:05 UTC (permalink / raw)
To: Eric Dumazet, Neal Cardwell, David S . Miller, Jakub Kicinski,
Paolo Abeni, Matthieu Baerts, Mat Martineau
Cc: Simon Horman, Kuniyuki Iwashima, Kuniyuki Iwashima, netdev, mptcp
Even when a skb is not bloated, tcp_collapse() starts collapsing
if the next skb has overlapping data.
Also, tcp_collapse() does not stop until it reaches the tail or
unmergable skb (SYN/FIN, unreadable frags).
Assume we detect a low-density skb followed by fully-packed 4K-MSS
GRO skbs:
skb->len truesize scaling_ratio
skb #1 1448 2944 125 (49%)
skb #2 65536 66432 252 (98%)
skb #3 65536 66432 252 (98%)
...
skb #9 65536 66432 252 (98%)
--------------------------------------------
total 534400
Then, even the high-density skbs are repacked into lower-density
skbs, and we end up consuming much more memory.
skb->len truesize scaling_ratio
nskb #1 3776 4352 222 (86%)
nskb #2 3776 4352 222 (86%)
...
nskb #139 3776 4352 222 (86%)
nskb #140 872 2304 96 (37%)
--------------------------------------------
total 607232 (+72832!)
Even if we find a collapsible skb, we should not collapse
subsequent skbs with scaling_ratio >= TCP_COLLAPSE_SCALING_RATIO.
Conversely, even if tp->scaling_ratio is smaller than
TCP_COLLAPSE_SCALING_RATIO, we should repack skbs once we
start collapsing, which results in higher-density skb.
Let's remove the next skb condition in the first loop,
search for collapsible bloated skb range, and repeat
collapsing for each range.
Note that even when the next skb is not bloated, we must
collapse it for OOO rbtree if it has overlapping data.
if ((list || !before(TCP_SKB_CB(n)->seq, sub_end)) &&
!tcp_collapse_skb_bloated(n, sub_end,
TCP_COLLAPSE_SCALING_RATIO))
For example, bloated skb_1 and non-bloated skb_2 have overlapping
data and only skb_1 is collapsed to nskb_1 and nskb_2.
skb_1 = [1000, 5000]
skb_2 = [2000, 66000]
Then, nskb_2 will be inserted after skb_2 in rbtree, wrongly
setting tp->ooo_last_skb to 5000.
nskb_1 = [1000, 4776]
skb_2 = [2000, 66000]
nskb_2 = [4776, 5000]
This case is still inefficient, but a later patch handles this.
Another notable thing is that tcp_skb_can_collapse_rx() now checks
candidates against the first skb before mptcp_skb_ext_move(),
not against each nskb.
When a skb with an MPTCP ext spans multiple nskbs, collapsing
previously stopped at the next skb with the same ext because later
nskbs lacked the ext.
skb1 (ext A), skb2 (ext A)
nskb1 (ext A), nskb2 (no ext) ^
^- not collapsible -'
Now such skbs are also collapsed with the redundant ext dropped,
while the first nskb still carries the same mapping in sequence
order.
skb1 (ext A), skb2 (ext A)
nskb1 (ext A), nskb2 (no ext) <-' collapsed into nskb2
Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
---
v2: Describe slight behavioural change for MPTCP
---
net/ipv4/tcp_input.c | 66 +++++++++++++++++++++++++-------------------
1 file changed, 37 insertions(+), 29 deletions(-)
diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
index e07f43347409..5bd42a74c6f5 100644
--- a/net/ipv4/tcp_input.c
+++ b/net/ipv4/tcp_input.c
@@ -5839,7 +5839,7 @@ static struct sk_buff *tcp_collapse_one(struct sock *sk, struct sk_buff *skb,
}
/* Collapse contiguous sequence of skbs head..tail with
- * sequence numbers start..end.
+ * sequence numbers start..sub_end.
*
* If tail is NULL, this means until the end of the queue.
*
@@ -5848,15 +5848,18 @@ static struct sk_buff *tcp_collapse_one(struct sock *sk, struct sk_buff *skb,
*/
static void
tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
- struct sk_buff *head, struct sk_buff *tail, u32 start, u32 end)
+ struct sk_buff *head, struct sk_buff *tail, u32 start)
{
struct sk_buff *skb = head, *n;
struct sk_buff_head tmp;
u8 scaling_ratio;
+ u32 sub_end;
scaling_ratio = min(tcp_sk(sk)->scaling_ratio,
TCP_COLLAPSE_SCALING_RATIO);
+ __skb_queue_head_init(&tmp);
+
/* First, check that queue is collapsible and find
* the point where collapsing can be useful.
*/
@@ -5872,38 +5875,48 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
/* The first skb to collapse is:
* - not SYN/FIN and
- * - the unread portion starting from "start" is bloated, or
- * overlaps to the next one and mptcp allow collapsing.
+ * - the unread portion starting from "start" is bloated.
*/
- if (!skb_frags_readable(skb) ||
- TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN))
- goto skip_this;
-
- if (tcp_collapse_skb_bloated(skb, start, scaling_ratio))
- break;
-
- if (n && n != tail && skb_frags_readable(n) &&
- tcp_skb_can_collapse_rx(skb, n) &&
- TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(n)->seq)
+ if (skb_frags_readable(skb) &&
+ !(TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)) &&
+ tcp_collapse_skb_bloated(skb, start, scaling_ratio))
break;
-skip_this:
/* Decided to skip this, advance start seq. */
start = TCP_SKB_CB(skb)->end_seq;
}
if (!skb || skb == tail)
- return;
+ goto end;
- __skb_queue_head_init(&tmp);
+ /* Second, find consecutive collapsible skb. */
+ sub_end = TCP_SKB_CB(skb)->end_seq;
+
+ for (n = tcp_skb_next(skb, list); n && n != tail; n = tcp_skb_next(n, list)) {
+ if (!skb_frags_readable(n) ||
+ TCP_SKB_CB(n)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN) ||
+ !tcp_skb_can_collapse_rx(skb, n))
+ break;
+
+ if (!after(TCP_SKB_CB(n)->end_seq, sub_end))
+ continue;
+
+ /* 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))
+ break;
- while (before(start, end)) {
- int copy = min_t(int, TCP_COLLAPSE_MAX_SIZE, end - start);
+ sub_end = TCP_SKB_CB(n)->end_seq;
+ }
+
+ 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);
if (!nskb)
- break;
+ goto end;
memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
TCP_SKB_CB(nskb)->has_rxtstamp = false;
@@ -5942,15 +5955,11 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
}
if (!before(start, TCP_SKB_CB(skb)->end_seq)) {
skb = tcp_collapse_one(sk, skb, list, root);
- if (!skb ||
- skb == tail ||
- !tcp_skb_can_collapse_rx(nskb, skb) ||
- (TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)) ||
- !skb_frags_readable(skb))
- goto end;
}
}
}
+
+ goto restart;
end:
skb_queue_walk_safe(&tmp, skb, n)
tcp_rbtree_insert(root, skb);
@@ -5989,7 +5998,7 @@ static void tcp_collapse_ofo_queue(struct sock *sk)
if (range_truesize != head->truesize ||
end - start >= SKB_WITH_OVERHEAD(PAGE_SIZE)) {
tcp_collapse(sk, NULL, &tp->out_of_order_queue,
- head, skb, start, end);
+ head, skb, start);
} else {
sum_tiny += range_truesize;
if (sum_tiny > sk->sk_rcvbuf >> 3)
@@ -6103,8 +6112,7 @@ static int tcp_prune_queue(struct sock *sk, const struct sk_buff *in_skb)
if (!skb_queue_empty(&sk->sk_receive_queue))
tcp_collapse(sk, &sk->sk_receive_queue, NULL,
skb_peek(&sk->sk_receive_queue),
- NULL,
- tp->copied_seq, tp->rcv_nxt);
+ NULL, tp->copied_seq);
if (tcp_can_ingest(sk, in_skb))
return 0;
--
2.56.0.679.gc527c65306-goog
^ permalink raw reply related [flat|nested] 13+ messages in thread* Re: [PATCH v2 net-next 3/7] tcp: Stop collapsing if not bloated.
2026-11-16 23:05 ` [PATCH v2 net-next 3/7] tcp: Stop collapsing if not bloated Kuniyuki Iwashima
@ 2026-11-17 23:13 ` netdev-bot+sashiko
0 siblings, 0 replies; 13+ messages in thread
From: netdev-bot+sashiko @ 2026-11-17 23:13 UTC (permalink / raw)
To: kuniyu
Cc: edumazet, ncardwell, davem, kuba, pabeni, matttbe, martineau,
horms, kuni1840, netdev, mptcp
Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The header comment of tcp_collapse() in net/ipv4/tcp_input.c now
reads "Collapse contiguous sequence of skbs head..tail with sequence…
--
tcp: Stop collapsing if not bloated.
With this change, tcp_collapse() starts collapsing only at a bloated skb.
It extends the range over consecutive collapsible skbs that are bloated
or overlapping, copies that sub-range, and then restarts to look for the
next one. The end parameter is dropped from tcp_collapse(), and both
callers are updated.
> diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
> index 598d4ff3e1f4b..d2d4796984f39 100644
> --- a/net/ipv4/tcp_input.c
> +++ b/net/ipv4/tcp_input.c
> @@ -5839,7 +5839,7 @@ static struct sk_buff *tcp_collapse_one(struct sock *sk, struct sk_buff *skb,
> }
>
> /* Collapse contiguous sequence of skbs head..tail with
> - * sequence numbers start..end.
> + * sequence numbers start..sub_end.
[Severity: Low]
This isn't a bug, but could the header comment of tcp_collapse() be
reworded?
sub_end is not a parameter. It is a local variable that the second loop
recomputes for each sub-range, so callers never see it.
The comment also still describes collapsing a single contiguous range.
After this patch, one call can collapse several separate sub-ranges
between head and tail, with uncollapsed skbs left between them:
/* Second, find consecutive collapsible skb. */
sub_end = TCP_SKB_CB(skb)->end_seq;
...
goto restart;
The comment stays the same through the rest of the series, up to
"selftest: packetdrill: Add test for tcp_collapse()."
> *
> * If tail is NULL, this means until the end of the queue.
> *
[ ... ]
> + /* Second, find consecutive collapsible skb. */
> + sub_end = TCP_SKB_CB(skb)->end_seq;
> +
> + for (n = tcp_skb_next(skb, list); n && n != tail; n = tcp_skb_next(n, list)) {
[ ... ]
> + sub_end = TCP_SKB_CB(n)->end_seq;
> + }
[ ... ]
> +
> + goto restart;
> end:
> skb_queue_walk_safe(&tmp, skb, n)
> tcp_rbtree_insert(root, skb);
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261116230555.2305867-1-kuniyu%40google.com
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH v2 net-next 4/7] tcp: Skip collapsing if collapsed skb is less-dense.
2026-11-16 23:05 [PATCH v2 net-next 0/7] tcp: Prevent tcp_collapse() from bloating queue Kuniyuki Iwashima
` (2 preceding siblings ...)
2026-11-16 23:05 ` [PATCH v2 net-next 3/7] tcp: Stop collapsing if not bloated Kuniyuki Iwashima
@ 2026-11-16 23:05 ` Kuniyuki Iwashima
2026-11-17 23:13 ` netdev-bot+sashiko
2026-11-16 23:05 ` [PATCH v2 net-next 5/7] tcp: Allocate all new skb before collapsing Kuniyuki Iwashima
` (2 subsequent siblings)
6 siblings, 1 reply; 13+ messages in thread
From: Kuniyuki Iwashima @ 2026-11-16 23:05 UTC (permalink / raw)
To: Eric Dumazet, Neal Cardwell, David S . Miller, Jakub Kicinski,
Paolo Abeni, Matthieu Baerts, Mat Martineau
Cc: Simon Horman, Kuniyuki Iwashima, Kuniyuki Iwashima, netdev, mptcp
Once tcp_collapse() finds a collapsible skb, it collapses
consecutive bloated skbs.
However, the end result could be the same or larger than
as is in some cases:
1. skb is already minimum
If SKB_HEAD_ALIGN(len) <= SKB_SMALL_HEAD_CACHE_SIZE,
skb is allocated from skbuff_small_head, and skb->truesize
never changes.
2. SKB_HEAD_ALIGN() and kmalloc_size_roundup() in alloc_skb()
Assume two bloated skbs with len = 1448 B (truesize =
2304 B, scaling_ratio = 160) and len = 300 B (truesize =
896 B, scaling_ratio = 85), whose total truesize is
3200 B.
Collapsing them calls alloc_skb(1748), which rounds up
SKB_HEAD_ALIGN(1748) = 2112 B to 4096 B and allocates a
new skb with truesize = 4352 B (+1152 B!).
Even worse, in case 1, such a skb is reallocated repeatedly
by every tcp_collapse() in vain.
Let's calculate the final collapsed truesize before collapsing
skbs and skip the range if it does not shrink.
We could allocate skbs first and calculate the new truesize,
but given tcp_collapse() is called under memory pressure,
we want to avoid unnecessary allocation.
Note that kmalloc_reserve_size() uses __always_inline so that
this change does not affect non-FDO builds, and returns 0 when
skb_small_head_cache is used, similar to kmalloc_pfmemalloc().
Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
---
v2: Add compute_skb_truesize() instead of macro
---
include/linux/skbuff.h | 2 ++
net/core/skbuff.c | 26 +++++++++++++++++++++++---
net/ipv4/tcp_input.c | 24 +++++++++++++++++++++++-
3 files changed, 48 insertions(+), 4 deletions(-)
diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h
index 1d6832878930..310e4881045b 100644
--- a/include/linux/skbuff.h
+++ b/include/linux/skbuff.h
@@ -274,6 +274,8 @@
SKB_DATA_ALIGN(sizeof(struct sk_buff)) + \
SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
+unsigned int compute_skb_truesize(unsigned int size, gfp_t flags);
+
struct net_device;
struct scatterlist;
struct pipe_inode_info;
diff --git a/net/core/skbuff.c b/net/core/skbuff.c
index 2e81aacc998b..a89eaadc729f 100644
--- a/net/core/skbuff.c
+++ b/net/core/skbuff.c
@@ -589,6 +589,17 @@ struct sk_buff *napi_build_skb(void *data, unsigned int frag_size)
}
EXPORT_SYMBOL(napi_build_skb);
+static __always_inline size_t kmalloc_reserve_size(unsigned int size, gfp_t flags)
+{
+ size_t obj_size = SKB_HEAD_ALIGN(size);
+
+ if (obj_size <= SKB_SMALL_HEAD_CACHE_SIZE &&
+ !(flags & KMALLOC_NOT_NORMAL_BITS))
+ return 0;
+
+ return kmalloc_size_roundup(obj_size);
+}
+
static void *kmalloc_pfmemalloc(size_t obj_size, gfp_t flags, int node)
{
if (!gfp_pfmemalloc_allowed(flags))
@@ -612,9 +623,8 @@ static void *kmalloc_reserve(unsigned int *size, gfp_t flags, int node,
size_t obj_size;
void *obj;
- obj_size = SKB_HEAD_ALIGN(*size);
- if (obj_size <= SKB_SMALL_HEAD_CACHE_SIZE &&
- !(flags & KMALLOC_NOT_NORMAL_BITS)) {
+ obj_size = kmalloc_reserve_size(*size, flags);
+ if (!obj_size) {
obj = kmem_cache_alloc_node(net_hotdata.skb_small_head_cache,
flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
node);
@@ -651,6 +661,16 @@ static void *kmalloc_reserve(unsigned int *size, gfp_t flags, int node,
return obj;
}
+unsigned int compute_skb_truesize(unsigned int size, gfp_t flags)
+{
+ size_t obj_size = kmalloc_reserve_size(size, flags);
+
+ if (!obj_size)
+ obj_size = SKB_SMALL_HEAD_CACHE_SIZE;
+
+ return obj_size + SKB_DATA_ALIGN(sizeof(struct sk_buff));
+}
+
/* Allocate a new skbuff. We do this ourselves so we can fill in a few
* 'private' fields and also do memory statistics to find all the
* [BEEP] leaks.
diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
index 5bd42a74c6f5..0815e1891fba 100644
--- a/net/ipv4/tcp_input.c
+++ b/net/ipv4/tcp_input.c
@@ -5850,8 +5850,10 @@ static void
tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
struct sk_buff *head, struct sk_buff *tail, u32 start)
{
+ int old_truesize, new_truesize;
struct sk_buff *skb = head, *n;
struct sk_buff_head tmp;
+ int remainder, nskbs;
u8 scaling_ratio;
u32 sub_end;
@@ -5891,6 +5893,7 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
/* Second, find consecutive collapsible skb. */
sub_end = TCP_SKB_CB(skb)->end_seq;
+ old_truesize = skb->truesize;
for (n = tcp_skb_next(skb, list); n && n != tail; n = tcp_skb_next(n, list)) {
if (!skb_frags_readable(n) ||
@@ -5898,8 +5901,10 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
!tcp_skb_can_collapse_rx(skb, n))
break;
- if (!after(TCP_SKB_CB(n)->end_seq, sub_end))
+ if (!after(TCP_SKB_CB(n)->end_seq, sub_end)) {
+ old_truesize += n->truesize;
continue;
+ }
/* seq and end_seq must increase monotonically for rbtree. */
if ((list || !before(TCP_SKB_CB(n)->seq, sub_end)) &&
@@ -5908,6 +5913,23 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
break;
sub_end = TCP_SKB_CB(n)->end_seq;
+ old_truesize += n->truesize;
+ }
+
+ /* Third, check if the new skbs have high density. */
+ nskbs = (sub_end - start) / TCP_COLLAPSE_MAX_SIZE;
+ new_truesize = nskbs * SKB_TRUESIZE(TCP_COLLAPSE_MAX_SIZE);
+
+ remainder = (sub_end - start) % TCP_COLLAPSE_MAX_SIZE;
+ if (remainder) {
+ nskbs++;
+ new_truesize += compute_skb_truesize(remainder, GFP_ATOMIC);
+ }
+
+ if (old_truesize <= new_truesize) {
+ start = sub_end;
+ skb = n;
+ goto restart;
}
while (before(start, sub_end)) {
--
2.56.0.679.gc527c65306-goog
^ permalink raw reply related [flat|nested] 13+ messages in thread* Re: [PATCH v2 net-next 4/7] tcp: Skip collapsing if collapsed skb is less-dense.
2026-11-16 23:05 ` [PATCH v2 net-next 4/7] tcp: Skip collapsing if collapsed skb is less-dense Kuniyuki Iwashima
@ 2026-11-17 23:13 ` netdev-bot+sashiko
0 siblings, 0 replies; 13+ messages in thread
From: netdev-bot+sashiko @ 2026-11-17 23:13 UTC (permalink / raw)
To: kuniyu
Cc: edumazet, ncardwell, davem, kuba, pabeni, matttbe, martineau,
horms, kuni1840, netdev, mptcp
Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 2
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] tcp: rbtree order corruption in tcp_collapse() on skipped
overlapping skb
- [Low] kmalloc_reserve() in net/core/skbuff.c now rounds the skb head
size up twice.
- [Low] The concern says the local `nskbs` in tcp_collapse()
(net/ipv4/tcp_input.c) is set but never read in this patch, and that…
--
tcp: Skip collapsing if collapsed skb is less-dense.
This patch makes tcp_collapse() estimate the truesize of the collapsed
skbs before allocating them. If the result would not be smaller than the
original skbs, the range is skipped. To support this, the head size
calculation in kmalloc_reserve() is moved into a new kmalloc_reserve_size()
helper, and compute_skb_truesize() is added.
> diff --git a/net/core/skbuff.c b/net/core/skbuff.c
> index 2e81aacc998b6..a89eaadc729f4 100644
> --- a/net/core/skbuff.c
> +++ b/net/core/skbuff.c
> @@ -589,6 +589,17 @@ struct sk_buff *napi_build_skb(void *data, unsigned int frag_size)
> }
> EXPORT_SYMBOL(napi_build_skb);
>
> +static __always_inline size_t kmalloc_reserve_size(unsigned int size, gfp_t flags)
> +{
> + size_t obj_size = SKB_HEAD_ALIGN(size);
> +
> + if (obj_size <= SKB_SMALL_HEAD_CACHE_SIZE &&
> + !(flags & KMALLOC_NOT_NORMAL_BITS))
> + return 0;
> +
> + return kmalloc_size_roundup(obj_size);
> +}
> +
[ ... ]
> @@ -612,9 +623,8 @@ static void *kmalloc_reserve(unsigned int *size, gfp_t flags, int node,
> size_t obj_size;
> void *obj;
>
> - obj_size = SKB_HEAD_ALIGN(*size);
> - if (obj_size <= SKB_SMALL_HEAD_CACHE_SIZE &&
> - !(flags & KMALLOC_NOT_NORMAL_BITS)) {
> + obj_size = kmalloc_reserve_size(*size, flags);
> + if (!obj_size) {
> obj = kmem_cache_alloc_node(net_hotdata.skb_small_head_cache,
> flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
> node);
[Severity: Low]
Does kmalloc_reserve() still need its own kmalloc_size_roundup() call?
kmalloc_reserve_size() already returns the rounded-up size. The unchanged
code after the small head branch rounds it a second time:
net/core/skbuff.c:kmalloc_reserve() {
...
obj_size = kmalloc_size_roundup(obj_size);
/* The following cast might truncate high-order bits of obj_size, this
* is harmless because kmalloc(obj_size >= 2^32) will fail anyway.
*/
*size = (unsigned int)obj_size;
...
}
The result is the same because the roundup is idempotent. However,
kmalloc_size_roundup() is an out-of-line function in mm/slab_common.c.
So every alloc_skb() head larger than SKB_SMALL_HEAD_CACHE_SIZE now does
the slab lookup twice.
That doesn't seem to match this part of the commit message:
Note that kmalloc_reserve_size() uses __always_inline so that
this change does not affect non-FDO builds, ...
Could the leftover obj_size = kmalloc_size_roundup(obj_size) line in
kmalloc_reserve() be removed? The duplicate call is still there at the
end of the series.
> diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
> index d2d4796984f39..92a4c978f9309 100644
> --- a/net/ipv4/tcp_input.c
> +++ b/net/ipv4/tcp_input.c
[ ... ]
> @@ -5908,6 +5913,23 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
> break;
>
> sub_end = TCP_SKB_CB(n)->end_seq;
> + old_truesize += n->truesize;
> + }
> +
> + /* Third, check if the new skbs have high density. */
> + nskbs = (sub_end - start) / TCP_COLLAPSE_MAX_SIZE;
> + new_truesize = nskbs * SKB_TRUESIZE(TCP_COLLAPSE_MAX_SIZE);
> +
> + remainder = (sub_end - start) % TCP_COLLAPSE_MAX_SIZE;
> + if (remainder) {
> + nskbs++;
[Severity: Medium]
Can this skip leave the out_of_order_queue rbtree out of order?
On the ofo path (list == NULL), new nskbs are only queued on tmp and
inserted into root at the end label. The second loop can also stop at
an n that overlaps the current range, because tcp_skb_can_collapse_rx()
is checked before the monotonic seq check. Examples are MPTCP skbs
with different mappings, or skbs with different decrypted bits.
Say the ofo queue has skb_1 [1000, 5000) and an overlapping skb_2
[4500, 5100), and tcp_skb_can_collapse_rx(skb_1, skb_2) is false.
skb_1 is collapsed into nskbs on tmp, the last one covering
[4776, 5000). We then restart at skb_2 with start = 5000.
skb_2 is bloated for its 100 unread bytes. Before this patch it was
collapsed into a new [5000, 5100) skb and erased from root. Now
compute_skb_truesize(100, GFP_ATOMIC) returns the small head
truesize, which equals skb_2->truesize. So the range is skipped and
skb_2 stays in root with seq 4500.
At the end label, tcp_rbtree_insert() orders by seq only. The
[4776, 5000) nskb is placed to the right of skb_2 [4500, 5100), so
end_seq is no longer monotonic.
tcp_collapse_ofo_queue() then sets tp->ooo_last_skb to that nskb
(end_seq 5000) instead of skb_2. As a result, tcp_data_queue_ofo()
takes the fast path for segments in [5000, 5100) without checking the
overlap with skb_2. tcp_ofo_queue() later drops the nskb once skb_2
advances rcv_nxt.
A remote peer can trigger this with overlapping out-of-order
segments while the socket is under receive memory pressure.
Should the skip be avoided on the rbtree when the skb starts before
start, i.e. it overlaps data already collapsed into tmp? Or is there
a better way to keep the ordering intact in that case?
[Severity: Low]
This isn't a bug, but nothing reads nskbs after this nskbs++ in
tcp_collapse(), so the increment is a dead store in this patch. nskbs is
read earlier to compute new_truesize, so the compiler does not warn.
The next patch in the series ("tcp: Allocate all new skb before
collapsing.") reads the incremented value in its pre-allocation loop, so
the series resolves this. Would it be cleaner to add the increment in
that patch instead?
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261116230555.2305867-1-kuniyu%40google.com
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH v2 net-next 5/7] tcp: Allocate all new skb before collapsing.
2026-11-16 23:05 [PATCH v2 net-next 0/7] tcp: Prevent tcp_collapse() from bloating queue Kuniyuki Iwashima
` (3 preceding siblings ...)
2026-11-16 23:05 ` [PATCH v2 net-next 4/7] tcp: Skip collapsing if collapsed skb is less-dense Kuniyuki Iwashima
@ 2026-11-16 23:05 ` Kuniyuki Iwashima
2026-11-17 23:13 ` netdev-bot+sashiko
2026-11-16 23:05 ` [PATCH v2 net-next 6/7] tcp: Batch memory accounting in tcp_collapse() Kuniyuki Iwashima
2026-11-16 23:05 ` [PATCH v2 net-next 7/7] selftest: packetdrill: Add test for tcp_collapse() Kuniyuki Iwashima
6 siblings, 1 reply; 13+ messages in thread
From: Kuniyuki Iwashima @ 2026-11-16 23:05 UTC (permalink / raw)
To: Eric Dumazet, Neal Cardwell, David S . Miller, Jakub Kicinski,
Paolo Abeni, Matthieu Baerts, Mat Martineau
Cc: Simon Horman, Kuniyuki Iwashima, Kuniyuki Iwashima, netdev, mptcp
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;
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);
+ }
+
+ while (before(start, sub_end)) {
+ int copy = min_t(int, TCP_COLLAPSE_MAX_SIZE, sub_end - start);
+
+ nskb = __skb_dequeue(&pool);
memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
TCP_SKB_CB(nskb)->has_rxtstamp = false;
skb_copy_decrypted(nskb, skb);
@@ -5985,6 +5996,8 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
end:
skb_queue_walk_safe(&tmp, skb, n)
tcp_rbtree_insert(root, skb);
+
+ __skb_queue_purge_reason(&pool, SKB_CONSUMED);
}
/* Collapse ofo queue. Algorithm: select contiguous sequence of skbs
--
2.56.0.679.gc527c65306-goog
^ permalink raw reply related [flat|nested] 13+ messages in thread* Re: [PATCH v2 net-next 5/7] tcp: Allocate all new skb before collapsing.
2026-11-16 23:05 ` [PATCH v2 net-next 5/7] tcp: Allocate all new skb before collapsing Kuniyuki Iwashima
@ 2026-11-17 23:13 ` netdev-bot+sashiko
0 siblings, 0 replies; 13+ messages in thread
From: netdev-bot+sashiko @ 2026-11-17 23:13 UTC (permalink / raw)
To: kuniyu
Cc: edumazet, ncardwell, davem, kuba, pabeni, matttbe, martineau,
horms, kuni1840, netdev, mptcp
Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 2
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] This patch makes tcp_collapse() (net/ipv4/tcp_input.c)
allocate every replacement skb for a section up front with GFP_ATOMIC,…
- [Low] The commit message says the old alloc_skb() failure path only
left duplicate skbs that waste memory.
- [Low] In tcp_collapse() (net/ipv4/tcp_input.c), this patch adds a
second job to the second-loop break condition: stop once…
--
tcp: Allocate all new skb before collapsing.
This commit makes tcp_collapse() allocate every replacement skb for a
section into a temporary pool before it copies any data. An alloc_skb()
failure then no longer leaves partially copied originals and duplicate
new skbs behind. Sections are capped at TCP_COLLAPSE_MAX_TOTAL_SIZE (16
skbs), except for a single bloated skb or overlapping ofo skbs.
> 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.
[Severity: Low]
The code change looks right. For the ofo rbtree case, though, is this
description of the old failure path complete?
In the old code, a failed alloc_skb() jumped to end:. That path inserted
every deferred nskb from tmp with tcp_rbtree_insert() while the partially
copied original was still in the tree. tcp_rbtree_insert() sends equal
seq to the right:
if (before(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb1)->seq))
p = &parent->rb_left;
else
p = &parent->rb_right;
So the first nskb sorts after the original even though its end_seq is
smaller. Its seq is equal to or after the original's seq.
If this happens at the tail of the ofo queue, tcp_collapse_ofo_queue()
sets tp->ooo_last_skb = skb_rb_last(). That points at an skb whose
end_seq is below data already queued, which breaks the assumption behind
the tcp_data_queue_ofo() fast path:
if (!before(seq, TCP_SKB_CB(tp->ooo_last_skb)->end_seq)) {
This patch fixes that as well, because no partial copy is left behind.
Could the commit message mention the restored ordering invariant, so
stable backporters can see it?
> diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
> index 92a4c978f9309..afc3c69eba781 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)
[Severity: Low]
This isn't a bug, but would a short comment help here? Neither this macro
nor the break condition in tcp_collapse() says that the cap:
- bounds the all-or-nothing preallocation into pool,
- is measured in sequence space from start, and
- is skipped for the first skb and for overlapping ofo skbs.
The only comment above the condition is still:
/* seq and end_seq must increase monotonically for rbtree. */
and it only covers the overlap exemption.
[ ... ]
> @@ -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;
>
> sub_end = TCP_SKB_CB(n)->end_seq;
[ ... ]
> @@ -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);
> + }
[Severity: Medium]
Can the batch allocated here grow far beyond TCP_COLLAPSE_MAX_TOTAL_SIZE
for the ofo rbtree?
When list is NULL and n overlaps the current section,
before(TCP_SKB_CB(n)->seq, sub_end) is true. The whole break condition in
the second loop is then false. Neither the new cap nor the bloat test
runs, and n is always added to the section.
The overlap chain appears to be under the remote peer's control.
tcp_data_queue_ofo() keeps partially overlapping segments:
if (after(seq, TCP_SKB_CB(skb1)->seq)) {
/* Partial overlap. */
tcp_dsack_set(sk, seq, TCP_SKB_CB(skb1)->end_seq);
tcp_try_coalesce() refuses them because from->seq != to->end_seq.
tcp_collapse_ofo_queue() only ends a range at a gap:
if (!skb ||
after(TCP_SKB_CB(skb)->seq, end) ||
before(TCP_SKB_CB(skb)->end_seq, start)) {
A sender could send a long chain of small ofo segments, each overlapping
the previous one by one byte. That gives one tcp_collapse() section
covering the whole ofo queue. Small segments count as bloated, so they
also pass the density check.
This loop would then allocate about (sub_end - start) /
TCP_COLLAPSE_MAX_SIZE GFP_ATOMIC skbs at once. Every original skb is
still queued at that point. The pool is not charged to sk_rmem_alloc
until each nskb is dequeued. That stays true after the later patch
"tcp: Batch memory accounting in tcp_collapse()".
Before this patch, originals were freed as they were consumed, so the
extra memory at peak was about one new skb. Now the peak per socket is
close to old_truesize + new_truesize, limited only by sk_rcvbuf. Is that
intended?
There is also the failure case. One alloc_skb() failure discards the
whole batch. Under memory pressure, could a queue shaped this way never
make collapse progress?
The commit message says the cap bounds the temporary allocation and
treats overlapping OOO skbs as an exception. That exception seems to be
fully under the remote sender's control. The preallocation loop is also
unchanged at the end of the series.
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261116230555.2305867-1-kuniyu%40google.com
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH v2 net-next 6/7] tcp: Batch memory accounting in tcp_collapse().
2026-11-16 23:05 [PATCH v2 net-next 0/7] tcp: Prevent tcp_collapse() from bloating queue Kuniyuki Iwashima
` (4 preceding siblings ...)
2026-11-16 23:05 ` [PATCH v2 net-next 5/7] tcp: Allocate all new skb before collapsing Kuniyuki Iwashima
@ 2026-11-16 23:05 ` Kuniyuki Iwashima
2026-11-16 23:05 ` [PATCH v2 net-next 7/7] selftest: packetdrill: Add test for tcp_collapse() Kuniyuki Iwashima
6 siblings, 0 replies; 13+ messages in thread
From: Kuniyuki Iwashima @ 2026-11-16 23:05 UTC (permalink / raw)
To: Eric Dumazet, Neal Cardwell, David S . Miller, Jakub Kicinski,
Paolo Abeni, Matthieu Baerts, Mat Martineau
Cc: Simon Horman, Kuniyuki Iwashima, Kuniyuki Iwashima, netdev, mptcp
Once tcp_collapse_one() calls sock_rfree(), skb->truesize is
reclaimed to sk->sk_forward_alloc by sk_mem_uncharge().
However, sk_mem_uncharge() rounds down sk->sk_forward_alloc
immediately once it reaches 4K since commit 4890b686f408 ("net:
keep sk->sk_forward_alloc as small as possible").
This unnecessarily reclaims memory for newly allocated skbs,
and the following skb_set_owner_r() for them underflows
sk->sk_forward_alloc without sk_forced_mem_schedule().
Let's cache the delta during tcp_collapse() and batch accounting
at the end.
Then, we no longer need sk_forced_mem_schedule(), and instead
DEBUG_NET_WARN_ON_ONCE() is added to catch a future issue.
Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
Reviewed-by: Eric Dumazet <edumazet@kernel.org>
---
net/ipv4/tcp_input.c | 34 +++++++++++++++++++++++++++++-----
1 file changed, 29 insertions(+), 5 deletions(-)
diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
index ca4cdb93373d..713dcabd88b4 100644
--- a/net/ipv4/tcp_input.c
+++ b/net/ipv4/tcp_input.c
@@ -5822,6 +5822,24 @@ static bool tcp_collapse_skb_bloated(const struct sk_buff *skb,
return __tcp_win_from_space(scaling_ratio, skb->truesize) > unread;
}
+static int tcp_collapse_uncharge(struct sk_buff *skb)
+{
+ DEBUG_NET_WARN_ON_ONCE(skb->destructor != sock_rfree);
+
+ skb->sk = NULL;
+ skb->destructor = NULL;
+
+ return skb->truesize;
+}
+
+static int tcp_collapse_charge(struct sk_buff *skb, struct sock *sk)
+{
+ skb->sk = sk;
+ skb->destructor = sock_rfree;
+
+ return -skb->truesize;
+}
+
static struct sk_buff *tcp_collapse_one(struct sock *sk, struct sk_buff *skb,
struct sk_buff_head *list,
struct rb_root *root)
@@ -5857,6 +5875,7 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
int remainder, nskbs;
struct sk_buff *nskb;
u8 scaling_ratio;
+ int released = 0;
u32 sub_end;
scaling_ratio = min(tcp_sk(sk)->scaling_ratio,
@@ -5874,6 +5893,7 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
/* No new bits? It is possible on ofo queue. */
if (!before(start, TCP_SKB_CB(skb)->end_seq)) {
+ released += tcp_collapse_uncharge(skb);
skb = tcp_collapse_one(sk, skb, list, root);
goto restart;
}
@@ -5959,11 +5979,8 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
__skb_queue_before(list, skb, nskb);
else
__skb_queue_tail(&tmp, nskb); /* defer rbtree insertion */
- /* nskb is charged before the skbs it replaces are freed.
- * Make sure sk->sk_forward_alloc can not become negative.
- */
- sk_forced_mem_schedule(sk, nskb->truesize);
- skb_set_owner_r(nskb, sk);
+
+ released += tcp_collapse_charge(nskb, sk);
mptcp_skb_ext_move(nskb, skb);
/* Copy data, releasing collapsed skbs. */
@@ -5987,6 +6004,7 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
start += size;
}
if (!before(start, TCP_SKB_CB(skb)->end_seq)) {
+ released += tcp_collapse_uncharge(skb);
skb = tcp_collapse_one(sk, skb, list, root);
}
}
@@ -5998,6 +6016,12 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
tcp_rbtree_insert(root, skb);
__skb_queue_purge_reason(&pool, SKB_CONSUMED);
+
+ if (released) {
+ DEBUG_NET_WARN_ON_ONCE(released < 0);
+ atomic_sub(released, &sk->sk_rmem_alloc);
+ sk_mem_uncharge(sk, released);
+ }
}
/* Collapse ofo queue. Algorithm: select contiguous sequence of skbs
--
2.56.0.679.gc527c65306-goog
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH v2 net-next 7/7] selftest: packetdrill: Add test for tcp_collapse().
2026-11-16 23:05 [PATCH v2 net-next 0/7] tcp: Prevent tcp_collapse() from bloating queue Kuniyuki Iwashima
` (5 preceding siblings ...)
2026-11-16 23:05 ` [PATCH v2 net-next 6/7] tcp: Batch memory accounting in tcp_collapse() Kuniyuki Iwashima
@ 2026-11-16 23:05 ` Kuniyuki Iwashima
2026-11-17 23:13 ` netdev-bot+sashiko
6 siblings, 1 reply; 13+ messages in thread
From: Kuniyuki Iwashima @ 2026-11-16 23:05 UTC (permalink / raw)
To: Eric Dumazet, Neal Cardwell, David S . Miller, Jakub Kicinski,
Paolo Abeni, Matthieu Baerts, Mat Martineau
Cc: Simon Horman, Kuniyuki Iwashima, Kuniyuki Iwashima, netdev, mptcp
tcp_collapse() now does not collapse high-density skbs and a
low-density skb sandwiched by high-density skbs.
tcp_rcv_collapse_bloated.pkt verifies the behaviour by
1. set a large SO_RCVBUF
2. send one high-density skb with 100 bytes read()
3. send one high-density skb
4. send one low-density skb
5. send one high-density skb
6. send 30 low-density skbs
7. send one high-density skb
8. set a small SO_RCVBUF
9. send a small data to trigger tcp_collapse()
10. check TcpExtTCPRcvCollapsed via nstat
Without the series, the first high-density skb is collapsed.
In addition, the first low-density skb drags the following
high-density skb into collapsing, consuming more memory.
If we remove sk_forced_mem_schedule() without this series,
sk->sk_forward_alloc becomes negative at the end of tcp_collapse().
Note that 30 segments are to make the memory savings large
enough so that the margin can absorb minor size delta of
struct sk_buff or L1_CACHE_BYTES across configs.
skb truesize, TCP_COLLAPSE_MAX_SIZE, TCP_COLLAPSE_SCALING_RATIO
depend on PAGE_SIZE being 4K, so the test is skipped if getconf
returns non-4K PAGE_SIZE.
Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
---
.../selftests/net/packetdrill/ksft_runner.sh | 9 ++
.../packetdrill/tcp_rcv_collapse_bloated.pkt | 135 ++++++++++++++++++
2 files changed, 144 insertions(+)
create mode 100644 tools/testing/selftests/net/packetdrill/tcp_rcv_collapse_bloated.pkt
diff --git a/tools/testing/selftests/net/packetdrill/ksft_runner.sh b/tools/testing/selftests/net/packetdrill/ksft_runner.sh
index 0a97d5ae3469..250d143bb9f7 100755
--- a/tools/testing/selftests/net/packetdrill/ksft_runner.sh
+++ b/tools/testing/selftests/net/packetdrill/ksft_runner.sh
@@ -44,6 +44,15 @@ if [ -z "$(which packetdrill)" ]; then
exit "$KSFT_SKIP"
fi
+page_size=$(grep -E '^--define="?PAGE_SIZE=' $script | sed -E 's/.*PAGE_SIZE="?([0-9]+)"?.*/\1/')
+if [[ -n "$page_size" ]]; then
+ cur_page_size=$(getconf PAGESIZE 2>/dev/null)
+ if [[ "$cur_page_size" != "$page_size" ]]; then
+ ktap_skip_all "requires PAGE_SIZE=$page_size (current: ${cur_page_size:-unknown})"
+ exit "$KSFT_SKIP"
+ fi
+fi
+
declare -a optargs
failfunc=ktap_test_fail
diff --git a/tools/testing/selftests/net/packetdrill/tcp_rcv_collapse_bloated.pkt b/tools/testing/selftests/net/packetdrill/tcp_rcv_collapse_bloated.pkt
new file mode 100644
index 000000000000..90e8be86968f
--- /dev/null
+++ b/tools/testing/selftests/net/packetdrill/tcp_rcv_collapse_bloated.pkt
@@ -0,0 +1,135 @@
+// SPDX-License-Identifier: GPL-2.0
+//
+// Test that tcp_collapse() only collapses bloated skb runs, skips leading
+// and trailing high-density skbs, skips isolated bloated skbs when collapsing
+// them would not shrink truesize, and properly recovers receive buffer space.
+
+--mss=1000
+--define="PAGE_SIZE=4096"
+
+`./defaults.sh`
+
+// Establish a connection with a large receive buffer.
+// __sock_set_rcvbuf() doubles, so 2000000
+ +0 socket(..., SOCK_STREAM, IPPROTO_TCP) = 3
+ +0 setsockopt(3, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0
+ +0 setsockopt(3, SOL_SOCKET, SO_RCVBUF, [1000000], 4) = 0
+ +0 bind(3, ..., ...) = 0
+ +0 listen(3, 1) = 0
+
+ +0 < S 0:0(0) win 32792 <mss 1000,nop,nop,sackOK,nop,wscale 7>
+ +0 > S. 0:0(0) ack 1 win 65535 <mss 1460,nop,nop,sackOK,nop,wscale 4>
+ +0 < . 1:1(0) ack 1 win 65535
+
+ +0 accept(3, ..., ...) = 4
+
+// 1. Queue two high-density 62,000 B segments (each adds 15 pages of frag
+// truesize with enough margin across NET_SKB_PAD, L1_CACHE_BYTES, and
+// MAX_SKB_FRAGS configs).
+// Read 100 bytes from the first one so before(skb->seq, tp->copied_seq) is true.
+ +0 < P. 1:62001(62000) ack 1 win 65535
+ * > . 1:1(0) ack 62001
+ +0 read(4, ..., 100) = 100
+
+ +0 < P. 62001:124001(62000) ack 1 win 65535
+ * > . 1:1(0) ack 124001
+
+// 2. Queue an isolated bloated 1,000 B segment.
+ +0 < P. 124001:125001(1000) ack 1 win 65535
+ * > . 1:1(0) ack 125001
+
+// 3. Queue another high-density 62,000 B segment.
+ +0 < P. 125001:187001(62000) ack 1 win 65535
+ * > . 1:1(0) ack 187001
+
+// 4. Queue a run of 30 bloated 1,000 B segments.
+ +0 < P. 187001:188001(1000) ack 1 win 65535
+ * > . 1:1(0) ack 188001
+ +0 < P. 188001:189001(1000) ack 1 win 65535
+ * > . 1:1(0) ack 189001
+ +0 < P. 189001:190001(1000) ack 1 win 65535
+ * > . 1:1(0) ack 190001
+ +0 < P. 190001:191001(1000) ack 1 win 65535
+ * > . 1:1(0) ack 191001
+ +0 < P. 191001:192001(1000) ack 1 win 65535
+ * > . 1:1(0) ack 192001
+ +0 < P. 192001:193001(1000) ack 1 win 65535
+ * > . 1:1(0) ack 193001
+ +0 < P. 193001:194001(1000) ack 1 win 65535
+ * > . 1:1(0) ack 194001
+ +0 < P. 194001:195001(1000) ack 1 win 65535
+ * > . 1:1(0) ack 195001
+ +0 < P. 195001:196001(1000) ack 1 win 65535
+ * > . 1:1(0) ack 196001
+ +0 < P. 196001:197001(1000) ack 1 win 65535
+ * > . 1:1(0) ack 197001
+ +0 < P. 197001:198001(1000) ack 1 win 65535
+ * > . 1:1(0) ack 198001
+ +0 < P. 198001:199001(1000) ack 1 win 65535
+ * > . 1:1(0) ack 199001
+ +0 < P. 199001:200001(1000) ack 1 win 65535
+ * > . 1:1(0) ack 200001
+ +0 < P. 200001:201001(1000) ack 1 win 65535
+ * > . 1:1(0) ack 201001
+ +0 < P. 201001:202001(1000) ack 1 win 65535
+ * > . 1:1(0) ack 202001
+ +0 < P. 202001:203001(1000) ack 1 win 65535
+ * > . 1:1(0) ack 203001
+ +0 < P. 203001:204001(1000) ack 1 win 65535
+ * > . 1:1(0) ack 204001
+ +0 < P. 204001:205001(1000) ack 1 win 65535
+ * > . 1:1(0) ack 205001
+ +0 < P. 205001:206001(1000) ack 1 win 65535
+ * > . 1:1(0) ack 206001
+ +0 < P. 206001:207001(1000) ack 1 win 65535
+ * > . 1:1(0) ack 207001
+ +0 < P. 207001:208001(1000) ack 1 win 65535
+ * > . 1:1(0) ack 208001
+ +0 < P. 208001:209001(1000) ack 1 win 65535
+ * > . 1:1(0) ack 209001
+ +0 < P. 209001:210001(1000) ack 1 win 65535
+ * > . 1:1(0) ack 210001
+ +0 < P. 210001:211001(1000) ack 1 win 65535
+ * > . 1:1(0) ack 211001
+ +0 < P. 211001:212001(1000) ack 1 win 65535
+ * > . 1:1(0) ack 212001
+ +0 < P. 212001:213001(1000) ack 1 win 65535
+ * > . 1:1(0) ack 213001
+ +0 < P. 213001:214001(1000) ack 1 win 65535
+ * > . 1:1(0) ack 214001
+ +0 < P. 214001:215001(1000) ack 1 win 65535
+ * > . 1:1(0) ack 215001
+ +0 < P. 215001:216001(1000) ack 1 win 65535
+ * > . 1:1(0) ack 216001
+ +0 < P. 216001:217001(1000) ack 1 win 65535
+ * > . 1:1(0) ack 217001
+
+// 5. Queue a trailing high-density 62,000 B segment.
+ +0 < P. 217001:279001(62000) ack 1 win 65535
+ * > . 1:1(0) ack 279001
+
+// Reset SNMP counters.
+ +0 `nstat -n`
+
+// Shrink SO_RCVBUF so sk_rcvbuf (320,000) is smaller than current
+// sk_rmem_alloc (334,592 on x86_64 default), but larger than sk_rmem_alloc
+// after collapsing the 30 bloated skbs (300,288 on x86_64 default).
+ +0 setsockopt(4, SOL_SOCKET, SO_RCVBUF, [160000], 4) = 0
+
+// Inject the next 1,000 B segment with a changed window to bypass
+// header prediction. This triggers tcp_prune_queue() -> tcp_collapse().
+// Only the 30 bloated segments should be collapsed; the high-density
+// segments and the isolated bloated segment are skipped. sk_rmem_alloc
+// shrinks below sk_rcvbuf, so tcp_can_ingest() succeeds and the packet
+// is accepted.
+ +0 < P. 279001:280001(1000) ack 1 win 65534
+ * > . 1:1(0) ack 280001
+
+// Verify exactly 30 skbs were collapsed.
+ +0 `nstat -s | grep TcpExtTCPRcvCollapsed | grep -q " 30 "`
+
+// Verify no packets were dropped.
+ +0 `test -z "$(nstat -s | grep TcpExtTCPRcvQDrop)"`
+
+// Read all remaining queued data to verify data integrity.
+ +0 read(4, ..., 300000) = 279900
--
2.56.0.679.gc527c65306-goog
^ permalink raw reply related [flat|nested] 13+ messages in thread* Re: [PATCH v2 net-next 7/7] selftest: packetdrill: Add test for tcp_collapse().
2026-11-16 23:05 ` [PATCH v2 net-next 7/7] selftest: packetdrill: Add test for tcp_collapse() Kuniyuki Iwashima
@ 2026-11-17 23:13 ` netdev-bot+sashiko
0 siblings, 0 replies; 13+ messages in thread
From: netdev-bot+sashiko @ 2026-11-17 23:13 UTC (permalink / raw)
To: kuniyu
Cc: edumazet, ncardwell, davem, kuba, pabeni, matttbe, martineau,
horms, kuni1840, netdev, mptcp
Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] tcp_rcv_collapse_bloated.pkt cannot catch a regression in the
high-density/unread-bytes check, tcp_collapse_skb_bloated(), even…
- [Low] The last step of tcp_rcv_collapse_bloated.pkt claims to 'verify
data integrity', but `read(4, ..., 300000) = 279900` only checks the…
--
selftest: packetdrill: Add test for tcp_collapse().
This adds a packetdrill test, tcp_rcv_collapse_bloated.pkt. It queues a
mix of high-density and low-density skbs, shrinks SO_RCVBUF to trigger
tcp_prune_queue()->tcp_collapse(), and then checks TcpExtTCPRcvCollapsed
to confirm that exactly 30 skbs were collapsed. It also changes
ksft_runner.sh to skip any script whose PAGE_SIZE define does not match
getconf PAGESIZE.
> diff --git a/tools/testing/selftests/net/packetdrill/tcp_rcv_collapse_bloated.pkt b/tools/testing/selftests/net/packetdrill/tcp_rcv_collapse_bloated.pkt
> new file mode 100644
> index 0000000000000..90e8be86968f8
> --- /dev/null
> +++ b/tools/testing/selftests/net/packetdrill/tcp_rcv_collapse_bloated.pkt
> @@ -0,0 +1,135 @@
> +// SPDX-License-Identifier: GPL-2.0
> +//
> +// Test that tcp_collapse() only collapses bloated skb runs, skips leading
> +// and trailing high-density skbs, skips isolated bloated skbs when collapsing
> +// them would not shrink truesize, and properly recovers receive buffer space.
[ ... ]
> +// 1. Queue two high-density 62,000 B segments (each adds 15 pages of frag
> +// truesize with enough margin across NET_SKB_PAD, L1_CACHE_BYTES, and
> +// MAX_SKB_FRAGS configs).
> +// Read 100 bytes from the first one so before(skb->seq, tp->copied_seq) is true.
> + +0 < P. 1:62001(62000) ack 1 win 65535
> + * > . 1:1(0) ack 62001
> + +0 read(4, ..., 100) = 100
[Severity: Low]
Can this test catch a regression in the density check itself?
The header comment says the test checks that tcp_collapse() skips
leading and trailing high-density skbs. The partial read here is meant to
cover the copied_seq case. However, every high-density skb is 62,000 B.
That is already larger than TCP_COLLAPSE_MAX_TOTAL_SIZE, which is
16 * 3776 = 60416 on 4K pages.
Because of this, two other checks in tcp_collapse() settle the outcome
for these skbs, whatever tcp_collapse_skb_bloated() returns. First, the
second loop stops any run from extending into a 62,000 B skb:
net/ipv4/tcp_input.c:tcp_collapse() {
...
if ((list || !before(TCP_SKB_CB(n)->seq, sub_end)) &&
(TCP_SKB_CB(n)->end_seq - start >= TCP_COLLAPSE_MAX_TOTAL_SIZE ||
!tcp_collapse_skb_bloated(n, sub_end,
TCP_COLLAPSE_SCALING_RATIO)))
break;
...
}
Second, the truesize comparison always skips a single 62,000 B skb:
if (old_truesize <= new_truesize) {
start = sub_end;
skb = n;
goto restart;
}
Here new_truesize = 16 * SKB_TRUESIZE(3776) + compute_skb_truesize(~1.5K)
= 69632 + 2304 = 71936. That is above the roughly 65792 truesize implied
by the rmem figures in the test.
If tcp_collapse_skb_bloated() is forced to always return true, the walk
over the queue looks like this:
skb1 (start = copied_seq = 101): breaks at skb2 because
124001 - 101 >= 60416, then the truesize check skips it
skb2: breaks at the isolated skb (63000 >= 60416), then skipped
isolated skb: breaks at skb3 (63000 >= 60416); old 2304 <= new
compute_skb_truesize(1000) = 2304, so skipped
skb3: breaks at the first small skb, then skipped
30-skb run: collapses, then breaks at the trailing skb
(92000 >= 60416)
trailing skb: the truesize check skips it
That still gives exactly 30 for TcpExtTCPRcvCollapsed, so the test
passes. It would also pass if the old before(TCP_SKB_CB(skb)->seq, start)
trigger came back in the first loop. In this test, the
!tcp_collapse_skb_bloated() break in the second loop never ends a run.
The test does catch the pre-series behaviour. But would a later
regression in tcp_collapse_skb_bloated(), or in the copied_seq-based
unread computation, pass this test unnoticed?
One option is to use high-density skbs small enough that a run stays
under TCP_COLLAPSE_MAX_TOTAL_SIZE, and where collapsing them would reduce
truesize. Then the density check would be what decides the outcome.
[ ... ]
> +// Read all remaining queued data to verify data integrity.
> + +0 read(4, ..., 300000) = 279900
[Severity: Low]
Does this read() actually verify data integrity? With the "..." buffer
form, packetdrill only checks the return value. The injected payloads are
also all identical zero-filled bytes.
Suppose the copy loop in tcp_collapse() used the wrong offset or source
skb:
int offset = start - TCP_SKB_CB(skb)->seq;
...
if (skb_copy_bits(skb, offset, skb_put(nskb, size), size))
or reordered bytes inside the collapsed range. read() would still return
279900, and the test would pass.
Should the comment say that this step only checks the byte count? Or
could the test send non-uniform payloads and verify their contents?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261116230555.2305867-1-kuniyu%40google.com
^ permalink raw reply [flat|nested] 13+ messages in thread