Skip to content

refine: Minor refine vm-mm#99

Merged
junyu0312 merged 1 commit intomainfrom
refine
Mar 11, 2026
Merged

refine: Minor refine vm-mm#99
junyu0312 merged 1 commit intomainfrom
refine

Conversation

@junyu0312
Copy link
Owner

@junyu0312 junyu0312 commented Mar 11, 2026

Summary by CodeRabbit

  • Refactor

    • Reworked internal memory container and region model for clearer modularity and consistent access.
  • Bug Fixes

    • Memory operations now correctly handle data spanning multiple memory regions.
    • Removed obsolete error cases to simplify error surface.

@coderabbitai
Copy link

coderabbitai bot commented Mar 11, 2026

Caution

Review failed

Pull request was closed or merged during review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 3d07c446-3178-4a92-8d50-d9bfe0905bb4

📥 Commits

Reviewing files that changed from the base of the PR and between bd4cbcf and 07a48a9.

📒 Files selected for processing (30)
  • crates/vm-bootloader/src/boot_loader/arch/aarch64.rs
  • crates/vm-bootloader/src/initrd_loader.rs
  • crates/vm-bootloader/src/kernel_loader/linux/bzimage.rs
  • crates/vm-bootloader/src/kernel_loader/linux/image.rs
  • crates/vm-core/src/virt.rs
  • crates/vm-core/src/virt/hvp.rs
  • crates/vm-core/src/virt/hvp/mm.rs
  • crates/vm-device/src/device/virtio/virtio_balloon_traditional.rs
  • crates/vm-device/src/device/virtio/virtio_blk.rs
  • crates/vm-device/src/device/virtio/virtio_mmio_kbd.rs
  • crates/vm-machine/src/device.rs
  • crates/vm-machine/src/firmware/bios.rs
  • crates/vm-mm/src/allocator.rs
  • crates/vm-mm/src/allocator/mmap_allocator.rs
  • crates/vm-mm/src/error.rs
  • crates/vm-mm/src/lib.rs
  • crates/vm-mm/src/manager.rs
  • crates/vm-mm/src/memory_container.rs
  • crates/vm-mm/src/region.rs
  • crates/vm-virtio/src/device/virtqueue.rs
  • crates/vm-virtio/src/transport.rs
  • crates/vm-virtio/src/transport/mmio.rs
  • crates/vm-virtio/src/transport/mmio/mmio_handler.rs
  • crates/vm-virtio/src/transport/pci.rs
  • crates/vm-virtio/src/transport/pci/common_config_handler.rs
  • crates/vm-virtio/src/transport/pci/device_handler.rs
  • crates/vm-virtio/src/transport/pci/isr_handler.rs
  • crates/vm-virtio/src/transport/pci/notify_handler.rs
  • crates/vm-virtio/src/virtqueue.rs
  • crates/vm-virtio/src/virtqueue/virtq_desc_table.rs
💤 Files with no reviewable changes (1)
  • crates/vm-mm/src/error.rs
🚧 Files skipped from review as they are similar to previous changes (16)
  • crates/vm-machine/src/device.rs
  • crates/vm-virtio/src/device/virtqueue.rs
  • crates/vm-virtio/src/transport.rs
  • crates/vm-device/src/device/virtio/virtio_blk.rs
  • crates/vm-mm/src/memory_container.rs
  • crates/vm-virtio/src/transport/mmio/mmio_handler.rs
  • crates/vm-virtio/src/virtqueue/virtq_desc_table.rs
  • crates/vm-virtio/src/virtqueue.rs
  • crates/vm-virtio/src/transport/pci/common_config_handler.rs
  • crates/vm-bootloader/src/kernel_loader/linux/bzimage.rs
  • crates/vm-machine/src/firmware/bios.rs
  • crates/vm-mm/src/allocator/mmap_allocator.rs
  • crates/vm-device/src/device/virtio/virtio_mmio_kbd.rs
  • crates/vm-virtio/src/transport/pci/notify_handler.rs
  • crates/vm-bootloader/src/boot_loader/arch/aarch64.rs
  • crates/vm-core/src/virt.rs

📝 Walkthrough

Walkthrough

Relocates and refactors the MemoryContainer trait into vm-mm::memory_container, renames/extends its API (to_hva → hva, adds length), updates MemoryRegion/MemoryAddressSpace to infer region length and handle multi-region copy/memset, and removes two error variants.

Changes

