Enhance the SEAMCALL wrapper tdh_phymem_page_reclaim() to support huge pages by introducing new parameters: "folio", "start_idx", and "npages". These parameters specify the physical memory to be reclaimed, i.e., starting from the page at "start_idx" within a folio and spanning "npages" contiguous PFNs. The specified memory must be entirely contained within a single folio. Return TDX_SW_ERROR if the size of the reclaimed memory does not match the specified size. On the KVM side, introduce tdx_reclaim_folio() to align with and invoke the SEAMCALL wrapper tdh_phymem_page_reclaim(). The "noclear" parameter specifies whether tdx_clear_folio() should be subsequently invoked within tdx_reclaim_folio(). Additionally, provide two helper functions, tdx_reclaim_page() and tdx_reclaim_page_noclear(), to facilitate the reclaiming of 4KB pages. Signed-off-by: Xiaoyao Li Signed-off-by: Isaku Yamahata Signed-off-by: Yan Zhao --- RFC v2: - Introduce new params "folio", "start_idx" and "npages" to wrapper tdh_phymem_page_reclaim(). - Move the checking of return size from KVM to x86/virt and return error. - Rename tdx_reclaim_page() to tdx_reclaim_folio(). - Add two helper functions tdx_reclaim_page() tdx_reclaim_page_noclear() to faciliate the reclaiming of 4KB pages. RFC v1: - Rebased and split patch. --- arch/x86/include/asm/tdx.h | 3 ++- arch/x86/kvm/vmx/tdx.c | 27 ++++++++++++++++++--------- arch/x86/virt/vmx/tdx/tdx.c | 12 ++++++++++-- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h index a125bb20a28a..f1bd74348b34 100644 --- a/arch/x86/include/asm/tdx.h +++ b/arch/x86/include/asm/tdx.h @@ -189,7 +189,8 @@ u64 tdh_mng_init(struct tdx_td *td, u64 td_params, u64 *extended_err); u64 tdh_vp_init(struct tdx_vp *vp, u64 initial_rcx, u32 x2apicid); u64 tdh_vp_rd(struct tdx_vp *vp, u64 field, u64 *data); u64 tdh_vp_wr(struct tdx_vp *vp, u64 field, u64 data, u64 mask); -u64 tdh_phymem_page_reclaim(struct page *page, u64 *tdx_pt, u64 *tdx_owner, u64 *tdx_size); +u64 tdh_phymem_page_reclaim(struct folio *folio, unsigned long start_idx, unsigned long npages, + u64 *tdx_pt, u64 *tdx_owner, u64 *tdx_size); u64 tdh_mem_track(struct tdx_td *tdr); u64 tdh_mem_page_remove(struct tdx_td *td, u64 gpa, u64 level, u64 *ext_err1, u64 *ext_err2); u64 tdh_phymem_cache_wb(bool resume); diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index 4fabefb27135..facfe589e006 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -327,11 +327,12 @@ static void tdx_no_vcpus_enter_stop(struct kvm *kvm) } /* TDH.PHYMEM.PAGE.RECLAIM is allowed only when destroying the TD. */ -static int __tdx_reclaim_page(struct page *page) +static int tdx_reclaim_folio(struct folio *folio, unsigned long start_idx, + unsigned long npages, bool noclear) { u64 err, tdx_pt, tdx_owner, tdx_size; - err = tdh_phymem_page_reclaim(page, &tdx_pt, &tdx_owner, &tdx_size); + err = tdh_phymem_page_reclaim(folio, start_idx, npages, &tdx_pt, &tdx_owner, &tdx_size); /* * No need to check for TDX_OPERAND_BUSY; all TD pages are freed @@ -342,19 +343,25 @@ static int __tdx_reclaim_page(struct page *page) pr_tdx_error_3(TDH_PHYMEM_PAGE_RECLAIM, err, tdx_pt, tdx_owner, tdx_size); return -EIO; } + + if (!noclear) + tdx_clear_folio(folio, start_idx, npages); return 0; } static int tdx_reclaim_page(struct page *page) { - int r; + struct folio *folio = page_folio(page); - r = __tdx_reclaim_page(page); - if (!r) - tdx_clear_page(page); - return r; + return tdx_reclaim_folio(folio, folio_page_idx(folio, page), 1, false); } +static int tdx_reclaim_page_noclear(struct page *page) +{ + struct folio *folio = page_folio(page); + + return tdx_reclaim_folio(folio, folio_page_idx(folio, page), 1, true); +} /* * Reclaim the TD control page(s) which are crypto-protected by TDX guest's @@ -587,7 +594,7 @@ static void tdx_reclaim_td_control_pages(struct kvm *kvm) if (!kvm_tdx->td.tdr_page) return; - if (__tdx_reclaim_page(kvm_tdx->td.tdr_page)) + if (tdx_reclaim_page_noclear(kvm_tdx->td.tdr_page)) return; /* @@ -1932,11 +1939,13 @@ static int tdx_sept_remove_private_spte(struct kvm *kvm, gfn_t gfn, enum pg_level level, kvm_pfn_t pfn) { struct page *page = pfn_to_page(pfn); + struct folio *folio = page_folio(page); int ret; if (!is_hkid_assigned(to_kvm_tdx(kvm))) { KVM_BUG_ON(!kvm->vm_dead, kvm); - ret = tdx_reclaim_page(page); + ret = tdx_reclaim_folio(folio, folio_page_idx(folio, page), + KVM_PAGES_PER_HPAGE(level), false); if (!ret) { tdx_pamt_put(page, level); tdx_unpin(kvm, page); diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c index 64219c659844..9ed585bde062 100644 --- a/arch/x86/virt/vmx/tdx/tdx.c +++ b/arch/x86/virt/vmx/tdx/tdx.c @@ -1966,19 +1966,27 @@ EXPORT_SYMBOL_GPL(tdh_vp_init); * So despite the names, they must be interpted specially as described by the spec. Return * them only for error reporting purposes. */ -u64 tdh_phymem_page_reclaim(struct page *page, u64 *tdx_pt, u64 *tdx_owner, u64 *tdx_size) +u64 tdh_phymem_page_reclaim(struct folio *folio, unsigned long start_idx, unsigned long npages, + u64 *tdx_pt, u64 *tdx_owner, u64 *tdx_size) { + struct page *start = folio_page(folio, start_idx); struct tdx_module_args args = { - .rcx = page_to_phys(page), + .rcx = page_to_phys(start), }; u64 ret; + if (start_idx + npages > folio_nr_pages(folio)) + return TDX_OPERAND_INVALID; + ret = seamcall_ret(TDH_PHYMEM_PAGE_RECLAIM, &args); *tdx_pt = args.rcx; *tdx_owner = args.rdx; *tdx_size = args.r8; + if (npages != (1 << (*tdx_size) * PTE_SHIFT)) + return TDX_SW_ERROR; + return ret; } EXPORT_SYMBOL_GPL(tdh_phymem_page_reclaim); -- 2.43.2