Skip to content

[VFIO] Chapter 3: Section: 3 Hot-plug/unplug - #6356

Open
ShadowCurse wants to merge 7 commits into
firecracker-microvm:feature/vfiofrom
ShadowCurse:vfio_part_3_3
Open

ShadowCurse wants to merge 7 commits into
firecracker-microvm:feature/vfiofrom
ShadowCurse:vfio_part_3_3

Conversation

@ShadowCurse

Copy link
Copy Markdown
Contributor

Changes

Add support for the VFO hot-plug/unplug.

Additionally do some refactoring of the PciDevices and DeviceManager to bring order to both old virtio and new vfio hot-plug/unplug code.

Reason

Part of #5679

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 checkbuild --all to verify that the PR passes
    build checks on all supported architectures.
  • I have run tools/devtool checkstyle --no-clippy 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.

@codecov

codecov Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 70.27864% with 96 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.61%. Comparing base (500efc6) to head (f5b069f).

⚠️ Current head f5b069f differs from pull request most recent head 885604a

Please upload reports for the commit 885604a to get more accurate results.

Files with missing lines Patch % Lines
src/vmm/src/device_manager/pci_mngr.rs 59.50% 66 Missing ⚠️
src/vmm/src/device_manager/mod.rs 84.88% 13 Missing ⚠️
src/vmm/src/rpc_interface.rs 0.00% 12 Missing ⚠️
src/vmm/src/lib.rs 90.90% 3 Missing ⚠️
src/firecracker/src/api_server/parsed_request.rs 0.00% 2 Missing ⚠️
Additional details and impacted files
@@               Coverage Diff                @@
##           feature/vfio    #6356      +/-   ##
================================================
- Coverage         82.72%   82.61%   -0.11%     
================================================
  Files               284      284              
  Lines             33815    34002     +187     
================================================
+ Hits              27972    28090     +118     
- Misses             5843     5912      +69     
Flag Coverage Δ
5.10-m5n.metal 82.66% <70.27%> (-0.11%) ⬇️
5.10-m6a.metal 82.06% <70.27%> (-0.12%) ⬇️
5.10-m6g.metal 79.86% <70.18%> (-0.06%) ⬇️
5.10-m6i.metal 82.66% <70.27%> (-0.12%) ⬇️
5.10-m7a.metal-48xl 82.05% <70.27%> (-0.12%) ⬇️
5.10-m7g.metal 79.86% <70.18%> (-0.06%) ⬇️
5.10-m7i.metal-24xl 82.64% <70.27%> (-0.10%) ⬇️
5.10-m7i.metal-48xl 82.64% <70.27%> (-0.12%) ⬇️
5.10-m8g.metal-24xl 79.86% <70.18%> (-0.06%) ⬇️
5.10-m8g.metal-48xl 79.86% <70.18%> (-0.06%) ⬇️
5.10-m8i.metal-48xl 82.63% <70.27%> (-0.12%) ⬇️
5.10-m8i.metal-96xl 82.63% <70.27%> (-0.12%) ⬇️
5.10-m9g.metal-48xl 79.86% <70.18%> (-0.06%) ⬇️
6.1-m5n.metal 82.78% <70.27%> (-0.10%) ⬇️
6.1-m6a.metal 82.18% <70.27%> (-0.12%) ⬇️
6.1-m6g.metal 79.85% <70.18%> (-0.06%) ⬇️
6.1-m6i.metal 82.78% <70.27%> (-0.12%) ⬇️
6.1-m7a.metal-48xl 82.17% <70.27%> (-0.13%) ⬇️
6.1-m7g.metal 79.85% <70.18%> (-0.06%) ⬇️
6.1-m7i.metal-24xl 82.79% <70.27%> (-0.11%) ⬇️
6.1-m7i.metal-48xl 82.79% <70.27%> (-0.12%) ⬇️
6.1-m8g.metal-24xl 79.85% <70.18%> (-0.06%) ⬇️
6.1-m8g.metal-48xl 79.85% <70.18%> (-0.06%) ⬇️
6.1-m8i.metal-48xl 82.79% <70.27%> (-0.12%) ⬇️
6.1-m8i.metal-96xl 82.79% <70.27%> (-0.12%) ⬇️
6.1-m9g.metal-48xl 79.85% <70.18%> (-0.06%) ⬇️
6.18-m5n.metal 82.78% <70.27%> (-0.10%) ⬇️
6.18-m6a.metal 82.19% <70.27%> (-0.12%) ⬇️
6.18-m6g.metal 79.95% <70.18%> (-0.06%) ⬇️
6.18-m6i.metal 82.78% <70.27%> (-0.12%) ⬇️
6.18-m7a.metal-48xl 82.17% <70.27%> (-0.12%) ⬇️
6.18-m7g.metal 79.95% <70.18%> (-0.06%) ⬇️
6.18-m7i.metal-24xl 82.79% <70.27%> (-0.12%) ⬇️
6.18-m7i.metal-48xl 82.79% <70.27%> (-0.13%) ⬇️
6.18-m8g.metal-24xl 79.95% <70.18%> (-0.06%) ⬇️
6.18-m8g.metal-48xl 79.95% <70.18%> (-0.06%) ⬇️
6.18-m8i.metal-48xl 82.79% <70.27%> (-0.12%) ⬇️
6.18-m8i.metal-96xl 82.79% <70.27%> (-0.12%) ⬇️
6.18-m9g.metal-48xl 79.95% <70.18%> (-0.06%) ⬇️

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

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

