From 6771551300c9ce5d98291c01b956b75a03d5babb Mon Sep 17 00:00:00 2001 From: Shiqiang Zhang Date: Wed, 29 Apr 2026 11:55:21 +0800 Subject: [PATCH] anolis: dma: swiotlb: align SWIOTLB buffer to 2MB for TDX guest ANBZ: #34088 In TDX guest environment, SWIOTLB buffers are converted to shared memory for DMA operations. Using 2MB alignment provides: - IOMMU can use 2MB large page mappings, reducing page table entries by 512x and improving IOTLB efficiency - Avoids EPT large page splitting when buffer boundaries align with 2MB boundaries, preventing page table fragmentation and EPT violations - Reduces both CPU TLB and IOMMU TLB pressure for better DMA performance The 2MB alignment is applied when swiotlb=any is specified, buffer size is 2MB aligned, and TDX guest is detected. This optimization only affects TDX guests; other platforms and architectures continue using PAGE_SIZE alignment. Signed-off-by: Shiqiang Zhang --- arch/x86/coco/tdx/tdx.c | 6 ++++++ include/linux/cc_platform.h | 6 ++++++ kernel/dma/swiotlb.c | 8 ++++++++ 3 files changed, 20 insertions(+) diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c index e165bd546474..03ffa90b0f70 100644 --- a/arch/x86/coco/tdx/tdx.c +++ b/arch/x86/coco/tdx/tdx.c @@ -201,6 +201,12 @@ int tdx_mcall_extend_rtmr(u8 *data, u8 index) } EXPORT_SYMBOL_GPL(tdx_mcall_extend_rtmr); +bool cc_platform_has_tdx_guest(void) +{ + return cpu_feature_enabled(X86_FEATURE_TDX_GUEST); +} +EXPORT_SYMBOL_GPL(cc_platform_has_tdx_guest); + static void __noreturn tdx_panic(const char *msg) { struct tdx_hypercall_args args = { diff --git a/include/linux/cc_platform.h b/include/linux/cc_platform.h index 672c777956f7..b622e25ac531 100644 --- a/include/linux/cc_platform.h +++ b/include/linux/cc_platform.h @@ -112,4 +112,10 @@ bool cc_platform_has_csv3(void); static inline bool cc_platform_has_csv3(void) { return false; } #endif /* CONFIG_HYGON_CSV */ +#ifdef CONFIG_INTEL_TDX_GUEST +bool cc_platform_has_tdx_guest(void); +#else /* !CONFIG_INTEL_TDX_GUEST */ +static inline bool cc_platform_has_tdx_guest(void) { return false; } +#endif /* CONFIG_INTEL_TDX_GUEST */ + #endif /* _LINUX_CC_PLATFORM_H */ diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index aff8b59ce109..67d8805e3a12 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -302,6 +302,14 @@ swiotlb_init(int verbose) align = SZ_16M; } + /* + * In TDX guest environment, use 2MB alignment for SWIOTLB buffer + * to optimize IOMMU page table and avoid EPT large page splitting. + */ + if (cc_platform_has_tdx_guest() && swiotlb_any && + bytes >= SZ_2M) + align = SZ_2M; + /* * For TDX, SEV or CSV without tee-io, all dma have to use * shared memory, that is, using the swiotlb mechanism. -- Gitee