Skip to content

Commit b5ae12e

Browse files
committed
lib/crc32: expose whether the lib is really optimized at runtime
Make the CRC32 library export a function crc32_optimizations() which returns flags that indicate which CRC32 functions are actually executing optimized code at runtime. This will be used to determine whether the crc32[c]-$arch shash algorithms should be registered in the crypto API. btrfs could also start using these flags instead of the hack that it currently uses where it parses the crypto_shash_driver_name. Reviewed-by: Ard Biesheuvel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Eric Biggers <[email protected]>
1 parent d36cebe commit b5ae12e

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

arch/arm64/lib/crc32-glue.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,15 @@ u32 __pure crc32_be_arch(u32 crc, const u8 *p, size_t len)
8585
}
8686
EXPORT_SYMBOL(crc32_be_arch);
8787

88+
u32 crc32_optimizations(void)
89+
{
90+
if (alternative_has_cap_likely(ARM64_HAS_CRC32))
91+
return CRC32_LE_OPTIMIZATION |
92+
CRC32_BE_OPTIMIZATION |
93+
CRC32C_OPTIMIZATION;
94+
return 0;
95+
}
96+
EXPORT_SYMBOL(crc32_optimizations);
97+
8898
MODULE_LICENSE("GPL");
8999
MODULE_DESCRIPTION("arm64-optimized CRC32 functions");

arch/riscv/lib/crc32-riscv.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,5 +297,15 @@ u32 __pure crc32_be_arch(u32 crc, const u8 *p, size_t len)
297297
}
298298
EXPORT_SYMBOL(crc32_be_arch);
299299

300+
u32 crc32_optimizations(void)
301+
{
302+
if (riscv_has_extension_likely(RISCV_ISA_EXT_ZBC))
303+
return CRC32_LE_OPTIMIZATION |
304+
CRC32_BE_OPTIMIZATION |
305+
CRC32C_OPTIMIZATION;
306+
return 0;
307+
}
308+
EXPORT_SYMBOL(crc32_optimizations);
309+
300310
MODULE_LICENSE("GPL");
301311
MODULE_DESCRIPTION("Accelerated CRC32 implementation with Zbc extension");

include/linux/crc32.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ static inline u32 __pure __crc32c_le(u32 crc, const u8 *p, size_t len)
3737
return crc32c_le_base(crc, p, len);
3838
}
3939

40+
/*
41+
* crc32_optimizations() returns flags that indicate which CRC32 library
42+
* functions are using architecture-specific optimizations. Unlike
43+
* IS_ENABLED(CONFIG_CRC32_ARCH) it takes into account the different CRC32
44+
* variants and also whether any needed CPU features are available at runtime.
45+
*/
46+
#define CRC32_LE_OPTIMIZATION BIT(0) /* crc32_le() is optimized */
47+
#define CRC32_BE_OPTIMIZATION BIT(1) /* crc32_be() is optimized */
48+
#define CRC32C_OPTIMIZATION BIT(2) /* __crc32c_le() is optimized */
49+
#if IS_ENABLED(CONFIG_CRC32_ARCH)
50+
u32 crc32_optimizations(void);
51+
#else
52+
static inline u32 crc32_optimizations(void) { return 0; }
53+
#endif
54+
4055
/**
4156
* crc32_le_combine - Combine two crc32 check values into one. For two
4257
* sequences of bytes, seq1 and seq2 with lengths len1

0 commit comments

Comments
 (0)