From 58bebe332a9f67167d2ae36fe92f9666ba416c39 Mon Sep 17 00:00:00 2001 From: "alex.lyn" Date: Wed, 26 Feb 2025 18:33:28 +0800 Subject: [PATCH] runtime-rs: Simplify iommu group base name extraction from symlink Just get base name from iommu group symlink is enough. As the validation will be handled in subsequent steps when constructing the full path /sys/kernel/iommu_groups/$iommu_group. In this PR, it will remove dupicalted validation of iommu_group. Signed-off-by: alex.lyn --- .../hypervisor/src/device/driver/vfio.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/runtime-rs/crates/hypervisor/src/device/driver/vfio.rs b/src/runtime-rs/crates/hypervisor/src/device/driver/vfio.rs index c2e8dacafa..d0f3b5ea12 100644 --- a/src/runtime-rs/crates/hypervisor/src/device/driver/vfio.rs +++ b/src/runtime-rs/crates/hypervisor/src/device/driver/vfio.rs @@ -5,6 +5,7 @@ // use std::{ + ffi::OsStr, fs, path::{Path, PathBuf}, process::Command, @@ -759,14 +760,21 @@ pub fn get_vfio_iommu_group(bdf: String) -> Result { let iommugrp_symlink = fs::read_link(&iommugrp_path) .map_err(|e| anyhow!("read iommu group symlink failed {:?}", e))?; - // get base name from iommu group symlink: X - let iommu_group = get_base_name(iommugrp_symlink)? - .into_string() - .map_err(|e| anyhow!("failed to get iommu group {:?}", e))?; + // Just get base name from iommu group symlink is enough as it will be checked + // within the full path /sys/kernel/iommu_groups/$iommu_group in the subsequent step. + let iommu_group = iommugrp_symlink + .file_name() + .and_then(OsStr::to_str) + .ok_or_else(|| { + anyhow!( + "failed to get iommu group with symlink {:?}", + iommugrp_symlink + ) + })?; // we'd better verify the path to ensure it dose exist. if !Path::new(SYS_KERN_IOMMU_GROUPS) - .join(&iommu_group) + .join(iommu_group) .join("devices") .join(dbdf.as_str()) .exists()