diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index da4b02e4212..f44772de95a 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -607,6 +607,8 @@ Key mechanisms (all under `pkg/sandbox/`): - **Lazy memory / UFFD** (`uffd/`): on resume, Firecracker restores the VM without loading memory; a userfaultfd handler serves page faults directly from the template's memfile, so only touched pages are read. An optional prefetcher warms known-hot pages. + The orchestrator's read-only view of a guest memfd uses `MAP_NORESERVE`, matching + Firecracker, so observing it does not reserve the guest's whole hugepage capacity. - **Copy-on-write rootfs** (`rootfs/`, `nbd/`, `block/`): the template rootfs stays read-only; writes go to a per-sandbox COW cache exposed to Firecracker as an NBD block device served by an in-process userspace NBD server. On pause, the dirty blocks are exported as a diff. The diff --git a/docs/debugging-memfd-hugetlb.md b/docs/debugging-memfd-hugetlb.md new file mode 100644 index 00000000000..7fa5e3550f8 --- /dev/null +++ b/docs/debugging-memfd-hugetlb.md @@ -0,0 +1,51 @@ +# Reproducing memfd observer reservation failures + +With hugetlb-backed guest memory, snapshot restore can fail before the guest runs: + +```text +failed to wrap memfd: mmap memfd: cannot allocate memory +``` + +Firecracker maps its guest memfd with `MAP_SHARED | MAP_NORESERVE`. A second, +read-only mapping without `MAP_NORESERVE` establishes reservations for the +unreserved part of the entire file. This can exhaust the hugepage pool even +though the observer has not read any pages and ordinary RAM remains available. +`block.NewFromFd` must preserve the owner's no-reservation policy. + +## Deterministic regression + +Run from the repository root on Linux with the toolchain required by `go.work`: + +```sh +go test -v -count=1 ./packages/orchestrator/pkg/sandbox/block \ + -run '^TestNewFromFd_HugetlbNoReservation$' +``` + +The test requires a supported 1 GiB hugepage size whose `nr_hugepages` and +`nr_overcommit_hugepages` are both zero. It also requires permission to create a +hugetlb memfd (root, `CAP_IPC_LOCK`, or the configured hugetlb group). It reports +a skip when those prerequisites are unavailable; a skipped run is not a +successful reproduction. + +The fixture creates a sparse 1 GiB memfd and an owner mapping with +`MAP_NORESERVE`. It first verifies that a reserving observer fails with +`ENOMEM`, then calls `NewFromFd` on a duplicate descriptor. Without the fix, +that call fails with the same error. With the fix, it succeeds and both mappings +are closed normally. + +No pages are accessed, no physical huge pages are consumed, and no host sysctl +is changed. Do not populate or read these deliberately unbacked mappings: doing +so can raise `SIGBUS`. + +Existing memfd data/export tests and the ENOMEM classification test can be run +together with the regression: + +```sh +go test -race -v -count=1 ./packages/orchestrator/pkg/sandbox/block \ + -run 'TestNewFromFd|TestNewCacheFromMemfd|TestDedupedMemfdCache|TestMemfdIdentitySource' +``` + +This change avoids an observer-induced reservation; it does not make physical +memory unlimited or guarantee that future guest faults can succeed under actual +hugepage exhaustion. Capacity planning and admission checks remain necessary. +See the [Linux hugetlb reservation documentation](https://docs.kernel.org/mm/hugetlbfs_reserv.html). diff --git a/packages/orchestrator/pkg/sandbox/block/memfd.go b/packages/orchestrator/pkg/sandbox/block/memfd.go index e5fa0257959..f6afa6c25e9 100644 --- a/packages/orchestrator/pkg/sandbox/block/memfd.go +++ b/packages/orchestrator/pkg/sandbox/block/memfd.go @@ -31,9 +31,8 @@ type Memfd struct { mmap []byte } -// GuestMemoryError is the guest-memory mmap failing with ENOMEM: the hugepage -// pool could not back the whole guest, because sandboxes still hold the pages -// or the pool is smaller than the guest. It keeps the errno's text. +// GuestMemoryError identifies ENOMEM while mapping guest memory. +// It keeps the errno's text. type GuestMemoryError struct{ Err error } func (e GuestMemoryError) Error() string { return e.Err.Error() } @@ -46,7 +45,10 @@ func NewFromFd(fd int) (*Memfd, error) { return nil, fmt.Errorf("fstat memfd: %w", err) } - b, err := unix.Mmap(fd, 0, int(st.Size), unix.PROT_READ, unix.MAP_SHARED) + // Firecracker maps this lazily populated hugetlb memfd with MAP_NORESERVE. + // Keep the same policy for our read-only view: otherwise mmap reserves the + // whole guest's hugepages and can fail with ENOMEM before we read any page. + b, err := unix.Mmap(fd, 0, int(st.Size), unix.PROT_READ, unix.MAP_SHARED|unix.MAP_NORESERVE) if err != nil { _ = unix.Close(fd) if errors.Is(err, unix.ENOMEM) { diff --git a/packages/orchestrator/pkg/sandbox/block/memfd_hugetlb_test.go b/packages/orchestrator/pkg/sandbox/block/memfd_hugetlb_test.go new file mode 100644 index 00000000000..2016b66770a --- /dev/null +++ b/packages/orchestrator/pkg/sandbox/block/memfd_hugetlb_test.go @@ -0,0 +1,56 @@ +//go:build linux + +package block + +import ( + "errors" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/stretchr/testify/require" + "golang.org/x/sys/unix" +) + +// An empty 1 GiB pool makes the reservation failure deterministic without +// consuming physical memory or changing a pool used by running guests. +// Never access either mapping: there is deliberately no backing huge page. +func TestNewFromFd_HugetlbNoReservation(t *testing.T) { //nolint:paralleltest // observes host hugepage pool state + const pool = "/sys/kernel/mm/hugepages/hugepages-1048576kB" + for _, name := range []string{"nr_hugepages", "nr_overcommit_hugepages"} { + value, err := os.ReadFile(filepath.Join(pool, name)) + if err != nil || strings.TrimSpace(string(value)) != "0" { + t.Skip("requires a supported, empty 1 GiB hugetlb pool with no overcommit") + } + } + + fd, err := unix.MemfdCreate("e2b-noreserve-test", unix.MFD_CLOEXEC|unix.MFD_HUGETLB|unix.MFD_HUGE_1GB) + if errors.Is(err, unix.EPERM) || errors.Is(err, unix.EACCES) || errors.Is(err, unix.EINVAL) || errors.Is(err, unix.ENOSYS) { + t.Skipf("hugetlb memfd unavailable: %v", err) + } + require.NoError(t, err) + t.Cleanup(func() { _ = unix.Close(fd) }) + + const size = 1 << 30 + require.NoError(t, unix.Ftruncate(fd, size)) + + // Match Firecracker's owner mapping. No huge pages are allocated or read. + owner, err := unix.Mmap(fd, 0, size, unix.PROT_READ|unix.PROT_WRITE, unix.MAP_SHARED|unix.MAP_NORESERVE) + require.NoError(t, err) + t.Cleanup(func() { require.NoError(t, unix.Munmap(owner)) }) + + // Verify that the fixture really exercises the reservation failure. + reserving, err := unix.Mmap(fd, 0, size, unix.PROT_READ, unix.MAP_SHARED) + if err == nil { + require.NoError(t, unix.Munmap(reserving)) + } + require.ErrorIs(t, err, unix.ENOMEM) + + observerFD, err := unix.Dup(fd) + require.NoError(t, err) + observer, err := NewFromFd(observerFD) // consumes observerFD on both paths + require.NoError(t, err, "observing guest memory must not reserve additional huge pages") + t.Cleanup(func() { require.NoError(t, observer.Close()) }) + require.Len(t, observer.mmap, size) +}