pci: Support injecting interrupts from externally-provided irqfds

The virtio vhost-user device backend prefers to use externally-provided
eventfds as irqfds.  This allows the frontend VM to notify the backend
VM directly, without the need for a userspace proxy process.  Since the
frontend can provide irqfds at any time, the backend needs to register
and unregister irqfds dynamically.

This is tricky because the functions that access the irqfd table all
take `&self`, not `&mut self`.  The obvious solution to this problem is
to wrap the table in a mutex.  Most of these functions are not called on
hot paths, but `.notifier()` is called whenever Cloud Hypervisor needs
to inject an interrupt into a guest.  Most devices don't need to
register irqfds at runtime, and for them, slowing down interrupt
injection would be wasteful.

Instead, require devices to opt-in to irqfd registration.  The irqfd
table now comes in two forms: one that contains a mutex and one that
does not.  The one containing a mutex can be mutated freely, while
attempting to mutate the one that does not will panic.

Right now, no code registeres irqfds at runtime, but this will change in
subsequent commits.

Signed-off-by: Demi Marie Obenour <demiobenour@gmail.com>
This commit is contained in:
Demi Marie Obenour
2026-02-11 20:08:45 -05:00
committed by Sebastien Boeuf
parent 9f62c33d00
commit d609410b8b
8 changed files with 194 additions and 33 deletions

View File

@@ -4201,8 +4201,8 @@ impl DeviceManager {
return Err(DeviceManagerError::MissingNode);
}
// Allows support for one MSI-X vector per queue. It also adds 1
// as we need to take into account the dedicated vector to notify
// Allows support for one MSI-X vector per interrupt needed by the device.
// It also adds 1 as we need to take into account the dedicated vector to notify
// about a virtio config change.
let msix_num = (virtio_device.lock().unwrap().queue_max_sizes().len() + 1) as u16;

View File

@@ -86,6 +86,9 @@ impl InterruptRoute {
)
}
// This is currently not used, but the upcoming vhost-guest feature
// will use it. Use #[allow(dead_code)] to suppress a compiler
// warning.
#[allow(dead_code)]
pub fn set_notifier(
&mut self,
@@ -96,7 +99,7 @@ impl InterruptRoute {
if self.registered {
if let Some(ref irq_fd) = self.irq_fd {
vm.register_irqfd(irq_fd, self.gsi)
.map_err(|e| io::Error::other(format!("Failed registering irq_fd: {e}")))?
.map_err(|e| io::Error::other(format!("Failed registering irq_fd: {e}")))?;
}
// If the irqfd cannot be unregistered, what to do? Spin?
// Returning an error isn't helpful as the new irqfd is already registered.
@@ -235,6 +238,19 @@ impl InterruptSourceGroup for MsiInterruptGroup {
let routes = self.gsi_msi_routes.lock().unwrap();
self.set_gsi_routes(&routes)
}
fn set_notifier(
&mut self,
index: InterruptIndex,
eventfd: Option<EventFd>,
vm: &dyn hypervisor::Vm,
) -> Result<()> {
if let Some(route) = self.irq_routes.get(&index) {
return route.lock().unwrap().set_notifier(eventfd, vm);
}
Ok(())
}
}
pub struct LegacyUserspaceInterruptGroup {
@@ -323,6 +339,26 @@ impl InterruptManager for LegacyUserspaceInterruptManager {
}
}
impl MsiInterruptManager {
fn create_group_raw(
&self,
config: <Self as InterruptManager>::GroupConfig,
) -> Result<MsiInterruptGroup> {
let mut allocator = self.allocator.lock().unwrap();
let mut irq_routes: HashMap<InterruptIndex, Mutex<InterruptRoute>> =
HashMap::with_capacity(config.count as usize);
for i in config.base..config.base + config.count {
irq_routes.insert(i, Mutex::new(InterruptRoute::new(&mut allocator)?));
}
Ok(MsiInterruptGroup::new(
self.vm.clone(),
self.gsi_msi_routes.clone(),
irq_routes,
))
}
}
impl InterruptManager for MsiInterruptManager {
type GroupConfig = MsiIrqGroupConfig;
@@ -341,6 +377,14 @@ impl InterruptManager for MsiInterruptManager {
)))
}
fn create_group_mut(
&self,
config: Self::GroupConfig,
) -> vm_device::interrupt::Result<Arc<Mutex<dyn InterruptSourceGroup>>> {
let r = self.create_group_raw(config)?;
Ok(Arc::new(Mutex::new(r)))
}
fn destroy_group(&self, _group: Arc<dyn InterruptSourceGroup>) -> Result<()> {
Ok(())
}