Netdev List
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: kuniyu@google.com
Cc: edumazet@kernel.org, ncardwell@google.com, davem@davemloft.net,
	kuba@kernel.org, pabeni@redhat.com, matttbe@kernel.org,
	martineau@kernel.org, horms@kernel.org, kuni1840@gmail.com,
	netdev@vger.kernel.org, mptcp@lists.linux.dev
Subject: Re: [PATCH v2 net-next 4/7] tcp: Skip collapsing if collapsed skb is less-dense.
Date: Tue, 17 Nov 2026 23:13:40 +0000	[thread overview]
Message-ID: <179495722091.3136.1223304567026487070@kernel.org> (raw)
In-Reply-To: <20261116230555.2305867-5-kuniyu@google.com>

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

  reply	other threads:[~2026-11-17 23:13 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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-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
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
2026-11-17 23:13   ` netdev-bot+sashiko [this message]
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
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
2026-11-17 23:13   ` netdev-bot+sashiko

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=179495722091.3136.1223304567026487070@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=kuni1840@gmail.com \
    --cc=kuniyu@google.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox