Skip to content

Commit

Permalink
runtime-rs: fixed bug on core-sched error handling
Browse files Browse the repository at this point in the history
Kernel code returns -errno, this should check negative values.

Fixes: #4429
Signed-off-by: Ji-Xinyou <jerryji0414@outlook.com>
  • Loading branch information
Ji-Xinyou committed Aug 3, 2022
1 parent 591dfa4 commit a355812
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
23 changes: 10 additions & 13 deletions src/runtime-rs/crates/shim/src/core_sched.rs
Expand Up @@ -38,7 +38,7 @@ pub const PR_SCHED_CORE_SHARE_FROM: usize = 3;
pub fn core_sched_create(pidtype: usize) -> Result<(), Errno> {
let errno = unsafe { nix::libc::prctl(PR_SCHED_CORE, PR_SCHED_CORE_CREATE, 0, pidtype, 0) };
if errno != 0 {
Err(nix::errno::Errno::from_i32(errno))
Err(nix::errno::Errno::from_i32(-errno))
} else {
Ok(())
}
Expand All @@ -50,7 +50,7 @@ pub fn core_sched_share_from(pid: usize, pidtype: usize) -> Result<(), Errno> {
let errno =
unsafe { nix::libc::prctl(PR_SCHED_CORE, PR_SCHED_CORE_SHARE_FROM, pid, pidtype, 0) };
if errno != 0 {
Err(nix::errno::Errno::from_i32(errno))
Err(nix::errno::Errno::from_i32(-errno))
} else {
Ok(())
}
Expand Down Expand Up @@ -87,18 +87,15 @@ mod tests {
// therefore it does not make sense to assert a successful prctl call
// but we can still make sure that the return value is a possible value
let e = core_sched_create(PROCESS_GROUP);
match e {
Err(errno) => {
if errno != EINVAL
&& errno != ENODEV
&& errno != ENOMEM
&& errno != EPERM
&& errno != ESRCH
{
panic!("impossible return value {:?}", errno);
}
if let Err(errno) = e {
if errno != EINVAL
&& errno != ENODEV
&& errno != ENOMEM
&& errno != EPERM
&& errno != ESRCH
{
panic!("impossible return value {:?}", errno);
}
Ok(()) => {}
}
}
}
Expand Down
10 changes: 3 additions & 7 deletions src/runtime-rs/crates/shim/src/shim_run.rs
Expand Up @@ -8,7 +8,6 @@ use std::os::unix::io::RawFd;

use anyhow::{Context, Result};
use kata_sys_util::spec::get_bundle_path;
use nix::errno::Errno;

use crate::{
core_sched, logger,
Expand Down Expand Up @@ -71,14 +70,11 @@ fn get_server_fd() -> Result<RawFd> {
}

// TODO: currently we log a warning on fail (i.e. kernel version < 5.14), maybe just exit
fn try_core_sched() -> Result<(), Errno> {
// TODO: more test on higher version of kernel
fn try_core_sched() -> Result<()> {
if let Ok(v) = std::env::var("SCHED_CORE") {
info!(
sl!(),
"containerd wants to enable core scheduling, SCHED_CORE={}(expected 1)", v
);
if !v.is_empty() {
return core_sched::core_sched_create(core_sched::PROCESS_GROUP);
core_sched::core_sched_create(core_sched::PROCESS_GROUP)?
}
}
Ok(())
Expand Down

0 comments on commit a355812

Please sign in to comment.