Netdev List
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: zihanx@nebusec.ai
Cc: netdev@vger.kernel.org, linux-doc@vger.kernel.org,
	davem@davemloft.net, edumazet@kernel.org, pabeni@redhat.com,
	willemb@google.com, horms@kernel.org, rjethwani@purestorage.com,
	zirajs7@gmail.com, leitao@debian.org, brauner@kernel.org,
	jiayuan.chen@shopee.com, runyu.xiao@seu.edu.cn,
	tom@herbertland.com, corbet@lwn.net, skhan@linuxfoundation.org,
	rdunlap@infradead.org, vega@nebusec.ai, kuba@kernel.org
Subject: Re: [PATCH net v2 1/2] net: kcm: account retained message memory
Date: Sat, 05 Dec 2026 03:39:51 +0000	[thread overview]
Message-ID: <179644199129.3146.4441563389803400930@kernel.org> (raw)
In-Reply-To: <c3949ece3d30e646f24bbd69a8af542a6285ede2.1796043121.git.zihanx@nebusec.ai>

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

Critical: 0 · High: 2 · Medium: 4 · Low: 0

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).

- [High] The non-splice path in kcm_sendmsg() (net/kcm/kcmsock.c) charges
  every new page-frag fragment the whole pfrag->size: `padding =…
- [High] kcm_splice_from_iter() (net/kcm/kcmsock.c) sets `charge =
  folio_size(page_folio(page))` for each page that does not coalesce.
- [Medium] The patch carries 'Cc: stable@vger.kernel.org' but is about
  500 lines (445 insertions, 52 deletions).
- [Medium] kcm_sendmsg() checks for space using SKB_TRUESIZE(0), as
  `skb_charge` for the head skb (line 1002) and `tskb_charge` for…
- [Medium] In kcm_sendmsg(), `skb_charge` is declared `size_t` (line
  965).
- [Medium] net: kcm: missing min_space check in kcm_wait_memory() and
  wait_for_space

--

net: kcm: account retained message memory

This commit makes kcm_sendmsg() charge per-message skb truesize and a
conservative page backing amount (a whole pfrag or a whole folio) to
sk_wmem_queued. The charge is tracked in kcm_tx_msg->wmem_charge and
released when the message is sent or discarded. It also adds the
kcm_splice_from_iter() and kcm_wait_memory() helpers, plus new -EMSGSIZE
exits when a message would grow past the sndbuf recorded for it.

Link: https://bugtracker.nebusec.ai/f/12034

> kcm_sendmsg() charges copied payload bytes, but fragmented messages
> also retain page backing and skb metadata. A message made from small
> fragments can therefore consume substantially more memory than its
> payload charge while staying within sk_sndbuf.

[ ... ]

> Cc: stable@vger.kernel.org

[Severity: Medium]
This is tagged for stable, but it is roughly 500 lines (445 insertions,
52 deletions). Documentation/process/stable-kernel-rules.rst says a stable
patch cannot be bigger than 100 lines with context, and must be obviously
correct.

The patch also makes several user-visible changes:

- it forks sk_stream_wait_memory() into kcm_wait_memory() with new
  -EMSGSIZE exits
- it replaces skb_splice_from_iter() with kcm_splice_from_iter()
- it adds a per-message sndbuf cap
- it adds full pfrag and folio-size charging

Together these turn sends that used to succeed into -EMSGSIZE. The commit
message doesn't mention any of them.

The follow-up commit "net: kcm: document transmit memory budget" documents
the new EMSGSIZE semantics. It is not tagged for stable, so stable trees
would get the behaviour change without the documentation.

Is "while staying within sk_sndbuf" accurate for the old behaviour? Before
this patch, sk_stream_memory_free() was only checked before the head skb
was allocated. A single message's payload was never limited by
sk_sndbuf.

> diff --git a/net/kcm/kcmsock.c b/net/kcm/kcmsock.c
> index ca789812124e8..d97db8adb31d4 100644
> --- a/net/kcm/kcmsock.c
> +++ b/net/kcm/kcmsock.c

[ ... ]

> @@ -50,6 +53,129 @@ static inline struct kcm_tx_msg *kcm_tx_msg(struct sk_buff *skb)

[ ... ]

> +static ssize_t kcm_splice_from_iter(struct sock *sk, struct kcm_tx_msg *txm,
> +				    struct sk_buff *skb,
> +				    struct iov_iter *iter, size_t maxsize,
> +				    size_t *min_space)
> +{

[ ... ]

> +	len = iov_iter_extract_pages(iter, &pages, maxsize, 1, 0, &off);
> +	if (len <= 0)
> +		return len ?: -EIO;

[ ... ]

> +	part = min_t(size_t, PAGE_SIZE - off, len);
> +	i = skb_shinfo(skb)->nr_frags;
> +	charge = part;
> +	if (!skb_can_coalesce(skb, i, page, off)) {
> +		if (i >= frag_limit) {
> +			err = -E2BIG;
> +			goto revert;
> +		}
> +		charge = folio_size(page_folio(page));
> +	}
> +
> +	if (txm->wmem_charge > txm->sndbuf ||
> +	    charge > txm->sndbuf - txm->wmem_charge) {
> +		err = -EMSGSIZE;
> +		goto revert;
> +	}

[Severity: High]
When the source is a large folio, is the whole folio charged once for
every page?

iov_iter_extract_pages() is called with maxpages=1, so each call returns
a single struct page. skb_can_coalesce() only merges when the frag uses
the same struct page and the offset is contiguous:

include/linux/skbuff.h:skb_can_coalesce_netmem() {
    ...
	return netmem == skb_frag_netmem(frag) &&
	       off == skb_frag_off(frag) + skb_frag_size(frag);
}

So page N+1 of a large folio never coalesces with page N, and each 4K page
is charged folio_size(). A 64 KiB folio is then charged 16 * 64 KiB =
1 MiB for 64 KiB of data. With the default sndbuf, a spliced message
larger than about 12 KiB fails with -EMSGSIZE.

For a 2 MiB THP in the page cache (tmpfs huge=, XFS or ext4 large folios),
the first page alone exceeds any unprivileged sndbuf (wmem_max defaults to
212992):

sendfile()
  splice_to_socket()
    sock_sendmsg(MSG_SPLICE_PAGES)
      kcm_sendmsg()
        kcm_splice_from_iter()
          charge = folio_size(page_folio(page));
          -> -EMSGSIZE

Before this patch, these paths went through skb_splice_from_iter() and
succeeded.

[ ... ]

> @@ -750,12 +876,94 @@ static void kcm_push(struct kcm_sock *kcm)

[ ... ]

>  static int kcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
>  {
>  	struct sock *sk = sock->sk;
>  	struct kcm_sock *kcm = kcm_sk(sk);
>  	struct sk_buff *skb = NULL, *head = NULL, *frag_prev = NULL;
> -	size_t copy, copied = 0;
> +	size_t copy, copied = 0, sndbuf, skb_charge;
> +	int wmem_queued;

[ ... ]

> +	sndbuf = READ_ONCE(sk->sk_sndbuf);
> +	skb_charge = SKB_TRUESIZE(0);
> +	/* Account for the head skb before admitting the message payload. */
> +	if (msg_data_left(msg) &&
> +	    (msg_data_left(msg) > sndbuf ||
> +	     skb_charge > sndbuf - msg_data_left(msg))) {
> +		err = -EMSGSIZE;
> +		goto out_error;
>  	}
>  
>  	if (msg_data_left(msg)) {
> +		while (skb_charge > sk_stream_wspace(sk)) {
> +			kcm_push(kcm);
> +			if (skb_charge <= sk_stream_wspace(sk))
> +				break;

[Severity: Medium]
skb_charge is a size_t, but sk_stream_wspace() returns an int
(sk_sndbuf - sk_wmem_queued). That value goes negative when sk_sndbuf
drops below sk_wmem_queued. Two ways this can happen:

- setsockopt(SO_SNDBUF) lowers it while MSG_BATCH messages are queued
- sk_page_frag_refill() fails and sk_stream_moderate_sndbuf() shrinks it

Wouldn't the negative int be converted to a huge unsigned value here? That
would skip the wait loop exactly when there is no space.

kcm_wmem_charge() then does a signed comparison:

	if (charge > sk_stream_wspace(sk) ||

That check fails, and the head charge below returns -ENOMEM. Blocking
callers get -ENOMEM instead of waiting, and non-blocking callers get
-ENOMEM instead of -EAGAIN. The old !sk_stream_memory_free() check pushed
and waited in this case.

[ ... ]

>  		skb = head;
> +		kcm_tx_msg(head)->sndbuf = sndbuf;
> +		kcm_tx_msg(head)->wmem_charge = 0;
> +		if (!kcm_wmem_charge(sk, kcm_tx_msg(head), head->truesize)) {
> +			kfree_skb(head);
> +			head = NULL;
> +			err = -ENOMEM;
> +			goto out_error;
> +		}

[Severity: Medium]
kcm_wait_memory() is not told how much space the caller needs.
wait_for_space is reached with min_space set to pfrag->size (32 KiB
on 4K page systems), to folio_size() from kcm_splice_from_iter(), or
to SKB_TRUESIZE(0) for a new frag_list skb. If sk_stream_wspace(sk)
is positive but below min_space, sk_stream_memory_free() is already
true on entry, so kcm_wait_memory() takes the vm_wait branch:

- it sleeps for a random 2..HZ/5+1 jiffies, or until the next wakeup
- it then sees sk_stream_memory_free() && !vm_wait and returns 0

Nothing checks that min_space bytes are actually free. kcm_sendmsg()
goes back to start, hits the same shortfall and lands in
wait_for_space again. With the default MAX_SCHEDULE_TIMEOUT the
timeout never decreases. A blocking sender therefore polls on a
timer instead of sleeping until enough space is released, and it
holds kcm->tx_mutex the whole time. The head skb loop has the same
pattern with sk_stream_wait_memory() when sk_stream_wspace(sk) is
positive but below skb_charge.

The sndbuf checks don't account for min_space either. wait_for_space
compares min_space against the txm->sndbuf snapshot. Both
wait_for_space and kcm_wait_memory() only compare the live sk_sndbuf
against txm->wmem_charge.

Suppose SO_SNDBUF or sk_stream_moderate_sndbuf() leaves sk_sndbuf
between wmem_charge and wmem_charge + min_space while other messages
are still queued. The request can no longer be satisfied, but the
sender keeps polling until the queue drains completely. Only then
does the sk_wmem_queued <= wmem_charge check return -EMSGSIZE. If
the psock peer stops reading, the queue never drains.

Should kcm_wait_memory() take min_space and wait until
sk_stream_wspace(sk) is at least min_space, rather than for
sk_stream_memory_free()? It could then return -EMSGSIZE as soon as
min_space exceeds the live sk_sndbuf minus wmem_charge.

[Severity: Medium]
The admission checks above use skb_charge = SKB_TRUESIZE(0), but the
charge here is head->truesize from alloc_skb(0). alloc_skb(0) goes through
kmalloc_reserve(), which sets *size = SKB_SMALL_HEAD_CACHE_SIZE.
__finalize_skb_around() then does:

	size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
	skb->truesize = SKB_TRUESIZE(size);

The result is a few hundred bytes larger than SKB_TRUESIZE(0). If the free
space falls inside that gap, the pre-check passes and kcm_wmem_charge()
fails.

Is a hard -ENOMEM intended here, for a sender that would have waited
before?

The frag_list skb has the same mismatch. The non-splice MAX_SKB_FRAGS path
and the splice -E2BIG path both check tskb_charge = SKB_TRUESIZE(0). Then
kcm_alloc_frag_skb() charges tskb->truesize and fails the same way. That
failure goes to wait_for_memory:

wait_for_memory:
		wmem_queued = READ_ONCE(sk->sk_wmem_queued);
		kcm_push(kcm);
		...
		err = kcm_wait_memory(sk, &timeo, head);
		if (err)
			goto out_error;

Unlike wait_for_space, this path has no sk_wmem_queued <= wmem_charge
exit. Suppose the unfinished message is the only charged object:

- kcm_push() frees nothing
- kcm_wait_memory() sees sk_stream_memory_free() true
- it sleeps a random vm_wait of up to about HZ/5 and returns 0
- with the default MAX_SCHEDULE_TIMEOUT, the timeout never decreases

Can this loop forever? Each pass, the SKB_TRUESIZE(0) pre-check passes,
the real charge fails, and the code goes back to wait_for_memory. The
sendmsg() or sendfile() call would spin until a signal arrives. It holds
kcm->tx_mutex the whole time, so other senders on the same socket sit in
uninterruptible mutex_lock().

[ ... ]

> @@ -809,65 +1067,144 @@ static int kcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)

[ ... ]

>  		} else {
> -			copy = min_t(int, msg_data_left(msg),
> +			size_t budget = min_t(size_t, space, msg_space);
> +			size_t padding = 0;
> +
> +			copy = min_t(size_t, msg_data_left(msg),
>  				     pfrag->size - pfrag->offset);
> -			if (!sk_wmem_schedule(sk, copy))
> +			copy = min_t(size_t, copy, budget);
> +			if (!merge) {
> +				if (pfrag->size > msg_space) {
> +					err = -EMSGSIZE;
> +					goto out_error;
> +				}
> +				if (pfrag->size > space) {
> +					min_space = pfrag->size;
> +					goto wait_for_space;
> +				}
> +				copy = min_t(size_t, msg_data_left(msg),
> +					     pfrag->size - pfrag->offset);
> +				padding = pfrag->size - copy;
> +				if (padding &&
> +				    !kcm_wmem_charge(sk, txm, padding))
> +					goto wait_for_memory;
> +			}

[Severity: High]
Doesn't this charge every message at least one full pfrag, even for a
1 byte payload?

The first fragment of a new message never merges, because
skb_can_coalesce() returns false for i == 0. So this block always runs for
it. skb_page_frag_refill() sets:

	pfrag->size = PAGE_SIZE << SKB_FRAG_PAGE_ORDER;

That is 32 KiB on 4K page systems and 64 KiB on 64K page systems, whatever
sk_sndbuf is.

padding is pfrag->size - copy even when pfrag->offset > 0 and earlier
messages on the same page were already charged for it. The same page is
then counted once per message.

Some consequences:

With setsockopt(SO_SNDBUF, 16384), sk_sndbuf becomes 32768. msg_space is
then 32768 - head->truesize, which is smaller than pfrag->size. Every
non-splice sendmsg() returns -EMSGSIZE, where it used to succeed.

Without SOCK_SNDBUF_LOCK, a failed sk_page_frag_refill() calls
sk_stream_moderate_sndbuf(), which shrinks sk_sndbuf toward
SOCK_MIN_SNDBUF. Nothing in net/kcm/ grows it back. Later sends keep
failing the wmem_charge >= sk_sndbuf and pfrag->size > msg_space checks.

With the default 212992 byte sndbuf, only about 6 messages fit in the
queue (about 3 on 64K page systems). Before, roughly sndbuf bytes of
payload could be queued.

The new comment in struct kcm_tx_msg calls this a "conservative page
backing charge". Since shared pfrag pages are counted once per message,
isn't this double counting rather than retained memory?

The documentation patch in this series only says the maximum payload can
be smaller than SO_SNDBUF. It doesn't mention that a small SO_SNDBUF
leaves the socket unable to send at all.

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1796043121.git.zihanx%40nebusec.ai

  reply	other threads:[~2026-12-05  3:39 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01  6:37 [PATCH net v2 0/2] kcm: bound message memory by send buffer Zihan Xi
2026-12-01  6:38 ` [PATCH net v2 1/2] net: kcm: account retained message memory Zihan Xi
2026-12-05  3:39   ` netdev-bot+sashiko [this message]
2026-12-01  6:38 ` [PATCH net v2 2/2] net: kcm: document transmit memory budget Zihan Xi
2026-12-05  3:39   ` netdev-bot+sashiko

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=179644199129.3146.4441563389803400930@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=brauner@kernel.org \
    --cc=corbet@lwn.net \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=jiayuan.chen@shopee.com \
    --cc=kuba@kernel.org \
    --cc=leitao@debian.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=rdunlap@infradead.org \
    --cc=rjethwani@purestorage.com \
    --cc=runyu.xiao@seu.edu.cn \
    --cc=skhan@linuxfoundation.org \
    --cc=tom@herbertland.com \
    --cc=vega@nebusec.ai \
    --cc=willemb@google.com \
    --cc=zihanx@nebusec.ai \
    --cc=zirajs7@gmail.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