Article View: linux.kernel.netdev
Article #19098Re: [PATCH net] tcp: fix negative sk_forward_alloc after MSG_ZEROCOPY append
From: Jiayuan Chen
Date: Thu, 19 Nov 2026 10:23
Date: Thu, 19 Nov 2026 10:23
110 lines
12828 bytes
12828 bytes
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.
Technical Details
Message-ID:
<4b841fff-d909-4bb4-bce8-9ab2be319ae1@linux.dev>Path:
mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-366941-linux-kernel-netdev=dewy.ruReferences:
<20261119090828.343748-1-jiayuan.chen@linux.dev> <CAL4Wiip2qPc5D_hRQb871m2cSHy-5++tNOOEr9sQYLhCxxknwA@mail.gmail.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-366941-linux-kernel-netdev=dewy.ru From: jiayuan.chen@linux.dev (Jiayuan Chen) Newsgroups: linux.kernel.netdev Subject: Re: [PATCH net] tcp: fix negative sk_forward_alloc after MSG_ZEROCOPY append Date: Thu, 19 Nov 2026 18:23:51 +0800 Organization: Usenet Network Message-ID: <4b841fff-d909-4bb4-bce8-9ab2be319ae1@linux.dev> References: <20261119090828.343748-1-jiayuan.chen@linux.dev> <CAL4Wiip2qPc5D_hRQb871m2cSHy-5++tNOOEr9sQYLhCxxknwA@mail.gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Info: usenet.network; logging-data="2847851"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:k2OK5H/5fOoz6ZDmOdtNXamraWc= sha256:L4hOOGiUPT3k+LzcXKYa+92Onb7bl9U79gOySsAfxW0= sha1:xujIM9T0rZZpSu4vNjK9NWxIk0A= sha256:sRFWsUzw4r05+kLiTYEJr/0kr/i7yWGE6fRp/k+1gUg= List-ID: <netdev.vger.kernel.org> X-Mailpost-Empty-Hdrs: X-Spam-Level X-Received: from localhost (localhost [127.0.0.1]) by mail.dewy.ru (Postfix) with ESMTP id 61FA01155BD; Thu, 19 Nov 2026 13:24:40 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -2.05 X-Spam-Status: No, score=-2.05 tagged_above=-9999 required=6.31 tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249, HK_RANDOM_FROM=1, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (1024-bit key) header.d=linux.dev X-Received: from mail.dewy.ru ([127.0.0.1]) by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id CnA9IFles8ON; Thu, 19 Nov 2026 13:24:35 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=104.64.211.4; helo=sin.lore.kernel.org; envelope-from=netdev+bounces-366941-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=linux.dev X-Received: from sin.lore.kernel.org (sin.lore.kernel.org [104.64.211.4]) by mail.dewy.ru (Postfix) with ESMTPS id 2F48C1155B5; Thu, 19 Nov 2026 13:24:26 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sin.lore.kernel.org (Postfix) with ESMTP id 6B476248108; Thu, 19 Nov 2026 10:24:07 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795083844; b=PE4mYFeKvZ0C/jAN/VZ6mSwBn7ixDHnqnLHx21rT8KL623C1m8GL2Tq7qWs/qJ6zHV6o IYZEPAB25REXxZ9F1E/WyLAoppKpfvyTO7s/JIvMXa+Ae90D8tgr9FqXa/zQzmDRtcdzD fFyjOq5iV6L6sUeTnDzj4WIDsdZmgO9iLwugDF8x7h/6yU+sqpeBRlwbb76/Qt1W/unMd 2Ru8PWCJdRP9IcjQTyff1mavfV+OAvbCAW3kWUoUEnCYHDt5Otvr2JDq8TzEMJual0q2H QdYeWN2/ltKwG4FCNDdms0uAVAiUOgH6Z6q2BoVaHyJQPT0XgelLFkWFMFj0sbnAEig== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795083844; h=X-Original-To:Received:DMARC-Filter:X-Envelope-To:DKIM-Signature: X-Envelope-To:Received:X-Mizu-Trace-ID:X-Migadu-Flow:Message-ID:Date: X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe: List-Unsubscribe:List-Owner:Precedence:MIME-Version:User-Agent: Subject:To:Cc:References:From:In-Reply-To:Content-Type: Content-Transfer-Encoding; bh=xlbmEaq499875rOI5vwNWFDvgLSY3rciI0JnxyKuoac=; b=B0wZNrtbNObu1vJ7a0OV+G7tC844VgqaGHHPPL+yGtqijIbtpq3o8wDTwL++NfgCR+2s qlUG+VPajhG+QpzOOsdVivSHAT0pdDYeoCQjP2WWhb8+0rJZGPMY85wP/WDwcjASYcvo/ XO1Y/QuEFOsCSLCjP7/B2T5XyVLuCfC+L1UaF9743HnuftAXpQoC6cB1ALG3wIv+tL84g GSvl+WENix4pR+Y8bR+UqNM0nngnMNhO/amHuEhRarvAfyOxXqWq0rQ1/hg+nQEEtEQ4b +md9alSNYnLiX6eQrzPnh2E4tYUSqjmaWfWvZlz23ewnOUKDkhwjPfWy36l+FlXY9HQ== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b="CNK/9gmJ"; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 290B2606A334; Thu, 19 Nov 2026 10:24:04 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=CNK/9gmJ X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.79 X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795083843; b=GCZoQES/166CoM/aXxjKwKyk8eBuMhQFpodXKuZ34Zshq86TZoAWWbO6SXz2lBh6uSAa 3MhYWc6oRS6ZDzOfn+ukOq/L/e/9PSRD7HjhMyFqNg4zDZv7e5znh+TmXvKcM2eTHlpIE rKMikRJPg3btRnlVhHDVJVFn4vP+hHDVWNKR1EBcHx53WB+TgeJGoarLRBrdHLEZCealG kktPmuaQB15bXj4rMC2xlTIXFXK0KU/jOiTJQmXInJWdL1H3HVlsFe4xR0nGvt/rMav4Y /6WNjqR5bD4NQyZbNuZklz1t8aP2MAO/FFd12p0ly8e6Gd5nBknfmgiUEjEp91zyIlw== X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795083843; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:Message-ID:Date:MIME-Version:User-Agent: Subject:To:Cc:References:From:In-Reply-To:Content-Type: Content-Transfer-Encoding; bh=xlbmEaq499875rOI5vwNWFDvgLSY3rciI0JnxyKuoac=; b=ginTG52ySOuUYgIBBTAM99hQ3dQT5j9RtFFbs2OBray63EeMPMD1umdt+3fNzqOaj5lv ubO66pBU2lsVCCDLIfYm4AfWrGq3lspxB5L8DsUj/QbbPOdXmgLM5w6O9VzcH+D1Jm9sY BSUws7LeV22p0oZ/bogfeuRegjf6dgcKrOIpL92QZPB+oGjjAzGW8fph4ptVojhjGb0hK 5gUzgiKawN9VD6ozlaDHzbqypP+O+sBzNX9l5JuAH16H+p0NTP1Kb0k8jA3yG5Ym31zRu fFaJHs2v/fTeJIAfe57LVFXvVqzt7nYDHzlO4XMGpLzcuUc1Ajb1f5JP71H1AL1rOyA== X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b="CNK/9gmJ"; arc=none smtp.remote-ip=91.218.175.79 X-Received: from mta0.migadu.com (out-79.mta0.migadu.com [91.218.175.79]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6DC25606A326 for <netdev@vger.kernel.org>; Thu, 19 Nov 2026 10:24:02 +0000 (UTC) X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6DC25606A326 X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev X-Envelope-To: netdev@vger.kernel.org X-DKIM-Signature: a=rsa-sha256; bh=9tehFAS2+iUu3i78g+gaEId15gNW5inSOvWWXgrmAFA=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795083840; v=1; x=1795688640; b=CNK/9gmJ4K2rBn8qVe8XbQ8LUxsT+b44lJjgXDRUSJj2x7X55GYpnmrAH3cdNClS9fsI04Rb oniGWBz9OeKORxOjHUddSSTo7qU+ZfKFxAvfV2M+4s9MSUHWWaybVcfK8VFiy7ndmSo62H+h8Om 7NL6ySlMR0X3Ad7PWdskLiGM= X-Envelope-To: netdev@vger.kernel.org X-Received: by smtp.migadu.com with ESMTPS id 2ed0ae7ef4b3ef77; Thu, 19 Nov 2026 10:24:00 +0000 X-Mizu-Trace-ID: 2ed0ae7ef4b3ef77 X-Migadu-Flow: FLOW_OUT X-Mailing-List: netdev@vger.kernel.org X-List-Post: <mailto:netdev@vger.kernel.org> X-List-Help: <mailto:netdev+help@vger.kernel.org> X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org> X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org> X-List-Owner: <mailto:netdev+owner@vger.kernel.org> X-Precedence: list X-User-Agent: Mozilla Thunderbird X-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 X-In-Reply-To: <CAL4Wiip2qPc5D_hRQb871m2cSHy-5++tNOOEr9sQYLhCxxknwA@mail.gmail.com>