tcp: make smp_rmb() conditional in tcp_poll()
Subsystems (3)
PATCH 1/1 tcp: make smp_rmb() conditional in tcp_poll()
Commit a4d258036ed9 ("tcp: Fix race in tcp_poll") added smp_rmb() in tcp_poll() and smp_wmb() in tcp_reset() (now tcp_done_with_error()) to ensure that if tcp_poll() observed socket closure, it would also observe sk->sk_err.
Currently, tcp_poll() unconditionally executes smp_rmb() at the end of every invocation, which on weakly-ordered architectures such as ARM64 emits a memory barrier instruction (dmb ishld) on the poll fast path, even for healthy, active sockets.
However, tcp_poll() only needs this barrier if socket closure has been observed, to ensure that the error code set by tcp_done_with_error() before socket closure is visible before returning EPOLLERR.
Move smp_rmb() inside the conditional block handling socket closure (shutdown == SHUTDOWN_MASK || state == TCP_CLOSE). For healthy connected sockets in epoll, tcp_poll() avoids the barrier entirely.
--- net/ipv4/tcp.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
net/ipv4/tcp.c +6 -3
@@ -578,8 +578,13 @@ __poll_t tcp_poll(struct file *file, struct socket *sock, poll_table *wait)* blocking on fresh not-connected or disconnected socket. --ANK */ shutdown =READ_ONCE (sk->sk_shutdown); -if (shutdown ==SHUTDOWN_MASK || state ==TCP_CLOSE ) +if (shutdown ==SHUTDOWN_MASK || state ==TCP_CLOSE ) { mask |=EPOLLHUP ; +/* Coupled with smp_wmb() in tcp_done_with_error() to ensure +* sk->sk_err is visible if socket closure was observed. +*/ +smp_rmb (); + }if (shutdown &RCV_SHUTDOWN ) mask |=EPOLLIN |EPOLLRDNORM |EPOLLRDHUP ;
@@ -626,8 +631,6 @@ __poll_t tcp_poll(struct file *file, struct socket *sock, poll_table *wait)*/ mask |=EPOLLOUT |EPOLLWRNORM ; } -/* This barrier is coupled with smp_wmb() in tcp_done_with_error() */ -smp_rmb ();if (READ_ONCE (sk->sk_err) || !skb_queue_empty_lockless (&sk->sk_error_queue)) mask |=EPOLLERR ;
Signature · 1 line
2.55.0.1007.g17ff1f9808-goog
Quoted 14 lines · Eric Dumazet
Commit
a4d258036ed9("tcp: Fix race intcp_poll") addedsmp_rmb()intcp_poll()andsmp_wmb()intcp_reset()(nowtcp_done_with_error()) to ensure that iftcp_poll()observed socket closure, it would also observesk->sk_err.Currently,
tcp_poll()unconditionally executessmp_rmb()at the end of every invocation, which on weakly-ordered architectures such as ARM64 emits a memory barrier instruction (dmb ishld) on the poll fast path, even for healthy, active sockets.However,
tcp_poll()only needs this barrier if socket closure has been observed, to ensure that the error code set bytcp_done_with_error()before socket closure is visible before returning EPOLLERR.
Quoted 5 lines
Move
smp_rmb()inside the conditional block handling socket closure (shutdown ==SHUTDOWN_MASK|| state ==TCP_CLOSE). For healthy connected sockets in epoll,tcp_poll()avoids the barrier entirely.Signed-off-by Eric Dumazet <[email protected]>
This patch was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:
Hello:
This patch was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:
Quoted 11 lines · On Sun, 13 Sep 2026 12:32:24 +0000 you wrote:
Commit
a4d258036ed9("tcp: Fix race intcp_poll") addedsmp_rmb()intcp_poll()andsmp_wmb()intcp_reset()(nowtcp_done_with_error()) to ensure that iftcp_poll()observed socket closure, it would also observesk->sk_err.Currently,
tcp_poll()unconditionally executessmp_rmb()at the end of every invocation, which on weakly-ordered architectures such as ARM64 emits a memory barrier instruction (dmb ishld) on the poll fast path, even for healthy, active sockets.[...]
Here is the summary with links:
- [net-next] tcp: make smp_rmb() conditional in tcp_poll()
https://git.kernel.org/netdev/net-next/c/e8c06b895281
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html