Skip to content

Commit cf26cc9

Browse files
committed
refactor: repalce PAGE_SIZE with GUEST/HOST_PAGE_SIZE
Firecracker was assumming page sizes for both host and guest are 4K. But they can differ, so split into 2 values. Signed-off-by: Egor Lazarchuk <[email protected]>
1 parent 83aec7b commit cf26cc9

File tree

6 files changed

+11
-9
lines changed

6 files changed

+11
-9
lines changed

src/vmm/src/arch/aarch64/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pub fn initrd_load_addr(
9494
guest_mem: &GuestMemoryMmap,
9595
initrd_size: usize,
9696
) -> Result<u64, ConfigurationError> {
97-
let round_to_pagesize = |size| (size + (super::PAGE_SIZE - 1)) & !(super::PAGE_SIZE - 1);
97+
let round_to_pagesize = |size| (size + (super::GUEST_PAGE_SIZE - 1)) & !(super::GUEST_PAGE_SIZE - 1);
9898
match GuestAddress(get_fdt_addr(guest_mem)).checked_sub(round_to_pagesize(initrd_size) as u64) {
9999
Some(offset) => {
100100
if guest_mem.address_in_range(offset) {

src/vmm/src/arch/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,11 @@ pub struct InitrdConfig {
5252
pub size: usize,
5353
}
5454

55-
/// Default (smallest) memory page size for the supported architectures.
56-
pub const PAGE_SIZE: usize = 4096;
55+
/// Default page size for the guest OS.
56+
pub const GUEST_PAGE_SIZE: usize = 4096;
57+
58+
/// Default page size for the host OS.
59+
pub const HOST_PAGE_SIZE: usize = 4096;
5760

5861
impl fmt::Display for DeviceType {
5962
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

src/vmm/src/arch/x86_64/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ pub fn initrd_load_addr(
9797
return Err(ConfigurationError::InitrdAddress);
9898
}
9999

100-
let align_to_pagesize = |address| address & !(super::PAGE_SIZE - 1);
100+
let align_to_pagesize = |address| address & !(super::GUEST_PAGE_SIZE - 1);
101101
Ok(align_to_pagesize(lowmem_size - initrd_size) as u64)
102102
}
103103

src/vmm/src/builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,7 +1303,7 @@ pub mod tests {
13031303
use crate::vstate::memory::GuestMemory;
13041304
let image = make_test_bin();
13051305

1306-
let mem_size: usize = image.len() * 2 + crate::arch::PAGE_SIZE;
1306+
let mem_size: usize = image.len() * 2 + crate::arch::GUEST_PAGE_SIZE;
13071307

13081308
let tempfile = TempFile::new().unwrap();
13091309
let mut tempfile = tempfile.into_file();
@@ -1342,7 +1342,7 @@ pub mod tests {
13421342
let tempfile = TempFile::new().unwrap();
13431343
let mut tempfile = tempfile.into_file();
13441344
tempfile.write_all(&image).unwrap();
1345-
let gm = single_region_mem_at(crate::arch::PAGE_SIZE as u64 + 1, image.len() * 2);
1345+
let gm = single_region_mem_at(crate::arch::GUEST_PAGE_SIZE as u64 + 1, image.len() * 2);
13461346

13471347
let res = load_initrd(&gm, &mut tempfile);
13481348
assert!(

src/vmm/src/devices/virtio/iov_deque.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::os::fd::AsRawFd;
66
use libc::{c_int, c_void, iovec, off_t, size_t};
77
use memfd;
88

9-
use crate::arch::PAGE_SIZE;
9+
use crate::arch::HOST_PAGE_SIZE;
1010

1111
#[derive(Debug, thiserror::Error, displaydoc::Display)]
1212
pub enum IovDequeError {
@@ -93,7 +93,7 @@ unsafe impl<const L: u16> Send for IovDeque<L> {}
9393

9494
impl<const L: u16> IovDeque<L> {
9595
const BYTES: usize = L as usize * std::mem::size_of::<iovec>();
96-
const _ASSERT: () = assert!(Self::BYTES % PAGE_SIZE == 0);
96+
const _ASSERT: () = assert!(Self::BYTES % HOST_PAGE_SIZE == 0);
9797

9898
/// Create a [`memfd`] object that represents a single physical page
9999
fn create_memfd() -> Result<memfd::Memfd, IovDequeError> {

src/vmm/src/devices/virtio/iovec.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,6 @@ mod verification {
821821
type IoVecBufferMutDefault = super::IoVecBufferMut<FIRECRACKER_MAX_QUEUE_SIZE>;
822822
type IovDequeDefault = IovDeque<FIRECRACKER_MAX_QUEUE_SIZE>;
823823

824-
use crate::arch::PAGE_SIZE;
825824
use crate::devices::virtio::queue::FIRECRACKER_MAX_QUEUE_SIZE;
826825

827826
// Maximum memory size to use for our buffers. For the time being 1KB.

0 commit comments

Comments
 (0)