From 24ec095295cfe060b00cd54cf24b4a5753dfaabc Mon Sep 17 00:00:00 2001 From: David Sterba Date: Thu, 16 Feb 2023 03:30:46 +0100 Subject: [PATCH] btrfs-progs: crypto: add common function for accelerated initialization Prepare a single location that will detect or set accelerated versions of hash algorithms. Right now it's the crc32c, blake2 and sha256 do an if-else switch while crc32c sets a function pointer. Signed-off-by: David Sterba --- btrfs.c | 8 ++++---- convert/main.c | 6 ++++-- crypto/hash-speedtest.c | 2 +- crypto/hash.c | 5 +++++ crypto/hash.h | 2 ++ image/main.c | 8 +++++--- mkfs/main.c | 6 ++++-- 7 files changed, 25 insertions(+), 12 deletions(-) diff --git a/btrfs.c b/btrfs.c index 0f817872..f95425e7 100644 --- a/btrfs.c +++ b/btrfs.c @@ -22,7 +22,8 @@ #include #include #include "kernel-shared/volumes.h" -#include "crypto/crc32c.h" +#include "crypto/hash.h" +#include "common/cpu-utils.h" #include "common/utils.h" #include "common/string-utils.h" #include "common/help.h" @@ -403,9 +404,8 @@ int main(int argc, char **argv) cmd = parse_command_token(argv[0], &btrfs_cmd_group); handle_help_options_next_level(cmd, argc, argv); - - crc32c_optimization_init(); - + cpu_detect_flags(); + hash_init_accel(); fixup_argv0(argv, cmd->token); ret = cmd_execute(cmd, argc, argv); diff --git a/convert/main.c b/convert/main.c index c7be19f4..2ca38cb7 100644 --- a/convert/main.c +++ b/convert/main.c @@ -99,10 +99,11 @@ #include "kernel-shared/disk-io.h" #include "kernel-shared/volumes.h" #include "kernel-shared/transaction.h" -#include "crypto/crc32c.h" +#include "crypto/hash.h" #include "common/defs.h" #include "common/extent-cache.h" #include "common/internal.h" +#include "common/cpu-utils.h" #include "common/messages.h" #include "common/task-utils.h" #include "common/path-utils.h" @@ -1834,7 +1835,8 @@ int BOX_MAIN(convert)(int argc, char *argv[]) u32 copy_fsid = 0; char fsid[BTRFS_UUID_UNPARSED_SIZE] = {0}; - crc32c_optimization_init(); + cpu_detect_flags(); + hash_init_accel(); btrfs_assert_feature_buf_size(); printf("btrfs-convert from %s\n\n", PACKAGE_STRING); diff --git a/crypto/hash-speedtest.c b/crypto/hash-speedtest.c index d5043a66..3657970c 100644 --- a/crypto/hash-speedtest.c +++ b/crypto/hash-speedtest.c @@ -203,6 +203,7 @@ int main(int argc, char **argv) { cpu_detect_flags(); cpu_print_flags(); + hash_init_accel(); optind = 0; while (1) { @@ -248,7 +249,6 @@ int main(int argc, char **argv) { iterations = 1; } - crc32c_optimization_init(); memset(buf, 0, 4096); printf("Block size: %d\n", blocksize); diff --git a/crypto/hash.c b/crypto/hash.c index 1aa65e6d..4ee65385 100644 --- a/crypto/hash.c +++ b/crypto/hash.c @@ -20,6 +20,11 @@ #include "crypto/sha.h" #include "crypto/blake2.h" +void hash_init_accel(void) +{ + crc32c_optimization_init(); +} + /* * Default builtin implementations */ diff --git a/crypto/hash.h b/crypto/hash.h index 425576f4..40a579ce 100644 --- a/crypto/hash.h +++ b/crypto/hash.h @@ -26,4 +26,6 @@ int hash_xxhash(const u8 *buf, size_t length, u8 *out); int hash_sha256(const u8 *buf, size_t length, u8 *out); int hash_blake2b(const u8 *buf, size_t length, u8 *out); +void hash_init_accel(void); + #endif diff --git a/image/main.c b/image/main.c index 65aa3b30..132be126 100644 --- a/image/main.c +++ b/image/main.c @@ -40,8 +40,10 @@ #include "kernel-shared/volumes.h" #include "kernel-shared/extent_io.h" #include "crypto/crc32c.h" +#include "crypto/hash.h" #include "common/internal.h" #include "common/messages.h" +#include "common/cpu-utils.h" #include "common/box.h" #include "common/utils.h" #include "common/extent-cache.h" @@ -494,9 +496,6 @@ static int metadump_init(struct metadump_struct *md, struct btrfs_root *root, md->pending_start = (u64)-1; md->compress_level = compress_level; md->sanitize_names = sanitize_names; - if (sanitize_names == SANITIZE_COLLISIONS) - crc32c_optimization_init(); - md->name_tree.rb_node = NULL; md->num_threads = num_threads; pthread_cond_init(&md->cond, NULL); @@ -3074,6 +3073,9 @@ int BOX_MAIN(image)(int argc, char *argv[]) int usage_error = 0; FILE *out; + cpu_detect_flags(); + hash_init_accel(); + while (1) { static const struct option long_options[] = { { "help", no_argument, NULL, GETOPT_VAL_HELP}, diff --git a/mkfs/main.c b/mkfs/main.c index 341ba408..1a8c22e1 100644 --- a/mkfs/main.c +++ b/mkfs/main.c @@ -38,10 +38,11 @@ #include "kernel-shared/volumes.h" #include "kernel-shared/transaction.h" #include "kernel-shared/zoned.h" -#include "crypto/crc32c.h" +#include "crypto/hash.h" #include "common/defs.h" #include "common/internal.h" #include "common/messages.h" +#include "common/cpu-utils.h" #include "common/utils.h" #include "common/path-utils.h" #include "common/device-utils.h" @@ -1034,7 +1035,8 @@ int BOX_MAIN(mkfs)(int argc, char **argv) char *source_dir = NULL; bool source_dir_set = false; - crc32c_optimization_init(); + cpu_detect_flags(); + hash_init_accel(); btrfs_config_init(); btrfs_assert_feature_buf_size();