From 8374107c8732ea3a82f99a28d948b017bc6fdbad Mon Sep 17 00:00:00 2001 From: John Starks Date: Thu, 23 Jul 2026 11:30:32 +0300 Subject: [PATCH 1/3] petri: enable THP for shared memory and on Windows Petri gated transparent huge pages behind private anonymous memory and a Linux host, since that was the only backing where THP had any effect. The membacking layer now marks shared (file/memfd) guest RAM as THP-eligible and implements Windows soft large pages, so those restrictions are stale and needlessly suppress THP for VMs backed by shared memory or running on Windows hosts. Drop the private-memory and Linux-host conditions and pass the requested THP setting straight through; membacking already suppresses it where it genuinely does not apply, such as explicit hugetlb backings. Update the MemoryConfig docs to match. This should improve test performance on Windows hosts. --- petri/src/vm/mod.rs | 11 ++++++----- petri/src/vm/openvmm/construct.rs | 10 +++++----- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/petri/src/vm/mod.rs b/petri/src/vm/mod.rs index 4e552a043c..5da5ad84ad 100644 --- a/petri/src/vm/mod.rs +++ b/petri/src/vm/mod.rs @@ -2345,13 +2345,14 @@ pub struct MemoryConfig { /// /// Only applies to the OpenVMM backend; ignored by Hyper-V. pub private_memory: Option, - /// Mark private guest RAM as eligible for Transparent Huge Pages (THP), + /// Mark guest RAM as eligible for Transparent Huge Pages (THP), /// improving performance for large allocations. /// - /// Defaults to `true`. Only takes effect when the guest RAM is backed by - /// private anonymous memory (see [`Self::private_memory`]) and only on - /// Linux; it is silently ignored otherwise (shared memory, hugetlb/file - /// backing, OpenHCL, PCAT/Gen1, or non-Linux hosts). + /// Defaults to `true`. Applies to both private anonymous and shared + /// (file/memfd) guest RAM, on Linux (via `madvise`) and on Windows (via + /// soft large pages). It is silently ignored where it does not apply, such + /// as explicit hugetlb/large-page (`with_hugepages`) backings, which are + /// already huge. /// /// Only applies to the OpenVMM backend; ignored by Hyper-V. pub transparent_hugepages: bool, diff --git a/petri/src/vm/openvmm/construct.rs b/petri/src/vm/openvmm/construct.rs index 67c37d84ee..ad93b75e33 100644 --- a/petri/src/vm/openvmm/construct.rs +++ b/petri/src/vm/openvmm/construct.rs @@ -488,11 +488,11 @@ impl PetriVmConfigOpenVmm { None => !private_incompatible, }; - // THP is only valid for private anonymous memory and only on - // Linux; disable it otherwise to avoid a memory build error. - let transparent_hugepages = - transparent_hugepages && private_memory && cfg!(target_os = "linux"); - + // THP applies to both private anonymous and shared (file/memfd) + // guest RAM, and on both Linux (madvise-based) and Windows + // (soft large pages). The membacking layer suppresses it where it + // does not apply (e.g. explicit hugetlb backings), so pass the + // requested value through unchanged. let make_mem = |size: u64| openvmm_defs::config::MemoryConfig { mem_size: size, prefetch_memory: false, From 39fd2c3811644fe75277c3c8f0837bbfe5e5235b Mon Sep 17 00:00:00 2001 From: John Starks Date: Thu, 23 Jul 2026 11:50:41 +0300 Subject: [PATCH 2/3] petri: document THP overrides in with_hugepages/with_memory_backing_file Address review feedback that the THP doc over-promised for shared backings. Spell out that `with_hugepages` (already-huge hugetlb pages) and `with_memory_backing_file` (file-backed for save/restore, not an anonymous memfd) force THP off, so callers that set transparent_hugepages and then enable those options don't expect it to take effect. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 8d243e0c-9ec6-4169-acd6-37d6afc78c8a --- petri/src/vm/mod.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/petri/src/vm/mod.rs b/petri/src/vm/mod.rs index 5da5ad84ad..965890160b 100644 --- a/petri/src/vm/mod.rs +++ b/petri/src/vm/mod.rs @@ -2348,11 +2348,17 @@ pub struct MemoryConfig { /// Mark guest RAM as eligible for Transparent Huge Pages (THP), /// improving performance for large allocations. /// - /// Defaults to `true`. Applies to both private anonymous and shared - /// (file/memfd) guest RAM, on Linux (via `madvise`) and on Windows (via - /// soft large pages). It is silently ignored where it does not apply, such - /// as explicit hugetlb/large-page (`with_hugepages`) backings, which are - /// already huge. + /// Defaults to `true`. Applies to private anonymous guest RAM and to + /// shared memfd-backed RAM, on Linux (via `madvise`) and on Windows (via + /// soft large pages). + /// + /// It has no effect where THP does not apply, and some backend helpers + /// force it off: + /// - [`with_hugepages`](crate::openvmm::PetriVmConfigOpenVmm::with_hugepages) + /// uses explicit hugetlb/large pages, which are already huge. + /// - [`with_memory_backing_file`](crate::openvmm::PetriVmConfigOpenVmm::with_memory_backing_file) + /// uses a file-backed mapping (for snapshot save/restore) rather than an + /// anonymous memfd. /// /// Only applies to the OpenVMM backend; ignored by Hyper-V. pub transparent_hugepages: bool, From 95648bbe5daa85e858d8b4efe1fa5eaecc305e5e Mon Sep 17 00:00:00 2001 From: John Starks Date: Thu, 23 Jul 2026 11:55:32 +0300 Subject: [PATCH 3/3] petri: stop forcing THP off in with_hugepages/with_memory_backing_file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The two shared-memory helpers set transparent_hugepages = false by hand. For with_hugepages that is dead code: membacking already suppresses THP for any hugetlb backing (transparent_hugepages && !hugepages), so the setting never reaches the mapping. For with_memory_backing_file it was hiding behavior rather than reflecting a real constraint — on Linux the madvise is a harmless no-op on a real file, and on Windows it simply lets soft large pages apply. If soft large pages are wrong for a file-backed mapping we want that to surface, not be silently masked. Let the requested THP value flow through in both helpers and trust membacking as the single source of truth for where THP applies. Revert the MemoryConfig doc to the simpler wording now that no helper contradicts it; membacking still transparently ignores THP for hugetlb backings. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 8d243e0c-9ec6-4169-acd6-37d6afc78c8a --- petri/src/vm/mod.rs | 13 ++++--------- petri/src/vm/openvmm/modify.rs | 2 -- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/petri/src/vm/mod.rs b/petri/src/vm/mod.rs index 965890160b..ec76b89c8b 100644 --- a/petri/src/vm/mod.rs +++ b/petri/src/vm/mod.rs @@ -2350,15 +2350,10 @@ pub struct MemoryConfig { /// /// Defaults to `true`. Applies to private anonymous guest RAM and to /// shared memfd-backed RAM, on Linux (via `madvise`) and on Windows (via - /// soft large pages). - /// - /// It has no effect where THP does not apply, and some backend helpers - /// force it off: - /// - [`with_hugepages`](crate::openvmm::PetriVmConfigOpenVmm::with_hugepages) - /// uses explicit hugetlb/large pages, which are already huge. - /// - [`with_memory_backing_file`](crate::openvmm::PetriVmConfigOpenVmm::with_memory_backing_file) - /// uses a file-backed mapping (for snapshot save/restore) rather than an - /// anonymous memfd. + /// soft large pages). It has no effect on explicit hugetlb/large-page + /// backings (see + /// [`with_hugepages`](crate::openvmm::PetriVmConfigOpenVmm::with_hugepages)), + /// which are already huge. /// /// Only applies to the OpenVMM backend; ignored by Hyper-V. pub transparent_hugepages: bool, diff --git a/petri/src/vm/openvmm/modify.rs b/petri/src/vm/openvmm/modify.rs index 12fb36d6fa..bead7f3c17 100644 --- a/petri/src/vm/openvmm/modify.rs +++ b/petri/src/vm/openvmm/modify.rs @@ -358,7 +358,6 @@ impl PetriVmConfigOpenVmm { for node in &mut self.config.numa.nodes { if let Some(mem) = &mut node.mem { mem.private_memory = false; - mem.transparent_hugepages = false; } } self @@ -383,7 +382,6 @@ impl PetriVmConfigOpenVmm { mem.hugepages = true; mem.hugepage_size = hugepage_size; mem.private_memory = false; - mem.transparent_hugepages = false; } } self