Skip to content

Keep pages the kernel will not take back, and in regions backed up front - #6

Merged
clabby merged 1 commit into
readme-introfrom
keep-locked-pages
Nov 14, 2026
Merged

clabby merged 1 commit into
readme-introfrom
keep-locked-pages

Conversation

@clabby

@clabby clabby commented Nov 14, 2026

Copy link
Copy Markdown
Owner

The bug. On Linux, MADV_DONTNEED fails with EINVAL on pages locked in memory. sys::discard ignored the result, so a purge pass or trim recorded those chunks as given back. On Linux a given-back chunk is assumed to read as zeros, so the next zeroed request on those chunks skipped the clearing and returned whatever was there before.

  • Who it hit: every Commit::Lock region after its first purge, and any region in a program that pins its own memory with mlockall, whatever the commit mode.
  • How I reproduced it: with Commit::Lock, all 4 alloc_zeroed calls after a trim returned the 1 MiB of 0xAA written before the free. Lazy and Populate regions were clean.

The fix:

  • sys::discard now returns whether the kernel took the pages.
  • Chunks::purge_pass and Chunks::drain: their visit callback returns that result. A run counts as purged only when its pages actually went. A refused run keeps its dirty bit, so it is cleared before a zeroed request uses it.
  • No retrying: the first refusal makes the region keep its pages from then on. The purge clock stops and trim only empties heaps, because retrying would only be refused again.
  • Regions backed up front with Commit::Populate or Commit::Lock keep their pages from the start. Before this, Populate gave idle pages back and faulted them in again, which broke the promise in its docs that the allocator never takes a page fault.

Docs: the Commit and Region::trim docs, DESIGN.md (sections 3 and 10) and the README's limits now say which regions keep their pages.

Tests:

  • chunks::a_run_whose_pages_stayed_is_not_taken_for_zeros runs on every platform. A refused run stays dirty and is not reported as purged.
  • region::memory_the_kernel_will_not_take_back_is_cleared_for_zeroed_requests pins a block's pages with mlock in a Lazy region, frees it, trims, and asks for a zeroed block on the same chunks. It fails on Linux without the fix.
  • region::a_region_backed_up_front_keeps_its_pages checks with mincore, for Populate and Lock, that the pages stay resident through a trim and that a zeroed block reads as zeros. It fails on Linux without the fix.
  • Skipped cases: the pinning tests skip the part that needs RLIMIT_MEMLOCK when the limit is too low, as the existing Commit::Lock test does.

Checks: just lint, just lint-docs, and tests on macOS (135) and Linux. Also just loom, and the chunk, region and zeroing tests under Miri.

This PR is stacked on #4, which is stacked on #5.

On Linux, MADV_DONTNEED fails on pages that are locked in memory. discard
ignored the result, so a purge or a trim recorded locked chunks as given back,
which there means zero-filled, and the next zeroed request on them skipped the
clearing and returned the old contents. Every Commit::Lock region hit this
after its first purge, and so would any region of a program that pins its
memory with mlockall.

discard now reports whether the kernel took the pages. The purge pass and the
drain record a run as purged only when its pages went, so a refused run stays
dirty and is cleared before a zeroed request uses it. The first refusal makes
the region keep its pages from then on, since retrying would only be refused
again.

A region backed up front with Commit::Populate or Commit::Lock now keeps its
pages from the start: its purge clock never runs and trim only empties heaps.
Populate used to give idle pages back and fault them in again, which its
documentation promised it would not do. The docs, DESIGN.md and the README's
limits say what each mode keeps.
@clabby
clabby added this pull request to stack #7 November 14, 2026 15:57
@clabby
clabby merged commit e690d4d into main Nov 14, 2026
13 checks passed
@clabby
clabby deleted the keep-locked-pages branch November 14, 2026 21:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant