AI Trajectory


Trajectory: (Expand/Collapse All)
Seq Timestamp Type Name Duration
0/0 2026/09/04 02:05 flow patch-triage
1m Args:
null

Results:
{
  "EnableConfigs": null,
  "FocusSymbols": [],
  "KMSANReasoning": "",
  "NeedsKMSAN": false,
  "Reasoning": "The patch series introduces Dynamic PAMT (DPAMT) support for Intel TDX (Trust Domain Extensions) host in the kernel and KVM. The modified code is entirely confined to Intel TDX host initialization and KVM TDX guest management (under arch/x86/virt/vmx/tdx and arch/x86/kvm/vmx/tdx.c, plus mirrored TDP handling guarded by KVM_X86_TDX_VM). These paths strictly require physical Intel CPUs with TDX host platform support (X86_FEATURE_TDX_HOST_PLATFORM, MSR_IA32_MKTME_KEYID_PARTITIONING, SEAMRR, and the Intel TDX firmware module), which are not present or emulated in standard QEMU/GCE fuzzing environments. Consequently, the modified code is structurally unreachable for fuzzing.",
  "WorthFuzzing": false
}

1/1 2026/09/04 02:05 action read-patch-diff
0m Args:
null

Results:
{
  "PatchDiff": "commit 6f3a5eaba903c2124d8fc6ad6755285ce8dfd5cc\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate:   Fri Sep 4 02:05:52 2026 +0000\n\n    syz-cluster: applied patch under review\n\ndiff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt\nindex 68647ff4bdd24..e7558b8382acf 100644\n--- a/Documentation/admin-guide/kernel-parameters.txt\n+++ b/Documentation/admin-guide/kernel-parameters.txt\n@@ -7582,6 +7582,16 @@ Kernel parameters\n \n \ttdfx=\t\t[HW,DRM]\n \n+\ttdx_dpamt=\n+\t\t\t[X86] Controls whether TDX will use Dynamic PAMT\n+\t\t\tto save memory, when supported.\n+\n+\t\t\tValid parameters: \"on\", \"off\"\n+\t\t\tDefault: \"off\"\n+\n+\t\t\tFor details see:\n+\t\t\tDocumentation/arch/x86/tdx.rst\n+\n \ttest_suspend=\t[SUSPEND]\n \t\t\tFormat: { \"mem\" | \"standby\" | \"freeze\" }[,N]\n \t\t\tSpecify \"mem\" (for Suspend-to-RAM) or \"standby\" (for\ndiff --git a/Documentation/arch/x86/tdx.rst b/Documentation/arch/x86/tdx.rst\nindex 3303499ad4c6f..de09967ce8d8c 100644\n--- a/Documentation/arch/x86/tdx.rst\n+++ b/Documentation/arch/x86/tdx.rst\n@@ -200,6 +200,33 @@ reflects the TCB of the currently running TDX module and therefore\n changes after an update. By contrast, TEE_TCB_SVN reflects the TCB at TD\n launch time and is not affected.\n \n+Dynamic PAMT\n+------------\n+\n+The Physical Address Metadata Table (PAMT) is metadata in which the TDX\n+module keeps data about each physical page (think struct page). Space\n+for it is allocated by the VMM, consumes up to about 0.4% of system\n+memory and needs to be supplied to the TDX module when the TDX module is\n+first loaded.\n+\n+Dynamic PAMT is an add-on feature that allows a VMM to dynamically\n+allocate the part of the PAMT which tracks 4KB pages. This reduces the\n+amount of memory that TDX consumes while TDs are not in use.\n+\n+When Dynamic PAMT is in use, dmesg shows it like::\n+\n+  [..] virt/tdx: Enable Dynamic PAMT\n+  [..] virt/tdx: 10092 KB allocated for PAMT\n+  [..] virt/tdx: TDX-Module initialized\n+\n+Dynamic PAMT is only enabled when supported and the ``tdx_dpamt=`` kernel\n+parameter is set to \"on\". The feature is off by default because TDX module\n+internal details prevent Dynamic PAMT from working on all keyid partitioning\n+configurations. When the TDX module is fixed to include these constraints in\n+its enumeration of Dynamic PAMT support, kernel support can be changed to\n+default on. For more information, consult the Intel TDX documentation about\n+Dynamic PAMT.\n+\n TDX Interaction to Other Kernel Components\n ------------------------------------------\n \ndiff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h\nindex e213c9ae3e301..5c358c40eae8a 100644\n--- a/arch/x86/include/asm/kvm-x86-ops.h\n+++ b/arch/x86/include/asm/kvm-x86-ops.h\n@@ -99,6 +99,7 @@ KVM_X86_OP_OPTIONAL_RET0(tdp_has_smep)\n KVM_X86_OP(load_mmu_pgd)\n KVM_X86_OP_OPTIONAL_RET0(set_external_spte)\n KVM_X86_OP_OPTIONAL(free_external_spt)\n+KVM_X86_OP_OPTIONAL_RET0(topup_external_cache)\n KVM_X86_OP(has_wbinvd_exit)\n KVM_X86_OP(get_l2_tsc_offset)\n KVM_X86_OP(get_l2_tsc_multiplier)\ndiff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h\nindex 683bb8bf43a94..57d37491c7c09 100644\n--- a/arch/x86/include/asm/kvm_host.h\n+++ b/arch/x86/include/asm/kvm_host.h\n@@ -1644,6 +1644,7 @@ struct kvm_x86_ops {\n \t/* Update external page tables for page table about to be freed. */\n \tvoid (*free_external_spt)(struct kvm *kvm, struct kvm_mmu_page *sp);\n \n+\tint (*topup_external_cache)(struct kvm_vcpu *vcpu, int min_nr_spts);\n \n \tbool (*has_wbinvd_exit)(void);\n \ndiff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h\nindex 89e97d5761d89..e186dfe5bf885 100644\n--- a/arch/x86/include/asm/tdx.h\n+++ b/arch/x86/include/asm/tdx.h\n@@ -36,6 +36,7 @@\n /* Bit definitions of TDX_FEATURES0 metadata field */\n #define TDX_FEATURES0_TD_PRESERVING\tBIT_ULL(1)\n #define TDX_FEATURES0_NO_RBP_MOD\tBIT_ULL(18)\n+#define TDX_FEATURES0_DYNAMIC_PAMT\tBIT_ULL(36)\n \n #ifndef __ASSEMBLER__\n \n@@ -118,12 +119,34 @@ static inline bool tdx_supports_runtime_update(const struct tdx_sys_info *sysinf\n \treturn sysinfo-\u003efeatures.tdx_features0 \u0026 TDX_FEATURES0_TD_PRESERVING;\n }\n \n+bool tdx_supports_dynamic_pamt(const struct tdx_sys_info *sysinfo);\n+\n+/* Simple structure for pre-allocating DPAMT pages outside of spinlocks. */\n+struct tdx_pamt_cache {\n+\tstruct list_head page_list;\n+\tint cnt;\n+};\n+\n+static inline void tdx_init_pamt_cache(struct tdx_pamt_cache *cache)\n+{\n+\tINIT_LIST_HEAD(\u0026cache-\u003epage_list);\n+\tcache-\u003ecnt = 0;\n+}\n+\n+void tdx_free_pamt_cache(struct tdx_pamt_cache *cache);\n+int tdx_topup_pamt_cache(struct tdx_pamt_cache *cache, unsigned long npages);\n+int tdx_pamt_get(kvm_pfn_t pfn, struct tdx_pamt_cache *cache);\n+void tdx_pamt_put(kvm_pfn_t pfn);\n+\n int tdx_guest_keyid_alloc(void);\n u32 tdx_get_nr_guest_keyids(void);\n void tdx_guest_keyid_free(unsigned int keyid);\n \n void tdx_quirk_reset_paddr(unsigned long base, unsigned long size);\n \n+struct page *tdx_alloc_control_page(void);\n+void tdx_free_control_page(struct page *page);\n+\n struct tdx_td {\n \t/* TD root structure: */\n \tstruct page *tdr_page;\ndiff --git a/arch/x86/include/asm/tdx_global_metadata.h b/arch/x86/include/asm/tdx_global_metadata.h\nindex 41150d546589c..8a3cc1a2a41e2 100644\n--- a/arch/x86/include/asm/tdx_global_metadata.h\n+++ b/arch/x86/include/asm/tdx_global_metadata.h\n@@ -1,7 +1,7 @@\n /* SPDX-License-Identifier: GPL-2.0 */\n-/* Automatically generated TDX global metadata structures. */\n-#ifndef _X86_VIRT_TDX_AUTO_GENERATED_TDX_GLOBAL_METADATA_H\n-#define _X86_VIRT_TDX_AUTO_GENERATED_TDX_GLOBAL_METADATA_H\n+/* TDX global metadata structures. */\n+#ifndef _X86_VIRT_TDX_TDX_GLOBAL_METADATA_H\n+#define _X86_VIRT_TDX_TDX_GLOBAL_METADATA_H\n \n #include \u003clinux/types.h\u003e\n \n@@ -21,6 +21,9 @@ struct tdx_sys_info_tdmr {\n \tu16 pamt_4k_entry_size;\n \tu16 pamt_2m_entry_size;\n \tu16 pamt_1g_entry_size;\n+\n+\t/* Optional metadata, if DPAMT is supported */\n+\tu8  pamt_page_bitmap_entry_bits;\n };\n \n struct tdx_sys_info_td_ctrl {\ndiff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c\nindex 064ecc33b9267..2816861ca279c 100644\n--- a/arch/x86/kvm/mmu/mmu.c\n+++ b/arch/x86/kvm/mmu/mmu.c\n@@ -617,6 +617,10 @@ static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu, bool maybe_indirect)\n \t\t\t\t\t       PT64_ROOT_MAX_LEVEL);\n \t\tif (r)\n \t\t\treturn r;\n+\n+\t\tr = kvm_x86_call(topup_external_cache)(vcpu, PT64_ROOT_MAX_LEVEL);\n+\t\tif (r)\n+\t\t\treturn r;\n \t}\n \tr = kvm_mmu_topup_memory_cache(\u0026vcpu-\u003earch.mmu_shadow_page_cache,\n \t\t\t\t       PT64_ROOT_MAX_LEVEL);\ndiff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c\nindex b272c20586a74..7c79f4d4bf99a 100644\n--- a/arch/x86/kvm/vmx/tdx.c\n+++ b/arch/x86/kvm/vmx/tdx.c\n@@ -362,7 +362,7 @@ static void tdx_reclaim_control_page(struct page *ctrl_page)\n \tif (tdx_reclaim_page(ctrl_page))\n \t\treturn;\n \n-\t__free_page(ctrl_page);\n+\ttdx_free_control_page(ctrl_page);\n }\n \n struct tdx_flush_vp_arg {\n@@ -589,7 +589,7 @@ static void tdx_reclaim_td_control_pages(struct kvm *kvm)\n \n \ttdx_quirk_reset_paddr(page_to_phys(kvm_tdx-\u003etd.tdr_page), PAGE_SIZE);\n \n-\t__free_page(kvm_tdx-\u003etd.tdr_page);\n+\ttdx_free_control_page(kvm_tdx-\u003etd.tdr_page);\n \tkvm_tdx-\u003etd.tdr_page = NULL;\n }\n \n@@ -681,6 +681,8 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu)\n \tif (!irqchip_split(vcpu-\u003ekvm))\n \t\treturn -EINVAL;\n \n+\ttdx_init_pamt_cache(\u0026tdx-\u003epamt_cache);\n+\n \tfpstate_set_confidential(\u0026vcpu-\u003earch.guest_fpu);\n \tvcpu-\u003earch.apic-\u003eguest_apic_protected = true;\n \tINIT_LIST_HEAD(\u0026tdx-\u003evt.pi_wakeup_list);\n@@ -866,6 +868,8 @@ void tdx_vcpu_free(struct kvm_vcpu *vcpu)\n \tstruct vcpu_tdx *tdx = to_tdx(vcpu);\n \tint i;\n \n+\ttdx_free_pamt_cache(\u0026tdx-\u003epamt_cache);\n+\n \tif (vcpu-\u003ecpu != -1) {\n \t\tKVM_BUG_ON(tdx-\u003estate == VCPU_TD_STATE_INITIALIZED, vcpu-\u003ekvm);\n \t\ttdx_flush_vp_on_cpu(vcpu);\n@@ -1618,6 +1622,17 @@ void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level)\n \ttd_vmcs_write64(to_tdx(vcpu), SHARED_EPT_POINTER, root_hpa);\n }\n \n+static int tdx_topup_external_pamt_cache(struct kvm_vcpu *vcpu, int min_nr_spts)\n+{\n+\t/*\n+\t * Minus one page to exclude the root SPT, but plus one page for a\n+\t * possible 4KB private mapping.\n+\t */\n+\tmin_nr_spts += -1 + 1;\n+\n+\treturn tdx_topup_pamt_cache(\u0026to_tdx(vcpu)-\u003epamt_cache, min_nr_spts);\n+}\n+\n static int tdx_mem_page_add(struct kvm *kvm, gfn_t gfn, enum pg_level level,\n \t\t\t    kvm_pfn_t pfn)\n {\n@@ -1676,16 +1691,28 @@ static struct page *tdx_spte_to_sept_pt(struct kvm *kvm, gfn_t gfn,\n static int tdx_sept_map_nonleaf_spte(struct kvm *kvm, gfn_t gfn,\n \t\t\t\t     enum pg_level level, u64 new_spte)\n {\n+\tstruct kvm_vcpu *vcpu = kvm_get_running_vcpu();\n \tgpa_t gpa = gfn_to_gpa(gfn);\n \tu64 err, entry, level_state;\n \tstruct page *sept_pt;\n+\tint ret;\n+\n+\tif (KVM_BUG_ON(!vcpu, kvm))\n+\t\treturn -EIO;\n \n \tsept_pt = tdx_spte_to_sept_pt(kvm, gfn, new_spte, level);\n \tif (!sept_pt)\n \t\treturn -EIO;\n \n+\tret = tdx_pamt_get(page_to_pfn(sept_pt), \u0026to_tdx(vcpu)-\u003epamt_cache);\n+\tif (KVM_BUG_ON(ret, kvm))\n+\t\treturn ret;\n+\n \terr = tdh_mem_sept_add(\u0026to_kvm_tdx(kvm)-\u003etd, gpa, level, sept_pt,\n \t\t\t       \u0026entry, \u0026level_state);\n+\tif (err)\n+\t\ttdx_pamt_put(page_to_pfn(sept_pt));\n+\n \tif (unlikely(tdx_operand_busy(err)))\n \t\treturn -EBUSY;\n \n@@ -1698,8 +1725,13 @@ static int tdx_sept_map_nonleaf_spte(struct kvm *kvm, gfn_t gfn,\n static int tdx_sept_map_leaf_spte(struct kvm *kvm, gfn_t gfn, enum pg_level level,\n \t\t\t\t  u64 new_spte)\n {\n+\tstruct kvm_vcpu *vcpu = kvm_get_running_vcpu();\n \tstruct kvm_tdx *kvm_tdx = to_kvm_tdx(kvm);\n \tkvm_pfn_t pfn = spte_to_pfn(new_spte);\n+\tint ret;\n+\n+\tif (KVM_BUG_ON(!vcpu, kvm))\n+\t\treturn -EIO;\n \n \t/* TODO: handle large pages. */\n \tif (KVM_BUG_ON(level != PG_LEVEL_4K, kvm))\n@@ -1707,6 +1739,10 @@ static int tdx_sept_map_leaf_spte(struct kvm *kvm, gfn_t gfn, enum pg_level leve\n \n \tWARN_ON_ONCE((new_spte \u0026 VMX_EPT_RWX_MASK) != VMX_EPT_RWX_MASK);\n \n+\tret = tdx_pamt_get(pfn, \u0026to_tdx(vcpu)-\u003epamt_cache);\n+\tif (KVM_BUG_ON(ret, kvm))\n+\t\treturn ret;\n+\n \t/*\n \t * Ensure pre_fault_allowed is read by kvm_arch_vcpu_pre_fault_memory()\n \t * before kvm_tdx-\u003estate.  Userspace must not be allowed to pre-fault\n@@ -1719,10 +1755,15 @@ static int tdx_sept_map_leaf_spte(struct kvm *kvm, gfn_t gfn, enum pg_level leve\n \t * If the TD isn't finalized/runnable, then userspace is initializing\n \t * the VM image via KVM_TDX_INIT_MEM_REGION; ADD the page to the TD.\n \t */\n-\tif (unlikely(kvm_tdx-\u003estate != TD_STATE_RUNNABLE))\n-\t\treturn tdx_mem_page_add(kvm, gfn, level, pfn);\n+\tif (likely(kvm_tdx-\u003estate == TD_STATE_RUNNABLE))\n+\t\tret = tdx_mem_page_aug(kvm, gfn, level, pfn);\n+\telse\n+\t\tret = tdx_mem_page_add(kvm, gfn, level, pfn);\n+\n+\tif (ret)\n+\t\ttdx_pamt_put(pfn);\n \n-\treturn tdx_mem_page_aug(kvm, gfn, level, pfn);\n+\treturn ret;\n }\n \n /*\n@@ -1819,6 +1860,7 @@ static int tdx_sept_remove_leaf_spte(struct kvm *kvm, gfn_t gfn,\n \t\treturn -EIO;\n \n \ttdx_quirk_reset_paddr(PFN_PHYS(pfn), PAGE_SIZE);\n+\ttdx_pamt_put(pfn);\n \treturn 0;\n }\n \n@@ -1862,6 +1904,8 @@ static int tdx_sept_set_private_spte(struct kvm *kvm, gfn_t gfn, u64 old_spte,\n  */\n static void tdx_sept_free_private_spt(struct kvm *kvm, struct kvm_mmu_page *sp)\n {\n+\tstruct page *sept_pt = virt_to_page(sp-\u003eexternal_spt);\n+\n \t/*\n \t * KVM doesn't (yet) zap page table pages in mirror page table while\n \t * TD is active, though guest pages mapped in mirror page table could be\n@@ -1875,15 +1919,15 @@ static void tdx_sept_free_private_spt(struct kvm *kvm, struct kvm_mmu_page *sp)\n \t * the page to prevent the kernel from accessing the encrypted page.\n \t */\n \tif (KVM_BUG_ON(is_hkid_assigned(to_kvm_tdx(kvm)), kvm) ||\n-\t    tdx_reclaim_page(virt_to_page(sp-\u003eexternal_spt)))\n+\t    tdx_reclaim_page(sept_pt))\n \t\tgoto out;\n \n \t/*\n-\t * Immediately free the S-EPT page because RCU-time free is unnecessary\n-\t * after TDH.PHYMEM.PAGE.RECLAIM ensures there are no outstanding\n-\t * readers.\n+\t * Immediately free the S-EPT page as the TDX subsystem doesn't support\n+\t * freeing pages from RCU callbacks, and more importantly because\n+\t * TDH.PHYMEM.PAGE.RECLAIM ensures there are no outstanding readers.\n \t */\n-\tfree_page((unsigned long)sp-\u003eexternal_spt);\n+\ttdx_free_control_page(sept_pt);\n out:\n \tsp-\u003eexternal_spt = NULL;\n }\n@@ -2456,7 +2500,7 @@ static int __tdx_td_init(struct kvm *kvm, struct td_params *td_params,\n \n \tret = -ENOMEM;\n \n-\ttdr_page = alloc_page(GFP_KERNEL_ACCOUNT);\n+\ttdr_page = tdx_alloc_control_page();\n \tif (!tdr_page)\n \t\tgoto free_hkid;\n \n@@ -2469,7 +2513,7 @@ static int __tdx_td_init(struct kvm *kvm, struct td_params *td_params,\n \t\tgoto free_tdr;\n \n \tfor (i = 0; i \u003c kvm_tdx-\u003etd.tdcs_nr_pages; i++) {\n-\t\ttdcs_pages[i] = alloc_page(GFP_KERNEL_ACCOUNT);\n+\t\ttdcs_pages[i] = tdx_alloc_control_page();\n \t\tif (!tdcs_pages[i])\n \t\t\tgoto free_tdcs;\n \t}\n@@ -2587,10 +2631,8 @@ static int __tdx_td_init(struct kvm *kvm, struct td_params *td_params,\n teardown:\n \t/* Only free pages not yet added, so start at 'i' */\n \tfor (; i \u003c kvm_tdx-\u003etd.tdcs_nr_pages; i++) {\n-\t\tif (tdcs_pages[i]) {\n-\t\t\t__free_page(tdcs_pages[i]);\n-\t\t\ttdcs_pages[i] = NULL;\n-\t\t}\n+\t\ttdx_free_control_page(tdcs_pages[i]);\n+\t\ttdcs_pages[i] = NULL;\n \t}\n \tif (!kvm_tdx-\u003etd.tdcs_pages)\n \t\tkfree(tdcs_pages);\n@@ -2605,16 +2647,13 @@ static int __tdx_td_init(struct kvm *kvm, struct td_params *td_params,\n \tfree_cpumask_var(packages);\n \n free_tdcs:\n-\tfor (i = 0; i \u003c kvm_tdx-\u003etd.tdcs_nr_pages; i++) {\n-\t\tif (tdcs_pages[i])\n-\t\t\t__free_page(tdcs_pages[i]);\n-\t}\n+\tfor (i = 0; i \u003c kvm_tdx-\u003etd.tdcs_nr_pages; i++)\n+\t\ttdx_free_control_page(tdcs_pages[i]);\n \tkfree(tdcs_pages);\n \tkvm_tdx-\u003etd.tdcs_pages = NULL;\n \n free_tdr:\n-\tif (tdr_page)\n-\t\t__free_page(tdr_page);\n+\ttdx_free_control_page(tdr_page);\n \tkvm_tdx-\u003etd.tdr_page = NULL;\n \n free_hkid:\n@@ -2948,7 +2987,7 @@ static int tdx_td_vcpu_init(struct kvm_vcpu *vcpu, u64 vcpu_rcx)\n \tint ret, i;\n \tu64 err;\n \n-\tpage = alloc_page(GFP_KERNEL_ACCOUNT);\n+\tpage = tdx_alloc_control_page();\n \tif (!page)\n \t\treturn -ENOMEM;\n \ttdx-\u003evp.tdvpr_page = page;\n@@ -2968,7 +3007,7 @@ static int tdx_td_vcpu_init(struct kvm_vcpu *vcpu, u64 vcpu_rcx)\n \t}\n \n \tfor (i = 0; i \u003c kvm_tdx-\u003etd.tdcx_nr_pages; i++) {\n-\t\tpage = alloc_page(GFP_KERNEL_ACCOUNT);\n+\t\tpage = tdx_alloc_control_page();\n \t\tif (!page) {\n \t\t\tret = -ENOMEM;\n \t\t\tgoto free_tdcx;\n@@ -2990,7 +3029,7 @@ static int tdx_td_vcpu_init(struct kvm_vcpu *vcpu, u64 vcpu_rcx)\n \t\t\t * method, but the rest are freed here.\n \t\t\t */\n \t\t\tfor (; i \u003c kvm_tdx-\u003etd.tdcx_nr_pages; i++) {\n-\t\t\t\t__free_page(tdx-\u003evp.tdcx_pages[i]);\n+\t\t\t\ttdx_free_control_page(tdx-\u003evp.tdcx_pages[i]);\n \t\t\t\ttdx-\u003evp.tdcx_pages[i] = NULL;\n \t\t\t}\n \t\t\treturn -EIO;\n@@ -3018,16 +3057,14 @@ static int tdx_td_vcpu_init(struct kvm_vcpu *vcpu, u64 vcpu_rcx)\n \n free_tdcx:\n \tfor (i = 0; i \u003c kvm_tdx-\u003etd.tdcx_nr_pages; i++) {\n-\t\tif (tdx-\u003evp.tdcx_pages[i])\n-\t\t\t__free_page(tdx-\u003evp.tdcx_pages[i]);\n+\t\ttdx_free_control_page(tdx-\u003evp.tdcx_pages[i]);\n \t\ttdx-\u003evp.tdcx_pages[i] = NULL;\n \t}\n \tkfree(tdx-\u003evp.tdcx_pages);\n \ttdx-\u003evp.tdcx_pages = NULL;\n \n free_tdvpr:\n-\tif (tdx-\u003evp.tdvpr_page)\n-\t\t__free_page(tdx-\u003evp.tdvpr_page);\n+\ttdx_free_control_page(tdx-\u003evp.tdvpr_page);\n \ttdx-\u003evp.tdvpr_page = NULL;\n \ttdx-\u003evp.tdvpr_pa = 0;\n \n@@ -3487,6 +3524,10 @@ int __init tdx_hardware_setup(void)\n \n \tvt_x86_ops.set_external_spte = tdx_sept_set_private_spte;\n \tvt_x86_ops.free_external_spt = tdx_sept_free_private_spt;\n+\n+\tif (tdx_supports_dynamic_pamt(tdx_sysinfo))\n+\t\tvt_x86_ops.topup_external_cache = tdx_topup_external_pamt_cache;\n+\n \tvt_x86_ops.protected_apic_has_interrupt = tdx_protected_apic_has_interrupt;\n \treturn 0;\n \ndiff --git a/arch/x86/kvm/vmx/tdx.h b/arch/x86/kvm/vmx/tdx.h\nindex ac8323a68b163..fd368e3ee060b 100644\n--- a/arch/x86/kvm/vmx/tdx.h\n+++ b/arch/x86/kvm/vmx/tdx.h\n@@ -72,6 +72,8 @@ struct vcpu_tdx {\n \n \tu64 map_gpa_next;\n \tu64 map_gpa_end;\n+\n+\tstruct tdx_pamt_cache pamt_cache;\n };\n \n void tdh_vp_rd_failed(struct vcpu_tdx *tdx, char *uclass, u32 field, u64 err);\ndiff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c\nindex 1b9ff749dd8e0..2acab6e5f5f87 100644\n--- a/arch/x86/virt/vmx/tdx/tdx.c\n+++ b/arch/x86/virt/vmx/tdx/tdx.c\n@@ -30,6 +30,7 @@\n #include \u003clinux/suspend.h\u003e\n #include \u003clinux/syscore_ops.h\u003e\n #include \u003clinux/idr.h\u003e\n+#include \u003clinux/vmalloc.h\u003e\n #include \u003casm/page.h\u003e\n #include \u003casm/special_insns.h\u003e\n #include \u003casm/msr-index.h\u003e\n@@ -46,6 +47,11 @@\n #include \"seamcall_internal.h\"\n #include \"tdx.h\"\n \n+/* Number of DPAMT pages to be provided to TDX module per 2MB region of PA */\n+#define TDX_DPAMT_ENTRY_PAGE_CNT 2\n+\n+static bool tdx_enable_dpamt __ro_after_init;\n+\n struct tdx_module_state {\n \tbool initialized;\n \tbool sysinit_done;\n@@ -63,6 +69,14 @@ static DEFINE_PER_CPU(bool, tdx_lp_initialized);\n \n static struct tdmr_info_list tdx_tdmr_list;\n \n+/*\n+ * On a machine with DPAMT, the kernel maintains a reference counter\n+ * for every 2MB range. The counter indicates how many users there are for\n+ * the DPAMT at the 2MB range. The kernel allocates DPAMT refcounts at\n+ * initialization.\n+ */\n+static atomic_t *dpamt_refcounts;\n+\n /* All TDX-usable memory regions.  Protected by mem_hotplug_lock. */\n static LIST_HEAD(tdx_memlist);\n \n@@ -252,6 +266,42 @@ static struct syscore tdx_syscore = {\n \t.ops = \u0026tdx_syscore_ops,\n };\n \n+/*\n+ * Allocate DPAMT reference counters for all physical memory.\n+ *\n+ * It consumes 2MB for every 1TB of physical memory.\n+ */\n+static __init int init_dpamt_refcounts(void)\n+{\n+\tsize_t size = DIV_ROUND_UP(max_pfn, PTRS_PER_PTE) * sizeof(*dpamt_refcounts);\n+\n+\tif (!tdx_supports_dynamic_pamt(\u0026tdx_sysinfo))\n+\t\treturn 0;\n+\n+\tdpamt_refcounts = vzalloc(size);\n+\tif (!dpamt_refcounts)\n+\t\treturn -ENOMEM;\n+\n+\treturn 0;\n+}\n+\n+static __init void free_dpamt_refcounts(void)\n+{\n+\tif (!tdx_supports_dynamic_pamt(\u0026tdx_sysinfo))\n+\t\treturn;\n+\n+\tvfree(dpamt_refcounts);\n+\tdpamt_refcounts = NULL;\n+}\n+\n+static atomic_t *tdx_find_dpamt_refcount(unsigned long pfn)\n+{\n+\t/* Find which PMD a PFN is in. */\n+\tunsigned long index = pfn \u003e\u003e (PMD_SHIFT - PAGE_SHIFT);\n+\n+\treturn \u0026dpamt_refcounts[index];\n+}\n+\n /*\n  * Add a memory region as a TDX memory block.  The caller must make sure\n  * all memory regions are added in address ascending order and don't\n@@ -510,35 +560,37 @@ static __init int fill_out_tdmrs(struct list_head *tmb_list,\n \treturn 0;\n }\n \n+static __init unsigned long tdmr_get_pamt_bitmap_sz(struct tdmr_info *tdmr)\n+{\n+\tunsigned long pamt_sz, nr_pamt_entries;\n+\tint bits_per_entry;\n+\n+\tbits_per_entry = tdx_sysinfo.tdmr.pamt_page_bitmap_entry_bits;\n+\tnr_pamt_entries = tdmr-\u003esize \u003e\u003e PAGE_SHIFT;\n+\tpamt_sz = DIV_ROUND_UP(nr_pamt_entries * bits_per_entry, BITS_PER_BYTE);\n+\n+\treturn PAGE_ALIGN(pamt_sz);\n+}\n+\n /*\n  * Calculate PAMT size given a TDMR and a page size.  The returned\n  * PAMT size is always aligned up to 4K page boundary.\n  */\n-static __init unsigned long tdmr_get_pamt_sz(struct tdmr_info *tdmr, int pgsz,\n-\t\t\t\t\t     u16 pamt_entry_size)\n+static __init unsigned long tdmr_get_pamt_sz(struct tdmr_info *tdmr, int pgsz)\n {\n \tunsigned long pamt_sz, nr_pamt_entries;\n+\tconst int tdx_pg_size_shift[TDX_PS_NR] = { PAGE_SHIFT, PMD_SHIFT, PUD_SHIFT };\n+\tconst u16 pamt_entry_size[TDX_PS_NR] = {\n+\t\ttdx_sysinfo.tdmr.pamt_4k_entry_size,\n+\t\ttdx_sysinfo.tdmr.pamt_2m_entry_size,\n+\t\ttdx_sysinfo.tdmr.pamt_1g_entry_size,\n+\t};\n \n-\tswitch (pgsz) {\n-\tcase TDX_PS_4K:\n-\t\tnr_pamt_entries = tdmr-\u003esize \u003e\u003e PAGE_SHIFT;\n-\t\tbreak;\n-\tcase TDX_PS_2M:\n-\t\tnr_pamt_entries = tdmr-\u003esize \u003e\u003e PMD_SHIFT;\n-\t\tbreak;\n-\tcase TDX_PS_1G:\n-\t\tnr_pamt_entries = tdmr-\u003esize \u003e\u003e PUD_SHIFT;\n-\t\tbreak;\n-\tdefault:\n-\t\tWARN_ON_ONCE(1);\n-\t\treturn 0;\n-\t}\n+\tnr_pamt_entries = tdmr-\u003esize \u003e\u003e tdx_pg_size_shift[pgsz];\n+\tpamt_sz = nr_pamt_entries * pamt_entry_size[pgsz];\n \n-\tpamt_sz = nr_pamt_entries * pamt_entry_size;\n \t/* TDX requires PAMT size must be 4K aligned */\n-\tpamt_sz = ALIGN(pamt_sz, PAGE_SIZE);\n-\n-\treturn pamt_sz;\n+\treturn PAGE_ALIGN(pamt_sz);\n }\n \n /*\n@@ -576,15 +628,11 @@ static __init int tdmr_get_nid(struct tdmr_info *tdmr, struct list_head *tmb_lis\n  * within @tdmr, and set up PAMTs for @tdmr.\n  */\n static __init int tdmr_set_up_pamt(struct tdmr_info *tdmr,\n-\t\t\t\t   struct list_head *tmb_list,\n-\t\t\t\t   u16 pamt_entry_size[])\n+\t\t\t\t   struct list_head *tmb_list)\n {\n-\tunsigned long pamt_base[TDX_PS_NR];\n-\tunsigned long pamt_size[TDX_PS_NR];\n-\tunsigned long tdmr_pamt_base;\n \tunsigned long tdmr_pamt_size;\n \tstruct page *pamt;\n-\tint pgsz, nid;\n+\tint nid;\n \n \tnid = tdmr_get_nid(tdmr, tmb_list);\n \n@@ -592,13 +640,18 @@ static __init int tdmr_set_up_pamt(struct tdmr_info *tdmr,\n \t * Calculate the PAMT size for each TDX supported page size\n \t * and the total PAMT size.\n \t */\n-\ttdmr_pamt_size = 0;\n-\tfor (pgsz = TDX_PS_4K; pgsz \u003c TDX_PS_NR; pgsz++) {\n-\t\tpamt_size[pgsz] = tdmr_get_pamt_sz(tdmr, pgsz,\n-\t\t\t\t\tpamt_entry_size[pgsz]);\n-\t\ttdmr_pamt_size += pamt_size[pgsz];\n+\ttdmr-\u003epamt_1g_size = tdmr_get_pamt_sz(tdmr, TDX_PS_1G);\n+\ttdmr-\u003epamt_2m_size = tdmr_get_pamt_sz(tdmr, TDX_PS_2M);\n+\n+\tif (tdx_supports_dynamic_pamt(\u0026tdx_sysinfo)) {\n+\t\t/* With DPAMT, PAMT_4K is replaced with a bitmap */\n+\t\ttdmr-\u003epamt_4k_size = tdmr_get_pamt_bitmap_sz(tdmr);\n+\t} else {\n+\t\ttdmr-\u003epamt_4k_size = tdmr_get_pamt_sz(tdmr, TDX_PS_4K);\n \t}\n \n+\ttdmr_pamt_size = tdmr-\u003epamt_4k_size + tdmr-\u003epamt_2m_size + tdmr-\u003epamt_1g_size;\n+\n \t/*\n \t * Allocate one chunk of physically contiguous memory for all\n \t * PAMTs.  This helps minimize the PAMT's use of reserved areas\n@@ -606,25 +659,17 @@ static __init int tdmr_set_up_pamt(struct tdmr_info *tdmr,\n \t */\n \tpamt = alloc_contig_pages(tdmr_pamt_size \u003e\u003e PAGE_SHIFT, GFP_KERNEL,\n \t\t\tnid, \u0026node_online_map);\n-\tif (!pamt)\n-\t\treturn -ENOMEM;\n \n \t/*\n-\t * Break the contiguous allocation back up into the\n-\t * individual PAMTs for each page size.\n+\t * tdmr-\u003epamt_4k_base is still zero so the error\n+\t * path of the caller will skip freeing the PAMT.\n \t */\n-\ttdmr_pamt_base = page_to_pfn(pamt) \u003c\u003c PAGE_SHIFT;\n-\tfor (pgsz = TDX_PS_4K; pgsz \u003c TDX_PS_NR; pgsz++) {\n-\t\tpamt_base[pgsz] = tdmr_pamt_base;\n-\t\ttdmr_pamt_base += pamt_size[pgsz];\n-\t}\n+\tif (!pamt)\n+\t\treturn -ENOMEM;\n \n-\ttdmr-\u003epamt_4k_base = pamt_base[TDX_PS_4K];\n-\ttdmr-\u003epamt_4k_size = pamt_size[TDX_PS_4K];\n-\ttdmr-\u003epamt_2m_base = pamt_base[TDX_PS_2M];\n-\ttdmr-\u003epamt_2m_size = pamt_size[TDX_PS_2M];\n-\ttdmr-\u003epamt_1g_base = pamt_base[TDX_PS_1G];\n-\ttdmr-\u003epamt_1g_size = pamt_size[TDX_PS_1G];\n+\ttdmr-\u003epamt_4k_base = page_to_phys(pamt);\n+\ttdmr-\u003epamt_2m_base = tdmr-\u003epamt_4k_base + tdmr-\u003epamt_4k_size;\n+\ttdmr-\u003epamt_1g_base = tdmr-\u003epamt_2m_base + tdmr-\u003epamt_2m_size;\n \n \treturn 0;\n }\n@@ -655,10 +700,7 @@ static __init void tdmr_do_pamt_func(struct tdmr_info *tdmr,\n \ttdmr_get_pamt(tdmr, \u0026pamt_base, \u0026pamt_size);\n \n \t/* Do nothing if PAMT hasn't been allocated for this TDMR */\n-\tif (!pamt_size)\n-\t\treturn;\n-\n-\tif (WARN_ON_ONCE(!pamt_base))\n+\tif (!pamt_base)\n \t\treturn;\n \n \tpamt_func(pamt_base, pamt_size);\n@@ -684,14 +726,12 @@ static __init void tdmrs_free_pamt_all(struct tdmr_info_list *tdmr_list)\n \n /* Allocate and set up PAMTs for all TDMRs */\n static __init int tdmrs_set_up_pamt_all(struct tdmr_info_list *tdmr_list,\n-\t\t\t\t\tstruct list_head *tmb_list,\n-\t\t\t\t\tu16 pamt_entry_size[])\n+\t\t\t\t\tstruct list_head *tmb_list)\n {\n \tint i, ret = 0;\n \n \tfor (i = 0; i \u003c tdmr_list-\u003enr_consumed_tdmrs; i++) {\n-\t\tret = tdmr_set_up_pamt(tdmr_entry(tdmr_list, i), tmb_list,\n-\t\t\t\tpamt_entry_size);\n+\t\tret = tdmr_set_up_pamt(tdmr_entry(tdmr_list, i), tmb_list);\n \t\tif (ret)\n \t\t\tgoto err;\n \t}\n@@ -968,18 +1008,13 @@ static __init int construct_tdmrs(struct list_head *tmb_list,\n \t\t\t\t  struct tdmr_info_list *tdmr_list,\n \t\t\t\t  struct tdx_sys_info_tdmr *sysinfo_tdmr)\n {\n-\tu16 pamt_entry_size[TDX_PS_NR] = {\n-\t\tsysinfo_tdmr-\u003epamt_4k_entry_size,\n-\t\tsysinfo_tdmr-\u003epamt_2m_entry_size,\n-\t\tsysinfo_tdmr-\u003epamt_1g_entry_size,\n-\t};\n \tint ret;\n \n \tret = fill_out_tdmrs(tmb_list, tdmr_list);\n \tif (ret)\n \t\treturn ret;\n \n-\tret = tdmrs_set_up_pamt_all(tdmr_list, tmb_list, pamt_entry_size);\n+\tret = tdmrs_set_up_pamt_all(tdmr_list, tmb_list);\n \tif (ret)\n \t\treturn ret;\n \n@@ -998,6 +1033,8 @@ static __init int construct_tdmrs(struct list_head *tmb_list,\n \treturn ret;\n }\n \n+#define TDX_SYS_CONFIG_DYNAMIC_PAMT\tBIT(16)\n+\n static __init int config_tdx_module(struct tdmr_info_list *tdmr_list,\n \t\t\t\t    u64 global_keyid)\n {\n@@ -1026,6 +1063,12 @@ static __init int config_tdx_module(struct tdmr_info_list *tdmr_list,\n \targs.rcx = __pa(tdmr_pa_array);\n \targs.rdx = tdmr_list-\u003enr_consumed_tdmrs;\n \targs.r8 = global_keyid;\n+\n+\tif (tdx_supports_dynamic_pamt(\u0026tdx_sysinfo)) {\n+\t\tpr_info(\"Enable Dynamic PAMT\\n\");\n+\t\targs.r8 |= TDX_SYS_CONFIG_DYNAMIC_PAMT;\n+\t}\n+\n \tret = seamcall_prerr(TDH_SYS_CONFIG, \u0026args);\n \n \t/* Free the array as it is not required anymore. */\n@@ -1167,10 +1210,14 @@ static __init int init_tdx_module(void)\n \t */\n \tget_online_mems();\n \n-\tret = build_tdx_memlist(\u0026tdx_memlist);\n+\tret = init_dpamt_refcounts();\n \tif (ret)\n \t\tgoto out_put_tdxmem;\n \n+\tret = build_tdx_memlist(\u0026tdx_memlist);\n+\tif (ret)\n+\t\tgoto err_free_dpamt_refcounts;\n+\n \t/* Allocate enough space for constructing TDMRs */\n \tret = alloc_tdmr_list(\u0026tdx_tdmr_list, \u0026tdx_sysinfo.tdmr);\n \tif (ret)\n@@ -1220,6 +1267,8 @@ static __init int init_tdx_module(void)\n \tfree_tdmr_list(\u0026tdx_tdmr_list);\n err_free_tdxmem:\n \tfree_tdx_memlist(\u0026tdx_memlist);\n+err_free_dpamt_refcounts:\n+\tfree_dpamt_refcounts();\n \tgoto out_put_tdxmem;\n }\n \n@@ -2005,6 +2054,293 @@ u64 tdh_phymem_page_wbinvd_hkid(u64 hkid, kvm_pfn_t pfn)\n }\n EXPORT_SYMBOL_FOR_KVM(tdh_phymem_page_wbinvd_hkid);\n \n+bool tdx_supports_dynamic_pamt(const struct tdx_sys_info *sysinfo)\n+{\n+\tif (!tdx_enable_dpamt)\n+\t\treturn false;\n+\n+\treturn sysinfo-\u003efeatures.tdx_features0 \u0026 TDX_FEATURES0_DYNAMIC_PAMT;\n+}\n+EXPORT_SYMBOL_FOR_KVM(tdx_supports_dynamic_pamt);\n+\n+static struct page *tdx_alloc_page_pamt_cache(struct tdx_pamt_cache *cache)\n+{\n+\tstruct page *page;\n+\n+\tpage = list_first_entry_or_null(\u0026cache-\u003epage_list, struct page, lru);\n+\tif (page) {\n+\t\tlist_del(\u0026page-\u003elru);\n+\t\tcache-\u003ecnt--;\n+\t}\n+\n+\treturn page;\n+}\n+\n+static struct page *alloc_dpamt_page(struct tdx_pamt_cache *cache)\n+{\n+\tif (cache)\n+\t\treturn tdx_alloc_page_pamt_cache(cache);\n+\n+\treturn alloc_page(GFP_KERNEL_ACCOUNT);\n+}\n+\n+static int alloc_pamt_array(struct page **pamt_pages, struct tdx_pamt_cache *cache)\n+{\n+\tint i, j;\n+\n+\tfor (i = 0; i \u003c TDX_DPAMT_ENTRY_PAGE_CNT; i++) {\n+\t\tpamt_pages[i] = alloc_dpamt_page(cache);\n+\t\tif (!pamt_pages[i])\n+\t\t\tgoto err;\n+\t}\n+\n+\treturn 0;\n+\n+err:\n+\tfor (j = 0; j \u003c i; j++)\n+\t\t__free_page(pamt_pages[j]);\n+\n+\treturn -ENOMEM;\n+}\n+\n+static void free_pamt_array(struct page **pamt_pages)\n+{\n+\tint i;\n+\n+\tfor (i = 0; i \u003c TDX_DPAMT_ENTRY_PAGE_CNT; i++) {\n+\t\t/*\n+\t\t * Reset pages unconditionally to cover cases\n+\t\t * where they were passed to the TDX module.\n+\t\t */\n+\t\ttdx_quirk_reset_paddr(page_to_phys(pamt_pages[i]), PAGE_SIZE);\n+\n+\t\t__free_page(pamt_pages[i]);\n+\t}\n+}\n+\n+/* Helper for building DPAMT seamcall() arguments. */\n+static u64 pamt_2mb_arg(kvm_pfn_t pfn)\n+{\n+\t/* Find the 2MB-wide DPAMT region for 'pfn': */\n+\tunsigned long hpa_2mb = ALIGN_DOWN(pfn \u003c\u003c PAGE_SHIFT, PMD_SIZE);\n+\n+\t/*\n+\t * TDX ABI requires specifying the page level the installed DPAMT\n+\t * backing will cover, even though today only 2MB is supported.\n+\t */\n+\treturn hpa_2mb | TDX_PS_2M;\n+}\n+\n+/* Add DPAMT backing for the 2MB region surrounding the given pfn. */\n+static u64 tdh_phymem_pamt_add(kvm_pfn_t pfn, struct page **pamt_pages)\n+{\n+\tstruct tdx_module_args args = {\n+\t\t.rcx = pamt_2mb_arg(pfn),\n+\t\t.rdx = page_to_phys(pamt_pages[0]),\n+\t\t.r8  = page_to_phys(pamt_pages[1]),\n+\t};\n+\n+\treturn seamcall(TDH_PHYMEM_PAMT_ADD, \u0026args);\n+}\n+\n+/* Remove DPAMT backing for the 2MB region surrounding the given pfn. */\n+static u64 tdh_phymem_pamt_remove(kvm_pfn_t pfn, struct page **pamt_pages)\n+{\n+\tstruct tdx_module_args args = {\n+\t\t.rcx = pamt_2mb_arg(pfn),\n+\t};\n+\tu64 ret;\n+\n+\tret = seamcall_ret(TDH_PHYMEM_PAMT_REMOVE, \u0026args);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\t/* Copy PAMT pages out of the struct per the TDX ABI */\n+\tpamt_pages[0] = phys_to_page(args.rdx);\n+\tpamt_pages[1] = phys_to_page(args.r8);\n+\n+\treturn 0;\n+}\n+\n+/* Serializes adding/removing DPAMT memory */\n+static DEFINE_SPINLOCK(dpamt_lock);\n+\n+/* Bump DPAMT refcount for the given pfn and allocate DPAMT backing if needed. */\n+int tdx_pamt_get(kvm_pfn_t pfn, struct tdx_pamt_cache *cache)\n+{\n+\tstruct page *pamt_pages[TDX_DPAMT_ENTRY_PAGE_CNT];\n+\tatomic_t *dpamt_refcount;\n+\tu64 tdx_status;\n+\tint ret;\n+\n+\tif (!tdx_supports_dynamic_pamt(\u0026tdx_sysinfo))\n+\t\treturn 0;\n+\n+\tdpamt_refcount = tdx_find_dpamt_refcount(pfn);\n+\n+\t/*\n+\t * If the DPAMT entry is already added (i.e. refcount \u003e= 1),\n+\t * then just increment the refcount.\n+\t */\n+\tif (atomic_inc_not_zero(dpamt_refcount))\n+\t\treturn 0;\n+\n+\tret = alloc_pamt_array(pamt_pages, cache);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\tspin_lock(\u0026dpamt_lock);\n+\n+\t/*\n+\t * Unlike tdx_pamt_put() which uses atomic_dec_and_lock() to\n+\t * atomically handle the 1-\u003e0 transition, the get side has no\n+\t * equivalent combined primitive for 0-\u003e1. Recheck under the\n+\t * lock since another get may have already done the 0-\u003e1\n+\t * transition after both saw atomic_inc_not_zero() fail.\n+\t */\n+\tif (atomic_inc_not_zero(dpamt_refcount))\n+\t\tgoto out_free;\n+\n+\ttdx_status = tdh_phymem_pamt_add(pfn, pamt_pages);\n+\tif (WARN_ON_ONCE(tdx_status != TDX_SUCCESS)) {\n+\t\tret = -EIO;\n+\t\tgoto out_free;\n+\t}\n+\n+\t/*\n+\t * The refcount is zero, and this locked path is the\n+\t * only way to increase it from 0-\u003e1.\n+\t */\n+\tatomic_set(dpamt_refcount, 1);\n+\tspin_unlock(\u0026dpamt_lock);\n+\treturn 0;\n+\n+out_free:\n+\tspin_unlock(\u0026dpamt_lock);\n+\tfree_pamt_array(pamt_pages);\n+\n+\treturn ret;\n+}\n+EXPORT_SYMBOL_FOR_KVM(tdx_pamt_get);\n+\n+/* Drop DPAMT refcount for the given pfn and free DPAMT backing if needed. */\n+void tdx_pamt_put(kvm_pfn_t pfn)\n+{\n+\tstruct page *pamt_pages[TDX_DPAMT_ENTRY_PAGE_CNT] = {};\n+\tatomic_t *dpamt_refcount;\n+\tu64 tdx_status;\n+\n+\tif (!tdx_supports_dynamic_pamt(\u0026tdx_sysinfo))\n+\t\treturn;\n+\n+\tdpamt_refcount = tdx_find_dpamt_refcount(pfn);\n+\n+\t/*\n+\t * If there is more than 1 reference on the DPAMT entry, don't\n+\t * remove it yet. Just decrement the refcount.\n+\t */\n+\tif (!atomic_dec_and_lock(dpamt_refcount, \u0026dpamt_lock))\n+\t\treturn;\n+\n+\ttdx_status = tdh_phymem_pamt_remove(pfn, pamt_pages);\n+\n+\t/*\n+\t * Don't free pamt_pages as it could hold garbage when\n+\t * tdh_phymem_pamt_remove() fails.  Don't panic/BUG_ON(), as\n+\t * there is no risk of data corruption, but do yell loudly as\n+\t * failure indicates a kernel bug, memory is being leaked, and\n+\t * the dangling DPAMT entry may cause future operations to fail.\n+\t */\n+\tif (WARN_ON_ONCE(tdx_status != TDX_SUCCESS)) {\n+\t\t/*\n+\t\t * atomic_dec_and_lock() already decremented it to 0,\n+\t\t * but the DPAMT entry still exists since REMOVE failed.\n+\t\t */\n+\t\tatomic_set(dpamt_refcount, 1);\n+\t\tgoto out_unlock;\n+\t}\n+\n+\tspin_unlock(\u0026dpamt_lock);\n+\tfree_pamt_array(pamt_pages);\n+\treturn;\n+out_unlock:\n+\tspin_unlock(\u0026dpamt_lock);\n+}\n+EXPORT_SYMBOL_FOR_KVM(tdx_pamt_put);\n+\n+void tdx_free_pamt_cache(struct tdx_pamt_cache *cache)\n+{\n+\tstruct page *page;\n+\n+\twhile ((page = tdx_alloc_page_pamt_cache(cache)))\n+\t\t__free_page(page);\n+}\n+EXPORT_SYMBOL_FOR_KVM(tdx_free_pamt_cache);\n+\n+int tdx_topup_pamt_cache(struct tdx_pamt_cache *cache, unsigned long npages)\n+{\n+\tif (WARN_ON_ONCE(!tdx_supports_dynamic_pamt(\u0026tdx_sysinfo)))\n+\t\treturn 0;\n+\n+\tnpages *= TDX_DPAMT_ENTRY_PAGE_CNT;\n+\n+\twhile (cache-\u003ecnt \u003c npages) {\n+\t\tstruct page *page = alloc_page(GFP_KERNEL_ACCOUNT);\n+\n+\t\tif (!page)\n+\t\t\treturn -ENOMEM;\n+\n+\t\tlist_add(\u0026page-\u003elru, \u0026cache-\u003epage_list);\n+\t\tcache-\u003ecnt++;\n+\t}\n+\n+\treturn 0;\n+}\n+EXPORT_SYMBOL_FOR_KVM(tdx_topup_pamt_cache);\n+\n+/*\n+ * Return a page that can be gifted to the TDX module for use as a \"control\"\n+ * page, i.e. pages that are used for control structures for a given TDX\n+ * guest, and thus obtain TDX protections, including DPAMT tracking.\n+ */\n+struct page *tdx_alloc_control_page(void)\n+{\n+\tstruct page *page;\n+\n+\tpage = alloc_page(GFP_KERNEL_ACCOUNT);\n+\tif (!page)\n+\t\treturn NULL;\n+\n+\tif (tdx_pamt_get(page_to_pfn(page), NULL)) {\n+\t\t__free_page(page);\n+\t\treturn NULL;\n+\t}\n+\n+\treturn page;\n+}\n+EXPORT_SYMBOL_FOR_KVM(tdx_alloc_control_page);\n+\n+/*\n+ * Free a page that was gifted to the TDX module for use as a control\n+ * page. After this, the page is no longer protected by TDX.\n+ */\n+void tdx_free_control_page(struct page *page)\n+{\n+\tif (!page)\n+\t\treturn;\n+\n+\ttdx_pamt_put(page_to_pfn(page));\n+\t__free_page(page);\n+}\n+EXPORT_SYMBOL_FOR_KVM(tdx_free_control_page);\n+\n+static int __init tdx_dpamt_setup(char *str)\n+{\n+\treturn kstrtobool(str, \u0026tdx_enable_dpamt) == 0;\n+}\n+\n+__setup(\"tdx_dpamt=\", tdx_dpamt_setup);\n+\n void tdx_sys_disable(void)\n {\n \tstruct tdx_module_args args = {};\ndiff --git a/arch/x86/virt/vmx/tdx/tdx.h b/arch/x86/virt/vmx/tdx/tdx.h\nindex bdfd0e1e337ac..a886c54decaad 100644\n--- a/arch/x86/virt/vmx/tdx/tdx.h\n+++ b/arch/x86/virt/vmx/tdx/tdx.h\n@@ -48,6 +48,8 @@\n #define TDH_SYS_CONFIG\t\t\t45\n #define TDH_SYS_SHUTDOWN\t\t52\n #define TDH_SYS_UPDATE\t\t\t53\n+#define TDH_PHYMEM_PAMT_ADD\t\t58\n+#define TDH_PHYMEM_PAMT_REMOVE\t\t59\n #define TDH_SYS_DISABLE\t\t\t69\n \n /*\ndiff --git a/arch/x86/virt/vmx/tdx/tdx_global_metadata.c b/arch/x86/virt/vmx/tdx/tdx_global_metadata.c\nindex e49c300f23d43..98ebf17aab1ce 100644\n--- a/arch/x86/virt/vmx/tdx/tdx_global_metadata.c\n+++ b/arch/x86/virt/vmx/tdx/tdx_global_metadata.c\n@@ -1,6 +1,6 @@\n // SPDX-License-Identifier: GPL-2.0\n /*\n- * Automatically generated functions to read TDX global metadata.\n+ * Functions to read TDX global metadata.\n  *\n  * This file doesn't compile on its own as it lacks of inclusion\n  * of SEAMCALL wrapper primitive which reads global metadata.\n@@ -33,6 +33,18 @@ static __init int get_tdx_sys_info_features(struct tdx_sys_info_features *sysinf\n \treturn ret;\n }\n \n+static __init int get_tdx_sys_info_tdmr_dpamt(struct tdx_sys_info_tdmr *sysinfo_tdmr)\n+{\n+\tint ret;\n+\tu64 val;\n+\n+\tret = read_sys_metadata_field(0x9100000000000013, \u0026val);\n+\tif (!ret)\n+\t\tsysinfo_tdmr-\u003epamt_page_bitmap_entry_bits = val;\n+\n+\treturn ret;\n+}\n+\n static __init int get_tdx_sys_info_tdmr(struct tdx_sys_info_tdmr *sysinfo_tdmr)\n {\n \tint ret = 0;\n@@ -129,5 +141,14 @@ static __init int get_tdx_sys_info(struct tdx_sys_info *sysinfo)\n \tret = ret ?: get_tdx_sys_info_td_ctrl(\u0026sysinfo-\u003etd_ctrl);\n \tret = ret ?: get_tdx_sys_info_td_conf(\u0026sysinfo-\u003etd_conf);\n \n+\t/*\n+\t * The kernel supports using TDX without DPAMT, so\n+\t * avoid reporting failure if it's not supported. Don't\n+\t * try to support buggy TDX modules that advertise\n+\t * DPAMT but don't expose the metadata.\n+\t */\n+\tif (!ret \u0026\u0026 tdx_supports_dynamic_pamt(sysinfo))\n+\t\tret = get_tdx_sys_info_tdmr_dpamt(\u0026sysinfo-\u003etdmr);\n+\n \treturn ret;\n }\n"
}

