From 6c2fc233e23773af1b372fdefc6948c5056fa588 Mon Sep 17 00:00:00 2001 From: Jianyong Wu Date: Thu, 29 Oct 2020 13:25:43 +0800 Subject: [PATCH] agent: create pci root Bus Path for arm64 port https://github.com/kata-containers/agent/pull/860 here. Fixes: #1059 Signed-off-by: Jianyong Wu --- src/agent/Cargo.toml | 1 + src/agent/src/linux_abi.rs | 45 +++++++++++++++++++++++++++++++++++--- src/agent/src/uevent.rs | 7 ++++-- 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/agent/Cargo.toml b/src/agent/Cargo.toml index 3013b2d107..b1af99ee98 100644 --- a/src/agent/Cargo.toml +++ b/src/agent/Cargo.toml @@ -21,6 +21,7 @@ signal-hook = "0.1.9" scan_fmt = "0.2.3" scopeguard = "1.0.0" regex = "1" + # slog: # - Dynamic keys required to allow HashMap keys to be slog::Serialized. # - The 'max_*' features allow changing the log level at runtime diff --git a/src/agent/src/linux_abi.rs b/src/agent/src/linux_abi.rs index b41fcf1ba9..6cf9306b6f 100644 --- a/src/agent/src/linux_abi.rs +++ b/src/agent/src/linux_abi.rs @@ -5,8 +5,10 @@ /// Linux ABI related constants. -pub const SYSFS_DIR: &str = "/sys"; +#[cfg(target_arch = "aarch64")] +use std::fs; +pub const SYSFS_DIR: &str = "/sys"; pub const SYSFS_PCI_BUS_PREFIX: &str = "/sys/bus/pci/devices"; pub const SYSFS_PCI_BUS_RESCAN_FILE: &str = "/sys/bus/pci/rescan"; #[cfg(any( @@ -15,9 +17,46 @@ pub const SYSFS_PCI_BUS_RESCAN_FILE: &str = "/sys/bus/pci/rescan"; target_arch = "x86_64", target_arch = "x86" ))] -pub const PCI_ROOT_BUS_PATH: &str = "/devices/pci0000:00"; +pub fn create_pci_root_bus_path() -> String { + String::from("/devices/pci0000:00") +} + #[cfg(target_arch = "aarch64")] -pub const PCI_ROOT_BUS_PATH: &str = "/devices/platform/4010000000.pcie/pci0000:00"; +pub fn create_pci_root_bus_path() -> String { + let ret = String::from("/devices/platform/4010000000.pcie/pci0000:00"); + + let mut sysfs_dir = String::from(SYSFS_DIR); + let mut start_root_bus_path = String::from("/devices/platform/"); + let end_root_bus_path = String::from("/pci0000:00"); + + sysfs_dir.push_str(&start_root_bus_path); + let entries = match fs::read_dir(sysfs_dir) { + Ok(e) => e, + Err(_) => return ret, + }; + for entry in entries { + let pathname = match entry { + Ok(p) => p.path(), + Err(_) => return ret, + }; + let dir_name = match pathname.file_name() { + Some(p) => p.to_str(), + None => return ret, + }; + let dir_name = match dir_name { + Some(p) => p, + None => return ret, + }; + let dir_name = String::from(dir_name); + if dir_name.ends_with(".pcie") { + start_root_bus_path.push_str(&dir_name); + start_root_bus_path.push_str(&end_root_bus_path); + return start_root_bus_path; + } + } + + ret +} pub const SYSFS_CPU_ONLINE_PATH: &str = "/sys/devices/system/cpu"; diff --git a/src/agent/src/uevent.rs b/src/agent/src/uevent.rs index 35e8515633..42f1590ab1 100644 --- a/src/agent/src/uevent.rs +++ b/src/agent/src/uevent.rs @@ -48,13 +48,16 @@ impl Uevent { // Check whether this is a block device hot-add event. fn is_block_add_event(&self) -> bool { + let pci_root_bus_path = create_pci_root_bus_path(); self.action == U_EVENT_ACTION_ADD && self.subsystem == "block" - && self.devpath.starts_with(PCI_ROOT_BUS_PATH) + && self.devpath.starts_with(&pci_root_bus_path) && self.devname != "" } fn handle_block_add_event(&self, sandbox: &Arc>) { + let pci_root_bus_path = create_pci_root_bus_path(); + // Keep the same lock order as device::get_device_name(), otherwise it may cause deadlock. let mut w = GLOBAL_DEVICE_WATCHER.lock().unwrap(); let mut sb = sandbox.lock().unwrap(); @@ -69,7 +72,7 @@ impl Uevent { let empties: Vec<_> = w .iter() .filter(|(dev_addr, _)| { - let pci_p = format!("{}/{}", PCI_ROOT_BUS_PATH, *dev_addr); + let pci_p = format!("{}/{}", pci_root_bus_path, *dev_addr); // blk block device devpath.starts_with(pci_p.as_str()) ||