Article View: muc.lists.freebsd.bugs
Article #903[Bug 301482] mincore(2): a scan restart after a concurrent map change zeroes vector entries already filled, and can fail with EN...
From: bugzilla-noreply
Date: Fri, 04 Dec 2026 09:19
Date: Fri, 04 Dec 2026 09:19
161 lines
9697 bytes
9697 bytes
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=301482
--- Comment #3 from Noel Minamino <noel.minamino@gmail.com> ---
Follow-up: the slowness has the same cause; a second patch fixes both
Attachments with this comment:
mincore-chunk.diff alternative fix, against releng/15.1 (kern_mincore()
is the same in main); an alternative to
mincore-restart.diff
mc5.c edge cases without concurrency (chunk boundaries,
holes, len 0, unaligned addresses, bad vectors)
mc6.c latency of other threads while one thread calls
mincore() in a loop
------------------------------------------------------------------------
Why the call becomes slow
I measured the unpatched kernel (15.1-RELEASE-p6) with DTrace while mc3e
ran with one map-changing thread:
- The scan restarts again and again: 27 million restarts in 300 calls of
256 pages, up to 646,539 restarts in one call.
- The time goes into the per-page work that each restart repeats: in
the profiles of the mincore() thread, subyte() and lock_delay()
(spinning to take the map lock again while the other thread holds it
for writing) take most of the samples. Sleeping on the lock is rare:
lockstat saw 4 shared sx blocks on the vm map in 58 seconds.
Each restart keeps addr at the page where the change was seen and resets
lastvecindex, so the next subyte() rewrites vec[0 .. vecindex-1] with
zeros. This happens with the map lock dropped. The further the scan gets,
the longer the window without the lock, and the more likely another map
change falls into it. The scan then hardly moves forward.
The first patch (rescan from first_addr) removes the wrong zeros, but a
call still has to pass the whole range without a single map change, so
it stays just as slow (26 s for 300 calls).
Both problems come from the same design: the lock is dropped for every
page (for subyte()), and any change anywhere in the map restarts the scan.
------------------------------------------------------------------------
The second patch (mincore-chunk.diff)
kern_mincore() now works in chunks of at most 64 pages:
- the map is read-locked while one chunk is examined; the per-page code
is unchanged and moves into a helper, mincore_chunk();
- the results go into a 64-byte buffer on the stack;
- the lock is released and the buffer is copied out with copyout();
- the next chunk looks up its first map entry again.
The map cannot change while a chunk is examined, so there is no restart,
and entries already copied out are never overwritten. A range that was
fully mapped can no longer fail with ENOMEM because of a restart. The
values of an earlier chunk describe the map at the time that chunk was
examined; that was already the case before.
An empty range is still looked up once, so an unmapped address still
fails with ENOMEM, as before.
One visible difference: when the range contains a hole, the call still
fails with ENOMEM, but the entries for the chunks before the hole may
already have been written. The old code also wrote entries before failing
in some of these cases. mc5.c shows this; all other mc5.c results are
identical with and without the patch.
------------------------------------------------------------------------
Results
amd64 VM with 6 vCPUs, same machine and binaries. "Unpatched" is
15.1-RELEASE-p6 GENERIC. "Patched" is releng/15.1 (15.1-RELEASE-p8)
GENERIC with the patch; the host runs below used the test kernel from the
next section with the chunk size set to 64, which differs from the
attached patch only in using a malloc()ed buffer and a sysctl for the
size. The attached patch itself was tested in a bhyve guest (below).
256-page calls (mc3e), one map-changing thread, unless noted.
Unpatched, 300 calls:
UFS 28 s, 219 calls with wrong zeros, 2 ENOMEM
tmpfs 43 s, 232 calls with wrong zeros
3 map-changing threads 132 s, 294 calls with wrong zeros, 4 ENOMEM
vm.mincore_mapped=0 57 s, 265 calls with wrong zeros, 2 ENOMEM
100,000 calls not done in 600 s
Patched:
1,000,000 calls 4.6 s, 0 wrong, 0 ENOMEM
3 threads, 300,000 calls 3.6 s, 0 wrong, 0 ENOMEM
vm.mincore_mapped=0,
300,000 calls 2.9 s, 0 wrong, 0 ENOMEM
No map-changing thread, 100,000 calls: 0.70 s unpatched, 0.20 s patched.
mc4.c (repeat the call and OR the results), entries still 0 after
1/2/3/4/8 calls:
unpatched, 300 rounds 36,911 / 18,806 / 12,798 / 8,256 / 2,299
patched, 30,000 rounds 0 / 0 / 0 / 0 / 0
To make sure the map-changing thread really ran, I counted the calls with
DTrace: 446,796 munmap() calls during 600,000 mincore() calls.
In a bhyve guest (4 vCPUs), the attached patch passes the libc test
lib/libc/sys/mincore_test (kyua, 3/3), reports no wrong zeros and no
ENOMEM in the mc3e runs (1 and 3 map-changing threads, UFS and tmpfs,
vm.mincore_mapped=1 and 0) and in mc4.c, and gives the same mc5.c results
as the unpatched kernel except in the ENOMEM cases described above. The
map-changing thread is slow in this nested guest, so the guest does not
show the slowness itself.
------------------------------------------------------------------------
Effect on other threads, and the chunk size
While a chunk is examined, a thread that wants to change the map waits
for the read lock, and page faults appear to queue behind that writer.
mc6.c measures this in the worst case: one thread calls mincore() over
65,536 pages in a tight loop, one thread does mmap()+munmap() of 64 KiB,
one thread takes first-touch page faults. I made the chunk size a
temporary sysctl for this test only; it is not in the patch.
chunk page fault mmap+munmap one mincore() call
(pages) p99 / max per second over 65,536 pages
- 2.8 us / 0.2 ms 1,180,000 - (no mincore() thread)
16 17 us / 0.4 ms 1,140,000 5.4 ms
64 36 us / 0.9 ms 910,000 1.7 ms
256 206 us / 2.0 ms 420,000 0.65 ms
1024 64 us / 3.4 ms 130,000 0.52 ms
4096 2.5 ms / 28 ms 110,000 0.49 ms
With 4096 pages per chunk (as far as I can see, what Linux uses) the
page fault latency of other threads became bad in this test, so I chose
64. Yielding the CPU between chunks did not help. Unpatched, one call
over 4,096 pages took more than 4 seconds and one over 65,536 pages did
not finish within 60 seconds; the other threads kept their normal speed
(measured with 4,096 pages), but the mincore() caller hardly got an
answer.
This is a worst case: mincore() runs without a pause. I have not measured
a realistic mix, and not ranges larger than 1 GiB.
------------------------------------------------------------------------
Either patch fixes the wrong values. The second patch also fixes the
slowness, but it is a larger change and it changes how long the map
lock is held, so it needs more review. If you prefer, the one-line patch
could go in first and the second one later.
As before, I did this work with the help of an AI coding assistant and
checked the analysis and the measurements myself.
--
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-301482-227-nSHm0fwQDS@https.bugs.freebsd.org/bugzilla/>Path:
mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!3.eu.feeder.erje.net!feeder.erje.net!news2.arglkargh.de!news.karotte.org!news.muc.de!.POSTED.news.muc.de!not-for-mailReferences:
<bug-301482-227@https.bugs.freebsd.org/bugzilla/>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!news.mb-net.net!open-news-network.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 301482] mincore(2): a scan restart after a concurrent map change zeroes vector entries already filled, and can fail with ENOMEM Date: Fri, 04 Dec 2026 09:19:28 +0000 Organization: Newsgate at muc.de e.V. Approved: news-admin@muc.de Distribution: world Message-ID: <bug-301482-227-nSHm0fwQDS@https.bugs.freebsd.org/bugzilla/> References: <bug-301482-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="59168"; 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=1796375968; 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: in-reply-to:in-reply-to:references:references; bh=52oa9kIzKhx/lE6YcnDzOUJNKzKkXBI32tOT1BbBxz0=; b=yTELaVmwVZUnMhW+c1aZ0mnZYHB0NS8AqIVvAapt7AZY1vOVyhT0o8WSGeB6qjLqlY0BA4 a14ULVH2+R9VCJktbzBrM0XHeZ7GfU+ysI8mstN7TC8L1Ym2xJMZyYxYtliU3Lw+Px9DPw IXmyWadF2pmwhSM9IT6RpFjazJJxZnhAtAtpMHDdapZhov/VwFf3fuCIWVasoY2ZaL35D8 fVXQR86DOJQxLyMRHBCNsuuBhB21X2iqiYOfcRzUD0cAAHbDiH/FQWC9blsobpUYxlQVUs Me3+7iMaeyEWUybIGzVqhBJwd3IJtNi964UzJScRmH0MwveG/nuKEdCc87e7hg== X-Authentication-Warning: kenobi.freebsd.org: www set sender to bugzilla-noreply@freebsd.org using -f In-Reply-To: <bug-301482-227@https.bugs.freebsd.org/bugzilla/> Auto-Submitted: auto-generated Precedence: list X-Newsgate-Id: f02c70b8849f+ X-No-Archive: Yes