Skip to content

Optimization: Cancel pending async page loads of decommitted pages instead of waiting for them #16151

Open

Description

@EtiennePerot

The idea

During runsc restore --background, the async page loader fills the MemoryFile from the pages file while the application already runs. If the application MADV_DONTNEEDs part of a huge-page-backed anonymous mapping meanwhile, MemoryFile.Decommit punches a hole that the loader later refills with checkpoint data (or overwrites when an in-flight read lands), so the application reads back stale data instead of zeros. In a probe, about 70% of decommitted pages came back stale. Open PR #15722 found this race and fixes it by awaiting the load of every decommitted range before decommitting. That is correct, but it stalls madvise on restore I/O: a batch of 64 small MADV_DONTNEEDs takes 153 ms on a fast disk and 5.4 s on a 64 MB/s one, instead of about 1.4 ms. The bug only exists where application huge pages are enabled (shmem_enabled=advise with a compatible defrag setting); COS GKE nodes (shmem_enabled=never) are unaffected.

gVisor could fix the race without stalling madvise, by cancelling the loads of decommitted pages instead. Decommit would remove unloaded ranges that haven't been read and have no waiters, since they will read as zero anyway. For ranges whose read is in flight (or awaited), it would set a flag so that the loader decommits them again on completion, before they leave unloaded and before waiters wake. Since every access to an unloaded page already waits for that point, no one sees stale data, and writes made after the wait are kept. #15722's mm-side change (propagating load errors from Decommit, so madvise returns EIO after a failed load) could be reused unchanged.

Performance impact

  • Stale pages after MADV_DONTNEED during background restore: 45-49 of 64 → 0, including when the sandbox is checkpointed again before loading completes.
  • madvise batch latency stays the same as today (about 1.4-1.7 ms, not significant), against 153 ms (fast disk) and 5.4 s (64 MB/s) with Fix decommit racing with asynchronous checkpoint reads #15722's await approach.
  • Total async load time unchanged; pages that are cancelled are never read.
  • //test/benchmarks/base: no change at p<0.01 in startup_test, node_test or usage_test (SizeNginx memory +0.75% at p=0.021 did not replicate in 16 further rounds).

Beat the autoresearch

The following benchmark results were benchmarked using this patch: master...EtiennePerot:gvisor:autoresearch/decommit-cancels-unloaded

Add value by beating these benchmark numbers, or reducing any regressions they cause, or both
goos: linux
goarch: amd64
pkg: background restore + MADV_DONTNEED probe harness (512 MiB app)
                                         │    before    │                after                 │
                                         │    sec/op    │    sec/op      vs base               │
MadviseBatch/disk=fast                     1.395m ± 20%   1.593m ±  12%       ~ (p=0.265 n=20)
AsyncLoad/disk=fast                        248.0m ±  3%   244.0m ±   4%       ~ (p=0.846 n=20)
MadviseBatch/disk=64MBps                   1.450m ± 42%   1.665m ± 152%       ~ (p=0.353 n=10)
AsyncLoad/disk=64MBps                       8.006 ±  1%    8.006 ±   0%       ~ (p=0.839 n=10)
DoubleCheckpointMadviseBatch/disk=64MBps   1.666m ± 28%   1.705m ±  24%       ~ (p=0.684 n=10)
geomean                                    23.18m         24.50m         +5.71%

                                         │   before    │                  after                   │
                                         │ stale-pages │ stale-pages  vs base                     │
MadviseBatch/disk=fast                      45.00 ± 7%     0.00 ± 0%  -100.00% (p=0.000 n=20)
MadviseBatch/disk=64MBps                    48.50 ± 7%     0.00 ± 0%  -100.00% (p=0.000 n=10)
DoubleCheckpointMadviseBatch/disk=64MBps    45.00 ± 7%     0.00 ± 0%  -100.00% (p=0.000 n=10)
geomean                                     46.14                     ?                       ¹ ²
¹ summaries must be >0 to compute geomean
² ratios must be >0 to compute geomean

pkg: gvisor.dev/gvisor/test/benchmarks/base
                       │   before    │               after                │
                       │   sec/op    │   sec/op     vs base               │
