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 <dsterba@suse.com>
This commit is contained in:
David Sterba 2023-02-16 03:30:46 +01:00
parent 7fba4bf4db
commit 24ec095295
7 changed files with 25 additions and 12 deletions

View File

@ -22,7 +22,8 @@
#include <stdbool.h> #include <stdbool.h>
#include <strings.h> #include <strings.h>
#include "kernel-shared/volumes.h" #include "kernel-shared/volumes.h"
#include "crypto/crc32c.h" #include "crypto/hash.h"
#include "common/cpu-utils.h"
#include "common/utils.h" #include "common/utils.h"
#include "common/string-utils.h" #include "common/string-utils.h"
#include "common/help.h" #include "common/help.h"
@ -403,9 +404,8 @@ int main(int argc, char **argv)
cmd = parse_command_token(argv[0], &btrfs_cmd_group); cmd = parse_command_token(argv[0], &btrfs_cmd_group);
handle_help_options_next_level(cmd, argc, argv); handle_help_options_next_level(cmd, argc, argv);
cpu_detect_flags();
crc32c_optimization_init(); hash_init_accel();
fixup_argv0(argv, cmd->token); fixup_argv0(argv, cmd->token);
ret = cmd_execute(cmd, argc, argv); ret = cmd_execute(cmd, argc, argv);

View File

@ -99,10 +99,11 @@
#include "kernel-shared/disk-io.h" #include "kernel-shared/disk-io.h"
#include "kernel-shared/volumes.h" #include "kernel-shared/volumes.h"
#include "kernel-shared/transaction.h" #include "kernel-shared/transaction.h"
#include "crypto/crc32c.h" #include "crypto/hash.h"
#include "common/defs.h" #include "common/defs.h"
#include "common/extent-cache.h" #include "common/extent-cache.h"
#include "common/internal.h" #include "common/internal.h"
#include "common/cpu-utils.h"
#include "common/messages.h" #include "common/messages.h"
#include "common/task-utils.h" #include "common/task-utils.h"
#include "common/path-utils.h" #include "common/path-utils.h"
@ -1834,7 +1835,8 @@ int BOX_MAIN(convert)(int argc, char *argv[])
u32 copy_fsid = 0; u32 copy_fsid = 0;
char fsid[BTRFS_UUID_UNPARSED_SIZE] = {0}; char fsid[BTRFS_UUID_UNPARSED_SIZE] = {0};
crc32c_optimization_init(); cpu_detect_flags();
hash_init_accel();
btrfs_assert_feature_buf_size(); btrfs_assert_feature_buf_size();
printf("btrfs-convert from %s\n\n", PACKAGE_STRING); printf("btrfs-convert from %s\n\n", PACKAGE_STRING);

View File

@ -203,6 +203,7 @@ int main(int argc, char **argv) {
cpu_detect_flags(); cpu_detect_flags();
cpu_print_flags(); cpu_print_flags();
hash_init_accel();
optind = 0; optind = 0;
while (1) { while (1) {
@ -248,7 +249,6 @@ int main(int argc, char **argv) {
iterations = 1; iterations = 1;
} }
crc32c_optimization_init();
memset(buf, 0, 4096); memset(buf, 0, 4096);
printf("Block size: %d\n", blocksize); printf("Block size: %d\n", blocksize);

View File

@ -20,6 +20,11 @@
#include "crypto/sha.h" #include "crypto/sha.h"
#include "crypto/blake2.h" #include "crypto/blake2.h"
void hash_init_accel(void)
{
crc32c_optimization_init();
}
/* /*
* Default builtin implementations * Default builtin implementations
*/ */

View File

@ -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_sha256(const u8 *buf, size_t length, u8 *out);
int hash_blake2b(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 #endif

View File

@ -40,8 +40,10 @@
#include "kernel-shared/volumes.h" #include "kernel-shared/volumes.h"
#include "kernel-shared/extent_io.h" #include "kernel-shared/extent_io.h"
#include "crypto/crc32c.h" #include "crypto/crc32c.h"
#include "crypto/hash.h"
#include "common/internal.h" #include "common/internal.h"
#include "common/messages.h" #include "common/messages.h"
#include "common/cpu-utils.h"
#include "common/box.h" #include "common/box.h"
#include "common/utils.h" #include "common/utils.h"
#include "common/extent-cache.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->pending_start = (u64)-1;
md->compress_level = compress_level; md->compress_level = compress_level;
md->sanitize_names = sanitize_names; md->sanitize_names = sanitize_names;
if (sanitize_names == SANITIZE_COLLISIONS)
crc32c_optimization_init();
md->name_tree.rb_node = NULL; md->name_tree.rb_node = NULL;
md->num_threads = num_threads; md->num_threads = num_threads;
pthread_cond_init(&md->cond, NULL); pthread_cond_init(&md->cond, NULL);
@ -3074,6 +3073,9 @@ int BOX_MAIN(image)(int argc, char *argv[])
int usage_error = 0; int usage_error = 0;
FILE *out; FILE *out;
cpu_detect_flags();
hash_init_accel();
while (1) { while (1) {
static const struct option long_options[] = { static const struct option long_options[] = {
{ "help", no_argument, NULL, GETOPT_VAL_HELP}, { "help", no_argument, NULL, GETOPT_VAL_HELP},

View File

@ -38,10 +38,11 @@
#include "kernel-shared/volumes.h" #include "kernel-shared/volumes.h"
#include "kernel-shared/transaction.h" #include "kernel-shared/transaction.h"
#include "kernel-shared/zoned.h" #include "kernel-shared/zoned.h"
#include "crypto/crc32c.h" #include "crypto/hash.h"
#include "common/defs.h" #include "common/defs.h"
#include "common/internal.h" #include "common/internal.h"
#include "common/messages.h" #include "common/messages.h"
#include "common/cpu-utils.h"
#include "common/utils.h" #include "common/utils.h"
#include "common/path-utils.h" #include "common/path-utils.h"
#include "common/device-utils.h" #include "common/device-utils.h"
@ -1034,7 +1035,8 @@ int BOX_MAIN(mkfs)(int argc, char **argv)
char *source_dir = NULL; char *source_dir = NULL;
bool source_dir_set = false; bool source_dir_set = false;
crc32c_optimization_init(); cpu_detect_flags();
hash_init_accel();
btrfs_config_init(); btrfs_config_init();
btrfs_assert_feature_buf_size(); btrfs_assert_feature_buf_size();