[PATCH v2 0/3] Fix three racy updates to MSR_K7_HWCR
From: Jim Mattson
Date: Fri Jun 12 2026 - 17:57:45 EST
I was backporting commit 65f55a301766 ("x86/CPU/AMD: Add CPUID faulting
support") to a local branch based on Linux v6.12, when our internal Sashiko
asked:
> Can this corrupt MSR_K7_HWCR? disable_cpuid()->set_cpuid_faulting() is
> called with preemption disabled, but interrupts are still enabled. Since
> msr_set_bit() performs a read-modify-write without disabling interrupts,
> if an IPI arrives between the read and write and modifies MSR_K7_HWCR
> (e.g. acpi-cpufreq toggling Core Performance Boost), the IPI's update
> will be lost.
To confirm that this wasn't just AI slop, I set up an empirical test on a
Turin system. First, I replaced the amd-pstate cpufreq driver with
acpi-cpufreq. Then I ran a test program, where one thread repeatedly reads
CPU0's HWCR, toggles /sys/devices/system/cpu/cpufreq/boost, reads CPU0's
HWCR again, and then verifies that the CPB_DIS bit has flipped. A second
thread, pinned to CPU0, repeatedly calls arch_prctl(ARCH_SET_CPUID, <val>),
where <val> alternates between 0 and 1. With the second thread running, the
first thread soon fails the verification step, indicating that the CPB_DIS
bit change is, in fact, lost.
Per Boris's review of v1, this version hoists the HWCR update logic out
into a new helper, amd_update_hwcr(), which performs the read-modify-write
with interrupts disabled, and converts the three runtime
(non-initialization) HWCR read-modify-write sites to use it:
* set_cpuid_faulting(), fixing the race demonstrated above (patch 1);
* toggle_hw_mce_inject(), which previously performed the
read-modify-write as two independent crosscalls (patch 2);
* boost_set_msr() in acpi-cpufreq, whose process-context invocation on
the cpufreq policy teardown path can race with an HWCR update made by
the MCE injector's crosscall (patch 3).
Initialization-time HWCR updates are left alone for now, to avoid excessive
churn.
v1 -> v2:
- Add some of the cover letter details to the first patch
- Hoist the HWCR update logic out into amd_update_hwcr() [Boris]
- Use the helper in toggle_hw_mce_inject(), collapsing the split
read/write crosscalls into one
- Use the helper in boost_set_msr() [Boris]
v1: https://lore.kernel.org/all/20260609211611.466231-1-jmattson@xxxxxxxxxx/
Jim Mattson (3):
x86/CPU/AMD: Avoid racy updates to MSR_K7_HWCR in set_cpuid_faulting()
x86/mce/inject: Avoid racy updates to MSR_K7_HWCR in
toggle_hw_mce_inject()
cpufreq: ACPI: Avoid racy updates to MSR_K7_HWCR in boost_set_msr()
arch/x86/include/asm/msr-index.h | 3 +++
arch/x86/include/asm/processor.h | 2 ++
arch/x86/kernel/cpu/amd.c | 42 ++++++++++++++++++++++++++++++++
arch/x86/kernel/cpu/mce/inject.c | 34 +++++++++++++++++++-------
arch/x86/kernel/process.c | 4 +--
drivers/cpufreq/acpi-cpufreq.c | 7 +++---
6 files changed, 78 insertions(+), 14 deletions(-)
base-commit: 2b414a95b8f7307d42173ba9e580d6d3e2bcbfce
--
2.54.0.1136.gdb2ca164c4-goog