From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796441995; b=ecxiCEYEPiDeBGyYaQejf+gibpt9A9pyiL5QNDrhMxf9qcEHDGi3Upkp+ISQteuflm6M E/4MqTJtejlKwIKjhiYgu8gzdwYrzI6BczQeeuYWCXBIuz0gyMPaVVoH4XuyUamlZOyZW r1uicnXr32otWTsRuu3ZAwcntnQqAN22v6IuBc5C39qHifAsuAnPmQQ9LTXqtURUGVpOo oYkVtizH/uuB3PuwoPFXnr0rJ/JtZPbxOHvQ5rHXzTXgJ8a59COz25fINfk3ur1uxqN0m Y1HEaFqzLIvIj5Ffa5ukSY6eXrzbvS4fz8IMt/MhK8cM9AEFVq44OocTKAAa9vuMbIw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796441995; h=Received:DKIM-Signature:Subject:From:To:Cc:Date:Message-ID: In-Reply-To:References:X-sashiko-severity:Content-Type: Content-Transfer-Encoding:MIME-Version; bh=TBVg1hIlcsWxsiV15ThhUtPzJMfuQYbUPUMGUVNAbJs=; b=rbmizovyQxASj1PytIP47/MualWOgxruXSIrBzZK7Xjzu6V9G+WvLdhj+KU8bopqdI65 kM10akc3aKGmVc3FqQc01REhbGYzisaFe+8bx7GxW7Pq3CDouxln7OIJ5ISdevmAGhCWG K8jMp61XF5F/XRogcPn3kbET9ix1ObY9djyazhqijS9CxMFkvmI3emopoyxbiSsc46Esk wd2s8WxTkq2EwJLqSZ5dTSM+90SvecJ42/Z074vqP+w2zRmsPs3wJsVwhDrlFT8J3RWnG LXSXbmaVWU6ytDfRD/ICZ5jkEKicdTCgXRrVF2f4/8EnDZ7AjR11O0RYmBt+uOzOuMg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="U/EmsVeV"; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E276B606A338; Sat, 5 Dec 2026 03:39:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=U/EmsVeV Received: by smtp.kernel.org (Postfix) with ESMTPSA id 974671F00936; Sat, 5 Dec 2026 03:39:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796441994; bh=TBVg1hIlcsWxsiV15ThhUtPzJMfuQYbUPUMGUVNAbJs=; h=Subject:From:To:Cc:Date:In-Reply-To:References; b=U/EmsVeVJmmzrxHpl7YayWF/4UhUi+0oDP8E8O49G1jnMTw5iVH2EH7dZyAlWmxif WH6+L10XGmfLh43TYBshDW5I5L9LTwkupv31QZW7u/qPU6bJsSH6cdUu7lNL4xHX90 U/G1nrBRVPsU2DAN/1bVE8T9iFc/XubQUCFA8YP4iRRAmG1EycyIMxxuygxmW7gods X1yKMlZuwSrGVThLuF+oRPxifxp5nJIP0dV0xo5MUVOQcoH1o/t/qdirCLnH7kglZC rXO+liied0ZIMKKhzMQVXCj8aam/Ldaq/Va5cUTZ/6rgcupBxb7+G5UsEcRabNCXrV tibdqJjgDALfg== Subject: Re: [PATCH net v2 2/2] net: kcm: document transmit memory budget 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 Date: Sat, 05 Dec 2026 03:39:53 +0000 Message-ID: <179644199314.3146.5014098859356358811@kernel.org> In-Reply-To: <45ffb33a96ca45bcef39985177e420a206e0a4cd.1796043121.git.zihanx@nebusec.ai> References: <45ffb33a96ca45bcef39985177e420a206e0a4cd.1796043121.git.zihanx@nebusec.ai> X-sashiko-severity: Medium Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 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