From a2bdbdfae5a7002cbab5a5d90a84afa623faf07c Mon Sep 17 00:00:00 2001 From: Stefano Garzarella Date: Mon, 26 Jan 2026 17:16:09 +0100 Subject: [PATCH 1/2] kernel: fix unused `slots` variable When `virtio-drivers` feature is enabled, but `block` is not enabled we have the current warnings: warning: unused variable: `slots` --> kernel/src/svsm.rs:166:13 | 166 | let mut slots = probe_mmio_slots(config); | ^^^^^ help: if this is intentional, prefix it with an underscore: `_slots` | = note: `#[warn(unused_variables)]` on by default warning: variable does not need to be mutable --> kernel/src/svsm.rs:166:9 | 166 | let mut slots = probe_mmio_slots(config); | ----^^^^^ | | | help: remove this `mut` | = note: `#[warn(unused_mut)]` on by default Move `probe_mmio_slots` under `block` feature for now. Suggested-by: Jon Lange Signed-off-by: Stefano Garzarella --- kernel/src/svsm.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/kernel/src/svsm.rs b/kernel/src/svsm.rs index 02a042208a..33e9f49e88 100755 --- a/kernel/src/svsm.rs +++ b/kernel/src/svsm.rs @@ -57,7 +57,7 @@ use svsm::svsm_paging::{ use svsm::task::{KernelThreadStartInfo, schedule_init, start_kernel_task}; use svsm::types::PAGE_SIZE; use svsm::utils::{MemoryRegion, ScopedRef, round_to_pages}; -#[cfg(feature = "virtio-drivers")] +#[cfg(all(feature = "virtio-drivers", feature = "block"))] use svsm::virtio::probe_mmio_slots; #[cfg(all(feature = "vtpm", not(test)))] use svsm::vtpm::vtpm_init; @@ -163,11 +163,12 @@ fn mapping_info_init(launch_info: &KernelLaunchInfo) { /// Returns an error when a virtio device is found but its driver initialization fails. #[cfg(feature = "virtio-drivers")] fn initialize_virtio_mmio() -> Result<(), SvsmError> { - let mut slots = probe_mmio_slots(); #[cfg(feature = "block")] { use svsm::block::virtio_blk::initialize_block; + + let mut slots = probe_mmio_slots(); initialize_block(&mut slots)?; } From bab01796e9868527ba73d5fb09a0946930a83ee5 Mon Sep 17 00:00:00 2001 From: Stefano Garzarella Date: Mon, 26 Jan 2026 12:29:41 +0100 Subject: [PATCH 2/2] virtio/mmio: move fw_cfg check into probe_mmio_slots() Move the fw_cfg availability check from svsm_init() into probe_mmio_slots(), which is the only place where fw_cfg is actually needed. Pass `SvsmConfig` as a parameter to probe_mmio_slots() to perform this check. This prepares for future work where virtio MMIO addresses may be discovered via device tree instead of fw_cfg. By passing `SvsmConfig`, the probe function can internally decide how to discover devices without callers needing to know the details. Also derive Default for MmioSlots to simplify returning empty slot collections. Signed-off-by: Stefano Garzarella --- kernel/src/svsm.rs | 11 +++-------- kernel/src/virtio/mmio.rs | 15 ++++++++++----- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/kernel/src/svsm.rs b/kernel/src/svsm.rs index 33e9f49e88..1af50f9059 100755 --- a/kernel/src/svsm.rs +++ b/kernel/src/svsm.rs @@ -162,13 +162,12 @@ fn mapping_info_init(launch_info: &KernelLaunchInfo) { /// Returns Ok if initialization is successful or no virtio devices are found /// Returns an error when a virtio device is found but its driver initialization fails. #[cfg(feature = "virtio-drivers")] -fn initialize_virtio_mmio() -> Result<(), SvsmError> { - +fn initialize_virtio_mmio(_config: &SvsmConfig<'_>) -> Result<(), SvsmError> { #[cfg(feature = "block")] { use svsm::block::virtio_blk::initialize_block; - let mut slots = probe_mmio_slots(); + let mut slots = probe_mmio_slots(_config); initialize_block(&mut slots)?; } @@ -436,11 +435,7 @@ fn svsm_init(launch_info: &KernelLaunchInfo) { virt_log_usage(); #[cfg(feature = "virtio-drivers")] - if config.has_fw_cfg_port() { - // Virtio cannot exist if there is no fw_cfg, so do not bother to - // attempt initialization if it is not present. - initialize_virtio_mmio().expect("Failed to initialize virtio-mmio drivers"); - } + initialize_virtio_mmio(&config).expect("Failed to initialize virtio-mmio drivers"); if let Err(e) = SVSM_PLATFORM.launch_fw(&config) { panic!("Failed to launch FW: {e:?}"); diff --git a/kernel/src/virtio/mmio.rs b/kernel/src/virtio/mmio.rs index f1cb2e9acd..11d5a0ae0f 100644 --- a/kernel/src/virtio/mmio.rs +++ b/kernel/src/virtio/mmio.rs @@ -12,6 +12,7 @@ use virtio_drivers::transport::{DeviceType, Transport, mmio::MmioTransport}; use crate::{ address::PhysAddr, + config::SvsmConfig, fw_cfg::FwCfg, mm::{GlobalRangeGuard, map_global_range_4k_shared, pagetable::PTEntryFlags}, platform::SVSM_PLATFORM, @@ -25,7 +26,7 @@ pub struct MmioSlot { pub transport: MmioTransport, } -#[derive(Debug)] +#[derive(Debug, Default)] pub struct MmioSlots { slots: Vec, } @@ -47,14 +48,18 @@ pub struct MmioSlots { /// Returns an [`MmioSlots`] collection containing all discovered virtio-MMIO devices. /// Returns an empty collection if no devices are found or if the fw_cfg interface /// is unavailable. -pub fn probe_mmio_slots() -> MmioSlots { +pub fn probe_mmio_slots(config: &SvsmConfig<'_>) -> MmioSlots { + // Virtio MMIO addresses are discovered via fw_cfg, so skip probing + // if it is not present. + if !config.has_fw_cfg_port() { + return MmioSlots::default(); + } + virtio_init(); let cfg = FwCfg::new(SVSM_PLATFORM.get_io_port()); let Ok(dev) = cfg.get_virtio_mmio_addresses() else { - return MmioSlots { - slots: Vec::::new(), - }; + return MmioSlots::default(); }; let mut slots = Vec::with_capacity(dev.len());