Fix racy and incorrect updates to MSR_K7_HWCR
Get series with b4
b4 am [email protected]Versions newest first · linked by author and title
- v3 Latest4 patches 7 repliesIn review
- v23 patches 4 repliesSuperseded
- v11 patch 3 repliesSuperseded
Changes in v3 from the cover letter
- Add WARN_ON_ONCE(in_nmi()) to the helper to ensure NMI safety [Yosry].
- Redesign amd_update_hwcr() to be a simple bit-based wrapper [Yosry].
- Fix cpufreq_boost_down_prep() to target the correct CPU via IPI, resolving the wrong-CPU update and Intel preemption/migration races [Sashiko].
- Drop the Fixes tag from patch 4, since the race is now fixed in patch 3.
Patches in v3 No applies recorded yet
| # | Subject and files | Size | Replies | Applied |
|---|---|---|---|---|
| 1/4 | x86/CPU/AMD: Avoid racy updates to MSR_K7_HWCR in set_cpuid_faulting()arch/ arch/ arch/ | +41 | 0 replies | |
| 2/4 | x86/mce/inject: Avoid racy updates to MSR_K7_HWCR during MCE injectionarch/ arch/ | +16 | 0 replies | |
| 3/4 | cpufreq: ACPI: Use IPI to update boost MSR in cpufreq_boost_down_prep()drivers/ | +4 | 6 replies | |
| 4/4 | cpufreq: ACPI: Use amd_update_hwcr() for MSR_K7_HWCR updatesdrivers/ | +1 | 1 reply |
Reviews trailers per patch
1 of 4 patches has a Reviewed-by or Acked-by.
| Patch | Rafael J. Wysocki (Intel) |
|---|---|
| 1/4 | |
| 2/4 | |
| 3/4 | |
| 4/4 | A-b |
Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.
Cover letter [PATCH v3 0/4] Fix racy and incorrect updates to MSR_K7_HWCR
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:
Quoted 6 lines
Can this corrupt
MSR_K7_HWCR? disable_cpuid()->set_cpuid_faulting() is called with preemption disabled, but interrupts are still enabled. Sincemsr_set_bit()performs a read-modify-write without disabling interrupts, if an IPI arrives between the read and write and modifiesMSR_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.
Andrew Cooper raised a valid concern regarding the core-scoped nature of MSR_K7_HWCR on some AMD CPUs. When the MSR is shared between SMT siblings, a cross-CPU race is possible if SMT threads concurrently modify the MSR. Centralizing all runtime updates through the amd_update_hwcr() helper ensures that if explicit serialization is desired, it can be implemented transparently in a single place. Note that on recent AMD CPUs, the MSR is thread-scoped.
Google's internal Sashiko highlighted a separate, pre-existing architectural issue in acpi-cpufreq that Borislav and I briefly touched on:
during shared policy teardown (e.g., driver unload), acpi_cpufreq_cpu_exit() is only executed for the final CPU in the policy. As a result, cpufreq_boost_down_prep() only restores the boost-disable MSR bit on that single CPU, leaving all other sibling CPUs that shared the policy with the disable bit still set. On Intel platforms, where the turbo-disable MSR bit acts as a logical OR across the package, this leaves the entire package permanently boost-disabled after the driver is unloaded. This issue out of scope for this series, as it requires a broader redesign of cpufreq policy teardown coordination, and should be addressed separately.
v2 -> v3:
- Add WARN_ON_ONCE(in_nmi()) to the helper to ensure NMI safety [Yosry].
- Redesign amd_update_hwcr() to be a simple bit-based wrapper [Yosry].
- Fix cpufreq_boost_down_prep() to target the correct CPU via IPI, resolving the wrong-CPU update and Intel preemption/migration races [Sashiko].
- Drop the Fixes tag from patch 4, since the race is now fixed in patch 3.
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
Jim Mattson (4):
x86/CPU/AMD: Avoid racy updates to MSR_K7_HWCR in set_cpuid_faulting() x86/mce/inject: Avoid racy updates to MSR_K7_HWCR during MCE injection
cpufreq: ACPI: Use IPI to update boost MSR in
cpufreq_boost_down_prep()
cpufreq: ACPI: Use amd_update_hwcr() for MSR_K7_HWCR updates
arch/x86/include/asm/msr-index.h | 2 ++ arch/x86/include/asm/processor.h | 2 ++ arch/x86/kernel/cpu/amd.c | 38 ++++++++++++++++++++++++++ arch/x86/kernel/cpu/mce/inject.c | 46 ++++++++++---------------------- arch/x86/kernel/process.c | 5 +--- drivers/cpufreq/acpi-cpufreq.c | 11 ++++---- 6 files changed, 62 insertions(+), 42 deletions(-)
base-commit: 83f1454877cc292b88baf13c829c16ce6937d120