From 59363848a29fb18762a91023c1318922764bc843 Mon Sep 17 00:00:00 2001 From: Avril Date: Sat, 30 Apr 2022 22:46:01 +0100 Subject: [PATCH] Started: `memfile::hp`: `MFD_HUGETLB` can be used when a size is known to exceed into the boundary of a hugepage. Currently in the process of writing function to collect all possible hugepage sizes and generate the correct flag masks for `memfd_create(, MFD_HUGETLB | ...)` for each one; and the smallest one that contains the known size can be used for the `memfd_create()` call (if the size fits any at all.) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TODO: This will be a seperate comptime feature: `hugetlb`: Extend `memfile` with the ability to allocate apporpirately sized huge-pages when input size is known to extend into them. Fortune for collect's current commit: Blessing − 吉 --- Cargo.lock | 2 +- src/memfile.rs | 3 +++ src/memfile/hp.rs | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 src/memfile/hp.rs diff --git a/Cargo.lock b/Cargo.lock index 675a63a..b3b56d9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -84,7 +84,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "collect" -version = "1.0.1" +version = "1.0.2" dependencies = [ "bitflags", "bytes", diff --git a/src/memfile.rs b/src/memfile.rs index a0e7431..50b6b5f 100644 --- a/src/memfile.rs +++ b/src/memfile.rs @@ -16,6 +16,9 @@ use std::{ pub mod fd; pub mod error; mod map; +//TODO: #[cfg(feature="hugetlb")] +mod hp; + /// Flags passed to `memfd_create()` when used in this module const MEMFD_CREATE_FLAGS: libc::c_uint = libc::MFD_CLOEXEC; diff --git a/src/memfile/hp.rs b/src/memfile/hp.rs new file mode 100644 index 0000000..61e2235 --- /dev/null +++ b/src/memfile/hp.rs @@ -0,0 +1,35 @@ +//! # Huge pages +//! Calculates available huge page sizes, and creates `memfd_create()` flag masks for a `MFD_HUGETLB` fd. +use super::*; +use std::{ + path::Path, +}; +use libc::{ + MFD_HUGETLB, + MAP_HUGE_SHIFT, +}; + +/// The location in the kernel's API which shows all valid huge-page sizes. +/// +/// This is a directory, and its subdirectories' *names* will contain the size in this format: `hugepages-(\d+)kB` (where the first capture-group is the size in kB.) +/// The contents of those subdirectories themselves are irrelevent for our purpose. +pub const HUGEPAGE_SIZES_LOCATION: &'static str = "/sys/kernel/mm/hugepages"; + +/// Take a directory path and try to parse the hugepage size from it. +/// +/// All subdirectories from `HUGEPAGE_SIZES_LOCATION` should be passed to this, and the correct system-valid hugepage size will be returned for that specific hugepage. +fn find_size_bytes(path: impl AsRef) -> Option +{ + let path= path.as_ref(); + if !path.is_dir() { + return None; + } + + let dir_name = path.file_name()?; + // location of the `-` in the dir name + let split_loc = memchr::memchr(b'-', dir_name.as_bytes())?; + + //TODO: find the `k` (XXX: Is it always in kB? Or do we have to find the last non-digit byte instead?) For now, we can just memchr('k') I think -- look into kernel spec for this later. + + None +}