From 9e96e331c384387d400fe81574b26faf9ed8c6a0 Mon Sep 17 00:00:00 2001 From: Mike Gerdts Date: Wed, 21 Dec 2022 12:27:28 -0600 Subject: [PATCH] fio_plugin: initalize when create_serialize=0 When the fio plugin is used with reate_serialize set to false, fio calls the init callback (spdk_fio_init()) before calling the setup callback (spdk_fio_setup()). When create_serialize is true, spdk_fio_setup() is called earlier. Both spdk_fio_setup() and spdk_fio_init() call functions that require that the SPDK libraries have been initialized. While it is arguably a bug in SPDK that per-thread initialization happens before global initialization, it is not terribly difficult to work around in fio_plugin. The workaround implemented in this patch splits the start of the plugin's init thread into its own function and calls it from both spdk_fio_setup() and spdk_fio_init(). The init thread is started while holding a mutex, and as before there is a guard to be sure that the init thread is created only once. Fixes issue #2843 Signed-off-by: Mike Gerdts Change-Id: I20d1863a88b75b416283f9466ee1186d8ef05063 Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/16053 Tested-by: SPDK CI Jenkins Reviewed-by: Jim Harris Reviewed-by: Ben Walker --- examples/bdev/fio_plugin/fio_plugin.c | 33 +++++++++++++++++++++------ 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/examples/bdev/fio_plugin/fio_plugin.c b/examples/bdev/fio_plugin/fio_plugin.c index ecb48a697..92690be91 100644 --- a/examples/bdev/fio_plugin/fio_plugin.c +++ b/examples/bdev/fio_plugin/fio_plugin.c @@ -531,6 +531,26 @@ fio_redirected_to_dev_null(void) return true; } +static int +spdk_fio_init_spdk_env(struct thread_data *td) +{ + static pthread_mutex_t setup_lock = PTHREAD_MUTEX_INITIALIZER; + + pthread_mutex_lock(&setup_lock); + if (!g_spdk_env_initialized) { + if (spdk_fio_init_env(td)) { + pthread_mutex_unlock(&setup_lock); + SPDK_ERRLOG("failed to initialize\n"); + return -1; + } + + g_spdk_env_initialized = true; + } + pthread_mutex_unlock(&setup_lock); + + return 0; +} + /* Called for each thread to fill in the 'real_file_size' member for * each file associated with this thread. This is called prior to * the init operation (spdk_fio_init()) below. This call will occur @@ -564,13 +584,8 @@ spdk_fio_setup(struct thread_data *td) return -1; } - if (!g_spdk_env_initialized) { - if (spdk_fio_init_env(td)) { - SPDK_ERRLOG("failed to initialize\n"); - return -1; - } - - g_spdk_env_initialized = true; + if (spdk_fio_init_spdk_env(td) != 0) { + return -1; } ctx.u.sa.td = td; @@ -709,6 +724,10 @@ spdk_fio_init(struct thread_data *td) struct spdk_fio_thread *fio_thread; int rc; + if (spdk_fio_init_spdk_env(td) != 0) { + return -1; + } + /* If thread has already been initialized, do nothing. */ if (td->io_ops_data) { return 0;