Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests: vDSO: check cpu caps before running chacha test

Some archs -- arm64 and s390x -- implemented chacha using instructions
that are available most places, but aren't always available. The kernel
handles this just fine, but the selftest does not. Check the hwcaps
before running, and skip the test if the cpu doesn't support it. As
well, on s390x, always emit the fallback instructions of an alternative
block, to ensure maximum compatibility.

Co-developed-by: Heiko Carstens <hca@linux.ibm.com>
Signed-off-by: Heiko Carstens <hca@linux.ibm.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>

+30
+10
tools/include/asm/alternative.h
··· 2 2 #ifndef _TOOLS_ASM_ALTERNATIVE_ASM_H 3 3 #define _TOOLS_ASM_ALTERNATIVE_ASM_H 4 4 5 + #if defined(__s390x__) 6 + #ifdef __ASSEMBLY__ 7 + .macro ALTERNATIVE oldinstr, newinstr, feature 8 + \oldinstr 9 + .endm 10 + #endif 11 + #else 12 + 5 13 /* Just disable it so we can build arch/x86/lib/memcpy_64.S for perf bench: */ 6 14 7 15 #define ALTERNATIVE # 16 + 17 + #endif 8 18 9 19 #endif
+20
tools/testing/selftests/vDSO/vdso_test_chacha.c
··· 5 5 6 6 #include <tools/le_byteshift.h> 7 7 #include <sys/random.h> 8 + #include <sys/auxv.h> 8 9 #include <string.h> 9 10 #include <stdint.h> 10 11 #include <stdbool.h> 11 12 #include "../kselftest.h" 13 + 14 + #if defined(__aarch64__) 15 + static bool cpu_has_capabilities(void) 16 + { 17 + return getauxval(AT_HWCAP) & HWCAP_ASIMD; 18 + } 19 + #elif defined(__s390x__) 20 + static bool cpu_has_capabilities(void) 21 + { 22 + return getauxval(AT_HWCAP) & HWCAP_S390_VXRS; 23 + } 24 + #else 25 + static bool cpu_has_capabilities(void) 26 + { 27 + return true; 28 + } 29 + #endif 12 30 13 31 static uint32_t rol32(uint32_t word, unsigned int shift) 14 32 { ··· 85 67 uint8_t output1[BLOCK_SIZE * BLOCKS], output2[BLOCK_SIZE * BLOCKS]; 86 68 87 69 ksft_print_header(); 70 + if (!cpu_has_capabilities()) 71 + ksft_exit_skip("Required CPU capabilities missing\n"); 88 72 ksft_set_plan(1); 89 73 90 74 for (unsigned int trial = 0; trial < TRIALS; ++trial) {