Skip to contentSkip to conversation

tcp: rate limit receive queue collapsing

PATCH Applied Eric Dumazet 6 participants 6 messages Nov 5 – Nov 9

Subsystems (4)
lore
Share & tools
lore

Eric Dumazet

author maintainer
#0 lore raw

PATCH 1/1 tcp: rate limit receive queue collapsing

When a socket is over its receive buffer limit and its sk_rcvbuf can not grow, every incoming segment that needs memory goes through tcp_prune_queue(), which runs tcp_collapse_ofo_queue() and tcp_collapse() on the receive queue. Both walk their whole queue, even when there is nothing left to collapse.

A peer can trigger this walk per packet, without refilling memory:

1) The segment lands last in the out-of-order queue. Since commit b0e01253a764 ("tcp: refine tcp_prune_ofo_queue() logic"), tcp_prune_ofo_queue() drops nothing, the segment is dropped and the queues are unchanged. The peer can send it again forever.

2) The segment lands second to last: one skb is pruned, and one small segment fills that space again.

3) The out-of-order queue is empty and the receive queue alone is over the limit (application not reading). Collapsing frees nothing, the out-of-order segment is dropped, and the window is not closed.

4) Collapsing frees just one skb, enough to queue the segment. The next segment is over the limit again.

Only allow a collapse if at least 1/8 of sk_rcvbuf of new memory was queued in the receive queues since the previous collapse. This restores the "12.5 % of the queue capacity" amortization of the 2018 SegmentSmack fixes (CVE-2018-5390), using the same unit as the sk_rcvbuf limit, so that peers sending small (bloated) segments are not penalized.

The credit is the net growth of sk_rmem_alloc in tcp_queue_rcv() and tcp_data_queue_ofo(), so that:

- Coalescing a segment into the tailroom of the previous skb charges no memory, and earns no credit.
- An out-of-order segment replacing or covering queued skbs only earns the memory it adds.
- Duplicates earn nothing.
- Data moved from the out-of-order queue to the receive queue (tcp_ofo_queue()) is not credited twice.

In-order data is credited too, so that a receive queue filled by bloated in-order segments can still be collapsed when a hole appears.

In-order segments are rate limited as well: a peer could otherwise send small in-order segments that each trigger a collapse freeing a single skb, enough to queue the segment, while the out-of-order queue holds many skbs that can not be collapsed.

The credit is tracked in a new u32 tp->rcv_collapse_credit, placed in a hole of the tcp_sock_write_rx group: no other field moves, and tcp_sock size is unchanged. tcp_queue_rcv() now samples sk_rmem_alloc, and writes the credit until it saturates at sk_rcvbuf, so that the fast path stops writing it after a full buffer of new memory. Saturating at the goal would not work: sk_rcvbuf, and the goal with it, can grow later (for instance in tcp_clamp_window(), just before the goal is checked), and a socket over its limit can not earn more credit until the application reads.

When collapsing is not allowed, tcp_prune_queue() goes directly to tcp_prune_ofo_queue(), which is cheap in the cases above.

A regular flow can see a difference when it goes over its limit before 1/8 of sk_rcvbuf of new memory was queued since the previous collapse:
out-of-order skbs can be pruned (even for an in-order segment filling a hole), and an in-order segment that still does not fit is dropped, with a zero window advertised, where a collapse might have made room.

Fixes b0e01253a764 ("tcp: refine tcp_prune_ofo_queue() logic")

---
Upcoming net-next work, once this patch reaches net-next:

- tcp: add TCPRcvCollapseSkipped counter
Count the collapses skipped by tcp_prune_queue(). A high rate can indicate a peer repeatedly sending segments that can not be queued.

- selftests/net: packetdrill: add tcp_rcv_collapse_ratelimit.pkt Fill the out-of-order queue of a socket with a small locked receive buffer, then send 10 times a segment landing last in the out-of-order queue. Check that no collapse is attempted, and that filling the hole still lets the application read data. It uses the new counter.

- tcp: use SKB_DROP_REASON_SOCKET_RCVBUFF when over sk_rcvbuf
- net: add SKB_DROP_REASON_MEMCG
Today, TCP reports all these drops as SKB_DROP_REASON_PROTO_MEM, whether the socket is over sk_rcvbuf, the memcg charge failed, or tcp_mem limits are reached.

 .../networking/net_cachelines/tcp_sock.rst    |  1 +
 include/linux/tcp.h                           |  1 +
 net/ipv4/tcp.c                                |  2 +
 net/ipv4/tcp_input.c                          | 55 +++++++++++++++----
 4 files changed, 48 insertions(+), 11 deletions(-)
