Skip to content

chore: Update to kvm-bindings 0.8.0 #4581

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

Merged
merged 4 commits into from
Apr 30, 2024

Conversation

roypat
Copy link
Contributor

@roypat roypat commented Apr 25, 2024

Changes

The goal of this PR is to update kvm-bindings from our custom fork to upstream version 0.8.0, as since rust-vmm/kvm-bindings#101 the serialization support we had our fork for is merged upstream.

Additionally, refactor our emulation code to be more easily unit-testable.

Reason

The refactors are necessary due to a change in kvm-ioctls. The self parameter to VcpuFd::run is now a mutable reference. Please see the final commit for a detailed breakdown of why this was incompatible with our existing mocking-based approach to testing vmexits.

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

  • If a specific issue led to this PR, this PR closes the issue.
  • The description of changes is clear and encompassing.
  • Any required documentation changes (code and docs) are included in this
    PR.
  • API changes follow the Runbook for Firecracker API changes.
  • User-facing changes are mentioned in CHANGELOG.md.
  • All added/changed functionality is tested.
  • New TODOs link to an issue.
  • Commits meet
    contribution quality standards.

  • This functionality cannot be added in rust-vmm.

Copy link

codecov bot commented Apr 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 82.14%. Comparing base (4a28b9c) to head (b50f51c).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4581   +/-   ##
=======================================
  Coverage   82.13%   82.14%           
=======================================
  Files         255      255           
  Lines       31274    31277    +3     
=======================================
+ Hits        25688    25691    +3     
  Misses       5586     5586           
Flag Coverage Δ
4.14-c5n.metal 79.63% <100.00%> (+<0.01%) ⬆️
4.14-c7g.metal ?
4.14-m5n.metal 79.62% <100.00%> (+<0.01%) ⬆️
4.14-m6a.metal 78.84% <100.00%> (-0.01%) ⬇️
4.14-m6g.metal 76.69% <98.71%> (-0.01%) ⬇️
4.14-m6i.metal 79.62% <100.00%> (+<0.01%) ⬆️
4.14-m7g.metal 76.69% <98.71%> (-0.01%) ⬇️
5.10-c5n.metal 82.15% <100.00%> (+<0.01%) ⬆️
5.10-m5n.metal 82.14% <100.00%> (+<0.01%) ⬆️
5.10-m6a.metal 81.44% <100.00%> (-0.01%) ⬇️
5.10-m6g.metal 79.46% <98.71%> (-0.01%) ⬇️
5.10-m6i.metal 82.13% <100.00%> (+<0.01%) ⬆️
5.10-m7g.metal 79.46% <98.71%> (-0.01%) ⬇️
6.1-c5n.metal 82.15% <100.00%> (+<0.01%) ⬆️
6.1-c7g.metal ?
6.1-m5n.metal 82.14% <100.00%> (+<0.01%) ⬆️
6.1-m6a.metal 81.44% <100.00%> (-0.01%) ⬇️
6.1-m6g.metal 79.46% <98.71%> (+<0.01%) ⬆️
6.1-m6i.metal 82.13% <100.00%> (+<0.01%) ⬆️
6.1-m7g.metal 79.46% <98.71%> (-0.01%) ⬇️

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.

@roypat roypat force-pushed the kvm-bindings-0.8.0-again branch 3 times, most recently from 60ab983 to 6e6b4e6 Compare April 25, 2024 15:55
@roypat roypat added the Status: Awaiting review Indicates that a pull request is ready to be reviewed label Apr 26, 2024
@roypat roypat requested a review from ShadowCurse April 26, 2024 07:56
@roypat roypat force-pushed the kvm-bindings-0.8.0-again branch from 73c5678 to 01daf91 Compare April 26, 2024 13:45
@roypat roypat requested a review from zulinx86 April 29, 2024 08:47
Copy link
Contributor

@zulinx86 zulinx86 left a comment

Choose a reason for hiding this comment

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

looks good to me overall other than tiny nitpicking!

roypat and others added 3 commits April 30, 2024 15:52
Separate the call to `KVM_RUN` from the handling of the result value.
This makes the handling of the `VcpuExit` unit-testable without needing
to hack in `cfg(not(test))` code that compiles out the `KVM_RUN` call at
compile time.

For this, separate the parts of KvmVcpu that the exit handler needs
mutable access to into a `Peripherals` structure, which is passed to
`handle_kvm_exit`. This is needed so that we can shared the `&mut` to
`kvm_vcpu` into `fd` and "the rest".

Co-Authored-By: Egor Lazarchuk <[email protected]>
Signed-off-by: Patrick Roy <[email protected]>
Upgrade kvm-bindings to 0.8.0 to make use of upstream serialization
support.

New kvm-bindings no longer derives `Clone` on `kvm_xsave` (since this is
not correct to do for all use cases post Linux 5.17, due to the
structure now having a flexible array members). While Firecracker does
not make use of this flexible array members (according to KVM
documentation, it will only be present if `arch_prctl` is used [1]),
meaning our usage of `Clone` was not problematic, it's easier for now to
just clean up our use-sites of `kvm_xsave::clone` (which was only used
in test code)  - turns out they were either dead code, or should be
replaced with successive calls to `save_state()`.

Additionally, `VcpuFd::run` now takes `self` by mutable reference, which
is the reason for the preceding refactors. The result of `run` holds
references into the `kvm_run` structure contained by `VcpuFd`, meaning
the mutable borrow on `Vcpu::fd` will stay alive until the return value
is dropped. However, if the call to `run` is behind a function call that
takes the entirety of `Vcpu` by mutable reference, this means that we
cannot borrow any other fields of `Vcpu` mutably until we drop the
`VcpuExit` - yet we need to do exactly this to actually handle the
KVM_EXIT. So, instead inline the call to `run` so that the borrow
checker understand we only borrow `Vcpu::fd` instead of the entire
`Vcpu` object, meaning it allows us to access the _other_ fields of the
`Vcpu` struct during handling.

[1]: https://docs.kernel.org/virt/kvm/api.html#kvm-get-xsave2

Signed-off-by: Patrick Roy <[email protected]>
Delete various error variants in the x86_64 version of KvmVcpuError that
were never constructed/used.

Signed-off-by: Patrick Roy <[email protected]>
@roypat roypat force-pushed the kvm-bindings-0.8.0-again branch from 30b343b to 7d49849 Compare April 30, 2024 14:52
@roypat roypat merged commit d2ce649 into firecracker-microvm:main Apr 30, 2024
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Awaiting review Indicates that a pull request is ready to be reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants