Skip to content

Commit d0b2534

Browse files
daschinmoy21rbradford
authored andcommitted
pci, devices, virtio-devices, vmm: Refactor allocate_bars
Refactor PciDevice::allocate_bars trait and all implementations to take &mut SystemAllocator instead of &Arc<Mutex<SystemAllocator>>, removing double indirection. The caller in device_manager.rs now acquires the lock before calling allocate_bars. Signed-off-by: Chinmoy <daschinmoyy21@gmail.com>
1 parent ef9133a commit d0b2534

8 files changed

Lines changed: 12 additions & 14 deletions

File tree

devices/src/ivshmem.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ impl BusDevice for IvshmemDevice {
217217
impl PciDevice for IvshmemDevice {
218218
fn allocate_bars(
219219
&mut self,
220-
_allocator: &Arc<Mutex<SystemAllocator>>,
220+
_allocator: &mut SystemAllocator,
221221
mmio32_allocator: &mut AddressAllocator,
222222
mmio64_allocator: &mut AddressAllocator,
223223
resources: Option<Vec<Resource>>,

devices/src/pvmemcontrol.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
use std::collections::HashMap;
77
use std::ffi::CString;
8-
use std::sync::{Arc, Barrier, Mutex, RwLock};
8+
use std::sync::{Arc, Barrier, RwLock};
99
use std::{io, result};
1010

1111
use log::{debug, warn};
@@ -722,7 +722,7 @@ impl PciDevice for PvmemcontrolPciDevice {
722722

723723
fn allocate_bars(
724724
&mut self,
725-
_allocator: &Arc<Mutex<SystemAllocator>>,
725+
_allocator: &mut SystemAllocator,
726726
mmio32_allocator: &mut AddressAllocator,
727727
_mmio64_allocator: &mut AddressAllocator,
728728
resources: Option<Vec<Resource>>,

devices/src/pvpanic.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
use std::any::Any;
77
use std::result;
8-
use std::sync::{Arc, Barrier, Mutex};
8+
use std::sync::{Arc, Barrier};
99

1010
use anyhow::anyhow;
1111
use event_monitor::event;
@@ -174,7 +174,7 @@ impl PciDevice for PvPanicDevice {
174174

175175
fn allocate_bars(
176176
&mut self,
177-
_allocator: &Arc<Mutex<SystemAllocator>>,
177+
_allocator: &mut SystemAllocator,
178178
mmio32_allocator: &mut AddressAllocator,
179179
_mmio64_allocator: &mut AddressAllocator,
180180
resources: Option<Vec<Resource>>,

pci/src/device.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
66

77
use std::any::Any;
8-
use std::sync::{Arc, Barrier, Mutex};
8+
use std::sync::{Arc, Barrier};
99
use std::{io, result};
1010

1111
use serde::{Deserialize, Serialize};
@@ -49,7 +49,7 @@ pub trait PciDevice: Send {
4949
/// returns an address. Returns a Vec of (GuestAddress, GuestUsize) tuples.
5050
fn allocate_bars(
5151
&mut self,
52-
_allocator: &Arc<Mutex<SystemAllocator>>,
52+
_allocator: &mut SystemAllocator,
5353
_mmio32_allocator: &mut AddressAllocator,
5454
_mmio64_allocator: &mut AddressAllocator,
5555
_resources: Option<Vec<Resource>>,

pci/src/vfio.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ impl VfioCommon {
602602
#[allow(unused_variables)]
603603
pub(crate) fn allocate_bars(
604604
&mut self,
605-
allocator: &Arc<Mutex<SystemAllocator>>,
605+
allocator: &mut SystemAllocator,
606606
mmio32_allocator: &mut AddressAllocator,
607607
mmio64_allocator: &mut AddressAllocator,
608608
resources: Option<&[Resource]>,
@@ -741,8 +741,6 @@ impl VfioCommon {
741741
PciBarRegionType::IoRegion => {
742742
// The address needs to be 4 bytes aligned.
743743
allocator
744-
.lock()
745-
.unwrap()
746744
.allocate_io_addresses(restored_bar_addr, region_size, Some(0x4))
747745
.ok_or(PciDeviceError::IoAllocationFailed(region_size))?
748746
}
@@ -1852,7 +1850,7 @@ const PCI_ROM_EXP_BAR_INDEX: usize = 12;
18521850
impl PciDevice for VfioPciDevice {
18531851
fn allocate_bars(
18541852
&mut self,
1855-
allocator: &Arc<Mutex<SystemAllocator>>,
1853+
allocator: &mut SystemAllocator,
18561854
mmio32_allocator: &mut AddressAllocator,
18571855
mmio64_allocator: &mut AddressAllocator,
18581856
resources: Option<Vec<Resource>>,

pci/src/vfio_user.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ impl Vfio for VfioUserClientWrapper {
391391
impl PciDevice for VfioUserPciDevice {
392392
fn allocate_bars(
393393
&mut self,
394-
allocator: &Arc<Mutex<SystemAllocator>>,
394+
allocator: &mut SystemAllocator,
395395
mmio32_allocator: &mut AddressAllocator,
396396
mmio64_allocator: &mut AddressAllocator,
397397
resources: Option<Vec<Resource>>,

virtio-devices/src/transport/pci_device.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,7 @@ impl PciDevice for VirtioPciDevice {
978978

979979
fn allocate_bars(
980980
&mut self,
981-
_allocator: &Arc<Mutex<SystemAllocator>>,
981+
_allocator: &mut SystemAllocator,
982982
mmio32_allocator: &mut AddressAllocator,
983983
mmio64_allocator: &mut AddressAllocator,
984984
resources: Option<Vec<Resource>>,

vmm/src/device_manager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3995,7 +3995,7 @@ impl DeviceManager {
39953995
.lock()
39963996
.unwrap()
39973997
.allocate_bars(
3998-
&self.address_manager.allocator,
3998+
&mut self.address_manager.allocator.lock().unwrap(),
39993999
&mut self.pci_segments[segment_id as usize]
40004000
.mem32_allocator
40014001
.lock()

0 commit comments

Comments
 (0)