Repository navigation
Keep pages the kernel will not take back, and in regions backed up front - #6
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug. On Linux,
MADV_DONTNEEDfails withEINVALon pages locked in memory.sys::discardignored the result, so a purge pass ortrimrecorded 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.Commit::Lockregion after its first purge, and any region in a program that pins its own memory withmlockall, whatever the commit mode.Commit::Lock, all 4alloc_zeroedcalls after atrimreturned the 1 MiB of 0xAA written before the free.LazyandPopulateregions were clean.The fix:
sys::discardnow returns whether the kernel took the pages.Chunks::purge_passandChunks::drain: theirvisitcallback 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.trimonly empties heaps, because retrying would only be refused again.Commit::PopulateorCommit::Lockkeep their pages from the start. Before this,Populategave 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
CommitandRegion::trimdocs, 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_zerosruns 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_requestspins a block's pages withmlockin aLazyregion, 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_pageschecks withmincore, forPopulateandLock, that the pages stay resident through atrimand that a zeroed block reads as zeros. It fails on Linux without the fix.RLIMIT_MEMLOCKwhen the limit is too low, as the existingCommit::Locktest does.Checks:
just lint,just lint-docs, and tests on macOS (135) and Linux. Alsojust loom, and the chunk, region and zeroing tests under Miri.This PR is stacked on #4, which is stacked on #5.