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
30 changes: 26 additions & 4 deletions block/src/raw_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause

use std::fs::File;
use std::io::Error;
use std::io::{self, Error};
use std::os::unix::fs::FileTypeExt;
use std::os::unix::io::{AsRawFd, RawFd};

use io_uring::{IoUring, opcode, types};
Expand Down Expand Up @@ -68,9 +69,30 @@ impl disk_file::SparseCapable for RawFileDisk {

impl disk_file::Resizable for RawFileDisk {
fn resize(&mut self, size: u64) -> BlockResult<()> {
self.file
.set_len(size)
.map_err(|e| BlockError::new(BlockErrorKind::Io, DiskFileError::ResizeError(e)))
let fd_metadata = self
.file
.metadata()
.map_err(|e| BlockError::new(BlockErrorKind::Io, DiskFileError::ResizeError(e)))?;

if fd_metadata.file_type().is_block_device() {
// Block devices cannot be resized via ftruncate - they are resized
// externally (LVM, losetup -c, etc.). Verify the size matches.
Comment thread
vincent-thomas marked this conversation as resolved.
let (actual_size, _) = query_device_size(&self.file)
.map_err(|e| BlockError::new(BlockErrorKind::Io, DiskFileError::ResizeError(e)))?;
if actual_size != size {
return Err(BlockError::new(
BlockErrorKind::Io,
DiskFileError::ResizeError(io::Error::other(format!(
"Block device size {actual_size} does not match requested size {size}"
))),
));
}
Ok(())
} else {
self.file
.set_len(size)
.map_err(|e| BlockError::new(BlockErrorKind::Io, DiskFileError::ResizeError(e)))
}
}
}

Expand Down
8 changes: 7 additions & 1 deletion virtio-devices/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,13 @@ impl Pausable for VirtioCommon {
"Pausing virtio-{}",
VirtioDeviceType::from(self.device_type)
);
self.paused.store(true, Ordering::SeqCst);

// If already paused, return early to avoid deadlock waiting on barrier
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This situation occurs when the VMM thread holds a device mutex while
calling an operation that triggers pause(), and a vCPU thread
simultaneously needs that same mutex for MMIO access. With slow I/O
backends (like RBD/Ceph), the timing window for this race is larger,
making the deadlock more likely to occur.

Ah, wait. Doesn't this open another race window? Another thread may unpause this too early? Or are we protected by mutexes?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe this is another place where we need a three-way handshake with atomic

RUNNING -> PAUSING -> PAUSED

It might not strictly needed in this case since we don't really do anything after.

// for worker threads that are already parked.
if self.paused.swap(true, Ordering::SeqCst) {
return Ok(());
}

if let Some(pause_evt) = &self.pause_evt {
pause_evt
.write(1)
Expand Down
Loading