Skip to content

[RFC] Make mmap_unix and mmap_xen compatible #317

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

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
d6ce55f
Add naive default impl for `GuestMemory::find_region()`
roypat Feb 5, 2025
60e582d
introduce `region` module
roypat Mar 14, 2025
2c8f95d
Generalize GuestMemoryMmap to arbitrary GuestMemoryRegions
roypat Feb 5, 2025
69ffd45
refactor: use `matches!` instead of to_string() for tests
roypat Feb 5, 2025
f91e18a
generically impl Bytes for all R: GuestMemoryRegion
roypat Mar 14, 2025
cff6fe6
test: move GuestRegionCollection specific tests to region.rs
roypat Mar 14, 2025
5e4e1d4
refactor(test): Use mock regions in atomic.rs tests
roypat Mar 14, 2025
ba461bd
refactor(mmap): Add Error type for check_file_offset
roypat Mar 14, 2025
aa37f15
refactor(xen): allow extracting guest_base from `MmapXen`
roypat Mar 14, 2025
43f2e5c
xen: implement GuestMemoryRegion for mmap_xen::MmapRegion
roypat Mar 14, 2025
18db9b5
xen: Introduce GuestMemoryXen and use it in doc tests
roypat Mar 14, 2025
5813cd1
test: run mmap.rs tests with all available backends
roypat Mar 14, 2025
c6f5a63
test: remove duplicated test
roypat Mar 14, 2025
4ab3783
refactor(test): use new text fixtures in guest_memory.rs
roypat Mar 14, 2025
87ce16c
refactor: scatter mmap.rs contents to unix/windows modules
roypat Mar 14, 2025
e7b1f7a
refactor: Add GuestMemoryMmapError
roypat Mar 14, 2025
f6d9b2d
Make mmap_unix.rs and mmap_xen.rs not mutually exclusive
roypat Mar 14, 2025
7621660
ci: drop test runners with subset of features
roypat Mar 14, 2025
e83ab67
xen: fix clippy warning
roypat Mar 17, 2025
7d4ecd2
ci: add xen to features for coverage
roypat Mar 17, 2025
87e2daa
chore: adjust coverage number
roypat Mar 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 0 additions & 52 deletions .buildkite/custom-tests.json
Original file line number Diff line number Diff line change
@@ -1,61 +1,9 @@
{
"tests": [
{
"test_name": "build-gnu-mmap",
"command": "cargo build --release --features=xen",
"platform": ["x86_64", "aarch64"]
},
{
"test_name": "build-gnu-mmap-no-xen",
"command": "cargo build --release --features=backend-mmap",
"platform": ["x86_64", "aarch64"]
},
{
"test_name": "build-musl-mmap",
"command": "cargo build --release --features=xen --target {target_platform}-unknown-linux-musl",
"platform": ["x86_64", "aarch64"]
},
{
"test_name": "build-musl-mmap-no-xen",
"command": "cargo build --release --features=backend-mmap --target {target_platform}-unknown-linux-musl",
"platform": ["x86_64", "aarch64"]
},
{
"test_name": "miri",
"command": "RUST_BACKTRACE=1 MIRIFLAGS='-Zmiri-disable-isolation -Zmiri-backtrace=full' cargo +nightly miri test --features backend-mmap",
"platform": ["x86_64", "aarch64"]
},
{
"test_name": "unittests-gnu-no-xen",
"command": "cargo test --features 'backend-bitmap backend-mmap backend-atomic' --workspace",
"platform": [
"x86_64",
"aarch64"
]
},
{
"test_name": "unittests-musl-no-xen",
"command": "cargo test --features 'backend-bitmap backend-mmap backend-atomic' --workspace --target {target_platform}-unknown-linux-musl",
"platform": [
"x86_64",
"aarch64"
]
},
{
"test_name": "clippy-no-xen",
"command": "cargo clippy --workspace --bins --examples --benches --features 'backend-bitmap backend-mmap backend-atomic' --all-targets -- -D warnings -D clippy::undocumented_unsafe_blocks",
"platform": [
"x86_64",
"aarch64"
]
},
{
"test_name": "check-warnings-no-xen",
"command": "RUSTFLAGS=\"-D warnings\" cargo check --all-targets --features 'backend-bitmap backend-mmap backend-atomic' --workspace",
"platform": [
"x86_64",
"aarch64"
]
}
]
}
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@
### Added

