Skip to content

Conversation

ShadowCurse
Copy link
Contributor

Changes

Add virtio-pmem device support.

Closes #5448

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 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.

@ShadowCurse ShadowCurse self-assigned this Oct 3, 2025
Copy link

codecov bot commented Oct 3, 2025

Codecov Report

❌ Patch coverage is 72.97297% with 140 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.62%. Comparing base (f335a0a) to head (7d83503).
⚠️ Report is 3 commits behind head on main.

⚠️ Current head 7d83503 differs from pull request most recent head 9a554b4

Please upload reports for the commit 9a554b4 to get more accurate results.

Files with missing lines Patch % Lines
src/vmm/src/devices/virtio/pmem/device.rs 67.67% 64 Missing ⚠️
src/vmm/src/devices/virtio/pmem/event_handler.rs 23.07% 40 Missing ⚠️
src/vmm/src/builder.rs 77.14% 8 Missing ⚠️
src/vmm/src/rpc_interface.rs 0.00% 8 Missing ⚠️
src/firecracker/src/api_server/request/pmem.rs 77.27% 5 Missing ⚠️
src/vmm/src/devices/virtio/pmem/metrics.rs 85.29% 5 Missing ⚠️
src/vmm/src/resources.rs 58.33% 5 Missing ⚠️
src/vmm/src/vmm_config/pmem.rs 93.33% 3 Missing ⚠️
src/vmm/src/device_manager/persist.rs 96.55% 1 Missing ⚠️
src/vmm/src/devices/virtio/pmem/persist.rs 95.23% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5463      +/-   ##
==========================================
- Coverage   82.79%   82.62%   -0.17%     
==========================================
  Files         263      269       +6     
  Lines       27226    27729     +503     
==========================================
+ Hits        22541    22911     +370     
- Misses       4685     4818     +133     
Flag Coverage Δ
5.10-m5n.metal 82.77% <72.97%> (-0.20%) ⬇️
5.10-m6a.metal 82.02% <72.97%> (-0.20%) ⬇️
5.10-m6g.metal 79.43% <72.97%> (-0.15%) ⬇️
5.10-m6i.metal 82.76% <72.97%> (-0.22%) ⬇️
5.10-m7a.metal-48xl 82.01% <72.97%> (-0.20%) ⬇️
5.10-m7g.metal 79.44% <72.97%> (-0.14%) ⬇️
5.10-m7i.metal-24xl 82.74% <72.97%> (-0.21%) ⬇️
5.10-m7i.metal-48xl 82.74% <72.97%> (-0.20%) ⬇️
5.10-m8g.metal-24xl 79.43% <72.97%> (-0.14%) ⬇️
5.10-m8g.metal-48xl 79.43% <72.97%> (-0.15%) ⬇️
6.1-m5n.metal 82.79% <72.97%> (-0.21%) ⬇️
6.1-m6a.metal 82.05% <72.97%> (-0.20%) ⬇️
6.1-m6g.metal 79.43% <72.97%> (-0.15%) ⬇️
6.1-m6i.metal 82.80% <72.97%> (-0.21%) ⬇️
6.1-m7a.metal-48xl 82.04% <72.97%> (-0.20%) ⬇️
6.1-m7g.metal 79.44% <72.97%> (-0.14%) ⬇️
6.1-m7i.metal-24xl 82.81% <72.97%> (-0.20%) ⬇️
6.1-m7i.metal-48xl 82.81% <72.97%> (-0.20%) ⬇️
6.1-m8g.metal-24xl 79.43% <72.97%> (-0.14%) ⬇️
6.1-m8g.metal-48xl 79.44% <72.97%> (-0.14%) ⬇️

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.

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

@ShadowCurse ShadowCurse force-pushed the virtio_pmem branch 2 times, most recently from d8f9547 to 5970613 Compare October 6, 2025 11:01
@ShadowCurse ShadowCurse marked this pull request as ready for review October 6, 2025 12:57
@ShadowCurse ShadowCurse added Status: Awaiting review Indicates that a pull request is ready to be reviewed Type: Enhancement Indicates new feature requests labels Oct 6, 2025
@ShadowCurse ShadowCurse force-pushed the virtio_pmem branch 2 times, most recently from a8bedbb to 1d2aeb2 Compare October 6, 2025 14:18
msync is used by virtio-pmem device to trigger sync of mmaped
file content to the underlying file.

Signed-off-by: Egor Lazarchuk <yegorlz@amazon.co.uk>
Copy link
Contributor

@Manciukic Manciukic left a comment

Choose a reason for hiding this comment

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

  • we should update docs/device-api.md.
  • changelog entry
  • any performance tests? we could check how fast we can read or write the entire pmem or maybe we can integrate it with the block tests using fio

libc::MS_SYNC,
) < 0
{
result = FAILURE;
Copy link
Contributor

Choose a reason for hiding this comment

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

we should log the error

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added

fn interrupt_trigger(&self) -> &dyn VirtioInterrupt {
self.device_state
.active_state()
.expect("Device is not implemented")
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: "is not active"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

}
}

