Skip to content

fix(orchestrator): preserve MAP_NORESERVE for guest memfd views - #3834

Closed
ClearLove181 wants to merge 1 commit into
e2b-dev:mainfrom
ClearLove181:fix/memfd-noreserve
Closed

ClearLove181 wants to merge 1 commit into
e2b-dev:mainfrom
ClearLove181:fix/memfd-noreserve

Conversation

@ClearLove181

Copy link
Copy Markdown

Restoring a hugetlb-backed sandbox can fail in NewFromFd before any guest page is read:

failed to wrap memfd: mmap memfd: cannot allocate memory

Firecracker maps its guest memfd with MAP_SHARED | MAP_NORESERVE, but the orchestrator's read-only mapping used only MAP_SHARED. For hugetlbfs, that observer establishes reservations for the file's unreserved range. With several sandboxes, or overlapping old/new memfds during pause and resume, this can fail even while ordinary RAM is available.

This change adds MAP_NORESERVE to the observer, preserving the owner's lazy-allocation policy. GuestMemoryError wrapping for genuine mmap ENOMEM remains in place. It does not change placement checks or host hugepage settings, and it does not guarantee future page faults can succeed under actual memory exhaustion.

The accounting change is intentional: an observer no longer adds the guest's unpopulated range to HugePages_Rsvd. Deployments that require admission against configured guest RAM should use the explicit allocated-memory admission limit rather than relying on this read-only view to reserve it. Hugepage fault-time exhaustion remains possible, as it already is for the owner's MAP_NORESERVE mapping.

Regression and reproduction

TestNewFromFd_HugetlbNoReservation uses an empty 1 GiB hugepage pool with overcommit disabled. It creates a sparse memfd and a no-reservation owner mapping, verifies that a reserving observer returns ENOMEM, and then checks that NewFromFd succeeds. Neither mapping is accessed, so the test consumes no physical huge pages and changes no sysctl. Unsupported hosts or callers without hugetlb permissions report an explicit skip.

Run from the repository root on a suitable Linux host:

go test -v -count=1 ./packages/orchestrator/pkg/sandbox/block \
  -run '^TestNewFromFd_HugetlbNoReservation$'

Removing only MAP_NORESERVE from NewFromFd makes the new regression fail with mmap memfd: cannot allocate memory; restoring the flag makes it pass. Detailed prerequisites and commands are in docs/debugging-memfd-hugetlb.md.

Validation

Tested on Linux x86_64 (Ubuntu 24.04 / WSL2 kernel 6.6.87.2), using Go 1.27.3 and golangci-lint 2.15.0, based on main at 391b224:

  • The new regression fails before the fix with mmap memfd: cannot allocate memory and passes after the fix; it was executed, not skipped.
  • go test -race -v -count=1 ./packages/orchestrator/pkg/sandbox/block -run 'TestNewFromFd|TestNewCacheFromMemfd|TestDedupedMemfdCache|TestMemfdIdentitySource' — passed, including the existing TestNewFromFdMarksENOMEM classification test.
  • go test -race -count=1 -timeout=5m ./packages/orchestrator/pkg/sandbox/block — the full block package passed.
  • From packages/orchestrator: golangci-lint run ./pkg/sandbox/block/... --timeout=5m — 0 issues.
  • git diff --cached --check — passed.

Repository-wide make test / make lint and the KVM integration suite were not run; the current-main checks above are scoped to the affected package.

The same fix was also backported to a self-hosted Linux deployment using 4 GiB sandboxes. The previously failing release then passed pause/resume, post-resume checks, destroy/recreate, and object-storage persistence verification. This deployment validation is separate from the current-main test results above.

Reference: Linux hugetlb reservation semantics.

Avoid observer-induced HugeTLB reservations during lazy snapshot restore.
Add a deterministic empty-pool regression and document reproduction steps.
Preserve ENOMEM classification and existing placement policy.
@cla-bot

cla-bot Bot commented Nov 17, 2026

Copy link
Copy Markdown

We require contributors to sign our Contributor License Agreement, and we don't have @ClearLove181 on file. You can sign our CLA at https://e2b.dev/docs/cla . Once you've signed, post a comment here that says '@cla-bot check'

@ryan-sana

Copy link
Copy Markdown
Contributor

Hey @ClearLove181, thanks for the careful write-up and repro! You're right that our read-only view is what reserves the huge pages, but that's on purpose: it lets a node refuse a sandbox it can't fully back at start, instead of the guest crashing later on a page fault the pool can't serve. So we'll pass on this one.

For the pause → resume overlap, CREATE_MEMORY_REFUSAL=true retries the create until the stopping sandbox's pages are back. Otherwise the pool just needs room for each sandbox's full memory. Thanks again 🙏

@ryan-sana ryan-sana closed this Nov 17, 2026
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.

2 participants