From 0b15bca9afb882759a62a77446ab6e8050c648ce Mon Sep 17 00:00:00 2001 From: ihciah Date: Sun, 18 Aug 2024 15:07:39 +0000 Subject: [PATCH] improve: remove Sized restriction from memory parameter Accept ?Sized type as memory parameter. Signed-off-by: ihciah --- virtio-blk/src/stdio_executor.rs | 4 +-- virtio-queue/CHANGELOG.md | 3 ++ virtio-queue/src/descriptor_utils.rs | 4 +-- virtio-queue/src/lib.rs | 30 +++++++++++++------- virtio-queue/src/queue.rs | 41 ++++++++++++++++++---------- virtio-queue/src/queue_sync.rs | 16 +++++++---- 6 files changed, 64 insertions(+), 34 deletions(-) diff --git a/virtio-blk/src/stdio_executor.rs b/virtio-blk/src/stdio_executor.rs index 8405271a..fd322bae 100644 --- a/virtio-blk/src/stdio_executor.rs +++ b/virtio-blk/src/stdio_executor.rs @@ -246,7 +246,7 @@ impl StdIoBackend { /// # Arguments /// * `mem` - A reference to the guest memory. /// * `request` - The request to execute. - pub fn process_request( + pub fn process_request( &mut self, mem: &M, request: &Request, @@ -301,7 +301,7 @@ impl StdIoBackend { /// # Arguments /// * `mem` - A reference to the guest memory. /// * `request` - The request to execute. - pub fn execute(&mut self, mem: &M, request: &Request) -> Result { + pub fn execute(&mut self, mem: &M, request: &Request) -> Result { let offset = request .sector() .checked_shl(u32::from(SECTOR_SHIFT)) diff --git a/virtio-queue/CHANGELOG.md b/virtio-queue/CHANGELOG.md index 78fd28f5..da6e0488 100644 --- a/virtio-queue/CHANGELOG.md +++ b/virtio-queue/CHANGELOG.md @@ -5,6 +5,9 @@ ## Added - `Reader`/`Writer` classes to iterate over descriptors +### Changed +- `Queue` accepts memory representation without `Sized`. + # v0.11.0 ## Changed diff --git a/virtio-queue/src/descriptor_utils.rs b/virtio-queue/src/descriptor_utils.rs index 2e9bac18..a8b1f82e 100644 --- a/virtio-queue/src/descriptor_utils.rs +++ b/virtio-queue/src/descriptor_utils.rs @@ -162,7 +162,7 @@ impl<'a, B: BitmapSlice> Reader<'a, B> { M: GuestMemory, <::R as GuestMemoryRegion>::B: WithBitmapSlice<'a, S = B>, T: Deref, - T::Target: GuestMemory + Sized, + T::Target: GuestMemory, { let mut total_len: usize = 0; let buffers = desc_chain @@ -274,7 +274,7 @@ impl<'a, B: BitmapSlice> Writer<'a, B> { M: GuestMemory, <::R as GuestMemoryRegion>::B: WithBitmapSlice<'a, S = B>, T: Deref, - T::Target: GuestMemory + Sized, + T::Target: GuestMemory, { let mut total_len: usize = 0; let buffers = desc_chain diff --git a/virtio-queue/src/lib.rs b/virtio-queue/src/lib.rs index df3d1893..6814eaab 100644 --- a/virtio-queue/src/lib.rs +++ b/virtio-queue/src/lib.rs @@ -146,7 +146,7 @@ pub trait QueueT: for<'a> QueueGuard<'a> { Self: Sized; /// Check whether the queue configuration is valid. - fn is_valid(&self, mem: &M) -> bool; + fn is_valid(&self, mem: &M) -> bool; /// Reset the queue to the initial state. fn reset(&mut self); @@ -198,30 +198,40 @@ pub trait QueueT: for<'a> QueueGuard<'a> { /// # Panics /// /// Panics if order is Release or AcqRel. - fn avail_idx(&self, mem: &M, order: Ordering) -> Result, Error> - where - M: GuestMemory + ?Sized; + fn avail_idx( + &self, + mem: &M, + order: Ordering, + ) -> Result, Error>; /// Read the `idx` field from the used ring. /// /// # Panics /// /// Panics if order is Release or AcqRel. - fn used_idx(&self, mem: &M, order: Ordering) -> Result, Error>; + fn used_idx( + &self, + mem: &M, + order: Ordering, + ) -> Result, Error>; /// Put a used descriptor head into the used ring. - fn add_used(&mut self, mem: &M, head_index: u16, len: u32) - -> Result<(), Error>; + fn add_used( + &mut self, + mem: &M, + head_index: u16, + len: u32, + ) -> Result<(), Error>; /// Enable notification events from the guest driver. /// /// Return true if one or more descriptors can be consumed from the available ring after /// notifications were enabled (and thus it's possible there will be no corresponding /// notification). - fn enable_notification(&mut self, mem: &M) -> Result; + fn enable_notification(&mut self, mem: &M) -> Result; /// Disable notification events from the guest driver. - fn disable_notification(&mut self, mem: &M) -> Result<(), Error>; + fn disable_notification(&mut self, mem: &M) -> Result<(), Error>; /// Check whether a notification to the guest is needed. /// @@ -229,7 +239,7 @@ pub trait QueueT: for<'a> QueueGuard<'a> { /// driver will actually be notified, remember the associated index in the used ring, and /// won't return `true` again until the driver updates `used_event` and/or the notification /// conditions hold once more. - fn needs_notification(&mut self, mem: &M) -> Result; + fn needs_notification(&mut self, mem: &M) -> Result; /// Return the index of the next entry in the available ring. fn next_avail(&self) -> u16; diff --git a/virtio-queue/src/queue.rs b/virtio-queue/src/queue.rs index 4a69b133..4dae4d7e 100644 --- a/virtio-queue/src/queue.rs +++ b/virtio-queue/src/queue.rs @@ -191,7 +191,7 @@ impl Queue { // Helper method that writes `val` to the `avail_event` field of the used ring, using // the provided ordering. - fn set_avail_event( + fn set_avail_event( &self, mem: &M, val: u16, @@ -211,7 +211,7 @@ impl Queue { } // Set the value of the `flags` field of the used ring, applying the specified ordering. - fn set_used_flags( + fn set_used_flags( &mut self, mem: &M, val: u16, @@ -225,7 +225,11 @@ impl Queue { // // Every access in this method uses `Relaxed` ordering because a fence is added by the caller // when appropriate. - fn set_notification(&mut self, mem: &M, enable: bool) -> Result<(), Error> { + fn set_notification( + &mut self, + mem: &M, + enable: bool, + ) -> Result<(), Error> { if enable { if self.event_idx_enabled { // We call `set_avail_event` using the `next_avail` value, instead of reading @@ -254,7 +258,11 @@ impl Queue { // Neither of these interrupt suppression methods are reliable, as they are not synchronized // with the device, but they serve as useful optimizations. So we only ensure access to the // virtq_avail.used_event is atomic, but do not need to synchronize with other memory accesses. - fn used_event(&self, mem: &M, order: Ordering) -> Result, Error> { + fn used_event( + &self, + mem: &M, + order: Ordering, + ) -> Result, Error> { // This can not overflow an u64 since it is working with relatively small numbers compared // to u64::MAX. let used_event_offset = @@ -296,7 +304,7 @@ impl QueueT for Queue { }) } - fn is_valid(&self, mem: &M) -> bool { + fn is_valid(&self, mem: &M) -> bool { let queue_size = self.size as u64; let desc_table = self.desc_table; // The multiplication can not overflow an u64 since we are multiplying an u16 with a @@ -419,10 +427,11 @@ impl QueueT for Queue { self.event_idx_enabled = enabled; } - fn avail_idx(&self, mem: &M, order: Ordering) -> Result, Error> - where - M: GuestMemory + ?Sized, - { + fn avail_idx( + &self, + mem: &M, + order: Ordering, + ) -> Result, Error> { let addr = self .avail_ring .checked_add(2) @@ -434,7 +443,11 @@ impl QueueT for Queue { .map_err(Error::GuestMemory) } - fn used_idx(&self, mem: &M, order: Ordering) -> Result, Error> { + fn used_idx( + &self, + mem: &M, + order: Ordering, + ) -> Result, Error> { let addr = self .used_ring .checked_add(2) @@ -446,7 +459,7 @@ impl QueueT for Queue { .map_err(Error::GuestMemory) } - fn add_used( + fn add_used( &mut self, mem: &M, head_index: u16, @@ -504,7 +517,7 @@ impl QueueT for Queue { // break; // } // } - fn enable_notification(&mut self, mem: &M) -> Result { + fn enable_notification(&mut self, mem: &M) -> Result { self.set_notification(mem, true)?; // Ensures the following read is not reordered before any previous write operation. fence(Ordering::SeqCst); @@ -519,11 +532,11 @@ impl QueueT for Queue { .map(|idx| idx != self.next_avail) } - fn disable_notification(&mut self, mem: &M) -> Result<(), Error> { + fn disable_notification(&mut self, mem: &M) -> Result<(), Error> { self.set_notification(mem, false) } - fn needs_notification(&mut self, mem: &M) -> Result { + fn needs_notification(&mut self, mem: &M) -> Result { let used_idx = self.next_used; // Complete all the writes in add_used() before reading the event. diff --git a/virtio-queue/src/queue_sync.rs b/virtio-queue/src/queue_sync.rs index 6e666bea..3aaa7f6f 100644 --- a/virtio-queue/src/queue_sync.rs +++ b/virtio-queue/src/queue_sync.rs @@ -60,7 +60,7 @@ impl QueueT for QueueSync { }) } - fn is_valid(&self, mem: &M) -> bool { + fn is_valid(&self, mem: &M) -> bool { self.lock_state().is_valid(mem) } @@ -115,11 +115,15 @@ impl QueueT for QueueSync { self.lock_state().avail_idx(mem, order) } - fn used_idx(&self, mem: &M, order: Ordering) -> Result, Error> { + fn used_idx( + &self, + mem: &M, + order: Ordering, + ) -> Result, Error> { self.lock_state().used_idx(mem, order) } - fn add_used( + fn add_used( &mut self, mem: &M, head_index: u16, @@ -128,15 +132,15 @@ impl QueueT for QueueSync { self.lock_state().add_used(mem, head_index, len) } - fn enable_notification(&mut self, mem: &M) -> Result { + fn enable_notification(&mut self, mem: &M) -> Result { self.lock_state().enable_notification(mem) } - fn disable_notification(&mut self, mem: &M) -> Result<(), Error> { + fn disable_notification(&mut self, mem: &M) -> Result<(), Error> { self.lock_state().disable_notification(mem) } - fn needs_notification(&mut self, mem: &M) -> Result { + fn needs_notification(&mut self, mem: &M) -> Result { self.lock_state().needs_notification(mem) }