[PATCH v13 18/22] x86/virt/tdx: Keep TDMRs when module initialization is successful

From: Kai Huang
Date: Fri Aug 25 2023 - 08:18:25 EST


On the platforms with the "partial write machine check" erratum, the
kexec() needs to convert all TDX private pages back to normal before
booting to the new kernel. Otherwise, the new kernel may get unexpected
machine check.

There's no existing infrastructure to track TDX private pages. Change
to keep TDMRs when module initialization is successful so that they can
be used to find PAMTs.

Signed-off-by: Kai Huang <kai.huang@xxxxxxxxx>
---

v12 -> v13:
- Split "improve error handling" part out as a separate patch.

v11 -> v12 (new patch):
- Defer keeping TDMRs logic to this patch for better review
- Improved error handling logic (Nikolay/Kirill in patch 15)


---
arch/x86/virt/vmx/tdx/tdx.c | 24 +++++++++++-------------
1 file changed, 11 insertions(+), 13 deletions(-)

diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c
index b410fe66fd74..ea1363ceaa28 100644
--- a/arch/x86/virt/vmx/tdx/tdx.c
+++ b/arch/x86/virt/vmx/tdx/tdx.c
@@ -132,6 +132,8 @@ static DEFINE_MUTEX(tdx_module_lock);
/* All TDX-usable memory regions. Protected by mem_hotplug_lock. */
static LIST_HEAD(tdx_memlist);

+static struct tdmr_info_list tdx_tdmr_list;
+
/*
* Do the module global initialization if not done yet. It can be
* done on any cpu. It's always called with interrupts disabled.
@@ -1080,7 +1082,6 @@ static int init_tdmrs(struct tdmr_info_list *tdmr_list)
static int init_tdx_module(void)
{
struct tdsysinfo_struct *tdsysinfo;
- struct tdmr_info_list tdmr_list;
struct cmr_info *cmr_array;
int tdsysinfo_size;
int cmr_array_size;
@@ -1123,17 +1124,17 @@ static int init_tdx_module(void)
goto out_put_tdxmem;

/* Allocate enough space for constructing TDMRs */
- ret = alloc_tdmr_list(&tdmr_list, tdsysinfo);
+ ret = alloc_tdmr_list(&tdx_tdmr_list, tdsysinfo);
if (ret)
goto out_free_tdxmem;

/* Cover all TDX-usable memory regions in TDMRs */
- ret = construct_tdmrs(&tdx_memlist, &tdmr_list, tdsysinfo);
+ ret = construct_tdmrs(&tdx_memlist, &tdx_tdmr_list, tdsysinfo);
if (ret)
goto out_free_tdmrs;

/* Pass the TDMRs and the global KeyID to the TDX module */
- ret = config_tdx_module(&tdmr_list, tdx_global_keyid);
+ ret = config_tdx_module(&tdx_tdmr_list, tdx_global_keyid);
if (ret)
goto out_free_pamts;

@@ -1153,7 +1154,7 @@ static int init_tdx_module(void)
goto out_reset_pamts;

/* Initialize TDMRs to complete the TDX module initialization */
- ret = init_tdmrs(&tdmr_list);
+ ret = init_tdmrs(&tdx_tdmr_list);
out_reset_pamts:
if (ret) {
/*
@@ -1170,20 +1171,17 @@ static int init_tdx_module(void)
* back to normal. But do the conversion anyway here
* as suggested by the TDX spec.
*/
- tdmrs_reset_pamt_all(&tdmr_list);
+ tdmrs_reset_pamt_all(&tdx_tdmr_list);
}
out_free_pamts:
if (ret)
- tdmrs_free_pamt_all(&tdmr_list);
+ tdmrs_free_pamt_all(&tdx_tdmr_list);
else
pr_info("%lu KBs allocated for PAMT.\n",
- tdmrs_count_pamt_kb(&tdmr_list));
+ tdmrs_count_pamt_kb(&tdx_tdmr_list));
out_free_tdmrs:
- /*
- * Always free the buffer of TDMRs as they are only used during
- * module initialization.
- */
- free_tdmr_list(&tdmr_list);
+ if (ret)
+ free_tdmr_list(&tdx_tdmr_list);
out_free_tdxmem:
if (ret)
free_tdx_memlist(&tdx_memlist);
--
2.41.0