Cohort / File(s) Summary
Core trait & module
crates/vm-mm/src/memory_container.rs, crates/vm-mm/src/lib.rs
Add new public MemoryContainer trait (hva(), length()); export memory_container module and add crate-wide #![deny(warnings)].
Allocator & mmap impl
crates/vm-mm/src/allocator.rs, crates/vm-mm/src/allocator/mmap_allocator.rs
Remove local MemoryContainer trait; import new trait and implement MemoryContainer for MmapMut with hva() and length(); simplify allocation flow.
Memory region API
crates/vm-mm/src/region.rs
Remove len field and ctor length param; new ctor MemoryRegion::new(gpa, memory); replace to_hva() with hva() and add len() delegating to container.
Memory manager (address space)
crates/vm-mm/src/manager.rs
Remove IntoIterator impl; rewrite get_by_gpa, gpa_to_hva, add multi-region-aware memset and copy_from_slice, use region.hva() and region.len() consistently; update overlap logic.
Error surface
crates/vm-mm/src/error.rs
Removed MemoryIsNotAllocated and MemoryAlreadyAllocated variants.
VM core & hvp
crates/vm-core/src/virt.rs, crates/vm-core/src/virt/hvp.rs, crates/vm-core/src/virt/hvp/mm.rs
Update imports to new trait; adapt MemoryRegion::new usage; implement MemoryContainer changes for MemoryWrapper (hva() and length()).
Bootloader loaders
crates/vm-bootloader/src/.../aarch64.rs, crates/vm-bootloader/src/initrd_loader.rs, crates/vm-bootloader/src/kernel_loader/linux/bzimage.rs, crates/vm-bootloader/src/kernel_loader/linux/image.rs
Update MemoryContainer import path and adjust copy_from_slice calls to take slices (length inferred from slice).
Device & machine code
crates/vm-device/src/..., crates/vm-machine/src/device.rs, crates/vm-machine/src/firmware/bios.rs
Update MemoryContainer import paths to vm_mm::memory_container::MemoryContainer across device and machine modules.
Virtio & transport
crates/vm-virtio/src/...
Update MemoryContainer import path across virtio codebase (virtqueue, transport, pci handlers, mmio handlers, etc.).

Sequence Diagram

sequenceDiagram
    participant Caller
    participant MAS as MemoryAddressSpace
    participant MR as MemoryRegion
    participant MC as MemoryContainer

    Note over Caller,MAS: New multi-region copy/memset flow
    Caller->>MAS: copy_from_slice(gpa, buf)
    loop per-region until buf exhausted
        MAS->>MAS: get_by_gpa(gpa)
        MAS->>MR: request hva()
        MR->>MC: delegate hva()
        MC-->>MR: *mut u8
        MR-->>MAS: *mut u8
        MAS->>MR: request len()
        MR->>MC: delegate length()
        MC-->>MR: usize
        MR-->>MAS: usize
        MAS->>MAS: memcpy within region boundary
        MAS->>MAS: advance gpa and buf offset
    end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~50 minutes

Possibly related PRs

Poem

🐰 I hopped through crates with nimble paws,

Renamed my steps and measured my cause,
Regions now stretch and copy with care,
Pointers and lengths all tidy and fair,
A little refactor — a rabbit's applause!

🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 26.92% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Title check ❓ Inconclusive The title 'Minor refine vm-mm' is vague and uses generic phrasing that doesn't convey specific information about the changeset's core intent. Provide a more specific title that clearly describes the main refactoring, such as 'Refactor: Move MemoryContainer trait to dedicated module' or 'Refactor: Extract MemoryContainer trait and update memory region API'.
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch refine

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
crates/vm-mm/src/manager.rs (1)

45-78: Two-pass approach in memset is intentional but consider documenting the rationale.

The implementation uses two loops: first to validate all regions exist, then to perform the memset. This provides "all-or-nothing" semantics (no partial writes on failure), which is a good safety property. Consider adding a brief comment explaining this design choice.

