Skip to content
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

refactor: move arch specific code from vm.rs into modules #5031

Merged
merged 12 commits into from
Feb 21, 2025

Conversation

roypat
Copy link
Contributor

@roypat roypat commented Feb 7, 2025

Follow a similar approach we already take for the vcpu module, where all
the architecture specific stuff is inside aarch64/x86_64 modules,
keeping only the common code in the vm module.

No functional change intended.

Signed-off-by: Patrick Roy [email protected]


Some more unmotivated-at-face-value code I had lying around, which I wrote while messing around for #4522, but which constitutes an improvement standalone anyway (at least imo it makes the code a bit easier to read due to less cfg-ing)

License Acceptance

By submitting this pull request, I confirm that my contribution is made under
the terms of the Apache 2.0 license. For more information on following Developer
Certificate of Origin and signing off your commits, please check
CONTRIBUTING.md.

PR Checklist

  • I have read and understand CONTRIBUTING.md.
  • I have run tools/devtool checkstyle to verify that the PR passes the
    automated style checks.
  • I have described what is done in these changes, why they are needed, and
    how they are solving the problem in a clear and encompassing way.
  • I have updated any relevant documentation (both in code and in the docs)
    in the PR.
  • I have mentioned all user-facing changes in CHANGELOG.md.
  • If a specific issue led to this PR, this PR closes the issue.
  • When making API changes, I have followed the
    Runbook for Firecracker API changes.
  • I have tested all new and changed functionalities in unit tests and/or
    integration tests.
  • I have linked an issue to every new TODO.

  • This functionality cannot be added in rust-vmm.

Follow a similar approach we already take for the vcpu module, where all
the architecture specific stuff is inside aarch64/x86_64 modules,
keeping only the common code in the vm module.

No functional change intended.

Signed-off-by: Patrick Roy [email protected]

@roypat roypat requested review from ShadowCurse, bchalios and kalyazin and removed request for ShadowCurse February 7, 2025 17:20
@roypat roypat force-pushed the vm-refactor-pr branch 4 times, most recently from 21779af to 4e0dca0 Compare February 13, 2025 17:35
Copy link

codecov bot commented Feb 13, 2025

Codecov Report

Attention: Patch coverage is 93.75000% with 12 lines in your changes missing coverage. Please review.

Project coverage is 83.20%. Comparing base (51bbc77) to head (ecdc8c8).
Report is 11 commits behind head on main.

Files with missing lines Patch % Lines
src/vmm/src/vstate/vm/x86_64.rs 89.01% 10 Missing ⚠️
src/vmm/src/builder.rs 90.00% 1 Missing ⚠️
src/vmm/src/vstate/vm/aarch64.rs 97.50% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5031      +/-   ##
==========================================
+ Coverage   83.16%   83.20%   +0.04%     
==========================================
  Files         245      247       +2     
  Lines       26645    26639       -6     
