diff --git a/crates/runc-shim/Cargo.toml b/crates/runc-shim/Cargo.toml index 2e31e3fe..996316b9 100644 --- a/crates/runc-shim/Cargo.toml +++ b/crates/runc-shim/Cargo.toml @@ -33,6 +33,7 @@ serde_json = "1.0.74" oci-spec = "0.6.0" crossbeam = "0.8.1" uuid = { version = "1.0.0", features = ["v4"] } +prctl = "1.0.0" # Async dependencies async-trait = { workspace = true } diff --git a/crates/runc-shim/src/service.rs b/crates/runc-shim/src/service.rs index 512a3d31..859de39a 100644 --- a/crates/runc-shim/src/service.rs +++ b/crates/runc-shim/src/service.rs @@ -78,8 +78,27 @@ impl Shim for Service { } None => {} } + #[cfg(not(target_os = "linux"))] + let thp_disabled = String::new(); + #[cfg(target_os = "linux")] + // Our goal is to set thp disable = true on the shim side and then restore thp + // disable before starting runc. So we only need to focus on the return value + // of the function get_thp_disabled, which is Result. + let thp_disabled = match prctl::get_thp_disable() { + Ok(x) => { + // The return value of the function set_thp_disabled is Result<(), i32>, + // we don't care if the setting is successful, because even if the + // setting failed, we should not exit the shim process, therefore, + // there is no need to pay attention to the set_thp_disabled function's + // return value. + let _ = prctl::set_thp_disable(true); + x.to_string() + } + Err(_) => String::new(), + }; + let vars: Vec<(&str, &str)> = vec![("THP_DISABLED", thp_disabled.as_str())]; - let address = spawn(opts, &grouping, Vec::new()).await?; + let address = spawn(opts, &grouping, vars).await?; write_str_to_file("address", &address).await?; Ok(address) } diff --git a/crates/runc/Cargo.toml b/crates/runc/Cargo.toml index a4c8b67a..e06276ce 100644 --- a/crates/runc/Cargo.toml +++ b/crates/runc/Cargo.toml @@ -28,6 +28,7 @@ thiserror = "1.0.30" time = { version = "0.3.7", features = ["serde", "std"] } uuid = { version = "1.0.0", features = ["v4"] } os_pipe = "1.0.0" +prctl = "1.0.0" # Async dependencies tokio = { workspace = true, features = ["full"], optional = true } diff --git a/crates/runc/src/lib.rs b/crates/runc/src/lib.rs index d222dc1b..cce6b460 100644 --- a/crates/runc/src/lib.rs +++ b/crates/runc/src/lib.rs @@ -366,8 +366,22 @@ pub trait Spawner: Debug { /// and some other utilities. #[cfg(feature = "async")] impl Runc { - async fn launch(&self, cmd: Command, combined_output: bool) -> Result { + async fn launch(&self, mut cmd: Command, combined_output: bool) -> Result { debug!("Execute command {:?}", cmd); + unsafe { + cmd.pre_exec(move || { + #[cfg(target_os = "linux")] + if let Ok(thp) = std::env::var("THP_DISABLED") { + if let Ok(thp_disabled) = thp.parse::() { + if let Err(e) = prctl::set_thp_disable(thp_disabled) { + debug!("set_thp_disable err: {}", e); + }; + } + } + Ok(()) + }); + } + let (status, pid, stdout, stderr) = self.spawner.execute(cmd).await?; if status.success() { let output = if combined_output {