cros_async: Skip zero length reads and writes

Sometimes the guest asks to read or write zero bytes. Skip sending that
to the kernel as it is a no-op.

Change-Id: I67ac3cd75551b994517eedb02830b3ea9451fb15
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2275722
Commit-Queue: Dylan Reid <dgreid@chromium.org>
Tested-by: Dylan Reid <dgreid@chromium.org>
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
This commit is contained in:
Dylan Reid 2020-06-25 17:54:28 -07:00 committed by Commit Bot
parent 9affaf1ae0
commit 68ef7cfc9a
2 changed files with 14 additions and 0 deletions

View file

@ -38,6 +38,13 @@ impl<R: IoSource + ?Sized + Unpin> Future for ReadMem<'_, '_, R> {
type Output = Result<u32>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
// Special case reading zero bytes, no need to bother the kernel with an empty operation.
if let UringFutState::Init((_, _, regions)) = &self.state {
if regions.iter().all(|region| region.len == 0) {
return Poll::Ready(Ok(0));
}
}
let state = std::mem::replace(&mut self.state, UringFutState::Processing);
let (new_state, ret) = match state.advance(
|(file_offset, mem, mem_offsets)| {

View file

@ -38,6 +38,13 @@ impl<R: IoSource + ?Sized + Unpin> Future for WriteMem<'_, '_, R> {
type Output = Result<u32>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
// Special case writing zero bytes, no need to bother the kernel with an empty operation.
if let UringFutState::Init((_, _, regions)) = &self.state {
if regions.iter().all(|region| region.len == 0) {
return Poll::Ready(Ok(0));
}
}
let state = std::mem::replace(&mut self.state, UringFutState::Processing);
let (new_state, ret) = match state.advance(
|(file_offset, mem, mem_offsets)| {