Article View: muc.lists.freebsd.bugs
Article #811[Bug 301205] mbuf(9): m_mbuftouio() loops until m is NULL without testing progress < total, so partial reads walk the rest of th...
From: bugzilla-noreply
Date: Fri, 27 Nov 2026 22:38
Date: Fri, 27 Nov 2026 22:38
102 lines
6614 bytes
6614 bytes
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=301205
Bug ID: 301205
Summary: mbuf(9): m_mbuftouio() loops until m is NULL without
testing progress < total, so partial reads walk the
rest of the chain calling zero-length uiomove()
Product: Base System
Version: 16.0-CURRENT
Hardware: Any
OS: Any
Status: New
Severity: Affects Only Me
Priority: ---
Component: kern
Assignee: bugs@FreeBSD.org
Reporter: laamalif@proton.me
In sys/kern/uipc_mbuf.c (main 2534dcb233ad), m_mbuftouio() copies up to
total bytes into uio across an mbuf chain, but its loop condition only
tests m != NULL:
if (len > 0)
total = min(uio->uio_resid, len);
else
total = uio->uio_resid;
/* Fill the uio with data from the mbufs. */
for (; m != NULL; m = m->m_next) {
length = min(m->m_len, total - progress);
if ((m->m_flags & M_EXTPG) != 0)
error = m_unmapped_uiomove(m, 0, uio, length);
else
error = uiomove(mtod(m, void *), length, uio);
if (error)
return (error);
progress += length;
}
Once progress reaches total, length becomes 0, yet the loop walks every
remaining mbuf to the tail of the chain and calls uiomove() (or
m_unmapped_uiomove()) with length 0 on each one. soreceive_stream_locked()
(used by TCP when net.inet.tcp.soreceive_stream=1) and tunread() call
m_mbuftouio(); draining a 1 MiB receive buffer of 1024-byte segments in
64-byte reads turns 16384 reads into ~8.4 million mbuf steps and
zero-length uiomove() calls per MiB.
On FreeBSD 16.0-CURRENT amd64 GENERIC-NODEBUG (main 2534dcb233ad, Xeon
6975P-C) with net.inet.tcp.soreceive_stream=1, draining 1 MiB queued in
1024-byte TCP segments via 64-byte read(2) calls (ms per MiB, 10 runs):
cc -O2 -Wall -Wextra -Werror -o mbuftouio_partial_read mbuftouio_partial_read.c
for i in $(jot 10); do cpuset -l 2 ./mbuftouio_partial_read partial; done
x base_mbuftouio_partial.txt
+ patched_mbuftouio_partial.txt
+--------------------------------------------------------------------------+
|+ |
|+ |
|+ |
|+ |
|+ x |
|+ x |
|+ xx|
|+ xx|
|+ xx|
|+ xx|
|A A||
+--------------------------------------------------------------------------+
N Min Max Median Avg Stddev
x 10 42.359 42.949 42.6025 42.6237 0.19711083
+ 10 3.362 3.389 3.3795 3.3775 0.0086313383
Difference at 95.0% confidence
-39.2462 +/- 0.131085
-92.076% +/- 0.0278163%
(Student's t, pooled s = 0.139512)
DTrace fbt::uiomove:entry count across one run (warmup + timed MiB)
drops from 16795650 to 34818. On unmapped M_EXTPG mbufs queued via
sendfile(2) over an IFCAP_MEXTPG epair(4) VNET link (64-byte reads,
N=10), mean drain time drops from 31.933 ms to 3.515 ms (-88.99% +/-
0.39%) and fbt::m_unmapped_uiomove:entry drops from 11971565 to 32150.
With 65536-byte reads, mean drain time drops from 0.252 ms to 0.210 ms
(-16.62% +/- 1.35%); EFAULT recovery, 73-byte cross-page reads, and
debug GENERIC (INVARIANTS and WITNESS) all pass.
Adding progress < total to the for loop in m_mbuftouio() stops traversal
as soon as the requested byte count has been copied.
Tested on 16.0-CURRENT amd64 with mapped and sendfile(2) M_EXTPG mbufs;
I have not tested InfiniBand SDP.
--
You are receiving this mail because:
You are the assignee for the bug.
--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de
Technical Details
Message-ID:
<bug-301205-227@https.bugs.freebsd.org/bugzilla/>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu!newsfeed.bofh.team!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!feeder.erje.net!news2.arglkargh.de!news.karotte.org!news.muc.de!.POSTED.news.muc.de!not-for-mailShow Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!newsfeed.bofh.team!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!feeder.erje.net!news2.arglkargh.de!news.karotte.org!news.muc.de!.POSTED.news.muc.de!not-for-mail From: bugzilla-noreply@freebsd.org Newsgroups: muc.lists.freebsd.bugs Subject: [Bug 301205] mbuf(9): m_mbuftouio() loops until m is NULL without testing progress < total, so partial reads walk the rest of the chain calling zero-length uiomove() Date: Fri, 27 Nov 2026 22:38:16 +0000 Organization: Newsgate at muc.de e.V. Approved: news-admin@muc.de Distribution: world Message-ID: <bug-301205-227@https.bugs.freebsd.org/bugzilla/> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Injection-Info: news.muc.de; posting-host="news.muc.de:193.149.48.2"; logging-data="43639"; mail-complaints-to="news-admin@muc.de" X-Original-To: bugs@mlmmj.nyi.freebsd.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1795819096; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=DJl0owU0D1ALzopMAe1vhfuv1KGsR68qUNomQhM7bNU=; b=xX2BvTKzmxv4WtOUk0JcPmGceybcN4AFrFqAShPEPmwuYq4ephSBPez8SsnwFAYgfzMzM3 PsmTmwHMTyPhHrhE9mE4BuIiRisln+1GECXMFT1s6LqKzuSHDR8nu0Lycx1oUfOwychUVl Htyd5tiEmNzW5J3m29heU+mYROQh4XWD6iMPnQv2IP1szdkQOuY1p3mybBgRVy/QHTdUc4 AtumunS/OiDQIRdgV9gSQeeiRWAAyt231ynAVL6GLRIWu5G6Sar6bzPNLzqcQRVV6B3/xS Vfr8ao9/RwnuqxyZQj7eVJF16e9dGW0oRaJYlBNT8Jj3rP99uB4gveCcgY0Wbw== X-Authentication-Warning: kenobi.freebsd.org: www set sender to bugzilla-noreply@freebsd.org using -f Auto-Submitted: auto-generated Precedence: list X-Newsgate-Id: f02c70b8849f+ X-No-Archive: Yes