Skip to content

Commit 846cdef

Browse files
committed
refactor: replace PAGE_SIZE with GUEST/HOST_PAGE_SIZE
Firecracker was assuming 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 e70bfab commit 846cdef

File tree

8 files changed

+21
-17
lines changed

8 files changed

+21
-17
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ 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 =
98+
|size| (size + (super::GUEST_PAGE_SIZE - 1)) & !(super::GUEST_PAGE_SIZE - 1);
9899
match GuestAddress(get_fdt_addr(guest_mem)).checked_sub(round_to_pagesize(initrd_size) as u64) {
99100
Some(offset) => {
100101
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
@@ -1305,7 +1305,7 @@ pub(crate) mod tests {
13051305
use crate::vstate::memory::GuestMemory;
13061306
let image = make_test_bin();
13071307

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

13101310
let tempfile = TempFile::new().unwrap();
13111311
let mut tempfile = tempfile.into_file();
@@ -1344,7 +1344,7 @@ pub(crate) mod tests {
13441344
let tempfile = TempFile::new().unwrap();
13451345
let mut tempfile = tempfile.into_file();
13461346
tempfile.write_all(&image).unwrap();
1347-
let gm = single_region_mem_at(crate::arch::PAGE_SIZE as u64 + 1, image.len() * 2);
1347+
let gm = single_region_mem_at(crate::arch::GUEST_PAGE_SIZE as u64 + 1, image.len() * 2);
13481348

13491349
let res = load_initrd(&gm, &mut tempfile);
13501350
assert!(

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

Lines changed: 4 additions & 4 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 {
@@ -79,8 +79,8 @@ pub enum IovDequeError {
7979
// ```
8080
//
8181
// This value must be a multiple of 256 because this is the maximum number of `iovec` can fit into
82-
// 1 memory page: 256 * sizeof(iovec) == 4096 == PAGE_SIZE. IovDeque only operates with `PAGE_SIZE`
83-
// granularity.
82+
// 1 memory page: 256 * sizeof(iovec) == 4096 == HOST_PAGE_SIZE. IovDeque only operates with
83+
// `HOST_PAGE_SIZE` granularity.
8484
#[derive(Debug)]
8585
pub struct IovDeque<const L: u16> {
8686
pub iov: *mut libc::iovec,
@@ -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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -815,13 +815,13 @@ mod verification {
815815
use vm_memory::VolatileSlice;
816816

817817
use super::IoVecBuffer;
818+
use crate::arch::GUEST_PAGE_SIZE;
818819
use crate::devices::virtio::iov_deque::IovDeque;
819820
// Redefine `IoVecBufferMut` and `IovDeque` with specific length. Otherwise
820821
// Rust will not know what to do.
821822
type IoVecBufferMutDefault = super::IoVecBufferMut<FIRECRACKER_MAX_QUEUE_SIZE>;
822823
type IovDequeDefault = IovDeque<FIRECRACKER_MAX_QUEUE_SIZE>;
823824

824-
use crate::arch::PAGE_SIZE;
825825
use crate::devices::virtio::queue::FIRECRACKER_MAX_QUEUE_SIZE;
826826

827827
// Maximum memory size to use for our buffers. For the time being 1KB.
@@ -912,8 +912,8 @@ mod verification {
912912
// SAFETY: safe because the layout has non-zero size
913913
let mem = unsafe {
914914
std::alloc::alloc(std::alloc::Layout::from_size_align_unchecked(
915-
2 * PAGE_SIZE,
916-
PAGE_SIZE,
915+
2 * GUEST_PAGE_SIZE,
916+
GUEST_PAGE_SIZE,
917917
))
918918
};
919919
IovDequeDefault {

src/vmm/src/gdb/target.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use vm_memory::{Bytes, GuestAddress, GuestMemoryError};
3333
use super::arch;
3434
#[cfg(target_arch = "aarch64")]
3535
use crate::arch::aarch64::vcpu::VcpuError as AarchVcpuError;
36-
use crate::arch::PAGE_SIZE;
36+
use crate::arch::GUEST_PAGE_SIZE;
3737
use crate::logger::{error, info};
3838
use crate::utils::u64_to_usize;
3939
use crate::vstate::vcpu::VcpuSendEventError;
@@ -396,7 +396,7 @@ impl MultiThreadBase for FirecrackerTarget {
396396
// Compute the amount space left in the page after the gpa
397397
let read_len = std::cmp::min(
398398
data.len(),
399-
PAGE_SIZE - (u64_to_usize(gpa) & (PAGE_SIZE - 1)),
399+
GUEST_PAGE_SIZE - (u64_to_usize(gpa) & (GUEST_PAGE_SIZE - 1)),
400400
);
401401

402402
vmm.guest_memory()
@@ -430,7 +430,7 @@ impl MultiThreadBase for FirecrackerTarget {
430430
// Compute the amount space left in the page after the gpa
431431
let write_len = std::cmp::min(
432432
data.len(),
433-
PAGE_SIZE - (u64_to_usize(gpa) & (PAGE_SIZE - 1)),
433+
GUEST_PAGE_SIZE - (u64_to_usize(gpa) & (GUEST_PAGE_SIZE - 1)),
434434
);
435435

436436
vmm.guest_memory()

src/vmm/src/vstate/vm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ pub(crate) mod tests {
593593
let res = vm.set_kvm_memory_regions(&gm, false);
594594
res.unwrap();
595595

596-
// Trying to set a memory region with a size that is not a multiple of PAGE_SIZE
596+
// Trying to set a memory region with a size that is not a multiple of GUEST_PAGE_SIZE
597597
// will result in error.
598598
let gm = single_region_mem(0x10);
599599
let res = vm.set_kvm_memory_regions(&gm, false);

0 commit comments

Comments
 (0)