Skip to content

Commit 1ec5c44

Browse files
committed
refactor(mincore): pass addr and len to mincore_bitmap
In preparation to adding support for multiple memory slots, refactor the mincore_bitmap function to accept a pointer and length rather than an entire memory region object. Signed-off-by: Riccardo Mancini <[email protected]>
1 parent 545324c commit 1ec5c44

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

src/vmm/src/vstate/vm.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ impl Vm {
296296
.fd()
297297
.get_dirty_log(region.slot, u64_to_usize(region.len()))
298298
.map_err(VmError::GetDirtyLog)?,
299-
None => mincore_bitmap(&region.inner)?,
299+
None => mincore_bitmap(region.as_ptr(), u64_to_usize(region.len()))?,
300300
};
301301
Ok((region.slot, bitmap))
302302
})
@@ -481,7 +481,7 @@ impl Vm {
481481

482482
/// Use `mincore(2)` to overapproximate the dirty bitmap for the given memslot. To be used
483483
/// if a diff snapshot is requested, but dirty page tracking wasn't enabled.
484-
fn mincore_bitmap(region: &GuestRegionMmap) -> Result<Vec<u64>, VmError> {
484+
fn mincore_bitmap(addr: *mut u8, len: usize) -> Result<Vec<u64>, VmError> {
485485
// TODO: Once Host 5.10 goes out of support, we can make this more robust and work on
486486
// swap-enabled systems, by doing mlock2(MLOCK_ONFAULT)/munlock() in this function (to
487487
// force swapped-out pages to get paged in, so that mincore will consider them incore).
@@ -492,22 +492,16 @@ fn mincore_bitmap(region: &GuestRegionMmap) -> Result<Vec<u64>, VmError> {
492492
// is a hugetlbfs VMA (e.g. to report a single hugepage as "present", mincore will
493493
// give us 512 4k markers with the lowest bit set).
494494
let page_size = host_page_size();
495-
let mut mincore_bitmap = vec![0u8; u64_to_usize(region.len()) / page_size];
496-
let mut bitmap = vec![0u64; (u64_to_usize(region.len()) / page_size).div_ceil(64)];
495+
let mut mincore_bitmap = vec![0u8; len / page_size];
496+
let mut bitmap = vec![0u64; (len / page_size).div_ceil(64)];
497497

498498
// SAFETY: The safety invariants of GuestRegionMmap ensure that region.as_ptr() is a valid
499499
// userspace mapping of size region.len() bytes. The bitmap has exactly one byte for each
500500
// page in this userspace mapping. Note that mincore does not operate on bitmaps like
501501
// KVM_MEM_LOG_DIRTY_PAGES, but rather it uses 8 bits per page (e.g. 1 byte), setting the
502502
// least significant bit to 1 if the page corresponding to a byte is in core (available in
503503
// the page cache and resolvable via just a minor page fault).
504-
let r = unsafe {
505-
libc::mincore(
506-
region.as_ptr().cast::<libc::c_void>(),
507-
u64_to_usize(region.len()),
508-
mincore_bitmap.as_mut_ptr(),
509-
)
510-
};
504+
let r = unsafe { libc::mincore(addr.cast(), len, mincore_bitmap.as_mut_ptr()) };
511505

512506
if r != 0 {
513507
return Err(VmError::Mincore(vmm_sys_util::errno::Error::last()));

0 commit comments

Comments
 (0)