-
Notifications
You must be signed in to change notification settings - Fork 2.5k
feat: enable THP for guest memory #6003
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,9 +34,11 @@ pub enum MachineConfigError { | |
| /// Describes the possible (huge)page configurations for a microVM's memory. | ||
| #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] | ||
| pub enum HugePageConfig { | ||
| /// Do not use hugepages, e.g. back guest memory by 4K | ||
| /// Back guest memory by 4K pages, no hugepage behavior | ||
| #[default] | ||
| None, | ||
| /// Use madvise(MADV_HUGEPAGE) for transparent huge pages | ||
| Transparent, | ||
| /// Back guest memory by 2MB hugetlbfs pages | ||
| #[serde(rename = "2M")] | ||
| Hugetlbfs2M, | ||
|
|
@@ -49,6 +51,10 @@ impl HugePageConfig { | |
| let divisor = match self { | ||
| // Any integer memory size expressed in MiB will be a multiple of 4096KiB. | ||
| HugePageConfig::None => 1, | ||
| // Note: THP technically supports memory not 2MB aligned, however that would mean | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The wording is slightly confusing because here it talks about alignment (if it's misaligned there can also be 4KiB pages at the head too), but then it talks about enforcing size, not alignment. |
||
| // some pages at the tail would be forced to be 4k size. To avoid performance/fragmentation surprises, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is everybody "aligned" (no pun intended) with this? I'm not necessarily against this, but equally I don't think it's a big problem having a few 4K pages at the end of the memory region (especially since THP is not a guarantee anyway). And we already know that internal customers often use non 2MiB multiples. |
||
| // having a memory multiple of 2MB is wiser. | ||
| HugePageConfig::Transparent => 2, | ||
| HugePageConfig::Hugetlbfs2M => 2, | ||
| }; | ||
|
|
||
|
|
@@ -59,11 +65,20 @@ impl HugePageConfig { | |
| /// create a mapping backed by huge pages as described by this [`HugePageConfig`]. | ||
| pub fn mmap_flags(&self) -> libc::c_int { | ||
| match self { | ||
| HugePageConfig::None => 0, | ||
| HugePageConfig::None | HugePageConfig::Transparent => 0, | ||
| HugePageConfig::Hugetlbfs2M => libc::MAP_HUGETLB | libc::MAP_HUGE_2MB, | ||
| } | ||
| } | ||
|
|
||
| /// Returns the flags required to pass to [libc::madvise], after allocating anonymous guest memory. | ||
| /// Note: returning [libc::MADV_NORMAL] might skip the call to `madvise` entirely. | ||
| pub fn madvise_flags(&self) -> libc::c_int { | ||
| match self { | ||
| HugePageConfig::Transparent => libc::MADV_HUGEPAGE, | ||
| HugePageConfig::None | HugePageConfig::Hugetlbfs2M => libc::MADV_NORMAL, | ||
| } | ||
| } | ||
|
|
||
| /// Returns `true` iff this [`HugePageConfig`] describes a hugetlbfs-based configuration. | ||
| pub fn is_hugetlbfs(&self) -> bool { | ||
| matches!(self, HugePageConfig::Hugetlbfs2M) | ||
|
|
@@ -72,7 +87,7 @@ impl HugePageConfig { | |
| /// Gets the page size in bytes of this [`HugePageConfig`]. | ||
| pub fn page_size(&self) -> usize { | ||
| match self { | ||
| HugePageConfig::None => 4096, | ||
| HugePageConfig::None | HugePageConfig::Transparent => 4096, | ||
| HugePageConfig::Hugetlbfs2M => 2 * 1024 * 1024, | ||
| } | ||
| } | ||
|
|
@@ -81,7 +96,7 @@ impl HugePageConfig { | |
| impl From<HugePageConfig> for Option<memfd::HugetlbSize> { | ||
| fn from(value: HugePageConfig) -> Self { | ||
| match value { | ||
| HugePageConfig::None => None, | ||
| HugePageConfig::None | HugePageConfig::Transparent => None, | ||
| HugePageConfig::Hugetlbfs2M => Some(memfd::HugetlbSize::Huge2MB), | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With mTHP, can't it also be sizes other than 2 MiB?