Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,7 @@ dependencies = [
"closeable_mutex",
"parking_lot",
"range_map_vec",
"test_with_tracing",
"thiserror 2.0.16",
"tracing",
]
Expand All @@ -649,6 +650,7 @@ dependencies = [
"closeable_mutex",
"parking_lot",
"range_map_vec",
"test_with_tracing",
"tracing",
]

Expand Down
6 changes: 6 additions & 0 deletions Guide/src/reference/devices/virtio/virtio-fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,12 @@ without being followed, and an alias whose ancestor has become a link fails
restore. A handle that the guest held open for writing must still be on a
writable path.

Capture checks those identities first, rather than save a snapshot that
cannot restore: it fails while the guest holds an object whose name the host
has given to another object, as host software does when it renames a new file
over a file that the guest holds open, until the guest kernel releases that
object, as it does once the guest closes the file.

A snapshot of an aggregate records each child's name, canonical host path,
root identity, access mode, and denied, allowed, and writable paths, and the
attachment's identity is the SHA-256 of the children's names and root
Expand Down
3 changes: 3 additions & 0 deletions vm/chipset_arc_mutex_device/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@ parking_lot.workspace = true
thiserror.workspace = true
tracing.workspace = true

[dev-dependencies]
test_with_tracing.workspace = true

[lints]
workspace = true
32 changes: 28 additions & 4 deletions vm/chipset_arc_mutex_device/src/test_chipset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,11 @@ impl ControlMmioIntercept for TestDeviceRange {

fn offset_of(&self, addr: u64) -> Option<u64> {
let base = self.addr?;

(base..(base + self.len))
.contains(&addr)
.then(|| addr - base)
// Compare the offset with the length: the exclusive end of a region
// that ends at the top of the address space does not fit in the
// address type.
let offset = addr.checked_sub(base)?;
(offset < self.len).then_some(offset)
}
}

Expand Down Expand Up @@ -190,6 +191,7 @@ impl MmioInterceptServices for TestChipsetServicesImpl<'_> {
#[cfg(test)]
mod tests {
use super::*;
use test_with_tracing::test;

mod sample_dev {
use super::*;
Expand Down Expand Up @@ -262,4 +264,26 @@ mod tests {

Ok(())
}

#[test]
fn mmio_region_ending_at_the_top_finds_its_offsets() {
let mut mapper = TestMmioRangeMapper {
dev: Weak::<CloseableMutex<sample_dev::SampleDevice>>::new(),
map: Default::default(),
};
let mut region = mapper.new_io_region("top", 0x1000);
let base = u64::MAX - 0xfff;
assert_eq!(region.offset_of(u64::MAX), None);
region.map(base);
assert_eq!(region.addr(), Some(base));
assert_eq!(region.offset_of(base - 1), None);
assert_eq!(region.offset_of(base), Some(0));
assert_eq!(region.offset_of(u64::MAX), Some(0xfff));
region.map(base - 1);
assert_eq!(region.offset_of(base - 1), Some(0));
assert_eq!(region.offset_of(u64::MAX - 1), Some(0xfff));
assert_eq!(region.offset_of(u64::MAX), None);
region.unmap();
assert_eq!(region.offset_of(base), None);
}
}
3 changes: 3 additions & 0 deletions vm/chipset_device_fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@ arbitrary = { workspace = true, features = ["derive"] }
parking_lot.workspace = true
tracing.workspace = true

[dev-dependencies]
test_with_tracing.workspace = true

[lints]
workspace = true
61 changes: 57 additions & 4 deletions vm/chipset_device_fuzz/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,10 +489,11 @@ macro_rules! impl_intercept {

fn offset_of(&self, addr: $usize) -> Option<$usize> {
let base = self.addr?;

(base..(base + self.len))
.contains(&addr)
.then(|| addr - base)
// Compare the offset with the length: the exclusive end of a
// region that ends at the top of the address space does not
// fit in the address type.
let offset = addr.checked_sub(base)?;
(offset < self.len).then_some(offset)
}
}
};
Expand Down Expand Up @@ -628,3 +629,55 @@ impl PollDeviceServices for FuzzChipsetServicesImpl<'_> {
self.took_poll.get()
}
}

#[cfg(test)]
mod tests {
use super::*;
use test_with_tracing::test;

enum NoDevice {}

impl ChipsetDevice for NoDevice {}

fn mapper<U: PartialOrd + Clone>() -> FuzzRegisterIntercept<U> {
FuzzRegisterIntercept {
dev: Weak::<CloseableMutex<NoDevice>>::new(),
map: Default::default(),
}
}

#[test]
fn pio_region_ending_at_the_top_finds_its_offsets() {
let mut region = mapper::<u16>().new_io_region("top", 2);
assert_eq!(region.offset_of(0xffff), None);
region.map(0xfffe);
assert_eq!(region.addr(), Some(0xfffe));
assert_eq!(region.offset_of(0xfffd), None);
assert_eq!(region.offset_of(0xfffe), Some(0));
assert_eq!(region.offset_of(0xffff), Some(1));
region.map(0xfffd);
assert_eq!(region.offset_of(0xfffd), Some(0));
assert_eq!(region.offset_of(0xfffe), Some(1));
assert_eq!(region.offset_of(0xffff), None);
region.unmap();
assert_eq!(region.offset_of(0xfffe), None);
}

#[test]
fn mmio_region_ending_at_the_top_finds_its_offsets() {
let mut region = mapper::<u64>().new_io_region("top", 0x1000);
let base = u64::MAX - 0xfff;
assert_eq!(region.offset_of(u64::MAX), None);
region.map(base);
assert_eq!(region.addr(), Some(base));
assert_eq!(region.offset_of(base - 1), None);
assert_eq!(region.offset_of(base), Some(0));
assert_eq!(region.offset_of(u64::MAX), Some(0xfff));
region.map(base - 1);
assert_eq!(region.offset_of(base - 1), Some(0));
assert_eq!(region.offset_of(u64::MAX - 1), Some(0xfff));
assert_eq!(region.offset_of(u64::MAX), None);
region.unmap();
assert_eq!(region.offset_of(base), None);
}
}
8 changes: 5 additions & 3 deletions vm/devices/virtio/virtiofs/src/inode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,18 +365,20 @@ impl VirtioFsInode {
*path = new_path;
}

/// Increments the lookup count without updating the path, saturating at `u64::MAX`.
/// Increments the lookup count without updating the path, saturating at `u64::MAX`, and
/// returns whether the count changed.
///
/// This is used when returning an existing inode in a FUSE reply (e.g., for hard links)
/// where the kernel will track the reference and later send a forget.
///
/// A count that wrapped to zero would let the next forget evict an inode that the guest
/// still references, so the count stops at `u64::MAX` instead.
pub fn inc_lookup(&self) {
pub fn inc_lookup(&self) -> bool {
self.lookup_count
.update(Ordering::AcqRel, Ordering::Acquire, |count| {
count.saturating_add(1)
});
})
!= u64::MAX
}

/// Decrements the lookup count, and returns the new count.
Expand Down
Loading
Loading