Documentation/networking/net_cachelines/tcp_sock.rst +1 -0
index 93c85b621ec2..554e8c14a401 100644
@@ -107,6 +107,7 @@ u32[3]                        received_ecn_bytes      read_mostly         read_w
 u8:4                          received_ce_pending     read_mostly         read_write
 u32[3]                        delivered_ecn_bytes                         read_write
 u16                           pkts_acked_ewma                             read_write
+u32                           rcv_collapse_credit                         read_write          tcp_queue_rcv,tcp_data_queue_ofo,tcp_prune_queue
 u8:2                          syn_ect_snt             write_mostly        read_write
 u8:2                          syn_ect_rcv             read_mostly         read_write
 u8:2                          accecn_minlen           write_mostly        read_write
include/linux/tcp.h +1 -0
index 5a4c61fc240e..ef6c7e77f74c 100644
@@ -366,6 +366,7 @@ struct tcp_sock {
 	u32	rcv_rtt_last_tsecr;
 	u32	delivered_ecn_bytes[3];
 	u16	pkts_acked_ewma;/* Pkts acked EWMA for AccECN cep heuristic */
+	u32	rcv_collapse_credit; /* memory queued since last collapse */
 	struct {
 		u32	rtt_us;
 		u32	seq;
net/ipv4/tcp.c +2 -0
index b9de7c302ba8..da3bdf7269d7 100644
@@ -3490,6 +3490,7 @@ int tcp_disconnect(struct sock *sk, int flags)
 	tp->rx_opt.dsack = 0;
 	tp->rx_opt.num_sacks = 0;
 	tp->rcv_ooopack = 0;
+	tp->rcv_collapse_credit = 0;
 
 
 	/* Clean up fastopen related fields */
@@ -5308,6 +5309,7 @@ static void __init tcp_struct_check(void)
 	CACHELINE_ASSERT_GROUP_MEMBER(struct tcp_sock, tcp_sock_write_rx, rcv_rtt_last_tsecr);
 	CACHELINE_ASSERT_GROUP_MEMBER(struct tcp_sock, tcp_sock_write_rx, delivered_ecn_bytes);
 	CACHELINE_ASSERT_GROUP_MEMBER(struct tcp_sock, tcp_sock_write_rx, pkts_acked_ewma);
+	CACHELINE_ASSERT_GROUP_MEMBER(struct tcp_sock, tcp_sock_write_rx, rcv_collapse_credit);
 	CACHELINE_ASSERT_GROUP_MEMBER(struct tcp_sock, tcp_sock_write_rx, rcv_rtt_est);
 	CACHELINE_ASSERT_GROUP_MEMBER(struct tcp_sock, tcp_sock_write_rx, rcvq_space);
 }
net/ipv4/tcp_input.c +44 -11

100 lines not on this page · raw message

base-commit: 57889a8f6d49fc153aa0017d13086ab736567f8a

Signature · 1 line
2.53.0
#1 lore raw
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

Hi!

This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.

- Whether the issue was actually triggered, or is only theoretical (e.g. found by code inspection). If it was triggered please include the symptoms, like the stack trace or error messages.

Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account. If the series needs another revision for other reasons, please include the information in the commit messages then.

The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.

Reviewed-by on v1 1/1
#2 lore raw
In reply to Eric Dumazet
Quoted 67 lines · Eric Dumazet

When a socket is over its receive buffer limit and its sk_rcvbuf can not grow, every incoming segment that needs memory goes through tcp_prune_queue(), which runs tcp_collapse_ofo_queue() and tcp_collapse() on the receive queue. Both walk their whole queue, even when there is nothing left to collapse.

A peer can trigger this walk per packet, without refilling memory:

1) The segment lands last in the out-of-order queue. Since commit b0e01253a764 ("tcp: refine tcp_prune_ofo_queue() logic"), tcp_prune_ofo_queue() drops nothing, the segment is dropped and the queues are unchanged. The peer can send it again forever.

2) The segment lands second to last: one skb is pruned, and one small segment fills that space again.

3) The out-of-order queue is empty and the receive queue alone is over the limit (application not reading). Collapsing frees nothing, the out-of-order segment is dropped, and the window is not closed.

4) Collapsing frees just one skb, enough to queue the segment. The next segment is over the limit again.

Only allow a collapse if at least 1/8 of sk_rcvbuf of new memory was queued in the receive queues since the previous collapse. This restores the "12.5 % of the queue capacity" amortization of the 2018 SegmentSmack fixes (CVE-2018-5390), using the same unit as the sk_rcvbuf limit, so that peers sending small (bloated) segments are not penalized.

