Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UFFD (v1.2.1) #10

Draft
wants to merge 13 commits into
base: v1.2.1-base
Choose a base branch
from
4 changes: 2 additions & 2 deletions .cargo/config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[build]
target-dir = "build/cargo_target"
target = "x86_64-unknown-linux-musl"
# target = "x86_64-unknown-linux-musl"
# target-dir = "build/cargo_target"

[net]
git-fetch-with-cli = true
29 changes: 20 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions resources/seccomp/aarch64-unknown-linux-musl.json
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,19 @@
}
]
},
{
"syscall": "msync",
"comment": "Used to sync memory from mmap to disk",
"args": [
{
"index": 2,
"type": "dword",
"op": "eq",
"val": 4,
"comment": "MS_SYNC"
}
]
},
{
"syscall": "rt_sigaction",
"comment": "rt_sigaction is used by libc::abort during a panic to install the default handler for SIGABRT",
Expand Down
25 changes: 25 additions & 0 deletions resources/seccomp/x86_64-unknown-linux-musl.json
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,31 @@
}
]
},
{
"syscall": "msync",
"comment": "Used to sync memory from mmap to disk",
"args": [
{
"index": 2,
"type": "dword",
"op": "eq",
"val": 4,
"comment": "MS_SYNC"
}
]
},
{
"syscall": "memfd_create",
"comment": "Used to create a memory backed file descriptor that can be used to save memory to"
},
{
"syscall": "nanosleep",
"comment": "Debugging sleep"
},
{
"syscall": "copy_file_range",
"comment": "debugging"
},
{
"syscall": "rt_sigaction",
"comment": "rt_sigaction is used by libc::abort during a panic to install the default handler for SIGABRT",
Expand Down
2 changes: 2 additions & 0 deletions src/api_server/src/parsed_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::request::logger::parse_put_logger;
use crate::request::machine_configuration::{
parse_get_machine_config, parse_patch_machine_config, parse_put_machine_config,
};
use crate::request::memory_backend::parse_put_memory_backend;
use crate::request::metrics::parse_put_metrics;
use crate::request::mmds::{parse_get_mmds, parse_patch_mmds, parse_put_mmds};
use crate::request::net::{parse_patch_net, parse_put_net};
Expand Down Expand Up @@ -112,6 +113,7 @@ impl ParsedRequest {
(Method::Put, "network-interfaces", Some(body)) => {
parse_put_net(body, path_tokens.get(1))
}
(Method::Put, "memory-backend", Some(body)) => parse_put_memory_backend(body),
(Method::Put, "shutdown-internal", None) => {
Ok(ParsedRequest::new(RequestAction::ShutdownInternal))
}
Expand Down
46 changes: 46 additions & 0 deletions src/api_server/src/request/memory_backend.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

use super::super::VmmAction;
use crate::parsed_request::{Error, ParsedRequest};
use crate::request::Body;
use logger::{IncMetric, METRICS};
use vmm::vmm_config::snapshot::MemBackendConfig;

pub(crate) fn parse_put_memory_backend(body: &Body) -> Result<ParsedRequest, Error> {
METRICS.put_api_requests.memory_backend_cfg_count.inc();
Ok(ParsedRequest::new_sync(VmmAction::SetMemoryBackend(
serde_json::from_slice::<MemBackendConfig>(body.raw()).map_err(|e| {
METRICS.put_api_requests.memory_backend_cfg_fails.inc();
Error::SerdeJson(e)
})?,
)))
}

#[cfg(test)]
mod tests {
use std::path::PathBuf;

use vmm::vmm_config::snapshot::MemBackendType;

use super::*;

#[test]
fn test_parse_memory_backing_file() {
assert!(parse_put_memory_backend(&Body::new("invalid_payload")).is_err());

let body = r#"{
"backend_type": "File",
"backend_path": "./memory.snap"
}"#;
let same_body = MemBackendConfig {
backend_type: MemBackendType::File,
backend_path: PathBuf::from("./memory.snap"),
};
let result = parse_put_memory_backend(&Body::new(body));
assert!(result.is_ok());
let parsed_req = result.unwrap_or_else(|_e| panic!("Failed test."));

assert!(parsed_req == ParsedRequest::new_sync(VmmAction::SetMemoryBackend(same_body)));
}
}
1 change: 1 addition & 0 deletions src/api_server/src/request/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod drive;
pub mod instance_info;
pub mod logger;
pub mod machine_configuration;
pub mod memory_backend;
pub mod metrics;
pub mod mmds;
pub mod net;
Expand Down
23 changes: 23 additions & 0 deletions src/api_server/swagger/firecracker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,29 @@ paths:
description: Internal server error
schema:
$ref: "#/definitions/Error"

/memory-backend:
put:
summary: Configures a memory backend to sync the memory changes from during the runtime of the vm
operationId: putMemoryBackend
parameters:
- name: body
in: body
description: The memory backend to use
required: true
schema:
$ref: "#/definitions/MemoryBackend"
responses:
204:
description: Memory backend configured
400:
description: Memory backend failed
schema:
$ref: "#/definitions/Error"
default:
description: Internal server error.
schema:
$ref: "#/definitions/Error"

/metrics:
put:
Expand Down
2 changes: 2 additions & 0 deletions src/cpuid/src/transformer/amd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ impl CpuidTransformer for AmdCpuidTransformer {
leaf_0x8000001d::LEAF_NUM => Some(amd::update_extended_cache_topology_entry),
leaf_0x8000001e::LEAF_NUM => Some(amd::update_extended_apic_id_entry),
0x8000_0002..=0x8000_0004 => Some(common::update_brand_string_entry),
// Disable async PF, as it hangs the VM for some reason when loading from snapshot/uffd.
0x4000_0001 => Some(common::disable_kvm_feature_async_pf),
_ => None,
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/cpuid/src/transformer/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ pub fn update_brand_string_entry(
Ok(())
}

// KVM feature bits
#[cfg(target_arch = "x86_64")]
const KVM_FEATURE_ASYNC_PF_INT_BIT: u32 = 14;

pub fn disable_kvm_feature_async_pf(
entry: &mut kvm_cpuid_entry2,
_vm_spec: &VmSpec,
) -> Result<(), Error> {
entry.eax.write_bit(KVM_FEATURE_ASYNC_PF_INT_BIT, false);

Ok(())
}

pub fn update_cache_parameters_entry(
entry: &mut kvm_cpuid_entry2,
vm_spec: &VmSpec,
Expand Down
2 changes: 2 additions & 0 deletions src/cpuid/src/transformer/intel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ impl CpuidTransformer for IntelCpuidTransformer {
leaf_0xa::LEAF_NUM => Some(intel::update_perf_mon_entry),
leaf_0xb::LEAF_NUM => Some(intel::update_extended_topology_entry),
0x8000_0002..=0x8000_0004 => Some(common::update_brand_string_entry),
// Disable async PF, as it hangs the VM for some reason when loading from snapshot/uffd.
0x4000_0001 => Some(common::disable_kvm_feature_async_pf),
_ => None,
}
}
Expand Down
41 changes: 21 additions & 20 deletions src/devices/src/virtio/balloon/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub(crate) fn compact_page_frame_numbers(v: &mut [u32]) -> Vec<(u32, u32)> {
pub(crate) fn remove_range(
guest_memory: &GuestMemoryMmap,
range: (GuestAddress, u64),
restored: bool,
_restored: bool,
) -> std::result::Result<(), RemoveRegionError> {
let (guest_address, range_len) = range;

Expand All @@ -80,25 +80,26 @@ pub(crate) fn remove_range(
.get_host_address(guest_address)
.map_err(|_| RemoveRegionError::AddressTranslation)?;

// Mmap a new anonymous region over the present one in order to create a hole.
// This workaround is (only) needed after resuming from a snapshot because the guest memory
// is mmaped from file as private and there is no `madvise` flag that works for this case.
if restored {
// SAFETY: The address and length are known to be valid.
let ret = unsafe {
libc::mmap(
phys_address.cast(),
range_len as usize,
libc::PROT_READ | libc::PROT_WRITE,
libc::MAP_FIXED | libc::MAP_ANONYMOUS | libc::MAP_PRIVATE,
-1,
0,
)
};
if ret == libc::MAP_FAILED {
return Err(RemoveRegionError::MmapFail(io::Error::last_os_error()));
}
};
// CodeSandbox: since we use UFFD handler, this is not needed for us. In fact, it breaks the UFFD handler
// if this happens right now, as it unregisters the UFFD handler for the given range.
// // Mmap a new anonymous region over the present one in order to create a hole.
// // This workaround is (only) needed after resuming from a snapshot because the guest memory
// // is mmaped from file as private and there is no `madvise` flag that works for this case.
// if restored {
// let ret = unsafe {
// libc::mmap(
// phys_address as *mut _,
// range_len as usize,
// libc::PROT_READ | libc::PROT_WRITE,
// libc::MAP_FIXED | libc::MAP_ANONYMOUS | libc::MAP_PRIVATE,
// -1,
// 0,
// )
// };
// if ret == libc::MAP_FAILED {
// return Err(RemoveRegionError::MmapFail(io::Error::last_os_error()));
// }
// };

// Madvise the region in order to mark it as not used.
// SAFETY: The address and length are known to be valid.
Expand Down
5 changes: 2 additions & 3 deletions src/jailer/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,8 @@ impl Env {
// a new PathBuf, with something like chroot_dir.join(exec_file_name) ?!
self.chroot_dir.push(exec_file_name);

// TODO: hard link instead of copy? This would save up disk space, but hard linking is
// not always possible :(
fs::copy(&self.exec_file_path, &self.chroot_dir).map_err(|err| {
// We hard link instead of copy for space savings and to retain the capabilities
fs::hard_link(&self.exec_file_path, &self.chroot_dir).map_err(|err| {
Error::Copy(self.exec_file_path.clone(), self.chroot_dir.clone(), err)
})?;

Expand Down
4 changes: 4 additions & 0 deletions src/logger/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,10 @@ pub struct PutRequestsMetrics {
pub machine_cfg_count: SharedIncMetric,
/// Number of failures in configuring the machine.
pub machine_cfg_fails: SharedIncMetric,
/// Number of PUTs for setting memory backing file.
pub memory_backend_cfg_count: SharedIncMetric,
/// Number of failures in configuring the machine.
pub memory_backend_cfg_fails: SharedIncMetric,
/// Number of PUTs for initializing the metrics system.
pub metrics_count: SharedIncMetric,
/// Number of failures in initializing the metrics system.
Expand Down
2 changes: 1 addition & 1 deletion src/vm-memory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ pub fn create_guest_memory(
for region in regions {
let flags = match region.0 {
None => libc::MAP_NORESERVE | libc::MAP_PRIVATE | libc::MAP_ANONYMOUS,
Some(_) => libc::MAP_NORESERVE | libc::MAP_PRIVATE,
Some(_) => libc::MAP_NORESERVE | libc::MAP_SHARED,
};

let mmap_region =
Expand Down
10 changes: 7 additions & 3 deletions src/vmm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ lazy_static = "1.4.0"
libc = "0.2.117"
serde = { version = "1.0.136", features = ["derive"] }
serde_json = "1.0.78"
userfaultfd = "0.5.0"
userfaultfd = { git = "https://github.com/codesandbox/userfaultfd-rs.git", rev = "b11a187b5743847dda76ed8df5419c3607d21375", features = [
"linux5_7",
] }
versionize = "0.1.6"
versionize_derive = "0.1.4"
vm-allocator = "0.1.0"
derive_more = { version = "0.99.17", default-features = false, features = ["from"] }
derive_more = { version = "0.99.17", default-features = false, features = [
"from",
] }
thiserror = "1.0.32"

arch = { path = "../arch" }
Expand All @@ -28,7 +32,7 @@ logger = { path = "../logger" }
mmds = { path = "../mmds" }
rate_limiter = { path = "../rate_limiter" }
seccompiler = { path = "../seccompiler" }
snapshot = { path = "../snapshot"}
snapshot = { path = "../snapshot" }
utils = { path = "../utils" }
virtio_gen = { path = "../virtio_gen" }
vm-memory = { path = "../vm-memory" }
Expand Down
Loading