Skip to contentSkip to conversation

tcp: preserve timestamps across receive queue collapse

PATCH Applied Jason Xing 4 participants 9 messages Sep 24 – Sep 30

Subsystems (3)
lore
Share & tools
lore

Jason Xing

author
#0 lore raw

PATCH v3 1/1 tcp: preserve timestamps across receive queue collapse

When tcp_collapse() rebuilds skbs under memory pressure, the copy process doesn't include the right tstamp and hwtstamp from the old skb. And memcpy(nskb->cb, skb->cb, ...) copies has_rxtstamp, but nskb->tstamp and hwtstamps are left at zero, so tcp_recv_timestamp() ends up emitting no cmsg at all.

In net timestamping case, if such an skb happens to be the last one consumed in a recvmsg() call, the application receives no RX timestamp for that call.

Fix this by copying both tstamp and hwtstamp of the last skb to the new skb, matching tcp_try_coalesce()/tcp_add_backlog().

Note that the has_rxtstamp flag can still be inherited through the cb memcpy from an skb that contributes no bytes (fully covered skb left in the ofo tree by the tcp_ooo_try_coalesce() -> coalesce_done path), so set TCP_SKB_CB(nskb)->has_rxtstamp to false which makes the new block the only place setting it.

Fixes 98aaa913b4ed ("tcp: Extend SOF_TIMESTAMPING_RX_SOFTWARE to TCP recvmsg")

---
V3

1. revise the commit message with more accurate description (Eric)
2. fix a corner case (where a skb can contribute no bytes if OOO happens) spotted by AI and Eric

V2

1. add one sentence in commit log from Eric's reply
2. use the same semantic as tcp_add_backlog which is to sync the last skb (Eric)

---
 net/ipv4/tcp_input.c | 7 +++++++
 1 file changed, 7 insertions(+)
net/ipv4/tcp_input.c +7 -0
index 92bc60716f33..f58954ddecee 100644
@@ -5834,6 +5834,7 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
 			break;
 
 		memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
+		TCP_SKB_CB(nskb)->has_rxtstamp = false;
 		skb_copy_decrypted(nskb, skb);
 		TCP_SKB_CB(nskb)->seq = TCP_SKB_CB(nskb)->end_seq = start;
 		if (list)
@@ -5854,6 +5855,12 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
 				if (skb_copy_bits(skb, offset, skb_put(nskb, size), size))
 					BUG();
 				TCP_SKB_CB(nskb)->end_seq += size;
+				if (TCP_SKB_CB(skb)->has_rxtstamp) {
+					TCP_SKB_CB(nskb)->has_rxtstamp = true;
+					nskb->tstamp = skb->tstamp;
+					skb_hwtstamps(nskb)->hwtstamp =
+						skb_hwtstamps(skb)->hwtstamp;
+				}
 				copy -= size;
 				start += size;
 			}
Signature · 1 line
2.43.7
maintainer
#1 lore raw
Quoted 19 lines · On Thu, 24 Sep 2026 23:25:29 +0800 Jason Xing wrote:

When tcp_collapse() rebuilds skbs under memory pressure, the copy process doesn't include the right tstamp and hwtstamp from the old skb. And memcpy(nskb->cb, skb->cb, ...) copies has_rxtstamp, but nskb->tstamp and hwtstamps are left at zero, so tcp_recv_timestamp() ends up emitting no cmsg at all.

In net timestamping case, if such an skb happens to be the last one consumed in a recvmsg() call, the application receives no RX timestamp for that call.

Fix this by copying both tstamp and hwtstamp of the last skb to the new skb, matching tcp_try_coalesce()/tcp_add_backlog().

Note that the has_rxtstamp flag can still be inherited through the cb memcpy from an skb that contributes no bytes (fully covered skb left in the ofo tree by the tcp_ooo_try_coalesce() -> coalesce_done path), so set TCP_SKB_CB(nskb)->has_rxtstamp to false which makes the new block the only place setting it.