fn write_config(&mut self, _offset: u64, _data: &[u8]) {}
Copy link
Contributor

Choose a reason for hiding this comment

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

we could log unexpected attempts to write

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we don't log such writes in any device, so don't think we should do it here.

Comment on lines 168 to 174
let next = self.common.next_kvm_slot.load(Ordering::Relaxed);
if next == self.common.max_memslots {
None
} else {
self.common.next_kvm_slot.store(next + 1, Ordering::Relaxed);
Some(next)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

to really be atomic we should do a fetch_add.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

pmem.avail_features = state.virtio_state.avail_features;
pmem.acked_features = state.virtio_state.acked_features;

pmem.set_mem_region(constructor_args.vm)?;
Copy link
Contributor

Choose a reason for hiding this comment

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

is the allocator state getting restored?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes

docs/pmem.md Outdated
"id": "pmem0",
"path_on_host": "./some_file",
"root_device": true,
"read_only": fasle
Copy link
Contributor

Choose a reason for hiding this comment

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

-      "read_only": fasle
+      "read_only": false

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

\"root_device\": true,
\"read_only\": false
}"
```
Copy link
Contributor

Choose a reason for hiding this comment

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

we should probably mention snapshot/restore behaviour as well

and also security considerations about sharing memory (which we do not recommend).

We can also mention performance considerations: ie that even though pages are in memory, the guest still needs to exit to the kernel to set up the pagetable mappings. Using hugetlbfs to back the file would be faster (but will consume memory).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added snapshot, security and performance sections

}
let mmap_len = align_up(file_len, Self::ALIGNMENT);

let mut flags_1 = libc::MAP_SHARED | libc::MAP_ANONYMOUS | libc::MAP_NORESERVE;
Copy link
Contributor

Choose a reason for hiding this comment

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

PRIVATE?

nit: having the flags inline with the syscall would make it more readable

assert False


def test_pmem_add(uvm_plain_any, microvm_factory):
Copy link
Contributor

Choose a reason for hiding this comment

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

should we check writes are persisted to disk?

Comment on lines +32 to +33
> `DAX` support is not uniform for all file systems. Check the documentation for
> the file system you want to use before enabling `DAX`.
Copy link
Contributor

Choose a reason for hiding this comment

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

it works on ext4, right? does it need any specific options (ie 4096 block size) or just works?

Add implementations of device, event handling, metrics.
Add device config and builder types for API use.

Signed-off-by: Egor Lazarchuk <yegorlz@amazon.co.uk>
Update VmResources type with virtio-pmem configuration
field to allow virtio-pmem devices be configured
through config files and later through API calls.

Signed-off-by: Egor Lazarchuk <yegorlz@amazon.co.uk>
Both virtio-block and virtio-pmem can act as root devices
for a VM. Add a check to prevent specifing more than 1 root
device for a VM.

Signed-off-by: Egor Lazarchuk <yegorlz@amazon.co.uk>
Add /pmem/id PUT request for virtio-pmem configuration.
Add corresponding metrics.

Signed-off-by: Egor Lazarchuk <yegorlz@amazon.co.uk>
Virtio-pmem devices need to allocate a memory region in guest physical
memory. The safe place to do this is past 64bit MMIO region.

Signed-off-by: Egor Lazarchuk <yegorlz@amazon.co.uk>
Add a counter for KVM slot ids into VmCommon struct. This is done
because virtio-pmem device needs to obtain it's KVM slot id
independently from number of slots in GuestMemoryMmap.

Signed-off-by: Egor Lazarchuk <yegorlz@amazon.co.uk>
Add methods to attach virtio-pmem devices to Vmm.
Add methods to create KVM memory slot for virtio-pmem devices.

Signed-off-by: Egor Lazarchuk <yegorlz@amazon.co.uk>
Add logic to store and restore virtio-pmem device information
in a snapshot.

Signed-off-by: Egor Lazarchuk <yegorlz@amazon.co.uk>
Add functional and API tests for virtio-pmem device
and its configuration fields

Signed-off-by: Egor Lazarchuk <yegorlz@amazon.co.uk>
Expose virtio-pmem metrics in the logger, so they are exported in
metrics.json.
Update integration tests to expect new metrics.

Signed-off-by: Egor Lazarchuk <yegorlz@amazon.co.uk>
Add description of pmem endpoint.

Signed-off-by: Egor Lazarchuk <yegorlz@amazon.co.uk>
Add new document about virtio-pmem configuration and usage.

Signed-off-by: Egor Lazarchuk <yegorlz@amazon.co.uk>
Add a note about addition of virtio-pmem device.

Signed-off-by: Egor Lazarchuk <yegorlz@amazon.co.uk>
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 Type: Enhancement Indicates new feature requests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[virtio-pmem] Add support for virtio-pmem device
2 participants