arm64: proton-pack: Add Spectre-BSE mitigation for Cortex-A7{2,3,5}
Subsystems (2)
cover arm64: proton-pack: Add Spectre-BSE mitigation for Cortex-A7{2,3,5}
Hello!
Spectre-BSE is a variant of Spectre-BHB that abuses a power-saving mode on some older cores to dodge the BHB mitigation applied to the branch predictor.
Only A72r0 actually needs anything doing - this is basically a bug in the published BHB mitigation sequence that was published for A72r0. This series moves A72r0 to use the WA1 firmware call for mitigation, and adds the necessary reporting parts for user-space to discover which parts of BHB/BSE are mitigated or vulnerable.
WA1 is used instead of WA3 which was new for BHB because we can't rely on hypervisors not to use the 'local' workaround, and for Spectre-BSE we don't need to worry about discovery via. (Which is why WA3 exists - for cores not vulnerable to the issue mititaged by WA1).
Arm's description of this vulnerability can be found here:
https://developer.arm.com/Arm%20Security%20Center/Spectre-BSE
This series is based on arm64/for-next/core, and can be retrieved from:
https://git.kernel.org/pub/scm/linux/kernel/git/morse/linux.git/log/?h=spectre_bse/v1
Backports of this version can also be found under spectre_bse/backports of the above repo.
Because this vulnerability is hard to expoit, but the cost of mitigating it is high - the mitigation is disabled by default. (see the last patch). To enable the mitigation, a command-line argument is needed:
'spectre_bse'.
This series adds more things to the vulnerability reporting file:
| morse@adam:~$ cat /sys/devices/system/cpu/vulnerabilities/spectre_v2 | Mitigation: CSV2, BHB, but not BSE
or:
| Mitigation: CSV2, BHB, BSE
(KVM maintainers are on CC as this changes which mitigation KVM enables,
and there is some hypervisor reasoning in the last patch)
Thanks,
James Morse (3):
arm64: proton-pack: Move the loop and firmware enable sequences into
helpers
arm64: proton-pack: Add Spectre-BSE mitigation for Cortex-A7{2,3,5}
arm64: proton-pack: Prefer WA1 for BHB on Cortex-A72 r0pX
arch/arm64/include/asm/assembler.h | 4 +- arch/arm64/include/asm/spectre.h | 1 + arch/arm64/kernel/proton-pack.c | 230 +++++++++++++++++++++++------ 3 files changed, 191 insertions(+), 44 deletions(-)
Signature · 1 line
2.39.2
PATCH 1/3 arm64: proton-pack: Move the loop and firmware enable sequences into helpers
A variant of Spectre-BHB needs to be able to tweak the way the BHB mitigation is selected. Move the enable routines for the loop and firmware vectors into helpers.
--- arch/arm64/kernel/proton-pack.c | 80 +++++++++++++++++++-------------- 1 file changed, 47 insertions(+), 33 deletions(-)
arch/arm64/kernel/proton-pack.c +47 -33
@@ -999,9 +999,53 @@ static int __init parse_spectre_bhb_param(char *str) }early_param ("nospectre_bhb" , parse_spectre_bhb_param); -void spectre_bhb_enable_mitigation (const struct arm64_cpu_capabilities *entry) +static void spectre_bhb_enable_fw_mitigation (void ) {bp_hardening_cb_t cpu_cb; +struct bp_hardening_data *data =this_cpu_ptr (&bp_hardening_data); + +/* +* Ensure KVM uses one of the spectre bp_hardening +* vectors. The indirect vector doesn't include the EL3 +* call, so needs upgrading to +* HYP_VECTOR_SPECTRE_INDIRECT. +*/ +if (!data->slot || data->slot ==HYP_VECTOR_INDIRECT ) + data->slot +=1 ; + +this_cpu_set_vectors (EL1_VECTOR_BHB_FW ); + +/* +* The WA3 call in the vectors supersedes the WA1 call +* made during context-switch. Uninstall any firmware +* bp_hardening callback. +*/ + cpu_cb =spectre_v2_get_sw_mitigation_cb (); +if (__this_cpu_read (bp_hardening_data.fn) != cpu_cb) +__this_cpu_write (bp_hardening_data.fn,NULL ); + +set_bit (BHB_FW , &system_bhb_mitigations); +} + +static void spectre_bhb_enable_loop_mitigation (void ) +{ +struct bp_hardening_data *data =this_cpu_ptr (&bp_hardening_data); + +/* +* Ensure KVM uses the indirect vector which will have the +* branchy-loop added. A57/A72-r0 will already have selected +* the spectre-indirect vector, which is sufficient for BHB +* too. +*/ +if (!data->slot) + data->slot =HYP_VECTOR_INDIRECT ; + +this_cpu_set_vectors (EL1_VECTOR_BHB_LOOP ); +set_bit (BHB_LOOP , &system_bhb_mitigations); +} + +void spectre_bhb_enable_mitigation (const struct arm64_cpu_capabilities *entry) +{enum mitigation_state fw_state, state =SPECTRE_VULNERABLE ;struct bp_hardening_data *data =this_cpu_ptr (&bp_hardening_data);
@@ -1029,43 +1073,13 @@ void spectre_bhb_enable_mitigation(const struct arm64_cpu_capabilities *entry) state =SPECTRE_MITIGATED ;set_bit (BHB_INSN , &system_bhb_mitigations); }else if (spectre_bhb_loop_affected (SCOPE_LOCAL_CPU )) { -/* -* Ensure KVM uses the indirect vector which will have the -* branchy-loop added. A57/A72-r0 will already have selected -* the spectre-indirect vector, which is sufficient for BHB -* too. -*/ -if (!data->slot) - data->slot =HYP_VECTOR_INDIRECT ; - -this_cpu_set_vectors (EL1_VECTOR_BHB_LOOP ); +spectre_bhb_enable_loop_mitigation (); state =SPECTRE_MITIGATED ; -set_bit (BHB_LOOP , &system_bhb_mitigations); }else if (is_spectre_bhb_fw_affected (SCOPE_LOCAL_CPU )) { fw_state =spectre_bhb_get_cpu_fw_mitigation_state ();if (fw_state ==SPECTRE_MITIGATED ) { -/* -* Ensure KVM uses one of the spectre bp_hardening -* vectors. The indirect vector doesn't include the EL3 -* call, so needs upgrading to -* HYP_VECTOR_SPECTRE_INDIRECT. -*/ -if (!data->slot || data->slot ==HYP_VECTOR_INDIRECT ) - data->slot +=1 ; - -this_cpu_set_vectors (EL1_VECTOR_BHB_FW ); - -/* -* The WA3 call in the vectors supersedes the WA1 call -* made during context-switch. Uninstall any firmware -* bp_hardening callback. -*/ - cpu_cb =spectre_v2_get_sw_mitigation_cb (); -if (__this_cpu_read (bp_hardening_data.fn) != cpu_cb) -__this_cpu_write (bp_hardening_data.fn,NULL ); - +spectre_bhb_enable_fw_mitigation (); state =SPECTRE_MITIGATED ; -set_bit (BHB_FW , &system_bhb_mitigations); } }
Signature · 1 line
2.39.2
PATCH 2/3 arm64: proton-pack: Add Spectre-BSE mitigation for Cortex-A7{2,3,5}
In reply to James MorseA group of older Cortex cores are affected by Spectre-BSE which on one core bypasses the existing Spectre-BHB mitigation.
The workaround is to use the Spectre-BHB 'arch_workaround_3' firmware call. For Cortex-A7{3,5}, this is the existing mitigation so only the reporting needs updating.
For Cortex-A72 if WA3 is not implemented, there is still a benefit from enabling the Spectre-BHB branchy-loop.
This leads to special-casing anything affected by Spectre-BSE to avoid enabling the branchy-loop, and instead only do this if the firmware WA3 call is not implemented. Such platforms will be reported as:
| Mitigation: Branch predictor hardening, BHB, but not BSE via the Spectre-v2 vulnerabilities file.
---
Nothing relies on the BSE entries being in the BHB firmware list, but this is done on the principle of least-surprise.
--- arch/arm64/include/asm/spectre.h | 1 + arch/arm64/kernel/proton-pack.c | 116 ++++++++++++++++++++++++++++++- 2 files changed, 114 insertions(+), 3 deletions(-)
arch/arm64/include/asm/spectre.h +1 -0
@@ -96,6 +96,7 @@ void spectre_v4_enable_task_mitigation(struct task_struct *tsk);enum mitigation_state arm64_get_meltdown_state (void );enum mitigation_state arm64_get_spectre_bhb_state (void ); +enum mitigation_state arm64_get_spectre_bse_state (void );bool is_spectre_bhb_affected (const struct arm64_cpu_capabilities *entry,int scope);u8 spectre_bhb_loop_affected (int scope);void spectre_bhb_enable_mitigation (const struct arm64_cpu_capabilities *__unused);
arch/arm64/kernel/proton-pack.c +113 -3
187 lines not on this page · raw message
Signature · 1 line
2.39.2
PATCH 3/3 arm64: proton-pack: Prefer WA1 for BHB on Cortex-A72 r0pX
In reply to James MorseCortex-A72 r0pX is affected by Spectre-BSE which bypasses the existing Spectre-BHB mitigation. As implemented by TFA, both WA1 and WA3 are sufficient to mitigate Spectre-BHB and Spectre-BSE on Cortex-A72 r0pX. The performance cost will be the same.
The incomplete mitigation is the branchy-loop that was previously used. It isn't possible to discover if a hypervisor implements WA3 using the branchy-loop, so WA3 can't be trusted for this CPU.
Instead, use WA1. This involves duplicating the BHB_FW bit in the mitigations bitmap and patching in the appropriate immediate to the mitigation sequence. If both WA3 and WA1 are selected, WA1 should take priority.
--- arch/arm64/include/asm/assembler.h | 4 ++- arch/arm64/kernel/proton-pack.c | 58 ++++++++++++++++++++---------- 2 files changed, 42 insertions(+), 20 deletions(-)
arch/arm64/include/asm/assembler.h +3 -1
@@ -838,7 +838,9 @@ alternative_cb_end#ifdef CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY stp x0, x1, [sp, #-16]! stp x2, x3, [sp, #-16]! - mov w0, #ARM_SMCCC_ARCH_WORKAROUND_3 +alternative_cb ARM64_ALWAYS_SYSTEM, spectre_bhb_patch_wa3 + mov w0, #ARM_SMCCC_ARCH_WORKAROUND_3 // Maybe patched to WA1 +alternative_cb_end alternative_cb ARM64_ALWAYS_SYSTEM, smccc_patch_fw_mitigation_conduit nop // Patched to SMC/HVC #0 alternative_cb_end
arch/arm64/kernel/proton-pack.c +39 -19
140 lines not on this page · raw message
Signature · 1 line
2.39.2
Hi,
Quoted 22 lines · James Morse
Hello!
Spectre-BSE is a variant of Spectre-BHB that abuses a power-saving mode on some older cores to dodge the BHB mitigation applied to the branch predictor.
Only A72r0 actually needs anything doing - this is basically a bug in the published BHB mitigation sequence that was published for A72r0. This series moves A72r0 to use the WA1 firmware call for mitigation, and adds the necessary reporting parts for user-space to discover which parts of BHB/BSE are mitigated or vulnerable.
WA1 is used instead of WA3 which was new for BHB because we can't rely on hypervisors not to use the 'local' workaround, and for Spectre-BSE we don't need to worry about discovery via. (Which is why WA3 exists - for cores not vulnerable to the issue mititaged by WA1).
Arm's description of this vulnerability can be found here:
https://developer.arm.com/Arm%20Security%20Center/Spectre-BSEThis series is based on arm64/for-next/core, and can be retrieved from:
https://git.kernel.org/pub/scm/linux/kernel/git/morse/linux.git/log/?h=spectre_bse/v1
Quoted 7 lines
Backports of this version can also be found under spectre_bse/backports of the above repo.
Because this vulnerability is hard to expoit, but the cost of mitigating it is high - the mitigation is disabled by default. (see the last patch). To enable the mitigation, a command-line argument is needed:
'spectre_bse'.
The Amazon Linux kernel team evaluated these patches on EC2 A1 instances running Amazon Linux 2 and UnixBench. We can confirm that patch impact is significant, especially for syscall overhead.
UnixBench results in comparison to disabled mitigations (AL2, kernel 5.15, EC2 A1.4xlarge instance):
Dhrystone 2 -- +0.01% 2prec Whetstone -- +0.01% Execl throughput -- +21.39% File Copy 1024/2000 -- +45.40% File Copy 256/500 -- +46.52% File Copy 4096/8000 -- +25.68% Pipe Throughput -- +51.46% Pipe based ctx switch -- +10.91% Process creation -- +4.35% Shell Scripts x1 -- +20.00% Shell Scripts x8 -- +26.68% System Call Overhead -- +55.82% Total Score -- +28.36%
Best,
Bjoern
Quoted 56 lines · Bjoern
Hi,
Quoted 23 lines · James Morse
Hello!
Spectre-BSE is a variant of Spectre-BHB that abuses a power-saving mode on some older cores to dodge the BHB mitigation applied to the branch predictor.
Only A72r0 actually needs anything doing - this is basically a bug in the published BHB mitigation sequence that was published for A72r0. This series moves A72r0 to use the WA1 firmware call for mitigation, and adds the necessary reporting parts for user-space to discover which parts of BHB/BSE are mitigated or vulnerable.
WA1 is used instead of WA3 which was new for BHB because we can't rely on hypervisors not to use the 'local' workaround, and for Spectre-BSE we don't need to worry about discovery via. (Which is why WA3 exists - for cores not vulnerable to the issue mititaged by WA1).
Arm's description of this vulnerability can be found here:
https://developer.arm.com/Arm%20Security%20Center/Spectre-BSEThis series is based on arm64/for-next/core, and can be retrieved from:
https://git.kernel.org/pub/scm/linux/kernel/git/morse/linux.git/log/?h=spectre_bse/v1Quoted 7 lines
Backports of this version can also be found under spectre_bse/backports of the above repo.
Because this vulnerability is hard to expoit, but the cost of mitigating it is high - the mitigation is disabled by default. (see the last patch). To enable the mitigation, a command-line argument is needed:
'spectre_bse'.The Amazon Linux kernel team evaluated these patches on EC2 A1 instances running Amazon Linux 2 and UnixBench. We can confirm that patch impact is significant, especially for syscall overhead.
UnixBench results in comparison to disabled mitigations (AL2, kernel 5.15, EC2 A1.4xlarge instance):
Dhrystone 2 -- +0.01% 2prec Whetstone -- +0.01% Execl throughput -- +21.39% File Copy 1024/2000 -- +45.40% File Copy 256/500 -- +46.52% File Copy 4096/8000 -- +25.68% Pipe Throughput -- +51.46% Pipe based ctx switch -- +10.91% Process creation -- +4.35% Shell Scripts x1 -- +20.00% Shell Scripts x8 -- +26.68% System Call Overhead -- +55.82% Total Score -- +28.36%
Quoted 3 lines
Best,
Bjoern
We also conducted full sized workload tests that we consider representative of common use cases for A1 instances. The data we see shows the impact can be significant depending on workload:
NGINX server configured as a load-balancer: -20% Memcached loaded so P99 response latency <10ms: -29% Memcached loaded so P99 response latency <1ms: -2% Wordpress blog server: -2%
Thanks,
Geoff
Hi again James,
Quoted 20 lines
Spectre-BSE is a variant of Spectre-BHB that abuses a power-saving mode on some older cores to dodge the BHB mitigation applied to the branch predictor.
Only A72r0 actually needs anything doing - this is basically a bug in the published BHB mitigation sequence that was published for A72r0. This series moves A72r0 to use the WA1 firmware call for mitigation, and adds the necessary reporting parts for user-space to discover which parts of BHB/BSE are mitigated or vulnerable.
WA1 is used instead of WA3 which was new for BHB because we can't rely on hypervisors not to use the 'local' workaround, and for Spectre-BSE we don't need to worry about discovery via. (Which is why WA3 exists - for cores not vulnerable to the issue mititaged by WA1).
Arm's description of this vulnerability can be found here:
https://developer.arm.com/Arm%20Security%20Center/Spectre-BSEThis series is based on arm64/for-next/core, and can be retrieved from:
https://git.kernel.org/pub/scm/linux/kernel/git/morse/linux.git/log/?h=spectre_bse/v1
Quoted 7 lines
Backports of this version can also be found under spectre_bse/backports of the above repo.
Because this vulnerability is hard to expoit, but the cost of mitigating it is high - the mitigation is disabled by default. (see the last patch). To enable the mitigation, a command-line argument is needed:
'spectre_bse'.
I see that the patch to introduce this parameter is in the git series you linked above, but it is on top of the three patches in this series. Did you intend to send it as patch 4/4?
Best regards,
Bjoern
Hi Bjoern,
Quoted 23 lines · Bjoern
Quoted 23 lines
Spectre-BSE is a variant of Spectre-BHB that abuses a power-saving mode on some older cores to dodge the BHB mitigation applied to the branch predictor.
Only A72r0 actually needs anything doing - this is basically a bug in the published BHB mitigation sequence that was published for A72r0. This series moves A72r0 to use the WA1 firmware call for mitigation, and adds the necessary reporting parts for user-space to discover which parts of BHB/BSE are mitigated or vulnerable.
WA1 is used instead of WA3 which was new for BHB because we can't rely on hypervisors not to use the 'local' workaround, and for Spectre-BSE we don't need to worry about discovery via. (Which is why WA3 exists - for cores not vulnerable to the issue mititaged by WA1).
Arm's description of this vulnerability can be found here:
https://developer.arm.com/Arm%20Security%20Center/Spectre-BSEThis series is based on arm64/for-next/core, and can be retrieved from:
https://git.kernel.org/pub/scm/linux/kernel/git/morse/linux.git/log/?h=spectre_bse/v1Backports of this version can also be found under spectre_bse/backports of the above repo.
Quoted 7 lines
Quoted 4 lines
Because this vulnerability is hard to expoit, but the cost of mitigating it is high - the mitigation is disabled by default. (see the last patch). To enable the mitigation, a command-line argument is needed:
'spectre_bse'.I see that the patch to introduce this parameter is in the git series you linked above, but it is on top of the three patches in this series. Did you intend to send it as patch 4/4?
Yes. Oops. Thanks for point that out!
Thanks,
James
[PATCH 4/3] arm64: proton-pack: Disable Spectre-BSE mitigation by default
In reply to James MorseThe Spectre-BSE vulnerability is hard to exploit, requiring abuse of the branch history (which is small on these cores), confusing the branch predictor, and finding a useful gadget to expose data.
Depending on the frequency of syscalls, the cost of mitigating this is high - and platforms cannot chose between Spectre-v2 mitigation and Spectre-BSE mitigation as they use the same firmware call.
Disable the Spectre-BSE mitigation by default, and add a command line option to enable it in environments where this vulnerability is considered a problem.
--- Documentation/admin-guide/kernel-parameters.txt | 5 +++++ arch/arm64/kernel/proton-pack.c | 11 ++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-)
Documentation/admin-guide/kernel-parameters.txt +5 -0
@@ -6250,6 +6250,11 @@ may still be vulnerable to syscall attacks. off - Disable the mitigation. + spectre_bse [ARM64,EARLY] Enable mitigations for Spectre-BSE (branch + status eviction) on vulnerable CPUs. Mitigations are disabled + by default due to the difficulty of exploitation and the cost + of mitigation. + spectre_v2= [X86,EARLY] Control mitigation of Spectre variant 2 (indirect branch speculation) vulnerability. The default operation protects the kernel from
arch/arm64/kernel/proton-pack.c +10 -1
@@ -1088,6 +1088,15 @@ static int __init parse_spectre_bhb_param(char *str) }early_param ("nospectre_bhb" , parse_spectre_bhb_param); +/* Spectre-BSE mitigations are disabled by default */ +static bool __read_mostly __spectre_bse; +static int __init parse_spectre_bse_param (char *str) +{ + __spectre_bse = true; +return 0 ; +} +early_param ("spectre_bse" , parse_spectre_bse_param); +static void spectre_bhb_enable_fw_mitigation (enum bhb_mitigation_bits fw_wa) {bp_hardening_cb_t cpu_cb;
@@ -1185,7 +1194,7 @@ void spectre_bhb_enable_mitigation(const struct arm64_cpu_capabilities *entry)/* Spectre BSE needs to upgrade the BHB mitigation to use firmware */ if (bse_upgrade_loop_mitigation) { bse_state =spectre_bse_get_cpu_fw_mitigation_state (BHB_FW_WA1 ); -if (bse_state ==SPECTRE_MITIGATED ) { +if (bse_state ==SPECTRE_MITIGATED && __spectre_bse) {/* * For affected cores the firmware implementions of WA1 * and WA3 are both sufficient for BSE, but what about
Signature · 1 line
2.39.2