Runtime TDX module update support
Subsystems (6)
v8 cover Runtime TDX module update support
Hi Reviewers,
This v8 mainly addresses issues raised by Rick and collects his Reviewed-by tags.
The most notable changes are removing pieces that are not strictly needed for the basic runtime update support and tightening several commit messages to make them more direct and to the point. In particular, patches 08 and 15 were substantially rewritten, and commit messages for patches 16 and 17 were reworked, so those would benefit from another review. The rest of the series has only minor changes. I'm hoping this series can be merged for 7.2.
Changelog:v7->v8:
- rebase onto v7.1-rc1 to resolve conflicts with the merged VMXON series
- flatten the P-SEAMLDR sysfs ABI by exposing seamldr_version and num_remaining_updates directly under /sys/devices/faux/tdx_host/, and clarify when they are visible
- don't preemptively handle "PAGE_SIZE != 4KB" case
- tighten the fw_upload ABI: return FW_UPLOAD_ERR_BUSY for TD-build contention and collapse other update failures to firmware-invalid
- consolidate tdx_blob validation in a separate function and clarify what is validated by the kernel vs. P-SEAMLDR
- rework the changelog of aborting updates on any failure; always use READ_ONCE()/WRITE_ONCE() for the shared "failed" flag access
- rewrite the changelog of handling the race between updates and TD builds; always pass the compat flag to TDH.SYS.SHUTDOWN
- refresh only the TDX module update_version after a successful update; drop the broader post-update metadata/feature checks
- explain the kernel's role and responsibilities in TDX module updates in the cover letter
- collect new review/ack tags
- minor typo and wording fixes
- v7: https://lore.kernel.org/kvm/[email protected]/
(For transparency, note that I used AI tools to help proofread this cover-letter and commit messages)
This series adds support for runtime TDX module updates that preserve running TDX guests. It is also available at:
https://github.com/gaochaointel/linux-dev/commits/tdx-module-updates-v8/
== Background ==
Intel TDX isolates Trusted Domains (TDs), or confidential guests, from the host. A key component of Intel TDX is the TDX module, which enforces security policies to protect the memory and CPU states of TDs from the host. However, the TDX module is software that requires updates.
== Problems ==
Currently, the TDX module is loaded by the BIOS at boot time, and the only way to update it is through a reboot, which results in significant system downtime. Users expect the TDX module to be updatable at runtime without disrupting TDX guests.
== Solution ==
On TDX platforms, P-SEAMLDR[1] is a component within the protected SEAM range. It is loaded by the BIOS and provides the host with functions to install a TDX module at runtime.
This series implements runtime TDX module updates through the fw_upload mechanism. That interface is a good fit because TDX module selection is not a simple "load a known file from disk" problem. The update image to load depends on module versioning, compatibility rules. fw_upload lets userspace choose the module explicitly while the kernel provides the update mechanism.
This design intentionally keeps most update validation/policy in userspace. The kernel exposes the information userspace needs, such as TDX module version and P-SEAMLDR information, but userspace is responsible for understanding TDX module's versioning and compatibility rules and for choosing an appropriate update image (see "TDX module versioning" below).
The kernel still enforces the pieces that must be handled in-kernel:
1. Validate the tdx_blob header fields that are not passed through tothe TDX module. Just the standard overflow and reserved bits defensive ABI stuff.
2. Make sure no non-update SEAMCALLs are called during the update.
3. Make sure SEAMCALLs are on the right CPU, for any the user has made available to the kernel.
4. Handle the race between updates and concurrent TD builds by returning -EBUSY to userspace.
Everything else remains a userspace responsibility.
In the unlikely event the update fails, for example userspace picks an incompatible update image, or the image is otherwise corrupted, all TDs will experience SEAMCALL failures and be killed. The recovery of TD operation from that event requires a reboot.
Given there is no mechanism to quiesce SEAMCALLs, the TDs themselves must pause execution over an update. The most straightforward way to meet the 'pause TDs while update executes' constraint is to run the update in stop_machine() context. All other evaluated solutions export more complexity to KVM, or exports more fragility to userspace.
== How to test this series ==
Use the userspace tool below to select the appropriate TDX module and install it via the interfaces exposed by this series:
# git clone https://github.com/intel/tdx-module-binaries # cd tdx-module-binaries # python version_select_and_load.py --update
== Other information relevant to Runtime TDX module updates ==
=== TDX module versioning ===
Each TDX module is assigned a version number x.y.z, where x represents the "major" version, y the "minor" version, and z the "update" version.
Runtime TDX module updates are restricted to Z-stream releases.
Note that Z-stream releases do not necessarily guarantee compatibility. A new release may not be compatible with all previous versions. To address this, Intel provides a separate file containing compatibility information, which specifies the minimum module version required for a particular update. This information is referenced by the tool to determine if two modules are compatible.
=== TCB Stability ===
Updates change the TCB as viewed by attestation reports. In TDX there is a distinction between launch-time version and current version where runtime TDX module updates cause that latter version number to change, subject to Z-stream constraints.
The concern that a malicious host may attack confidential VMs by loading insecure updates was addressed by Alex in [3]. Similarly, the scenario where some "theoretical paranoid tenant" in the cloud wants to audit updates and stop trusting the host after updates until audit completion was also addressed in [4]. Users not in the cloud control the host machine and can manage updates themselves, so they don't have these concerns.
See more about the implications of current TCB version changes in attestation as summarized by Dave in [5].
=== TDX module Distribution Model ===
At a high level, Intel publishes all TDX modules on the github [2], along with a mapping_file.json which documents the compatibility information about each TDX module and a userspace tool to install the TDX module. OS vendors can package these modules and distribute them. Administrators install the package and use the tool to select the appropriate TDX module and install it via the interfaces exposed by this series.
[1]: https://cdrdv2.intel.com/v1/dl/getContent/733584
[2]: https://github.com/intel/tdx-module-binaries
[3]: https://lore.kernel.org/all/[email protected]/
[4]: https://lore.kernel.org/all/[email protected]/
[5]: https://lore.kernel.org/all/[email protected]/
Chao Gao (20):
coco/tdx-host: Introduce a "tdx_host" device coco/tdx-host: Expose TDX module version x86/virt/seamldr: Introduce a wrapper for P-SEAMLDR SEAMCALLs x86/virt/seamldr: Add a helper to retrieve P-SEAMLDR information coco/tdx-host: Expose P-SEAMLDR information via sysfs coco/tdx-host: Implement firmware upload sysfs ABI for TDX module
updates
x86/virt/seamldr: Allocate and populate a module update request x86/virt/seamldr: Introduce skeleton for TDX module updates x86/virt/seamldr: Shut down the current TDX module x86/virt/tdx: Reset software states during TDX module shutdown x86/virt/seamldr: Install a new TDX module x86/virt/seamldr: Do TDX per-CPU initialization after module
installation
x86/virt/tdx: Restore TDX module state
x86/virt/tdx: Refresh TDX module version after update x86/virt/tdx: Reject updates during concurrent TD build x86/virt/seamldr: Abort updates on failure coco/tdx-host: Don't expose P-SEAMLDR features on CPUs with erratum x86/virt/tdx: Enable TDX module runtime updates coco/tdx-host: Document TDX module update compatibility criteria x86/virt/tdx: Document TDX module update
Kai Huang (1):
x86/virt/tdx: Move low level SEAMCALL helpers out of <asm/tdx.h>
.../ABI/testing/sysfs-devices-faux-tdx-host | 67 ++++ Documentation/arch/x86/tdx.rst | 36 ++ arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/seamldr.h | 37 ++ arch/x86/include/asm/tdx.h | 69 ++-- arch/x86/include/asm/tdx_global_metadata.h | 4 + arch/x86/include/asm/vmx.h | 1 + arch/x86/kvm/vmx/tdx_errno.h | 2 - arch/x86/virt/vmx/tdx/Makefile | 2 +- arch/x86/virt/vmx/tdx/seamcall_internal.h | 109 ++++++ arch/x86/virt/vmx/tdx/seamldr.c | 330 ++++++++++++++++++ arch/x86/virt/vmx/tdx/tdx.c | 157 ++++++--- arch/x86/virt/vmx/tdx/tdx.h | 9 +- arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 17 +- drivers/virt/coco/Kconfig | 2 + drivers/virt/coco/Makefile | 1 + drivers/virt/coco/tdx-host/Kconfig | 12 + drivers/virt/coco/tdx-host/Makefile | 1 + drivers/virt/coco/tdx-host/tdx-host.c | 221 ++++++++++++ 19 files changed, 970 insertions(+), 108 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-devices-faux-tdx-host create mode 100644 arch/x86/include/asm/seamldr.h create mode 100644 arch/x86/virt/vmx/tdx/seamcall_internal.h create mode 100644 arch/x86/virt/vmx/tdx/seamldr.c create mode 100644 drivers/virt/coco/tdx-host/Kconfig create mode 100644 drivers/virt/coco/tdx-host/Makefile create mode 100644 drivers/virt/coco/tdx-host/tdx-host.c
base-commit: 254f49634ee16a731174d2ae34bc50bd5f45e731
Signature · 1 line
2.47.1
PATCH v8 1/21 x86/virt/tdx: Move low level SEAMCALL helpers out of <asm/tdx.h>
From: Kai Huang <[email protected]>
TDX host core code implements three seamcall*() helpers to make SEAMCALLs to the TDX module. Currently, they are implemented in <asm/tdx.h> and are exposed to other kernel code which includes <asm/tdx.h>.
However, other than the TDX host core, seamcall*() are not expected to be used by other kernel code directly. For instance, for all SEAMCALLs that are used by KVM, the TDX host core exports a wrapper function for each of them.
Move seamcall*() and related code out of <asm/tdx.h> and make them only visible to TDX host core.
Since TDX host core tdx.c is already very heavy, don't put low level seamcall*() code there but to a new dedicated "seamcall_internal.h". Also, currently tdx.c has seamcall_prerr*() helpers which additionally print error message when calling seamcall*() fails. Move them to "seamcall_internal.h" as well. In such way all low level SEAMCALL helpers are in a dedicated place, which is much more readable.
Copy the copyright notice from the original files and consolidate the date ranges to:
Copyright (C) 2021-2023 Intel Corporation
---
v8:
- s/SEAMCALL/SEAMCALLs [Rick]
--- arch/x86/include/asm/tdx.h | 47 ---------- arch/x86/virt/vmx/tdx/seamcall_internal.h | 109 ++++++++++++++++++++++ arch/x86/virt/vmx/tdx/tdx.c | 47 +--------- 3 files changed, 111 insertions(+), 92 deletions(-) create mode 100644 arch/x86/virt/vmx/tdx/seamcall_internal.h
arch/x86/include/asm/tdx.h +0 -47
@@ -97,54 +97,7 @@ static inline long tdx_kvm_hypercall(unsigned int nr, unsigned long p1,#endif /* CONFIG_INTEL_TDX_GUEST && CONFIG_KVM_GUEST */ #ifdef CONFIG_INTEL_TDX_HOST -u64 __seamcall (u64 fn,struct tdx_module_args *args); -u64 __seamcall_ret (u64 fn,struct tdx_module_args *args); -u64 __seamcall_saved_ret (u64 fn,struct tdx_module_args *args);void tdx_init (void ); - -#include <linux/preempt.h> -#include <asm/archrandom.h> -#include <asm/processor.h> - -typedef u64 (*sc_func_t )(u64 fn,struct tdx_module_args *args); - -static __always_inline u64 __seamcall_dirty_cache(sc_func_t func, u64 fn, -struct tdx_module_args *args) -{ -lockdep_assert_preemption_disabled (); - -/* -* SEAMCALLs are made to the TDX module and can generate dirty -* cachelines of TDX private memory. Mark cache state incoherent -* so that the cache can be flushed during kexec. -* -* This needs to be done before actually making the SEAMCALL, -* because kexec-ing CPU could send NMI to stop remote CPUs, -* in which case even disabling IRQ won't help here. -*/ -this_cpu_write (cache_state_incoherent, true); - -return func (fn, args); -} - -static __always_inline u64 sc_retry(sc_func_t func, u64 fn, -struct tdx_module_args *args) -{ -int retry =RDRAND_RETRY_LOOPS ; -u64 ret; - -do { -preempt_disable (); - ret =__seamcall_dirty_cache (func, fn, args); -preempt_enable (); - }while (ret ==TDX_RND_NO_ENTROPY && --retry); - -return ret; -} - -#define seamcall (_fn, _args) sc_retry(__seamcall, (_fn), (_args)) -#define seamcall_ret (_fn, _args) sc_retry(__seamcall_ret, (_fn), (_args)) -#define seamcall_saved_ret (_fn, _args) sc_retry(__seamcall_saved_ret, (_fn), (_args))const char *tdx_dump_mce_info (struct mce *m);const struct tdx_sys_info *tdx_get_sysinfo (void );
arch/x86/virt/vmx/tdx/seamcall_internal.h new file +109 -0
109 lines not on this page · raw message
arch/x86/virt/vmx/tdx/tdx.c +2 -45
59 lines not on this page · raw message
Signature · 1 line
2.47.1
Re: [PATCH v8 01/21] x86/virt/tdx: Move low level SEAMCALL helpers out of <asm/tdx.h>
Quoted 27 lines · Chao Gao
From: Kai Huang <[email protected]>
TDX host core code implements three seamcall*() helpers to make SEAMCALLs to the TDX module. Currently, they are implemented in <
asm/tdx.h> and are exposed to other kernel code which includes <asm/tdx.h>.However, other than the TDX host core, seamcall*() are not expected to be used by other kernel code directly. For instance, for all SEAMCALLs that are used by KVM, the TDX host core exports a wrapper function for each of them.
Move seamcall*() and related code out of <
asm/tdx.h> and make them only visible to TDX host core.Since TDX host core
tdx.cis already very heavy, don't put low level seamcall*() code there but to a new dedicated "seamcall_internal.h". Also, currentlytdx.chas seamcall_prerr*() helpers which additionally print error message when calling seamcall*() fails. Move them to "seamcall_internal.h" as well. In such way all low level SEAMCALL helpers are in a dedicated place, which is much more readable.Copy the copyright notice from the original files and consolidate the date ranges to:
Copyright (C) 2021-2023 Intel Corporation
Quoted 8 lines
Signed-off-by Kai Huang <[email protected]>Signed-off-by Chao Gao <[email protected]>Reviewed-by Zhenzhong Duan <[email protected]>Reviewed-by Binbin Wu <[email protected]>Reviewed-by Tony Lindgren <[email protected]>Reviewed-by Kiryl Shutsemau (Meta) <[email protected]>Reviewed-by Xiaoyao Li <[email protected]>Acked-by Dave Hansen <[email protected]>
PATCH v8 2/21 coco/tdx-host: Introduce a "tdx_host" device
In reply to Chao GaoTDX depends on a platform firmware module that is invoked via instructions similar to vmenter (i.e. enter into a new privileged "root-mode" context to manage private memory and private device mechanisms). It is a software construct that depends on the CPU vmxon state to enable invocation of TDX module ABIs. Unlike other Trusted Execution Environment (TEE) platform implementations that employ a firmware module running on a PCI device with an MMIO mailbox for communication, TDX has no hardware device to point to as the TEE Secure Manager (TSM).
Create a virtual device not only to align with other implementations but also to make it easier to
- expose metadata (e.g., TDX module version, seamldr version etc) to the userspace as device attributes
- implement firmware uploader APIs which are tied to a device. This is needed to support TDX module runtime updates
- enable TDX Connect which will share a common infrastructure with other platform implementations. In the TDX Connect context, every architecture has a TSM, represented by a PCIe or virtual device. The new "tdx_host" device will serve the TSM role.
A faux device is used for TDX because the TDX module is singular within the system and lacks associated platform resources. Using a faux device eliminates the need to create a stub bus.
The call to tdx_get_sysinfo() ensures that the TDX module is ready to provide services.
Note that AMD has a PCI device for the PSP for SEV and ARM CCA will likely have a faux device [1].
--- arch/x86/virt/vmx/tdx/tdx.c | 2 +- drivers/virt/coco/Kconfig | 2 ++ drivers/virt/coco/Makefile | 1 + drivers/virt/coco/tdx-host/Kconfig | 10 +++++++ drivers/virt/coco/tdx-host/Makefile | 1 + drivers/virt/coco/tdx-host/tdx-host.c | 43 +++++++++++++++++++++++++++ 6 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 drivers/virt/coco/tdx-host/Kconfig create mode 100644 drivers/virt/coco/tdx-host/Makefile create mode 100644 drivers/virt/coco/tdx-host/tdx-host.c
arch/x86/virt/vmx/tdx/tdx.c +1 -1
@@ -1487,7 +1487,7 @@ const struct tdx_sys_info *tdx_get_sysinfo(void)return (const struct tdx_sys_info *)&tdx_sysinfo; } -EXPORT_SYMBOL_FOR_KVM (tdx_get_sysinfo); +EXPORT_SYMBOL_FOR_MODULES (tdx_get_sysinfo,"kvm-intel,tdx-host" );u32 tdx_get_nr_guest_keyids (void ) {
drivers/virt/coco/Kconfig +2 -0
@@ -17,5 +17,7 @@ source "drivers/virt/coco/arm-cca-guest/Kconfig" source "drivers/virt/coco/guest/Kconfig" endif +source "drivers/virt/coco/tdx-host/Kconfig" + config TSM bool
drivers/virt/coco/Makefile +1 -0
@@ -6,6 +6,7 @@ obj-$(CONFIG_EFI_SECRET) += efi_secret/ obj-$(CONFIG_ARM_PKVM_GUEST) += pkvm-guest/ obj-$(CONFIG_SEV_GUEST) += sev-guest/ obj-$(CONFIG_INTEL_TDX_GUEST) += tdx-guest/ +obj-$(CONFIG_INTEL_TDX_HOST) += tdx-host/ obj-$(CONFIG_ARM_CCA_GUEST) += arm-cca-guest/ obj-$(CONFIG_TSM) += tsm-core.o obj-$(CONFIG_TSM_GUEST) += guest/
drivers/virt/coco/tdx-host/Kconfig new file +10 -0
@@ -0,0 +1,10 @@ +config TDX_HOST_SERVICES + tristate "TDX Host Services Driver" + depends on INTEL_TDX_HOST + default m + help + Enable access to TDX host services like module update and + extensions (e.g. TDX Connect). + + Say y or m if enabling support for confidential virtual machine + support (CONFIG_INTEL_TDX_HOST). The module is called tdx_host.ko.
drivers/virt/coco/tdx-host/Makefile new file +1 -0
@@ -0,0 +1 @@ +obj-$(CONFIG_TDX_HOST_SERVICES) += tdx-host.o
drivers/virt/coco/tdx-host/tdx-host.c new file +43 -0
43 lines not on this page · raw message
Signature · 1 line
2.47.1
PATCH v8 3/21 coco/tdx-host: Expose TDX module version
In reply to Chao GaoFor TDX module updates, userspace needs to select compatible update versions based on the current module version. This design delegates module selection complexity to userspace because TDX module update policies are complex and version series are platform-specific.
For example, the 1.5.x series is for certain platform generations, while the 2.0.x series is intended for others. And TDX module 1.5.x may be updated to 1.5.y but not to 1.5.y+1.
Expose the TDX module version to userspace via sysfs to aid module selection. Since the TDX faux device will drive module updates, expose the version as its attribute.
One bonus of exposing TDX module version via sysfs is: TDX module version information remains available even after dmesg logs are cleared.
Define TDX_VERSION_FMT macro for the TDX version format since it will be used multiple times. Also convert an existing print statement to use it.
== Background ==
The "faux device + device attribute" approach compares to other update mechanisms as follows:
1. AMD SEV leverages an existing PCI device for the PSP to expose metadata. TDX uses a faux device as it doesn't have PCI device in its architecture.
2. Microcode uses per-CPU virtual devices to report microcode revisions because CPUs can have different revisions. But, there is only a single TDX module, so exposing the TDX module version through a global TDX faux device is appropriate
3. ARM's CCA implementation isn't in-tree yet, but will likely follow a similar faux device approach, though it's unclear whether they need to expose firmware version information
--- .../ABI/testing/sysfs-devices-faux-tdx-host | 6 +++++ arch/x86/include/asm/tdx.h | 6 +++++ arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 2 +- drivers/virt/coco/tdx-host/tdx-host.c | 26 ++++++++++++++++++- 4 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-devices-faux-tdx-host
Documentation/ABI/testing/sysfs-devices-faux-tdx-host new file +6 -0
@@ -0,0 +1,6 @@ +What: /sys/devices/faux/tdx_host/version +Contact: [email protected] +Description: (RO) Report the version of the loaded TDX module. The TDX module + version is formatted as x.y.z, where "x" is the major version, + "y" is the minor version and "z" is the update version. Versions + are used for bug reporting, TDX module updates etc.
arch/x86/include/asm/tdx.h +6 -0
@@ -38,6 +38,12 @@#include <asm/tdx_global_metadata.h> #include <linux/pgtable.h> +/* +* TDX module and P-SEAMLDR version convention: "major.minor.update" +* (e.g., "1.5.08") with zero-padded two-digit update field. +*/ +#define TDX_VERSION_FMT "%u.%u.%02u" +/* * Used by the #VE exception handler to gather the #VE exception * info from the TDX module. This is a software only structure
arch/x86/virt/vmx/tdx/tdx_global_metadata.c +1 -1
@@ -106,7 +106,7 @@ static __init int get_tdx_sys_info(struct tdx_sys_info *sysinfo) ret = ret ?:get_tdx_sys_info_version (&sysinfo->version); -pr_info ("Module version: %u.%u.%02u\n" , +pr_info ("Module version: " TDX_VERSION_FMT "\n" , sysinfo->version.major_version, sysinfo->version.minor_version, sysinfo->version.update_version);
drivers/virt/coco/tdx-host/tdx-host.c +25 -1
@@ -8,6 +8,7 @@#include <linux/device/faux.h> #include <linux/module.h> #include <linux/mod_devicetable.h> +#include <linux/sysfs.h> #include <asm/cpu_device_id.h> #include <asm/tdx.h>
@@ -18,6 +19,29 @@ static const struct x86_cpu_id tdx_host_ids[] = { };MODULE_DEVICE_TABLE (x86cpu, tdx_host_ids); +static ssize_t version_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +const struct tdx_sys_info *tdx_sysinfo =tdx_get_sysinfo (); +const struct tdx_sys_info_version *ver; + +if (!tdx_sysinfo) +return -ENXIO ; + + ver = &tdx_sysinfo->version; + +return sysfs_emit (buf,TDX_VERSION_FMT "\n" , ver->major_version, + ver->minor_version, + ver->update_version); +} +static DEVICE_ATTR_RO (version); + +static struct attribute *tdx_host_attrs[] = { + &dev_attr_version.attr, +NULL , +}; +ATTRIBUTE_GROUPS (tdx_host); +static struct faux_device *fdev;static int __init tdx_host_init (void )
@@ -25,7 +49,7 @@ static int __init tdx_host_init(void)if (!x86_match_cpu (tdx_host_ids) || !tdx_get_sysinfo ())return -ENODEV ; - fdev =faux_device_create (KBUILD_MODNAME ,NULL ,NULL ); + fdev =faux_device_create_with_groups (KBUILD_MODNAME ,NULL ,NULL , tdx_host_groups);if (!fdev)return -ENODEV ;
Signature · 1 line
2.47.1
PATCH v8 4/21 x86/virt/seamldr: Introduce a wrapper for P-SEAMLDR SEAMCALLs
In reply to Chao GaoThe TDX architecture uses the "SEAMCALL" instruction to communicate with SEAM mode software. Right now, the only SEAM mode software that the kernel communicates with is the TDX module. But, there is actually another component that runs in SEAM mode but it is separate from the TDX module:
the persistent SEAM loader or "P-SEAMLDR". Right now, the only component that communicates with it is the BIOS which loads the TDX module itself at boot. But, to support updating the TDX module, the kernel now needs to be able to talk to it.
P-SEAMLDR SEAMCALLs differ from TDX module SEAMCALLs in areas such as concurrency requirements. Add a P-SEAMLDR wrapper to handle these differences and prepare for implementing concrete functions.
Use seamcall_prerr() (not '_ret') because current P-SEAMLDR calls do not use any output registers other than RAX.
Note that unlike P-SEAMLDR, there is also a non-persistent SEAM loader ("NP-SEAMLDR"). This is an authenticated code module (ACM) that is not callable at runtime. Only BIOS launches it to load P-SEAMLDR at boot; the kernel does not need to interact with it for runtime update.
--- arch/x86/virt/vmx/tdx/Makefile | 2 +- arch/x86/virt/vmx/tdx/seamldr.c | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 arch/x86/virt/vmx/tdx/seamldr.c
arch/x86/virt/vmx/tdx/Makefile +1 -1
@@ -1,2 +1,2 @@ # SPDX-License-Identifier: GPL-2.0-only -obj-y += seamcall.o tdx.o +obj-y += seamcall.o seamldr.o tdx.o
arch/x86/virt/vmx/tdx/seamldr.c new file +25 -0
@@ -0,0 +1,25 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* P-SEAMLDR support for TDX module management features like runtime updates +* +* Copyright (C) 2025 Intel Corporation +*/ +#define pr_fmt (fmt) "seamldr: " fmt + +#include <linux/spinlock.h> + +#include "seamcall_internal.h" + +/* +* Serialize P-SEAMLDR calls since the hardware only allows a single CPU to +* interact with P-SEAMLDR simultaneously. Use raw version as the calls can +* be made with interrupts disabled, where plain spinlocks are prohibited in +* PREEMPT_RT kernels as they become sleeping locks. +*/ +static DEFINE_RAW_SPINLOCK (seamldr_lock); + +static __maybe_unused int seamldr_call (u64 fn,struct tdx_module_args *args) +{ +guard (raw_spinlock)(&seamldr_lock); +return seamcall_prerr (fn, args); +}
Signature · 1 line
2.47.1
PATCH v8 5/21 x86/virt/seamldr: Add a helper to retrieve P-SEAMLDR information
In reply to Chao GaoP-SEAMLDR reports its state via SEAMLDR.INFO, including its version and the number of remaining runtime updates.
This information is useful for userspace. For example, the admin can use the P-SEAMLDR version to determine whether a candidate TDX module is compatible with the running loader, and can use the remaining update count to determine whether another runtime update is still possible.
Add a helper to retrieve P-SEAMLDR information in preparation for exposing P-SEAMLDR version and other necessary information to userspace. Export the new kAPI for use by tdx-host.ko.
Note that there are two distinct P-SEAMLDR APIs with similar names:
SEAMLDR.INFO: Returns a SEAMLDR_INFO structure containing SEAMLDR
information such as version and remaining updates.
SEAMLDR.SEAMINFO: Returns a SEAMLDR_SEAMINFO structure containing SEAM
and system information such as Convertible Memory Regions (CMRs) and number of CPUs and sockets.
The former is used here.
For details, see "Intel® Trust Domain Extensions - SEAM Loader (SEAMLDR) Interface Specification".
--- arch/x86/include/asm/seamldr.h | 36 +++++++++++++++++++++++++++++++++ arch/x86/virt/vmx/tdx/seamldr.c | 20 +++++++++++++++++- 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 arch/x86/include/asm/seamldr.h
arch/x86/include/asm/seamldr.h new file +36 -0
@@ -0,0 +1,36 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_X86_SEAMLDR_H +#define _ASM_X86_SEAMLDR_H + +#include <linux/types.h> + +/* +* This is called the "SEAMLDR_INFO" data structure and is defined +* in "SEAM Loader (SEAMLDR) Interface Specification". +* +* The SEAMLDR.INFO documentation requires this to be aligned to a +* 256-byte boundary. +*/ +struct seamldr_info { +u32 version; +u32 attributes; +u32 vendor_id; +u32 build_date; +u16 build_num; +u16 minor_version; +u16 major_version; +u16 update_version; +u32 acm_x2apicid; +u32 num_remaining_updates; +u8 seam_info[128 ]; +u8 seam_ready; +u8 seam_debug; +u8 p_seam_ready; +u8 reserved[93 ]; +} __packed__aligned(256) ; + +static_assert (sizeof (struct seamldr_info ) ==256 ); + +int seamldr_get_info (struct seamldr_info *seamldr_info); + +#endif /* _ASM_X86_SEAMLDR_H */
arch/x86/virt/vmx/tdx/seamldr.c +19 -1
@@ -8,8 +8,13 @@#include <linux/spinlock.h> +#include <asm/seamldr.h> +#include "seamcall_internal.h" +/* P-SEAMLDR SEAMCALL leaf function */ +#define P_SEAMLDR_INFO 0x8000000000000000 +/* * Serialize P-SEAMLDR calls since the hardware only allows a single CPU to * interact with P-SEAMLDR simultaneously. Use raw version as the calls can
@@ -18,8 +23,21 @@*/ static DEFINE_RAW_SPINLOCK (seamldr_lock); -static __maybe_unused int seamldr_call (u64 fn,struct tdx_module_args *args) +static int seamldr_call (u64 fn,struct tdx_module_args *args) {guard (raw_spinlock)(&seamldr_lock);return seamcall_prerr (fn, args); } + +int seamldr_get_info (struct seamldr_info *seamldr_info) +{ +struct tdx_module_args args = {}; + +/* +* Use slow_virt_to_phys() since @seamldr_info may be allocated on +* the stack. +*/ + args.rcx =slow_virt_to_phys (seamldr_info); +return seamldr_call (P_SEAMLDR_INFO , &args); +} +EXPORT_SYMBOL_FOR_MODULES (seamldr_get_info,"tdx-host" );
Signature · 1 line
2.47.1
PATCH v8 6/21 coco/tdx-host: Expose P-SEAMLDR information via sysfs
In reply to Chao GaoTDX module updates require userspace to select the appropriate module to load. Expose necessary information to facilitate this decision. Two values are needed:
- P-SEAMLDR version: for compatibility checks between TDX module and P-SEAMLDR
- num_remaining_updates: indicates how many updates can be performed
Expose them as tdx-host device attributes. Make seamldr attributes visible only when the update feature is supported, as that's their sole purpose.
Note that the underlying P-SEAMLDR attributes are available regardless of update support; this only restricts their visibility in Linux.
---
v8:
- explain when the two attributes are available and how they relate to TDX module update support [Rick]
- drop erratum stuff [Rick, Dave]
- drop the separate directory for p-seamldr attributes [Dave]
- s/SEAM loader/P-SEAMLDR
- don't use DEFINE_SIMPLE_SYSFS_GROUP_VISIBLE() as it doesn't work on an unnamed group.
--- .../ABI/testing/sysfs-devices-faux-tdx-host | 22 +++++++ arch/x86/include/asm/tdx.h | 6 ++ drivers/virt/coco/tdx-host/tdx-host.c | 65 ++++++++++++++++++- 3 files changed, 92 insertions(+), 1 deletion(-)
Documentation/ABI/testing/sysfs-devices-faux-tdx-host +22 -0
@@ -4,3 +4,25 @@ Description: (RO) Report the version of the loaded TDX module. The TDX module version is formatted as x.y.z, where "x" is the major version, "y" is the minor version and "z" is the update version. Versions are used for bug reporting, TDX module updates etc. + +What: /sys/devices/faux/tdx_host/seamldr_version +Contact: [email protected] +Description: (RO) Report the version of the loaded P-SEAMLDR. The P-SEAMLDR + version is formatted as x.y.z, where "x" is the major version, + "y" is the minor version and "z" is the update version. Versions + are used for bug reporting and compatibility checks. + +What: /sys/devices/faux/tdx_host/num_remaining_updates +Contact: [email protected] +Description: (RO) Report the number of remaining updates. TDX maintains a + log about each TDX module that has been loaded. This log has + a finite size, which limits the number of TDX module updates + that can be performed. + + After each successful update, the number reduces by one. Once it + reaches zero, further updates will fail until next reboot. The + number is always zero if the P-SEAMLDR doesn't support updates. + + See Intel® Trust Domain Extensions - SEAM Loader (SEAMLDR) + Interface Specification, Chapter "SEAMLDR_INFO" and Chapter + "SEAMLDR.INSTALL" for more information.
arch/x86/include/asm/tdx.h +6 -0
@@ -107,6 +107,12 @@ void tdx_init(void);const char *tdx_dump_mce_info (struct mce *m);const struct tdx_sys_info *tdx_get_sysinfo (void ); +static inline bool tdx_supports_runtime_update (const struct tdx_sys_info *sysinfo) +{ +/* To be enabled when kernel is ready. */ +return false; +} +int tdx_guest_keyid_alloc (void );u32 tdx_get_nr_guest_keyids (void );void tdx_guest_keyid_free (unsigned int keyid);
drivers/virt/coco/tdx-host/tdx-host.c +64 -1
77 lines not on this page · raw message
Signature · 1 line
2.47.1
PATCH v8 7/21 coco/tdx-host: Implement firmware upload sysfs ABI for TDX module updates
In reply to Chao GaoLinux kernel supports two primary firmware update mechanisms:
- request_firmware()
- firmware upload (or fw_upload)
The former is used by microcode updates, SEV firmware updates, etc. The latter is used by CXL and FPGA firmware updates.
One key difference between them is: request_firmware() loads a named file from the filesystem where the filename is kernel-controlled, while fw_upload accepts firmware data directly from userspace.
Use fw_upload for TDX module updates as loading a named file isn't suitable for TDX (see below for more reasons). Specifically, register TDX faux device with fw_upload framework to expose sysfs interfaces and implement operations to process data blobs supplied by userspace.
Why fw_upload instead of request_firmware()?
============================================
The explicit file selection capabilities of fw_upload is preferred over the implicit file selection of request_firmware() for the following reasons:
a. Intel distributes all versions of the TDX module, allowing admins to load any version rather than always defaulting to the latest. This flexibility is necessary because future extensions may require reverting to a previous version to clear fatal errors.
b. Some module version series are platform-specific. For example, the 1.5.x series is for certain platform generations, while the 2.0.x series is intended for others.
c. The update policy for TDX module updates is non-linear at times. The latest TDX module may not be compatible. For example, TDX module 1.5.x may be updated to 1.5.y but not to 1.5.y+1. This policy is documented separately in a file released along with each TDX module release.
So, the default policy of "request_firmware()" of "always load latest", is not suitable for TDX. Userspace needs to deploy a more sophisticated policy check (e.g., latest may not be compatible), and there is potential operator choice to consider.
Just have userspace pick rather than add kernel mechanism to change the default policy of request_firmware().
---
Dave also suggested making .poll_complete() optional in fw_upload_ops. That will be handled in a separate series.
v8:
- don't preemptively handle bugs that never occurs, i.e., drop the WARN_ON_ONCE(offset). [Dave]
- tighten up the comment in .poll_complete() [Dave]
- make the error-code mapping reviewable [Rick]
--- arch/x86/include/asm/seamldr.h | 1 + arch/x86/virt/vmx/tdx/seamldr.c | 15 +++++ drivers/virt/coco/tdx-host/Kconfig | 2 + drivers/virt/coco/tdx-host/tdx-host.c | 87 ++++++++++++++++++++++++++- 4 files changed, 102 insertions(+), 3 deletions(-)
arch/x86/include/asm/seamldr.h +1 -0
@@ -32,5 +32,6 @@ struct seamldr_info {static_assert (sizeof (struct seamldr_info ) ==256 );int seamldr_get_info (struct seamldr_info *seamldr_info); +int seamldr_install_module (const u8 *data,u32 size);#endif /* _ASM_X86_SEAMLDR_H */
arch/x86/virt/vmx/tdx/seamldr.c +15 -0
@@ -6,6 +6,7 @@*/ #define pr_fmt (fmt) "seamldr: " fmt +#include <linux/mm.h> #include <linux/spinlock.h> #include <asm/seamldr.h>
@@ -41,3 +42,17 @@ int seamldr_get_info(struct seamldr_info *seamldr_info)return seamldr_call (P_SEAMLDR_INFO , &args); }EXPORT_SYMBOL_FOR_MODULES (seamldr_get_info,"tdx-host" ); + +/** +* seamldr_install_module - Install a new TDX module. +* @data: Pointer to the TDX module update blob. +* @size: Size of the TDX module update blob. +* +* Returns 0 on success, negative error code on failure. +*/ +int seamldr_install_module (const u8 *data,u32 size) +{ +/* TODO: Update TDX module here */ +return 0 ; +} +EXPORT_SYMBOL_FOR_MODULES (seamldr_install_module,"tdx-host" );
drivers/virt/coco/tdx-host/Kconfig +2 -0
@@ -1,6 +1,8 @@ config TDX_HOST_SERVICES tristate "TDX Host Services Driver" depends on INTEL_TDX_HOST + select FW_LOADER + select FW_UPLOAD default m help Enable access to TDX host services like module update and
drivers/virt/coco/tdx-host/tdx-host.c +84 -3
117 lines not on this page · raw message
Signature · 1 line
2.47.1
Re: [PATCH v8 07/21] coco/tdx-host: Implement firmware upload sysfs ABI for TDX module updates
Quoted 3 lines · Chao Gao
Linux kernel supports two primary firmware update mechanisms:
-request_firmware()
- firmware upload (orfw_upload)
All the stuff here is good info, but it was hard to extract the implementation information from the background.
I think this would do:
Select fw_upload for doing TDX module updates. The process of selecting among available update images is complicated and nuanced. Punt the selection policy out to userspace.
...
drivers/virt/coco/tdx-host/tdx-host.c static const struct attribute_group *tdx_host_groups[] = { · line 170
Quoted 6 lines
drivers/virt/coco/tdx-host/tdx-host.c +6
-0+static int seamldr_init (struct device *dev) +{ +struct fw_upload *tdx_fwl; + +if (!can_expose_seamldr ()) +return 0 ;
can_expose_seamldr() has a not great name.
Why not just have naming that says:
if (supports_runtime_update()) ...
Why abstract this out to what can or can't be exposed?
Re: [PATCH v8 07/21] coco/tdx-host: Implement firmware upload sysfs ABI for TDX module updates
Quoted 5 lines · Dave Hansen
Quoted 3 lines · Chao Gao
Linux kernel supports two primary firmware update mechanisms:
-request_firmware()
- firmware upload (orfw_upload)
Quoted 8 lines
All the stuff here is good info, but it was hard to extract the implementation information from the background.
I think this would do:
Select
fw_uploadfor doing TDX module updates. The process of selecting among available update images is complicated and nuanced. Punt the selection policy out to userspace.
Agreed. I'll add that as a TL;DR.
Quoted 11 lines
...
drivers/virt/coco/tdx-host/tdx-host.c static const struct attribute_group *tdx_host_groups[] = { · line 170
Quoted 6 lines
drivers/virt/coco/tdx-host/tdx-host.c +6
-0+static int seamldr_init (struct device *dev) +{ +struct fw_upload *tdx_fwl; + +if (!can_expose_seamldr ()) +return 0 ;
can_expose_seamldr()has a not great name.
Quoted 6 lines
Why not just have naming that says:
if (supports_runtime_update()) ...Why abstract this out to what can or can't be exposed?
Sure, I'll use supports_runtime_update().
I originally used can_expose_seamldr() because I was focused on the VMCS-clobbering erratum, where SEAMLDR sysfs cannot be exposed to userspace.
PATCH v8 8/21 x86/virt/seamldr: Allocate and populate a module update request
In reply to Chao GaoP-SEAMLDR uses the SEAMLDR_PARAMS structure to describe TDX module update requests. This structure contains physical addresses pointing to the module binary and its signature file (or sigstruct), along with an update scenario field.
TDX modules are distributed in the tdx_blob format defined in blob_structure.txt from the "Intel TDX module Binaries Repository". A tdx_blob contains a header, sigstruct, and module binary. This is also the format supplied by the userspace to the kernel.
Parse the tdx_blob format and populate a SEAMLDR_PARAMS structure. The header is consumed solely by the kernel to extract the sigstruct and module, so validate it before processing to protect the kernel ABI. The sigstruct and module are passed to and validated by P-SEAMLDR, so don't duplicate any validation in the kernel.
Note: the sigstruct_pa field in SEAMLDR_PARAMS has been extended to a 4-element array. The updated "SEAM Loader (SEAMLDR) Interface Specification" will be published separately.
---
v8:
- Consolidate all tdx_blob validation into a dedicated helper instead of scattering checks across two functions. [Rick]
- Clarify which validations are performed by the kernel and which are intentionally left to P-SEAMLDR/userspace. [Rick]
- Drop free_seamldr_params() helper [Rick]
- Change params->version assignment to a single sig_size > 4K expression [Rick]
- Don't preemptively handle "PAGE_SIZE != 4KB" [Dave]
--- arch/x86/virt/vmx/tdx/seamldr.c | 155 ++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+)
arch/x86/virt/vmx/tdx/seamldr.c +155 -0
@@ -7,6 +7,7 @@#define pr_fmt (fmt) "seamldr: " fmt#include <linux/mm.h> +#include <linux/slab.h> #include <linux/spinlock.h> #include <asm/seamldr.h>
@@ -16,6 +17,33 @@/* P-SEAMLDR SEAMCALL leaf function */ #define P_SEAMLDR_INFO 0x8000000000000000 +#define SEAMLDR_MAX_NR_MODULE_PAGES 496 +#define SEAMLDR_MAX_NR_SIG_PAGES 4 + +/* +* The seamldr_params "scenario" field specifies the operation mode: +* 0: Install TDX module from scratch (not used by kernel) +* 1: Update existing TDX module to a compatible version +*/ +#define SEAMLDR_SCENARIO_UPDATE 1 + +/* +* This is called the "SEAMLDR_PARAMS" data structure and is defined +* in "SEAM Loader (SEAMLDR) Interface Specification". +* +* It describes the TDX module that will be installed. +*/ +struct seamldr_params { +u32 version; +u32 scenario; +u64 sigstruct_pa[SEAMLDR_MAX_NR_SIG_PAGES ]; +u8 reserved[80 ]; +u64 num_module_pages; +u64 mod_pages_pa_list[SEAMLDR_MAX_NR_MODULE_PAGES ]; +} __packed; + +static_assert (sizeof (struct seamldr_params ) ==4096 ); +/* * Serialize P-SEAMLDR calls since the hardware only allows a single CPU to * interact with P-SEAMLDR simultaneously. Use raw version as the calls can
@@ -43,6 +71,128 @@ int seamldr_get_info(struct seamldr_info *seamldr_info) }EXPORT_SYMBOL_FOR_MODULES (seamldr_get_info,"tdx-host" ); +/* +* Intel TDX module blob. Its format is defined at: +* https://github.com/intel/tdx-module-binaries/blob/main/blob_structure.txt +* +* Note this structure differs from the reference above: the two variable-length +* fields "@sigstruct" and "@module" are represented as a single "@data" field +* here and split programmatically using the offset_of_module value. +* +* Note @offset_of_module is relative to the start of struct tdx_blob, not +* @data, and @length is the total length of the blob, not the length of +* @data. +*/ +struct tdx_blob { +u16 version; +u16 checksum; +u32 offset_of_module; +u8 signature[8 ]; +u32 length; +u32 reserved0; +u64 reserved1[509 ]; +u8 data[]; +} __packed; + +/* Supported versions of the tdx_blob */ +#define TDX_BLOB_VERSION_1 0x100 + +/* +* Blob fields are processed by the kernel and the payloads +* are passed to the TDX module. Do normal user input type +* check for any fields that don't get passed to the TDX module. +*/ +static const struct tdx_blob *get_and_check_blob (const u8 *data,u32 size) +{ +const struct tdx_blob *blob = (const void *)data; + +/* +* Ensure the size is valid otherwise reading any field from the +* blob may overflow. +*/ +if (size <=sizeof (struct tdx_blob )) +return ERR_PTR (-EINVAL ); + +/* +* Don't care about user passing the wrong file, but protect +* kernel ABI by preventing accepting garbage. +*/ +if (memcmp (blob->signature,"TDX-BLOB" ,8 )) +return ERR_PTR (-EINVAL ); + +/* +* Ensure the offset of the module is within valid bounds and +* page-aligned. +*/ +if (blob->offset_of_module >= size || blob->offset_of_module <=sizeof (struct tdx_blob )) +return ERR_PTR (-EINVAL ); +if (!IS_ALIGNED (blob->offset_of_module,PAGE_SIZE )) +return ERR_PTR (-EINVAL ); + +if (blob->version !=TDX_BLOB_VERSION_1 ) +return ERR_PTR (-EINVAL ); + +if (blob->reserved0 ||memchr_inv (blob->reserved1,0 ,sizeof (blob->reserved1))) +return ERR_PTR (-EINVAL ); + +return blob; +} + +static struct seamldr_params *alloc_seamldr_params (const struct tdx_blob *blob,unsigned int blob_size) +{ +struct seamldr_params *params; +int module_pg_cnt, sig_pg_cnt; +const u8 *sig, *module; +int i; + + params = (struct seamldr_params *)get_zeroed_page (GFP_KERNEL ); +if (!params) +return ERR_PTR (-ENOMEM ); + +/* +* Split the blob into a sigstruct and a module. Assume all +* size/offsets are within bounds of blob_size due to prior checks. +*/ + sig = blob->data; + sig_pg_cnt = (blob->offset_of_module -sizeof (struct tdx_blob )) >>PAGE_SHIFT ; + module = (const u8 *)blob + blob->offset_of_module; + module_pg_cnt = (blob_size - blob->offset_of_module) >>PAGE_SHIFT ; + +/* +* Only use version 1 when required (sigstruct > 4KB) for backward +* compatibility with P-SEAMLDR that lacks version 1 support. +*/ + params->version = sig_pg_cnt >1 ; + params->scenario =SEAMLDR_SCENARIO_UPDATE ; + +for (i =0 ; i <MIN (sig_pg_cnt,SEAMLDR_MAX_NR_SIG_PAGES ); i++) { + params->sigstruct_pa[i] =vmalloc_to_pfn (sig) <<PAGE_SHIFT ; + sig +=PAGE_SIZE ; + } + + params->num_module_pages =MIN (module_pg_cnt,SEAMLDR_MAX_NR_MODULE_PAGES ); +for (i =0 ; i < params->num_module_pages; i++) { + params->mod_pages_pa_list[i] =vmalloc_to_pfn (module) <<PAGE_SHIFT ; + module +=PAGE_SIZE ; + } + +return params; +} + +static struct seamldr_params *init_seamldr_params (const u8 *data,u32 size) +{ +const struct tdx_blob *blob; + + blob =get_and_check_blob (data, size); +if (IS_ERR (blob)) +return ERR_CAST (blob); + +return alloc_seamldr_params (blob, size); +} + +DEFINE_FREE(free_seamldr_params, struct seamldr_params *, + if (!IS_ERR_OR_NULL(_T)) free_page((unsigned long)_T)) +/** * seamldr_install_module - Install a new TDX module. * @data: Pointer to the TDX module update blob.
@@ -52,6 +202,11 @@ EXPORT_SYMBOL_FOR_MODULES(seamldr_get_info, "tdx-host");*/ int seamldr_install_module (const u8 *data,u32 size) { + struct seamldr_params *params __free(free_seamldr_params) = +init_seamldr_params (data, size); +if (IS_ERR (params)) +return PTR_ERR (params); +/* TODO: Update TDX module here */ return 0 ; }
Signature · 1 line
2.47.1
Re: [PATCH v8 08/21] x86/virt/seamldr: Allocate and populate a module update request
Quoted 16 lines · Chao Gao
P-SEAMLDR uses the
SEAMLDR_PARAMSstructure to describe TDX module update requests. This structure contains physical addresses pointing to the module binary and its signature file (or sigstruct), along with an update scenario field.TDX modules are distributed in the
tdx_blobformat defined in blob_structure.txt from the "Intel TDX module Binaries Repository". Atdx_blobcontains a header, sigstruct, and module binary. This is also the format supplied by the userspace to the kernel.Parse the
tdx_blobformat and populate aSEAMLDR_PARAMSstructure. The header is consumed solely by the kernel to extract the sigstruct and module, so validate it before processing to protect the kernel ABI. The sigstruct and module are passed to and validated by P-SEAMLDR, so don't duplicate any validation in the kernel.
Quoted 3 lines
Note: the
sigstruct_pafield inSEAMLDR_PARAMShas been extended to a 4-element array. The updated "SEAM Loader (SEAMLDR) Interface Specification" will be published separately.
These changelogs have all the right info, but I find them really hard to parse. For instance, if you're going to have a 'struct seamldr_params', then just stick with that name. Don't use the "SEAMLDR_PARAMS" name too.
Start with the data structures:
There are two important ABIs here:
'struct tdx_blob' - the on-disk and in-memory format for a TDX
module update image.
'struct seamldr_params' - The in-memory ABI passed to the TDX module
loader. Points to a single 'struct tdx_blob'
Quoted 13 lines
arch/x86/virt/vmx/tdx/seamldr.c +1
-0@@ -7,6 +7,7 @@#define pr_fmt (fmt) "seamldr: " fmt#include <linux/mm.h> +#include <linux/slab.h> #include <linux/spinlock.h> #include <asm/seamldr.h>
arch/x86/virt/vmx/tdx/seamldr.c line 21
Quoted 5 lines
arch/x86/virt/vmx/tdx/seamldr.c +2
-0@@ -16,6 +17,33 @@/* P-SEAMLDR SEAMCALL leaf function */ #define P_SEAMLDR_INFO 0x8000000000000000 +#define SEAMLDR_MAX_NR_MODULE_PAGES 496 +#define SEAMLDR_MAX_NR_SIG_PAGES 4
Gah. All this complexity for the variable-length sigstruct to save a maximum of 4 pages. Wow.
This whole thing could have been:
struct tdx_image {
u16 version; // This ABI is always 0x100
u16 checksum;
u8 signature[8];
u32 length;
u8 reserved[4076];
u8 sigstruct[SIGSTRUCT_SIZE];
u8 module[];
}One variable array. No module offset calculations or munging.
Why do we do this to ourselves for 3 measly pages? ;)
Quoted 28 lines
arch/x86/virt/vmx/tdx/seamldr.c +24
-0+/* +* The seamldr_params "scenario" field specifies the operation mode: +* 0: Install TDX module from scratch (not used by kernel) +* 1: Update existing TDX module to a compatible version +*/ +#define SEAMLDR_SCENARIO_UPDATE 1 + +/* +* This is called the "SEAMLDR_PARAMS" data structure and is defined +* in "SEAM Loader (SEAMLDR) Interface Specification". +* +* It describes the TDX module that will be installed. +*/ +struct seamldr_params { +u32 version; +u32 scenario; +u64 sigstruct_pa[SEAMLDR_MAX_NR_SIG_PAGES ]; +u8 reserved[80 ]; +u64 num_module_pages; +u64 mod_pages_pa_list[SEAMLDR_MAX_NR_MODULE_PAGES ]; +} __packed; + +static_assert (sizeof (struct seamldr_params ) ==4096 ); +/* * Serialize P-SEAMLDR calls since the hardware only allows a single CPU to * interact with P-SEAMLDR simultaneously. Use raw version as the calls can
arch/x86/virt/vmx/tdx/seamldr.c int seamldr_get_info(struct seamldr_info *seamldr_info) · line 76
Quoted 6 lines
arch/x86/virt/vmx/tdx/seamldr.c +3
-0@@ -43,6 +71,128 @@ int seamldr_get_info(struct seamldr_info *seamldr_info) }EXPORT_SYMBOL_FOR_MODULES (seamldr_get_info,"tdx-host" ); +/* +* Intel TDX module blob. Its format is defined at: +* https://github.com/intel/tdx-module-binaries/blob/main/blob_structure.txt
Heh, so URLs are not OK in changelogs because they go stale, but they're fine in the code?
arch/x86/virt/vmx/tdx/seamldr.c int seamldr_get_info(struct seamldr_info *seamldr_info) · line 80
Quoted 3 lines
arch/x86/virt/vmx/tdx/seamldr.c +3
-0+* Note this structure differs from the reference above: the two variable-length +* fields "@sigstruct" and "@module" are represented as a single "@data" field +* here and split programmatically using the offset_of_module value.
This is good info. But, it's copied and pasted between the changelog and here. I'd choose one, honestly.
arch/x86/virt/vmx/tdx/seamldr.c int seamldr_get_info(struct seamldr_info *seamldr_info) · line 85
Quoted 4 lines
arch/x86/virt/vmx/tdx/seamldr.c +4
-0+* Note @offset_of_module is relative to the start of struct tdx_blob, not +* @data, and @length is the total length of the blob, not the length of +* @data. +*/
Out of line comments aren't great. Do these in the data structure if at all possible. Or, in the code. For instance:
arch/x86/virt/vmx/tdx/seamldr.c int seamldr_get_info(struct seamldr_info *seamldr_info) · line 95
Quoted 10 lines
arch/x86/virt/vmx/tdx/seamldr.c +10
-0+struct tdx_blob { +u16 version;// This ABI is always 0x100 +u16 checksum; +u32 offset_of_module;// from start of tdx_blob +u8 signature[8 ]; +u32 length; +u32 reserved0; +u64 reserved1[509 ]; +u8 data[];// contains sigstruct[] and module[] +} __packed;
That's probably _better_ than the two duplicated comments that are there now.
Also, why bother having two reserved arrays instead of:
u8 reserved[4076];
?
arch/x86/virt/vmx/tdx/seamldr.c int seamldr_get_info(struct seamldr_info *seamldr_info) · line 98
Quoted 2 lines
arch/x86/virt/vmx/tdx/seamldr.c +2
-0+/* Supported versions of the tdx_blob */ +#define TDX_BLOB_VERSION_1 0x100
The comment here doesn't help much.
arch/x86/virt/vmx/tdx/seamldr.c int seamldr_get_info(struct seamldr_info *seamldr_info) · line 104
Quoted 5 lines
arch/x86/virt/vmx/tdx/seamldr.c +5
-0+/* +* Blob fields are processed by the kernel and the payloads +* are passed to the TDX module. Do normal user input type +* check for any fields that don't get passed to the TDX module. +*/
I made it this far, but I rather despise the 'blob' terminology. It's just bad naming. We should really just call it 'tdx_update_image' or 'tdx_image' everywhere and stop saying 'blob'. Blob is one of those names that people throw at things when they give up on naming.
arch/x86/virt/vmx/tdx/seamldr.c int seamldr_get_info(struct seamldr_info *seamldr_info) · line 114
Quoted 10 lines
arch/x86/virt/vmx/tdx/seamldr.c +10
-0+static const struct tdx_blob *get_and_check_blob (const u8 *data,u32 size) +{ +const struct tdx_blob *blob = (const void *)data; + +/* +* Ensure the size is valid otherwise reading any field from the +* blob may overflow. +*/ +if (size <=sizeof (struct tdx_blob )) +return ERR_PTR (-EINVAL );
Couple of things here:
First, using sizeof() on a type with a variable-length array is a big warning sign. It needs commenting. It's especially subtle because this will go on and parse patently invalid 'data' images that don't even have room for sigstruct[] or module[].
This is *specifically* about the pre-data[] fields that are going to be read below.
arch/x86/virt/vmx/tdx/seamldr.c int seamldr_get_info(struct seamldr_info *seamldr_info) · line 121
Quoted 6 lines
arch/x86/virt/vmx/tdx/seamldr.c +6
-0+/* +* Don't care about user passing the wrong file, but protect +* kernel ABI by preventing accepting garbage. +*/ +if (memcmp (blob->signature,"TDX-BLOB" ,8 )) +return ERR_PTR (-EINVAL );
Is there really no helper in the kernel anywhere that can safely do the 8-byte compare against two known-to-the-compiler 8-byte-wide fields without hard-coding the 8?
arch/x86/virt/vmx/tdx/seamldr.c int seamldr_get_info(struct seamldr_info *seamldr_info) · line 128
Quoted 6 lines
arch/x86/virt/vmx/tdx/seamldr.c +6
-0+/* +* Ensure the offset of the module is within valid bounds and +* page-aligned. +*/ +if (blob->offset_of_module >= size || blob->offset_of_module <=sizeof (struct tdx_blob )) +return ERR_PTR (-EINVAL );
Again, the sizeof(struct tdx_blob) is wonky. Why does this disallow pointing blob->offset_of_module at reserved1[508] but not sigstruct[]?
arch/x86/virt/vmx/tdx/seamldr.c int seamldr_get_info(struct seamldr_info *seamldr_info) · line 124
Quoted 2 lines
arch/x86/virt/vmx/tdx/seamldr.c +2
-0+if (!IS_ALIGNED (blob->offset_of_module,PAGE_SIZE )) +return ERR_PTR (-EINVAL );
Wait a sec. Unless blob->offset_of_module==0, how could this check pass and "blob->offset_of_module <= sizeof(struct tdx_blob)" fail?
arch/x86/virt/vmx/tdx/seamldr.c int seamldr_get_info(struct seamldr_info *seamldr_info) · line 126
Quoted 2 lines
arch/x86/virt/vmx/tdx/seamldr.c +2
-0+if (blob->version !=TDX_BLOB_VERSION_1 ) +return ERR_PTR (-EINVAL );
This should be the first check, IMNHO. If this doesn't pass then the rest of the fields are invalid. No?
arch/x86/virt/vmx/tdx/seamldr.c int seamldr_get_info(struct seamldr_info *seamldr_info) · line 136
Quoted 2 lines
arch/x86/virt/vmx/tdx/seamldr.c +2
-0+if (blob->reserved0 ||memchr_inv (blob->reserved1,0 ,sizeof (blob->reserved1))) +return ERR_PTR (-EINVAL );
There should not be two reserved, must-be-0 fields. There should be 1. There must be 1.
Also I don't like the proposed data structure. It would make a lot more sense to me if it were:
struct tdx_image_header {
u16 version; // This ABI is always 0x100
u16 checksum;
u32 offset_of_module; // from start of the header
u8 signature[8];
u32 length;
u8 reserved[4076];
}struct p {
u8[PAGE_SIZE];
};struct tdx_image {
struct tdx_image_header h;
struct p pages[];
};Then you can do things like check if sizeof(struct tdx_image_header) == PAGE_SIZE. Or whether offset_of_module points past the header.
That stuff only makes sense if you separate out the header structure from the payloads which are the page-aligned sigstruct and module image itself.
But exposing the double-variable-length arrays seems really wonky to me.
arch/x86/virt/vmx/tdx/seamldr.c int seamldr_get_info(struct seamldr_info *seamldr_info) · line 142
Quoted 5 lines
arch/x86/virt/vmx/tdx/seamldr.c +5
-0+return blob; +} + +static struct seamldr_params *alloc_seamldr_params (const struct tdx_blob *blob,unsigned int blob_size) +{
This does far more than "alloc" something.
arch/x86/virt/vmx/tdx/seamldr.c int seamldr_get_info(struct seamldr_info *seamldr_info) · line 150
Quoted 8 lines
arch/x86/virt/vmx/tdx/seamldr.c +8
-0+struct seamldr_params *params; +int module_pg_cnt, sig_pg_cnt; +const u8 *sig, *module; +int i; + + params = (struct seamldr_params *)get_zeroed_page (GFP_KERNEL ); +if (!params) +return ERR_PTR (-ENOMEM );
kzmalloc(PAGE_SIZE, GFP_KERNEL) will save you a cast.
arch/x86/virt/vmx/tdx/seamldr.c int seamldr_get_info(struct seamldr_info *seamldr_info) · line 157
Quoted 6 lines
arch/x86/virt/vmx/tdx/seamldr.c +6
-0+/* +* Split the blob into a sigstruct and a module. Assume all +* size/offsets are within bounds of blob_size due to prior checks. +*/ + sig = blob->data; + sig_pg_cnt = (blob->offset_of_module -sizeof (struct tdx_blob )) >>PAGE_SHIFT ;
Of course, the size of the first thing is defined by the offset of the second thing.
This really should just be called ->end_of_sig.
arch/x86/virt/vmx/tdx/seamldr.c int seamldr_get_info(struct seamldr_info *seamldr_info) · line 159
Quoted 2 lines
arch/x86/virt/vmx/tdx/seamldr.c +2
-0+ module = (const u8 *)blob + blob->offset_of_module; + module_pg_cnt = (blob_size - blob->offset_of_module) >>PAGE_SHIFT ;
This looks halfway sane:
/* adjust for size of the header: */ sig_size = blob->end_of_sig - PAGE_SIZE; module_size = module_image_size - blob->end_of_sig;
Then, page-adjust it later. One bit of magic at a time, please.
arch/x86/virt/vmx/tdx/seamldr.c int seamldr_get_info(struct seamldr_info *seamldr_info) · line 165
Quoted 5 lines
arch/x86/virt/vmx/tdx/seamldr.c +5
-0+/* +* Only use version 1 when required (sigstruct > 4KB) for backward +* compatibility with P-SEAMLDR that lacks version 1 support. +*/ + params->version = sig_pg_cnt >1 ;
Ewwww.
But what do we do if we're on an old P-SEAMLDR but get a big sigstruct? It'll just fail?
How many old P-SEAMLDRs are there in the wild? Do we even care about this?
arch/x86/virt/vmx/tdx/seamldr.c int seamldr_get_info(struct seamldr_info *seamldr_info) · line 168
Quoted 3 lines
arch/x86/virt/vmx/tdx/seamldr.c +3
-0+ params->scenario =SEAMLDR_SCENARIO_UPDATE ; + +for (i =0 ; i <MIN (sig_pg_cnt,SEAMLDR_MAX_NR_SIG_PAGES ); i++) {
Same for the MIN(). Do all the calculations separate from the loop.
arch/x86/virt/vmx/tdx/seamldr.c int seamldr_get_info(struct seamldr_info *seamldr_info) · line 177
Quoted 9 lines
arch/x86/virt/vmx/tdx/seamldr.c +9
-0+ params->sigstruct_pa[i] =vmalloc_to_pfn (sig) <<PAGE_SHIFT ; + sig +=PAGE_SIZE ; + } + + params->num_module_pages =MIN (module_pg_cnt,SEAMLDR_MAX_NR_MODULE_PAGES ); +for (i =0 ; i < params->num_module_pages; i++) { + params->mod_pages_pa_list[i] =vmalloc_to_pfn (module) <<PAGE_SHIFT ; + module +=PAGE_SIZE ; + }
Really what you want here is a helper. Have it take the module or sigstruct pointer, a pointer to the pa_list[] and a maximum size.
Then call the helper twice.
Quoted 6 lines
arch/x86/virt/vmx/tdx/seamldr.c +6
-0+return params; +} + +static struct seamldr_params *init_seamldr_params (const u8 *data,u32 size) +{ +const struct tdx_blob *blob;
arch/x86/virt/vmx/tdx/seamldr.c int seamldr_get_info(struct seamldr_info *seamldr_info) · line 194
Quoted 10 lines
arch/x86/virt/vmx/tdx/seamldr.c +10
-0+ + blob =get_and_check_blob (data, size); +if (IS_ERR (blob)) +return ERR_CAST (blob); + +return alloc_seamldr_params (blob, size); +} + +DEFINE_FREE(free_seamldr_params, struct seamldr_params *, + if (!IS_ERR_OR_NULL(_T)) free_page((unsigned long)_T))
Is this really worth it?
Quoted 5 lines
arch/x86/virt/vmx/tdx/seamldr.c +0
-0/** * seamldr_install_module - Install a new TDX module. * @data: Pointer to the TDX module update blob. @@ -52,6 +202,11 @@ EXPORT_SYMBOL_FOR_MODULES(seamldr_get_info, "tdx-host");*/
arch/x86/virt/vmx/tdx/seamldr.c EXPORT_SYMBOL_FOR_MODULES(seamldr_get_info, "tdx-host"); · line 212
Quoted 10 lines
arch/x86/virt/vmx/tdx/seamldr.c +5
-0int seamldr_install_module (const u8 *data,u32 size) { + struct seamldr_params *params __free(free_seamldr_params) = +init_seamldr_params (data, size); +if (IS_ERR (params)) +return PTR_ERR (params); +/* TODO: Update TDX module here */ return 0 ; }
IMNHO, this patch has way too much going on. It took well over an hour to go through it. That's problematic.
Edgecombe, Rick P
maintainerRe: [PATCH v8 08/21] x86/virt/seamldr: Allocate and populate a module update request
Quoted 7 lines · Dave Hansen
Quoted 4 lines
+/*
+ * Intel TDX module blob. Its format is defined at:
+ *
https://github.com/intel/tdx-module-binaries/blob/main/blob_structure.txtHeh, so URLs are not OK in changelogs because they go stale, but they're fine in the code?
Hmm, we usually could refer to a document by name. But this is just a txt in a github repo... Maybe just:
Intel TDX module blob is a format for distributing TDX module updates. It is parsed by the host before internal bits are passed to the TDX module. For details, see the latest TDX module update repository.
To me the confusing part in all of this is that there is a format that the host has to parse and then pass different bits into the TDX module. Usually something would just take the format it defines. So I think that is probably good context to have around it.
Re: [PATCH v8 08/21] x86/virt/seamldr: Allocate and populate a module update request
Quoted 10 lines · Rick P
Quoted 6 lines · Dave Hansen
Quoted 4 lines
+/*
+ * Intel TDX module blob. Its format is defined at:
+ *
https://github.com/intel/tdx-module-binaries/blob/main/blob_structure.txtHeh, so URLs are not OK in changelogs because they go stale, but they're fine in the code?
Hmm, we usually could refer to a document by name. But this is just a txt in a github repo... Maybe just:
Quoted 8 lines
Intel TDX module blob is a format for distributing TDX module updates. It is parsed by the host before internal bits are passed to the TDX module. For details, see the latest TDX module update repository.
To me the confusing part in all of this is that there is a format that the host has to parse and then pass different bits into the TDX module. Usually something would just take the format it defines. So I think that is probably good context to have around it.
Just say:
/* Intel TDX module update ABI structure. aka. "TDX module blob" */
We don't need to tell folks how to use Google. Just give them the right keywords to dump in there.
Re: [PATCH v8 08/21] x86/virt/seamldr: Allocate and populate a module update request
In reply to Dave HansenQuoted 13 lines
Quoted 8 lines
header is consumed solely by the kernel to extract the sigstruct and module, so validate it before processing to protect the kernel ABI. The sigstruct and module are passed to and validated by P-SEAMLDR, so don't duplicate any validation in the kernel.
Note: the
sigstruct_pafield inSEAMLDR_PARAMShas been extended to a 4-element array. The updated "SEAM Loader (SEAMLDR) Interface Specification" will be published separately.These changelogs have all the right info, but I find them really hard to parse. For instance, if you're going to have a 'struct
seamldr_params', then just stick with that name. Don't use the "SEAMLDR_PARAMS" name too.
Quoted 8 lines
Start with the data structures:
There are two important ABIs here:
'struct
tdx_blob' - the on-disk and in-memory format for a TDX
module update image.
'structseamldr_params' - The in-memory ABI passed to the TDX module
loader. Points to a single 'structtdx_blob'
Thanks for the thorough review.
Your comments all make sense to me. I just want to confirm two points below.
Quoted 10 lines
arch/x86/virt/vmx/tdx/seamldr.c int seamldr_get_info(struct seamldr_info *seamldr_info) · line 121
Quoted 6 lines
arch/x86/virt/vmx/tdx/seamldr.c +6
-0+/* +* Don't care about user passing the wrong file, but protect +* kernel ABI by preventing accepting garbage. +*/ +if (memcmp (blob->signature,"TDX-BLOB" ,8 )) +return ERR_PTR (-EINVAL );Is there really no helper in the kernel anywhere that can safely do the 8-byte compare against two known-to-the-compiler 8-byte-wide fields without hard-coding the 8?
I couldn't find a helper that automatically derives the comparison length from the operands. 'strcmp()' is not suitable here because 'blob->signature' is not NUL-terminated.
Do you mean just avoiding the hard-coded 8, e.g.
if (memcmp(blob->signature, "TDX-BLOB", sizeof(blob->signature))) return ERR_PTR(-EINVAL);
or define the 'u8 signature[8]' as a u64 and compare it with a constant, like
/* Little-endian encoding of "TDX-BLOB" string */
#define TDX_IMAGE_SIGNATURE 0x424f4c422d584454ULL
if (blob->signature != TDX_IMAGE_SIGNATURE) return ERR_PTR(-EINVAL);
Quoted 10 lines
arch/x86/virt/vmx/tdx/seamldr.c int seamldr_get_info(struct seamldr_info *seamldr_info) · line 150
Quoted 8 lines
arch/x86/virt/vmx/tdx/seamldr.c +8
-0+struct seamldr_params *params; +int module_pg_cnt, sig_pg_cnt; +const u8 *sig, *module; +int i; + + params = (struct seamldr_params *)get_zeroed_page (GFP_KERNEL ); +if (!params) +return ERR_PTR (-ENOMEM );
kzmalloc(PAGE_SIZE, GFP_KERNEL)will save you a cast.
I noticed that 'kzalloc_obj()' can be used here, which avoids spelling out the size and GFP flags explicitly. So I ended up with:
params = kzalloc_obj(*params);
If you would prefer 'kzalloc(PAGE_SIZE, GFP_KERNEL)', I can switch to that.
Re: [PATCH v8 08/21] x86/virt/seamldr: Allocate and populate a module update request
On 5/7/26 06:19, Chao Gao wrote:
...
Quoted 20 lines
Quoted 10 lines
arch/x86/virt/vmx/tdx/seamldr.c int seamldr_get_info(struct seamldr_info *seamldr_info) · line 121
Quoted 6 lines
arch/x86/virt/vmx/tdx/seamldr.c +6
-0+/* +* Don't care about user passing the wrong file, but protect +* kernel ABI by preventing accepting garbage. +*/ +if (memcmp (blob->signature,"TDX-BLOB" ,8 )) +return ERR_PTR (-EINVAL );Is there really no helper in the kernel anywhere that can safely do the 8-byte compare against two known-to-the-compiler 8-byte-wide fields without hard-coding the 8?
I couldn't find a helper that automatically derives the comparison length from the operands. '
strcmp()' is not suitable here because 'blob->signature' is not NUL-terminated.Do you mean just avoiding the hard-coded 8, e.g.
if (memcmp(blob->signature, "TDX-BLOB", sizeof(blob->signature))) return ERR_PTR(-EINVAL);
Quoted 7 lines
or define the 'u8 signature[8]' as a u64 and compare it with a constant, like
/* Little-endian encoding of "TDX-BLOB" string */
#defineTDX_IMAGE_SIGNATURE0x424f4c422d584454ULLif (blob->signature != TDX_IMAGE_SIGNATURE) return ERR_PTR(-EINVAL);
Either one of those is fine with me. I'd probably do the sizeof() variant, but no strong preference.
Quoted 11 lines
Quoted 10 lines
arch/x86/virt/vmx/tdx/seamldr.c int seamldr_get_info(struct seamldr_info *seamldr_info) · line 150
Quoted 8 lines
arch/x86/virt/vmx/tdx/seamldr.c +8
-0+struct seamldr_params *params; +int module_pg_cnt, sig_pg_cnt; +const u8 *sig, *module; +int i; + + params = (struct seamldr_params *)get_zeroed_page (GFP_KERNEL ); +if (!params) +return ERR_PTR (-ENOMEM );
kzmalloc(PAGE_SIZE, GFP_KERNEL)will save you a cast.
Quoted 4 lines
I noticed that '
kzalloc_obj()' can be used here, which avoids spelling out the size and GFP flags explicitly. So I ended up with:params = kzalloc_obj(*params);
That's fine too.
PATCH v8 9/21 x86/virt/seamldr: Introduce skeleton for TDX module updates
In reply to Chao GaoTDX module updates require careful synchronization with other TDX operations. The requirements are (#1/#2 reflect current behavior that must be preserved):
1. SEAMCALLs need to be callable from both process and IRQ contexts.
2. SEAMCALLs need to be able to run concurrently across CPUs
3. During updates, only update-related SEAMCALLs are permitted; all other SEAMCALLs shouldn't be called.
4. During updates, all online CPUs must participate in the update work.
No single lock primitive satisfies all requirements. For instance, rwlock_t handles #1/#2 but fails #4: CPUs spinning with IRQs disabled cannot be directed to perform update work.
Use stop_machine() as it is the only well-understood mechanism that can meet all requirements.
And TDX module updates consist of several steps (See Intel® Trust Domain Extensions (Intel® TDX) Module Base Architecture Specification, Chapter "TD-Preserving TDX module Update"). Ordering requirements between steps mandate lockstep synchronization across all CPUs.
multi_cpu_stop() is a good example of performing a multi-step task in lockstep. But it doesn't synchronize steps within the callback function it takes. So, implement one based on its pattern to establish the skeleton for TDX module updates. Specifically, add a global state machine where each state represents a step in the update flow. The state advances only after all CPUs acknowledge completing their work in the current state. This acknowledgment mechanism is what ensures lockstep execution.
Potential alternative to stop_machine()
=======================================
An alternative approach is to lock all KVM entry points and kick all vCPUs. Here, KVM entry points refer to KVM VM/vCPU ioctl entry points, implemented in KVM common code (virt/kvm). Adding a locking mechanism there would affect all architectures KVM supports. And to lock only TDX vCPUs, new logic would be needed to identify TDX vCPUs, which the KVM common code currently lacks. This would add significant complexity and maintenance overhead to KVM for this TDX-specific use case, so don't take this approach.
---
v8:
- Add a "so don't take this approach" after alternative solution discussion in the changelog [Rick]
- Use imperative mood for a comment [Dave]
--- arch/x86/virt/vmx/tdx/seamldr.c | 79 ++++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 2 deletions(-)
arch/x86/virt/vmx/tdx/seamldr.c +77 -2
@@ -7,8 +7,10 @@#define pr_fmt (fmt) "seamldr: " fmt#include <linux/mm.h> +#include <linux/nmi.h> #include <linux/slab.h> #include <linux/spinlock.h> +#include <linux/stop_machine.h> #include <asm/seamldr.h>
@@ -190,6 +192,77 @@ static struct seamldr_params *init_seamldr_params(const u8 *data, u32 size)return alloc_seamldr_params (blob, size); } +/* +* During a TDX module update, all CPUs start from MODULE_UPDATE_START and +* progress to MODULE_UPDATE_DONE. Each state is associated with certain +* work. For some states, just one CPU needs to perform the work, while +* other CPUs just wait during those states. +*/ +enum module_update_state { +MODULE_UPDATE_START , +MODULE_UPDATE_DONE , +}; + +static struct { +enum module_update_state state; +int thread_ack; +/* +* Protect update_data. Raw spinlock as it will be acquired from +* interrupt-disabled contexts. +*/ +raw_spinlock_t lock; +} update_data = { + .lock =__RAW_SPIN_LOCK_UNLOCKED (update_data.lock) +}; + +static void set_target_state (enum module_update_state state) +{ +/* Reset ack counter. */ + update_data.thread_ack =num_online_cpus (); + update_data.state = state; +} + +/* Last one to ack a state moves to the next state. */ +static void ack_state (void ) +{ +guard (raw_spinlock)(&update_data.lock); + update_data.thread_ack--; +if (!update_data.thread_ack) +set_target_state (update_data.state +1 ); +} + +/* +* See multi_cpu_stop() from where this multi-cpu state-machine was +* adopted, and the rationale for touch_nmi_watchdog(). +*/ +static int do_seamldr_install_module (void *seamldr_params) +{ +enum module_update_state newstate, curstate =MODULE_UPDATE_START ; +int ret =0 ; + +do { +/* Chill out and re-read update_data. */ +cpu_relax (); + newstate =READ_ONCE (update_data.state); + +if (newstate != curstate) { + curstate = newstate; +switch (curstate) { +/* TODO: add the update steps. */ +default : +break ; + } + +ack_state (); + }else { +touch_nmi_watchdog (); +rcu_momentary_eqs (); + } + }while (curstate !=MODULE_UPDATE_DONE ); + +return ret; +} + DEFINE_FREE(free_seamldr_params, struct seamldr_params *, if (!IS_ERR_OR_NULL(_T)) free_page((unsigned long)_T))
@@ -207,7 +280,9 @@ int seamldr_install_module(const u8 *data, u32 size)if (IS_ERR (params))return PTR_ERR (params); -/* TODO: Update TDX module here */ -return 0 ; +/* Ensure a stable set of online CPUs for the update process. */ +guard (cpus_read_lock)(); +set_target_state (MODULE_UPDATE_START +1 ); +return stop_machine_cpuslocked (do_seamldr_install_module, params, cpu_online_mask); }EXPORT_SYMBOL_FOR_MODULES (seamldr_install_module,"tdx-host" );
Signature · 1 line
2.47.1
Re: [PATCH v8 09/21] x86/virt/seamldr: Introduce skeleton for TDX module updates
arch/x86/virt/vmx/tdx/seamldr.c static struct seamldr_params *init_seamldr_params(const u8 *data, u32 size) · line 208
Quoted 3 lines · Chao Gao
arch/x86/virt/vmx/tdx/seamldr.c +3
-0+static struct { + enum module_update_state state; + int thread_ack;
multi_stop_data has an atomic_t for this.
You have an int.
Which one is right?
Re: [PATCH v8 09/21] x86/virt/seamldr: Introduce skeleton for TDX module updates
Quoted 10 lines · Dave Hansen
arch/x86/virt/vmx/tdx/seamldr.c static struct seamldr_params *init_seamldr_params(const u8 *data, u32 size) · line 208
Quoted 3 lines · Chao Gao
arch/x86/virt/vmx/tdx/seamldr.c +3
-0+static struct { + enum module_update_state state; + int thread_ack;
multi_stop_datahas anatomic_tfor this.You have an int.
Which one is right?
You pointed out that using atomic_t and memory barriers for synchronization was overly complicated. So, I switched to use a spinlock, and thread_ack can now be a plain int.
Re: [PATCH v8 09/21] x86/virt/seamldr: Introduce skeleton for TDX module updates
Quoted 8 lines · Chao Gao
Quoted 7 lines · Dave Hansen
arch/x86/virt/vmx/tdx/seamldr.c static struct seamldr_params *init_seamldr_params(const u8 *data, u32 size) · line 208
Quoted 3 lines · Chao Gao
arch/x86/virt/vmx/tdx/seamldr.c +3
-0+static struct { + enum module_update_state state; + int thread_ack;
multi_stop_datahas anatomic_tfor this.
Quoted 7 lines
Quoted 3 lines
You have an int.
Which one is right?
You pointed out that using
atomic_tand memory barriers for synchronization was overly complicated. So, I switched to use a spinlock, andthread_ackcan now be a plain int.
Good point.
Could you make this a bit more obvious, please?
I honestly don't like guard(). Maybe I'm old-fashioned, but I really, really like critical sections to be, well, explicit *sections* of code.
Second, you have two functions defined next to each other with similar names:
static void ack_state(void)
static void set_target_state(enum module_update_state state)
Both of which manipulate the same data. One takes the lock. One doesn't. That could be fixed with comments.
PATCH v8 10/21 x86/virt/seamldr: Shut down the current TDX module
In reply to Chao GaoThe first step of TDX module updates is shutting down the current TDX Module. This step also packs state information that needs to be preserved across updates as handoff data, which will be consumed by the updated module. The handoff data is stored internally in the SEAM range and is hidden from the kernel.
To ensure a successful update, the new module must be able to consume the handoff data generated by the old module. Since handoff data layout may change between modules, the handoff data is versioned. Each module has a native handoff version and provides backward support for several older versions.
The complete handoff versioning protocol is complex as it supports both module upgrades and downgrades. See details in Intel® Trust Domain Extensions (Intel® TDX) Module Base Architecture Specification, Chapter "Handoff Versioning".
Ideally, the kernel needs to retrieve the handoff versions supported by the current module and the new module and select a version supported by both. But, since this implementation chooses to only support module upgrades, simply request the current module to generate handoff data using its highest supported version, expecting that the new module will likely support it.
Retrieve the module's handoff version from TDX global metadata and add an update step to shut down the module. Module shutdown has global effect, so it only needs to run on one CPU.
Note that the handoff information isn't cached in tdx_sysinfo. It is used only for module shutdown, and is present only when the TDX module supports updates. Caching it in get_tdx_sys_info() would require extra update-support guards and refreshing the cached value across module updates.
---
v8:
- Enhance the changelog to also talk about what the patch does instead of just "why". [Rick]
- For simplicity, don't cache handoff version in tdx_sysinfo
--- arch/x86/include/asm/tdx_global_metadata.h | 4 ++++ arch/x86/virt/vmx/tdx/seamldr.c | 11 ++++++++++- arch/x86/virt/vmx/tdx/tdx.c | 19 ++++++++++++++++++- arch/x86/virt/vmx/tdx/tdx.h | 3 +++ arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 13 +++++++++++++ 5 files changed, 48 insertions(+), 2 deletions(-)
arch/x86/include/asm/tdx_global_metadata.h +4 -0
@@ -40,6 +40,10 @@ struct tdx_sys_info_td_conf {u64 cpuid_config_values[128 ][2 ]; }; +struct tdx_sys_info_handoff { +u16 module_hv; +}; +struct tdx_sys_info {struct tdx_sys_info_version version;struct tdx_sys_info_features features;
arch/x86/virt/vmx/tdx/seamldr.c +10 -1
@@ -15,6 +15,7 @@#include <asm/seamldr.h> #include "seamcall_internal.h" +#include "tdx.h" /* P-SEAMLDR SEAMCALL leaf function */ #define P_SEAMLDR_INFO 0x8000000000000000
@@ -200,6 +201,7 @@ static struct seamldr_params *init_seamldr_params(const u8 *data, u32 size)*/ enum module_update_state {MODULE_UPDATE_START , +MODULE_UPDATE_SHUTDOWN ,MODULE_UPDATE_DONE , };
@@ -238,8 +240,12 @@ static void ack_state(void)static int do_seamldr_install_module (void *seamldr_params) {enum module_update_state newstate, curstate =MODULE_UPDATE_START ; +int cpu =smp_processor_id (); +bool primary;int ret =0 ; + primary =cpumask_first (cpu_online_mask) == cpu; + do { /* Chill out and re-read update_data. */ cpu_relax();
@@ -248,7 +254,10 @@ static int do_seamldr_install_module(void *seamldr_params)if (newstate != curstate) { curstate = newstate;switch (curstate) { -/* TODO: add the update steps. */ +case MODULE_UPDATE_SHUTDOWN : +if (primary) + ret =tdx_module_shutdown (); +break ;default :break ; }
arch/x86/virt/vmx/tdx/tdx.c +18 -1
@@ -287,7 +287,7 @@ static __init int build_tdx_memlist(struct list_head *tmb_list)return ret; } -static __init int read_sys_metadata_field (u64 field_id,u64 *data) +static int read_sys_metadata_field (u64 field_id,u64 *data) {struct tdx_module_args args = {};int ret;
@@ -1233,6 +1233,23 @@ static __init int tdx_enable(void) }subsys_initcall (tdx_enable); +int tdx_module_shutdown (void ) +{ +struct tdx_sys_info_handoff handoff = {}; +struct tdx_module_args args = {}; +int ret; + + ret =get_tdx_sys_info_handoff (&handoff); +WARN_ON_ONCE (ret); + +/* +* Use the module's handoff version as it is the highest the +* module can produce and most likely supported by newer modules. +*/ + args.rcx = handoff.module_hv; +return seamcall_prerr (TDH_SYS_SHUTDOWN , &args); +} +static bool is_pamt_page (unsigned long phys) {struct tdmr_info_list *tdmr_list = &tdx_tdmr_list;
arch/x86/virt/vmx/tdx/tdx.h +3 -0
@@ -46,6 +46,7 @@#define TDH_PHYMEM_PAGE_WBINVD 41#define TDH_VP_WR 43#define TDH_SYS_CONFIG 45 +#define TDH_SYS_SHUTDOWN 52/* * SEAMCALL leaf:
@@ -110,4 +111,6 @@ struct tdmr_info_list { int max_tdmrs; /* How many 'tdmr_info's are allocated */ }; +int tdx_module_shutdown (void ); +#endif
arch/x86/virt/vmx/tdx/tdx_global_metadata.c +13 -0
@@ -100,6 +100,19 @@ static __init int get_tdx_sys_info_td_conf(struct tdx_sys_info_td_conf *sysinfo_return ret; } +static int get_tdx_sys_info_handoff (struct tdx_sys_info_handoff *sysinfo_handoff) +{ +int ret; +u64 val; + + ret =read_sys_metadata_field (0x8900000100000000 , &val); +if (ret) +return ret; + + sysinfo_handoff->module_hv = val; +return 0 ; +} +static __init int get_tdx_sys_info (struct tdx_sys_info *sysinfo) {int ret =0 ;
Signature · 1 line
2.47.1
Re: [PATCH v8 10/21] x86/virt/seamldr: Shut down the current TDX module
arch/x86/virt/vmx/tdx/seamldr.c static void ack_state(void) · line 247
Quoted 8 lines · Chao Gao
static int
do_seamldr_install_module(void *seamldr_params){arch/x86/virt/vmx/tdx/seamldr.c +3
-0enum module_update_state newstate, curstate =MODULE_UPDATE_START ; +int cpu =smp_processor_id (); +bool primary;int ret =0 ; + primary =cpumask_first (cpu_online_mask) == cpu;
Isn't cpumask_first(cpu_online_mask)==0, always? I thought CPU 0 could never be offlined.
Re: [PATCH v8 10/21] x86/virt/seamldr: Shut down the current TDX module
Quoted 8 lines · Dave Hansen
Quoted 7 lines · Chao Gao
static int
do_seamldr_install_module(void *seamldr_params){arch/x86/virt/vmx/tdx/seamldr.c +2
-0enum module_update_state newstate, curstate =MODULE_UPDATE_START ; +int cpu =smp_processor_id (); +bool primary;int ret =0 ;
Quoted 4 lines
arch/x86/virt/vmx/tdx/seamldr.c static void ack_state(void) · line 247
Quoted 1 line
arch/x86/virt/vmx/tdx/seamldr.c +1
-0+ primary =cpumask_first (cpu_online_mask) == cpu;Isn't cpumask_first(cpu_online_mask)==0, always? I thought CPU 0 could never be offlined.
Not always. On x86, CPU 0 can be offlined at runtime if the kernel is booted with the cpu0_hotplug command-line option. See cpu_hotplug.rst.
Re: [PATCH v8 10/21] x86/virt/seamldr: Shut down the current TDX module
Quoted 9 lines · Chao Gao
Quoted 8 lines · Dave Hansen
Quoted 7 lines · Chao Gao
static int
do_seamldr_install_module(void *seamldr_params){arch/x86/virt/vmx/tdx/seamldr.c +2
-0enum module_update_state newstate, curstate =MODULE_UPDATE_START ; +int cpu =smp_processor_id (); +bool primary;int ret =0 ;
Quoted 5 lines
Quoted 3 lines
arch/x86/virt/vmx/tdx/seamldr.c static void ack_state(void) · line 247
Quoted 1 line
arch/x86/virt/vmx/tdx/seamldr.c +1
-0+ primary =cpumask_first (cpu_online_mask) == cpu;Isn't cpumask_first(cpu_online_mask)==0, always? I thought CPU 0 could never be offlined.
Not always. On x86, CPU 0 can be offlined at runtime if the kernel is booted with the
cpu0_hotplugcommand-line option. Seecpu_hotplug.rst.
See e59e74dc48a309cb848ffc3d76a0d61aa6803c05.
Yes, the docs are stale.
PATCH v8 11/21 x86/virt/tdx: Reset software states during TDX module shutdown
In reply to Chao GaoThe TDX module requires a one-time global initialization (TDH.SYS.INIT) and per-CPU initialization (TDH.SYS.LP.INIT) before use. These initializations are guarded by software flags to prevent repetition.
After TDX module updates, the new TDX module requires the same global and per-CPU initializations, but the existing software flags prevent re-initialization.
Reset all software flags guarding the initialization flows to allow the global and per-CPU initializations to be triggered again after updates.
As tdx_module_initialized is changed at runtime, drop its "__ro_after_init" tag.
---
v8:
- drop "__ro_after_init" from tdx_module_initialized.
--- arch/x86/virt/vmx/tdx/tdx.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-)
arch/x86/virt/vmx/tdx/tdx.c +21 -5
@@ -55,11 +55,14 @@ static DEFINE_PER_CPU(bool, tdx_lp_initialized);static struct tdmr_info_list tdx_tdmr_list; +static bool sysinit_done; +static int sysinit_ret; +/* All TDX-usable memory regions. Protected by mem_hotplug_lock. */ static LIST_HEAD (tdx_memlist);static struct tdx_sys_info tdx_sysinfo__ro_after_init ; -static bool tdx_module_initialized__ro_after_init ; +static bool tdx_module_initialized;/* * Do the module global initialization once and return its result.
@@ -69,8 +72,6 @@ static int try_init_module_global(void) {struct tdx_module_args args = {};static DEFINE_RAW_SPINLOCK (sysinit_lock); -static bool sysinit_done; -static int sysinit_ret;raw_spin_lock (&sysinit_lock);
@@ -1237,7 +1238,7 @@ int tdx_module_shutdown(void) {struct tdx_sys_info_handoff handoff = {};struct tdx_module_args args = {}; -int ret; +int ret, cpu; ret =get_tdx_sys_info_handoff (&handoff);WARN_ON_ONCE (ret);
@@ -1247,7 +1248,22 @@ int tdx_module_shutdown(void)* module can produce and most likely supported by newer modules. */ args.rcx = handoff.module_hv; -return seamcall_prerr (TDH_SYS_SHUTDOWN , &args); + ret =seamcall_prerr (TDH_SYS_SHUTDOWN , &args); +if (ret) +return ret; + +/* +* Clear global and per-CPU initialization flags so the new module +* can be fully re-initialized after a successful update. +* +* No locks needed as no concurrent accesses can occur here. +*/ + tdx_module_initialized = false; + sysinit_done = false; + sysinit_ret =0 ; +for_each_possible_cpu (cpu) +per_cpu (tdx_lp_initialized, cpu) = false; +return 0 ; }static bool is_pamt_page (unsigned long phys)
Signature · 1 line
2.47.1
Re: [PATCH v8 11/21] x86/virt/tdx: Reset software states during TDX module shutdown
arch/x86/virt/vmx/tdx/tdx.c int tdx_module_shutdown(void) · line 1265
Quoted 11 lines · Chao Gao
arch/x86/virt/vmx/tdx/tdx.c +11
-0+/* +* Clear global and per-CPU initialization flags so the new module +* can be fully re-initialized after a successful update. +* +* No locks needed as no concurrent accesses can occur here. +*/ + tdx_module_initialized = false; + sysinit_done = false; + sysinit_ret =0 ; +for_each_possible_cpu (cpu) +per_cpu (tdx_lp_initialized, cpu) = false;
This speaks to needing refactoring.
If there's global TDX state, it needs to be in a global TDX state structure, not scattered across random global variables.
Imagine the structure is:
struct tdx_module_config foo;
That's 0's at boot. You have to init the TDX module to bring it out of 0's to valid state. It actually means something if you do:
memset(&foo, 0, sizeof(foo));
Because it takes it right back to its bss state. That ^ is also handy because it naturally just works if new state gets added.
Guess what will happen the next time someone adds:
int sysinit_new_fancy_thing;
Someone will forget to add it to this code. Then the module gets updated and breaks things in fun ways.
Re: [PATCH v8 11/21] x86/virt/tdx: Reset software states during TDX module shutdown
Quoted 30 lines · Dave Hansen
Review
Quoted 11 lines · Chao Gao
(quoted diff) +11
-0+ /* + * Clear global and per-CPU initialization flags so the new module + * can be fully re-initialized after a successful update. + * + * No locks needed as no concurrent accesses can occur here. + */ + tdx_module_initialized = false; + sysinit_done = false; + sysinit_ret = 0; + for_each_possible_cpu(cpu) + per_cpu(tdx_lp_initialized, cpu) = false;This speaks to needing refactoring.
If there's global TDX state, it needs to be in a global TDX state structure, not scattered across random global variables.
Imagine the structure is:
struct
tdx_module_configfoo;That's 0's at boot. You have to init the TDX module to bring it out of 0's to valid state. It actually means something if you do:
memset(&foo, 0, sizeof(foo));Because it takes it right back to its bss state. That ^ is also handy because it naturally just works if new state gets added.
Quoted 6 lines
Guess what will happen the next time someone adds:
int sysinit_new_fancy_thing;Someone will forget to add it to this code. Then the module gets updated and breaks things in fun ways.
Makes sense. I will slot in a patch like this:
From 1578bd211c732f7773475819cbf145fe9fedfcb5 Mon Sep 17 00:00:00 2001
From: Chao Gao <[email protected]>
Date: Tue, 5 May 2026 22:46:39 -0700
Subject: [PATCH] x86/virt/tdx: Consolidate TDX global initialization state
The kernel uses several global flags to guard one-time TDX initialization flows and prevent them from being repeated.
When the TDX module is updated, all of this state must be reset so that the module can be initialized again. Today those flags are kept as separate global variables, which makes the reset path awkward and easy to miss when new state is added.
Group the flags into a single structure so they can be reset together, for example with memset(), and so future state that needs the same handling is easier to manage.
Drop the __ro_after_init annotation from tdx_module_initialized to make it consistent with the other two flags.
--- arch/x86/virt/vmx/tdx/tdx.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-)
arch/x86/virt/vmx/tdx/tdx.c +7 -1
@@ -44,6 +44,13 @@#include <asm/virt.h> #include "tdx.h" +struct tdx_module_state { +bool initialized; +bool sysinit_done; +int sysinit_ret; +}; + +static struct tdx_module_state tdx_module_state;static u32 tdx_global_keyid__ro_after_init ;static u32 tdx_guest_keyid_start__ro_after_init ;static u32 tdx_nr_guest_keyids__ro_after_init ;
@@ -58,7 +65,6 @@ static struct tdmr_info_list tdx_tdmr_list;static LIST_HEAD (tdx_memlist);static struct tdx_sys_info tdx_sysinfo__ro_after_init ; -static bool tdx_module_initialized__ro_after_init ;typedef void (*sc_err_func_t )(u64 fn,u64 err,struct tdx_module_args *args);
@@ -113,30 +119,28 @@ static int try_init_module_global(void) {
struct tdx_module_args args = {};
static DEFINE_RAW_SPINLOCK(sysinit_lock);- static bool sysinit_done;
- static int sysinit_ret;
raw_spin_lock(&sysinit_lock);
- if (sysinit_done)
+ if (tdx_module_state.sysinit_done)
goto out; /* RCX is module attributes and all bits are reserved */ args.rcx = 0;
- sysinit_ret = seamcall_prerr(TDH_SYS_INIT, &args);
+ tdx_module_state.sysinit_ret = seamcall_prerr(TDH_SYS_INIT, &args);
/*
* The first SEAMCALL also detects the TDX module, thus
* it can fail due to the TDX module is not loaded.
* Dump message to let the user know.
*/
- if (sysinit_ret == -ENODEV)
+ if (tdx_module_state.sysinit_ret == -ENODEV)
pr_err("module not loaded\n");- sysinit_done = true;
+ tdx_module_state.sysinit_done = true;
out:
raw_spin_unlock(&sysinit_lock);
- return sysinit_ret;
+ return tdx_module_state.sysinit_ret;
}
/**
@@ -1299,7 +1303,7 @@ static __init int tdx_enable(void)
register_syscore(&tdx_syscore);
- tdx_module_initialized = true;
+ tdx_module_state.initialized = true;
pr_info("TDX-Module initialized\n");
return 0; }
@@ -1554,7 +1558,7 @@ void __init tdx_init(void)
const struct tdx_sys_info *tdx_get_sysinfo(void)
{
- if (!tdx_module_initialized)
+ if (!tdx_module_state.initialized)
return NULL; return (const struct tdx_sys_info *)&tdx_sysinfo;
Signature · 1 line
2.52.0
PATCH v8 12/21 x86/virt/seamldr: Install a new TDX module
In reply to Chao GaoFollowing the shutdown of the existing TDX module, the update process continues with installing the new module. P-SEAMLDR provides the SEAMLDR.INSTALL SEAMCALL to perform this installation, which must be executed on all CPUs.
Implement SEAMLDR.INSTALL and execute it on every CPU.
---
v8:
- Standardize tdx_module_args initialization. For consistency, initialize each field on separate lines after declaration, instead of initializing them in the declaration.
--- arch/x86/virt/vmx/tdx/seamldr.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
arch/x86/virt/vmx/tdx/seamldr.c +13 -0
@@ -19,6 +19,7 @@/* P-SEAMLDR SEAMCALL leaf function */ #define P_SEAMLDR_INFO 0x8000000000000000 +#define P_SEAMLDR_INSTALL 0x8000000000000001#define SEAMLDR_MAX_NR_MODULE_PAGES 496#define SEAMLDR_MAX_NR_SIG_PAGES 4
@@ -74,6 +75,14 @@ int seamldr_get_info(struct seamldr_info *seamldr_info) }EXPORT_SYMBOL_FOR_MODULES (seamldr_get_info,"tdx-host" ); +static int seamldr_install (const struct seamldr_params *params) +{ +struct tdx_module_args args = {}; + + args.rcx =__pa (params); +return seamldr_call (P_SEAMLDR_INSTALL , &args); +} +/* * Intel TDX module blob. Its format is defined at: * https://github.com/intel/tdx-module-binaries/blob/main/blob_structure.txt
@@ -202,6 +211,7 @@ static struct seamldr_params *init_seamldr_params(const u8 *data, u32 size)enum module_update_state {MODULE_UPDATE_START ,MODULE_UPDATE_SHUTDOWN , +MODULE_UPDATE_CPU_INSTALL ,MODULE_UPDATE_DONE , };
@@ -258,6 +268,9 @@ static int do_seamldr_install_module(void *seamldr_params)if (primary) ret =tdx_module_shutdown ();break ; +case MODULE_UPDATE_CPU_INSTALL : + ret =seamldr_install (seamldr_params); +break ;default :break ; }
Signature · 1 line
2.47.1
Re: [PATCH v8 12/21] x86/virt/seamldr: Install a new TDX module
arch/x86/virt/vmx/tdx/seamldr.c static int do_seamldr_install_module(void *seamldr_params) · line 273
Quoted 3 lines · Chao Gao
arch/x86/virt/vmx/tdx/seamldr.c +3
-0+case MODULE_UPDATE_CPU_INSTALL : + ret =seamldr_install (seamldr_params); +break ;
I really despise this naming. Could you please clarify with comments?
This reads like it is installing a seamldr, not telling a seamldr to perform an install.
Edgecombe, Rick P
maintainerRe: [PATCH v8 12/21] x86/virt/seamldr: Install a new TDX module
Quoted 9 lines · Dave Hansen
arch/x86/virt/vmx/tdx/seamldr.c static int do_seamldr_install_module(void *seamldr_params) · line 273
Quoted 3 lines · Chao Gao
arch/x86/virt/vmx/tdx/seamldr.c +3
-0+case MODULE_UPDATE_CPU_INSTALL : + ret =seamldr_install (seamldr_params); +break ;I really despise this naming. Could you please clarify with comments?
This reads like it is installing a seamldr, not telling a seamldr to perform an install.
Yea it does read that way. We kind of have a convention around the seamcall wrappers matching the tdx docs name. tdh_foo_bar() is pretty clear. Unfortunately the seamldr calls are defined like SEAMLDR.INSTALL.
We don't need to make matching the wrappers name match the tdx docs be a rule. I just considered trying to make a seamldr name schema to make it clear that these are seamldr call names. But it is probably better to just special case this one with a clearer name we give it, like seamldr_tdx_module_install(). Which is slightly long, but both clear on the purpose and that it is in the family of seamldr_() functions.
Re: [PATCH v8 12/21] x86/virt/seamldr: Install a new TDX module
Quoted 7 lines · Rick P
Quoted 6 lines · Dave Hansen
arch/x86/virt/vmx/tdx/seamldr.c static int do_seamldr_install_module(void *seamldr_params) · line 273
Quoted 3 lines · Chao Gao
arch/x86/virt/vmx/tdx/seamldr.c +3
-0+case MODULE_UPDATE_CPU_INSTALL : + ret =seamldr_install (seamldr_params); +break ;I really despise this naming. Could you please clarify with comments?
Quoted 5 lines
Quoted 2 lines
This reads like it is installing a seamldr, not telling a seamldr to perform an install.
Yea it does read that way. We kind of have a convention around the seamcall wrappers matching the tdx docs name.
tdh_foo_bar()is pretty clear. Unfortunately the seamldr calls are defined like SEAMLDR.INSTALL.
The naming convention is fine, really. It does make thing easier to look up in the documentation.
In this case, just comment it, please. It's only a single site.
PATCH v8 13/21 x86/virt/seamldr: Do TDX per-CPU initialization after module installation
In reply to Chao GaoAfter installing the new TDX module, each CPU needs to be initialized again to make the CPU ready to run any other SEAMCALLs. So, export and call tdx_cpu_enable() on all CPUs.
---
v8:
- export tdx_cpu_enable(). it is unexported by VMXON series.
--- arch/x86/include/asm/tdx.h | 1 + arch/x86/virt/vmx/tdx/seamldr.c | 4 ++++ arch/x86/virt/vmx/tdx/tdx.c | 2 +- 3 files changed, 6 insertions(+), 1 deletion(-)
arch/x86/include/asm/tdx.h +1 -0
@@ -104,6 +104,7 @@ static inline long tdx_kvm_hypercall(unsigned int nr, unsigned long p1,#ifdef CONFIG_INTEL_TDX_HOST void tdx_init (void ); +int tdx_cpu_enable (void );const char *tdx_dump_mce_info (struct mce *m);const struct tdx_sys_info *tdx_get_sysinfo (void );
arch/x86/virt/vmx/tdx/seamldr.c +4 -0
@@ -212,6 +212,7 @@ enum module_update_state {MODULE_UPDATE_START ,MODULE_UPDATE_SHUTDOWN ,MODULE_UPDATE_CPU_INSTALL , +MODULE_UPDATE_CPU_INIT , MODULE_UPDATE_DONE, };
@@ -271,6 +272,9 @@ static int do_seamldr_install_module(void *seamldr_params)case MODULE_UPDATE_CPU_INSTALL : ret =seamldr_install (seamldr_params);break ; +case MODULE_UPDATE_CPU_INIT : + ret =tdx_cpu_enable (); +break ;default :break ; }
arch/x86/virt/vmx/tdx/tdx.c +1 -1
@@ -101,7 +101,7 @@ static int try_init_module_global(void)* (and TDX module global initialization SEAMCALL if not done) on local cpu to * make this cpu be ready to run any other SEAMCALLs. */ -static int tdx_cpu_enable (void ) +int tdx_cpu_enable (void ) {struct tdx_module_args args = {};int ret;
Signature · 1 line
2.47.1
PATCH v8 14/21 x86/virt/tdx: Restore TDX module state
In reply to Chao GaoTDX module state was packed as handoff data during module shutdown. After per-CPU initialization, the new module can restore TDX module state from handoff data to preserve running TDs.
Once the restoration is done, the TDX module update is complete, which means the new module is ready to handle requests from the host and guests.
Implement the new TDH.SYS.UPDATE SEAMCALL to restore TDX module state and invoke it on one CPU since it only needs to be called once.
For error handling, Intel® Trust Domain Extensions (Intel® TDX) Module Base Architecture Specification, Chapter "Restore TDX Module State after a TD-Preserving Update" states
If TDH.SYS.UPDATE returns an error, then the host VMM can continue with the non-update sequence (TDH.SYS.CONFIG, TDH.SYS.KEY.CONFIG etc.). In this case all existing TDs are lost. Alternatively, the host VMM can request the P-SEAMLDR to update to another TDX module. If that update is successful, existing TDs are preserved.
Given the complexity and uncertain value of above recovery paths, simply propagate errors.
Also note that the location and the format of handoff data is defined by the TDX module. The new module knows where to get handoff data and how to parse it. The kernel doesn't need to provide its location, format etc.
---
v8:
- don't add a duplicate error code as seamcal_prerr() will do that
- don't reset tdx module status to ERORR on error
--- arch/x86/virt/vmx/tdx/seamldr.c | 5 +++++ arch/x86/virt/vmx/tdx/tdx.c | 13 +++++++++++++ arch/x86/virt/vmx/tdx/tdx.h | 2 ++ 3 files changed, 20 insertions(+)
arch/x86/virt/vmx/tdx/seamldr.c +5 -0
@@ -213,6 +213,7 @@ enum module_update_state {MODULE_UPDATE_SHUTDOWN ,MODULE_UPDATE_CPU_INSTALL ,MODULE_UPDATE_CPU_INIT , +MODULE_UPDATE_RUN_UPDATE , MODULE_UPDATE_DONE, };
@@ -275,6 +276,10 @@ static int do_seamldr_install_module(void *seamldr_params)case MODULE_UPDATE_CPU_INIT : ret =tdx_cpu_enable ();break ; +case MODULE_UPDATE_RUN_UPDATE : +if (primary) + ret =tdx_module_run_update (); +break ;default :break ; }
arch/x86/virt/vmx/tdx/tdx.c +13 -0
@@ -1266,6 +1266,19 @@ int tdx_module_shutdown(void)return 0 ; } +int tdx_module_run_update (void ) +{ +struct tdx_module_args args = {}; +int ret; + + ret =seamcall_prerr (TDH_SYS_UPDATE , &args); +if (ret) +return ret; + + tdx_module_initialized = true; +return 0 ; +} +static bool is_pamt_page (unsigned long phys) {struct tdmr_info_list *tdmr_list = &tdx_tdmr_list;
arch/x86/virt/vmx/tdx/tdx.h +2 -0
@@ -47,6 +47,7 @@#define TDH_VP_WR 43#define TDH_SYS_CONFIG 45#define TDH_SYS_SHUTDOWN 52 +#define TDH_SYS_UPDATE 53/* * SEAMCALL leaf:
@@ -112,5 +113,6 @@ struct tdmr_info_list { };int tdx_module_shutdown (void ); +int tdx_module_run_update (void );#endif
Signature · 1 line
2.47.1
PATCH v8 15/21 x86/virt/tdx: Refresh TDX module version after update
In reply to Chao GaoThe kernel exposes the TDX module version through sysfs so userspace can check update compatibility. That information needs to remain accurate across runtime updates.
A runtime update may change the module's update_version, so refresh the cached version after a successful update and emit a log message to show the version change.
Drop __ro_after_init from tdx_sysinfo because it is now updated at runtime.
Perform the refresh outside of stop_machine() since printk() within stop_machine() would add significant latency.
Do not refresh the rest of tdx_sysinfo. Refreshing them at runtime could disrupt running software that relies on the previously reported values.
Note that major and minor versions are not refreshed because runtime updates are supported only between releases with identical major and minor versions.
---
Sashiko flagged a potential torn-read concern: update_version is read via sysfs while it is refreshed post-update. But, update_version is a naturally-aligned u16, and on x86, the compiler won't split aligned u16 accesses. So READ_ONCE/WRITE_ONCE() aren't needed for update_version.
v8:
- drop the unnecessary old/new metadata comparison
- do not refresh the handoff version, since it is no longer cached
- rename the helper to reflect its purpose instead of using the generic tdx_module_post_update()
--- arch/x86/virt/vmx/tdx/seamldr.c | 8 +++++++- arch/x86/virt/vmx/tdx/tdx.c | 21 ++++++++++++++++++++- arch/x86/virt/vmx/tdx/tdx.h | 1 + arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 2 +- 4 files changed, 29 insertions(+), 3 deletions(-)
arch/x86/virt/vmx/tdx/seamldr.c +7 -1
@@ -306,6 +306,8 @@ DEFINE_FREE(free_seamldr_params, struct seamldr_params *,*/ int seamldr_install_module (const u8 *data,u32 size) { +int ret; + struct seamldr_params *params __free(free_seamldr_params) =init_seamldr_params (data, size);if (IS_ERR (params))
@@ -314,6 +316,10 @@ int seamldr_install_module(const u8 *data, u32 size)/* Ensure a stable set of online CPUs for the update process. */ guard (cpus_read_lock)();set_target_state (MODULE_UPDATE_START +1 ); -return stop_machine_cpuslocked (do_seamldr_install_module, params, cpu_online_mask); + ret =stop_machine_cpuslocked (do_seamldr_install_module, params, cpu_online_mask); +if (ret) +return ret; + +return tdx_module_refresh_version (); }EXPORT_SYMBOL_FOR_MODULES (seamldr_install_module,"tdx-host" );
arch/x86/virt/vmx/tdx/tdx.c +20 -1
@@ -61,7 +61,7 @@ static int sysinit_ret;/* All TDX-usable memory regions. Protected by mem_hotplug_lock. */ static LIST_HEAD (tdx_memlist); -static struct tdx_sys_info tdx_sysinfo__ro_after_init ; +static struct tdx_sys_info tdx_sysinfo;static bool tdx_module_initialized;/*
@@ -1279,6 +1279,25 @@ int tdx_module_run_update(void)return 0 ; } +int tdx_module_refresh_version (void ) +{ +struct tdx_sys_info_version *old, new; +int ret; + +/* Shouldn't fail as the update has succeeded. */ + ret =get_tdx_sys_info_version (&new); +WARN_ON_ONCE (ret); + + old = &tdx_sysinfo.version; +pr_info ("version " TDX_VERSION_FMT " -> " TDX_VERSION_FMT "\n" , + old->major_version, old->minor_version, old->update_version, + new.major_version, new.minor_version, new.update_version); + +/* Major/minor versions should not change across updates. */ + tdx_sysinfo.version.update_version = new.update_version; +return 0 ; +} +static bool is_pamt_page (unsigned long phys) {struct tdmr_info_list *tdmr_list = &tdx_tdmr_list;
arch/x86/virt/vmx/tdx/tdx.h +1 -0
@@ -114,5 +114,6 @@ struct tdmr_info_list {int tdx_module_shutdown (void );int tdx_module_run_update (void ); +int tdx_module_refresh_version (void );#endif
arch/x86/virt/vmx/tdx/tdx_global_metadata.c +1 -1
@@ -7,7 +7,7 @@* Include this file to other C file instead. */ -static __init int get_tdx_sys_info_version (struct tdx_sys_info_version *sysinfo_version) +static int get_tdx_sys_info_version (struct tdx_sys_info_version *sysinfo_version) {int ret =0 ;u64 val;
Signature · 1 line
2.47.1
Re: [PATCH v8 15/21] x86/virt/tdx: Refresh TDX module version after update
Quoted 8 lines · Chao Gao
The kernel exposes the TDX module version through sysfs so userspace can check update compatibility. That information needs to remain accurate across runtime updates.
A runtime update may change the module's
update_version, so refresh the cached version after a successful update and emit a log message to show the version change.
Quoted 7 lines
Drop
__ro_after_initfromtdx_sysinfobecause it is now updated at runtime.Perform the refresh outside of
stop_machine()sinceprintk()withinstop_machine()would add significant latency.Do not refresh the rest of
tdx_sysinfo. Refreshing them at runtime could disrupt running software that relies on the previously reported values.
This needs more explanation. I think the reasoning is quite nuanced.
tdx_sysinfo is just a cache of what the TDX module is and can do. If that changes, it means the TDX module changed. So you somehow need to argue why it's OK to hide those changes from the tdx_sysinfo users.
Why would they be confused by tdx_sysinfo changes but not by the TDX module *itself* changing?
Quoted 2 lines
Note that major and minor versions are not refreshed because runtime updates are supported only between releases with identical major and minor versions.
I'd rather have this in code than a changelog comment.
If they can't change then warn if they do.
Quoted 14 lines
arch/x86/virt/vmx/tdx/seamldr.c +2
-0@@ -306,6 +306,8 @@ DEFINE_FREE(free_seamldr_params, struct seamldr_params *,*/ int seamldr_install_module (const u8 *data,u32 size) { +int ret; + struct seamldr_params *params __free(free_seamldr_params) =init_seamldr_params (data, size);if (IS_ERR (params))
arch/x86/virt/vmx/tdx/seamldr.c int seamldr_install_module(const u8 *data, u32 size) · line 324
Quoted 10 lines
arch/x86/virt/vmx/tdx/seamldr.c +5
-1@@ -314,6 +316,10 @@ int seamldr_install_module(const u8 *data, u32 size)/* Ensure a stable set of online CPUs for the update process. */ guard (cpus_read_lock)();set_target_state (MODULE_UPDATE_START +1 ); -return stop_machine_cpuslocked (do_seamldr_install_module, params, cpu_online_mask); + ret =stop_machine_cpuslocked (do_seamldr_install_module, params, cpu_online_mask); +if (ret) +return ret; + +return tdx_module_refresh_version (); }
This is one reason I rather dislike guard().
Does tdx_module_refresh_version() need to be guarded by 'cpus_read_lock'?
arch/x86/virt/vmx/tdx/seamldr.c int seamldr_install_module(const u8 *data, u32 size)
Quoted 3 lines
arch/x86/virt/vmx/tdx/seamldr.c +3
-0+int tdx_module_refresh_version (void ) +{ +struct tdx_sys_info_version *old, new;
Two variable definitions, please. IMNHO pointers and plain variables are different types. Even if you can, they should not be defined together.
arch/x86/virt/vmx/tdx/seamldr.c
Quoted 10 lines
arch/x86/virt/vmx/tdx/seamldr.c +10
-0+int ret; + +/* Shouldn't fail as the update has succeeded. */ + ret =get_tdx_sys_info_version (&new); +WARN_ON_ONCE (ret); + + old = &tdx_sysinfo.version; +pr_info ("version " TDX_VERSION_FMT " -> " TDX_VERSION_FMT "\n" , + old->major_version, old->minor_version, old->update_version, + new.major_version, new.minor_version, new.update_version);
Having 'new' and 'old' be different types is really wonky. What's wrong with:
struct tdx_sys_info_version old = tdx_sysinfo.version;
?
arch/x86/virt/vmx/tdx/seamldr.c
Quoted 2 lines
arch/x86/virt/vmx/tdx/seamldr.c +2
-0+/* Major/minor versions should not change across updates. */ + tdx_sysinfo.version.update_version = new.update_version;
^ very odd tab
Also, how much of this do you *NEED*? You don't need to print the old version. You don't really need to _print_ the new version either.
Isn't this arguably all fluff?
If the fluff went away would we even be talking about the funky pointer versus plain type gunk?
Second, this is all open-coded and completely discrete from the code that originally sets 'tdx_sysinfo.version'. Can it be unified?
Edgecombe, Rick P
maintainerRe: [PATCH v8 15/21] x86/virt/tdx: Refresh TDX module version after update
Quoted 11 lines · Dave Hansen
Quoted 2 lines
Do not refresh the rest of
tdx_sysinfo. Refreshing them at runtime could disrupt running software that relies on the previously reported values.This needs more explanation. I think the reasoning is quite nuanced.
tdx_sysinfois just a cache of what the TDX module is and can do. If that changes, it means the TDX module changed. So you somehow need to argue why it's OK to hide those changes from thetdx_sysinfousers.Why would they be confused by
tdx_sysinfochanges but not by the TDX module *itself* changing?
We shouldn't refresh them because they don't change, right Chao? It's not because we are hiding things?
Quoted 7 lines
Quoted 3 lines
Note that major and minor versions are not refreshed because runtime updates are supported only between releases with identical major and minor versions.
I'd rather have this in code than a changelog comment.
If they can't change then warn if they do.
Re: [PATCH v8 15/21] x86/virt/tdx: Refresh TDX module version after update
In reply to Dave HansenQuoted 17 lines · Dave Hansen
Quoted 8 lines · Chao Gao
The kernel exposes the TDX module version through sysfs so userspace can check update compatibility. That information needs to remain accurate across runtime updates.
A runtime update may change the module's
update_version, so refresh the cached version after a successful update and emit a log message to show the version change.Quoted 7 lines
Drop
__ro_after_initfromtdx_sysinfobecause it is now updated at runtime.Perform the refresh outside of
stop_machine()sinceprintk()withinstop_machine()would add significant latency.Do not refresh the rest of
tdx_sysinfo. Refreshing them at runtime could disrupt running software that relies on the previously reported values.
Quoted 8 lines
This needs more explanation. I think the reasoning is quite nuanced.
tdx_sysinfois just a cache of what the TDX module is and can do. If that changes, it means the TDX module changed. So you somehow need to argue why it's OK to hide those changes from thetdx_sysinfousers.Why would they be confused by
tdx_sysinfochanges but not by the TDX module *itself* changing?
Good point. The key assumption here is that module updates are fully backward compatible, so running software can continue to work without seeing the new tdx_sysinfo. I will revise the changelog. See below.
Quoted 7 lines
Quoted 3 lines
Note that major and minor versions are not refreshed because runtime updates are supported only between releases with identical major and minor versions.
I'd rather have this in code than a changelog comment.
If they can't change then warn if they do.
Maybe I can just drop the note as I don't want to add code to preemptively catch theoretical module bugs.
I added it because Sashiko pointed out that assigning the whole version struct outside stop_machine() could allow sysfs readers to observe a partially updated version. As we don't need to print new module version, I will move that assignment into stop_machine(), which addresses that issue. After that, there is no need to mention that major/minor versions are identical across updates.
Quoted 28 lines
Quoted 15 lines
arch/x86/virt/vmx/tdx/seamldr.c +2
-0@@ -306,6 +306,8 @@ DEFINE_FREE(free_seamldr_params, struct seamldr_params *,*/ int seamldr_install_module (const u8 *data,u32 size) { +int ret; + struct seamldr_params *params __free(free_seamldr_params) =init_seamldr_params (data, size);if (IS_ERR (params))arch/x86/virt/vmx/tdx/seamldr.c int seamldr_install_module(const u8 *data, u32 size) · line 324
Quoted 10 lines
arch/x86/virt/vmx/tdx/seamldr.c +5
-1@@ -314,6 +316,10 @@ int seamldr_install_module(const u8 *data, u32 size)/* Ensure a stable set of online CPUs for the update process. */ guard (cpus_read_lock)();set_target_state (MODULE_UPDATE_START +1 ); -return stop_machine_cpuslocked (do_seamldr_install_module, params, cpu_online_mask); + ret =stop_machine_cpuslocked (do_seamldr_install_module, params, cpu_online_mask); +if (ret) +return ret; + +return tdx_module_refresh_version (); }This is one reason I rather dislike
guard().
Quoted 1 line
Does
tdx_module_refresh_version()need to be guarded by 'cpus_read_lock'?
No. It can be moved out of 'cpus_read_lock'.
Quoted 9 lines
?
arch/x86/virt/vmx/tdx/seamldr.c int seamldr_install_module(const u8 *data, u32 size)
Quoted 2 lines
arch/x86/virt/vmx/tdx/seamldr.c +2
-0+/* Major/minor versions should not change across updates. */ + tdx_sysinfo.version.update_version = new.update_version;^ very odd tab
Quoted 4 lines
Also, how much of this do you *NEED*? You don't need to print the old version. You don't really need to _print_ the new version either.
Isn't this arguably all fluff?
I initially kept tdx module update similar to the microcode update. But yes, printing the new version is not strictly needed. Once the unnecessary complexity is dropped, the patch becomes quite small:
commit 90e5a66b3f54af96d5895f6707ecdeef4bc4a3ed
Author: Chao Gao <[email protected]> Date: Tue Mar 31 05:41:29 2026 -0700
x86/virt/tdx: Refresh TDX module version after update
The kernel exposes the TDX module version through sysfs so userspace can check update compatibility. That information needs to remain accurate across runtime updates.
A runtime update may change the module's update_version, so refresh the cached version after a successful update.
Drop __ro_after_init from tdx_sysinfo because it is now updated at runtime.
Do not refresh the rest of tdx_sysinfo even if those values change. Current tdx_sysinfo users (e.g., KVM) can continue to work across module updates without seeing the new values because module updates are required to be backward compatible. And those users are not written to re-validate tdx_sysinfo values after an update, so refreshing them could be risky.
For example, a user may decide both setup and teardown behavior purely based on the reported capabilities. If refreshed tdx_sysinfo starts reporting a newly gained capability, later code may assume the corresponding setup exists and try to use or tear it down, even though no such setup was done before the update.
---
v9:
- don't print old and new version [Dave]
- explain why it's OK to hide changes from the tdx_sysinfo users [Dave]
- update versions in stop_machine context
- don't mention major/minor versions are idential across updates. That fact is not relevant here.
arch/x86/virt/vmx/tdx/tdx.c +1 -1
@@ -66,7 +66,7 @@ static struct tdmr_info_list tdx_tdmr_list;/* All TDX-usable memory regions. Protected by mem_hotplug_lock. */ static LIST_HEAD (tdx_memlist); -static struct tdx_sys_info tdx_sysinfo__ro_after_init ; +static struct tdx_sys_info tdx_sysinfo;/* * Do the module global initialization once and return its result.
@@ -1305,6 +1305,10 @@ int tdx_module_run_update(void)
if (ret) return ret;
+ /* Shouldn't fail as the update has succeeded. */
+ ret = get_tdx_sys_info_version(&tdx_sysinfo.version);
+ WARN_ON_ONCE(ret);
+
tdx_module_state.initialized = true; return 0;
}
arch/x86/virt/vmx/tdx/tdx_global_metadata.c +1 -1
@@ -7,7 +7,7 @@* Include this file to other C file instead. */ -static __init int get_tdx_sys_info_version (struct tdx_sys_info_version *sysinfo_version) +static int get_tdx_sys_info_version (struct tdx_sys_info_version *sysinfo_version) {
int ret = 0; u64 val;
PATCH v8 16/21 x86/virt/tdx: Reject updates during concurrent TD build
In reply to Chao Gaotl;dr: A TDX module erratum can silently corrupt TD measurement state if a module update races with TD build. Handle that by rejecting the update, instead of introducing new TD-build ioctl failure paths.
Long Version:
Updates must not break unrelated operations. For TDX module updates, this means an update must not interfere with other TDX flows.
A TDX module erratum violates that expectation: if an update races with TD build, TD build output can be corrupted, e.g. the measurement hash, which later causes attestation failure.
The TDX module provides two independent, opt-in mitigations for this erratum:
1. Reject updates while TD build is in progress. This mitigation can be requested via TDH.SYS.SHUTDOWN.
2. Do not reject the update for this race, but instead fail later SEAMCALLs in the overlapping TD build flow. This mitigation can be requested via TDH.SYS.UPDATE.
The kernel can choose option 1, option 2, or neither.
Choose option 1 to confine failures to the update path and preserve existing TD build and KVM ioctl behavior. Userspace already controls update timing, and retrying a rejected update is straightforward.
Option 2 would make TD build failures explicit, but it would also introduce new error paths in existing KVM ioctls. That complicates KVM error handling and risks ABI instability. Sean previously rejected that approach [1].
Choosing neither option was also considered and rejected. Leaving this erratum unhandled would allow an update racing with TD build to silently corrupt TD build output. That violates the requirement that TDX module updates must not interfere with unrelated TDX flows.
Request race detection during TDH.SYS.SHUTDOWN and map a detected race to -EBUSY, and report it to userspace as FW_UPLOAD_ERR_BUSY. This lets userspace distinguish the race from other failures and retry the update.
Do not pre-check support for this race-detection capability. If it is unsupported, rely on the TDX module to reject module shutdown.
This implementation is based on a reference patch by Vishal [2].
Note: moving NO_RBP_MOD definition is to centralize the bit definitions.
---
v8:
- rewrite the changelog [Rick]
- alway pass the compat flag to the TDX module [Rick]
--- arch/x86/include/asm/tdx.h | 11 +++++++++-- arch/x86/kvm/vmx/tdx_errno.h | 2 -- arch/x86/virt/vmx/tdx/tdx.c | 26 +++++++++++++++++++++++--- arch/x86/virt/vmx/tdx/tdx.h | 3 --- drivers/virt/coco/tdx-host/tdx-host.c | 2 ++ 5 files changed, 34 insertions(+), 10 deletions(-)
arch/x86/include/asm/tdx.h +9 -2
@@ -26,11 +26,18 @@#define TDX_SEAMCALL_GP (TDX_SW_ERROR | X86_TRAP_GP)#define TDX_SEAMCALL_UD (TDX_SW_ERROR | X86_TRAP_UD) +#define TDX_SEAMCALL_STATUS_MASK 0xFFFFFFFF00000000ULL +/* * TDX module SEAMCALL leaf function error codes */ -#define TDX_SUCCESS 0ULL -#define TDX_RND_NO_ENTROPY 0x8000020300000000ULL +#define TDX_SUCCESS 0ULL +#define TDX_RND_NO_ENTROPY 0x8000020300000000ULL +#define TDX_UPDATE_COMPAT_SENSITIVE 0x8000051200000000ULL + +/* Bit definitions of TDX_FEATURES0 metadata field */ +#define TDX_FEATURES0_NO_RBP_MOD BIT_ULL(18) +#define TDX_FEATURES0_UPDATE_COMPAT BIT_ULL(47)#ifndef __ASSEMBLER__
arch/x86/kvm/vmx/tdx_errno.h +0 -2
@@ -4,8 +4,6 @@#ifndef __KVM_X86_TDX_ERRNO_H#define __KVM_X86_TDX_ERRNO_H -#define TDX_SEAMCALL_STATUS_MASK 0xFFFFFFFF00000000ULL -/* * TDX SEAMCALL Status Codes (returned in RAX) */
arch/x86/virt/vmx/tdx/tdx.c +23 -3
@@ -1234,10 +1234,13 @@ static __init int tdx_enable(void) }subsys_initcall (tdx_enable); +#define TDX_SYS_SHUTDOWN_AVOID_COMPAT_SENSITIVE BIT(16) +int tdx_module_shutdown (void ) {struct tdx_sys_info_handoff handoff = {};struct tdx_module_args args = {}; +u64 err;int ret, cpu; ret =get_tdx_sys_info_handoff (&handoff);
@@ -1248,9 +1251,26 @@ int tdx_module_shutdown(void)* module can produce and most likely supported by newer modules. */ args.rcx = handoff.module_hv; - ret =seamcall_prerr (TDH_SYS_SHUTDOWN , &args); -if (ret) -return ret; + +/* +* Mitigate the erratum where updates can break concurrent TD +* build. Do not pre-check support for this flag. If unsupported, +* rely on the TDX module to reject shutdown requests. +*/ + args.rcx |=TDX_SYS_SHUTDOWN_AVOID_COMPAT_SENSITIVE ; + + err =seamcall (TDH_SYS_SHUTDOWN , &args); + +/* +* Return -EBUSY to signal that some ongoing flows are incompatible +* with updates so that userspace can retry. +*/ +if ((err &TDX_SEAMCALL_STATUS_MASK ) ==TDX_UPDATE_COMPAT_SENSITIVE ) +return -EBUSY ; +if (err) { +seamcall_err (TDH_SYS_SHUTDOWN , err, &args); +return -EIO ; + }/* * Clear global and per-CPU initialization flags so the new module
arch/x86/virt/vmx/tdx/tdx.h +0 -3
@@ -86,9 +86,6 @@ struct tdmr_info { DECLARE_FLEX_ARRAY(struct tdmr_reserved_area, reserved_areas); } __packed__aligned(TDMR_INFO_ALIGNMENT) ; -/* Bit definitions of TDX_FEATURES0 metadata field */ -#define TDX_FEATURES0_NO_RBP_MOD BIT(18) -/* * Do not put any hardware-defined TDX structure representations below * this comment!
drivers/virt/coco/tdx-host/tdx-host.c +2 -0
@@ -127,6 +127,8 @@ static enum fw_upload_err tdx_fw_write(struct fw_upload *fwl, const u8 *data,case 0 : *written = size;return FW_UPLOAD_ERR_NONE ; +case -EBUSY : +return FW_UPLOAD_ERR_BUSY ;default :return FW_UPLOAD_ERR_FW_INVALID ; }
Signature · 1 line
2.47.1
Re: [PATCH v8 16/21] x86/virt/tdx: Reject updates during concurrent TD build
Quoted 3 lines · Chao Gao
tl;dr: A TDX module erratum can silently corrupt TD measurement state if a module update races with TD build. Handle that by rejecting the update, instead of introducing new TD-build ioctl failure paths.
The downside of this needs to be discussed. Namely that module updates can be blocked forever.
Quoted 1 line
Long Version:
...
This explanation is confusing.
Focus on what the patch *does* and its features and downsides.
*Then* broach the alternatives. But, please, clearly separate out this patch from other opining.
Quoted 12 lines
arch/x86/include/asm/tdx.h +2
-0@@ -26,11 +26,18 @@#define TDX_SEAMCALL_GP (TDX_SW_ERROR | X86_TRAP_GP)#define TDX_SEAMCALL_UD (TDX_SW_ERROR | X86_TRAP_UD) +#define TDX_SEAMCALL_STATUS_MASK 0xFFFFFFFF00000000ULL +/* * TDX module SEAMCALL leaf function error codes
arch/x86/include/asm/tdx.h line 40
Quoted 10 lines
arch/x86/include/asm/tdx.h +7
-2*/ -#define TDX_SUCCESS 0ULL -#define TDX_RND_NO_ENTROPY 0x8000020300000000ULL +#define TDX_SUCCESS 0ULL +#define TDX_RND_NO_ENTROPY 0x8000020300000000ULL +#define TDX_UPDATE_COMPAT_SENSITIVE 0x8000051200000000ULL + +/* Bit definitions of TDX_FEATURES0 metadata field */ +#define TDX_FEATURES0_NO_RBP_MOD BIT_ULL(18) +#define TDX_FEATURES0_UPDATE_COMPAT BIT_ULL(47)
Refactor first. Add new features second.
Quoted 37 lines
arch/x86/include/asm/tdx.h +0
-0#ifndef __ASSEMBLER__arch/x86/kvm/vmx/tdx_errno.h +0
-2@@ -4,8 +4,6 @@#ifndef __KVM_X86_TDX_ERRNO_H#define __KVM_X86_TDX_ERRNO_H -#define TDX_SEAMCALL_STATUS_MASK 0xFFFFFFFF00000000ULL -/* * TDX SEAMCALL Status Codes (returned in RAX) */ arch/x86/virt/vmx/tdx/tdx.c +3
-0@@ -1234,10 +1234,13 @@ static __init int tdx_enable(void) }subsys_initcall (tdx_enable); +#define TDX_SYS_SHUTDOWN_AVOID_COMPAT_SENSITIVE BIT(16) +int tdx_module_shutdown (void ) {struct tdx_sys_info_handoff handoff = {};struct tdx_module_args args = {}; +u64 err;int ret, cpu; ret =get_tdx_sys_info_handoff (&handoff);@@ -1248,9 +1251,26 @@ int tdx_module_shutdown(void)* module can produce and most likely supported by newer modules. */ args.rcx = handoff.module_hv;
arch/x86/virt/vmx/tdx/tdx.c int tdx_module_shutdown(void) · line 1260
Quoted 10 lines
arch/x86/virt/vmx/tdx/tdx.c +7
-3- ret =seamcall_prerr (TDH_SYS_SHUTDOWN , &args); -if (ret) -return ret; + +/* +* Mitigate the erratum where updates can break concurrent TD +* build. Do not pre-check support for this flag. If unsupported, +* rely on the TDX module to reject shutdown requests. +*/ + args.rcx |=TDX_SYS_SHUTDOWN_AVOID_COMPAT_SENSITIVE ;
"Mitigate the erratum..." is a strange way to start this.
This would be a much better format I think:
/*
* This flag will <say what it does> if <triggering event>
* happens. That eliminates exposure to a TDX erratum which
* can <explain bad things here>.
*
* This flag is not supported by all TDX modules and may cause
* the shutdown (and subsequent update procedure) to fail. */
arch/x86/virt/vmx/tdx/tdx.c int tdx_module_shutdown(void) · line 1267
Quoted 6 lines
arch/x86/virt/vmx/tdx/tdx.c +6
-0+ err =seamcall (TDH_SYS_SHUTDOWN , &args); + +/* +* Return -EBUSY to signal that some ongoing flows are incompatible +* with updates so that userspace can retry. +*/
/* * The shutdown ran into a "sensitive" ongoing operation, like * TD build. Signal to userspace that it can retry. */
arch/x86/virt/vmx/tdx/tdx.c int tdx_module_shutdown(void) · line 1273
Quoted 6 lines
arch/x86/virt/vmx/tdx/tdx.c +6
-0+if ((err &TDX_SEAMCALL_STATUS_MASK ) ==TDX_UPDATE_COMPAT_SENSITIVE ) +return -EBUSY ; +if (err) { +seamcall_err (TDH_SYS_SHUTDOWN , err, &args); +return -EIO ; + }
Whitespace between the if()s please.
PATCH v8 17/21 x86/virt/seamldr: Abort updates on failure
In reply to Chao GaoTDX module update is a multi-step process, and each step can fail.
The current update flow continues to later steps after an error. Continuing after failure leaves the TDX module in an unrecoverable state.
One failure case must remain recoverable: update contention with an ongoing TD build. The agreed kernel behavior for this case is to fail the update with -EBUSY so userspace can retry later.
Abort the update on any failure. For the contention case, this provides a recoverable failure mode because the failure occurs before any TDX module state is changed. Use the same rule for all errors to avoid special-casing -EBUSY.
Introduce a shared "failed" flag. When a CPU fails, set the flag and force all CPUs to exit the update loop. A failing CPU does not acknowledge the current step, so other CPUs remain at that step until they observe the "failed" flag and exit.
Use READ_ONCE()/WRITE_ONCE() for the flag because it is used for lockless communication between stop_machine workers. Also use WRITE_ONCE() for the initial clear to keep accesses to the flag uniform and explicit.
---
v8:
- Explain why aborting updates is necessary. [Rick]
- always use READ_ONCE()/WRITE_ONCE() for the "failed" flag.
--- arch/x86/virt/vmx/tdx/seamldr.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
arch/x86/virt/vmx/tdx/seamldr.c +7 -2
@@ -220,6 +220,7 @@ enum module_update_state { static struct { enum module_update_state state; int thread_ack; + bool failed; /* * Protect update_data. Raw spinlock as it will be acquired from * interrupt-disabled contexts.
@@ -284,12 +285,15 @@ static int do_seamldr_install_module(void *seamldr_params)break ; } -ack_state (); +if (ret) +WRITE_ONCE (update_data.failed, true); +else +ack_state (); }else {touch_nmi_watchdog ();rcu_momentary_eqs (); } - }while (curstate !=MODULE_UPDATE_DONE ); + }while (curstate !=MODULE_UPDATE_DONE && !READ_ONCE (update_data.failed));return ret; }
@@ -315,6 +319,7 @@ int seamldr_install_module(const u8 *data, u32 size)/* Ensure a stable set of online CPUs for the update process. */ guard (cpus_read_lock)(); +WRITE_ONCE (update_data.failed, false);set_target_state (MODULE_UPDATE_START +1 ); ret =stop_machine_cpuslocked (do_seamldr_install_module, params, cpu_online_mask);if (ret)
Signature · 1 line
2.47.1
Re: [PATCH v8 17/21] x86/virt/seamldr: Abort updates on failure
I don't like how this is being done.
1. Introduce this do{}while() loop
2. Do 20 other patches
3. Introduce a thing that can make it change
4. Change the fundamental flow of the loop, to fix #3
I'd much rather have:
1. Introduce this do{}while() loop
2. Tweak fundamental flow of the loop from the last patch when I can remember it. Allude to future failures.
3. Do 20 other patches
4. Introduce a thing that uses #2
Quoted 15 lines
arch/x86/virt/vmx/tdx/seamldr.c +1
-0@@ -220,6 +220,7 @@ enum module_update_state { static struct { enum module_update_state state; int thread_ack; + bool failed; /* * Protect update_data. Raw spinlock as it will be acquired from * interrupt-disabled contexts.@@ -284,12 +285,15 @@ static int do_seamldr_install_module(void *seamldr_params)break ; }
arch/x86/virt/vmx/tdx/seamldr.c static int do_seamldr_install_module(void *seamldr_params) · line 295
Quoted 10 lines
arch/x86/virt/vmx/tdx/seamldr.c +4
-1-ack_state (); +if (ret) +WRITE_ONCE (update_data.failed, true); +else +ack_state (); }else {touch_nmi_watchdog ();rcu_momentary_eqs (); }
I don't like how this is turning out either. I don't like all the nested conditions or ack_state() that hides its mucking with update data while its caller mucks with it directly. It's just all hacked together.
Defer all of the acking, and *failed* acking to the ack_state() helper.
Also, I'm kinda peeved that you copied and pasted the touch_nmi_watchdog()/rcu_momentary_eqs() bits and none of the comments. This is a rather subtle use of both. If you want this to be a normal "spinning in stop machine" idiom, then create a helper and put the comments there.
Also, this is a case where:
do {
cpu_relax();
newstate = READ_ONCE(update_data.state);
if (newstate == curstate) {
// can cpu_relax() just go in here??
touch_nmi_watchdog();
rcu_momentary_eqs();
continue;
}
switch() {
// state changing here
}
} while (...);is a much more sane setup. You're not paying the if() indentation cost for the entire state transition block. You're also putting the "shut up the warnings" code out of the way where you can forget about it.
arch/x86/virt/vmx/tdx/seamldr.c int seamldr_install_module(const u8 *data, u32 size) · line 325
Quoted 13 lines
arch/x86/virt/vmx/tdx/seamldr.c +2
-1- }while (curstate !=MODULE_UPDATE_DONE ); + }while (curstate !=MODULE_UPDATE_DONE && !READ_ONCE (update_data.failed));return ret; }@@ -315,6 +319,7 @@ int seamldr_install_module(const u8 *data, u32 size)/* Ensure a stable set of online CPUs for the update process. */ guard (cpus_read_lock)(); +WRITE_ONCE (update_data.failed, false);set_target_state (MODULE_UPDATE_START +1 ); ret =stop_machine_cpuslocked (do_seamldr_install_module, params, cpu_online_mask);if (ret)
I kinda wish this 'update_data.failed' set was named. This is trying to bring 'update_data' into some initial state. Let's _call_ it that. Honestly, I wouldn't hate if that function just also did the spinlock init since it's so ugly do to statically.
Re: [PATCH v8 17/21] x86/virt/seamldr: Abort updates on failure
Quoted 7 lines · Dave Hansen
I don't like how this is being done.
1. Introduce this do{}while() loop
2. Do 20 other patches
3. Introduce a thing that can make it change
4. Change the fundamental flow of the loop, to fix #3
Quoted 7 lines
I'd much rather have:
1. Introduce this do{}while() loop
2. Tweak fundamental flow of the loop from the last patch when I can remember it. Allude to future failures.
3. Do 20 other patches
4. Introduce a thing that uses #2
OK, that makes sense. I'll reorder the series so this patch comes immediately after the skeleton patch.
Quoted 32 lines
Quoted 17 lines
arch/x86/virt/vmx/tdx/seamldr.c +1
-0@@ -220,6 +220,7 @@ enum module_update_state { static struct { enum module_update_state state; int thread_ack; + bool failed; /* * Protect update_data. Raw spinlock as it will be acquired from * interrupt-disabled contexts.@@ -284,12 +285,15 @@ static int do_seamldr_install_module(void *seamldr_params)break ; }arch/x86/virt/vmx/tdx/seamldr.c static int do_seamldr_install_module(void *seamldr_params) · line 295
Quoted 10 lines
arch/x86/virt/vmx/tdx/seamldr.c +4
-1-ack_state (); +if (ret) +WRITE_ONCE (update_data.failed, true); +else +ack_state (); }else {touch_nmi_watchdog ();rcu_momentary_eqs (); }I don't like how this is turning out either. I don't like all the nested conditions or
ack_state()that hides its mucking with update data while its caller mucks with it directly. It's just all hacked together.
Quoted 1 line
Defer all of the acking, and *failed* acking to the
ack_state()helper.
OK. I'll fold both normal and failed acking into ack_state().
Quoted 6 lines
Also, I'm kinda peeved that you copied and pasted the touch_nmi_watchdog()/rcu_momentary_eqs() bits and none of the comments. This is a rather subtle use of both. If you want this to be a normal "spinning in stop machine" idiom, then create a helper and put the comments there.
Those two calls were added in stop_machine() to improve debuggability.
The issue they address is that a stop_machine() callback can hang on one CPU. Without touch_nmi_watchdog() and rcu_momentary_eqs(), the other CPUs that are merely spinning in the wait loop can also report hard lockup and RCU stall warnings, which obscures the actual stuck CPU.
I agree that this behavior makes sense in stop_machine() as common infrastructure. But this update path does not take an arbitrary callback function, so that that debuggability is not strictly necessary here. I'll drop those calls from this path unless there is an objection.
Quoted 19 lines
Also, this is a case where:
do { cpu_relax(); newstate = READ_ONCE(update_data.state); if (newstate == curstate) { // can cpu_relax() just go in here?? touch_nmi_watchdog(); rcu_momentary_eqs(); continue; } switch() { // state changing here } } while (...);
Quoted 4 lines
is a much more sane setup. You're not paying the
if()indentation cost for the entire state transition block. You're also putting the "shut up the warnings" code out of the way where you can forget about it.
Agreed. Will do.
PATCH v8 18/21 coco/tdx-host: Don't expose P-SEAMLDR features on CPUs with erratum
In reply to Chao GaoSome TDX-capable CPUs have an erratum, as documented in Intel® Trust Domain CPU Architectural Extensions (May 2021 edition) Chapter 2.3:
SEAMRET from the P-SEAMLDR clears the current VMCS structure pointed to by the current-VMCS pointer. A VMM that invokes the P-SEAMLDR using SEAMCALL must reload the current-VMCS, if required, using the VMPTRLD instruction.
Clearing the current VMCS behind KVM's back will break KVM.
This erratum is not present when IA32_VMX_BASIC[60] is set. Add a CPU bug bit for this erratum and refuse to expose P-SEAMLDR features (e.g., TDX module updates) on affected CPUs.
== Alternatives ==
Two workarounds were considered but both were rejected:
1. Save/restore the current VMCS around P-SEAMLDR calls. This produces ugly assembly code [1] and doesn't play well with #MCE or #NMI if they need to use the current VMCS.
2. Move KVM's VMCS tracking logic to the TDX core code, which would break the boundary between KVM and the TDX core code [2].
--- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/vmx.h | 1 + arch/x86/virt/vmx/tdx/tdx.c | 11 +++++++++++ drivers/virt/coco/tdx-host/tdx-host.c | 8 ++++++++ 4 files changed, 21 insertions(+)
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -573,4 +573,5 @@#define X86_BUG_ITS_NATIVE_ONLY X86_BUG( 1*32+ 8)/* "its_native_only" CPU is affected by ITS, VMX is not affected */ #define X86_BUG_TSA X86_BUG( 1*32+ 9)/* "tsa" CPU is affected by Transient Scheduler Attacks */ #define X86_BUG_VMSCAPE X86_BUG( 1*32+10)/* "vmscape" CPU is affected by VMSCAPE attacks from guests */ +#define X86_BUG_SEAMRET_INVD_VMCS X86_BUG( 1*32+11)/* "seamret_invd_vmcs" SEAMRET from P-SEAMLDR clears the current VMCS */ #endif /* _ASM_X86_CPUFEATURES_H */
arch/x86/include/asm/vmx.h +1 -0
@@ -147,6 +147,7 @@ struct vmcs {#define VMX_BASIC_INOUT BIT_ULL(54)#define VMX_BASIC_TRUE_CTLS BIT_ULL(55)#define VMX_BASIC_NO_HW_ERROR_CODE_CC BIT_ULL(56) +#define VMX_BASIC_NO_SEAMRET_INVD_VMCS BIT_ULL(60)static inline u32 vmx_basic_vmcs_revision_id (u64 vmx_basic) {
arch/x86/virt/vmx/tdx/tdx.c +11 -0
@@ -41,6 +41,7 @@#include <asm/processor.h> #include <asm/mce.h> #include <asm/virt.h> +#include <asm/vmx.h> #include "seamcall_internal.h" #include "tdx.h"
@@ -1495,6 +1496,8 @@ static struct notifier_block tdx_memory_nb = {static void __init check_tdx_erratum (void ) { +u64 basic_msr; +/* * These CPUs have an erratum. A partial write from non-TD * software (e.g. via MOVNTI variants or UC/WC mapping) to TDX
@@ -1506,6 +1509,14 @@ static void __init check_tdx_erratum(void)case INTEL_EMERALDRAPIDS_X :setup_force_cpu_bug (X86_BUG_TDX_PW_MCE ); } + +/* +* Some TDX-capable CPUs have an erratum where the current VMCS is +* cleared after calling into P-SEAMLDR. +*/ +rdmsrq (MSR_IA32_VMX_BASIC , basic_msr); +if (!(basic_msr &VMX_BASIC_NO_SEAMRET_INVD_VMCS )) +setup_force_cpu_bug (X86_BUG_SEAMRET_INVD_VMCS ); }void __init tdx_init (void )
drivers/virt/coco/tdx-host/tdx-host.c +8 -0
@@ -92,6 +92,14 @@ static bool can_expose_seamldr(void)if (!sysinfo)return 0 ; +/* +* Calling P-SEAMLDR on CPUs with the seamret_invd_vmcs bug clears +* the current VMCS, which breaks KVM. Verify the erratum is not +* present before exposing P-SEAMLDR features. +*/ +if (boot_cpu_has_bug (X86_BUG_SEAMRET_INVD_VMCS )) +return false; +return tdx_supports_runtime_update (sysinfo); }
Signature · 1 line
2.47.1
Re: [PATCH v8 18/21] coco/tdx-host: Don't expose P-SEAMLDR features on CPUs with erratum
Quoted 8 lines · Chao Gao
Some TDX-capable CPUs have an erratum, as documented in Intel® Trust Domain CPU Architectural Extensions (May 2021 edition) Chapter 2.3:
SEAMRET from the P-SEAMLDR clears the current VMCS structure pointed to by the current-VMCS pointer. A VMM that invokes the P-SEAMLDR using SEAMCALL must reload the current-VMCS, if required, using the VMPTRLD instruction.
Quoted 5 lines
Clearing the current VMCS behind KVM's back will break KVM.
This erratum is not present when IA32_VMX_BASIC[60] is set. Add a CPU bug bit for this erratum and refuse to expose P-SEAMLDR features (e.g., TDX module updates) on affected CPUs.
This seems totally random.
Shouldn't this be way back when can_expose_seamldr() got defined in the first place?
Quoted 1 line
+#define
X86_BUG_SEAMRET_INVD_VMCSX86_BUG( 1*32+11)/* "seamret_invd_vmcs" SEAMRET from P-SEAMLDR clears the current VMCS */
I find myself wondering if this is worth a bug bit.
Re: [PATCH v8 18/21] coco/tdx-host: Don't expose P-SEAMLDR features on CPUs with erratum
Quoted 11 lines · Dave Hansen
Quoted 10 lines · Chao Gao
Some TDX-capable CPUs have an erratum, as documented in Intel® Trust Domain CPU Architectural Extensions (May 2021 edition) Chapter 2.3:
SEAMRET from the P-SEAMLDR clears the current VMCS structure pointed to by the current-VMCS pointer. A VMM that invokes the P-SEAMLDR using SEAMCALL must reload the current-VMCS, if required, using the VMPTRLD instruction.
Clearing the current VMCS behind KVM's back will break KVM.
Quoted 8 lines
Quoted 3 lines
This erratum is not present when IA32_VMX_BASIC[60] is set. Add a CPU bug bit for this erratum and refuse to expose P-SEAMLDR features (e.g., TDX module updates) on affected CPUs.
This seems totally random.
Shouldn't this be way back when
can_expose_seamldr()got defined in the first place?
I split this out because the erratum needs a longer changelog and some discussion of alternatives. I also wanted the initial can_expose_seamldr() patch to focus on introducing the gating mechanism, without bundling in every detailed check from the start. The update do-while loop and the uABI stuff are the core of this series, while this erratum check is not, so I placed this patch later.
That said, I am perfectly fine with moving this patch to immediately follow the patch that introduces can_expose_seamldr().
Quoted 3 lines
Quoted 1 line
+#define
X86_BUG_SEAMRET_INVD_VMCSX86_BUG( 1*32+11)/* "seamret_invd_vmcs" SEAMRET from P-SEAMLDR clears the current VMCS */I find myself wondering if this is worth a bug bit.
The bug bit was added in v5:
https://lore.kernel.org/all/[email protected]/#t
Kai suggested this approach for two reasons:
1. It is consistent with how X86_BUG_TDX_PW_MCE is handled.
2. It gives userspace a clue as to why the module update feature is unavailable.
That reasoning made sense to me, and I do not see a strong reason not to use the "bug bit" infrastructure. If there is no objection to it, I will add a short explanation to the changelog.
PATCH v8 19/21 x86/virt/tdx: Enable TDX module runtime updates
In reply to Chao GaoAll pieces of TDX module runtime updates are in place. Enable it if it is supported.
--- arch/x86/include/asm/tdx.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/include/asm/tdx.h +2 -2
@@ -36,6 +36,7 @@#define TDX_UPDATE_COMPAT_SENSITIVE 0x8000051200000000ULL/* 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_UPDATE_COMPAT BIT_ULL(47)
@@ -117,8 +118,7 @@ const struct tdx_sys_info *tdx_get_sysinfo(void);static inline bool tdx_supports_runtime_update (const struct tdx_sys_info *sysinfo) { -/* To be enabled when kernel is ready. */ -return false; +return sysinfo->features.tdx_features0 &TDX_FEATURES0_TD_PRESERVING ; }int tdx_guest_keyid_alloc (void );
Signature · 1 line
2.47.1
PATCH v8 20/21 coco/tdx-host: Document TDX module update compatibility criteria
In reply to Chao GaoThe TDX module update protocol facilitates compatible runtime updates.
Document the compatibility criteria and indicators of update failures.
Note that runtime TDX module updates are an "update at your own risk" operation; userspace is responsible for ensureing that the update meets the compatibility criteria.
---
v8:
- Do not map -EIO and -ENOMEM to separate fw_upload errors. There is no current need to distinguish them in the userspace ABI, and fw_upload has no matching error code for -ENOMEM.
- some wording changes.
--- .../ABI/testing/sysfs-devices-faux-tdx-host | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+)
Documentation/ABI/testing/sysfs-devices-faux-tdx-host +39 -0
@@ -26,3 +26,42 @@ Description: (RO) Report the number of remaining updates. TDX maintains a See Intel® Trust Domain Extensions - SEAM Loader (SEAMLDR) Interface Specification, Chapter "SEAMLDR_INFO" and Chapter "SEAMLDR.INSTALL" for more information. + +What: /sys/devices/faux/tdx_host/firmware/tdx_module +Contact: [email protected] +Description: (Directory) The tdx_module directory implements the fw_upload + sysfs ABI, see Documentation/ABI/testing/sysfs-class-firmware + for the general description of the attributes @data, @cancel, + @error, @loading, @remaining_size, and @status. This ABI + facilitates "Compatible TDX module Updates". A compatible update + is one that meets the following criteria: + + Does not interrupt or interfere with any current TDX + operation or TD VM. + + Does not invalidate any previously consumed module metadata + values outside of the TEE_TCB_SVN_2 field (updated Security + Version Number) in TD Quotes. + + Does not require validation of new module metadata fields. By + implication, new module features and capabilities are only + available by installing the module at reboot (BIOS or EFI + helper loaded). + + See tdx_host/firmware/tdx_module/error for information on + update failure indicators. + +What: /sys/devices/faux/tdx_host/firmware/tdx_module/error +Contact: [email protected] +Description: (RO) See Documentation/ABI/testing/sysfs-class-firmware for + baseline expectations for this file. The <ERROR> part in the + <STATUS>:<ERROR> format can be: + + "device-busy": The update conflicts with an in-progress TDX + operation. + + "firmware-invalid": The update failed for any other reason. + + A "firmware-invalid" result may be fatal. If the TDX module is + lost, further TDX operation is not possible, and reading + /sys/devices/faux/tdx_host/version returns -ENXIO.
Signature · 1 line
2.47.1
PATCH v8 21/21 x86/virt/tdx: Document TDX module update
In reply to Chao GaoDocument TDX module update as a subsection of "TDX Host Kernel Support" to provide background information and cover key points that developers and users may need to know, for example:
- update is done in stop_machine() context
- update instructions and results
- update policy and tooling
--- Documentation/arch/x86/tdx.rst | 36 ++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+)
Documentation/arch/x86/tdx.rst +36 -0
@@ -73,6 +73,42 @@ initialize:: [..] virt/tdx: TDX-Module initialization failed ... +TDX module Runtime Update +------------------------- + +The TDX architecture includes a persistent SEAM loader (P-SEAMLDR) that +runs in SEAM mode separately from the TDX module. The kernel can +communicate with P-SEAMLDR to perform runtime updates of the TDX module. + +During update, the TDX module becomes unresponsive to other TDX operations. +To prevent components using TDX (such as KVM) from experiencing unexpected +errors during updates, updates are performed in stop_machine() context. + +TDX module update has complex compatibility requirements; the new module +must be compatible with the current CPU, P-SEAMLDR, and running TDX module. +Rather than implementing complex module selection and policy enforcement +logic in the kernel, userspace is responsible for auditing and selecting +appropriate updates. + +Updates use the standard firmware upload interface. See +Documentation/driver-api/firmware/fw_upload.rst for detailed instructions. + +Successful updates are logged in dmesg: + [..] virt/tdx: version 1.5.20 -> 1.5.24 + +If updates failed, running TDs may be killed and further TDX operations may +not be possible until reboot. For detailed error information, see +Documentation/ABI/testing/sysfs-devices-faux-tdx-host. + +Given the risk of losing existing TDs, userspace should verify that the +update is compatible with the current system and properly validated before +applying it. + +A reference userspace tool that implements necessary checks is available +at: + + https://github.com/intel/tdx-module-binaries + TDX Interaction to Other Kernel Components ------------------------------------------
Signature · 1 line
2.47.1