Skip to content

Commit e42b346

Browse files
committed
feat: query host page size
Define global variable with host page size and update it at the very beginning of the main function in Firecracker. This way data types which rely on specific host page size can adapt to it. Signed-off-by: Egor Lazarchuk <[email protected]>
1 parent d5bb8b1 commit e42b346

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/firecracker/src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use seccomp::FilterError;
2020
use seccompiler::BpfThreadMap;
2121
use utils::arg_parser::{ArgParser, Argument};
2222
use utils::validators::validate_instance_id;
23+
use vmm::arch::update_host_page_size;
2324
use vmm::builder::StartMicrovmError;
2425
use vmm::logger::{
2526
debug, error, info, LoggerConfig, ProcessTimeReporter, StoreMetric, LOGGER, METRICS,
@@ -108,6 +109,8 @@ fn main_exec() -> Result<(), MainError> {
108109
// Initialize the logger.
109110
LOGGER.init().map_err(MainError::SetLogger)?;
110111

112+
update_host_page_size();
113+
111114
// We need this so that we can reset terminal to canonical mode if panic occurs.
112115
let stdin = io::stdin();
113116

src/vmm/src/arch/mod.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use std::fmt;
55

6+
use log::warn;
67
use serde::{Deserialize, Serialize};
78

89
/// Module for aarch64 related functionality.
@@ -56,7 +57,23 @@ pub struct InitrdConfig {
5657
pub const GUEST_PAGE_SIZE: usize = 4096;
5758

5859
/// Default page size for the host OS.
59-
pub const HOST_PAGE_SIZE: usize = 4096;
60+
pub static mut HOST_PAGE_SIZE: usize = 4096;
61+
62+
/// Updates the HOST_PAGE_SIZE global variable to the output of
63+
/// sysconf(_SC_PAGESIZE). If call is unsuccessfull the default
64+
/// value of 4096 remains.
65+
pub fn update_host_page_size() {
66+
// # Safety:
67+
// There is nothing unsafe here.
68+
let r = unsafe { libc::sysconf(libc::_SC_PAGESIZE) };
69+
if r < 0 {
70+
warn!("Could not get host page size with sysconf, assuming default 4K host pages");
71+
} else {
72+
// # Safety:
73+
// The value is checked.
74+
unsafe { HOST_PAGE_SIZE = usize::try_from(r).unwrap() };
75+
}
76+
}
6077

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

0 commit comments

Comments
 (0)