Skip to contentSkip to conversation

tcp: use WRITE_ONCE() for tsoffset in tcp_v6_connect()

PATCH Applied Wesley Atwell 4 participants 5 messages Mar 10 – Mar 12

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tcp: use WRITE_ONCE() for tsoffset in tcp_v6_connect()

Commit dd23c9f1e8d5 ("tcp: annotate data-races around tp->tsoffset") updated do_tcp_getsockopt() to read tp->tsoffset with READ_ONCE() for TCP_TIMESTAMP because another CPU may change it concurrently.

tcp_v6_connect() still stores tp->tsoffset with a plain write. That store runs under lock_sock() via inet_stream_connect(), but the socket lock does not serialize a concurrent getsockopt(TCP_TIMESTAMP) from another task sharing the socket.

Use WRITE_ONCE() for the tcp_v6_connect() store so the connect-time writer matches the lockless TCP_TIMESTAMP reader. This also makes the IPv6 path consistent with tcp_v4_connect().

---
 net/ipv6/tcp_ipv6.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv6/tcp_ipv6.c +1 -1
index bb09d5ccf599..ba7cd7d3d4da 100644
@@ -325,7 +325,7 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
 						 inet->inet_dport);
 		if (!tp->write_seq)
 			WRITE_ONCE(tp->write_seq, st.seq);
-		tp->tsoffset = st.ts_off;
+		WRITE_ONCE(tp->tsoffset, st.ts_off);
 	}
 
 	if (tcp_fastopen_defer_connect(sk, &err))
Signature · 1 line
2.34.1
#1 lore raw

On Mon, Mar 09, 2026 at 07:26:04PM +0800, Wesley Atwell wrote:

You should use [PATCH net] instead of [PATCH] to indicate which tree this patch is intended for.

Quoted 31 lines

Commit dd23c9f1e8d5 ("tcp: annotate data-races around tp->tsoffset") updated do_tcp_getsockopt() to read tp->tsoffset with READ_ONCE() for TCP_TIMESTAMP because another CPU may change it concurrently.

tcp_v6_connect() still stores tp->tsoffset with a plain write. That store runs under lock_sock() via inet_stream_connect(), but the socket lock does not serialize a concurrent getsockopt(TCP_TIMESTAMP) from another task sharing the socket.

Use WRITE_ONCE() for the tcp_v6_connect() store so the connect-time writer matches the lockless TCP_TIMESTAMP reader. This also makes the IPv6 path consistent with tcp_v4_connect().

---
 net/ipv6/tcp_ipv6.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv6/tcp_ipv6.c +1 -1
index bb09d5ccf599..ba7cd7d3d4da 100644
@@ -325,7 +325,7 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
 						 inet->inet_dport);
 		if (!tp->write_seq)
 			WRITE_ONCE(tp->write_seq, st.seq);
-		tp->tsoffset = st.ts_off;
+		WRITE_ONCE(tp->tsoffset, st.ts_off);
 	}
 
 	if (tcp_fastopen_defer_connect(sk, &err))
Reviewed-by
#2 lore raw
Quoted 6 lines · Jiayuan Chen

On Mon, Mar 09, 2026 at 07:26:04PM +0800, Wesley Atwell wrote:

You should use [PATCH net] instead of [PATCH] to indicate which tree this patch is intended for.

I saw the discrepancy while cooking

commit 165573e41f2f66ef98940cf65f838b2cb575d9d1 tcp: secure_seq:
add back ports to TS offset

For this reason, it is better to use net-next tree and avoid merge conflicts.

Note that TCP_TIMESTAMP getsockopt() is probably not used while another thread is doing a connect(),
it is mostly used for TCP_REPAIR. No need for stable backports.

Reviewed-by on v1 1/1
#3 lore raw
In reply to Wesley Atwell
Quoted 9 lines · Wesley Atwell

Commit dd23c9f1e8d5 ("tcp: annotate data-races around tp->tsoffset") updated do_tcp_getsockopt() to read tp->tsoffset with READ_ONCE() for TCP_TIMESTAMP because another CPU may change it concurrently.

tcp_v6_connect() still stores tp->tsoffset with a plain write. That store runs under lock_sock() via inet_stream_connect(), but the socket lock does not serialize a concurrent getsockopt(TCP_TIMESTAMP) from another task sharing the socket.

Quoted 5 lines

Use WRITE_ONCE() for the tcp_v6_connect() store so the connect-time writer matches the lockless TCP_TIMESTAMP reader. This also makes the IPv6 path consistent with tcp_v4_connect().

I think maintainer will apply it to net-next tree after noticing Eric's comment..

Quoted 17 lines
---
 net/ipv6/tcp_ipv6.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv6/tcp_ipv6.c +1 -1
index bb09d5ccf599..ba7cd7d3d4da 100644
@@ -325,7 +325,7 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
 						 inet->inet_dport);
 		if (!tp->write_seq)
 			WRITE_ONCE(tp->write_seq, st.seq);
-		tp->tsoffset = st.ts_off;
+		WRITE_ONCE(tp->tsoffset, st.ts_off);
 	}
 
 	if (tcp_fastopen_defer_connect(sk, &err))
#4 lore raw
In reply to Wesley Atwell
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 10 lines · On Mon, 9 Mar 2026 19:26:04 -0600 you wrote:

Commit dd23c9f1e8d5 ("tcp: annotate data-races around tp->tsoffset") updated do_tcp_getsockopt() to read tp->tsoffset with READ_ONCE() for TCP_TIMESTAMP because another CPU may change it concurrently.

tcp_v6_connect() still stores tp->tsoffset with a plain write. That store runs under lock_sock() via inet_stream_connect(), but the socket lock does not serialize a concurrent getsockopt(TCP_TIMESTAMP) from another task sharing the socket.

[...]

Here is the summary with links:
- tcp: use WRITE_ONCE() for tsoffset in tcp_v6_connect()

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

You are awesome, thank you!

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