The credit is the net growth of sk_rmem_alloc in tcp_queue_rcv() and tcp_data_queue_ofo(), so that:

- Coalescing a segment into the tailroom of the previous skb charges no memory, and earns no credit.
- An out-of-order segment replacing or covering queued skbs only earns the memory it adds.
- Duplicates earn nothing.
- Data moved from the out-of-order queue to the receive queue (tcp_ofo_queue()) is not credited twice.

In-order data is credited too, so that a receive queue filled by bloated in-order segments can still be collapsed when a hole appears.

In-order segments are rate limited as well: a peer could otherwise send small in-order segments that each trigger a collapse freeing a single skb, enough to queue the segment, while the out-of-order queue holds many skbs that can not be collapsed.

The credit is tracked in a new u32 tp->rcv_collapse_credit, placed in a hole of the tcp_sock_write_rx group: no other field moves, and tcp_sock size is unchanged. tcp_queue_rcv() now samples sk_rmem_alloc, and writes the credit until it saturates at sk_rcvbuf, so that the fast path stops writing it after a full buffer of new memory. Saturating at the goal would not work: sk_rcvbuf, and the goal with it, can grow later (for instance in tcp_clamp_window(), just before the goal is checked), and a socket over its limit can not earn more credit until the application reads.

When collapsing is not allowed, tcp_prune_queue() goes directly to tcp_prune_ofo_queue(), which is cheap in the cases above.

A regular flow can see a difference when it goes over its limit before 1/8 of sk_rcvbuf of new memory was queued since the previous collapse:
out-of-order skbs can be pruned (even for an in-order segment filling a hole), and an in-order segment that still does not fit is dropped, with a zero window advertised, where a collapse might have made room.

Quoted 3 lines
Fixes b0e01253a764 ("tcp: refine tcp_prune_ofo_queue() logic")
reviewer Reviewed-by on v1 1/1
#3 lore raw
In reply to Eric Dumazet
Quoted 67 lines · Eric Dumazet

When a socket is over its receive buffer limit and its sk_rcvbuf can not grow, every incoming segment that needs memory goes through tcp_prune_queue(), which runs tcp_collapse_ofo_queue() and tcp_collapse() on the receive queue. Both walk their whole queue, even when there is nothing left to collapse.

A peer can trigger this walk per packet, without refilling memory:

1) The segment lands last in the out-of-order queue. Since commit b0e01253a764 ("tcp: refine tcp_prune_ofo_queue() logic"), tcp_prune_ofo_queue() drops nothing, the segment is dropped and the queues are unchanged. The peer can send it again forever.

2) The segment lands second to last: one skb is pruned, and one small segment fills that space again.

3) The out-of-order queue is empty and the receive queue alone is over the limit (application not reading). Collapsing frees nothing, the out-of-order segment is dropped, and the window is not closed.

4) Collapsing frees just one skb, enough to queue the segment. The next segment is over the limit again.

Only allow a collapse if at least 1/8 of sk_rcvbuf of new memory was queued in the receive queues since the previous collapse. This restores the "12.5 % of the queue capacity" amortization of the 2018 SegmentSmack fixes (CVE-2018-5390), using the same unit as the sk_rcvbuf limit, so that peers sending small (bloated) segments are not penalized.

The credit is the net growth of sk_rmem_alloc in tcp_queue_rcv() and tcp_data_queue_ofo(), so that:

- Coalescing a segment into the tailroom of the previous skb charges no memory, and earns no credit.
- An out-of-order segment replacing or covering queued skbs only earns the memory it adds.
- Duplicates earn nothing.
- Data moved from the out-of-order queue to the receive queue (tcp_ofo_queue()) is not credited twice.

In-order data is credited too, so that a receive queue filled by bloated in-order segments can still be collapsed when a hole appears.

In-order segments are rate limited as well: a peer could otherwise send small in-order segments that each trigger a collapse freeing a single skb, enough to queue the segment, while the out-of-order queue holds many skbs that can not be collapsed.

The credit is tracked in a new u32 tp->rcv_collapse_credit, placed in a hole of the tcp_sock_write_rx group: no other field moves, and tcp_sock size is unchanged. tcp_queue_rcv() now samples sk_rmem_alloc, and writes the credit until it saturates at sk_rcvbuf, so that the fast path stops writing it after a full buffer of new memory. Saturating at the goal would not work: sk_rcvbuf, and the goal with it, can grow later (for instance in tcp_clamp_window(), just before the goal is checked), and a socket over its limit can not earn more credit until the application reads.

