From 63dab8444940b6103a7526df7b3963199ff23f08 Mon Sep 17 00:00:00 2001 From: Changpeng Liu Date: Thu, 17 Nov 2022 14:40:21 +0800 Subject: [PATCH] lib/vhost: fix race condition when destroying a device `rte_vhost_driver_unregister` API for removing socket is not asynchronous, it may call SPDK ops for adding a new connection or removing a connection, so we can't hold the user device lock when calling this function, and reject to add a new connection while calling `rte_vhost_driver_unregister`. Fix issue #2748. Change-Id: I5594224f26374b2336d64175ecd5e5ec3d545a58 Signed-off-by: Changpeng Liu Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/15483 Tested-by: SPDK CI Jenkins Reviewed-by: Shuhei Matsumoto Reviewed-by: Jim Harris --- lib/vhost/rte_vhost_user.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/vhost/rte_vhost_user.c b/lib/vhost/rte_vhost_user.c index 5a7e04425..8bb21b389 100644 --- a/lib/vhost/rte_vhost_user.c +++ b/lib/vhost/rte_vhost_user.c @@ -929,6 +929,11 @@ new_connection(int vid) user_dev = to_user_dev(vdev); pthread_mutex_lock(&user_dev->lock); + if (user_dev->registered == false) { + SPDK_ERRLOG("Device %s is unregistered\n", ctrlr_name); + pthread_mutex_unlock(&user_dev->lock); + return -1; + } /* We expect sessions inside user_dev->vsessions to be sorted in ascending * order in regard of vsession->id. For now we always set id = vsessions_cnt++ @@ -1841,6 +1846,7 @@ vhost_user_dev_unregister(struct spdk_vhost_dev *vdev) pthread_mutex_unlock(&user_dev->lock); return -EBUSY; } + user_dev->registered = false; pthread_mutex_unlock(&user_dev->lock); /* There are no valid connections now, and it's not an error if the domain