vmm: enable THP for anon shmem by Champ-Goblem · Pull Request #7646 · cloud-hypervisor/cloud-hypervisor · GitHub
Skip to content

vmm: enable THP for anon shmem#7646

Merged
rbradford merged 1 commit intocloud-hypervisor:mainfrom
Champ-Goblem:patch/enable-thp-for-shmem
Feb 6, 2026
Merged

vmm: enable THP for anon shmem#7646
rbradford merged 1 commit intocloud-hypervisor:mainfrom
Champ-Goblem:patch/enable-thp-for-shmem

Conversation

@Champ-Goblem
Copy link
Copy Markdown
Contributor

The kernel allows madvise on shared memory if /sys/kernel/mm/transparent_hugepage/shmem_enabled is set. Try and configure THP via madvise when anonymous shared memory is used. If this fails, only a warning log is emitted and THP won't be enabled.

@Champ-Goblem Champ-Goblem requested a review from a team as a code owner January 28, 2026 18:31
@Champ-Goblem Champ-Goblem force-pushed the patch/enable-thp-for-shmem branch from 359a97f to 7712b85 Compare January 28, 2026 18:40
Comment thread vmm/src/memory_manager.rs Outdated
Comment thread vmm/src/memory_manager.rs
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If hugetlbfs is already being used, does it make sense to also call MADV_HUGEPAGE?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have pushed a change which only tries THP when hugepages flag is disabled

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW - thp defaults to true so this is probably being done right now away.

Copy link
Copy Markdown
Member

@rbradford rbradford left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch - I want to improve our THP documentation and flow.

Comment thread vmm/src/memory_manager.rs Outdated
@Champ-Goblem Champ-Goblem force-pushed the patch/enable-thp-for-shmem branch from 7712b85 to b53ac72 Compare January 30, 2026 18:26
Copy link
Copy Markdown
Member

@rbradford rbradford left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think my suggestion will make the meaning of thp vs enable_thp clearer.

Comment thread vmm/src/memory_manager.rs Outdated
thp: bool,
) -> Result<MmapRegion<AtomicBitmap>, Error> {
let mut mmap_flags = libc::MAP_NORESERVE;
let mut enable_thp = false;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about let mut enable_thp = thp;

Comment thread vmm/src/memory_manager.rs Outdated
// because the MAP_PRIVATE will trigger CoW against the backing file with
// the VFIO pinning
mmap_flags |= libc::MAP_SHARED;
enable_thp = !hugepages;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enable_thp = !hugepages;

Comment thread vmm/src/memory_manager.rs Outdated
}

if region.file_offset().is_none() && thp {
if thp && (region.file_offset().is_none() || enable_thp) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if enable_thp && region.file_offset().is_none()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would essentially be equivalent to the original if logic, because in the case of anon shmem, file_offset is Some, so THP would end up not being enabled. enable_thp || region.file_offset().is_none() would end up trying to enable THP even if the user did not pass thp=true?

So I think using your original suggested change or relabeling enable_thp to something like try_thp seems more appropriate?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. Let's call the variable thp_madvise and set it where appropriate - that's nice and explicit. Can you also move the info! statement outside that check - I don't think it should be thp gated.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have pushed a new commit that incorporates the above suggestions. Let me know if that looks good or if I missed anything. Thanks!

@rbradford
Copy link
Copy Markdown
Member

@Champ-Goblem Champ-Goblem force-pushed the patch/enable-thp-for-shmem branch from b53ac72 to 4059160 Compare February 3, 2026 18:50
@rbradford rbradford enabled auto-merge February 3, 2026 22:41
@rbradford rbradford added this pull request to the merge queue Feb 3, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Feb 4, 2026
Comment thread vmm/src/memory_manager.rs Outdated
@Champ-Goblem Champ-Goblem force-pushed the patch/enable-thp-for-shmem branch from 4059160 to 482f1b3 Compare February 4, 2026 10:52
Comment thread vmm/src/memory_manager.rs
The kernel allows madvise on shared memory if
/sys/kernel/mm/transparent_hugepage/shmem_enabled is set.
Always try and configure THP via madvise when
the user requests THP be enabled.
If this fails, only a warning log is emitted and THP won't be enabled.

Signed-off-by: Champ-Goblem <cameron@northflank.com>
@Champ-Goblem Champ-Goblem force-pushed the patch/enable-thp-for-shmem branch from 482f1b3 to ebc5a16 Compare February 6, 2026 17:14
@rbradford rbradford enabled auto-merge February 6, 2026 17:48
@rbradford rbradford added this pull request to the merge queue Feb 6, 2026
Merged via the queue into cloud-hypervisor:main with commit 1e0eba6 Feb 6, 2026
43 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

4 participants