Skip to content

Commit

Permalink
Add MADV_POPULATE_{READ,WRITE}.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse-Bakker authored May 31, 2023
1 parent b098f9e commit 6579655
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ edition = "2018"
stable_deref_trait = { version = "1.0", optional = true }

[target.'cfg(unix)'.dependencies]
libc = "0.2"
libc = "0.2.143"

[dev-dependencies]
tempfile = "3"
Expand Down
67 changes: 67 additions & 0 deletions src/advice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,73 @@ pub enum Advice {
#[cfg(target_os = "linux")]
HwPoison = libc::MADV_HWPOISON,

/// **MADV_POPULATE_READ** - Linux only (since Linux 5.14)
///
/// Populate (prefault) page tables readable, faulting in all
/// pages in the range just as if manually reading from each
/// page; however, avoid the actual memory access that would have
/// been performed after handling the fault.
///
/// In contrast to MAP_POPULATE, MADV_POPULATE_READ does not hide
/// errors, can be applied to (parts of) existing mappings and
/// will always populate (prefault) page tables readable. One
/// example use case is prefaulting a file mapping, reading all
/// file content from disk; however, pages won't be dirtied and
/// consequently won't have to be written back to disk when
/// evicting the pages from memory.
///
/// Depending on the underlying mapping, map the shared zeropage,
/// preallocate memory or read the underlying file; files with
/// holes might or might not preallocate blocks. If populating
/// fails, a SIGBUS signal is not generated; instead, an error is
/// returned.
///
/// If MADV_POPULATE_READ succeeds, all page tables have been
/// populated (prefaulted) readable once. If MADV_POPULATE_READ
/// fails, some page tables might have been populated.
///
/// MADV_POPULATE_READ cannot be applied to mappings without read
/// permissions and special mappings, for example, mappings
/// marked with kernel-internal flags such as VM_PFNMAP or VM_IO,
/// or secret memory regions created using memfd_secret(2).
///
/// Note that with MADV_POPULATE_READ, the process can be killed
/// at any moment when the system runs out of memory.
#[cfg(target_os = "linux")]
PopulateRead = libc::MADV_POPULATE_READ,

/// **MADV_POPULATE_WRITE** - Linux only (since Linux 5.14)
///
/// Populate (prefault) page tables writable, faulting in all
/// pages in the range just as if manually writing to each each
/// page; however, avoid the actual memory access that would have
/// been performed after handling the fault.
///
/// In contrast to MAP_POPULATE, MADV_POPULATE_WRITE does not
/// hide errors, can be applied to (parts of) existing mappings
/// and will always populate (prefault) page tables writable.
/// One example use case is preallocating memory, breaking any
/// CoW (Copy on Write).
///
/// Depending on the underlying mapping, preallocate memory or
/// read the underlying file; files with holes will preallocate
/// blocks. If populating fails, a SIGBUS signal is not gener‐
/// ated; instead, an error is returned.
///
/// If MADV_POPULATE_WRITE succeeds, all page tables have been
/// populated (prefaulted) writable once. If MADV_POPULATE_WRITE
/// fails, some page tables might have been populated.
///
/// MADV_POPULATE_WRITE cannot be applied to mappings without
/// write permissions and special mappings, for example, mappings
/// marked with kernel-internal flags such as VM_PFNMAP or VM_IO,
/// or secret memory regions created using memfd_secret(2).
///
/// Note that with MADV_POPULATE_WRITE, the process can be killed
/// at any moment when the system runs out of memory.
#[cfg(target_os = "linux")]
PopulateWrite = libc::MADV_POPULATE_WRITE,

/// **MADV_ZERO_WIRED_PAGES** - Darwin only
///
/// Indicates that the application would like the wired pages in this address range to be
Expand Down

0 comments on commit 6579655

Please sign in to comment.