KVM: TDX huge page support for private memory
Subsystems (5)
v4 cover KVM: TDX huge page support for private memory
This is v4 of the TDX huge page series. This revision incorporates changes from Sean's combined DPAMT + Huge Pages series [1], addresses review feedback from v3 [0], fixes bugs, refines commit logs and code comments, isolates gmem-dependent glue code, and aligns SoB tags with tip tree preference.
As with prior versions, TDX huge page support requires the TDX module to report the ENHANCED_DEMOTE_INTERRUPTIBILITY feature. Additionally, this revision enables TDX huge pages only when both DPAMT and guest_memfd in-place conversion are enabled. (See the 'Base' section below for the dependency stack. A working branch is at [2]).
TDX huge page support relies on guest_memfd (gmem) as the backing huge page allocator. Pending the availability of upstream HugeTLB-based gmem, an out-of-tree allocator allocating 2MB folios from the buddy is used for testing. However, the core of this TDX huge page series is gmem-agnostic, independent of the specific gmem allocator.
In previous upstream planning discussions [3], Sean noted openness to landing this series (e.g., via a topic branch) ahead of in-tree huge page support in guest_memfd. To facilitate this, the gmem-dependent glue code is isolated into 4 patches tagged "GMEM-DEPENDENT" at the tail of this series.
The primary goals of this revision are to:
- Collect Reviewed-by tags (especially for tip-tree patches 1-2).
- Seek feedback and alignment from Sean on the following KVM/gmem changes:
* Adjusting the topup count of page pairs in PAMT cache for splitting S-EPT entries (patch 8).
* Splitting guest_memfd invalidation into stages: invalidate_start, prezap, zap, and invalidate_end (patches 14-15).
* Renaming the hook .gmem_convert() to .gmem_prezap() (referencing prior
discussions at [4]) (patch 15).
Earlier settled items
---------------------
1. TDX huge pages series depends on DPAMT series, and patches the TDX huge page series should not be divided based on whether they are DPAMT-related.
2. Only enable TDX huge pages when gmem in-place conversion is enabled.
3. Only enable TDX huge pages when the TDX module supports the ENHANCED_DEMOTE_INTERRUPTIBILITY feature.
4. Do not support page merging for the initial huge page support.
5. Hook splitting S-EPT entries to .set_external_spte(), and use PFN instead of folio/page to manage guest pages.
6. Handle the installation and uninstallation of external_spt's DPAMT pages when mapping, splitting, and unmapping S-EPT entries, rather than during page allocation and freeing.
7. Reuse the .topup_external_cache() hook to allocate PAMT pages for splitting, and use a mutex instead of a spinlock to protect the per-VM PAMT cache.
8. Add and export an API kvm_tdp_mmu_mirrors_split_huge_pages() to split mirror page tables for a given range.
9. Honor the guest's accept level by splitting any existing mapping higher than the guest's accept level, setting the guest inhibit bit after performing the split, and never clearing the flag. Take mmu_lock for write during the process to keep things simple.
10. Have gmem invoke arch code, which further invokes a gmem hook to trigger S-EPT entry splitting under a non-vCPU context. (Note: the gmem hook name and invocation sequence under a specific gmem implementation are not yet finalized.)
Main changes from v3
--------------------
1. Reorganized patches so that they are no longer divided by whether they are DPAMT-related.
2. Hooked to .set_external_spte() for splitting.
3. Added a dedicated API kvm_tdp_mmu_mirrors_split_huge_pages() for splitting mirror roots.
4. Reused the .topup_external_cache() hook to top up DPAMT pages used during S-EPT entry splitting. Extended the hook to work under both vCPU and non-vCPU contexts. Used a mutex instead of a spinlock to protect the per-VM PAMT cache under a non-vCPU context.
5. Had gmem trigger S-EPT splitting via the .gmem_prezap() hook.
6. Moved considerations of not supporting promotion from the cover letter to the patch log.
7. Dropped patches for working with per-VM memory attributes. Only enabled huge pages when gmem in-place conversions are enabled.
8. Based TDX huge pages on DPAMT. Simplified the DEMOTE SEAMCALL implementation by only enabling TDX huge pages when DPAMT is enabled.
9. Split out gmem glue code into patches tagged with "GMEM-DEPENDENT".
10. Fixed bugs:
- adjusted the topup PAMT page pair count for splitting.
- used a global spinlock to avoid contention between DEMOTE and PAMT.{ADD/REMOVE} SEAMCALLs.
11. Dropped cache-flush-related handling.
Patches Layout
--------------
Patches 1-2: Enhance/Introduce SEAMCALL wrappers/helpers to support huge
pages. (tip tree patches)
Patches 3-4: Enhance private pages reset and disallow page merging in the
mirror page table (KVM patches).
Patches 5-11: Support for splitting under vCPU context. (KVM patches)
Patches 12-16: Support for splitting under non-vCPU context. (KVM patches)
Patch 12: Core support to split S-EPT under non-vCPU context. Patches 13-14: Gmem refactors.
Patches 15-16: Have gmem trigger splitting under a non-vCPU
context.
Patch 17: Turns on TDX huge page. (KVM patch)
Base
----
This revision is based on kvm-x86-next-2026.09.22 (containing gmem stop returning struct page in pfn lookup series [7] and gmem in-place conversion series [8]), plus the following series in the stack (refer to branch [2]):
- DPAMT v11 [5].
- Drop unneeded cache flushing [6]
- gmem 2MB buddy [9] + an alignment fix and an out-of-place conversion workaround. *
*: For testing purposes, this revision uses the out-of-tree gmem 2MB buddy
solution as the huge page allocator.
The gmem 2MB buddy allocator allocates 2MB folios from the buddy for private memory, while the shared memory is allocated from a different backend (specified by HVA). To avoid fragmentation, private-to-shared conversions only split private mappings without splitting or reclaiming the backing 2MB folios. The 2MB folios are always retained in the gmem inode filemap cache without splitting even though part of them are not mapped as private memory.
Since the shared memory is not allocated from gmem (ensured by having gmem 2MB buddy allocator mode intentionally incompatible with the MMAP flag), commit 640d75f52828 ("KVM: guest_memfd: Always fault from guest_memfd if in-place conversion is enabled") is reverted in the testing branch [2] as a workaround to allow out-of-place conversions.
The gmem 2MB buddy solution and the workaround to allow out-of-place conversions do not affect the code of the TDX private huge page series. Both of them can be dropped once the final HugeTLB-based gmem is available (with some rebasing effort for GMEM-DEPENDENT patches).
Testing
-------
A TDX module enumerating the ENHANCED_DEMOTE_INTERRUPTIBILITY feature is required (AFAIK, TDX module 1.5.28 is the earliest version that reports this feature; For testing on versions prior to 1.5.28, a workaround patch to force enable TDX huge page can be applied [10]).
To test TDX huge pages, set the following module parameters at boot/load:
kvm.gmem_in_place_conversion=on kvm.gmem_2MB_private_mem_from_buddy_for_testing=on kvm_intel.tdx=on
kvm_intel.tdx_huge_page=on
The count of active 2MB mappings can be verified via:
/sys/kernel/debug/kvm/pages_2m
QEMU and TDX KVM selftests enhanced with in-place conversion uABI support are required.
Farrah Chen tested this stack on EMR platform with TDX module version 1.5.41.00.1153:
- 2MB huge pages were allocated and mapped as expected.
- Stress testing running 5 concurrent TDs alongside 5 standard VMs completed successfully with no issues observed.
- Stress testing running 63 concurrent TDs completed successfully with no issues observed.
Thanks
Yan
[0] v3: https://lore.kernel.org/all/[email protected]
[1] DPAMT + Hugepage: https://lore.kernel.org/all/[email protected]
[2] A working branch: https://github.com/intel-staging/tdx/tree/huge_page_v4
[3] Upstream planning: https://lore.kernel.org/all/[email protected]
[4] gmem_convert() discussion: https://lore.kernel.org/all/[email protected]
[5] DPAMT: https://lore.kernel.org/all/[email protected]/
[6] drop cache flush: https://lore.kernel.org/all/[email protected]
[7] gmem pfn lookup: https://lore.kernel.org/all/[email protected]
[8] gmem in-place conversion: https://lore.kernel.org/all/[email protected]
[9] gmem 2MB buddy: https://github.com/intel-staging/tdx/commit/5ba004e1b4af4d928acb7fde006195bbe84035a3
[10] workaround on old TDX module: https://github.com/intel-staging/tdx/commit/f1fe677b82e11fc97679a04e078b2fd05da55b34
Isaku Yamahata (1):
KVM: x86/tdp_mmu: Alloc external_spt page for mirror page table
splitting
Rick Edgecombe (1):
KVM: x86/mmu: Prevent huge page promotion for mirror roots in fault
path
Sean Christopherson (4):
KVM: x86/mmu: Allocate DPAMT pages for vCPU-induced page split
KVM: x86/mmu: Add a TDP MMU API to split huge pages for mirror roots [GMEM-DEPENDENT] KVM: guest_memfd: Add helpers to get start/end gfns
give gmem+slot+pgoff
[GMEM-DEPENDENT] KVM: guest_memfd: Add a pre-zap hook .gmem_prezap()
Yan Zhao (11):
x86/virt/tdx: Enhance tdx_pamt_get/put() to support huge pages x86/virt/tdx: Add a SEAMCALL wrapper to demote a 2MB huge page
KVM: TDX: Reset private huge pages after S-EPT page removal
KVM: TDX: Add core support for splitting/demoting 2MB S-EPT mappings
to 4KB
KVM: TDX: Adjust the topup count of DPAMT page pairs for splitting
S-EPT
KVM: x86/mmu: Introduce hugepage_set_guest_inhibit()
KVM: TDX: Honor the guest's accept level contained in an EPT violation
KVM: x86/mmu: Add support for splitting S-EPT entry under non-vCPU
context
[GMEM-DEPENDENT] KVM: guest_memfd: Split kvm_gmem_invalidate_start()
to start() and zap()
[GMEM-DEPENDENT] KVM: TDX: Implement .gmem_prezap() hook to split
S-EPT
KVM: TDX: Turn on PG_LEVEL_2M
arch/x86/include/asm/kvm-x86-ops.h | 3 + arch/x86/include/asm/kvm_host.h | 18 +- arch/x86/include/asm/tdx.h | 13 +- arch/x86/kvm/Kconfig | 1 + arch/x86/kvm/mmu.h | 4 + arch/x86/kvm/mmu/mmu.c | 28 ++- arch/x86/kvm/mmu/tdp_mmu.c | 79 +++++-- arch/x86/kvm/mmu/tdp_mmu.h | 2 + arch/x86/kvm/vmx/tdx.c | 322 +++++++++++++++++++++++++++-- arch/x86/kvm/vmx/tdx.h | 5 + arch/x86/kvm/vmx/tdx_arch.h | 3 + arch/x86/kvm/x86.c | 8 + arch/x86/virt/vmx/tdx/tdx.c | 94 ++++++++- arch/x86/virt/vmx/tdx/tdx.h | 1 + include/linux/kvm_host.h | 5 + include/linux/kvm_types.h | 1 + virt/kvm/Kconfig | 4 + virt/kvm/guest_memfd.c | 135 +++++++++++- 18 files changed, 662 insertions(+), 64 deletions(-)
Signature · 1 line
2.43.2
PATCH v4 1/17 x86/virt/tdx: Enhance tdx_pamt_get/put() to support huge pages
The TDX module uses PAMT to track metadata for every physical page (within TDMR) across three levels: 1GB, 2MB, and 4KB. The VMM is responsible for providing backing pages for the PAMT. Without Dynamic PAMT (DPAMT), all PAMT backing pages are allocated statically at system boot time. With DPAMT enabled, PAMT backing pages tracking physical pages at the 4KB level (referred to as DPAMT pages) are dynamically installed and uninstalled at runtime.
Currently, the VMM invokes tdx_pamt_get()/tdx_pamt_put() to install and uninstall DPAMT pages under the assumption that all physical pages are tracked at 4KB level. However, when a guest page is mapped as a huge page in the S-EPT, the TDX module tracks it directly at the huge page level, meaning installation and uninstallation of DPAMT pages for huge pages is neither needed nor applicable.
Add a "level" parameter to tdx_pamt_get()/tdx_pamt_put() so the helpers can skip DPAMT page installation and uninstallation for huge pages.
TD control pages (including S-EPT page table pages) are managed by the TDX module at the 4KB level. Therefore, pass PG_LEVEL_4K to tdx_pamt_get()/tdx_pamt_put() for those pages.
This patch is based on earlier work from Kiryl Shutsemau.
--- arch/x86/include/asm/tdx.h | 4 ++-- arch/x86/kvm/vmx/tdx.c | 14 +++++++++----- arch/x86/virt/vmx/tdx/tdx.c | 12 ++++++------ 3 files changed, 17 insertions(+), 13 deletions(-)
arch/x86/include/asm/tdx.h +2 -2
@@ -135,8 +135,8 @@ static inline void tdx_init_pamt_cache(struct tdx_pamt_cache *cache)void tdx_free_pamt_cache (struct tdx_pamt_cache *cache);int tdx_topup_pamt_cache (struct tdx_pamt_cache *cache,unsigned long npages); -int tdx_pamt_get (kvm_pfn_t pfn,struct tdx_pamt_cache *cache); -void tdx_pamt_put (kvm_pfn_t pfn); +int tdx_pamt_get (kvm_pfn_t pfn,enum pg_level level,struct tdx_pamt_cache *cache); +void tdx_pamt_put (kvm_pfn_t pfn,enum pg_level level);int tdx_guest_keyid_alloc (void );u32 tdx_get_nr_guest_keyids (void );
arch/x86/kvm/vmx/tdx.c +9 -5
@@ -1710,14 +1710,18 @@ static int tdx_sept_map_nonleaf_spte(struct kvm *kvm, gfn_t gfn,if (!sept_pt)return -EIO ; - ret =tdx_pamt_get (page_to_pfn (sept_pt), &to_tdx (vcpu)->pamt_cache); +/* +* An S-EPT page table page is tracked by the TDX module at 4KB level, +* so pass 4KB instead of the target S-EPT level. +*/ + ret =tdx_pamt_get (page_to_pfn (sept_pt),PG_LEVEL_4K , &to_tdx (vcpu)->pamt_cache);if (KVM_BUG_ON (ret, kvm))return ret; err =tdh_mem_sept_add (&to_kvm_tdx (kvm)->td, gpa, level, sept_pt, &entry, &level_state);if (err) -tdx_pamt_put (page_to_pfn (sept_pt)); +tdx_pamt_put (page_to_pfn (sept_pt),PG_LEVEL_4K );if (unlikely (tdx_operand_busy (err)))return -EBUSY ;
@@ -1745,7 +1749,7 @@ static int tdx_sept_map_leaf_spte(struct kvm *kvm, gfn_t gfn, enum pg_level leveWARN_ON_ONCE ((new_spte &VMX_EPT_RWX_MASK ) !=VMX_EPT_RWX_MASK ); - ret =tdx_pamt_get (pfn, &to_tdx (vcpu)->pamt_cache); + ret =tdx_pamt_get (pfn, level, &to_tdx (vcpu)->pamt_cache);if (KVM_BUG_ON (ret, kvm))return ret;
@@ -1767,7 +1771,7 @@ static int tdx_sept_map_leaf_spte(struct kvm *kvm, gfn_t gfn, enum pg_level leve ret =tdx_mem_page_add (kvm, gfn, level, pfn);if (ret) -tdx_pamt_put (pfn); +tdx_pamt_put (pfn, level);return ret; }
@@ -1862,7 +1866,7 @@ static int tdx_sept_remove_leaf_spte(struct kvm *kvm, gfn_t gfn,return -EIO ;tdx_quirk_reset_paddr (PFN_PHYS (pfn),PAGE_SIZE ); -tdx_pamt_put (pfn); +tdx_pamt_put (pfn, level);return 0 ; }
arch/x86/virt/vmx/tdx/tdx.c +6 -6
@@ -2112,14 +2112,14 @@ static u64 tdh_phymem_pamt_remove(kvm_pfn_t pfn, struct page **pamt_pages)static DEFINE_SPINLOCK (dpamt_lock);/* Bump DPAMT refcount for the given pfn and allocate DPAMT backing if needed. */ -int tdx_pamt_get (kvm_pfn_t pfn,struct tdx_pamt_cache *cache) +int tdx_pamt_get (kvm_pfn_t pfn,enum pg_level level,struct tdx_pamt_cache *cache) {struct page *pamt_pages[TDX_DPAMT_ENTRY_PAGE_CNT ];atomic_t *dpamt_refcount;u64 tdx_status;int ret; -if (!tdx_supports_dynamic_pamt (&tdx_sysinfo)) +if (!tdx_supports_dynamic_pamt (&tdx_sysinfo) || level !=PG_LEVEL_4K )return 0 ; ret =alloc_pamt_array (pamt_pages, cache);
@@ -2157,13 +2157,13 @@ int tdx_pamt_get(kvm_pfn_t pfn, struct tdx_pamt_cache *cache)EXPORT_SYMBOL_FOR_KVM (tdx_pamt_get);/* Drop DPAMT refcount for the given pfn and free DPAMT backing if needed. */ -void tdx_pamt_put (kvm_pfn_t pfn) +void tdx_pamt_put (kvm_pfn_t pfn,enum pg_level level) {struct page *pamt_pages[TDX_DPAMT_ENTRY_PAGE_CNT ] = {};atomic_t *dpamt_refcount;u64 tdx_status; -if (!tdx_supports_dynamic_pamt (&tdx_sysinfo)) +if (!tdx_supports_dynamic_pamt (&tdx_sysinfo) || level !=PG_LEVEL_4K )return ; dpamt_refcount =tdx_find_dpamt_refcount (pfn);
@@ -2243,7 +2243,7 @@ struct page *tdx_alloc_control_page(void)if (!page)return NULL ; -if (tdx_pamt_get (page_to_pfn (page),NULL )) { +if (tdx_pamt_get (page_to_pfn (page),PG_LEVEL_4K ,NULL )) {__free_page (page);return NULL ; }
@@ -2261,7 +2261,7 @@ void tdx_free_control_page(struct page *page)if (!page)return ; -tdx_pamt_put (page_to_pfn (page)); +tdx_pamt_put (page_to_pfn (page),PG_LEVEL_4K );__free_page (page); }EXPORT_SYMBOL_FOR_KVM (tdx_free_control_page);
Signature · 1 line
2.43.2
Edgecombe, Rick P
maintainerRe: [PATCH v4 01/17] x86/virt/tdx: Enhance tdx_pamt_get/put() to support huge pages
Quoted 15 lines · Yan Zhao
The TDX module uses PAMT to track metadata for every physical page (within TDMR) across three levels: 1GB, 2MB, and 4KB. The VMM is responsible for providing backing pages for the PAMT. Without Dynamic PAMT (DPAMT), all PAMT backing pages are allocated statically at system boot time. With DPAMT enabled, PAMT backing pages tracking physical pages at the 4KB level (referred to as DPAMT pages) are dynamically installed and uninstalled at runtime.
Currently, the VMM invokes tdx_pamt_get()/tdx_pamt_put() to install and uninstall DPAMT pages under the assumption that all physical pages are tracked at 4KB level. However, when a guest page is mapped as a huge page in the S-EPT, the TDX module tracks it directly at the huge page level, meaning installation and uninstallation of DPAMT pages for huge pages is neither needed nor applicable.
Quoted 2 lines
Add a "level" parameter to tdx_pamt_get()/tdx_pamt_put() so the helpers can skip DPAMT page installation and uninstallation for huge pages.
I guess this means we need to make sure to get/put at the right level that the TDX module is using it as. For example in this implementation, if a huge page gets demoted, we can't call them like this:
1. tdx_pamt_get(huge_pfn, PG_LEVEL_2MB)
2. demote(huge_pfn)
3. tdx_pamt_put(huge_pfn, PG_LEVEL_2MB)
So it is up to the caller to reason about what the mapping level is.
Which makes me wonder what is the point of passing level? The caller has to reason about what level the TDX module is using the memory at. The pamt_get/put() have to take their best guess at the ambiguity. For example line 3 above could actually free the PAMT, but it can't know to do that. Unless we added some internal tracking, but it seems too much.
There are only three places where a non-4KB level is passed to get/put. We could alternatively drop this patch and have KVM decide whether to call get/put or not. Since it is actually the only thing that knows the right page size.
What do you think?
Quoted 135 lines
TD control pages (including S-EPT page table pages) are managed by the TDX module at the 4KB level. Therefore, pass
PG_LEVEL_4Kto tdx_pamt_get()/tdx_pamt_put() for those pages.This patch is based on earlier work from Kiryl Shutsemau.
Signed-off-by Yan Zhao <[email protected]>--- arch/x86/include/asm/tdx.h | 4 ++-- arch/x86/kvm/vmx/tdx.c | 14 +++++++++----- arch/x86/virt/vmx/tdx/tdx.c | 12 ++++++------ 3 files changed, 17 insertions(+), 13 deletions(-)arch/x86/include/asm/tdx.h +2
-2@@ -135,8 +135,8 @@ static inline void tdx_init_pamt_cache(struct tdx_pamt_cache *cache)void tdx_free_pamt_cache (struct tdx_pamt_cache *cache);int tdx_topup_pamt_cache (struct tdx_pamt_cache *cache,unsigned long npages); -int tdx_pamt_get (kvm_pfn_t pfn,struct tdx_pamt_cache *cache); -void tdx_pamt_put (kvm_pfn_t pfn); +int tdx_pamt_get (kvm_pfn_t pfn,enum pg_level level,struct tdx_pamt_cache *cache); +void tdx_pamt_put (kvm_pfn_t pfn,enum pg_level level);int tdx_guest_keyid_alloc (void );u32 tdx_get_nr_guest_keyids (void );arch/x86/kvm/vmx/tdx.c +9
-5@@ -1710,14 +1710,18 @@ static int tdx_sept_map_nonleaf_spte(struct kvm *kvm, gfn_t gfn,if (!sept_pt)return -EIO ; - ret =tdx_pamt_get (page_to_pfn (sept_pt), &to_tdx (vcpu)->pamt_cache); +/* +* An S-EPT page table page is tracked by the TDX module at 4KB level, +* so pass 4KB instead of the target S-EPT level. +*/ + ret =tdx_pamt_get (page_to_pfn (sept_pt),PG_LEVEL_4K , &to_tdx (vcpu)->pamt_cache);if (KVM_BUG_ON (ret, kvm))return ret; err =tdh_mem_sept_add (&to_kvm_tdx (kvm)->td, gpa, level, sept_pt, &entry, &level_state);if (err) -tdx_pamt_put (page_to_pfn (sept_pt)); +tdx_pamt_put (page_to_pfn (sept_pt),PG_LEVEL_4K );if (unlikely (tdx_operand_busy (err)))return -EBUSY ;@@ -1745,7 +1749,7 @@ static int tdx_sept_map_leaf_spte(struct kvm *kvm, gfn_t gfn, enum pg_level leveWARN_ON_ONCE ((new_spte &VMX_EPT_RWX_MASK ) !=VMX_EPT_RWX_MASK ); - ret =tdx_pamt_get (pfn, &to_tdx (vcpu)->pamt_cache); + ret =tdx_pamt_get (pfn, level, &to_tdx (vcpu)->pamt_cache);if (KVM_BUG_ON (ret, kvm))return ret;@@ -1767,7 +1771,7 @@ static int tdx_sept_map_leaf_spte(struct kvm *kvm, gfn_t gfn, enum pg_level leve ret =tdx_mem_page_add (kvm, gfn, level, pfn);if (ret) -tdx_pamt_put (pfn); +tdx_pamt_put (pfn, level);return ret; }@@ -1862,7 +1866,7 @@ static int tdx_sept_remove_leaf_spte(struct kvm *kvm, gfn_t gfn,return -EIO ;tdx_quirk_reset_paddr (PFN_PHYS (pfn),PAGE_SIZE ); -tdx_pamt_put (pfn); +tdx_pamt_put (pfn, level);return 0 ; }arch/x86/virt/vmx/tdx/tdx.c +6
-6@@ -2112,14 +2112,14 @@ static u64 tdh_phymem_pamt_remove(kvm_pfn_t pfn, struct page **pamt_pages)static DEFINE_SPINLOCK (dpamt_lock);/* Bump DPAMT refcount for the given pfn and allocate DPAMT backing if needed. */ -int tdx_pamt_get (kvm_pfn_t pfn,struct tdx_pamt_cache *cache) +int tdx_pamt_get (kvm_pfn_t pfn,enum pg_level level,struct tdx_pamt_cache *cache) {struct page *pamt_pages[TDX_DPAMT_ENTRY_PAGE_CNT ];atomic_t *dpamt_refcount;u64 tdx_status;int ret; -if (!tdx_supports_dynamic_pamt (&tdx_sysinfo)) +if (!tdx_supports_dynamic_pamt (&tdx_sysinfo) || level !=PG_LEVEL_4K )return 0 ; ret =alloc_pamt_array (pamt_pages, cache);@@ -2157,13 +2157,13 @@ int tdx_pamt_get(kvm_pfn_t pfn, struct tdx_pamt_cache *cache)EXPORT_SYMBOL_FOR_KVM (tdx_pamt_get);/* Drop DPAMT refcount for the given pfn and free DPAMT backing if needed. */ -void tdx_pamt_put (kvm_pfn_t pfn) +void tdx_pamt_put (kvm_pfn_t pfn,enum pg_level level) {struct page *pamt_pages[TDX_DPAMT_ENTRY_PAGE_CNT ] = {};atomic_t *dpamt_refcount;u64 tdx_status; -if (!tdx_supports_dynamic_pamt (&tdx_sysinfo)) +if (!tdx_supports_dynamic_pamt (&tdx_sysinfo) || level !=PG_LEVEL_4K )return ; dpamt_refcount =tdx_find_dpamt_refcount (pfn);@@ -2243,7 +2243,7 @@ struct page *tdx_alloc_control_page(void)if (!page)return NULL ; -if (tdx_pamt_get (page_to_pfn (page),NULL )) { +if (tdx_pamt_get (page_to_pfn (page),PG_LEVEL_4K ,NULL )) {__free_page (page);return NULL ; }@@ -2261,7 +2261,7 @@ void tdx_free_control_page(struct page *page)if (!page)return ; -tdx_pamt_put (page_to_pfn (page)); +tdx_pamt_put (page_to_pfn (page),PG_LEVEL_4K );__free_page (page); }EXPORT_SYMBOL_FOR_KVM (tdx_free_control_page);
Re: [PATCH v4 01/17] x86/virt/tdx: Enhance tdx_pamt_get/put() to support huge pages
Quoted 19 lines · Rick P
Quoted 15 lines · Yan Zhao
The TDX module uses PAMT to track metadata for every physical page (within TDMR) across three levels: 1GB, 2MB, and 4KB. The VMM is responsible for providing backing pages for the PAMT. Without Dynamic PAMT (DPAMT), all PAMT backing pages are allocated statically at system boot time. With DPAMT enabled, PAMT backing pages tracking physical pages at the 4KB level (referred to as DPAMT pages) are dynamically installed and uninstalled at runtime.
Currently, the VMM invokes tdx_pamt_get()/tdx_pamt_put() to install and uninstall DPAMT pages under the assumption that all physical pages are tracked at 4KB level. However, when a guest page is mapped as a huge page in the S-EPT, the TDX module tracks it directly at the huge page level, meaning installation and uninstallation of DPAMT pages for huge pages is neither needed nor applicable.
Quoted 2 lines
Add a "level" parameter to tdx_pamt_get()/tdx_pamt_put() so the helpers can skip DPAMT page installation and uninstallation for huge pages.
Quoted 6 lines
I guess this means we need to make sure to get/put at the right level that the TDX module is using it as. For example in this implementation, if a huge page gets demoted, we can't call them like this:
1.tdx_pamt_get(huge_pfn, PG_LEVEL_2MB)
2.demote(huge_pfn)
3.tdx_pamt_put(huge_pfn, PG_LEVEL_2MB)
Actually, calling in this way is fine, since steps 1 & 3 are effectively no-ops. However, we can't call them like below.
1. tdx_pamt_get(huge_pfn, PG_LEVEL_2MB)
2. demote(huge_pfn)
3. tdx_pamt_put(huge_pfn, PG_LEVEL_4KB)
That's why 1 & 3 are not required and DPAMT pages management for the private guest page being demoted are handled in demote().
Quoted 1 line
So it is up to the caller to reason about what the mapping level is.
Yes.
Quoted 10 lines
Which makes me wonder what is the point of passing level? The caller has to reason about what level the TDX module is using the memory at. The pamt_get/put() have to take their best guess at the ambiguity. For example line 3 above could actually free the PAMT, but it can't know to do that. Unless we added some internal tracking, but it seems too much.
There are only three places where a non-4KB level is passed to get/put. We could alternatively drop this patch and have KVM decide whether to call get/put or not. Since it is actually the only thing that knows the right page size. What do you think?
Are you suggesting the implementation in v3 [1]?
In DPAMT combined v5, Sean moved the level checking from KVM to tdx_pamt_{get/put}() [2].
However, Sean's version introduced __tdx_pamt_{get/put}() for invocations where 4KB size is enforced.
During our internal review, you said:
"Can you also justify why to have two functions instead of page LEVEL_4K in a few places?"
So, compared to passing level to tdx_pamt_{get/put}() while introducing __tdx_pamt_{get/put}() for the always-4KB invocations, I updated the patch to drop __tdx_pamt_{get/put}(), having KVM pass 4KB directly in the always-4KB invocations.
I'm actually ok with any of these approaches. It seems they all require the caller to know about the right level size and whether to invoke PAMT get/put.
[1] https://lore.kernel.org/all/[email protected]
[2] https://lore.kernel.org/all/[email protected]
Edgecombe, Rick P
maintainerRe: [PATCH v4 01/17] x86/virt/tdx: Enhance tdx_pamt_get/put() to support huge pages
Quoted 20 lines · Yan Zhao
Quoted 19 lines · Rick P
Quoted 17 lines · Yan Zhao
The TDX module uses PAMT to track metadata for every physical page (within TDMR) across three levels: 1GB, 2MB, and 4KB. The VMM is responsible for providing backing pages for the PAMT. Without Dynamic PAMT (DPAMT), all PAMT backing pages are allocated statically at system boot time. With DPAMT enabled, PAMT backing pages tracking physical pages at the 4KB level (referred to as DPAMT pages) are dynamically installed and uninstalled at runtime.
Currently, the VMM invokes tdx_pamt_get()/tdx_pamt_put() to install and uninstall DPAMT pages under the assumption that all physical pages are tracked at 4KB level. However, when a guest page is mapped as a huge page in the S-EPT, the TDX module tracks it directly at the huge page level, meaning installation and uninstallation of DPAMT pages for huge pages is neither needed nor applicable.
Add a "level" parameter to tdx_pamt_get()/tdx_pamt_put() so the helpers can skip DPAMT page installation and uninstallation for huge pages.
Quoted 8 lines
Quoted 6 lines
I guess this means we need to make sure to get/put at the right level that the TDX module is using it as. For example in this implementation, if a huge page gets demoted, we can't call them like this:
1.tdx_pamt_get(huge_pfn, PG_LEVEL_2MB)
2.demote(huge_pfn)
3.tdx_pamt_put(huge_pfn, PG_LEVEL_2MB)Actually, calling in this way is fine, since steps 1 & 3 are effectively no-ops. However, we can't call them like below.
But step 2 incremented the refcounts. Then step 3 skips the decrement. So don't you need?
1. tdx_pamt_get(huge_pfn, PG_LEVEL_2MB)
2. demote(huge_pfn)
3. for (int i...) tdx_pamt_put(huge_pfn+i, PG_LEVEL_4KB);
Quoted 17 lines
1.
tdx_pamt_get(huge_pfn, PG_LEVEL_2MB)
2.demote(huge_pfn)
3.tdx_pamt_put(huge_pfn, PG_LEVEL_4KB)That's why 1 & 3 are not required and DPAMT pages management for the private guest page being demoted are handled in
demote().Quoted 1 line
So it is up to the caller to reason about what the mapping level is.
Yes.
Quoted 6 lines
Which makes me wonder what is the point of passing level? The caller has to reason about what level the TDX module is using the memory at. The pamt_get/put() have to take their best guess at the ambiguity. For example line 3 above could actually free the PAMT, but it can't know to do that. Unless we added some internal tracking, but it seems too much.
Quoted 5 lines
Quoted 4 lines
There are only three places where a non-4KB level is passed to get/put. We could alternatively drop this patch and have KVM decide whether to call get/put or not. Since it is actually the only thing that knows the right page size. What do you think?
Are you suggesting the implementation in v3 [1]?
Yea that was the path I was wandering down.
Quoted 10 lines
In DPAMT combined v5, Sean moved the level checking from KVM to tdx_pamt_{get/put}() [2].
However, Sean's version introduced __tdx_pamt_{get/put}() for invocations where 4KB size is enforced.During our internal review, you said:
"Can you also justify why to have two functions instead of pageLEVEL_4Kin a few places?"
Quoted 7 lines
So, compared to passing level to tdx_pamt_{get/put}() while introducing __tdx_pamt_{get/put}() for the always-4KB invocations, I updated the patch to drop __tdx_pamt_{get/put}(), having KVM pass 4KB directly in the always-4KB invocations.
I'm actually ok with any of these approaches. It seems they all require the caller to know about the right level size and whether to invoke PAMT get/put.
Yea. That is kind of the problem. The caller needs to understand the impact of the mapping level. The level arg isn't able wrap everything within the helpers. So how the get/put helpers handle it is internal details that caller needs to know. So there is abstraction leakage I think.
So probably the most important thing is that it is clear how to use it. Then whatever is the simplest code. It might be Kiryl's implementation. What do you think?
Or, hmm. It would be easy to catch misuse on the put side by (dpamt_refcount != 0 && level > PG_LEVEL_4K)? Then some comments to help user use the existing design correctly? Either of those seem ok. But my concern was that this part of the design wasn't clear. So most important is actually to explain the why of the design with an argument of the benefits.
Re: [PATCH v4 01/17] x86/virt/tdx: Enhance tdx_pamt_get/put() to support huge pages
Quoted 27 lines · Rick P
Quoted 26 lines · Yan Zhao
Quoted 25 lines · Rick P
Quoted 15 lines · Yan Zhao
The TDX module uses PAMT to track metadata for every physical page (within TDMR) across three levels: 1GB, 2MB, and 4KB. The VMM is responsible for providing backing pages for the PAMT. Without Dynamic PAMT (DPAMT), all PAMT backing pages are allocated statically at system boot time. With DPAMT enabled, PAMT backing pages tracking physical pages at the 4KB level (referred to as DPAMT pages) are dynamically installed and uninstalled at runtime.
Currently, the VMM invokes tdx_pamt_get()/tdx_pamt_put() to install and uninstall DPAMT pages under the assumption that all physical pages are tracked at 4KB level. However, when a guest page is mapped as a huge page in the S-EPT, the TDX module tracks it directly at the huge page level, meaning installation and uninstallation of DPAMT pages for huge pages is neither needed nor applicable.
Quoted 2 lines
Add a "level" parameter to tdx_pamt_get()/tdx_pamt_put() so the helpers can skip DPAMT page installation and uninstallation for huge pages.
I guess this means we need to make sure to get/put at the right level that the TDX module is using it as. For example in this implementation, if a huge page gets demoted, we can't call them like this:
1.tdx_pamt_get(huge_pfn, PG_LEVEL_2MB)
2.demote(huge_pfn)
3.tdx_pamt_put(huge_pfn, PG_LEVEL_2MB)
Quoted 8 lines
Quoted 2 lines
Actually, calling in this way is fine, since steps 1 & 3 are effectively no-ops. However, we can't call them like below.
But step 2 incremented the refcounts. Then step 3 skips the decrement. So don't you need?
1.tdx_pamt_get(huge_pfn, PG_LEVEL_2MB)
2.demote(huge_pfn)
3. for (int i...)tdx_pamt_put(huge_pfn+i, PG_LEVEL_4KB);
Oh, what I mean is there's no need for tdx_sept_split_leaf_spte() to invoke either 1 or 3. It only needs to invoke 2, though it doesn't break anything if tdx_sept_split_leaf_spte() incorrectly invokes tdx_pamt_{get/put}(huge_pfn, PG_LEVEL_2MB).
tdx_pamt_put(pfn, PG_LEVEL_4KB) for each 4K page is done in tdx_sept_remove_leaf_spte().
See more in the below contract.
Quoted 17 lines
Quoted 9 lines
In DPAMT combined v5, Sean moved the level checking from KVM to tdx_pamt_{get/put}() [2].
However, Sean's version introduced __tdx_pamt_{get/put}() for invocations where 4KB size is enforced.During our internal review, you said:
"Can you also justify why to have two functions instead of pageLEVEL_4Kin a few places?"Quoted 7 lines
So, compared to passing level to tdx_pamt_{get/put}() while introducing __tdx_pamt_{get/put}() for the always-4KB invocations, I updated the patch to drop __tdx_pamt_{get/put}(), having KVM pass 4KB directly in the always-4KB invocations.
I'm actually ok with any of these approaches. It seems they all require the caller to know about the right level size and whether to invoke PAMT get/put.
Quoted 8 lines
Yea. That is kind of the problem. The caller needs to understand the impact of the mapping level. The level arg isn't able wrap everything within the helpers. So how the get/put helpers handle it is internal details that caller needs to know. So there is abstraction leakage I think.
So probably the most important thing is that it is clear how to use it. Then whatever is the simplest code. It might be Kiryl's implementation. What do you think?
Hmm, I don't see checking level in KVM is better or simpler than passing the mapping level to tdx_pamt_{get/put}(). KVM still needs to know tdx_sept_split_leaf_spte() can't invoke tdx_pamt_get() for guest pages.
So, what about explaining the contract as a comment to tdx_pamt_{get/put}()? That is:
(1) KVM needs to invoke tdx_pamt_get() before it maps a new page (guest page or page table page).
So, KVM needs to invoke tdx_pamt_get() in tdx_sept_map_leaf_spte() for guest pages, and tdx_sept_map_nonleaf_spte() for page table pages, and tdx_sept_split_leaf_spte() needs to invoke tdx_pamt_get() for the page table page.
However, tdx_sept_split_leaf_spte() doesn't need to invoke tdx_pamt_get() for the guest page since the page has already been mapped.
(2) KVM needs to invoke tdx_pamt_put() after it unmaps a page (guest page or page table page.
So, KVM invokes tdx_pamt_put() in tdx_sept_remove_leaf_spte() and tdx_free_control_page() in tdx_sept_free_private_spt(). It also needs to invoke tdx_pamt_put() when it fails to map a page.
With the contract, it seems to be simpler for KVM to pass the level to tdx_pamt_{get/put}() ?
The dpamt refcount adjustment are invisible to KVM.
Do you think the contract is clear?
Quoted 5 lines
Or, hmm. It would be easy to catch misuse on the put side by (
dpamt_refcount!= 0 && level >PG_LEVEL_4K)? Then some comments to help user use the existing design correctly? Either of those seem ok. But my concern was that this part of the design wasn't clear. So most important is actually to explain the why of the design with an argument of the benefits.
If the above contract looks good to you, maybe I can explain the contract and design of the API in both the code comment and the patch log.
Edgecombe, Rick P
maintainerRe: [PATCH v4 01/17] x86/virt/tdx: Enhance tdx_pamt_get/put() to support huge pages
Quoted 95 lines · Yan Zhao
Quoted 27 lines · Rick P
Quoted 26 lines · Yan Zhao
Quoted 25 lines · Rick P
Quoted 15 lines · Yan Zhao
The TDX module uses PAMT to track metadata for every physical page (within TDMR) across three levels: 1GB, 2MB, and 4KB. The VMM is responsible for providing backing pages for the PAMT. Without Dynamic PAMT (DPAMT), all PAMT backing pages are allocated statically at system boot time. With DPAMT enabled, PAMT backing pages tracking physical pages at the 4KB level (referred to as DPAMT pages) are dynamically installed and uninstalled at runtime.
Currently, the VMM invokes tdx_pamt_get()/tdx_pamt_put() to install and uninstall DPAMT pages under the assumption that all physical pages are tracked at 4KB level. However, when a guest page is mapped as a huge page in the S-EPT, the TDX module tracks it directly at the huge page level, meaning installation and uninstallation of DPAMT pages for huge pages is neither needed nor applicable.
Quoted 2 lines
Add a "level" parameter to tdx_pamt_get()/tdx_pamt_put() so the helpers can skip DPAMT page installation and uninstallation for huge pages.
I guess this means we need to make sure to get/put at the right level that the TDX module is using it as. For example in this implementation, if a huge page gets demoted, we can't call them like this:
1.tdx_pamt_get(huge_pfn, PG_LEVEL_2MB)
2.demote(huge_pfn)
3.tdx_pamt_put(huge_pfn, PG_LEVEL_2MB)Quoted 8 lines
Quoted 2 lines
Actually, calling in this way is fine, since steps 1 & 3 are effectively no-ops. However, we can't call them like below.
But step 2 incremented the refcounts. Then step 3 skips the decrement. So don't you need?
1.tdx_pamt_get(huge_pfn, PG_LEVEL_2MB)
2.demote(huge_pfn)
3. for (int i...)tdx_pamt_put(huge_pfn+i, PG_LEVEL_4KB);Oh, what I mean is there's no need for
tdx_sept_split_leaf_spte()to invoke either 1 or 3. It only needs to invoke 2, though it doesn't break anything iftdx_sept_split_leaf_spte()incorrectly invokes tdx_pamt_{get/put}(huge_pfn,PG_LEVEL_2MB).
tdx_pamt_put(pfn, PG_LEVEL_4KB)for each 4K page is done intdx_sept_remove_leaf_spte().See more in the below contract.
Quoted 17 lines
Quoted 9 lines
In DPAMT combined v5, Sean moved the level checking from KVM to tdx_pamt_{get/put}() [2].
However, Sean's version introduced __tdx_pamt_{get/put}() for invocations where 4KB size is enforced.During our internal review, you said:
"Can you also justify why to have two functions instead of pageLEVEL_4Kin a few places?"Quoted 7 lines
So, compared to passing level to tdx_pamt_{get/put}() while introducing __tdx_pamt_{get/put}() for the always-4KB invocations, I updated the patch to drop __tdx_pamt_{get/put}(), having KVM pass 4KB directly in the always-4KB invocations.
I'm actually ok with any of these approaches. It seems they all require the caller to know about the right level size and whether to invoke PAMT get/put.
Quoted 8 lines
Yea. That is kind of the problem. The caller needs to understand the impact of the mapping level. The level arg isn't able wrap everything within the helpers. So how the get/put helpers handle it is internal details that caller needs to know. So there is abstraction leakage I think.
So probably the most important thing is that it is clear how to use it. Then whatever is the simplest code. It might be Kiryl's implementation. What do you think?
Hmm, I don't see checking level in KVM is better or simpler than passing the mapping level to tdx_pamt_{get/put}(). KVM still needs to know
tdx_sept_split_leaf_spte()can't invoketdx_pamt_get()for guest pages.So, what about explaining the contract as a comment to tdx_pamt_{get/put}()? That is:
(1) KVM needs to invoke
tdx_pamt_get()before it maps a new page (guest page or page table page).So, KVM needs to invoke
tdx_pamt_get()intdx_sept_map_leaf_spte()for guest pages, andtdx_sept_map_nonleaf_spte()for page table pages, andtdx_sept_split_leaf_spte()needs to invoketdx_pamt_get()for the page table page.
However,tdx_sept_split_leaf_spte()doesn't need to invoketdx_pamt_get()for the guest page since the page has already been mapped.(2) KVM needs to invoke
tdx_pamt_put()after it unmaps a page (guest page or page table page.So, KVM invokes
tdx_pamt_put()intdx_sept_remove_leaf_spte()andtdx_free_control_page()intdx_sept_free_private_spt(). It also needs to invoketdx_pamt_put()when it fails to map a page.
Quoted 5 lines
With the contract, it seems to be simpler for KVM to pass the level to tdx_pamt_{get/put}() ?
The dpamt refcount adjustment are invisible to KVM.Do you think the contract is clear?
No, it doesn't seem clear. It's not clear which combination of args will update the refcount or mapping level for different scenarios. The above seems to just say what recipes will work, which only makes sense since you know all the internal details. It doesn't describe a clear contact.
I think it is like - Pamt has a level, and three operations manipulate it. The pamt level needs to match what TDX will map the page at.
The pamt get/put infra keeps refcounts for 4KB level page use. They may change the mapping level, but they always update the refcount which is an explicit 4KB level thing. So to me it makes much more sense if they stay 4KB level operations.
Then the contract is simply:
There are only two possible mapping levels: 2MB and 4KB. There is a refcount for each 2MB aligned range. The mapping level will be 4KB when there is any refcount for 2MB aligned range.
tdx_pamt_get() increments the refcount tdx_pamt_put() decrements the refcount tdh_mem_page_demote() increments the refcount for each 4KB page
The mapping level will be determined by the refcount after each operation is complete.
If you want to pass the level, somehow it has to be clear how the refcount and mapping level will be adjusted for each combination of operations.
Quoted 8 lines
Quoted 6 lines
Or, hmm. It would be easy to catch misuse on the put side by (
dpamt_refcount!= 0 && level >PG_LEVEL_4K)? Then some comments to help user use the existing design correctly? Either of those seem ok. But my concern was that this part of the design wasn't clear. So most important is actually to explain the why of the design with an argument of the benefits.If the above contract looks good to you, maybe I can explain the contract and design of the API in both the code comment and the patch log.
Re: [PATCH v4 01/17] x86/virt/tdx: Enhance tdx_pamt_get/put() to support huge pages
Quoted 115 lines · Rick P
Quoted 95 lines · Yan Zhao
Quoted 27 lines · Rick P
Quoted 26 lines · Yan Zhao
Quoted 25 lines · Rick P
Quoted 15 lines · Yan Zhao
The TDX module uses PAMT to track metadata for every physical page (within TDMR) across three levels: 1GB, 2MB, and 4KB. The VMM is responsible for providing backing pages for the PAMT. Without Dynamic PAMT (DPAMT), all PAMT backing pages are allocated statically at system boot time. With DPAMT enabled, PAMT backing pages tracking physical pages at the 4KB level (referred to as DPAMT pages) are dynamically installed and uninstalled at runtime.
Currently, the VMM invokes tdx_pamt_get()/tdx_pamt_put() to install and uninstall DPAMT pages under the assumption that all physical pages are tracked at 4KB level. However, when a guest page is mapped as a huge page in the S-EPT, the TDX module tracks it directly at the huge page level, meaning installation and uninstallation of DPAMT pages for huge pages is neither needed nor applicable.
Quoted 2 lines
Add a "level" parameter to tdx_pamt_get()/tdx_pamt_put() so the helpers can skip DPAMT page installation and uninstallation for huge pages.
I guess this means we need to make sure to get/put at the right level that the TDX module is using it as. For example in this implementation, if a huge page gets demoted, we can't call them like this:
1.tdx_pamt_get(huge_pfn, PG_LEVEL_2MB)
2.demote(huge_pfn)
3.tdx_pamt_put(huge_pfn, PG_LEVEL_2MB)Quoted 8 lines
Quoted 2 lines
Actually, calling in this way is fine, since steps 1 & 3 are effectively no-ops. However, we can't call them like below.
But step 2 incremented the refcounts. Then step 3 skips the decrement. So don't you need?
1.tdx_pamt_get(huge_pfn, PG_LEVEL_2MB)
2.demote(huge_pfn)
3. for (int i...)tdx_pamt_put(huge_pfn+i, PG_LEVEL_4KB);Oh, what I mean is there's no need for
tdx_sept_split_leaf_spte()to invoke either 1 or 3. It only needs to invoke 2, though it doesn't break anything iftdx_sept_split_leaf_spte()incorrectly invokes tdx_pamt_{get/put}(huge_pfn,PG_LEVEL_2MB).
tdx_pamt_put(pfn, PG_LEVEL_4KB)for each 4K page is done intdx_sept_remove_leaf_spte().See more in the below contract.
Quoted 17 lines
Quoted 9 lines
In DPAMT combined v5, Sean moved the level checking from KVM to tdx_pamt_{get/put}() [2].
However, Sean's version introduced __tdx_pamt_{get/put}() for invocations where 4KB size is enforced.During our internal review, you said:
"Can you also justify why to have two functions instead of pageLEVEL_4Kin a few places?"Quoted 7 lines
So, compared to passing level to tdx_pamt_{get/put}() while introducing __tdx_pamt_{get/put}() for the always-4KB invocations, I updated the patch to drop __tdx_pamt_{get/put}(), having KVM pass 4KB directly in the always-4KB invocations.
I'm actually ok with any of these approaches. It seems they all require the caller to know about the right level size and whether to invoke PAMT get/put.
Quoted 8 lines
Yea. That is kind of the problem. The caller needs to understand the impact of the mapping level. The level arg isn't able wrap everything within the helpers. So how the get/put helpers handle it is internal details that caller needs to know. So there is abstraction leakage I think.
So probably the most important thing is that it is clear how to use it. Then whatever is the simplest code. It might be Kiryl's implementation. What do you think?
Hmm, I don't see checking level in KVM is better or simpler than passing the mapping level to tdx_pamt_{get/put}(). KVM still needs to know
tdx_sept_split_leaf_spte()can't invoketdx_pamt_get()for guest pages.So, what about explaining the contract as a comment to tdx_pamt_{get/put}()? That is:
(1) KVM needs to invoke
tdx_pamt_get()before it maps a new page (guest page or page table page).So, KVM needs to invoke
tdx_pamt_get()intdx_sept_map_leaf_spte()for guest pages, andtdx_sept_map_nonleaf_spte()for page table pages, andtdx_sept_split_leaf_spte()needs to invoketdx_pamt_get()for the page table page.
However,tdx_sept_split_leaf_spte()doesn't need to invoketdx_pamt_get()for the guest page since the page has already been mapped.(2) KVM needs to invoke
tdx_pamt_put()after it unmaps a page (guest page or page table page.So, KVM invokes
tdx_pamt_put()intdx_sept_remove_leaf_spte()andtdx_free_control_page()intdx_sept_free_private_spt(). It also needs to invoketdx_pamt_put()when it fails to map a page.Quoted 5 lines
With the contract, it seems to be simpler for KVM to pass the level to tdx_pamt_{get/put}() ?
The dpamt refcount adjustment are invisible to KVM.Do you think the contract is clear?
No, it doesn't seem clear. It's not clear which combination of args will update the refcount or mapping level for different scenarios. The above seems to just say what recipes will work, which only makes sense since you know all the internal details. It doesn't describe a clear contact.
I think it is like - Pamt has a level, and three operations manipulate it. The pamt level needs to match what TDX will map the page at.
The pamt get/put infra keeps refcounts for 4KB level page use. They may change the mapping level, but they always update the refcount which is an explicit 4KB level thing. So to me it makes much more sense if they stay 4KB level operations.
Quoted 8 lines
Then the contract is simply:
There are only two possible mapping levels: 2MB and 4KB. There is a refcount for each 2MB aligned range. The mapping level will be 4KB when there is any refcount for 2MB aligned range.
tdx_pamt_get()increments the refcount
tdx_pamt_put()decrements the refcount
I think refcounts should not appear in the contract. What is meaningful to KVM is not the refcount adjustment, but preparing and unpreparing PAMT pages for the physical page KVM wants to install/uninstall.
Whenever KVM invokes tdx_pamt_get(), it wants to ensure DPAMT pages are installed before installing a physical page; when KVM invokes tdx_pamt_put(), it wants to ensure DPAMT pages can be torn down correctly after uninstalling a physical page. So, perhaps a more accurate naming for tdx_pamt_get()/tdx_pamt_put() would be tdx_pamt_prepare()/tdx_pamt_unprepare()?
The refcounts are internal implementation details of tdx_pamt_{get/put}() and are hidden from KVM. It is tdx_pamt_{get/put}() that decides to use per-2MB refcounts to track the invocation count, and ensures that PAMT pages are (un)installed only once for each 2MB range.
The contract is therefore simple for KVM:
whenever KVM installs/uninstalls a physical page, invoke tdx_pamt_{get/put}() and specify the physical range by providing the level.
Quoted 1 line
tdh_mem_page_demote()increments the refcount for each 4KB page
Since tdh_mem_page_demote() only splits the mapping level of an already installed physical page (without removing any physical page), KVM does not need to invoke tdx_pamt_{get/put}() for the guest private page. As refcounts are hidden from KVM, KVM is unaware of the underlying refcount adjustment for the split huge page.
On the other hand, since tdh_mem_page_demote() will install a new page table page, KVM needs to invoke tdx_pamt_get() at level 4KB to prepare for the new page table page (which is 4KB) before invoking tdh_mem_page_demote().
Quoted 5 lines
The mapping level will be determined by the refcount after each operation is complete.
If you want to pass the level, somehow it has to be clear how the refcount and mapping level will be adjusted for each combination of operations.
When a level is passed to tdx_pamt_{get/put}(), it indicates that KVM wants to prepare/unprepare PAMT pages for a physical page at the specified level.
Edgecombe, Rick P
maintainerRe: [PATCH v4 01/17] x86/virt/tdx: Enhance tdx_pamt_get/put() to support huge pages
Quoted 10 lines · Yan Zhao
Quoted 10 lines
Then the contract is simply:
There are only two possible mapping levels: 2MB and 4KB. There is a refcount
for each 2MB aligned range. The mapping level will be 4KB when there is any
refcount for 2MB aligned range.
tdx_pamt_get()increments the refcount
tdx_pamt_put()decrements the refcount
Quoted 3 lines
I think refcounts should not appear in the contract. What is meaningful to KVM is not the refcount adjustment, but preparing and unpreparing PAMT pages for the physical page KVM wants to install/uninstall.
Well get and put are already refcount associated terms...
Quoted 7 lines
Whenever KVM invokes
tdx_pamt_get(), it wants to ensure DPAMT pages are installed before installing a physical page; when KVM invokestdx_pamt_put(), it wants to ensure DPAMT pages can be torn down correctly after uninstalling a physical page. So, perhaps a more accurate naming for tdx_pamt_get()/tdx_pamt_put() would be tdx_pamt_prepare()/tdx_pamt_unprepare()?
I don't see a reason to change the just settled pattern. It went through many rounds of reviews and no one ever objected to the get/put pattern. AFAICT the only scenario where it doesn't fit for huge pages if it is changed to accept args for other levels. But as before there is nothing to do for those other levels. And the callers already know this because they have to top up the cache appropriately. So there is no benefit that I can see. And following from that... no need to rename the functions already.
Quoted 26 lines
The refcounts are internal implementation details of tdx_pamt_{get/put}() and are hidden from KVM. It is tdx_pamt_{get/put}() that decides to use per-2MB refcounts to track the invocation count, and ensures that PAMT pages are (un)installed only once for each 2MB range.
The contract is therefore simple for KVM:
whenever KVM installs/uninstalls a physical page, invoke tdx_pamt_{get/put}() and specify the physical range by providing the level.Quoted 1 line
tdh_mem_page_demote()increments the refcount for each 4KB pageSince
tdh_mem_page_demote()only splits the mapping level of an already installed physical page (without removing any physical page), KVM does not need
to invoke tdx_pamt_{get/put}() for the guest private page. As refcounts are hidden from KVM, KVM is unaware of the underlying refcount adjustment for the split huge page.On the other hand, since
tdh_mem_page_demote()will install a new page table page, KVM needs to invoketdx_pamt_get()at level 4KB to prepare for the new page table page (which is 4KB) before invokingtdh_mem_page_demote().Quoted 4 lines
The mapping level will be determined by the refcount after each operation is
complete.
Quoted 5 lines
Quoted 3 lines
If you want to pass the level, somehow it has to be clear how the refcount and
mapping level will be adjusted for each combination of operations.When a level is passed to tdx_pamt_{get/put}(), it indicates that KVM wants to prepare/unprepare PAMT pages for a physical page at the specified level.
As already discussed, there is only one level where we would need to do anything.
Re: [PATCH v4 01/17] x86/virt/tdx: Enhance tdx_pamt_get/put() to support huge pages
Quoted 59 lines · Rick P
Quoted 10 lines · Yan Zhao
Quoted 10 lines
Then the contract is simply:
There are only two possible mapping levels: 2MB and 4KB. There is a refcount
for each 2MB aligned range. The mapping level will be 4KB when there is any
refcount for 2MB aligned range.
tdx_pamt_get()increments the refcount
tdx_pamt_put()decrements the refcountQuoted 3 lines
I think refcounts should not appear in the contract. What is meaningful to KVM is not the refcount adjustment, but preparing and unpreparing PAMT pages for the physical page KVM wants to install/uninstall.
Well get and put are already refcount associated terms...
Quoted 7 lines
Whenever KVM invokes
tdx_pamt_get(), it wants to ensure DPAMT pages are installed before installing a physical page; when KVM invokestdx_pamt_put(), it wants to ensure DPAMT pages can be torn down correctly after uninstalling a physical page. So, perhaps a more accurate naming for tdx_pamt_get()/tdx_pamt_put() would be tdx_pamt_prepare()/tdx_pamt_unprepare()?I don't see a reason to change the just settled pattern. It went through many rounds of reviews and no one ever objected to the get/put pattern. AFAICT the only scenario where it doesn't fit for huge pages if it is changed to accept args for other levels. But as before there is nothing to do for those other levels. And the callers already know this because they have to top up the cache appropriately. So there is no benefit that I can see. And following from that... no need to rename the functions already.
Quoted 26 lines
The refcounts are internal implementation details of tdx_pamt_{get/put}() and are hidden from KVM. It is tdx_pamt_{get/put}() that decides to use per-2MB refcounts to track the invocation count, and ensures that PAMT pages are (un)installed only once for each 2MB range.
The contract is therefore simple for KVM:
whenever KVM installs/uninstalls a physical page, invoke tdx_pamt_{get/put}() and specify the physical range by providing the level.Quoted 1 line
tdh_mem_page_demote()increments the refcount for each 4KB pageSince
tdh_mem_page_demote()only splits the mapping level of an already installed physical page (without removing any physical page), KVM does not need
to invoke tdx_pamt_{get/put}() for the guest private page. As refcounts are hidden from KVM, KVM is unaware of the underlying refcount adjustment for the split huge page.On the other hand, since
tdh_mem_page_demote()will install a new page table page, KVM needs to invoketdx_pamt_get()at level 4KB to prepare for the new page table page (which is 4KB) before invokingtdh_mem_page_demote().Quoted 4 lines
The mapping level will be determined by the refcount after each operation is
complete.
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
If you want to pass the level, somehow it has to be clear how the refcount and
mapping level will be adjusted for each combination of operations.When a level is passed to tdx_pamt_{get/put}(), it indicates that KVM wants to prepare/unprepare PAMT pages for a physical page at the specified level.
As already discussed, there is only one level where we would need to do anything.
Hmm. Ok. Let's drop this patch then and have KVM check the level instead, as in the v3 implementation:
https://lore.kernel.org/all/[email protected]
PATCH v4 2/17 x86/virt/tdx: Add a SEAMCALL wrapper to demote a 2MB huge page
In reply to Yan Zhaotl;dr: Introduce a SEAMCALL wrapper to invoke the TDH_MEM_PAGE_DEMOTE SEAMCALL for splitting a 2MB page mapped in S-EPT, and export it as an API.
The following assumptions are made by this wrapper:
1. The huge page is mapped at 2MB level in the S-EPT before invoking this wrapper.
2. DPAMT is enabled.
3. The TDX module supports uninterruptible demote.
The only expected error returned from this wrapper is TDX_OPERAND_BUSY. Callers may need to kick vCPUs out of guest mode to avoid this error. Callers must provide a pamt_cache for drawing DPAMT pages for guest memory and ensure that pages can be drawn from the pamt_cache locklessly in this wrapper.
Callers must ensure no concurrent invocations of this wrapper on the same GPA.
Long version:
Huge pages offer improved TLB efficiency, faster page walks, better memory contiguity, and reduced page fault and memory overhead (fewer intermediate page table structures, and no 4KB DPAMT pages). However, splitting/demoting a huge page is required in the following cases:
- when the guest accepts the private memory at a lower level,
- when part of the huge page is converted from private to shared, or
- when VMM wants to reclaim part of the huge page.
The TDH_MEM_PAGE_DEMOTE SEAMCALL demotes a guest private huge page by locating the corresponding leaf huge S-EPT entry and replacing it with a non-leaf S-EPT entry pointing to a new page table page, which holds the split leaf S-EPT entries. When DPAMT is enabled, upon successfully demoting a 2MB page to 4KB pages, the SEAMCALL performs DPAMT pages installation for the 2MB physical memory range, similar to the DPAMT pages installation performed by the TDH_PHYMEM_PAMT_ADD SEAMCALL.
The wrapper args "gpa", "level" specify the original huge leaf S-EPT entry; "new_sept_pt" specifies the newly added page table page; "pamt_cache" provides a pair of pages to gift to the TDX module as DPAMT pages; "pfn" is used to locate the dpamt_refcount; and the output args "ext_err1" and "ext_err2" allow the caller to retrieve SEAMCALL failure information.
Three assumptions are made by this wrapper (as in "tl;dr"). Assumption 1 holds because the VMM can only create mappings up to 2MB level in the S-EPT via the TDH_MEM_PAGE_AUG SEAMCALL. 1GB mappings in the S-EPT are only possible via the TDH_MEM_PAGE_PROMOTE SEAMCALL, which is not yet supported.
Assumptions 1 and 2 together simplify the wrapper implementation by eliminating the need to check whether DPAMT-related handling is required.
Assumption 3 is a tradeoff reached after research and discussion: a TDX module that does not support uninterruptible demote returns the error TDX_INTERRUPTED_RESTARTABLE if any pending host interrupts are detected during the TDH_MEM_PAGE_DEMOTE SEAMCALL. Since the TDX module cannot guarantee a maximum retry count to ensure forward progress of the demotion, interrupt storms could result in a DoS if the host retries endlessly. Disabling interrupts before invoking the TDH_MEM_PAGE_DEMOTE SEAMCALL also does not work, as the TDX module also checks for pending NMIs. Therefore, the tradeoff is to disable huge pages when the TDX module does not support uninterruptible demote. This is acceptable for basic TDX huge page enabling, since the SEAMCALL execution time under uninterruptible demote mode remains reasonable [1][2].
Later patches in KVM will enforce theses assumptions by setting the maximum mapping level to 2MB, disallowing page merging, and disabling TDX huge pages if assumption 2 or 3 is not met. This wrapper warns and returns TDX_SW_ERROR if any assumption is violated [6].
Due to assumptions 1 and 2, the wrapper always performs DPAMT-related handling before and after invoking the core helper __tdh_mem_page_demote():
- Draw a pair of pages from pamt_cache and gift them to the TDX module for DPAMT pages installation. The pamt_cache is a list of pre-allocated pages provided by the caller. The caller ensures that drawing pages from the list is contention-free, either by using a per-vCPU thread-local list or by holding a per-VM lock. If the SEAMCALL fails, the pair of pages is freed directly rather than being re-inserted into the list, for simplicity.
- Use the global spinlock dpamt_lock to avoid potential contention between the TDH_MEM_PAGE_DEMOTE and TDH_PHYMEM_PAMT_{ADD/REMOVE} SEAMCALLs [3]. The contention here can be cross-VM, similar to that between TDH_PHYMEM_PAMT_ADD and TDH_PHYMEM_PAMT_{ADD/REMOVE} [4]. dpamt_lock is acquired directly without any pre-testing, since a 2MB huge page mapped at the 2MB level in the S-EPT must have no existing dpamt_refcount before demotion unless there is a bug. For defensive programming, warn and return an error if dpamt_refcount is non-zero after acquiring dpamt_lock.
- Set dpamt_refcount to 512 upon successful demotion so that future invocations of tdx_pamt_put() when removing the split 4KB pages work correctly.
Note: The new_sept_pt page, which will be added as the S-EPT page table
page upon successful demotion, also requires DPAMT pages installation before invoking the TDH_MEM_PAGE_DEMOTE SEAMCALL. This wrapper relies on the caller to invoke tdx_pamt_get() separately for the new_sept_pt page prior to calling this wrapper.
Additionally, caller must ensure no concurrent invocations of this wrapper on the same GPA (for example, by holding KVM's write mmu_lock). Otherwise, a second invocation on the same GPA may encounter an incorrect dpamt_refcount or a SEAMCALL error caused by attempting to have the TDX module demote a 4KB page. This not only simplifies the wrapper implementation, but also prevents potential issues in the caller (for example, avoiding a level mismatch between the KVM mirror EPT entry and the corresponding S-EPT entry).
The only expected error returned from this wrapper is TDX_OPERAND_BUSY, caused by potential contention between the TDH_MEM_PAGE_DEMOTE SEAMCALL and TDH_VP_ENTER SEAMALL or guest TDCALLs operating on the S-EPT entry being split, due to lack of lock protection. This error is returned directly to the caller without retrying; the caller may handle it by kicking vCPUs and preventing them from re-entering guest mode to resolve the contention.
For defensive programming, the wrapper still takes arg "level" (though it's now always expected to be 2MB); a warning is issued before returning any unexpected errors, regardless of whether the caller would issue a similar warning [5][6].
This patch is based on earlier work from Xiaoyao Li, Isaku Yamahata, and Kiryl Shutsemau.
---
v4:
- Rebased to DPAMT v11.
- Merged in DPAMT-related handling. (Sean).
- Only allow demotion when DPAMT is enabled. (Rick)
- Protect DEMOTE and pamt_refcount with dpamt_lock. (Yan)
- ENHANCE to ENHANCED in TDX_FEATURES0_ENHANCED_DEMOTE_INTERRUPTIBILITY. (Kai)
- tdx_supports_demote_nointerrupt() --> tdx_huge_page_demote_uninterruptible(). (Kai)
- Updated the patch log/SoB to match tip's preference.
v3:
- Use a var name that clearly tell that the page is used as a page table page. (Binbin).
- Check if TDX module supports feature ENHANCE_DEMOTE_INTERRUPTIBILITY. (Kai).
RFC v2:
- Refine the patch log (Rick).
- Do not handle TDX_INTERRUPTED_RESTARTABLE as the new TDX modules in planning do not check interrupts for basic TDX.
RFC v1:
- Rebased and split patch. Updated patch log.
--- arch/x86/include/asm/tdx.h | 9 ++++ arch/x86/virt/vmx/tdx/tdx.c | 82 +++++++++++++++++++++++++++++++++++-- arch/x86/virt/vmx/tdx/tdx.h | 1 + 3 files changed, 89 insertions(+), 3 deletions(-)
arch/x86/include/asm/tdx.h +9 -0
@@ -37,6 +37,7 @@#define TDX_FEATURES0_TD_PRESERVING BIT_ULL(1)#define TDX_FEATURES0_NO_RBP_MOD BIT_ULL(18)#define TDX_FEATURES0_DYNAMIC_PAMT BIT_ULL(36) +#define TDX_FEATURES0_ENHANCED_DEMOTE_INTERRUPTIBILITY BIT_ULL(51)#ifndef __ASSEMBLER__
@@ -119,6 +120,11 @@ static inline bool tdx_supports_runtime_update(const struct tdx_sys_info *sysinfreturn sysinfo->features.tdx_features0 &TDX_FEATURES0_TD_PRESERVING ; } +static inline bool tdx_huge_page_demote_uninterruptible (const struct tdx_sys_info *sysinfo) +{ +return sysinfo->features.tdx_features0 &TDX_FEATURES0_ENHANCED_DEMOTE_INTERRUPTIBILITY ; +} +bool tdx_supports_dynamic_pamt (const struct tdx_sys_info *sysinfo);/* Simple structure for pre-allocating DPAMT pages outside of spinlocks. */
@@ -184,6 +190,9 @@ u64 tdh_mng_key_config(struct tdx_td *td);u64 tdh_mng_create (struct tdx_td *td,u16 hkid);u64 tdh_vp_create (struct tdx_td *td,struct tdx_vp *vp);u64 tdh_mng_rd (struct tdx_td *td,u64 field,u64 *data); +u64 tdh_mem_page_demote (struct tdx_td *td,u64 gpa,enum pg_level level,kvm_pfn_t pfn, +struct page *new_sept_pt,struct tdx_pamt_cache *pamt_cache, +u64 *ext_err1,u64 *ext_err2);u64 tdh_mr_extend (struct tdx_td *td,u64 gpa,u64 *ext_err1,u64 *ext_err2);u64 tdh_mr_finalize (struct tdx_td *td);u64 tdh_vp_flush (struct tdx_vp *vp);
arch/x86/virt/vmx/tdx/tdx.c +79 -3
106 lines not on this page · raw message
arch/x86/virt/vmx/tdx/tdx.h +1 -0
@@ -24,6 +24,7 @@#define TDH_MNG_KEY_CONFIG 8#define TDH_MNG_CREATE 9#define TDH_MNG_RD 11 +#define TDH_MEM_PAGE_DEMOTE 15#define TDH_MR_EXTEND 16#define TDH_MR_FINALIZE 17#define TDH_VP_FLUSH 18
Signature · 1 line
2.43.2
Edgecombe, Rick P
maintainerRe: [PATCH v4 02/17] x86/virt/tdx: Add a SEAMCALL wrapper to demote a 2MB huge page
Quoted 28 lines · Yan Zhao
tl;dr: Introduce a SEAMCALL wrapper to invoke the
TDH_MEM_PAGE_DEMOTESEAMCALL for splitting a 2MB page mapped in S-EPT, and export it as an API.The following assumptions are made by this wrapper:
1. The huge page is mapped at 2MB level in the S-EPT before invoking this wrapper.
2. DPAMT is enabled.
3. The TDX module supports uninterruptible demote.The only expected error returned from this wrapper is
TDX_OPERAND_BUSY. Callers may need to kick vCPUs out of guest mode to avoid this error. Callers must provide apamt_cachefor drawing DPAMT pages for guest memory and ensure that pages can be drawn from thepamt_cachelocklessly in this wrapper.
Callers must ensure no concurrent invocations of this wrapper on the same GPA.Long version:
Huge pages offer improved TLB efficiency, faster page walks, better memory contiguity, and reduced page fault and memory overhead (fewer intermediate page table structures, and no 4KB DPAMT pages). However, splitting/demoting a huge page is required in the following cases:
- when the guest accepts the private memory at a lower level,
- when part of the huge page is converted from private to shared, or
Quoted 1 line
- when VMM wants to reclaim part of the huge page.
^ what exactly is this case? You mean like calling hole punch because VMM decided guest is done with that memory?
Quoted 37 lines
The
TDH_MEM_PAGE_DEMOTESEAMCALL demotes a guest private huge page by locating the corresponding leaf huge S-EPT entry and replacing it with a non-leaf S-EPT entry pointing to a new page table page, which holds the split leaf S-EPT entries. When DPAMT is enabled, upon successfully demoting a 2MB page to 4KB pages, the SEAMCALL performs DPAMT pages installation for the 2MB physical memory range, similar to the DPAMT pages installation performed by theTDH_PHYMEM_PAMT_ADDSEAMCALL.The wrapper args "gpa", "level" specify the original huge leaf S-EPT entry; "
new_sept_pt" specifies the newly added page table page; "pamt_cache" provides a pair of pages to gift to the TDX module as DPAMT pages; "pfn" is used to locate thedpamt_refcount; and the output args "ext_err1" and "ext_err2" allow the caller to retrieve SEAMCALL failure information.Three assumptions are made by this wrapper (as in "tl;dr"). Assumption 1 holds because the VMM can only create mappings up to 2MB level in the S-EPT via the
TDH_MEM_PAGE_AUGSEAMCALL. 1GB mappings in the S-EPT are only possible via theTDH_MEM_PAGE_PROMOTESEAMCALL, which is not yet supported.Assumptions 1 and 2 together simplify the wrapper implementation by eliminating the need to check whether DPAMT-related handling is required.
Assumption 3 is a tradeoff reached after research and discussion: a TDX module that does not support uninterruptible demote returns the error
TDX_INTERRUPTED_RESTARTABLEif any pending host interrupts are detected during theTDH_MEM_PAGE_DEMOTESEAMCALL. Since the TDX module cannot guarantee a maximum retry count to ensure forward progress of the demotion, interrupt storms could result in a DoS if the host retries endlessly. Disabling interrupts before invoking theTDH_MEM_PAGE_DEMOTESEAMCALL also does not work, as the TDX module also checks for pending NMIs. Therefore, the tradeoff is to disable huge pages when the TDX module does not support uninterruptible demote. This is acceptable for basic TDX huge page enabling, since the SEAMCALL execution time under uninterruptible demote mode remains reasonable [1][2].
Quoted 2 lines
Later patches
Don't mention "patches". Instead "changes" because the patch is a temporary form.
Quoted 4 lines
in KVM will enforce theses assumptions by setting the maximum
mapping level to 2MB, disallowing page merging, and disabling TDX huge pages if assumption 2 or 3 is not met. This wrapper warns and returnsTDX_SW_ERRORif any assumption is violated [6].
If you call it tdh_mem_page_demote_2mb() as below, the assumption is self documenting. No need for handling other input.
Quoted 10 lines
Due to assumptions 1 and 2, the wrapper always performs DPAMT-related handling before and after invoking the core helper
__tdh_mem_page_demote():
- Draw a pair of pages frompamt_cacheand gift them to the TDX module for DPAMT pages installation. Thepamt_cacheis a list of pre-allocated pages provided by the caller. The caller ensures that drawing pages from the list is contention-free, either by using a per-vCPU thread-local list or by holding a per-VM lock. If the SEAMCALL fails, the pair of pages is freed directly rather than being re-inserted into the list, for simplicity.
pamt_cache is pretty straightforward. Could be a sentence or two of why.
Quoted 9 lines
- Use the global spinlock
dpamt_lockto avoid potential contention between theTDH_MEM_PAGE_DEMOTEand TDH_PHYMEM_PAMT_{ADD/REMOVE} SEAMCALLs [3]. The contention here can be cross-VM, similar to that betweenTDH_PHYMEM_PAMT_ADDand TDH_PHYMEM_PAMT_{ADD/REMOVE} [4].dpamt_lockis acquired directly without any pre-testing, since a 2MB huge page mapped at the 2MB level in the S-EPT must have no existingdpamt_refcountbefore demotion unless there is a bug. For defensive programming, warn and return an error ifdpamt_refcountis non-zero after acquiringdpamt_lock.
Im not on board with the reasoning of defensive programming in these. It is not enough to say to add something to be defensive. It needs to explain the likely or nasty possible bug case, and why nothing else will catch it. And a lot of these seem at odds with the existing code. Like demote path gets extra checks that regular paths don't have.
Quoted 10 lines
- Set
dpamt_refcountto 512 upon successful demotion so that future invocations oftdx_pamt_put()when removing the split 4KB pages work correctly.Note: The
new_sept_ptpage, which will be added as the S-EPT page table
page upon successful demotion, also requires DPAMT pages installation before invoking theTDH_MEM_PAGE_DEMOTESEAMCALL. This wrapper relies on the caller to invoketdx_pamt_get()separately for thenew_sept_ptpage prior to calling this wrapper.
Maybe organize it like explaining the two types of memory involved in the operation. The private mem and the page tables, and which ones are handled by this operation. Rather than putting it in a note. Start with a short description of the problem being solved by the why.
Quoted 10 lines
Additionally, caller must ensure no concurrent invocations of this wrapper on the same GPA (for example, by holding KVM's write
mmu_lock). Otherwise, a second invocation on the same GPA may encounter an incorrectdpamt_refcountor a SEAMCALL error caused by attempting to have the TDX module demote a 4KB page. This not only simplifies the wrapper implementation, but also prevents potential issues in the caller (for example, avoiding a level mismatch between the KVM mirror EPT entry and the corresponding S-EPT entry).
Quoted 6 lines
The only expected error returned from this wrapper is
TDX_OPERAND_BUSY, caused by potential contention between theTDH_MEM_PAGE_DEMOTESEAMCALL andTDH_VP_ENTERSEAMALL or guest TDCALLs operating on the S-EPT entry being split, due to lack of lock protection. This error is returned directly to the caller without retrying; the caller may handle it by kicking vCPUs and preventing them from re-entering guest mode to resolve the contention.
I'd put it that this is pretty much an existing issue, and KVM has the tools to handle it already.
Quoted 5 lines
For defensive programming, the wrapper still takes arg "level" (though it's now always expected to be 2MB); a warning is issued before returning any unexpected errors, regardless of whether the caller would issue a similar warning [5][6].
Or just call it tdh_mem_page_demote_2mb()
In general we should focus on the why here. How it works should be mostly clear from the code and comments.
Quoted 102 lines
This patch is based on earlier work from Xiaoyao Li, Isaku Yamahata, and Kiryl Shutsemau.
Signed-off-by Yan Zhao <[email protected]>---
v4:
- Rebased to DPAMT v11.
- Merged in DPAMT-related handling. (Sean).
- Only allow demotion when DPAMT is enabled. (Rick)
- Protect DEMOTE andpamt_refcountwithdpamt_lock. (Yan)
- ENHANCE to ENHANCED inTDX_FEATURES0_ENHANCED_DEMOTE_INTERRUPTIBILITY. (Kai)
-tdx_supports_demote_nointerrupt()-->tdx_huge_page_demote_uninterruptible(). (Kai)
- Updated the patch log/SoB to match tip's preference.v3:
- Use a var name that clearly tell that the page is used as a page table page. (Binbin).
- Check if TDX module supports featureENHANCE_DEMOTE_INTERRUPTIBILITY. (Kai).RFC v2:
- Refine the patch log (Rick).
- Do not handleTDX_INTERRUPTED_RESTARTABLEas the new TDX modules in planning do not check interrupts for basic TDX.RFC v1:
- Rebased and split patch. Updated patch log.--- arch/x86/include/asm/tdx.h | 9 ++++ arch/x86/virt/vmx/tdx/tdx.c | 82 +++++++++++++++++++++++++++++++++++-- arch/x86/virt/vmx/tdx/tdx.h | 1 + 3 files changed, 89 insertions(+), 3 deletions(-)arch/x86/include/asm/tdx.h +9
-0@@ -37,6 +37,7 @@#define TDX_FEATURES0_TD_PRESERVING BIT_ULL(1)#define TDX_FEATURES0_NO_RBP_MOD BIT_ULL(18)#define TDX_FEATURES0_DYNAMIC_PAMT BIT_ULL(36) +#define TDX_FEATURES0_ENHANCED_DEMOTE_INTERRUPTIBILITY BIT_ULL(51)#ifndef __ASSEMBLER__@@ -119,6 +120,11 @@ static inline bool tdx_supports_runtime_update(const struct tdx_sys_info *sysinfreturn sysinfo->features.tdx_features0 &TDX_FEATURES0_TD_PRESERVING ; } +static inline bool tdx_huge_page_demote_uninterruptible (const struct tdx_sys_info *sysinfo) +{ +return sysinfo->features.tdx_features0 &TDX_FEATURES0_ENHANCED_DEMOTE_INTERRUPTIBILITY ; +} +bool tdx_supports_dynamic_pamt (const struct tdx_sys_info *sysinfo);/* Simple structure for pre-allocating DPAMT pages outside of spinlocks. */ @@ -184,6 +190,9 @@ u64 tdh_mng_key_config(struct tdx_td *td);u64 tdh_mng_create (struct tdx_td *td,u16 hkid);u64 tdh_vp_create (struct tdx_td *td,struct tdx_vp *vp);u64 tdh_mng_rd (struct tdx_td *td,u64 field,u64 *data); +u64 tdh_mem_page_demote (struct tdx_td *td,u64 gpa,enum pg_level level,kvm_pfn_t pfn, +struct page *new_sept_pt,struct tdx_pamt_cache *pamt_cache, +u64 *ext_err1,u64 *ext_err2);u64 tdh_mr_extend (struct tdx_td *td,u64 gpa,u64 *ext_err1,u64 *ext_err2);u64 tdh_mr_finalize (struct tdx_td *td);u64 tdh_vp_flush (struct tdx_vp *vp);arch/x86/virt/vmx/tdx/tdx.c +6
-0@@ -75,6 +75,9 @@ static struct tdmr_info_list tdx_tdmr_list;*/ static atomic_t *dpamt_refcounts; +/* Serializes adding/removing DPAMT memory */ +static DEFINE_SPINLOCK (dpamt_lock); +/* All TDX-usable memory regions. Protected by mem_hotplug_lock. */ static LIST_HEAD (tdx_memlist);@@ -82,6 +85,9 @@ static struct tdx_sys_info tdx_sysinfo;static DEFINE_RAW_SPINLOCK (sysinit_lock); +static int alloc_pamt_array (struct page **pamt_pages,struct tdx_pamt_cache *cache); +static void free_pamt_array (struct page **pamt_pages); +/* * Do the module global initialization once and return its result. * It can be done on any cpu, and from task or IRQ context.
arch/x86/virt/vmx/tdx/tdx.c u64 tdh_mng_rd(struct tdx_td *td, u64 field, u64 *data) · line 1841
Quoted 9 lines
arch/x86/virt/vmx/tdx/tdx.c +6
-0@@ -1827,6 +1833,79 @@ u64 tdh_mng_rd(struct tdx_td *td, u64 field, u64 *data) }EXPORT_SYMBOL_FOR_KVM (tdh_mng_rd); +static u64 __tdh_mem_page_demote (struct tdx_td *td,u64 gpa,enum pg_level level, +struct page *new_sept_pt,struct page **dpamt_pages, +u64 *ext_err1,u64 *ext_err2) +{ +struct tdx_module_args args = { + .rcx = gpa |pg_level_to_tdx_sept_level (level),
we don't need to support anything other than 2MB, right?
Quoted 22 lines
arch/x86/virt/vmx/tdx/tdx.c +22
-0+ .rdx = tdx_tdr_pa(td), + .r8 = page_to_phys(new_sept_pt), + .r12 = page_to_phys(dpamt_pages[0]), + .r13 = page_to_phys(dpamt_pages[1]), + }; +u64 ret; + + ret =seamcall_saved_ret (TDH_MEM_PAGE_DEMOTE , &args); + + *ext_err1 = args.rcx; + *ext_err2 = args.rdx; +return ret; +} + +u64 tdh_mem_page_demote (struct tdx_td *td,u64 gpa,enum pg_level level,kvm_pfn_t pfn, +struct page *new_sept_pt,struct tdx_pamt_cache *pamt_cache, +u64 *ext_err1,u64 *ext_err2) +{ +atomic_t *dpamt_refcount =tdx_find_dpamt_refcount (pfn); +struct page *dpamt_pages[TDX_DPAMT_ENTRY_PAGE_CNT ]; +u64 ret; +
arch/x86/virt/vmx/tdx/tdx.c u64 tdh_mng_rd(struct tdx_td *td, u64 field, u64 *data) · line 1873
Quoted 10 lines
arch/x86/virt/vmx/tdx/tdx.c +10
-0+ *ext_err1 =0 ; + *ext_err2 =0 ; + +if (WARN_ON_ONCE (!tdx_huge_page_demote_uninterruptible (&tdx_sysinfo) || + !tdx_supports_dynamic_pamt (&tdx_sysinfo) || + level !=PG_LEVEL_2M )) +return TDX_SW_ERROR ; + +if (WARN_ON_ONCE (alloc_pamt_array (dpamt_pages, pamt_cache))) +return TDX_SW_ERROR ;
Why handle this differently than other pamt flows? It also looks like this warning was added since the last version I saw...
Quoted 4 lines
arch/x86/virt/vmx/tdx/tdx.c +4
-0+ +/* +* The dpamt_lock is used to avoid contention between the +* TDH_MEM_PAGE_DEMOTE and TDH_PHYMEM_PAMT_{ADD/REMOVE} SEAMCALLs.
arch/x86/virt/vmx/tdx/tdx.c u64 tdh_mng_rd(struct tdx_td *td, u64 field, u64 *data) · line 1887
Quoted 10 lines
arch/x86/virt/vmx/tdx/tdx.c +10
-0+*/ +spin_lock (&dpamt_lock); + +/* +* The caller ensures that DEMOTE is only invoked on a 2MB mapping, so +* dpamt_refcount must be 0. +*/ +if (WARN_ON_ONCE (atomic_read (dpamt_refcount))) { + ret =TDX_SW_ERROR ; + goto out_free;
What if we don't handle this case? It won't get caught by the (ret != TDX_SUCCESS) check below? I'm getting deja vu about all the defensive programming. I swear made this comment before.
arch/x86/virt/vmx/tdx/tdx.c u64 tdh_mng_rd(struct tdx_td *td, u64 field, u64 *data) · line 1897
Quoted 10 lines
arch/x86/virt/vmx/tdx/tdx.c +10
-0+ } + + ret =__tdh_mem_page_demote (td, gpa, level, new_sept_pt, dpamt_pages, + ext_err1, ext_err2); +if (ret !=TDX_SUCCESS ) { +WARN_ON_ONCE ((ret &TDX_SEAMCALL_STATUS_MASK ) !=TDX_OPERAND_BUSY ); + goto out_free; + } + +atomic_set (dpamt_refcount,PMD_SIZE /PAGE_SIZE );
This used to be PTRS_PER_PMD and we talked about setting it to 512 per the spec. in an internal review:
Decrementing PTRS_PER_PMD but the comment explicitly says 512. The TDX spec explicitly says 512, so I'd just do atomic_set(pamt_refcount, 512);
Now it has changed to PMD_SIZE / PAGE_SIZE. There seems to have been at least a few changes that missed the version log. What happened? AI review? We need to make sure we are not rolling back previous decisions silently...
Can you share more widely if there are new unmentioned changes in the rest of the series?
Quoted 36 lines
arch/x86/virt/vmx/tdx/tdx.c +11
-3+spin_unlock (&dpamt_lock); + +return TDX_SUCCESS ; + +out_free: +spin_unlock (&dpamt_lock); +free_pamt_array (dpamt_pages); +return ret; +} +EXPORT_SYMBOL_FOR_KVM (tdh_mem_page_demote); +u64 tdh_mr_extend (struct tdx_td *td,u64 gpa,u64 *ext_err1,u64 *ext_err2) {struct tdx_module_args args = {@@ -2108,9 +2187,6 @@ static u64 tdh_phymem_pamt_remove(kvm_pfn_t pfn, struct page **pamt_pages)return 0 ; } -/* Serializes adding/removing DPAMT memory */ -static DEFINE_SPINLOCK (dpamt_lock); -/* Bump DPAMT refcount for the given pfn and allocate DPAMT backing if needed. */ int tdx_pamt_get (kvm_pfn_t pfn,enum pg_level level,struct tdx_pamt_cache *cache) {arch/x86/virt/vmx/tdx/tdx.h +1
-0@@ -24,6 +24,7 @@#define TDH_MNG_KEY_CONFIG 8#define TDH_MNG_CREATE 9#define TDH_MNG_RD 11 +#define TDH_MEM_PAGE_DEMOTE 15#define TDH_MR_EXTEND 16#define TDH_MR_FINALIZE 17#define TDH_VP_FLUSH 18
Re: [PATCH v4 02/17] x86/virt/tdx: Add a SEAMCALL wrapper to demote a 2MB huge page
Quoted 29 lines · Rick P
Quoted 27 lines · Yan Zhao
tl;dr: Introduce a SEAMCALL wrapper to invoke the
TDH_MEM_PAGE_DEMOTESEAMCALL for splitting a 2MB page mapped in S-EPT, and export it as an API.The following assumptions are made by this wrapper:
1. The huge page is mapped at 2MB level in the S-EPT before invoking this wrapper.
2. DPAMT is enabled.
3. The TDX module supports uninterruptible demote.The only expected error returned from this wrapper is
TDX_OPERAND_BUSY. Callers may need to kick vCPUs out of guest mode to avoid this error. Callers must provide apamt_cachefor drawing DPAMT pages for guest memory and ensure that pages can be drawn from thepamt_cachelocklessly in this wrapper.
Callers must ensure no concurrent invocations of this wrapper on the same GPA.Long version:
Huge pages offer improved TLB efficiency, faster page walks, better memory contiguity, and reduced page fault and memory overhead (fewer intermediate page table structures, and no 4KB DPAMT pages). However, splitting/demoting a huge page is required in the following cases:
- when the guest accepts the private memory at a lower level,
- when part of the huge page is converted from private to shared, or
Quoted 4 lines
Quoted 1 line
- when VMM wants to reclaim part of the huge page.
^ what exactly is this case? You mean like calling hole punch because VMM decided guest is done with that memory?
"Yes, by reclaiming I was referring to operations causing specific folios to be removed from the gmem inode's filemap cache. e.g., the ones triggering hole punch.
Quoted 36 lines
Quoted 36 lines
The
TDH_MEM_PAGE_DEMOTESEAMCALL demotes a guest private huge page by locating the corresponding leaf huge S-EPT entry and replacing it with a non-leaf S-EPT entry pointing to a new page table page, which holds the split leaf S-EPT entries. When DPAMT is enabled, upon successfully demoting a 2MB page to 4KB pages, the SEAMCALL performs DPAMT pages installation for the 2MB physical memory range, similar to the DPAMT pages installation performed by theTDH_PHYMEM_PAMT_ADDSEAMCALL.The wrapper args "gpa", "level" specify the original huge leaf S-EPT entry; "
new_sept_pt" specifies the newly added page table page; "pamt_cache" provides a pair of pages to gift to the TDX module as DPAMT pages; "pfn" is used to locate thedpamt_refcount; and the output args "ext_err1" and "ext_err2" allow the caller to retrieve SEAMCALL failure information.Three assumptions are made by this wrapper (as in "tl;dr"). Assumption 1 holds because the VMM can only create mappings up to 2MB level in the S-EPT via the
TDH_MEM_PAGE_AUGSEAMCALL. 1GB mappings in the S-EPT are only possible via theTDH_MEM_PAGE_PROMOTESEAMCALL, which is not yet supported.Assumptions 1 and 2 together simplify the wrapper implementation by eliminating the need to check whether DPAMT-related handling is required.
Assumption 3 is a tradeoff reached after research and discussion: a TDX module that does not support uninterruptible demote returns the error
TDX_INTERRUPTED_RESTARTABLEif any pending host interrupts are detected during theTDH_MEM_PAGE_DEMOTESEAMCALL. Since the TDX module cannot guarantee a maximum retry count to ensure forward progress of the demotion, interrupt storms could result in a DoS if the host retries endlessly. Disabling interrupts before invoking theTDH_MEM_PAGE_DEMOTESEAMCALL also does not work, as the TDX module also checks for pending NMIs. Therefore, the tradeoff is to disable huge pages when the TDX module does not support uninterruptible demote. This is acceptable for basic TDX huge page enabling, since the SEAMCALL execution time under uninterruptible demote mode remains reasonable [1][2].
Quoted 5 lines
Quoted 2 lines
Later patches
Don't mention "patches". Instead "changes" because the patch is a temporary form.
Thanks. Will update it.
Quoted 7 lines
Quoted 4 lines
in KVM will enforce theses assumptions by setting the maximum
mapping level to 2MB, disallowing page merging, and disabling TDX huge pages if assumption 2 or 3 is not met. This wrapper warns and returnsTDX_SW_ERRORif any assumption is violated [6].If you call it
tdh_mem_page_demote_2mb()as below, the assumption is self documenting. No need for handling other input.
Good idea!
Since now the demote() implementation only works when DPAMT is enabled (due to your previous suggestion to get rid of "if/else" code for DPAMT), is it necessary to futher rename it to tdh_mem_page_demote_2mb_dpamt_enabled() ?
Quoted 11 lines
Quoted 9 lines
Due to assumptions 1 and 2, the wrapper always performs DPAMT-related handling before and after invoking the core helper
__tdh_mem_page_demote():
- Draw a pair of pages frompamt_cacheand gift them to the TDX module for DPAMT pages installation. Thepamt_cacheis a list of pre-allocated pages provided by the caller. The caller ensures that drawing pages from the list is contention-free, either by using a per-vCPU thread-local list or by holding a per-VM lock. If the SEAMCALL fails, the pair of pages is freed directly rather than being re-inserted into the list, for simplicity.
pamt_cacheis pretty straightforward. Could be a sentence or two of why.
Got it.
Quoted 9 lines
Quoted 8 lines
- Use the global spinlock
dpamt_lockto avoid potential contention between theTDH_MEM_PAGE_DEMOTEand TDH_PHYMEM_PAMT_{ADD/REMOVE} SEAMCALLs [3]. The contention here can be cross-VM, similar to that betweenTDH_PHYMEM_PAMT_ADDand TDH_PHYMEM_PAMT_{ADD/REMOVE} [4].dpamt_lockis acquired directly without any pre-testing, since a 2MB huge page mapped at the 2MB level in the S-EPT must have no existingdpamt_refcountbefore demotion unless there is a bug. For defensive programming, warn and return an error ifdpamt_refcountis non-zero after acquiringdpamt_lock.
Quoted 5 lines
Im not on board with the reasoning of defensive programming in these. It is not enough to say to add something to be defensive. It needs to explain the likely or nasty possible bug case, and why nothing else will catch it. And a lot of these seem at odds with the existing code. Like demote path gets extra checks that regular paths don't have.
Please check the explanation of "defensive programming" below.
Quoted 10 lines
Quoted 9 lines
- Set
dpamt_refcountto 512 upon successful demotion so that future invocations oftdx_pamt_put()when removing the split 4KB pages work correctly.Note: The
new_sept_ptpage, which will be added as the S-EPT page table
page upon successful demotion, also requires DPAMT pages installation before invoking theTDH_MEM_PAGE_DEMOTESEAMCALL. This wrapper relies on the caller to invoketdx_pamt_get()separately for thenew_sept_ptpage prior to calling this wrapper.
Quoted 4 lines
Maybe organize it like explaining the two types of memory involved in the operation. The private mem and the page tables, and which ones are handled by this operation. Rather than putting it in a note. Start with a short description of the problem being solved by the why.
Will do.
Quoted 16 lines
Quoted 9 lines
Additionally, caller must ensure no concurrent invocations of this wrapper on the same GPA (for example, by holding KVM's write
mmu_lock). Otherwise, a second invocation on the same GPA may encounter an incorrectdpamt_refcountor a SEAMCALL error caused by attempting to have the TDX module demote a 4KB page. This not only simplifies the wrapper implementation, but also prevents potential issues in the caller (for example, avoiding a level mismatch between the KVM mirror EPT entry and the corresponding S-EPT entry).Quoted 6 lines
The only expected error returned from this wrapper is
TDX_OPERAND_BUSY, caused by potential contention between theTDH_MEM_PAGE_DEMOTESEAMCALL andTDH_VP_ENTERSEAMALL or guest TDCALLs operating on the S-EPT entry being split, due to lack of lock protection. This error is returned directly to the caller without retrying; the caller may handle it by kicking vCPUs and preventing them from re-entering guest mode to resolve the contention.
Quoted 2 lines
I'd put it that this is pretty much an existing issue, and KVM has the tools to handle it already.
Ok.
Quoted 6 lines
Quoted 4 lines
For defensive programming, the wrapper still takes arg "level" (though it's now always expected to be 2MB); a warning is issued before returning any unexpected errors, regardless of whether the caller would issue a similar warning [5][6].
Or just call it
tdh_mem_page_demote_2mb()
With tdh_mem_page_demote_2mb(), are you ok with returning TDX_SW_ERROR due to level != 2MB?
Quoted 2 lines
In general we should focus on the why here. How it works should be mostly clear from the code and comments.
Explanation for "defensive programming":
In our internal review, I originall wrote the code as
if (!tdx_huge_page_demote_uninterruptible(&tdx_sysinfo) ||
!tdx_supports_dynamic_pamt(&tdx_sysinfo) || level != PG_LEVEL_2M)
goto out; // In "out:", *ext_err1/2 are set to 0 and TDX_SW_ERROR is returned.You commented that
"Hmm, this could all be a warnings I think. I guess it wades into Sean's new tendency to survive MMU errors. But do we think there is special risk here? What horrible thing can go wrong? I'd want a justification for why we need to handle this bug and not just report it via warning.
"
So, in this version, I set *ext_err1/2 to 0 by default and added WARN_ON_ONCE() to every unexpected error.
Quoted 6 lines
Quoted 6 lines
...
Quoted 8 lines
arch/x86/virt/vmx/tdx/tdx.c u64 tdh_mng_rd(struct tdx_td *td, u64 field, u64 *data) · line 1841
Quoted 6 lines
arch/x86/virt/vmx/tdx/tdx.c +6
-0+static u64 __tdh_mem_page_demote (struct tdx_td *td,u64 gpa,enum pg_level level, +struct page *new_sept_pt,struct page **dpamt_pages, +u64 *ext_err1,u64 *ext_err2) +{ +struct tdx_module_args args = { + .rcx = gpa |pg_level_to_tdx_sept_level (level),we don't need to support anything other than 2MB, right?
Yeah, with tdh_mem_page_demote_2mb(), I'll hardcode the level to 2MB here.
Quoted 27 lines
Quoted 27 lines
arch/x86/virt/vmx/tdx/tdx.c +27
-0+ .rdx = tdx_tdr_pa(td), + .r8 = page_to_phys(new_sept_pt), + .r12 = page_to_phys(dpamt_pages[0]), + .r13 = page_to_phys(dpamt_pages[1]), + }; +u64 ret; + + ret =seamcall_saved_ret (TDH_MEM_PAGE_DEMOTE , &args); + + *ext_err1 = args.rcx; + *ext_err2 = args.rdx; +return ret; +} + +u64 tdh_mem_page_demote (struct tdx_td *td,u64 gpa,enum pg_level level,kvm_pfn_t pfn, +struct page *new_sept_pt,struct tdx_pamt_cache *pamt_cache, +u64 *ext_err1,u64 *ext_err2) +{ +atomic_t *dpamt_refcount =tdx_find_dpamt_refcount (pfn); +struct page *dpamt_pages[TDX_DPAMT_ENTRY_PAGE_CNT ]; +u64 ret; + + *ext_err1 =0 ; + *ext_err2 =0 ; + + if (WARN_ON_ONCE(!tdx_huge_page_demote_uninterruptible(&tdx_sysinfo) || + !tdx_supports_dynamic_pamt(&tdx_sysinfo) ||
Quoted 8 lines
arch/x86/virt/vmx/tdx/tdx.c u64 tdh_mng_rd(struct tdx_td *td, u64 field, u64 *data) · line 1873
Quoted 5 lines
arch/x86/virt/vmx/tdx/tdx.c +5
-0+ level != PG_LEVEL_2M)) +return TDX_SW_ERROR ; + +if (WARN_ON_ONCE (alloc_pamt_array (dpamt_pages, pamt_cache))) +return TDX_SW_ERROR ;Why handle this differently than other pamt flows? It also looks like this warning was added since the last version I saw...
Right. The warning was added since v4 due to the internal review.
I added this WARN_ON_ONCE() after alloc_pamt_array() fails based on the following internal review comment:
"Hmm, so this makes me wonder, what kind of error do we want to return to KVM. Does KVM ever expect this to fail? We handle PAGE_AUG failing, but can we talk about how KVM will call this. What kind of failures does it want to know about and what kind of failures can it do something about? Otherwise to review this patch you need to look ahead to the code and understand the KVM MMU. Is it possible to give some justification in the log for why the error exposure is the way it is?"
I also noticed in the tdx_pamt_get() path, the return value of alloc_pamt_array() is passed back to KVM directly, triggering KVM_BUG_ON(). However, since Sean previously pointed out double warnings (i.e., both in demote() and KVM are acceptable): "Two WARNs isn't the end of the world. It might even be helpful in some cases, e.g. to more precisely document what went wrong." [5], I finally added WARN_ON_ONCE() in this case.
Should I also add a comment such as "The caller must ensure there are enough pages in the pamt_cache"?
Quoted 9 lines
Quoted 9 lines
arch/x86/virt/vmx/tdx/tdx.c +9
-0+ +/* +* The dpamt_lock is used to avoid contention between the +* TDH_MEM_PAGE_DEMOTE and TDH_PHYMEM_PAMT_{ADD/REMOVE} SEAMCALLs. +*/ +spin_lock (&dpamt_lock); + +/* +* The caller ensures that DEMOTE is only invoked on a 2MB mapping, so
Quoted 8 lines
arch/x86/virt/vmx/tdx/tdx.c u64 tdh_mng_rd(struct tdx_td *td, u64 field, u64 *data) · line 1887
Quoted 5 lines
arch/x86/virt/vmx/tdx/tdx.c +5
-0+* dpamt_refcount must be 0. +*/ +if (WARN_ON_ONCE (atomic_read (dpamt_refcount))) { + ret =TDX_SW_ERROR ; + goto out_free;What if we don't handle this case? It won't get caught by the (ret !=
TDX_SUCCESS) check below? I'm getting deja vu about all the defensive
It should be caught due to DEMOTE seamcall failure.
However, it makes me uncomfortable to set dpamt_refcount to 512 without first asserting that it's 0.
Quoted 1 line
programming. I swear made this comment before.
You are right. Re-reading your comment made me realize I had missed that, as the error handling in the previous internal version was convoluted.
Quoted 7 lines
Quoted 7 lines
arch/x86/virt/vmx/tdx/tdx.c +7
-0+ } + + ret =__tdh_mem_page_demote (td, gpa, level, new_sept_pt, dpamt_pages, + ext_err1, ext_err2); +if (ret !=TDX_SUCCESS ) { +WARN_ON_ONCE ((ret &TDX_SEAMCALL_STATUS_MASK ) !=TDX_OPERAND_BUSY ); + goto out_free;
Quoted 8 lines
arch/x86/virt/vmx/tdx/tdx.c u64 tdh_mng_rd(struct tdx_td *td, u64 field, u64 *data) · line 1897
Quoted 3 lines
arch/x86/virt/vmx/tdx/tdx.c +3
-0+ } + +atomic_set (dpamt_refcount,PMD_SIZE /PAGE_SIZE );This used to be
PTRS_PER_PMDand we talked about setting it to 512 per the spec. in an internal review:
DecrementingPTRS_PER_PMDbut the comment explicitly says 512. The TDX spec explicitly says 512, so I'd just doatomic_set(pamt_refcount, 512);
I did replace PTRS_PER_PMD with 512 after your comment. Then internally, Sashiko suggested:
"This isn't a bug, but would it be worth expressing the 512 symbolically rather than as a bare literal here?
The seed value has to match the granularity used elsewhere in this file. tdx_find_dpamt_refcount() indexes with:
pfn >> (PMD_SHIFT - PAGE_SHIFT)
and the array is sized with PTRS_PER_PTE. Each later tdx_pamt_put() on a 4KB page decrements this refcount one-by-one, and only the final 1->0 transition fires TDH_PHYMEM_PAMT_REMOVE, so this seed represents the count of 4KB sub-pages in the 2MB region.
On x86-64 PTRS_PER_PTE == 512 == PMD_SIZE/PAGE_SIZE, so the value is correct today. Using PTRS_PER_PTE (or a named define) instead of the literal 512 would keep this seed in lockstep with the indexing logic if the PMD/page geometry ever changes. Was a bare literal preferred here for a reason?"
Given that the TDX spec refers to 512 as: "The host VMM can split the mapping of a single 2MB or 1GB page to 512 consecutive 4KB or 2MB pages...", I think using PMD_SIZE / PAGE_SIZE would be more appropriate here (though personally I think PTRS_PER_PMD is also fine).
I didn't consider it as a big issue so I didn't mention it in the chagnelog.
Quoted 3 lines
Now it has changed to
PMD_SIZE/PAGE_SIZE. There seems to have been at least a few changes that missed the version log. What happened? AI review? We need to make sure we are not rolling back previous decisions silently...
Will mark this kind of change next time.
Quoted 2 lines
Can you share more widely if there are new unmentioned changes in the rest of the series?
Besides the main changes listed in the "Main changes from v3" section of the changelog, and those already documented in the changelog of each patch, the following changes vs v3 are undocumented:
Patch 1: added a level arg and have the callee to decide whether to skip PAMT
add/put.
Patch 2: - PTRS_PER_PMD to PMD_SIZE / PAGE_SIZE,
- atomic_cmpxchg_release() to atomic_set() due to the spinlock protection.
- added a helper __tdh_mem_page_demote().
Patch 3: implementation is much simpler due to the the base code changes
introduced by the previous MMU refactor and folio/page to pfn series.
Patch 4: - Moved is_mirror_sp() immediately above disallowed_hugepage_adjust()
instead of saving it in a local variable.
- added a comment explaining the disallowing reason.
Edgecombe, Rick P
maintainerRe: [PATCH v4 02/17] x86/virt/tdx: Add a SEAMCALL wrapper to demote a 2MB huge page
Quoted 2 lines · Yan Zhao
Quoted 1 line
we don't need to support anything other than 2MB, right?
Yeah, with
tdh_mem_page_demote_2mb(), I'll hardcode the level to 2MB here.
Just to be sure... We don't need the level arg anymore, so don't need to warn about it.
Quoted 41 lines
Quoted 31 lines
Quoted 31 lines
arch/x86/virt/vmx/tdx/tdx.c +15
-0+ .rdx = tdx_tdr_pa(td), + .r8 = page_to_phys(new_sept_pt), + .r12 = page_to_phys(dpamt_pages[0]), + .r13 = page_to_phys(dpamt_pages[1]), + }; +u64 ret; + + ret =seamcall_saved_ret (TDH_MEM_PAGE_DEMOTE , &args); + + *ext_err1 = args.rcx; + *ext_err2 = args.rdx; +return ret; +} + +u64 tdh_mem_page_demote(struct tdx_td *td, u64 gpa, enum pg_level level,
kvm_pfn_tpfn,(quoted diff) +1
-0+ struct page *new_sept_pt, struct tdx_pamt_cache
*pamt_cache,arch/x86/virt/vmx/tdx/tdx.c +10
-0+ u64 *ext_err1, u64 *ext_err2) +{ +atomic_t *dpamt_refcount =tdx_find_dpamt_refcount (pfn); +struct page *dpamt_pages[TDX_DPAMT_ENTRY_PAGE_CNT ]; +u64 ret; + + *ext_err1 =0 ; + *ext_err2 =0 ; + + if(WARN_ON_ONCE(!tdx_huge_page_demote_uninterruptible(&tdx_sysinfo) ||
arch/x86/virt/vmx/tdx/tdx.c +1
-0+ !tdx_supports_dynamic_pamt(&tdx_sysinfo) ||Quoted 8 lines
arch/x86/virt/vmx/tdx/tdx.c u64 tdh_mng_rd(struct tdx_td *td, u64 field, u64 *data) · line 1873
Quoted 5 lines
arch/x86/virt/vmx/tdx/tdx.c +5
-0+ level != PG_LEVEL_2M)) +return TDX_SW_ERROR ; + +if (WARN_ON_ONCE (alloc_pamt_array (dpamt_pages, pamt_cache))) +return TDX_SW_ERROR ;Why handle this differently than other pamt flows? It also looks like this warning was added since the last version I saw...
Right. The warning was added since v4 due to the internal review.
Quoted 9 lines
I added this
WARN_ON_ONCE()afteralloc_pamt_array()fails based on the following internal review comment:
"Hmm, so this makes me wonder, what kind of error do we want to return to KVM. Does KVM ever expect this to fail? We handlePAGE_AUGfailing, but can we talk about how KVM will call this. What kind of failures does it want to know about and what kind of failures can it do something about? Otherwise to review this patch you need to look ahead to the code and understand the KVM MMU. Is it possible to give some justification in the log for why the error exposure is the way it is?"
I don't see the connection to turning that into a warning.
But besides that, please don't silently churn the code like this. Especially when you provide a version log, because it kind of implies that nothing else was changed.
Quoted 9 lines
I also noticed in the
tdx_pamt_get()path, the return value ofalloc_pamt_array()
is passed back to KVM directly, triggeringKVM_BUG_ON(). However, since Sean previously pointed out double warnings (i.e., both indemote()and KVM are acceptable): "Two WARNs isn't the end of the world. It might even be helpful in
some cases, e.g. to more precisely document what went wrong." [5], I finally addedWARN_ON_ONCE()in this case.
"Two warns isn't the end of the world" is saying something different than "add two warns when you can". So why do that here? If we have code that doesn't have a clear reason it is going to be hard to get this patch acked. Kind of what I was trying to say in the quote above.
Quoted 3 lines
Should I also add a comment such as "The caller must ensure there are enough pages in the
pamt_cache"?
I think it's obvious. That is the whole point of the cache.
Quoted 2 lines
It looks like Sean was saying to warn about subtle contract breaking. He also says in that thread:
Document what exactly? Most of this should be common sense. E.g. we don't generally document that pointers must be non-NULL, because that goes without saying 99.9% of the time.
But besides that we are trying to get Dave's ack here. So I don't think extrapolating a comment Sean made is going to suffecient for having code that is different than the rest without explanation. So let's think about how to get that ack. Obviously we should take Sean's input as well. But while we are on the maintainer boundary, we can't call it settled.
Quoted 21 lines
Quoted 13 lines
Quoted 13 lines
arch/x86/virt/vmx/tdx/tdx.c +4
-0+ +/* +* The dpamt_lock is used to avoid contention between the +* TDH_MEM_PAGE_DEMOTE and TDH_PHYMEM_PAMT_{ADD/REMOVE} SEAMCALLs.
arch/x86/virt/vmx/tdx/tdx.c +5
-0+*/ +spin_lock (&dpamt_lock); + +/* +* The caller ensures that DEMOTE is only invoked on a 2MB mapping, so
Quoted 8 lines
arch/x86/virt/vmx/tdx/tdx.c u64 tdh_mng_rd(struct tdx_td *td, u64 field, u64 *data) · line 1887
Quoted 5 lines
arch/x86/virt/vmx/tdx/tdx.c +5
-0+* dpamt_refcount must be 0. +*/ +if (WARN_ON_ONCE (atomic_read (dpamt_refcount))) { + ret =TDX_SW_ERROR ; + goto out_free;What if we don't handle this case? It won't get caught by the (ret !=
TDX_SUCCESS) check below? I'm getting deja vu about all the defensive
Quoted 3 lines
It should be caught due to DEMOTE seamcall failure.
However, it makes me uncomfortable to setdpamt_refcountto 512 without first asserting that it's 0.
Hmm, yea it seems like good check to have. But I'm doubting how much we should handle the error. Vs just warn about it. Not sure...
Quoted 47 lines
Quoted 2 lines
programming. I swear made this comment before.
You are right. Re-reading your comment made me realize I had missed that, as the error handling in the previous internal version was convoluted.
Quoted 11 lines
Quoted 11 lines
arch/x86/include/asm/tdx.h +3
-0+ } + + ret = __tdh_mem_page_demote(td, gpa, level, new_sept_pt,
dpamt_pages,arch/x86/virt/vmx/tdx/tdx.c +3
-0+ ext_err1, ext_err2); +if (ret !=TDX_SUCCESS ) { + WARN_ON_ONCE((ret & TDX_SEAMCALL_STATUS_MASK) !=
TDX_OPERAND_BUSY);arch/x86/virt/vmx/tdx/tdx.c +3
-0+ goto out_free; + } +Quoted 8 lines
arch/x86/virt/vmx/tdx/tdx.c u64 tdh_mng_rd(struct tdx_td *td, u64 field, u64 *data) · line 1897
Quoted 1 line
arch/x86/virt/vmx/tdx/tdx.c +1
-0+atomic_set (dpamt_refcount,PMD_SIZE /PAGE_SIZE );This used to be
PTRS_PER_PMDand we talked about setting it to 512 per the spec.
in an internal review:
DecrementingPTRS_PER_PMDbut the comment explicitly says 512. The TDX spec
explicitly says 512, so I'd just doatomic_set(pamt_refcount, 512);I did replace
PTRS_PER_PMDwith 512 after your comment. Then internally, Sashiko
suggested:"This isn't a bug, but would it be worth expressing the 512 symbolically rather than as a bare literal here?
The seed value has to match the granularity used elsewhere in this file.
tdx_find_dpamt_refcount()indexes with:pfn >> (
PMD_SHIFT-PAGE_SHIFT)and the array is sized with
PTRS_PER_PTE. Each latertdx_pamt_put()on a 4KB page decrements this refcount one-by-one, and only the final 1->0 transition firesTDH_PHYMEM_PAMT_REMOVE, so this seed represents the count of 4KB sub-pages in the 2MB region.On x86-64
PTRS_PER_PTE== 512 == PMD_SIZE/PAGE_SIZE, so the value is correct today. UsingPTRS_PER_PTE(or a named define) instead of the literal 512 would keep this seed in lockstep with the indexing logic if the PMD/page geometry ever changes. Was a bare literal preferred here for a reason?"
Quoted 6 lines
Given that the TDX spec refers to 512 as: "The host VMM can split the mapping of a single 2MB or 1GB page to 512 consecutive 4KB or 2MB pages...", I think using
PMD_SIZE/PAGE_SIZEwould be more appropriate here (though personally I thinkPTRS_PER_PMDis also fine).I didn't consider it as a big issue so I didn't mention it in the chagnelog.
I think it kinda is actually. It was a point of discussion that got settled and then silently changed. This makes it hard to review successive versions of the series. It all has to be reviewed from scratch.
The right thing to do would have been to re-open that thread where it got closed. Sashiko doesn't get a style vote. but you do if you changed your mind. But changing your mind is not a reason to silently change the code to something other than discussed.
My pref would be 512 or PTRS_PER_PMD before PMD_SIZE / PAGE_SIZE.
Quoted 13 lines
Quoted 4 lines
Now it has changed to
PMD_SIZE/PAGE_SIZE. There seems to have been at least a few changes that missed the version log. What happened? AI review? We need to make sure we are not rolling back previous decisions silently...Will mark this kind of change next time.
Quoted 3 lines
Can you share more widely if there are new unmentioned changes in the rest of
the series?Besides the main changes listed in the "Main changes from v3" section of the changelog, and those already documented in the changelog of each patch, the following changes vs v3 are undocumented:
Quoted 5 lines
Patch 1: added a level arg and have the callee to decide whether to skip PAMT add/put.
Patch 2: -PTRS_PER_PMDtoPMD_SIZE/PAGE_SIZE,
-atomic_cmpxchg_release()toatomic_set()due to the spinlock protection.
Can you explain more on this one? I missed it.
Quoted 1 line
- added a helper
__tdh_mem_page_demote().
why?
Quoted 5 lines
Patch 3: implementation is much simpler due to the the base code changes introduced by the previous MMU refactor and folio/page to pfn series. Patch 4: - Moved
is_mirror_sp()immediately abovedisallowed_hugepage_adjust()instead of saving it in a local variable.
- added a comment explaining the disallowing reason.
Re: [PATCH v4 02/17] x86/virt/tdx: Add a SEAMCALL wrapper to demote a 2MB huge page
Quoted 6 lines · Rick P
Quoted 2 lines · Yan Zhao
Quoted 1 line
we don't need to support anything other than 2MB, right?
Yeah, with
tdh_mem_page_demote_2mb(), I'll hardcode the level to 2MB here.Just to be sure... We don't need the level arg anymore, so don't need to warn about it.
Got it.
Quoted 17 lines
Quoted 7 lines
Quoted 5 lines
arch/x86/virt/vmx/tdx/tdx.c u64 tdh_mng_rd(struct tdx_td *td, u64 field, u64 *data) · line 1873
Quoted 2 lines
arch/x86/virt/vmx/tdx/tdx.c +2
-0+if (WARN_ON_ONCE (alloc_pamt_array (dpamt_pages, pamt_cache))) +return TDX_SW_ERROR ;Why handle this differently than other pamt flows? It also looks like this warning was added since the last version I saw...
Right. The warning was added since v4 due to the internal review.
Quoted 9 lines
I added this
WARN_ON_ONCE()afteralloc_pamt_array()fails based on the following internal review comment:
"Hmm, so this makes me wonder, what kind of error do we want to return to KVM. Does KVM ever expect this to fail? We handlePAGE_AUGfailing, but can we talk about how KVM will call this. What kind of failures does it want to know about and what kind of failures can it do something about? Otherwise to review this patch you need to look ahead to the code and understand the KVM MMU. Is it possible to give some justification in the log for why the error exposure is the way it is?"
Quoted 1 line
I don't see the connection to turning that into a warning.
Oh, sorry. I was a bit rushed with the last reply.
Turning it into a warning was because:
1. defensive programming:
In KVM, kvm_mmu_memory_cache_alloc() triggers BUG_ON() when there's no page in a cache. So, I think we can make a warning when there's no page in pamt_cache.
2. your previous comment to "level != 2M" checking:
"Hmm, this could all be a warnings I think. I guess it wades into Sean's new
tendency to survive MMU errors. But do we think there is special risk here? What horrible thing can go wrong? I'd want a justification for why we need to handle this bug and not just report it via warning."
3. Sean's previous comments related to "two WARNs".
Quoted 3 lines
But besides that, please don't silently churn the code like this. Especially when you provide a version log, because it kind of implies that nothing else was changed.
Ok. Will do.
(I took the easy way out by only listing the major ones, as the changes from v3 to v4 involved Sean's v5, many internal review versions, and changes due to rebasing to the latest DPAMT).
Quoted 4 lines
Quoted 2 lines
Should I also add a comment such as "The caller must ensure there are enough pages in the
pamt_cache"?I think it's obvious. That is the whole point of the cache.
So, are you suggesting just propagating the error to KVM without the warning in demote()?
Quoted 4 lines
Quoted 2 lines
It looks like Sean was saying to warn about subtle contract breaking. He also
No argues. Just want to ensure I can write the code correctly in the future:
If KVM invokes DEMOTE without topup enough pages in cache, is it a subtle contract breaking?
Quoted 4 lines
says in that thread:
Document what exactly? Most of this should be common sense. E.g. we don't generally document that pointers must be non-NULL, because that goes without saying 99.9% of the time.
Here are more background:
1. I asked in https://lore.kernel.org/all/[email protected]:
"
BTW, I have another question about the SEAMCALL wrapper implementation, as Kai also pointed out in [2]: since the SEAMCALL wrappers now serve as APIs available to callers besides KVM, should the SEAMCALL wrappers return TDX_OPERAND_INVALID or WARN_ON() (or WARN_ON_ONCE()) on sanity check failure?
By returning TDX_OPERAND_INVALID, the caller can check the return code, adjust the input or trigger WARN_ON() by itself;
By triggering WARN_ON() directly in the SEAMCALL wrapper, we need to document this requirement for the SEAMCALL wrappers and have the caller invoke the API correctly.
So, it looks that "WARN_ON() directly in the SEAMCALL wrapper" is the preferred approach, right?
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]
".
2. Sean replied:
"Why not both? But maybe TDX_SW_ERROR instead of TDX_OPERAND_INVALID?".
3. I said:
"The reason for 'not both' is that there's already TDX_BUG_ON_2() in KVM after the SEAMCALL wrapper returns a non-BUSY error. I'm not sure if having double WARN_ON_ONCE() calls is good, so I intended to let the caller decide whether to warn."
4. Sean said "Two WARNs isn't the end of the world. It might even be helpful in some cases, e.g. to more precisely document what went wrong."
Quoted 5 lines
But besides that we are trying to get Dave's ack here. So I don't think extrapolating a comment Sean made is going to suffecient for having code that is different than the rest without explanation. So let's think about how to get that ack. Obviously we should take Sean's input as well. But while we are on the maintainer boundary, we can't call it settled.
Hmm, yes.
Not sure if Dave likes "two WARNs" or not as Sean suggested in
https://lore.kernel.org/all/[email protected].
He was in that thread, but maybe he didn't see Sean's mails.
To avoid extrapolating, what about suppressing most WARN_ON_ONCE() in demote() in the next version? (see the code at tail of this mail).
Quoted 5 lines
Quoted 3 lines
Patch 2: -
PTRS_PER_PMDtoPMD_SIZE/PAGE_SIZE,
-atomic_cmpxchg_release()toatomic_set()due to the spinlock protection.Can you explain more on this one? I missed it.
There was no spin lock to protect DEMOTE SEAMCALL execution, so atomic_cmpxchg_release() was used to ensure the original refcount is 0 while updating new refcount to 512.
Now, with the dpamt_lock protection, atomic_set() is used to align with the way of updating refcount in tdx_pamt_get() and tdx_pamt_put(), though the atomic_*() version is actually unnecessary.
Or since the atomic_*() version is anyway unnecessary, what about adding the asserting of 0 like this?
WARN_ON_ONCE(atomic_cmpxchg_release(dpamt_refcount, 0, PTRS_PER_PMD));
Quoted 3 lines
Quoted 1 line
- added a helper
__tdh_mem_page_demote().why?
To make the helper focused on invoking the DEMOTE SEAMCALL, without dealing with locks, refcounts, page retrieval, etc. similar to how tdh_phymem_pamt_add() relates to tdx_pamt_get().
Do you think the helper is unnecessary?
Does the following implementation look good to you?
changes from v4:
- dropped __tdh_mem_page_demote() helper
- dropped "if (WARN_ON_ONCE(atomic_read(dpamt_refcount)". Used atomic_cmpxchg_release() and WARN_ON_ONCE() on 0 original refcount.
- used PTRS_PER_PMD.
- invoke reinsert_pamt_array() on failure.
- renamed to tdh_mem_page_demote_2mb() and dropped 2MB checking.
- dropped WARN_ON_ONCE() for DPAMT is not supported, alloc_pamt_array() failure and SEAMCALL failure.
u64 tdh_mem_page_demote_2mb(struct tdx_td *td, u64 gpa, kvm_pfn_t pfn, struct page *new_sept_pt,
struct tdx_pamt_cache *pamt_cache, u64 *ext_err1, u64 *ext_err2)
{
struct tdx_module_args args = {
.rcx = gpa | pg_level_to_tdx_sept_level(PG_LEVEL_2M),
.rdx = tdx_tdr_pa(td),
.r8 = page_to_phys(new_sept_pt),
};
atomic_t *dpamt_refcount = tdx_find_dpamt_refcount(pfn);
struct page *dpamt_pages[TDX_DPAMT_ENTRY_PAGE_CNT];
*ext_err1 = 0;
*ext_err2 = 0;
u64 ret;
if (!tdx_supports_dynamic_pamt(&tdx_sysinfo))
return TDX_SW_ERROR;
ret = alloc_pamt_array(dpamt_pages, pamt_cache);
if (ret)
return ret;
args.r12 = page_to_phys(dpamt_pages[0]),
args.r13 = page_to_phys(dpamt_pages[1]),
/*
* The dpamt_lock is used to avoid contention between the
* TDH_MEM_PAGE_DEMOTE and TDH_PHYMEM_PAMT_{ADD/REMOVE} SEAMCALLs.
*/
spin_lock(&dpamt_lock);
ret = seamcall_saved_ret(TDH_MEM_PAGE_DEMOTE, &args);
*ext_err1 = args.rcx;
*ext_err2 = args.rdx;
if (ret != TDX_SUCCESS)
goto out_free;
atomic_set(dpamt_refcount, PTRS_PER_PMD);
WARN_ON_ONCE(atomic_cmpxchg_release(dpamt_refcount, 0,
PTRS_PER_PMD));
spin_unlock(&dpamt_lock);
return TDX_SUCCESS;out_free:
spin_unlock(&dpamt_lock);
reinsert_pamt_array(pamt_cache, dpamt_pages);
return ret;
}EXPORT_SYMBOL_FOR_KVM(tdh_mem_page_demote_2mb);
Edgecombe, Rick P
maintainerRe: [PATCH v4 02/17] x86/virt/tdx: Add a SEAMCALL wrapper to demote a 2MB huge page
Quoted 43 lines · Yan Zhao
Quoted 6 lines · Rick P
Quoted 2 lines · Yan Zhao
Quoted 1 line
we don't need to support anything other than 2MB, right?
Yeah, with
tdh_mem_page_demote_2mb(), I'll hardcode the level to 2MB here.Just to be sure... We don't need the level arg anymore, so don't need to warn about it.
Got it.
Quoted 17 lines
Quoted 7 lines
Quoted 5 lines
arch/x86/virt/vmx/tdx/tdx.c u64 tdh_mng_rd(struct tdx_td *td, u64 field, u64 *data) · line 1873
Quoted 2 lines
arch/x86/virt/vmx/tdx/tdx.c +2
-0+if (WARN_ON_ONCE (alloc_pamt_array (dpamt_pages, pamt_cache))) +return TDX_SW_ERROR ;Why handle this differently than other pamt flows? It also looks like this warning was added since the last version I saw...
Right. The warning was added since v4 due to the internal review.
Quoted 9 lines
I added this
WARN_ON_ONCE()afteralloc_pamt_array()fails based on the following internal review comment:
"Hmm, so this makes me wonder, what kind of error do we want to return to KVM. Does KVM ever expect this to fail? We handlePAGE_AUGfailing, but can we talk about how KVM will call this. What kind of failures does it want to know about and what kind of failures can it do something about? Otherwise to review this patch you need to look ahead to the code and understand the KVM MMU. Is it possible to give some justification in the log for why the error exposure is the way it is?"Quoted 1 line
I don't see the connection to turning that into a warning.
Oh, sorry. I was a bit rushed with the last reply.
Turning it into a warning was because:
1. defensive programming:
In KVM,kvm_mmu_memory_cache_alloc()triggersBUG_ON()when there's no page in a cache. So, I think we can make a warning when there's no page inpamt_cache.2. your previous comment to "level != 2M" checking:
"Hmm, this could all be a warnings I think. I guess it wades into Sean's new
tendency to survive MMU errors. But do we think there is special risk here? What horrible thing can go wrong? I'd want a justification for why we need to handle this bug and not just report it via warning."3. Sean's previous comments related to "two WARNs".
Quoted 7 lines
Quoted 3 lines
But besides that, please don't silently churn the code like this. Especially when you provide a version log, because it kind of implies that nothing else was changed.
Ok. Will do.
(I took the easy way out by only listing the major ones, as the changes from v3 to v4 involved Sean's v5, many internal review versions, and changes due to rebasing to the latest DPAMT).
Then just say:
- Lots of minor changes from AI review (Sashiko)
The serious problem is something that implies that it is unchanged but actually is changed.
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Should I also add a comment such as "The caller must ensure there are enough pages in the
pamt_cache"?I think it's obvious. That is the whole point of the cache.
So, are you suggesting just propagating the error to KVM without the warning in
demote()?
Maybe instead you can answer why do anything different here than the other alloc_pamt_array() callers? Is there something special? Then you can justify it.
Otherwise, it looks like trying to change the norms of the existing code. Regardless of whether it is even a good idea, I think you have enough on your hands to get this series upstream.
Quoted 9 lines
Quoted 5 lines
Quoted 3 lines
It looks like Sean was saying to warn about subtle contract breaking. He also
No argues. Just want to ensure I can write the code correctly in the future:
If KVM invokes DEMOTE without topup enough pages in cache, is it a subtle contract breaking?
Doesn't seem that subtle to me? It is not too different than passing NULL because your allocation failed.
Quoted 29 lines
Quoted 5 lines
says in that thread:
Document what exactly? Most of this should be common sense. E.g. we don't generally document that pointers must be non-NULL, because that goes without saying 99.9% of the time.Here are more background:
1. I asked in https://lore.kernel.org/all/[email protected]:
"
BTW, I have another question about the SEAMCALL wrapper implementation, as Kai also pointed out in [2]: since the SEAMCALL wrappers now serve as APIs available to callers besides KVM, should the SEAMCALL wrappers returnTDX_OPERAND_INVALIDorWARN_ON()(orWARN_ON_ONCE()) on sanity check failure?By returning
TDX_OPERAND_INVALID, the caller can check the return code, adjust the input or triggerWARN_ON()by itself;
By triggeringWARN_ON()directly in the SEAMCALL wrapper, we need to document this requirement for the SEAMCALL wrappers and have the caller invoke the API correctly.So, it looks that "WARN_ON() directly in the SEAMCALL wrapper" is the preferred approach, right? [1] https://lore.kernel.org/all/[email protected]/ [2] https://lore.kernel.org/all/[email protected] ".2. Sean replied:
"Why not both? But maybeTDX_SW_ERRORinstead ofTDX_OPERAND_INVALID?".
Quoted 8 lines
3. I said:
"The reason for 'not both' is that there's alreadyTDX_BUG_ON_2()in KVM after the SEAMCALL wrapper returns a non-BUSY error. I'm not sure if having doubleWARN_ON_ONCE()calls is good, so I intended to let the caller decide whether to warn."4. Sean said "Two WARNs isn't the end of the world. It might even be helpful in some cases, e.g. to more precisely document what went wrong."
Let's just mirror the existing, very recently discussed and agreed code unless we can say there is a new different problem with demote.
Quoted 24 lines
Quoted 7 lines
But besides that we are trying to get Dave's ack here. So I don't think extrapolating a comment Sean made is going to suffecient for having code that is different than the rest without explanation. So let's think about how to get that ack. Obviously we should take Sean's input as well. But while we are on the maintainer boundary, we can't call it settled.
Hmm, yes.
Not sure if Dave likes "two WARNs" or not as Sean suggested in
https://lore.kernel.org/all/[email protected].
He was in that thread, but maybe he didn't see Sean's mails.To avoid extrapolating, what about suppressing most
WARN_ON_ONCE()indemote()in the next version? (see the code at tail of this mail).Quoted 5 lines
Quoted 3 lines
Patch 2: -
PTRS_PER_PMDtoPMD_SIZE/PAGE_SIZE,
-atomic_cmpxchg_release()toatomic_set()due to the spinlock protection.Can you explain more on this one? I missed it.
There was no spin lock to protect DEMOTE SEAMCALL execution, so
atomic_cmpxchg_release()was used to ensure the original refcount is 0 while updating new refcount to 512.
Quoted 8 lines
Now, with the
dpamt_lockprotection,atomic_set()is used to align with the way of updating refcount intdx_pamt_get()andtdx_pamt_put(), though the atomic_*() version is actually unnecessary.Or since the atomic_*() version is anyway unnecessary, what about adding the asserting of 0 like this?
WARN_ON_ONCE(atomic_cmpxchg_release(dpamt_refcount, 0, PTRS_PER_PMD));
We want this patch to make the most sense on top of the existing code. Try to make it as simple as possible. Don't worry as much about if the code is 100% optimal. To get the code into a the desired state, it is way, way easier if 99% of it is already upstream. So keep in mind we can continue to develop it.
Doing everything inside a giant lock is pretty simple pattern so I don't see a giant risk. Do you?
Quoted 21 lines
Quoted 4 lines
Quoted 2 lines
- added a helper
__tdh_mem_page_demote().why?
To make the helper focused on invoking the DEMOTE SEAMCALL, without dealing with locks, refcounts, page retrieval, etc. similar to how
tdh_phymem_pamt_add()relates totdx_pamt_get().
Do you think the helper is unnecessary?Does the following implementation look good to you?
changes from v4:
- dropped__tdh_mem_page_demote()helper
- dropped "if (WARN_ON_ONCE(atomic_read(dpamt_refcount)". Usedatomic_cmpxchg_release()andWARN_ON_ONCE()on 0 original refcount.
- usedPTRS_PER_PMD.
- invokereinsert_pamt_array()on failure.
- renamed totdh_mem_page_demote_2mb()and dropped 2MB checking.
- droppedWARN_ON_ONCE()for DPAMT is not supported,alloc_pamt_array()failure and SEAMCALL failure.u64 tdh_mem_page_demote_2mb(struct
tdx_td*td, u64 gpa,kvm_pfn_tpfn, struct page*new_sept_pt,
Quoted 15 lines
struct tdx_pamt_cache *pamt_cache, u64 *ext_err1, u64 *ext_err2)
{
struct tdx_module_args args = {
.rcx = gpa | pg_level_to_tdx_sept_level(PG_LEVEL_2M),
.rdx = tdx_tdr_pa(td),
.r8 = page_to_phys(new_sept_pt),
};
atomic_t *dpamt_refcount = tdx_find_dpamt_refcount(pfn);
struct page *dpamt_pages[TDX_DPAMT_ENTRY_PAGE_CNT];
*ext_err1 = 0;
*ext_err2 = 0;
u64 ret;
if (!tdx_supports_dynamic_pamt(&tdx_sysinfo))
return TDX_SW_ERROR;Do we need to handle this logically? The below will return some error in any case, I'd think. I'd make it a warning with no conditional. The comment of the limitation is important and the warning does that.
In general DPAMT maintenance is also verified by the TDX module when the pages it covers get used. So if we get things wrong, there are already duplicate checks to catch things.
Quoted 17 lines
ret = alloc_pamt_array(dpamt_pages, pamt_cache);
if (ret)
return ret;
args.r12 = page_to_phys(dpamt_pages[0]),
args.r13 = page_to_phys(dpamt_pages[1]),
/*
* The dpamt_lock is used to avoid contention between the
* TDH_MEM_PAGE_DEMOTE and TDH_PHYMEM_PAMT_{ADD/REMOVE} SEAMCALLs.
*/
spin_lock(&dpamt_lock);
ret = seamcall_saved_ret(TDH_MEM_PAGE_DEMOTE, &args);
*ext_err1 = args.rcx;Quoted 8 lines
*ext_err2 = args.rdx;
if (ret != TDX_SUCCESS)
goto out_free;
atomic_set(dpamt_refcount, PTRS_PER_PMD);
WARN_ON_ONCE(atomic_cmpxchg_release(dpamt_refcount, 0,
PTRS_PER_PMD));As discussed above.
Quoted 12 lines
spin_unlock(&dpamt_lock); return TDX_SUCCESS;
out_free:spin_unlock(&dpamt_lock); reinsert_pamt_array(pamt_cache, dpamt_pages); return ret; }
EXPORT_SYMBOL_FOR_KVM(tdh_mem_page_demote_2mb);
Re: [PATCH v4 02/17] x86/virt/tdx: Add a SEAMCALL wrapper to demote a 2MB huge page
Quoted 10 lines · Rick P
Quoted 4 lines
Ok. Will do.
(I took the easy way out by only listing the major ones, as the changes from v3 to v4 involved Sean's v5, many internal review versions, and changes due to rebasing to the latest DPAMT).Then just say:
- Lots of minor changes from AI review (Sashiko)The serious problem is something that implies that it is unchanged but actually is changed.
I see. Thanks!
I'll avoid it next time.
Quoted 15 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
Should I also add a comment such as "The caller must ensure there are enough pages in the
pamt_cache"?I think it's obvious. That is the whole point of the cache.
So, are you suggesting just propagating the error to KVM without the warning in
demote()?Maybe instead you can answer why do anything different here than the other
alloc_pamt_array()callers? Is there something special? Then you can justify it.Otherwise, it looks like trying to change the norms of the existing code. Regardless of whether it is even a good idea, I think you have enough on your hands to get this series upstream.
Quoted 2 lines
Quoted 2 lines
Quoted 1 line
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
It looks like Sean was saying to warn about subtle contract breaking. He also
No argues. Just want to ensure I can write the code correctly in the future:
If KVM invokes DEMOTE without topup enough pages in cache, is it a subtle contract breaking?
Doesn't seem that subtle to me? It is not too different than passing NULL because your allocation failed.
Hmm, ok. Now I see the difference:tdx_pamt_get() can't trigger WARN_ON_ONCE() when alloc_pamt_array() fails, because cache is allowed to be NULL, so alloc_pamt_array() in tdx_pamt_get() is possible and allowed to fail due to out of memory. So,
- in the NULL-cache paths, KVM handles the error and fails the ioctl;
- in the !NULL-cache case, KVM triggers KVM_BUG_ON().
However, the cache should always be !NULL in demote() and alloc_pamt_array() is never expected to fail for demote(), so I added WARN_ON_ONCE() to catch this contract violation.
So, does this difference justify the WARN_ON_ONCE() on alloc_pamt_array() failure in demote()?
Besides, I noticed there's a WARN_ON_ONCE() in tdx_pamt_get() when tdh_phymem_pamt_add() fails:
"if (WARN_ON_ONCE(tdx_status != TDX_SUCCESS))",
Should I also add this when TDH_MEM_PAGE_DEMOTE() fails?
Quoted 34 lines
Quoted 34 lines
Quoted 4 lines
says in that thread:
Document what exactly? Most of this should be common sense. E.g. we don't generally document that pointers must be non-NULL, because that goes without saying 99.9% of the time.Here are more background:
1. I asked in https://lore.kernel.org/all/[email protected]:
"
BTW, I have another question about the SEAMCALL wrapper implementation, as Kai also pointed out in [2]: since the SEAMCALL wrappers now serve as APIs available to callers besides KVM, should the SEAMCALL wrappers returnTDX_OPERAND_INVALIDorWARN_ON()(orWARN_ON_ONCE()) on sanity check failure?By returning
TDX_OPERAND_INVALID, the caller can check the return code, adjust the input or triggerWARN_ON()by itself;
By triggeringWARN_ON()directly in the SEAMCALL wrapper, we need to document this requirement for the SEAMCALL wrappers and have the caller invoke the API correctly.So, it looks that "WARN_ON() directly in the SEAMCALL wrapper" is the preferred approach, right? [1] https://lore.kernel.org/all/[email protected]/ [2] https://lore.kernel.org/all/[email protected] ".2. Sean replied:
"Why not both? But maybeTDX_SW_ERRORinstead ofTDX_OPERAND_INVALID?".3. I said:
"The reason for 'not both' is that there's alreadyTDX_BUG_ON_2()in KVM after the SEAMCALL wrapper returns a non-BUSY error. I'm not sure if having doubleWARN_ON_ONCE()calls is good, so I intended to let the caller decide whether to warn."
Quoted 5 lines
Quoted 2 lines
4. Sean said "Two WARNs isn't the end of the world. It might even be helpful in some cases, e.g. to more precisely document what went wrong."
Let's just mirror the existing, very recently discussed and agreed code unless we can say there is a new different problem with demote.
Please check if the difference mentioned above can justify the warning.
Quoted 31 lines
Quoted 22 lines
Quoted 5 lines
But besides that we are trying to get Dave's ack here. So I don't think extrapolating a comment Sean made is going to suffecient for having code that is different than the rest without explanation. So let's think about how to get that ack. Obviously we should take Sean's input as well. But while we are on the maintainer boundary, we can't call it settled.
Hmm, yes.
Not sure if Dave likes "two WARNs" or not as Sean suggested in
https://lore.kernel.org/all/[email protected].
He was in that thread, but maybe he didn't see Sean's mails.To avoid extrapolating, what about suppressing most
WARN_ON_ONCE()indemote()in the next version? (see the code at tail of this mail).Quoted 5 lines
Quoted 3 lines
Patch 2: -
PTRS_PER_PMDtoPMD_SIZE/PAGE_SIZE,
-atomic_cmpxchg_release()toatomic_set()due to the spinlock protection.Can you explain more on this one? I missed it.
There was no spin lock to protect DEMOTE SEAMCALL execution, so
atomic_cmpxchg_release()was used to ensure the original refcount is 0 while updating new refcount to 512.Quoted 8 lines
Now, with the
dpamt_lockprotection,atomic_set()is used to align with the way of updating refcount intdx_pamt_get()andtdx_pamt_put(), though the atomic_*() version is actually unnecessary.Or since the atomic_*() version is anyway unnecessary, what about adding the asserting of 0 like this?
WARN_ON_ONCE(atomic_cmpxchg_release(dpamt_refcount, 0, PTRS_PER_PMD));
Quoted 7 lines
We want this patch to make the most sense on top of the existing code. Try to make it as simple as possible. Don't worry as much about if the code is 100% optimal. To get the code into a the desired state, it is way, way easier if 99% of it is already upstream. So keep in mind we can continue to develop it.
Doing everything inside a giant lock is pretty simple pattern so I don't see a giant risk. Do you?
Yeah, I'm not concerned about code optimization here, so, I used atomic_set() in v4.
My question is if atomic_cmpxchg_release() would look better to you, since it implicitly checks that dpamt_refcount is 0 before setting the value, allowing me to drop
if (WARN_ON_ONCE(atomic_read(dpamt_refcount))) {
ret = TDX_SW_ERROR;
goto out_free;
}But from your later comment, it seems you prefer atomic_set()?
Quoted 36 lines
Quoted 20 lines
Quoted 3 lines
Quoted 1 line
- added a helper
__tdh_mem_page_demote().why?
To make the helper focused on invoking the DEMOTE SEAMCALL, without dealing with locks, refcounts, page retrieval, etc. similar to how
tdh_phymem_pamt_add()relates totdx_pamt_get().
Do you think the helper is unnecessary?Does the following implementation look good to you?
changes from v4:
- dropped__tdh_mem_page_demote()helper
- dropped "if (WARN_ON_ONCE(atomic_read(dpamt_refcount)". Usedatomic_cmpxchg_release()andWARN_ON_ONCE()on 0 original refcount.
- usedPTRS_PER_PMD.
- invokereinsert_pamt_array()on failure.
- renamed totdh_mem_page_demote_2mb()and dropped 2MB checking.
- droppedWARN_ON_ONCE()for DPAMT is not supported,alloc_pamt_array()failure and SEAMCALL failure.u64 tdh_mem_page_demote_2mb(struct
tdx_td*td, u64 gpa,kvm_pfn_tpfn, struct page*new_sept_pt,Quoted 15 lines
struct tdx_pamt_cache *pamt_cache, u64 *ext_err1, u64 *ext_err2) { struct tdx_module_args args = { .rcx = gpa | pg_level_to_tdx_sept_level(PG_LEVEL_2M), .rdx = tdx_tdr_pa(td), .r8 = page_to_phys(new_sept_pt), }; atomic_t *dpamt_refcount = tdx_find_dpamt_refcount(pfn); struct page *dpamt_pages[TDX_DPAMT_ENTRY_PAGE_CNT]; *ext_err1 = 0; *ext_err2 = 0; u64 ret; if (!tdx_supports_dynamic_pamt(&tdx_sysinfo)) return TDX_SW_ERROR;
Quoted 7 lines
Do we need to handle this logically? The below will return some error in any case, I'd think. I'd make it a warning with no conditional. The comment of the limitation is important and the warning does that.
In general DPAMT maintenance is also verified by the TDX module when the pages it covers get used. So if we get things wrong, there are already duplicate checks to catch things.
Ok. So like this?
/* * For implementation simplicity, only support demotion when * DPAMT is enabled. */ WARN_ON_ONCE(!tdx_supports_dynamic_pamt(&tdx_sysinfo));
Quoted 5 lines
Quoted 5 lines
ret = alloc_pamt_array(dpamt_pages, pamt_cache); if (ret) return ret;
Quoted 8 lines
Quoted 8 lines
args.r12 = page_to_phys(dpamt_pages[0]), args.r13 = page_to_phys(dpamt_pages[1]), /* * The dpamt_lock is used to avoid contention between the * TDH_MEM_PAGE_DEMOTE and TDH_PHYMEM_PAMT_{ADD/REMOVE} SEAMCALLs. */ spin_lock(&dpamt_lock);
If you prefer using atomic_set() to adjust dpamt_refcount, what about inserting
WARN_ON_ONCE(atomic_read(dpamt_refcount);
here,
Quoted 5 lines
Quoted 5 lines
ret = seamcall_saved_ret(TDH_MEM_PAGE_DEMOTE, &args); *ext_err1 = args.rcx; *ext_err2 = args.rdx;
Quoted 8 lines
Quoted 6 lines
if (ret != TDX_SUCCESS) goto out_free; atomic_set(dpamt_refcount, PTRS_PER_PMD); WARN_ON_ONCE(atomic_cmpxchg_release(dpamt_refcount, 0, PTRS_PER_PMD));As discussed above.
then invoke
atomic_set(dpamt_refcount, PTRS_PER_PMD);
here ?
Quoted 13 lines
Quoted 12 lines
spin_unlock(&dpamt_lock); return TDX_SUCCESS;
out_free:spin_unlock(&dpamt_lock); reinsert_pamt_array(pamt_cache, dpamt_pages); return ret; }
EXPORT_SYMBOL_FOR_KVM(tdh_mem_page_demote_2mb);
Re: [PATCH v4 02/17] x86/virt/tdx: Add a SEAMCALL wrapper to demote a 2MB huge page
Quoted 9 lines · Yan Zhao
Quoted 9 lines
Quoted 9 lines
u64 tdh_mem_page_demote_2mb(struct
tdx_td*td, u64 gpa,kvm_pfn_tpfn, struct page*new_sept_pt,struct tdx_pamt_cache *pamt_cache, u64 *ext_err1, u64 *ext_err2) { struct tdx_module_args args = { .rcx = gpa | pg_level_to_tdx_sept_level(PG_LEVEL_2M), .rdx = tdx_tdr_pa(td), .r8 = page_to_phys(new_sept_pt), }; atomic_t *dpamt_refcount = tdx_find_dpamt_refcount(pfn);
Oh, when DPAMT is not enabled, this line makes it erroneous to access the uninitialized dpamt_refcounts.
Quoted 9 lines
Quoted 9 lines
Quoted 7 lines
struct page *dpamt_pages[TDX_DPAMT_ENTRY_PAGE_CNT]; *ext_err1 = 0; *ext_err2 = 0; u64 ret; if (!tdx_supports_dynamic_pamt(&tdx_sysinfo)) return TDX_SW_ERROR;Do we need to handle this logically? The below will return some error in any
So, we need this check and should move tdx_find_dpamt_refcount() after it.
Would the below version look good to you?
Changes vs the version in
https://lore.kernel.org/all/as9OHnM4mceEre%[email protected]:
- Added WARN_ON_ONCE() for tdx_supports_dynamic_pamt() and moved tdx_find_dpamt_refcount() after checking DPAMT is enabled.
- Added WARN_ON_ONCE() for DEMOTE SEAMCALL failure.
- atomic_cmpxchg_release() to atomic_set() and added WARN_ON_ONCE(atomic_read(dpamt_refcount)).
(I didn't add the warning for alloc_pamt_array(). Will add it if you think the justification in
https://lore.kernel.org/all/[email protected] makes sense.
u64 tdh_mem_page_demote_2mb(struct tdx_td *td, u64 gpa, kvm_pfn_t pfn, struct page *new_sept_pt,
struct tdx_pamt_cache *pamt_cache, u64 *ext_err1, u64 *ext_err2)
{
struct tdx_module_args args = {
.rcx = gpa | pg_level_to_tdx_sept_level(PG_LEVEL_2M),
.rdx = tdx_tdr_pa(td),
.r8 = page_to_phys(new_sept_pt),
};
struct page *dpamt_pages[TDX_DPAMT_ENTRY_PAGE_CNT];
atomic_t *dpamt_refcount;
u64 ret;
*ext_err1 = 0;
*ext_err2 = 0;
if (WARN_ON_ONCE(!tdx_supports_dynamic_pamt(&tdx_sysinfo)))
return TDX_SW_ERROR;
dpamt_refcount = tdx_find_dpamt_refcount(pfn);
ret = alloc_pamt_array(dpamt_pages, pamt_cache);
if (ret)
return TDX_SW_ERROR;
args.r12 = page_to_phys(dpamt_pages[0]),
args.r13 = page_to_phys(dpamt_pages[1]),
/*
* The dpamt_lock is used to avoid contention between the
* TDH_MEM_PAGE_DEMOTE and TDH_PHYMEM_PAMT_{ADD/REMOVE} SEAMCALLs.
*/
spin_lock(&dpamt_lock);
WARN_ON_ONCE(atomic_read(dpamt_refcount));
ret = seamcall_saved_ret(TDH_MEM_PAGE_DEMOTE, &args);
*ext_err1 = args.rcx;
*ext_err2 = args.rdx;
if (WARN_ON_ONCE(ret != TDX_SUCCESS))
goto out_free;
atomic_set(dpamt_refcount, PTRS_PER_PMD);
spin_unlock(&dpamt_lock);
return TDX_SUCCESS;out_free:
spin_unlock(&dpamt_lock);
reinsert_pamt_array(pamt_cache, dpamt_pages);
return ret;
}EXPORT_SYMBOL_FOR_KVM(tdh_mem_page_demote_2mb);
Re: [PATCH v4 02/17] x86/virt/tdx: Add a SEAMCALL wrapper to demote a 2MB huge page
Quoted 51 lines · Yan Zhao
Quoted 9 lines · Yan Zhao
Quoted 9 lines
Quoted 9 lines
u64 tdh_mem_page_demote_2mb(struct
tdx_td*td, u64 gpa,kvm_pfn_tpfn, struct page*new_sept_pt,struct tdx_pamt_cache *pamt_cache, u64 *ext_err1, u64 *ext_err2) { struct tdx_module_args args = { .rcx = gpa | pg_level_to_tdx_sept_level(PG_LEVEL_2M), .rdx = tdx_tdr_pa(td), .r8 = page_to_phys(new_sept_pt), }; atomic_t *dpamt_refcount = tdx_find_dpamt_refcount(pfn);Oh, when DPAMT is not enabled, this line makes it erroneous to access the uninitialized
dpamt_refcounts.Quoted 9 lines
Quoted 9 lines
Quoted 7 lines
struct page *dpamt_pages[TDX_DPAMT_ENTRY_PAGE_CNT]; *ext_err1 = 0; *ext_err2 = 0; u64 ret; if (!tdx_supports_dynamic_pamt(&tdx_sysinfo)) return TDX_SW_ERROR;Do we need to handle this logically? The below will return some error in any
So, we need this check and should move
tdx_find_dpamt_refcount()after it.Would the below version look good to you?
Changes vs the version in
https://lore.kernel.org/all/as9OHnM4mceEre%[email protected]:
- AddedWARN_ON_ONCE()fortdx_supports_dynamic_pamt()and movedtdx_find_dpamt_refcount()after checking DPAMT is enabled.
- AddedWARN_ON_ONCE()for DEMOTE SEAMCALL failure.
-atomic_cmpxchg_release()toatomic_set()and added WARN_ON_ONCE(atomic_read(dpamt_refcount)).(I didn't add the warning for
alloc_pamt_array(). Will add it if you think the justification in
https://lore.kernel.org/all/[email protected] makes sense.u64 tdh_mem_page_demote_2mb(struct
tdx_td*td, u64 gpa,kvm_pfn_tpfn, struct page*new_sept_pt,struct tdx_pamt_cache *pamt_cache, u64 *ext_err1, u64 *ext_err2) { struct tdx_module_args args = { .rcx = gpa | pg_level_to_tdx_sept_level(PG_LEVEL_2M), .rdx = tdx_tdr_pa(td), .r8 = page_to_phys(new_sept_pt), }; struct page *dpamt_pages[TDX_DPAMT_ENTRY_PAGE_CNT]; atomic_t *dpamt_refcount; u64 ret;
Quoted 7 lines
*ext_err1 = 0;
*ext_err2 = 0;
if (WARN_ON_ONCE(!tdx_supports_dynamic_pamt(&tdx_sysinfo)))
return TDX_SW_ERROR;
dpamt_refcount = tdx_find_dpamt_refcount(pfn);Can move this line after alloc_pamt_array() to mirror the sequence in tdx_pamt_get().
Quoted 3 lines
ret = alloc_pamt_array(dpamt_pages, pamt_cache);
if (ret)
return TDX_SW_ERROR;return ret;
Quoted 33 lines
args.r12 = page_to_phys(dpamt_pages[0]), args.r13 = page_to_phys(dpamt_pages[1]), /* * The dpamt_lock is used to avoid contention between the * TDH_MEM_PAGE_DEMOTE and TDH_PHYMEM_PAMT_{ADD/REMOVE} SEAMCALLs. */ spin_lock(&dpamt_lock); WARN_ON_ONCE(atomic_read(dpamt_refcount)); ret = seamcall_saved_ret(TDH_MEM_PAGE_DEMOTE, &args); *ext_err1 = args.rcx; *ext_err2 = args.rdx; if (WARN_ON_ONCE(ret != TDX_SUCCESS)) goto out_free; atomic_set(dpamt_refcount, PTRS_PER_PMD); spin_unlock(&dpamt_lock); return TDX_SUCCESS;
out_free:spin_unlock(&dpamt_lock); reinsert_pamt_array(pamt_cache, dpamt_pages); return ret; }
EXPORT_SYMBOL_FOR_KVM(tdh_mem_page_demote_2mb);
Edgecombe, Rick P
maintainerRe: [PATCH v4 02/17] x86/virt/tdx: Add a SEAMCALL wrapper to demote a 2MB huge page
Quoted 14 lines · Yan Zhao
Quoted 14 lines
u64 tdh_mem_page_demote_2mb(struct
tdx_td*td, u64 gpa,kvm_pfn_tpfn, struct page*new_sept_pt,
structtdx_pamt_cache*pamt_cache, u64*ext_err1, u64*ext_err2)
{
structtdx_module_argsargs = {
.rcx = gpa |pg_level_to_tdx_sept_level(PG_LEVEL_2M), .rdx =tdx_tdr_pa(td),
.r8 =page_to_phys(new_sept_pt),
};
struct page *dpamt_pages[TDX_DPAMT_ENTRY_PAGE_CNT];atomic_t*dpamt_refcount;
u64 ret;
Quoted 5 lines
Quoted 5 lines
*ext_err1= 0;
*ext_err2= 0;if (WARN_ON_ONCE(!tdx_supports_dynamic_pamt(&tdx_sysinfo))) return
TDX_SW_ERROR;
Why did it change back? Based on the "mirror the other code for error handling level", I guess there could be an argument for conditional.
Hmm, the other checks are about making the code work transparently work whether PAMT is enabled or not. But this one is about handling code calling the function incorrectly (since we don't support huge pages without dpamt). So I think I'd still recommend what I suggested last time of no conditional return. With a comment like you had. I also don't see anything blowing up too badly if the warning is triggered.
Quoted 4 lines
Quoted 2 lines
dpamt_refcount=tdx_find_dpamt_refcount(pfn);Can move this line after
alloc_pamt_array()to mirror the sequence intdx_pamt_get().
The less that stands out as having to think about, the better.
Quoted 33 lines
Quoted 4 lines
ret =
alloc_pamt_array(dpamt_pages, pamt_cache); if (ret)
returnTDX_SW_ERROR;return ret;Quoted 28 lines
args.r12 =
page_to_phys(dpamt_pages[0]),
args.r13 =page_to_phys(dpamt_pages[1]),/*
* Thedpamt_lockis used to avoid contention between the
*TDH_MEM_PAGE_DEMOTEand TDH_PHYMEM_PAMT_{ADD/REMOVE} SEAMCALLs. */
spin_lock(&dpamt_lock);WARN_ON_ONCE(atomic_read(dpamt_refcount));
ret =
seamcall_saved_ret(TDH_MEM_PAGE_DEMOTE, &args);
*ext_err1= args.rcx;
*ext_err2= args.rdx;if (
WARN_ON_ONCE(ret != TDX_SUCCESS))
gotoout_free;
atomic_set(dpamt_refcount, PTRS_PER_PMD);spin_unlock(&dpamt_lock);return
TDX_SUCCESS;
out_free:
spin_unlock(&dpamt_lock);reinsert_pamt_array(pamt_cache, dpamt_pages);
Is "reinsert" the right name here? It is just adding them to the cache and resetting them. So why not just adjust free_pamt_array() to take a cache arg instead of adding another function? In tdx_pamt_put() pass NULL so the pages go back to the page allocator. Might need a prep patch. Would have to test.
This patch will end up getting stripped down the minimum we need for this series. Any new functions or extra code will get scrutiny.
Quoted 4 lines
Quoted 4 lines
return ret;
}EXPORT_SYMBOL_FOR_KVM(tdh_mem_page_demote_2mb);
Otherwise looks better to me.
Re: [PATCH v4 02/17] x86/virt/tdx: Add a SEAMCALL wrapper to demote a 2MB huge page
Quoted 24 lines · Rick P
Quoted 14 lines · Yan Zhao
Quoted 14 lines
u64 tdh_mem_page_demote_2mb(struct
tdx_td*td, u64 gpa,kvm_pfn_tpfn, struct page*new_sept_pt,
structtdx_pamt_cache*pamt_cache, u64*ext_err1, u64*ext_err2)
{
structtdx_module_argsargs = {
.rcx = gpa |pg_level_to_tdx_sept_level(PG_LEVEL_2M), .rdx =tdx_tdr_pa(td),
.r8 =page_to_phys(new_sept_pt),
};
struct page *dpamt_pages[TDX_DPAMT_ENTRY_PAGE_CNT];atomic_t*dpamt_refcount;
u64 ret;Quoted 5 lines
Quoted 5 lines
*ext_err1= 0;
*ext_err2= 0;if (WARN_ON_ONCE(!tdx_supports_dynamic_pamt(&tdx_sysinfo))) return
TDX_SW_ERROR;Why did it change back? Based on the "mirror the other code for error handling level", I guess there could be an argument for conditional.
Quoted 6 lines
Hmm, the other checks are about making the code work transparently work whether PAMT is enabled or not. But this one is about handling code calling the function incorrectly (since we don't support huge pages without dpamt). So I think I'd still recommend what I suggested last time of no conditional return. With a comment like you had. I also don't see anything blowing up too badly if the warning is triggered.
My concern is that without bailing out when DPAMT is not enabled, invoking tdx_find_dpamt_refcount() would cause a NULL pointer dereference.
Or could I re-introduce the "dpamt" bool? This would allow huge pages to be supported even when DPAMT is not enabled (with no other places needing to change). The only downside is that it would introduce 3 additional 'if' comparisons:
u64 tdh_mem_page_demote_2mb(struct tdx_td *td, u64 gpa, kvm_pfn_t pfn, struct page *new_sept_pt,
struct tdx_pamt_cache *pamt_cache, u64 *ext_err1, u64 *ext_err2)
{
struct tdx_module_args args = {
.rcx = gpa | pg_level_to_tdx_sept_level(PG_LEVEL_2M),
.rdx = tdx_tdr_pa(td),
.r8 = page_to_phys(new_sept_pt),
};
bool dpamt = tdx_supports_dynamic_pamt(&tdx_sysinfo);
struct page *dpamt_pages[TDX_DPAMT_ENTRY_PAGE_CNT];
atomic_t *dpamt_refcount;
u64 ret;
*ext_err1 = 0;
*ext_err2 = 0;
if (dpamt) {
dpamt_refcount = tdx_find_dpamt_refcount(pfn);
ret = alloc_pamt_array(dpamt_pages, pamt_cache);
if (ret)
return TDX_SW_ERROR;
args.r12 = page_to_phys(dpamt_pages[0]);
args.r13 = page_to_phys(dpamt_pages[1]);
/*
* The dpamt_lock is used to avoid contention between the
* TDH_MEM_PAGE_DEMOTE and TDH_PHYMEM_PAMT_{ADD/REMOVE} SEAMCALLs.
*/
spin_lock(&dpamt_lock);
WARN_ON_ONCE(atomic_read(dpamt_refcount));
}
ret = seamcall_saved_ret(TDH_MEM_PAGE_DEMOTE, &args);
*ext_err1 = args.rcx;
*ext_err2 = args.rdx;
if (WARN_ON_ONCE(ret != TDX_SUCCESS))
goto out_free;
if (dpamt) {
atomic_set(dpamt_refcount, PTRS_PER_PMD);
spin_unlock(&dpamt_lock);
}
return TDX_SUCCESS;out_free:
if (dpamt) {
spin_unlock(&dpamt_lock);
free_pamt_array(pamt_cache, dpamt_pages);
}
return ret;
}EXPORT_SYMBOL_FOR_KVM(tdh_mem_page_demote_2mb);
What do you think?
Quoted 9 lines
Quoted 3 lines
Quoted 1 line
dpamt_refcount=tdx_find_dpamt_refcount(pfn);Can move this line after
alloc_pamt_array()to mirror the sequence intdx_pamt_get().The less that stands out as having to think about, the better.
Quoted 3 lines
Quoted 3 lines
ret =
alloc_pamt_array(dpamt_pages, pamt_cache); if (ret)
Does the justification for WARN_ON_ONCE() on alloc_pamt_array() failure that I provided in https://lore.kernel.org/all/[email protected]
look good to you?
Could you share your thoughts on it?
Quoted 27 lines
Quoted 27 lines
Quoted 1 line
return
TDX_SW_ERROR;return ret;Quoted 25 lines
args.r12 =
page_to_phys(dpamt_pages[0]),
args.r13 =page_to_phys(dpamt_pages[1]),/*
* Thedpamt_lockis used to avoid contention between the
*TDH_MEM_PAGE_DEMOTEand TDH_PHYMEM_PAMT_{ADD/REMOVE} SEAMCALLs. */
spin_lock(&dpamt_lock);WARN_ON_ONCE(atomic_read(dpamt_refcount));
ret =
seamcall_saved_ret(TDH_MEM_PAGE_DEMOTE, &args);
*ext_err1= args.rcx;
*ext_err2= args.rdx;if (
WARN_ON_ONCE(ret != TDX_SUCCESS))
gotoout_free;
atomic_set(dpamt_refcount, PTRS_PER_PMD);spin_unlock(&dpamt_lock);return
TDX_SUCCESS;
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
out_free:
spin_unlock(&dpamt_lock);reinsert_pamt_array(pamt_cache, dpamt_pages);Is "reinsert" the right name here? It is just adding them to the cache and resetting them. So why not just adjust
free_pamt_array()to take a cache arg instead of adding another function? Intdx_pamt_put()pass NULL so the pages go back to the page allocator. Might need a prep patch. Would have to test.
Will do.
Quoted 2 lines
This patch will end up getting stripped down the minimum we need for this series. Any new functions or extra code will get scrutiny.
I agree!
(my only concern is the risk of NULL pointer dereference).
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines
return ret;
}EXPORT_SYMBOL_FOR_KVM(tdh_mem_page_demote_2mb);Otherwise looks better to me.
Thanks!
Edgecombe, Rick P
maintainerRe: [PATCH v4 02/17] x86/virt/tdx: Add a SEAMCALL wrapper to demote a 2MB huge page
Quoted 25 lines · Yan Zhao
Quoted 24 lines · Rick P
Quoted 14 lines · Yan Zhao
Quoted 14 lines
u64 tdh_mem_page_demote_2mb(struct
tdx_td*td, u64 gpa,kvm_pfn_tpfn, struct page*new_sept_pt,
structtdx_pamt_cache*pamt_cache, u64*ext_err1, u64*ext_err2)
{
structtdx_module_argsargs = {
.rcx = gpa |pg_level_to_tdx_sept_level(PG_LEVEL_2M), .rdx =tdx_tdr_pa(td),
.r8 =page_to_phys(new_sept_pt),
};
struct page *dpamt_pages[TDX_DPAMT_ENTRY_PAGE_CNT];atomic_t*dpamt_refcount;
u64 ret;Quoted 5 lines
Quoted 5 lines
*ext_err1= 0;
*ext_err2= 0;if (WARN_ON_ONCE(!tdx_supports_dynamic_pamt(&tdx_sysinfo))) return
TDX_SW_ERROR;Why did it change back? Based on the "mirror the other code for error handling level", I guess there could be an argument for conditional.
Quoted 8 lines
Quoted 6 lines
Hmm, the other checks are about making the code work transparently work whether PAMT is enabled or not. But this one is about handling code calling the function incorrectly (since we don't support huge pages without dpamt). So I think I'd still recommend what I suggested last time of no conditional return. With a comment like you had. I also don't see anything blowing up too badly if the warning is triggered.
My concern is that without bailing out when DPAMT is not enabled, invoking
tdx_find_dpamt_refcount()would cause a NULL pointer dereference.
If this function is called when PAMT is not available in the module (which should be rare going forward), and it is called in a rare scenario from KVM that is not caught during basic testing, then you would be left with an easily debugable crash.
In the current code tdx_get_pamt_cache() in tdx_sept_split_leaf_spte() would return NULL and you wouldn't even call tdh_mem_page_demote_2mb(). So it seems like excess paranoia to me.
Quoted 63 lines
Or could I re-introduce the "dpamt" bool? This would allow huge pages to be supported even when DPAMT is not enabled (with no other places needing to change). The only downside is that it would introduce 3 additional 'if' comparisons:
u64 tdh_mem_page_demote_2mb(struct
tdx_td*td, u64 gpa,kvm_pfn_tpfn, struct page*new_sept_pt,struct tdx_pamt_cache *pamt_cache, u64 *ext_err1, u64 *ext_err2) { struct tdx_module_args args = { .rcx = gpa | pg_level_to_tdx_sept_level(PG_LEVEL_2M), .rdx = tdx_tdr_pa(td), .r8 = page_to_phys(new_sept_pt), }; bool dpamt = tdx_supports_dynamic_pamt(&tdx_sysinfo); struct page *dpamt_pages[TDX_DPAMT_ENTRY_PAGE_CNT]; atomic_t *dpamt_refcount; u64 ret; *ext_err1 = 0; *ext_err2 = 0; if (dpamt) { dpamt_refcount = tdx_find_dpamt_refcount(pfn); ret = alloc_pamt_array(dpamt_pages, pamt_cache); if (ret) return TDX_SW_ERROR; args.r12 = page_to_phys(dpamt_pages[0]); args.r13 = page_to_phys(dpamt_pages[1]); /* * The dpamt_lock is used to avoid contention between the * TDH_MEM_PAGE_DEMOTE and TDH_PHYMEM_PAMT_{ADD/REMOVE} SEAMCALLs. */ spin_lock(&dpamt_lock); WARN_ON_ONCE(atomic_read(dpamt_refcount)); } ret = seamcall_saved_ret(TDH_MEM_PAGE_DEMOTE, &args); *ext_err1 = args.rcx; *ext_err2 = args.rdx; if (WARN_ON_ONCE(ret != TDX_SUCCESS)) goto out_free; if (dpamt) { atomic_set(dpamt_refcount, PTRS_PER_PMD); spin_unlock(&dpamt_lock); } return TDX_SUCCESS;
out_free:if (dpamt) { spin_unlock(&dpamt_lock); free_pamt_array(pamt_cache, dpamt_pages); } return ret; }
Quoted 3 lines
EXPORT_SYMBOL_FOR_KVM(tdh_mem_page_demote_2mb);What do you think?
Adding support for a setup that will increasingly not exist in the real world as the TDX modules get upgraded, for our basic initial implementation?
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
dpamt_refcount=tdx_find_dpamt_refcount(pfn);Can move this line after
alloc_pamt_array()to mirror the sequence intdx_pamt_get().The less that stands out as having to think about, the better.
Quoted 1 line
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
ret =
alloc_pamt_array(dpamt_pages, pamt_cache); if (ret)Does the justification for
WARN_ON_ONCE()onalloc_pamt_array()failure that I provided in https://lore.kernel.org/all/[email protected]
look good to you?Could you share your thoughts on it?
I don't see a comment above alloc_pamt_array(). If you mean the comment above tdx_supports_dynamic_pamt(), I think it looks good.
Quoted 36 lines
Quoted 28 lines
Quoted 28 lines
Quoted 2 lines
return
TDX_SW_ERROR;return ret;Quoted 25 lines
args.r12 =
page_to_phys(dpamt_pages[0]),
args.r13 =page_to_phys(dpamt_pages[1]),/*
* Thedpamt_lockis used to avoid contention between the
*TDH_MEM_PAGE_DEMOTEand TDH_PHYMEM_PAMT_{ADD/REMOVE} SEAMCALLs. */
spin_lock(&dpamt_lock);WARN_ON_ONCE(atomic_read(dpamt_refcount));
ret =
seamcall_saved_ret(TDH_MEM_PAGE_DEMOTE, &args);
*ext_err1= args.rcx;
*ext_err2= args.rdx;if (
WARN_ON_ONCE(ret != TDX_SUCCESS))
gotoout_free;
atomic_set(dpamt_refcount, PTRS_PER_PMD);spin_unlock(&dpamt_lock);return
TDX_SUCCESS;Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
out_free:
spin_unlock(&dpamt_lock);reinsert_pamt_array(pamt_cache, dpamt_pages);Is "reinsert" the right name here? It is just adding them to the cache and resetting them. So why not just adjust
free_pamt_array()to take a cache arg instead of adding another function? Intdx_pamt_put()pass NULL so the pages go back to the page allocator. Might need a prep patch. Would have to test.
Quoted 6 lines
Will do.
Quoted 2 lines
This patch will end up getting stripped down the minimum we need for this series. Any new functions or extra code will get scrutiny.
I agree!
(my only concern is the risk of NULL pointer dereference).
As above, let's just match the existing code. Otherwise I think its just going to get questions and not merged. We want this to have as little to talk about as possible. If we want to change the defensive level (I don't), it is going to need a bunch of prep patches instead of just embarking on a new paradigm for additional huge page functions. I don't see a need to do that, and for the sake of getting basic huge page support, I think that is a good thing. Right?
Quoted 7 lines
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
return ret;
}EXPORT_SYMBOL_FOR_KVM(tdh_mem_page_demote_2mb);Otherwise looks better to me.
Thanks!
Re: [PATCH v4 02/17] x86/virt/tdx: Add a SEAMCALL wrapper to demote a 2MB huge page
Quoted 11 lines · Rick P
Quoted 4 lines
Quoted 2 lines
This patch will end up getting stripped down the minimum we need for this series. Any new functions or extra code will get scrutiny.
I agree!
(my only concern is the risk of NULL pointer dereference).As above, let's just match the existing code. Otherwise I think its just going to get questions and not merged. We want this to have as little to talk about as possible. If we want to change the defensive level (I don't), it is going to need a bunch of prep patches instead of just embarking on a new paradigm for additional huge page functions. I don't see a need to do that, and for the sake of getting basic huge page support, I think that is a good thing. Right?
Thanks, I'll follow your suggestion.
Does the following code look good to you?
u64 tdh_mem_page_demote_2mb(struct tdx_td *td, u64 gpa, kvm_pfn_t pfn, struct page *new_sept_pt,
struct tdx_pamt_cache *pamt_cache, u64 *ext_err1, u64 *ext_err2)
{
struct tdx_module_args args = {
.rcx = gpa | pg_level_to_tdx_sept_level(PG_LEVEL_2M),
.rdx = tdx_tdr_pa(td),
.r8 = page_to_phys(new_sept_pt),
};
struct page *dpamt_pages[TDX_DPAMT_ENTRY_PAGE_CNT];
atomic_t *dpamt_refcount;
u64 ret;
/*
* For implementation simplicity, only support demotion when
* DPAMT is enabled.
*/
WARN_ON_ONCE(!tdx_supports_dynamic_pamt(&tdx_sysinfo));
/* A NULL pamt_cache is unexpected, so failure indicates a software error */
ret = alloc_pamt_array(dpamt_pages, pamt_cache);
if (ret) {
*ext_err1 = 0;
*ext_err2 = 0;
return TDX_SW_ERROR;
}
dpamt_refcount = tdx_find_dpamt_refcount(pfn);
args.r12 = page_to_phys(dpamt_pages[0]);
args.r13 = page_to_phys(dpamt_pages[1]);
/*
* The dpamt_lock is used to avoid contention between the
* TDH_MEM_PAGE_DEMOTE and TDH_PHYMEM_PAMT_{ADD/REMOVE} SEAMCALLs.
*/
spin_lock(&dpamt_lock);
ret = seamcall_saved_ret(TDH_MEM_PAGE_DEMOTE, &args);
*ext_err1 = args.rcx;
*ext_err2 = args.rdx;
if (WARN_ON_ONCE(ret != TDX_SUCCESS))
goto out_free;
atomic_set(dpamt_refcount, PTRS_PER_PMD);
spin_unlock(&dpamt_lock);
return TDX_SUCCESS;out_free:
spin_unlock(&dpamt_lock);
free_pamt_array(pamt_cache, dpamt_pages);
return ret;
}EXPORT_SYMBOL_FOR_KVM(tdh_mem_page_demote_2mb);
Edgecombe, Rick P
maintainerRe: [PATCH v4 02/17] x86/virt/tdx: Add a SEAMCALL wrapper to demote a 2MB huge page
Quoted 14 lines · Yan Zhao
u64 tdh_mem_page_demote_2mb(struct
tdx_td*td, u64 gpa,kvm_pfn_tpfn, struct page*new_sept_pt,
structtdx_pamt_cache*pamt_cache, u64*ext_err1, u64*ext_err2)
{
structtdx_module_argsargs = {
.rcx = gpa |pg_level_to_tdx_sept_level(PG_LEVEL_2M), .rdx =tdx_tdr_pa(td),
.r8 =page_to_phys(new_sept_pt),
};
struct page *dpamt_pages[TDX_DPAMT_ENTRY_PAGE_CNT];atomic_t*dpamt_refcount;
u64 ret;
Quoted 8 lines
/*
* For implementation simplicity, only support demotion when
* DPAMT is enabled.
*/ WARN_ON_ONCE(!tdx_supports_dynamic_pamt(&tdx_sysinfo));/* A NULL
pamt_cacheis unexpected, so failure indicates a software error */
On the edge of unnecessary to me, but ok.
Quoted 28 lines
ret =
alloc_pamt_array(dpamt_pages, pamt_cache); if (ret) {
*ext_err1= 0;
*ext_err2= 0;
returnTDX_SW_ERROR;
}
dpamt_refcount=tdx_find_dpamt_refcount(pfn);args.r12 =
page_to_phys(dpamt_pages[0]);
args.r13 =page_to_phys(dpamt_pages[1]);/*
* Thedpamt_lockis used to avoid contention between the
*TDH_MEM_PAGE_DEMOTEand TDH_PHYMEM_PAMT_{ADD/REMOVE} SEAMCALLs. */
spin_lock(&dpamt_lock);ret =
seamcall_saved_ret(TDH_MEM_PAGE_DEMOTE, &args);*ext_err1= args.rcx;
*ext_err2= args.rdx;if (
WARN_ON_ONCE(ret != TDX_SUCCESS))
gotoout_free;
atomic_set(dpamt_refcount, PTRS_PER_PMD);
spin_unlock(&dpamt_lock);
Quoted 8 lines
return
TDX_SUCCESS;
out_free:
spin_unlock(&dpamt_lock);
free_pamt_array(pamt_cache, dpamt_pages);return ret;
}
LGTM
Re: [PATCH v4 02/17] x86/virt/tdx: Add a SEAMCALL wrapper to demote a 2MB huge page
Quoted 21 lines · Rick P
Quoted 20 lines · Yan Zhao
u64 tdh_mem_page_demote_2mb(struct
tdx_td*td, u64 gpa,kvm_pfn_tpfn, struct page*new_sept_pt,
structtdx_pamt_cache*pamt_cache, u64*ext_err1, u64*ext_err2)
{
structtdx_module_argsargs = {
.rcx = gpa |pg_level_to_tdx_sept_level(PG_LEVEL_2M), .rdx =tdx_tdr_pa(td),
.r8 =page_to_phys(new_sept_pt),
};
struct page *dpamt_pages[TDX_DPAMT_ENTRY_PAGE_CNT];atomic_t*dpamt_refcount;
u64 ret;/*
* For implementation simplicity, only support demotion when
* DPAMT is enabled.
*/ WARN_ON_ONCE(!tdx_supports_dynamic_pamt(&tdx_sysinfo));
Quoted 4 lines
Quoted 2 lines
/* A NULL
pamt_cacheis unexpected, so failure indicates a software error */On the edge of unnecessary to me, but ok.
I'll remove this comment then :)
Quoted 30 lines
Quoted 30 lines
ret =
alloc_pamt_array(dpamt_pages, pamt_cache); if (ret) {
*ext_err1= 0;
*ext_err2= 0;
returnTDX_SW_ERROR;
}
dpamt_refcount=tdx_find_dpamt_refcount(pfn);args.r12 =
page_to_phys(dpamt_pages[0]);
args.r13 =page_to_phys(dpamt_pages[1]);/*
* Thedpamt_lockis used to avoid contention between the
*TDH_MEM_PAGE_DEMOTEand TDH_PHYMEM_PAMT_{ADD/REMOVE} SEAMCALLs. */
spin_lock(&dpamt_lock);ret =
seamcall_saved_ret(TDH_MEM_PAGE_DEMOTE, &args);*ext_err1= args.rcx;
*ext_err2= args.rdx;if (
WARN_ON_ONCE(ret != TDX_SUCCESS))
gotoout_free;
atomic_set(dpamt_refcount, PTRS_PER_PMD);
spin_unlock(&dpamt_lock);return
TDX_SUCCESS;
Quoted 8 lines
Quoted 6 lines
out_free:
spin_unlock(&dpamt_lock);
free_pamt_array(pamt_cache, dpamt_pages);return ret;
}LGTM
Oops, I accidentally included changes for the workaround for a TDX module that does not support uninterruptible demote, and forgot that the busy error is valid.
Could you please check if the following version looks good to you? Changes vs the last version:
- Added WARN on !tdx_huge_page_demote_uninterruptible() and updated the comment above the warning.
- Do not warn on TDX_OPERAND_BUSY.
- Removed the comment above alloc_pamt_array().
Thanks a lot!
u64 tdh_mem_page_demote_2mb(struct tdx_td *td, u64 gpa, kvm_pfn_t pfn,
struct page *new_pt, struct tdx_pamt_cache *pamt_cache,
u64 *ext_err1, u64 *ext_err2)
{
struct tdx_module_args args = {
.rcx = gpa | pg_level_to_tdx_sept_level(PG_LEVEL_2M),
.rdx = tdx_tdr_pa(td),
.r8 = page_to_phys(new_pt),
};
struct page *dpamt_pages[TDX_DPAMT_ENTRY_PAGE_CNT];
atomic_t *dpamt_refcount;
u64 ret;
/*
* For simplicity, only support demotion when DPAMT is enabled and when the
* TDH_MEM_PAGE_DEMOTE SEAMCALL does not return TDX_INTERRUPTED_RESTARTABLE.
*/
WARN_ON_ONCE(!tdx_huge_page_demote_uninterruptible(&tdx_sysinfo) ||
!tdx_supports_dynamic_pamt(&tdx_sysinfo));
if (alloc_pamt_array(dpamt_pages, pamt_cache)) {
*ext_err1 = 0;
*ext_err2 = 0;
return TDX_SW_ERROR;
}
dpamt_refcount = tdx_find_dpamt_refcount(pfn);
args.r12 = page_to_phys(dpamt_pages[0]);
args.r13 = page_to_phys(dpamt_pages[1]);
/*
* The dpamt_lock is used to avoid contention between the
* TDH_MEM_PAGE_DEMOTE and TDH_PHYMEM_PAMT_{ADD/REMOVE} SEAMCALLs.
*/
spin_lock(&dpamt_lock);
ret = seamcall_saved_ret(TDH_MEM_PAGE_DEMOTE, &args);
*ext_err1 = args.rcx;
*ext_err2 = args.rdx;
if (ret != TDX_SUCCESS) {
WARN_ON_ONCE((ret & TDX_SEAMCALL_STATUS_MASK) != TDX_OPERAND_BUSY);
goto out_free;
}
atomic_set(dpamt_refcount, PTRS_PER_PMD);
spin_unlock(&dpamt_lock);
return TDX_SUCCESS;out_free:
spin_unlock(&dpamt_lock); free_pamt_array(pamt_cache, dpamt_pages); return ret; }
EXPORT_SYMBOL_FOR_KVM(tdh_mem_page_demote_2mb);
PATCH v4 3/17 KVM: TDX: Reset private huge pages after S-EPT page removal
In reply to Yan Zhao"Resetting" TDX pages is a workaround for the TDX_PW_MCE erratum. Once a leaf SPTE can map to a huge page in the S-EPT, the entire huge physical range, rather than just the first 4KB, needs to be reset before the host can reuse it.
--- arch/x86/kvm/vmx/tdx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/vmx/tdx.c +1 -1
@@ -1865,7 +1865,7 @@ static int tdx_sept_remove_leaf_spte(struct kvm *kvm, gfn_t gfn,if (TDX_BUG_ON_2 (err,TDH_MEM_PAGE_REMOVE , entry, level_state, kvm))return -EIO ; -tdx_quirk_reset_paddr (PFN_PHYS (pfn),PAGE_SIZE ); +tdx_quirk_reset_paddr (PFN_PHYS (pfn),KVM_HPAGE_SIZE (level));tdx_pamt_put (pfn, level);return 0 ; }
Signature · 1 line
2.43.2
Edgecombe, Rick P
maintainerRe: [PATCH v4 03/17] KVM: TDX: Reset private huge pages after S-EPT page removal
Quoted 4 lines · Yan Zhao
"Resetting" TDX pages is a workaround for the
TDX_PW_MCEerratum. Once a leaf SPTE can map to a huge page in the S-EPT, the entire huge physical range, rather than just the first 4KB, needs to be reset before the host can reuse it.
Code looks good and simple enough, but the log is a tiny bit rough. Probably add about TDX_PW_MCE needing to reset any private memory in a special way, and make it clearer that TDX huge pages will be enabled in upcoming changes. And is not supported today (so this is not a bug fix).
Quoted 19 lines
Signed-off-by Yan Zhao <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/kvm/vmx/tdx.c +1
-1@@ -1865,7 +1865,7 @@ static int tdx_sept_remove_leaf_spte(struct kvm *kvm, gfn_t gfn,if (TDX_BUG_ON_2 (err,TDH_MEM_PAGE_REMOVE , entry, level_state, kvm))return -EIO ; -tdx_quirk_reset_paddr (PFN_PHYS (pfn),PAGE_SIZE ); +tdx_quirk_reset_paddr (PFN_PHYS (pfn),KVM_HPAGE_SIZE (level));tdx_pamt_put (pfn, level);return 0 ; }
Re: [PATCH v4 03/17] KVM: TDX: Reset private huge pages after S-EPT page removal
Quoted 10 lines · Rick P
Quoted 4 lines · Yan Zhao
"Resetting" TDX pages is a workaround for the
TDX_PW_MCEerratum. Once a leaf SPTE can map to a huge page in the S-EPT, the entire huge physical range, rather than just the first 4KB, needs to be reset before the host can reuse it.Code looks good and simple enough, but the log is a tiny bit rough. Probably add about
TDX_PW_MCEneeding to reset any private memory in a special way, and make it clearer that TDX huge pages will be enabled in upcoming changes. And is not supported today (so this is not a bug fix).
Thanks for the suggestion.
What about:
"
On TDX_PW_MCE erratum platforms, a partial write (with KeyID 0) to a TDX private memory cacheline will silently "poison" the line. A subsequent read triggers a machine check.
On such platforms, "resetting" TDX private pages by invoking MOVDIR64B on the previous TDX private memory before returning them to the host is required to avoid potential machine checks.
Before introducing huge page support, the reset size is forced to 4KB when removing a private leaf mapping. To prepare for the upcoming changes to allow mapping and unmapping private memory as a huge page, update the reset size to the actual mapping size to support resetting the entire huge page range.
"
Quoted 19 lines
Quoted 18 lines
Signed-off-by Yan Zhao <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/kvm/vmx/tdx.c +1
-1@@ -1865,7 +1865,7 @@ static int tdx_sept_remove_leaf_spte(struct kvm *kvm, gfn_t gfn,if (TDX_BUG_ON_2 (err,TDH_MEM_PAGE_REMOVE , entry, level_state, kvm))return -EIO ; -tdx_quirk_reset_paddr (PFN_PHYS (pfn),PAGE_SIZE ); +tdx_quirk_reset_paddr (PFN_PHYS (pfn),KVM_HPAGE_SIZE (level));tdx_pamt_put (pfn, level);return 0 ; }
PATCH v4 4/17 KVM: x86/mmu: Prevent huge page promotion for mirror roots in fault path
In reply to Yan ZhaoFrom: Rick Edgecombe <[email protected]>
Disallow huge page promotion in the TDP MMU for mirror roots as KVM doesn't currently support promoting S-EPT entries due to the complexity incurred by the TDX module's rules for huge page promotion.
- The current TDX module requires all 4KB leafs to be either all PENDING or all ACCEPTED before a successful promotion to 2MB. This requirement prevents successful page merging after partially converting a 2MB range from private to shared and then back to private, which is the primary scenario necessitating page promotion.
- The TDX module effectively requires a break-before-make sequence (to satisfy its TLB flushing rules), i.e., creates a window of time where a different vCPU can encounter faults on a SPTE that KVM is trying to promote to a huge page. To avoid unexpected BUSY errors, KVM would need to FREEZE the non-leaf SPTE before replacing it with a huge SPTE.
Disable huge page promotion for all map() operations, as supporting page promotion when building the initial image is still non-trivial, and the vast majority of images are ~4MB or less, i.e., the benefit of creating huge pages during TD build time is minimal.
--- arch/x86/kvm/mmu/mmu.c | 3 ++- arch/x86/kvm/mmu/tdp_mmu.c | 12 +++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-)
arch/x86/kvm/mmu/mmu.c +2 -1
@@ -3533,7 +3533,8 @@ void disallowed_hugepage_adjust(struct kvm_page_fault *fault, u64 spte, int cur_ cur_level == fault->goal_level &&is_shadow_present_pte (spte) && !is_large_pte (spte) && - spte_to_child_sp(spte)->nx_huge_page_disallowed) { + ((spte_to_child_sp (spte)->nx_huge_page_disallowed) || + is_mirror_sp(spte_to_child_sp(spte)))) {/* * A small SPTE exists for this pfn, but FNAME(fetch), * direct_map(), or kvm_tdp_mmu_map() would like to create a
arch/x86/kvm/mmu/tdp_mmu.c +11 -1
@@ -1233,7 +1233,17 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)for_each_tdp_pte (iter, kvm, root, fault->gfn, fault->gfn +1 ) {int r; -if (fault->nx_huge_page_workaround_enabled) +/* +* Don't replace a page table (non-leaf) SPTE with a huge SPTE +* (a.k.a. hugepage promotion) if the NX hugepage workaround is +* enabled, as doing so will cause significant thrashing if one +* or more leaf SPTEs need to be executable. +* +* Disallow hugepage promotion for mirror roots as KVM doesn't +* (yet) support promoting S-EPT entries while holding mmu_lock +* for read (due to complexity induced by the TDX-Module APIs). +*/ +if (fault->nx_huge_page_workaround_enabled ||is_mirror_sp (root))disallowed_hugepage_adjust (fault, iter.old_spte, iter.level);/*
Signature · 1 line
2.43.2
Re: [PATCH v4 04/17] KVM: x86/mmu: Prevent huge page promotion for mirror roots in fault path
Quoted 23 lines · Yan Zhao
From: Rick Edgecombe <[email protected]>
Disallow huge page promotion in the TDP MMU for mirror roots as KVM doesn't currently support promoting S-EPT entries due to the complexity incurred by the TDX module's rules for huge page promotion.
- The current TDX module requires all 4KB leafs to be either all PENDING or all ACCEPTED before a successful promotion to 2MB. This requirement prevents successful page merging after partially converting a 2MB range from private to shared and then back to private, which is the primary scenario necessitating page promotion.
- The TDX module effectively requires a break-before-make sequence (to satisfy its TLB flushing rules), i.e., creates a window of time where a different vCPU can encounter faults on a SPTE that KVM is trying to promote to a huge page. To avoid unexpected BUSY errors, KVM would need to FREEZE the non-leaf SPTE before replacing it with a huge SPTE.
Disable huge page promotion for all
map()operations, as supporting page promotion when building the initial image is still non-trivial, and the vast majority of images are ~4MB or less, i.e., the benefit of creating huge pages during TD build time is minimal.
Quoted 6 lines
Signed-off-by Rick Edgecombe <[email protected]>[sean: check root, add comment, rewrite changelog]Signed-off-by Sean Christopherson <[email protected]>Co-developed-by Yan Zhao <[email protected]>Signed-off-by Yan Zhao <[email protected]>---
LGTM. I'm listed as the patch author I think because I suggested it via diff, but due to the longish SOB chain after here is this too:
PATCH v4 5/17 KVM: x86/tdp_mmu: Alloc external_spt page for mirror page table splitting
In reply to Yan ZhaoFrom: Isaku Yamahata <[email protected]>
Enhance tdp_mmu_alloc_sp_for_split() to allocate a page table page for the external page table in preparation for splitting the mirror page table.
When the mirror page table is split in tdp_mmu_split_huge_page(), the corresponding external page table also needs to be split. Therefore, allocate external_spt in tdp_mmu_alloc_sp_for_split() to prepare for the splitting.
The external_spt will be gifted to the TDX module and mapped as a page table page in the S-EPT during splitting. Since the TDX module will initialize the page content in the DEMOTE SEAMCALL, there is no need to zero external_spt.
---
v4:
- "bool mirror" --> "bool is_mirror_sp". (Sean)
- Use __get_free_page() instead of get_zeroed_page(). (Sean)
- let is_mirror_root be const and called once. (Sean)
v3:
- Removed unnecessary declaration of tdp_mmu_alloc_sp_for_split(). (Kai)
- Fixed a typo in the patch log. (Kai)
RFC v2:
- NO change.
RFC v1:
- Rebased and simplified the code.
--- arch/x86/kvm/mmu/tdp_mmu.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
arch/x86/kvm/mmu/tdp_mmu.c +12 -2
@@ -1466,7 +1466,7 @@ bool kvm_tdp_mmu_wrprot_slot(struct kvm *kvm,return spte_set; } -static struct kvm_mmu_page *tdp_mmu_alloc_sp_for_split (void ) +static struct kvm_mmu_page *tdp_mmu_alloc_sp_for_split (bool is_mirror_sp) {struct kvm_mmu_page *sp;
@@ -1480,6 +1480,15 @@ static struct kvm_mmu_page *tdp_mmu_alloc_sp_for_split(void)return NULL ; } +if (is_mirror_sp) { + sp->external_spt = (void *)__get_free_page (GFP_KERNEL_ACCOUNT ); +if (!sp->external_spt) { +free_page ((unsigned long )sp->spt); +kmem_cache_free (mmu_page_header_cache, sp); +return NULL ; + } + } +return sp; }
@@ -1527,6 +1536,7 @@ static int tdp_mmu_split_huge_pages_root(struct kvm *kvm, gfn_t start, gfn_t end, int target_level, bool shared) { + const bool is_mirror_root = is_mirror_sp(root);struct kvm_mmu_page *sp =NULL ;struct tdp_iter iter;
@@ -1559,7 +1569,7 @@ static int tdp_mmu_split_huge_pages_root(struct kvm *kvm,else write_unlock (&kvm->mmu_lock); - sp =tdp_mmu_alloc_sp_for_split (); + sp =tdp_mmu_alloc_sp_for_split (is_mirror_root);if (shared)read_lock (&kvm->mmu_lock);
Signature · 1 line
2.43.2
Re: [PATCH v4 05/17] KVM: x86/tdp_mmu: Alloc external_spt page for mirror page table splitting
Quoted 69 lines · Yan Zhao
From: Isaku Yamahata <[email protected]>
Enhance
tdp_mmu_alloc_sp_for_split()to allocate a page table page for the external page table in preparation for splitting the mirror page table.When the mirror page table is split in
tdp_mmu_split_huge_page(), the corresponding external page table also needs to be split. Therefore, allocateexternal_sptintdp_mmu_alloc_sp_for_split()to prepare for the splitting.The
external_sptwill be gifted to the TDX module and mapped as a page table page in the S-EPT during splitting. Since the TDX module will initialize the page content in the DEMOTE SEAMCALL, there is no need to zeroexternal_spt.Signed-off-by Isaku Yamahata <[email protected]>[sean: use __get_free_page(), let is_mirror_root be const and called once]Signed-off-by Sean Christopherson <[email protected]>Co-developed-by Yan Zhao <[email protected]>Signed-off-by Yan Zhao <[email protected]>---
v4:
- "bool mirror" --> "boolis_mirror_sp". (Sean)
- Use__get_free_page()instead ofget_zeroed_page(). (Sean)
- letis_mirror_rootbe const and called once. (Sean)v3:
- Removed unnecessary declaration oftdp_mmu_alloc_sp_for_split(). (Kai)
- Fixed a typo in the patch log. (Kai)RFC v2:
- NO change.RFC v1:
- Rebased and simplified the code.--- arch/x86/kvm/mmu/tdp_mmu.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)arch/x86/kvm/mmu/tdp_mmu.c +10
-1@@ -1466,7 +1466,7 @@ bool kvm_tdp_mmu_wrprot_slot(struct kvm *kvm,return spte_set; } -static struct kvm_mmu_page *tdp_mmu_alloc_sp_for_split (void ) +static struct kvm_mmu_page *tdp_mmu_alloc_sp_for_split (bool is_mirror_sp) {struct kvm_mmu_page *sp;@@ -1480,6 +1480,15 @@ static struct kvm_mmu_page *tdp_mmu_alloc_sp_for_split(void)return NULL ; } +if (is_mirror_sp) { + sp->external_spt = (void *)__get_free_page (GFP_KERNEL_ACCOUNT ); +if (!sp->external_spt) { +free_page ((unsigned long )sp->spt); +kmem_cache_free (mmu_page_header_cache, sp); +return NULL ; + } + } +return sp; }
arch/x86/kvm/mmu/tdp_mmu.c static int tdp_mmu_split_huge_pages_root(struct kvm *kvm, · line 1539
Quoted 4 lines
arch/x86/kvm/mmu/tdp_mmu.c +1
-0@@ -1527,6 +1536,7 @@ static int tdp_mmu_split_huge_pages_root(struct kvm *kvm, gfn_t start, gfn_t end, int target_level, bool shared) { + const bool is_mirror_root = is_mirror_sp(root);
Nit:
is_mirror_root and is_mirror_sp(root) almost even read the same. And the local var is only every used once even at the end of this series. I wonder about just having tdp_mmu_alloc_sp_for_split(is_mirror_sp(root)), or even just passing the root in.
Otherwise:
Quoted 12 lines
arch/x86/kvm/mmu/tdp_mmu.c +1
-1struct kvm_mmu_page *sp =NULL ;struct tdp_iter iter;@@ -1559,7 +1569,7 @@ static int tdp_mmu_split_huge_pages_root(struct kvm *kvm,else write_unlock (&kvm->mmu_lock); - sp =tdp_mmu_alloc_sp_for_split (); + sp =tdp_mmu_alloc_sp_for_split (is_mirror_root);if (shared)read_lock (&kvm->mmu_lock);
Re: [PATCH v4 05/17] KVM: x86/tdp_mmu: Alloc external_spt page for mirror page table splitting
Quoted 77 lines · Rick P
Quoted 69 lines · Yan Zhao
From: Isaku Yamahata <[email protected]>
Enhance
tdp_mmu_alloc_sp_for_split()to allocate a page table page for the external page table in preparation for splitting the mirror page table.When the mirror page table is split in
tdp_mmu_split_huge_page(), the corresponding external page table also needs to be split. Therefore, allocateexternal_sptintdp_mmu_alloc_sp_for_split()to prepare for the splitting.The
external_sptwill be gifted to the TDX module and mapped as a page table page in the S-EPT during splitting. Since the TDX module will initialize the page content in the DEMOTE SEAMCALL, there is no need to zeroexternal_spt.Signed-off-by Isaku Yamahata <[email protected]>[sean: use __get_free_page(), let is_mirror_root be const and called once]Signed-off-by Sean Christopherson <[email protected]>Co-developed-by Yan Zhao <[email protected]>Signed-off-by Yan Zhao <[email protected]>---
v4:
- "bool mirror" --> "boolis_mirror_sp". (Sean)
- Use__get_free_page()instead ofget_zeroed_page(). (Sean)
- letis_mirror_rootbe const and called once. (Sean)v3:
- Removed unnecessary declaration oftdp_mmu_alloc_sp_for_split(). (Kai)
- Fixed a typo in the patch log. (Kai)RFC v2:
- NO change.RFC v1:
- Rebased and simplified the code.--- arch/x86/kvm/mmu/tdp_mmu.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)arch/x86/kvm/mmu/tdp_mmu.c +10
-1@@ -1466,7 +1466,7 @@ bool kvm_tdp_mmu_wrprot_slot(struct kvm *kvm,return spte_set; } -static struct kvm_mmu_page *tdp_mmu_alloc_sp_for_split (void ) +static struct kvm_mmu_page *tdp_mmu_alloc_sp_for_split (bool is_mirror_sp) {struct kvm_mmu_page *sp;@@ -1480,6 +1480,15 @@ static struct kvm_mmu_page *tdp_mmu_alloc_sp_for_split(void)return NULL ; } +if (is_mirror_sp) { + sp->external_spt = (void *)__get_free_page (GFP_KERNEL_ACCOUNT ); +if (!sp->external_spt) { +free_page ((unsigned long )sp->spt); +kmem_cache_free (mmu_page_header_cache, sp); +return NULL ; + } + } +return sp; }arch/x86/kvm/mmu/tdp_mmu.c static int tdp_mmu_split_huge_pages_root(struct kvm *kvm, · line 1539
Quoted 4 lines
arch/x86/kvm/mmu/tdp_mmu.c +1
-0@@ -1527,6 +1536,7 @@ static int tdp_mmu_split_huge_pages_root(struct kvm *kvm, gfn_t start, gfn_t end, int target_level, bool shared) { + const bool is_mirror_root = is_mirror_sp(root);Nit:
Quoted 3 lines
is_mirror_rootandis_mirror_sp(root)almost even read the same. And the local var is only every used once even at the end of this series. I wonder about just having tdp_mmu_alloc_sp_for_split(is_mirror_sp(root)), or even just passing the
LGTM since is_mirror_sp() is a "static inline" function.
Quoted 19 lines
root in.
Otherwise:
Reviewed-by Rick Edgecombe <[email protected]>Quoted 12 lines
arch/x86/kvm/mmu/tdp_mmu.c +1
-1struct kvm_mmu_page *sp =NULL ;struct tdp_iter iter;@@ -1559,7 +1569,7 @@ static int tdp_mmu_split_huge_pages_root(struct kvm *kvm,else write_unlock (&kvm->mmu_lock); - sp =tdp_mmu_alloc_sp_for_split (); + sp =tdp_mmu_alloc_sp_for_split (is_mirror_root);if (shared)read_lock (&kvm->mmu_lock);
PATCH v4 6/17 KVM: x86/mmu: Allocate DPAMT pages for vCPU-induced page split
In reply to Yan ZhaoFrom: Sean Christopherson <[email protected]>
Extend the TDP MMU to allocate Dynamic PAMT backing pages (DPAMT pages) for vCPU-induced huge page splits in mirror roots when DPAMT is enabled.
Leverage the .topup_external_cache() interface to topup the DPAMT cache when allocating a new child page table for splitting. The DPAMT cache is currently a per-vCPU thread-local list. When a vCPU-induced page split occurs, DPAMT pages can be drawn locklessly from the list.
Pass min_nr_spts as 1 to .topup_external_cache(), indicating there's one new S-EPT page table page. So, tdx_topup_external_pamt_cache() will allocate DPAMT page pairs for both the newly added S-EPT page table page and the demoted guest private page.
tdp_mmu_alloc_sp_for_split() is currently not reachable from a non-vCPU context for mirror roots, since dirty page tracking is not yet allowed on mirror roots. So, simply add a WARN if tdx_topup_external_pamt_cache() is invoked under a non-vCPU context.
---
v4: new patch.
--- arch/x86/kvm/mmu/tdp_mmu.c | 24 +++++++++++++++--------- arch/x86/kvm/vmx/tdx.c | 3 +++ 2 files changed, 18 insertions(+), 9 deletions(-)
arch/x86/kvm/mmu/tdp_mmu.c +15 -9
@@ -1475,21 +1475,27 @@ static struct kvm_mmu_page *tdp_mmu_alloc_sp_for_split(bool is_mirror_sp)return NULL ; sp->spt = (void *)__get_free_page (GFP_KERNEL_ACCOUNT ); -if (!sp->spt) { -kmem_cache_free (mmu_page_header_cache, sp); -return NULL ; - } +if (!sp->spt) + goto err_spt;if (is_mirror_sp) { sp->external_spt = (void *)__get_free_page (GFP_KERNEL_ACCOUNT ); -if (!sp->external_spt) { -free_page ((unsigned long )sp->spt); -kmem_cache_free (mmu_page_header_cache, sp); -return NULL ; - } +if (!sp->external_spt) + goto err_external_spt; + +if (kvm_x86_call (topup_external_cache)(kvm_get_running_vcpu (),1 )) + goto err_external_split; }return sp; + +err_external_split: +free_page ((unsigned long )sp->external_spt); +err_external_spt: +free_page ((unsigned long )sp->spt); +err_spt: +kmem_cache_free (mmu_page_header_cache, sp); +return NULL ; }/* Note, the caller is responsible for initializing @sp. */
arch/x86/kvm/vmx/tdx.c +3 -0
@@ -1630,6 +1630,9 @@ void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level)static int tdx_topup_external_pamt_cache (struct kvm_vcpu *vcpu,int min_nr_spts) { +if (WARN_ON_ONCE (!vcpu)) +return -EIO ; +/* * Minus one page to exclude the root SPT, but plus one page for a * possible 4KB private mapping.
Signature · 1 line
2.43.2
Re: [PATCH v4 06/17] KVM: x86/mmu: Allocate DPAMT pages for vCPU-induced page split
Quoted 20 lines · Yan Zhao
From: Sean Christopherson <[email protected]>
Extend the TDP MMU to allocate Dynamic PAMT backing pages (DPAMT pages) for vCPU-induced huge page splits in mirror roots when DPAMT is enabled.
Leverage the .topup_external_cache() interface to topup the DPAMT cache when allocating a new child page table for splitting. The DPAMT cache is currently a per-vCPU thread-local list. When a vCPU-induced page split occurs, DPAMT pages can be drawn locklessly from the list.
Pass
min_nr_sptsas 1 to .topup_external_cache(), indicating there's one new S-EPT page table page. So,tdx_topup_external_pamt_cache()will allocate DPAMT page pairs for both the newly added S-EPT page table page and the demoted guest private page.
tdp_mmu_alloc_sp_for_split()is currently not reachable from a non-vCPU context for mirror roots, since dirty page tracking is not yet allowed on mirror roots. So, simply add a WARN iftdx_topup_external_pamt_cache()is invoked under a non-vCPU context.
Quoted 4 lines
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Yan Zhao <[email protected]>---
v4: new patch.
New? It is essentially this with a new log, right?
https://lore.kernel.org/kvm/[email protected]/#t
Re: [PATCH v4 06/17] KVM: x86/mmu: Allocate DPAMT pages for vCPU-induced page split
Quoted 21 lines · Rick P
Quoted 20 lines · Yan Zhao
From: Sean Christopherson <[email protected]>
Extend the TDP MMU to allocate Dynamic PAMT backing pages (DPAMT pages) for vCPU-induced huge page splits in mirror roots when DPAMT is enabled.
Leverage the .topup_external_cache() interface to topup the DPAMT cache when allocating a new child page table for splitting. The DPAMT cache is currently a per-vCPU thread-local list. When a vCPU-induced page split occurs, DPAMT pages can be drawn locklessly from the list.
Pass
min_nr_sptsas 1 to .topup_external_cache(), indicating there's one new S-EPT page table page. So,tdx_topup_external_pamt_cache()will allocate DPAMT page pairs for both the newly added S-EPT page table page and the demoted guest private page.
tdp_mmu_alloc_sp_for_split()is currently not reachable from a non-vCPU context for mirror roots, since dirty page tracking is not yet allowed on mirror roots. So, simply add a WARN iftdx_topup_external_pamt_cache()is invoked under a non-vCPU context.
Quoted 7 lines
Quoted 4 lines
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Yan Zhao <[email protected]>---
v4: new patch.New? It is essentially this with a new log, right?
https://lore.kernel.org/kvm/[email protected]/#t
Ha, "new patch" is relative to v3.
It's indeed introduced by Sean from the DPAMT combined v5.
Quoted 4 lines
Reviewed-by Rick Edgecombe <[email protected]>
Re: [PATCH v4 06/17] KVM: x86/mmu: Allocate DPAMT pages for vCPU-induced page split
Quoted 27 lines · Yan Zhao
Quoted 26 lines · Rick P
Quoted 24 lines · Yan Zhao
From: Sean Christopherson <[email protected]>
Extend the TDP MMU to allocate Dynamic PAMT backing pages (DPAMT pages) for vCPU-induced huge page splits in mirror roots when DPAMT is enabled.
Leverage the .topup_external_cache() interface to topup the DPAMT cache when allocating a new child page table for splitting. The DPAMT cache is currently a per-vCPU thread-local list. When a vCPU-induced page split occurs, DPAMT pages can be drawn locklessly from the list.
Pass
min_nr_sptsas 1 to .topup_external_cache(), indicating there's one new S-EPT page table page. So,tdx_topup_external_pamt_cache()will allocate DPAMT page pairs for both the newly added S-EPT page table page and the demoted guest private page.
tdp_mmu_alloc_sp_for_split()is currently not reachable from a non-vCPU context for mirror roots, since dirty page tracking is not yet allowed on mirror roots. So, simply add a WARN iftdx_topup_external_pamt_cache()is invoked under a non-vCPU context.Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Yan Zhao <[email protected]>---
v4: new patch.
Quoted 4 lines
Quoted 2 lines
New? It is essentially this with a new log, right?
https://lore.kernel.org/kvm/[email protected]/#tHa, "new patch" is relative to v3.
It's indeed introduced by Sean from the DPAMT combined v5.
Let me add a Link to Sean's original post in the next version.
Quoted 4 lines
Quoted 4 lines
Reviewed-by Rick Edgecombe <[email protected]>
Edgecombe, Rick P
maintainerRe: [PATCH v4 06/17] KVM: x86/mmu: Allocate DPAMT pages for vCPU-induced page split
In reply to Yan ZhaoQuoted 4 lines · Yan Zhao
Quoted 2 lines
New? It is essentially this with a new log, right?
https://lore.kernel.org/kvm/[email protected]/#tHa, "new patch" is relative to v3.
It's indeed introduced by Sean from the DPAMT combined v5.
Doh! yes.
PATCH v4 7/17 KVM: TDX: Add core support for splitting/demoting 2MB S-EPT mappings to 4KB
In reply to Yan ZhaoAdd support for splitting, a.k.a. demoting, a 2MB S-EPT leaf mapping to 512 smaller 4KB leaf mappings. As per the TDX module rules, first invoke MEM.RANGE.BLOCK to put the huge S-EPT leaf entry into a splittable state, then do MEM.TRACK and kick all vCPUs outside of guest mode to flush TLBs, and finally do MEM.PAGE.DEMOTE to demote/split the huge S-EPT leaf mapping.
Assert the mmu_lock is held for write, as the BLOCK => TRACK => DEMOTE sequence needs to be "atomic" to guarantee success (and because mmu_lock must be held for write to use tdh_do_no_vcpus()).
Note, even with kvm->mmu_lock held for write, tdh_mem_page_demote() may contend with tdh_vp_enter() and potentially with the guest's S-EPT entry operations. Therefore, wrap the call with tdh_do_no_vcpus() to kick other vCPUs out of the guest and prevent tdh_vp_enter() to ensure success.
Invoke tdx_pamt_get() before invoking tdh_mem_page_demote() so that DPAMT pages for the new S-EPT page table page are installed before the DEMOTE SEAMCALL when DPAMT is enabled. DPAMT pages for the guest memory must be installed inside the DEMOTE SEAMCALL, since it is impossible to do so before a successful demotion.
Instead of allocating and freeing DPAMT pages for guest pages on the KVM side, pass pamt_cache to tdh_mem_page_demote() and let it draw DPAMT pages from pamt_cache before the DEMOTE SEAMCALL. This prevents KVM from having to manage DPAMT pages directly via alloc_pamt_array() and free_pamt_array(), or having knowledge of DPAMT-specific details such as TDX_DPAMT_ENTRY_PAGE_CNT.
[sean: wire up via op set_external_spte(), merge in DPAMT-related code,
massage changelog]
---
v4:
- Hooked tdx_sept_split_leaf_spte() in x86 op set_external_spte() instead of in x86 op split_external_spte() which was no longer introduced in v4. (Sean).
- Renamed tdx_sept_split_private_spte() --> tdx_sept_split_leaf_spte().
- Merged in DPAMT-related code (i.e., passing to_tdx(vcpu)->pamt_cache to tdh_mem_page_demote(). (Sean).
- Assert new_spte is non-leaf. (Yan)
v3:
- Rebased on top of Sean's cleanup series.
- Call out UNBLOCK is not required after DEMOTE. (Kai)
- tdx_sept_split_private_spt() --> tdx_sept_split_private_spte().
RFC v2:
- Split out the code to handle the error TDX_INTERRUPTED_RESTARTABLE.
- Rebased to 6.16.0-rc6 (the way of defining TDX hook changes).
RFC v1:
- Split patch for exclusive mmu_lock only,
- Invoke tdx_sept_zap_private_spte() and tdx_track() for splitting.
- Handled busy error of tdh_mem_page_demote() by kicking off vCPUs.
--- arch/x86/kvm/vmx/tdx.c | 69 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/tdx.c +68 -1
@@ -1873,11 +1873,74 @@ static int tdx_sept_remove_leaf_spte(struct kvm *kvm, gfn_t gfn,return 0 ; } +/* +* Split a huge mapping into smaller mappings at a lower level. Currently only +* supports splitting 2MB mappings (KVM doesn't yet support 1GB mappings for TDX +* guests). +* +* Invoke "BLOCK + TRACK + kick off vCPUs (inside tdx_track())" since the TDX +* module does not yet support the NON-BLOCKING-RESIZE feature for DEMOTE. +* +* No UNBLOCK is needed after a successful DEMOTE. +* +* Under write mmu_lock, kick off all vCPUs and disallow vCPUs from entering to +* ensure DEMOTE will succeed on the second invocation if the first invocation +* returns BUSY. +*/ +static int tdx_sept_split_leaf_spte (struct kvm *kvm,gfn_t gfn,u64 old_spte, +u64 new_spte,enum pg_level level) +{ +struct kvm_vcpu *vcpu =kvm_get_running_vcpu (); +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +gpa_t gpa =gfn_to_gpa (gfn); +u64 err, entry, level_state; +struct page *sept_pt; +int r; + +lockdep_assert_held_write (&kvm->mmu_lock); + +if (KVM_BUG_ON (!is_last_spte (old_spte, level) ||is_last_spte (new_spte, level), kvm)) +return -EIO ; + + sept_pt =tdx_spte_to_sept_pt (kvm, gfn, new_spte, level); +if (!sept_pt) +return -EIO ; + +if (KVM_BUG_ON (!vcpu || vcpu->kvm != kvm, kvm)) +return -EIO ; + + r =tdx_pamt_get (page_to_pfn (sept_pt),PG_LEVEL_4K , &to_tdx (vcpu)->pamt_cache); +if (KVM_BUG_ON (r, kvm)) +return r; + + err =tdh_do_no_vcpus (tdh_mem_range_block, kvm, &kvm_tdx->td, gpa, + level, &entry, &level_state); +if (TDX_BUG_ON_2 (err,TDH_MEM_RANGE_BLOCK , entry, level_state, kvm)) { + r = -EIO ; + goto err; + } + +tdx_track (kvm); + err =tdh_do_no_vcpus (tdh_mem_page_demote, kvm, &kvm_tdx->td, gpa, + level,spte_to_pfn (old_spte), sept_pt, + &to_tdx (vcpu)->pamt_cache, &entry, &level_state); +if (TDX_BUG_ON_2 (err,TDH_MEM_PAGE_DEMOTE , entry, level_state, kvm)) { + r = -EIO ; + goto err; + } + +return 0 ; +err: +tdx_pamt_put (page_to_pfn (sept_pt),PG_LEVEL_4K ); +return r; +} +/* * Handle changes for * (1) leaf SPTEs from non-present to present * (2) non-leaf SPTEs from non-present to present * (3) leaf SPTEs from present to non-present +* (4) present leaf SPTEs to present non-leaf SPTEs (splitting) * * - (1) and (2) must be under shared mmu_lock. If (1) and (2) are under * exclusive mmu_lock (currently impossible), contention errors may lead to
@@ -1888,13 +1951,17 @@ static int tdx_sept_remove_leaf_spte(struct kvm *kvm, gfn_t gfn,* (currently impossible), warnings will be generated due to * lockdep_assert_held_write() or TDX_BUG_ON() caused by concurrent BLOCK, * TRACK, REMOVE. -* - Promotion/demotion is not yet supported. +* - (4) must be under write mmu_lock currently. +* - Promotion is not yet supported. */ static int tdx_sept_set_private_spte (struct kvm *kvm,gfn_t gfn,u64 old_spte,u64 new_spte,enum pg_level level) {lockdep_assert_held (&kvm->mmu_lock); +if (is_shadow_present_pte (old_spte) &&is_shadow_present_pte (new_spte)) +return tdx_sept_split_leaf_spte (kvm, gfn, old_spte, new_spte, level); +if (is_shadow_present_pte (old_spte))return tdx_sept_remove_leaf_spte (kvm, gfn, level, old_spte);
Signature · 1 line
2.43.2
Edgecombe, Rick P
maintainerRe: [PATCH v4 07/17] KVM: TDX: Add core support for splitting/demoting 2MB S-EPT mappings to 4KB
Quoted 28 lines · Yan Zhao
Add support for splitting, a.k.a. demoting, a 2MB S-EPT leaf mapping to 512 smaller 4KB leaf mappings. As per the TDX module rules, first invoke MEM.RANGE.BLOCK to put the huge S-EPT leaf entry into a splittable state, then do MEM.TRACK and kick all vCPUs outside of guest mode to flush TLBs, and finally do MEM.PAGE.DEMOTE to demote/split the huge S-EPT leaf mapping.
Assert the
mmu_lockis held for write, as the BLOCK => TRACK => DEMOTE sequence needs to be "atomic" to guarantee success (and becausemmu_lockmust be held for write to usetdh_do_no_vcpus()).Note, even with
kvm->mmu_lockheld for write,tdh_mem_page_demote()may contend withtdh_vp_enter()and potentially with the guest's S-EPT entry operations. Therefore, wrap the call withtdh_do_no_vcpus()to kick other vCPUs out of the guest and preventtdh_vp_enter()to ensure success.Invoke
tdx_pamt_get()before invokingtdh_mem_page_demote()so that DPAMT pages for the new S-EPT page table page are installed before the DEMOTE SEAMCALL when DPAMT is enabled. DPAMT pages for the guest memory must be installed inside the DEMOTE SEAMCALL, since it is impossible to do so before a successful demotion.Instead of allocating and freeing DPAMT pages for guest pages on the KVM side, pass
pamt_cachetotdh_mem_page_demote()and let it draw DPAMT pages frompamt_cachebefore the DEMOTE SEAMCALL. This prevents KVM from having to manage DPAMT pages directly viaalloc_pamt_array()andfree_pamt_array(), or having knowledge of DPAMT-specific details such asTDX_DPAMT_ENTRY_PAGE_CNT.
Quoted 2 lines
Signed-off-by Xiaoyao Li <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>
The SOB seems wrong. You are the the listed author, but these don't have a Co- developed-by tag.
Quoted 42 lines
[sean: wire up via op
set_external_spte(), merge in DPAMT-related code,
massage changelog]Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Yan Zhao <[email protected]>---
v4:
- Hookedtdx_sept_split_leaf_spte()in x86 opset_external_spte()instead of in x86 opsplit_external_spte()which was no longer introduced in v4. (Sean).
- Renamedtdx_sept_split_private_spte()-->tdx_sept_split_leaf_spte().
- Merged in DPAMT-related code (i.e., passing to_tdx(vcpu)->pamt_cache totdh_mem_page_demote(). (Sean).
- Assertnew_spteis non-leaf. (Yan)v3:
- Rebased on top of Sean's cleanup series.
- Call out UNBLOCK is not required after DEMOTE. (Kai)
-tdx_sept_split_private_spt()-->tdx_sept_split_private_spte().RFC v2:
- Split out the code to handle the errorTDX_INTERRUPTED_RESTARTABLE.
- Rebased to 6.16.0-rc6 (the way of defining TDX hook changes).RFC v1:
- Split patch for exclusivemmu_lockonly,
- Invoketdx_sept_zap_private_spte()andtdx_track()for splitting.
- Handled busy error oftdh_mem_page_demote()by kicking off vCPUs.--- arch/x86/kvm/vmx/tdx.c | 69 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-)arch/x86/kvm/vmx/tdx.c +3
-0@@ -1873,11 +1873,74 @@ static int tdx_sept_remove_leaf_spte(struct kvm *kvm, gfn_t gfn,return 0 ; } +/* +* Split a huge mapping into smaller mappings at a lower level. Currently only +* supports splitting 2MB mappings (KVM doesn't yet support 1GB mappings for TDX
arch/x86/kvm/vmx/tdx.c static int tdx_sept_remove_leaf_spte(struct kvm *kvm, gfn_t gfn, · line 1888
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+* guests). +* +* Invoke "BLOCK + TRACK + kick off vCPUs (inside tdx_track())" since the TDX +* module does not yet support the NON-BLOCKING-RESIZE feature for DEMOTE. +* +* No UNBLOCK is needed after a successful DEMOTE. +* +* Under write mmu_lock, kick off all vCPUs and disallow vCPUs from entering to +* ensure DEMOTE will succeed on the second invocation if the first invocation +* returns BUSY.
How about putting these in the function instead of up here?
Quoted 30 lines
arch/x86/kvm/vmx/tdx.c +30
-0+*/ +static int tdx_sept_split_leaf_spte (struct kvm *kvm,gfn_t gfn,u64 old_spte, +u64 new_spte,enum pg_level level) +{ +struct kvm_vcpu *vcpu =kvm_get_running_vcpu (); +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +gpa_t gpa =gfn_to_gpa (gfn); +u64 err, entry, level_state; +struct page *sept_pt; +int r; + +lockdep_assert_held_write (&kvm->mmu_lock); + +if (KVM_BUG_ON (!is_last_spte (old_spte, level) ||is_last_spte (new_spte, level), kvm)) +return -EIO ; + + sept_pt =tdx_spte_to_sept_pt (kvm, gfn, new_spte, level); +if (!sept_pt) +return -EIO ; + +if (KVM_BUG_ON (!vcpu || vcpu->kvm != kvm, kvm)) +return -EIO ; + + r =tdx_pamt_get (page_to_pfn (sept_pt),PG_LEVEL_4K , &to_tdx (vcpu)->pamt_cache); +if (KVM_BUG_ON (r, kvm)) +return r; + + err =tdh_do_no_vcpus (tdh_mem_range_block, kvm, &kvm_tdx->td, gpa, + level, &entry, &level_state); +if (TDX_BUG_ON_2 (err,TDH_MEM_RANGE_BLOCK , entry, level_state, kvm)) {
arch/x86/kvm/vmx/tdx.c static int tdx_sept_remove_leaf_spte(struct kvm *kvm, gfn_t gfn, · line 1928
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+ r = -EIO ; + goto err; + } + +tdx_track (kvm); + err =tdh_do_no_vcpus (tdh_mem_page_demote, kvm, &kvm_tdx->td, gpa, + level,spte_to_pfn (old_spte), sept_pt, + &to_tdx (vcpu)->pamt_cache, &entry, &level_state); +if (TDX_BUG_ON_2 (err,TDH_MEM_PAGE_DEMOTE , entry, level_state, kvm)) { + r = -EIO ;
Not trying to rollback with an unlock in this case seems like the right call, but can we have a comment justification?
Quoted 19 lines
arch/x86/kvm/vmx/tdx.c +10
-0+ goto err; + } + +return 0 ; +err: +tdx_pamt_put (page_to_pfn (sept_pt),PG_LEVEL_4K ); +return r; +} +/* * Handle changes for * (1) leaf SPTEs from non-present to present * (2) non-leaf SPTEs from non-present to present * (3) leaf SPTEs from present to non-present +* (4) present leaf SPTEs to present non-leaf SPTEs (splitting) * * - (1) and (2) must be under shared mmu_lock. If (1) and (2) are under * exclusive mmu_lock (currently impossible), contention errors may lead to
arch/x86/kvm/vmx/tdx.c static int tdx_sept_remove_leaf_spte(struct kvm *kvm, gfn_t gfn, · line 1954
Quoted 5 lines
arch/x86/kvm/vmx/tdx.c +1
-1@@ -1888,13 +1951,17 @@ static int tdx_sept_remove_leaf_spte(struct kvm *kvm, gfn_t gfn,* (currently impossible), warnings will be generated due to * lockdep_assert_held_write() or TDX_BUG_ON() caused by concurrent BLOCK, * TRACK, REMOVE. -* - Promotion/demotion is not yet supported. +* - (4) must be under write mmu_lock currently.
Currently?
arch/x86/kvm/vmx/tdx.c static int tdx_sept_remove_leaf_spte(struct kvm *kvm, gfn_t gfn, · line 1955
Quoted 1 line
arch/x86/kvm/vmx/tdx.c +1
-0+* - Promotion is not yet supported.
And we are not trying to support it, right?
Can we not make this imply some roadmap of enhancements that may never happen for a long time if ever?
arch/x86/kvm/vmx/tdx.c static int tdx_sept_remove_leaf_spte(struct kvm *kvm, gfn_t gfn, · line 1967
Quoted 12 lines
arch/x86/kvm/vmx/tdx.c +3
-0*/ static int tdx_sept_set_private_spte (struct kvm *kvm,gfn_t gfn,u64 old_spte,u64 new_spte,enum pg_level level) {lockdep_assert_held (&kvm->mmu_lock); +if (is_shadow_present_pte (old_spte) &&is_shadow_present_pte (new_spte)) +return tdx_sept_split_leaf_spte (kvm, gfn, old_spte, new_spte, level); +if (is_shadow_present_pte (old_spte))return tdx_sept_remove_leaf_spte (kvm, gfn, level, old_spte);
Except for those small comments, overall looks good to me.
Re: [PATCH v4 07/17] KVM: TDX: Add core support for splitting/demoting 2MB S-EPT mappings to 4KB
Quoted 29 lines · Rick P
Quoted 28 lines · Yan Zhao
Add support for splitting, a.k.a. demoting, a 2MB S-EPT leaf mapping to 512 smaller 4KB leaf mappings. As per the TDX module rules, first invoke MEM.RANGE.BLOCK to put the huge S-EPT leaf entry into a splittable state, then do MEM.TRACK and kick all vCPUs outside of guest mode to flush TLBs, and finally do MEM.PAGE.DEMOTE to demote/split the huge S-EPT leaf mapping.
Assert the
mmu_lockis held for write, as the BLOCK => TRACK => DEMOTE sequence needs to be "atomic" to guarantee success (and becausemmu_lockmust be held for write to usetdh_do_no_vcpus()).Note, even with
kvm->mmu_lockheld for write,tdh_mem_page_demote()may contend withtdh_vp_enter()and potentially with the guest's S-EPT entry operations. Therefore, wrap the call withtdh_do_no_vcpus()to kick other vCPUs out of the guest and preventtdh_vp_enter()to ensure success.Invoke
tdx_pamt_get()before invokingtdh_mem_page_demote()so that DPAMT pages for the new S-EPT page table page are installed before the DEMOTE SEAMCALL when DPAMT is enabled. DPAMT pages for the guest memory must be installed inside the DEMOTE SEAMCALL, since it is impossible to do so before a successful demotion.Instead of allocating and freeing DPAMT pages for guest pages on the KVM side, pass
pamt_cachetotdh_mem_page_demote()and let it draw DPAMT pages frompamt_cachebefore the DEMOTE SEAMCALL. This prevents KVM from having to manage DPAMT pages directly viaalloc_pamt_array()andfree_pamt_array(), or having knowledge of DPAMT-specific details such asTDX_DPAMT_ENTRY_PAGE_CNT.
Quoted 5 lines
Quoted 2 lines
Signed-off-by Xiaoyao Li <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>The SOB seems wrong. You are the the listed author, but these don't have a Co- developed-by tag.
The current implementation has been substantially reworked from the original one
https://lore.kernel.org/all/b387dcfc499d2f17e448cd4574115d1fd2437921.1708933624.git.isaku.yamahata@intel.com/
Should I use Originally-by tag?
Quoted 39 lines
Quoted 39 lines
[sean: wire up via op
set_external_spte(), merge in DPAMT-related code,
massage changelog]Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Yan Zhao <[email protected]>---
v4:
- Hookedtdx_sept_split_leaf_spte()in x86 opset_external_spte()instead of in x86 opsplit_external_spte()which was no longer introduced in v4. (Sean).
- Renamedtdx_sept_split_private_spte()-->tdx_sept_split_leaf_spte().
- Merged in DPAMT-related code (i.e., passing to_tdx(vcpu)->pamt_cache totdh_mem_page_demote(). (Sean).
- Assertnew_spteis non-leaf. (Yan)v3:
- Rebased on top of Sean's cleanup series.
- Call out UNBLOCK is not required after DEMOTE. (Kai)
-tdx_sept_split_private_spt()-->tdx_sept_split_private_spte().RFC v2:
- Split out the code to handle the errorTDX_INTERRUPTED_RESTARTABLE.
- Rebased to 6.16.0-rc6 (the way of defining TDX hook changes).RFC v1:
- Split patch for exclusivemmu_lockonly,
- Invoketdx_sept_zap_private_spte()andtdx_track()for splitting.
- Handled busy error oftdh_mem_page_demote()by kicking off vCPUs.--- arch/x86/kvm/vmx/tdx.c | 69 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-)arch/x86/kvm/vmx/tdx.c +0
-0@@ -1873,11 +1873,74 @@ static int tdx_sept_remove_leaf_spte(struct kvm *kvm, gfn_t gfn,return 0 ; }
Quoted 15 lines
arch/x86/kvm/vmx/tdx.c static int tdx_sept_remove_leaf_spte(struct kvm *kvm, gfn_t gfn, · line 1888
Quoted 13 lines
arch/x86/kvm/vmx/tdx.c +13
-0+/* +* Split a huge mapping into smaller mappings at a lower level. Currently only +* supports splitting 2MB mappings (KVM doesn't yet support 1GB mappings for TDX +* guests). +* +* Invoke "BLOCK + TRACK + kick off vCPUs (inside tdx_track())" since the TDX +* module does not yet support the NON-BLOCKING-RESIZE feature for DEMOTE. +* +* No UNBLOCK is needed after a successful DEMOTE. +* +* Under write mmu_lock, kick off all vCPUs and disallow vCPUs from entering to +* ensure DEMOTE will succeed on the second invocation if the first invocation +* returns BUSY. How about putting these in the function instead of up here?
Sounds good.
Quoted 35 lines
Quoted 35 lines
arch/x86/kvm/vmx/tdx.c +35
-0+*/ +static int tdx_sept_split_leaf_spte (struct kvm *kvm,gfn_t gfn,u64 old_spte, +u64 new_spte,enum pg_level level) +{ +struct kvm_vcpu *vcpu =kvm_get_running_vcpu (); +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +gpa_t gpa =gfn_to_gpa (gfn); +u64 err, entry, level_state; +struct page *sept_pt; +int r; + +lockdep_assert_held_write (&kvm->mmu_lock); + +if (KVM_BUG_ON (!is_last_spte (old_spte, level) ||is_last_spte (new_spte, level), kvm)) +return -EIO ; + + sept_pt =tdx_spte_to_sept_pt (kvm, gfn, new_spte, level); +if (!sept_pt) +return -EIO ; + +if (KVM_BUG_ON (!vcpu || vcpu->kvm != kvm, kvm)) +return -EIO ; + + r =tdx_pamt_get (page_to_pfn (sept_pt),PG_LEVEL_4K , &to_tdx (vcpu)->pamt_cache); +if (KVM_BUG_ON (r, kvm)) +return r; + + err =tdh_do_no_vcpus (tdh_mem_range_block, kvm, &kvm_tdx->td, gpa, + level, &entry, &level_state); +if (TDX_BUG_ON_2 (err,TDH_MEM_RANGE_BLOCK , entry, level_state, kvm)) { + r = -EIO ; + goto err; + } + +tdx_track (kvm);
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c static int tdx_sept_remove_leaf_spte(struct kvm *kvm, gfn_t gfn, · line 1928
Quoted 5 lines
arch/x86/kvm/vmx/tdx.c +5
-0+ err =tdh_do_no_vcpus (tdh_mem_page_demote, kvm, &kvm_tdx->td, gpa, + level,spte_to_pfn (old_spte), sept_pt, + &to_tdx (vcpu)->pamt_cache, &entry, &level_state); +if (TDX_BUG_ON_2 (err,TDH_MEM_PAGE_DEMOTE , entry, level_state, kvm)) { + r = -EIO ;Not trying to rollback with an unlock in this case seems like the right call, but can we have a comment justification?
Makes sense.
What about:
"Something severe went wrong. The VM is going to die.
No need to unblock, which could potentially fail as well."
Quoted 27 lines
Quoted 19 lines
arch/x86/kvm/vmx/tdx.c +10
-0+ goto err; + } + +return 0 ; +err: +tdx_pamt_put (page_to_pfn (sept_pt),PG_LEVEL_4K ); +return r; +} +/* * Handle changes for * (1) leaf SPTEs from non-present to present * (2) non-leaf SPTEs from non-present to present * (3) leaf SPTEs from present to non-present +* (4) present leaf SPTEs to present non-leaf SPTEs (splitting) * * - (1) and (2) must be under shared mmu_lock. If (1) and (2) are under * exclusive mmu_lock (currently impossible), contention errors may lead to arch/x86/kvm/vmx/tdx.c static int tdx_sept_remove_leaf_spte(struct kvm *kvm, gfn_t gfn, · line 1954
Quoted 5 lines
arch/x86/kvm/vmx/tdx.c +1
-1@@ -1888,13 +1951,17 @@ static int tdx_sept_remove_leaf_spte(struct kvm *kvm, gfn_t gfn,* (currently impossible), warnings will be generated due to * lockdep_assert_held_write() or TDX_BUG_ON() caused by concurrent BLOCK, * TRACK, REMOVE. -* - Promotion/demotion is not yet supported. +* - (4) must be under write mmu_lock currently. Currently?
Quoted 6 lines
arch/x86/kvm/vmx/tdx.c static int tdx_sept_remove_leaf_spte(struct kvm *kvm, gfn_t gfn, · line 1955
Quoted 1 line
arch/x86/kvm/vmx/tdx.c +1
-0+* - Promotion is not yet supported. And we are not trying to support it, right?
Can we not make this imply some roadmap of enhancements that may never happen for a long time if ever?
Ok. Let me correct them to:
- (4) must be under write mmu_lock.
- Promotion is not supported
Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/vmx/tdx.c +0
-0*/ static int tdx_sept_set_private_spte (struct kvm *kvm,gfn_t gfn,u64 old_spte,u64 new_spte,enum pg_level level) {lockdep_assert_held (&kvm->mmu_lock);
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c static int tdx_sept_remove_leaf_spte(struct kvm *kvm, gfn_t gfn, · line 1967
Quoted 6 lines
arch/x86/kvm/vmx/tdx.c +3
-0+if (is_shadow_present_pte (old_spte) &&is_shadow_present_pte (new_spte)) +return tdx_sept_split_leaf_spte (kvm, gfn, old_spte, new_spte, level); +if (is_shadow_present_pte (old_spte))return tdx_sept_remove_leaf_spte (kvm, gfn, level, old_spte);Except for those small comments, overall looks good to me.
Thanks for the detailed review!
Edgecombe, Rick P
maintainerRe: [PATCH v4 07/17] KVM: TDX: Add core support for splitting/demoting 2MB S-EPT mappings to 4KB
Quoted 8 lines · Yan Zhao
Quoted 3 lines
The SOB seems wrong. You are the the listed author, but these don't have a Co-
developed-by tag.The current implementation has been substantially reworked from the original one
https://lore.kernel.org/all/b387dcfc499d2f17e448cd4574115d1fd2437921.1708933624.git.isaku.yamahata@intel.com/Should I use Originally-by tag?
For the tags I don't have a problem with the author change. But the tags need to pass checkpatch, tip maintainer docs, etc. Which I expect this doesn't. I would have just moved the other contributors to co-developed-by.
Comments looked fine to me.
Re: [PATCH v4 07/17] KVM: TDX: Add core support for splitting/demoting 2MB S-EPT mappings to 4KB
Quoted 8 lines · Rick P
Quoted 7 lines · Yan Zhao
Quoted 3 lines
The SOB seems wrong. You are the the listed author, but these don't have a Co-
developed-by tag.The current implementation has been substantially reworked from the original one
https://lore.kernel.org/all/b387dcfc499d2f17e448cd4574115d1fd2437921.1708933624.git.isaku.yamahata@intel.com/
Quoted 5 lines
Quoted 1 line
Should I use Originally-by tag?
For the tags I don't have a problem with the author change. But the tags need to pass checkpatch, tip maintainer docs, etc. Which I expect this doesn't. I would have just moved the other contributors to co-developed-by.
Will update according to your suggestion!
Quoted 1 line
Comments looked fine to me.
Thanks.
PATCH v4 8/17 KVM: TDX: Adjust the topup count of DPAMT page pairs for splitting S-EPT
In reply to Yan ZhaoKVM needs to allocate enough DPAMT page pairs in the pamt_cache for consumption by both page table pages and guest pages. Since the DPAMT page pair for the S-EPT root page is already allocated during TD initialization, there is no need to allocate the DPAMT page pair for the S-EPT root page. Therefore, previously the DPAMT page pairs required equals "min_nr_spts - 1 + 1".
When splitting S-EPT, min_nr_spts does not include the root SPT. So, limit the -1 calculation to when min_nr_spts equals root_level, though this will cause one pair over-allocation in the normal page fault path because PT64_ROOT_MAX_LEVEL is always passed even when launching a 4-level TD.
Additionally, KVM may need to retry tdh_mem_page_demote() a second time, causing the DPAMT page pair for the guest private pages to be drawn from the pamt_cache twice in the worst case. Therefore, add an extra +1 to cover this worst-case scenario.
The slight over-allocation is acceptable since KVM already pre-allocates more pages than needed (e.g., when mapping huge pages) in case of the worst-case scenario.
--- arch/x86/kvm/vmx/tdx.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-)
arch/x86/kvm/vmx/tdx.c +22 -4
@@ -1630,16 +1630,34 @@ void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level)static int tdx_topup_external_pamt_cache (struct kvm_vcpu *vcpu,int min_nr_spts) { +int dpamt_pairs; +if (WARN_ON_ONCE (!vcpu))return -EIO ; +/* Exclude the root SPT, as its DPAMT page pair is already installed */ +if (min_nr_spts == vcpu->kvm->arch.mirror_root_level) + min_nr_spts -=1 ; + +/* +* Each S-EPT page table page + 4KB guest private page needs a pair of +* DPAMT pages. +*/ + dpamt_pairs = min_nr_spts +1 ; +/* -* Minus one page to exclude the root SPT, but plus one page for a -* possible 4KB private mapping. +* After each topup, KVM may invoke DEMOTE at most twice. The first +* DEMOTE invocation draws two pairs of pages from the cache: one for +* the S-EPT page table page and one for the guest private memory. +* Since these pages are not returned to the cache, the second DEMOTE +* invocation still needs to consume one additional pair for the guest +* private memory (the pair for the S-EPT page table page is reused for +* the 2nd invocation). Increase the topup count to account for this +* worst-case scenario. */ - min_nr_spts +=-1 +1 ; + dpamt_pairs +=1 ; -return tdx_topup_pamt_cache (&to_tdx (vcpu)->pamt_cache, min_nr_spts); +return tdx_topup_pamt_cache (&to_tdx (vcpu)->pamt_cache, dpamt_pairs); } static int tdx_mem_page_add(struct kvm *kvm, gfn_t gfn, enum pg_level level,
Signature · 1 line
2.43.2
Edgecombe, Rick P
maintainerRe: [PATCH v4 08/17] KVM: TDX: Adjust the topup count of DPAMT page pairs for splitting S-EPT
Quoted 12 lines · Yan Zhao
KVM needs to allocate enough DPAMT page pairs in the
pamt_cachefor consumption by both page table pages and guest pages. Since the DPAMT page pair for the S-EPT root page is already allocated during TD initialization, there is no need to allocate the DPAMT page pair for the S-EPT root page. Therefore, previously the DPAMT page pairs required equals "min_nr_spts- 1 + 1".When splitting S-EPT,
min_nr_sptsdoes not include the root SPT. So, limit the -1 calculation to whenmin_nr_sptsequalsroot_level, though this will cause one pair over-allocation in the normal page fault path becausePT64_ROOT_MAX_LEVELis always passed even when launching a 4-level TD.
Quoted 8 lines
Additionally, KVM may need to retry
tdh_mem_page_demote()a second time, causing the DPAMT page pair for the guest private pages to be drawn from thepamt_cachetwice in the worst case. Therefore, add an extra +1 to cover this worst-case scenario.The slight over-allocation is acceptable since KVM already pre-allocates more pages than needed (e.g., when mapping huge pages) in case of the worst-case scenario.
This is assumes way too much about the callers and way too convoluted. The existing code already did but, now this is just too far. We need another solution.
Questions below on what that might be.
Quoted 12 lines
Signed-off-by Yan Zhao <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-)arch/x86/kvm/vmx/tdx.c +0
-0@@ -1630,16 +1630,34 @@ void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level)
arch/x86/kvm/vmx/tdx.c void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level) · line 1640
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +5
-0static int tdx_topup_external_pamt_cache (struct kvm_vcpu *vcpu,int min_nr_spts) { +int dpamt_pairs; +if (WARN_ON_ONCE (!vcpu))return -EIO ; +/* Exclude the root SPT, as its DPAMT page pair is already installed */ +if (min_nr_spts == vcpu->kvm->arch.mirror_root_level) + min_nr_spts -=1 ;
Over allocating a bit is not the end of the world...
arch/x86/kvm/vmx/tdx.c void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level) · line 1659
Quoted 12 lines
arch/x86/kvm/vmx/tdx.c +8
-2+ +/* +* Each S-EPT page table page + 4KB guest private page needs a pair of +* DPAMT pages. +*/ + dpamt_pairs = min_nr_spts +1 ; +/* -* Minus one page to exclude the root SPT, but plus one page for a -* possible 4KB private mapping. +* After each topup, KVM may invoke DEMOTE at most twice.
Why twice? You mean the BUSY retry attempt, right? If you do, couldn't we fix this problem within tdh_mem_page_demote()?
arch/x86/kvm/vmx/tdx.c void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level)
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +6
-0The first + * DEMOTE invocation draws two pairs of pages from the cache: one for + * the S-EPT page table page and one for the guest private memory. + * Since these pages are not returned to the cache, the second DEMOTE + * invocation still needs to consume one additional pair for the guest + * private memory (the pair for the S-EPT page table page is reused for + * the 2nd invocation).
Another idea, change the op to be:
int topup_external_cache(struct kvm_vcpu *vcpu, bool root, bool private_page, int min_nr_spts);
Normal topup can set:
root=true
private_page=truemin_nr_spts = PT64_ROOT_MAX_LEVEL - 1
Then we can calculate exactly what we need. And even better, the existing code won't nee a comment to explain the weirdness.
Quoted 11 lines
arch/x86/kvm/vmx/tdx.c +3
-2Increase the topup count to account for this +* worst-case scenario. */ - min_nr_spts +=-1 +1 ; + dpamt_pairs +=1 ; -return tdx_topup_pamt_cache (&to_tdx (vcpu)->pamt_cache, min_nr_spts); +return tdx_topup_pamt_cache (&to_tdx (vcpu)->pamt_cache, dpamt_pairs); } static int tdx_mem_page_add(struct kvm *kvm, gfn_t gfn, enum pg_level level,
Edgecombe, Rick P
maintainerRe: [PATCH v4 08/17] KVM: TDX: Adjust the topup count of DPAMT page pairs for splitting S-EPT
Quoted 11 lines · Rick Edgecombe
Another idea, change the op to be:
inttopup_external_cache(struct kvm_vcpu *vcpu, bool root, bool private_page, int min_nr_spts);Normal topup can set:
root=true
private_page=truemin_nr_spts=PT64_ROOT_MAX_LEVEL- 1Then we can calculate exactly what we need. And even better, the existing code won't nee a comment to explain the weirdness.
Actually since this gets a struct kvm arg later in the series, maybe we could just have two ops. One for topping up from a fault and another for a split.
Re: [PATCH v4 08/17] KVM: TDX: Adjust the topup count of DPAMT page pairs for splitting S-EPT
Quoted 10 lines · Rick P
Quoted 9 lines · Rick Edgecombe
Another idea, change the op to be:
inttopup_external_cache(struct kvm_vcpu *vcpu, bool root, bool private_page, int min_nr_spts);Normal topup can set:
root=true
private_page=truemin_nr_spts=PT64_ROOT_MAX_LEVEL- 1
Quoted 5 lines
Quoted 2 lines
Then we can calculate exactly what we need. And even better, the existing code won't nee a comment to explain the weirdness.
Actually since this gets a struct kvm arg later in the series, maybe we could just have two ops. One for topping up from a fault and another for a split.
Hmm, no? That would make the min_nr_spts arg meaningless. Reusing the topping up op for the fault also scales better, e.g., it could support splitting under the fault path in the future.
Besides, I actually introduced a separate cache and op for split in v3,
https://lore.kernel.org/all/[email protected].
Sean merged the cache/op for split with the cache/op for fault in DPAMT+Hugepage v5.
Edgecombe, Rick P
maintainerRe: [PATCH v4 08/17] KVM: TDX: Adjust the topup count of DPAMT page pairs for splitting S-EPT
Quoted 11 lines · Yan Zhao
Quoted 10 lines · Rick P
Quoted 9 lines · Rick Edgecombe
Another idea, change the op to be:
inttopup_external_cache(struct kvm_vcpu *vcpu, bool root, bool private_page, int min_nr_spts);Normal topup can set:
root=true
private_page=truemin_nr_spts=PT64_ROOT_MAX_LEVEL- 1
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Then we can calculate exactly what we need. And even better, the existing code won't nee a comment to explain the weirdness.
Actually since this gets a struct kvm arg later in the series, maybe we could just have two ops. One for topping up from a fault and another for a split.
Hmm, no? That would make the
min_nr_sptsarg meaningless. Reusing the topping up op for the fault also scales better, e.g., it could support splitting under the fault path in the future.
I don't see how min_nr_spts is meaningless. What do you mean?
Let me spell it out a bit more. For a fault it would have math like this:
int topup(bool root, bool private_page, int nr_extra_spts) {
add_to_cache(private_page + nr_extra_spts)
}
For fault:topup(true, true, PT64_ROOT_MAX_LEVEL - 1)
For split:topup(false, false, 1)
Why have root as a separate arg even though it is never used? Because it makes the logic make sense from code KVM.
Quoted 5 lines
Besides, I actually introduced a separate cache and op for split in v3,
https://lore.kernel.org/all/[email protected].
Sean merged the cache/op for split with the cache/op for fault in DPAMT+Hugepage v5.
Yea, but he didn't have any big assumptions in it like this. It was way simpler.
Re: [PATCH v4 08/17] KVM: TDX: Adjust the topup count of DPAMT page pairs for splitting S-EPT
Quoted 30 lines · Rick P
Quoted 11 lines · Yan Zhao
Quoted 10 lines · Rick P
Quoted 9 lines · Rick Edgecombe
Another idea, change the op to be:
inttopup_external_cache(struct kvm_vcpu *vcpu, bool root, bool private_page, int min_nr_spts);Normal topup can set:
root=true
private_page=truemin_nr_spts=PT64_ROOT_MAX_LEVEL- 1Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Then we can calculate exactly what we need. And even better, the existing code won't nee a comment to explain the weirdness.
Actually since this gets a struct kvm arg later in the series, maybe we could just have two ops. One for topping up from a fault and another for a split.
Hmm, no? That would make the
min_nr_sptsarg meaningless. Reusing the topping up op for the fault also scales better, e.g., it could support splitting under the fault path in the future.I don't see how
min_nr_sptsis meaningless. What do you mean?Let me spell it out a bit more. For a fault it would have math like this:
int
topup(bool root, bool private_page, int nr_extra_spts){
add_to_cache(private_page + nr_extra_spts)
}
Quoted 5 lines
For fault:
topup(true, true, PT64_ROOT_MAX_LEVEL - 1)For split:
topup(false, false, 1)
Hmm, having the caller perform the -1 calculation gives me some hesitation. The meaning of nr_extra_spts is not straightforward, though it seems to mean the number of spts excluding the root.
private_page arg should be unnecessary, since the split path also needs to allocate DPAMT pages for the guest private page.
Quoted 2 lines
Why have root as a separate arg even though it is never used? Because it makes the logic make sense from code KVM.
What about:
int topup(bool has_root, int nr_spts)
{
add_to_cache(nr_spts + (has_root ? -1 : 0) + 1);
}
For fault:topup(true, PT64_ROOT_MAX_LEVEL)
For split:topup(false, 1)
Quoted 6 lines
Quoted 4 lines
Besides, I actually introduced a separate cache and op for split in v3,
https://lore.kernel.org/all/[email protected].
Sean merged the cache/op for split with the cache/op for fault in DPAMT+Hugepage v5.Yea, but he didn't have any big assumptions in it like this. It was way simpler.
Since the re-insertion solution looks good to you, it should be not be too complex now :)
Given that the fault path theoretically also needs to address the root-only -1 calculation issue, having split and fault share the same op seems reasonable?
Edgecombe, Rick P
maintainerRe: [PATCH v4 08/17] KVM: TDX: Adjust the topup count of DPAMT page pairs for splitting S-EPT
Quoted 8 lines · Yan Zhao
Quoted 5 lines
For fault:
topup(true, true, PT64_ROOT_MAX_LEVEL - 1)For split:
topup(false, false, 1)Hmm, having the caller perform the -1 calculation gives me some hesitation. The meaning of
nr_extra_sptsis not straightforward, though it seems to mean the number of spts excluding the root.
It means "extra" beyond the root. How about:
int topup(bool root_spt, int nr_extra_spts, bool private_page)
The idea is to tell external infra what the fault handler might do. It might add a root, it might add a private leaf and it may need extra spts beyond the root. These are all concepts known to mirror EPT and also has all the info needed for the calculation on the TDX side, and also doesn't assume anything about TDX implementation from the mirror side. There is extra info passed, that is not needed. But the gain is that the code on both sides and calculation are self documenting.
Quoted 7 lines
private_pagearg should be unnecessary, since the split path also needs to allocate DPAMT pages for the guest private page.Quoted 3 lines
Why have root as a separate arg even though it is never used? Because it makes
the logic make sense from code KVM.
Quoted 6 lines
What about:
int
topup(bool has_root, int nr_spts)
{
add_to_cache(nr_spts + (has_root ? -1 : 0) + 1); }
"(has_root ? -1 : 0) + 1)" is going to need a comment. So I was suggesting just to make it obvious in the code.
Quoted 19 lines
For fault:
topup(true, PT64_ROOT_MAX_LEVEL)For split:
topup(false, 1)Quoted 7 lines
Quoted 4 lines
Besides, I actually introduced a separate cache and op for split in v3,
https://lore.kernel.org/all/[email protected].
Sean merged the cache/op for split with the cache/op for fault in DPAMT+Hugepage v5.Yea, but he didn't have any big assumptions in it like this. It was way simpler.
Since the re-insertion solution looks good to you, it should be not be too complex now :)
Given that the fault path theoretically also needs to address the root-only -1 calculation issue, having split and fault share the same op seems reasonable?
Re: [PATCH v4 08/17] KVM: TDX: Adjust the topup count of DPAMT page pairs for splitting S-EPT
Quoted 14 lines · Rick P
Quoted 8 lines · Yan Zhao
Quoted 5 lines
For fault:
topup(true, true, PT64_ROOT_MAX_LEVEL - 1)For split:
topup(false, false, 1)Hmm, having the caller perform the -1 calculation gives me some hesitation. The meaning of
nr_extra_sptsis not straightforward, though it seems to mean the number of spts excluding the root.It means "extra" beyond the root. How about:
int
topup(bool root_spt, int nr_extra_spts, bool private_page)
Quoted 7 lines
The idea is to tell external infra what the fault handler might do. It might add a root, it might add a private leaf and it may need extra spts beyond the root. These are all concepts known to mirror EPT and also has all the info needed for the calculation on the TDX side, and also doesn't assume anything about TDX implementation from the mirror side. There is extra info passed, that is not needed. But the gain is that the code on both sides and calculation are self documenting.
It seems strange to me to have nr_extra_spts if root_spt is false, in which case nr_extra_spts are all the spts the caller wants to topup for, so why call them extra?
Also, is there a case where "private_page" is false? I can't find one... Specifically for the "split" case (patch 6,
https://lore.kernel.org/all/[email protected]/) where .topup_external_cache() is invoked in tdp_mmu_alloc_sp_for_split(), how does the caller (tdp_mmu_alloc_sp_for_split()) know whether private_page should be true or false without TDX's specific knowledge?
So, I propose dropping the private_page argument and renaming root_spt to has_root_spt. i.e., either:
(1) int topup(bool has_root_spt, int nr_spts);
or
(2) int topup(bool has_root_spt, int nonroot_spts);
I prefer (1), as it gives the caller the freedom to always pass PT64_ROOT_MAX_LEVEL even for 4-level VMs. Additionally, has_root_spt in topup() in (2) would be useless.
int topup(bool has_root_spt, int nonroot_spts)
{
add_to_cache(nonroot_spts + 1);
}
Quoted 8 lines
Quoted 8 lines
private_pagearg should be unnecessary, since the split path also needs to allocate DPAMT pages for the guest private page.Quoted 3 lines
Why have root as a separate arg even though it is never used? Because it makes
the logic make sense from code KVM.What about:
Quoted 7 lines
Quoted 4 lines
int
topup(bool has_root, int nr_spts)
{
add_to_cache(nr_spts + (has_root ? -1 : 0) + 1); }"(
has_root? -1 : 0) + 1)" is going to need a comment. So I was suggesting just to make it obvious in the code.
like this?
int topup(bool has_root, int nr_spts)
{
int pairs = nr_spts;
/* the one for root has been allocated */
if (has_root)
pairs -= 1;
/* for guest private page */
pairs += 1; add_to_cache(pairs);
}
Quoted 19 lines
Quoted 18 lines
For fault:
topup(true, PT64_ROOT_MAX_LEVEL)For split:
topup(false, 1)Quoted 7 lines
Quoted 4 lines
Besides, I actually introduced a separate cache and op for split in v3,
https://lore.kernel.org/all/[email protected].
Sean merged the cache/op for split with the cache/op for fault in DPAMT+Hugepage v5.Yea, but he didn't have any big assumptions in it like this. It was way simpler.
Since the re-insertion solution looks good to you, it should be not be too complex now :)
Given that the fault path theoretically also needs to address the root-only -1 calculation issue, having split and fault share the same op seems reasonable?
Edgecombe, Rick P
maintainerRe: [PATCH v4 08/17] KVM: TDX: Adjust the topup count of DPAMT page pairs for splitting S-EPT
Quoted 10 lines · Yan Zhao
Quoted 10 lines
The idea is to tell external infra what the fault handler might do. It might add
a root, it might add a private leaf and it may need extra spts beyond the root.
These are all concepts known to mirror EPT and also has all the info needed for
the calculation on the TDX side, and also doesn't assume anything about TDX implementation from the mirror side. There is extra info passed, that is not needed. But the gain is that the code on both sides and calculation are self documenting.
Quoted 4 lines
It seems strange to me to have
nr_extra_sptsifroot_sptis false, in which casenr_extra_sptsare all the spts the caller wants to topup for, so why call them extra?
Can you suggest a better name for "extra"?
Quoted 9 lines
Also, is there a case where "
private_page" is false? I can't find one... Specifically for the "split" case (patch 6,
https://lore.kernel.org/all/[email protected]/)
where
.topup_external_cache() is invoked intdp_mmu_alloc_sp_for_split(), how does the
caller (tdp_mmu_alloc_sp_for_split()) know whetherprivate_pageshould be true or false without TDX's specific knowledge?
The point of spelling it all out was to make the contact clear and the implementation doesn't have to assume anything about the callers.
Re: [PATCH v4 08/17] KVM: TDX: Adjust the topup count of DPAMT page pairs for splitting S-EPT
Quoted 11 lines · Rick P
Quoted 10 lines · Yan Zhao
Quoted 10 lines
The idea is to tell external infra what the fault handler might do. It might add
a root, it might add a private leaf and it may need extra spts beyond the root.
These are all concepts known to mirror EPT and also has all the info needed for
the calculation on the TDX side, and also doesn't assume anything about TDX implementation from the mirror side. There is extra info passed, that is not needed. But the gain is that the code on both sides and calculation are self documenting.
Quoted 6 lines
Quoted 4 lines
It seems strange to me to have
nr_extra_sptsifroot_sptis false, in which casenr_extra_sptsare all the spts the caller wants to topup for, so why call them extra?Can you suggest a better name for "extra"?
I think non-root_spts is clearer.
Quoted 11 lines
Quoted 8 lines
Also, is there a case where "
private_page" is false? I can't find one... Specifically for the "split" case (patch 6,
https://lore.kernel.org/all/[email protected]/)
where
.topup_external_cache() is invoked intdp_mmu_alloc_sp_for_split(), how does the
caller (tdp_mmu_alloc_sp_for_split()) know whetherprivate_pageshould be true or false without TDX's specific knowledge?The point of spelling it all out was to make the contact clear and the implementation doesn't have to assume anything about the callers.
Hmm, but it's hard for the caller to know if private_page is true. We should avoid leaking TDX specific knowledge to KVM MMU, right?
Edgecombe, Rick P
maintainerRe: [PATCH v4 08/17] KVM: TDX: Adjust the topup count of DPAMT page pairs for splitting S-EPT
Quoted 4 lines · Yan Zhao
Quoted 2 lines
The point of spelling it all out was to make the contact clear and the implementation doesn't have to assume anything about the callers.
Hmm, but it's hard for the caller to know if
private_pageis true. We should avoid leaking TDX specific knowledge to KVM MMU, right?
Wait, why won't the caller won't know if it might map a new private page? This is basically to cover the +1 part of the "+ 1 -1" in the calculation. I was thinking to set it to true for normal ept violation (like an accept) and false for split.
Re: [PATCH v4 08/17] KVM: TDX: Adjust the topup count of DPAMT page pairs for splitting S-EPT
Quoted 10 lines · Rick P
Quoted 4 lines · Yan Zhao
Quoted 2 lines
The point of spelling it all out was to make the contact clear and the implementation doesn't have to assume anything about the callers.
Hmm, but it's hard for the caller to know if
private_pageis true. We should avoid leaking TDX specific knowledge to KVM MMU, right?Wait, why won't the caller won't know if it might map a new private page? This is basically to cover the +1 part of the "+ 1 -1" in the calculation. I was thinking to set it to true for normal ept violation (like an accept) and false for split.
In TDX's .topup_external_cache() hook, tdx_topup_external_pamt_cache() always needs to allocate DPAMT pages for guest private pages, even for the split case.
However, from KVM MMU's perspective, it is unnatural to specify "private_page" as true when tdp_mmu_alloc_sp_for_split() is preparing the mid-level page table page.
So, I think we should keep the current convention of just specifying min_nr_spts, and let the TDX-specific tdx_topup_external_pamt_cache() account for the +1 part for guest private pages, which is invisible and should not be exposed to KVM MMU.
So, what about
topup(bool with_root_spt, int min_nr_spts) ?
Edgecombe, Rick P
maintainerRe: [PATCH v4 08/17] KVM: TDX: Adjust the topup count of DPAMT page pairs for splitting S-EPT
Quoted 18 lines · Yan Zhao
Quoted 10 lines · Rick P
Quoted 4 lines · Yan Zhao
Quoted 2 lines
The point of spelling it all out was to make the contact clear and the implementation doesn't have to assume anything about the callers.
Hmm, but it's hard for the caller to know if
private_pageis true. We should avoid leaking TDX specific knowledge to KVM MMU, right?Wait, why won't the caller won't know if it might map a new private page? This is basically to cover the +1 part of the "+ 1 -1" in the calculation. I was thinking to set it to true for normal ept violation (like an accept) and false for split.
In TDX's .topup_external_cache() hook,
tdx_topup_external_pamt_cache()always needs to allocate DPAMT pages for guest private pages, even for the split case.However, from KVM MMU's perspective, it is unnatural to specify "
private_page" as true whentdp_mmu_alloc_sp_for_split()is preparing the mid-level page table page.
Quoted 8 lines
So, I think we should keep the current convention of just specifying
min_nr_spts, and let the TDX-specifictdx_topup_external_pamt_cache()account for the +1 part for guest private pages, which is invisible and should not be exposed to KVM MMU.So, what about
topup(bool with_root_spt, int min_nr_spts)?
Hmm, ok I think I see what you are saying. Because each 4KB page will be confined to a 2MB aligned region, we need at most 1 extra page pair. Not 512. But the core MMU should not know this at all. Hmmm....
Yea let's go with your suggestion. Can you share the implementation here first though so we can close it without risk of more respins?
Re: [PATCH v4 08/17] KVM: TDX: Adjust the topup count of DPAMT page pairs for splitting S-EPT
Quoted 26 lines · Rick P
Quoted 25 lines · Yan Zhao
Quoted 10 lines · Rick P
Quoted 4 lines · Yan Zhao
Quoted 2 lines
The point of spelling it all out was to make the contact clear and the implementation doesn't have to assume anything about the callers.
Hmm, but it's hard for the caller to know if
private_pageis true. We should avoid leaking TDX specific knowledge to KVM MMU, right?Wait, why won't the caller won't know if it might map a new private page? This is basically to cover the +1 part of the "+ 1 -1" in the calculation. I was thinking to set it to true for normal ept violation (like an accept) and false for split.
In TDX's .topup_external_cache() hook,
tdx_topup_external_pamt_cache()always needs to allocate DPAMT pages for guest private pages, even for the split case.However, from KVM MMU's perspective, it is unnatural to specify "
private_page" as true whentdp_mmu_alloc_sp_for_split()is preparing the mid-level page table page.So, I think we should keep the current convention of just specifying
min_nr_spts, and let the TDX-specifictdx_topup_external_pamt_cache()account for the +1 part for guest private pages, which is invisible and should not be exposed to KVM MMU.So, what about
Quoted 8 lines
Quoted 1 line
topup(bool with_root_spt, int min_nr_spts)?Hmm, ok I think I see what you are saying. Because each 4KB page will be confined to a 2MB aligned region, we need at most 1 extra page pair. Not 512. But the core MMU should not know this at all. Hmmm....
Yea let's go with your suggestion. Can you share the implementation here first though so we can close it without risk of more respins?
Sure.
Here's how tdx_topup_external_pamt_cache() looks by the end of the series:
static int tdx_topup_external_pamt_cache(struct kvm *kvm, struct kvm_vcpu *vcpu,
bool with_root_spt, int min_nr_spts)
{
struct tdx_pamt_cache *pamt_cache;
int dpamt_pairs = min_nr_spts;
pamt_cache = tdx_get_pamt_cache(kvm, vcpu);
if (!pamt_cache)
return -EIO;
/* DPAMT page pair for the root SPT has already been installed */
if (with_root_spt)
dpamt_pairs -= 1;
/* +1 pair for 4KB guest private page */
dpamt_pairs += 1;
return tdx_topup_pamt_cache(pamt_cache, dpamt_pairs);
}In the fault path, mmu_topup_memory_caches() calls:
r = kvm_x86_call(topup_external_cache)(vcpu->kvm, vcpu, true, PT64_ROOT_MAX_LEVEL);
In the split path, tdp_mmu_alloc_sp_for_split() calls:
kvm_x86_call(topup_external_cache)(kvm, kvm_get_running_vcpu(), false, 1);
Edgecombe, Rick P
maintainerRe: [PATCH v4 08/17] KVM: TDX: Adjust the topup count of DPAMT page pairs for splitting S-EPT
Quoted 15 lines · Yan Zhao
static int tdx_topup_external_pamt_cache(struct kvm *kvm, struct
kvm_vcpu*vcpu,
boolwith_root_spt, intmin_nr_spts) {
structtdx_pamt_cache*pamt_cache;
intdpamt_pairs=min_nr_spts;
pamt_cache=tdx_get_pamt_cache(kvm, vcpu); if (!pamt_cache)
return-EIO;/* DPAMT page pair for the root SPT has already been installed */ if (
with_root_spt)
dpamt_pairs-= 1;
Quoted 5 lines
/* +1 pair for 4KB guest private page */
dpamt_pairs+= 1;return
tdx_topup_pamt_cache(pamt_cache, dpamt_pairs); }
Yes this looks much clearer than even the upstream code. Do you have any better ideas than "4KB guest private page" though? Because in the split case it is actually for 512 guest private pages. We should put an artful comment to explain both cases while not making it look like something that has a better solution. Because I'm thinking there isn't one.
Quoted 7 lines
In the fault path,
mmu_topup_memory_caches()calls:
r = kvm_x86_call(topup_external_cache)(vcpu->kvm, vcpu, true,PT64_ROOT_MAX_LEVEL);In the split path,
tdp_mmu_alloc_sp_for_split()calls:
kvm_x86_call(topup_external_cache)(kvm,kvm_get_running_vcpu(), false, 1);
Re: [PATCH v4 08/17] KVM: TDX: Adjust the topup count of DPAMT page pairs for splitting S-EPT
Quoted 19 lines · Rick P
Quoted 18 lines · Yan Zhao
static int tdx_topup_external_pamt_cache(struct kvm *kvm, struct
kvm_vcpu*vcpu,
boolwith_root_spt, intmin_nr_spts) {
structtdx_pamt_cache*pamt_cache;
intdpamt_pairs=min_nr_spts;
pamt_cache=tdx_get_pamt_cache(kvm, vcpu); if (!pamt_cache)
return-EIO;/* DPAMT page pair for the root SPT has already been installed */ if (
with_root_spt)
dpamt_pairs-= 1;/* +1 pair for 4KB guest private page */
dpamt_pairs+= 1;
Quoted 8 lines
Quoted 2 lines
return
tdx_topup_pamt_cache(pamt_cache, dpamt_pairs); }Yes this looks much clearer than even the upstream code. Do you have any better ideas than "4KB guest private page" though? Because in the split case it is actually for 512 guest private pages. We should put an artful comment to explain both cases while not making it look like something that has a better solution. Because I'm thinking there isn't one.
What about a comment like
/*
* +1 pair to account for mapping a new 4KB guest page or splitting a
* 2MB guest page into 512 4KB pages.
*/?
Quoted 7 lines
Quoted 6 lines
In the fault path,
mmu_topup_memory_caches()calls:
r = kvm_x86_call(topup_external_cache)(vcpu->kvm, vcpu, true,PT64_ROOT_MAX_LEVEL);In the split path,
tdp_mmu_alloc_sp_for_split()calls:
kvm_x86_call(topup_external_cache)(kvm,kvm_get_running_vcpu(), false, 1);
Re: [PATCH v4 08/17] KVM: TDX: Adjust the topup count of DPAMT page pairs for splitting S-EPT
Quoted 11 lines · Yan Zhao
Quoted 10 lines · Rick P
Quoted 9 lines · Yan Zhao
static int tdx_topup_external_pamt_cache(struct kvm *kvm, struct
kvm_vcpu*vcpu,
boolwith_root_spt, intmin_nr_spts) {
structtdx_pamt_cache*pamt_cache;
intdpamt_pairs=min_nr_spts;
pamt_cache=tdx_get_pamt_cache(kvm, vcpu); if (!pamt_cache)
if (KVM_BUG_ON(!pamt_cache, kvm))
If no objection, I'll add KVM_BUG_ON() here to avoid unnecessary repeated retries of topup() after such an error occurs, as it would indicate a KVM bug.
Quoted 32 lines
Quoted 9 lines
Quoted 9 lines
return
-EIO;/* DPAMT page pair for the root SPT has already been installed */ if (
with_root_spt)
dpamt_pairs-= 1;/* +1 pair for 4KB guest private page */
dpamt_pairs+= 1;Quoted 8 lines
Quoted 2 lines
return
tdx_topup_pamt_cache(pamt_cache, dpamt_pairs); }Yes this looks much clearer than even the upstream code. Do you have any better ideas than "4KB guest private page" though? Because in the split case it is actually for 512 guest private pages. We should put an artful comment to explain both cases while not making it look like something that has a better solution. Because I'm thinking there isn't one.
What about a comment like
/* * +1 pair to account for mapping a new 4KB guest page or splitting a * 2MB guest page into 512 4KB pages. */?
Quoted 7 lines
Quoted 6 lines
In the fault path,
mmu_topup_memory_caches()calls:
r = kvm_x86_call(topup_external_cache)(vcpu->kvm, vcpu, true,PT64_ROOT_MAX_LEVEL);In the split path,
tdp_mmu_alloc_sp_for_split()calls:
kvm_x86_call(topup_external_cache)(kvm,kvm_get_running_vcpu(), false, 1);
Edgecombe, Rick P
maintainerRe: [PATCH v4 08/17] KVM: TDX: Adjust the topup count of DPAMT page pairs for splitting S-EPT
In reply to Yan ZhaoQuoted 9 lines · Yan Zhao
Quoted 8 lines
Yes this looks much clearer than even the upstream code. Do you have any better
ideas than "4KB guest private page" though? Because in the split case it is actually for 512 guest private pages. We should put an artful comment to explain
both cases while not making it look like something that has a better solution.
Because I'm thinking there isn't one.
Quoted 6 lines
What about a comment like
/** +1 pair to account for mapping a new 4KB guest page or splitting a
* 2MB guest page into 512 4KB pages.
*/
?
Can we mention why we only need one for 512 pages, what about?
/*
* +1 pair to account for mapping a new 4KB guest page or splitting a
* 2MB guest page. Since the resulting split 2MB page will be 2MB
* aligned it will only end up needing one DPAMT page pair to cover all
* 512 4KB pages.
*/
Re: [PATCH v4 08/17] KVM: TDX: Adjust the topup count of DPAMT page pairs for splitting S-EPT
Quoted 10 lines · Rick P
Quoted 9 lines · Yan Zhao
Quoted 8 lines
Yes this looks much clearer than even the upstream code. Do you have any better
ideas than "4KB guest private page" though? Because in the split case it is actually for 512 guest private pages. We should put an artful comment to explain
both cases while not making it look like something that has a better solution.
Because I'm thinking there isn't one.
Quoted 8 lines
Quoted 6 lines
What about a comment like
/** +1 pair to account for mapping a new 4KB guest page or splitting a
* 2MB guest page into 512 4KB pages.
*/
?Can we mention why we only need one for 512 pages, what about?
But "2MB guest page" should already imply that the physical range is 2MB aligned. Instead of expanding the details in the comment, would it be better to put the explanation in the patch changelog?
Quoted 8 lines
/*
* +1 pair to account for mapping a new 4KB guest page or splitting a
* 2MB guest page. Since the resulting split 2MB page will be 2MB
* aligned it will only end up needing one DPAMT page pair to cover all
* 512 4KB pages.
*/
Edgecombe, Rick P
maintainerRe: [PATCH v4 08/17] KVM: TDX: Adjust the topup count of DPAMT page pairs for splitting S-EPT
Quoted 5 lines · Yan Zhao
Quoted 1 line
Can we mention why we only need one for 512 pages, what about?
But "2MB guest page" should already imply that the physical range is 2MB aligned. Instead of expanding the details in the comment, would it be better to put the explanation in the patch changelog?
I think the detail that is missing is that DPAMT only needs one page pair per 2MB aligned region. That info is encapsulated in arch/x86 side. So I think this code needs a comment to cover that part.
That the 2MB page is 2MB aligned is not necessary. Can you propose something else?
Quoted 7 lines
Quoted 7 lines
/*
* +1 pair to account for mapping a new 4KB guest page or splitting a
* 2MB guest page. Since the resulting split 2MB page will be 2MB
* aligned it will only end up needing one DPAMT page pair to cover all
* 512 4KB pages.
*/
Re: [PATCH v4 08/17] KVM: TDX: Adjust the topup count of DPAMT page pairs for splitting S-EPT
Quoted 10 lines · Rick P
Quoted 5 lines · Yan Zhao
Quoted 1 line
Can we mention why we only need one for 512 pages, what about?
But "2MB guest page" should already imply that the physical range is 2MB aligned. Instead of expanding the details in the comment, would it be better to put the explanation in the patch changelog?
I think the detail that is missing is that DPAMT only needs one page pair per 2MB aligned region. That info is encapsulated in
arch/x86side. So I think this code needs a comment to cover that part.
Makes sense.
Quoted 2 lines
That the 2MB page is 2MB aligned is not necessary. Can you propose something else?
What about
/*
* Only one DPAMT page pair is needed per 2MB aligned region.
* +1 pair to account for mapping a new 4KB guest page or splitting a
* 2MB guest page into 512 4KB pages.
*/
?
Quoted 9 lines
Quoted 7 lines
Quoted 7 lines
/*
* +1 pair to account for mapping a new 4KB guest page or splitting a
* 2MB guest page. Since the resulting split 2MB page will be 2MB
* aligned it will only end up needing one DPAMT page pair to cover all
* 512 4KB pages.
*/
Edgecombe, Rick P
maintainerRe: [PATCH v4 08/17] KVM: TDX: Adjust the topup count of DPAMT page pairs for splitting S-EPT
Quoted 5 lines · Yan Zhao
/*
* Only one DPAMT page pair is needed per 2MB aligned region.
* +1 pair to account for mapping a new 4KB guest page or splitting a
* 2MB guest page into 512 4KB pages.
*/
LGTM
Re: [PATCH v4 08/17] KVM: TDX: Adjust the topup count of DPAMT page pairs for splitting S-EPT
In reply to Edgecombe, Rick PQuoted 22 lines · Rick P
Quoted 12 lines · Yan Zhao
KVM needs to allocate enough DPAMT page pairs in the
pamt_cachefor consumption by both page table pages and guest pages. Since the DPAMT page pair for the S-EPT root page is already allocated during TD initialization, there is no need to allocate the DPAMT page pair for the S-EPT root page. Therefore, previously the DPAMT page pairs required equals "min_nr_spts- 1 + 1".When splitting S-EPT,
min_nr_sptsdoes not include the root SPT. So, limit the -1 calculation to whenmin_nr_sptsequalsroot_level, though this will cause one pair over-allocation in the normal page fault path becausePT64_ROOT_MAX_LEVELis always passed even when launching a 4-level TD.Quoted 8 lines
Additionally, KVM may need to retry
tdh_mem_page_demote()a second time, causing the DPAMT page pair for the guest private pages to be drawn from thepamt_cachetwice in the worst case. Therefore, add an extra +1 to cover this worst-case scenario.The slight over-allocation is acceptable since KVM already pre-allocates more pages than needed (e.g., when mapping huge pages) in case of the worst-case scenario.
Quoted 5 lines
This is assumes way too much about the callers and way too convoluted. The existing code already did but, now this is just too far. We need another solution.
Questions below on what that might be.
Thanks for the review!
Quoted 19 lines
Quoted 19 lines
Signed-off-by Yan Zhao <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-)arch/x86/kvm/vmx/tdx.c +2
-0@@ -1630,16 +1630,34 @@ void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level)static int tdx_topup_external_pamt_cache (struct kvm_vcpu *vcpu,int min_nr_spts) { +int dpamt_pairs; +if (WARN_ON_ONCE (!vcpu))return -EIO ;
Quoted 5 lines
arch/x86/kvm/vmx/tdx.c void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level) · line 1640
Quoted 3 lines
arch/x86/kvm/vmx/tdx.c +3
-0+/* Exclude the root SPT, as its DPAMT page pair is already installed */ +if (min_nr_spts == vcpu->kvm->arch.mirror_root_level) + min_nr_spts -=1 ;Over allocating a bit is not the end of the world...
Before this patch, dpamt_pairs = min_nr_spts - 1 + 1.
However, when min_nr_spts is 1, the correct dpamt_pairs should be 2 instead of 1 (in the case when DEMOTE does not fail).
i.e., without this change, we would allocate 1 less page, which is a bug.
Quoted 7 lines
Quoted 7 lines
arch/x86/kvm/vmx/tdx.c +7
-0+ +/* +* Each S-EPT page table page + 4KB guest private page needs a pair of +* DPAMT pages. +*/ + dpamt_pairs = min_nr_spts +1 ; +
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level) · line 1650
Quoted 5 lines
arch/x86/kvm/vmx/tdx.c +1
-2/* -* Minus one page to exclude the root SPT, but plus one page for a -* possible 4KB private mapping. +* After each topup, KVM may invoke DEMOTE at most twice. Why twice? You mean the BUSY retry attempt, right? If you do, couldn't we fix this problem within
tdh_mem_page_demote()?
In tdh_mem_page_demote(), dpamt_pages are allocated from pamt_cache before invoking the SEAMCALL, and freed after the SEAMCALL fails.
So, if KVM retries on the BUSY error for at most twice, an extra pair of dpamt_pages are allocated from the pamt_cache.
If we want to fix this problem within tdh_mem_page_demote(), one possible solution is to re-insert the dpamt_pages back to the pamt_cache list.
e.g., add the following diff to patch 2.
arch/x86/virt/vmx/tdx/tdx.c +20 -1
@@ -87,6 +87,7 @@ static DEFINE_RAW_SPINLOCK(sysinit_lock);static int alloc_pamt_array (struct page **pamt_pages,struct tdx_pamt_cache *cache);static void free_pamt_array (struct page **pamt_pages); +static void reinsert_pamt_array (struct tdx_pamt_cache *cache,struct page **pamt_pages);/* * Do the module global initialization once and return its result.
@@ -1904,7 +1905,7 @@ u64 tdh_mem_page_demote(struct tdx_td *td, u64 gpa, enum pg_level level, kvm_pfn out_free:spin_unlock (&dpamt_lock); -free_pamt_array (dpamt_pages); +reinsert_pamt_array (pamt_cache, dpamt_pages);return ret; }EXPORT_SYMBOL_FOR_KVM (tdh_mem_page_demote);
@@ -2146,6 +2147,24 @@ static void free_pamt_array(struct page **pamt_pages) } } +static void reinsert_pamt_array (struct tdx_pamt_cache *cache,struct page **pamt_pages) +{ +int i; + +if (!cache) +return ; + +for (i =0 ; i <TDX_DPAMT_ENTRY_PAGE_CNT ; i++) { +/* +* Reset pages unconditionally to cover cases +* where they were passed to the TDX module. +*/ +tdx_quirk_reset_paddr (page_to_phys (pamt_pages[i]),PAGE_SIZE ); + +list_add (&pamt_pages[i]->lru, &cache->page_list); + cache->cnt++; + } +}/* Helper for building DPAMT seamcall() arguments. */ static u64 pamt_2mb_arg (kvm_pfn_t pfn) {
Reinsertion should be locklessly safe since the pamt_cache is either per-vCPU or protected by the caller when it's per-VM.
Quoted 15 lines
arch/x86/kvm/vmx/tdx.c void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level) · line 1660
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +6
-0The first + * DEMOTE invocation draws two pairs of pages from the cache: one for + * the S-EPT page table page and one for the guest private memory. + * Since these pages are not returned to the cache, the second DEMOTE + * invocation still needs to consume one additional pair for the guest + * private memory (the pair for the S-EPT page table page is reused for + * the 2nd invocation).Another idea, change the op to be:
inttopup_external_cache(struct kvm_vcpu *vcpu, bool root, bool private_page, int min_nr_spts);
Quoted 7 lines
Normal topup can set:
root=true
private_page=truemin_nr_spts=PT64_ROOT_MAX_LEVEL- 1Then we can calculate exactly what we need. And even better, the existing code won't nee a comment to explain the weirdness.
The existing code looks like this:
static int tdx_topup_external_pamt_cache(struct kvm_vcpu *vcpu, int min_nr_spts)
{
/*
* Minus one page to exclude the root SPT, but plus one page for a
* possible 4KB private mapping.
*/
min_nr_spts += -1 + 1;
return tdx_topup_pamt_cache(&to_tdx(vcpu)->pamt_cache, min_nr_spts);
}If we agree on the reinserting solution for DEMOTE, then tdx_topup_external_pamt_cache() could look like this:
static int tdx_topup_external_pamt_cache(struct kvm *kvm, struct kvm_vcpu *vcpu,
int min_nr_spts)
{
struct tdx_pamt_cache *pamt_cache;
int dpamt_pairs;
pamt_cache = tdx_get_pamt_cache(kvm, vcpu);
if (!pamt_cache)
return -EIO;
/* Exclude the root SPT, as its DPAMT page pair is already installed */
if (min_nr_spts == kvm->arch.mirror_root_level)
min_nr_spts -= 1;
/*
* Each S-EPT page table page + 4KB guest private page needs a pair of
* DPAMT pages.
*/
dpamt_pairs = min_nr_spts + 1;
return tdx_topup_pamt_cache(pamt_cache, dpamt_pairs);
}IMHO, it's clearer than having the caller indicate whether it's root or not.
Quoted 12 lines
Quoted 11 lines
arch/x86/kvm/vmx/tdx.c +3
-2Increase the topup count to account for this +* worst-case scenario. */ - min_nr_spts +=-1 +1 ; + dpamt_pairs +=1 ; -return tdx_topup_pamt_cache (&to_tdx (vcpu)->pamt_cache, min_nr_spts); +return tdx_topup_pamt_cache (&to_tdx (vcpu)->pamt_cache, dpamt_pairs); } static int tdx_mem_page_add(struct kvm *kvm, gfn_t gfn, enum pg_level level,
Edgecombe, Rick P
maintainerRe: [PATCH v4 08/17] KVM: TDX: Adjust the topup count of DPAMT page pairs for splitting S-EPT
Quoted 55 lines · Yan Zhao
Quoted 22 lines · Rick P
Quoted 12 lines · Yan Zhao
KVM needs to allocate enough DPAMT page pairs in the
pamt_cachefor consumption by both page table pages and guest pages. Since the DPAMT page pair for the S-EPT root page is already allocated during TD initialization, there is no need to allocate the DPAMT page pair for the S-EPT root page. Therefore, previously the DPAMT page pairs required equals "min_nr_spts- 1 + 1".When splitting S-EPT,
min_nr_sptsdoes not include the root SPT. So, limit the -1 calculation to whenmin_nr_sptsequalsroot_level, though this will cause one pair over-allocation in the normal page fault path becausePT64_ROOT_MAX_LEVELis always passed even when launching a 4-level TD.Quoted 8 lines
Additionally, KVM may need to retry
tdh_mem_page_demote()a second time, causing the DPAMT page pair for the guest private pages to be drawn from thepamt_cachetwice in the worst case. Therefore, add an extra +1 to cover this worst-case scenario.The slight over-allocation is acceptable since KVM already pre-allocates more pages than needed (e.g., when mapping huge pages) in case of the worst-case scenario.
Quoted 5 lines
This is assumes way too much about the callers and way too convoluted. The existing code already did but, now this is just too far. We need another solution.
Questions below on what that might be.
Thanks for the review!
Quoted 19 lines
Quoted 19 lines
Signed-off-by Yan Zhao <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-)arch/x86/kvm/vmx/tdx.c +2
-0@@ -1630,16 +1630,34 @@ void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level)static int tdx_topup_external_pamt_cache (struct kvm_vcpu *vcpu,int min_nr_spts) { +int dpamt_pairs; +if (WARN_ON_ONCE (!vcpu))return -EIO ;Quoted 5 lines
arch/x86/kvm/vmx/tdx.c void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level) · line 1640
Quoted 3 lines
arch/x86/kvm/vmx/tdx.c +3
-0+/* Exclude the root SPT, as its DPAMT page pair is already installed */ +if (min_nr_spts == vcpu->kvm->arch.mirror_root_level) + min_nr_spts -=1 ;Over allocating a bit is not the end of the world...
Quoted 4 lines
Before this patch,
dpamt_pairs=min_nr_spts- 1 + 1.
However, whenmin_nr_sptsis 1, the correctdpamt_pairsshould be 2 instead of 1 (in the case when DEMOTE does not fail).
i.e., without this change, we would allocate 1 less page, which is a bug.
I wasn't suggesting to not change the function. I was suggesting to consider a solution that assumes less about the callers, at the cost of over allocating.
Quoted 16 lines
Quoted 16 lines
arch/x86/kvm/vmx/tdx.c void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level) · line 1650
Quoted 13 lines
arch/x86/kvm/vmx/tdx.c +8
-2+ +/* +* Each S-EPT page table page + 4KB guest private page needs a pair of +* DPAMT pages. +*/ + dpamt_pairs = min_nr_spts +1 ; +/* -* Minus one page to exclude the root SPT, but plus one page for a -* possible 4KB private mapping. +* After each topup, KVM may invoke DEMOTE at most twice. Why twice? You mean the BUSY retry attempt, right? If you do, couldn't we fix this problem within
tdh_mem_page_demote()?
Quoted 7 lines
In
tdh_mem_page_demote(),dpamt_pagesare allocated frompamt_cachebefore invoking the SEAMCALL, and freed after the SEAMCALL fails.
So, if KVM retries on the BUSY error for at most twice, an extra pair ofdpamt_pagesare allocated from thepamt_cache.If we want to fix this problem within
tdh_mem_page_demote(), one possible solution is to re-insert thedpamt_pagesback to thepamt_cachelist.
Yea that is what I was suggesting.
Quoted 92 lines
e.g., add the following diff to patch 2.
arch/x86/virt/vmx/tdx/tdx.c +20
-1@@ -87,6 +87,7 @@ static DEFINE_RAW_SPINLOCK(sysinit_lock);static int alloc_pamt_array (struct page **pamt_pages,struct tdx_pamt_cache *cache);static void free_pamt_array (struct page **pamt_pages); +static void reinsert_pamt_array (struct tdx_pamt_cache *cache,struct page **pamt_pages);/* * Do the module global initialization once and return its result. @@ -1904,7 +1905,7 @@ u64 tdh_mem_page_demote(struct tdx_td *td, u64 gpa, enum pg_level level, kvm_pfn out_free:spin_unlock (&dpamt_lock); -free_pamt_array (dpamt_pages); +reinsert_pamt_array (pamt_cache, dpamt_pages);return ret; }EXPORT_SYMBOL_FOR_KVM (tdh_mem_page_demote);@@ -2146,6 +2147,24 @@ static void free_pamt_array(struct page **pamt_pages) } } +static void reinsert_pamt_array (struct tdx_pamt_cache *cache,struct page **pamt_pages) +{ +int i; + +if (!cache) +return ; + +for (i =0 ; i <TDX_DPAMT_ENTRY_PAGE_CNT ; i++) { +/* +* Reset pages unconditionally to cover cases +* where they were passed to the TDX module. +*/ +tdx_quirk_reset_paddr (page_to_phys (pamt_pages[i]),PAGE_SIZE ); + +list_add (&pamt_pages[i]->lru, &cache->page_list); + cache->cnt++; + } +}/* Helper for building DPAMT seamcall() arguments. */ static u64 pamt_2mb_arg (kvm_pfn_t pfn) {Reinsertion should be locklessly safe since the
pamt_cacheis either per-vCPU or protected by the caller when it's per-VM.Quoted 15 lines
arch/x86/kvm/vmx/tdx.c void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level) · line 1660
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +6
-0The first + * DEMOTE invocation draws two pairs of pages from the cache: one for + * the S-EPT page table page and one for the guest private memory. + * Since these pages are not returned to the cache, the second DEMOTE + * invocation still needs to consume one additional pair for the guest + * private memory (the pair for the S-EPT page table page is reused for + * the 2nd invocation).Another idea, change the op to be:
inttopup_external_cache(struct kvm_vcpu *vcpu, bool root, bool private_page, int min_nr_spts);Quoted 7 lines
Normal topup can set:
root=true
private_page=truemin_nr_spts=PT64_ROOT_MAX_LEVEL- 1Then we can calculate exactly what we need. And even better, the existing code won't nee a comment to explain the weirdness.
The existing code looks like this:
static inttdx_topup_external_pamt_cache(struct kvm_vcpu *vcpu, int min_nr_spts){ /* * Minus one page to exclude the root SPT, but plus one page for a * possible 4KB private mapping. */ min_nr_spts += -1 + 1; return tdx_topup_pamt_cache(&to_tdx(vcpu)->pamt_cache, min_nr_spts); }If we agree on the reinserting solution for DEMOTE, then
tdx_topup_external_pamt_cache()could look like this:static int tdx_topup_external_pamt_cache(struct kvm *kvm, struct
kvm_vcpu*vcpu,
intmin_nr_spts)
Quoted 11 lines
{
struct tdx_pamt_cache *pamt_cache;
int dpamt_pairs;
pamt_cache = tdx_get_pamt_cache(kvm, vcpu);
if (!pamt_cache)
return -EIO;
/* Exclude the root SPT, as its DPAMT page pair is already installed */
if (min_nr_spts == kvm->arch.mirror_root_level)
min_nr_spts -= 1;This is assuming too much about the caller.
Quoted 24 lines
/* * Each S-EPT page table page + 4KB guest private page needs a pair of * DPAMT pages. */ dpamt_pairs = min_nr_spts + 1; return tdx_topup_pamt_cache(pamt_cache, dpamt_pairs); }IMHO, it's clearer than having the caller indicate whether it's root or not.
Quoted 12 lines
Quoted 11 lines
arch/x86/kvm/vmx/tdx.c +3
-2Increase the topup count to account for this +* worst-case scenario. */ - min_nr_spts +=-1 +1 ; + dpamt_pairs +=1 ; -return tdx_topup_pamt_cache (&to_tdx (vcpu)->pamt_cache, min_nr_spts); +return tdx_topup_pamt_cache (&to_tdx (vcpu)->pamt_cache, dpamt_pairs); } static int tdx_mem_page_add(struct kvm *kvm, gfn_t gfn, enum pg_level level,
Re: [PATCH v4 08/17] KVM: TDX: Adjust the topup count of DPAMT page pairs for splitting S-EPT
Quoted 56 lines · Rick P
Quoted 55 lines · Yan Zhao
Quoted 22 lines · Rick P
Quoted 12 lines · Yan Zhao
KVM needs to allocate enough DPAMT page pairs in the
pamt_cachefor consumption by both page table pages and guest pages. Since the DPAMT page pair for the S-EPT root page is already allocated during TD initialization, there is no need to allocate the DPAMT page pair for the S-EPT root page. Therefore, previously the DPAMT page pairs required equals "min_nr_spts- 1 + 1".When splitting S-EPT,
min_nr_sptsdoes not include the root SPT. So, limit the -1 calculation to whenmin_nr_sptsequalsroot_level, though this will cause one pair over-allocation in the normal page fault path becausePT64_ROOT_MAX_LEVELis always passed even when launching a 4-level TD.Quoted 8 lines
Additionally, KVM may need to retry
tdh_mem_page_demote()a second time, causing the DPAMT page pair for the guest private pages to be drawn from thepamt_cachetwice in the worst case. Therefore, add an extra +1 to cover this worst-case scenario.The slight over-allocation is acceptable since KVM already pre-allocates more pages than needed (e.g., when mapping huge pages) in case of the worst-case scenario.
Quoted 5 lines
This is assumes way too much about the callers and way too convoluted. The existing code already did but, now this is just too far. We need another solution.
Questions below on what that might be.
Thanks for the review!
Quoted 24 lines
Quoted 12 lines
Signed-off-by Yan Zhao <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-)arch/x86/kvm/vmx/tdx.c +0
-0@@ -1630,16 +1630,34 @@ void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level)arch/x86/kvm/vmx/tdx.c void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level) · line 1640
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +5
-0static int tdx_topup_external_pamt_cache (struct kvm_vcpu *vcpu,int min_nr_spts) { +int dpamt_pairs; +if (WARN_ON_ONCE (!vcpu))return -EIO ; +/* Exclude the root SPT, as its DPAMT page pair is already installed */ +if (min_nr_spts == vcpu->kvm->arch.mirror_root_level) + min_nr_spts -=1 ;Over allocating a bit is not the end of the world...
Quoted 7 lines
Quoted 4 lines
Before this patch,
dpamt_pairs=min_nr_spts- 1 + 1.
However, whenmin_nr_sptsis 1, the correctdpamt_pairsshould be 2 instead of 1 (in the case when DEMOTE does not fail).
i.e., without this change, we would allocate 1 less page, which is a bug.I wasn't suggesting to not change the function. I was suggesting to consider a solution that assumes less about the callers, at the cost of over allocating.
Ok :)
Quoted 20 lines
Quoted 20 lines
Quoted 7 lines
Quoted 7 lines
arch/x86/kvm/vmx/tdx.c +7
-0+ +/* +* Each S-EPT page table page + 4KB guest private page needs a pair of +* DPAMT pages. +*/ + dpamt_pairs = min_nr_spts +1 ; +Quoted 8 lines
arch/x86/kvm/vmx/tdx.c void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level) · line 1650
Quoted 5 lines
arch/x86/kvm/vmx/tdx.c +1
-2/* -* Minus one page to exclude the root SPT, but plus one page for a -* possible 4KB private mapping. +* After each topup, KVM may invoke DEMOTE at most twice. Why twice? You mean the BUSY retry attempt, right? If you do, couldn't we fix this problem within
tdh_mem_page_demote()?In
tdh_mem_page_demote(),dpamt_pagesare allocated frompamt_cachebefore invoking the SEAMCALL, and freed after the SEAMCALL fails.
So, if KVM retries on the BUSY error for at most twice, an extra pair ofdpamt_pagesare allocated from thepamt_cache.
Quoted 4 lines
Quoted 2 lines
If we want to fix this problem within
tdh_mem_page_demote(), one possible solution is to re-insert thedpamt_pagesback to thepamt_cachelist.Yea that is what I was suggesting.
Glad you also like it!
Quoted 99 lines
Quoted 99 lines
e.g., add the following diff to patch 2.
arch/x86/virt/vmx/tdx/tdx.c +20
-1@@ -87,6 +87,7 @@ static DEFINE_RAW_SPINLOCK(sysinit_lock);static int alloc_pamt_array (struct page **pamt_pages,struct tdx_pamt_cache *cache);static void free_pamt_array (struct page **pamt_pages); +static void reinsert_pamt_array (struct tdx_pamt_cache *cache,struct page **pamt_pages);/* * Do the module global initialization once and return its result. @@ -1904,7 +1905,7 @@ u64 tdh_mem_page_demote(struct tdx_td *td, u64 gpa, enum pg_level level, kvm_pfn out_free:spin_unlock (&dpamt_lock); -free_pamt_array (dpamt_pages); +reinsert_pamt_array (pamt_cache, dpamt_pages);return ret; }EXPORT_SYMBOL_FOR_KVM (tdh_mem_page_demote);@@ -2146,6 +2147,24 @@ static void free_pamt_array(struct page **pamt_pages) } } +static void reinsert_pamt_array (struct tdx_pamt_cache *cache,struct page **pamt_pages) +{ +int i; + +if (!cache) +return ; + +for (i =0 ; i <TDX_DPAMT_ENTRY_PAGE_CNT ; i++) { +/* +* Reset pages unconditionally to cover cases +* where they were passed to the TDX module. +*/ +tdx_quirk_reset_paddr (page_to_phys (pamt_pages[i]),PAGE_SIZE ); + +list_add (&pamt_pages[i]->lru, &cache->page_list); + cache->cnt++; + } +}/* Helper for building DPAMT seamcall() arguments. */ static u64 pamt_2mb_arg (kvm_pfn_t pfn) {Reinsertion should be locklessly safe since the
pamt_cacheis either per-vCPU or protected by the caller when it's per-VM.Quoted 15 lines
arch/x86/kvm/vmx/tdx.c void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level) · line 1660
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +6
-0The first + * DEMOTE invocation draws two pairs of pages from the cache: one for + * the S-EPT page table page and one for the guest private memory. + * Since these pages are not returned to the cache, the second DEMOTE + * invocation still needs to consume one additional pair for the guest + * private memory (the pair for the S-EPT page table page is reused for + * the 2nd invocation).Another idea, change the op to be:
inttopup_external_cache(struct kvm_vcpu *vcpu, bool root, bool private_page, int min_nr_spts);Quoted 7 lines
Normal topup can set:
root=true
private_page=truemin_nr_spts=PT64_ROOT_MAX_LEVEL- 1Then we can calculate exactly what we need. And even better, the existing code won't nee a comment to explain the weirdness.
The existing code looks like this:
static inttdx_topup_external_pamt_cache(struct kvm_vcpu *vcpu, int min_nr_spts){ /* * Minus one page to exclude the root SPT, but plus one page for a * possible 4KB private mapping. */ min_nr_spts += -1 + 1; return tdx_topup_pamt_cache(&to_tdx(vcpu)->pamt_cache, min_nr_spts); }If we agree on the reinserting solution for DEMOTE, then
tdx_topup_external_pamt_cache()could look like this:static int tdx_topup_external_pamt_cache(struct kvm *kvm, struct
kvm_vcpu*vcpu,
intmin_nr_spts){ struct tdx_pamt_cache *pamt_cache; int dpamt_pairs; pamt_cache = tdx_get_pamt_cache(kvm, vcpu); if (!pamt_cache) return -EIO;
Quoted 5 lines
Quoted 3 lines
/* Exclude the root SPT, as its DPAMT page pair is already installed */ if (min_nr_spts == kvm->arch.mirror_root_level) min_nr_spts -= 1;This is assuming too much about the caller.
Please check if the proposal in [*] (the one based on your suggestion) looks good to you.
[*] https://lore.kernel.org/kvm/[email protected]
Quoted 25 lines
Quoted 24 lines
/* * Each S-EPT page table page + 4KB guest private page needs a pair of * DPAMT pages. */ dpamt_pairs = min_nr_spts + 1; return tdx_topup_pamt_cache(pamt_cache, dpamt_pairs); }IMHO, it's clearer than having the caller indicate whether it's root or not.
Quoted 12 lines
Quoted 11 lines
arch/x86/kvm/vmx/tdx.c +3
-2Increase the topup count to account for this +* worst-case scenario. */ - min_nr_spts +=-1 +1 ; + dpamt_pairs +=1 ; -return tdx_topup_pamt_cache (&to_tdx (vcpu)->pamt_cache, min_nr_spts); +return tdx_topup_pamt_cache (&to_tdx (vcpu)->pamt_cache, dpamt_pairs); } static int tdx_mem_page_add(struct kvm *kvm, gfn_t gfn, enum pg_level level,
PATCH v4 9/17 KVM: x86/mmu: Introduce hugepage_set_guest_inhibit()
In reply to Yan ZhaoTDX requires guests to accept S-EPT mappings created by the host KVM. Due to the current implementation of the TDX module, if a guest accepts a GFN at a lower level after KVM maps it at a higher level, the TDX module will emulate an EPT violation VMExit to KVM instead of returning a size mismatch error to the guest. If KVM fails to perform page splitting in the VMExit handler, the guest's accept operation will be triggered again upon re-entering the guest, causing a repeated EPT violation VMExit.
To facilitate passing the guest's accept level information to the KVM MMU core and to prevent the repeated mapping of a GFN at different levels due to different accept levels specified by different vCPUs, introduce the interface hugepage_set_guest_inhibit(). This interface specifies across vCPUs that mapping at a certain level is inhibited from the guest.
Intentionally don't provide an API to clear KVM_LPAGE_GUEST_INHIBIT_FLAG for the time being, as detecting that it's ok to (re)install a huge page is tricky (and costly if KVM wants to be 100% accurate), and KVM doesn't currently support huge page promotion (only direct installation of huge pages) for S-EPT.
As a result, the only scenario where clearing the flag would likely allow KVM to install a huge page is when an entire 2MB / 1GB range is converted to shared or private. But if the guest is accepting at 4KB granularity, odds are good the guest is using the memory for something "special" and will never convert the entire range to shared (and/or back to private). Punt that optimization to the future, if it's ever needed.
Unlike hugepage_set/test_mixed(), which are placed under CONFIG_KVM_VM_MEMORY_ATTRIBUTES since they are unneeded when per-VM memory attributes is disabled, hugepage_set/test_guest_inhibit() are invoked when per-gmem memory attributes is enabled, since TDX huge page support is intentionally enabled only when per-gmem memory attributes is enabled.
---
v4:
- Explain *why* the flag is never cleared. (Sean)
- Explain why hugepage_set/test_guest_inhibit() are not placed near to hugepage_set/test_mixed().
v3:
- Use EXPORT_SYMBOL_FOR_KVM_INTERNAL().
RFC v2:
- new in RFC v2
--- arch/x86/kvm/mmu.h | 4 ++++ arch/x86/kvm/mmu/mmu.c | 23 +++++++++++++++++++---- 2 files changed, 23 insertions(+), 4 deletions(-)
arch/x86/kvm/mmu.h +4 -0
@@ -410,4 +410,8 @@ static inline bool kvm_is_gfn_alias(struct kvm *kvm, gfn_t gfn) {return gfn &kvm_gfn_direct_bits (kvm); } + +void hugepage_set_guest_inhibit (struct kvm_memory_slot *slot,gfn_t gfn,int level); +bool hugepage_test_guest_inhibit (struct kvm_memory_slot *slot,gfn_t gfn,int level); +#endif
arch/x86/kvm/mmu/mmu.c +19 -4
@@ -750,12 +750,14 @@ static bool kvm_gfn_is_lpage_allowed(struct kvm *kvm, }/* -* The most significant bit in disallow_lpage tracks whether or not memory -* attributes are mixed, i.e. not identical for all gfns at the current level. +* The 2 most significant bits in disallow_lpage track whether or not memory +* attributes are mixed, i.e. not identical for all gfns at the current level, +* or whether or not guest inhibits the current level of hugepage at the gfn. * The lower order bits are used to refcount other cases where a hugepage is * disallowed, e.g. if KVM has shadow a page table at the gfn. */ -#define KVM_LPAGE_MIXED_FLAG BIT(31) +#define KVM_LPAGE_MIXED_FLAG BIT(31) +#define KVM_LPAGE_GUEST_INHIBIT_FLAG BIT(30)static void update_gfn_disallow_lpage_count (const struct kvm_memory_slot *slot,gfn_t gfn,int count)
@@ -768,7 +770,8 @@ static void update_gfn_disallow_lpage_count(const struct kvm_memory_slot *slot, old = linfo->disallow_lpage; linfo->disallow_lpage += count; -WARN_ON_ONCE ((old ^ linfo->disallow_lpage) &KVM_LPAGE_MIXED_FLAG ); +WARN_ON_ONCE ((old ^ linfo->disallow_lpage) & + (KVM_LPAGE_MIXED_FLAG |KVM_LPAGE_GUEST_INHIBIT_FLAG )); } }
@@ -782,6 +785,18 @@ void kvm_mmu_gfn_allow_lpage(const struct kvm_memory_slot *slot, gfn_t gfn)update_gfn_disallow_lpage_count (slot, gfn,-1 ); } +bool hugepage_test_guest_inhibit (struct kvm_memory_slot *slot,gfn_t gfn,int level) +{ +return lpage_info_slot (gfn, slot, level)->disallow_lpage &KVM_LPAGE_GUEST_INHIBIT_FLAG ; +} +EXPORT_SYMBOL_FOR_KVM_INTERNAL (hugepage_test_guest_inhibit); + +void hugepage_set_guest_inhibit (struct kvm_memory_slot *slot,gfn_t gfn,int level) +{ +lpage_info_slot (gfn, slot, level)->disallow_lpage |=KVM_LPAGE_GUEST_INHIBIT_FLAG ; +} +EXPORT_SYMBOL_FOR_KVM_INTERNAL (hugepage_set_guest_inhibit); +static void account_shadowed (struct kvm *kvm,struct kvm_mmu_page *sp) {struct kvm_memslots *slots;
Signature · 1 line
2.43.2
Re: [PATCH v4 09/17] KVM: x86/mmu: Introduce hugepage_set_guest_inhibit()
Quoted 27 lines · Yan Zhao
TDX requires guests to accept S-EPT mappings created by the host KVM. Due to the current implementation of the TDX module, if a guest accepts a GFN at a lower level after KVM maps it at a higher level, the TDX module will emulate an EPT violation VMExit to KVM instead of returning a size mismatch error to the guest. If KVM fails to perform page splitting in the VMExit handler, the guest's accept operation will be triggered again upon re-entering the guest, causing a repeated EPT violation VMExit.
To facilitate passing the guest's accept level information to the KVM MMU core and to prevent the repeated mapping of a GFN at different levels due to different accept levels specified by different vCPUs, introduce the interface
hugepage_set_guest_inhibit(). This interface specifies across vCPUs that mapping at a certain level is inhibited from the guest.Intentionally don't provide an API to clear
KVM_LPAGE_GUEST_INHIBIT_FLAGfor the time being, as detecting that it's ok to (re)install a huge page is tricky (and costly if KVM wants to be 100% accurate), and KVM doesn't currently support huge page promotion (only direct installation of huge pages) for S-EPT.As a result, the only scenario where clearing the flag would likely allow KVM to install a huge page is when an entire 2MB / 1GB range is converted to shared or private. But if the guest is accepting at 4KB granularity, odds are good the guest is using the memory for something "special" and will never convert the entire range to shared (and/or back to private). Punt that optimization to the future, if it's ever needed.
Quoted 5 lines
Unlike hugepage_set/test_mixed(), which are placed under
CONFIG_KVM_VM_MEMORY_ATTRIBUTESsince they are unneeded when per-VM memory attributes is disabled, hugepage_set/test_guest_inhibit() are invoked when per-gmem memory attributes is enabled, since TDX huge page support is intentionally enabled only when per-gmem memory attributes is enabled.
Not sure why we need the above paragraph. Especially the gmem part. It looks like it was added since v5. Was it an AI review item?
With that addressed:
Quoted 87 lines
Suggested-by Rick Edgecombe <[email protected]>Suggested-by Sean Christopherson <[email protected]>[sean: explain *why* the flag is never cleared]Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Yan Zhao <[email protected]>---
v4:
- Explain *why* the flag is never cleared. (Sean)
- Explain why hugepage_set/test_guest_inhibit() are not placed near to hugepage_set/test_mixed().v3:
- UseEXPORT_SYMBOL_FOR_KVM_INTERNAL().RFC v2:
- new in RFC v2--- arch/x86/kvm/mmu.h | 4 ++++ arch/x86/kvm/mmu/mmu.c | 23 +++++++++++++++++++---- 2 files changed, 23 insertions(+), 4 deletions(-)arch/x86/kvm/mmu.h +4
-0@@ -410,4 +410,8 @@ static inline bool kvm_is_gfn_alias(struct kvm *kvm, gfn_t gfn) {return gfn &kvm_gfn_direct_bits (kvm); } + +void hugepage_set_guest_inhibit (struct kvm_memory_slot *slot,gfn_t gfn,int level); +bool hugepage_test_guest_inhibit (struct kvm_memory_slot *slot,gfn_t gfn,int level); +#endif arch/x86/kvm/mmu/mmu.c +19
-4@@ -750,12 +750,14 @@ static bool kvm_gfn_is_lpage_allowed(struct kvm *kvm, }/* -* The most significant bit in disallow_lpage tracks whether or not memory -* attributes are mixed, i.e. not identical for all gfns at the current level. +* The 2 most significant bits in disallow_lpage track whether or not memory +* attributes are mixed, i.e. not identical for all gfns at the current level, +* or whether or not guest inhibits the current level of hugepage at the gfn. * The lower order bits are used to refcount other cases where a hugepage is * disallowed, e.g. if KVM has shadow a page table at the gfn. */ -#define KVM_LPAGE_MIXED_FLAG BIT(31) +#define KVM_LPAGE_MIXED_FLAG BIT(31) +#define KVM_LPAGE_GUEST_INHIBIT_FLAG BIT(30)static void update_gfn_disallow_lpage_count (const struct kvm_memory_slot *slot,gfn_t gfn,int count)@@ -768,7 +770,8 @@ static void update_gfn_disallow_lpage_count(const struct kvm_memory_slot *slot, old = linfo->disallow_lpage; linfo->disallow_lpage += count; -WARN_ON_ONCE ((old ^ linfo->disallow_lpage) &KVM_LPAGE_MIXED_FLAG ); +WARN_ON_ONCE ((old ^ linfo->disallow_lpage) & + (KVM_LPAGE_MIXED_FLAG |KVM_LPAGE_GUEST_INHIBIT_FLAG )); } }@@ -782,6 +785,18 @@ void kvm_mmu_gfn_allow_lpage(const struct kvm_memory_slot *slot, gfn_t gfn)update_gfn_disallow_lpage_count (slot, gfn,-1 ); } +bool hugepage_test_guest_inhibit (struct kvm_memory_slot *slot,gfn_t gfn,int level) +{ +return lpage_info_slot (gfn, slot, level)->disallow_lpage &KVM_LPAGE_GUEST_INHIBIT_FLAG ; +} +EXPORT_SYMBOL_FOR_KVM_INTERNAL (hugepage_test_guest_inhibit); + +void hugepage_set_guest_inhibit (struct kvm_memory_slot *slot,gfn_t gfn,int level) +{ +lpage_info_slot (gfn, slot, level)->disallow_lpage |=KVM_LPAGE_GUEST_INHIBIT_FLAG ; +} +EXPORT_SYMBOL_FOR_KVM_INTERNAL (hugepage_set_guest_inhibit); +static void account_shadowed (struct kvm *kvm,struct kvm_mmu_page *sp) {struct kvm_memslots *slots;
Re: [PATCH v4 09/17] KVM: x86/mmu: Introduce hugepage_set_guest_inhibit()
Quoted 28 lines · Rick P
Quoted 27 lines · Yan Zhao
TDX requires guests to accept S-EPT mappings created by the host KVM. Due to the current implementation of the TDX module, if a guest accepts a GFN at a lower level after KVM maps it at a higher level, the TDX module will emulate an EPT violation VMExit to KVM instead of returning a size mismatch error to the guest. If KVM fails to perform page splitting in the VMExit handler, the guest's accept operation will be triggered again upon re-entering the guest, causing a repeated EPT violation VMExit.
To facilitate passing the guest's accept level information to the KVM MMU core and to prevent the repeated mapping of a GFN at different levels due to different accept levels specified by different vCPUs, introduce the interface
hugepage_set_guest_inhibit(). This interface specifies across vCPUs that mapping at a certain level is inhibited from the guest.Intentionally don't provide an API to clear
KVM_LPAGE_GUEST_INHIBIT_FLAGfor the time being, as detecting that it's ok to (re)install a huge page is tricky (and costly if KVM wants to be 100% accurate), and KVM doesn't currently support huge page promotion (only direct installation of huge pages) for S-EPT.As a result, the only scenario where clearing the flag would likely allow KVM to install a huge page is when an entire 2MB / 1GB range is converted to shared or private. But if the guest is accepting at 4KB granularity, odds are good the guest is using the memory for something "special" and will never convert the entire range to shared (and/or back to private). Punt that optimization to the future, if it's ever needed.
Quoted 8 lines
Quoted 5 lines
Unlike hugepage_set/test_mixed(), which are placed under
CONFIG_KVM_VM_MEMORY_ATTRIBUTESsince they are unneeded when per-VM memory attributes is disabled, hugepage_set/test_guest_inhibit() are invoked when per-gmem memory attributes is enabled, since TDX huge page support is intentionally enabled only when per-gmem memory attributes is enabled.Not sure why we need the above paragraph. Especially the gmem part. It looks like it was added since v5. Was it an AI review item?
The internal Sashiko review suggested explaining why hugepage_set/test_guest_inhibit() are not placed next to hugepage_set/test_mixed(), given their similar naming, which seems reasonable to me.
Quoted 1 line
With that addressed:
So what about:
"
Place hugepage_set/test_guest_inhibit() near kvm_mmu_gfn_(dis)allow_lpage(), rather than next to hugepage_set/test_mixed(), so that they can be invoked outside of CONFIG_KVM_VM_MEMORY_ATTRIBUTES.
"
Quoted 1 line
Reviewed-by Rick Edgecombe <[email protected]>
Thanks!
PATCH v4 10/17 KVM: x86/mmu: Add a TDP MMU API to split huge pages for mirror roots
In reply to Yan ZhaoFrom: Sean Christopherson <[email protected]>
Add an exported API to split huge pages in mirror roots for a given gfn range. TDX will use the API to split huge pages in preparation for partially zapping a private huge page, e.g., for converting a huge page from private to shared, or for splitting a huge page to match the guest's ACCEPT level.
For all intents and purposes, no functional change intended.
---
v4:
- new patch.
--- arch/x86/kvm/mmu/tdp_mmu.c | 40 ++++++++++++++++++++++++++++---------- arch/x86/kvm/mmu/tdp_mmu.h | 2 ++ 2 files changed, 32 insertions(+), 10 deletions(-)
arch/x86/kvm/mmu/tdp_mmu.c +30 -10
@@ -1616,6 +1616,26 @@ static int tdp_mmu_split_huge_pages_root(struct kvm *kvm,return 0 ; } +static int tdp_mmu_split_huge_pages (struct kvm *kvm,int as_id, +enum kvm_tdp_mmu_root_types type, +gfn_t start,gfn_t end, +int target_level,bool shared) +{ +struct kvm_mmu_page *root; +int r; + +kvm_lockdep_assert_mmu_lock_held (kvm, shared); + +__for_each_tdp_mmu_root_yield_safe (kvm, root, as_id, type) { + r =tdp_mmu_split_huge_pages_root (kvm, root, start, end, + target_level, shared); +if (r) { +kvm_tdp_mmu_put_root (kvm, root); +return r; + } + } +return 0 ; +}/* * Try to split all huge pages mapped by the TDP MMU down to the target level.
@@ -1625,18 +1645,18 @@ void kvm_tdp_mmu_try_split_huge_pages(struct kvm *kvm, gfn_t start, gfn_t end, int target_level, bool shared) { - struct kvm_mmu_page *root; -int r =0 ; + tdp_mmu_split_huge_pages(kvm, slot->as_id, KVM_VALID_ROOTS, start, end, +target_level ,shared ); +} -kvm_lockdep_assert_mmu_lock_held (kvm, shared); -for_each_valid_tdp_mmu_root_yield_safe (kvm, root, slot->as_id) { - r =tdp_mmu_split_huge_pages_root (kvm, root, start, end, target_level, shared); -if (r) { -kvm_tdp_mmu_put_root (kvm, root); -break ; - } - } +int kvm_tdp_mmu_mirrors_split_huge_pages (struct kvm *kvm,gfn_t start, +gfn_t end,int target_level) +{ +/* The as_id for mirror roots can only be 0 */ +return tdp_mmu_split_huge_pages (kvm,0 ,KVM_MIRROR_ROOTS , start, end, + target_level, false); } +EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_tdp_mmu_mirrors_split_huge_pages);static bool tdp_mmu_need_write_protect (struct kvm *kvm,struct kvm_mmu_page *sp) {
arch/x86/kvm/mmu/tdp_mmu.h +2 -0
@@ -97,6 +97,8 @@ void kvm_tdp_mmu_try_split_huge_pages(struct kvm *kvm,const struct kvm_memory_slot *slot, gfn_t start, gfn_t end, int target_level, bool shared); +int kvm_tdp_mmu_mirrors_split_huge_pages (struct kvm *kvm,gfn_t start, +gfn_t end,int target_level);static inline void kvm_tdp_mmu_walk_lockless_begin (void ) {
Signature · 1 line
2.43.2
Re: [PATCH v4 10/17] KVM: x86/mmu: Add a TDP MMU API to split huge pages for mirror roots
Quoted 8 lines · Yan Zhao
From: Sean Christopherson <[email protected]>
Add an exported API to split huge pages in mirror roots for a given gfn range. TDX will use the API to split huge pages in preparation for partially zapping a private huge page, e.g., for converting a huge page from private to shared, or for splitting a huge page to match the guest's ACCEPT level.
Quoted 4 lines
For all intents and purposes, no functional change intended.
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Yan Zhao <[email protected]>
PATCH v4 11/17 KVM: TDX: Honor the guest's accept level contained in an EPT violation
In reply to Yan ZhaoTDX requires guests to accept S-EPT mappings created by the host KVM. Due to the current implementation of the TDX module, if a guest accepts a GFN at a lower level after KVM maps it at a higher level, the TDX module will synthesize an EPT Violation VM-Exit to KVM instead of returning a size mismatch error to the guest. If KVM fails to perform page splitting in the EPT Violation handler, the guest's ACCEPT operation will be triggered again upon re-entering the guest, causing a repeated EPT Violation VM-Exit.
To ensure forward progress, honor the guest's accept level if an EPT Violation VM-Exit contains the guest accept level (the TDX module provides the level when synthesizing a VM-Exit in response to a failed guest ACCEPT, e.g., due to accept level < host mapping level or due to intermediate paging structure missing or inaccessible).
(1) Set the guest inhibit bit in the lpage info to prevent KVM's MMU from mapping at a higher level than the guest's accept level.
(2) Split any existing mapping higher than the guest's accept level.
For now, take mmu_lock for write across the entire operation to keep things simple. This can/will be revisited when the TDX module adds support for NON-BLOCKING-RESIZE, at which point KVM can split the huge page without needing to handle UNBLOCK failure if the DEMOTE fails.
To avoid unnecessarily contending mmu_lock, check if the inhibit flag is already set before acquiring mmu_lock, e.g. so that vCPUs doing ACCEPT on a region of memory aren't completely serialized. Note, this relies on
(a) setting the inhibit after performing the split, and (b) never clearing the flag, e.g., to avoid false positives and potentially triggering the zero-step mitigation.
Note: EPT Violation VM-Exits without the guest's accept level are *never* caused by the guest's ACCEPT operation, but instead occur if the guest accesses memory before said memory is accepted. Since KVM can't obtain the guest accept level info from such EPT Violations (the ACCEPT operation hasn't occurred yet), KVM may still map at a higher level than the guest's later ACCEPT level.
So, the typical guest/KVM interaction flow is:
- If guest accesses private memory without first accepting it, (like non-Linux guests):
1. Guest accesses a private memory.
2. KVM finds it can map the GFN at 2MB. So, AUG at 2MB.
3. Guest accepts the GFN at 4KB.
4. KVM receives an EPT violation with eeq_type of ACCEPT + 4KB level.
5. KVM splits the 2MB mapping.
6. Guest accepts successfully and accesses the page.
- If guest first accepts private memory before accessing it, (like Linux guests):
1. Guest accepts a private memory at 4KB.
2. KVM receives an EPT violation with eeq_type of ACCEPT + 4KB level.
3. KVM AUG at 4KB.
4. Guest accepts successfully and accesses the page.
---
v4:
- In [1], Sean renamed tdx_honor_guest_accept_level() to tdx_handle_mismatched_accept(). In v4, Yan further renamed it to tdx_handle_guest_accept_ept_violation(), because EPT violations caused by a guest's ACCEPT operation are not necessarily due to the guest's ACCEPT level mismatching KVM's mapping level. In the case where a guest accepts memory before ever accessing it, EPT violations caused by the guest's ACCEPT operation occur when there are no present KVM mappings.
- Introduced helper tdx_is_mismatched_accepted() (Yan renamed it to tdx_is_guest_accept_ept_violation()). (Sean)
- Introduced helper tdx_get_ept_violation_level(). (Sean)
- Invoked kvm_tdp_mmu_mirrors_split_huge_pages() instead of kvm_split_cross_boundary_leafs(). (Sean)
[1] https://lore.kernel.org/all/[email protected]
--- arch/x86/kvm/vmx/tdx.c | 75 +++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx_arch.h | 3 ++ 2 files changed, 78 insertions(+)
arch/x86/kvm/vmx/tdx.c +75 -0
@@ -14,6 +14,7 @@#include "tdx.h" #include "vmx.h" #include "mmu/spte.h" +#include "mmu/tdp_mmu.h" #include "common.h" #include "posted_intr.h" #include "irq.h"
@@ -2049,6 +2050,76 @@ static inline bool tdx_is_sept_violation_unexpected_pending(struct kvm_vcpu *vcpreturn !(eq &EPT_VIOLATION_PROT_MASK ); } +static bool tdx_is_guest_accept_ept_violation (struct kvm_vcpu *vcpu) +{ +return (to_tdx (vcpu)->ext_exit_qualification &TDX_EXT_EXIT_QUAL_TYPE_MASK ) == +TDX_EXT_EXIT_QUAL_TYPE_ACCEPT ; +} + +static int tdx_get_ept_violation_level (struct kvm_vcpu *vcpu) +{ +u64 ext_exit_qual =to_tdx (vcpu)->ext_exit_qualification; + +return (((ext_exit_qual &TDX_EXT_EXIT_QUAL_INFO_MASK ) >> +TDX_EXT_EXIT_QUAL_INFO_SHIFT ) &GENMASK (2 ,0 )) +1 ; +} + +/* +* An EPT violation can be either due to the guest's ACCEPT operation or +* due to the guest's access of memory before the guest accepts the +* memory. +* +* Type TDX_EXT_EXIT_QUAL_TYPE_ACCEPT in the extended exit qualification +* identifies EPT violations caused by the guest's ACCEPT operation, which must +* contain a valid guest accept level. For such EPT violations, honor guest's +* accept level by setting guest inhibit bit on levels above the guest accept +* level and split the existing mapping for the faulting GFN if it's with a +* higher level than the guest accept level. +* +* Do nothing if the EPT violation is not caused by the guest's ACCEPT +* operation. KVM will map the GFN without considering the guest's accept level +* (unless the guest inhibit bit is already set). +*/ +static int tdx_handle_guest_accept_ept_violation (struct kvm_vcpu *vcpu,gfn_t gfn) +{ +struct kvm_memory_slot *slot =kvm_vcpu_gfn_to_memslot (vcpu, gfn); +struct kvm *kvm = vcpu->kvm; +gfn_t start, end; +int level, r; + +if (!slot || !tdx_is_guest_accept_ept_violation (vcpu)) +return 0 ; + +if (WARN_ON_ONCE (!VALID_PAGE (vcpu->arch.mmu->mirror_root_hpa))) +return 0 ; + + level =tdx_get_ept_violation_level (vcpu); +if (level >PG_LEVEL_2M ) +return 0 ; + +if (hugepage_test_guest_inhibit (slot, gfn, level +1 )) +return 0 ; + +guard (write_lock)(&kvm->mmu_lock); + + start =gfn_round_for_level (gfn, level); + end = start +KVM_PAGES_PER_HPAGE (level); + + r =kvm_tdp_mmu_mirrors_split_huge_pages (kvm, start, end, level); +if (r) +return r; + +/* +* No TLB flush is required, as the "BLOCK + TRACK + kick off vCPUs" +* sequence required by the TDX-Module includes a TLB flush. +*/ +hugepage_set_guest_inhibit (slot, gfn, level +1 ); +if (level ==PG_LEVEL_4K ) +hugepage_set_guest_inhibit (slot, gfn, level +2 ); + +return 0 ; +} +static int tdx_handle_ept_violation (struct kvm_vcpu *vcpu) {unsigned long exit_qual;
@@ -2074,6 +2145,10 @@ static int tdx_handle_ept_violation(struct kvm_vcpu *vcpu)*/ exit_qual =EPT_VIOLATION_ACC_WRITE ; + ret =tdx_handle_guest_accept_ept_violation (vcpu,gpa_to_gfn (gpa)); +if (ret) +return ret; +/* Only private GPA triggers zero-step mitigation */ local_retry = true; }else {
arch/x86/kvm/vmx/tdx_arch.h +3 -0
@@ -76,7 +76,10 @@ struct tdx_cpuid_value { } __packed;#define TDX_EXT_EXIT_QUAL_TYPE_MASK GENMASK(3, 0) +#define TDX_EXT_EXIT_QUAL_TYPE_ACCEPT 1#define TDX_EXT_EXIT_QUAL_TYPE_PENDING_EPT_VIOLATION 6 +#define TDX_EXT_EXIT_QUAL_INFO_MASK GENMASK(63, 32) +#define TDX_EXT_EXIT_QUAL_INFO_SHIFT 32/* * TD_PARAMS is provided as an input to TDH_MNG_INIT, the size of which is 1024B. */
Signature · 1 line
2.43.2
Edgecombe, Rick P
maintainerRe: [PATCH v4 11/17] KVM: TDX: Honor the guest's accept level contained in an EPT violation
arch/x86/kvm/vmx/tdx.c static inline bool tdx_is_sept_violation_unexpected_pending(struct kvm_vcpu *vcp · line 2115
Quoted 5 lines · Yan Zhao
arch/x86/kvm/vmx/tdx.c +5
-0+ +/* +* No TLB flush is required, as the "BLOCK + TRACK + kick off vCPUs" +* sequence required by the TDX-Module includes a TLB flush. +*/
I think the TLB flush is actually more obvious than the level + 1/2 bits below. It is worried about inhibiting 1GB mappings, why?
Quoted 7 lines
arch/x86/kvm/vmx/tdx.c +7
-0+hugepage_set_guest_inhibit (slot, gfn, level +1 ); +if (level ==PG_LEVEL_4K ) +hugepage_set_guest_inhibit (slot, gfn, level +2 ); + +return 0 ; +} +
Re: [PATCH v4 11/17] KVM: TDX: Honor the guest's accept level contained in an EPT violation
Quoted 10 lines · Rick P
arch/x86/kvm/vmx/tdx.c static inline bool tdx_is_sept_violation_unexpected_pending(struct kvm_vcpu *vcp · line 2115
Quoted 5 lines · Yan Zhao
arch/x86/kvm/vmx/tdx.c +5
-0+ +/* +* No TLB flush is required, as the "BLOCK + TRACK + kick off vCPUs" +* sequence required by the TDX-Module includes a TLB flush. +*/ I think the TLB flush is actually more obvious than the level + 1/2 bits below. It is worried about inhibiting 1GB mappings, why?
Hmm, including inhibiting 1GB mappings can make the logic more generic and more correct in concept. That is, when the guest accept level is 2MB, 1GB mappings should be inhibited conceptually. This help remove the code dependency on the assumption that 1GB mappings are impossible with the initial implementation, given that inhibiting 1GB mappings as well is simple.
Quoted 7 lines
arch/x86/kvm/vmx/tdx.c static inline bool tdx_is_sept_violation_unexpected_pending(struct kvm_vcpu *vcp · line 2122
Quoted 7 lines
arch/x86/kvm/vmx/tdx.c +7
-0+hugepage_set_guest_inhibit (slot, gfn, level +1 ); +if (level ==PG_LEVEL_4K ) +hugepage_set_guest_inhibit (slot, gfn, level +2 ); + +return 0 ; +} +
If you have a strong opinion to drop inhibiting 1GB mappings for now, I can add the following diff:
@@ -2183,7 +2172,13 @@ static int tdx_handle_guest_accept_ept_violation(struct kvm_vcpu *vcpu, gfn_t gf
return 0;
level = tdx_get_ept_violation_level(vcpu);- if (level > PG_LEVEL_2M)
+ KVM_BUG_ON(level != PG_LEVEL_2M && level != PG_LEVEL_4K, kvm);
+
+ /*
+ * No need to inhibit 1GB mappings since they are unsupported with + * the initial huge page implementation. + */
+ if (level == PG_LEVEL_2M)
return 0;
if (hugepage_test_guest_inhibit(slot, gfn, level + 1))
@@ -2203,8 +2198,6 @@ static int tdx_handle_guest_accept_ept_violation(struct kvm_vcpu *vcpu, gfn_t gf
* sequence required by the TDX-Module includes a TLB flush.
*/
hugepage_set_guest_inhibit(slot, gfn, level + 1);- if (level == PG_LEVEL_4K)
- hugepage_set_guest_inhibit(slot, gfn, level + 2);
return 0;
}
Edgecombe, Rick P
maintainerRe: [PATCH v4 11/17] KVM: TDX: Honor the guest's accept level contained in an EPT violation
Quoted 11 lines · Yan Zhao
Quoted 10 lines · Rick P
arch/x86/kvm/vmx/tdx.c static inline bool tdx_is_sept_violation_unexpected_pending(struct kvm_vcpu *vcp · line 2115
Quoted 5 lines · Yan Zhao
arch/x86/kvm/vmx/tdx.c +5
-0+ +/* +* No TLB flush is required, as the "BLOCK + TRACK + kick off vCPUs" +* sequence required by the TDX-Module includes a TLB flush. +*/ I think the TLB flush is actually more obvious than the level + 1/2 bits below. It is worried about inhibiting 1GB mappings, why?
Quoted 5 lines
Hmm, including inhibiting 1GB mappings can make the logic more generic and more correct in concept. That is, when the guest accept level is 2MB, 1GB mappings should be inhibited conceptually. This help remove the code dependency on the assumption that 1GB mappings are impossible with the initial implementation, given that inhibiting 1GB mappings as well is simple.
I strongly think the code or comments should not prep for 1GB S-EPT pages or promotion. Warning if 1GB S-EPT pages is tried is good. But I don't see how silently handling this particular corner of huge pages helps anything.
If/when we get to 1GB mappings the code could have evolved at that point. So introducing code or comments now will add constraints on other work that is done. We don't want to think about it.
Quoted 12 lines
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c static inline bool tdx_is_sept_violation_unexpected_pending(struct kvm_vcpu *vcp · line 2122
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c +7
-0+hugepage_set_guest_inhibit (slot, gfn, level +1 ); +if (level ==PG_LEVEL_4K ) +hugepage_set_guest_inhibit (slot, gfn, level +2 ); + +return 0 ; +} +If you have a strong opinion to drop inhibiting 1GB mappings for now, I can add the following diff:
Review static int tdx_handle_guest_accept_ept_violation(struct kvm_vcpu *vcpu, gfn_t gf · line 2179
Quoted 9 lines
(quoted diff) +5
-1@@ -2183,7 +2172,13 @@ static int tdx_handle_guest_accept_ept_violation(struct kvm_vcpu *vcpu, gfn_t gf return 0; level = tdx_get_ept_violation_level(vcpu); - if (level > PG_LEVEL_2M) + KVM_BUG_ON(level != PG_LEVEL_2M && level != PG_LEVEL_4K, kvm); + + /* + * No need to inhibit 1GB mappings since they are unsupported with + * the initial huge page implementation.
This also implies 1GB is coming...
Quoted 7 lines
(quoted diff) +2
-0+ */ + if (level == PG_LEVEL_2M) return 0; if (hugepage_test_guest_inhibit(slot, gfn, level + 1))
Review static int tdx_handle_guest_accept_ept_violation(struct kvm_vcpu *vcpu, gfn_t gf · line 2203
Quoted 8 lines
(quoted diff) +0
-2@@ -2203,8 +2198,6 @@ static int tdx_handle_guest_accept_ept_violation(struct kvm_vcpu *vcpu, gfn_t gf * sequence required by the TDX-Module includes a TLB flush. */ hugepage_set_guest_inhibit(slot, gfn, level + 1); - if (level == PG_LEVEL_4K) - hugepage_set_guest_inhibit(slot, gfn, level + 2); return 0; }
There are a lot of possible things that are not handled if a 1GB mapping sneaks through.
Why not KVM_BUG_ON() if something tries to map 1GB in the appropriate tdx_sept_set_private_spte() sub functions. tdx_sept_split_leaf_spte() should be covered already, but not tdx_sept_map_leaf_spte() looks like.
Re: [PATCH v4 11/17] KVM: TDX: Honor the guest's accept level contained in an EPT violation
Quoted 18 lines · Rick P
Quoted 11 lines · Yan Zhao
Quoted 10 lines · Rick P
arch/x86/kvm/vmx/tdx.c static inline bool tdx_is_sept_violation_unexpected_pending(struct kvm_vcpu *vcp · line 2115
Quoted 5 lines · Yan Zhao
arch/x86/kvm/vmx/tdx.c +5
-0+ +/* +* No TLB flush is required, as the "BLOCK + TRACK + kick off vCPUs" +* sequence required by the TDX-Module includes a TLB flush. +*/ I think the TLB flush is actually more obvious than the level + 1/2 bits below. It is worried about inhibiting 1GB mappings, why?
Quoted 5 lines
Hmm, including inhibiting 1GB mappings can make the logic more generic and more correct in concept. That is, when the guest accept level is 2MB, 1GB mappings should be inhibited conceptually. This help remove the code dependency on the assumption that 1GB mappings are impossible with the initial implementation, given that inhibiting 1GB mappings as well is simple.
Quoted 3 lines
I strongly think the code or comments should not prep for 1GB S-EPT pages or promotion. Warning if 1GB S-EPT pages is tried is good. But I don't see how silently handling this particular corner of huge pages helps anything.
Hmm. I didn't realize inhibiting 1GB S-EPT pages would be regarded as a prep for 1GB S-EPT pages enabling or promotion.
Quoted 3 lines
If/when we get to 1GB mappings the code could have evolved at that point. So introducing code or comments now will add constraints on other work that is done. We don't want to think about it.
I'll drop the 1GB inhibition if it causes confusion for the reader.
Quoted 13 lines
Quoted 13 lines
Quoted 7 lines
arch/x86/kvm/vmx/tdx.c static inline bool tdx_is_sept_violation_unexpected_pending(struct kvm_vcpu *vcp · line 2122
Quoted 7 lines
arch/x86/kvm/vmx/tdx.c +7
-0+hugepage_set_guest_inhibit (slot, gfn, level +1 ); +if (level ==PG_LEVEL_4K ) +hugepage_set_guest_inhibit (slot, gfn, level +2 ); + +return 0 ; +} +If you have a strong opinion to drop inhibiting 1GB mappings for now, I can add the following diff:
(quoted diff) +0
-0@@ -2183,7 +2172,13 @@ static int tdx_handle_guest_accept_ept_violation(struct kvm_vcpu *vcpu, gfn_t gf return 0;
Quoted 9 lines
Review static int tdx_handle_guest_accept_ept_violation(struct kvm_vcpu *vcpu, gfn_t gf · line 2179
Quoted 7 lines
(quoted diff) +5
-1level = tdx_get_ept_violation_level(vcpu); - if (level > PG_LEVEL_2M) + KVM_BUG_ON(level != PG_LEVEL_2M && level != PG_LEVEL_4K, kvm); + + /* + * No need to inhibit 1GB mappings since they are unsupported with + * the initial huge page implementation.This also implies 1GB is coming...
What about simply commenting as
"No need to inhibit 1GB mappings since they are unsupported." ?
Quoted 19 lines
Quoted 7 lines
(quoted diff) +2
-0+ */ + if (level == PG_LEVEL_2M) return 0; if (hugepage_test_guest_inhibit(slot, gfn, level + 1))Review static int tdx_handle_guest_accept_ept_violation(struct kvm_vcpu *vcpu, gfn_t gf · line 2203
Quoted 8 lines
(quoted diff) +0
-2@@ -2203,8 +2198,6 @@ static int tdx_handle_guest_accept_ept_violation(struct kvm_vcpu *vcpu, gfn_t gf * sequence required by the TDX-Module includes a TLB flush. */ hugepage_set_guest_inhibit(slot, gfn, level + 1); - if (level == PG_LEVEL_4K) - hugepage_set_guest_inhibit(slot, gfn, level + 2); return 0; }There are a lot of possible things that are not handled if a 1GB mapping sneaks through.
Quoted 3 lines
Why not
KVM_BUG_ON()if something tries to map 1GB in the appropriatetdx_sept_set_private_spte()sub functions.tdx_sept_split_leaf_spte()should be covered already, but nottdx_sept_map_leaf_spte()looks like.
Hmm, there's an implicit KVM_BUG_ON() inside tdx_sept_map_leaf_spte() about the mapping level: both tdx_mem_page_aug() and tdx_mem_page_add() return error if the mapping level is 1GB.
Edgecombe, Rick P
maintainerRe: [PATCH v4 11/17] KVM: TDX: Honor the guest's accept level contained in an EPT violation
Quoted 18 lines · Yan Zhao
Quoted 8 lines · Rick P
Quoted 7 lines · Yan Zhao
Quoted 1 line · Rick P
On Mon, 2026-09-28 at 17:11 +0800, Yan Zhao wrote:
(quoted diff) +0
-0@@ -2183,7 +2172,13 @@ static inttdx_handle_guest_accept_ept_violation(struct
kvm_vcpu*vcpu,gfn_tgf return 0;Quoted 9 lines
Quoted 7 lines
level =
tdx_get_ept_violation_level(vcpu);
- if (level >PG_LEVEL_2M)
+KVM_BUG_ON(level != PG_LEVEL_2M && level != PG_LEVEL_4K, kvm); +
+ /*
+ * No need to inhibit 1GB mappings since they are unsupported with + * the initial huge page implementation.This also implies 1GB is coming...
Quoted 2 lines
What about simply commenting as
"No need to inhibit 1GB mappings since they are unsupported." ?
Seems reasonable.
Quoted 22 lines
Quoted 22 lines
Quoted 14 lines
+ */
+ if (level ==PG_LEVEL_2M)
return 0;if (
hugepage_test_guest_inhibit(slot, gfn, level + 1))(quoted diff) +0
-0@@ -2203,8 +2198,6 @@ static inttdx_handle_guest_accept_ept_violation(struct
kvm_vcpu*vcpu,gfn_tgf
* sequence required by the TDX-Module includes a TLB flush. */
hugepage_set_guest_inhibit(slot, gfn, level + 1); - if (level ==PG_LEVEL_4K)
-hugepage_set_guest_inhibit(slot, gfn, level + 2);Quoted 3 lines
return 0;
}There are a lot of possible things that are not handled if a 1GB mapping sneaks
through.
Quoted 8 lines
Quoted 4 lines
Why not
KVM_BUG_ON()if something tries to map 1GB in the appropriatetdx_sept_set_private_spte()sub functions.tdx_sept_split_leaf_spte()should be
covered already, but nottdx_sept_map_leaf_spte()looks like.Hmm, there's an implicit
KVM_BUG_ON()insidetdx_sept_map_leaf_spte()about the
mapping level: bothtdx_mem_page_aug()andtdx_mem_page_add()return error if the mapping level is 1GB.
Oh right. So it's covered. I guess the only thing missing is explicit KVM_BUG_ON for documentation. I'm ok leaving it out though.
PATCH v4 12/17 KVM: x86/mmu: Add support for splitting S-EPT entry under non-vCPU context
In reply to Yan ZhaoAdd support for splitting S-EPT entries under a non-vCPU context. This prepares for zapping a subset of a huge mapping in S-EPT caused by private-to-shared conversions or guest_memfd reclaiming of physical memory.
KVM must precisely zap/remove S-EPT entries to avoid clobbering guest memory (the lifetime of guest private memory is tied to the S-EPT). So, KVM needs to first split a huge mapping so that small mappings can be zapped precisely.
Since there's no vCPU context, introduce a per-VM PAMT cache of pre-allocated pages used to populate the Dynamic PAMT. Add a helper tdx_get_pamt_cache() to select the per-VM PAMT cache when there's no vCPU context. Add the "kvm" arg to .topup_external_cache() and its caller tdp_mmu_alloc_sp_for_split() for the purpose of passing the "kvm" arg to tdx_get_pamt_cache().
Use a mutex to guard the entire cycle from the per-VM PAMT cache topup to drawing pages from the cache. Using a mutex (e.g., versus a spinlock) is important as it allows KVM to only drop and re-aquire the mmu_lock (a spinlock) while continuing holding the mutex for memory allocation.
Introduce a local static function tdx_sept_split_huge_pages(), which acquires the mutex before triggering the S-EPT entries splitting under a non-vCPU context. This function is intended to be invoked by guest_memfd via an arch hook in a later patch. Though functions related to dirty page tracking can also trigger splitting under a non-vCPU context, they do not yet involve mirror roots. So, how those functions should acquire the mutex is deferred to a later consideration.
tdx_sept_split_huge_pages() internally invokes API kvm_tdp_mmu_mirrors_split_huge_pages() to split mirror roots. To avoid unnecessary work, explicitly detect unaligned head and tail pages relative to the max page size supported by KVM (currently 2MB for private memory), and split only those pages, as only unaligned head/tail pages will undergo partial zapping.
---
v4:
- New patch.
- Split out the registration of the .gmem_prezap hook into a later patch to isolate gmem-related changes. (Yan)
--- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/mmu/mmu.c | 2 +- arch/x86/kvm/mmu/tdp_mmu.c | 7 +-- arch/x86/kvm/vmx/tdx.c | 94 +++++++++++++++++++++++++++++---- arch/x86/kvm/vmx/tdx.h | 5 ++ 5 files changed, 96 insertions(+), 14 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
@@ -1650,7 +1650,7 @@ struct kvm_x86_ops {/* Update external page tables for page table about to be freed. */ void (*free_external_spt)(struct kvm *kvm,struct kvm_mmu_page *sp); -int (*topup_external_cache)(struct kvm_vcpu *vcpu,int min_nr_spts); +int (*topup_external_cache)(struct kvm *kvm,struct kvm_vcpu *vcpu,int min_nr_spts);bool (*has_wbinvd_exit)(void );
arch/x86/kvm/mmu/mmu.c +1 -1
@@ -618,7 +618,7 @@ static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu, bool maybe_indirect)if (r)return r; - r =kvm_x86_call (topup_external_cache)(vcpu,PT64_ROOT_MAX_LEVEL ); + r =kvm_x86_call (topup_external_cache)(vcpu->kvm, vcpu,PT64_ROOT_MAX_LEVEL );if (r)return r; }
arch/x86/kvm/mmu/tdp_mmu.c +4 -3
@@ -1466,7 +1466,8 @@ bool kvm_tdp_mmu_wrprot_slot(struct kvm *kvm,return spte_set; } -static struct kvm_mmu_page *tdp_mmu_alloc_sp_for_split (bool is_mirror_sp) +static struct kvm_mmu_page *tdp_mmu_alloc_sp_for_split (struct kvm *kvm, +bool is_mirror_sp) {struct kvm_mmu_page *sp;
@@ -1483,7 +1484,7 @@ static struct kvm_mmu_page *tdp_mmu_alloc_sp_for_split(bool is_mirror_sp)if (!sp->external_spt) goto err_external_spt; -if (kvm_x86_call (topup_external_cache)(kvm_get_running_vcpu (),1 )) +if (kvm_x86_call (topup_external_cache)(kvm,kvm_get_running_vcpu (),1 )) goto err_external_split; }
@@ -1575,7 +1576,7 @@ static int tdp_mmu_split_huge_pages_root(struct kvm *kvm,else write_unlock (&kvm->mmu_lock); - sp =tdp_mmu_alloc_sp_for_split (is_mirror_root); + sp =tdp_mmu_alloc_sp_for_split (kvm, is_mirror_root);if (shared)read_lock (&kvm->mmu_lock);
arch/x86/kvm/vmx/tdx.c +85 -9
151 lines not on this page · raw message
arch/x86/kvm/vmx/tdx.h +5 -0
@@ -47,6 +47,11 @@ struct kvm_tdx {* Set/unset is protected with kvm->mmu_lock. */ bool wait_for_sept_zap; + +/* The per-VM cache for DPAMT pages for S-EPT pages and guest pages */ +struct tdx_pamt_cache pamt_cache; +/* Protect the per-VM cache for DPAMT pages */ +struct mutex pamt_cache_lock; };/* TDX module vCPU states */
Signature · 1 line
2.43.2
PATCH v4 13/17 KVM: guest_memfd: Add helpers to get start/end gfns give gmem+slot+pgoff
In reply to Yan ZhaoFrom: Sean Christopherson <[email protected]>
Add helpers for getting a gfn given a gmem slot+pgoff, and for getting a gfn given a starting or ending pgoff, i.e. an offset that may be beyond the range of the memslot binding. Providing helpers will avoid duplicate boilerplate code "if" future code also needs to iterate over gfn ranges.
No functional change intended.
--- virt/kvm/guest_memfd.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-)
virt/kvm/guest_memfd.c +17 -4
@@ -80,6 +80,21 @@ static pgoff_t kvm_gmem_get_index(struct kvm_memory_slot *slot, gfn_t gfn)return gfn - slot->base_gfn + slot->gmem.pgoff; } +static gfn_t kvm_gmem_get_gfn (struct kvm_memory_slot *slot,pgoff_t pgoff) +{ +return slot->base_gfn + pgoff - slot->gmem.pgoff; +} + +static gfn_t kvm_gmem_get_start_gfn (struct kvm_memory_slot *slot,pgoff_t start) +{ +return kvm_gmem_get_gfn (slot,max (slot->gmem.pgoff, start)); +} + +static gfn_t kvm_gmem_get_end_gfn (struct kvm_memory_slot *slot,pgoff_t end) +{ +return kvm_gmem_get_gfn (slot,min (slot->gmem.pgoff + slot->npages, end)); +} +static u64 kvm_gmem_get_default_attributes (struct inode *inode) {bool init_shared =GMEM_I (inode)->flags &GUEST_MEMFD_FLAG_INIT_SHARED ;
@@ -212,11 +227,9 @@ static void __kvm_gmem_invalidate_start(struct gmem_file *f, pgoff_t start,unsigned long index;xa_for_each_range (&f->bindings, index, slot, start, end -1 ) { -pgoff_t pgoff = slot->gmem.pgoff; -struct kvm_gfn_range gfn_range = { - .start = slot->base_gfn +max (pgoff, start) - pgoff, - .end = slot->base_gfn +min (pgoff + slot->npages, end) - pgoff, + .start =kvm_gmem_get_start_gfn (slot, start), + .end =kvm_gmem_get_end_gfn (slot, end), .slot = slot, .may_block = true, .attr_filter = attr_filter,
Signature · 1 line
2.43.2
Re: [PATCH v4 13/17] [GMEM-DEPENDENT] KVM: guest_memfd: Add helpers to get start/end gfns give gmem+slot+pgoff
Quoted 11 lines · Yan Zhao
From: Sean Christopherson <[email protected]>
Add helpers for getting a gfn given a gmem slot+pgoff, and for getting a gfn given a starting or ending pgoff, i.e. an offset that may be beyond the range of the memslot binding. Providing helpers will avoid duplicate boilerplate code "if" future code also needs to iterate over gfn ranges.
No functional change intended.
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Yan Zhao <[email protected]>
PATCH v4 14/17 KVM: guest_memfd: Split kvm_gmem_invalidate_start() to start() and zap()
In reply to Yan ZhaoSplit the kvm_gmem_invalidate_start() to two parts:
1) kvm_gmem_invalidate_start(): notifies KVM that MMU invalidation starts, and updates the invalidate range.
2) kvm_gmem_zap(): triggers the actual zapping of KVM secondary MMU mappings.
This prepares adding and invoking a .prezap() hook to guest_memfd.
No functional changes expected.
--- virt/kvm/guest_memfd.c | 49 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 6 deletions(-)
virt/kvm/guest_memfd.c +43 -6
@@ -217,9 +217,8 @@ static enum kvm_gfn_range_filter kvm_gmem_get_all_gfns_filter(struct inode *inodreturn KVM_FILTER_PRIVATE ; } -static void __kvm_gmem_invalidate_start (struct gmem_file *f,pgoff_t start, -pgoff_t end, -enum kvm_gfn_range_filter attr_filter) +static void __kvm_gmem_zap (struct gmem_file *f,pgoff_t start,pgoff_t end, +enum kvm_gfn_range_filter attr_filter) {bool flush = false, found_memslot = false;struct kvm_memory_slot *slot;
@@ -239,7 +238,6 @@ static void __kvm_gmem_invalidate_start(struct gmem_file *f, pgoff_t start, found_memslot = true;KVM_MMU_LOCK (kvm); -kvm_mmu_invalidate_start (kvm); } flush |=kvm_mmu_unmap_gfn_range (kvm, &gfn_range);
@@ -256,6 +254,41 @@ static void __kvm_gmem_invalidate_start(struct gmem_file *f, pgoff_t start,KVM_MMU_UNLOCK (kvm); } +static void kvm_gmem_zap (struct inode *inode,pgoff_t start,pgoff_t end, +enum kvm_gfn_range_filter filter) +{ +struct gmem_file *f; + +kvm_gmem_for_each_file (f, inode) +__kvm_gmem_zap (f, start, end, filter); +} + +static void __kvm_gmem_invalidate_start (struct gmem_file *f,pgoff_t start, +pgoff_t end, +enum kvm_gfn_range_filter attr_filter) +{ +bool found_memslot = false; +struct kvm_memory_slot *slot; +struct kvm *kvm = f->kvm; +unsigned long index; + +xa_for_each_range (&f->bindings, index, slot, start, end -1 ) { +gfn_t invalidate_start =kvm_gmem_get_start_gfn (slot, start); +gfn_t invalidate_end =kvm_gmem_get_end_gfn (slot, end); + +if (!found_memslot) { + found_memslot = true; + +KVM_MMU_LOCK (kvm); +kvm_mmu_invalidate_start (kvm); + } +kvm_mmu_invalidate_range_add (kvm, invalidate_start, invalidate_end); + } + +if (found_memslot) +KVM_MMU_UNLOCK (kvm); +} +static void kvm_gmem_invalidate_start (struct inode *inode,pgoff_t start,pgoff_t end,enum kvm_gfn_range_filter filter)
@@ -309,6 +342,7 @@ static long kvm_gmem_punch_hole(struct inode *inode, loff_t offset, loff_t len)filemap_invalidate_lock (inode->i_mapping);kvm_gmem_invalidate_start (inode, start, end, filter); +kvm_gmem_zap (inode, start, end, filter);truncate_inode_pages_range (inode->i_mapping, offset, offset + len -1 );
@@ -392,6 +426,7 @@ static long kvm_gmem_fallocate(struct file *file, int mode, loff_t offset,static int kvm_gmem_release (struct inode *inode,struct file *file) { +enum kvm_gfn_range_filter filter =kvm_gmem_get_all_gfns_filter (inode);struct gmem_file *f = file->private_data;struct kvm_memory_slot *slot;struct kvm *kvm = f->kvm;
@@ -461,8 +496,8 @@ static int kvm_gmem_release(struct inode *inode, struct file *file)* Zap all SPTEs pointed at by this file. Do not free the backing * memory, as its lifetime is associated with the inode, not the file. */ -__kvm_gmem_invalidate_start (f,0 ,-1ul , -kvm_gmem_get_all_gfns_filter (inode)); +__kvm_gmem_invalidate_start (f,0 ,-1ul , filter); +__kvm_gmem_zap (f,0 ,-1ul , filter);__kvm_gmem_invalidate_end (f,0 ,-1ul );list_del (&f->entry);
@@ -789,6 +824,7 @@ static int __kvm_gmem_set_attributes(struct inode *inode, pgoff_t start, filter = to_private ?KVM_FILTER_SHARED :KVM_FILTER_PRIVATE ;kvm_gmem_invalidate_start (inode, start, end, filter); +kvm_gmem_zap (inode, start, end, filter);if (!to_private &&kvm_arch_has_gmem_convert ())kvm_gmem_make_shared (inode, start, end);
@@ -890,6 +926,7 @@ static int kvm_gmem_error_folio(struct address_space *mapping, struct folio *fol filter =kvm_gmem_get_all_gfns_filter (inode);kvm_gmem_invalidate_start (inode, start, end, filter); +kvm_gmem_zap (inode, start, end, filter);/* * Do not truncate the range, what action is taken in response to the
Signature · 1 line
2.43.2
PATCH v4 15/17 KVM: guest_memfd: Add a pre-zap hook .gmem_prezap()
In reply to Yan ZhaoFrom: Sean Christopherson <[email protected]>
Add a gmem 'pre-zap' hook to allow arch code to take action before a zap, e.g., for shared<=>private conversion, and just as importantly, to let arch code reject performing the actual zap, e.g., if the conversion requires new page tables and KVM hits an OOM situation.
The arch code and hook will be used by TDX to split huge mappings as necessary to avoid over-zapping PTEs, which for all intents and purposes corrupts guest data for TDX VMs (memory is wiped when private PTEs are removed).
The hook is allowed to fail, however, there is no rollback when an error occurs. Therefore, the hook implementation is expected to be safe without any rollback on error. For example, in TDX, the hook splits huge mappings as necessary to avoid over-zapping PTEs. It is safe to leave the preceding successfully split mappings as-is rather than merging them back.
Currently, the pre-zap hook is invoked before zaps for memory attribute conversions and punch hole operations. The invocation in punch hole should be a no-op if the punch hole range is aligned to the huge page size. There is no need to trigger the pre-zap hook before releasing gmem, as all mappings will be gone anyway. The pre-zap hook is not invoked in kvm_gmem_error_folio() to avoid introducing additional failure points, and since when kvm_gmem_error_folio() is invoked, the VM is about to be killed, over-zapping is not a concern in that case.
---
- Rebased to gmem in-place conversion v13.
- Renamed .gmem_convert() in [1] to .gmem_prezap() as previously noted in [2].
- Added a comment for .gmem_prezap() noting that the hook is allowed to fail and must be safe without any rollback in the event of an error.(Yan)
[1] https://lore.kernel.org/all/[email protected]
[2] https://lore.kernel.org/all/[email protected]
--- arch/x86/include/asm/kvm-x86-ops.h | 3 ++ arch/x86/include/asm/kvm_host.h | 16 ++++++++ arch/x86/kvm/x86.c | 8 ++++ include/linux/kvm_host.h | 5 +++ include/linux/kvm_types.h | 1 + virt/kvm/Kconfig | 4 ++ virt/kvm/guest_memfd.c | 65 +++++++++++++++++++++++++++++- 7 files changed, 101 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm-x86-ops.h +3 -0
@@ -157,6 +157,9 @@ KVM_X86_OP_OPTIONAL(gmem_make_shared)#ifdef CONFIG_HAVE_KVM_ARCH_GMEM_INVALIDATE KVM_X86_OP_OPTIONAL(gmem_invalidate_range)#endif +#ifdef CONFIG_HAVE_KVM_ARCH_GMEM_PREZAP +KVM_X86_OP_OPTIONAL_RET0(gmem_prezap) +#endif KVM_X86_OP_OPTIONAL_RET0(gmem_max_mapping_level)#endif
arch/x86/include/asm/kvm_host.h +16 -0
@@ -1744,6 +1744,19 @@ struct kvm_x86_ops {#endif #ifdef CONFIG_HAVE_KVM_ARCH_GMEM_INVALIDATE void (*gmem_invalidate_range)(struct kvm *kvm,struct kvm_gfn_range *range); +#endif +#ifdef CONFIG_HAVE_KVM_ARCH_GMEM_PREZAP +/* +* Preparation before gmem triggering MMU zap, e.g., splitting huge +* mappings in S-EPT to prevent over-zapping in TDX. +* Note: Though the preparation is allowed to fail, it must be safe to +* proceed without any rollback when an error occurs. For example, if an +* error occurs while splitting a huge mapping, it is safe to leave the +* preceding successfully split mappings as-is rather than merging them +* back. +*/ +int (*gmem_prezap)(struct kvm *kvm,gfn_t start,gfn_t end, +enum kvm_gfn_range_filter attr_filter);#endif int (*gmem_max_mapping_level)(struct kvm *kvm,kvm_pfn_t pfn,bool is_private); };
@@ -1866,6 +1879,9 @@ enum kvm_intr_type {#ifdef CONFIG_HAVE_KVM_ARCH_GMEM_CONVERT #define kvm_arch_has_gmem_convert () (!!kvm_x86_ops.gmem_make_private)#endif +#ifdef CONFIG_HAVE_KVM_ARCH_GMEM_PREZAP +#define kvm_arch_has_gmem_prezap () (!!kvm_x86_ops.gmem_prezap) +#endif #define kvm_arch_has_readonly_mem (kvm) (!(kvm)->arch.has_protected_state)
arch/x86/kvm/x86.c +8 -0
@@ -10667,6 +10667,14 @@ void kvm_arch_gmem_invalidate_range(struct kvm *kvm, struct kvm_gfn_range *rangekvm_x86_call (gmem_invalidate_range)(kvm, range); }#endif + +#ifdef CONFIG_HAVE_KVM_ARCH_GMEM_PREZAP +int kvm_arch_gmem_prezap (struct kvm *kvm,gfn_t start,gfn_t end, +enum kvm_gfn_range_filter attr_filter) +{ +return kvm_x86_call (gmem_prezap)(kvm, start, end, attr_filter); +} +#endif #endif void kvm_fixup_and_inject_pf_error (struct kvm_vcpu *vcpu,gva_t gva,u16 error_code)
include/linux/kvm_host.h +5 -0
@@ -2622,6 +2622,11 @@ void kvm_arch_gmem_make_shared(kvm_pfn_t pfn, kvm_pfn_t nr_pages);#define kvm_arch_has_gmem_convert () false#endif +#ifdef CONFIG_HAVE_KVM_ARCH_GMEM_PREZAP +int kvm_arch_gmem_prezap (struct kvm *kvm,gfn_t start,gfn_t end, +enum kvm_gfn_range_filter attr_filter); +#endif +#ifdef CONFIG_HAVE_KVM_ARCH_GMEM_POPULATE /** * kvm_gmem_populate() - Populate/prepare a GPA range with guest data
include/linux/kvm_types.h +1 -0
@@ -49,6 +49,7 @@ struct kvm_vcpu_init;struct kvm_memslots ;enum kvm_mr_change ; +enum kvm_gfn_range_filter ;/* * Address types:
virt/kvm/Kconfig +4 -0
@@ -116,6 +116,10 @@ config HAVE_KVM_ARCH_GMEM_INVALIDATE bool depends on KVM_GUEST_MEMFD +config HAVE_KVM_ARCH_GMEM_PREZAP + bool + depends on KVM_GUEST_MEMFD + config HAVE_KVM_ARCH_GMEM_POPULATE bool depends on KVM_GUEST_MEMFD
virt/kvm/guest_memfd.c +64 -1
103 lines not on this page · raw message
Signature · 1 line
2.43.2
Re: [PATCH v4 15/17] [GMEM-DEPENDENT] KVM: guest_memfd: Add a pre-zap hook .gmem_prezap()
Quoted 1 line · Yan Zhao
From: Sean Christopherson <[email protected]>
...
Quoted 1 line
Not-Yet-Signed-off-by Sean Christopherson <[email protected]>
Part of this patch is taken from Sean's patch [1], and another part is split out from Sean's [2]. I renamed the op name to .gmem_prezap(), defined it as safe-without-rollback in the op comment, and had it invoked by __kvm_gmem_set_attributes() after rebasing.
Since I wasn't sure my changes would look good to Sean, I used the Not-Yet tag.
Rick reminded me that using a tag in this way is not appropriate. Since both [1] and [2] carry Sean's SoB, I'll update the tags and changelog parts to:
---
v4:
1. Split the parts of introducing .gmem_convert() and updating kvm_arch_gmem_convert() from the patch in link [2] and merged them into patch [1]. (Yan)
2. Renamed .gmem_convert() in [1] to .gmem_prezap() as previously noted in
[3] (Yan)
3. Used attr_filter for private/shared info. (Yan)
4. Added a comment for .gmem_prezap() noting that the hook is allowed to fail and must be safe without any rollback in the event of an error. (Yan).
5. Rebased gmem in-place conversion series and invoke .gmem_prezap() in kvm_gmem_set_attributes(). (Yan)
Quoted 11 lines
[Yan: Renamed to .gmem_prezap(), used attr_filter for private/shared info]Signed-off-by Yan Zhao <[email protected]>---
- Rebased to gmem in-place conversion v13.
- Renamed .gmem_convert() in [1] to .gmem_prezap() as previously noted in [2].
- Added a comment for .gmem_prezap() noting that the hook is allowed to fail and must be safe without any rollback in the event of an error.(Yan)[1] https://lore.kernel.org/all/[email protected]
[2] https://lore.kernel.org/all/[email protected]
PATCH v4 16/17 KVM: TDX: Implement .gmem_prezap() hook to split S-EPT
In reply to Yan ZhaoImplement the .gmem_prezap() hook to split S-EPT huge mappings before guest_memfd zaps S-EPT, in case KVM zaps only a subset of an S-EPT huge mapping's range. This allows guest_memfd to precisely zap/remove S-EPT entries to avoid clobbering guest memory, as the lifetime of guest private memory is tied to the S-EPT. That is, KVM must first split a huge mapping so that it does not over-zap irrelevant subranges of the huge mapping, which could cause TD termination.
--- arch/x86/kvm/Kconfig | 1 + arch/x86/kvm/vmx/tdx.c | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-)
arch/x86/kvm/Kconfig +1 -0
@@ -147,6 +147,7 @@ config KVM_INTEL_TDX default y depends on INTEL_TDX_HOST select HAVE_KVM_ARCH_GMEM_POPULATE + select HAVE_KVM_ARCH_GMEM_PREZAP help Provides support for launching Intel Trust Domain Extensions (TDX) confidential VMs on Intel processors.
arch/x86/kvm/vmx/tdx.c +16 -2
@@ -2089,8 +2089,7 @@ static int tdx_sept_split_straddling_huge_pages_to_level(struct kvm *kvm, gfn_t* per-VM cache of pre-allocated pages used to populate the Dynamic PAMT when * splitting S-EPT huge pages. */ -static int __maybe_unused tdx_sept_split_huge_pages (struct kvm *kvm,gfn_t start, -gfn_t end) +static int tdx_sept_split_huge_pages (struct kvm *kvm,gfn_t start,gfn_t end) {guard (mutex)(&to_kvm_tdx (kvm)->pamt_cache_lock);
@@ -2103,6 +2102,20 @@ static int __maybe_unused tdx_sept_split_huge_pages(struct kvm *kvm, gfn_t startreturn tdx_sept_split_straddling_huge_pages_to_level (kvm, start, end,PG_LEVEL_4K ); } +/* +* Before zapping private mappings, KVM must first split any huge mappings that +* straddle the zapping range, as KVM mustn't overzap private mappings for TDX +* guests, i.e. KVM must zap _exactly_ [start, end). +*/ +static int tdx_gmem_prezap (struct kvm *kvm,gfn_t start,gfn_t end, +enum kvm_gfn_range_filter attr_filter) +{ +if (!(attr_filter &KVM_FILTER_PRIVATE ) || !kvm_has_mirrored_tdp (kvm)) +return 0 ; + +return tdx_sept_split_huge_pages (kvm, start, end); +} +void tdx_deliver_interrupt (struct kvm_lapic *apic,int delivery_mode,int trig_mode,int vector) {
@@ -3799,6 +3812,7 @@ int __init tdx_hardware_setup(void) vt_x86_ops.set_external_spte = tdx_sept_set_private_spte; vt_x86_ops.free_external_spt = tdx_sept_free_private_spt; + vt_x86_ops.gmem_prezap = tdx_gmem_prezap;if (tdx_supports_dynamic_pamt (tdx_sysinfo)) vt_x86_ops.topup_external_cache = tdx_topup_external_pamt_cache;
Signature · 1 line
2.43.2
PATCH v4 17/17 KVM: TDX: Turn on PG_LEVEL_2M
In reply to Yan ZhaoIntroduce a module parameter named "tdx_huge_page" for kvm-intel.ko and enable TDX huge pages if the module parameter is true and if:
(1) KVM supports gmem in-place conversions,
(2) the TDX module supports uninterruptible demote, and
(3) the TDX module supports DPAMT.
Condition (1) forces TDX huge pages to be the first user of gmem in-place conversion. Condition (2) is required because the current splitting implementation depends on the TDX module's non-interruptible demote feature. Condition (3) simplifies the DEMOTE SEAMCALL's implementation.
When TDX huge page support is enabled, report to KVM MMU that the maximum allowed mapping level for private memory is 2MB when the TD is RUNNABLE, while forcing it to 4KB during TD build time. This is because KVM can only create mappings up to 2MB level in the S-EPT via the TDH_MEM_PAGE_AUG SEAMCALL, while TDH_MEM_PAGE_ADD mandates the mapping level must be 4KB. 1GB mappings in the S-EPT are only possible via promotion, which is not yet supported due to the complexity incurred by the TDX module's rules for huge page promotion.
---
v4:
- Disallowed huge page if gmem_in_place_conversion is false. (Sean)
- Updated map part due to MMU refactor. (Sean)
- Disallowed huge page if DPAMT is not enabled. (Rick)
v3:
- Introduce the module param enable_tdx_huge_page and disable to toggle TDX huge page support.
- Disable TDX huge page if TDX module does not support TDX_FEATURES0_ENHANCE_DEMOTE_INTERRUPTIBILITY. (Kai).
- Explain why not allow 2M before TD is RUNNABLE in patch log.(Kai)
- Add comment to explain the relationship between returning PG_LEVEL_2M and guest accept level. (Kai)
- Dropped some KVM_BUG_ON()s due to rebasing. Updated KVM_BUG_ON()s on mapping levels to take into account of enable_tdx_huge_page.
RFC v2:
- Merged RFC v1's patch 4 (forcing PG_LEVEL_4K before TD runnable) with patch 9 (allowing PG_LEVEL_2M after TD runnable).
--- arch/x86/kvm/vmx/tdx.c | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-)
arch/x86/kvm/vmx/tdx.c +34 -7
@@ -57,6 +57,9 @@bool enable_tdx__ro_after_init ;module_param_named (tdx, enable_tdx, bool,0444 ); +static bool __read_mostly enable_tdx_huge_page = true; +module_param_named (tdx_huge_page, enable_tdx_huge_page, bool,0444 ); +static const struct tdx_sys_info *tdx_sysinfo;void tdh_vp_rd_failed (struct vcpu_tdx *tdx,char *uclass,u32 field,u64 err)
@@ -1786,8 +1789,9 @@ static int tdx_sept_map_leaf_spte(struct kvm *kvm, gfn_t gfn, enum pg_level leveif (KVM_BUG_ON (!vcpu, kvm))return -EIO ; -/* TODO: handle large pages. */ -if (KVM_BUG_ON (level !=PG_LEVEL_4K , kvm)) +/* TODO: Support hugepages when building the initial TD image. */ +if (KVM_BUG_ON (level !=PG_LEVEL_4K && +to_kvm_tdx (kvm)->state !=TD_STATE_RUNNABLE , kvm))return -EIO ;WARN_ON_ONCE ((new_spte &VMX_EPT_RWX_MASK ) !=VMX_EPT_RWX_MASK );
@@ -1883,10 +1887,6 @@ static int tdx_sept_remove_leaf_spte(struct kvm *kvm, gfn_t gfn,if (KVM_BUG_ON (!is_hkid_assigned (to_kvm_tdx (kvm)), kvm))return -EIO ; -/* TODO: handle large pages. */ -if (KVM_BUG_ON (level !=PG_LEVEL_4K , kvm)) -return -EIO ; - err =tdh_do_no_vcpus (tdh_mem_range_block, kvm, &kvm_tdx->td, gpa, level, &entry, &level_state);if (TDX_BUG_ON_2 (err,TDH_MEM_RANGE_BLOCK , entry, level_state, kvm))
@@ -3665,12 +3665,34 @@ int tdx_vcpu_ioctl(struct kvm_vcpu *vcpu, void __user *argp)return ret; } +/* +* For private pages: +* +* Force KVM to map at 4KB level when !enable_tdx_huge_page (e.g., due to +* incompatible TDX module) or before TD state is RUNNABLE. +* +* Always allow KVM to map at 2MB level in other cases, though KVM may still map +* the page at 4KB (i.e., passing in PG_LEVEL_4K to AUG) due to +* (1) the backend folio is 4KB, +* (2) disallow_lpage restrictions: +* - mixed private/shared pages in the 2MB range +* - level misalignment due to slot base_gfn, slot size, and ugfn +* - guest_inhibit bit set due to guest's 4KB accept level +* (3) page merging is disallowed (e.g., when part of a 2MB range has been +* mapped at 4KB level during TD build time). +*/ int tdx_gmem_max_mapping_level (struct kvm *kvm,kvm_pfn_t pfn,bool is_private) {if (!is_private)return 0 ; -return PG_LEVEL_4K ; +if (!enable_tdx_huge_page) +return PG_LEVEL_4K ; + +if (unlikely (to_kvm_tdx (kvm)->state !=TD_STATE_RUNNABLE )) +return PG_LEVEL_4K ; + +return PG_LEVEL_2M ; }void tdx_hardware_unsetup (void )
@@ -3749,6 +3771,11 @@ static int __init __tdx_hardware_setup(void)if (misc_cg_set_capacity (MISC_CG_RES_TDX ,tdx_get_nr_guest_keyids ()))return -EINVAL ; +if (enable_tdx_huge_page && (!gmem_in_place_conversion || + !tdx_huge_page_demote_uninterruptible (tdx_sysinfo) || + !tdx_supports_dynamic_pamt (tdx_sysinfo))) + enable_tdx_huge_page = false; +return 0 ; }
Signature · 1 line
2.43.2