Blog

Firecracker memory hotplug for agent VMs: trigger on anonymous memory, not a soft limit

· by

TL;DR. A Rust build was killed for memory in our 1 GiB agent VMs, and we expected to fix that by giving every VM 2.5 GiB. That size could not use our snapshot template and kept 2.4 GiB of host memory after each build, so we kept the 1024 MiB boot size and added a 1536 MiB Firecracker virtio-mem region that the guest asks for. Our first two triggers used the cgroup soft limit: one stalled a build for more than 20 minutes, and the other plugged the whole region for page cache before the build started. The trigger that works reads anonymous memory and sets no soft limit. With it, a build whose compiler needs 2 GB took 2 min 26 s, against 2 min 22 s on the fixed 2.5 GiB VM, and the memory went back to the host about two minutes later. Production has run this since December 4, 2026 and had granted no request when we wrote this, so the numbers below are from staging, a prototype and a pressure test.

Plori runs each agent session in a Firecracker microVM. Our post on running Firecracker on nested KVM describes the nodes: 2 vCPUs, 8 GB, a 6.9 GiB budget for VMs, and an overcommit ratio of 1.5 backed by balloons. This post continues it with the next memory problem those nodes had. A VM had 1024 MiB, and the agent's commands ran in a cgroup limited to 728 MiB, because the guest keeps 256 MiB for its own system processes. Compiling a large Rust crate does not fit in that. An agent that tried to build agnix, an open-source linter for the configuration files of AI coding assistants, had rustc killed in the cgroup, so the task ended without a build. The staging measurements below all build agnix.

Firecracker memory hotplug for agent VMs, in 36 seconds

A larger fixed VM could not use our snapshot template

We tested two fixed sizes. A 2272 MiB guest still had rustc killed. A 2560 MiB guest built agnix in 2 min 22 s with every test passing. The compiler's largest process peaked at 1.96 to 2.0 GB in every run.

The 2560 MiB size has two costs on the production node:

Cost Fixed 2560 MiB 1024 MiB plus a 1536 MiB region
VM snapshot template Cannot be built on the node: it needs 2560 + 2 × 2560 + 64 = 7744 MiB. Those VMs cold boot (healthy in about 1,060 ms, against 180 ms from a template on staging). Built with nothing plugged. The unplugged region is a hole in the memory file.
Memory after a build Firecracker resident memory stayed near 2.4 GiB until the VM stopped. Returned about two minutes after the build.

A fixed size charges every VM for the extra 1.5 GiB all the time, whether or not its command needs it, and on an 8 GB node that charge removes the template restore that made a VM healthy in 180 ms. We dropped the fixed size for that reason.

The guest asks its node for memory

Firecracker has supported memory hotplug through a virtio-mem device since version 1.14. The VM is configured before start with a separate pool of memory:

curl --unix-socket "$socket" -X PUT 'http://localhost/hotplug/memory' \
  -H 'Content-Type: application/json' \
  -d '{"total_size_mib": 1536, "block_size_mib": 2, "slot_size_mib": 128}'

The pool starts unplugged. A later PATCH /hotplug/memory with a requested_size_mib asks the guest driver to plug or unplug blocks. The device is only the mechanism: something has to decide when to resize. The host cannot tell which guest memory is a compiler's heap and which is page cache, so in our design the guest asks.

Diagram of the five steps by which an agent VM asks its node for memory: the guest init watcher sees usage near the limit, posts a target to the node daemon, the node daemon sets the size on Firecracker's virtio-mem device, the memory appears in the guest, and the watcher raises the command limit.Diagram of the five steps by which an agent VM asks its node for memory: the guest init watcher sees usage near the limit, posts a target to the node daemon, the node daemon sets the size on Firecracker's virtio-mem device, the memory appears in the guest, and the watcher raises the command limit.
The request path. The numbers match the steps below.
  1. A watcher in the guest's init process reads the anonymous and shared memory of the agent's command cgroup every 100 ms. When that comes within 192 MiB of the hard limit, it acts.
  2. The watcher posts a target size to the node daemon over a private link between that VM and its node.
  3. The node daemon grants the largest multiple of 128 MiB that keeps the node's total guest memory within its budget and leaves the host its reserve of available memory. It refuses while the node is under memory pressure. Then it sets the size on the VM's virtio-mem device.
  4. The guest kernel brings the new memory online. We boot with memhp_default_state=online_movable, so hotplugged memory goes to the movable zone and can be unplugged later.
  5. The watcher raises the cgroup's hard limit to the new total minus the same 256 MiB.

When the command's anonymous memory has fitted the boot limit again for 120 seconds, the watcher reclaims page cache down to the boot limit, restores the limit, and posts a target of 0.

Placement and the node's per-VM reservation count the boot size only, so a node admits as many VMs as it did before. The node accounts for a grant while it is plugged. A request that is refused or cut short leaves the command at its current limit, where the kernel can kill it for memory, exactly as it could before the change.

A soft limit stalled the build, then plugged memory for page cache

The first design used the cgroup's soft limit, memory.high, set 192 MiB under the hard limit, and kept it there after the whole region was plugged. The kernel's cgroup documentation says what happens to a cgroup over that boundary: its processes "are throttled and put under heavy reclaim pressure". The build's working set was about 2.2 GiB, which is above that soft limit. The build made no visible progress for more than 20 minutes while Firecracker's resident memory stayed at 2471 MiB, and we stopped it.

