Skip to content

Commit 94272b0

Browse files
ahunter6hansendc
authored andcommitted
x86/tdx: Eliminate duplicate code in tdx_clear_page()
tdx_clear_page() and reset_tdx_pages() duplicate the TDX page clearing logic. Rename reset_tdx_pages() to tdx_quirk_reset_paddr() and create tdx_quirk_reset_page() to call tdx_quirk_reset_paddr() and be used in place of tdx_clear_page(). The new name reflects that, in fact, the clearing is necessary only for hardware with a certain quirk. That is dealt with in a subsequent patch but doing the rename here avoids additional churn. Note reset_tdx_pages() is slightly different from tdx_clear_page() because, more appropriately, it uses mb() in place of __mb(). Except when extra debugging is enabled (kcsan at present), mb() just calls __mb(). Signed-off-by: Adrian Hunter <adrian.hunter@intel.com> Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com> Reviewed-by: Kirill A. Shutemov <kas@kernel.org> Reviewed-by: Binbin Wu <binbin.wu@linux.intel.com> Reviewed-by: Xiaoyao Li <xiaoyao.li@intel.com> Reviewed-by: Rick Edgecombe <rick.p.edgecombe@intel.com> Acked-by: Kai Huang <kai.huang@intel.com> Acked-by: Sean Christopherson <seanjc@google.com> Acked-by: Vishal Annapurve <vannapurve@google.com> Link: https://lore.kernel.org/all/20250819155811.136099-2-adrian.hunter%40intel.com
1 parent d8b483b commit 94272b0

3 files changed

Lines changed: 13 additions & 24 deletions

File tree

arch/x86/include/asm/tdx.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ int tdx_guest_keyid_alloc(void);
131131
u32 tdx_get_nr_guest_keyids(void);
132132
void tdx_guest_keyid_free(unsigned int keyid);
133133

134+
void tdx_quirk_reset_page(struct page *page);
135+
134136
struct tdx_td {
135137
/* TD root structure: */
136138
struct page *tdr_page;

arch/x86/kvm/vmx/tdx.c

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -281,25 +281,6 @@ static inline void tdx_disassociate_vp(struct kvm_vcpu *vcpu)
281281
vcpu->cpu = -1;
282282
}
283283

284-
static void tdx_clear_page(struct page *page)
285-
{
286-
const void *zero_page = (const void *) page_to_virt(ZERO_PAGE(0));
287-
void *dest = page_to_virt(page);
288-
unsigned long i;
289-
290-
/*
291-
* The page could have been poisoned. MOVDIR64B also clears
292-
* the poison bit so the kernel can safely use the page again.
293-
*/
294-
for (i = 0; i < PAGE_SIZE; i += 64)
295-
movdir64b(dest + i, zero_page);
296-
/*
297-
* MOVDIR64B store uses WC buffer. Prevent following memory reads
298-
* from seeing potentially poisoned cache.
299-
*/
300-
__mb();
301-
}
302-
303284
static void tdx_no_vcpus_enter_start(struct kvm *kvm)
304285
{
305286
struct kvm_tdx *kvm_tdx = to_kvm_tdx(kvm);
@@ -345,7 +326,7 @@ static int tdx_reclaim_page(struct page *page)
345326

346327
r = __tdx_reclaim_page(page);
347328
if (!r)
348-
tdx_clear_page(page);
329+
tdx_quirk_reset_page(page);
349330
return r;
350331
}
351332

@@ -593,7 +574,7 @@ static void tdx_reclaim_td_control_pages(struct kvm *kvm)
593574
pr_tdx_error(TDH_PHYMEM_PAGE_WBINVD, err);
594575
return;
595576
}
596-
tdx_clear_page(kvm_tdx->td.tdr_page);
577+
tdx_quirk_reset_page(kvm_tdx->td.tdr_page);
597578

598579
__free_page(kvm_tdx->td.tdr_page);
599580
kvm_tdx->td.tdr_page = NULL;
@@ -1714,7 +1695,7 @@ static int tdx_sept_drop_private_spte(struct kvm *kvm, gfn_t gfn,
17141695
pr_tdx_error(TDH_PHYMEM_PAGE_WBINVD, err);
17151696
return -EIO;
17161697
}
1717-
tdx_clear_page(page);
1698+
tdx_quirk_reset_page(page);
17181699
tdx_unpin(kvm, page);
17191700
return 0;
17201701
}

arch/x86/virt/vmx/tdx/tdx.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ static int tdmrs_set_up_pamt_all(struct tdmr_info_list *tdmr_list,
637637
* clear these pages. Note this function doesn't flush cache of
638638
* these TDX private pages. The caller should make sure of that.
639639
*/
640-
static void reset_tdx_pages(unsigned long base, unsigned long size)
640+
static void tdx_quirk_reset_paddr(unsigned long base, unsigned long size)
641641
{
642642
const void *zero_page = (const void *)page_address(ZERO_PAGE(0));
643643
unsigned long phys, end;
@@ -654,9 +654,15 @@ static void reset_tdx_pages(unsigned long base, unsigned long size)
654654
mb();
655655
}
656656

657+
void tdx_quirk_reset_page(struct page *page)
658+
{
659+
tdx_quirk_reset_paddr(page_to_phys(page), PAGE_SIZE);
660+
}
661+
EXPORT_SYMBOL_GPL(tdx_quirk_reset_page);
662+
657663
static void tdmr_reset_pamt(struct tdmr_info *tdmr)
658664
{
659-
tdmr_do_pamt_func(tdmr, reset_tdx_pages);
665+
tdmr_do_pamt_func(tdmr, tdx_quirk_reset_paddr);
660666
}
661667

662668
static void tdmrs_reset_pamt_all(struct tdmr_info_list *tdmr_list)

0 commit comments

Comments
 (0)