Skip to content

runsc: add restore --background-load=eager|lazy - #15721

Open
jingyugao wants to merge 2 commits into
gvisor:masterfrom
jingyugao:feat/restore-background-throttle
Open

jingyugao wants to merge 2 commits into
gvisor:masterfrom
jingyugao:feat/restore-background-throttle

Conversation

@jingyugao

@jingyugao jingyugao commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

During background restore, reads of pages without waiters and page-fault reads share the storage device. The device serves requests roughly in order, so a page fault waits behind all prefetch bytes in flight: up to 48 MiB with the default reader (12 reads × 4 MiB).

This matters for agent sandboxes: they often keep a browser or a Node.js dev server in memory, but the first actions after restore are short commands like ls and cat, which touch little of it.

Following the review suggestion, this PR adds runsc restore --background-load=eager|lazy:

  • eager (default): current behavior.
  • lazy: no reads of pages without waiters. Pages are read when the application faults on them (a fault on an async-loading MemoryFile already maps and loads up to 32 MiB around it), or when MemoryFile.AwaitLoadAll() needs them before the next checkpoint. The pages file must stay available until then. Requires --background.

The async page loader no longer depends on background reads to make progress: awaited loads wake it when it is idle, and it completes when all MemoryFiles are registered and loaded, rather than when it first has no work after registration. lazy is then only a condition that skips reads of pages without waiters; eager behavior does not change.

Results

Kubernetes node, 8 vCPU, local virtual disk, read bandwidth limited with cgroup io.max (the limit also covers prewarm reads). The page cache is dropped before each restore.

Time from restore start to the first command done. Measured on the node: the first command runs with runsc exec as soon as the application starts, so orchestration (Pod creation, readiness probes) is excluded. "1 background read" is the earlier version of this PR (prefetch limited to one read in flight).

Workload (memory) Bandwidth 1 background read lazy lazy + prewarm¹
Headless Chromium, 4 tabs (~740 MB) → ls / 50 MB/s 6.60 s 2.28 s 4.17 s
100 MB/s 3.85 s 1.46 s 2.45 s
Ghost blog, Node.js (~170 MB) → first HTTP response 50 MB/s 5.25 s 5.12 s 4.87 s
100 MB/s 2.53 s 2.84 s 2.49 s
Idle sandbox (~18 MB) → ls / none / 100 MB/s 0.21 / 0.26 s 0.24 / 0.24 s —

¹ cat of the checkpoint files into the page cache, started with the restore.

Median of 3–5 runs, configurations interleaved; Chromium at 50 MB/s: p = 0.008 (two-sided exact Mann-Whitney U, 5 runs each).

  • Most of the gain is in the restore itself: from restore start to the application running takes 0.24–0.48 s with lazy, versus 1.5–4.3 s otherwise, because kernel restore page faults no longer wait behind prefetch reads.
  • A first request that touches much memory (Ghost renders a page from its heap) takes many serial faults with lazy, so it is not faster. Prewarming the page cache brings it back to the same level.
  • For an idle sandbox, the time is about 0.2 s in all modes; about 0.16 s of it is waiting for the application container to be started.

End to end with eager (restore request through the orchestrator to the first command, 3 runs):

Workload Bandwidth eager lazy
Chromium → ls / 100 MB/s 8.10 s 2.72 s
none 1.46 s 1.50 s
Ghost → first HTTP response 100 MB/s 3.41 s 3.66 s
none 1.40 s 1.93 s

Cost. With lazy, the next checkpoint first reads the pages that are still unloaded. Without a bandwidth limit this added 0.4–0.75 s to suspend (170–700 MB). On slow storage it is about (unloaded bytes / bandwidth), unless the page cache was prewarmed.

Tests

  • TestAsyncPageLoadLazy: no reads start before a page is awaited; one awaited page causes one read; the load does not complete while pages are unloaded; AwaitLoadAll() loads the remaining pages with the correct contents.
  • Passed //pkg/sentry/pgalloc:pgalloc_test (30 runs; TestAsyncSaveToPreservesContents covers eager completion), //pkg/sentry/kernel:kernel_test, //runsc/sandbox:sandbox_test, //runsc/config:config_test, make lint. Built //runsc, //runsc/cmd/sentry:gvisor_sentry, //runsc/container:container_test.

Separately, on current master containerManager.StartSubcontainer() calls RestoreSubcontainer(args, nil), and RestoreSubcontainer() writes reply.KernelStarted, so a runsc start of a subcontainer on a restoring sandbox panics (introduced in a49825f). The end-to-end tests above ran with a local fix (&sentryapi.RestoreReply{}); it is not part of this PR.

