Skip to content

sys::prctl: adding calls about core scheduling for isolation based on #2629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/2629.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `get_sched_core`, `set_sched_core`, `share_sched_core_from` and `share_sched_core_to` for Linux
52 changes: 52 additions & 0 deletions src/sys/prctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use crate::errno::Errno;
use crate::sys::signal::Signal;
use crate::unistd::Pid;
use crate::Result;

use libc::{c_int, c_ulong, c_void};
Expand Down Expand Up @@ -226,3 +227,54 @@ pub fn set_vma_anon_name(addr: NonNull<c_void>, length: NonZeroUsize, name: Opti

Errno::result(res).map(drop)
}

/// Get the (scheduling) cookie of the calling thread
pub fn get_sched_core() -> Result<u32> {
let mut cookie: u32 = 0;
let res = unsafe {
libc::prctl(
libc::PR_SCHED_CORE,
libc::PR_SCHED_CORE_GET,
&mut cookie, 0, 0)
};

match Errno::result(res) {
Ok(_) => Ok(cookie),
Err(e) => Err(e),
}
}

/// Set a cookie for the calling thread
pub fn set_sched_core(cookie: u32) -> Result<()> {
let res = unsafe {
libc::prctl(
libc::PR_SCHED_CORE,
libc::PR_SCHED_CORE_CREATE,
&cookie, 0, 0)
};

Errno::result(res).map(drop)
}

/// Share the cookie with another thread
pub fn share_sched_core_from(pid: Pid) -> Result<()> {
let res = unsafe {
libc::prctl(
libc::PR_SCHED_CORE,
libc::PR_SCHED_CORE_SHARE_FROM,
&pid, 0, 0)
};

Errno::result(res).map(drop)
}
/// Calling thread shares the same cookie with another thread
pub fn share_sched_core_to(pid: Pid) -> Result<()> {
let res = unsafe {
libc::prctl(
libc::PR_SCHED_CORE,
libc::PR_SCHED_CORE_SHARE_TO,
&pid, 0, 0)
};

Errno::result(res).map(drop)
}
29 changes: 29 additions & 0 deletions test/sys/test_prctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,33 @@ mod test_prctl {
.unwrap_or_default();
prctl::set_vma_anon_name(ptr, sz, None).unwrap_or_default();
}

#[test]
fn test_sched_core() {
use nix::errno::Errno;
use nix::unistd::Pid;

let c: u32 = 1234;
let p = Pid::from_raw(0);
let mut err = prctl::set_sched_core(c).unwrap_err();
match err {
Errno::EINVAL | Errno::ESRCH | Errno::ENODEV => (),
e => panic!("unexpected error {e}"),
}
err = prctl::get_sched_core().unwrap_err();
match err {
Errno::EINVAL | Errno::ESRCH | Errno::ENODEV => (),
e => panic!("unexpected error {e}"),
}
err = prctl::share_sched_core_from(p).unwrap_err();
match err {
Errno::EINVAL | Errno::ESRCH | Errno::ENODEV => (),
e => panic!("unexpected error {e}"),
}
err = prctl::share_sched_core_to(p).unwrap_err();
match err {
Errno::EINVAL | Errno::ESRCH | Errno::ENODEV => (),
e => panic!("unexpected error {e}"),
}
}
}