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 2/2] net: kcm: document transmit memory budget
Date: Sat, 05 Dec 2026 03:39:53 +0000 [thread overview]
Message-ID: <179644199314.3146.5014098859356358811@kernel.org> (raw)
In-Reply-To: <45ffb33a96ca45bcef39985177e420a206e0a4cd.1796043121.git.zihanx@nebusec.ai>
Thank you for your contribution! Sashiko AI review found 5 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 4 · 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 kcm.rst paragraph (lines 105-108) says the transmit
budget covers the message data, skb metadata and the backing pages…
- [Medium] kcm.rst (lines 109-110) says the limit for a message "is taken
when message construction starts", and mentions only that a later…
- [Medium] net: kcm: broken partial-wspace wait logic in
kcm_wait_memory()
- [Medium] net: kcm: signed/unsigned sk_stream_wspace() comparison in
kcm_sendmsg()
- [Low] kcm.rst (lines 108-109) says "A send that would exceed the per-
socket budget fails with EMSGSIZE." Two parts of the code disagree…
--
net: kcm: document transmit memory budget
This commit adds a paragraph to Documentation/networking/kcm.rst about the
SO_SNDBUF budget that KCM applies on transmit. The paragraph covers which
memory is charged, when EMSGSIZE is returned, and when the limit for a
message is captured.
> diff --git a/Documentation/networking/kcm.rst b/Documentation/networking/kcm.rst
> index 07778f29211c4..e64fd3c357730 100644
> --- a/Documentation/networking/kcm.rst
> +++ b/Documentation/networking/kcm.rst
> @@ -102,6 +102,14 @@ greater than this limit a corresponding error (EMSGSIZE) is posted on the TCP
> socket. The BPF program may also enforce a maximum messages size and report an
> error when it is exceeded.
>
> +On transmit, the KCM socket's SO_SNDBUF also limits the memory used to build
> +and queue a message. This budget includes the message data, skb metadata and
> +the backing pages retained by page fragments, so the maximum payload can be
> +smaller than SO_SNDBUF. A send that would exceed the per-socket budget fails
[Severity: Medium]
Does this match the accounting in kcm_sendmsg()? For each copy that does not
coalesce, the whole page_frag has to fit in the message budget. The whole
page_frag is charged whatever pfrag->offset is:
net/kcm/kcmsock.c:kcm_sendmsg() {
...
if (!merge) {
if (pfrag->size > msg_space) {
err = -EMSGSIZE;
goto out_error;
}
...
padding = pfrag->size - copy;
if (padding &&
!kcm_wmem_charge(sk, txm, padding))
goto wait_for_memory;
}
...
txm->wmem_charge += copy;
...
}
skb_page_frag_refill() normally hands out a 32 KiB page
(PAGE_SIZE << SKB_FRAG_PAGE_ORDER). Take SO_SNDBUF=16384, which gives
sk_sndbuf=32768. Then pfrag->size is larger than msg_space
(32768 - head->truesize). Wouldn't even a 1-byte send fail with EMSGSIZE?
In that case the maximum payload is zero, not just "smaller than
SO_SNDBUF".
sk_page_frag() can also return current->task_frag. Earlier bytes of that
page may belong to other sockets, but they are still billed to this
message.
On the merge path (MSG_MORE on DGRAM, or no MSG_EOR on SEQPACKET), padding
is 0, but txm->wmem_charge += copy still runs. The earlier padding charge
already covered those bytes, so they get charged twice. That is not in the
list of what the budget includes.
The splice path charges the whole folio for each page that does not
coalesce:
net/kcm/kcmsock.c:kcm_splice_from_iter() {
...
if (!skb_can_coalesce(skb, i, page, off)) {
...
charge = folio_size(page_folio(page));
}
...
}
skb_can_coalesce() needs the same struct page, so each subpage of a large
folio can be charged the full folio size. Can spliced sends from
large-folio page cache hit EMSGSIZE at the default sndbuf?
The accounting itself comes from the preceding commit "net: kcm: account
retained message memory". This paragraph seems to understate it.
> +with EMSGSIZE. The limit for a message is taken when message construction
[Severity: Medium]
This is outside the documentation change itself, but it decides whether
the EMSGSIZE and blocking behaviour described here actually holds. What
does kcm_wait_memory() do when wait_for_space is entered with some write
space left, but less than min_space?
kcm_proto has no .stream_memory_free, so sk_stream_memory_free() is true
whenever sk->sk_wmem_queued < sk->sk_sndbuf. kcm_wait_memory() then
takes the vm_wait backoff path:
net/kcm/kcmsock.c:kcm_wait_memory() {
...
if (sk_stream_memory_free(sk)) {
current_timeo = get_random_u32_below(HZ / 5) + 2;
vm_wait = current_timeo;
}
...
}
That path looks meant for a failed page allocation while socket space
is available. Here the socket space is the problem. wait_for_space in
kcm_sendmsg() needs sk_stream_wspace(sk) >= min_space, and min_space is
pfrag->size, the folio size or SKB_TRUESIZE(0). The wake condition
(sk_stream_memory_free(sk) && !vm_wait) stays false until vm_wait
expires, so sk_write_space() from kcm_write_msgs() can't wake the
sender. Once vm_wait is cleared, the loop exits with err = 0 even
though wspace is still below min_space:
if (sk_stream_memory_free(sk) && !vm_wait)
break;
kcm_sendmsg() then goes back to start, lands in wait_for_space again
and repeats. With a 32 KiB page_frag that covers any wspace from 1 to
32767 bytes. Doesn't this turn into a poll loop with sleeps of up to
about HZ / 5, rather than a wait for write space?
The shrink case looks worse. Suppose sk_sndbuf was lowered so that
txm->wmem_charge < sk->sk_sndbuf < txm->wmem_charge + min_space, and
another message is still on sk_write_queue. wait_for_space only checks
min_space against the captured limit:
if (min_space > kcm_tx_msg(head)->sndbuf -
kcm_tx_msg(head)->wmem_charge) {
kcm_wait_memory() only aborts when txm->wmem_charge >= sk->sk_sndbuf.
Neither check fires. The sender keeps looping even though min_space
can never fit in the current sk_sndbuf, and it does not get the
EMSGSIZE this paragraph promises.
Should kcm_wait_memory() be given min_space, wait until
sk_stream_wspace(sk) >= min_space, and check min_space against the live
sk_sndbuf as well as txm->sndbuf? That fix would presumably belong in
the preceding "net: kcm: account retained message memory" commit
rather than here.
[Severity: Low]
Is "per-socket budget" the right term here? The commit message calls it a
"per-message SO_SNDBUF budget". EMSGSIZE seems to come back only when the
message's own charge would go past txm->sndbuf - txm->wmem_charge, or when
the message is the only thing queued.
When socket-wide write space runs short, kcm_sendmsg() goes to
wait_for_space. It then blocks in kcm_wait_memory(), or returns EAGAIN with
MSG_DONTWAIT. kcm_splice_from_iter() also returns EAGAIN in that case:
if (charge > sk_stream_wspace(sk)) {
*min_space = charge;
err = -EAGAIN;
goto revert;
}
SOCK_SEQPACKET behaves differently again. If the budget is hit after some
bytes were already copied in the current call, out_error returns a short
count with no error:
if (copied)
goto partial_message;
EMSGSIZE then only shows up on the next call. Should the paragraph mention
this?
> +starts; changing SO_SNDBUF does not enlarge an already started message. Set
[Severity: Medium]
Is this accurate for kcm_sendmsg()? The limit is captured when the head is
allocated:
kcm_tx_msg(head)->sndbuf = sndbuf;
The accumulated charge is also checked against the live sk->sk_sndbuf in
several places: at the top of the copy loop, after sk_page_frag_refill(),
in wait_for_space, in wait_for_memory, and in kcm_wait_memory(). For
example:
if (txm->wmem_charge >= txm->sndbuf ||
txm->wmem_charge >= READ_ONCE(sk->sk_sndbuf)) {
err = -EMSGSIZE;
goto out_error;
}
So the effective limit looks like min(captured sndbuf, current
sk_sndbuf), and a started message is aborted if sk_sndbuf shrinks.
Two things can shrink it. A user can lower SO_SNDBUF. The kernel can also
lower it: when sk_page_frag_refill() fails, it calls
sk_stream_moderate_sndbuf() on sockets without SOCK_SNDBUF_LOCK. The code
comments say as much:
/* sndbuf can shrink while page-frag allocation moderates it. */
For DGRAM sockets, out_error then throws away the whole partly built
message:
} else {
if (head)
kcm_wmem_uncharge_msg(sk, head);
kfree_skb(head);
kcm->seq_skb = NULL;
}
That includes bytes that earlier MSG_MORE sendmsg() calls reported as
sent. Should the documentation cover the shrink case and this loss of the
pending message?
[ ... ]
> @@ -102,6 +102,14 @@ greater than this limit a corresponding error (EMSGSIZE) is posted on the TCP
> socket. The BPF program may also enforce a maximum messages size and report an
> error when it is exceeded.
>
> +On transmit, the KCM socket's SO_SNDBUF also limits the memory used to build
> +and queue a message. This budget includes the message data, skb metadata and
> +the backing pages retained by page fragments, so the maximum payload can be
> +smaller than SO_SNDBUF. A send that would exceed the per-socket budget fails
> +with EMSGSIZE. The limit for a message is taken when message construction
> +starts; changing SO_SNDBUF does not enlarge an already started message. Set
> +SO_SNDBUF before sending messages that require a larger budget.
> +
> A timeout may be set for assembling messages on a receive socket. The timeout
> value is taken from the receive timeout of the attached TCP socket (this is set
> by SO_RCVTIMEO). If the timer expires before assembly is complete an error
[Severity: Medium]
Does starting a new message work when sk->sk_wmem_queued is already
above sk->sk_sndbuf? That is the state after SO_SNDBUF is lowered with
messages still queued, or after sk_page_frag_refill() moderates
sk_sndbuf. This sentence gives advice about exactly that situation.
sk_stream_wspace() returns an int, which is negative here, but
skb_charge in kcm_sendmsg() is a size_t:
net/kcm/kcmsock.c:kcm_sendmsg() {
size_t copy, copied = 0, sndbuf, skb_charge;
...
while (skb_charge > sk_stream_wspace(sk)) {
kcm_push(kcm);
if (skb_charge <= sk_stream_wspace(sk))
break;
...
}
The negative int is converted to a huge size_t, so the loop condition
is false. Both kcm_push() and sk_stream_wait_memory() are skipped.
head is then allocated and charged with kcm_wmem_charge(sk,
kcm_tx_msg(head), head->truesize). kcm_wmem_charge() takes an int
charge, so there charge > sk_stream_wspace(sk) is true and the charge
fails:
err = -ENOMEM;
goto out_error;
A blocking sender therefore gets ENOMEM straight away instead of
waiting for the queue to drain, and the already queued messages are not
pushed first. The new paragraph does not mention ENOMEM either. Should
skb_charge be compared as a signed value, or should a negative
sk_stream_wspace() be handled explicitly before the loop?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1796043121.git.zihanx%40nebusec.ai
prev parent 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
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 [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=179644199314.3146.5014098859356358811@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