Skip to content

Commit

Permalink
lib/crc32: expose whether the lib is really optimized at runtime
Browse files Browse the repository at this point in the history
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 <ardb@kernel.org>
Link: https://lore.kernel.org/r/20241202010844.144356-4-ebiggers@kernel.org
Signed-off-by: Eric Biggers <ebiggers@google.com>
  • Loading branch information
Eric Biggers committed Dec 2, 2024
1 parent d36cebe commit b5ae12e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
10 changes: 10 additions & 0 deletions arch/arm64/lib/crc32-glue.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,15 @@ u32 __pure crc32_be_arch(u32 crc, const u8 *p, size_t len)
}
EXPORT_SYMBOL(crc32_be_arch);

u32 crc32_optimizations(void)
{
if (alternative_has_cap_likely(ARM64_HAS_CRC32))
return CRC32_LE_OPTIMIZATION |
CRC32_BE_OPTIMIZATION |
CRC32C_OPTIMIZATION;
return 0;
}
EXPORT_SYMBOL(crc32_optimizations);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("arm64-optimized CRC32 functions");
10 changes: 10 additions & 0 deletions arch/riscv/lib/crc32-riscv.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,5 +297,15 @@ u32 __pure crc32_be_arch(u32 crc, const u8 *p, size_t len)
}
EXPORT_SYMBOL(crc32_be_arch);

u32 crc32_optimizations(void)
{
if (riscv_has_extension_likely(RISCV_ISA_EXT_ZBC))
return CRC32_LE_OPTIMIZATION |
CRC32_BE_OPTIMIZATION |
CRC32C_OPTIMIZATION;
return 0;
}
EXPORT_SYMBOL(crc32_optimizations);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Accelerated CRC32 implementation with Zbc extension");
15 changes: 15 additions & 0 deletions include/linux/crc32.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ static inline u32 __pure __crc32c_le(u32 crc, const u8 *p, size_t len)
return crc32c_le_base(crc, p, len);
}

/*
* crc32_optimizations() returns flags that indicate which CRC32 library
* functions are using architecture-specific optimizations. Unlike
* IS_ENABLED(CONFIG_CRC32_ARCH) it takes into account the different CRC32
* variants and also whether any needed CPU features are available at runtime.
*/
#define CRC32_LE_OPTIMIZATION BIT(0) /* crc32_le() is optimized */
#define CRC32_BE_OPTIMIZATION BIT(1) /* crc32_be() is optimized */
#define CRC32C_OPTIMIZATION BIT(2) /* __crc32c_le() is optimized */
#if IS_ENABLED(CONFIG_CRC32_ARCH)
u32 crc32_optimizations(void);
#else
static inline u32 crc32_optimizations(void) { return 0; }
#endif

/**
* crc32_le_combine - Combine two crc32 check values into one. For two
* sequences of bytes, seq1 and seq2 with lengths len1
Expand Down

0 comments on commit b5ae12e

Please sign in to comment.