From: Jim Mattson <jmattson@google.com>
To: bp@alien8.de, tglx@kernel.org, x86@kernel.org, rafael@kernel.org,
viresh.kumar@linaro.org, yosry@kernel.org,
andrew.cooper3@citrix.com, ludloff@gmail.com
Cc: linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org,
Jim Mattson <jmattson@google.com>
Subject: [PATCH v3 2/4] x86/mce/inject: Avoid racy updates to MSR_K7_HWCR during MCE injection
Date: Thu, 18 Jun 2026 15:45:25 -0700 [thread overview]
Message-ID: <20260618224527.1506419-3-jmattson@google.com> (raw)
In-Reply-To: <20260618224527.1506419-1-jmattson@google.com>
MCE injection performs a read-modify-write of MSR_K7_HWCR as two
independent crosscalls (via toggle_hw_mce_inject) wrapping the actual MSR
writes (prepare_msrs). Another HWCR update on the target CPU could be lost
if it occurred between these crosscalls.
Introduce ipi_inject_mce() to perform the entire injection sequence (toggle
ON, write MSRs, toggle OFF) in a single IPI callback, ensuring atomicity on
the target CPU.
For the local CPU initialization path in check_hw_inj_possible(), use
amd_update_hwcr() directly to avoid IPI overhead and ensure safe updates.
Remove toggle_hw_mce_inject() as it is no longer used.
Opportunistically, replace the open-coded BIT(18) with a new
MSR_K7_HWCR_MCSTATUSWREN macro.
Fixes: 21690934d934 ("EDAC, mce_amd_inj: Enable direct writes to MCE MSRs")
Link: https://sashiko.dev/#/patchset/20260612215729.1532175-1-jmattson%40google.com?part=2
Assisted-by: Gemini:gemini-3.5-pro
Signed-off-by: Jim Mattson <jmattson@google.com>
---
arch/x86/include/asm/msr-index.h | 2 ++
arch/x86/kernel/cpu/mce/inject.c | 46 ++++++++++----------------------
2 files changed, 16 insertions(+), 32 deletions(-)
diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h
index 86554de9a3f5..29c4abade594 100644
--- a/arch/x86/include/asm/msr-index.h
+++ b/arch/x86/include/asm/msr-index.h
@@ -896,6 +896,8 @@
#define MSR_K7_HWCR 0xc0010015
#define MSR_K7_HWCR_SMMLOCK_BIT 0
#define MSR_K7_HWCR_SMMLOCK BIT_ULL(MSR_K7_HWCR_SMMLOCK_BIT)
+#define MSR_K7_HWCR_MCSTATUSWREN_BIT 18
+#define MSR_K7_HWCR_MCSTATUSWREN BIT_ULL(MSR_K7_HWCR_MCSTATUSWREN_BIT)
#define MSR_K7_HWCR_IRPERF_EN_BIT 30
#define MSR_K7_HWCR_IRPERF_EN BIT_ULL(MSR_K7_HWCR_IRPERF_EN_BIT)
#define MSR_K7_HWCR_CPUID_USER_DIS_BIT 35
diff --git a/arch/x86/kernel/cpu/mce/inject.c b/arch/x86/kernel/cpu/mce/inject.c
index 6f8a49d8baeb..1e017d8e23e4 100644
--- a/arch/x86/kernel/cpu/mce/inject.c
+++ b/arch/x86/kernel/cpu/mce/inject.c
@@ -31,6 +31,7 @@
#include <asm/mce.h>
#include <asm/msr.h>
#include <asm/nmi.h>
+#include <asm/processor.h>
#include <asm/smp.h>
#include "internal.h"
@@ -311,30 +312,6 @@ static struct notifier_block inject_nb = {
.notifier_call = mce_inject_raise,
};
-/*
- * Caller needs to be make sure this cpu doesn't disappear
- * from under us, i.e.: get_cpu/put_cpu.
- */
-static int toggle_hw_mce_inject(unsigned int cpu, bool enable)
-{
- struct msr val;
- int err;
-
- err = rdmsrq_on_cpu(cpu, MSR_K7_HWCR, &val.q);
- if (err) {
- pr_err("%s: error reading HWCR\n", __func__);
- return err;
- }
-
- enable ? (val.l |= BIT(18)) : (val.l &= ~BIT(18));
-
- err = wrmsrq_on_cpu(cpu, MSR_K7_HWCR, val.q);
- if (err)
- pr_err("%s: error writing HWCR\n", __func__);
-
- return err;
-}
-
static int __set_inj(const char *buf)
{
int i;
@@ -500,6 +477,12 @@ static void prepare_msrs(void *info)
wrmsrq(MSR_IA32_MCx_MISC(b), m.misc);
}
}
+static void ipi_inject_mce(void *info)
+{
+ amd_update_hwcr(MSR_K7_HWCR_MCSTATUSWREN_BIT, true);
+ prepare_msrs(info);
+ amd_update_hwcr(MSR_K7_HWCR_MCSTATUSWREN_BIT, false);
+}
static void do_inject(void)
{
@@ -556,13 +539,13 @@ static void do_inject(void)
if (!cpu_online(cpu))
goto err;
- toggle_hw_mce_inject(cpu, true);
-
i_mce.mcgstatus = mcg_status;
i_mce.inject_flags = inj_type;
- smp_call_function_single(cpu, prepare_msrs, &i_mce, 0);
- toggle_hw_mce_inject(cpu, false);
+ if (smp_call_function_single(cpu, ipi_inject_mce, &i_mce, 1)) {
+ pr_err("%s: Error injecting MCE on CPU %d\n", __func__, cpu);
+ goto err;
+ }
switch (inj_type) {
case DFR_INT_INJ:
@@ -727,7 +710,6 @@ static void __init debugfs_init(void)
static void check_hw_inj_possible(void)
{
- int cpu;
u8 bank;
/*
@@ -737,7 +719,7 @@ static void check_hw_inj_possible(void)
if (!cpu_feature_enabled(X86_FEATURE_SMCA))
return;
- cpu = get_cpu();
+ get_cpu();
for (bank = 0; bank < MAX_NR_BANKS; ++bank) {
u64 status = MCI_STATUS_VAL, ipid;
@@ -747,7 +729,7 @@ static void check_hw_inj_possible(void)
if (!ipid)
continue;
- toggle_hw_mce_inject(cpu, true);
+ amd_update_hwcr(MSR_K7_HWCR_MCSTATUSWREN_BIT, true);
wrmsrq_safe(mca_msr_reg(bank, MCA_STATUS), status);
rdmsrq_safe(mca_msr_reg(bank, MCA_STATUS), &status);
@@ -759,7 +741,7 @@ static void check_hw_inj_possible(void)
"Try using APEI EINJ instead.\n");
}
- toggle_hw_mce_inject(cpu, false);
+ amd_update_hwcr(MSR_K7_HWCR_MCSTATUSWREN_BIT, false);
break;
}
--
2.55.0.rc0.799.gd6f94ed593-goog
next prev parent reply other threads:[~2026-06-18 22:45 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-18 22:45 [PATCH v3 0/4] Fix racy and incorrect updates to MSR_K7_HWCR Jim Mattson
2026-06-18 22:45 ` [PATCH v3 1/4] x86/CPU/AMD: Avoid racy updates to MSR_K7_HWCR in set_cpuid_faulting() Jim Mattson
2026-06-18 22:45 ` Jim Mattson [this message]
2026-06-18 22:45 ` [PATCH v3 3/4] cpufreq: ACPI: Use IPI to update boost MSR in cpufreq_boost_down_prep() Jim Mattson
2026-06-18 22:45 ` [PATCH v3 4/4] cpufreq: ACPI: Use amd_update_hwcr() for MSR_K7_HWCR updates Jim Mattson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260618224527.1506419-3-jmattson@google.com \
--to=jmattson@google.com \
--cc=andrew.cooper3@citrix.com \
--cc=bp@alien8.de \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=ludloff@gmail.com \
--cc=rafael@kernel.org \
--cc=tglx@kernel.org \
--cc=viresh.kumar@linaro.org \
--cc=x86@kernel.org \
--cc=yosry@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox