Skip to content

Commit 110628e

Browse files
author
Eric Biggers
committed
lib/crc: x86: Reorganize crc-pclmul static_call initialization
Reorganize the crc-pclmul static_call initialization to place more of the logic in the *_mod_init_arch() functions instead of in the INIT_CRC_PCLMUL macro. This provides the flexibility to do more than a single static_call update for each CPU feature check. Right away, optimize crc64_mod_init_arch() to check the CPU features just once instead of twice, doing both the crc64_msb and crc64_lsb static_call updates together. A later commit will also use this to initialize an additional static_key when crc32_lsb_vpclmul_avx512() is enabled. Acked-by: Ard Biesheuvel <ardb@kernel.org> Link: https://lore.kernel.org/r/20250719224938.126512-2-ebiggers@kernel.org Signed-off-by: Eric Biggers <ebiggers@kernel.org>
1 parent 9b0236f commit 110628e

4 files changed

Lines changed: 44 additions & 21 deletions

File tree

lib/crc/x86/crc-pclmul-template.h

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,20 @@ crc_t prefix##_vpclmul_avx512(crc_t crc, const u8 *p, size_t len, \
2525
const void *consts_ptr); \
2626
DEFINE_STATIC_CALL(prefix##_pclmul, prefix##_pclmul_sse)
2727

28-
#define INIT_CRC_PCLMUL(prefix) \
29-
do { \
30-
if (boot_cpu_has(X86_FEATURE_VPCLMULQDQ) && \
31-
boot_cpu_has(X86_FEATURE_AVX2) && \
32-
cpu_has_xfeatures(XFEATURE_MASK_YMM, NULL)) { \
33-
if (boot_cpu_has(X86_FEATURE_AVX512BW) && \
34-
boot_cpu_has(X86_FEATURE_AVX512VL) && \
35-
!boot_cpu_has(X86_FEATURE_PREFER_YMM) && \
36-
cpu_has_xfeatures(XFEATURE_MASK_AVX512, NULL)) { \
37-
static_call_update(prefix##_pclmul, \
38-
prefix##_vpclmul_avx512); \
39-
} else { \
40-
static_call_update(prefix##_pclmul, \
41-
prefix##_vpclmul_avx2); \
42-
} \
43-
} \
44-
} while (0)
28+
static inline bool have_vpclmul(void)
29+
{
30+
return boot_cpu_has(X86_FEATURE_VPCLMULQDQ) &&
31+
boot_cpu_has(X86_FEATURE_AVX2) &&
32+
cpu_has_xfeatures(XFEATURE_MASK_YMM, NULL);
33+
}
34+
35+
static inline bool have_avx512(void)
36+
{
37+
return boot_cpu_has(X86_FEATURE_AVX512BW) &&
38+
boot_cpu_has(X86_FEATURE_AVX512VL) &&
39+
!boot_cpu_has(X86_FEATURE_PREFER_YMM) &&
40+
cpu_has_xfeatures(XFEATURE_MASK_AVX512, NULL);
41+
}
4542

4643
/*
4744
* Call a [V]PCLMULQDQ optimized CRC function if the data length is at least 16

lib/crc/x86/crc-t10dif.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ static inline void crc_t10dif_mod_init_arch(void)
2323
{
2424
if (boot_cpu_has(X86_FEATURE_PCLMULQDQ)) {
2525
static_branch_enable(&have_pclmulqdq);
26-
INIT_CRC_PCLMUL(crc16_msb);
26+
if (have_vpclmul()) {
27+
if (have_avx512())
28+
static_call_update(crc16_msb_pclmul,
29+
crc16_msb_vpclmul_avx512);
30+
else
31+
static_call_update(crc16_msb_pclmul,
32+
crc16_msb_vpclmul_avx2);
33+
}
2734
}
2835
}

lib/crc/x86/crc32.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,15 @@ static inline void crc32_mod_init_arch(void)
7777
static_branch_enable(&have_crc32);
7878
if (boot_cpu_has(X86_FEATURE_PCLMULQDQ)) {
7979
static_branch_enable(&have_pclmulqdq);
80-
INIT_CRC_PCLMUL(crc32_lsb);
80+
if (have_vpclmul()) {
81+
if (have_avx512()) {
82+
static_call_update(crc32_lsb_pclmul,
83+
crc32_lsb_vpclmul_avx512);
84+
} else {
85+
static_call_update(crc32_lsb_pclmul,
86+
crc32_lsb_vpclmul_avx2);
87+
}
88+
}
8189
}
8290
}
8391

lib/crc/x86/crc64.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,18 @@ static inline void crc64_mod_init_arch(void)
3131
{
3232
if (boot_cpu_has(X86_FEATURE_PCLMULQDQ)) {
3333
static_branch_enable(&have_pclmulqdq);
34-
INIT_CRC_PCLMUL(crc64_msb);
35-
INIT_CRC_PCLMUL(crc64_lsb);
34+
if (have_vpclmul()) {
35+
if (have_avx512()) {
36+
static_call_update(crc64_msb_pclmul,
37+
crc64_msb_vpclmul_avx512);
38+
static_call_update(crc64_lsb_pclmul,
39+
crc64_lsb_vpclmul_avx512);
40+
} else {
41+
static_call_update(crc64_msb_pclmul,
42+
crc64_msb_vpclmul_avx2);
43+
static_call_update(crc64_lsb_pclmul,
44+
crc64_lsb_vpclmul_avx2);
45+
}
46+
}
3647
}
3748
}

0 commit comments

Comments
 (0)