Skip to content

Commit

Permalink
feat: query host page size
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
ShadowCurse committed Nov 15, 2024
1 parent d5bb8b1 commit e42b346
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/firecracker/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use seccomp::FilterError;
use seccompiler::BpfThreadMap;
use utils::arg_parser::{ArgParser, Argument};
use utils::validators::validate_instance_id;
use vmm::arch::update_host_page_size;
use vmm::builder::StartMicrovmError;
use vmm::logger::{
debug, error, info, LoggerConfig, ProcessTimeReporter, StoreMetric, LOGGER, METRICS,
Expand Down Expand Up @@ -108,6 +109,8 @@ fn main_exec() -> Result<(), MainError> {
// Initialize the logger.
LOGGER.init().map_err(MainError::SetLogger)?;

update_host_page_size();

// We need this so that we can reset terminal to canonical mode if panic occurs.
let stdin = io::stdin();

Expand Down
19 changes: 18 additions & 1 deletion src/vmm/src/arch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use std::fmt;

use log::warn;
use serde::{Deserialize, Serialize};

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

/// Default page size for the host OS.
pub const HOST_PAGE_SIZE: usize = 4096;
pub static mut HOST_PAGE_SIZE: usize = 4096;

/// Updates the HOST_PAGE_SIZE global variable to the output of
/// sysconf(_SC_PAGESIZE). If call is unsuccessfull the default
/// value of 4096 remains.
pub fn update_host_page_size() {
// # Safety:
// There is nothing unsafe here.
let r = unsafe { libc::sysconf(libc::_SC_PAGESIZE) };
if r < 0 {
warn!("Could not get host page size with sysconf, assuming default 4K host pages");
} else {
// # Safety:
// The value is checked.
unsafe { HOST_PAGE_SIZE = usize::try_from(r).unwrap() };
}
}

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

0 comments on commit e42b346

Please sign in to comment.