[PATCH 1/2] x86/MCE: Extend size of the MCE Records pool

From: Avadhut Naik
Date: Wed Feb 07 2024 - 17:58:32 EST


Currently, 2 pages are allocated for the MCE Records pool during system
bootup. Records of MCEs (struct mce) occurring on the system are added to
the pool through mce_gen_pool_add() in MC context. These records are then
decoded later, in process context through notifier chains.

However, on systems with high CPU count, the 2 pages allocated for the
pool might not be sufficient in some instances. Successive MCEs received
back-to-back, before they are decoded through mce_gen_pool_process(), will
result in the pool getting exhausted. Consequently, some MCE records will
be missed. The issue further intensifies since the amount of memory
associated with a system typically increases with the CPU count, thereby,
increasing the probability of MCEs being received.

The limit of 2 pages for the MCE records pool was set more than 8 years
ago and has not been revised till date. The CPU count and the amount of
memory associated with a system however, have increased enormously since
then. Additionally, the size of MCE Records (struct mce) too has increased
from 88 bytes to 124 bytes.

Extend the size of MCE Records pool to better serve modern systems. The
increase in size depends on the CPU count of the system. Currently, since
size of struct mce is 124 bytes, each logical CPU of the system will have
space for at least 2 MCE records available in the pool. To get around the
allocation woes during early boot time, the same is undertaken using
late_initcall().

Signed-off-by: Avadhut Naik <avadhut.naik@xxxxxxx>
---
arch/x86/kernel/cpu/mce/core.c | 3 +++
arch/x86/kernel/cpu/mce/genpool.c | 22 ++++++++++++++++++++++
arch/x86/kernel/cpu/mce/internal.h | 1 +
3 files changed, 26 insertions(+)

diff --git a/arch/x86/kernel/cpu/mce/core.c b/arch/x86/kernel/cpu/mce/core.c
index b5cc557cfc37..5d6d7994d549 100644
--- a/arch/x86/kernel/cpu/mce/core.c
+++ b/arch/x86/kernel/cpu/mce/core.c
@@ -2901,6 +2901,9 @@ static int __init mcheck_late_init(void)
if (mca_cfg.recovery)
enable_copy_mc_fragile();

+ if (mce_gen_pool_extend())
+ pr_info("Couldn't extend MCE records pool!\n");
+
mcheck_debugfs_init();

/*
diff --git a/arch/x86/kernel/cpu/mce/genpool.c b/arch/x86/kernel/cpu/mce/genpool.c
index fbe8b61c3413..aed01612d342 100644
--- a/arch/x86/kernel/cpu/mce/genpool.c
+++ b/arch/x86/kernel/cpu/mce/genpool.c
@@ -20,6 +20,7 @@
* 2 pages to save MCE events for now (~80 MCE records at most).
*/
#define MCE_POOLSZ (2 * PAGE_SIZE)
+#define CPU_GEN_MEMSZ 256

static struct gen_pool *mce_evt_pool;
static LLIST_HEAD(mce_event_llist);
@@ -116,6 +117,27 @@ int mce_gen_pool_add(struct mce *mce)
return 0;
}

+int mce_gen_pool_extend(void)
+{
+ unsigned long addr, len;
+ int ret = -ENOMEM;
+ u32 num_threads;
+
+ num_threads = num_present_cpus();
+ len = PAGE_ALIGN(num_threads * CPU_GEN_MEMSZ);
+ addr = (unsigned long)kzalloc(len, GFP_KERNEL);
+
+ if (!addr)
+ goto out;
+
+ ret = gen_pool_add(mce_evt_pool, addr, len, -1);
+ if (ret)
+ kfree((void *)addr);
+
+out:
+ return ret;
+}
+
static int mce_gen_pool_create(void)
{
struct gen_pool *tmpp;
diff --git a/arch/x86/kernel/cpu/mce/internal.h b/arch/x86/kernel/cpu/mce/internal.h
index 01f8f03969e6..81e35ec58ebc 100644
--- a/arch/x86/kernel/cpu/mce/internal.h
+++ b/arch/x86/kernel/cpu/mce/internal.h
@@ -33,6 +33,7 @@ void mce_gen_pool_process(struct work_struct *__unused);
bool mce_gen_pool_empty(void);
int mce_gen_pool_add(struct mce *mce);
int mce_gen_pool_init(void);
+int mce_gen_pool_extend(void);
struct llist_node *mce_gen_pool_prepare_records(void);

int mce_severity(struct mce *a, struct pt_regs *regs, char **msg, bool is_excp);
--
2.34.1