Skip to content

Commit 6102824

Browse files
committed
msi: use Vec to store GSIs for MSI vectors
We were using a Hashmap to store the GSIs that were used by the vectors of an MSI-X group. These vectors were always indexed starting by 0, so we can just use a simple Vec. Signed-off-by: Babis Chalios <[email protected]>
1 parent ed8e826 commit 6102824

File tree

2 files changed

+22
-24
lines changed

2 files changed

+22
-24
lines changed

src/vmm/src/devices/virtio/transport/pci/device.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ pub struct VirtioPciDeviceState {
305305
pub pci_configuration_state: PciConfigurationState,
306306
pub pci_dev_state: VirtioPciCommonConfigState,
307307
pub msix_state: MsixConfigState,
308-
pub msi_vector_group: HashMap<u32, u32>,
308+
pub msi_vector_group: Vec<u32>,
309309
pub bar_configuration: Vec<PciBarConfiguration>,
310310
}
311311

src/vmm/src/vstate/vm.rs

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl MsiVector {
110110
/// MSI interrupts created for a VirtIO device
111111
pub struct MsiVectorGroup {
112112
vm: Arc<Vm>,
113-
irq_routes: HashMap<u32, MsiVector>,
113+
irq_routes: Vec<MsiVector>,
114114
}
115115

116116
impl MsiVectorGroup {
@@ -123,28 +123,25 @@ impl MsiVectorGroup {
123123
}
124124

125125
impl<'a> Persist<'a> for MsiVectorGroup {
126-
type State = HashMap<u32, u32>;
126+
type State = Vec<u32>;
127127
type ConstructorArgs = Arc<Vm>;
128128
type Error = InterruptError;
129129

130130
fn save(&self) -> Self::State {
131131
// We don't save the "enabled" state of the MSI interrupt. PCI devices store the MSI-X
132132
// configuration and make sure that the vector is enabled during the restore path if it was
133133
// initially enabled
134-
self.irq_routes
135-
.iter()
136-
.map(|(id, route)| (*id, route.gsi))
137-
.collect()
134+
self.irq_routes.iter().map(|route| route.gsi).collect()
138135
}
139136

140137
fn restore(
141138
constructor_args: Self::ConstructorArgs,
142139
state: &Self::State,
143140
) -> std::result::Result<Self, Self::Error> {
144-
let mut irq_routes = HashMap::new();
141+
let mut irq_routes = Vec::with_capacity(state.len());
145142

146-
for (id, gsi) in state {
147-
irq_routes.insert(*id, MsiVector::new(*gsi, false)?);
143+
for gsi in state {
144+
irq_routes.push(MsiVector::new(*gsi, false)?);
148145
}
149146

150147
Ok(MsiVectorGroup {
@@ -156,15 +153,15 @@ impl<'a> Persist<'a> for MsiVectorGroup {
156153

157154
impl InterruptSourceGroup for MsiVectorGroup {
158155
fn enable(&self) -> vm_device::interrupt::Result<()> {
159-
for route in self.irq_routes.values() {
156+
for route in &self.irq_routes {
160157
route.enable(&self.vm.common.fd)?;
161158
}
162159

163160
Ok(())
164161
}
165162

166163
fn disable(&self) -> vm_device::interrupt::Result<()> {
167-
for route in self.irq_routes.values() {
164+
for route in &self.irq_routes {
168165
route.disable(&self.vm.common.fd)?;
169166
}
170167

@@ -180,7 +177,9 @@ impl InterruptSourceGroup for MsiVectorGroup {
180177
}
181178

182179
fn notifier(&self, index: InterruptIndex) -> Option<&EventFd> {
183-
self.irq_routes.get(&index).map(|route| &route.event_fd)
180+
self.irq_routes
181+
.get(index as usize)
182+
.map(|route| &route.event_fd)
184183
}
185184

186185
fn update(
@@ -199,7 +198,7 @@ impl InterruptSourceGroup for MsiVectorGroup {
199198
InterruptSourceConfig::MsiIrq(config) => config,
200199
};
201200

202-
if let Some(route) = self.irq_routes.get(&index) {
201+
if let Some(route) = self.irq_routes.get(index as usize) {
203202
// When an interrupt is masked the GSI will not be passed to KVM through
204203
// KVM_SET_GSI_ROUTING. So, call [`disable()`] to unregister the interrupt file
205204
// descriptor before passing the interrupt routes to KVM
@@ -593,14 +592,13 @@ impl Vm {
593592
/// Create a group of MSI-X interrupts
594593
pub fn create_msix_group(vm: Arc<Vm>, count: u16) -> Result<MsiVectorGroup, InterruptError> {
595594
debug!("Creating new MSI group with {count} vectors");
596-
let mut irq_routes = HashMap::with_capacity(count as usize);
597-
for (gsi, i) in vm
595+
let mut irq_routes = Vec::with_capacity(count as usize);
596+
for gsi in vm
598597
.resource_allocator()
599598
.allocate_gsi_msi(count as u32)?
600599
.iter()
601-
.zip(0u32..)
602600
{
603-
irq_routes.insert(i, MsiVector::new(*gsi, false)?);
601+
irq_routes.push(MsiVector::new(*gsi, false)?);
604602
}
605603

606604
Ok(MsiVectorGroup { vm, irq_routes })
@@ -821,21 +819,21 @@ pub(crate) mod tests {
821819
let msix_group = create_msix_group(&vm);
822820

823821
// Initially all vectors are disabled
824-
for route in msix_group.irq_routes.values() {
822+
for route in &msix_group.irq_routes {
825823
assert!(!route.enabled.load(Ordering::Acquire))
826824
}
827825

828826
// Enable works
829827
msix_group.enable().unwrap();
830-
for route in msix_group.irq_routes.values() {
828+
for route in &msix_group.irq_routes {
831829
assert!(route.enabled.load(Ordering::Acquire));
832830
}
833831
// Enabling an enabled group doesn't error out
834832
msix_group.enable().unwrap();
835833

836834
// Disable works
837835
msix_group.disable().unwrap();
838-
for route in msix_group.irq_routes.values() {
836+
for route in &msix_group.irq_routes {
839837
assert!(!route.enabled.load(Ordering::Acquire))
840838
}
841839
// Disabling a disabled group doesn't error out
@@ -921,7 +919,7 @@ pub(crate) mod tests {
921919
}
922920

923921
// All vectors should be disabled
924-
for vector in msix_group.irq_routes.values() {
922+
for vector in &msix_group.irq_routes {
925923
assert!(!vector.enabled.load(Ordering::Acquire));
926924
}
927925

@@ -1018,8 +1016,8 @@ pub(crate) mod tests {
10181016
// Even if an MSI group is enabled, we don't save it as such. During restoration, the PCI
10191017
// transport will make sure the correct config is set for the vectors and enable them
10201018
// accordingly.
1021-
for (id, vector) in msix_group.irq_routes {
1022-
let new_vector = restored_group.irq_routes.get(&id).unwrap();
1019+
for (id, vector) in msix_group.irq_routes.iter().enumerate() {
1020+
let new_vector = &restored_group.irq_routes[id];
10231021
assert_eq!(vector.gsi, new_vector.gsi);
10241022
assert!(!new_vector.enabled.load(Ordering::Acquire));
10251023
}

0 commit comments

Comments
 (0)