tcp: Prevent tcp_collapse() from bloating queue.
Subsystems (5)
Kuniyuki Iwashima
reviewerv2 cover tcp: Prevent tcp_collapse() from bloating queue.
This is a follow-up for commit 031c403d13e5 ("tcp: do not let tcp_collapse() drive sk_forward_alloc negative").
The core algorithm in tcp_collapse() is so simple and has not been changed since commit 1da177e4c3f4 ("Linux-2.6.12-rc2"); finding a collapsible skb and repacking it into up-to-SKB_MAX_ORDER() skb.
Now that we use 4K MSS and tp->scaling_ratio, the simple algorithm does not work well, and it could even end up consuming more memory contrary to the intention.
For example, the scaling ratio of SKB_MAX_ORDER() is 222, but typically tp->scaling_ratio for 4K MSS flow is 252, and repacking data rather bloats high-density skb.
Also, once a low-density skb starts collapsing, it drags all the following skbs into collapsing regardless of the density.
This series changes the algorithm so that it skips high-density skb and a single low-density skb between high-density skbs, batches memory accounting at the end, and removes sk_forced_mem_schedule().
This series still keeps linear SKB_MAX_ORDER(0, 0) skbs as the collapse target, and Eric is working on a follow-up patch to store the collapsed payload in order-0 page frags instead.
https://lore.kernel.org/netdev/CAL4WiiqekjxZw3EoDYXLc0Phd3i0GStE2ZHWQkH2iAkSM5D8TQ@mail.gmail.com/
Changes:
v2:
* Patch 1: Describe SYN/FIN behaviour change
* Patch 3: Describe behavioural change for MPTCP
* Patch 4: Add compute_skb_truesize() instead of macro
* Patch 7: Rename test to tcp_rcv_collapse_bloated.pkt
v1: https://lore.kernel.org/netdev/[email protected]/
Kuniyuki Iwashima (7):
tcp: Refactor the first loop in tcp_collapse().
tcp: Don't collapse high density skb.
tcp: Stop collapsing if not bloated.
tcp: Skip collapsing if collapsed skb is less-dense.
tcp: Allocate all new skb before collapsing.
tcp: Batch memory accounting in tcp_collapse().
selftest: packetdrill: Add test for tcp_collapse().
include/linux/skbuff.h | 2 + net/core/skbuff.c | 26 ++- net/ipv4/tcp_input.c | 171 +++++++++++++----- .../selftests/net/packetdrill/ksft_runner.sh | 9 + .../packetdrill/tcp_rcv_collapse_bloated.pkt | 135 ++++++++++++++ 5 files changed, 292 insertions(+), 51 deletions(-) create mode 100644 tools/testing/selftests/net/packetdrill/tcp_rcv_collapse_bloated.pkt
Signature · 1 line
2.56.0.679.gc527c65306-goog
Kuniyuki Iwashima
reviewerPATCH v2 1/7 tcp: Refactor the first loop in tcp_collapse().
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.
---
v2: Document SYN/FIN behaviour change
--- net/ipv4/tcp_input.c | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-)
net/ipv4/tcp_input.c +10 -18
@@ -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);
Signature · 1 line
2.56.0.679.gc527c65306-goog
Kuniyuki Iwashima
reviewerPATCH v2 2/7 tcp: Don't collapse high density skb.
In reply to Kuniyuki Iwashimatcp_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.
--- net/ipv4/tcp_input.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-)
net/ipv4/tcp_input.c +20 -4
@@ -5808,6 +5808,19 @@ static struct sk_buff *tcp_skb_next(struct sk_buff *skb, struct sk_buff_head *lireturn 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 );
Signature · 1 line
2.56.0.679.gc527c65306-goog
[email protected]
AI reviewRe: [PATCH v2 net-next 2/7] tcp: Don't collapse high density skb.
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…
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.
Quoted 5 lines
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?
If the patch is meant only as a heuristic improvement for net-next, could the commit message say that?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261116230555.2305867-1-kuniyu%40google.com
Kuniyuki Iwashima
reviewerPATCH v2 3/7 tcp: Stop collapsing if not bloated.
In reply to Kuniyuki IwashimaEven 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
---
v2: Describe slight behavioural change for MPTCP
--- net/ipv4/tcp_input.c | 66 +++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 29 deletions(-)
net/ipv4/tcp_input.c +37 -29
@@ -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 ;
Signature · 1 line
2.56.0.679.gc527c65306-goog
[email protected]
AI reviewRe: [PATCH v2 net-next 3/7] tcp: Stop collapsing if not bloated.
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…
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.
net/ipv4/tcp_input.c static struct sk_buff *tcp_collapse_one(struct sock *sk, struct sk_buff *skb, · line 5842
Quoted 10 lines
net/ipv4/tcp_input.c +1
-1@@ -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()."
Quoted 3 lines
net/ipv4/tcp_input.c +0
-0* * If tail is NULL, this means until the end of the queue. *
[ ... ]
Quoted 4 lines
net/ipv4/tcp_input.c +4
-0+/* 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)) {
[ ... ]
Quoted 2 lines
net/ipv4/tcp_input.c +2
-0+ sub_end =TCP_SKB_CB (n)->end_seq; + }
[ ... ]
Quoted 5 lines
net/ipv4/tcp_input.c +2
-0+ + goto restart; end: skb_queue_walk_safe(&tmp, skb, n)tcp_rbtree_insert (root, skb);
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261116230555.2305867-1-kuniyu%40google.com
Kuniyuki Iwashima
reviewerPATCH v2 4/7 tcp: Skip collapsing if collapsed skb is less-dense.
In reply to Kuniyuki IwashimaOnce 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().
---
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(-)
include/linux/skbuff.h +2 -0
@@ -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 ;
net/core/skbuff.c +23 -3
@@ -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.
net/ipv4/tcp_input.c +23 -1
@@ -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)) {
Signature · 1 line
2.56.0.679.gc527c65306-goog
[email protected]
AI reviewRe: [PATCH v2 net-next 4/7] tcp: Skip collapsing if collapsed skb is less-dense.
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] tcp: rbtree order corruption in tcp_collapse() on skipped overlapping skb - [Low] net: skbuff…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] tcp: rbtree order corruption in tcp_collapse() on skipped overlapping skb
- [Low] net: skbuff: duplicate kmalloc_size_roundup() call in kmalloc_reserve()
--
commit a3eb35002975ecc31de00e85abdd6d7c9480540e
Author: Kuniyuki Iwashima <[email protected]>
tcp: Skip collapsing if collapsed skb is less-dense.
This commit introduces compute_skb_truesize() and uses it in tcp_collapse() to skip collapsing a range of SKBs when the resulting truesize would not decrease.
Quoted 24 lines
net/core/skbuff.c +11
-0@@ -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;
net/core/skbuff.c static void *kmalloc_reserve(unsigned int *size, gfp_t flags, int node, · line 630
Quoted 10 lines
net/core/skbuff.c +2
-3void *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() now call kmalloc_size_roundup() twice for allocations that do not use skb_small_head_cache?
kmalloc_reserve_size() already returns kmalloc_size_roundup(obj_size), but kmalloc_reserve() still calls kmalloc_size_roundup(obj_size) again right after the if (!obj_size) block. Since kmalloc_size_roundup() is an out-of-line function in mm/slab_common.c without const or pure attributes, non-small SKB allocations make a redundant call on the hotpath.
[ ... ]
Quoted 4 lines
net/ipv4/tcp_input.c +0
-0
[ ... ]
Quoted 12 lines
net/ipv4/tcp_input.c +8
-0@@ -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 ;
net/ipv4/tcp_input.c tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root, · line 5933
Quoted 10 lines
net/ipv4/tcp_input.c +9
-0+if (remainder) { + nskbs++; + new_truesize +=compute_skb_truesize (remainder,GFP_ATOMIC ); + } + +if (old_truesize <= new_truesize) { + start = sub_end; + skb = n; + goto restart; }
[Severity: Medium]
Can skipping a sub-range on the out-of-order rbtree (list == NULL) break the monotonic seq and end_seq ordering of tp->out_of_order_queue when skb partially overlaps the previously collapsed range?
Suppose tp->out_of_order_queue holds skb_1 [1000, 5000] (truesize 8448) and an overlapping skb_2 [4500, 5100] (truesize 896 from skbuff_small_head) where !tcp_skb_can_collapse_rx(skb_1, skb_2) is true (for example, due to differing MPTCP DSS extensions or decrypted state). The first pass collapses skb_1 into nskb_1 [1000, 4776] and nskb_2 [4776, 5000] on the deferred tmp queue, erases skb_1 from root, and advances start to 5000 and skb to skb_2.
On the next pass at restart, the unread 100 bytes of skb_2 [4500, 5100] are bloated, but compute_skb_truesize(100, GFP_ATOMIC) is 896, so old_truesize <= new_truesize holds and skb_2 is left in root with seq 4500 and end_seq 5100. At the end label, tcp_rbtree_insert() orders by seq and inserts nskb_2 [4776, 5000] to the right of skb_2 [4500, 5100]:
net/ipv4/tcp_input.c:tcp_collapse() {
...
end:
skb_queue_walk_safe(&tmp, skb, n)
tcp_rbtree_insert(root, skb);
}tcp_collapse_ofo_queue() then sets tp->ooo_last_skb = skb_rb_last() to nskb_2 (end_seq 5000) instead of skb_2 (end_seq 5100), causing later tcp_data_queue_ofo() fastpath checks to misbehave and tcp_ofo_queue() to drop nskb_2 as out-of-order after skb_2 advances tp->rcv_nxt to 5100.
Checking (list || !before(TCP_SKB_CB(skb)->seq, start)) before taking the old_truesize <= new_truesize skip path would ensure overlapping OOO SKBs are still trimmed and re-keyed at start.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
[email protected]
AI reviewRe: [PATCH v2 net-next 4/7] tcp: Skip collapsing if collapsed skb is less-dense.
In reply to Kuniyuki IwashimaThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
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.
Quoted 19 lines
net/core/skbuff.c +11
-0@@ -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); +} +
[ ... ]
net/core/skbuff.c static void *kmalloc_reserve(unsigned int *size, gfp_t flags, int node, · line 630
Quoted 12 lines
net/core/skbuff.c +2
-3@@ -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.
Quoted 4 lines
net/ipv4/tcp_input.c +0
-0
[ ... ]
Quoted 4 lines
net/ipv4/tcp_input.c +0
-0@@ -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;
net/ipv4/tcp_input.c tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root, · line 5925
Quoted 10 lines
net/ipv4/tcp_input.c +10
-0+ 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?
[ ... ]
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261116230555.2305867-1-kuniyu%40google.com
Kuniyuki Iwashima
reviewerPATCH v2 5/7 tcp: Allocate all new skb before collapsing.
In reply to Kuniyuki IwashimaAt 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.
--- net/ipv4/tcp_input.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-)
net/ipv4/tcp_input.c +21 -8
@@ -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
Signature · 1 line
2.56.0.679.gc527c65306-goog
[email protected]
AI reviewRe: [PATCH v2 net-next 5/7] tcp: Allocate all new skb before collapsing.
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
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.
Quoted 3 lines
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?
net/ipv4/tcp_input.c static struct sk_buff *tcp_skb_next(struct sk_buff *skb, struct sk_buff_head *li · line 5815
Quoted 9 lines
net/ipv4/tcp_input.c +1
-0@@ -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.
[ ... ]
Quoted 12 lines
net/ipv4/tcp_input.c +3
-2@@ -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;
[ ... ]
Quoted 7 lines
net/ipv4/tcp_input.c +0
-3@@ -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;
net/ipv4/tcp_input.c tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root, · line 5948
Quoted 10 lines
net/ipv4/tcp_input.c +5
-2- - 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.
[ ... ]
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261116230555.2305867-1-kuniyu%40google.com
Eric Dumazet
maintainerRe: [PATCH v2 net-next 5/7] tcp: Allocate all new skb before collapsing.
In reply to Kuniyuki IwashimaQuoted 69 lines · Kuniyuki Iwashima
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 atTCP_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 <[email protected]>Reviewed-by Eric Dumazet <[email protected]>--- net/ipv4/tcp_input.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-)net/ipv4/tcp_input.c +4
-1@@ -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.
net/ipv4/tcp_input.c tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root, · line 5917
Quoted 9 lines
net/ipv4/tcp_input.c +3
-2@@ -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.
Quoted 18 lines
net/ipv4/tcp_input.c +2
-3sub_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;
net/ipv4/tcp_input.c tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root, · line 5948
Quoted 10 lines
net/ipv4/tcp_input.c +5
-2- - 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.
Kuniyuki Iwashima
reviewerRe: [PATCH v2 net-next 5/7] tcp: Allocate all new skb before collapsing.
Quoted 84 lines · Eric Dumazet
Quoted 69 lines · Kuniyuki Iwashima
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 atTCP_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 <[email protected]>Reviewed-by Eric Dumazet <[email protected]>--- net/ipv4/tcp_input.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-)net/ipv4/tcp_input.c +4
-1@@ -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. net/ipv4/tcp_input.c tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root, · line 5917
Quoted 9 lines
net/ipv4/tcp_input.c +3
-2@@ -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.
Quoted 6 lines
For the ofo queue, as long as
n->seqis beforesub_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 thattcp_collapse()sees as a single section covering the whole ofo queue.
Exactly...
Quoted 54 lines
Quoted 19 lines
net/ipv4/tcp_input.c +2
-3sub_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;net/ipv4/tcp_input.c tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root, · line 5948
Quoted 10 lines
net/ipv4/tcp_input.c +5
-2- - 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_ATOMICmemory 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_SIZEmust 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_endis correct, remember that the cap has been exceeded, then skip the section the same way patch 4 does:
Quoted 6 lines
start = sub_end; skb = n; goto restart;
tcp_prune_ofo_queue()will take care of these skbs if we are still over the limit.
Sounds good, I'll apply the strategy in v3.
Quoted 18 lines
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.
Quoted 3 lines
Whatever option you choose, please add a comment next to
TCP_COLLAPSE_MAX_TOTAL_SIZEdescribing what it bounds and its exceptions.
Will fix that and add a comment as it's a hard limit for multiple skbs.
Thank you !
---
pw-bot: cr
Kuniyuki Iwashima
reviewerPATCH v2 6/7 tcp: Batch memory accounting in tcp_collapse().
In reply to Kuniyuki IwashimaOnce 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.
--- net/ipv4/tcp_input.c | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-)
net/ipv4/tcp_input.c +29 -5
@@ -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
Signature · 1 line
2.56.0.679.gc527c65306-goog
Kuniyuki Iwashima
reviewerPATCH v2 7/7 selftest: packetdrill: Add test for tcp_collapse().
In reply to Kuniyuki Iwashimatcp_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.
--- .../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
tools/testing/selftests/net/packetdrill/ksft_runner.sh +9 -0
@@ -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
tools/testing/selftests/net/packetdrill/tcp_rcv_collapse_bloated.pkt new file +135 -0
135 lines not on this page · raw message
Signature · 1 line
2.56.0.679.gc527c65306-goog
[email protected]
AI reviewRe: [PATCH v2 net-next 7/7] selftest: packetdrill: Add test for tcp_collapse().
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…
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.
Quoted 11 lines
tools/testing/selftests/net/packetdrill/tcp_rcv_collapse_bloated.pkt new file +5
-0@@ -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.
[ ... ]
tools/testing/selftests/net/packetdrill/tcp_rcv_collapse_bloated.pkt line 32
Quoted 7 lines
tools/testing/selftests/net/packetdrill/tcp_rcv_collapse_bloated.pkt new file +7
-0+// 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.
[ ... ]
tools/testing/selftests/net/packetdrill/tcp_rcv_collapse_bloated.pkt line 135
Quoted 2 lines
tools/testing/selftests/net/packetdrill/tcp_rcv_collapse_bloated.pkt new file +2
-0+// 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?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261116230555.2305867-1-kuniyu%40google.com
Hi Kuniyuki,
Thank you for your modifications, that's great!
Our CI did some validations and here is its report:
- KVM Validation: normal (except selftest_mptcp_join): Success! ✅
- KVM Validation: normal (only selftest_mptcp_join): Success! ✅
- KVM Validation: debug (except selftest_mptcp_join): Unstable: 1 failed test(s): selftest_diag ⚠️
- KVM Validation: debug (only selftest_mptcp_join): Success! ✅
- KVM Validation: btf-normal (only bpftest_all): Success! ✅
- KVM Validation: btf-debug (only bpftest_all): Success! ✅
- Perf: Unstable: 1 failed test(s): validation:sched-weak-secondary ⚠️
- Task: https://github.com/multipath-tcp/mptcp_net-next/actions/runs/43984329957
Initiator: Patchew Applier
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/2bc9687ac49a
Patchwork: https://patchwork.kernel.org/project/mptcp/list/?series=1213235
If there are some issues, you can reproduce them using the same environment as the one used by the CI thanks to a docker image, e.g.:
$ cd [kernel source code]
$ docker run -v "${PWD}:${PWD}:rw" -w "${PWD}" --privileged --rm -it \
--pull always mptcp/mptcp-upstream-virtme-docker:latest \
auto-normalFor more details:
https://github.com/multipath-tcp/mptcp-upstream-virtme-docker
Please note that despite all the efforts that have been already done to have a stable tests suite when executed on a public CI like here, it is possible some reported issues are not due to your modifications. Still, do not hesitate to help us improve that ;-)
Cheers,
MPTCP GH Action bot
Bot operated by Matthieu Baerts (NGI0 Core)