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

Kai Huang
2023-11-09T11:58:09Z

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. Keep
TDMRs when module initialization is successful so that they can be used
to find PAMTs.

Signed-off-by: Kai Huang <kai.huang@intel.com>
Reviewed-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
Reviewed-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
---

v14 -> v15:
- No change

v13 -> v14:
- "Change to keep" -> "Keep" (Kirill)
- Add Kirill/Rick's tags

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 ac47d58f8c74..753e435a3040 100644
--- a/arch/x86/virt/vmx/tdx/tdx.c
+++ b/arch/x86/virt/vmx/tdx/tdx.c
@@ -44,6 +44,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;
+
typedef void (*sc_err_func_t)(u64 fn, u64 err, struct tdx_module_args *args);

static inline void seamcall_err(u64 fn, u64 err, struct tdx_module_args *args)
@@ -1059,7 +1061,6 @@ static int init_tdmrs(struct tdmr_info_list *tdmr_list)
static int init_tdx_module(void)
{
struct tdx_tdmr_sysinfo tdmr_sysinfo;
- struct tdmr_info_list tdmr_list;
int ret;

/*
@@ -1083,17 +1084,17 @@ static int init_tdx_module(void)
goto out_free_tdxmem;

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

/* Cover all TDX-usable memory regions in TDMRs */
- ret = construct_tdmrs(&tdx_memlist, &tdmr_list, &tdmr_sysinfo);
+ ret = construct_tdmrs(&tdx_memlist, &tdx_tdmr_list, &tdmr_sysinfo);
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;

@@ -1113,7 +1114,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) {
/*
@@ -1130,20 +1131,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