net/tcp: optimise locking for blocking splice
Subsystems (3)
PATCH v2 1/1 net/tcp: optimise locking for blocking splice
Even when tcp_splice_read() reads all it was asked for, for blocking sockets it'll release and immediately regrab the socket lock, loop around and break on the while check.
Check tss.len right after we adjust it, and return if we're done. That saves us one release_sock(); lock_sock(); pair per successful blocking splice read.
---
v2: go with Paolo's suggestion
aggressively shrink the patch
net/ipv4/tcp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/tcp.c +1 -1
@@ -839,7 +839,7 @@ ssize_t tcp_splice_read(struct socket *sock, loff_t *ppos, tss.len -= ret; spliced += ret; -if (!timeo) +if (!tss.len || !timeo)break ;release_sock (sk);lock_sock (sk);
Signature · 1 line
2.40.0
Quoted 23 lines · Pavel Begunkov
Even when
tcp_splice_read()reads all it was asked for, for blocking sockets it'll release and immediately regrab the socket lock, loop around and break on the while check.Check tss.len right after we adjust it, and return if we're done. That saves us one
release_sock();lock_sock(); pair per successful blocking splice read.Signed-off-by Pavel Begunkov <[email protected]>---
v2: go with Paolo's suggestion
aggressively shrink the patchnet/ipv4/tcp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)net/ipv4/tcp.c +0
-0
net/ipv4/tcp.c ssize_t tcp_splice_read(struct socket *sock, loff_t *ppos, · line 845
Quoted 8 lines
net/ipv4/tcp.c +1
-1@@ -839,7 +839,7 @@ ssize_t tcp_splice_read(struct socket *sock, loff_t *ppos, tss.len -= ret; spliced += ret; -if (!timeo) +if (!tss.len || !timeo)break ;release_sock (sk);lock_sock (sk);
SGTM, thanks.
I wonder if the "release_sock();sock_lock();" could be replaced by sk_flush_backlog() anyway ?
Or is there any other reason for this dance ?
net/ipv4/tcp.c +0 -0
100644
net/ipv4/tcp.c +1 -2
@@ -841,8 +841,7 @@ ssize_t tcp_splice_read(struct socket *sock, loff_t *ppos,if (!timeo)break ; -release_sock (sk); -lock_sock (sk); +sk_flush_backlog (); if (sk->sk_err || sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN) ||
Quoted 35 lines · Eric Dumazet
Quoted 23 lines · Pavel Begunkov
Even when
tcp_splice_read()reads all it was asked for, for blocking sockets it'll release and immediately regrab the socket lock, loop around and break on the while check.Check tss.len right after we adjust it, and return if we're done. That saves us one
release_sock();lock_sock(); pair per successful blocking splice read.Signed-off-by Pavel Begunkov <[email protected]>---
v2: go with Paolo's suggestion
aggressively shrink the patchnet/ipv4/tcp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)net/ipv4/tcp.c +0
-0net/ipv4/tcp.c ssize_t tcp_splice_read(struct socket *sock, loff_t *ppos, · line 845
Quoted 8 lines
net/ipv4/tcp.c +1
-1@@ -839,7 +839,7 @@ ssize_t tcp_splice_read(struct socket *sock, loff_t *ppos, tss.len -= ret; spliced += ret; -if (!timeo) +if (!tss.len || !timeo)break ;release_sock (sk);lock_sock (sk);SGTM, thanks.
Quoted 5 lines
Reviewed-by Eric Dumazet <[email protected]>I wonder if the "release_sock();sock_lock();" could be replaced by
sk_flush_backlog()anyway ?
Or is there any other reason for this dance ?
Now as you mentioned, it definitely sounds like that. And the code is 15 years old, perhaps nobody was paying attention.
Quoted 15 lines
net/ipv4/tcp.c +0
-0100644
net/ipv4/tcp.c +1
-2@@ -841,8 +841,7 @@ ssize_t tcp_splice_read(struct socket *sock, loff_t *ppos,if (!timeo)break ; -release_sock (sk); -lock_sock (sk); +sk_flush_backlog (); if (sk->sk_err || sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN) ||
Signature · 1 line
Pavel Begunkov
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 9 lines · On Fri, 23 Jun 2023 13:38:55 +0100 you wrote:
Even when
tcp_splice_read()reads all it was asked for, for blocking sockets it'll release and immediately regrab the socket lock, loop around and break on the while check.Check tss.len right after we adjust it, and return if we're done. That saves us one
release_sock();lock_sock(); pair per successful blocking splice read.[...]
Here is the summary with links:
- [net-next,v2] net/tcp: optimise locking for blocking splice
https://git.kernel.org/netdev/net-next/c/2fe11c9d36ee
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html