Skip to content

nvme_driver: don't flr nvme devices #1714

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

Merged
merged 9 commits into from
Jul 21, 2025
Merged
Changes from 1 commit
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
98 changes: 82 additions & 16 deletions openhcl/underhill_core/src/nvme_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ use pal_async::task::Spawn;
use pal_async::task::Task;
use std::collections::HashMap;
use std::collections::hash_map;
use std::sync::Arc;
use thiserror::Error;
use tracing::Instrument;
use user_driver::vfio::PciDeviceResetMethod;
use user_driver::vfio::VfioDevice;
use user_driver::vfio::vfio_set_device_reset_method;
use vm_resource::AsyncResolveResource;
use vm_resource::ResourceId;
use vm_resource::ResourceResolver;
Expand Down Expand Up @@ -226,6 +229,80 @@ struct NvmeManagerWorker {
dma_client_spawner: DmaClientSpawner,
}

async fn create_nvme_device(
driver_source: &VmTaskDriverSource,
pci_id: &str,
vp_count: u32,
is_isolated: bool,
dma_client: Arc<dyn user_driver::DmaClient>,
) -> Result<nvme_driver::NvmeDriver<VfioDevice>, InnerError> {
// Disable FLR on vfio attach/detach; this allows faster system
// startup/shutdown with the caveat that the device needs to be properly
// sent through the shutdown path during servicing operations, as that is
// the only cleanup performed. If the device fails to initialize, turn FLR
// on and try again, so that the reset is invoked on the next attach.
let update_reset = |method: PciDeviceResetMethod| {
if let Err(err) = vfio_set_device_reset_method(pci_id, method) {
tracing::warn!(
?method,
err = &err as &dyn std::error::Error,
"Failed to update reset_method"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: lowercase "failed" to match others

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take that as a follow-up. I've got more changes to come in this code base. Thanks Chris!

);
}
};
let mut last_err = None;
for reset_method in [PciDeviceResetMethod::NoReset, PciDeviceResetMethod::Flr] {
update_reset(reset_method);
match try_create_nvme_device(
driver_source,
pci_id,
vp_count,
is_isolated,
dma_client.clone(),
)
.await
{
Ok(device) => {
if !matches!(reset_method, PciDeviceResetMethod::NoReset) {
update_reset(PciDeviceResetMethod::NoReset);
}
return Ok(device);
}
Err(err) => {
tracing::error!(
pci_id,
?reset_method,
err = &err as &dyn std::error::Error,
"failed to create nvme device"
);
last_err = Some(err);
}
}
}
Err(last_err.unwrap())
}

async fn try_create_nvme_device(
driver_source: &VmTaskDriverSource,
pci_id: &str,
vp_count: u32,
is_isolated: bool,
dma_client: Arc<dyn user_driver::DmaClient>,
) -> Result<nvme_driver::NvmeDriver<VfioDevice>, InnerError> {
let device = VfioDevice::new(driver_source, pci_id, dma_client)
.instrument(tracing::info_span!("vfio_device_open", pci_id))
.await
.map_err(InnerError::Vfio)?;

// TODO: For now, any isolation means use bounce buffering. This
// needs to change when we have nvme devices that support DMA to
// confidential memory.
nvme_driver::NvmeDriver::new(driver_source, vp_count, device, is_isolated)
.instrument(tracing::info_span!("nvme_driver_init", pci_id))
.await
.map_err(InnerError::DeviceInitFailed)
}

impl NvmeManagerWorker {
async fn run(&mut self, mut recv: mesh::Receiver<Request>) {
let (join_span, nvme_keepalive) = loop {
Expand Down Expand Up @@ -315,26 +392,15 @@ impl NvmeManagerWorker {
})
.map_err(InnerError::DmaClient)?;

let device = VfioDevice::new(&self.driver_source, entry.key(), dma_client)
.instrument(tracing::info_span!("vfio_device_open", pci_id))
.await
.map_err(InnerError::Vfio)?;

// TODO: For now, any isolation means use bounce buffering. This
// needs to change when we have nvme devices that support DMA to
// confidential memory.
let driver = nvme_driver::NvmeDriver::new(
let driver = create_nvme_device(
&self.driver_source,
&pci_id,
self.vp_count,
device,
self.is_isolated,
dma_client,
)
.instrument(tracing::info_span!(
"nvme_driver_init",
pci_id = entry.key()
))
.await
.map_err(InnerError::DeviceInitFailed)?;
.await?;
//.map_err(InnerError::DeviceInitFailed)?;

entry.insert(driver)
}
Expand Down
Loading