2/1 2026/09/04 02:05 agent patch-evaluator
1m Model:
core

Args:
null

Results:
{
  "EnableConfigs": null,
  "FocusSymbols": [],
  "Reasoning": "The patch series introduces Dynamic PAMT (DPAMT) support for Intel TDX (Trust Domain Extensions) host in the kernel and KVM. The modified code is entirely confined to Intel TDX host initialization and KVM TDX guest management (under arch/x86/virt/vmx/tdx and arch/x86/kvm/vmx/tdx.c, plus mirrored TDP handling guarded by KVM_X86_TDX_VM). These paths strictly require physical Intel CPUs with TDX host platform support (X86_FEATURE_TDX_HOST_PLATFORM, MSR_IA32_MKTME_KEYID_PARTITIONING, SEAMRR, and the Intel TDX firmware module), which are not present or emulated in standard QEMU/GCE fuzzing environments. Consequently, the modified code is structurally unreachable for fuzzing.",
  "WorthFuzzing": false
}

Instruction:
You are an expert Linux kernel maintainer and security engineer.
Your job is to review a provided patch series and evaluate whether it warrants fuzzing with syzkaller.

IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in
your workspace. Do NOT rely on internal assumptions. You must actively use your code access
tools to inspect the actual source code, callers, and surrounding context.