When collapsing is not allowed, tcp_prune_queue() goes directly to tcp_prune_ofo_queue(), which is cheap in the cases above.

A regular flow can see a difference when it goes over its limit before 1/8 of sk_rcvbuf of new memory was queued since the previous collapse:
out-of-order skbs can be pruned (even for an in-order segment filling a hole), and an in-order segment that still does not fit is dropped, with a zero window advertised, where a collapse might have made room.

Quoted 3 lines
Fixes b0e01253a764 ("tcp: refine tcp_prune_ofo_queue() logic")
maintainer Reviewed-by
#4 lore raw
Quoted 69 lines · Kuniyuki Iwashima
Quoted 67 lines · Eric Dumazet

When a socket is over its receive buffer limit and its sk_rcvbuf can not grow, every incoming segment that needs memory goes through tcp_prune_queue(), which runs tcp_collapse_ofo_queue() and tcp_collapse() on the receive queue. Both walk their whole queue, even when there is nothing left to collapse.

A peer can trigger this walk per packet, without refilling memory:

1) The segment lands last in the out-of-order queue. Since commit b0e01253a764 ("tcp: refine tcp_prune_ofo_queue() logic"), tcp_prune_ofo_queue() drops nothing, the segment is dropped and the queues are unchanged. The peer can send it again forever.

2) The segment lands second to last: one skb is pruned, and one small segment fills that space again.

3) The out-of-order queue is empty and the receive queue alone is over the limit (application not reading). Collapsing frees nothing, the out-of-order segment is dropped, and the window is not closed.

4) Collapsing frees just one skb, enough to queue the segment. The next segment is over the limit again.

Only allow a collapse if at least 1/8 of sk_rcvbuf of new memory was queued in the receive queues since the previous collapse. This restores the "12.5 % of the queue capacity" amortization of the 2018 SegmentSmack fixes (CVE-2018-5390), using the same unit as the sk_rcvbuf limit, so that peers sending small (bloated) segments are not penalized.

The credit is the net growth of sk_rmem_alloc in tcp_queue_rcv() and tcp_data_queue_ofo(), so that:

- Coalescing a segment into the tailroom of the previous skb charges no memory, and earns no credit.
- An out-of-order segment replacing or covering queued skbs only earns the memory it adds.
- Duplicates earn nothing.
- Data moved from the out-of-order queue to the receive queue (tcp_ofo_queue()) is not credited twice.

In-order data is credited too, so that a receive queue filled by bloated in-order segments can still be collapsed when a hole appears.

In-order segments are rate limited as well: a peer could otherwise send small in-order segments that each trigger a collapse freeing a single skb, enough to queue the segment, while the out-of-order queue holds many skbs that can not be collapsed.

The credit is tracked in a new u32 tp->rcv_collapse_credit, placed in a hole of the tcp_sock_write_rx group: no other field moves, and tcp_sock size is unchanged. tcp_queue_rcv() now samples sk_rmem_alloc, and writes the credit until it saturates at sk_rcvbuf, so that the fast path stops writing it after a full buffer of new memory. Saturating at the goal would not work: sk_rcvbuf, and the goal with it, can grow later (for instance in tcp_clamp_window(), just before the goal is checked), and a socket over its limit can not earn more credit until the application reads.

When collapsing is not allowed, tcp_prune_queue() goes directly to tcp_prune_ofo_queue(), which is cheap in the cases above.

A regular flow can see a difference when it goes over its limit before 1/8 of sk_rcvbuf of new memory was queued since the previous collapse:
out-of-order skbs can be pruned (even for an in-order segment filling a hole), and an in-order segment that still does not fit is dropped, with a zero window advertised, where a collapse might have made room.

Quoted 5 lines
Quoted 3 lines
Fixes b0e01253a764 ("tcp: refine tcp_prune_ofo_queue() logic")

Thanks, Eric!

neal

#5 lore raw
In reply to Eric Dumazet
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 9 lines · On Thu, 5 Nov 2026 13:40:43 +0100 you wrote:

When a socket is over its receive buffer limit and its sk_rcvbuf can not grow, every incoming segment that needs memory goes through tcp_prune_queue(), which runs tcp_collapse_ofo_queue() and tcp_collapse() on the receive queue. Both walk their whole queue, even when there is nothing left to collapse.

A peer can trigger this walk per packet, without refilling memory:

[...]

Here is the summary with links:
- [net] tcp: rate limit receive queue collapsing

    https://git.kernel.org/netdev/net/c/c2213d9b61c1

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html