Skip to content

Commit bc5671b

Browse files
committed
refactor: de-generify async_io engine
These generic type parameters were leaking out from the io_uring module. Having a generic ring structure in the io_uring module: makes sense. Having a generic file engine: probably makes less sense (what are we ever gonna process in there except block io requests?). Just replace all instances of `T` in the virtio module with `PendingRequest` - that's what it all ended up getting monomorphized to anyway. Signed-off-by: Patrick Roy <[email protected]>
1 parent 5eaa6e0 commit bc5671b

File tree

4 files changed

+136
-117
lines changed

4 files changed

+136
-117
lines changed

src/vmm/src/devices/virtio/block/virtio/device.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub enum FileEngineType {
5252
#[derive(Debug)]
5353
pub struct DiskProperties {
5454
pub file_path: String,
55-
pub file_engine: FileEngine<PendingRequest>,
55+
pub file_engine: FileEngine,
5656
pub nsectors: u64,
5757
pub image_id: [u8; VIRTIO_BLK_ID_BYTES as usize],
5858
}

src/vmm/src/devices/virtio/block/virtio/io/async_io.rs

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ use std::os::unix::io::AsRawFd;
99
use vm_memory::GuestMemoryError;
1010
use vmm_sys_util::eventfd::EventFd;
1111

12-
use crate::devices::virtio::block::virtio::IO_URING_NUM_ENTRIES;
13-
use crate::devices::virtio::block::virtio::io::UserDataError;
12+
use crate::devices::virtio::block::virtio::io::RequestError;
13+
use crate::devices::virtio::block::virtio::{IO_URING_NUM_ENTRIES, PendingRequest};
1414
use crate::io_uring::operation::{Cqe, OpCode, Operation};
1515
use crate::io_uring::restriction::Restriction;
16-
use crate::io_uring::{self, IoUring, IoUringError};
16+
use crate::io_uring::{IoUring, IoUringError};
1717
use crate::logger::log_dev_preview_warning;
1818
use crate::vstate::memory::{GuestAddress, GuestMemory, GuestMemoryExtension, GuestMemoryMmap};
1919

@@ -34,47 +34,44 @@ pub enum AsyncIoError {
3434
}
3535

3636
#[derive(Debug)]
37-
pub struct AsyncFileEngine<T> {
37+
pub struct AsyncFileEngine {
3838
file: File,
39-
ring: IoUring<WrappedUserData<T>>,
39+
ring: IoUring<WrappedRequest>,
4040
completion_evt: EventFd,
4141
}
4242

4343
#[derive(Debug)]
44-
pub struct WrappedUserData<T> {
44+
pub struct WrappedRequest {
4545
addr: Option<GuestAddress>,
46-
user_data: T,
46+
req: PendingRequest,
4747
}
4848

49-
impl<T: Debug> WrappedUserData<T> {
50-
fn new(user_data: T) -> Self {
51-
WrappedUserData {
52-
addr: None,
53-
user_data,
54-
}
49+
impl WrappedRequest {
50+
fn new(req: PendingRequest) -> Self {
51+
WrappedRequest { addr: None, req }
5552
}
5653

57-
fn new_with_dirty_tracking(addr: GuestAddress, user_data: T) -> Self {
58-
WrappedUserData {
54+
fn new_with_dirty_tracking(addr: GuestAddress, req: PendingRequest) -> Self {
55+
WrappedRequest {
5956
addr: Some(addr),
60-
user_data,
57+
req,
6158
}
6259
}
6360

64-
fn mark_dirty_mem_and_unwrap(self, mem: &GuestMemoryMmap, count: u32) -> T {
61+
fn mark_dirty_mem_and_unwrap(self, mem: &GuestMemoryMmap, count: u32) -> PendingRequest {
6562
if let Some(addr) = self.addr {
6663
mem.mark_dirty(addr, count as usize)
6764
}
6865

69-
self.user_data
66+
self.req
7067
}
7168
}
7269

73-
impl<T: Debug> AsyncFileEngine<T> {
70+
impl AsyncFileEngine {
7471
fn new_ring(
7572
file: &File,
7673
completion_fd: RawFd,
77-
) -> Result<IoUring<WrappedUserData<T>>, io_uring::IoUringError> {
74+
) -> Result<IoUring<WrappedRequest>, IoUringError> {
7875
IoUring::new(
7976
u32::from(IO_URING_NUM_ENTRIES),
8077
vec![file],
@@ -90,7 +87,7 @@ impl<T: Debug> AsyncFileEngine<T> {
9087
)
9188
}
9289

93-
pub fn from_file(file: File) -> Result<AsyncFileEngine<T>, AsyncIoError> {
90+
pub fn from_file(file: File) -> Result<AsyncFileEngine, AsyncIoError> {
9491
log_dev_preview_warning("Async file IO", Option::None);
9592

9693
let completion_evt = EventFd::new(libc::EFD_NONBLOCK).map_err(AsyncIoError::EventFd)?;
@@ -128,19 +125,19 @@ impl<T: Debug> AsyncFileEngine<T> {
128125
mem: &GuestMemoryMmap,
129126
addr: GuestAddress,
130127
count: u32,
131-
user_data: T,
132-
) -> Result<(), UserDataError<T, AsyncIoError>> {
128+
req: PendingRequest,
129+
) -> Result<(), RequestError<AsyncIoError>> {
133130
let buf = match mem.get_slice(addr, count as usize) {
134131
Ok(slice) => slice.ptr_guard_mut().as_ptr(),
135132
Err(err) => {
136-
return Err(UserDataError {
137-
user_data,
133+
return Err(RequestError {
134+
req,
138135
error: AsyncIoError::GuestMemory(err),
139136
});
140137
}
141138
};
142139

143-
let wrapped_user_data = WrappedUserData::new_with_dirty_tracking(addr, user_data);
140+
let wrapped_user_data = WrappedRequest::new_with_dirty_tracking(addr, req);
144141

145142
self.ring
146143
.push(Operation::read(
@@ -150,8 +147,8 @@ impl<T: Debug> AsyncFileEngine<T> {
150147
offset,
151148
wrapped_user_data,
152149
))
153-
.map_err(|(io_uring_error, data)| UserDataError {
154-
user_data: data.user_data,
150+
.map_err(|(io_uring_error, data)| RequestError {
151+
req: data.req,
155152
error: AsyncIoError::IoUring(io_uring_error),
156153
})
157154
}
@@ -162,19 +159,19 @@ impl<T: Debug> AsyncFileEngine<T> {
162159
mem: &GuestMemoryMmap,
163160
addr: GuestAddress,
164161
count: u32,
165-
user_data: T,
166-
) -> Result<(), UserDataError<T, AsyncIoError>> {
162+
req: PendingRequest,
163+
) -> Result<(), RequestError<AsyncIoError>> {
167164
let buf = match mem.get_slice(addr, count as usize) {
168165
Ok(slice) => slice.ptr_guard_mut().as_ptr(),
169166
Err(err) => {
170-
return Err(UserDataError {
171-
user_data,
167+
return Err(RequestError {
168+
req,
172169
error: AsyncIoError::GuestMemory(err),
173170
});
174171
}
175172
};
176173

177-
let wrapped_user_data = WrappedUserData::new(user_data);
174+
let wrapped_user_data = WrappedRequest::new(req);
178175

179176
self.ring
180177
.push(Operation::write(
@@ -184,19 +181,19 @@ impl<T: Debug> AsyncFileEngine<T> {
184181
offset,
185182
wrapped_user_data,
186183
))
187-
.map_err(|(io_uring_error, data)| UserDataError {
188-
user_data: data.user_data,
184+
.map_err(|(io_uring_error, data)| RequestError {
185+
req: data.req,
189186
error: AsyncIoError::IoUring(io_uring_error),
190187
})
191188
}
192189

193-
pub fn push_flush(&mut self, user_data: T) -> Result<(), UserDataError<T, AsyncIoError>> {
194-
let wrapped_user_data = WrappedUserData::new(user_data);
190+
pub fn push_flush(&mut self, req: PendingRequest) -> Result<(), RequestError<AsyncIoError>> {
191+
let wrapped_user_data = WrappedRequest::new(req);
195192

196193
self.ring
197194
.push(Operation::fsync(0, wrapped_user_data))
198-
.map_err(|(io_uring_error, data)| UserDataError {
199-
user_data: data.user_data,
195+
.map_err(|(io_uring_error, data)| RequestError {
196+
req: data.req,
200197
error: AsyncIoError::IoUring(io_uring_error),
201198
})
202199
}
@@ -233,11 +230,14 @@ impl<T: Debug> AsyncFileEngine<T> {
233230
Ok(())
234231
}
235232

236-
fn do_pop(&mut self) -> Result<Option<Cqe<WrappedUserData<T>>>, AsyncIoError> {
233+
fn do_pop(&mut self) -> Result<Option<Cqe<WrappedRequest>>, AsyncIoError> {
237234
self.ring.pop().map_err(AsyncIoError::IoUring)
238235
}
239236

240-
pub fn pop(&mut self, mem: &GuestMemoryMmap) -> Result<Option<Cqe<T>>, AsyncIoError> {
237+
pub fn pop(
238+
&mut self,
239+
mem: &GuestMemoryMmap,
240+
) -> Result<Option<Cqe<PendingRequest>>, AsyncIoError> {
241241
let cqe = self.do_pop()?.map(|cqe| {
242242
let count = cqe.count();
243243
cqe.map_user_data(|wrapped_user_data| {

0 commit comments

Comments
 (0)