DPDK patches and discussions
 help / color / mirror / Atom feed
* [PATCH] vhost: add runtime locking check in unsafe APIs
@ 2022-05-10  8:25 Maxime Coquelin
  2022-05-10  9:00 ` Ding, Xuan
  2022-05-10  9:00 ` Hu, Jiayu
  0 siblings, 2 replies; 6+ messages in thread
From: Maxime Coquelin @ 2022-05-10  8:25 UTC (permalink / raw)
  To: dev, chenbo.xia, xuan.ding, jiayu.hu, cheng1.jiang, sunil.pai.g,
	david.marchand
  Cc: Maxime Coquelin

This patch adds runtime checks in unsafe Vhost async APIs,
to ensure the access lock is taken.

The detection won't work every time, as another thread
could take the lock, but it would help to detect misuse
of these unsafe API.

Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
---
 lib/vhost/vhost.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/lib/vhost/vhost.c b/lib/vhost/vhost.c
index df0bb9d043..39cbeb415c 100644
--- a/lib/vhost/vhost.c
+++ b/lib/vhost/vhost.c
@@ -1732,6 +1732,12 @@ rte_vhost_async_channel_register_thread_unsafe(int vid, uint16_t queue_id)
 	if (unlikely(vq == NULL || !dev->async_copy))
 		return -1;
 
+	if (unlikely(!rte_spinlock_is_locked(&vq->access_lock))) {
+		VHOST_LOG_CONFIG(ERR, "(%s) %s() called without access lock taken.\n",
+				dev->ifname, __func__);
+		return -1;
+	}
+
 	return async_channel_register(vid, queue_id);
 }
 
@@ -1796,6 +1802,12 @@ rte_vhost_async_channel_unregister_thread_unsafe(int vid, uint16_t queue_id)
 	if (vq == NULL)
 		return -1;
 
+	if (unlikely(!rte_spinlock_is_locked(&vq->access_lock))) {
+		VHOST_LOG_CONFIG(ERR, "(%s) %s() called without access lock taken.\n",
+				dev->ifname, __func__);
+		return -1;
+	}
+
 	if (!vq->async)
 		return 0;
 
@@ -1925,6 +1937,12 @@ rte_vhost_async_get_inflight_thread_unsafe(int vid, uint16_t queue_id)
 	if (vq == NULL)
 		return ret;
 
+	if (unlikely(!rte_spinlock_is_locked(&vq->access_lock))) {
+		VHOST_LOG_CONFIG(ERR, "(%s) %s() called without access lock taken.\n",
+				dev->ifname, __func__);
+		return -1;
+	}
+
 	if (!vq->async)
 		return ret;
 
-- 
2.35.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-05-11  1:52 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-10  8:25 [PATCH] vhost: add runtime locking check in unsafe APIs Maxime Coquelin
2022-05-10  9:00 ` Ding, Xuan
2022-05-10  9:09   ` Maxime Coquelin
2022-05-10  9:00 ` Hu, Jiayu
2022-05-10  9:14   ` Maxime Coquelin
2022-05-11  1:51     ` Hu, Jiayu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).