Skip to content

Pmem: Fix resource leaks#5855

Merged
ilstam merged 5 commits intofirecracker-microvm:mainfrom
ilstam:pmem-fix-leaks
Apr 29, 2026
Merged

Pmem: Fix resource leaks#5855
ilstam merged 5 commits intofirecracker-microvm:mainfrom
ilstam:pmem-fix-leaks

Conversation

@ilstam
Copy link
Copy Markdown
Contributor

@ilstam ilstam commented Apr 23, 2026

There are resource leaks in Pmem which are not a problem currently, but will be a problem once we have device hotplugging support.

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.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 23, 2026

Codecov Report

❌ Patch coverage is 91.72414% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.79%. Comparing base (f599f3b) to head (1fec14b).
⚠️ Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
src/vmm/src/devices/virtio/pmem/device.rs 91.89% 9 Missing ⚠️
src/vmm/src/builder.rs 85.71% 2 Missing ⚠️
src/vmm/src/devices/virtio/pmem/persist.rs 87.50% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5855      +/-   ##
==========================================
- Coverage   82.87%   82.79%   -0.09%     
==========================================
  Files         276      276              
  Lines       29728    29764      +36     
==========================================
+ Hits        24637    24643       +6     
- Misses       5091     5121      +30     
Flag Coverage Δ
5.10-m5n.metal 83.08% <91.72%> (-0.11%) ⬇️
5.10-m6a.metal 82.41% <91.72%> (-0.10%) ⬇️
5.10-m6g.metal 79.70% <91.72%> (-0.08%) ⬇️
5.10-m6i.metal 83.08% <91.72%> (-0.09%) ⬇️
5.10-m7a.metal-48xl 82.40% <91.72%> (-0.10%) ⬇️
5.10-m7g.metal 79.70% <91.72%> (-0.09%) ⬇️
5.10-m7i.metal-24xl 83.05% <91.72%> (-0.09%) ⬇️
5.10-m7i.metal-48xl 83.05% <91.72%> (-0.09%) ⬇️
5.10-m8g.metal-24xl 79.69% <91.72%> (-0.09%) ⬇️
5.10-m8g.metal-48xl 79.70% <91.72%> (-0.09%) ⬇️
5.10-m8i.metal-48xl 83.05% <91.72%> (-0.09%) ⬇️
5.10-m8i.metal-96xl 83.05% <91.72%> (-0.09%) ⬇️
6.1-m5n.metal 83.11% <91.72%> (-0.10%) ⬇️
6.1-m6a.metal 82.44% <91.72%> (-0.09%) ⬇️
6.1-m6g.metal 79.70% <91.72%> (-0.08%) ⬇️
6.1-m6i.metal 83.11% <91.72%> (-0.10%) ⬇️
6.1-m7a.metal-48xl 82.43% <91.72%> (-0.10%) ⬇️
6.1-m7g.metal 79.70% <91.72%> (-0.09%) ⬇️
6.1-m7i.metal-24xl 83.12% <91.72%> (-0.10%) ⬇️
6.1-m7i.metal-48xl 83.12% <91.72%> (-0.10%) ⬇️
6.1-m8g.metal-24xl 79.70% <91.72%> (-0.09%) ⬇️
6.1-m8g.metal-48xl 79.70% <91.72%> (-0.08%) ⬇️
6.1-m8i.metal-48xl 83.13% <91.72%> (-0.09%) ⬇️
6.1-m8i.metal-96xl 83.12% <91.72%> (-0.10%) ⬇️

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.

@ilstam ilstam added the Status: Awaiting review Indicates that a pull request is ready to be reviewed label Apr 23, 2026
Manciukic
Manciukic previously approved these changes Apr 24, 2026
Copy link
Copy Markdown
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.

LGTM, just a few non-blocking nitpicks

Comment thread src/vmm/src/devices/virtio/pmem/device.rs Outdated
Comment thread src/vmm/src/devices/virtio/pmem/device.rs Outdated
Comment thread src/vmm/src/devices/virtio/pmem/device.rs Outdated
Comment thread src/vmm/src/devices/virtio/pmem/device.rs
Comment thread src/vmm/src/devices/virtio/pmem/device.rs Outdated
Comment thread src/vmm/src/vmm_config/pmem.rs
Comment thread src/vmm/src/devices/virtio/pmem/device.rs Outdated
Comment thread src/vmm/src/devices/virtio/pmem/device.rs Outdated
Comment thread src/vmm/src/devices/virtio/pmem/device.rs Outdated
Comment thread src/vmm/src/devices/virtio/pmem/device.rs Outdated
Comment thread src/vmm/src/devices/virtio/pmem/device.rs Outdated
Comment thread src/vmm/src/devices/virtio/pmem/device.rs Outdated
Comment thread src/vmm/src/devices/virtio/pmem/device.rs Outdated
Manciukic
Manciukic previously approved these changes Apr 29, 2026
Copy link
Copy Markdown
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.

LGTM, just a possible minor issue about the drop ordering

Comment thread src/vmm/src/devices/virtio/pmem/device.rs Outdated
ShadowCurse
ShadowCurse previously approved these changes Apr 29, 2026
ilstam added 5 commits April 29, 2026 17:03
Pmem::alloc_region() panics if it fails to allocate the address range it
needs. That is especially problematic for hot-plugging support, since it
means failing to attach a device would kill the entire VM. Remove the
unwrap and introduce a new error code that is propagated to the caller
in case of failure.

Signed-off-by: Ilias Stamatis <ilstam@amazon.com>
Change PmemBuilder to store Vec<PmemConfig> instead of
Vec<Arc<Mutex<Pmem>>>. Created the actual Pmem device objects in
attach_pmem_devices() in builder.rs, matching the pattern already used
by memory_hotplug/virtio-mem. This means VmResources no longer holds
device objects for pmem, only configs.

This change will allows us to move the alloc_region() and
set_pmem_region() calls into the Pmem object constructor and use RAII in
a subsequent patch.

Since the backing file is no longer validated at config time, move the
zero-size file test from test_api.py to test_pmem.py where it now
asserts the error at boot time.

Signed-off-by: Ilias Stamatis <ilstam@amazon.com>
If EventFd allocation fails in new_with_queues() then the Pmem drop()
function never runs and the memory allocated with mmap() is never freed.

To fix this introduce a RAII PmemMmap struct that performs mmap in its
constructor and munmap in its Drop implementation.

Signed-off-by: Ilias Stamatis <ilstam@amazon.com>
Pass config_space and acked_features as arguments to new_with_queues()
instead of mutating the Pmem struct from the restore() caller. This
moves the state initialization into the constructor.

ConfigSpace is passed as an Option because in a subsequent patch we will
allocate the guest memory region if ConfigSpace is not set and make
alloc_region() non public.

avail_features is not passed because it is determined by the device
implementation and cannot be changed by the guest, so it is always
the same value regardless of whether we are creating a new device or
restoring from a snapshot.

Signed-off-by: Ilias Stamatis <ilstam@amazon.com>
The Pmem struct does not fully use RAII, it rather has separate
alloc_region() and set_mem_region() calls to allocate a guest memory
region and then allocate a KVM memory slot and configure it. Crucially
it never deallocates these resources anywhere which was not a problem
until now, but it will become a problem once we support device
hot-unplugging.

Introduce GuestPmemRegion and KvmMemSlot RAII structs that manage the
lifecycle of the GPA allocation and KVM user memory region respectively.
These replace the manual alloc_region() and set_mem_region() calls.
Both resources are now allocated inside new_with_queues(), making the
constructor self-contained. Callers no longer need to call
alloc_region() or set_mem_region() separately.

Signed-off-by: Ilias Stamatis <ilstam@amazon.com>
@ilstam ilstam dismissed stale reviews from ShadowCurse and Manciukic via 1fec14b April 29, 2026 16:10
@ilstam ilstam enabled auto-merge (rebase) April 29, 2026 16:24
@ilstam ilstam merged commit ae57b7a into firecracker-microvm:main Apr 29, 2026
7 checks passed
@ilstam ilstam deleted the pmem-fix-leaks branch April 29, 2026 16:37
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