From eb666d24085fd21f0a087dcfb2be9baa8287707d Mon Sep 17 00:00:00 2001 From: Patrick Fu Date: Tue, 13 Oct 2020 09:45:46 +0800 Subject: [PATCH] vhost: fix async unregister deadlock When async unregister function is invoked in certain vhost event callbacks (e.g. vring state change), deadlock may occur due to recursive spinlock acquire. This patch uses trylock() primitive in the unregister API to avoid deadlock. Fixes: 78639d54563a ("vhost: introduce async enqueue registration API") Cc: stable@dpdk.org Signed-off-by: Patrick Fu Reviewed-by: Maxime Coquelin --- lib/librte_vhost/vhost.c | 9 +++++++-- lib/librte_vhost/vhost_user.c | 4 ++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/librte_vhost/vhost.c b/lib/librte_vhost/vhost.c index 3235658980..6068c38ec6 100644 --- a/lib/librte_vhost/vhost.c +++ b/lib/librte_vhost/vhost.c @@ -1633,10 +1633,15 @@ int rte_vhost_async_channel_unregister(int vid, uint16_t queue_id) return ret; ret = 0; - rte_spinlock_lock(&vq->access_lock); if (!vq->async_registered) - goto out; + return ret; + + if (!rte_spinlock_trylock(&vq->access_lock)) { + VHOST_LOG_CONFIG(ERR, "Failed to unregister async channel. " + "virt queue busy.\n"); + return -1; + } if (vq->async_pkts_inflight_n) { VHOST_LOG_CONFIG(ERR, "Failed to unregister async channel. " diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c index 1656ec7364..d20c8c57ad 100644 --- a/lib/librte_vhost/vhost_user.c +++ b/lib/librte_vhost/vhost_user.c @@ -1980,9 +1980,9 @@ vhost_user_set_vring_enable(struct virtio_net **pdev, "set queue enable: %d to qp idx: %d\n", enable, index); - if (!enable && dev->virtqueue[index]->async_registered) { + if (enable && dev->virtqueue[index]->async_registered) { if (dev->virtqueue[index]->async_pkts_inflight_n) { - VHOST_LOG_CONFIG(ERR, "failed to disable vring. " + VHOST_LOG_CONFIG(ERR, "failed to enable vring. " "async inflight packets must be completed first\n"); return RTE_VHOST_MSG_RESULT_ERR; } -- 2.20.1