Commit graph

17 commits

Author SHA1 Message Date
Shintaro Kawamura
94217cc88b swap: make swap/enable not default
"enable" feature in swap crate was set as default to run
unit/integration tests of the crate. https://crrev.com/c/4486546

However missing `default-features = false` at non-root Cargo.toml cause
other crates (e.g. x86_64) installing userfaultfd always.

This makes the swap/enable non-default. CQ still runs unit/integration tests of the swap crate on aarch64 and x86_64.

BUG=b:281935498
TEST=cargo test -p swap --features=swap/enable

Change-Id: I57436c3cb8cbdfac04c3145cc599261b89cf4e0e
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4518090
Commit-Queue: Shin Kawamura <kawasin@google.com>
Reviewed-by: Dennis Kempin <denniskempin@google.com>
2023-05-12 06:15:16 +00:00
Shintaro Kawamura
018e8caf99 swap: add trim command
"crosvm swap trim <socket_path>" command for trimming 2 types of pages
in the staging memory before swapping out to reduce the disk I/O.

* clean pages
* zero pages

The doc comment of TrimContext explains the page types.

BUG=b:265592787
TEST=cargo test -p swap

Change-Id: I5c33149f7d0bfd712f07fd11eb9aa07c1a8b0e7a
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4357224
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Shin Kawamura <kawasin@google.com>
Reviewed-by: David Stevens <stevensd@chromium.org>
2023-03-29 01:22:02 +00:00
Shintaro Kawamura
37f8a7da56 swap: use named memfd for staging memory
ARCVM tracks its memory usage by monitoring the process group shared
memory usage `Platform.Memory.ARC.Shmem`. The staging memory should be
a shared memory not a private anonymous memory.

The staging memory in Region originally does not need to be `Option`
because leaked memory on the staging memory is removed when vmm-swap is
disabled. Rather it has introduced useless complexity to the codebase.

BUG=b:269682102
TEST=cargo test -p swap

Change-Id: I3f032340753b14519dc7dcd76690a7b4b248c1f4
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4332624
Reviewed-by: David Stevens <stevensd@chromium.org>
Commit-Queue: Shin Kawamura <kawasin@google.com>
2023-03-22 06:42:35 +00:00
Shintaro Kawamura
3a38ca057a swap: swap in on a background thread
This is for optimizing swap in performance. This improves the app launch
latency during vmm-swap being disabled from 8s to 2s (while it was
originally 900ms if vmm-swap is disabled).

Each handler method is guarded by mutex.

There would be futher optimization to protect region index calculation
only and unblock UFFD_COPY on both main and swap-in threads. However we
must experiment the benefit and the complexity introduced to the
codebase from it first.

BUG=b:265606668
TEST=cargo test -p swap

Change-Id: I556d7a616da1843fbff8d02d80c85550c7eed93a
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4317028
Reviewed-by: David Stevens <stevensd@chromium.org>
Commit-Queue: Shin Kawamura <kawasin@google.com>
2023-03-22 06:01:44 +00:00
Shintaro Kawamura
bd8d3cc856 swap: open swap file before forking the monitor process
Opeing a file on the monitor process is blocking the upcoming minijail
migration. Open the swap file on the main process and pass the file
descriptor to the monitor process.

Using a single swap file and mmap different ranges of it for different
regions is efficient.

BUG=b:269372016
TEST=cargo test -p swap

Change-Id: I4be150419d263ebe76ad00dae2cafce6e8660cbd
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4253294
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Shin Kawamura <kawasin@google.com>
Reviewed-by: David Stevens <stevensd@chromium.org>
2023-02-17 02:18:39 +00:00
Daniel Verkamp
924f5a29ef data_model: Deprecate VolatileRef
This is a reland of commit d39e5811df

This change mark VolatileRef as deprecated instead of removing it
completely. This change also removed all related methods and functions
using VolatileRef that libcras don't use.

Original change's description:
> data_model: remove VolatileRef
>
> All uses except in test code have been eliminated, so we can remove it
> now.
>
> This was an unsafe abstraction, and we have better alternatives (such as
> the read_obj()/write_obj() functions) that do not create a long-lived
> mutable reference that could easily alias other slices.
>
> BUG=None
> TEST=tools/presubmit --all
>
> Change-Id: I84f1e2487d6211ce24b8fc992fa7675765870132
> Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/3824000
> Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
> Reviewed-by: Keiichi Watanabe <keiichiw@chromium.org>
> Reviewed-by: Alexandre Courbot <acourbot@chromium.org>

