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
3 changes: 3 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1606,6 +1606,7 @@ dependencies = [
"pal_async",
"pal_uring",
"scsi_buffers",
"seccompiler",
"storage_tests",
"tempfile",
"test_with_tracing",
Expand Down Expand Up @@ -6668,6 +6669,7 @@ dependencies = [
"pal_async_test",
"pal_event",
"parking_lot",
"seccompiler",
"slab",
"smallbox",
"socket2",
Expand Down Expand Up @@ -10845,6 +10847,7 @@ dependencies = [
"memory_range",
"mesh_protobuf",
"safe_intrinsics",
"test_with_tracing",
"thiserror 2.0.16",
"x86defs",
]
Expand Down
26 changes: 26 additions & 0 deletions Guide/src/dev_guide/tests/vmm.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ that was saved and into a VP of a new partition. Each registers by hand, since
it builds a second partition, and the wake-up, an NMI or a fixed interrupt, also
reaches the restored VP before it first runs, as the vector of a LAPIC timer
that expired during a snapshot's downtime does.
WHP's user-mode APIC cannot hold the Hyper-V idle state, which only the
hypervisor's APIC gives a VP, so `user_mode_apic_refuses_an_idle_vp_hv1`
checks that a restore of an idle activity into a partition with the user-mode
APIC fails, and leaves the activity as it was.
A guest can send itself an SMI, and KVM takes the VP into SMM, where it saves
the state that the SMI interrupts into the RAM at SMBASE through its SMM address
space, and marks the whole VM dead when no RAM is mapped there. The KVM-only
Expand All @@ -105,6 +109,28 @@ does, and checks that it reads as halted and does not run on. KVM clears the
kick only from Linux 6.15 on; before, the case requires the backend to refuse
the halt instead. It writes its code into the fixture's RAM, so it registers by
hand.
The SynIC of each VP of a KVM partition with Hyper-V enlightenments has a route
of its own in the partition's table of 2048 GSIs, after the 24 lines of the
I/O APIC, so such a partition can have at most 2024 VPs. The KVM-only case
`refuses_more_enlightened_vps_than_synic_routes`, in `vp_count.rs`, creates
partitions without building them, so without creating their VPs, and checks
that one of 2025 enlightened VPs is refused with an error, and that one of 2024
enlightened VPs, and one of 2025 VPs without enlightenments, are not. It
creates partitions of its own, so it registers by hand.
WHP also has a backend, `whp_user_mode_apic`, whose APIC the VMM emulates, as
`whp:user_mode_apic` has it, for the cases of `user_mode_apic.rs` alone. The
VMM keeps such a VP's halt and its wait for a SIPI, and WHP, which keeps the
rest of its activity, denies a write of the hypervisor's internal activity
state.
`restores_the_activity_in_each_state` restores an activity in each state that
the VMM keeps, with every other part of the activity set, which must read back
as it was given, in the VP and, from a save of it, in a new partition's VP.
`restores_a_new_vp_into_a_new_partition` restores a save of a new VP into a new
partition, as a snapshot of a VM that has not run does, without Hv1 and with
it. `an_init_makes_an_ap_wait_for_a_sipi` sends the AP of a fixture of two VPs
an INIT, which must make it wait for a SIPI, and then a SIPI, which must start
it at the SIPI's vector, twice. They register by hand, since each builds two
partitions or one of two VPs.

The contract functions operate on the public `virt` interfaces. Each function
has an adjacent `backend_test!` declaration that registers it with Petri for
Expand Down
8 changes: 4 additions & 4 deletions openhcl/underhill_threadpool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,8 +672,8 @@ impl TimerDriver for ThreadpoolDriver {
impl pal_async::io_uring::IoUringDriver for ThreadpoolDriver {
type Submitter = IoInitiator;

fn io_uring_submitter(&self) -> Option<&IoInitiator> {
Some(self.client(None).initiator())
fn io_uring_submitter(&self) -> io::Result<&IoInitiator> {
Ok(self.client(None).initiator())
}
}

Expand Down Expand Up @@ -776,7 +776,7 @@ impl TimerDriver for RetargetableDriver {
impl pal_async::io_uring::IoUringDriver for RetargetableDriver {
type Submitter = IoInitiator;

fn io_uring_submitter(&self) -> Option<&IoInitiator> {
Some(self.inner.current_driver().initiator())
fn io_uring_submitter(&self) -> io::Result<&IoInitiator> {
Ok(self.inner.current_driver().initiator())
}
}
3 changes: 3 additions & 0 deletions support/pal/pal_async/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ tempfile = { workspace = true, optional = true }
io-uring.workspace = true
once_cell.workspace = true

[target.'cfg(target_os = "linux")'.dev-dependencies]
seccompiler.workspace = true

[target.'cfg(unix)'.dependencies]
libc = { workspace = true, features = ["extra_traits"] }

Expand Down
31 changes: 17 additions & 14 deletions support/pal/pal_async/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,24 @@ pub trait Driver: 'static + Send + Sync {
handle: RawHandle,
) -> io::Result<PollImpl<dyn IoOverlapped>>;

/// Returns whether the given opcode is supported by the ring.
/// Returns whether the driver's io-uring supports the given opcode, setting
/// up the io-uring if the driver has not yet.
///
/// Fails with the error that keeps the driver from providing an io-uring:
/// one of kind [`io::ErrorKind::Unsupported`] if the driver does not
/// support io-uring, or the error with which setting up its io-uring
/// failed, such as `ENOMEM` from `io_uring_setup`. A later call tries to
/// set it up again.
#[cfg(target_os = "linux")]
fn io_uring_probe(&self, opcode: u8) -> bool;
fn io_uring_probe(&self, opcode: u8) -> io::Result<bool>;

/// Submits an io-uring SQE for asynchronous execution.
///
/// Returns a future that completes with the IO result. The future **aborts
/// the process** if dropped while the IO is in flight, since there is no
/// way to synchronously cancel an in-flight io-uring operation.
/// way to synchronously cancel an in-flight io-uring operation. The future
/// fails with the error of [`Driver::io_uring_probe`] if the driver cannot
/// provide an io-uring.
///
/// # Safety
///
Expand Down Expand Up @@ -200,11 +209,10 @@ where
Ok(smallbox::smallbox!(self.new_wait(fd, read_size)?))
}

fn io_uring_probe(&self, opcode: u8) -> bool {
fn io_uring_probe(&self, opcode: u8) -> io::Result<bool> {
use crate::io_uring::IoUringSubmit as _;

self.io_uring_submitter()
.is_some_and(|submitter| submitter.probe(opcode))
Ok(self.io_uring_submitter()?.probe(opcode))
}

unsafe fn io_uring_submit(
Expand All @@ -215,12 +223,7 @@ where

Box::pin(async move {
// SAFETY: caller guarantees contract
unsafe {
self.io_uring_submitter()
.ok_or(io::ErrorKind::Unsupported)?
.submit(sqe)
}
.await
unsafe { self.io_uring_submitter()?.submit(sqe) }.await
})
}
}
Expand Down Expand Up @@ -277,7 +280,7 @@ impl Driver for Box<dyn Driver> {
}

#[cfg(target_os = "linux")]
fn io_uring_probe(&self, opcode: u8) -> bool {
fn io_uring_probe(&self, opcode: u8) -> io::Result<bool> {
self.as_ref().io_uring_probe(opcode)
}

Expand Down Expand Up @@ -338,7 +341,7 @@ impl Driver for Arc<dyn Driver> {
}

#[cfg(target_os = "linux")]
fn io_uring_probe(&self, opcode: u8) -> bool {
fn io_uring_probe(&self, opcode: u8) -> io::Result<bool> {
self.as_ref().io_uring_probe(opcode)
}

Expand Down
4 changes: 2 additions & 2 deletions support/pal/pal_async/src/executor_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,9 +611,9 @@ pub mod io_uring_tests {

/// Verify probe returns true for NOP and false for an invalid opcode.
async fn uring_probe(uring: &dyn Driver) {
assert!(uring.io_uring_probe(opcode::Nop::CODE));
assert!(uring.io_uring_probe(opcode::Nop::CODE).unwrap());
// Opcode 255 is not a valid io-uring opcode.
assert!(!uring.io_uring_probe(255));
assert!(!uring.io_uring_probe(255).unwrap());
}

/// Submit multiple NOPs concurrently and verify all complete.
Expand Down
9 changes: 6 additions & 3 deletions support/pal/pal_async/src/io_uring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::io;
///
/// All types that participate in the `Driver` blanket impl on Linux must
/// implement this trait. There is no default—implementors must explicitly
/// return `None` if they do not support io-uring, so that wrapper types
/// return an error if they do not support io-uring, so that wrapper types
/// do not silently drop the capability.
#[cfg(target_os = "linux")]
pub trait IoUringDriver {
Expand All @@ -25,8 +25,11 @@ pub trait IoUringDriver {
/// Use [`NoIoUring`] if the driver does not support io-uring.
type Submitter: IoUringSubmit;

/// Returns an io-uring submitter.
fn io_uring_submitter(&self) -> Option<&Self::Submitter>;
/// Returns an io-uring submitter, or the error that keeps the driver from
/// providing one: an error of kind [`io::ErrorKind::Unsupported`] if the
/// driver does not support io-uring, or the error with which setting up
/// its io-uring failed, such as `ENOMEM` from `io_uring_setup`.
fn io_uring_submitter(&self) -> io::Result<&Self::Submitter>;
}

/// A type that represents the absence of io-uring support.
Expand Down
56 changes: 46 additions & 10 deletions support/pal/pal_async/src/unix/epoll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,16 +465,14 @@ impl TimerDriver for EpollDriver {
impl crate::io_uring::IoUringDriver for EpollDriver {
type Submitter = EpollIoUring;

fn io_uring_submitter(&self) -> Option<&EpollIoUring> {
self.inner
.io_uring
.get_or_try_init(|| {
EpollIoUring::new(
self.inner.epfd.0.as_raw_fd(),
self.inner.wake_event.as_fd().as_raw_fd(),
)
})
.ok()
fn io_uring_submitter(&self) -> io::Result<&EpollIoUring> {
// A failed set-up leaves the cell empty, so the next call tries again.
self.inner.io_uring.get_or_try_init(|| {
EpollIoUring::new(
self.inner.epfd.0.as_raw_fd(),
self.inner.wake_event.as_fd().as_raw_fd(),
)
})
}
}

Expand Down Expand Up @@ -559,4 +557,42 @@ mod tests {
fn uring_works() {
EpollPool::run_with(executor_tests::io_uring_tests::uring_tests)
}

/// A failed set-up of the io-uring reports its error, with the errno of
/// `io_uring_setup`, rather than that the driver lacks io-uring support,
/// and does not stick: the next probe sets the io-uring up.
#[test]
fn uring_setup_failure_is_reported_and_retried() {
use crate::driver::Driver;
use io_uring::opcode;
use seccompiler::BpfProgram;
use seccompiler::SeccompAction;
use seccompiler::SeccompFilter;

let pool = EpollPool::new();
let driver = pool.driver();
// Refuse io_uring_setup with ENOMEM, as a host under memory pressure
// can, on a thread of its own: a seccomp filter binds only the thread
// that installs it, and the threads that this one starts.
let error = std::thread::scope(|scope| {
scope
.spawn(|| {
let filter: BpfProgram = SeccompFilter::new(
[(libc::SYS_io_uring_setup, vec![])].into(),
SeccompAction::Allow,
SeccompAction::Errno(libc::ENOMEM as u32),
std::env::consts::ARCH.try_into().unwrap(),
)
.unwrap()
.try_into()
.unwrap();
seccompiler::apply_filter(&filter).unwrap();
driver.io_uring_probe(opcode::Nop::CODE).unwrap_err()
})
.join()
.unwrap()
});
assert_eq!(error.raw_os_error(), Some(libc::ENOMEM));
assert!(driver.io_uring_probe(opcode::Nop::CODE).unwrap());
}
}
7 changes: 5 additions & 2 deletions support/pal/pal_async/src/unix/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,10 @@ impl crate::process::macos::ProcessWaitDriver for LocalDriver {
impl crate::io_uring::IoUringDriver for LocalDriver {
type Submitter = crate::io_uring::NoIoUring;

fn io_uring_submitter(&self) -> Option<&Self::Submitter> {
None
fn io_uring_submitter(&self) -> io::Result<&Self::Submitter> {
Err(io::Error::new(
io::ErrorKind::Unsupported,
"driver does not support io-uring",
))
}
}
4 changes: 2 additions & 2 deletions support/pal/pal_uring/src/uring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,8 @@ impl pal_async::io_uring::IoUringSubmit for IoInitiator {
impl pal_async::io_uring::IoUringDriver for IoInitiator {
type Submitter = Self;

fn io_uring_submitter(&self) -> Option<&Self> {
Some(self)
fn io_uring_submitter(&self) -> io::Result<&Self> {
Ok(self)
}
}

Expand Down
1 change: 1 addition & 0 deletions vm/devices/storage/disk_blockdevice/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ futures.workspace = true
hvdef.workspace = true
once_cell.workspace = true
pal_uring.workspace = true
seccompiler.workspace = true
storage_tests.workspace = true
tempfile.workspace = true
test_with_tracing.workspace = true
Expand Down
Loading
Loading