Skip to content

Commit e7e0efe

Browse files
committed
pci: Return error from add_device() instead of asserting
Replace the assert in PciBus::add_device() with a proper error return, allowing callers to handle duplicate device IDs gracefully. This is important for the snapshot restore path where a corrupted snapshot should not crash the VMM. Signed-off-by: Ilias Stamatis <ilstam@amazon.com>
1 parent 3cf3a58 commit e7e0efe

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

src/vmm/src/device_manager/pci_mngr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl PciDevices {
121121
.pci_bus
122122
.lock()
123123
.expect("Poisoned lock")
124-
.add_device(sbdf.device(), virtio_device.clone());
124+
.add_device(sbdf.device(), virtio_device.clone())?;
125125

126126
self.virtio_devices
127127
.insert((device_type, id), virtio_device.clone());

src/vmm/src/pci/bus.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ use crate::vstate::bus::BusDevice;
2121
pub enum PciRootError {
2222
/// Could not find an available device slot on the PCI bus.
2323
NoPciDeviceSlotAvailable,
24+
/// PCI device ID {0} is already in use.
25+
DuplicateDeviceId(u8),
2426
}
2527

2628
const VENDOR_ID_INTEL: u16 = 0x8086;
@@ -105,13 +107,17 @@ impl PciBus {
105107
}
106108

107109
/// Insert a device in the bus
108-
pub fn add_device(&mut self, device_id: u8, device: Arc<Mutex<dyn PciDevice>>) {
109-
assert!(
110-
!self.devices.contains_key(&device_id),
111-
"PCI device ID {device_id} already in use"
112-
);
110+
pub fn add_device(
111+
&mut self,
112+
device_id: u8,
113+
device: Arc<Mutex<dyn PciDevice>>,
114+
) -> Result<(), PciRootError> {
115+
if self.devices.contains_key(&device_id) {
116+
return Err(PciRootError::DuplicateDeviceId(device_id));
117+
}
113118
self.devices.insert(device_id, device);
114119
self.device_ids[device_id as usize] = true;
120+
Ok(())
115121
}
116122

117123
/// Remove a device from the bus and free its device ID slot
@@ -582,7 +588,8 @@ mod tests {
582588
fn initialize_bus() -> (PciConfigMmio, PciConfigIo) {
583589
let root = PciRoot::new(None);
584590
let mut bus = PciBus::new(root);
585-
bus.add_device(1, Arc::new(Mutex::new(PciDevMock::new())));
591+
bus.add_device(1, Arc::new(Mutex::new(PciDevMock::new())))
592+
.unwrap();
586593

587594
let bus = Arc::new(Mutex::new(bus));
588595
(PciConfigMmio::new(bus.clone()), PciConfigIo::new(bus))

0 commit comments

Comments
 (0)