TESTED=CQ

BUG=b:204409584
FIXED=b:236759218

Change-Id: I4019870a2321fcd8610669862b5e6ed9bf7c2282
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4215512
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Zihan Chen <zihanchen@google.com>
2023-02-01 23:40:54 +00:00
Shintaro Kawamura
397b103a64 swap: introduce Error type for userfaultfd
This is a refactoring to use original Error type instead of UffdError
from userfaultfd crate. This helps /dev/userfaultfd support which
returns std::io::Error on opening userfaultfd.

This contains a minor change to move register/unregister helpers to
userfaultfd.rs

BUG=b:266642532
TEST=cargo test -p swap

Change-Id: I24079999b018c0f1683fd7b4ee0f97153bd8914d
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4196761
Reviewed-by: David Stevens <stevensd@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Shin Kawamura <kawasin@google.com>
2023-02-01 01:05:25 +00:00
Shintaro Kawamura
f05ef071ae swap: move guest memory to staging memory on multi thread
Enabling vmm-swap copies all the guest memory to the staging memory
while freezing whole the crosvm. Reducing the latency of it is
important.

The most time consuming part of enabing vmm-swap is pure memory copy
which easily can be executed on the multi-thread.

BUG=b:263830401
TEST=cargo test -p swap

Change-Id: I307ad1c459158113cc654e7ba1541381a38b65bb
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4142083
Reviewed-by: David Stevens <stevensd@chromium.org>
Commit-Queue: Shin Kawamura <kawasin@google.com>
Reviewed-by: Dennis Kempin <denniskempin@google.com>
2023-01-23 04:52:02 +00:00
Dennis Kempin
acc162000f Add conditional compilation for unix-only crates
Instead of configuring which crates to --exclude in
test_config.py, we can use conditional compilation to
exclude code that is not supported on windows.

This allows more fine-grained control and also allows
us to use plain cargo for building without complicated
configuration and exclusions.

BUG=b:265829867
TEST=cargo test --lib --bins --workspace
	--target=x86_64-pc-windows-gnu
	--features=all-mingw64

Change-Id: I8422c3f08053bc27d9896b220876a56bd25543d6
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4165868
Reviewed-by: Vikram Auradkar <auradkar@google.com>
Commit-Queue: Dennis Kempin <denniskempin@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2023-01-19 21:21:59 +00:00
Shintaro Kawamura
b266be4e08 swap: add PresentList
Both StagingMemory and SwapFile have similar requirements for tracking
page state. Create a single PresentList utility with the needed
functionality, and refactor both to use the utility.

This change helps the effort to swap-in concurrently.

BUG=b:265606668
TEST=cargo test -p swap

Change-Id: If21649de918d9b98fdcbf895e5368b46c277b8e3
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4164548
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: David Stevens <stevensd@chromium.org>
Commit-Queue: Shin Kawamura <kawasin@google.com>
2023-01-18 02:18:27 +00:00
Shintaro Kawamura
e835633bf8 swap: MADV_REMOVE aligning with the THP size
When Transparent Hugepage on the host linux is enabled and crosvm runs
with `--hugepages` flag enabled, MADV_REMOVE can be unreliable if it
isn't aligned to THP size. On kernels without 71725ed10c40 ("mm: huge
tmpfs: try to split_huge_page() when punching hole"), MADV_REMOVE will
simply zero out subpages. Even with that change, if anything else in the
kernel happens to hold a ref to a THP concurrently with MADV_REMOVE,
then the THP cannot be split and will simply be zeroed. When THP aren't
split, subsequent faults will be minor faults instead of major faults
and thus won't be handled by the current vmm-swap implementation.

This patch queries /sys/kernel/mm/transparent_hugepage/hpage_pmd_size
for the THP size. If that file is not available (e.g. due to
sandboxing), then it falls back to a THP size of 2MiB, which is the case
for all common configurations supported by crosvm.

BUG=b:265242249
TEST=cargo test -p swap

