Thread View: linux.kernel.netdev
1 messages
1 total messages
Started by Eric Dumazet
Wed, 18 Nov 2026 10:28
Re: [PATCH v2 net-next 5/7] tcp: Allocate all new skb before collapsing.
Author: 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/
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads