Skip to content

Commit

Permalink
Add mlockall and munlockall
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Oct 9, 2023
1 parent 1a9d129 commit e8f0748
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/backend/libc/mm/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,20 @@ pub(crate) unsafe fn userfaultfd(flags: UserfaultfdFlags) -> io::Result<OwnedFd>
}
ret_owned_fd(userfaultfd(bitflags_bits!(flags)))
}

/// Locks all pages mapped into the address space of the calling process.
///
/// This includes the pages of the code, data and stack segment, as well as shared libraries,
/// user space kernel data, shared memory, and memory-mapped files. All mapped pages are guaranteed
/// to be resident in RAM when the call returns successfully; the pages are guaranteed to stay in RAM
/// until later unlocked.
#[inline]
pub(crate) fn mlockall(flags: MlockFlags) -> io::Result<()> {
unsafe { ret(c::mlockall(bitflags_bits!(flags))) }
}

/// Unlocks all pages mapped into the address space of the calling process.
#[inline]
pub(crate) fn munlockall() -> io::Result<()> {
unsafe { ret(c::munlockall()) }
}
19 changes: 19 additions & 0 deletions src/backend/libc/mm/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,3 +442,22 @@ bitflags! {
const _ = !0;
}
}

bitflags! {
/// `MCL_*` flags for use with [`mlockall`].
///
/// [`mlockall`]: crate::mm::mlockall
pub struct MlockallFlags: i32 {
// libc doesn't define `MCL_ONFAULT` yet.
// const ONFAULT = libc::MCL_ONFAULT;
/// Lock all pages which will become mapped into the address
/// space of the process in the future. These could be, for
/// instance, new pages required by a growing heap and stack
/// as well as new memory-mapped files or shared memory
/// regions.
const FUTURE = libc::MCL_FUTURE;
/// Lock all pages which are currently mapped into the address
/// space of the process.
const CURRENT = libc::MCL_CURRENT;
}
}
17 changes: 17 additions & 0 deletions src/backend/linux_raw/mm/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,20 @@ pub(crate) unsafe fn munlock(addr: *mut c::c_void, length: usize) -> io::Result<
pub(crate) unsafe fn userfaultfd(flags: UserfaultfdFlags) -> io::Result<OwnedFd> {
ret_owned_fd(syscall_readonly!(__NR_userfaultfd, flags))
}

/// Locks all pages mapped into the address space of the calling process.
///
/// This includes the pages of the code, data and stack segment, as well as shared libraries,
/// user space kernel data, shared memory, and memory-mapped files. All mapped pages are guaranteed
/// to be resident in RAM when the call returns successfully; the pages are guaranteed to stay in RAM
/// until later unlocked.
#[inline]
pub(crate) fn mlockall(flags: MlockFlags) -> io::Result<()> {
unsafe { ret(syscall_readonly!(__NR_mlockall, flags)) }
}

/// Unlocks all pages mapped into the address space of the calling process.
#[inline]
pub(crate) fn munlockall() -> io::Result<()> {
unsafe { ret(syscall_readonly!(__NR_munlockall)) }
}
28 changes: 28 additions & 0 deletions src/backend/linux_raw/mm/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,31 @@ bitflags! {
const _ = !0;
}
}

bitflags! {
/// `MCL_*` flags for use with [`mlockall`].
///
/// [`mlockall`]: crate::mm::mlockall
pub struct MlockallFlags: u32 {
/// Used together with `MCL_CURRENT`, `MCL_FUTURE`, or both. Mark
/// all current (with `MCL_CURRENT`) or future (with `MCL_FUTURE`)
/// mappings to lock pages when they are faulted in. When
/// used with `MCL_CURRENT`, all present pages are locked, but
/// `mlockall()` will not fault in non-present pages. When used
/// with `MCL_FUTURE`, all future mappings will be marked to
/// lock pages when they are faulted in, but they will not be
/// populated by the lock when the mapping is created.
/// `MCL_ONFAULT` must be used with either `MCL_CURRENT` or
/// `MCL_FUTURE` or both.
const ONFAULT = linux_raw_sys::general::MCL_ONFAULT;
/// Lock all pages which will become mapped into the address
/// space of the process in the future. These could be, for
/// instance, new pages required by a growing heap and stack
/// as well as new memory-mapped files or shared memory
/// regions.
const FUTURE = linux_raw_sys::general::MCL_FUTURE;
/// Lock all pages which are currently mapped into the address
/// space of the process.
const CURRENT = linux_raw_sys::general::MCL_CURRENT;
}
}

0 comments on commit e8f0748

Please sign in to comment.