==========================================
+ Hits        22159    22166       +7     
+ Misses       4486     4473      -13     
Flag Coverage Δ
5.10-c5n.metal 83.69% <92.71%> (+0.04%) ⬆️
5.10-m5n.metal 83.67% <92.71%> (+0.05%) ⬆️
5.10-m6a.metal 82.88% <92.71%> (+0.05%) ⬆️
5.10-m6g.metal 79.67% <96.80%> (+0.08%) ⬆️
5.10-m6i.metal 83.67% <92.71%> (+0.05%) ⬆️
5.10-m7g.metal 79.67% <96.80%> (+0.08%) ⬆️
6.1-c5n.metal 83.69% <92.71%> (+0.04%) ⬆️
6.1-m5n.metal 83.67% <92.71%> (+0.03%) ⬆️
6.1-m6a.metal 82.88% <92.71%> (+0.05%) ⬆️
6.1-m6g.metal 79.67% <96.80%> (+0.08%) ⬆️
6.1-m6i.metal 83.66% <92.71%> (+0.03%) ⬆️
6.1-m7g.metal 79.67% <96.80%> (+0.08%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

bchalios
bchalios previously approved these changes Feb 18, 2025
ShadowCurse
ShadowCurse previously approved these changes Feb 18, 2025
Follow a similar approach we already take for the vcpu module, where all
the architecture specific stuff is inside aarch64/x86_64 modules,
keeping only the common code in the `vm` module.

No functional change intended.

Signed-off-by: Patrick Roy <[email protected]>
Introduce `enum ArchVmError` which contains architecture specific
variants for VM operations (mostly related to snapshot
creation/restore). Do this by adapting exiting architecture specific
enums that were used for snapshot restoration (x86_64)/GIC creation
(aarch64).

While we're at it, remove unused/duplicated enum variants, and on x86
use the SetIrqChip enum variant for failures inside `setup_irq_chip`
instead of producing some generic `VmError`.

Admittedly, there's a lot more cleanup that can be done here, but for
now this is enough to attain my goals (separating architecture specific
code).

No functional change intended (apart from error messages changing).

Signed-off-by: Patrick Roy <[email protected]>
Similarly to what we do in the vcpu module for the Vcpu/KvmVcpu
structs. The difference is that we do not implement any function on
`ArchVm`, so that common fields like `fd` can live in `Vm`, instead of
needing to be duplicated into all the `ArchVm` types.

Signed-off-by: Patrick Roy <[email protected]>
This allows us to not need to feed `struct Kvm` all the way down to Vcpu
creation.

While we're at it, abstract away the `MsrList` type, and just have a
helper that returns a slice of u32 (as every call site converts the
MsrList to this anyway).

Signed-off-by: Patrick Roy <[email protected]>
This abstracts away the weird irqchip setup dance that builder.rs has
been doing (where depending on architecture, irqchip needs to be created
before or after vcpu creation. Removes a whole bunch of code that
builder.rs that didn't really have too much business being there

Signed-off-by: Patrick Roy <[email protected]>
Instead of doing the irqchip dance inside unit testing code, just call
vm.create_vcpus, which does it for us.

Signed-off-by: Patrick Roy <[email protected]>
We have some utility functions for creating dummy VMs/etc. in unittests,
so let's try to use them instead of copy-pasting the same few lines
around.

Signed-off-by: Patrick Roy <[email protected]>
There's no need to use the GuestMemoryMmap to resolve the pointer to the
start of the GuestMemoryRegion by resolving the guest physical address
it starts at - because we literally store that pointer inside the
GuestMemoryRegion, so can just use that.

Signed-off-by: Patrick Roy <[email protected]>
Since we issue this ioctl with a constant address, it doesn't really
matter _when_ we do it. Internally, it causes a hidden memslot of size 3
pages to be created. If we create it _after_ all other memslots, then
this ioctl can fail with EEXIST if it would overlap some pre-existing
memslot. Now, instead the creation of the overlapping memslot would fail
with EEXIST. But this is a moot point, because if Firecracker ever tried
to create a memslot that overlaps this one, something is fundamentally
broken.

Signed-off-by: Patrick Roy <[email protected]>
All of these were dead code (but the compiler isn't able to tell us
because the vmm crate is a library crate where everything is `pub`, and
it doesn't know that no downstream users outside of the `firecracker`
crate exist, and that everything that's not used in `firecracker` is
dead code).

Signed-off-by: Patrick Roy <[email protected]>
Dropping a GuestMemoryMmap causes the memory to get unmapped, so if we
try to KVM_RUN after dropping it, we'll just get EFAULT.

Admittedly no idea how we didn't run into this issue before.

Signed-off-by: Patrick Roy <[email protected]>
@roypat roypat dismissed stale reviews from bchalios and ShadowCurse via 8a5cf78 February 18, 2025 17:40
@roypat roypat requested a review from bchalios February 19, 2025 07:38
@roypat roypat merged commit 538702b into firecracker-microvm:main Feb 21, 2025
6 of 7 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.

3 participants