Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not panic if virtio device activation return Err(...) #4665

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions src/vmm/src/device_manager/mmio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,11 @@ impl MMIODeviceManager {
vm.register_ioevent(queue_evt, &io_addr, u32::try_from(i).unwrap())
.map_err(MmioError::RegisterIoEvent)?;
}
vm.register_irqfd(locked_device.interrupt_evt(), device_info.irqs[0])
.map_err(MmioError::RegisterIrqFd)?;
vm.register_irqfd(
&locked_device.interrupt_trigger().irq_evt,
device_info.irqs[0],
)
.map_err(MmioError::RegisterIrqFd)?;
}

self.register_mmio_device(
Expand Down Expand Up @@ -513,13 +516,13 @@ impl DeviceInfoForFDT for MMIODeviceInfo {

#[cfg(test)]
mod tests {
use std::sync::atomic::AtomicU32;

use std::sync::Arc;

use utils::eventfd::EventFd;

use super::*;
use crate::devices::virtio::device::VirtioDevice;
use crate::devices::virtio::device::{IrqTrigger, VirtioDevice};
use crate::devices::virtio::queue::Queue;
use crate::devices::virtio::ActivateError;
use crate::utilities::test_utils::multi_region_mem;
Expand Down Expand Up @@ -566,7 +569,7 @@ mod tests {
dummy: u32,
queues: Vec<Queue>,
queue_evts: [EventFd; 1],
interrupt_evt: EventFd,
interrupt_trigger: IrqTrigger,
}

impl DummyDevice {
Expand All @@ -575,7 +578,7 @@ mod tests {
dummy: 0,
queues: QUEUE_SIZES.iter().map(|&s| Queue::new(s)).collect(),
queue_evts: [EventFd::new(libc::EFD_NONBLOCK).expect("cannot create eventFD")],
interrupt_evt: EventFd::new(libc::EFD_NONBLOCK).expect("cannot create eventFD"),
interrupt_trigger: IrqTrigger::new().expect("cannot create eventFD"),
}
}
}
Expand Down Expand Up @@ -607,12 +610,8 @@ mod tests {
&self.queue_evts
}

fn interrupt_evt(&self) -> &EventFd {
&self.interrupt_evt
}

fn interrupt_status(&self) -> Arc<AtomicU32> {
Arc::new(AtomicU32::new(0))
fn interrupt_trigger(&self) -> &IrqTrigger {
&self.interrupt_trigger
}

fn ack_features_by_page(&mut self, page: u32, value: u32) {
Expand Down
13 changes: 3 additions & 10 deletions src/vmm/src/devices/virtio/balloon/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// SPDX-License-Identifier: Apache-2.0

use std::fmt;
use std::sync::atomic::AtomicU32;
use std::sync::Arc;
use std::time::Duration;

use log::error;
Expand Down Expand Up @@ -584,12 +582,8 @@
&self.queue_evts
}

fn interrupt_evt(&self) -> &EventFd {
&self.irq_trigger.irq_evt
}

fn interrupt_status(&self) -> Arc<AtomicU32> {
self.irq_trigger.irq_status.clone()
fn interrupt_trigger(&self) -> &IrqTrigger {
&self.irq_trigger
}

fn read_config(&self, offset: u64, data: &mut [u8]) {
Expand Down Expand Up @@ -619,10 +613,9 @@
fn activate(&mut self, mem: GuestMemoryMmap) -> Result<(), ActivateError> {
self.device_state = DeviceState::Activated(mem);
if self.activate_evt.write(1).is_err() {
error!("Balloon: Cannot write to activate_evt");
METRICS.activate_fails.inc();
self.device_state = DeviceState::Inactive;
return Err(ActivateError::BadActivate);
return Err(ActivateError::EventFd);

Check warning on line 618 in src/vmm/src/devices/virtio/balloon/device.rs

View check run for this annotation

Codecov / codecov/patch

src/vmm/src/devices/virtio/balloon/device.rs#L618

Added line #L618 was not covered by tests
}

if self.stats_enabled() {
Expand Down
18 changes: 4 additions & 14 deletions src/vmm/src/devices/virtio/block/device.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
// Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

use std::sync::atomic::AtomicU32;
use std::sync::Arc;

use event_manager::{EventOps, Events, MutEventSubscriber};
use utils::eventfd::EventFd;

use super::persist::{BlockConstructorArgs, BlockState};
use super::vhost_user::device::{VhostUserBlock, VhostUserBlockConfig};
use super::virtio::device::{VirtioBlock, VirtioBlockConfig};
use super::BlockError;
use crate::devices::virtio::device::VirtioDevice;
use crate::devices::virtio::device::{IrqTrigger, VirtioDevice};
use crate::devices::virtio::queue::Queue;
use crate::devices::virtio::{ActivateError, TYPE_BLOCK};
use crate::rate_limiter::BucketUpdate;
Expand Down Expand Up @@ -176,17 +173,10 @@
}
}

fn interrupt_evt(&self) -> &EventFd {
match self {
Self::Virtio(b) => &b.irq_trigger.irq_evt,
Self::VhostUser(b) => &b.irq_trigger.irq_evt,
}
}

fn interrupt_status(&self) -> Arc<AtomicU32> {
fn interrupt_trigger(&self) -> &IrqTrigger {
match self {
Self::Virtio(b) => b.irq_trigger.irq_status.clone(),
Self::VhostUser(b) => b.irq_trigger.irq_status.clone(),
Self::Virtio(b) => &b.irq_trigger,
Self::VhostUser(b) => &b.irq_trigger,

Check warning on line 179 in src/vmm/src/devices/virtio/block/device.rs

View check run for this annotation

Codecov / codecov/patch

src/vmm/src/devices/virtio/block/device.rs#L179

Added line #L179 was not covered by tests
}
}

Expand Down
24 changes: 9 additions & 15 deletions src/vmm/src/devices/virtio/block/vhost_user/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// Portions Copyright 2019 Intel Corporation. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

use std::sync::atomic::AtomicU32;
use std::sync::Arc;

use log::error;
Expand Down Expand Up @@ -311,13 +310,8 @@ impl<T: VhostUserHandleBackend + Send + 'static> VirtioDevice for VhostUserBlock
&self.queue_evts
}

fn interrupt_evt(&self) -> &EventFd {
&self.irq_trigger.irq_evt
}

/// Returns the current device interrupt status.
fn interrupt_status(&self) -> Arc<AtomicU32> {
self.irq_trigger.irq_status.clone()
fn interrupt_trigger(&self) -> &IrqTrigger {
&self.irq_trigger
}

fn read_config(&self, offset: u64, data: &mut [u8]) {
Expand All @@ -342,13 +336,13 @@ impl<T: VhostUserHandleBackend + Send + 'static> VirtioDevice for VhostUserBlock
// with guest driver as well.
self.vu_handle
.set_features(self.acked_features)
.map_err(ActivateError::VhostUser)?;
self.vu_handle
.setup_backend(
&mem,
&[(0, &self.queues[0], &self.queue_evts[0])],
&self.irq_trigger,
)
.and_then(|()| {
self.vu_handle.setup_backend(
&mem,
&[(0, &self.queues[0], &self.queue_evts[0])],
&self.irq_trigger,
)
})
.map_err(|err| {
self.metrics.activate_fails.inc();
ActivateError::VhostUser(err)
Expand Down
14 changes: 4 additions & 10 deletions src/vmm/src/devices/virtio/block/virtio/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use std::io::{Seek, SeekFrom, Write};
use std::os::linux::fs::MetadataExt;
use std::path::PathBuf;
use std::sync::atomic::AtomicU32;
use std::sync::Arc;

use block_io::FileEngine;
Expand Down Expand Up @@ -609,13 +608,8 @@
&self.queue_evts
}

fn interrupt_evt(&self) -> &EventFd {
&self.irq_trigger.irq_evt
}

/// Returns the current device interrupt status.
fn interrupt_status(&self) -> Arc<AtomicU32> {
self.irq_trigger.irq_status.clone()
fn interrupt_trigger(&self) -> &IrqTrigger {
&self.irq_trigger
}

fn read_config(&self, offset: u64, mut data: &mut [u8]) {
Expand Down Expand Up @@ -658,8 +652,8 @@
}

if self.activate_evt.write(1).is_err() {
error!("Block: Cannot write to activate_evt");
return Err(ActivateError::BadActivate);
self.metrics.activate_fails.inc();
return Err(ActivateError::EventFd);

Check warning on line 656 in src/vmm/src/devices/virtio/block/virtio/device.rs

View check run for this annotation

Codecov / codecov/patch

src/vmm/src/devices/virtio/block/virtio/device.rs#L655-L656

Added lines #L655 - L656 were not covered by tests
}
self.device_state = DeviceState::Activated(mem);
Ok(())
Expand Down
15 changes: 6 additions & 9 deletions src/vmm/src/devices/virtio/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,12 @@ pub trait VirtioDevice: AsAny + Send {
/// Returns the device queues event fds.
fn queue_events(&self) -> &[EventFd];

/// Returns the device interrupt eventfd.
fn interrupt_evt(&self) -> &EventFd;

/// Returns the current device interrupt status.
fn interrupt_status(&self) -> Arc<AtomicU32>;
fn interrupt_status(&self) -> Arc<AtomicU32> {
Arc::clone(&self.interrupt_trigger().irq_status)
}

fn interrupt_trigger(&self) -> &IrqTrigger;

/// The set of feature bits shifted by `page * 32`.
fn avail_features_by_page(&self, page: u32) -> u32 {
Expand Down Expand Up @@ -266,11 +267,7 @@ pub(crate) mod tests {
todo!()
}

fn interrupt_evt(&self) -> &EventFd {
todo!()
}

fn interrupt_status(&self) -> Arc<AtomicU32> {
fn interrupt_trigger(&self) -> &IrqTrigger {
todo!()
}

Expand Down
Loading
Loading