diff mbox series

[net-next,v2] net/tcp: optimise locking for blocking splice

Message ID 80736a2cc6d478c383ea565ba825eaf4d1abd876.1687523671.git.asml.silence@gmail.com (mailing list archive)
State Accepted
Commit 2fe11c9d36ee2f3dc3c642588c5d9a22190674c9
Delegated to: Netdev Maintainers
Headers show
Series [net-next,v2] net/tcp: optimise locking for blocking splice | expand

Checks

Context Check Description
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 10 this patch: 10
netdev/cc_maintainers success CCed 6 of 6 maintainers
netdev/build_clang success Errors and warnings before: 8 this patch: 8
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 10 this patch: 10
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 8 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Pavel Begunkov June 23, 2023, 12:38 p.m. UTC
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 <asml.silence@gmail.com>
---

v2: go with Paolo's suggestion
    aggressively shrink the patch

 net/ipv4/tcp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Eric Dumazet June 23, 2023, 2:17 p.m. UTC | #1
On Fri, Jun 23, 2023 at 2:40 PM Pavel Begunkov <asml.silence@gmail.com> 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.
>
> Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
> ---
>
> v2: go with Paolo's suggestion
>     aggressively shrink the patch
>
>  net/ipv4/tcp.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
> index 71b42eef9dbf..d56edc2c885f 100644
> --- a/net/ipv4/tcp.c
> +++ b/net/ipv4/tcp.c
> @@ -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.

Reviewed-by: Eric Dumazet <edumazet@google.com>

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 ?

diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
index 71b42eef9dbf527098963bc03deecf55042e2021..d03d38060944d63d2728a7bf90a5c117b7852d8b
100644
--- a/net/ipv4/tcp.c
+++ b/net/ipv4/tcp.c
@@ -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) ||
Pavel Begunkov June 23, 2023, 3:03 p.m. UTC | #2
On 6/23/23 15:17, Eric Dumazet wrote:
> On Fri, Jun 23, 2023 at 2:40 PM Pavel Begunkov <asml.silence@gmail.com> 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.
>>
>> Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
>> ---
>>
>> v2: go with Paolo's suggestion
>>      aggressively shrink the patch
>>
>>   net/ipv4/tcp.c | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
>> index 71b42eef9dbf..d56edc2c885f 100644
>> --- a/net/ipv4/tcp.c
>> +++ b/net/ipv4/tcp.c
>> @@ -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.
> 
> Reviewed-by: Eric Dumazet <edumazet@google.com>
> 
> 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.


> diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
> index 71b42eef9dbf527098963bc03deecf55042e2021..d03d38060944d63d2728a7bf90a5c117b7852d8b
> 100644
> --- a/net/ipv4/tcp.c
> +++ b/net/ipv4/tcp.c
> @@ -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) ||
patchwork-bot+netdevbpf@kernel.org June 24, 2023, 10:30 p.m. UTC | #3
Hello:

This patch was applied to netdev/net-next.git (main)
by Jakub Kicinski <kuba@kernel.org>:

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!
diff mbox series

Patch

diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
index 71b42eef9dbf..d56edc2c885f 100644
--- a/net/ipv4/tcp.c
+++ b/net/ipv4/tcp.c
@@ -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);