💡 Suggested documentation
     pub fn memset(&self, mut gpa: u64, val: u8, len: usize) -> Result<(), Error> {
+        // First pass: validate all target regions exist (fail-fast, no partial writes)
         let mut check_gpa = gpa;
         let mut remaining = len;
 
         while remaining > 0 {
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@crates/vm-mm/src/manager.rs` around lines 45 - 78, Add a short explanatory
comment at the top of the memset function describing the intentional two-pass
design: the first loop calls try_get_region_by_gpa to validate that all GPA
ranges are backed by regions (so the whole operation will fail fast and avoid
partial writes), and the second loop performs the actual unsafe write via
region.hva().add(...).write_bytes(...) once validation succeeds; reference the
memset function and the helper try_get_region_by_gpa and note the
“all-or-nothing” semantics and safety rationale for doing validation before
writing.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@crates/vm-mm/src/manager.rs`:
- Around line 7-8: Update the top-level doc comment in manager.rs to remove the
outdated claim that the memory manager "does not support multiple regions yet"
and instead describe that multi-region operations are supported (specifically
for methods like memset and copy_from_slice on the memory manager type); edit
the docstring above the MemoryManager (or the struct/impl described) to mention
support for multi-region memset/copy_from_slice and briefly note that the
implementation uses a BTreeMap to manage regions.
- Line 80: The API changed: copy_from_slice(&self, gpa: u64, buf: &[u8]) now
takes only a slice, so update all callers (e.g., calls from
vm-bootloader::kernel_loader::linux::bzimage and vm-machine::firmware::bios)
that still pass a third length argument to pass an appropriately sized slice
instead of a length: replace calls like copy_from_slice(gpa, buf, len) with
copy_from_slice(gpa, &buf[..len]) (or &buf[..2], &initrd[..initrd_len],
bios_bin.as_slice(), etc.), ensuring you create a subslice when the previous
code passed an explicit length so the semantics remain identical.

---

Nitpick comments:
In `@crates/vm-mm/src/manager.rs`:
- Around line 45-78: Add a short explanatory comment at the top of the memset
function describing the intentional two-pass design: the first loop calls
try_get_region_by_gpa to validate that all GPA ranges are backed by regions (so
the whole operation will fail fast and avoid partial writes), and the second
loop performs the actual unsafe write via region.hva().add(...).write_bytes(...)
once validation succeeds; reference the memset function and the helper
try_get_region_by_gpa and note the “all-or-nothing” semantics and safety
rationale for doing validation before writing.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c421433b-b4d8-4b9d-aa8a-cb667576c560

📥 Commits

Reviewing files that changed from the base of the PR and between 9392015 and bd4cbcf.

📒 Files selected for processing (30)
  • crates/vm-bootloader/src/boot_loader/arch/aarch64.rs
  • crates/vm-bootloader/src/initrd_loader.rs
  • crates/vm-bootloader/src/kernel_loader/linux/bzimage.rs
  • crates/vm-bootloader/src/kernel_loader/linux/image.rs
  • crates/vm-core/src/virt.rs
  • crates/vm-core/src/virt/hvp.rs
  • crates/vm-core/src/virt/hvp/mm.rs
  • crates/vm-device/src/device/virtio/virtio_balloon_traditional.rs
  • crates/vm-device/src/device/virtio/virtio_blk.rs
  • crates/vm-device/src/device/virtio/virtio_mmio_kbd.rs
  • crates/vm-machine/src/device.rs
  • crates/vm-machine/src/firmware/bios.rs
  • crates/vm-mm/src/allocator.rs
  • crates/vm-mm/src/allocator/mmap_allocator.rs
  • crates/vm-mm/src/error.rs
  • crates/vm-mm/src/lib.rs
  • crates/vm-mm/src/manager.rs
  • crates/vm-mm/src/memory_container.rs
  • crates/vm-mm/src/region.rs
  • crates/vm-virtio/src/device/virtqueue.rs
  • crates/vm-virtio/src/transport.rs
  • crates/vm-virtio/src/transport/mmio.rs
  • crates/vm-virtio/src/transport/mmio/mmio_handler.rs
  • crates/vm-virtio/src/transport/pci.rs
  • crates/vm-virtio/src/transport/pci/common_config_handler.rs
  • crates/vm-virtio/src/transport/pci/device_handler.rs
  • crates/vm-virtio/src/transport/pci/isr_handler.rs
  • crates/vm-virtio/src/transport/pci/notify_handler.rs
  • crates/vm-virtio/src/virtqueue.rs
  • crates/vm-virtio/src/virtqueue/virtq_desc_table.rs
💤 Files with no reviewable changes (1)
  • crates/vm-mm/src/error.rs

Comment on lines +7 to +8
/// A trivial memory manager, it does not support multiple regions yet
/// in spite of using BTreeMap.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Doc comment is outdated.

The comment states "does not support multiple regions yet" but the implementation now handles multi-region memset and copy_from_slice operations. Please update the documentation to reflect the current capabilities.

📝 Suggested fix
-/// A trivial memory manager, it does not support multiple regions yet
-/// in spite of using BTreeMap.
+/// A memory manager supporting multiple non-overlapping regions.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
/// A trivial memory manager, it does not support multiple regions yet
/// in spite of using BTreeMap.
/// A memory manager supporting multiple non-overlapping regions.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@crates/vm-mm/src/manager.rs` around lines 7 - 8, Update the top-level doc
comment in manager.rs to remove the outdated claim that the memory manager "does
not support multiple regions yet" and instead describe that multi-region
operations are supported (specifically for methods like memset and
copy_from_slice on the memory manager type); edit the docstring above the
MemoryManager (or the struct/impl described) to mention support for multi-region
memset/copy_from_slice and briefly note that the implementation uses a BTreeMap
to manage regions.

@junyu0312 junyu0312 merged commit c3f9c83 into main Mar 11, 2026
7 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant