Skip to content

Commit 6e3c76d

Browse files
Serban Iorgaandreeaflorescu
authored andcommitted
use the configured OS page size in get_dirty_log()
Right now get_dirty_log() acts on the assumption that the page size is always 4K. This is not necessarily true, since the page size is configurable. This commit improves the logic by calling sysconf() to get the actual page size. Signed-off-by: Serban Iorga <[email protected]>
1 parent 229cb5b commit 6e3c76d

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

coverage_config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"coverage_score": 92.0,
2+
"coverage_score": 92.1,
33
"exclude_path": "",
44
"crate_features": ""
55
}

src/ioctls/vm.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -791,22 +791,24 @@ impl VmFd {
791791
///
792792
pub fn get_dirty_log(&self, slot: u32, memory_size: usize) -> Result<Vec<u64>> {
793793
// Compute the length of the bitmap needed for all dirty pages in one memory slot.
794-
// One memory page is 4KiB (4096 bits) and `KVM_GET_DIRTY_LOG` returns one dirty bit for
794+
// One memory page is `page_size` bytes and `KVM_GET_DIRTY_LOG` returns one dirty bit for
795795
// each page.
796-
let page_size = 4 << 10;
796+
let page_size = match unsafe { libc::sysconf(libc::_SC_PAGESIZE) } {
797+
-1 => return Err(errno::Error::last()),
798+
ps => ps as usize,
799+
};
797800

798-
let div_round_up = |dividend, divisor| (dividend + divisor - 1) / divisor;
799801
// For ease of access we are saving the bitmap in a u64 vector. We are using ceil to
800-
// make sure we count all dirty pages even when `mem_size` is not a multiple of
801-
// page_size * 64.
802-
let bitmap_size = div_round_up(memory_size, page_size * 64);
803-
let mut bitmap = vec![0; bitmap_size];
804-
let b_data = bitmap.as_mut_ptr() as *mut c_void;
802+
// make sure we count all dirty pages even when `memory_size` is not a multiple of
803+
// `page_size * 64`.
804+
let div_ceil = |dividend, divisor| (dividend + divisor - 1) / divisor;
805+
let bitmap_size = div_ceil(memory_size, page_size * 64);
806+
let mut bitmap = vec![0u64; bitmap_size];
805807
let dirtylog = kvm_dirty_log {
806808
slot,
807809
padding1: 0,
808810
__bindgen_anon_1: kvm_dirty_log__bindgen_ty_1 {
809-
dirty_bitmap: b_data,
811+
dirty_bitmap: bitmap.as_mut_ptr() as *mut c_void,
810812
},
811813
};
812814
// Safe because we know that our file is a VM fd, and we know that the amount of memory

0 commit comments

Comments
 (0)