For some reason `cargo fmt` from devctr does not re-flow these lines,
but `cargo fmt` on the host does.

Signed-off-by: Egor Lazarchuk <yegorlz@amazon.co.uk>
PCIe hot-plug needs an ACPI guest kernel, like the PCI hot-plug tests
do, so pin the VFIO tests which will also run with hot-plugged devices.
Actual tests will be added in the next commit.

Signed-off-by: Egor Lazarchuk <yegorlz@amazon.co.uk>
Wire up the code to allow hot-plugging of passthrough devices after VM
boot. The API is same as for usual passthrough device addition. The
device is placed behind a free PCIe root port and the guest is notified
about it, so no manual bus rescan is needed. Adding devices with
duplicated ids or host SBDFs is disallowed.

Signed-off-by: Egor Lazarchuk <yegorlz@amazon.co.uk>
With VFIO device hot-plug support we need to add all syscalls needed for
VFIO devices creation to the VMM thread.

Signed-off-by: Egor Lazarchuk <yegorlz@amazon.co.uk>
Next commit will add VFIO hot-unplug support and we neet to take care of
the KVM and VFIO container when last VFIO device/group is removed.

When the last VFIO group is detached from the container, the kernel
drops its DMA mappings and the container returns to the unset state.
This means that on the next hot-plug we will need to map the guest
memory again.

The reason we cannot just drop the `Container` with the last device is
that KVM allows only a single KVM_DEV_TYPE_VFIO device per VM and on
5.10 host kernel, dropping `Container` (which contains kvm vfio device)
does not destroy this device. This causes consecutive hot-plug requests
to fail since KVM will reject creating of a new kvm vfio device.

Signed-off-by: Egor Lazarchuk <yegorlz@amazon.co.uk>
Implement passthrough device removal and wire it up to the DELETE API.
Like for virtio devices, only devices behind PCIe root ports can be
removed, either gracefully after the guest acknowledges the removal or
forcefully when the `force` flag is set.

Once the last passthrough device is removed, the kernel releases its DMA
mappings of the guest memory when the last group is detached from it, so
a following hot-plug will DMA map guest memory again.

Forced removal resets the device and unpins the guest memory, which
can take over a second on some hosts, so exclude passthrough device API
calls from the test framework API call duration check.

Signed-off-by: Egor Lazarchuk <yegorlz@amazon.co.uk>
Passthrough and vhost-user devices write guest memory without going
through KVM dirty tracking or the Firecracker dirty bitmap. While they
are attached snapshots are refused, but once a hot-plugged device is
removed a diff snapshot would miss the pages it wrote, silently
corrupting the guest memory on restore.

Refuse diff snapshots after such a device was hot-plugged until a full
snapshot succeeds and captures all of the guest memory.

Signed-off-by: Egor Lazarchuk <yegorlz@amazon.co.uk>
@ShadowCurse
ShadowCurse marked this pull request as ready for review November 5, 2026 16:49
@ShadowCurse ShadowCurse added the Status: Awaiting review Indicates that a pull request is ready to be reviewed label Nov 5, 2026
@ShadowCurse ShadowCurse mentioned this pull request Nov 5, 2026
11 tasks
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.

1 participant