Skip to content

Commit 006c077

Browse files
yghannamsuryasaimadhu
authored andcommitted
x86/mce: Handle varying MCA bank counts
Linux reads MCG_CAP[Count] to find the number of MCA banks visible to a CPU. Currently, this number is the same for all CPUs and a warning is shown if there is a difference. The number of banks is overwritten with the MCG_CAP[Count] value of each following CPU that boots. According to the Intel SDM and AMD APM, the MCG_CAP[Count] value gives the number of banks that are available to a "processor implementation". The AMD BKDGs/PPRs further clarify that this value is per core. This value has historically been the same for every core in the system, but that is not an architectural requirement. Future AMD systems may have different MCG_CAP[Count] values per core, so the assumption that all CPUs will have the same MCG_CAP[Count] value will no longer be valid. Also, the first CPU to boot will allocate the struct mce_banks[] array using the number of banks based on its MCG_CAP[Count] value. The machine check handler and other functions use the global number of banks to iterate and index into the mce_banks[] array. So it's possible to use an out-of-bounds index on an asymmetric system where a following CPU sees a MCG_CAP[Count] value greater than its predecessors. Thus, allocate the mce_banks[] array to the maximum number of banks. This will avoid the potential out-of-bounds index since the value of mca_cfg.banks is capped to MAX_NR_BANKS. Set the value of mca_cfg.banks equal to the max of the previous value and the value for the current CPU. This way mca_cfg.banks will always represent the max number of banks detected on any CPU in the system. This will ensure that all CPUs will access all the banks that are visible to them. A CPU that can access fewer than the max number of banks will find the registers of the extra banks to be read-as-zero. Furthermore, print the resulting number of MCA banks in use. Do this in mcheck_late_init() so that the final value is printed after all CPUs have been initialized. Finally, get bank count from target CPU when doing injection with mce-inject module. [ bp: Remove out-of-bounds example, passify and cleanup commit message. ] Signed-off-by: Yazen Ghannam <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Cc: "H. Peter Anvin" <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: linux-edac <[email protected]> Cc: Pu Wen <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Tony Luck <[email protected]> Cc: Vishal Verma <[email protected]> Cc: x86-ml <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent f19501a commit 006c077

File tree

2 files changed

+14
-22
lines changed

2 files changed

+14
-22
lines changed

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

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,13 +1481,12 @@ EXPORT_SYMBOL_GPL(mce_notify_irq);
14811481
static int __mcheck_cpu_mce_banks_init(void)
14821482
{
14831483
int i;
1484-
u8 num_banks = mca_cfg.banks;
14851484

1486-
mce_banks = kcalloc(num_banks, sizeof(struct mce_bank), GFP_KERNEL);
1485+
mce_banks = kcalloc(MAX_NR_BANKS, sizeof(struct mce_bank), GFP_KERNEL);
14871486
if (!mce_banks)
14881487
return -ENOMEM;
14891488

1490-
for (i = 0; i < num_banks; i++) {
1489+
for (i = 0; i < MAX_NR_BANKS; i++) {
14911490
struct mce_bank *b = &mce_banks[i];
14921491

14931492
b->ctl = -1ULL;
@@ -1501,28 +1500,19 @@ static int __mcheck_cpu_mce_banks_init(void)
15011500
*/
15021501
static int __mcheck_cpu_cap_init(void)
15031502
{
1504-
unsigned b;
15051503
u64 cap;
1504+
u8 b;
15061505

15071506
rdmsrl(MSR_IA32_MCG_CAP, cap);
15081507

15091508
b = cap & MCG_BANKCNT_MASK;
1510-
if (!mca_cfg.banks)
1511-
pr_info("CPU supports %d MCE banks\n", b);
1512-
1513-
if (b > MAX_NR_BANKS) {
1514-
pr_warn("Using only %u machine check banks out of %u\n",
1515-
MAX_NR_BANKS, b);
1509+
if (WARN_ON_ONCE(b > MAX_NR_BANKS))
15161510
b = MAX_NR_BANKS;
1517-
}
15181511

1519-
/* Don't support asymmetric configurations today */
1520-
WARN_ON(mca_cfg.banks != 0 && b != mca_cfg.banks);
1521-
mca_cfg.banks = b;
1512+
mca_cfg.banks = max(mca_cfg.banks, b);
15221513

15231514
if (!mce_banks) {
15241515
int err = __mcheck_cpu_mce_banks_init();
1525-
15261516
if (err)
15271517
return err;
15281518
}
@@ -2481,6 +2471,8 @@ EXPORT_SYMBOL_GPL(mcsafe_key);
24812471

24822472
static int __init mcheck_late_init(void)
24832473
{
2474+
pr_info("Using %d MCE banks\n", mca_cfg.banks);
2475+
24842476
if (mca_cfg.recovery)
24852477
static_branch_inc(&mcsafe_key);
24862478

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@
4646
static struct mce i_mce;
4747
static struct dentry *dfs_inj;
4848

49-
static u8 n_banks;
50-
5149
#define MAX_FLAG_OPT_SIZE 4
5250
#define NBCFG 0x44
5351

@@ -570,9 +568,15 @@ static void do_inject(void)
570568
static int inj_bank_set(void *data, u64 val)
571569
{
572570
struct mce *m = (struct mce *)data;
571+
u8 n_banks;
572+
u64 cap;
573+
574+
/* Get bank count on target CPU so we can handle non-uniform values. */
575+
rdmsrl_on_cpu(m->extcpu, MSR_IA32_MCG_CAP, &cap);
576+
n_banks = cap & MCG_BANKCNT_MASK;
573577

574578
if (val >= n_banks) {
575-
pr_err("Non-existent MCE bank: %llu\n", val);
579+
pr_err("MCA bank %llu non-existent on CPU%d\n", val, m->extcpu);
576580
return -EINVAL;
577581
}
578582

@@ -665,10 +669,6 @@ static struct dfs_node {
665669
static int __init debugfs_init(void)
666670
{
667671
unsigned int i;
668-
u64 cap;
669-
670-
rdmsrl(MSR_IA32_MCG_CAP, cap);
671-
n_banks = cap & MCG_BANKCNT_MASK;
672672

673673
dfs_inj = debugfs_create_dir("mce-inject", NULL);
674674
if (!dfs_inj)

0 commit comments

Comments
 (0)