Quoted 8 lines
Fixes 98aaa913b4ed ("tcp: Extend SOF_TIMESTAMPING_RX_SOFTWARE to TCP recvmsg")

---
V3

1. revise the commit message with more accurate description (Eric)
2. fix a corner case (where a skb can contribute no bytes if OOO happens) spotted by AI and Eric

Looks good now, Eric?
https://lore.kernel.org/all/[email protected]/

(Jason, please see net for the up-to-date email addr for Eric)

maintainer
#2 lore raw
Quoted 29 lines · On Tue, 29 Sep 2026 16:53:18 -0700 Jakub Kicinski wrote:
Quoted 19 lines · On Thu, 24 Sep 2026 23:25:29 +0800 Jason Xing wrote:

When tcp_collapse() rebuilds skbs under memory pressure, the copy process doesn't include the right tstamp and hwtstamp from the old skb. And memcpy(nskb->cb, skb->cb, ...) copies has_rxtstamp, but nskb->tstamp and hwtstamps are left at zero, so tcp_recv_timestamp() ends up emitting no cmsg at all.

In net timestamping case, if such an skb happens to be the last one consumed in a recvmsg() call, the application receives no RX timestamp for that call.

Fix this by copying both tstamp and hwtstamp of the last skb to the new skb, matching tcp_try_coalesce()/tcp_add_backlog().

Note that the has_rxtstamp flag can still be inherited through the cb memcpy from an skb that contributes no bytes (fully covered skb left in the ofo tree by the tcp_ooo_try_coalesce() -> coalesce_done path), so set TCP_SKB_CB(nskb)->has_rxtstamp to false which makes the new block the only place setting it.

Quoted 8 lines
Fixes 98aaa913b4ed ("tcp: Extend SOF_TIMESTAMPING_RX_SOFTWARE to TCP recvmsg")

---
V3

1. revise the commit message with more accurate description (Eric)
2. fix a corner case (where a skb can contribute no bytes if OOO happens) spotted by AI and Eric

Quoted 4 lines

Looks good now, Eric?
https://lore.kernel.org/all/[email protected]/

(Jason, please see net for the up-to-date email addr for Eric)

I meant to also say - I'm leaning towards net-next on this one. Please LMK if you disagree

author
#3 lore raw
Quoted 31 lines · Jakub Kicinski
Quoted 29 lines · On Tue, 29 Sep 2026 16:53:18 -0700 Jakub Kicinski wrote:
Quoted 27 lines · On Thu, 24 Sep 2026 23:25:29 +0800 Jason Xing wrote:

When tcp_collapse() rebuilds skbs under memory pressure, the copy process doesn't include the right tstamp and hwtstamp from the old skb. And memcpy(nskb->cb, skb->cb, ...) copies has_rxtstamp, but nskb->tstamp and hwtstamps are left at zero, so tcp_recv_timestamp() ends up emitting no cmsg at all.

In net timestamping case, if such an skb happens to be the last one consumed in a recvmsg() call, the application receives no RX timestamp for that call.

Fix this by copying both tstamp and hwtstamp of the last skb to the new skb, matching tcp_try_coalesce()/tcp_add_backlog().

Note that the has_rxtstamp flag can still be inherited through the cb memcpy from an skb that contributes no bytes (fully covered skb left in the ofo tree by the tcp_ooo_try_coalesce() -> coalesce_done path), so set TCP_SKB_CB(nskb)->has_rxtstamp to false which makes the new block the only place setting it.

Fixes 98aaa913b4ed ("tcp: Extend SOF_TIMESTAMPING_RX_SOFTWARE to TCP recvmsg")

---
V3

1. revise the commit message with more accurate description (Eric)
2. fix a corner case (where a skb can contribute no bytes if OOO happens) spotted by AI and Eric

Quoted 4 lines
Quoted 4 lines

Looks good now, Eric?
https://lore.kernel.org/all/[email protected]/

(Jason, please see net for the up-to-date email addr for Eric)

Will do.

Quoted 3 lines

I meant to also say - I'm leaning towards net-next on this one. Please LMK if you disagree

I'm okay with net-next. Seems like I need to repost.

Thanks,
Jason

author
#4 lore raw
Quoted 38 lines · Jason Xing
Quoted 31 lines · Jakub Kicinski
Quoted 29 lines · On Tue, 29 Sep 2026 16:53:18 -0700 Jakub Kicinski wrote:
Quoted 27 lines · On Thu, 24 Sep 2026 23:25:29 +0800 Jason Xing wrote:

When tcp_collapse() rebuilds skbs under memory pressure, the copy process doesn't include the right tstamp and hwtstamp from the old skb. And memcpy(nskb->cb, skb->cb, ...) copies has_rxtstamp, but nskb->tstamp and hwtstamps are left at zero, so tcp_recv_timestamp() ends up emitting no cmsg at all.

In net timestamping case, if such an skb happens to be the last one consumed in a recvmsg() call, the application receives no RX timestamp for that call.

Fix this by copying both tstamp and hwtstamp of the last skb to the new skb, matching tcp_try_coalesce()/tcp_add_backlog().

Note that the has_rxtstamp flag can still be inherited through the cb memcpy from an skb that contributes no bytes (fully covered skb left in the ofo tree by the tcp_ooo_try_coalesce() -> coalesce_done path), so set TCP_SKB_CB(nskb)->has_rxtstamp to false which makes the new block the only place setting it.

Fixes 98aaa913b4ed ("tcp: Extend SOF_TIMESTAMPING_RX_SOFTWARE to TCP recvmsg")

---
V3

1. revise the commit message with more accurate description (Eric)
2. fix a corner case (where a skb can contribute no bytes if OOO happens) spotted by AI and Eric

Quoted 4 lines
Quoted 4 lines

Looks good now, Eric?
https://lore.kernel.org/all/[email protected]/

(Jason, please see net for the up-to-date email addr for Eric)

Quoted 7 lines

Will do.

Quoted 3 lines

I meant to also say - I'm leaning towards net-next on this one. Please LMK if you disagree

I'm okay with net-next. Seems like I need to repost.

And it also seems unnecessary to carry the Fixes tag in V4, but I will mention this commit in the message.

Thanks,
Jason

maintainer
#5 lore raw
In reply to Jason Xing
Quoted 4 lines · On Wed, 30 Sep 2026 09:53:11 +0800 Jason Xing wrote:
Quoted 2 lines

I meant to also say - I'm leaning towards net-next on this one. Please LMK if you disagree

I'm okay with net-next. Seems like I need to repost.

No need to repost.

maintainer Reviewed-by
#6 lore raw
Quoted 8 lines · Jakub Kicinski
Quoted 4 lines · On Wed, 30 Sep 2026 09:53:11 +0800 Jason Xing wrote:
Quoted 2 lines

I meant to also say - I'm leaning towards net-next on this one. Please LMK if you disagree

I'm okay with net-next. Seems like I need to repost.

No need to repost.

Sorry for missing this v2, LGTM, thanks!

#8 lore raw
In reply to Jason Xing
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

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

Quoted 11 lines · On Thu, 24 Sep 2026 23:25:29 +0800 you wrote:

When tcp_collapse() rebuilds skbs under memory pressure, the copy process doesn't include the right tstamp and hwtstamp from the old skb. And memcpy(nskb->cb, skb->cb, ...) copies has_rxtstamp, but nskb->tstamp and hwtstamps are left at zero, so tcp_recv_timestamp() ends up emitting no cmsg at all.

In net timestamping case, if such an skb happens to be the last one consumed in a recvmsg() call, the application receives no RX timestamp for that call.

[...]

Here is the summary with links:
- [v3,net] tcp: preserve timestamps across receive queue collapse

    https://git.kernel.org/netdev/net-next/c/1a924ab28fb5

You are awesome, thank you!

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