Skip to content

Commit 9ba18bf

Browse files
author
Rafal Stefanowski
committed
Fix race condition when adding cores in examine
Change-Id: I3607fd8a261755440fdd2948a027a37aaf66d67e Signed-off-by: Rafal Stefanowski <rafal.stefanowski@huawei.com>
1 parent ecbc45c commit 9ba18bf

2 files changed

Lines changed: 35 additions & 10 deletions

File tree

module/bdev/ocf/vbdev_ocf.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ _core_add_examine_add_cb(ocf_cache_t cache, ocf_core_t core, void *cb_arg, int e
585585
SPDK_NOTICELOG("OCF core '%s': added to cache '%s'\n",
586586
ocf_core_get_name(core), ocf_cache_get_name(cache));
587587

588-
/* If core was taken from waitlist, remove it from there. */
588+
/* If core was taken from wait list, remove it from there. */
589589
if (vbdev_ocf_core_waitlist_get_by_name(vbdev_ocf_core_get_name(core_ctx))) {
590590
vbdev_ocf_core_waitlist_remove(core_ctx);
591591
}
@@ -612,6 +612,13 @@ _core_add_examine_lock_cb(ocf_cache_t cache, void *cb_arg, int error)
612612
return;
613613
}
614614

615+
/* Check if core is loaded from metadata only if its try_add flag was not
616+
* set to true already during examine_config stage. That would mean that
617+
* this core was hot removed before and now it is being attached back. */
618+
if (!core_ctx->core_cfg.try_add) {
619+
core_ctx->core_cfg.try_add = vbdev_ocf_core_is_loaded(vbdev_ocf_core_get_name(core_ctx));
620+
}
621+
615622
ocf_mngt_cache_add_core(cache, &core_ctx->core_cfg, _core_add_examine_add_cb, core_ctx);
616623
}
617624

@@ -717,7 +724,7 @@ _examine_disk_core_visitor(ocf_core_t core, void *cb_arg)
717724
return 0;
718725
}
719726

720-
/* Get cache once to be in sync with adding core from waitlist scenario. */
727+
/* Get cache once to be in sync with adding core from wait list scenario. */
721728
ocf_mngt_cache_get(cache);
722729
ocf_mngt_cache_lock(cache, _core_add_examine_lock_cb, core_ctx);
723730

@@ -754,6 +761,9 @@ vbdev_ocf_module_examine_disk(struct spdk_bdev *bdev)
754761
continue;
755762
}
756763

764+
SPDK_NOTICELOG("OCF core '%s': adding from wait list to cache '%s'\n",
765+
vbdev_ocf_core_get_name(core_ctx), core_ctx->cache_name);
766+
757767
if (ocf_mngt_cache_get_by_name(vbdev_ocf_ctx, core_ctx->cache_name,
758768
OCF_CACHE_NAME_SIZE, &cache)) {
759769
SPDK_NOTICELOG("OCF core '%s': add deferred - waiting for OCF cache '%s'\n",
@@ -771,8 +781,6 @@ vbdev_ocf_module_examine_disk(struct spdk_bdev *bdev)
771781
return;
772782
}
773783

774-
core_ctx->core_cfg.try_add = vbdev_ocf_core_is_loaded(vbdev_ocf_core_get_name(core_ctx));
775-
776784
ocf_mngt_cache_lock(cache, _core_add_examine_lock_cb, core_ctx);
777785
return;
778786
}

module/bdev/ocf/vbdev_ocf_core.c

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ _core_is_loaded_cache_visitor(ocf_cache_t cache, void *cb_arg)
6767
if (!rc && !ocf_core_get_priv(core)) {
6868
/* Core context is assigned only after manual core add (either right
6969
* away if all devices are present, after corresponding cache start,
70-
* or base bdev appearance).
70+
* or base bdev showing up).
7171
* If there is no context, it means that this core was just added from
7272
* metadata during cache load and that's what we're looking for here. */
7373

@@ -518,24 +518,35 @@ _core_add_from_waitlist_lock_cb(ocf_cache_t cache, void *cb_arg, int error)
518518
return;
519519
}
520520

521+
core_ctx->core_cfg.try_add = vbdev_ocf_core_is_loaded(vbdev_ocf_core_get_name(core_ctx));
522+
521523
ocf_mngt_cache_add_core(cache, &core_ctx->core_cfg, _core_add_from_waitlist_add_cb, core_ctx);
522524
}
523525

524-
void
525-
vbdev_ocf_core_add_from_waitlist(ocf_cache_t cache)
526+
static void
527+
_core_add_from_waitlist_read_lock_cb(ocf_cache_t cache, void *cb_arg, int error)
526528
{
527529
struct vbdev_ocf_cache *cache_ctx = ocf_cache_get_priv(cache);
528530
struct vbdev_ocf_core *core_ctx;
529531
uint32_t cache_block_size = spdk_bdev_get_block_size(cache_ctx->base.bdev);
530532
uint32_t core_block_size;
531533

534+
SPDK_DEBUGLOG(vbdev_ocf, "OCF cache '%s': looking for its cores in wait list\n",
535+
ocf_cache_get_name(cache));
536+
537+
if (error) {
538+
SPDK_ERRLOG("OCF cache '%s': failed to acquire OCF cache lock (OCF error: %d)\n",
539+
ocf_cache_get_name(cache), error);
540+
return;
541+
}
542+
532543
vbdev_ocf_foreach_core_in_waitlist(core_ctx) {
533544
if (strcmp(ocf_cache_get_name(cache), core_ctx->cache_name) ||
534545
!vbdev_ocf_core_is_base_attached(core_ctx)) {
535546
continue;
536547
}
537548

538-
SPDK_NOTICELOG("OCF core '%s': adding from waitlist to cache '%s'\n",
549+
SPDK_NOTICELOG("OCF core '%s': adding from wait list to cache '%s'\n",
539550
vbdev_ocf_core_get_name(core_ctx), ocf_cache_get_name(cache));
540551

541552
core_block_size = spdk_bdev_get_block_size(core_ctx->base.bdev);
@@ -546,10 +557,16 @@ vbdev_ocf_core_add_from_waitlist(ocf_cache_t cache)
546557
continue;
547558
}
548559

549-
core_ctx->core_cfg.try_add = vbdev_ocf_core_is_loaded(vbdev_ocf_core_get_name(core_ctx));
550-
551560
ocf_mngt_cache_lock(cache, _core_add_from_waitlist_lock_cb, core_ctx);
552561
}
562+
563+
ocf_mngt_cache_read_unlock(cache);
564+
}
565+
566+
void
567+
vbdev_ocf_core_add_from_waitlist(ocf_cache_t cache)
568+
{
569+
ocf_mngt_cache_read_lock(cache, _core_add_from_waitlist_read_lock_cb, NULL);
553570
}
554571

555572
static void

0 commit comments

Comments
 (0)