================================================================================
1. CORE TRIAGE PHILOSOPHY
================================================================================
The goal of patch fuzzing is to discover crashes, regressions, exposed latent bugs,
and newly triggered assertions introduced by the patch series.

- REACHABILITY IS THE PRIMARY GATE:
  Fuzzing can only discover bugs in code that can actually execute in standard virtualized
  environments (GCE or QEMU, utilizing software-emulated devices like USB gadgets, netdev, tun/tap).
  If the modified code is structurally unreachable (see Section 2), it MUST NOT be fuzzed,
  regardless of whether it adds assertions or complex logic.

- DO NOT BLINDLY TRUST "NO FUNCTIONAL CHANGE" (NFCI) OR "REFACTORING" CLAIMS:
  Patch authors routinely label changes as "cleanups", "refactorings", or state
  "No functional change intended". Do NOT take these claims at face value.
  Code refactorings that rearrange logic, introduce helper functions, or alter state management
  in core subsystems frequently introduce subtle semantic shifts or uncover latent kernel bugs.
  If reachable executable code is modified or refactored, it MUST be fuzzed.

- NEW OR MODIFIED ASSERTIONS IN REACHABLE CODE MUST BE FUZZED:
  When a patch introduces or modifies runtime checks or assertions (e.g., WARN_ON*, VM_WARN_ON*,
  BUG_ON*, lockdep_assert*) in reachable code paths, it enforces new or stricter invariants.
  Even if the author believes the invariant always holds, fuzzing is essential to verify whether
  an unusual sequence of operations can violate it.

================================================================================
2. WHEN TO RETURN WorthFuzzing=false (NEGATIVE CRITERIA)
================================================================================
Return WorthFuzzing=false ONLY IF all modified code falls strictly into one or more of these categories:

- Non-kernel and non-executable changes:
  * Modifications to Documentation/, comments, or spelling fixes.
  * User-space directories, self-tests, samples, or scripts (e.g., tools/, samples/, scripts/, usr/)
    that do not affect the compiled kernel image (vmlinux) or kernel modules.
  * Purely decorative logging (e.g., message strings in pr_err, printk, dev_info) or tracepoints
    that do not alter control flow or data structures.
  * Build system or Kconfig changes that do not alter compiled C logic.
- Structurally unreachable hardware:
  * Vendor-specific PCIe switches, SmartNICs, or GPU drivers (e.g., mlxsw, pds_core, qed,
    ionic, amdgpu) requiring physical ASIC/PCIe cards not emulated in standard QEMU.
- Unreachable execution paths:
  * Driver teardown callbacks (.remove, .shutdown, pci_unregister_driver) executed only during
    physical PCI hot-unplug or manual sysfs driver unbinding.
  * Code paths exclusive to architectures other than the target architecture.

================================================================================
3. WHEN TO RETURN WorthFuzzing=true (POSITIVE CRITERIA)
================================================================================
Return WorthFuzzing=true whenever the patch touches reachable executable code, including:
- Core Subsystems:
  * Any logic modifications in memory management (mm/), synchronization/locking (kernel/locking/),
    BPF, scheduler, core networking, VFS, or syscall handling.
