x86/amd: Address AMD erratum #1485
PATCH v2 1/1 x86/amd: Address AMD erratum #1485
Fix adapted off Linux's mailing list:
https://lore.kernel.org/lkml/[email protected]/T/#u
---
v2:
* Removed v1/patch1, as it proved to be contentious
* Also changed is_zen[34]_uarch() into a heuristic check only
* Streamlined comments and commit message
--- xen/arch/x86/cpu/amd.c | 8 ++++++++ xen/arch/x86/include/asm/amd.h | 5 +++++ xen/arch/x86/include/asm/msr-index.h | 1 + 3 files changed, 14 insertions(+)
xen/arch/x86/cpu/amd.c +8 -0
@@ -1167,6 +1167,14 @@ static void cf_check init_amd(struct cpuinfo_x86 *c)if (c->x86 ==0x10 )__clear_bit (X86_FEATURE_MONITOR , c->x86_capability); +/* Fix for AMD erratum #1485 */ +if (!cpu_has_hypervisor && c->x86 ==0x19 &&is_zen4_uarch ()) { +rdmsrl (MSR_AMD64_BP_CFG , value); +#define ZEN4_BP_CFG_SHARED_BTB_FIX (1ull << 5) +wrmsrl (MSR_AMD64_BP_CFG , + value |ZEN4_BP_CFG_SHARED_BTB_FIX ); + } +if (!cpu_has_amd_erratum (c,AMD_ERRATUM_121 )) opt_allow_unsafe =1 ;else if (opt_allow_unsafe <0 )
xen/arch/x86/include/asm/amd.h +5 -0
@@ -145,11 +145,16 @@* Hygon (Fam18h) but without simple model number rules. Instead, use STIBP * as a heuristic that distinguishes the two. * +* For Zen3 and Zen4 (Fam19h) the heuristic is the presence of AutoIBRS, as +* it's Zen4-specific. +* * The caller is required to perform the appropriate vendor/family checks * first. */ #define is_zen1_uarch () (!boot_cpu_has(X86_FEATURE_AMD_STIBP))#define is_zen2_uarch () boot_cpu_has(X86_FEATURE_AMD_STIBP) +#define is_zen3_uarch () (!boot_cpu_has(X86_FEATURE_AUTO_IBRS)) +#define is_zen4_uarch () boot_cpu_has(X86_FEATURE_AUTO_IBRS)struct cpuinfo_x86 ;int cpu_has_amd_erratum (const struct cpuinfo_x86 *,int , ...);
xen/arch/x86/include/asm/msr-index.h +1 -0
@@ -403,6 +403,7 @@#define MSR_AMD64_DE_CFG 0xc0011029#define AMD64_DE_CFG_LFENCE_SERIALISE (_AC(1, ULL) << 1)#define MSR_AMD64_EX_CFG 0xc001102c +#define MSR_AMD64_BP_CFG 0xc001102e#define MSR_AMD64_DE_CFG2 0xc00110e3#define MSR_AMD64_DR0_ADDRESS_MASK 0xc0011027
Signature · 1 line
2.34.1
Quoted 5 lines · Alejandro Vallejo
xen/arch/x86/cpu/amd.c +0
-0
xen/arch/x86/cpu/amd.c static void cf_check init_amd(struct cpuinfo_x86 *c) · line 1175
Quoted 9 lines
xen/arch/x86/cpu/amd.c +6
-0@@ -1167,6 +1167,14 @@ static void cf_check init_amd(struct cpuinfo_x86 *c)if (c->x86 ==0x10 )__clear_bit (X86_FEATURE_MONITOR , c->x86_capability); +/* Fix for AMD erratum #1485 */ +if (!cpu_has_hypervisor && c->x86 ==0x19 &&is_zen4_uarch ()) { +rdmsrl (MSR_AMD64_BP_CFG , value); +#define ZEN4_BP_CFG_SHARED_BTB_FIX (1ull << 5) +wrmsrl (MSR_AMD64_BP_CFG , + value |ZEN4_BP_CFG_SHARED_BTB_FIX );
A #define indented like that is weird. I tend to either opencode it directly in the "value |" expression, or have a local variable called chickenbit.
This will surely be a core scope MSR rather than thread scope, at which point it the write ought to be conditional on seeing the chickenbit clear (hence needing to refer to the value at least twice, so use a local variable).
It probably also wants a note about non-atomic RMW, and how it's safe in practice. (See the Zenbleed comment).
Otherwise, LGTM.
As this is just cribbing from an existing example, I'm happy to adjust on commit, but it's probably better to double check in the PPR and retest.
~Andrew
Quoted 9 lines · Andrew Cooper
Quoted 8 lines · Alejandro Vallejo
xen/arch/x86/cpu/amd.c +0
-0@@ -1167,6 +1167,14 @@ static void cf_check init_amd(struct cpuinfo_x86 *c)if (c->x86 ==0x10 )__clear_bit (X86_FEATURE_MONITOR , c->x86_capability);
Quoted 10 lines
xen/arch/x86/cpu/amd.c static void cf_check init_amd(struct cpuinfo_x86 *c) · line 1175
Quoted 6 lines
xen/arch/x86/cpu/amd.c +6
-0+/* Fix for AMD erratum #1485 */ +if (!cpu_has_hypervisor && c->x86 ==0x19 &&is_zen4_uarch ()) { +rdmsrl (MSR_AMD64_BP_CFG , value); +#define ZEN4_BP_CFG_SHARED_BTB_FIX (1ull << 5) +wrmsrl (MSR_AMD64_BP_CFG , + value |ZEN4_BP_CFG_SHARED_BTB_FIX );A #define indented like that is weird. I tend to either opencode it directly in the "value |" expression, or have a local variable called chickenbit.
Ok, I don't mind either way. I'll just go with the chickenbit.
Quoted 2 lines
This will surely be a core scope MSR rather than thread scope,
It is, though I doubt it matters a whole lot. The writes are consistent anyway.
Quoted 4 lines
at which
point the write ought to be conditional on seeing the chickenbit clear (hence needing to refer to the value at least twice, so use a local variable).
I have serious doubts such a conditional would do much for boot times, but sure.
Quoted 3 lines
It probably also wants a note about non-atomic RMW, and how it's safe in practice. (See the Zenbleed comment).
Fair enough.
Quoted 7 lines
Otherwise, LGTM.
As this is just cribbing from an existing example, I'm happy to adjust on commit, but it's probably better to double check in the PPR and retest.
~Andrew
Let me send a v3 after re-testing with the conditional in place
Thanks,
Alejandro
Hi Alejandro,
The original e-mail didn't yet reach my inbox. So answering here as there are enough context.
Quoted 10 lines · Alejandro Vallejo
Quoted 9 lines · Andrew Cooper
Quoted 8 lines · Alejandro Vallejo
xen/arch/x86/cpu/amd.c +0
-0@@ -1167,6 +1167,14 @@ static void cf_check init_amd(struct cpuinfo_x86 *c)if (c->x86 ==0x10 )__clear_bit (X86_FEATURE_MONITOR , c->x86_capability);
Quoted 4 lines
Quoted 4 lines
xen/arch/x86/cpu/amd.c static void cf_check init_amd(struct cpuinfo_x86 *c) · line 1173
Quoted 4 lines
xen/arch/x86/cpu/amd.c +4
-0+/* Fix for AMD erratum #1485 */ +if (!cpu_has_hypervisor && c->x86 ==0x19 &&is_zen4_uarch ()) { +rdmsrl (MSR_AMD64_BP_CFG , value); +#define ZEN4_BP_CFG_SHARED_BTB_FIX (1ull << 5)
You are introducing a violation to MISRA Rule 7.3 [1] which was accepted recently. You want to use 1ULL rather than the lower case version.
[1] https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_07_03.c
Cheers,
Signature · 1 line
Julien Grall
Hi,
Quoted 23 lines · Julien Grall
Hi Alejandro,
The original e-mail didn't yet reach my inbox. So answering here as there are enough context.
Quoted 13 lines · Alejandro Vallejo
Quoted 12 lines · Andrew Cooper
xen/arch/x86/cpu/amd.c static void cf_check init_amd(struct cpuinfo_x86 *c) · line 1172
Quoted 11 lines · Alejandro Vallejo
xen/arch/x86/cpu/amd.c +4
-0@@ -1167,6 +1167,14 @@ static void cf_check init_amd(struct cpuinfo_x86 *c)if (c->x86 ==0x10 )__clear_bit (X86_FEATURE_MONITOR , c->x86_capability); +/* Fix for AMD erratum #1485 */ +if (!cpu_has_hypervisor && c->x86 ==0x19 &&is_zen4_uarch ()) { +rdmsrl (MSR_AMD64_BP_CFG , value); +#define ZEN4_BP_CFG_SHARED_BTB_FIX (1ull << 5)You are introducing a violation to MISRA Rule 7.3 [1] which was accepted recently. You want to use 1ULL rather than the lower case version.
Quoted 6 lines
[1] https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_07_03.c
Cheers,
Signature · 1 line
Julien Grall
Oh, I didn't know about that one. Fair enough.
Thanks,
Alejandro
Quoted 5 lines · Alejandro Vallejo
Quoted 2 lines · Andrew Cooper
On 13/10/2023 9:18 pm, Alejandro Vallejo wrote:
This will surely be a core scope MSR rather than thread scope,It is, though I doubt it matters a whole lot. The writes are consistent anyway.
This happens to be true because you introduced the first use of the MSR.
It ceases to be true for the next chickenbit in this MSR, which is why ...
Quoted 6 lines
Quoted 4 lines
at which
point the write ought to be conditional on seeing the chickenbit clear (hence needing to refer to the value at least twice, so use a local variable).I have serious doubts such a conditional would do much for boot times, but sure.
... this is not about boot time. It's about avoiding an unnecessary non-atomic action.
(TBH, when the second chickenbit comes, it's all suspect anyway and probably needs to end up like the pre-SSBD handling, which has horrifying complexity.)
~Andrew