From 0d06b3a597f08daff9da7360b693e96b925f76ba Mon Sep 17 00:00:00 2001 From: Robert Baldyga Date: Thu, 21 Nov 2024 21:26:00 +0100 Subject: [PATCH] Fix race condition during cache attach After attaching new cache device handle all the IOs in Pass-Through mode until all the d2c requests are completed. Signed-off-by: Robert Baldyga --- src/engine/cache_engine.c | 5 +++++ src/mngt/ocf_mngt_cache.c | 23 ++++++++++++++++++++--- src/ocf_cache_priv.h | 2 ++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/engine/cache_engine.c b/src/engine/cache_engine.c index f7f160e..2ab7972 100644 --- a/src/engine/cache_engine.c +++ b/src/engine/cache_engine.c @@ -162,6 +162,11 @@ void ocf_resolve_effective_cache_mode(ocf_cache_t cache, return; } + if (env_atomic_read(&cache->attach_pt)) { + req->cache_mode = ocf_req_cache_mode_pt; + return; + } + if (cache->pt_unaligned_io && !ocf_req_is_4k(req->addr, req->bytes)) { req->cache_mode = ocf_req_cache_mode_pt; return; diff --git a/src/mngt/ocf_mngt_cache.c b/src/mngt/ocf_mngt_cache.c index d1c8ad7..d9261c3 100644 --- a/src/mngt/ocf_mngt_cache.c +++ b/src/mngt/ocf_mngt_cache.c @@ -1901,18 +1901,35 @@ static void _ocf_mngt_attach_shutdown_status(ocf_pipeline_t pipeline, _ocf_mngt_attach_shutdown_status_complete, context); } + +static void _ocf_mngt_attach_post_init_finish(void *priv) +{ + struct ocf_cache_attach_context *context = priv; + ocf_cache_t cache = context->cache; + + ocf_refcnt_unfreeze(&cache->refcnt.d2c); + + env_atomic_set(&cache->attach_pt, 0); + + ocf_cache_log(cache, log_debug, "Cache attached\n"); + + ocf_pipeline_next(context->pipeline); +} + static void _ocf_mngt_attach_post_init(ocf_pipeline_t pipeline, void *priv, ocf_pipeline_arg_t arg) { struct ocf_cache_attach_context *context = priv; ocf_cache_t cache = context->cache; + env_atomic_set(&cache->attach_pt, 1); + ocf_cleaner_refcnt_unfreeze(cache); ocf_refcnt_unfreeze(&cache->refcnt.metadata); - ocf_cache_log(cache, log_debug, "Cache attached\n"); - - ocf_pipeline_next(pipeline); + ocf_refcnt_freeze(&cache->refcnt.d2c); + ocf_refcnt_register_zero_cb(&cache->refcnt.d2c, + _ocf_mngt_attach_post_init_finish, context); } static void _ocf_mngt_attach_handle_error( diff --git a/src/ocf_cache_priv.h b/src/ocf_cache_priv.h index 9a8e02d..b4ab8b6 100644 --- a/src/ocf_cache_priv.h +++ b/src/ocf_cache_priv.h @@ -106,6 +106,8 @@ struct ocf_cache { env_atomic flush_in_progress; env_mutex flush_mutex; + env_atomic attach_pt; + struct ocf_cleaner cleaner; struct list_head io_queues;