- Refactorings and Code Cleanups:
  * Any restructuring of reachable data structures, helper abstractions, or algorithm flows.
- Runtime Assertions and Defensive Checks:
  * Any introduction or alteration of assertions (WARN_ON*, VM_WARN_ON*, BUG_ON*, etc.) in reachable paths.
- Reachable Drivers and Protocols:
  * Drivers accessible via virtual buses (virtio, USB gadget, loopback, netlink, binder, sockets, etc.).

================================================================================
4. EXTRACTING FocusSymbols (PREVENTING DILUTION)
================================================================================
When WorthFuzzing=true, you must extract specific kernel functions into FocusSymbols to guide the fuzzer:

- AVOID UBIQUITOUS LIFECYCLE HOT-PATHS:
  Do NOT list generic, ubiquitous functions called by almost every program in the corpus
  (including, but not limited to: general memory allocators and deallocators, page fault
  and trap handlers, or core synchronization primitives; this is not an exhaustive list).
  Listing ubiquitous functions causes the fuzzer to classify thousands of unrelated tests as "focused",
  which severely dilutes fuzzing effort away from the actual changes.

- TARGET SPECIFIC FEATURE LOGIC AND ENTRYPOINTS:
  List functions that specifically implement the logic being added or altered, or direct API entrypoints
  for the subsystem feature under review.

- HANDLING STATIC INLINE FUNCTIONS IN HEADERS (.h):
  Compiler-inlined static functions (such as static inlines in mm/*.h or include/linux/*.h) lack
  distinct symbol addresses in vmlinux and cannot be targeted directly by symbol coverage filters.
  If the changes are primarily in static inline helpers, identify non-static, feature-specific caller
  functions in .c files that exercise them (avoiding ubiquitous lifecycle wrappers).

================================================================================
5. IDENTIFYING EnableConfigs
================================================================================
Identify any specific CONFIG_ options required to properly compile and reach the modified code:
- Inspect Kconfig files and #ifdef guards; do not make assumptions.
- Check "depends on" lines in Kconfig to include any non-standard parent subsystem configs needed.
- Strip any 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4").
Prefer calling several tools at the same time to save round-trips.


Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.

Prompt:
Target architecture: amd64

For your convenience, here is the diff of the changes:
commit 6f3a5eaba903c2124d8fc6ad6755285ce8dfd5cc
Author: syz-cluster <triage@syzkaller.com>
Date:   Fri Sep 4 02:05:52 2026 +0000

    syz-cluster: applied patch under review

diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 68647ff4bdd24..e7558b8382acf 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -7582,6 +7582,16 @@ Kernel parameters
 
 	tdfx=		[HW,DRM]
 
+	tdx_dpamt=
+			[X86] Controls whether TDX will use Dynamic PAMT
+			to save memory, when supported.
+
+			Valid parameters: "on", "off"
+			Default: "off"
+
+			For details see:
+			Documentation/arch/x86/tdx.rst
+
 	test_suspend=	[SUSPEND]
 			Format: { "mem" | "standby" | "freeze" }[,N]
 			Specify "mem" (for Suspend-to-RAM) or "standby" (for
diff --git a/Documentation/arch/x86/tdx.rst b/Documentation/arch/x86/tdx.rst
index 3303499ad4c6f..de09967ce8d8c 100644
--- a/Documentation/arch/x86/tdx.rst
+++ b/Documentation/arch/x86/tdx.rst
@@ -200,6 +200,33 @@ reflects the TCB of the currently running TDX module and therefore
 changes after an update. By contrast, TEE_TCB_SVN reflects the TCB at TD
 launch time and is not affected.
 
+Dynamic PAMT
+------------
+
+The Physical Address Metadata Table (PAMT) is metadata in which the TDX
+module keeps data about each physical page (think struct page). Space
+for it is allocated by the VMM, consumes up to about 0.4% of system
+memory and needs to be supplied to the TDX module when the TDX module is
+first loaded.
+
+Dynamic PAMT is an add-on feature that allows a VMM to dynamically
+allocate the part of the PAMT which tracks 4KB pages. This reduces the
+amount of memory that TDX consumes while TDs are not in use.
+
+When Dynamic PAMT is in use, dmesg shows it like::
+
+  [..] virt/tdx: Enable Dynamic PAMT
+  [..] virt/tdx: 10092 KB allocated for PAMT
+  [..] virt/tdx: TDX-Module initialized
+
+Dynamic PAMT is only enabled when supported and the ``tdx_dpamt=`` kernel
+parameter is set to "on". The feature is off by default because TDX module
+internal details prevent Dynamic PAMT from working on all keyid partitioning
+configurations. When the TDX module is fixed to include these constraints in
+its enumeration of Dynamic PAMT support, kernel support can be changed to
+default on. For more information, consult the Intel TDX documentation about
+Dynamic PAMT.
+
 TDX Interaction to Other Kernel Components
 ------------------------------------------
 
diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h
index e213c9ae3e301..5c358c40eae8a 100644
--- a/arch/x86/include/asm/kvm-x86-ops.h
+++ b/arch/x86/include/asm/kvm-x86-ops.h
@@ -99,6 +99,7 @@ KVM_X86_OP_OPTIONAL_RET0(tdp_has_smep)
 KVM_X86_OP(load_mmu_pgd)
 KVM_X86_OP_OPTIONAL_RET0(set_external_spte)
 KVM_X86_OP_OPTIONAL(free_external_spt)
+KVM_X86_OP_OPTIONAL_RET0(topup_external_cache)
 KVM_X86_OP(has_wbinvd_exit)
 KVM_X86_OP(get_l2_tsc_offset)
 KVM_X86_OP(get_l2_tsc_multiplier)
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 683bb8bf43a94..57d37491c7c09 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -1644,6 +1644,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);
 
 	bool (*has_wbinvd_exit)(void);
 
diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h
index 89e97d5761d89..e186dfe5bf885 100644
--- a/arch/x86/include/asm/tdx.h
+++ b/arch/x86/include/asm/tdx.h
@@ -36,6 +36,7 @@
 /* Bit definitions of TDX_FEATURES0 metadata field */
 #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)
 
 #ifndef __ASSEMBLER__
 
@@ -118,12 +119,34 @@ static inline bool tdx_supports_runtime_update(const struct tdx_sys_info *sysinf
 	return sysinfo->features.tdx_features0 & TDX_FEATURES0_TD_PRESERVING;
 }
 
+bool tdx_supports_dynamic_pamt(const struct tdx_sys_info *sysinfo);
+
+/* Simple structure for pre-allocating DPAMT pages outside of spinlocks. */
+struct tdx_pamt_cache {
+	struct list_head page_list;
+	int cnt;
+};
+
+static inline void tdx_init_pamt_cache(struct tdx_pamt_cache *cache)
+{
+	INIT_LIST_HEAD(&cache->page_list);
+	cache->cnt = 0;
+}
+
+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_guest_keyid_alloc(void);
 u32 tdx_get_nr_guest_keyids(void);
 void tdx_guest_keyid_free(unsigned int keyid);
 
 void tdx_quirk_reset_paddr(unsigned long base, unsigned long size);
 
+struct page *tdx_alloc_control_page(void);
+void tdx_free_control_page(struct page *page);
+
 struct tdx_td {
 	/* TD root structure: */
 	struct page *tdr_page;
diff --git a/arch/x86/include/asm/tdx_global_metadata.h b/arch/x86/include/asm/tdx_global_metadata.h
index 41150d546589c..8a3cc1a2a41e2 100644
--- a/arch/x86/include/asm/tdx_global_metadata.h
+++ b/arch/x86/include/asm/tdx_global_metadata.h
@@ -1,7 +1,7 @@
 /* SPDX-License-Identifier: GPL-2.0 */
-/* Automatically generated TDX global metadata structures. */
-#ifndef _X86_VIRT_TDX_AUTO_GENERATED_TDX_GLOBAL_METADATA_H
-#define _X86_VIRT_TDX_AUTO_GENERATED_TDX_GLOBAL_METADATA_H
+/* TDX global metadata structures. */
+#ifndef _X86_VIRT_TDX_TDX_GLOBAL_METADATA_H
+#define _X86_VIRT_TDX_TDX_GLOBAL_METADATA_H
 
 #include <linux/types.h>
 
@@ -21,6 +21,9 @@ struct tdx_sys_info_tdmr {
 	u16 pamt_4k_entry_size;
 	u16 pamt_2m_entry_size;
 	u16 pamt_1g_entry_size;
+
+	/* Optional metadata, if DPAMT is supported */
+	u8  pamt_page_bitmap_entry_bits;
 };
 
 struct tdx_sys_info_td_ctrl {
diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index 064ecc33b9267..2816861ca279c 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -617,6 +617,10 @@ static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu, bool maybe_indirect)
 					       PT64_ROOT_MAX_LEVEL);
 		if (r)
 			return r;
+
+		r = kvm_x86_call(topup_external_cache)(vcpu, PT64_ROOT_MAX_LEVEL);
+		if (r)
+			return r;
 	}
 	r = kvm_mmu_topup_memory_cache(&vcpu->arch.mmu_shadow_page_cache,
 				       PT64_ROOT_MAX_LEVEL);
diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c
index b272c20586a74..7c79f4d4bf99a 100644
--- a/arch/x86/kvm/vmx/tdx.c
+++ b/arch/x86/kvm/vmx/tdx.c
@@ -362,7 +362,7 @@ static void tdx_reclaim_control_page(struct page *ctrl_page)
 	if (tdx_reclaim_page(ctrl_page))
 		return;
 
-	__free_page(ctrl_page);
+	tdx_free_control_page(ctrl_page);
 }
 
 struct tdx_flush_vp_arg {
@@ -589,7 +589,7 @@ static void tdx_reclaim_td_control_pages(struct kvm *kvm)
 
 	tdx_quirk_reset_paddr(page_to_phys(kvm_tdx->td.tdr_page), PAGE_SIZE);
 
-	__free_page(kvm_tdx->td.tdr_page);
+	tdx_free_control_page(kvm_tdx->td.tdr_page);
 	kvm_tdx->td.tdr_page = NULL;
 }
 
@@ -681,6 +681,8 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu)
 	if (!irqchip_split(vcpu->kvm))
 		return -EINVAL;
 
+	tdx_init_pamt_cache(&tdx->pamt_cache);
+
 	fpstate_set_confidential(&vcpu->arch.guest_fpu);
 	vcpu->arch.apic->guest_apic_protected = true;
 	INIT_LIST_HEAD(&tdx->vt.pi_wakeup_list);
@@ -866,6 +868,8 @@ void tdx_vcpu_free(struct kvm_vcpu *vcpu)
 	struct vcpu_tdx *tdx = to_tdx(vcpu);
 	int i;
 
+	tdx_free_pamt_cache(&tdx->pamt_cache);
+
 	if (vcpu->cpu != -1) {
 		KVM_BUG_ON(tdx->state == VCPU_TD_STATE_INITIALIZED, vcpu->kvm);
 		tdx_flush_vp_on_cpu(vcpu);
@@ -1618,6 +1622,17 @@ void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level)
 	td_vmcs_write64(to_tdx(vcpu), SHARED_EPT_POINTER, root_hpa);
 }
 
