virtio-devices: vhost_user: Use VhostUserCommon::spawn_worker

Replace manual thread management with the use of
VhostUserCommon::spawn_worker() which is just a thin layer over
VirtioCommon::spawn_worker() but handling VhostUserCommon reset. This
removes some manual thread management and also triggers a reset upon
spawn failure.

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:16:51 +01:00
parent 7538398bc5
commit 815a1f0801
4 changed files with 5 additions and 28 deletions

View File

@@ -26,7 +26,6 @@ use super::super::{ActivateResult, VirtioCommon, VirtioDevice, VirtioDeviceType}
use super::vu_common_ctrl::{VhostUserConfig, VhostUserHandle};
use super::{DEFAULT_VIRTIO_FEATURES, Error, Result};
use crate::seccomp_filters::Thread;
use crate::thread_helper::spawn_virtio_thread;
use crate::vhost_user::{VhostUserCommon, VhostUserState};
use crate::{GuestMemoryMmap, GuestRegionMmap, VIRTIO_F_ACCESS_PLATFORM};
@@ -297,19 +296,15 @@ impl VirtioDevice for Blk {
let paused = self.vu_common.virtio_common.paused.clone();
let paused_sync = self.vu_common.virtio_common.paused_sync.clone();
let mut epoll_threads = Vec::new();
spawn_virtio_thread(
self.vu_common.spawn_worker(
&self.id,
&self.seccomp_action,
Thread::VirtioVhostBlock,
&mut epoll_threads,
&self.exit_evt,
device_status.clone(),
interrupt_cb.clone(),
move || handler.run(&paused, paused_sync.as_ref().unwrap()),
)?;
self.vu_common.virtio_common.epoll_threads = Some(epoll_threads);
Ok(())
}

View File

@@ -21,7 +21,6 @@ use vmm_sys_util::eventfd::EventFd;
use super::vu_common_ctrl::VhostUserHandle;
use super::{DEFAULT_VIRTIO_FEATURES, Error, Result};
use crate::seccomp_filters::Thread;
use crate::thread_helper::spawn_virtio_thread;
use crate::vhost_user::{VhostUserCommon, VhostUserState};
use crate::{
ActivateResult, GuestMemoryMmap, GuestRegionMmap, MmapRegion, VIRTIO_F_ACCESS_PLATFORM,
@@ -271,18 +270,15 @@ impl VirtioDevice for Fs {
let paused = self.vu_common.virtio_common.paused.clone();
let paused_sync = self.vu_common.virtio_common.paused_sync.clone();
let mut epoll_threads = Vec::new();
spawn_virtio_thread(
self.vu_common.spawn_worker(
&self.id,
&self.seccomp_action,
Thread::VirtioVhostFs,
&mut epoll_threads,
&self.exit_evt,
device_status.clone(),
interrupt_cb.clone(),
move || handler.run(&paused, paused_sync.as_ref().unwrap()),
)?;
self.vu_common.virtio_common.epoll_threads = Some(epoll_threads);
event!("virtio-device", "activated", "id", &self.id);
Ok(())

View File

@@ -22,7 +22,6 @@ use vmm_sys_util::eventfd::EventFd;
use super::vu_common_ctrl::VhostUserHandle;
use super::{Error, Result};
use crate::seccomp_filters::Thread;
use crate::thread_helper::spawn_virtio_thread;
use crate::vhost_user::{VhostUserCommon, VhostUserState};
use crate::{
ActivateResult, GuestMemoryMmap, GuestRegionMmap, MmapRegion, VIRTIO_F_ACCESS_PLATFORM,
@@ -333,18 +332,15 @@ impl VirtioDevice for GenericVhostUser {
let paused = self.vu_common.virtio_common.paused.clone();
let paused_sync = self.vu_common.virtio_common.paused_sync.clone();
let mut epoll_threads = Vec::new();
spawn_virtio_thread(
self.vu_common.spawn_worker(
&self.id,
&self.seccomp_action,
Thread::VirtioGenericVhostUser,
&mut epoll_threads,
&self.exit_evt,
device_status.clone(),
interrupt_cb.clone(),
move || handler.run(&paused, paused_sync.as_ref().unwrap()),
)?;
self.vu_common.virtio_common.epoll_threads = Some(epoll_threads);
event!("virtio-device", "activated", "id", &self.id);
Ok(())

View File

@@ -24,7 +24,6 @@ use vm_migration::{Migratable, MigratableError, Pausable, Snapshot, Snapshottabl
use vmm_sys_util::eventfd::EventFd;
use crate::seccomp_filters::Thread;
use crate::thread_helper::spawn_virtio_thread;
use crate::vhost_user::vu_common_ctrl::{VhostUserConfig, VhostUserHandle};
use crate::vhost_user::{DEFAULT_VIRTIO_FEATURES, Error, Result, VhostUserCommon, VhostUserState};
use crate::{
@@ -311,18 +310,15 @@ impl VirtioDevice for Net {
self.vu_common.virtio_common.paused_sync = Some(Arc::new(Barrier::new(3)));
let paused_sync = self.vu_common.virtio_common.paused_sync.clone();
let mut ctrl_threads = Vec::new();
spawn_virtio_thread(
self.vu_common.virtio_common.spawn_worker(
&format!("{}_ctrl", self.id),
&self.seccomp_action,
Thread::VirtioVhostNetCtl,
&mut ctrl_threads,
&self.exit_evt,
device_status.clone(),
interrupt_cb.clone(),
move || ctrl_handler.run_ctrl(&paused, paused_sync.as_ref().unwrap()),
)?;
self.vu_common.virtio_common.epoll_threads = Some(ctrl_threads);
}
let backend_req_handler: Option<FrontendReqHandler<BackendReqHandler>> = None;
@@ -349,16 +345,10 @@ impl VirtioDevice for Net {
let paused = self.vu_common.virtio_common.paused.clone();
let paused_sync = self.vu_common.virtio_common.paused_sync.clone();
let threads = self
.vu_common
.virtio_common
.epoll_threads
.get_or_insert_with(Vec::new);
spawn_virtio_thread(
self.vu_common.spawn_worker(
&self.id,
&self.seccomp_action,
Thread::VirtioVhostNet,
threads,
&self.exit_evt,
device_status.clone(),
interrupt_cb.clone(),