mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: jiayuan.chen@linux.dev
Cc: netdev@vger.kernel.org, edumazet@kernel.org,
	ncardwell@google.com, kuniyu@google.com, davem@davemloft.net,
	kuba@kernel.org, pabeni@redhat.com, horms@kernel.org,
	weiwan@google.com, soheil@google.com, shakeel.butt@linux.dev,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH net] tcp: fix negative sk_forward_alloc after MSG_ZEROCOPY append
Date: Fri, 20 Nov 2026 09:57:42 +0000	[thread overview]
Message-ID: <179516866272.3136.232647069823092530@kernel.org> (raw)
In-Reply-To: <20261119090828.343748-1-jiayuan.chen@linux.dev>

Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 1

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Medium] The new sk_forced_mem_schedule(sk, 0) only runs on the success
  path of skb_zerocopy_iter_stream().
- [Low] In tcp_sendmsg_locked() (net/ipv4/tcp.c), the MSG_ZEROCOPY branch
  only pre-schedules memory when the skb is not pure: `if…

--

tcp: fix negative sk_forward_alloc after MSG_ZEROCOPY append

The MSG_ZEROCOPY path in tcp_sendmsg_locked() can append to a skb that
already holds copied data. In that case only the user length is
scheduled, but the pinned pages are charged by truesize. This commit adds
sk_forced_mem_schedule(sk, 0) after a successful skb_zerocopy_iter_stream()
so that sk_forward_alloc does not go negative.

> diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
> index 832b25ddd3c2e..9f8c0954001bb 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);

[Severity: Medium]
Can sk_forward_alloc still go negative on the error path? The new call
only runs when skb_zerocopy_iter_stream() succeeds, because the
"if (err < 0) goto do_error" check comes first.

In skb_zerocopy_iter_stream(), the truesize charge made by
__zerocopy_sg_from_iter() is undone on -EFAULT only if ___pskb_trim()
succeeds:

net/core/skbuff.c:skb_zerocopy_iter_stream() {
    ...
	if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) {
		...
		if (!___pskb_trim(skb, orig_len)) {
			skb->truesize -= delta_truesize;
			sk_wmem_queued_add(sk, -delta_truesize);
			if (!skb_zcopy_pure(skb))
				sk_mem_uncharge(sk, delta_truesize);
		}
		skb->sk = save_sk;
		return err;
	}
    ...
}

___pskb_trim() fails when the skb is cloned and
pskb_expand_head(skb, 0, 0, GFP_ATOMIC) fails.

Suppose the write queue tail skb is not pure, so only copy was scheduled,
and it is cloned. If the user iovec faults after some pages are pinned and
the atomic allocation fails, those pages stay charged by truesize. The
caller then jumps to do_error without running the fix-up.

A later non-zerocopy sendmsg() under tcp_mem pressure would then get a
negative copy back from tcp_wmem_schedule(). That is the same case the
commit message describes.

Would it work to call sk_forced_mem_schedule(sk, 0) right after
skb_zerocopy_iter_stream() returns, before the error checks? On the paths
that do undo the charge, the call would do nothing.

[Severity: Low]
This isn't a bug, but does the new comment hold for pure zerocopy skbs?
Earlier in this branch, the pre-schedule is guarded:

	if (!skb_zcopy_pure(skb)) {
		copy = tcp_wmem_schedule(sk, copy);
		if (!copy)
			goto wait_for_space;
	}

__zerocopy_sg_from_iter() also skips the charge for pure skbs:

net/core/datagram.c:__zerocopy_sg_from_iter() {
    ...
		sk_wmem_queued_add(sk, truesize);
		if (!skb_zcopy_pure(skb))
			sk_mem_charge(sk, truesize);
    ...
}

So when SKBFL_PURE_ZEROCOPY is set, no pages were charged to
sk_forward_alloc. sk_forced_mem_schedule(sk, 0) is still called anyway.

In that case it does nothing, unless sk_forward_alloc is already negative
for some unrelated reason. Then it would force-charge without any warning
and hide that imbalance.

Could the call use the same !skb_zcopy_pure(skb) check as the
pre-schedule? Alternatively, the comment could say it covers the non-pure
case.

>  		} else if (zc == MSG_SPLICE_PAGES) {
>  			/* Splice in data if we can; copy if we can't. */
>  			if (tcp_downgrade_zcopy_pure(sk, skb))

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261119090828.343748-1-jiayuan.chen%40linux.dev

      parent reply	other threads:[~2026-11-20  9:57 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19  9:08 Jiayuan Chen
2026-11-19 10:06 ` Eric Dumazet
2026-11-19 10:23   ` Jiayuan Chen
2026-11-20  9:57 ` netdev-bot+sashiko [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=179516866272.3136.232647069823092530@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=jiayuan.chen@linux.dev \
    --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

all inboxes | Powered by JetHome®