StartupEmpty-16          174.5m ± 2%   173.1m ± 3%       ~ (p=0.149 n=16)
StartupNginx-16          459.8m ± 1%   461.9m ± 2%       ~ (p=0.515 n=16)
StartupNode-16           562.0m ± 1%   560.3m ± 2%       ~ (p=0.897 n=16)
CheckpointEmpty-16       55.72m ± 2%   55.21m ± 1%       ~ (p=0.423 n=16)
RestoreEmpty-16          185.9m ± 1%   185.9m ± 2%       ~ (p=0.752 n=16)
CheckpointNginx-16       89.99m ± 1%   90.23m ± 2%       ~ (p=0.985 n=16)
RestoreNginx-16          233.0m ± 2%   230.2m ± 2%       ~ (p=0.867 n=16)
Node/concurrency.1-16    1.594m ± 1%   1.603m ± 1%       ~ (p=0.515 n=16)
Node/concurrency.5-16    1.303m ± 3%   1.305m ± 1%       ~ (p=0.838 n=16)
Node/concurrency.10-16   1.297m ± 1%   1.302m ± 2%       ~ (p=0.985 n=16)
Node/concurrency.25-16   1.197m ± 2%   1.206m ± 5%       ~ (p=0.254 n=16)
geomean                  31.62m        31.60m       -0.06%

                       │      before       │                   after                    │
                       │ average_latency.s │ average_latency.s  vs base                 │
Node/concurrency.1-16          1.000m ± 0%         1.000m ± 0%       ~ (p=1.000 n=16) ¹
Node/concurrency.5-16          3.600m ± 3%         3.600m ± 3%       ~ (p=1.000 n=16)
Node/concurrency.10-16         6.900m ± 1%         6.900m ± 1%       ~ (p=0.212 n=16)
Node/concurrency.25-16         15.45m ± 1%         15.50m ± 1%       ~ (p=0.860 n=16)
geomean                        4.426m              4.430m       +0.08%
¹ all samples are equal

                       │         before          │                     after                      │
                       │ requests_per_second.QPS │ requests_per_second.QPS  vs base               │
Node/concurrency.1-16                 992.9 ± 0%                996.0 ± 1%       ~ (p=0.346 n=16)
Node/concurrency.5-16                1.398k ± 1%               1.401k ± 0%       ~ (p=0.717 n=16)
Node/concurrency.10-16               1.451k ± 1%               1.446k ± 2%       ~ (p=0.345 n=16)
Node/concurrency.25-16               1.606k ± 1%               1.601k ± 0%       ~ (p=0.570 n=16)
geomean                              1.341k                    1.341k       -0.04%

             │                before                │                            after                            │
             │ average_container_memory_usage.bytes │ average_container_memory_usage.bytes  vs base               │
SizeEmpty-16                            14.78M ± 1%                            14.94M ± 1%       ~ (p=0.110 n=16)
SizeNginx-16                            23.90M ± 1%                            24.08M ± 0%  +0.75% (p=0.021 n=16)
SizeNode-16                             48.77M ± 0%                            48.80M ± 1%       ~ (p=0.897 n=16)
geomean                                 25.83M                                 25.99M       +0.61%
Benchmark environment

GCE c4-standard-16 (c4-standard-8 for DoubleCheckpointMadviseBatch), COS cos-129-19506-448-8 (kernel 6.12.105+), hyperdisk-balanced, with THP switched at runtime to shmem_enabled=advise / defrag=defer so the affected code path is reachable. before is master at 2511bc60218a, after is the patch above. The probe harness fills 512 MiB in an app, checkpoints it uncompressed, drops caches, runs runsc restore --background, then MADV_DONTNEEDs 64 random 4 KiB pages in distinct 2 MiB huge pages and checks every byte: MadviseBatch is the time for the 64 calls, stale-pages how many of them read back non-zero, AsyncLoad the async page load time; disk=64MBps rows are throttled with io.max. DoubleCheckpointMadviseBatch additionally rewrites some pages and checkpoints and restores again while loading is still in progress before the check (stale count out of 60). The second block is the real //test/benchmarks/base targets run against docker runtimes for each build; the usage_test memory rows share names with size_test, so only their memory metric is shown.

Activity

  1. added this to the Lightspeed milestone on Oct 21, 2026
  2. added
    area: performance
    on Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: performance

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions