Skip to content

resource: make max_memory usable for Go and close reservation bypasses - #222

Open
congwang-mk wants to merge 3 commits into
mainfrom
prot-none-reservations
Open

resource: make max_memory usable for Go and close reservation bypasses#222
congwang-mk wants to merge 3 commits into
mainfrom
prot-none-reservations

Conversation

@congwang-mk

@congwang-mk congwang-mk commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Make max_memory usable for Go and close the two bypasses that follow from it.

  • Skip PROT_NONE reservations. Anonymous mmaps were charged by length regardless of prot. Go reserves over a gigabyte of PROT_NONE at startup, so any usable limit killed it before main ran. A PROT_NONE mapping backs nothing.
  • Judge mprotect. Once reservations are free, reserve + mprotect + touch would escape the ledger. mprotect now traps only when granting PROT_WRITE (BPF arg filter). It is judged, not charged; a maps read separating already-writable pages from new ones runs only when the whole length would exceed the limit.
  • Charge writable MAP_PRIVATE file mappings. /dev/zero mapped private and writable was anonymous memory the ledger never saw. Charging by length matches the kernel's data_vm.

Verified: Go hello world runs under a 64M limit, a 300 MB Go allocation is killed under it, Python and sh still run. Regression tests in Rust and Python for each step.

The mmap branch charged every anonymous mapping by length without
looking at prot, so address-space reservations counted as if they were
committed. The Go runtime reserves over a gigabyte of PROT_NONE at
startup (1.16 GB on go1.21 amd64, against 39 MB of writable mappings
and a 1.4 MB resident set), so any limit smaller than that killed a
Go program before main ran, and larger limits were no limit at all.

A PROT_NONE mapping backs nothing and is excluded from the kernel's
own data_vm count. Every path that later makes it real is still seen:
Go commits with a writable MAP_FIXED mmap, which this branch charges,
and glibc arenas grow via mprotect, which the statm floor picks up at
the next memory syscall. A Go hello world now runs under a 64M limit
and a 300 MB Go allocation is still killed under it.

Signed-off-by: Cong Wang <cwang@multikernel.io>
With PROT_NONE reservations no longer charged, a workload could reserve
address space for free, mprotect it writable, and touch all of it
without ever making the mmap or brk call that would have corrected the
ledger from statm. That turned the accounting lag into a deliberate
bypass.

mprotect now traps when it grants PROT_WRITE; a BPF argument filter
lets every other call through, so JITs flipping code pages RW to RX
pay nothing for the RX half. The handler judges the call but never
charges it: it cannot tell already-writable pages from newly committed
ones without reading maps, and a double charge could never be undone
since the floor only raises the ledger. The maps read that makes the
judgment exact runs only when the whole length would exceed the limit,
which keeps it off the hot path and stops a JIT near the limit from
being killed for re-granting write on its own code cache.

Signed-off-by: Cong Wang <cwang@multikernel.io>
Only MAP_ANONYMOUS mappings were charged, so a MAP_PRIVATE mapping of
a file with PROT_WRITE was free even though every written page becomes
a private anonymous copy. Mapping /dev/zero that way is an anonymous
mapping by another name and gave a workload arbitrary memory the ledger
never saw.

Such mappings are now charged by length like anonymous ones. That
matches the kernel's data_vm, which already counts writable private
mappings in full, so the floor would have raised the ledger to the same
value at the next event anyway; charging up front only makes the limit
apply before the first write instead of after. Shared and read-only
file mappings never create private pages and stay free.

Signed-off-by: Cong Wang <cwang@multikernel.io>
@congwang-mk congwang-mk changed the title resource: do not charge PROT_NONE reservations against max_memory resource: make max_memory usable for Go and close reservation bypasses Sep 11, 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.

1 participant