Change-Id: I466069d1bc07e7d1e78d57e8830471e0dc3a6c0b
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4160616
Reviewed-by: David Stevens <stevensd@chromium.org>
Commit-Queue: Shin Kawamura <kawasin@google.com>
2023-01-16 05:30:10 +00:00
Shintaro Kawamura
3e9e4ad946 swap: add max size limiter on moving pages to staging
Added `max_batch_size` parameter to PageHandler::move_to_staging().
Defer the MADV_REMOVE until the size of the batch reaches the max size
to reduce the system calls. If the size of a chunk is too big, it splits
the chunk into different batches to avoid memory usage spike.

BUG=b:263830401
TEST=cargo test -p swap

Change-Id: I8ca7e592a311980271cffd9bba8fbbe2a582ac3e
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4151867
Reviewed-by: David Stevens <stevensd@chromium.org>
Commit-Queue: Shin Kawamura <kawasin@google.com>
2023-01-12 02:33:42 +00:00
Shintaro Kawamura
0897c87424 swap: split vmm-swap out command
This splits the `crosvm swap enable` command, which pauses the whole
crosvm processes while it write all the active pages on the guest memory
into the swap file, into 2 commands.

On `crosvm swap enable` command, the monitor process just moves the
guest memroy to the staging memory while blocking the whole crosvm
processes with SIGSTOP.

On `crosvm swap out` command, the monitor process writes the active
pages in the staging memory to the swap file.

The page faults between `swap enable` and `swap out` are swapped in from
the staging memory.

By waiting some time before sending swap out request after enabling
vmm-swap, hot pages on the guest memory, which are frequently accessed,
are swapped in from the staging memory and we can reduce the TBW of the
swap file and reduce the latency of page faults for hot pages.

BUG=b:263830401
TEST=cargo test -p swap -p base

Change-Id: Ia83f5052cc0bd4d25089c6a6680e40a1fa1661c0
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4134149
Reviewed-by: David Stevens <stevensd@chromium.org>
Commit-Queue: Shin Kawamura <kawasin@google.com>
2023-01-10 08:07:57 +00:00
Shintaro Kawamura
fc88a6091d swap: add disable command to vmm-swap
Handling page faults using userfaultfd for all the time after vmm-swap
is enabled originally has overhead. By disabling swapping, we can use
crosvm without the overhead when the crosvm become used heavily again.

This also refactor PageHandler and extract register logic to lib.rs.

BUG=b:215093219
TEST=cargo test -p swap

Change-Id: Id81d84ebe40067b4f19b212f6b81cbaf249c0c3c
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4005474
Reviewed-by: David Stevens <stevensd@chromium.org>
Commit-Queue: Shin Kawamura <kawasin@google.com>
2022-11-18 01:23:45 +00:00
Shintaro Kawamura
f20622f6fb swap: handle ENOMEM error from userfaultfd
userfaultfd returns ENOMEM on register if the corresponding process is
obsolete (e.g. the process dies, memory space is refleshed by execve).

BUG=b:253555760
TEST=cargo test -p swap

Change-Id: I9a6a7f0ed45cdc91bdf17e5d29e4c215b7f575a5
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4025736
Reviewed-by: David Stevens <stevensd@chromium.org>
Commit-Queue: Shin Kawamura <kawasin@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2022-11-17 02:39:55 +00:00
Shintaro Kawamura
87679c98d2 swap: fix tempdir usage in tests
Directories from TempDir::into_path() are not automatically deleted.
We should use TempDir::path() instead.

BUG=b:253555760
TEST=cargo test -p swap

Change-Id: I9f0c5398a7eaa16317f9e40f16231f9b7df0a3a5
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4033426
Reviewed-by: David Stevens <stevensd@chromium.org>
Commit-Queue: Shin Kawamura <kawasin@google.com>
2022-11-17 02:37:47 +00:00
Shintaro Kawamura
df2ec8ec88 Reland "swap: add PageHandler"
origin:
https://chromium-review.googlesource.com/c/crosvm/crosvm/+/3944711

PageHandler handles events from userfaultfd using SwapFile.

tests are placed as integration tests since they rely on userfaultfd(2).

design document: go/tanooki-phase1-dd

BUG=b:215093219
TEST=cargo test -p swap page_handler

Change-Id: I64d213d27174d75ee618c4675ea52dbac58bfe59
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4016144
Reviewed-by: David Stevens <stevensd@chromium.org>
Commit-Queue: Shin Kawamura <kawasin@google.com>
2022-11-14 06:14:07 +00:00