From 0238bb734ae433ffd772aa95c02c4be5b9001dff Mon Sep 17 00:00:00 2001 From: Shuhei Matsumoto Date: Thu, 1 Oct 2020 13:47:53 +0900 Subject: [PATCH] lib/vhost_blk: Open bdev by bdev_name first in spdk_vhost_blk_construct() Open a bdev whose name matches bdev_name first in spdk_vhost_blk_construct() to avoid using spdk_bdev_get_by_name() to fix the race condition due to the time gap between spdk_bdev_get_by_name() and spdk_bdev_open_ext(). A pointer to a bdev is valid only while the bdev is opened. Signed-off-by: Shuhei Matsumoto Change-Id: I99fedd696aa44bb46c1fcc4f49ca37069f7adfef Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/4502 Community-CI: Broadcom CI Community-CI: Mellanox Build Bot Tested-by: SPDK CI Jenkins Reviewed-by: Changpeng Liu Reviewed-by: Aleksey Marchuk Reviewed-by: Tomasz Zawadzki --- lib/vhost/vhost_blk.c | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/lib/vhost/vhost_blk.c b/lib/vhost/vhost_blk.c index 2114248974..d794e49dd7 100644 --- a/lib/vhost/vhost_blk.c +++ b/lib/vhost/vhost_blk.c @@ -1250,13 +1250,6 @@ spdk_vhost_blk_construct(const char *name, const char *cpumask, const char *dev_ int ret = 0; spdk_vhost_lock(); - bdev = spdk_bdev_get_by_name(dev_name); - if (bdev == NULL) { - SPDK_ERRLOG("%s: bdev '%s' not found\n", - name, dev_name); - ret = -ENODEV; - goto out; - } bvdev = calloc(1, sizeof(*bvdev)); if (bvdev == NULL) { @@ -1264,6 +1257,14 @@ spdk_vhost_blk_construct(const char *name, const char *cpumask, const char *dev_ goto out; } + ret = spdk_bdev_open_ext(dev_name, true, bdev_event_cb, bvdev, &bvdev->bdev_desc); + if (ret != 0) { + SPDK_ERRLOG("%s: could not open bdev '%s', error=%d\n", + name, dev_name, ret); + goto out; + } + bdev = spdk_bdev_desc_get_bdev(bvdev->bdev_desc); + vdev = &bvdev->vdev; vdev->virtio_features = SPDK_VHOST_BLK_FEATURES_BASE; vdev->disabled_features = SPDK_VHOST_BLK_DISABLED_FEATURES; @@ -1284,13 +1285,6 @@ spdk_vhost_blk_construct(const char *name, const char *cpumask, const char *dev_ vdev->virtio_features |= (1ULL << VIRTIO_BLK_F_FLUSH); } - ret = spdk_bdev_open_ext(dev_name, true, bdev_event_cb, bvdev, &bvdev->bdev_desc); - if (ret != 0) { - SPDK_ERRLOG("%s: could not open bdev '%s', error=%d\n", - name, dev_name, ret); - goto out; - } - /* * When starting qemu with vhost-user-blk multiqueue, the vhost device will * be started/stopped many times, related to the queues num, as the