Skip to content

fix IovDeque for non 4K pages #5222

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ and this project adheres to

### Fixed

- [#5222](https://github.com/firecracker-microvm/firecracker/pull/5222): Fixed
`IovDeque` wraparound logic which prevented it from working properly on
systems with non 4K pages.

## [1.12.0]

### Added
Expand Down
18 changes: 15 additions & 3 deletions src/vmm/src/devices/virtio/iov_deque.rs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you to add a unit test to verify the behaviour when L < capacity? For example, using L=64 and testing a scenario where it was failing before this change

Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@
// Like that, the elements stored in the buffer are always laid out in contiguous virtual memory,
// so making a slice out of them does not require any copies.
//
// The `L` const generic determines the maximum number of `iovec` elements the queue should hold.
// The `L` const generic determines the maximum number of `iovec` elements the queue should hold
// at any point in time. The actual capacity of the queue may differ and will depend on the host
// page size.
//
// ```Rust
// pub struct iovec {
Expand All @@ -83,6 +85,7 @@
pub iov: *mut libc::iovec,
pub start: u16,
pub len: u16,
pub capacity: u16,
}

// SAFETY: This is `Send`. We hold sole ownership of the underlying buffer.
Expand Down Expand Up @@ -158,6 +161,14 @@
/// Create a new [`IovDeque`] that can hold memory described by a single VirtIO queue.
pub fn new() -> Result<Self, IovDequeError> {
let pages_bytes = Self::pages_bytes();
let capacity = pages_bytes / std::mem::size_of::<iovec>();
let capacity: u16 = capacity.try_into().unwrap();
assert!(
L <= capacity,
"Actual capacity {} is smaller than requested capacity {}",

Check warning on line 168 in src/vmm/src/devices/virtio/iov_deque.rs

View check run for this annotation

Codecov / codecov/patch

src/vmm/src/devices/virtio/iov_deque.rs#L168

Added line #L168 was not covered by tests
capacity,
L
);

let memfd = Self::create_memfd(pages_bytes)?;
let raw_memfd = memfd.as_file().as_raw_fd();
Expand Down Expand Up @@ -201,6 +212,7 @@
iov: buffer.cast(),
start: 0,
len: 0,
capacity,
})
}

Expand Down Expand Up @@ -258,8 +270,8 @@

self.start += nr_iovecs;
self.len -= nr_iovecs;
if self.start >= L {
self.start -= L;
if self.capacity <= self.start {
self.start -= self.capacity;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/vmm/src/devices/virtio/iovec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,7 @@ mod verification {
iov: mem.cast(),
start: kani::any_where(|&start| start < FIRECRACKER_MAX_QUEUE_SIZE),
len: 0,
capacity: FIRECRACKER_MAX_QUEUE_SIZE,
}
}

Expand Down