Enable TDX module extensions
Subsystems (3)
v3 cover Enable TDX module extensions
Hi,
This is another respin of the TDX module extensions series. Thank you all for the design discussion and architectural review. During the last version, some architecture discussions lead to changes. And the need to support DPAMT creates more touch points. But the all the design issues are settled, we don't have any major opens.
So Kiryl, Rick and TDX developers, please help review. If there is no major change, I hope to get your RBs and then ask Dave to look at this version.
== Changes ==
One change is that the extensions re-initialization patch is picked back into this series. In v2, the extensions re-initialization operation (TDH.EXT.INIT) was expected to be integrated in TDH.SYS.UPDATE. Host didn't have to explicitly invoke it on update anymore. But later an off-list discussion concludes that the separation of TDH.SYS.UPDATE & TDH.EXT.INIT brings more flexibility for needs from different types of VMMs, such as reducing the black-out time of update, allowing for new feature enabling... So now the host needs to call TDH.EXT.INIT right after TDH.SYS.UPDATE to restore the extensions functionalities.
However the add-on feature re-configuration is orthogonal to the update flow change. We should still start with the basic update functionality that restores the previous settings. Re-configuring new feature is a separate functionality and the use case is unclear now. So I still expect that a TDH.SYS.UPDATE v0 would restore add-on features that was configured during boot.
Rick pointed out that if TDH.EXT.INIT could always return a reasonable return code, we don't have to query ext_required metadata. A fix for the ABI is: TDH.EXT.INIT should return success if no extensions are required. We reached agreement with TDX module team and removed ext_required. After ext_required removal, the v2:Patch 3 became too trivial so merge it into v2:Patch 4.
Rick raised a question that a single 4K allocation per iteration on boot may not cause much fragmentation. Kiryl answered CONFIG_SHUFFLE_PAGE_ALLOCATOR may affect the actual behavior but we'd better verify this in practice. The test result shows the allocation is affected by several aspects, e.g. MPOL_INTERLEAVED, per-CPU page cache. The allocated pages were scattered across many more page blocks than expected.
The DPAMT is now queued in tip x86/tdx and will be default on in next rc1. Adding memory to the extensions without first installing DPAMT will trigger SEAMCALL failure and in turn fail the whole TDX module initialization. Add a new patch to support DPAMT.
Other changes:
- Patch 1: sizeof(tdmr_pa_array->phys[0]) instead of sizeof(u64)
(Rick)
- Patch 1: Re-phrase the code comments for struct tdmr_info_pa_array
(Tony)
- Patch 2: Update the stale changlog for bitmap arg of the wrapper
(Chao)
- Patch 2: s/get_tdx_addon_features0()/get_tdx_usable_addon_features0()
(Tony)
- Patch 3: Don't save the metadata in tdx_sysinfo (Rick)
- Patch 3: Tweak the code comment for memory_pool_required_pages == 0
(Chao)
- Patch 3: Re-phrase the code comment for struct tdx_hpa_list (Tony)
- Patch 5: Rename the reinit function as reinit_tdx_module_extensions()
(Tony)
- Patch 5: Move the extensions re-init under TDH.SYS.UPDATE (Rick)
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
Quoting v2: https://lore.kernel.org/lkml/[email protected]/
Quoting v1: https://lore.kernel.org/all/[email protected]/
== Overview ==
To date, SEAMCALL execution must either complete quickly to avoid stalling the host, or yield quickly at pre-defined interrupt checkpoints. This is acceptable for the existing SEAMCALL leafs, which perform simple, bounded operations.
However, some new features such as attestation and TD migration require higher level security protocols inside the TDX module, which cannot fit within that constraint. TDX solves this by making those operations inherently preemptible and resumable like OS tasks. TDX provides a separate SEAMCALL execution environment - the TDX module extensions - for those operations.
This capability allows for higher-level SEAMCALL ABI design - like "create a DICE-based attestation quote". Several new features, such as DICE-based attestation, TDISP and TD migration, use SEAMCALL leafs backed by the TDX module extensions.
The TDX module extensions need memory for their execution environment to serve these SEAMCALL leafs, so they need extra setup steps during TDX module initialization. The bulk of this series implements these setup steps.
At runtime, the host invokes these SEAMCALL leafs just as normal ones - if interrupted, simply re-invoke the leaf to resume.
For more information on TDX module extensions, please refer to [1].
[1] https://lore.kernel.org/lkml/[email protected]/
== Branch stack ==
This is based on tip x86/tdx. You can find the full branch stack at [2]. The DICE part is in the full branch as an example for extensions. But it does not include the other DICE feedbacks.
The full branch contains:
This series: Patch 1~7: This series, including this cover-letter.
Use case: Patch 8~N: The old DICE part as an example.
[2] https://github.com/intel-staging/tdx/tree/tdx-module-ext
---
Xu Yilun (6):
x86/virt/tdx: Move TDH.SYS.CONFIG operations into a wrapper x86/virt/tdx: Configure add-on features on TDX module init x86/virt/tdx: Add extra memory to TDX module for the extensions x86/virt/tdx: Make TDX module initialize the extensions x86/virt/tdx: Re-initialize the extensions on runtime TDX module update x86/virt/tdx: Support DPAMT when adding memory for the extensions
arch/x86/include/asm/tdx.h | 1 + arch/x86/include/asm/tdx_global_metadata.h | 4 + arch/x86/virt/vmx/tdx/tdx.h | 2 + arch/x86/virt/vmx/tdx/tdx.c | 252 ++++++++++++++++++++++++++-- arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 14 ++ 5 files changed, 259 insertions(+), 14 deletions(-)
---
base-commit: 5b8212d45d99b77c84e3ad305a295e9e65d5ef20
change-id: 20260916-tdx-module-ext-da1930f33c04
Best regards,
Signature · 1 line
Xu Yilun <[email protected]>
PATCH v3 1/6 x86/virt/tdx: Move TDH.SYS.CONFIG operations into a wrapper
In Linux, SEAMCALL wrappers are introduced to avoid broad SEAMCALL access by exposing only a selection of SEAMCALL leafs, but also to abstract the SEAMCALL register ABIs. The abstraction improves readability and reuse for SEAMCALL leafs that are called multiple times.
Some SEAMCALL leafs are not explicitly wrapped because the level of TDX ABI details needed to perform the call is low enough to flow well with the calling code.
For some of the currently unwrapped SEAMCALL leafs, the TDX architecture has evolved the ABI to introduce new leaf versions. The host can select the version to call based on what is supported by the loaded TDX module. When future kernel implements this version selection, more ABI details will leak into the surrounding caller code, which decreases the readability of the caller logic. To keep the ABI details contained, move the SEAMCALL leaf that will need version selection into a wrapper.
When defining the wrapper, the cleanest separation would be to have kernel data types for the SEAMCALL wrapper arguments, and have them marshaled into SEAMCALL leaf ABI types (often u64s) inside the wrapper. This works for many SEAMCALL leafs but becomes cumbersome when the register ABI type is a physical address which points to a buffer for an in-memory ABI. If the SEAMCALL wrapper only accepts kernel data types, it may need duplicate buffer allocation and copies to match the in-memory ABI. Another solution is to define a named helper structure that mirrors the in-memory ABI, populate it in a separate flow, then pass it to the SEAMCALL wrapper. struct seamldr_params is an existing example of this pattern.
TDH.SYS.CONFIG requires a list of TDMR information in the form of a PA array. The PA array is the in-memory ABI. Create a structure for the PA array, use it as the argument when creating the wrapper for TDH.SYS.CONFIG.
For readability, adjust a bit of the existing PA array size calculation:
use the new PA array structure's element type instead of hardcoding u64.
---
v3:
- sizeof(tdmr_pa_array->phys[0]) instead of sizeof(u64) (Rick)
- Re-phrase the code comments for struct tdmr_info_pa_array (Tony)
- Refactor the changelog
- Collect Reviewed-by tags
v2:
- Remove TDH.SYS.UPDATE wrapper (Dave & Rick)
- Talk about the handling of in-memory ABIs for SEAMCALL wrappers (Rick)
- Refactor the entire changelog according to Rick's suggestion (Rick)
- Add code comment for struct tdmr_info_pa_array (AI nitpicker)
- Use kernel data type for nr_tdmr_pa parameter (AI nitpicker)
v1:
- This patch is split out from the last series (Rick)
--- arch/x86/virt/vmx/tdx/tdx.c | 46 +++++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 14 deletions(-)
arch/x86/virt/vmx/tdx/tdx.c +32 -14
@@ -1033,11 +1033,36 @@ static __init int construct_tdmrs(struct list_head *tmb_list,#define TDX_SYS_CONFIG_DYNAMIC_PAMT BIT(16) +/* +* TDX module in-memory ABI to specify the TD Memory Regions (TDMRs) and their +* associated PAMT memory. An array of HPAs, each element points to a +* TDMR_INFO, see struct tdmr_info. +*/ +struct tdmr_info_pa_array { + DECLARE_FLEX_ARRAY(u64, phys); +}; + +static __init int tdx_sys_config (struct tdmr_info_pa_array *tdmr_pa_array, +unsigned int nr_tdmr_pa,u64 global_keyid) +{ +struct tdx_module_args args = { + .rcx =__pa (tdmr_pa_array), + .rdx = nr_tdmr_pa, + .r8 = global_keyid, + }; + +if (tdx_supports_dynamic_pamt (&tdx_sysinfo)) { +pr_info ("Enable Dynamic PAMT\n" ); + args.r8 |=TDX_SYS_CONFIG_DYNAMIC_PAMT ; + } + +return seamcall_prerr (TDH_SYS_CONFIG , &args); +} +static __init int config_tdx_module (struct tdmr_info_list *tdmr_list,u64 global_keyid) { -struct tdx_module_args args = {}; -u64 *tdmr_pa_array; +struct tdmr_info_pa_array *tdmr_pa_array;size_t array_sz;int i, ret;
@@ -1046,7 +1071,8 @@ static __init int config_tdx_module(struct tdmr_info_list *tdmr_list,* addresses of each TDMR. The array itself also has certain * alignment requirement. */ - array_sz = tdmr_list->nr_consumed_tdmrs *sizeof (u64); + array_sz = tdmr_list->nr_consumed_tdmrs * +sizeof (tdmr_pa_array->phys[0 ]); array_sz =roundup_pow_of_two (array_sz);if (array_sz <TDMR_INFO_PA_ARRAY_ALIGNMENT ) array_sz =TDMR_INFO_PA_ARRAY_ALIGNMENT ;
@@ -1056,18 +1082,10 @@ static __init int config_tdx_module(struct tdmr_info_list *tdmr_list,return -ENOMEM ;for (i =0 ; i < tdmr_list->nr_consumed_tdmrs; i++) - tdmr_pa_array[i] =__pa (tdmr_entry (tdmr_list, i)); - - 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 ; - } + tdmr_pa_array->phys[i] =__pa (tdmr_entry (tdmr_list, i)); - ret =seamcall_prerr (TDH_SYS_CONFIG , &args); + ret =tdx_sys_config (tdmr_pa_array, tdmr_list->nr_consumed_tdmrs, + global_keyid);/* Free the array as it is not required anymore. */ kfree (tdmr_pa_array);
Signature · 1 line
2.25.1
PATCH v3 2/6 x86/virt/tdx: Configure add-on features on TDX module init
In reply to Xu YilunThe TDX architecture identifies some features that are off by default but can be enabled by the host during TDX module initialization. They are classified as add-on features because enabling them affects existing TDX systems: they may change existing feature behavior, or reserve more memory.
The TDX module extends TDH.SYS.CONFIG with a new register argument to specify which add-on features to enable. This new argument is a bitmap that uses the same feature bits as TDX_FEATURES0. Note that Dynamic PAMT is an exception: although it is an add-on feature, it is controlled via a legacy, dedicated register argument [1].
The kernel needs to enable these add-on features when it supports them. Add a get_tdx_usable_addon_features0() helper to return the bitmap of the add-on features that the module & kernel both support. Initially, this helper returns 0. It will be updated to return specific feature bits as full kernel support lands. Pass this bitmap as an argument to TDH.SYS.CONFIG.
The TDX module requires SEAMCALL leaf version 1 for TDH.SYS.CONFIG when passing the new bitmap argument. A previous change [2] supports the versioned SEAMCALL leafs by adding a "version" field in struct tdx_module_args. Set the version field to 1 if the new bitmap argument is used to enable any add-on feature, otherwise keep the version as 0. This retains backward compatibility with older modules that don't recognize version 1.
---
v3:
- Update the stale changlog for bitmap arg of the wrapper (Chao)
- s/get_tdx_addon_features0()/get_tdx_usable_addon_features0() (Tony)
- Collect Reviewed-by tags
v2:
- Don't pass addon_features0 parameter around, get it in the wrappers (Dave)
- Remove TDH.SYS.UPDATE wrapper (Dave & Rick)
- Drop the changelog section explaining why add-on feature enabling is needed in this series, as it is a generally understood pattern (Rick)
- Add a note in the changelog that Dynamic PAMT is not enabled via the new bitmap argument (Rick)
- Add __init tag for get_tdx_addon_features0() (AI nitpicker)
v1:
- Use tdx_module_args.version to assign SEAMCALL leaf versions (Dave)
- Remove DICE specific descriptions (Rick)
- Remove the global var tdx_addon_features0 (Chao)
- Add a Macro to collect kernel supported add-on feature bits (Rick)
- Changelog & code comments change
--- arch/x86/virt/vmx/tdx/tdx.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
arch/x86/virt/vmx/tdx/tdx.c +19 -0
@@ -1042,9 +1042,19 @@ struct tdmr_info_pa_array {DECLARE_FLEX_ARRAY (u64, phys); }; +/* List all kernel-supported add-on features0 bits here */ +#define TDX_KERNEL_SUPPORTED_ADDON_FEATURES0 (0) + +static __init u64 get_tdx_usable_addon_features0 (void ) +{ +return tdx_sysinfo.features.tdx_features0 & +TDX_KERNEL_SUPPORTED_ADDON_FEATURES0 ; +} +static __init int tdx_sys_config (struct tdmr_info_pa_array *tdmr_pa_array,unsigned int nr_tdmr_pa,u64 global_keyid) { +u64 addon_features0 =get_tdx_usable_addon_features0 ();struct tdx_module_args args = { .rcx =__pa (tdmr_pa_array), .rdx = nr_tdmr_pa,
@@ -1056,6 +1066,15 @@ static __init int tdx_sys_config(struct tdmr_info_pa_array *tdmr_pa_array, args.r8 |=TDX_SYS_CONFIG_DYNAMIC_PAMT ; } +/* +* Use SEAMCALL version 1 that supports add-on features if any are +* requested. Otherwise use version 0 for backward compatibility. +*/ +if (addon_features0) { + args.r9 = addon_features0; + args.version =1 ; + } +return seamcall_prerr (TDH_SYS_CONFIG , &args); }
Signature · 1 line
2.25.1
PATCH v3 3/6 x86/virt/tdx: Add extra memory to TDX module for the extensions
In reply to Xu YilunTDX module extensions need memory for their execution environment to serve SEAMCALL leafs. The TDX architecture implements the extensions in such a way that they use the memory outside of SEAM range, so the kernel should add the memory upfront at initialization time.
Introduce a new memory adding process backed by a new SEAMCALL leaf TDH.EXT.MEM.ADD. The kernel queries TDX module how much memory needed, allocates it, adds it to the module, and never gets it back.
The TDX module accepts the memory in the form of an HPA array. This array is passed via a single 64-bit SEAMCALL leaf parameter, which encodes two values: the PFN of the container page holding the array, and the number of entries in the array. Create a helper to encode this format and name it after the TDX module term: HPA_LIST_INFO.
TDX module extensions consume tens of megabytes memory that will never be returned to the host. Use contiguous page allocation to isolate these large blocks entirely. This is a simple way to avoid permanent memory
fragmentation: requiring the full size to be contiguous is more expensive than needed but should be good during the boot time. Print the allocation amount on TDX module extensions initialization for visibility.
The TDX module provides a metadata field "memory_pool_required_pages" to indicate the required memory size. This metadata is present only when the module supports the extensions and is meaningful only after the add-on feature configuration. Don't read and cache the metadata in tdx_sysinfo at the very beginning of TDX module initialization. Instead read it right before allocating memory for the extensions, when it is guaranteed to be valid.
---
An alternative solution is to use a loop that gives memory on a memory error code - TDX_EXT_MEMORY_POOL_REQUIRED, add one page per iteration until TDH.EXT.INIT succeeds. Something like:
do {
ret = tdh_sys_init();
if (ret == TDX_EXT_MEMORY_POOL_REQUIRED)
tdh_ext_mem_add(); //single page
} while (ret == TDX_EXT_MEMORY_POOL_REQUIRED);This approach is slightly simpler as we don't have to query the module for the total memory, no memory pre-allocation or segmentation math. But allocating a single 4K page per iteration may cause permanent memory fragmentation.
v3:
- Merge remaining pre-check operations from the previous patch (Rick)
- Don't save the metadata in tdx_sysinfo (Rick)
- Tweak the code comment for memory_pool_required_pages == 0 (Chao)
- Re-phrase the code comment for struct tdx_hpa_list (Tony)
- Put the error handling comments on top of memory add loop, to avoid moving them around when DPAMT support is added in the following patch.
- Print memory size before adding memory; it won't be freed afterward, whether the operation succeeds or fails.
- Collect Reviewed-by tags
v2:
- Add code comments for container page allocation (Kiryl)
- Adjust comments/changelog for contiguous allocation of extensions memory (Kiryl)
- s/PFN array/HPA array in commit log (Kiryl)
- State the alternative solution (Rick)
- Add code comment for struct tdx_hpa_list
v1:
- Fix return value for SEAMCALL wrappers (Chao)
- Print SEAMCALL error code for SEAMCALL wrappers (Xiaoyao)
- Rename local vars to make the ext memory adding loop clear (Rick)
- Remove input parameters for tdx_ext_mem_setup() (Kevin)
- Add a Macro for tdh_hpa_list size.
- Change the SEAMALL wrapper parameter type,
struct page *hpa_list => struct tdx_hpa_list *hpa_list
- changelog & code comments
--- arch/x86/include/asm/tdx.h | 1 + arch/x86/include/asm/tdx_global_metadata.h | 4 + arch/x86/virt/vmx/tdx/tdx.h | 1 + arch/x86/virt/vmx/tdx/tdx.c | 140 ++++++++++++++++++++++++++++ arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 14 +++ 5 files changed, 160 insertions(+)
arch/x86/include/asm/tdx.h +1 -0
@@ -37,6 +37,7 @@#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) +#define TDX_FEATURES0_EXT BIT_ULL(39)#ifndef __ASSEMBLER__
arch/x86/include/asm/tdx_global_metadata.h +4 -0
@@ -47,6 +47,10 @@ struct tdx_sys_info_handoff {u16 module_hv; }; +struct tdx_sys_info_ext { +u32 memory_pool_required_pages; +}; +struct tdx_sys_info {struct tdx_sys_info_version version;struct tdx_sys_info_features features;
arch/x86/virt/vmx/tdx/tdx.h +1 -0
@@ -50,6 +50,7 @@#define TDH_SYS_UPDATE 53#define TDH_PHYMEM_PAMT_ADD 58#define TDH_PHYMEM_PAMT_REMOVE 59 +#define TDH_EXT_MEM_ADD 61#define TDH_SYS_DISABLE 69/* TDX page types */
arch/x86/virt/vmx/tdx/tdx.c +140 -0
152 lines not on this page · raw message
arch/x86/virt/vmx/tdx/tdx_global_metadata.c +14 -0
@@ -125,6 +125,20 @@ static int get_tdx_sys_info_handoff(struct tdx_sys_info_handoff *sysinfo_handoffreturn 0 ; } +static __init int get_tdx_sys_info_ext (struct tdx_sys_info_ext *sysinfo_ext) +{ +int ret; +u64 val; + + ret =read_sys_metadata_field (0x3100000200000000 , &val); +if (ret) +return ret; + + sysinfo_ext->memory_pool_required_pages = val; + +return 0 ; +} +static __init int get_tdx_sys_info (struct tdx_sys_info *sysinfo) {int ret =0 ;
Signature · 1 line
2.25.1
PATCH v3 4/6 x86/virt/tdx: Make TDX module initialize the extensions
In reply to Xu YilunTDX module extensions need memory for their execution environment to serve SEAMCALL leafs. Several add-on features depend on the extensions to execute their SEAMCALL leafs.
After providing all required memory to the TDX module, initialize TDX module extensions via TDH.EXT.INIT, then those add-on features can use their SEAMCALL leafs normally.
---
v1:
- Fix return value for SEAMCALL wrappers (Chao)
- Print SEAMCALL error code for SEAMCALL wrappers (Xiaoyao)
- Changelog & code comments
--- arch/x86/virt/vmx/tdx/tdx.h | 1 + arch/x86/virt/vmx/tdx/tdx.c | 25 ++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-)
arch/x86/virt/vmx/tdx/tdx.h +1 -0
@@ -50,6 +50,7 @@#define TDH_SYS_UPDATE 53#define TDH_PHYMEM_PAMT_ADD 58#define TDH_PHYMEM_PAMT_REMOVE 59 +#define TDH_EXT_INIT 60#define TDH_EXT_MEM_ADD 61#define TDH_SYS_DISABLE 69
arch/x86/virt/vmx/tdx/tdx.c +24 -1
@@ -1348,12 +1348,35 @@ static __init int tdx_ext_mem_setup(void)return ret; } +static __init int tdx_ext_init (void ) +{ +struct tdx_module_args args = {}; +u64 ret; + +do { + ret =seamcall (TDH_EXT_INIT , &args); + }while (ret ==TDX_INTERRUPTED_RESUMABLE ); + +if (ret !=TDX_SUCCESS ) { +pr_err ("TDH.EXT.INIT failed: 0x%016llx\n" , ret); +return -EIO ; + } + +return 0 ; +} +static __init int init_tdx_module_extensions (void ) { +int ret; +if (!(tdx_sysinfo.features.tdx_features0 &TDX_FEATURES0_EXT ))return 0 ; -return tdx_ext_mem_setup (); + ret =tdx_ext_mem_setup (); +if (ret) +return ret; + +return tdx_ext_init (); }static __init int init_tdx_module (void )
Signature · 1 line
2.25.1
PATCH v3 5/6 x86/virt/tdx: Re-initialize the extensions on runtime TDX module update
In reply to Xu YilunRuntime TDX module update introduces a mechanism to update the module firmware while preserving and restoring TDX operations. The extensions functionalities should also be re-initialized as part of the restoration process.
The TDX architecture supports an update flow which allows updated extensions to consume more memory than their original boot-time requirement. So the arch defines the extensions re-initialization flow the same as boot-up initialization: the host queries TDX module how much additional memory needed, allocates it, adds it to the module via TDH.EXT.MEM.ADD, then re-initializes the extensions via TDH.EXT.INIT.
Linux runs the updates in stop_machine() context, which prevents memory allocation. So for Linux, a compatible update must not install updated extensions that require additional memory.
Given that the memory for the extensions must not increase, the re-initialization skips the memory adding steps. It is simplified as:
- Check if the extensions are supported via TDX_FEATURES0_EXT. If not, skip the extensions re-initialization.
- Re-initialize the extensions via TDH.EXT.INIT. The SEAMCALL leaf will fail if the updated extensions require more memory, which indicates the update image is not compatible.
---
v3:
- Add this patch back cause the latest discussion decides the re-initialization won't be integrated in TDH.SYS.UPDATE, a dedicated TDH.EXT.INIT call is still needed (Dave & Rick)
- Drop the ext_required check (Rick)
- Rename the reinit function as reinit_tdx_module_extensions() (Tony)
- Move the reinit function right under tdh_sys_update() (Rick)
v2:
- Removed this patch cause the TDX module is expected to re-initialize the extensions on TDH.SYS.UPDATE
v1:
- Don't update the extensions metadata any more, only check the metadata originated at boot time.
- Remove memory_pool_required_pages check, let TDH.EXT.INIT fail if more memory required.
- Changelog & code comments
--- arch/x86/virt/vmx/tdx/tdx.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-)
arch/x86/virt/vmx/tdx/tdx.c +18 -1
@@ -1348,7 +1348,7 @@ static __init int tdx_ext_mem_setup(void)return ret; } -static __init int tdx_ext_init (void ) +static int tdx_ext_init (void ) {struct tdx_module_args args = {};u64 ret;
@@ -1379,6 +1379,19 @@ static __init int init_tdx_module_extensions(void)return tdx_ext_init (); } +/* +* Don't update the memory requirement metadata or try memory allocation in +* stop_machine(). If an incompatible update requires more memory, let the +* extensions re-initialization fail. +*/ +static int reinit_tdx_module_extensions (void ) +{ +if (!(tdx_sysinfo.features.tdx_features0 &TDX_FEATURES0_EXT )) +return 0 ; + +return tdx_ext_init (); +} +static __init int init_tdx_module (void ) {int ret;
@@ -1562,6 +1575,10 @@ int tdx_module_run_update(void)if (ret)return ret; + ret =reinit_tdx_module_extensions (); +if (ret) +return ret; + ret =get_tdx_sys_info_version (&tdx_sysinfo.version);/* * Only fails if there is something unexpected
Signature · 1 line
2.25.1
PATCH v3 6/6 x86/virt/tdx: Support DPAMT when adding memory for the extensions
In reply to Xu YilunThe TDX module uses Physical Address Metadata Table (PAMT) to track some state for each page of physical memory that it might use. 3 levels of PAMTs are used to track pages of different sizes - 1GB, 2MB and 4KB. Dynamic PAMT (DPAMT) allows saving memory by allocating 4KB PAMT dynamically, while the 1GB and 2MB levels remain allocated on TDX module initialization. The kernel has helpers to install 4K DPAMT.
Although the memory for the extensions is tens of megabytes and the host allocates it in a large chunk, the TDX module accepts it at 4K granularity. Thus the host has to install 4K DPAMT for each page of it.
Call tdx_pamt_get() for each page before adding it to TDX module. Normally, these operations should not fail, and if they do, intentionally keep the error handling as simple as before - leak all pages, including the installed 4K DPAMT metadata.
---
v3:
- New patch
--- arch/x86/virt/vmx/tdx/tdx.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
arch/x86/virt/vmx/tdx/tdx.c +8 -1
@@ -1330,8 +1330,15 @@ static __init int tdx_ext_mem_setup(void)struct page *chunk = page + added_pages;unsigned int i; -for (i =0 ; i < chunk_pages; i++) +for (i =0 ; i < chunk_pages; i++) { + ret =tdx_pamt_get (page_to_pfn (chunk + i),NULL ); +if (ret) { +WARN (1 ,"DPAMT setup error for extensions, stranded all pages\n" ); + goto out_free_hpa_list; + } + hpa_list->phys[i] =page_to_phys (chunk + i); + } ret =tdx_ext_mem_add (hpa_list, chunk_pages);if (ret) {
Signature · 1 line
2.25.1
[email protected]
AI reviewRe: [PATCH v3 3/6] x86/virt/tdx: Add extra memory to TDX module for the extensions
In reply to Xu YilunThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] x86/virt/tdx: kzalloc() breaks page alignment for hardware-facing struct under debug --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] x86/virt/tdx: kzalloc() breaks page alignment for hardware-facing struct under debug --
commit b27e647d007dabaa3a6b0eda05701af51bffc99e
Author: Xu Yilun <[email protected]>
x86/virt/tdx: Add extra memory to TDX module for the extensions
This commit introduces a new SEAMCALL leaf TDH.EXT.MEM.ADD to allocate and add execution environment memory to the TDX module for extensions. It allocates a page-sized structure tdx_hpa_list to pass the physical addresses of the allocated memory to the hardware.
Quoted 4 lines
arch/x86/virt/vmx/tdx/tdx.c +0
-0
[ ... ]
arch/x86/virt/vmx/tdx/tdx.c static __init int init_tdmrs(struct tdmr_info_list *tdmr_list) · line 1243
Quoted 5 lines
arch/x86/virt/vmx/tdx/tdx.c +5
-0+static __init u64 to_hpa_list_info (struct tdx_hpa_list *hpa_list, +unsigned int nr_pages) +{ +return FIELD_PREP (HPA_LIST_INFO_FIRST_ENTRY ,0 ) | + FIELD_PREP(HPA_LIST_INFO_PFN, PFN_DOWN(__pa(hpa_list))) |
[Severity: High]
If the hpa_list pointer is unaligned due to slab redzones, does this PFN_DOWN() truncation cause the TDX module to read the redzone metadata instead of the actual array?
Quoted 2 lines
arch/x86/virt/vmx/tdx/tdx.c +2
-0+FIELD_PREP (HPA_LIST_INFO_LAST_ENTRY , nr_pages -1 ); +}
[ ... ]
Quoted 7 lines
arch/x86/virt/vmx/tdx/tdx.c +7
-0+static __init int tdx_ext_mem_setup (void ) +{ +unsigned int required_pages, added_pages; +struct tdx_sys_info_ext sysinfo_ext; +struct tdx_hpa_list *hpa_list; +struct page *page; +int ret;
[ ... ]
arch/x86/virt/vmx/tdx/tdx.c static __init int init_tdmrs(struct tdmr_info_list *tdmr_list) · line 1299
Quoted 6 lines
arch/x86/virt/vmx/tdx/tdx.c +6
-0+/* +* Allocate the container page for the HPA_LIST. tdx_hpa_list is +* guaranteed to be page-sized by static_assert(), so kzalloc() +* guarantees the page alignment. +*/ + hpa_list =kzalloc_obj (*hpa_list);
[Severity: High]
Does kzalloc_obj() actually guarantee page alignment when CONFIG_KASAN or CONFIG_SLUB_DEBUG are enabled?
Since slab redzones offset the object within the page, a PAGE_SIZE allocation through the slab allocator might not return a page-aligned address under these debug configurations.
Would it be safer to use the page allocator (e.g. alloc_page() or get_zeroed_page()) instead of the slab allocator for a structure that requires strict page alignment when passed to hardware?
Quoted 2 lines
arch/x86/virt/vmx/tdx/tdx.c +2
-0+if (!hpa_list) +return -ENOMEM ;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
[email protected]
AI reviewRe: [PATCH v3 6/6] x86/virt/tdx: Support DPAMT when adding memory for the extensions
In reply to Xu YilunThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] tdx: allocator thrashing due to unconditional DPAMT allocations --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] tdx: allocator thrashing due to unconditional DPAMT allocations --
commit 02adad93d9f4243664b0cea8f1ad9c0b433e79a8
Author: Xu Yilun <[email protected]>
x86/virt/tdx: Support DPAMT when adding memory for the extensions
This commit updates the TDX module extension memory setup to install a 4K Dynamic PAMT (DPAMT) for each page before adding it. It calls tdx_pamt_get() for each page within the memory chunk.
arch/x86/virt/vmx/tdx/tdx.c static __init int tdx_ext_mem_setup(void) · line 1334
Quoted 11 lines
arch/x86/virt/vmx/tdx/tdx.c +2
-1@@ -1330,8 +1330,15 @@ static __init int tdx_ext_mem_setup(void)struct page *chunk = page + added_pages;unsigned int i; -for (i =0 ; i < chunk_pages; i++) +for (i =0 ; i < chunk_pages; i++) { + ret =tdx_pamt_get (page_to_pfn (chunk + i),NULL );
[Severity: Medium]
Will calling tdx_pamt_get() sequentially without a DPAMT cache cause severe allocator thrashing during boot?
Looking at tdx_pamt_get(), providing a NULL cache forces an unconditional allocation of DPAMT backing pages before checking the reference count:
arch/x86/virt/vmx/tdx/tdx.c:tdx_pamt_get() {
...
ret = alloc_pamt_array(pamt_pages, cache);
if (ret)
return ret;
dpamt_refcount = tdx_find_dpamt_refcount(pfn);
spin_lock(&dpamt_lock);
/*
* If the DPAMT entry is already added (i.e. refcount >= 1),
* then just increment the refcount.
*/
if (atomic_inc_not_zero(dpamt_refcount))
goto out_free;
...
out_free:
spin_unlock(&dpamt_lock);
free_pamt_array(pamt_pages);
...
}Since 511 out of 512 pages in each 2MB block will find the entry already present, the newly allocated pages are immediately freed. For tens of megabytes of extensions, this results in thousands of unnecessary allocations and frees. Furthermore, free_pamt_array() performs expensive memory resets, leading to unnecessary boot delays.
Could a struct tdx_pamt_cache be allocated locally in tdx_ext_mem_setup() and passed into tdx_pamt_get() to avoid this thrashing?
Quoted 9 lines
arch/x86/virt/vmx/tdx/tdx.c +6
-0+if (ret) { +WARN (1 ,"DPAMT setup error for extensions, stranded all pages\n" ); + goto out_free_hpa_list; + } + hpa_list->phys[i] =page_to_phys (chunk + i); + } ret =tdx_ext_mem_add (hpa_list, chunk_pages);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
[email protected]
AI reviewRe: [PATCH v3 5/6] x86/virt/tdx: Re-initialize the extensions on runtime TDX module update
In reply to Xu YilunThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] virt/tdx: stale boot metadata breaks TDX extension initialization --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] virt/tdx: stale boot metadata breaks TDX extension initialization --
commit c0a5cf03ba530712949cf538278d0905890ebf56
Author: Xu Yilun <[email protected]>
x86/virt/tdx: Re-initialize the extensions on runtime TDX module update
This commit introduces a mechanism to re-initialize TDX module extensions during a runtime update. It intentionally skips memory reallocation, expecting the TDH.EXT.INIT call to fail if the updated extensions require more memory than what was originally allocated at boot.
Quoted 2 lines
arch/x86/virt/vmx/tdx/tdx.c +0
-0
[ ... ]
arch/x86/virt/vmx/tdx/tdx.c static __init int init_tdx_module_extensions(void) · line 1393
Quoted 12 lines
arch/x86/virt/vmx/tdx/tdx.c +12
-0+/* +* Don't update the memory requirement metadata or try memory allocation in +* stop_machine(). If an incompatible update requires more memory, let the +* extensions re-initialization fail. +*/ +static int reinit_tdx_module_extensions (void ) +{ +if (!(tdx_sysinfo.features.tdx_features0 &TDX_FEATURES0_EXT )) +return 0 ; + +return tdx_ext_init (); +}
[Severity: High]
Will this logic use stale boot-time metadata to skip a mandatory initialization step for updated modules that introduce extension support?
reinit_tdx_module_extensions() unconditionally reads from tdx_sysinfo.features.tdx_features0. This struct is populated during boot initialization.
If an administrator installs a TDX module update where the new firmware introduces extension support, this check will incorrectly evaluate to 0 and skip tdx_ext_init().
When a new module adds extension support, it transitions to a pending initialization state after TDH.SYS.UPDATE and explicitly requires TDH.EXT.INIT. By returning 0 (success) here, the kernel proceeds as if the module is fully initialized.
The hardware is left trapped in an intermediate state, which will cause all subsequent SEAMCALLs to fail and active or new TDs to crash. Conversely, if an update removes extension support, the kernel will incorrectly attempt TDH.EXT.INIT and fail the update.
Should the kernel dynamically read the new module's feature bit via TDH.SYS.RD after the update to accurately determine if TDH.EXT.INIT is required?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
[email protected]
AI reviewRe: [PATCH v3 4/6] x86/virt/tdx: Make TDX module initialize the extensions
In reply to Xu YilunThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] x86/tdx: missing cache flush for extension memory before TDH_EXT_MEM_ADD --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] x86/tdx: missing cache flush for extension memory before TDH_EXT_MEM_ADD --
commit 218ae840fc4855126ea1a0f203de6a4e414fa8d7
Author: Xu Yilun <[email protected]>
x86/virt/tdx: Make TDX module initialize the extensions
TDX module extensions need memory for their execution environment to serve SEAMCALL leafs. Several add-on features depend on the extensions to execute their SEAMCALL leafs.
After providing all required memory to the TDX module, initialize TDX module extensions via TDH.EXT.INIT, then those add-on features can use their SEAMCALL leafs normally.
Quoted 5 lines
arch/x86/virt/vmx/tdx/tdx.c +0
-0@@ -1348,12 +1348,35 @@ static __init int tdx_ext_mem_setup(void)
[ ... ]
arch/x86/virt/vmx/tdx/tdx.c static __init int tdx_ext_mem_setup(void) · line 1375
Quoted 9 lines
static
__initintinit_tdx_module_extensions(void){arch/x86/virt/vmx/tdx/tdx.c +3
-1+int ret; +if (!(tdx_sysinfo.features.tdx_features0 &TDX_FEATURES0_EXT ))return 0 ; -return tdx_ext_mem_setup (); + ret =tdx_ext_mem_setup ();
[Severity: High]
Does tdx_ext_mem_setup() need to explicitly flush the extension memory cache lines?
During TDX module initialization, a global cache flush (wbinvd_on_all_cpus()) is performed via config_global_keyid(). After this global flush, init_tdx_module_extensions() is called, allocating memory via alloc_contig_pages() in tdx_ext_mem_setup().
Because this memory is allocated after the wbinvd, it may contain dirty cache lines. The pages are then passed to tdx_ext_mem_add() without an explicit cache flush (such as tdx_clflush_page()):
arch/x86/virt/vmx/tdx/tdx.c:tdx_ext_mem_setup() {
...
for (i = 0; i < chunk_pages; i++)
hpa_list->phys[i] = page_to_phys(chunk + i);
ret = tdx_ext_mem_add(hpa_list, chunk_pages);
...
}Could missing cache flushes when transferring memory from the host (KeyID 0) to the TDX module (which uses the global private KeyID) cause MKTME KeyID aliasing MCEs? If dirty cache lines associated with KeyID 0 remain in the CPU caches and are later evicted, they could corrupt the TDX module's private memory.
Quoted 5 lines
arch/x86/virt/vmx/tdx/tdx.c +4
-0+if (ret) +return ret; + +return tdx_ext_init (); }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Re: [PATCH v3 5/6] x86/virt/tdx: Re-initialize the extensions on runtime TDX module update
In reply to Xu YilunQuoted 19 lines · Xu Yilun
Runtime TDX module update introduces a mechanism to update the module firmware while preserving and restoring TDX operations. The extensions functionalities should also be re-initialized as part of the restoration process.
The TDX architecture supports an update flow which allows updated extensions to consume more memory than their original boot-time requirement. So the arch defines the extensions re-initialization flow the same as boot-up initialization: the host queries TDX module how much additional memory needed, allocates it, adds it to the module via TDH.EXT.MEM.ADD, then re-initializes the extensions via TDH.EXT.INIT.
Linux runs the updates in
stop_machine()context, which prevents memory allocation. So for Linux, a compatible update must not install updated extensions that require additional memory.Given that the memory for the extensions must not increase, the re-initialization skips the memory adding steps. It is simplified as:
Quoted 6 lines
- Check if the extensions are supported via
TDX_FEATURES0_EXT. If not, skip the extensions re-initialization.- Re-initialize the extensions via TDH.EXT.INIT. The SEAMCALL leaf will fail if the updated extensions require more memory, which indicates the update image is not compatible.
Re: [PATCH v3 6/6] x86/virt/tdx: Support DPAMT when adding memory for the extensions
In reply to Xu YilunQuoted 36 lines · Xu Yilun
The TDX module uses Physical Address Metadata Table (PAMT) to track some state for each page of physical memory that it might use. 3 levels of PAMTs are used to track pages of different sizes - 1GB, 2MB and 4KB. Dynamic PAMT (DPAMT) allows saving memory by allocating 4KB PAMT dynamically, while the 1GB and 2MB levels remain allocated on TDX module initialization. The kernel has helpers to install 4K DPAMT.
Although the memory for the extensions is tens of megabytes and the host allocates it in a large chunk, the TDX module accepts it at 4K granularity. Thus the host has to install 4K DPAMT for each page of it.
Call
tdx_pamt_get()for each page before adding it to TDX module. Normally, these operations should not fail, and if they do, intentionally keep the error handling as simple as before - leak all pages, including the installed 4K DPAMT metadata.Signed-off-by Xu Yilun <[email protected]>---
v3:
- New patch--- arch/x86/virt/vmx/tdx/tdx.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)arch/x86/virt/vmx/tdx/tdx.c +2
-1@@ -1330,8 +1330,15 @@ static __init int tdx_ext_mem_setup(void)struct page *chunk = page + added_pages;unsigned int i; -for (i =0 ; i < chunk_pages; i++) +for (i =0 ; i < chunk_pages; i++) { + ret =tdx_pamt_get (page_to_pfn (chunk + i),NULL );
arch/x86/virt/vmx/tdx/tdx.c static __init int tdx_ext_mem_setup(void) · line 1344
Quoted 10 lines
arch/x86/virt/vmx/tdx/tdx.c +6
-0+if (ret) { +WARN (1 ,"DPAMT setup error for extensions, stranded all pages\n" ); + goto out_free_hpa_list; + } + hpa_list->phys[i] =page_to_phys (chunk + i); + } ret =tdx_ext_mem_add (hpa_list, chunk_pages);if (ret) {
How about just fold this change in into patch 3/6?
Re: [PATCH v3 1/6] x86/virt/tdx: Move TDH.SYS.CONFIG operations into a wrapper
In reply to Xu YilunQuoted 2 lines · Xu Yilun
In Linux,
Kind of obviously the topic at hand...
Quoted 34 lines
SEAMCALL wrappers are introduced to avoid broad SEAMCALL access by exposing only a selection of SEAMCALL leafs, but also to abstract the SEAMCALL register ABIs. The abstraction improves readability and reuse for SEAMCALL leafs that are called multiple times.
Some SEAMCALL leafs are not explicitly wrapped because the level of TDX ABI details needed to perform the call is low enough to flow well with the calling code.
For some of the currently unwrapped SEAMCALL leafs, the TDX architecture has evolved the ABI to introduce new leaf versions. The host can select the version to call based on what is supported by the loaded TDX module. When future kernel implements this version selection, more ABI details will leak into the surrounding caller code, which decreases the readability of the caller logic. To keep the ABI details contained, move the SEAMCALL leaf that will need version selection into a wrapper.
When defining the wrapper, the cleanest separation would be to have kernel data types for the SEAMCALL wrapper arguments, and have them marshaled into SEAMCALL leaf ABI types (often u64s) inside the wrapper. This works for many SEAMCALL leafs but becomes cumbersome when the register ABI type is a physical address which points to a buffer for an in-memory ABI. If the SEAMCALL wrapper only accepts kernel data types, it may need duplicate buffer allocation and copies to match the in-memory ABI. Another solution is to define a named helper structure that mirrors the in-memory ABI, populate it in a separate flow, then pass it to the SEAMCALL wrapper. struct
seamldr_paramsis an existing example of this pattern.TDH.SYS.CONFIG requires a list of TDMR information in the form of a PA array. The PA array is the in-memory ABI. Create a structure for the PA array, use it as the argument when creating the wrapper for TDH.SYS.CONFIG.
Quoted 6 lines
For readability, adjust a bit of the existing PA array size calculation:
use the new PA array structure's element type instead of hardcoding u64.Signed-off-by Xu Yilun <[email protected]>Reviewed-by Nikolay Borisov <[email protected]>Reviewed-by Tony Lindgren <[email protected]>
Re: [PATCH v3 2/6] x86/virt/tdx: Configure add-on features on TDX module init
In reply to Xu YilunQuoted 6 lines · Xu Yilun
The TDX architecture identifies some features that are off by default but can be enabled by the host during TDX module initialization. They are classified as add-on features because enabling them affects existing TDX systems: they may change existing feature behavior, or reserve more memory.
Quoted 7 lines
The TDX module extends TDH.SYS.CONFIG with a new register argument to specify which add-on features to enable. This new argument is a bitmap that uses the same feature bits as
TDX_FEATURES0. Note that Dynamic PAMT is an exception: although it is an add-on feature, it is controlled via a legacy, dedicated register argument [1].The kernel needs to enable these add-on features when it supports them.
I made a similar comment on v1:
https://lore.kernel.org/lkml/[email protected]/
It is up to the kernel to decide if it wants to enable these features, when it supports them. The sentence sounds like there is some hard requirement to enable them when possible. I think the reasons to enable them based solely on available support could be:
1. They don't take significantly more memory or CPU resources, and limiting
possible configs reduces kernel complexity.
2. It would be rare that people don't want them on.
Maybe we tweak this to be?
Despite that these features can be add-ons due to introducing some overhead to TDX runtime, the impact of the first features will be low enough to pursue a simple approach of just enabling them when supported. If a feature with a very high overhead appears in the future, this can be revisited.
Quoted 14 lines
Add a
get_tdx_usable_addon_features0()helper to return the bitmap of the add-on features that the module & kernel both support. Initially, this helper returns 0. It will be updated to return specific feature bits as full kernel support lands. Pass this bitmap as an argument to TDH.SYS.CONFIG.The TDX module requires SEAMCALL leaf version 1 for TDH.SYS.CONFIG when passing the new bitmap argument. A previous change [2] supports the versioned SEAMCALL leafs by adding a "version" field in struct
tdx_module_args. Set the version field to 1 if the new bitmap argument is used to enable any add-on feature, otherwise keep the version as 0. This retains backward compatibility with older modules that don't recognize version 1.
Quoted 5 lines
Signed-off-by Xu Yilun <[email protected]>Reviewed-by Tony Lindgren <[email protected]>Reviewed-by Nikolay Borisov <[email protected]>
With the log nit,
Quoted 51 lines
---
v3:
- Update the stale changlog for bitmap arg of the wrapper (Chao)
- s/get_tdx_addon_features0()/get_tdx_usable_addon_features0() (Tony)
- Collect Reviewed-by tagsv2:
- Don't passaddon_features0parameter around, get it in the wrappers (Dave)
- Remove TDH.SYS.UPDATE wrapper (Dave & Rick)
- Drop the changelog section explaining why add-on feature enabling is needed in this series, as it is a generally understood pattern (Rick)
- Add a note in the changelog that Dynamic PAMT is not enabled via the new bitmap argument (Rick)
- Add__inittag forget_tdx_addon_features0()(AI nitpicker)v1:
- Use tdx_module_args.version to assign SEAMCALL leaf versions (Dave)
- Remove DICE specific descriptions (Rick)
- Remove the global vartdx_addon_features0(Chao)
- Add a Macro to collect kernel supported add-on feature bits (Rick)
- Changelog & code comments change--- arch/x86/virt/vmx/tdx/tdx.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)arch/x86/virt/vmx/tdx/tdx.c +10
-0@@ -1042,9 +1042,19 @@ struct tdmr_info_pa_array {DECLARE_FLEX_ARRAY (u64, phys); }; +/* List all kernel-supported add-on features0 bits here */ +#define TDX_KERNEL_SUPPORTED_ADDON_FEATURES0 (0) + +static __init u64 get_tdx_usable_addon_features0 (void ) +{ +return tdx_sysinfo.features.tdx_features0 & +TDX_KERNEL_SUPPORTED_ADDON_FEATURES0 ; +} +static __init int tdx_sys_config (struct tdmr_info_pa_array *tdmr_pa_array,unsigned int nr_tdmr_pa,u64 global_keyid) { +u64 addon_features0 =get_tdx_usable_addon_features0 ();struct tdx_module_args args = { .rcx =__pa (tdmr_pa_array), .rdx = nr_tdmr_pa,
arch/x86/virt/vmx/tdx/tdx.c static __init int tdx_sys_config(struct tdmr_info_pa_array *tdmr_pa_array, · line 1075
Quoted 10 lines
arch/x86/virt/vmx/tdx/tdx.c +7
-0@@ -1056,6 +1066,15 @@ static __init int tdx_sys_config(struct tdmr_info_pa_array *tdmr_pa_array, args.r8 |=TDX_SYS_CONFIG_DYNAMIC_PAMT ; } +/* +* Use SEAMCALL version 1 that supports add-on features if any are +* requested. Otherwise use version 0 for backward compatibility. +*/ +if (addon_features0) { + args.r9 = addon_features0; + args.version =1 ;
This is a slightly silly pattern:
args.r9 = 0;
if (addon_features0 != 0)
args.r9 = addon_features0;
...could just be:
args.r9 = addon_features0;
It could actually set args.version a similar branchless way.
But I think the version in this patch is super obvious about what is happening and has a nice place for the comment. And this is extremely far from a performance critical path.
Quoted 6 lines
arch/x86/virt/vmx/tdx/tdx.c +2
-0+ } +return seamcall_prerr (TDH_SYS_CONFIG , &args); }
Re: [PATCH v3 2/6] x86/virt/tdx: Configure add-on features on TDX module init
Quoted 26 lines · Rick P
Quoted 6 lines · Xu Yilun
The TDX architecture identifies some features that are off by default but can be enabled by the host during TDX module initialization. They are classified as add-on features because enabling them affects existing TDX systems: they may change existing feature behavior, or reserve more memory.
Quoted 7 lines
The TDX module extends TDH.SYS.CONFIG with a new register argument to specify which add-on features to enable. This new argument is a bitmap that uses the same feature bits as
TDX_FEATURES0. Note that Dynamic PAMT is an exception: although it is an add-on feature, it is controlled via a legacy, dedicated register argument [1].The kernel needs to enable these add-on features when it supports them.
I made a similar comment on v1:
https://lore.kernel.org/lkml/[email protected]/It is up to the kernel to decide if it wants to enable these features, when it supports them. The sentence sounds like there is some hard requirement to enable them when possible. I think the reasons to enable them based solely on available support could be:
1. They don't take significantly more memory or CPU resources, and limiting
possible configs reduces kernel complexity.
2. It would be rare that people don't want them on.
Quoted 6 lines
Maybe we tweak this to be?
Despite that these features can be add-ons due to introducing some overhead to TDX runtime, the impact of the first features will be low enough to pursue a simple approach of just enabling them when supported. If a feature with a very high overhead appears in the future, this can be revisited.
Good to me. I'll include it in v4.
[...]
Quoted 15 lines
arch/x86/virt/vmx/tdx/tdx.c static __init int tdx_sys_config(struct tdmr_info_pa_array *tdmr_pa_array, · line 1075
Quoted 11 lines
arch/x86/virt/vmx/tdx/tdx.c +7
-0@@ -1056,6 +1066,15 @@ static __init int tdx_sys_config(struct tdmr_info_pa_array *tdmr_pa_array, args.r8 |=TDX_SYS_CONFIG_DYNAMIC_PAMT ; } +/* +* Use SEAMCALL version 1 that supports add-on features if any are +* requested. Otherwise use version 0 for backward compatibility. +*/ +if (addon_features0) { + args.r9 = addon_features0; + args.version =1 ;This is a slightly silly pattern:
args.r9 = 0;
if (addon_features0!= 0)
Quoted 4 lines
args.r9 = addon_features0;...could just be:
args.r9 =addon_features0;
Yes, that's workable.
Quoted 2 lines
It could actually set args.version a similar branchless way.
Not sure how?
I don't think just "args.version = 1" can work. It will fail older modules which doesn't support add-on features & doesn't recognize TDH.SYS.CONFIG v1.
How about:
struct tdx_module_args args = {
.rcx = __pa(tdmr_pa_array),
.rdx = nr_tdmr_pa,
.r8 = global_keyid,
.r9 = addon_features0,
};
...
if (args.r9)
args.version = 1;Quoted 13 lines
But I think the version in this patch is super obvious about what is happening and has a nice place for the comment. And this is extremely far from a performance critical path.
Quoted 6 lines
arch/x86/virt/vmx/tdx/tdx.c +2
-0+ } +return seamcall_prerr (TDH_SYS_CONFIG , &args); }
Re: [PATCH v3 4/6] x86/virt/tdx: Make TDX module initialize the extensions
In reply to [email protected]Quoted 37 lines · On Mon, Oct 05, 2026 at 06:00:31PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] x86/tdx: missing cache flush for extension memory beforeTDH_EXT_MEM_ADD--commit
218ae840fc4855126ea1a0f203de6a4e414fa8d7
Author: Xu Yilun <[email protected]>x86/virt/tdx: Make TDX module initialize the extensions
TDX module extensions need memory for their execution environment to serve SEAMCALL leafs. Several add-on features depend on the extensions to execute their SEAMCALL leafs.
After providing all required memory to the TDX module, initialize TDX module extensions via TDH.EXT.INIT, then those add-on features can use their SEAMCALL leafs normally.
Quoted 5 lines
arch/x86/virt/vmx/tdx/tdx.c +0
-0@@ -1348,12 +1348,35 @@ static __init int tdx_ext_mem_setup(void)[ ... ]
arch/x86/virt/vmx/tdx/tdx.c static __init int tdx_ext_mem_setup(void) · line 1375
Quoted 9 lines
static
__initintinit_tdx_module_extensions(void){arch/x86/virt/vmx/tdx/tdx.c +3
-1+int ret; +if (!(tdx_sysinfo.features.tdx_features0 &TDX_FEATURES0_EXT ))return 0 ; -return tdx_ext_mem_setup (); + ret =tdx_ext_mem_setup ();[Severity: High]
Doestdx_ext_mem_setup()need to explicitly flush the extension memory cache lines?
Quoted 8 lines
During TDX module initialization, a global cache flush (
wbinvd_on_all_cpus()) is performed viaconfig_global_keyid(). After this global flush,init_tdx_module_extensions()is called, allocating memory viaalloc_contig_pages()intdx_ext_mem_setup().Because this memory is allocated after the wbinvd, it may contain dirty cache lines. The pages are then passed to
tdx_ext_mem_add()without an explicit cache flush (such astdx_clflush_page()):
There is no dirty cache issue. It is clarified in this series:
https://lore.kernel.org/all/[email protected]/
Quoted 24 lines
arch/x86/virt/vmx/tdx/tdx.c:tdx_ext_mem_setup() { ... for (i = 0; i < chunk_pages; i++) hpa_list->phys[i] = page_to_phys(chunk + i); ret = tdx_ext_mem_add(hpa_list, chunk_pages); ... }Could missing cache flushes when transferring memory from the host (KeyID 0) to the TDX module (which uses the global private KeyID) cause MKTME KeyID aliasing MCEs? If dirty cache lines associated with KeyID 0 remain in the CPU caches and are later evicted, they could corrupt the TDX module's private memory.
Quoted 5 lines
arch/x86/virt/vmx/tdx/tdx.c +4
-0+if (ret) +return ret; + +return tdx_ext_init (); }Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Re: [PATCH v3 6/6] x86/virt/tdx: Support DPAMT when adding memory for the extensions
In reply to [email protected]Quoted 55 lines · On Mon, Oct 05, 2026 at 05:57:13PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] tdx: allocator thrashing due to unconditional DPAMT allocations --commit
02adad93d9f4243664b0cea8f1ad9c0b433e79a8
Author: Xu Yilun <[email protected]>x86/virt/tdx: Support DPAMT when adding memory for the extensions
This commit updates the TDX module extension memory setup to install a 4K Dynamic PAMT (DPAMT) for each page before adding it. It calls
tdx_pamt_get()for each page within the memory chunk.arch/x86/virt/vmx/tdx/tdx.c static __init int tdx_ext_mem_setup(void) · line 1334
Quoted 11 lines
arch/x86/virt/vmx/tdx/tdx.c +2
-1@@ -1330,8 +1330,15 @@ static __init int tdx_ext_mem_setup(void)struct page *chunk = page + added_pages;unsigned int i; -for (i =0 ; i < chunk_pages; i++) +for (i =0 ; i < chunk_pages; i++) { + ret =tdx_pamt_get (page_to_pfn (chunk + i),NULL );[Severity: Medium]
Will callingtdx_pamt_get()sequentially without a DPAMT cache cause severe allocator thrashing during boot?Looking at
tdx_pamt_get(), providing a NULL cache forces an unconditional allocation of DPAMT backing pages before checking the reference count:arch/x86/virt/vmx/tdx/tdx.c:tdx_pamt_get() { ... ret = alloc_pamt_array(pamt_pages, cache); if (ret) return ret; dpamt_refcount = tdx_find_dpamt_refcount(pfn); spin_lock(&dpamt_lock); /* * If the DPAMT entry is already added (i.e. refcount >= 1), * then just increment the refcount. */ if (atomic_inc_not_zero(dpamt_refcount)) goto out_free; ... out_free: spin_unlock(&dpamt_lock); free_pamt_array(pamt_pages); ... }
Quoted 8 lines
Since 511 out of 512 pages in each 2MB block will find the entry already present, the newly allocated pages are immediately freed. For tens of megabytes of extensions, this results in thousands of unnecessary allocations and frees. Furthermore,
free_pamt_array()performs expensive memory resets, leading to unnecessary boot delays.Could a struct
tdx_pamt_cachebe allocated locally intdx_ext_mem_setup()and passed intotdx_pamt_get()to avoid this thrashing?
tdx_pamt_cache is for cases where memory allocation is not possible when adding DPAMT. I don't think it's good to use tdx_pamt_cache in normal cases.
Quoted 13 lines
Quoted 10 lines
arch/x86/virt/vmx/tdx/tdx.c +6
-0+if (ret) { +WARN (1 ,"DPAMT setup error for extensions, stranded all pages\n" ); + goto out_free_hpa_list; + } + hpa_list->phys[i] =page_to_phys (chunk + i); + } ret =tdx_ext_mem_add (hpa_list, chunk_pages);Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Re: [PATCH v3 6/6] x86/virt/tdx: Support DPAMT when adding memory for the extensions
In reply to Tony LindgrenQuoted 44 lines · Tony Lindgren
Quoted 43 lines · Xu Yilun
The TDX module uses Physical Address Metadata Table (PAMT) to track some state for each page of physical memory that it might use. 3 levels of PAMTs are used to track pages of different sizes - 1GB, 2MB and 4KB. Dynamic PAMT (DPAMT) allows saving memory by allocating 4KB PAMT dynamically, while the 1GB and 2MB levels remain allocated on TDX module initialization. The kernel has helpers to install 4K DPAMT.
Although the memory for the extensions is tens of megabytes and the host allocates it in a large chunk, the TDX module accepts it at 4K granularity. Thus the host has to install 4K DPAMT for each page of it.
Call
tdx_pamt_get()for each page before adding it to TDX module. Normally, these operations should not fail, and if they do, intentionally keep the error handling as simple as before - leak all pages, including the installed 4K DPAMT metadata.Signed-off-by Xu Yilun <[email protected]>---
v3:
- New patch--- arch/x86/virt/vmx/tdx/tdx.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)arch/x86/virt/vmx/tdx/tdx.c +8
-1@@ -1330,8 +1330,15 @@ static __init int tdx_ext_mem_setup(void)struct page *chunk = page + added_pages;unsigned int i; -for (i =0 ; i < chunk_pages; i++) +for (i =0 ; i < chunk_pages; i++) { + ret =tdx_pamt_get (page_to_pfn (chunk + i),NULL ); +if (ret) { +WARN (1 ,"DPAMT setup error for extensions, stranded all pages\n" ); + goto out_free_hpa_list; + } + hpa_list->phys[i] =page_to_phys (chunk + i); + }
Quoted 5 lines
arch/x86/virt/vmx/tdx/tdx.c static __init int tdx_ext_mem_setup(void) · line 1344
Quoted 3 lines
arch/x86/virt/vmx/tdx/tdx.c +0
-0ret =tdx_ext_mem_add (hpa_list, chunk_pages);if (ret) {How about just fold this change in into patch 3/6?
I think patch 3/6 is already quite heavy. Is it good that I put this patch right after patch 3/6?
Re: [PATCH v3 5/6] x86/virt/tdx: Re-initialize the extensions on runtime TDX module update
In reply to [email protected]Quoted 36 lines · On Mon, Oct 05, 2026 at 05:58:39PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High]virt/tdx: stale boot metadata breaks TDX extension initialization --commit
c0a5cf03ba530712949cf538278d0905890ebf56
Author: Xu Yilun <[email protected]>x86/virt/tdx: Re-initialize the extensions on runtime TDX module update
This commit introduces a mechanism to re-initialize TDX module extensions during a runtime update. It intentionally skips memory reallocation, expecting the TDH.EXT.INIT call to fail if the updated extensions require more memory than what was originally allocated at boot.
Quoted 2 lines
arch/x86/virt/vmx/tdx/tdx.c +0
-0[ ... ]
arch/x86/virt/vmx/tdx/tdx.c static __init int init_tdx_module_extensions(void) · line 1393
Quoted 12 lines
arch/x86/virt/vmx/tdx/tdx.c +12
-0+/* +* Don't update the memory requirement metadata or try memory allocation in +* stop_machine(). If an incompatible update requires more memory, let the +* extensions re-initialization fail. +*/ +static int reinit_tdx_module_extensions (void ) +{ +if (!(tdx_sysinfo.features.tdx_features0 &TDX_FEATURES0_EXT )) +return 0 ; + +return tdx_ext_init (); +}[Severity: High]
Will this logic use stale boot-time metadata to skip a mandatory initialization step for updated modules that introduce extension support?
Quoted 7 lines
reinit_tdx_module_extensions()unconditionally reads from tdx_sysinfo.features.tdx_features0. This struct is populated during boot initialization.If an administrator installs a TDX module update where the new firmware introduces extension support, this check will incorrectly evaluate to 0 and skip
tdx_ext_init().
Skipping tdx_ext_init() is fine, cause we didn't enable/use the extensions at boot-up and we shouldn't enable/use them after update.
Quoted 8 lines
When a new module adds extension support, it transitions to a pending initialization state after TDH.SYS.UPDATE and explicitly requires TDH.EXT.INIT. By returning 0 (success) here, the kernel proceeds as if the module is fully initialized.
The hardware is left trapped in an intermediate state, which will cause all subsequent SEAMCALLs to fail and active or new TDs to crash.
No, the basic TDX module functionality can fully work without extensions.
Quoted 3 lines
Conversely,
if an update removes extension support, the kernel will incorrectly attempt TDH.EXT.INIT and fail the update.
This is the incompatible update. Failing it is the intended behavior.
Quoted 7 lines
Should the kernel dynamically read the new module's feature bit via TDH.SYS.RD after the update to accurately determine if TDH.EXT.INIT is required?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Re: [PATCH v3 6/6] x86/virt/tdx: Support DPAMT when adding memory for the extensions
In reply to Xu YilunQuoted 45 lines · Xu Yilun
Quoted 44 lines · Tony Lindgren
Quoted 43 lines · Xu Yilun
The TDX module uses Physical Address Metadata Table (PAMT) to track some state for each page of physical memory that it might use. 3 levels of PAMTs are used to track pages of different sizes - 1GB, 2MB and 4KB. Dynamic PAMT (DPAMT) allows saving memory by allocating 4KB PAMT dynamically, while the 1GB and 2MB levels remain allocated on TDX module initialization. The kernel has helpers to install 4K DPAMT.
Although the memory for the extensions is tens of megabytes and the host allocates it in a large chunk, the TDX module accepts it at 4K granularity. Thus the host has to install 4K DPAMT for each page of it.
Call
tdx_pamt_get()for each page before adding it to TDX module. Normally, these operations should not fail, and if they do, intentionally keep the error handling as simple as before - leak all pages, including the installed 4K DPAMT metadata.Signed-off-by Xu Yilun <[email protected]>---
v3:
- New patch--- arch/x86/virt/vmx/tdx/tdx.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)arch/x86/virt/vmx/tdx/tdx.c +8
-1@@ -1330,8 +1330,15 @@ static __init int tdx_ext_mem_setup(void)struct page *chunk = page + added_pages;unsigned int i; -for (i =0 ; i < chunk_pages; i++) +for (i =0 ; i < chunk_pages; i++) { + ret =tdx_pamt_get (page_to_pfn (chunk + i),NULL ); +if (ret) { +WARN (1 ,"DPAMT setup error for extensions, stranded all pages\n" ); + goto out_free_hpa_list; + } + hpa_list->phys[i] =page_to_phys (chunk + i); + }
Quoted 8 lines
Quoted 5 lines
arch/x86/virt/vmx/tdx/tdx.c static __init int tdx_ext_mem_setup(void) · line 1344
Quoted 3 lines
arch/x86/virt/vmx/tdx/tdx.c +0
-0ret =tdx_ext_mem_add (hpa_list, chunk_pages);if (ret) {How about just fold this change in into patch 3/6?
I think patch 3/6 is already quite heavy. Is it good that I put this patch right after patch 3/6?
In that case you should describe that after commit XXX dynamic PAMT is enabled and tdx_pamt_get() must be used. Otherwise it's not clear that patch 3/6 will not work without this change.
Edgecombe, Rick P
maintainerRe: [PATCH v3 2/6] x86/virt/tdx: Configure add-on features on TDX module init
In reply to Xu YilunQuoted 13 lines · Xu Yilun
How about:
struct tdx_module_args args = { .rcx = __pa(tdmr_pa_array), .rdx = nr_tdmr_pa, .r8 = global_keyid, .r9 = addon_features0, }; ... if (args.r9) args.version = 1;
Sorry if it was written in a confusing way. I was actually saying I think the existing code is good. But logging to the list why, since I think it was debatable.
Edgecombe, Rick P
maintainerRe: [PATCH v3 6/6] x86/virt/tdx: Support DPAMT when adding memory for the extensions
In reply to Xu YilunQuoted 7 lines · Xu Yilun
Quoted 7 lines
Since 511 out of 512 pages in each 2MB block will find the entry already present, the newly allocated pages are immediately freed. For tens of megabytes of extensions, this results in thousands of unnecessary allocations
and frees. Furthermore,free_pamt_array()performs expensive memory resets, leading to unnecessary boot delays.
Quoted 7 lines
Quoted 3 lines
Could a struct
tdx_pamt_cachebe allocated locally intdx_ext_mem_setup()and
passed intotdx_pamt_get()to avoid this thrashing?
tdx_pamt_cacheis for cases where memory allocation is not possible when adding DPAMT. I don't think it's good to usetdx_pamt_cachein normal cases.
Since we are worrying about fragmented long term allocation of the actual pages of this memory, it does seem valid to worry about the PAMT memory for the same reason. But given the problem is 1/512 of the other one. Let's ignore it for now.
For the excess alloc/free, yea, lets leave it for future optimization. We are already doing some optimization ahead of time with the full page array implementation.
Re: [PATCH v3 1/6] x86/virt/tdx: Move TDH.SYS.CONFIG operations into a wrapper
In reply to Xu YilunQuoted 34 lines · Xu Yilun
In Linux, SEAMCALL wrappers are introduced to avoid broad SEAMCALL access by exposing only a selection of SEAMCALL leafs, but also to abstract the SEAMCALL register ABIs. The abstraction improves readability and reuse for SEAMCALL leafs that are called multiple times.
Some SEAMCALL leafs are not explicitly wrapped because the level of TDX ABI details needed to perform the call is low enough to flow well with the calling code.
For some of the currently unwrapped SEAMCALL leafs, the TDX architecture has evolved the ABI to introduce new leaf versions. The host can select the version to call based on what is supported by the loaded TDX module. When future kernel implements this version selection, more ABI details will leak into the surrounding caller code, which decreases the readability of the caller logic. To keep the ABI details contained, move the SEAMCALL leaf that will need version selection into a wrapper.
When defining the wrapper, the cleanest separation would be to have kernel data types for the SEAMCALL wrapper arguments, and have them marshaled into SEAMCALL leaf ABI types (often u64s) inside the wrapper. This works for many SEAMCALL leafs but becomes cumbersome when the register ABI type is a physical address which points to a buffer for an in-memory ABI. If the SEAMCALL wrapper only accepts kernel data types, it may need duplicate buffer allocation and copies to match the in-memory ABI. Another solution is to define a named helper structure that mirrors the in-memory ABI, populate it in a separate flow, then pass it to the SEAMCALL wrapper. struct
seamldr_paramsis an existing example of this pattern.TDH.SYS.CONFIG requires a list of TDMR information in the form of a PA array. The PA array is the in-memory ABI. Create a structure for the PA array, use it as the argument when creating the wrapper for TDH.SYS.CONFIG.
Quoted 6 lines
For readability, adjust a bit of the existing PA array size calculation:
use the new PA array structure's element type instead of hardcoding u64.Signed-off-by Xu Yilun <[email protected]>Reviewed-by Nikolay Borisov <[email protected]>Reviewed-by Tony Lindgren <[email protected]>
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Re: [PATCH v3 2/6] x86/virt/tdx: Configure add-on features on TDX module init
In reply to Xu YilunQuoted 27 lines · Xu Yilun
The TDX architecture identifies some features that are off by default but can be enabled by the host during TDX module initialization. They are classified as add-on features because enabling them affects existing TDX systems: they may change existing feature behavior, or reserve more memory.
The TDX module extends TDH.SYS.CONFIG with a new register argument to specify which add-on features to enable. This new argument is a bitmap that uses the same feature bits as
TDX_FEATURES0. Note that Dynamic PAMT is an exception: although it is an add-on feature, it is controlled via a legacy, dedicated register argument [1].The kernel needs to enable these add-on features when it supports them. Add a
get_tdx_usable_addon_features0()helper to return the bitmap of the add-on features that the module & kernel both support. Initially, this helper returns 0. It will be updated to return specific feature bits as full kernel support lands. Pass this bitmap as an argument to TDH.SYS.CONFIG.The TDX module requires SEAMCALL leaf version 1 for TDH.SYS.CONFIG when passing the new bitmap argument. A previous change [2] supports the versioned SEAMCALL leafs by adding a "version" field in struct
tdx_module_args. Set the version field to 1 if the new bitmap argument is used to enable any add-on feature, otherwise keep the version as 0. This retains backward compatibility with older modules that don't recognize version 1.
Quoted 5 lines
Signed-off-by Xu Yilun <[email protected]>Reviewed-by Tony Lindgren <[email protected]>Reviewed-by Nikolay Borisov <[email protected]>
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Kiryl Shutsemau
maintainerRe: [PATCH v3 6/6] x86/virt/tdx: Support DPAMT when adding memory for the extensions
In reply to Xu YilunQuoted 45 lines · Xu Yilun
Quoted 44 lines · Tony Lindgren
Quoted 43 lines · Xu Yilun
The TDX module uses Physical Address Metadata Table (PAMT) to track some state for each page of physical memory that it might use. 3 levels of PAMTs are used to track pages of different sizes - 1GB, 2MB and 4KB. Dynamic PAMT (DPAMT) allows saving memory by allocating 4KB PAMT dynamically, while the 1GB and 2MB levels remain allocated on TDX module initialization. The kernel has helpers to install 4K DPAMT.
Although the memory for the extensions is tens of megabytes and the host allocates it in a large chunk, the TDX module accepts it at 4K granularity. Thus the host has to install 4K DPAMT for each page of it.
Call
tdx_pamt_get()for each page before adding it to TDX module. Normally, these operations should not fail, and if they do, intentionally keep the error handling as simple as before - leak all pages, including the installed 4K DPAMT metadata.Signed-off-by Xu Yilun <[email protected]>---
v3:
- New patch--- arch/x86/virt/vmx/tdx/tdx.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)arch/x86/virt/vmx/tdx/tdx.c +8
-1@@ -1330,8 +1330,15 @@ static __init int tdx_ext_mem_setup(void)struct page *chunk = page + added_pages;unsigned int i; -for (i =0 ; i < chunk_pages; i++) +for (i =0 ; i < chunk_pages; i++) { + ret =tdx_pamt_get (page_to_pfn (chunk + i),NULL ); +if (ret) { +WARN (1 ,"DPAMT setup error for extensions, stranded all pages\n" ); + goto out_free_hpa_list; + } + hpa_list->phys[i] =page_to_phys (chunk + i); + }
Quoted 8 lines
Quoted 5 lines
arch/x86/virt/vmx/tdx/tdx.c static __init int tdx_ext_mem_setup(void) · line 1344
Quoted 3 lines
arch/x86/virt/vmx/tdx/tdx.c +0
-0ret =tdx_ext_mem_add (hpa_list, chunk_pages);if (ret) {How about just fold this change in into patch 3/6?
I think patch 3/6 is already quite heavy. Is it good that I put this patch right after patch 3/6?
But that breaks bisectability, no? Even placed right after 3/6, that commit fails TDX module init on a machine with DPAMT and extensions whenever the module asks for a non-zero pool.
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Re: [PATCH v3 4/6] x86/virt/tdx: Make TDX module initialize the extensions
In reply to Xu YilunQuoted 4 lines · Xu Yilun
TDX module extensions need memory for their execution environment to serve SEAMCALL leafs. Several add-on features depend on the extensions to execute their SEAMCALL leafs.
Quoted 8 lines
After providing all required memory to the TDX module, initialize TDX module extensions via TDH.EXT.INIT, then those add-on features can use their SEAMCALL leafs normally.
Signed-off-by Xu Yilun <[email protected]>Reviewed-by Xiaoyao Li <[email protected]>Reviewed-by Tony Lindgren <[email protected]>Reviewed-by Adrian Hunter <[email protected]>
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Re: [PATCH v3 5/6] x86/virt/tdx: Re-initialize the extensions on runtime TDX module update
In reply to Xu YilunQuoted 19 lines · Xu Yilun
Runtime TDX module update introduces a mechanism to update the module firmware while preserving and restoring TDX operations. The extensions functionalities should also be re-initialized as part of the restoration process.
The TDX architecture supports an update flow which allows updated extensions to consume more memory than their original boot-time requirement. So the arch defines the extensions re-initialization flow the same as boot-up initialization: the host queries TDX module how much additional memory needed, allocates it, adds it to the module via TDH.EXT.MEM.ADD, then re-initializes the extensions via TDH.EXT.INIT.
Linux runs the updates in
stop_machine()context, which prevents memory allocation. So for Linux, a compatible update must not install updated extensions that require additional memory.Given that the memory for the extensions must not increase, the re-initialization skips the memory adding steps. It is simplified as:
Quoted 8 lines
- Check if the extensions are supported via
TDX_FEATURES0_EXT. If not, skip the extensions re-initialization.- Re-initialize the extensions via TDH.EXT.INIT. The SEAMCALL leaf will fail if the updated extensions require more memory, which indicates the update image is not compatible.
Signed-off-by Xu Yilun <[email protected]>
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Edgecombe, Rick P
maintainerRe: [PATCH v3 3/6] x86/virt/tdx: Add extra memory to TDX module for the extensions
In reply to Xu YilunQuoted 11 lines · Xu Yilun
arch/x86/virt/vmx/tdx/tdx.c +11
-0+static __init int tdx_ext_mem_setup (void ) +{ +unsigned int required_pages, added_pages; +struct tdx_sys_info_ext sysinfo_ext; +struct tdx_hpa_list *hpa_list; +struct page *page; +int ret; + + ret =get_tdx_sys_info_ext (&sysinfo_ext); +if (ret) +return ret;
arch/x86/virt/vmx/tdx/tdx.c static __init int init_tdmrs(struct tdmr_info_list *tdmr_list) · line 1292
Quoted 10 lines
arch/x86/virt/vmx/tdx/tdx.c +10
-0+ + required_pages = sysinfo_ext.memory_pool_required_pages; + +/* +* Skip the memory setup if no memory is required. This may happen when +* no add-on features requiring TDX module extensions are configured +* via TDH.SYS.CONFIG. +*/ +if (!required_pages) +return 0 ;
Marshaling this through the struct just to get a set it to required_pages seems a bit silly. But I see required_pages is needed for line length.
Two options:
Make the get_tdx_sys_info_ext() just take an unsigned int pointer arg. When Chao's metadata reading stuff lands, we can keep the struct internal to the helper, to still re-use the macro struct mapper stuff.
Rename memory_pool_required_pages to required_pages to shorten the lines to something manageable and use it directly below.
Re: [PATCH v3 4/6] x86/virt/tdx: Make TDX module initialize the extensions
In reply to Xu YilunQuoted 3 lines · Xu Yilun
TDX module extensions need memory for their execution environment to serve SEAMCALL leafs. Several add-on features depend on the extensions to execute their SEAMCALL leafs.
Can we get a little more about what this SEAMCALL is actually doing? To be able to reason about how long it will take. Obviously it does nothing now, but when we enable the fist extension.
Quoted 10 lines
After providing all required memory to the TDX module, initialize TDX module extensions via TDH.EXT.INIT, then those add-on features can use their SEAMCALL leafs normally.
Signed-off-by Xu Yilun <[email protected]>Reviewed-by Xiaoyao Li <[email protected]>Reviewed-by Tony Lindgren <[email protected]>Reviewed-by Adrian Hunter <[email protected]>
With that addressed:
Re: [PATCH v3 5/6] x86/virt/tdx: Re-initialize the extensions on runtime TDX module update
In reply to Xu YilunQuoted 19 lines · Xu Yilun
Runtime TDX module update introduces a mechanism to update the module firmware while preserving and restoring TDX operations. The extensions functionalities should also be re-initialized as part of the restoration process.
The TDX architecture supports an update flow which allows updated extensions to consume more memory than their original boot-time requirement. So the arch defines the extensions re-initialization flow the same as boot-up initialization: the host queries TDX module how much additional memory needed, allocates it, adds it to the module via TDH.EXT.MEM.ADD, then re-initializes the extensions via TDH.EXT.INIT.
Linux runs the updates in
stop_machine()context, which prevents memory allocation. So for Linux, a compatible update must not install updated extensions that require additional memory.Given that the memory for the extensions must not increase, the re-initialization skips the memory adding steps. It is simplified as:
Quoted 8 lines
- Check if the extensions are supported via
TDX_FEATURES0_EXT. If not, skip the extensions re-initialization.- Re-initialize the extensions via TDH.EXT.INIT. The SEAMCALL leaf will fail if the updated extensions require more memory, which indicates the update image is not compatible.
Signed-off-by Xu Yilun <[email protected]>
Re: [PATCH v3 3/6] x86/virt/tdx: Add extra memory to TDX module for the extensions
In reply to [email protected]Quoted 21 lines · On Mon, Oct 05, 2026 at 05:56:52PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] x86/virt/tdx:kzalloc()breaks page alignment for hardware-facing struct under debug --commit
b27e647d007dabaa3a6b0eda05701af51bffc99e
Author: Xu Yilun <[email protected]>x86/virt/tdx: Add extra memory to TDX module for the extensions
This commit introduces a new SEAMCALL leaf TDH.EXT.MEM.ADD to allocate and add execution environment memory to the TDX module for extensions. It allocates a page-sized structure
tdx_hpa_listto pass the physical addresses of the allocated memory to the hardware.Quoted 4 lines
arch/x86/virt/vmx/tdx/tdx.c +0
-0[ ... ]
Quoted 8 lines
arch/x86/virt/vmx/tdx/tdx.c static __init int init_tdmrs(struct tdmr_info_list *tdmr_list) · line 1243
Quoted 5 lines
arch/x86/virt/vmx/tdx/tdx.c +5
-0+static __init u64 to_hpa_list_info (struct tdx_hpa_list *hpa_list, +unsigned int nr_pages) +{ +return FIELD_PREP (HPA_LIST_INFO_FIRST_ENTRY ,0 ) | + FIELD_PREP(HPA_LIST_INFO_PFN, PFN_DOWN(__pa(hpa_list))) |[Severity: High]
If thehpa_listpointer is unaligned due to slab redzones, does this
The size of struct tdx_hpa_list is guaranteed to be page aligned by:
static_assert(sizeof(struct tdx_hpa_list) == PAGE_SIZE);
So the allocated pointer must be page aligned, see below...
Quoted 29 lines
PFN_DOWN()truncation cause the TDX module to read the redzone metadata instead of the actual array?Quoted 2 lines
arch/x86/virt/vmx/tdx/tdx.c +2
-0+FIELD_PREP (HPA_LIST_INFO_LAST_ENTRY , nr_pages -1 ); +}[ ... ]
Quoted 7 lines
arch/x86/virt/vmx/tdx/tdx.c +7
-0+static __init int tdx_ext_mem_setup (void ) +{ +unsigned int required_pages, added_pages; +struct tdx_sys_info_ext sysinfo_ext; +struct tdx_hpa_list *hpa_list; +struct page *page; +int ret;[ ... ]
arch/x86/virt/vmx/tdx/tdx.c static __init int init_tdmrs(struct tdmr_info_list *tdmr_list) · line 1299
Quoted 6 lines
arch/x86/virt/vmx/tdx/tdx.c +6
-0+/* +* Allocate the container page for the HPA_LIST. tdx_hpa_list is +* guaranteed to be page-sized by static_assert(), so kzalloc() +* guarantees the page alignment. +*/ + hpa_list =kzalloc_obj (*hpa_list);[Severity: High]
Doeskzalloc_obj()actually guarantee page alignment whenCONFIG_KASANorCONFIG_SLUB_DEBUGare enabled?
Quoted 3 lines
Since slab redzones offset the object within the page, a
PAGE_SIZEallocation through the slab allocator might not return a page-aligned address under these debug configurations.
This is false positive. The following commit guarantees "the kmalloc() alignment to size explicitly guaranteed for power-of-two sizes under all configurations."
59bb47985c1d ("mm, sl[aou]b: guarantee natural alignment for kmalloc(power-of-two)")
Quoted 10 lines
Would it be safer to use the page allocator (e.g.
alloc_page()orget_zeroed_page()) instead of the slab allocator for a structure that requires strict page alignment when passed to hardware?Quoted 2 lines
arch/x86/virt/vmx/tdx/tdx.c +2
-0+if (!hpa_list) +return -ENOMEM ;Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [PATCH v3 3/6] x86/virt/tdx: Add extra memory to TDX module for the extensions
In reply to Edgecombe, Rick PQuoted 26 lines · Rick P
Quoted 11 lines · Xu Yilun
arch/x86/virt/vmx/tdx/tdx.c +11
-0+static __init int tdx_ext_mem_setup (void ) +{ +unsigned int required_pages, added_pages; +struct tdx_sys_info_ext sysinfo_ext; +struct tdx_hpa_list *hpa_list; +struct page *page; +int ret; + + ret =get_tdx_sys_info_ext (&sysinfo_ext); +if (ret) +return ret;arch/x86/virt/vmx/tdx/tdx.c static __init int init_tdmrs(struct tdmr_info_list *tdmr_list) · line 1292
Quoted 10 lines
arch/x86/virt/vmx/tdx/tdx.c +10
-0+ + required_pages = sysinfo_ext.memory_pool_required_pages; + +/* +* Skip the memory setup if no memory is required. This may happen when +* no add-on features requiring TDX module extensions are configured +* via TDH.SYS.CONFIG. +*/ +if (!required_pages) +return 0 ;Marshaling this through the struct just to get a set it to
required_pagesseems a bit silly. But I seerequired_pagesis needed for line length.
Quoted 5 lines
Two options:
Make the
get_tdx_sys_info_ext()just take an unsigned int pointer arg. When Chao's metadata reading stuff lands, we can keep the struct internal to the helper, to still re-use the macro struct mapper stuff.
struct tdx_sys_info_handoff is an existing example so I don't want to make it different.
Quoted 3 lines
Rename
memory_pool_required_pagestorequired_pagesto shorten the lines to something manageable and use it directly below.
I prefer the 2nd one. I also want to rename the sysinfo_ext to ext, to make the field short enough for direct usage everywhere.
struct tdx_sys_info ext ext;
ret = get_tdx_sys_info_ext(&ext);
if (ret)
return ret;
if (!ext.required_pages)
return 0;
...
Re: [PATCH v3 6/6] x86/virt/tdx: Support DPAMT when adding memory for the extensions
In reply to Kiryl ShutsemauQuoted 52 lines · Kiryl Shutsemau
Quoted 51 lines · Xu Yilun
Quoted 49 lines · Tony Lindgren
Quoted 36 lines · Xu Yilun
The TDX module uses Physical Address Metadata Table (PAMT) to track some state for each page of physical memory that it might use. 3 levels of PAMTs are used to track pages of different sizes - 1GB, 2MB and 4KB. Dynamic PAMT (DPAMT) allows saving memory by allocating 4KB PAMT dynamically, while the 1GB and 2MB levels remain allocated on TDX module initialization. The kernel has helpers to install 4K DPAMT.
Although the memory for the extensions is tens of megabytes and the host allocates it in a large chunk, the TDX module accepts it at 4K granularity. Thus the host has to install 4K DPAMT for each page of it.
Call
tdx_pamt_get()for each page before adding it to TDX module. Normally, these operations should not fail, and if they do, intentionally keep the error handling as simple as before - leak all pages, including the installed 4K DPAMT metadata.Signed-off-by Xu Yilun <[email protected]>---
v3:
- New patch--- arch/x86/virt/vmx/tdx/tdx.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)arch/x86/virt/vmx/tdx/tdx.c +2
-1@@ -1330,8 +1330,15 @@ static __init int tdx_ext_mem_setup(void)struct page *chunk = page + added_pages;unsigned int i; -for (i =0 ; i < chunk_pages; i++) +for (i =0 ; i < chunk_pages; i++) { + ret =tdx_pamt_get (page_to_pfn (chunk + i),NULL );arch/x86/virt/vmx/tdx/tdx.c static __init int tdx_ext_mem_setup(void) · line 1344
Quoted 10 lines
arch/x86/virt/vmx/tdx/tdx.c +6
-0+if (ret) { +WARN (1 ,"DPAMT setup error for extensions, stranded all pages\n" ); + goto out_free_hpa_list; + } + hpa_list->phys[i] =page_to_phys (chunk + i); + } ret =tdx_ext_mem_add (hpa_list, chunk_pages);if (ret) {How about just fold this change in into patch 3/6?
Quoted 6 lines
Quoted 2 lines
I think patch 3/6 is already quite heavy. Is it good that I put this patch right after patch 3/6?
But that breaks bisectability, no? Even placed right after 3/6, that commit fails TDX module init on a machine with DPAMT and extensions whenever the module asks for a non-zero pool.
It will not. Any patch for extension-backed features should be applied after this series. So the pool will always be zero until something like:
/* List all kernel-supported add-on features0 bits here */
-#define TDX_KERNEL_SUPPORTED_ADDON_FEATURES0 (0)
+#define TDX_KERNEL_SUPPORTED_ADDON_FEATURES0 (TDX_FEATURES0_QUOTE)
Re: [PATCH v3 4/6] x86/virt/tdx: Make TDX module initialize the extensions
In reply to Edgecombe, Rick PQuoted 8 lines · Rick P
Quoted 3 lines · Xu Yilun
TDX module extensions need memory for their execution environment to serve SEAMCALL leafs. Several add-on features depend on the extensions to execute their SEAMCALL leafs.
Can we get a little more about what this SEAMCALL is actually doing? To be able to reason about how long it will take. Obviously it does nothing now, but when we enable the fist extension.
Good to me. I re-wrote a new version:
TDX module extensions use kernel-gifted memory for their execution environment to serve SEAMCALL leafs. Several add-on features depend on the extensions to execute their SEAMCALL leafs.
After providing all required memory to the TDX module, initialize TDX module extensions via TDH.EXT.INIT. This is a time-consuming process, during which the TDX module loads its internal extension image into this memory and builds the execution environment. Once initialized, those add-on features can use their SEAMCALL leafs normally.
Re: [PATCH v3 6/6] x86/virt/tdx: Support DPAMT when adding memory for the extensions
In reply to Xu YilunQuoted 64 lines · Xu Yilun
Quoted 52 lines · Kiryl Shutsemau
Quoted 51 lines · Xu Yilun
Quoted 49 lines · Tony Lindgren
Quoted 36 lines · Xu Yilun
The TDX module uses Physical Address Metadata Table (PAMT) to track some state for each page of physical memory that it might use. 3 levels of PAMTs are used to track pages of different sizes - 1GB, 2MB and 4KB. Dynamic PAMT (DPAMT) allows saving memory by allocating 4KB PAMT dynamically, while the 1GB and 2MB levels remain allocated on TDX module initialization. The kernel has helpers to install 4K DPAMT.
Although the memory for the extensions is tens of megabytes and the host allocates it in a large chunk, the TDX module accepts it at 4K granularity. Thus the host has to install 4K DPAMT for each page of it.
Call
tdx_pamt_get()for each page before adding it to TDX module. Normally, these operations should not fail, and if they do, intentionally keep the error handling as simple as before - leak all pages, including the installed 4K DPAMT metadata.Signed-off-by Xu Yilun <[email protected]>---
v3:
- New patch--- arch/x86/virt/vmx/tdx/tdx.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)arch/x86/virt/vmx/tdx/tdx.c +2
-1@@ -1330,8 +1330,15 @@ static __init int tdx_ext_mem_setup(void)struct page *chunk = page + added_pages;unsigned int i; -for (i =0 ; i < chunk_pages; i++) +for (i =0 ; i < chunk_pages; i++) { + ret =tdx_pamt_get (page_to_pfn (chunk + i),NULL );arch/x86/virt/vmx/tdx/tdx.c static __init int tdx_ext_mem_setup(void) · line 1344
Quoted 10 lines
arch/x86/virt/vmx/tdx/tdx.c +6
-0+if (ret) { +WARN (1 ,"DPAMT setup error for extensions, stranded all pages\n" ); + goto out_free_hpa_list; + } + hpa_list->phys[i] =page_to_phys (chunk + i); + } ret =tdx_ext_mem_add (hpa_list, chunk_pages);if (ret) {How about just fold this change in into patch 3/6?
Quoted 6 lines
Quoted 2 lines
I think patch 3/6 is already quite heavy. Is it good that I put this patch right after patch 3/6?
But that breaks bisectability, no? Even placed right after 3/6, that commit fails TDX module init on a machine with DPAMT and extensions whenever the module asks for a non-zero pool.
It will not. Any patch for extension-backed features should be applied after this series. So the pool will always be zero until something like:
/* List all kernel-supported add-on features0 bits here */
Review
Quoted 2 lines
(quoted diff) +1
-1-#define TDX_KERNEL_SUPPORTED_ADDON_FEATURES0 (0) +#define TDX_KERNEL_SUPPORTED_ADDON_FEATURES0 (TDX_FEATURES0_QUOTE)
Okay, fair enough. Feel free to use my
on 3/6 and 6/6.
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Edgecombe, Rick P
maintainerRe: [PATCH v3 4/6] x86/virt/tdx: Make TDX module initialize the extensions
In reply to Xu YilunQuoted 11 lines · Xu Yilun
Good to me. I re-wrote a new version:
TDX module extensions use kernel-gifted memory for their execution environment to serve SEAMCALL leafs. Several add-on features depend on the extensions to execute their SEAMCALL leafs.
After providing all required memory to the TDX module, initialize TDX module extensions via TDH.EXT.INIT. This is a time-consuming process, during which the TDX module loads its internal extension image into this memory and builds the execution environment. Once initialized, those add-on features can use their SEAMCALL leafs normally.
Looks good. But I'd still wonder how "time consuming". Should we say on the order of booting a tiny VM? Maybe we don't know and that^ is the best we can do.
Edgecombe, Rick P
maintainerRe: [PATCH v3 3/6] x86/virt/tdx: Add extra memory to TDX module for the extensions
In reply to Xu YilunQuoted 5 lines · Xu Yilun
I prefer the 2nd one. I also want to rename the
sysinfo_extto ext, to make the field short enough for direct usage everywhere.struct
tdx_sys_infoext ext;
Quoted 8 lines
ret =
get_tdx_sys_info_ext(&ext);
if (ret)return ret;if (!ext.required_pages)
return 0;...
Ok to me.
Re: [PATCH v3 5/6] x86/virt/tdx: Re-initialize the extensions on runtime TDX module update
In reply to Edgecombe, Rick PQuoted 23 lines · Rick P
Quoted 22 lines · Xu Yilun
Runtime TDX module update introduces a mechanism to update the module firmware while preserving and restoring TDX operations. The extensions functionalities should also be re-initialized as part of the restoration process.
The TDX architecture supports an update flow which allows updated extensions to consume more memory than their original boot-time requirement. So the arch defines the extensions re-initialization flow the same as boot-up initialization: the host queries TDX module how much additional memory needed, allocates it, adds it to the module via TDH.EXT.MEM.ADD, then re-initializes the extensions via TDH.EXT.INIT.
Linux runs the updates in
stop_machine()context, which prevents memory allocation. So for Linux, a compatible update must not install updated extensions that require additional memory.Given that the memory for the extensions must not increase, the re-initialization skips the memory adding steps. It is simplified as:
- Check if the extensions are supported via
TDX_FEATURES0_EXT. If not, skip the extensions re-initialization.
Quoted 7 lines
Quoted 5 lines
- Re-initialize the extensions via TDH.EXT.INIT. The SEAMCALL leaf will fail if the updated extensions require more memory, which indicates the update image is not compatible.
Signed-off-by Xu Yilun <[email protected]>Reviewed-by Rick Edgecombe <[email protected]>
^
AI said you are edgecombe not edgecome. :)
I fixed them inplace.
Edgecombe, Rick P
maintainerRe: [PATCH v3 5/6] x86/virt/tdx: Re-initialize the extensions on runtime TDX module update
Quoted 5 lines · Xu Yilun
Quoted 1 line
Reviewed-by Rick Edgecombe <[email protected]>^
AI said you are edgecombe not edgecome. :)I fixed them inplace.
Jeez, can't do anything without AI these days :)