+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);
+}
+
 static int tdx_mem_page_add(struct kvm *kvm, gfn_t gfn, enum pg_level level,
 			    kvm_pfn_t pfn)
 {
@@ -1676,16 +1691,28 @@ static struct page *tdx_spte_to_sept_pt(struct kvm *kvm, gfn_t gfn,
 static int tdx_sept_map_nonleaf_spte(struct kvm *kvm, gfn_t gfn,
 				     enum pg_level level, u64 new_spte)
 {
+	struct kvm_vcpu *vcpu = kvm_get_running_vcpu();
 	gpa_t gpa = gfn_to_gpa(gfn);
 	u64 err, entry, level_state;
 	struct page *sept_pt;
+	int ret;
+
+	if (KVM_BUG_ON(!vcpu, kvm))
+		return -EIO;
 
 	sept_pt = tdx_spte_to_sept_pt(kvm, gfn, new_spte, level);
 	if (!sept_pt)
 		return -EIO;
 
+	ret = tdx_pamt_get(page_to_pfn(sept_pt), &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));
+
 	if (unlikely(tdx_operand_busy(err)))
 		return -EBUSY;
 
@@ -1698,8 +1725,13 @@ static int tdx_sept_map_nonleaf_spte(struct kvm *kvm, gfn_t gfn,
 static int tdx_sept_map_leaf_spte(struct kvm *kvm, gfn_t gfn, enum pg_level level,
 				  u64 new_spte)
 {
+	struct kvm_vcpu *vcpu = kvm_get_running_vcpu();
 	struct kvm_tdx *kvm_tdx = to_kvm_tdx(kvm);
 	kvm_pfn_t pfn = spte_to_pfn(new_spte);
+	int ret;
+
+	if (KVM_BUG_ON(!vcpu, kvm))
+		return -EIO;
 
 	/* TODO: handle large pages. */
 	if (KVM_BUG_ON(level != PG_LEVEL_4K, kvm))
@@ -1707,6 +1739,10 @@ static int tdx_sept_map_leaf_spte(struct kvm *kvm, gfn_t gfn, enum pg_level leve
 
 	WARN_ON_ONCE((new_spte & VMX_EPT_RWX_MASK) != VMX_EPT_RWX_MASK);
 
+	ret = tdx_pamt_get(pfn, &to_tdx(vcpu)->pamt_cache);
+	if (KVM_BUG_ON(ret, kvm))
+		return ret;
+
 	/*
 	 * Ensure pre_fault_allowed is read by kvm_arch_vcpu_pre_fault_memory()
 	 * before kvm_tdx->state.  Userspace must not be allowed to pre-fault
@@ -1719,10 +1755,15 @@ static int tdx_sept_map_leaf_spte(struct kvm *kvm, gfn_t gfn, enum pg_level leve
 	 * If the TD isn't finalized/runnable, then userspace is initializing
 	 * the VM image via KVM_TDX_INIT_MEM_REGION; ADD the page to the TD.
 	 */
-	if (unlikely(kvm_tdx->state != TD_STATE_RUNNABLE))
-		return tdx_mem_page_add(kvm, gfn, level, pfn);
+	if (likely(kvm_tdx->state == TD_STATE_RUNNABLE))
+		ret = tdx_mem_page_aug(kvm, gfn, level, pfn);
+	else
+		ret = tdx_mem_page_add(kvm, gfn, level, pfn);
+
+	if (ret)
+		tdx_pamt_put(pfn);
 
-	return tdx_mem_page_aug(kvm, gfn, level, pfn);
+	return ret;
 }
 
 /*
@@ -1819,6 +1860,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);
 	return 0;
 }
 
@@ -1862,6 +1904,8 @@ static int tdx_sept_set_private_spte(struct kvm *kvm, gfn_t gfn, u64 old_spte,
  */
 static void tdx_sept_free_private_spt(struct kvm *kvm, struct kvm_mmu_page *sp)
 {
+	struct page *sept_pt = virt_to_page(sp->external_spt);
+
 	/*
 	 * KVM doesn't (yet) zap page table pages in mirror page table while
 	 * TD is active, though guest pages mapped in mirror page table could be
@@ -1875,15 +1919,15 @@ static void tdx_sept_free_private_spt(struct kvm *kvm, struct kvm_mmu_page *sp)
 	 * the page to prevent the kernel from accessing the encrypted page.
 	 */
 	if (KVM_BUG_ON(is_hkid_assigned(to_kvm_tdx(kvm)), kvm) ||
-	    tdx_reclaim_page(virt_to_page(sp->external_spt)))
+	    tdx_reclaim_page(sept_pt))
 		goto out;
 
 	/*
-	 * Immediately free the S-EPT page because RCU-time free is unnecessary
-	 * after TDH.PHYMEM.PAGE.RECLAIM ensures there are no outstanding
-	 * readers.
+	 * Immediately free the S-EPT page as the TDX subsystem doesn't support
+	 * freeing pages from RCU callbacks, and more importantly because
+	 * TDH.PHYMEM.PAGE.RECLAIM ensures there are no outstanding readers.
 	 */
-	free_page((unsigned long)sp->external_spt);
+	tdx_free_control_page(sept_pt);
 out:
 	sp->external_spt = NULL;
 }
@@ -2456,7 +2500,7 @@ static int __tdx_td_init(struct kvm *kvm, struct td_params *td_params,
 
 	ret = -ENOMEM;
 
-	tdr_page = alloc_page(GFP_KERNEL_ACCOUNT);
+	tdr_page = tdx_alloc_control_page();
 	if (!tdr_page)
 		goto free_hkid;
 
@@ -2469,7 +2513,7 @@ static int __tdx_td_init(struct kvm *kvm, struct td_params *td_params,
 		goto free_tdr;
 
 	for (i = 0; i < kvm_tdx->td.tdcs_nr_pages; i++) {
-		tdcs_pages[i] = alloc_page(GFP_KERNEL_ACCOUNT);
+		tdcs_pages[i] = tdx_alloc_control_page();
 		if (!tdcs_pages[i])
 			goto free_tdcs;
 	}
@@ -2587,10 +2631,8 @@ static int __tdx_td_init(struct kvm *kvm, struct td_params *td_params,
 teardown:
 	/* Only free pages not yet added, so start at 'i' */
 	for (; i < kvm_tdx->td.tdcs_nr_pages; i++) {
-		if (tdcs_pages[i]) {
-			__free_page(tdcs_pages[i]);
-			tdcs_pages[i] = NULL;
-		}
+		tdx_free_control_page(tdcs_pages[i]);
+		tdcs_pages[i] = NULL;
 	}
 	if (!kvm_tdx->td.tdcs_pages)
 		kfree(tdcs_pages);
@@ -2605,16 +2647,13 @@ static int __tdx_td_init(struct kvm *kvm, struct td_params *td_params,
 	free_cpumask_var(packages);
 
 free_tdcs:
-	for (i = 0; i < kvm_tdx->td.tdcs_nr_pages; i++) {
-		if (tdcs_pages[i])
-			__free_page(tdcs_pages[i]);
-	}
+	for (i = 0; i < kvm_tdx->td.tdcs_nr_pages; i++)
+		tdx_free_control_page(tdcs_pages[i]);
 	kfree(tdcs_pages);
 	kvm_tdx->td.tdcs_pages = NULL;
 
 free_tdr:
-	if (tdr_page)
-		__free_page(tdr_page);
+	tdx_free_control_page(tdr_page);
 	kvm_tdx->td.tdr_page = NULL;
 
 free_hkid:
@@ -2948,7 +2987,7 @@ static int tdx_td_vcpu_init(struct kvm_vcpu *vcpu, u64 vcpu_rcx)
 	int ret, i;
 	u64 err;
 
-	page = alloc_page(GFP_KERNEL_ACCOUNT);
+	page = tdx_alloc_control_page();
 	if (!page)
 		return -ENOMEM;
 	tdx->vp.tdvpr_page = page;
@@ -2968,7 +3007,7 @@ static int tdx_td_vcpu_init(struct kvm_vcpu *vcpu, u64 vcpu_rcx)
 	}
 
 	for (i = 0; i < kvm_tdx->td.tdcx_nr_pages; i++) {
-		page = alloc_page(GFP_KERNEL_ACCOUNT);
+		page = tdx_alloc_control_page();
 		if (!page) {
 			ret = -ENOMEM;
 			goto free_tdcx;
@@ -2990,7 +3029,7 @@ static int tdx_td_vcpu_init(struct kvm_vcpu *vcpu, u64 vcpu_rcx)
 			 * method, but the rest are freed here.
 			 */
 			for (; i < kvm_tdx->td.tdcx_nr_pages; i++) {
-				__free_page(tdx->vp.tdcx_pages[i]);
+				tdx_free_control_page(tdx->vp.tdcx_pages[i]);
 				tdx->vp.tdcx_pages[i] = NULL;
 			}
 			return -EIO;
@@ -3018,16 +3057,14 @@ static int tdx_td_vcpu_init(struct kvm_vcpu *vcpu, u64 vcpu_rcx)
 
 free_tdcx:
 	for (i = 0; i < kvm_tdx->td.tdcx_nr_pages; i++) {
-		if (tdx->vp.tdcx_pages[i])
-			__free_page(tdx->vp.tdcx_pages[i]);
+		tdx_free_control_page(tdx->vp.tdcx_pages[i]);
 		tdx->vp.tdcx_pages[i] = NULL;
 	}
 	kfree(tdx->vp.tdcx_pages);
 	tdx->vp.tdcx_pages = NULL;
 
 free_tdvpr:
-	if (tdx->vp.tdvpr_page)
-		__free_page(tdx->vp.tdvpr_page);
+	tdx_free_control_page(tdx->vp.tdvpr_page);
 	tdx->vp.tdvpr_page = NULL;
 	tdx->vp.tdvpr_pa = 0;
 
@@ -3487,6 +3524,10 @@ 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;
+
+	if (tdx_supports_dynamic_pamt(tdx_sysinfo))
+		vt_x86_ops.topup_external_cache = tdx_topup_external_pamt_cache;
+
 	vt_x86_ops.protected_apic_has_interrupt = tdx_protected_apic_has_interrupt;
 	return 0;
 
diff --git a/arch/x86/kvm/vmx/tdx.h b/arch/x86/kvm/vmx/tdx.h
index ac8323a68b163..fd368e3ee060b 100644
--- a/arch/x86/kvm/vmx/tdx.h
+++ b/arch/x86/kvm/vmx/tdx.h
@@ -72,6 +72,8 @@ struct vcpu_tdx {
 
 	u64 map_gpa_next;
 	u64 map_gpa_end;
+
+	struct tdx_pamt_cache pamt_cache;
 };
 
 void tdh_vp_rd_failed(struct vcpu_tdx *tdx, char *uclass, u32 field, u64 err);
diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c
index 1b9ff749dd8e0..2acab6e5f5f87 100644
--- a/arch/x86/virt/vmx/tdx/tdx.c
+++ b/arch/x86/virt/vmx/tdx/tdx.c
@@ -30,6 +30,7 @@
 #include <linux/suspend.h>
 #include <linux/syscore_ops.h>
 #include <linux/idr.h>
+#include <linux/vmalloc.h>
 #include <asm/page.h>
 #include <asm/special_insns.h>
 #include <asm/msr-index.h>
@@ -46,6 +47,11 @@
 #include "seamcall_internal.h"
 #include "tdx.h"
 
+/* Number of DPAMT pages to be provided to TDX module per 2MB region of PA */
+#define TDX_DPAMT_ENTRY_PAGE_CNT 2
+
+static bool tdx_enable_dpamt __ro_after_init;
+
 struct tdx_module_state {
 	bool initialized;
 	bool sysinit_done;
@@ -63,6 +69,14 @@ static DEFINE_PER_CPU(bool, tdx_lp_initialized);
 
 static struct tdmr_info_list tdx_tdmr_list;
 
+/*
+ * On a machine with DPAMT, the kernel maintains a reference counter
+ * for every 2MB range. The counter indicates how many users there are for
+ * the DPAMT at the 2MB range. The kernel allocates DPAMT refcounts at
+ * initialization.
+ */
+static atomic_t *dpamt_refcounts;
+
 /* All TDX-usable memory regions.  Protected by mem_hotplug_lock. */
 static LIST_HEAD(tdx_memlist);
 
@@ -252,6 +266,42 @@ static struct syscore tdx_syscore = {
 	.ops = &tdx_syscore_ops,
 };
 
+/*
+ * Allocate DPAMT reference counters for all physical memory.
+ *
+ * It consumes 2MB for every 1TB of physical memory.
+ */
+static __init int init_dpamt_refcounts(void)
+{
+	size_t size = DIV_ROUND_UP(max_pfn, PTRS_PER_PTE) * sizeof(*dpamt_refcounts);
+
+	if (!tdx_supports_dynamic_pamt(&tdx_sysinfo))
+		return 0;
+
+	dpamt_refcounts = vzalloc(size);
+	if (!dpamt_refcounts)
+		return -ENOMEM;
+
+	return 0;
+}
+
+static __init void free_dpamt_refcounts(void)
+{
+	if (!tdx_supports_dynamic_pamt(&tdx_sysinfo))
+		return;
+
+	vfree(dpamt_refcounts);
+	dpamt_refcounts = NULL;
+}
+
+static atomic_t *tdx_find_dpamt_refcount(unsigned long pfn)
+{
+	/* Find which PMD a PFN is in. */
+	unsigned long index = pfn >> (PMD_SHIFT - PAGE_SHIFT);
+
+	return &dpamt_refcounts[index];
+}
+
 /*
  * Add a memory region as a TDX memory block.  The caller must make sure
  * all memory regions are added in address ascending order and don't
@@ -510,35 +560,37 @@ static __init int fill_out_tdmrs(struct list_head *tmb_list,
 	return 0;
 }
 
+static __init unsigned long tdmr_get_pamt_bitmap_sz(struct tdmr_info *tdmr)
+{
+	unsigned long pamt_sz, nr_pamt_entries;
+	int bits_per_entry;
+
+	bits_per_entry = tdx_sysinfo.tdmr.pamt_page_bitmap_entry_bits;
+	nr_pamt_entries = tdmr->size >> PAGE_SHIFT;
+	pamt_sz = DIV_ROUND_UP(nr_pamt_entries * bits_per_entry, BITS_PER_BYTE);
+
+	return PAGE_ALIGN(pamt_sz);
+}
+
 /*
  * Calculate PAMT size given a TDMR and a page size.  The returned
  * PAMT size is always aligned up to 4K page boundary.
  */
-static __init unsigned long tdmr_get_pamt_sz(struct tdmr_info *tdmr, int pgsz,
-					     u16 pamt_entry_size)
+static __init unsigned long tdmr_get_pamt_sz(struct tdmr_info *tdmr, int pgsz)
 {
 	unsigned long pamt_sz, nr_pamt_entries;
+	const int tdx_pg_size_shift[TDX_PS_NR] = { PAGE_SHIFT, PMD_SHIFT, PUD_SHIFT };
+	const u16 pamt_entry_size[TDX_PS_NR] = {
+		tdx_sysinfo.tdmr.pamt_4k_entry_size,
+		tdx_sysinfo.tdmr.pamt_2m_entry_size,
+		tdx_sysinfo.tdmr.pamt_1g_entry_size,
+	};
 
-	switch (pgsz) {
-	case TDX_PS_4K:
-		nr_pamt_entries = tdmr->size >> PAGE_SHIFT;
-		break;
-	case TDX_PS_2M:
-		nr_pamt_entries = tdmr->size >> PMD_SHIFT;
-		break;
-	case TDX_PS_1G:
-		nr_pamt_entries = tdmr->size >> PUD_SHIFT;
-		break;
-	default:
-		WARN_ON_ONCE(1);
-		return 0;
-	}
+	nr_pamt_entries = tdmr->size >> tdx_pg_size_shift[pgsz];
+	pamt_sz = nr_pamt_entries * pamt_entry_size[pgsz];
 
-	pamt_sz = nr_pamt_entries * pamt_entry_size;
 	/* TDX requires PAMT size must be 4K aligned */
-	pamt_sz = ALIGN(pamt_sz, PAGE_SIZE);
-
-	return pamt_sz;
+	return PAGE_ALIGN(pamt_sz);
 }
 
 /*
@@ -576,15 +628,11 @@ static __init int tdmr_get_nid(struct tdmr_info *tdmr, struct list_head *tmb_lis
  * within @tdmr, and set up PAMTs for @tdmr.
  */
 static __init int tdmr_set_up_pamt(struct tdmr_info *tdmr,
-				   struct list_head *tmb_list,
-				   u16 pamt_entry_size[])
+				   struct list_head *tmb_list)
 {
-	unsigned long pamt_base[TDX_PS_NR];
-	unsigned long pamt_size[TDX_PS_NR];
-	unsigned long tdmr_pamt_base;
 	unsigned long tdmr_pamt_size;
 	struct page *pamt;
-	int pgsz, nid;
+	int nid;
 
 	nid = tdmr_get_nid(tdmr, tmb_list);
 
@@ -592,13 +640,18 @@ static __init int tdmr_set_up_pamt(struct tdmr_info *tdmr,
 	 * Calculate the PAMT size for each TDX supported page size
 	 * and the total PAMT size.
 	 */
-	tdmr_pamt_size = 0;
-	for (pgsz = TDX_PS_4K; pgsz < TDX_PS_NR; pgsz++) {
-		pamt_size[pgsz] = tdmr_get_pamt_sz(tdmr, pgsz,
-					pamt_entry_size[pgsz]);
-		tdmr_pamt_size += pamt_size[pgsz];
+	tdmr->pamt_1g_size = tdmr_get_pamt_sz(tdmr, TDX_PS_1G);
+	tdmr->pamt_2m_size = tdmr_get_pamt_sz(tdmr, TDX_PS_2M);
+
+	if (tdx_supports_dynamic_pamt(&tdx_sysinfo)) {
+		/* With DPAMT, PAMT_4K is replaced with a bitmap */
+		tdmr->pamt_4k_size = tdmr_get_pamt_bitmap_sz(tdmr);
+	} else {
+		tdmr->pamt_4k_size = tdmr_get_pamt_sz(tdmr, TDX_PS_4K);
 	}
 
+	tdmr_pamt_size = tdmr->pamt_4k_size + tdmr->pamt_2m_size + tdmr->pamt_1g_size;
+
 	/*
 	 * Allocate one chunk of physically contiguous memory for all
 	 * PAMTs.  This helps minimize the PAMT's use of reserved areas
@@ -606,25 +659,17 @@ static __init int tdmr_set_up_pamt(struct tdmr_info *tdmr,
 	 */
 	pamt = alloc_contig_pages(tdmr_pamt_size >> PAGE_SHIFT, GFP_KERNEL,
 			nid, &node_online_map);
-	if (!pamt)
-		return -ENOMEM;
 
 	/*
-	 * Break the contiguous allocation back up into the
-	 * individual PAMTs for each page size.
+	 * tdmr->pamt_4k_base is still zero so the error
+	 * path of the caller will skip freeing the PAMT.
 	 */
-	tdmr_pamt_base = page_to_pfn(pamt) << PAGE_SHIFT;
-	for (pgsz = TDX_PS_4K; pgsz < TDX_PS_NR; pgsz++) {
-		pamt_base[pgsz] = tdmr_pamt_base;
-		tdmr_pamt_base += pamt_size[pgsz];
-	}
+	if (!pamt)
+		return -ENOMEM;
 
-	tdmr->pamt_4k_base = pamt_base[TDX_PS_4K];
-	tdmr->pamt_4k_size = pamt_size[TDX_PS_4K];
-	tdmr->pamt_2m_base = pamt_base[TDX_PS_2M];
-	tdmr->pamt_2m_size = pamt_size[TDX_PS_2M];
-	tdmr->pamt_1g_base = pamt_base[TDX_PS_1G];
-	tdmr->pamt_1g_size = pamt_size[TDX_PS_1G];
+	tdmr->pamt_4k_base = page_to_phys(pamt);
+	tdmr->pamt_2m_base = tdmr->pamt_4k_base + tdmr->pamt_4k_size;
+	tdmr->pamt_1g_base = tdmr->pamt_2m_base + tdmr->pamt_2m_size;
 
 	return 0;
 }
@@ -655,10 +700,7 @@ static __init void tdmr_do_pamt_func(struct tdmr_info *tdmr,
 	tdmr_get_pamt(tdmr, &pamt_base, &pamt_size);
 
 	/* Do nothing if PAMT hasn't been allocated for this TDMR */
-	if (!pamt_size)
-		return;
-
-	if (WARN_ON_ONCE(!pamt_base))
+	if (!pamt_base)
 		return;
 
 	pamt_func(pamt_base, pamt_size);
@@ -684,14 +726,12 @@ static __init void tdmrs_free_pamt_all(struct tdmr_info_list *tdmr_list)
 
 /* Allocate and set up PAMTs for all TDMRs */
 static __init int tdmrs_set_up_pamt_all(struct tdmr_info_list *tdmr_list,
-					struct list_head *tmb_list,
-					u16 pamt_entry_size[])
+					struct list_head *tmb_list)
 {
 	int i, ret = 0;
 
 	for (i = 0; i < tdmr_list->nr_consumed_tdmrs; i++) {
-		ret = tdmr_set_up_pamt(tdmr_entry(tdmr_list, i), tmb_list,
-				pamt_entry_size);
+		ret = tdmr_set_up_pamt(tdmr_entry(tdmr_list, i), tmb_list);
 		if (ret)
 			goto err;
 	}
@@ -968,18 +1008,13 @@ static __init int construct_tdmrs(struct list_head *tmb_list,
 				  struct tdmr_info_list *tdmr_list,
 				  struct tdx_sys_info_tdmr *sysinfo_tdmr)
 {
-	u16 pamt_entry_size[TDX_PS_NR] = {
-		sysinfo_tdmr->pamt_4k_entry_size,
-		sysinfo_tdmr->pamt_2m_entry_size,
-		sysinfo_tdmr->pamt_1g_entry_size,
-	};
 	int ret;
 
 	ret = fill_out_tdmrs(tmb_list, tdmr_list);
 	if (ret)
 		return ret;
 
-	ret = tdmrs_set_up_pamt_all(tdmr_list, tmb_list, pamt_entry_size);
+	ret = tdmrs_set_up_pamt_all(tdmr_list, tmb_list);
 	if (ret)
 		return ret;
 
@@ -998,6 +1033,8 @@ static __init int construct_tdmrs(struct list_head *tmb_list,
 	return ret;
 }
 
+#define TDX_SYS_CONFIG_DYNAMIC_PAMT	BIT(16)
+
 static __init int config_tdx_module(struct tdmr_info_list *tdmr_list,
 				    u64 global_keyid)
 {
@@ -1026,6 +1063,12 @@ static __init int config_tdx_module(struct tdmr_info_list *tdmr_list,
 	args.rcx = __pa(tdmr_pa_array);
 	args.rdx = tdmr_list->nr_consumed_tdmrs;
 	args.r8 = global_keyid;
+
+	if (tdx_supports_dynamic_pamt(&tdx_sysinfo)) {
+		pr_info("Enable Dynamic PAMT\n");
+		args.r8 |= TDX_SYS_CONFIG_DYNAMIC_PAMT;
+	}
+
 	ret = seamcall_prerr(TDH_SYS_CONFIG, &args);
 
 	/* Free the array as it is not required anymore. */
@@ -1167,10 +1210,14 @@ static __init int init_tdx_module(void)
 	 */
 	get_online_mems();
 
-	ret = build_tdx_memlist(&tdx_memlist);
+	ret = init_dpamt_refcounts();
 	if (ret)
 		goto out_put_tdxmem;
 
+	ret = build_tdx_memlist(&tdx_memlist);
+	if (ret)
+		goto err_free_dpamt_refcounts;
+
 	/* Allocate enough space for constructing TDMRs */
 	ret = alloc_tdmr_list(&tdx_tdmr_list, &tdx_sysinfo.tdmr);
 	if (ret)
@@ -1220,6 +1267,8 @@ static __init int init_tdx_module(void)
 	free_tdmr_list(&tdx_tdmr_list);
 err_free_tdxmem:
 	free_tdx_memlist(&tdx_memlist);
+err_free_dpamt_refcounts:
+	free_dpamt_refcounts();
 	goto out_put_tdxmem;
 }
 
@@ -2005,6 +2054,293 @@ u64 tdh_phymem_page_wbinvd_hkid(u64 hkid, kvm_pfn_t pfn)
 }
 EXPORT_SYMBOL_FOR_KVM(tdh_phymem_page_wbinvd_hkid);
 
+bool tdx_supports_dynamic_pamt(const struct tdx_sys_info *sysinfo)
+{
+	if (!tdx_enable_dpamt)
+		return false;
+
+	return sysinfo->features.tdx_features0 & TDX_FEATURES0_DYNAMIC_PAMT;
+}
+EXPORT_SYMBOL_FOR_KVM(tdx_supports_dynamic_pamt);
+
+static struct page *tdx_alloc_page_pamt_cache(struct tdx_pamt_cache *cache)
+{
+	struct page *page;
+
+	page = list_first_entry_or_null(&cache->page_list, struct page, lru);
+	if (page) {
+		list_del(&page->lru);
+		cache->cnt--;
+	}
+
+	return page;
+}
+
+static struct page *alloc_dpamt_page(struct tdx_pamt_cache *cache)
+{
+	if (cache)
+		return tdx_alloc_page_pamt_cache(cache);
+
+	return alloc_page(GFP_KERNEL_ACCOUNT);
+}
+
+static int alloc_pamt_array(struct page **pamt_pages, struct tdx_pamt_cache *cache)
+{
+	int i, j;
+
+	for (i = 0; i < TDX_DPAMT_ENTRY_PAGE_CNT; i++) {
+		pamt_pages[i] = alloc_dpamt_page(cache);
+		if (!pamt_pages[i])
+			goto err;
+	}
+
+	return 0;
+
+err:
+	for (j = 0; j < i; j++)
+		__free_page(pamt_pages[j]);
+
+	return -ENOMEM;
+}
+
+static void free_pamt_array(struct page **pamt_pages)
+{
+	int i;
+
+	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);
+
+		__free_page(pamt_pages[i]);
+	}
+}
+
+/* Helper for building DPAMT seamcall() arguments. */
+static u64 pamt_2mb_arg(kvm_pfn_t pfn)
+{
+	/* Find the 2MB-wide DPAMT region for 'pfn': */
+	unsigned long hpa_2mb = ALIGN_DOWN(pfn << PAGE_SHIFT, PMD_SIZE);
+
+	/*
+	 * TDX ABI requires specifying the page level the installed DPAMT
+	 * backing will cover, even though today only 2MB is supported.
+	 */
+	return hpa_2mb | TDX_PS_2M;
+}
+
+/* Add DPAMT backing for the 2MB region surrounding the given pfn. */
+static u64 tdh_phymem_pamt_add(kvm_pfn_t pfn, struct page **pamt_pages)
+{
+	struct tdx_module_args args = {
+		.rcx = pamt_2mb_arg(pfn),
+		.rdx = page_to_phys(pamt_pages[0]),
+		.r8  = page_to_phys(pamt_pages[1]),
+	};
+
+	return seamcall(TDH_PHYMEM_PAMT_ADD, &args);
+}
+
+/* Remove DPAMT backing for the 2MB region surrounding the given pfn. */
+static u64 tdh_phymem_pamt_remove(kvm_pfn_t pfn, struct page **pamt_pages)
+{
+	struct tdx_module_args args = {
+		.rcx = pamt_2mb_arg(pfn),
+	};
+	u64 ret;
+
+	ret = seamcall_ret(TDH_PHYMEM_PAMT_REMOVE, &args);
+	if (ret)
+		return ret;
+
+	/* Copy PAMT pages out of the struct per the TDX ABI */
+	pamt_pages[0] = phys_to_page(args.rdx);
+	pamt_pages[1] = phys_to_page(args.r8);
+
+	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, 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))
+		return 0;
+
+	dpamt_refcount = tdx_find_dpamt_refcount(pfn);
+
+	/*
+	 * If the DPAMT entry is already added (i.e. refcount >= 1),
+	 * then just increment the refcount.
+	 */
+	if (atomic_inc_not_zero(dpamt_refcount))
+		return 0;
+
+	ret = alloc_pamt_array(pamt_pages, cache);
+	if (ret)
+		return ret;
+
+	spin_lock(&dpamt_lock);
+
+	/*
+	 * Unlike tdx_pamt_put() which uses atomic_dec_and_lock() to
+	 * atomically handle the 1->0 transition, the get side has no
+	 * equivalent combined primitive for 0->1. Recheck under the
+	 * lock since another get may have already done the 0->1
+	 * transition after both saw atomic_inc_not_zero() fail.
+	 */
+	if (atomic_inc_not_zero(dpamt_refcount))
+		goto out_free;
+
+	tdx_status = tdh_phymem_pamt_add(pfn, pamt_pages);
+	if (WARN_ON_ONCE(tdx_status != TDX_SUCCESS)) {
+		ret = -EIO;
+		goto out_free;
+	}
+
+	/*
+	 * The refcount is zero, and this locked path is the
+	 * only way to increase it from 0->1.
+	 */
+	atomic_set(dpamt_refcount, 1);
+	spin_unlock(&dpamt_lock);
+	return 0;
+
+out_free:
+	spin_unlock(&dpamt_lock);
+	free_pamt_array(pamt_pages);
+
+	return ret;
+}
+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)
+{
+	struct page *pamt_pages[TDX_DPAMT_ENTRY_PAGE_CNT] = {};
+	atomic_t *dpamt_refcount;
+	u64 tdx_status;
+
+	if (!tdx_supports_dynamic_pamt(&tdx_sysinfo))
+		return;
+
+	dpamt_refcount = tdx_find_dpamt_refcount(pfn);
+
+	/*
+	 * If there is more than 1 reference on the DPAMT entry, don't
+	 * remove it yet. Just decrement the refcount.
+	 */
+	if (!atomic_dec_and_lock(dpamt_refcount, &dpamt_lock))
+		return;
+
+	tdx_status = tdh_phymem_pamt_remove(pfn, pamt_pages);
+
+	/*
+	 * Don't free pamt_pages as it could hold garbage when
+	 * tdh_phymem_pamt_remove() fails.  Don't panic/BUG_ON(), as
+	 * there is no risk of data corruption, but do yell loudly as
+	 * failure indicates a kernel bug, memory is being leaked, and
+	 * the dangling DPAMT entry may cause future operations to fail.
+	 */
+	if (WARN_ON_ONCE(tdx_status != TDX_SUCCESS)) {
+		/*
+		 * atomic_dec_and_lock() already decremented it to 0,
+		 * but the DPAMT entry still exists since REMOVE failed.
+		 */
+		atomic_set(dpamt_refcount, 1);
+		goto out_unlock;
+	}
+
+	spin_unlock(&dpamt_lock);
+	free_pamt_array(pamt_pages);
+	return;
+out_unlock:
+	spin_unlock(&dpamt_lock);
+}
+EXPORT_SYMBOL_FOR_KVM(tdx_pamt_put);
+
+void tdx_free_pamt_cache(struct tdx_pamt_cache *cache)
+{
+	struct page *page;
+
+	while ((page = tdx_alloc_page_pamt_cache(cache)))
+		__free_page(page);
+}
+EXPORT_SYMBOL_FOR_KVM(tdx_free_pamt_cache);
+
+int tdx_topup_pamt_cache(struct tdx_pamt_cache *cache, unsigned long npages)
+{
+	if (WARN_ON_ONCE(!tdx_supports_dynamic_pamt(&tdx_sysinfo)))
+		return 0;
+
+	npages *= TDX_DPAMT_ENTRY_PAGE_CNT;
+
+	while (cache->cnt < npages) {
+		struct page *page = alloc_page(GFP_KERNEL_ACCOUNT);
+
+		if (!page)
+			return -ENOMEM;
+
+		list_add(&page->lru, &cache->page_list);
+		cache->cnt++;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL_FOR_KVM(tdx_topup_pamt_cache);
+
+/*
+ * Return a page that can be gifted to the TDX module for use as a "control"
+ * page, i.e. pages that are used for control structures for a given TDX
+ * guest, and thus obtain TDX protections, including DPAMT tracking.
+ */
+struct page *tdx_alloc_control_page(void)
+{
+	struct page *page;
+
+	page = alloc_page(GFP_KERNEL_ACCOUNT);
+	if (!page)
+		return NULL;
+
+	if (tdx_pamt_get(page_to_pfn(page), NULL)) {
+		__free_page(page);
+		return NULL;
+	}
+
+	return page;
+}
+EXPORT_SYMBOL_FOR_KVM(tdx_alloc_control_page);
+
+/*
+ * Free a page that was gifted to the TDX module for use as a control
+ * page. After this, the page is no longer protected by TDX.
+ */
+void tdx_free_control_page(struct page *page)
+{
+	if (!page)
+		return;
+
+	tdx_pamt_put(page_to_pfn(page));
+	__free_page(page);
+}
+EXPORT_SYMBOL_FOR_KVM(tdx_free_control_page);
+
+static int __init tdx_dpamt_setup(char *str)
+{
+	return kstrtobool(str, &tdx_enable_dpamt) == 0;
+}
+
+__setup("tdx_dpamt=", tdx_dpamt_setup);
+
 void tdx_sys_disable(void)
 {
 	struct tdx_module_args args = {};
diff --git a/arch/x86/virt/vmx/tdx/tdx.h b/arch/x86/virt/vmx/tdx/tdx.h
index bdfd0e1e337ac..a886c54decaad 100644
--- a/arch/x86/virt/vmx/tdx/tdx.h
+++ b/arch/x86/virt/vmx/tdx/tdx.h
@@ -48,6 +48,8 @@
 #define TDH_SYS_CONFIG			45
 #define TDH_SYS_SHUTDOWN		52
 #define TDH_SYS_UPDATE			53
+#define TDH_PHYMEM_PAMT_ADD		58
+#define TDH_PHYMEM_PAMT_REMOVE		59
 #define TDH_SYS_DISABLE			69
 
 /*
diff --git a/arch/x86/virt/vmx/tdx/tdx_global_metadata.c b/arch/x86/virt/vmx/tdx/tdx_global_metadata.c
index e49c300f23d43..98ebf17aab1ce 100644
--- a/arch/x86/virt/vmx/tdx/tdx_global_metadata.c
+++ b/arch/x86/virt/vmx/tdx/tdx_global_metadata.c
@@ -1,6 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- * Automatically generated functions to read TDX global metadata.
+ * Functions to read TDX global metadata.
  *
  * This file doesn't compile on its own as it lacks of inclusion
  * of SEAMCALL wrapper primitive which reads global metadata.
@@ -33,6 +33,18 @@ static __init int get_tdx_sys_info_features(struct tdx_sys_info_features *sysinf
 	return ret;
 }
 
+static __init int get_tdx_sys_info_tdmr_dpamt(struct tdx_sys_info_tdmr *sysinfo_tdmr)
+{
+	int ret;
+	u64 val;
+
+	ret = read_sys_metadata_field(0x9100000000000013, &val);
+	if (!ret)
+		sysinfo_tdmr->pamt_page_bitmap_entry_bits = val;
+
+	return ret;
+}
+
 static __init int get_tdx_sys_info_tdmr(struct tdx_sys_info_tdmr *sysinfo_tdmr)
 {
 	int ret = 0;
@@ -129,5 +141,14 @@ static __init int get_tdx_sys_info(struct tdx_sys_info *sysinfo)
 	ret = ret ?: get_tdx_sys_info_td_ctrl(&sysinfo->td_ctrl);
 	ret = ret ?: get_tdx_sys_info_td_conf(&sysinfo->td_conf);
 
+	/*
+	 * The kernel supports using TDX without DPAMT, so
+	 * avoid reporting failure if it's not supported. Don't
+	 * try to support buggy TDX modules that advertise
+	 * DPAMT but don't expose the metadata.
+	 */
+	if (!ret && tdx_supports_dynamic_pamt(sysinfo))
+		ret = get_tdx_sys_info_tdmr_dpamt(&sysinfo->tdmr);
+
 	return ret;
 }

3/2 2026/09/04 02:05 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 18191
							output: 32
							thoughts: 450
						

Thoughts:
**Analyzing the TDX DPAMT Patch for Fuzzing Potential**

Okay, the patch is in. My initial thought is to figure out if it's a good candidate for syzkaller fuzzing. Let's dig in. First, I need to *understand* what this patch is even doing. Ah, it's adding Dynamic PAMT (DPAMT) support for Intel TDX, specifically in the KVM TDX and virt/vmx/tdx components. That's good information.

Now, let's see where the code's modified. I see changes across a decent chunk of the codebase: documentation, include files in `arch/x86`, and some key `kvm` directories, especially those dealing with `vmx` and `tdx` (including `tdx_global_metadata`). That breadth of changes is interesting. The deeper it goes the more opportunity for hidden edge cases.

Next, I need to quickly assess the fuzzability in our standard environments. Can we actually *reach* this functionality in QEMU or GCE? Does the TDX host (that Intel TDX module/SEAMCALL) function properly in a virtual environment without real TDX hardware? That's the critical question.

Wait a second… How does this TDX host get initialized? I need to understand the entry points. Let's do a quick dive. I should check `grepper` and look into `arch/x86/virt/vmx/tdx/` and `arch/x86/kvm/vmx/tdx.c` to figure out the initialization and usage of the TDX host.



4/2 2026/09/04 02:06 tool grepper
0m Args:
{
  "Expression": "init_tdx_module",
  "PathPrefix": "arch/x86/"
}

Results:
{
  "Output": "arch/x86/virt/vmx/tdx/tdx.c=1169=static __init int init_tdmrs(struct tdmr_info_list *tdmr_list)\n--\narch/x86/virt/vmx/tdx/tdx.c-1187-\narch/x86/virt/vmx/tdx/tdx.c:1188:static __init int init_tdx_module(void)\narch/x86/virt/vmx/tdx/tdx.c-1189-{\n--\narch/x86/virt/vmx/tdx/tdx.c=1275=static __init int tdx_enable(void)\n--\narch/x86/virt/vmx/tdx/tdx.c-1304-\narch/x86/virt/vmx/tdx/tdx.c:1305:\tret = init_tdx_module();\narch/x86/virt/vmx/tdx/tdx.c-1306-\tif (ret) {\n"
}

5/2 2026/09/04 02:06 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 19305
							output: 30
							thoughts: 21
						

Thoughts:
Here's my attempt at summarizing the thought process, tailored for an expert audience:

**Investigating TDX Host Initialization and Usage**

Okay, so I need to understand the lifecycle of the TDX host environment. First, I want to trace the calls to `tdx_enable`. That's the key function, I'm sure, and it should reveal the point at which the TDX features are activated. Knowing where this is called and the parameters passed will tell me quite a bit about the configuration. I'm especially interested in whether we're using early initialization techniques or deferring it. I'll need to correlate these call sites with any TDX-specific host initialization routines. Understanding the initialization sequence is crucial to identifying potential vulnerabilities or understanding performance bottlenecks within the TDX environment. My next step will be to identify any interaction with other parts of the system and note which modules are initialized first.



6/2 2026/09/04 02:06 tool grepper
0m Args:
{
  "Expression": "tdx_enable",
  "PathPrefix": "arch/x86/"
}

Results:
{
  "Output": "arch/x86/virt/vmx/tdx/tdx.c-52-\narch/x86/virt/vmx/tdx/tdx.c:53:static bool tdx_enable_dpamt __ro_after_init;\narch/x86/virt/vmx/tdx/tdx.c-54-\n--\narch/x86/virt/vmx/tdx/tdx.c=1188=static __init int init_tdx_module(void)\n--\narch/x86/virt/vmx/tdx/tdx.c-1274-\narch/x86/virt/vmx/tdx/tdx.c:1275:static __init int tdx_enable(void)\narch/x86/virt/vmx/tdx/tdx.c-1276-{\n--\narch/x86/virt/vmx/tdx/tdx.c-1317-}\narch/x86/virt/vmx/tdx/tdx.c:1318:subsys_initcall(tdx_enable);\narch/x86/virt/vmx/tdx/tdx.c-1319-\n--\narch/x86/virt/vmx/tdx/tdx.c=2057=bool tdx_supports_dynamic_pamt(const struct tdx_sys_info *sysinfo)\narch/x86/virt/vmx/tdx/tdx.c-2058-{\narch/x86/virt/vmx/tdx/tdx.c:2059:\tif (!tdx_enable_dpamt)\narch/x86/virt/vmx/tdx/tdx.c-2060-\t\treturn false;\n--\narch/x86/virt/vmx/tdx/tdx.c=2337=static int __init tdx_dpamt_setup(char *str)\narch/x86/virt/vmx/tdx/tdx.c-2338-{\narch/x86/virt/vmx/tdx/tdx.c:2339:\treturn kstrtobool(str, \u0026tdx_enable_dpamt) == 0;\narch/x86/virt/vmx/tdx/tdx.c-2340-}\n"
}

7/2 2026/09/04 02:06 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 20137
							output: 47
							thoughts: 32
						

Thoughts:
Here's my take on the thought process, as requested:

**Diving into TDX Enablement in the x86 Architecture**

Okay, time to get my hands dirty. I'm going to start by examining `arch/x86/virt/vmx/tdx/tdx.c` specifically around the `tdx_enable()` function. I need to understand exactly how the Trust Domain Extensions (TDX) are brought online within the VMX context. I expect this function will be the core of the TDX initialization. I'm particularly interested in the sequence of operations: what checks are performed, what registers are manipulated, and what specific control bits are set to enable TDX features. I'll need to dissect any calls to lower-level functions, such as those related to handling TDX-specific MSRs or interacting with the TDX module itself. This file will be the key to understanding the early boot stages and the secure setup of the TDX environment, so I expect to find some low-level details that'll give me a solid foundation for evaluating security implications.



8/2 2026/09/04 02:06 tool read-file
0m Args:
{
  "File": "arch/x86/virt/vmx/tdx/tdx.c",
  "FirstLine": 1270,
  "LineCount": 55
}

Results:
{
  "Contents": "1270:\terr_free_dpamt_refcounts:\n1271:\t\tfree_dpamt_refcounts();\n1272:\t\tgoto out_put_tdxmem;\n1273:\t}\n1274:\t\n1275:\tstatic __init int tdx_enable(void)\n1276:\t{\n1277:\t\tenum cpuhp_state state;\n1278:\t\tint ret;\n1279:\t\n1280:\t\tif (!cpu_feature_enabled(X86_FEATURE_TDX_HOST_PLATFORM)) {\n1281:\t\t\tpr_err(\"TDX not supported by the host platform\\n\");\n1282:\t\t\treturn -ENODEV;\n1283:\t\t}\n1284:\t\n1285:\t\tif (!cpu_feature_enabled(X86_FEATURE_XSAVE)) {\n1286:\t\t\tpr_err(\"XSAVE is required for TDX\\n\");\n1287:\t\t\treturn -EINVAL;\n1288:\t\t}\n1289:\t\n1290:\t\tif (!cpu_feature_enabled(X86_FEATURE_MOVDIR64B)) {\n1291:\t\t\tpr_err(\"MOVDIR64B is required for TDX\\n\");\n1292:\t\t\treturn -EINVAL;\n1293:\t\t}\n1294:\t\n1295:\t\tif (!cpu_feature_enabled(X86_FEATURE_SELFSNOOP)) {\n1296:\t\t\tpr_err(\"Self-snoop is required for TDX\\n\");\n1297:\t\t\treturn -ENODEV;\n1298:\t\t}\n1299:\t\n1300:\t\tstate = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, \"virt/tdx:online\",\n1301:\t\t\t\t\t  tdx_online_cpu, tdx_offline_cpu);\n1302:\t\tif (state \u003c 0)\n1303:\t\t\treturn state;\n1304:\t\n1305:\t\tret = init_tdx_module();\n1306:\t\tif (ret) {\n1307:\t\t\tpr_err(\"TDX-Module initialization failed (%d)\\n\", ret);\n1308:\t\t\tcpuhp_remove_state(state);\n1309:\t\t\treturn ret;\n1310:\t\t}\n1311:\t\n1312:\t\tregister_syscore(\u0026tdx_syscore);\n1313:\t\n1314:\t\ttdx_module_state.initialized = true;\n1315:\t\tpr_info(\"TDX-Module initialized\\n\");\n1316:\t\treturn 0;\n1317:\t}\n1318:\tsubsys_initcall(tdx_enable);\n1319:\t\n1320:\tint tdx_module_shutdown(void)\n1321:\t{\n1322:\t\tstruct tdx_sys_info_handoff handoff = {};\n1323:\t\tstruct tdx_module_args args = {};\n1324:\t\tint ret;\n"
}

9/2 2026/09/04 02:06 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 21252
							output: 39
							thoughts: 47
						

Thoughts:
**Investigating the TDX Host Platform Feature**

Okay, so I'm looking at `X86_FEATURE_TDX_HOST_PLATFORM`.  First things first: where is this thing actually *defined* and, more importantly, *detected* in the code?  Time to break out the grep.  Gotta trace its usage to understand its implications for the system. Let's see what the grep reveals about this TDX host platform feature.



10/2 2026/09/04 02:06 tool grepper
0m Args:
{
  "Expression": "X86_FEATURE_TDX_HOST_PLATFORM",
  "PathPrefix": "arch/x86/"
}

Results:
{
  "Output": "arch/x86/include/asm/cpufeatures.h-200-#define X86_FEATURE_CDP_L3\t\t( 7*32+ 6) /* \"cdp_l3\" Code and Data Prioritization L3 */\narch/x86/include/asm/cpufeatures.h:201:#define X86_FEATURE_TDX_HOST_PLATFORM\t( 7*32+ 7) /* \"tdx_host_platform\" Platform supports being a TDX host */\narch/x86/include/asm/cpufeatures.h-202-#define X86_FEATURE_HW_PSTATE\t\t( 7*32+ 8) /* \"hw_pstate\" AMD HW-PState */\n--\narch/x86/kvm/vmx/tdx.c=3467=int __init tdx_hardware_setup(void)\n--\narch/x86/kvm/vmx/tdx.c-3497-\narch/x86/kvm/vmx/tdx.c:3498:\tif (!cpu_feature_enabled(X86_FEATURE_TDX_HOST_PLATFORM)) {\narch/x86/kvm/vmx/tdx.c-3499-\t\tpr_err(\"TDX not supported by the host platform\\n\");\n--\narch/x86/virt/vmx/tdx/tdx.c=1275=static __init int tdx_enable(void)\n--\narch/x86/virt/vmx/tdx/tdx.c-1279-\narch/x86/virt/vmx/tdx/tdx.c:1280:\tif (!cpu_feature_enabled(X86_FEATURE_TDX_HOST_PLATFORM)) {\narch/x86/virt/vmx/tdx/tdx.c-1281-\t\tpr_err(\"TDX not supported by the host platform\\n\");\n--\narch/x86/virt/vmx/tdx/tdx.c=1417=static bool paddr_is_tdx_private(unsigned long phys)\n--\narch/x86/virt/vmx/tdx/tdx.c-1423-\narch/x86/virt/vmx/tdx/tdx.c:1424:\tif (!boot_cpu_has(X86_FEATURE_TDX_HOST_PLATFORM))\narch/x86/virt/vmx/tdx/tdx.c-1425-\t\treturn false;\n--\narch/x86/virt/vmx/tdx/tdx.c=1580=void __init tdx_init(void)\n--\narch/x86/virt/vmx/tdx/tdx.c-1631-\narch/x86/virt/vmx/tdx/tdx.c:1632:\tsetup_force_cpu_cap(X86_FEATURE_TDX_HOST_PLATFORM);\narch/x86/virt/vmx/tdx/tdx.c-1633-\n"
}

11/2 2026/09/04 02:06 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 22211
							output: 47
							thoughts: 47
						

Thoughts:
Here's my summary, as if I were thinking these thoughts:

**Analyzing the TDX Initialization**

Alright, let's dive into the core of the TDX initialization process. My first stop is going to be `tdx_init` within `arch/x86/virt/vmx/tdx/tdx.c`. Given my expertise in this area, I'm specifically targeting lines 1580 through 1640. This range typically holds critical setup functions, potential security checks, or configuration steps directly related to setting up the Trusted Domain Extension. I need to understand how the core functionalities of the TDX are being initialized in relation to Intel's architecture. I expect to find details concerning guest context initialization, TDX module registration, and potentially some error handling or verification processes. Let's see what gems are buried in this section.



12/2 2026/09/04 02:06 tool read-file
0m Args:
{
  "File": "arch/x86/virt/vmx/tdx/tdx.c",
  "FirstLine": 1580,
  "LineCount": 60
}

Results:
{
  "Contents": "1580:\tvoid __init tdx_init(void)\n1581:\t{\n1582:\t\tu32 tdx_keyid_start, nr_tdx_keyids;\n1583:\t\tint err;\n1584:\t\n1585:\t\terr = record_keyid_partitioning(\u0026tdx_keyid_start, \u0026nr_tdx_keyids);\n1586:\t\tif (err)\n1587:\t\t\treturn;\n1588:\t\n1589:\t\tpr_info(\"BIOS enabled: private KeyID range [%u, %u)\\n\",\n1590:\t\t\t\ttdx_keyid_start, tdx_keyid_start + nr_tdx_keyids);\n1591:\t\n1592:\t\t/*\n1593:\t\t * The TDX module itself requires one 'global KeyID' to protect\n1594:\t\t * its metadata.  If there's only one TDX KeyID, there won't be\n1595:\t\t * any left for TDX guests thus there's no point to enable TDX\n1596:\t\t * at all.\n1597:\t\t */\n1598:\t\tif (nr_tdx_keyids \u003c 2) {\n1599:\t\t\tpr_err(\"initialization failed: too few private KeyIDs available.\\n\");\n1600:\t\t\treturn;\n1601:\t\t}\n1602:\t\n1603:\t\t/*\n1604:\t\t * At this point, hibernation_available() indicates whether or\n1605:\t\t * not hibernation support has been permanently disabled.\n1606:\t\t */\n1607:\t\tif (hibernation_available()) {\n1608:\t\t\tpr_err(\"initialization failed: Hibernation support is enabled\\n\");\n1609:\t\t\treturn;\n1610:\t\t}\n1611:\t\n1612:\t\terr = register_memory_notifier(\u0026tdx_memory_nb);\n1613:\t\tif (err) {\n1614:\t\t\tpr_err(\"initialization failed: register_memory_notifier() failed (%d)\\n\",\n1615:\t\t\t\t\terr);\n1616:\t\t\treturn;\n1617:\t\t}\n1618:\t\n1619:\t#if defined(CONFIG_ACPI) \u0026\u0026 defined(CONFIG_SUSPEND)\n1620:\t\tpr_info(\"Disable ACPI S3. Turn off TDX in the BIOS to use ACPI S3.\\n\");\n1621:\t\tacpi_suspend_lowlevel = NULL;\n1622:\t#endif\n1623:\t\n1624:\t\t/*\n1625:\t\t * Just use the first TDX KeyID as the 'global KeyID' and\n1626:\t\t * leave the rest for TDX guests.\n1627:\t\t */\n1628:\t\ttdx_global_keyid = tdx_keyid_start;\n1629:\t\ttdx_guest_keyid_start = tdx_keyid_start + 1;\n1630:\t\ttdx_nr_guest_keyids = nr_tdx_keyids - 1;\n1631:\t\n1632:\t\tsetup_force_cpu_cap(X86_FEATURE_TDX_HOST_PLATFORM);\n1633:\t\n1634:\t\tcheck_tdx_erratum();\n1635:\t}\n1636:\t\n1637:\tconst struct tdx_sys_info *tdx_get_sysinfo(void)\n1638:\t{\n1639:\t\tif (!tdx_module_state.initialized)\n"
}

13/2 2026/09/04 02:06 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 23422
							output: 33
							thoughts: 36
						

Thoughts:
Here's my take on those thoughts:

**Analyzing `record_keyid_partitioning` in the TDX Context**

Okay, so I'm diving into this `record_keyid_partitioning` implementation. My immediate question is: how is the Trusted Domain Extension (TDX) host environment actually detected and initialized within this system? That's the crucial context here. I need to understand the connection. Therefore, I need to get back into the code and start my investigations from `record_keyid_partitioning`. Let me check that.



14/2 2026/09/04 02:06 tool grepper
0m Args:
{
  "Expression": "record_keyid_partitioning",
  "PathPrefix": "arch/x86/"
}

Results:
{
  "Output": "arch/x86/virt/vmx/tdx/tdx.c=1467=const char *tdx_dump_mce_info(struct mce *m)\n--\narch/x86/virt/vmx/tdx/tdx.c-1477-\narch/x86/virt/vmx/tdx/tdx.c:1478:static __init int record_keyid_partitioning(u32 *tdx_keyid_start,\narch/x86/virt/vmx/tdx/tdx.c-1479-\t\t\t\t\t    u32 *nr_tdx_keyids)\n--\narch/x86/virt/vmx/tdx/tdx.c=1580=void __init tdx_init(void)\n--\narch/x86/virt/vmx/tdx/tdx.c-1584-\narch/x86/virt/vmx/tdx/tdx.c:1585:\terr = record_keyid_partitioning(\u0026tdx_keyid_start, \u0026nr_tdx_keyids);\narch/x86/virt/vmx/tdx/tdx.c-1586-\tif (err)\n"
}

15/2 2026/09/04 02:06 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 23926
							output: 47
							thoughts: 34
						

Thoughts:
Here's my summary, as though I'm reading the code:

**Diving into `tdx.c` to Understand Key Partitioning**

Alright, let's get into the nitty-gritty of how TDX handles its record key ID partitioning, specifically within the x86 architecture and VMX virtualization. The target is the `record_keyid_partitioning` function, which resides in `arch/x86/virt/vmx/tdx/tdx.c`. My immediate thought is, "Okay, this is where the magic happens, where the TDX module figures out how to slice and dice the record key IDs." I'm expecting to find the core logic for how the system manages and partitions these IDs, likely related to secure memory allocation and access control within the TDX environment. This is definitely a critical component for understanding how TDX ensures the isolation and integrity of protected virtual machines. I'll need to pay close attention to the data structures and algorithms used here, as they'll likely reveal the partitioning strategy and any associated performance considerations.



16/2 2026/09/04 02:06 tool read-file
0m Args:
{
  "File": "arch/x86/virt/vmx/tdx/tdx.c",
  "FirstLine": 1475,
  "LineCount": 50
}

Results:
{
  "Contents": "1475:\t\treturn \"TDX private memory error. Possible kernel bug.\";\n1476:\t}\n1477:\t\n1478:\tstatic __init int record_keyid_partitioning(u32 *tdx_keyid_start,\n1479:\t\t\t\t\t\t    u32 *nr_tdx_keyids)\n1480:\t{\n1481:\t\tu32 _nr_mktme_keyids, _tdx_keyid_start, _nr_tdx_keyids;\n1482:\t\tstruct msr val;\n1483:\t\tint ret;\n1484:\t\n1485:\t\t/*\n1486:\t\t * IA32_MKTME_KEYID_PARTIONING:\n1487:\t\t *   Bit [31:0]:\tNumber of MKTME KeyIDs.\n1488:\t\t *   Bit [63:32]:\tNumber of TDX private KeyIDs.\n1489:\t\t */\n1490:\t\tret = rdmsrq_safe(MSR_IA32_MKTME_KEYID_PARTITIONING, \u0026val.q);\n1491:\t\t_nr_mktme_keyids = val.l;\n1492:\t\t_nr_tdx_keyids = val.h;\n1493:\t\tif (ret || !_nr_tdx_keyids)\n1494:\t\t\treturn -EINVAL;\n1495:\t\n1496:\t\t/* TDX KeyIDs start after the last MKTME KeyID. */\n1497:\t\t_tdx_keyid_start = _nr_mktme_keyids + 1;\n1498:\t\n1499:\t\t*tdx_keyid_start = _tdx_keyid_start;\n1500:\t\t*nr_tdx_keyids = _nr_tdx_keyids;\n1501:\t\n1502:\t\treturn 0;\n1503:\t}\n1504:\t\n1505:\tstatic bool is_tdx_memory(unsigned long start_pfn, unsigned long end_pfn)\n1506:\t{\n1507:\t\tstruct tdx_memblock *tmb;\n1508:\t\n1509:\t\t/*\n1510:\t\t * This check assumes that the start_pfn\u003c-\u003eend_pfn range does not\n1511:\t\t * cross multiple @tdx_memlist entries.  A single memory online\n1512:\t\t * event across multiple memblocks (from which @tdx_memlist\n1513:\t\t * entries are derived at the time of module initialization) is\n1514:\t\t * not possible.  This is because memory offline/online is done\n1515:\t\t * on granularity of 'struct memory_block', and the hotpluggable\n1516:\t\t * memory region (one memblock) must be multiple of memory_block.\n1517:\t\t */\n1518:\t\tlist_for_each_entry(tmb, \u0026tdx_memlist, list) {\n1519:\t\t\tif (start_pfn \u003e= tmb-\u003estart_pfn \u0026\u0026 end_pfn \u003c= tmb-\u003eend_pfn)\n1520:\t\t\t\treturn true;\n1521:\t\t}\n1522:\t\treturn false;\n1523:\t}\n1524:\t\n"
}

17/2 2026/09/04 02:06 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 25098
							output: 32
							thoughts: 650
						

Thoughts:
**Analyzing TDX Host Support in the Kernel**

Okay, let's break this down. First, I see `rdmsrq_safe(MSR_IA32_MKTME_KEYID_PARTITIONING, &val.q)`. This points directly to TDX (Trust Domain Extensions) key ID partitioning, so I need to understand the implications of this, particularly in relation to the host environment. Intel TDX mandates specific hardware: Sapphire Rapids or newer Xeon, with SEAMRR and a TDX module loaded by the firmware.

The immediate question is, can we emulate a TDX host using QEMU? And, a follow-up: can we achieve this within a QEMU instance on a GCE (Google Compute Engine) VM? The answer is a clear negative. Standard QEMU, and therefore GCE VMs, *don't* emulate the necessary hardware components: SEAMRR, the SEAMCALL instruction, the TDX module, or the `MSR_IA32_MKTME_KEYID_PARTITIONING` MSR.

So, where is the focus of this code modification? The patch specifically targets *host* TDX support, as evidenced by the file paths `arch/x86/virt/vmx/tdx/tdx.c` and `arch/x86/kvm/vmx/tdx.c`. But I need to be sure. Can this TDX host code, or KVM TDX host code, be triggered without the presence of physical TDX hardware?

Let's delve deeper into `arch/x86/kvm/mmu/mmu.c`. Within that, I see `mmu_topup_memory_caches` and the call to `kvm_x86_call(topup_external_cache)`. That's interesting. What is `kvm_x86_call(topup_external_cache)` exactly? Examining `arch/x86/include/asm/kvm-x86-ops.h`, it's defined as `KVM_X86_OP_OPTIONAL_RET0(topup_external_cache)`. This signifies an optional call that returns 0 if not implemented.

Now, looking at `arch/x86/kvm/vmx/tdx.c`, I see that `vt_x86_ops.topup_external_cache = tdx_topup_external_pamt_cache;` is assigned *only if* `tdx_supports_dynamic_pamt(tdx_sysinfo)` is true. For non-TDX environments (standard VMX or SVM), `topup_external_cache` is `NULL` (due to `OPTIONAL_RET0`), ensuring a return of 0. That confirms my suspicion.

So, to summarize and confirm my suspicions, I need to know when `vt_x86_ops` or `tdx_hardware_setup` are called. This would illuminate the conditions under which the TDX-specific host code is initialized and executed. I need to search for those identifiers within the codebase to fully understand the execution flow.



18/2 2026/09/04 02:06 tool grepper
0m Args:
{
  "Expression": "tdx_hardware_setup",
  "PathPrefix": "arch/x86/"
}

Results:
{
  "Output": "arch/x86/kvm/vmx/main.c=24=static __init int vt_hardware_setup(void)\n--\narch/x86/kvm/vmx/main.c-31-\narch/x86/kvm/vmx/main.c:32:\treturn enable_tdx ? tdx_hardware_setup() : 0;\narch/x86/kvm/vmx/main.c-33-}\n--\narch/x86/kvm/vmx/tdx.c=3388=void tdx_hardware_unsetup(void)\n--\narch/x86/kvm/vmx/tdx.c-3392-\narch/x86/kvm/vmx/tdx.c:3393:static int __init __tdx_hardware_setup(void)\narch/x86/kvm/vmx/tdx.c-3394-{\n--\narch/x86/kvm/vmx/tdx.c-3466-\narch/x86/kvm/vmx/tdx.c:3467:int __init tdx_hardware_setup(void)\narch/x86/kvm/vmx/tdx.c-3468-{\n--\narch/x86/kvm/vmx/tdx.c-3502-\narch/x86/kvm/vmx/tdx.c:3503:\tr = __tdx_hardware_setup();\narch/x86/kvm/vmx/tdx.c-3504-\tif (r) {\n--\narch/x86/kvm/vmx/tdx.h-9-\narch/x86/kvm/vmx/tdx.h:10:int tdx_hardware_setup(void);\narch/x86/kvm/vmx/tdx.h-11-void tdx_hardware_unsetup(void);\n"
}

19/2 2026/09/04 02:06 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 26967
							output: 45
							thoughts: 31
						

Thoughts:
Here's my take on the thought process, as if it were my own:

**Diving into TDX Setup in KVM/x86**

Okay, time to get my hands dirty. I need to understand how TDX (Trust Domain Extensions) are being set up in the KVM/x86 environment. The logical place to start is within the core VMX code, specifically `arch/x86/kvm/vmx/tdx.c`. I recall that the `tdx_hardware_setup` function is critical here. It's the point where the hardware-specific initializations for TDX are likely occurring. I'll need to scrutinize what's happening around that function; looking at the surrounding code will likely provide clues about the initialization sequence, the interactions with the processor's TDX-related features, and any potential error handling. I expect to find details on enabling TDX, configuring the TDX module, and maybe even setting up the initial guest environment within a TDX context. Let's see what gems are hidden in there!



20/2 2026/09/04 02:06 tool read-file
0m Args:
{
  "File": "arch/x86/kvm/vmx/tdx.c",
  "FirstLine": 3490,
  "LineCount": 45
}

Results:
{
  "Contents": "3490:\t\t\tgoto success_disable_tdx;\n3491:\t\t}\n3492:\t\n3493:\t\tif (!cpu_feature_enabled(X86_FEATURE_OSXSAVE)) {\n3494:\t\t\tpr_err(\"tdx: OSXSAVE is required for TDX\\n\");\n3495:\t\t\tgoto success_disable_tdx;\n3496:\t\t}\n3497:\t\n3498:\t\tif (!cpu_feature_enabled(X86_FEATURE_TDX_HOST_PLATFORM)) {\n3499:\t\t\tpr_err(\"TDX not supported by the host platform\\n\");\n3500:\t\t\tgoto success_disable_tdx;\n3501:\t\t}\n3502:\t\n3503:\t\tr = __tdx_hardware_setup();\n3504:\t\tif (r) {\n3505:\t\t\t/*\n3506:\t\t\t * Disable TDX only but don't fail to load module if the TDX\n3507:\t\t\t * module could not be loaded.  No need to print message saying\n3508:\t\t\t * \"module is not loaded\" because it was printed when the first\n3509:\t\t\t * SEAMCALL failed.  Don't bother unwinding the S-EPT hooks or\n3510:\t\t\t * vm_size, as kvm_x86_ops have already been finalized (and are\n3511:\t\t\t * intentionally not exported).  The S-EPT code is unreachable,\n3512:\t\t\t * and allocating a few more bytes per VM in a should-be-rare\n3513:\t\t\t * failure scenario is a non-issue.\n3514:\t\t\t */\n3515:\t\t\tif (r == -ENODEV)\n3516:\t\t\t\tgoto success_disable_tdx;\n3517:\t\n3518:\t\t\treturn r;\n3519:\t\t}\n3520:\t\n3521:\t\tKVM_SANITY_CHECK_VM_STRUCT_SIZE(kvm_tdx);\n3522:\t\n3523:\t\tvt_x86_ops.vm_size = max_t(unsigned int, vt_x86_ops.vm_size, sizeof(struct kvm_tdx));\n3524:\t\n3525:\t\tvt_x86_ops.set_external_spte = tdx_sept_set_private_spte;\n3526:\t\tvt_x86_ops.free_external_spt = tdx_sept_free_private_spt;\n3527:\t\n3528:\t\tif (tdx_supports_dynamic_pamt(tdx_sysinfo))\n3529:\t\t\tvt_x86_ops.topup_external_cache = tdx_topup_external_pamt_cache;\n3530:\t\n3531:\t\tvt_x86_ops.protected_apic_has_interrupt = tdx_protected_apic_has_interrupt;\n3532:\t\treturn 0;\n3533:\t\n3534:\tsuccess_disable_tdx:\n"
}

21/2 2026/09/04 02:06 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 28063
							output: 44
							thoughts: 1308
						

Thoughts:
**Analyzing TDX Support in a Virtualized Environment**

Okay, let's break this down. First, I need to understand how dynamic PAMT (Page Attribute Table) support in TDX is enabled. `tdx_supports_dynamic_pamt` is the key function. It hinges on `tdx_enable_dpamt` and, more importantly, the `TDX_FEATURES0_DYNAMIC_PAMT` feature flag reported by the TDX module itself via SEAMCALLs.  And, of course, `tdx_enable_dpamt` is off by default, so that's the starting point to consider.

The TDX host platform also requires specific hardware capabilities: physical CPU support (`X86_FEATURE_TDX_HOST_PLATFORM`), the MSR `MSR_IA32_MKTME_KEYID_PARTITIONING`, proper SEAMRR range registers configured in the BIOS, and functioning SEAMCALLs to the Intel TDX firmware module.

Now, the critical question: Does the "structurally unreachable hardware" identified in section 2 of the original prompt include the Intel TDX host?  My immediate thought is *yes*.

Let's verify that. Can standard QEMU emulate a TDX host? No. QEMU can only function as a TDX guest using KVM TDX running on a real TDX host.  QEMU TCG simply lacks the necessary support – no SEAMRR, no SEAMCALL functionality, and no MKTME partitioning MSR emulation.  In any standard QEMU/KVM nested virtualization scenario or QEMU TCG, `X86_FEATURE_TDX_HOST_PLATFORM` will not be present. The `rdmsrq_safe(MSR_IA32_MKTME_KEYID_PARTITIONING)` will inevitably fail, because the hardware is not present.

Therefore, the `tdx_init()` function would fail early on with `record_keyid_partitioning`. The key bit here is the following code block:

```c
ret = rdmsrq_safe(MSR_IA32_MKTME_KEYID_PARTITIONING, &val.q);
if (ret || !_nr_tdx_keyids)
    return -EINVAL;
```

Consequently, the `X86_FEATURE_TDX_HOST_PLATFORM` flag will never be set. The `tdx_enable()` function will fail immediately, and `tdx_hardware_setup()` will fail as well. This leads to `vt_x86_ops.topup_external_cache` remaining NULL. As a result, all the functions related to dynamic PAMT – `tdx_pamt_get`, `tdx_pamt_put`, `tdx_alloc_control_page`, `tdx_free_control_page`, etc. – will never be called. Even if they *were* called, `tdx_supports_dynamic_pamt` would return `false` immediately anyway (returning 0 immediately). The TDX KVM module, will never even be able to create a TDX VM without `enable_tdx`.

Now, let's investigate the recent change in `arch/x86/kvm/mmu/mmu.c`. My thinking now is that this patch introduces a new function pointer, `topup_external_cache` which can point to a function to handle external caching operations.

```c
		r = kvm_x86_call(topup_external_cache)(vcpu, PT64_ROOT_MAX_LEVEL);
		if (r)
			return r;
```

I will need to investigate exactly which functions can be set as `topup_external_cache`.



22/2 2026/09/04 02:06 tool read-file
0m Args:
{
  "File": "arch/x86/kvm/mmu/mmu.c",
  "FirstLine": 605,
  "LineCount": 35
}

Results:
{
  "Contents": " 605:\t\n 606:\tstatic int mmu_topup_memory_caches(struct kvm_vcpu *vcpu, bool maybe_indirect)\n 607:\t{\n 608:\t\tint r;\n 609:\t\n 610:\t\t/* 1 rmap, 1 parent PTE per level, and the prefetched rmaps. */\n 611:\t\tr = kvm_mmu_topup_memory_cache(\u0026vcpu-\u003earch.mmu_pte_list_desc_cache,\n 612:\t\t\t\t\t       1 + PT64_ROOT_MAX_LEVEL + PTE_PREFETCH_NUM);\n 613:\t\tif (r)\n 614:\t\t\treturn r;\n 615:\t\tif (kvm_has_mirrored_tdp(vcpu-\u003ekvm)) {\n 616:\t\t\tr = kvm_mmu_topup_memory_cache(\u0026vcpu-\u003earch.mmu_external_spt_cache,\n 617:\t\t\t\t\t\t       PT64_ROOT_MAX_LEVEL);\n 618:\t\t\tif (r)\n 619:\t\t\t\treturn r;\n 620:\t\n 621:\t\t\tr = kvm_x86_call(topup_external_cache)(vcpu, PT64_ROOT_MAX_LEVEL);\n 622:\t\t\tif (r)\n 623:\t\t\t\treturn r;\n 624:\t\t}\n 625:\t\tr = kvm_mmu_topup_memory_cache(\u0026vcpu-\u003earch.mmu_shadow_page_cache,\n 626:\t\t\t\t\t       PT64_ROOT_MAX_LEVEL);\n 627:\t\tif (r)\n 628:\t\t\treturn r;\n 629:\t\tif (maybe_indirect) {\n 630:\t\t\tr = kvm_mmu_topup_memory_cache(\u0026vcpu-\u003earch.mmu_shadowed_info_cache,\n 631:\t\t\t\t\t\t       PT64_ROOT_MAX_LEVEL);\n 632:\t\t\tif (r)\n 633:\t\t\t\treturn r;\n 634:\t\t}\n 635:\t\treturn kvm_mmu_topup_memory_cache(\u0026vcpu-\u003earch.mmu_page_header_cache,\n 636:\t\t\t\t\t\t  PT64_ROOT_MAX_LEVEL);\n 637:\t}\n 638:\t\n 639:\tstatic void mmu_free_memory_caches(struct kvm_vcpu *vcpu)\n"
}

23/2 2026/09/04 02:06 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 30873
							output: 37
							thoughts: 56
						

Thoughts:
**Analyzing KVM's Mirrored TDP Implementation**

Okay, so I'm looking at this piece of KVM code, and the first line that jumps out at me is `if (kvm_has_mirrored_tdp(vcpu->kvm))`. My immediate thought is, "Right, *kvm_has_mirrored_tdp*... what's that actually *do*?" I need to understand this check before I can truly grasp the surrounding logic.

Time to reach for the trusty grep command. I'll need to locate the definition of `kvm_has_mirrored_tdp` within the KVM source. That should give me the crucial context I need to figure out the intent and implications of this conditional statement. Once I have the definition, I can start to dissect how the mirrored TDP functionality is enabled or disabled, and more importantly, *why* we might be checking for it here.



24/2 2026/09/04 02:06 tool grepper
0m Args:
{
  "Expression": "kvm_has_mirrored_tdp",
  "PathPrefix": "arch/x86/kvm"
}

Results:
{
  "Output": "arch/x86/kvm/mmu.h=380=static inline gpa_t kvm_translate_gpa(struct kvm_vcpu *vcpu,\n--\narch/x86/kvm/mmu.h-391-\narch/x86/kvm/mmu.h:392:static inline bool kvm_has_mirrored_tdp(const struct kvm *kvm)\narch/x86/kvm/mmu.h-393-{\n--\narch/x86/kvm/mmu/mmu.c=606=static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu, bool maybe_indirect)\n--\narch/x86/kvm/mmu/mmu.c-614-\t\treturn r;\narch/x86/kvm/mmu/mmu.c:615:\tif (kvm_has_mirrored_tdp(vcpu-\u003ekvm)) {\narch/x86/kvm/mmu/mmu.c-616-\t\tr = kvm_mmu_topup_memory_cache(\u0026vcpu-\u003earch.mmu_external_spt_cache,\n--\narch/x86/kvm/mmu/mmu.c=3990=static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/mmu/mmu.c-3998-\tif (tdp_mmu_enabled) {\narch/x86/kvm/mmu/mmu.c:3999:\t\tif (kvm_has_mirrored_tdp(vcpu-\u003ekvm) \u0026\u0026\narch/x86/kvm/mmu/mmu.c-4000-\t\t    !VALID_PAGE(mmu-\u003emirror_root_hpa))\n--\narch/x86/kvm/mmu/tdp_mmu.h=30=static inline enum kvm_tdp_mmu_root_types kvm_gfn_range_filter_to_root_types(struct kvm *kvm,\n--\narch/x86/kvm/mmu/tdp_mmu.h-34-\narch/x86/kvm/mmu/tdp_mmu.h:35:\tif (!kvm_has_mirrored_tdp(kvm))\narch/x86/kvm/mmu/tdp_mmu.h-36-\t\treturn KVM_DIRECT_ROOTS;\n"
}

25/2 2026/09/04 02:06 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 31794
							output: 41
							thoughts: 31
						

Thoughts:
Here's my attempt at summarizing the thought process, tailored for an expert audience:

**Investigating Potential Mirrored TDP Issues in KVM**

Okay, so I'm diving into the KVM x86 code to understand how Transparent Page Sharing (TPS) and Translation Lookaside Buffer (TLB) management are interacting. Specifically, I need to check the potential implications of mirrored Translation Lookaside Buffer (TLB) entries within the KVM hypervisor. I'm going to start by confirming the existence and definition of `kvm_has_mirrored_tdp` in `arch/x86/kvm/mmu.h`. This likely flags whether the current system configuration enables or supports mirrored page tables, potentially influencing the MMU's behavior and performance, and importantly, which code paths are actively executed in the system. The specifics of the check will be key. I'll need to understand what conditions determine the value returned by this function, and then trace down any dependencies.



26/2 2026/09/04 02:06 tool read-file
0m Args:
{
  "File": "arch/x86/kvm/mmu.h",
  "FirstLine": 390,
  "LineCount": 20
}

Results:
{
  "Contents": " 390:\t}\n 391:\t\n 392:\tstatic inline bool kvm_has_mirrored_tdp(const struct kvm *kvm)\n 393:\t{\n 394:\t\treturn kvm-\u003earch.vm_type == KVM_X86_TDX_VM;\n 395:\t}\n 396:\t\n 397:\tstatic inline gfn_t kvm_gfn_direct_bits(const struct kvm *kvm)\n 398:\t{\n 399:\t\treturn kvm-\u003earch.gfn_direct_bits;\n 400:\t}\n 401:\t\n 402:\tstatic inline bool kvm_is_addr_direct(struct kvm *kvm, gpa_t gpa)\n 403:\t{\n 404:\t\tgpa_t gpa_direct_bits = gfn_to_gpa(kvm_gfn_direct_bits(kvm));\n 405:\t\n 406:\t\treturn !gpa_direct_bits || (gpa \u0026 gpa_direct_bits);\n 407:\t}\n 408:\t\n 409:\tstatic inline bool kvm_is_gfn_alias(struct kvm *kvm, gfn_t gfn)\n"
}

27/2 2026/09/04 02:06 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 32418
							output: 39
							thoughts: 93
						

Thoughts:
Okay, here's my understanding, framed as my internal thought process:

**KVM TDX VM Creation Logic**

Alright, let's break this down. The `kvm_has_mirrored_tdp(const struct kvm *kvm)` function is a simple check; it essentially tells us if a KVM instance is a TDX VM by comparing its architecture's VM type with `KVM_X86_TDX_VM`. Straightforward enough. But now I need to understand *when* and *how* a VM is actually assigned the `KVM_X86_TDX_VM` type during the creation process. The key is to trace where `KVM_X86_TDX_VM` is being utilized during the VM's instantiation. I'll need to dig into the KVM VM creation flow to determine the conditions that lead to this specific type being assigned. I'm going to follow the logic and determine the exact path by tracing the code!



28/2 2026/09/04 02:06 tool grepper
0m Args:
{
  "Expression": "KVM_X86_TDX_VM",
  "PathPrefix": "arch/x86/kvm"
}

Results:
{
  "Output": "arch/x86/kvm/mmu.h=392=static inline bool kvm_has_mirrored_tdp(const struct kvm *kvm)\narch/x86/kvm/mmu.h-393-{\narch/x86/kvm/mmu.h:394:\treturn kvm-\u003earch.vm_type == KVM_X86_TDX_VM;\narch/x86/kvm/mmu.h-395-}\n--\narch/x86/kvm/mmu/mmu.c=5958=static inline int kvm_mmu_get_tdp_level(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/mmu/mmu.c-5961-\narch/x86/kvm/mmu/mmu.c:5962:\tif (vcpu-\u003ekvm-\u003earch.vm_type == KVM_X86_TDX_VM)\narch/x86/kvm/mmu/mmu.c-5963-\t\tmaxpa = cpuid_query_maxguestphyaddr(vcpu);\n--\narch/x86/kvm/mmu/page_track.c=189=static int kvm_enable_external_write_tracking(struct kvm *kvm)\n--\narch/x86/kvm/mmu/page_track.c-194-\narch/x86/kvm/mmu/page_track.c:195:\tif (kvm-\u003earch.vm_type == KVM_X86_TDX_VM)\narch/x86/kvm/mmu/page_track.c-196-\t\treturn -EOPNOTSUPP;\n--\narch/x86/kvm/vmx/common.h=60=static __always_inline bool is_td(struct kvm *kvm)\narch/x86/kvm/vmx/common.h-61-{\narch/x86/kvm/vmx/common.h:62:\treturn kvm-\u003earch.vm_type == KVM_X86_TDX_VM;\narch/x86/kvm/vmx/common.h-63-}\n--\narch/x86/kvm/vmx/main.c=1058=static int __init vt_init(void)\n--\narch/x86/kvm/vmx/main.c-1078-\t\t\t\t__alignof__(struct vcpu_tdx));\narch/x86/kvm/vmx/main.c:1079:\t\tkvm_caps.supported_vm_types |= BIT(KVM_X86_TDX_VM);\narch/x86/kvm/vmx/main.c-1080-\t}\n"
}

29/2 2026/09/04 02:06 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 33473
							output: 44
							thoughts: 29
						

Thoughts:
Here's my summary, as if I were thinking these thoughts:

**Diving into KVM/VMX Core Logic**

Okay, I need to get to the bottom of this. Let's start by zeroing in on the x86 KVM/VMX code. My immediate thought is to examine `arch/x86/kvm/vmx/main.c`, and specifically around line 1070. That area usually deals with... well, it depends on the context, but it could relate to VM entry/exit handling, hardware virtualization setup, or potentially fault management specific to Intel's VT-x extensions. Knowing how deeply integrated and architecturally important VMX is, I bet this part of the code is crucial. From there I can follow the call path to understand what's happening and how this relates to my specific problem.



30/2 2026/09/04 02:06 tool read-file
0m Args:
{
  "File": "arch/x86/kvm/vmx/main.c",
  "FirstLine": 1060,
  "LineCount": 35
}

Results:
{
  "Contents": "1060:\t\tunsigned vcpu_size, vcpu_align;\n1061:\t\tint r;\n1062:\t\n1063:\t\tr = vmx_init();\n1064:\t\tif (r)\n1065:\t\t\treturn r;\n1066:\t\n1067:\t\t/*\n1068:\t\t * TDX and VMX have different vCPU structures.  Calculate the\n1069:\t\t * maximum size/align so that kvm_init() can use the larger\n1070:\t\t * values to create the kmem_vcpu_cache.\n1071:\t\t */\n1072:\t\tvcpu_size = sizeof(struct vcpu_vmx);\n1073:\t\tvcpu_align = __alignof__(struct vcpu_vmx);\n1074:\t\tif (enable_tdx) {\n1075:\t\t\tvcpu_size = max_t(unsigned, vcpu_size,\n1076:\t\t\t\t\tsizeof(struct vcpu_tdx));\n1077:\t\t\tvcpu_align = max_t(unsigned, vcpu_align,\n1078:\t\t\t\t\t__alignof__(struct vcpu_tdx));\n1079:\t\t\tkvm_caps.supported_vm_types |= BIT(KVM_X86_TDX_VM);\n1080:\t\t}\n1081:\t\n1082:\t\t/*\n1083:\t\t * Common KVM initialization _must_ come last, after this, /dev/kvm is\n1084:\t\t * exposed to userspace!\n1085:\t\t */\n1086:\t\tr = kvm_init(vcpu_size, vcpu_align, THIS_MODULE);\n1087:\t\tif (r)\n1088:\t\t\tgoto err_kvm_init;\n1089:\t\n1090:\t\treturn 0;\n1091:\t\n1092:\terr_kvm_init:\n1093:\t\tvmx_exit();\n1094:\t\treturn r;\n"
}

31/2 2026/09/04 02:06 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 34256
							output: 32
							thoughts: 41
						

Thoughts:
Here's my thought process, distilled:

**Navigating the TDX Implementation in KVM**

Okay, so I'm diving into the TDX implementation, specifically within KVM's VMX code. My immediate focus is on the conditional check: `if (enable_tdx)` in `arch/x86/kvm/vmx/main.c`. Clearly, this is a pivotal decision point related to whether or not TDX features are activated during VM execution.

My next logical step is to track down where the `enable_tdx` variable is actually being set. This is crucial for understanding the conditions under which TDX is enabled within the KVM environment. I need to trace back through the code, ideally starting from this check, to determine the initialization path and how the TDX enabling decision is ultimately made. Knowing the source of `enable_tdx` will provide insights into configuration options, hardware checks, or other runtime conditions that influence TDX’s operational state. I suspect this will be either a configuration flag or a determination made during initialization based on hardware capabilities and security policies.



32/2 2026/09/04 02:06 tool grepper
0m Args:
{
  "Expression": "enable_tdx",
  "PathPrefix": "arch/x86/kvm"
}

Results:
{
  "Output": "arch/x86/kvm/vmx/main.c=16=static void vt_disable_virtualization_cpu(void)\n--\narch/x86/kvm/vmx/main.c-18-\t/* Note, TDX *and* VMX need to be disabled if TDX is enabled. */\narch/x86/kvm/vmx/main.c:19:\tif (enable_tdx)\narch/x86/kvm/vmx/main.c-20-\t\ttdx_disable_virtualization_cpu();\n--\narch/x86/kvm/vmx/main.c=24=static __init int vt_hardware_setup(void)\n--\narch/x86/kvm/vmx/main.c-31-\narch/x86/kvm/vmx/main.c:32:\treturn enable_tdx ? tdx_hardware_setup() : 0;\narch/x86/kvm/vmx/main.c-33-}\n--\narch/x86/kvm/vmx/main.c=35=static void vt_hardware_unsetup(void)\narch/x86/kvm/vmx/main.c-36-{\narch/x86/kvm/vmx/main.c:37:\tif (enable_tdx)\narch/x86/kvm/vmx/main.c-38-\t\ttdx_hardware_unsetup();\n--\narch/x86/kvm/vmx/main.c=1058=static int __init vt_init(void)\n--\narch/x86/kvm/vmx/main.c-1073-\tvcpu_align = __alignof__(struct vcpu_vmx);\narch/x86/kvm/vmx/main.c:1074:\tif (enable_tdx) {\narch/x86/kvm/vmx/main.c-1075-\t\tvcpu_size = max_t(unsigned, vcpu_size,\n--\narch/x86/kvm/vmx/tdx.c-55-\narch/x86/kvm/vmx/tdx.c:56:bool enable_tdx __ro_after_init;\narch/x86/kvm/vmx/tdx.c:57:module_param_named(tdx, enable_tdx, bool, 0444);\narch/x86/kvm/vmx/tdx.c-58-\n--\narch/x86/kvm/vmx/tdx.c=3467=int __init tdx_hardware_setup(void)\n--\narch/x86/kvm/vmx/tdx.c-3474-\narch/x86/kvm/vmx/tdx.c:3475:\tif (!enable_tdx)\narch/x86/kvm/vmx/tdx.c-3476-\t\treturn 0;\n--\narch/x86/kvm/vmx/tdx.c-3534-success_disable_tdx:\narch/x86/kvm/vmx/tdx.c:3535:\tenable_tdx = 0;\narch/x86/kvm/vmx/tdx.c-3536-\treturn 0;\n--\narch/x86/kvm/vmx/tdx.h=11=void tdx_hardware_unsetup(void);\narch/x86/kvm/vmx/tdx.h-12-\narch/x86/kvm/vmx/tdx.h:13:extern bool enable_tdx;\narch/x86/kvm/vmx/tdx.h-14-\n--\narch/x86/kvm/vmx/tdx.h=187=TDX_BUILD_TDVPS_ACCESSORS(64, STATE_NON_ARCH, state_non_arch);\n--\narch/x86/kvm/vmx/tdx.h-189-#else\narch/x86/kvm/vmx/tdx.h:190:#define enable_tdx\t0\narch/x86/kvm/vmx/tdx.h-191-\n"
}

33/2 2026/09/04 02:06 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 35641
							output: 219
							thoughts: 1909
						

Thoughts:
**Analysis of Code Reachability for TDX Dynamic PAMT Support**

Okay, let's break this down. I've been looking at the implications of this new Dynamic PAMT support for Intel TDX, focusing on its reachability in a fuzzing context. From examining `arch/x86/kvm/vmx/tdx.c` and related code, specifically lines 3535, a clear picture emerges. The core of it boils down to this: without specific Intel hardware – a physical TDX-enabled host – this code path *never* gets hit.

The `tdx_hardware_setup()` function is critical. It checks for `X86_FEATURE_TDX_HOST_PLATFORM`. If that flag isn't set, which it won't be on standard hardware or QEMU, the setup fails and `enable_tdx` is set to 0. This, in turn, prevents `KVM_X86_TDX_VM` from being supported. Consequently, creating a TDX VM through `ioctl(KVM_CREATE_VM_TYPE)` will fail with `-EINVAL`, making `kvm_has_mirrored_tdp(vcpu->kvm)` always false.

This crucial `kvm_has_mirrored_tdp()` check is the gatekeeper. The MMU branch containing the calls to `kvm_mmu_topup_memory_cache` and `kvm_x86_call(topup_external_cache)` in `mmu_topup_memory_caches` is completely bypassed.  Furthermore, all the new functions tied to dynamic PAMT (e.g., `tdx_pamt_get`, `tdx_pamt_put`, the allocation and freeing functions, etc.) are unreachable. The kernel parameters for dynamic PAMT are only parsed if TDX is enabled on the host platform.

I've double-checked the boot-time functions modified, and they all fall into the same trap. Functions like `tdmr_get_pamt_sz` and `init_tdx_module()` are dependent on the `X86_FEATURE_TDX_HOST_PLATFORM` flag. The same applies to those KVM functions like `tdx_vcpu_create` and `tdx_hardware_setup`.  These all operate within the TDX host context and KVM TDX VM setup.

Now, let's apply the "Structurally unreachable hardware" guidelines. This code unequivocally fits. It requires specific, vendor-specific hardware – an Intel TDX host platform with the proper BIOS support and firmware module. This goes beyond the capabilities of standard QEMU or GCE environments used for fuzzing. It's not just that a feature isn't *enabled*, but that the foundational *hardware* isn't present.

Therefore, my conclusion is clear: `WorthFuzzing` should be set to `false`. The modified code is structurally unreachable. The patch is specifically targeting a hardware environment that isn't replicated in the typical fuzzing setup.



34/2 2026/09/04 02:07 tool set-results
0m Args:
{
  "EnableConfigs": [],
  "FocusSymbols": [],
  "Reasoning": "The patch series introduces Dynamic PAMT (DPAMT) support for Intel TDX (Trust Domain Extensions) host in the kernel and KVM. The modified code is entirely confined to Intel TDX host initialization and KVM TDX guest management (under arch/x86/virt/vmx/tdx and arch/x86/kvm/vmx/tdx.c, plus mirrored TDP handling guarded by KVM_X86_TDX_VM). These paths strictly require physical Intel CPUs with TDX host platform support (X86_FEATURE_TDX_HOST_PLATFORM, MSR_IA32_MKTME_KEYID_PARTITIONING, SEAMRR, and the Intel TDX firmware module), which are not present or emulated in standard QEMU/GCE fuzzing environments. Consequently, the modified code is structurally unreachable for fuzzing.",
  "WorthFuzzing": false
}

Results:
{
  "EnableConfigs": null,
  "FocusSymbols": [],
  "Reasoning": "The patch series introduces Dynamic PAMT (DPAMT) support for Intel TDX (Trust Domain Extensions) host in the kernel and KVM. The modified code is entirely confined to Intel TDX host initialization and KVM TDX guest management (under arch/x86/virt/vmx/tdx and arch/x86/kvm/vmx/tdx.c, plus mirrored TDP handling guarded by KVM_X86_TDX_VM). These paths strictly require physical Intel CPUs with TDX host platform support (X86_FEATURE_TDX_HOST_PLATFORM, MSR_IA32_MKTME_KEYID_PARTITIONING, SEAMRR, and the Intel TDX firmware module), which are not present or emulated in standard QEMU/GCE fuzzing environments. Consequently, the modified code is structurally unreachable for fuzzing.",
  "WorthFuzzing": false
}

35/1 2026/09/04 02:07 action If
0m Args:
{
  "WorthFuzzing": false
}

Results:
null

LLM Calls Summary:
Total Calls Total Tokens Avg Tokens Total Duration (Seconds) Avg Duration (Seconds)
Tool Calls Summary:
Total Calls Total Duration (Seconds) Avg Duration (Seconds)