virtio-devices: Use VirtioCommon::spawn_worker()

Replace use of spawn_virtio_thread() helper with the new method on
VirtioCommon to handle thread management as well as spawning. As a
result this cleanly handles reset if it fails to spawn the thread.

Assisted-by: Claude:Opus-4.7
Signed-off-by: Rob Bradford <rbradford@meta.com>
This commit is contained in:
Rob Bradford
2026-05-29 16:06:33 +01:00
parent 81e5e6d9dc
commit b5f1632177
11 changed files with 13 additions and 67 deletions

View File

@@ -39,7 +39,6 @@ use vm_virtio::checked_descriptor::DescriptorChainExt;
use vmm_sys_util::eventfd::EventFd;
use crate::seccomp_filters::Thread;
use crate::thread_helper::spawn_virtio_thread;
use crate::{
ActivateResult, EPOLL_HELPER_EVENT_LAST, EpollHelper, EpollHelperError, EpollHelperHandler,
GuestMemoryMmap, VIRTIO_F_ACCESS_PLATFORM, VIRTIO_F_VERSION_1, VirtioCommon, VirtioDevice,
@@ -688,19 +687,16 @@ impl VirtioDevice for Balloon {
let paused = self.common.paused.clone();
let paused_sync = self.common.paused_sync.clone();
let mut epoll_threads = Vec::new();
spawn_virtio_thread(
self.common.spawn_worker(
&self.id,
&self.seccomp_action,
Thread::VirtioBalloon,
&mut epoll_threads,
&self.exit_evt,
device_status.clone(),
interrupt_cb.clone(),
move || handler.run(&paused, paused_sync.as_ref().unwrap()),
)?;
self.common.epoll_threads = Some(epoll_threads);
event!("virtio-device", "activated", "id", &self.id);
Ok(())

View File

@@ -51,7 +51,6 @@ use super::{
VirtioInterruptType,
};
use crate::seccomp_filters::Thread;
use crate::thread_helper::spawn_virtio_thread;
use crate::{GuestMemoryMmap, VirtioInterrupt};
const SECTOR_SHIFT: u8 = 9;
@@ -405,7 +404,7 @@ impl BlockEpollHandler {
Ok(()) => {}
Err(e @ (Error::QueueIterator(_) | Error::QueueDuplicatedHeadIndex)) => {
// Virtqueue is corrupted or guest driver is misbehaving; exit
// the worker so spawn_virtio_thread marks the device NEEDS_RESET.
// the worker so spawn_worker marks the device NEEDS_RESET.
return Err(EpollHelperError::HandleEvent(anyhow!(
"Failed to process queue (submit): {e}"
)));
@@ -1108,7 +1107,6 @@ impl VirtioDevice for Block {
let writeback = self.is_writeback_enabled(self.config.writeback == 1);
self.set_writeback_mode(writeback);
let mut epoll_threads = Vec::new();
let event_idx = self.common.feature_acked(VIRTIO_RING_F_EVENT_IDX.into());
for i in 0..queues.len() {
@@ -1157,11 +1155,10 @@ impl VirtioDevice for Block {
let paused = self.common.paused.clone();
let paused_sync = self.common.paused_sync.clone();
spawn_virtio_thread(
self.common.spawn_worker(
&format!("{}_q{}", self.id.clone(), i),
&self.seccomp_action,
Thread::VirtioBlock,
&mut epoll_threads,
&self.exit_evt,
self.device_status.clone(),
interrupt_cb.clone(),
@@ -1169,7 +1166,6 @@ impl VirtioDevice for Block {
)?;
}
self.common.epoll_threads = Some(epoll_threads);
event!("virtio-device", "activated", "id", &self.id);
Ok(())

View File

@@ -30,7 +30,6 @@ use super::{
VirtioDeviceType, VirtioInterruptType,
};
use crate::seccomp_filters::Thread;
use crate::thread_helper::spawn_virtio_thread;
use crate::{GuestMemoryMmap, VirtioInterrupt};
const QUEUE_SIZE: u16 = 256;
@@ -764,21 +763,17 @@ impl VirtioDevice for Console {
let paused = self.common.paused.clone();
let paused_sync = self.common.paused_sync.clone();
let mut epoll_threads = Vec::new();
spawn_virtio_thread(
self.common.spawn_worker(
&self.id,
&self.seccomp_action,
Thread::VirtioConsole,
&mut epoll_threads,
&self.exit_evt,
device_status.clone(),
interrupt_cb.clone(),
move || handler.run(&paused, paused_sync.as_ref().unwrap()),
)?;
self.common.epoll_threads = Some(epoll_threads);
event!("virtio-device", "activated", "id", &self.id);
Ok(())
}

View File

@@ -30,7 +30,6 @@ use super::{
Error as DeviceError, VIRTIO_F_VERSION_1, VirtioCommon, VirtioDevice, VirtioDeviceType,
};
use crate::seccomp_filters::Thread;
use crate::thread_helper::spawn_virtio_thread;
use crate::{DmaRemapping, GuestMemoryMmap, VirtioInterrupt, VirtioInterruptType};
/// Queues sizes
@@ -1334,20 +1333,16 @@ impl VirtioDevice for Iommu {
let paused = self.common.paused.clone();
let paused_sync = self.common.paused_sync.clone();
let mut epoll_threads = Vec::new();
spawn_virtio_thread(
self.common.spawn_worker(
&self.id,
&self.seccomp_action,
Thread::VirtioIommu,
&mut epoll_threads,
&self.exit_evt,
device_status.clone(),
interrupt_cb.clone(),
move || handler.run(&paused, paused_sync.as_ref().unwrap()),
)?;
self.common.epoll_threads = Some(epoll_threads);
event!("virtio-device", "activated", "id", &self.id);
Ok(())
}

View File

@@ -43,7 +43,6 @@ use super::{
VirtioDeviceType,
};
use crate::seccomp_filters::Thread;
use crate::thread_helper::spawn_virtio_thread;
use crate::{GuestMemoryMmap, GuestRegionMmap, VirtioInterrupt, VirtioInterruptType};
const QUEUE_SIZE: u16 = 128;
@@ -998,19 +997,16 @@ impl VirtioDevice for Mem {
let paused = self.common.paused.clone();
let paused_sync = self.common.paused_sync.clone();
let mut epoll_threads = Vec::new();
spawn_virtio_thread(
self.common.spawn_worker(
&self.id,
&self.seccomp_action,
Thread::VirtioMem,
&mut epoll_threads,
&self.exit_evt,
device_status.clone(),
interrupt_cb.clone(),
move || handler.run(&paused, paused_sync.as_ref().unwrap()),
)?;
self.common.epoll_threads = Some(epoll_threads);
event!("virtio-device", "activated", "id", &self.id);
Ok(())

View File

@@ -41,7 +41,6 @@ use super::{
VirtioDeviceType, VirtioInterruptType,
};
use crate::seccomp_filters::Thread;
use crate::thread_helper::spawn_virtio_thread;
use crate::{GuestMemoryMmap, VirtioInterrupt};
/// Control queue
@@ -697,8 +696,6 @@ impl VirtioDevice for Net {
let qp_threads = (num_queues - ctrl_threads) / 2;
self.common.paused_sync = Some(Arc::new(Barrier::new(1 + qp_threads + ctrl_threads)));
let mut epoll_threads = Vec::new();
if has_ctrl_queue {
let ctrl_queue_index = num_queues - 1;
let (_, mut ctrl_queue, ctrl_queue_evt) = queues.remove(ctrl_queue_index);
@@ -721,11 +718,10 @@ impl VirtioDevice for Net {
let paused = self.common.paused.clone();
let paused_sync = self.common.paused_sync.clone();
spawn_virtio_thread(
self.common.spawn_worker(
&format!("{}_ctrl", self.id),
&self.seccomp_action,
Thread::VirtioNetCtl,
&mut epoll_threads,
&self.exit_evt,
self.device_status.clone(),
interrupt_cb.clone(),
@@ -798,11 +794,10 @@ impl VirtioDevice for Net {
let paused = self.common.paused.clone();
let paused_sync = self.common.paused_sync.clone();
spawn_virtio_thread(
self.common.spawn_worker(
&format!("{}_qp{}", self.id.clone(), i),
&self.seccomp_action,
Thread::VirtioNet,
&mut epoll_threads,
&self.exit_evt,
self.device_status.clone(),
interrupt_cb.clone(),
@@ -810,8 +805,6 @@ impl VirtioDevice for Net {
)?;
}
self.common.epoll_threads = Some(epoll_threads);
event!("virtio-device", "activated", "id", &self.id);
Ok(())
}

View File

@@ -36,7 +36,6 @@ use super::{
VirtioCommon, VirtioDevice, VirtioDeviceType,
};
use crate::seccomp_filters::Thread;
use crate::thread_helper::spawn_virtio_thread;
use crate::{GuestMemoryMmap, VirtioInterrupt, VirtioInterruptType};
const QUEUE_SIZE: u16 = 256;
@@ -411,21 +410,17 @@ impl VirtioDevice for Pmem {
let paused = self.common.paused.clone();
let paused_sync = self.common.paused_sync.clone();
let mut epoll_threads = Vec::new();
spawn_virtio_thread(
self.common.spawn_worker(
&self.id,
&self.seccomp_action,
Thread::VirtioPmem,
&mut epoll_threads,
&self.exit_evt,
device_status.clone(),
interrupt_cb.clone(),
move || handler.run(&paused, paused_sync.as_ref().unwrap()),
)?;
self.common.epoll_threads = Some(epoll_threads);
event!("virtio-device", "activated", "id", &self.id);
return Ok(());
}

View File

@@ -29,7 +29,6 @@ use super::{
VirtioCommon, VirtioDevice, VirtioDeviceType,
};
use crate::seccomp_filters::Thread;
use crate::thread_helper::spawn_virtio_thread;
use crate::{GuestMemoryMmap, VirtioInterrupt, VirtioInterruptType};
const QUEUE_SIZE: u16 = 256;
@@ -287,20 +286,16 @@ impl VirtioDevice for Rng {
let paused = self.common.paused.clone();
let paused_sync = self.common.paused_sync.clone();
let mut epoll_threads = Vec::new();
spawn_virtio_thread(
self.common.spawn_worker(
&self.id,
&self.seccomp_action,
Thread::VirtioRng,
&mut epoll_threads,
&self.exit_evt,
device_status.clone(),
interrupt_cb.clone(),
move || handler.run(&paused, paused_sync.as_ref().unwrap()),
)?;
self.common.epoll_threads = Some(epoll_threads);
event!("virtio-device", "activated", "id", &self.id);
return Ok(());
}

View File

@@ -28,7 +28,6 @@ use super::{
};
use crate::device::ActivationContext;
use crate::seccomp_filters::Thread;
use crate::thread_helper::spawn_virtio_thread;
use crate::{GuestMemoryMmap, VirtioInterrupt, VirtioInterruptType};
const QUEUE_SIZE: u16 = 256;
@@ -651,20 +650,16 @@ impl VirtioDevice for Rtc {
let paused = self.common.paused.clone();
let paused_sync = self.common.paused_sync.clone();
let mut epoll_threads = Vec::new();
spawn_virtio_thread(
self.common.spawn_worker(
&self.id,
&self.seccomp_action,
Thread::VirtioRtc,
&mut epoll_threads,
&self.exit_evt,
device_status.clone(),
interrupt_cb.clone(),
move || handler.run(&paused, paused_sync.as_ref().unwrap()),
)?;
self.common.epoll_threads = Some(epoll_threads);
event!("virtio-device", "activated", "id", &self.id);
Ok(())
}

View File

@@ -48,7 +48,6 @@ use vmm_sys_util::eventfd::EventFd;
///
use super::{VsockBackend, VsockPacket};
use crate::seccomp_filters::Thread;
use crate::thread_helper::spawn_virtio_thread;
use crate::{
ActivateResult, EPOLL_HELPER_EVENT_LAST, EpollHelper, EpollHelperError, EpollHelperHandler,
Error as DeviceError, GuestMemoryMmap, VIRTIO_F_ACCESS_PLATFORM, VIRTIO_F_IN_ORDER,
@@ -505,21 +504,17 @@ where
let paused = self.common.paused.clone();
let paused_sync = self.common.paused_sync.clone();
let mut epoll_threads = Vec::new();
spawn_virtio_thread(
self.common.spawn_worker(
&self.id,
&self.seccomp_action,
Thread::VirtioVsock,
&mut epoll_threads,
&self.exit_evt,
device_status.clone(),
interrupt_cb.clone(),
move || handler.run(&paused, paused_sync.as_ref().unwrap()),
)?;
self.common.epoll_threads = Some(epoll_threads);
event!("virtio-device", "activated", "id", &self.id);
Ok(())
}

View File

@@ -31,7 +31,6 @@ use super::{
VirtioDeviceType,
};
use crate::seccomp_filters::Thread;
use crate::thread_helper::spawn_virtio_thread;
use crate::{GuestMemoryMmap, VirtioInterrupt, VirtioInterruptType};
const QUEUE_SIZE: u16 = 8;
@@ -367,21 +366,17 @@ impl VirtioDevice for Watchdog {
let paused = self.common.paused.clone();
let paused_sync = self.common.paused_sync.clone();
let mut epoll_threads = Vec::new();
spawn_virtio_thread(
self.common.spawn_worker(
&self.id,
&self.seccomp_action,
Thread::VirtioWatchdog,
&mut epoll_threads,
&self.exit_evt,
device_status.clone(),
interrupt_cb.clone(),
move || handler.run(&paused, paused_sync.as_ref().unwrap()),
)?;
self.common.epoll_threads = Some(epoll_threads);
event!("virtio-device", "activated", "id", &self.id);
Ok(())
}