-
Notifications
You must be signed in to change notification settings - Fork 21
Virtio blk #36
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
base: next
Are you sure you want to change the base?
Virtio blk #36
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds support for virtual block devices (Virtio-blk) to the hypervisor by integrating MMIO-based virtio block device configuration and enhancing memory management capabilities.
- Wraps the VM's address space in
Arc<Mutex<>>to enable safe concurrent access from virtio device handlers - Implements memory region mapping logic with support for identical mapping, allocated mapping, and reserved mapping types
- Adds guest memory access closures (read/write operations and IRQ injection) for virtio device communication
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 11 comments.
| File | Description |
|---|---|
| src/vm.rs | Core changes: wraps address_space in Arc<Mutex<>>, adds memory region mapping logic, implements guest memory access closures for virtio devices, updates all address_space access sites with lock() calls |
| src/config.rs | Adds VirtioBlkMmioDeviceConfig support, exposes memory_regions field and related methods, adds get_vcpu_affinities_pcpu_ids method |
| Cargo.toml | Updates dependencies to personal fork branches for axdevice and axvmconfig to support virtio-blk features |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| match addr_space.translated_byte_buffer(addr, data.len()) { | ||
| Some(mut buffer) => { | ||
| let mut copied_bytes = 0; | ||
| for (_i, chunk) in buffer.iter_mut().enumerate() { |
Copilot
AI
Dec 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable _i in the iterator enumerate is unused (indicated by the underscore prefix). Consider removing the enumerate entirely and just using iter_mut() directly since the index is not needed.
| for (_i, chunk) in buffer.iter_mut().enumerate() { | |
| for chunk in buffer.iter_mut() { |
| let vcpu_id_pcpu_sets = config.phys_cpu_ls.get_vcpu_affinities_pcpu_ids(); | ||
|
|
Copilot
AI
Dec 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable vcpu_id_pcpu_sets is declared but never used in this function. Consider removing this unused variable declaration to avoid confusion and improve code cleanliness.
| let vcpu_id_pcpu_sets = config.phys_cpu_ls.get_vcpu_affinities_pcpu_ids(); |
| pub fn interrupt_mode(&self) -> VMInterruptMode { | ||
| self.interrupt_mode | ||
| } | ||
|
|
Copilot
AI
Dec 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The newly added public method get_vcpu_affinities_pcpu_ids() lacks documentation comments. Public API methods should have doc comments explaining their purpose, return value format, and usage to improve code maintainability and API usability.
| /// Returns the vCPU-to-pCPU affinity information for this VM. | |
| /// | |
| /// Each element in the returned vector is a tuple of | |
| /// `(vcpu_index, pcpu_id, pcpu_set_index)`: | |
| /// - `vcpu_index`: the index of the vCPU within this VM. | |
| /// - `pcpu_id`: an optional physical CPU identifier; `None` means the vCPU | |
| /// is not pinned to a specific physical CPU. | |
| /// - `pcpu_set_index`: the index into the configured physical CPU set that | |
| /// this vCPU is associated with. | |
| /// | |
| /// This is a convenience wrapper around the underlying physical CPU list, | |
| /// intended for callers that need to inspect or configure vCPU pinning. |
| Ok(_) => {}, | ||
| Err(e) => { | ||
| if e != AxErrorKind::AlreadyExists { | ||
| return Err(e.into()); | ||
| } | ||
| warn!("Memory region [{:#x}] already mapped, skipping...", mem_region.gpa); | ||
| } |
Copilot
AI
Dec 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This error handling pattern is duplicated from lines 240-244. The code for handling the AlreadyExists case is repeated for both MapIdentical and MapAlloc. Consider extracting this into a helper function to reduce code duplication and improve maintainability.
| let address_space = Arc::new(Mutex::new( | ||
| AddrSpace::new_empty(GuestPhysAddr::from(VM_ASPACE_BASE), VM_ASPACE_SIZE)? | ||
| )); |
Copilot
AI
Dec 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding Arc<Mutex<>> around the address_space introduces potential for lock contention and complexity. While this enables shared access needed for the guest memory access closures, consider whether a more fine-grained locking strategy or a different design pattern (such as channels or read-write locks) might better serve the concurrent access patterns. The current approach requires acquiring the lock for every address space operation, which could become a bottleneck.
| ) { | ||
| Ok(_) => {}, | ||
| Err(e) => { | ||
| if e != AxErrorKind::AlreadyExists { |
Copilot
AI
Dec 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error comparison if e != AxErrorKind::AlreadyExists is checking an error against an error kind. This is a type mismatch - e appears to be of type AxError based on the context (result of map_alloc), but it's being compared to AxErrorKind. You should extract the error kind from the error before comparison, for example using a method like .kind() if available, or match on the error's kind field.
| } | ||
| } | ||
|
|
||
|
|
Copilot
AI
Dec 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are two consecutive blank lines here. Consider removing one to maintain consistent spacing throughout the codebase.
| axdevice = { git = "https://github.com/DINGBROK423/axdevice.git", branch = "virt-blk", default-features = false } | ||
| axdevice_base = "0.1" | ||
| axvmconfig = { version = "0.1", default-features = false } | ||
| axvmconfig = { git = "https://github.com/DINGBROK423/axvmconfig.git", branch = "merge_add_virtio_mmio", default-features = false } |
Copilot
AI
Dec 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The dependencies are pointing to personal GitHub forks (DINGBROK423) instead of the official arceos-hypervisor organization repositories. For production use, these should point to stable, official releases or the main organization repositories to ensure maintenance, stability, and trustworthiness of the dependencies.
|
|
||
| [patch.crates-io] | ||
| axvmconfig = { git = "https://github.com/arceos-hypervisor/axvmconfig.git", branch = "next" } | ||
| axvmconfig = { git = "https://github.com/DINGBROK423/axvmconfig.git", branch = "merge_add_virtio_mmio", default-features = false } |
Copilot
AI
Dec 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line appears to be redundant - it patches axvmconfig to the same repository and branch that's already specified in the dependencies section at line 30. Consider removing this patch entry to avoid confusion and potential version conflicts.
| axvmconfig = { git = "https://github.com/DINGBROK423/axvmconfig.git", branch = "merge_add_virtio_mmio", default-features = false } |
| /// Returns configurations related to VM memory regions. | ||
| pub fn memory_regions(&self) -> &Vec<VmMemConfig> { | ||
| &self.memory_regions | ||
| } | ||
|
|
||
| // /// Adds a new memory region to the VM configuration. | ||
| // pub fn add_memory_region(&mut self, region: VmMemConfig) { | ||
| // self.memory_regions.push(region); | ||
| // } | ||
| /// Adds a new memory region to the VM configuration. | ||
| pub fn add_memory_region(&mut self, region: VmMemConfig) { | ||
| self.memory_regions.push(region); | ||
| } |
Copilot
AI
Dec 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The newly added public methods memory_regions(), add_memory_region(), and virtio_blk_mmio() lack documentation comments. Public API methods should have doc comments explaining their purpose, parameters, and return values to improve code maintainability and API usability.
为接入虚拟块设备做了相关适配