Skip to content

Commit 2d076ff

Browse files
author
szy
committed
fix clippy
1 parent 767c398 commit 2d076ff

File tree

6 files changed

+101
-113
lines changed

6 files changed

+101
-113
lines changed

src/vmm/config.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,21 @@ use axvm::{
44
config::{AxVMConfig, AxVMCrateConfig, VmMemMappingType},
55
};
66
use core::alloc::Layout;
7-
use fdt_parser::Fdt;
87
use memory_addr::MemoryAddr;
98

109
use crate::vmm::{VM, images::ImageLoader, vm_list::push_vm};
1110

1211
#[cfg(target_arch = "aarch64")]
1312
use crate::vmm::fdt::*;
1413

14+
#[cfg(target_arch = "aarch64")]
15+
use fdt_parser::Fdt;
16+
17+
#[cfg(target_arch = "aarch64")]
18+
use alloc::vec::Vec;
19+
1520
use alloc::collections::BTreeMap;
1621
use alloc::sync::Arc;
17-
use alloc::vec::Vec;
1822
use lazyinit::LazyInit;
1923
use spin::Mutex;
2024

@@ -48,16 +52,12 @@ pub mod config {
4852
let mut configs = Vec::new();
4953

5054
if let Ok(entries) = fs::read_dir(config_dir) {
51-
for entry in entries {
52-
if let Ok(entry) = entry {
53-
let path = entry.path();
54-
// Check if the file has a .toml extension
55-
let path_str = path.as_str();
56-
if path_str.ends_with(".toml") {
57-
if let Ok(content) = fs::read_to_string(path_str) {
58-
configs.push(content);
59-
}
60-
}
55+
for entry in entries.flatten() {
56+
let path = entry.path();
57+
// Check if the file has a .toml extension
58+
let path_str = path.as_str();
59+
if path_str.ends_with(".toml") && let Ok(content) = fs::read_to_string(path_str) {
60+
configs.push(content);
6161
}
6262
}
6363
}
@@ -74,6 +74,7 @@ pub mod config {
7474
include!(concat!(env!("OUT_DIR"), "/vm_configs.rs"));
7575
}
7676

77+
#[cfg(target_arch = "aarch64")]
7778
pub fn get_developer_provided_dtb(
7879
vm_cfg: &AxVMConfig,
7980
crate_config: &AxVMCrateConfig,
@@ -94,7 +95,7 @@ pub fn get_developer_provided_dtb(
9495
use axerrno::ax_err_type;
9596
use std::io::{BufReader, Read};
9697
if let Some(dtb_path) = &crate_config.kernel.dtb_path {
97-
let (dtb_file, dtb_size) = crate::vmm::images::open_image_file(&dtb_path).unwrap();
98+
let (dtb_file, dtb_size) = crate::vmm::images::open_image_file(dtb_path).unwrap();
9899
info!("DTB file in fs, size: 0x{:x}", dtb_size);
99100

100101
let mut file = BufReader::new(dtb_file);
@@ -133,7 +134,7 @@ pub fn get_vm_dtb_arc(vm_cfg: &AxVMConfig) -> Option<Arc<[u8]>> {
133134
fn handle_fdt_operations(vm_config: &mut AxVMConfig, vm_create_config: &AxVMCrateConfig) {
134135
let host_fdt_bytes = get_host_fdt();
135136
let host_fdt = Fdt::from_bytes(host_fdt_bytes)
136-
.map_err(|e| format!("Failed to parse FDT: {:#?}", e))
137+
.map_err(|e| format!("Failed to parse FDT: {e:#?}"))
137138
.expect("Failed to parse FDT");
138139
set_phys_cpu_sets(vm_config, &host_fdt, vm_create_config);
139140

@@ -185,8 +186,12 @@ pub fn init_guest_vms() {
185186
);
186187
}
187188

189+
#[cfg(target_arch = "aarch64")]
188190
let mut vm_config = AxVMConfig::from(vm_create_config.clone());
189191

192+
#[cfg(not(target_arch = "aarch64"))]
193+
let vm_config = AxVMConfig::from(vm_create_config.clone());
194+
190195
// Handle FDT-related operations for aarch64
191196
#[cfg(target_arch = "aarch64")]
192197
handle_fdt_operations(&mut vm_config, &vm_create_config);

src/vmm/fdt/create.rs

Lines changed: 40 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::vmm::{VMRef, images::load_vm_image_from_memory};
2424
/// Returns the generated DTB data
2525
pub fn crate_guest_fdt(
2626
fdt: &Fdt,
27-
passthrough_device_names: &Vec<String>,
27+
passthrough_device_names: &[String],
2828
crate_config: &AxVMCrateConfig,
2929
) -> Vec<u8> {
3030
let mut fdt_writer = FdtWriter::new().unwrap();
@@ -96,9 +96,7 @@ pub fn crate_guest_fdt(
9696
}
9797
assert_eq!(previous_node_level, 0);
9898

99-
let guest_fdt_bytes = fdt_writer.finish().unwrap();
100-
101-
guest_fdt_bytes
99+
fdt_writer.finish().unwrap()
102100
}
103101

104102
/// Generate guest FDT cache the result
@@ -133,29 +131,29 @@ enum NodeAction {
133131
fn determine_node_action(
134132
node: &Node,
135133
node_path: &str,
136-
passthrough_device_names: &Vec<String>,
134+
passthrough_device_names: &[String],
137135
) -> NodeAction {
138136
if node.name() == "/" {
139137
// Special handling for root node
140-
return NodeAction::RootNode;
138+
NodeAction::RootNode
141139
} else if node.name().starts_with("memory") {
142140
// Skip memory nodes, will add them later
143-
return NodeAction::Skip;
141+
NodeAction::Skip
144142
} else if node_path.starts_with("/cpus") {
145-
return NodeAction::CpuNode;
143+
NodeAction::CpuNode
146144
} else if passthrough_device_names.contains(&node_path.to_string()) {
147145
// Fully matched passthrough device node
148-
return NodeAction::IncludeAsPassthroughDevice;
146+
NodeAction::IncludeAsPassthroughDevice
149147
}
150148
// Check if the node is a descendant of a passthrough device (by path inclusion and level validation)
151149
else if is_descendant_of_passthrough_device(node_path, node.level, passthrough_device_names) {
152-
return NodeAction::IncludeAsChildNode;
150+
NodeAction::IncludeAsChildNode
153151
}
154152
// Check if the node is an ancestor of a passthrough device (by path inclusion and level validation)
155153
else if is_ancestor_of_passthrough_device(node_path, passthrough_device_names) {
156-
return NodeAction::IncludeAsAncestorNode;
154+
NodeAction::IncludeAsAncestorNode
157155
} else {
158-
return NodeAction::Skip;
156+
NodeAction::Skip
159157
}
160158
}
161159

@@ -165,7 +163,7 @@ fn determine_node_action(
165163
fn is_descendant_of_passthrough_device(
166164
node_path: &str,
167165
node_level: usize,
168-
passthrough_device_names: &Vec<String>,
166+
passthrough_device_names: &[String],
169167
) -> bool {
170168
for passthrough_path in passthrough_device_names {
171169
// Check if the current node is a descendant of a passthrough device
@@ -180,9 +178,9 @@ fn is_descendant_of_passthrough_device(
180178

181179
// If passthrough_path is the root node "/", then its child node level should be 2
182180
// Otherwise, the child node level should be higher than the parent node level
183-
if passthrough_path == "/" && current_node_level >= 2 {
184-
return true;
185-
} else if passthrough_path != "/" && current_node_level > expected_parent_level {
181+
if (passthrough_path == "/" && current_node_level >= 2)
182+
|| (passthrough_path != "/" && current_node_level > expected_parent_level)
183+
{
186184
return true;
187185
}
188186
}
@@ -210,11 +208,11 @@ fn handle_node_level_change(
210208
/// Determine if node is an ancestor of passthrough device
211209
fn is_ancestor_of_passthrough_device(
212210
node_path: &str,
213-
passthrough_device_names: &Vec<String>,
211+
passthrough_device_names: &[String],
214212
) -> bool {
215213
for passthrough_path in passthrough_device_names {
216214
// Check if the current node is an ancestor of a passthrough device
217-
if passthrough_path.starts_with(&node_path) && passthrough_path.len() > node_path.len() {
215+
if passthrough_path.starts_with(node_path) && passthrough_path.len() > node_path.len() {
218216
// Ensure it is a true ancestor path (separated by /)
219217
let next_char = passthrough_path.chars().nth(node_path.len()).unwrap_or(' ');
220218
if next_char == '/' || node_path == "/" {
@@ -226,36 +224,33 @@ fn is_ancestor_of_passthrough_device(
226224
}
227225

228226
/// Determine if CPU node is needed
229-
fn need_cpu_node(phys_cpu_ids: &Vec<usize>, node: &Node, node_path: &str) -> bool {
227+
fn need_cpu_node(phys_cpu_ids: &[usize], node: &Node, node_path: &str) -> bool {
230228
let mut should_include_node = false;
231229

232230
if !node_path.starts_with("/cpus/cpu@") {
233231
should_include_node = true;
234-
} else {
235-
if let Some(mut cpu_reg) = node.reg() {
236-
if let Some(reg_entry) = cpu_reg.next() {
237-
let cpu_address = reg_entry.address as usize;
238-
debug!(
239-
"Checking CPU node {} with address 0x{:x}",
240-
node.name(),
241-
cpu_address
242-
);
243-
// Check if this CPU address is in the configured phys_cpu_ids
244-
if phys_cpu_ids.contains(&cpu_address) {
245-
should_include_node = true;
246-
debug!(
247-
"CPU node {} with address 0x{:x} is in phys_cpu_ids, including in guest FDT",
248-
node.name(),
249-
cpu_address
250-
);
251-
} else {
252-
debug!(
253-
"CPU node {} with address 0x{:x} is NOT in phys_cpu_ids, skipping",
254-
node.name(),
255-
cpu_address
256-
);
257-
}
258-
}
232+
} else if let Some(mut cpu_reg) = node.reg()
233+
&& let Some(reg_entry) = cpu_reg.next() {
234+
let cpu_address = reg_entry.address as usize;
235+
debug!(
236+
"Checking CPU node {} with address 0x{:x}",
237+
node.name(),
238+
cpu_address
239+
);
240+
// Check if this CPU address is in the configured phys_cpu_ids
241+
if phys_cpu_ids.contains(&cpu_address) {
242+
should_include_node = true;
243+
debug!(
244+
"CPU node {} with address 0x{:x} is in phys_cpu_ids, including in guest FDT",
245+
node.name(),
246+
cpu_address
247+
);
248+
} else {
249+
debug!(
250+
"CPU node {} with address 0x{:x} is NOT in phys_cpu_ids, skipping",
251+
node.name(),
252+
cpu_address
253+
);
259254
}
260255
}
261256
should_include_node
@@ -415,7 +410,5 @@ pub fn update_cpu_node(fdt: &Fdt, host_fdt: &Fdt, crate_config: &AxVMCrateConfig
415410
}
416411
assert_eq!(previous_node_level, 0);
417412

418-
let guest_fdt_bytes = new_fdt.finish().unwrap();
419-
420-
guest_fdt_bytes
413+
new_fdt.finish().unwrap()
421414
}

src/vmm/fdt/device.rs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,10 @@ pub fn find_all_passthrough_devices(vm_cfg: &mut AxVMConfig, fdt: &Fdt) -> Vec<S
178178

179179
/// Build the full path of a node based on node level relationships
180180
/// Build the path by traversing all nodes and constructing paths based on level relationships to avoid path conflicts for nodes with the same name
181-
pub fn build_node_path(all_nodes: &Vec<Node>, target_index: usize) -> String {
181+
pub fn build_node_path(all_nodes: &[Node], target_index: usize) -> String {
182182
let mut path_stack: Vec<String> = Vec::new();
183183

184-
for i in 0..=target_index {
185-
let node = &all_nodes[i];
184+
for node in all_nodes.iter().take(target_index + 1) {
186185
let level = node.level;
187186

188187
if level == 1 {
@@ -215,16 +214,15 @@ pub fn build_optimized_node_cache<'a>(fdt: &'a Fdt) -> BTreeMap<String, Vec<Node
215214

216215
for (index, node) in all_nodes.iter().enumerate() {
217216
let node_path = build_node_path(&all_nodes, index);
218-
if let Some(existing_nodes) = node_cache.get(&node_path) {
219-
if !existing_nodes.is_empty() {
220-
error!(
221-
"Duplicate node path found: {} for node '{}' at level {}, existing node: '{}'",
222-
node_path,
223-
node.name(),
224-
node.level,
225-
existing_nodes[0].name()
226-
);
227-
}
217+
if let Some(existing_nodes) = node_cache.get(&node_path)
218+
&& !existing_nodes.is_empty() {
219+
error!(
220+
"Duplicate node path found: {} for node '{}' at level {}, existing node: '{}'",
221+
node_path,
222+
node.name(),
223+
node.level,
224+
existing_nodes[0].name()
225+
);
228226
}
229227

230228
trace!(
@@ -233,7 +231,7 @@ pub fn build_optimized_node_cache<'a>(fdt: &'a Fdt) -> BTreeMap<String, Vec<Node
233231
);
234232
node_cache
235233
.entry(node_path)
236-
.or_insert_with(Vec::new)
234+
.or_default()
237235
.push(node.clone());
238236
}
239237

@@ -372,9 +370,8 @@ fn get_cells_count_for_property(prop_name: &str, cells_info: &BTreeMap<String, u
372370
"power-domains" => "#power-domain-cells",
373371
"phys" => "#phy-cells",
374372
"interrupts" | "interrupts-extended" => "#interrupt-cells",
375-
"gpios" | _ if prop_name.ends_with("-gpios") || prop_name.ends_with("-gpio") => {
376-
"#gpio-cells"
377-
}
373+
"gpios" => "#gpio-cells",
374+
_ if prop_name.ends_with("-gpios") || prop_name.ends_with("-gpio") => "#gpio-cells",
378375
"dmas" => "#dma-cells",
379376
"thermal-sensors" => "#thermal-sensor-cells",
380377
"sound-dai" => "#sound-dai-cells",
@@ -405,7 +402,7 @@ fn parse_phandle_property(
405402
for (phandle, specifiers) in phandle_refs {
406403
if let Some((device_path, _cells_info)) = phandle_map.get(&phandle) {
407404
let spec_info = if !specifiers.is_empty() {
408-
format!(" (specifiers: {:?})", specifiers)
405+
format!(" (specifiers: {specifiers:?})")
409406
} else {
410407
String::new()
411408
};
@@ -504,7 +501,7 @@ fn get_descendant_nodes_by_path<'a>(
504501
};
505502

506503
// Traverse node_cache, find all nodes with parent path as prefix
507-
for (path, _nodes) in node_cache {
504+
for path in node_cache.keys() {
508505
// Check if path has parent path as prefix (and is not the parent path itself)
509506
if path.starts_with(&search_prefix) && path.len() > search_prefix.len() {
510507
// This is a descendant node path, add to results

0 commit comments

Comments
 (0)