- \[[#311](https://github.com/rust-vmm/vm-memory/pull/311)\] Allow compiling without the ReadVolatile and WriteVolatile implementations
- \[[#312](https://github.com/rust-vmm/vm-memory/pull/312)\] `GuestRegionContainer`, a generic container of `GuestMemoryRegion`s, generalizing `GuestMemoryMmap` (which
is now a type alias for `GuestRegionContainer<GuestRegionMmap>`)

### Changed

- \[[#307](https://github.com/rust-vmm/vm-memory/pull/304)\] Move `read_volatile_from`, `read_exact_volatile_from`,
`write_volatile_to` and `write_all_volatile_to` functions from the `GuestMemory` trait to the `Bytes` trait.
- \[[#312](https://github.com/rust-vmm/vm-memory/pull/312)\]: Give `GuestMemory::find_region` a default implementation,
based on linear search.
- \[[#312](https://github.com/rust-vmm/vm-memory/pull/312)\]: Implement `Bytes<MemoryRegionAddress>` generically
for all `R: GuestMemoryRegion`.
- \[[#317](https://github.com/rust-vmm/vm-memory/pull/317)\]: Make `xen` feature additive, meaning enabling it
no longer disables the `mmap_unix` module at compile time. For this, various Xen-related structs had to be
renamed due to naming conflicts.

### Removed

Expand Down
4 changes: 2 additions & 2 deletions coverage_config_x86_64.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"coverage_score": 92.92,
"coverage_score": 90.51,
"exclude_path": "mmap_windows.rs",
"crate_features": "backend-mmap,backend-atomic,backend-bitmap"
"crate_features": "backend-mmap,backend-atomic,backend-bitmap,xen"
}
64 changes: 34 additions & 30 deletions src/atomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,12 @@ impl<M: GuestMemory> GuestMemoryExclusiveGuard<'_, M> {
}

#[cfg(test)]
#[cfg(feature = "backend-mmap")]
mod tests {
use super::*;
use crate::{GuestAddress, GuestMemory, GuestMemoryRegion, GuestUsize, MmapRegion};
use crate::region::tests::{new_guest_memory_collection_from_regions, Collection, MockRegion};
use crate::{GuestAddress, GuestMemory, GuestMemoryRegion, GuestUsize};

type GuestMemoryMmap = crate::GuestMemoryMmap<()>;
type GuestRegionMmap = crate::GuestRegionMmap<()>;
type GuestMemoryMmapAtomic = GuestMemoryAtomic<GuestMemoryMmap>;
type GuestMemoryMmapAtomic = GuestMemoryAtomic<Collection>;

#[test]
fn test_atomic_memory() {
Expand All @@ -157,7 +155,7 @@ mod tests {
(GuestAddress(0x1000), region_size),
];
let mut iterated_regions = Vec::new();
let gmm = GuestMemoryMmap::from_ranges(&regions).unwrap();
let gmm = new_guest_memory_collection_from_regions(&regions).unwrap();
let gm = GuestMemoryMmapAtomic::new(gmm);
let mem = gm.memory();

Expand All @@ -166,7 +164,7 @@ mod tests {
}

for region in mem.iter() {
iterated_regions.push((region.start_addr(), region.len() as usize));
iterated_regions.push((region.start_addr(), region.len()));
}
assert_eq!(regions, iterated_regions);
assert_eq!(mem.num_regions(), 2);
Expand Down Expand Up @@ -207,7 +205,7 @@ mod tests {
(GuestAddress(0x0), region_size),
(GuestAddress(0x1000), region_size),
];
let gmm = GuestMemoryMmap::from_ranges(&regions).unwrap();
let gmm = new_guest_memory_collection_from_regions(&regions).unwrap();
let gm = GuestMemoryMmapAtomic::new(gmm);
let mem = {
let guard1 = gm.memory();
Expand All @@ -219,38 +217,44 @@ mod tests {
#[test]
fn test_atomic_hotplug() {
let region_size = 0x1000;
let regions = vec![
let regions = [
(GuestAddress(0x0), region_size),
(GuestAddress(0x10_0000), region_size),
];
let mut gmm = Arc::new(GuestMemoryMmap::from_ranges(&regions).unwrap());
let mut gmm = Arc::new(new_guest_memory_collection_from_regions(&regions).unwrap());
let gm: GuestMemoryAtomic<_> = gmm.clone().into();
let mem_orig = gm.memory();
assert_eq!(mem_orig.num_regions(), 2);

{
let guard = gm.lock().unwrap();
let new_gmm = Arc::make_mut(&mut gmm);
let mmap = Arc::new(
GuestRegionMmap::new(MmapRegion::new(0x1000).unwrap(), GuestAddress(0x8000))
.unwrap(),
);
let new_gmm = new_gmm.insert_region(mmap).unwrap();
let mmap = Arc::new(
GuestRegionMmap::new(MmapRegion::new(0x1000).unwrap(), GuestAddress(0x4000))
.unwrap(),
);
let new_gmm = new_gmm.insert_region(mmap).unwrap();
let mmap = Arc::new(
GuestRegionMmap::new(MmapRegion::new(0x1000).unwrap(), GuestAddress(0xc000))
.unwrap(),
);
let new_gmm = new_gmm.insert_region(mmap).unwrap();
let mmap = Arc::new(
GuestRegionMmap::new(MmapRegion::new(0x1000).unwrap(), GuestAddress(0xc000))
.unwrap(),
);
new_gmm.insert_region(mmap).unwrap_err();
let new_gmm = new_gmm
.insert_region(Arc::new(MockRegion {
start: GuestAddress(0x8000),
len: 0x1000,
}))
.unwrap();
let new_gmm = new_gmm
.insert_region(Arc::new(MockRegion {
start: GuestAddress(0x4000),
len: 0x1000,
}))
.unwrap();
let new_gmm = new_gmm
.insert_region(Arc::new(MockRegion {
start: GuestAddress(0xc000),
len: 0x1000,
}))
.unwrap();

new_gmm
.insert_region(Arc::new(MockRegion {
start: GuestAddress(0x8000),
len: 0x1000,
}))
.unwrap_err();

guard.replace(new_gmm);
}

Expand Down
4 changes: 2 additions & 2 deletions src/bitmap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ pub(crate) mod tests {
// them to test the mmap-based backend implementations for now. Going forward, the generic
// test functions defined here can be placed in a separate module (i.e. `test_utilities`)
// which is gated by a feature and can be used for testing purposes by other crates as well.
#[cfg(feature = "backend-mmap")]
#[cfg(all(feature = "backend-mmap", unix))]
fn test_guest_memory_region<R: GuestMemoryRegion>(region: &R) {
let dirty_addr = MemoryRegionAddress(0x0);
let val = 123u64;
Expand Down Expand Up @@ -308,7 +308,7 @@ pub(crate) mod tests {
);
}

#[cfg(feature = "backend-mmap")]
#[cfg(all(feature = "backend-mmap", unix))]
// Assumptions about M generated by f ...
pub fn test_guest_memory_and_region<M, F>(f: F)
where
Expand Down
2 changes: 1 addition & 1 deletion src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ pub trait Bytes<A> {
/// * Read bytes from /dev/urandom (uses the `backend-mmap` feature)
///
/// ```
/// # #[cfg(all(feature = "backend-mmap", feature = "rawfd"))]
/// # #[cfg(all(feature = "backend-mmap", feature = "rawfd", unix))]
/// # {
/// # use vm_memory::{Address, GuestMemory, Bytes, GuestAddress, GuestMemoryMmap};
/// # use std::fs::File;
Expand Down
Loading