Skip to content

Commit 8073b33

Browse files
committed
fix(mmio): avoid locking multiple times in same code branch
MMIO transport layer holds a Mutex to the VirtIO device. Within the logic that handles the handshake between the driver and the device, there were cases where we would take and release the lock multiple times within the same code branch. Change this to only take the lock once. Signed-off-by: Babis Chalios <[email protected]>
1 parent 3b06a84 commit 8073b33

File tree

1 file changed

+15
-12
lines changed
  • src/vmm/src/devices/virtio/transport

1 file changed

+15
-12
lines changed

src/vmm/src/devices/virtio/transport/mmio.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,12 @@ impl MmioTransport {
179179
}
180180
DRIVER_OK if self.device_status == (ACKNOWLEDGE | DRIVER | FEATURES_OK) => {
181181
self.device_status = status;
182-
let device_activated = self.locked_device().is_activated();
182+
let mut locked_device = self.device.lock().expect("Poisoned lock");
183+
let device_activated = locked_device.is_activated();
183184
if !device_activated {
184185
// temporary variable needed for borrow checker
185-
let activate_result = self
186-
.locked_device()
187-
.activate(self.mem.clone(), self.interrupt.clone());
186+
let activate_result =
187+
locked_device.activate(self.mem.clone(), self.interrupt.clone());
188188
if let Err(err) = activate_result {
189189
self.device_status |= DEVICE_NEEDS_RESET;
190190

@@ -201,16 +201,19 @@ impl MmioTransport {
201201
self.device_status |= FAILED;
202202
}
203203
_ if status == 0 => {
204-
if self.locked_device().is_activated() {
205-
let mut device_status = self.device_status;
206-
let reset_result = self.locked_device().reset();
207-
match reset_result {
208-
Some((_interrupt_evt, mut _queue_evts)) => {}
209-
None => {
210-
device_status |= FAILED;
204+
{
205+
let mut locked_device = self.device.lock().expect("Poisoned lock");
206+
if locked_device.is_activated() {
207+
let mut device_status = self.device_status;
208+
let reset_result = locked_device.reset();
209+
match reset_result {
210+
Some((_interrupt_evt, mut _queue_evts)) => {}
211+
None => {
212+
device_status |= FAILED;
213+
}
211214
}
215+
self.device_status = device_status;
212216
}
213-
self.device_status = device_status;
214217
}
215218

216219
// If the backend device driver doesn't support reset,

0 commit comments

Comments
 (0)