diff mbox series

[net] tcp: fix mem under-charging with zerocopy sendmsg()

Message ID 20220201065254.680532-1-eric.dumazet@gmail.com (mailing list archive)
State Accepted
Commit 479f5547239d970d3833f15f54a6481fffdb91ec
Delegated to: Netdev Maintainers
Headers show
Series [net] tcp: fix mem under-charging with zerocopy sendmsg() | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for net
netdev/fixes_present success Fixes tag present in non-next series
netdev/subject_prefix success Link
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 2 this patch: 2
netdev/cc_maintainers warning 2 maintainers not CCed: dsahern@kernel.org yoshfuji@linux-ipv6.org
netdev/build_clang success Errors and warnings before: 18 this patch: 18
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 7 this patch: 7
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 15 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Eric Dumazet Feb. 1, 2022, 6:52 a.m. UTC
From: Eric Dumazet <edumazet@google.com>

We got reports of following warning in inet_sock_destruct()

	WARN_ON(sk_forward_alloc_get(sk));

Whenever we add a non zero-copy fragment to a pure zerocopy skb,
we have to anticipate that whole skb->truesize will be uncharged
when skb is finally freed.

skb->data_len is the payload length. But the memory truesize
estimated by __zerocopy_sg_from_iter() is page aligned.

Fixes: 9b65b17db723 ("net: avoid double accounting for pure zerocopy skbs")
Signed-off-by: Eric Dumazet <edumazet@google.com>
Cc: Talal Ahmad <talalahmad@google.com>
Cc: Arjun Roy <arjunroy@google.com>
Cc: Soheil Hassas Yeganeh <soheil@google.com>
Cc: Willem de Bruijn <willemb@google.com>
---
 net/ipv4/tcp.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

Comments

Soheil Hassas Yeganeh Feb. 1, 2022, 4:10 p.m. UTC | #1
On Tue, Feb 1, 2022 at 1:53 AM Eric Dumazet <eric.dumazet@gmail.com> wrote:
>
> From: Eric Dumazet <edumazet@google.com>
>
> We got reports of following warning in inet_sock_destruct()
>
>         WARN_ON(sk_forward_alloc_get(sk));
>
> Whenever we add a non zero-copy fragment to a pure zerocopy skb,
> we have to anticipate that whole skb->truesize will be uncharged
> when skb is finally freed.
>
> skb->data_len is the payload length. But the memory truesize
> estimated by __zerocopy_sg_from_iter() is page aligned.
>
> Fixes: 9b65b17db723 ("net: avoid double accounting for pure zerocopy skbs")
> Signed-off-by: Eric Dumazet <edumazet@google.com>
> Cc: Talal Ahmad <talalahmad@google.com>
> Cc: Arjun Roy <arjunroy@google.com>
> Cc: Soheil Hassas Yeganeh <soheil@google.com>
> Cc: Willem de Bruijn <willemb@google.com>

Acked-by: Soheil Hassas Yeganeh <soheil@google.com>

Nice catch! and thank you for the fix!


> ---
>  net/ipv4/tcp.c | 7 +++++--
>  1 file changed, 5 insertions(+), 2 deletions(-)
>
> diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
> index 78e81465f5f3632f54093495d2f2a064e60c7237..bdf108f544a45a2aa24bc962fb81dfd0ca1e0682 100644
> --- a/net/ipv4/tcp.c
> +++ b/net/ipv4/tcp.c
> @@ -1322,10 +1322,13 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size)
>
>                         /* skb changing from pure zc to mixed, must charge zc */
>                         if (unlikely(skb_zcopy_pure(skb))) {
> -                               if (!sk_wmem_schedule(sk, skb->data_len))
> +                               u32 extra = skb->truesize -
> +                                           SKB_TRUESIZE(skb_end_offset(skb));
> +
> +                               if (!sk_wmem_schedule(sk, extra))
>                                         goto wait_for_space;
>
> -                               sk_mem_charge(sk, skb->data_len);
> +                               sk_mem_charge(sk, extra);
>                                 skb_shinfo(skb)->flags &= ~SKBFL_PURE_ZEROCOPY;
>                         }
>
> --
> 2.35.0.rc2.247.g8bbb082509-goog
>
patchwork-bot+netdevbpf@kernel.org Feb. 2, 2022, 4:30 a.m. UTC | #2
Hello:

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

On Mon, 31 Jan 2022 22:52:54 -0800 you wrote:
> From: Eric Dumazet <edumazet@google.com>
> 
> We got reports of following warning in inet_sock_destruct()
> 
> 	WARN_ON(sk_forward_alloc_get(sk));
> 
> Whenever we add a non zero-copy fragment to a pure zerocopy skb,
> we have to anticipate that whole skb->truesize will be uncharged
> when skb is finally freed.
> 
> [...]

Here is the summary with links:
  - [net] tcp: fix mem under-charging with zerocopy sendmsg()
    https://git.kernel.org/netdev/net/c/479f5547239d

You are awesome, thank you!
diff mbox series

Patch

diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
index 78e81465f5f3632f54093495d2f2a064e60c7237..bdf108f544a45a2aa24bc962fb81dfd0ca1e0682 100644
--- a/net/ipv4/tcp.c
+++ b/net/ipv4/tcp.c
@@ -1322,10 +1322,13 @@  int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size)
 
 			/* skb changing from pure zc to mixed, must charge zc */
 			if (unlikely(skb_zcopy_pure(skb))) {
-				if (!sk_wmem_schedule(sk, skb->data_len))
+				u32 extra = skb->truesize -
+					    SKB_TRUESIZE(skb_end_offset(skb));
+
+				if (!sk_wmem_schedule(sk, extra))
 					goto wait_for_space;
 
-				sk_mem_charge(sk, skb->data_len);
+				sk_mem_charge(sk, extra);
 				skb_shinfo(skb)->flags &= ~SKBFL_PURE_ZEROCOPY;
 			}