From cd5cd38149419c99a31564631ce6cb6e149a2a7a Mon Sep 17 00:00:00 2001 From: Pedro Henrique Penna Date: Mon, 9 Nov 2026 22:58:55 -0800 Subject: [PATCH] virtio: test PCI re-save before start and missing queue progress A restore validates the queues and the device-private state that it restores before it applies them, and an enabled queue of a DRIVER_OK device must carry its progress. It then hands the private state to the stopped device, and a save before the first start returns the restored state: queues that never started keep their restored progress, and the device its restored private state. The MMIO transport had tests of both for an active device. The PCI transport, which restores through the same core, had neither, although microsoft/nvx#1232 requires the restore contract on both transports. Add the PCI counterparts. A restore whose active queue lacks its progress fails before the device sees any state. A restored active device that is stopped and saved before its first start returns the restored queue progress and private state, and starts no queue. The module documentation of the virtio-net saved-state tests still said that an inactive transport defers the device-private state until the device is activated, as the transports did before they restored it into the stopped device. Its test checks that the restore gives the device its state before any queue starts, so say that. Tests: the new tests pass, and fail as their MMIO counterparts do when the restore no longer requires the progress of an active queue, or when a stop before the first start drops the restored progress. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../virtio/virtio/src/tests/saved_state.rs | 46 +++++++++++++++++++ .../virtio_net/src/tests/saved_state.rs | 2 +- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/vm/devices/virtio/virtio/src/tests/saved_state.rs b/vm/devices/virtio/virtio/src/tests/saved_state.rs index 0767a77e0e1..e1b10fde963 100644 --- a/vm/devices/virtio/virtio/src/tests/saved_state.rs +++ b/vm/devices/virtio/virtio/src/tests/saved_state.rs @@ -358,6 +358,28 @@ async fn mmio_restore_rejects_missing_active_queue_progress(driver: DefaultDrive assert!(destination.restore(saved).await.is_err()); } +#[async_test] +async fn pci_restore_rejects_missing_active_queue_progress(driver: DefaultDriver) { + use vmcore::save_restore::AsyncSaveRestore; + + let test_mem = VirtioTestMemoryAccess::new(); + let mut saved = save_active_private_pci(&driver, &test_mem).await; + assert!(saved.queues[0].common.enable); + assert!(saved.queues[0].common.queue_state.take().is_some()); + let (device, probe) = PrivateStateTestDevice::new(0, 1, false); + let mut destination = + VirtioPciTestDevice::new_with_device(&driver, &test_mem, Box::new(device)); + + let error = destination.pci_device.restore(saved).await.unwrap_err(); + assert!( + error_chain(&error).contains("missing progress state"), + "{error:?}" + ); + // Validation rejects the state before the device sees it. + assert_eq!(probe.restore_count.load(Ordering::Relaxed), 0); + assert_eq!(probe.start_count.load(Ordering::Relaxed), 0); +} + #[async_test] async fn mmio_private_state_round_trip(driver: DefaultDriver) { use crate::spec::mmio::VirtioMmioRegister; @@ -564,6 +586,30 @@ async fn mmio_resave_before_first_start_keeps_restored_state(driver: DefaultDriv assert_eq!(saved_value(resaved.device_state), 42); } +#[async_test] +async fn pci_resave_before_first_start_keeps_restored_state(driver: DefaultDriver) { + use vmcore::save_restore::AsyncSaveRestore; + + let test_mem = VirtioTestMemoryAccess::new(); + let saved = save_active_private_pci(&driver, &test_mem).await; + let (device, probe) = PrivateStateTestDevice::new(0, 1, false); + let mut destination = + VirtioPciTestDevice::new_with_device(&driver, &test_mem, Box::new(device)); + + destination.pci_device.restore(saved).await.unwrap(); + destination.pci_device.stop().await; + let resaved = destination.pci_device.save().await.unwrap(); + assert_eq!(probe.start_count.load(Ordering::Relaxed), 0); + assert_eq!( + resaved.queues[0].common.queue_state, + Some(QueueState { + avail_index: 3, + used_index: 1, + }) + ); + assert_eq!(saved_value(resaved.device_state), 42); +} + #[async_test] async fn mmio_active_zero_queue_device_restores_private_state(driver: DefaultDriver) { use vmcore::save_restore::AsyncSaveRestore; diff --git a/vm/devices/virtio/virtio_net/src/tests/saved_state.rs b/vm/devices/virtio/virtio_net/src/tests/saved_state.rs index 1308f42a285..2c01120b103 100644 --- a/vm/devices/virtio/virtio_net/src/tests/saved_state.rs +++ b/vm/devices/virtio/virtio_net/src/tests/saved_state.rs @@ -3,7 +3,7 @@ //! Saved-state tests: stopping the queues rewinds unused RX descriptors, //! partial queue lifecycles are saved and validated, and an inactive transport -//! defers the device-private state until the device is activated. +//! restores the device-private state before any queue starts. use super::QUEUE_SIZE; use super::RX_AVAIL_ADDR;