AI assistance is disclosed in accordance with CONTRIBUTING.md.

@gemini-code-assist

Copy link
Copy Markdown

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@jingyugao
jingyugao force-pushed the feat/restore-background-throttle branch from 433c671 to feb5c54 Compare October 13, 2026 02:47
@EtiennePerot EtiennePerot added this to the Lightspeed milestone Oct 22, 2026
@jingyugao jingyugao changed the title Throttle background checkpoint reads during restore pgalloc: add option to limit background reads during async page load Oct 28, 2026
@jingyugao
jingyugao force-pushed the feat/restore-background-throttle branch 2 times, most recently from 1858794 to 783c415 Compare October 28, 2026 05:17
@jingyugao
jingyugao marked this pull request as ready for review October 28, 2026 05:18
@ayushr2

ayushr2 commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Do you think a cleaner API will be just to have a restore mode where we lazy load with read ahead. Currently the restore is very eager. We can make it lazy. I assume that's the problem you're solving?

We can add a flag to runsc restore --background-load eager|lazy. Defaults to "eager" which is current behavior. For "lazy", we can totally kill all the prefetch reads. When the app page faults, we read ahead a fixed amount ahead. The lazy mode has several benefits:

  • In most CPU sandboxes, we don't require all the pages (even throughout the sandbox lifecycle). So we don't end up spending networking budgets on unneeded things.
  • CPU utilization on restore is very low. With eager loading, CPU utilization is quite high in the initial phase of sandbox while background loading is active.
  • It prioritizes the page fault loading always. What this patch is trying to achieve.

The downside is that all page-faults in the future incur more latency. But it's arguably more resource efficient. I think the flag captures the trade-off well.

The proposed --restore-background-slots API seems a bit awkward. We needed cleaner and more intuitive APIs.

@jingyugao
jingyugao force-pushed the feat/restore-background-throttle branch from 783c415 to cd1614f Compare October 28, 2026 13:55
@jingyugao jingyugao changed the title pgalloc: add option to limit background reads during async page load runsc: add restore --background-load=eager|lazy Oct 28, 2026
@jingyugao
jingyugao force-pushed the feat/restore-background-throttle branch from cd1614f to 8fcdd62 Compare October 28, 2026 14:07
@jingyugao

Copy link
Copy Markdown
Contributor Author

Thanks, done: the PR now adds runsc restore --background-load=eager|lazy (default eager) instead of --restore-background-slots. Faults already load up to 32 MiB around them, so there is no separate read-ahead.

Our use case is agent sandboxes: they often keep a browser or a Node.js dev server in memory, but the first actions after restore are short commands like ls and cat, which touch little of it. With lazy, time from restore start to the first ls in a headless Chromium sandbox drops 2.6–2.9x at 50–100 MB/s. The cost moves to the next checkpoint, which reads the unloaded pages first. Details are in the description.

Unrelated: on master, StartSubcontainer() calls RestoreSubcontainer(args, nil), which panics on reply.KernelStarted (from a49825f). Happy to send a one-line fix.

jingyugao added a commit to jingyugao/gvisor that referenced this pull request Oct 31, 2026

@ayushr2 ayushr2 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two issues remain in lazy restore completion and lifecycle handling; details are inline.

Codex posting on behalf of Ayush

Comment thread pkg/sentry/pgalloc/save_restore.go Resolved
Comment thread runsc/boot/controller.go Resolved

@ayushr2 ayushr2 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fold 7e7d36fa5 into 8fcdd62e5 before landing, and rebase away the merge commit. The completion/failure fixes are required for the lazy-restore feature to work correctly, so they belong in the same logical commit; gVisor applies PR commits directly rather than squashing them.

Codex posting on behalf of Ayush

Comment thread runsc/boot/controller.go Resolved
@jingyugao
jingyugao force-pushed the feat/restore-background-throttle branch from 79051b7 to 8c01fd4 Compare November 12, 2026 06:03
@jingyugao
jingyugao force-pushed the feat/restore-background-throttle branch from 8c01fd4 to 2c7b261 Compare November 30, 2026 03:05

@ayushr2 ayushr2 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found two issues that can persist after a lazy restore reports success.

Codex posting on behalf of Ayush