The second design removed the soft limit once the region was full and used a crossing of the soft limit as the signal to grow. It grew for the wrong memory. The toolchain download and the repository clone filled page cache, the cgroup crossed the boundary, and the whole 1536 MiB was plugged 20 seconds before the build started. The soft limit applies to all of the cgroup's memory, including file cache that the kernel can reclaim.

The design that shipped reads anon and shmem from the cgroup's memory.stat and sets no soft limit at all. Page cache never triggers growth, because the kernel reclaims it at the hard limit as it always did. Two checks on staging covered both kinds of memory:

Check Result
Write and read a 1 GiB file Nothing plugged.
Allocate anonymous memory at 100 MiB/s up to 2000 MiB Completed in 20.0 s. No out-of-memory event.

If you build a trigger like this, measure the memory that cannot be reclaimed. A limit or counter that includes page cache will fire for a git clone.

One build: plugged within 11 seconds of the start, returned two minutes after

We built agnix, a Rust workspace of six crates, from its December 2, 2026 commit in one agent VM on staging: a toolchain download, a clone, cargo test --workspace --no-run, then cargo test --workspace. The host sampled the VM every 10 seconds.

Line chart of one agent VM's host memory over eight minutes. Firecracker resident memory is about 110 MiB idle and reaches 954 MiB during download and clone with nothing plugged. 1536 MiB is plugged within 11 seconds of the build starting, memory peaks at 2545 MiB, and after 120 seconds of low use the memory is unplugged and 892 MiB stays.Line chart of one agent VM's host memory over eight minutes. Firecracker resident memory is about 110 MiB idle and reaches 954 MiB during download and clone with nothing plugged. 1536 MiB is plugged within 11 seconds of the build starting, memory peaks at 2545 MiB, and after 120 seconds of low use the memory is unplugged and 892 MiB stays.
One build on staging, sampled every 10 seconds.
Measurement Value
Firecracker resident memory, idle VM 109 to 139 MiB
During download and clone Up to 954 MiB, 0 MiB plugged
First sample with 1536 MiB plugged 11 s after cargo started
Build 2 min 26 s, exit 0
Tests 23 s, exit 0; the largest suite passed 4539 tests
Peak resident memory 2545 MiB
Command limit while plugged 2264 MiB; 0 out-of-memory kills
After the unplug 0 MiB plugged, 892 MiB resident

The memory does not fall back to the idle 110 MiB. The unplug returns the region, and the guest keeps what it still uses of its 1024 MiB boot memory.

A VM restored from a snapshot template gave the same build result as a cold-booted one, and the restore took 5 ms. That check ran on the commit before the final trigger.

The 10-second samples cannot show how long a plug takes. In a separate single-VM prototype on a host with native KVM, plugging 1536 MiB took 17 to 49 ms. Unplugging took 0.19 s after a 2 GiB anonymous allocation and 0.32 s with 1.5 GB of page cache in the guest, and each unplug returned about 1.5 GiB to the host. A plug that takes tens of milliseconds is consistent with the staging check above, where a command allocating 100 MiB/s reached 2000 MiB without an out-of-memory event.

A full node refuses, and the command fails as it did before

Before turning this on, we ran a pressure test on a disposable node of the production type, with the production build of the node daemon and the production overcommit ratio of 1.5. The load inside each building VM was an anonymous ramp to 1900 MiB plus 6.4 GB of file writes over 300 seconds.

VMs on the node Result
1, building Grew by 1536 MiB, completed, back to 0 within 180 s.
4, three building All three grew by 1536 MiB and completed. Available host memory fell to 360 MiB at its lowest. The node raised VM balloons 26 times. No VM was stopped. Memory was back within 180 s.
9, the most that node admits, three building One partial grant of 512 MiB and nine refusals. The three loads were killed for memory inside their own VMs, as they would be without hotplug.

In all three phases the host had no out-of-memory kill, the node daemon did not restart, and no session was stopped. The 360 MiB low point in the second phase is close to the limit. The node recovered memory with balloons and did not have to stop a VM, so hotplug on an overcommitted node depends on pressure handling that already works.

What do you need to run Firecracker memory hotplug?

  • Firecracker 1.17 or newer is what we would run. Hotplug arrived in 1.14. Releases 1.16 and 1.17 fix virtio-mem bugs, among them unplugged memory left writable after a snapshot restore and block accounting that could disagree with the KVM memory slots. We ran 1.17.0.
  • A guest kernel with CONFIG_VIRTIO_MEM, version 5.16 or newer on x86_64 according to Firecracker's documentation. Ours is 6.18.
  • memhp_default_state=online_movable on the kernel command line, or another way to bring hotplugged blocks online in the movable zone.
  • A trigger in the guest that reads anonymous memory, not total cgroup usage.
  • A grant rule on the host that can refuse.

Limits

  • No production VM had asked for memory when we wrote this. Every number here is from staging, the prototype or the pressure test.
  • The pressure test used a synthetic load, not a compiler, and at most three building VMs on one node. Its VMs were all agent sessions: it did not include user services or an idle spare VM, which the node stops first under pressure.
  • The grant rules (node budget, host reserve, refusal under pressure, partial grants) are covered by unit tests and by the full-node phase above, not by a long-running production workload.
  • The host cannot take plugged memory back. Only the guest returns it, so a VM that holds 1536 MiB and stays busy keeps it.
  • A command that needs more than about 2.2 GiB of anonymous memory is still killed.
  • This design fits a node where most VMs are idle most of the time. If every VM compiles at once, size the VMs for that load. For the choice of node, see which host is cheapest for Firecracker.