Netdev List
 help / color / mirror / Atom feed
From: Jiayuan Chen <jiayuan.chen@linux.dev>
To: Eric Dumazet <edumazet@kernel.org>
Cc: netdev@vger.kernel.org, Neal Cardwell <ncardwell@google.com>,
	Kuniyuki Iwashima <kuniyu@google.com>,
	"David S. Miller" <davem@davemloft.net>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Simon Horman <horms@kernel.org>, Wei Wang <weiwan@google.com>,
	Soheil Hassas Yeganeh <soheil@google.com>,
	Shakeel Butt <shakeel.butt@linux.dev>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH net] tcp: fix negative sk_forward_alloc after MSG_ZEROCOPY append
Date: Thu, 19 Nov 2026 18:23:51 +0800	[thread overview]
Message-ID: <4b841fff-d909-4bb4-bce8-9ab2be319ae1@linux.dev> (raw)
In-Reply-To: <CAL4Wiip2qPc5D_hRQb871m2cSHy-5++tNOOEr9sQYLhCxxknwA@mail.gmail.com>


On 11/19/26 6:06 PM, Eric Dumazet wrote:
> Le jeu. 19 nov. 2026 à 10:08, Jiayuan Chen <jiayuan.chen@linux.dev> a écrit :
>> Our local syzbot reported the following warning:
>>
>>   WARNING: ./include/linux/ucopysize.h:57 at tcp_sendmsg_locked+0x1e13/0x1f20
>>   Modules linked in:
>>   CPU: 3 UID: 0 PID: 377 Comm: syz.2.2248 Not tainted 7.4.0-rc2+ #398 PREEMPT
>>   RIP: 0010:tcp_sendmsg_locked+0x1e13/0x1f20
>>   Call Trace:
>>    <TASK>
>>    tcp_sendmsg+0x2c/0x50
>>    inet_sendmsg+0x62/0xb0
>>    __sys_sendto+0x2fe/0x350
>>    __x64_sys_sendto+0x7f/0xb0
>>    x64_sys_call+0x1375/0x27e0
>>    do_syscall_64+0xe4/0x470
>>    entry_SYSCALL_64_after_hwframe+0x76/0x7e
>>    </TASK>
>>
>> The problem is here:
>>
>>   if (!skb_zcopy_pure(skb)) {
>>           copy = tcp_wmem_schedule(sk, copy);
>>           if (!copy)
>>                   goto wait_for_space;
>>   }
>>
>> When MSG_ZEROCOPY data is appended to a skb that already holds copied
>> data, only the user length is scheduled, but the pinned pages are then
>> charged by their truesize:
>>
>>  skb_zerocopy_iter_stream
>>    __zerocopy_sg_from_iter
>>     zerocopy_fill_skb_from_iter  //truesize += PAGE_ALIGN(copied + start)
>>     sk_mem_charge(sk, truesize)  //sk_forward_alloc goes negative
>>
>> For example, sending 8 iovecs of 1 byte each schedules 8 bytes and
>> charges 32768 with 4K pages.
> This part is a separate bug: these 8 bytes sit in a single page.
>
> zerocopy_fill_skb_from_iter() adds PAGE_ALIGN(copied + start) to
> skb->truesize for each iov_iter_get_pages2() call, even when the
> chunk is merged into the previous frag and the page reference is
> released.
>
> With a corked socket, one copied byte, then one MSG_ZEROCOPY sendmsg()
> of 1024 bytes from a single page:
>
>   1 iovec             sk_wmem_queued += 4096
>   1024 iovecs         sk_wmem_queued += 4194304   (sk_sndbuf is 2626560)
>
> With your patch, this over-estimation would become a forced charge,
> above tcp_mem and memcg limits, with an amount controlled by the user.
>
> I have a fix [1] for this accounting (only charge the pages a frag did not
> already cover), I will send it shortly. With it, both cases above
> charge 4096 bytes, and sk_forward_alloc is still negative after the
> append, so your patch is still needed.


Thanks for the fix.


> Thanks.
>
>> On a later sendmsg() without MSG_ZEROCOPY, if sk_wmem_schedule() fails
>> (e.g. tcp_mem hard limit is reached), tcp_wmem_schedule() returns
>> min(copy, sk->sk_forward_alloc), which is negative. The negative copy
>> is passed to skb_copy_to_page_nocache() and triggers the warning, and
>> sendmsg() fails with -EFAULT.
>>
>> A few options were considered:
>>
>> 1) Schedule the truesize up front. It is not known before the pages
>>    are pinned, it depends on the iovec layout.
>> 2) Do not append zerocopy data to a non pure skb. This splits a small
>>    header and its zerocopy payload into two skbs.
>> 3) Schedule the missing part right after the pages are charged. They
>>    are already in the skb at this point, so this can not fail.
>>
>> Go with 3).
> Analysis looks correct, and 3) is consistent with what we did
> for tcp_collapse() recently.
>
>> Fixes: 849b425cd091 ("tcp: fix possible freeze in tx path under memory pressure")
>> Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
>> ---
>>  net/ipv4/tcp.c | 2 ++
>>  1 file changed, 2 insertions(+)
>>
>> diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
>> index d510e982dcea..8bd377542a89 100644
>> --- a/net/ipv4/tcp.c
>> +++ b/net/ipv4/tcp.c
>> @@ -1359,6 +1359,8 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size)
>>                         if (err < 0)
>>                                 goto do_error;
>>                         copy = err;
>> +                       /* Pages were charged by truesize, not by copy. */
>> +                       sk_forced_mem_schedule(sk, 0);
> Pure zerocopy skbs are not charged, so there is nothing to fix for
> them. Please avoid the extra work in this case:
>
>                         if (!skb_zcopy_pure(skb))
>                                 sk_forced_mem_schedule(sk, 0);


Will do in v2.


      reply	other threads:[~2026-11-19 10:24 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19  9:08 [PATCH net] tcp: fix negative sk_forward_alloc after MSG_ZEROCOPY append Jiayuan Chen
2026-11-19 10:06 ` Eric Dumazet
2026-11-19 10:23   ` Jiayuan Chen [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4b841fff-d909-4bb4-bce8-9ab2be319ae1@linux.dev \
    --to=jiayuan.chen@linux.dev \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ncardwell@google.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=shakeel.butt@linux.dev \
    --cc=soheil@google.com \
    --cc=weiwan@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox