Skip to content

Commit 7006046

Browse files
kaihuanghansendc
authored andcommitted
x86/mce: Differentiate real hardware #MCs from TDX erratum ones
The first few generations of TDX hardware have an erratum. Triggering it in Linux requires some kind of kernel bug involving relatively exotic memory writes to TDX private memory and will manifest via spurious-looking machine checks when reading the affected memory. Make an effort to detect these TDX-induced machine checks and spit out a new blurb to dmesg so folks do not think their hardware is failing. == Background == Virtually all kernel memory accesses operations happen in full cachelines. In practice, writing a "byte" of memory usually reads a 64 byte cacheline of memory, modifies it, then writes the whole line back. Those operations do not trigger this problem. This problem is triggered by "partial" writes where a write transaction of less than cacheline lands at the memory controller. The CPU does these via non-temporal write instructions (like MOVNTI), or through UC/WC memory mappings. The issue can also be triggered away from the CPU by devices doing partial writes via DMA. == Problem == A partial write to a TDX private memory cacheline will silently "poison" the line. Subsequent reads will consume the poison and generate a machine check. According to the TDX hardware spec, neither of these things should have happened. To add insult to injury, the Linux machine code will present these as a literal "Hardware error" when they were, in fact, a software-triggered issue. == Solution == In the end, this issue is hard to trigger. Rather than do something rash (and incomplete) like unmap TDX private memory from the direct map, improve the machine check handler. Currently, the #MC handler doesn't distinguish whether the memory is TDX private memory or not but just dump, for instance, below message: [...] mce: [Hardware Error]: CPU 147: Machine Check Exception: f Bank 1: bd80000000100134 [...] mce: [Hardware Error]: RIP 10:<ffffffffadb69870> {__tlb_remove_page_size+0x10/0xa0} ... [...] mce: [Hardware Error]: Run the above through 'mcelog --ascii' [...] mce: [Hardware Error]: Machine check: Data load in unrecoverable area of kernel [...] Kernel panic - not syncing: Fatal local machine check Which says "Hardware Error" and "Data load in unrecoverable area of kernel". Ideally, it's better for the log to say "software bug around TDX private memory" instead of "Hardware Error". But in reality the real hardware memory error can happen, and sadly such software-triggered #MC cannot be distinguished from the real hardware error. Also, the error message is used by userspace tool 'mcelog' to parse, so changing the output may break userspace. So keep the "Hardware Error". The "Data load in unrecoverable area of kernel" is also helpful, so keep it too. Instead of modifying above error log, improve the error log by printing additional TDX related message to make the log like: ... [...] mce: [Hardware Error]: Machine check: Data load in unrecoverable area of kernel [...] mce: [Hardware Error]: Machine Check: TDX private memory error. Possible kernel bug. Adding this additional message requires determination of whether the memory page is TDX private memory. There is no existing infrastructure to do that. Add an interface to query the TDX module to fill this gap. == Impact == This issue requires some kind of kernel bug to trigger. TDX private memory should never be mapped UC/WC. A partial write originating from these mappings would require *two* bugs, first mapping the wrong page, then writing the wrong memory. It would also be detectable using traditional memory corruption techniques like DEBUG_PAGEALLOC. MOVNTI (and friends) could cause this issue with something like a simple buffer overrun or use-after-free on the direct map. It should also be detectable with normal debug techniques. The one place where this might get nasty would be if the CPU read data then wrote back the same data. That would trigger this problem but would not, for instance, set off mechanisms like slab redzoning because it doesn't actually corrupt data. With an IOMMU at least, the DMA exposure is similar to the UC/WC issue. TDX private memory would first need to be incorrectly mapped into the I/O space and then a later DMA to that mapping would actually cause the poisoning event. [ dhansen: changelog tweaks ] Signed-off-by: Kai Huang <kai.huang@intel.com> Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com> Reviewed-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Reviewed-by: Yuan Yao <yuan.yao@intel.com> Reviewed-by: Dave Hansen <dave.hansen@linux.intel.com> Reviewed-by: Tony Luck <tony.luck@intel.com> Link: https://lore.kernel.org/all/20231208170740.53979-18-dave.hansen%40intel.com
1 parent 1e536e1 commit 7006046

4 files changed

Lines changed: 133 additions & 0 deletions

File tree

arch/x86/include/asm/tdx.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
#ifndef __ASSEMBLY__
3434

35+
#include <uapi/asm/mce.h>
36+
3537
/*
3638
* Used by the #VE exception handler to gather the #VE exception
3739
* info from the TDX module. This is a software only structure
@@ -113,10 +115,12 @@ static inline u64 sc_retry(sc_func_t func, u64 fn,
113115
#define seamcall_saved_ret(_fn, _args) sc_retry(__seamcall_saved_ret, (_fn), (_args))
114116
int tdx_cpu_enable(void);
115117
int tdx_enable(void);
118+
const char *tdx_dump_mce_info(struct mce *m);
116119
#else
117120
static inline void tdx_init(void) { }
118121
static inline int tdx_cpu_enable(void) { return -ENODEV; }
119122
static inline int tdx_enable(void) { return -ENODEV; }
123+
static inline const char *tdx_dump_mce_info(struct mce *m) { return NULL; }
120124
#endif /* CONFIG_INTEL_TDX_HOST */
121125

122126
#endif /* !__ASSEMBLY__ */

arch/x86/kernel/cpu/mce/core.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#include <asm/mce.h>
5353
#include <asm/msr.h>
5454
#include <asm/reboot.h>
55+
#include <asm/tdx.h>
5556

5657
#include "internal.h"
5758

@@ -228,11 +229,20 @@ static void wait_for_panic(void)
228229
panic("Panicing machine check CPU died");
229230
}
230231

232+
static const char *mce_dump_aux_info(struct mce *m)
233+
{
234+
if (boot_cpu_has_bug(X86_BUG_TDX_PW_MCE))
235+
return tdx_dump_mce_info(m);
236+
237+
return NULL;
238+
}
239+
231240
static noinstr void mce_panic(const char *msg, struct mce *final, char *exp)
232241
{
233242
struct llist_node *pending;
234243
struct mce_evt_llist *l;
235244
int apei_err = 0;
245+
const char *memmsg;
236246

237247
/*
238248
* Allow instrumentation around external facilities usage. Not that it
@@ -283,6 +293,11 @@ static noinstr void mce_panic(const char *msg, struct mce *final, char *exp)
283293
}
284294
if (exp)
285295
pr_emerg(HW_ERR "Machine check: %s\n", exp);
296+
297+
memmsg = mce_dump_aux_info(final);
298+
if (memmsg)
299+
pr_emerg(HW_ERR "Machine check: %s\n", memmsg);
300+
286301
if (!fake_panic) {
287302
if (panic_timeout == 0)
288303
panic_timeout = mca_cfg.panic_timeout;

arch/x86/virt/vmx/tdx/tdx.c

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <linux/log2.h>
2828
#include <linux/acpi.h>
2929
#include <linux/suspend.h>
30+
#include <linux/acpi.h>
3031
#include <asm/page.h>
3132
#include <asm/special_insns.h>
3233
#include <asm/msr-index.h>
@@ -35,6 +36,7 @@
3536
#include <asm/tdx.h>
3637
#include <asm/intel-family.h>
3738
#include <asm/processor.h>
39+
#include <asm/mce.h>
3840
#include "tdx.h"
3941

4042
static u32 tdx_global_keyid __ro_after_init;
@@ -942,6 +944,13 @@ static int construct_tdmrs(struct list_head *tmb_list,
942944
if (ret)
943945
tdmrs_free_pamt_all(tdmr_list);
944946

947+
/*
948+
* The tdmr_info_list is read-only from here on out.
949+
* Ensure that these writes are seen by other CPUs.
950+
* Pairs with a smp_rmb() in is_pamt_page().
951+
*/
952+
smp_wmb();
953+
945954
return ret;
946955
}
947956

@@ -1235,6 +1244,106 @@ int tdx_enable(void)
12351244
}
12361245
EXPORT_SYMBOL_GPL(tdx_enable);
12371246

1247+
static bool is_pamt_page(unsigned long phys)
1248+
{
1249+
struct tdmr_info_list *tdmr_list = &tdx_tdmr_list;
1250+
int i;
1251+
1252+
/* Ensure that all remote 'tdmr_list' writes are visible: */
1253+
smp_rmb();
1254+
1255+
/*
1256+
* The TDX module is no longer returning TDX_SYS_NOT_READY and
1257+
* is initialized. The 'tdmr_list' was initialized long ago
1258+
* and is now read-only.
1259+
*/
1260+
for (i = 0; i < tdmr_list->nr_consumed_tdmrs; i++) {
1261+
unsigned long base, size;
1262+
1263+
tdmr_get_pamt(tdmr_entry(tdmr_list, i), &base, &size);
1264+
1265+
if (phys >= base && phys < (base + size))
1266+
return true;
1267+
}
1268+
1269+
return false;
1270+
}
1271+
1272+
/*
1273+
* Return whether the memory page at the given physical address is TDX
1274+
* private memory or not.
1275+
*
1276+
* This can be imprecise for two known reasons:
1277+
* 1. PAMTs are private memory and exist before the TDX module is
1278+
* ready and TDH_PHYMEM_PAGE_RDMD works. This is a relatively
1279+
* short window that occurs once per boot.
1280+
* 2. TDH_PHYMEM_PAGE_RDMD reflects the TDX module's knowledge of the
1281+
* page. However, the page can still cause #MC until it has been
1282+
* fully converted to shared using 64-byte writes like MOVDIR64B.
1283+
* Buggy hosts might still leave #MC-causing memory in place which
1284+
* this function can not detect.
1285+
*/
1286+
static bool paddr_is_tdx_private(unsigned long phys)
1287+
{
1288+
struct tdx_module_args args = {
1289+
.rcx = phys & PAGE_MASK,
1290+
};
1291+
u64 sret;
1292+
1293+
if (!boot_cpu_has(X86_FEATURE_TDX_HOST_PLATFORM))
1294+
return false;
1295+
1296+
/* Get page type from the TDX module */
1297+
sret = __seamcall_ret(TDH_PHYMEM_PAGE_RDMD, &args);
1298+
1299+
/*
1300+
* The SEAMCALL will not return success unless there is a
1301+
* working, "ready" TDX module. Assume an absence of TDX
1302+
* private pages until SEAMCALL is working.
1303+
*/
1304+
if (sret)
1305+
return false;
1306+
1307+
/*
1308+
* SEAMCALL was successful -- read page type (via RCX):
1309+
*
1310+
* - PT_NDA: Page is not used by the TDX module
1311+
* - PT_RSVD: Reserved for Non-TDX use
1312+
* - Others: Page is used by the TDX module
1313+
*
1314+
* Note PAMT pages are marked as PT_RSVD but they are also TDX
1315+
* private memory.
1316+
*/
1317+
switch (args.rcx) {
1318+
case PT_NDA:
1319+
return false;
1320+
case PT_RSVD:
1321+
return is_pamt_page(phys);
1322+
default:
1323+
return true;
1324+
}
1325+
}
1326+
1327+
/*
1328+
* Some TDX-capable CPUs have an erratum. A write to TDX private
1329+
* memory poisons that memory, and a subsequent read of that memory
1330+
* triggers #MC.
1331+
*
1332+
* Help distinguish erratum-triggered #MCs from a normal hardware one.
1333+
* Just print additional message to show such #MC may be result of the
1334+
* erratum.
1335+
*/
1336+
const char *tdx_dump_mce_info(struct mce *m)
1337+
{
1338+
if (!m || !mce_is_memory_error(m) || !mce_usable_address(m))
1339+
return NULL;
1340+
1341+
if (!paddr_is_tdx_private(m->addr))
1342+
return NULL;
1343+
1344+
return "TDX private memory error. Possible kernel bug.";
1345+
}
1346+
12381347
static __init int record_keyid_partitioning(u32 *tdx_keyid_start,
12391348
u32 *nr_tdx_keyids)
12401349
{

arch/x86/virt/vmx/tdx/tdx.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,18 @@
1414
/*
1515
* TDX module SEAMCALL leaf functions
1616
*/
17+
#define TDH_PHYMEM_PAGE_RDMD 24
1718
#define TDH_SYS_KEY_CONFIG 31
1819
#define TDH_SYS_INIT 33
1920
#define TDH_SYS_RD 34
2021
#define TDH_SYS_LP_INIT 35
2122
#define TDH_SYS_TDMR_INIT 36
2223
#define TDH_SYS_CONFIG 45
2324

25+
/* TDX page types */
26+
#define PT_NDA 0x0
27+
#define PT_RSVD 0x1
28+
2429
/*
2530
* Global scope metadata field ID.
2631
*

0 commit comments

Comments
 (0)