arm64/efi: Remove unneeded SVE/SME fallback preserve/store handling

Since commit 7137a203b251 ("arm64/fpsimd: Permit kernel mode NEON with
IRQs off"), the only condition under which the fallback path is taken
for FP/SIMD preserve/restore across a EFI runtime call is when it is
called from hardirq or NMI context.

In practice, this only happens when the EFI pstore driver is called to
dump the kernel log buffer into a EFI variable under a panic, oops or
emergency_restart() condition, and none of these can be expected to
result in a return to user space for the task in question.

This means that the existing EFI-specific logic for preserving and
restoring SVE/SME state is pointless, and can be removed.

Instead, kill the task, so that an exceedingly unlikely inadvertent
return to user space does not proceed with a corrupted FP/SIMD state.
Also, retain the preserve and restore of the base FP/SIMD state, as that
might belong to kernel mode use of FP/SIMD. (Note that EFI runtime calls
are never invoked reentrantly, even in this case, and so any interrupted
kernel mode FP/SIMD usage will be unrelated to EFI)

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>

authored by Ard Biesheuvel and committed by Catalin Marinas 63de2b38 eb972eab

Changed files
+20 -110
arch
arm64
kernel
+20 -110
arch/arm64/kernel/fpsimd.c
··· 180 180 set_default_vl(ARM64_VEC_SVE, val); 181 181 } 182 182 183 - static u8 *efi_sve_state; 184 - 185 - #else /* ! CONFIG_ARM64_SVE */ 186 - 187 - /* Dummy declaration for code that will be optimised out: */ 188 - extern u8 *efi_sve_state; 189 - 190 183 #endif /* ! CONFIG_ARM64_SVE */ 191 184 192 185 #ifdef CONFIG_ARM64_SME ··· 1088 1095 return 0; 1089 1096 } 1090 1097 1091 - static void __init sve_efi_setup(void) 1092 - { 1093 - int max_vl = 0; 1094 - int i; 1095 - 1096 - if (!IS_ENABLED(CONFIG_EFI)) 1097 - return; 1098 - 1099 - for (i = 0; i < ARRAY_SIZE(vl_info); i++) 1100 - max_vl = max(vl_info[i].max_vl, max_vl); 1101 - 1102 - /* 1103 - * alloc_percpu() warns and prints a backtrace if this goes wrong. 1104 - * This is evidence of a crippled system and we are returning void, 1105 - * so no attempt is made to handle this situation here. 1106 - */ 1107 - if (!sve_vl_valid(max_vl)) 1108 - goto fail; 1109 - 1110 - efi_sve_state = kmalloc(SVE_SIG_REGS_SIZE(sve_vq_from_vl(max_vl)), 1111 - GFP_KERNEL); 1112 - if (!efi_sve_state) 1113 - goto fail; 1114 - 1115 - return; 1116 - 1117 - fail: 1118 - panic("Cannot allocate memory for EFI SVE save/restore"); 1119 - } 1120 - 1121 1098 void cpu_enable_sve(const struct arm64_cpu_capabilities *__always_unused p) 1122 1099 { 1123 1100 write_sysreg(read_sysreg(CPACR_EL1) | CPACR_EL1_ZEN_EL1EN, CPACR_EL1); ··· 1148 1185 if (sve_max_virtualisable_vl() < sve_max_vl()) 1149 1186 pr_warn("%s: unvirtualisable vector lengths present\n", 1150 1187 info->name); 1151 - 1152 - sve_efi_setup(); 1153 1188 } 1154 1189 1155 1190 /* ··· 1908 1947 #ifdef CONFIG_EFI 1909 1948 1910 1949 static struct user_fpsimd_state efi_fpsimd_state; 1911 - static bool efi_fpsimd_state_used; 1912 - static bool efi_sve_state_used; 1913 - static bool efi_sm_state; 1914 1950 1915 1951 /* 1916 1952 * EFI runtime services support functions ··· 1934 1976 if (may_use_simd()) { 1935 1977 kernel_neon_begin(&efi_fpsimd_state); 1936 1978 } else { 1937 - WARN_ON(preemptible()); 1938 - 1939 1979 /* 1940 - * If !efi_sve_state, SVE can't be in use yet and doesn't need 1941 - * preserving: 1980 + * We are running in hardirq or NMI context, and the only 1981 + * legitimate case where this might happen is when EFI pstore 1982 + * is attempting to record the system's dying gasps into EFI 1983 + * variables. This could be due to an oops, a panic or a call 1984 + * to emergency_restart(), and in none of those cases, we can 1985 + * expect the current task to ever return to user space again, 1986 + * or for the kernel to resume any normal execution, for that 1987 + * matter (an oops in hardirq context triggers a panic too). 1988 + * 1989 + * Therefore, there is no point in attempting to preserve any 1990 + * SVE/SME state here. On the off chance that we might have 1991 + * ended up here for a different reason inadvertently, kill the 1992 + * task and preserve/restore the base FP/SIMD state, which 1993 + * might belong to kernel mode FP/SIMD. 1942 1994 */ 1943 - if (system_supports_sve() && efi_sve_state != NULL) { 1944 - bool ffr = true; 1945 - u64 svcr; 1946 - 1947 - efi_sve_state_used = true; 1948 - 1949 - if (system_supports_sme()) { 1950 - svcr = read_sysreg_s(SYS_SVCR); 1951 - 1952 - efi_sm_state = svcr & SVCR_SM_MASK; 1953 - 1954 - /* 1955 - * Unless we have FA64 FFR does not 1956 - * exist in streaming mode. 1957 - */ 1958 - if (!system_supports_fa64()) 1959 - ffr = !(svcr & SVCR_SM_MASK); 1960 - } 1961 - 1962 - sve_save_state(efi_sve_state + sve_ffr_offset(sve_max_vl()), 1963 - &efi_fpsimd_state.fpsr, ffr); 1964 - 1965 - if (system_supports_sme()) 1966 - sysreg_clear_set_s(SYS_SVCR, 1967 - SVCR_SM_MASK, 0); 1968 - 1969 - } else { 1970 - fpsimd_save_state(&efi_fpsimd_state); 1971 - } 1972 - 1973 - efi_fpsimd_state_used = true; 1995 + pr_warn_ratelimited("Calling EFI runtime from %s context\n", 1996 + in_nmi() ? "NMI" : "hardirq"); 1997 + force_signal_inject(SIGKILL, SI_KERNEL, 0, 0); 1998 + fpsimd_save_state(&efi_fpsimd_state); 1974 1999 } 1975 2000 } 1976 2001 ··· 1965 2024 if (!system_supports_fpsimd()) 1966 2025 return; 1967 2026 1968 - if (!efi_fpsimd_state_used) { 2027 + if (may_use_simd()) { 1969 2028 kernel_neon_end(&efi_fpsimd_state); 1970 2029 } else { 1971 - if (system_supports_sve() && efi_sve_state_used) { 1972 - bool ffr = true; 1973 - 1974 - /* 1975 - * Restore streaming mode; EFI calls are 1976 - * normal function calls so should not return in 1977 - * streaming mode. 1978 - */ 1979 - if (system_supports_sme()) { 1980 - if (efi_sm_state) { 1981 - sysreg_clear_set_s(SYS_SVCR, 1982 - 0, 1983 - SVCR_SM_MASK); 1984 - 1985 - /* 1986 - * Unless we have FA64 FFR does not 1987 - * exist in streaming mode. 1988 - */ 1989 - if (!system_supports_fa64()) 1990 - ffr = false; 1991 - } 1992 - } 1993 - 1994 - sve_load_state(efi_sve_state + sve_ffr_offset(sve_max_vl()), 1995 - &efi_fpsimd_state.fpsr, ffr); 1996 - 1997 - efi_sve_state_used = false; 1998 - } else { 1999 - fpsimd_load_state(&efi_fpsimd_state); 2000 - } 2001 - 2002 - efi_fpsimd_state_used = false; 2030 + fpsimd_load_state(&efi_fpsimd_state); 2003 2031 } 2004 2032 } 2005 2033