// Fill remaining queue with reads for pages with no waiters.
throttled := apfl.throttled.Load()
if apfl.canEnqueue() && (!throttled || (apfl.qavail == maxParallel && apfl.curOp == nil)) {
if !apfl.lazy && apfl.canEnqueue() && (!throttled || (apfl.qavail == maxParallel && apfl.curOp == nil)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preserve MADV_DONTNEED on cold anonymous huge pages. After a lazy WaitRestore() succeeds, an application can discard part of a still-cold, privately owned anonymous huge page. MemoryManager.Decommit keeps that PMA and calls MemoryFile.Decommit, which zeroes the backing range but leaves it in amfl.unloaded. On the next fault, DataFD reads the pre-discard checkpoint bytes back into a range the application expects to be zero. Eager restore has drained those reads by the time WaitRestore() returns.

Could the partial-huge-page optimization fall back to the existing unmap/DecRef path while the MemoryFile is loading? That path already handles outstanding page reads.

Codex posting on behalf of Ayush

Comment thread runsc/boot/restore.go Outdated
}
}

if !r.lazy {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Release restore-only memory once the sandbox is ready. A single untouched page can keep postRestore blocked on pageLoadResult for the sandbox’s lifetime. This read of r.lazy after the receive keeps the whole restorer reachable even after onRestoreDone clears cm.restorer. That includes the closed statefile’s 256 KiB SimpleReader buffer and the decoded checkpoint OCI specs for every container. Eager restore releases them when prefetch finishes.

Could the late-error monitor retain just the dependencies it needs, or could the restorer drop the consumed reader and checkpoint specs at their last use?

Codex posting on behalf of Ayush

@jingyugao
jingyugao force-pushed the feat/restore-background-throttle branch from 2c7b261 to d79fb66 Compare December 2, 2026 02:38

@ayushr2 ayushr2 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The partial huge-page discard and restorer lifetime issues from the previous review are fixed. I found one remaining memory-accounting issue and a test caller that needs updating when this lands on current master.

Codex posting on behalf of Ayush

Comment thread pkg/sentry/pgalloc/pgalloc.go Resolved
Comment thread runsc/container/container.go Resolved
@jingyugao
jingyugao force-pushed the feat/restore-background-throttle branch 2 times, most recently from 2a3fea6 to 9629305 Compare December 2, 2026 08:16

@ayushr2 ayushr2 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The resident-neighbor accounting fix works. I found two resource costs in the revised accounting path and its new regression test.

Codex posting on behalf of Ayush

Comment thread pkg/sentry/pgalloc/save_restore.go Resolved
Comment thread pkg/sentry/pgalloc/save_restore_test.go Resolved
During background restore, reads of pages without waiters and page-fault
reads share the storage device, so page faults wait behind background
reads. With the default reader, up to 48 MiB can be queued ahead of a
page fault.

Add --background-load to runsc restore. eager (the default) keeps the
current behavior. lazy never starts reads of pages without waiters:
pages are read when the application faults on them (a fault on an
async-loading MemoryFile already maps up to 32 MiB around it), or when
MemoryFile.AwaitLoadAll() needs them before the next checkpoint. The
pages file must stay available and unchanged until then. lazy requires
--background.

The async page loader no longer depends on background reads to make
progress: awaited loads wake it when it is idle, and it completes when
all MemoryFiles are registered and loaded, rather than when it first has
no work after all MemoryFiles are registered. eager prefetch behavior
does not change.

Complete MemoryFiles when their remaining unloaded pages are discarded.
Release the metadata reader after metadata loading, and let lazy restore
finish without waiting for untouched pages. Preserve page-load error
handling before and after restore completion.

While a MemoryFile is loading, decommitting part of a hugepage-backed
private anonymous mapping invalidates the pma instead of zeroing the
backing range in place, so that checkpoint reads cannot restore
discarded data into the mapping. With lazy loading, this applies until
all pages are loaded or discarded.

After a lazy restore completes, waiting for page loading must not keep
the restorer reachable, so that consumed restore state is released.

Track host page commitment separately from pending checkpoint reads.
Account for pages when the host reports them resident or a checkpoint
read completes. Serialize lazy restore readiness with the recording
of terminal page-load errors.

Signed-off-by: gaojingyu <[email protected]>
Refine lazy restore cancellation and page-loader resource lifetime.

Allocate I/O vectors on demand for lazy loads and queue discarded
MemoryFiles without completing them twice.

Preserve partial huge-page mappings only when no complete interior huge
page must be invalidated. Add regression tests and async load/decommit
benchmarks.

Benchmark results (24 baseline and 48 modified samples):
- AsyncPageLoad/eager: 3.266 ms -> 3.152 ms (p=0.052).
- DecommitHugePage: 2.599 us -> 2.758 us (p=0.052).

Signed-off-by: gaojingyu <[email protected]>
@jingyugao
jingyugao force-pushed the feat/restore-background-throttle branch from 9629305 to f3a971b Compare December 2, 2026 10:06
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.

3 participants