Re: [PATCH v4 12/16] x86/virt/tdx: Add helpers to allow for pre-allocating pages
From: Yan Zhao
Date: Tue Jan 20 2026 - 02:48:49 EST
On Tue, Jan 20, 2026 at 03:10:59PM +0800, Huang, Kai wrote:
> On Thu, 2025-11-27 at 10:38 +0800, Binbin Wu wrote:
> >
> > On 11/27/2025 6:33 AM, Edgecombe, Rick P wrote:
> > > > >
> > > > > static int tdx_topup_external_fault_cache(struct kvm_vcpu *vcpu, unsigned int cnt)
> > > > > {
> > > > > - struct vcpu_tdx *tdx = to_tdx(vcpu);
> > > > > + struct tdx_prealloc *prealloc = &to_tdx(vcpu)->prealloc;
> > > > > + int min_fault_cache_size;
> > > > >
> > > > > - return kvm_mmu_topup_memory_cache(&tdx->mmu_external_spt_cache, cnt);
> > > > > + /* External page tables */
> > > > > + min_fault_cache_size = cnt;
> > > > > + /* Dynamic PAMT pages (if enabled) */
> > > > > + min_fault_cache_size += tdx_dpamt_entry_pages() * PT64_ROOT_MAX_LEVEL;
> > > > Is the value PT64_ROOT_MAX_LEVEL intended, since dynamic PAMT pages are only
> > > > needed for 4KB level?
> > > I'm not sure I follow. We need DPAMT backing for each S-EPT page table.
> > Oh, right!
> >
> > IIUIC, PT64_ROOT_MAX_LEVEL is actually
> > - PT64_ROOT_MAX_LEVEL - 1 for S-ETP pages since root page is not needed.
> > - 1 for TD private memory page
> >
> > It's better to add a comment about it.
> >
>
> But theoretically we don't need a pair of DPAMT pages for one 4K S-EPT
> page -- we only need a pair for a entire 2M range. If these S-EPT pages
> in the fault path are allocated from the same 2M range, we are actually
> over allocating DPAMT pages.
topup() always ensures that the min page count in cache is the max count of
pages a fault needs.
For example, mmu_topup_memory_caches() ensures there are at least
PT64_ROOT_MAX_LEVEL pages in mmu_page_header_cache, which are not always
consumed by each fault.
But in the worst-case conditions, we actually need that many.
In the end, the unused pages in cache will be freed by mmu_free_memory_caches().
> And AFAICT unfortunately there's no way to resolve this, unless we use
So, I don't think it's a problem.
And I agree with Binbin :)
> tdx_alloc_page() for S-EPT pages.