Skip to content

Commit

Permalink
fix(vfio): disable MSIX IRQs iff any was enabled
Browse files Browse the repository at this point in the history
Signed-off-by: Changyuan Lyu <[email protected]>
  • Loading branch information
Lencerf committed Dec 15, 2024
1 parent 5e90481 commit 577f98f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
17 changes: 16 additions & 1 deletion alioth/src/vfio/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ use std::os::fd::AsRawFd;
use std::os::unix::fs::FileExt;

use crate::mem;
use crate::vfio::bindings::{VfioDeviceInfo, VfioIrqInfo, VfioIrqSet, VfioRegionInfo};
use crate::vfio::bindings::{
VfioDeviceInfo, VfioIrqInfo, VfioIrqSet, VfioIrqSetData, VfioIrqSetFlag, VfioPciIrq,
VfioRegionInfo,
};
use crate::vfio::ioctls::{
vfio_device_get_info, vfio_device_get_irq_info, vfio_device_get_region_info, vfio_device_reset,
vfio_device_set_irqs,
Expand Down Expand Up @@ -63,6 +66,18 @@ pub trait Device: Debug + Send + Sync + 'static {
Ok(())
}

fn disable_all_irqs(&self, index: VfioPciIrq) -> Result<()> {
let vfio_irq_disable_all = VfioIrqSet {
argsz: size_of::<VfioIrqSet<0>>() as u32,
flags: VfioIrqSetFlag::DATA_NONE | VfioIrqSetFlag::ACTION_TRIGGER,
index: index.raw(),
start: 0,
count: 0,
data: VfioIrqSetData { eventfds: [] },
};
self.set_irqs(&vfio_irq_disable_all)
}

fn reset(&self) -> Result<()> {
unsafe { vfio_device_reset(self.fd()) }?;
Ok(())
Expand Down
30 changes: 8 additions & 22 deletions alioth/src/vfio/pci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,15 +569,13 @@ where
}

fn reset(&self) -> Result<()> {
let disable_msix = VfioIrqSet {
argsz: size_of::<VfioIrqSet<0>>() as u32,
flags: VfioIrqSetFlag::DATA_NONE | VfioIrqSetFlag::ACTION_TRIGGER,
index: VfioPciIrq::MSIX.raw(),
start: 0,
count: 0,
data: VfioIrqSetData { eventfds: [] },
};
self.config.dev.dev.set_irqs(&disable_msix)?;
let is_irqfd = |e| matches!(e, &MsixTableMmioEntry::IrqFd(_));
if self.msix_table.entries.read().iter().any(is_irqfd) {
let dev = &self.config.dev;
if let Err(e) = dev.dev.disable_all_irqs(VfioPciIrq::MSIX) {
log::error!("{}: failed to disable MSIX IRQs: {e:?}", dev.name)
}
}

self.msix_table.reset();
self.config.dev.dev.reset()
Expand Down Expand Up @@ -606,18 +604,6 @@ where
M: MsiSender,
D: Device,
{
fn disable_all_irqs(&self) -> Result<()> {
let vfio_irq_disable_all = VfioIrqSet {
argsz: size_of::<VfioIrqSet<0>>() as u32,
flags: VfioIrqSetFlag::DATA_NONE | VfioIrqSetFlag::ACTION_TRIGGER,
index: VfioPciIrq::MSIX.raw(),
start: 0,
count: 0,
data: VfioIrqSetData { eventfds: [] },
};
self.cdev.dev.set_irqs(&vfio_irq_disable_all)
}

fn enable_irqfd(&self, index: usize) -> Result<()> {
let mut entries = self.table.entries.write();
let Some(entry) = entries.get_mut(index) else {
Expand Down Expand Up @@ -650,7 +636,7 @@ where
// subindex for the first time.
// As long as the following set_irqs() succeeds, we can safely ignore
// the error here.
let _ = self.disable_all_irqs();
let _ = self.cdev.dev.disable_all_irqs(VfioPciIrq::MSIX);

let mut eventfds = [-1; 2048];
let mut count = 0;
Expand Down

0 comments on commit 577f98f

Please sign in to comment.