Until virtio avail ring is initialized (by VHOST_USER_SET_VRING_ADDR),
worker thread should not try to fetch crypto op, which would lead to
memory fault.
Fixes: 939066d96563 ("vhost/crypto: add public function implementation")
Cc: sta...@dpdk.org

Signed-off-by: Gowrishankar Muthukrishnan <gmuthukri...@marvell.com>
Acked-by: Akhil Goyal <gak...@marvell.com>
---
 lib/vhost/vhost_crypto.c | 25 ++++++++++++++++++++++---
 1 file changed, 22 insertions(+), 3 deletions(-)

diff --git a/lib/vhost/vhost_crypto.c b/lib/vhost/vhost_crypto.c
index 4c36df9cb2..38c5a7f06f 100644
--- a/lib/vhost/vhost_crypto.c
+++ b/lib/vhost/vhost_crypto.c
@@ -8,6 +8,7 @@
 #include <rte_mbuf.h>
 #include <rte_cryptodev.h>
 
+#include "iotlb.h"
 #include "rte_vhost_crypto.h"
 #include "vhost.h"
 #include "vhost_user.h"
@@ -1586,6 +1587,20 @@ rte_vhost_crypto_fetch_requests(int vid, uint32_t qid,
 
        vq = dev->virtqueue[qid];
 
+       if (unlikely(vq == NULL)) {
+               VC_LOG_ERR("Invalid virtqueue %u", qid);
+               return 0;
+       }
+
+       if (unlikely(rte_rwlock_read_trylock(&vq->access_lock) != 0))
+               return 0;
+
+       vhost_user_iotlb_rd_lock(vq);
+       if (unlikely(!vq->access_ok)) {
+               VC_LOG_DBG("Virtqueue %u vrings not yet initialized", qid);
+               goto out_unlock;
+       }
+
        avail_idx = *((volatile uint16_t *)&vq->avail->idx);
        start_idx = vq->last_used_idx;
        count = avail_idx - start_idx;
@@ -1593,7 +1608,7 @@ rte_vhost_crypto_fetch_requests(int vid, uint32_t qid,
        count = RTE_MIN(count, nb_ops);
 
        if (unlikely(count == 0))
-               return 0;
+               goto out_unlock;
 
        /* for zero copy, we need 2 empty mbufs for src and dst, otherwise
         * we need only 1 mbuf as src and dst
@@ -1603,7 +1618,7 @@ rte_vhost_crypto_fetch_requests(int vid, uint32_t qid,
                if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool,
                                (void **)mbufs, count * 2) < 0)) {
                        VC_LOG_ERR("Insufficient memory");
-                       return 0;
+                       goto out_unlock;
                }
 
                for (i = 0; i < count; i++) {
@@ -1633,7 +1648,7 @@ rte_vhost_crypto_fetch_requests(int vid, uint32_t qid,
                if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool,
                                (void **)mbufs, count) < 0)) {
                        VC_LOG_ERR("Insufficient memory");
-                       return 0;
+                       goto out_unlock;
                }
 
                for (i = 0; i < count; i++) {
@@ -1662,6 +1677,10 @@ rte_vhost_crypto_fetch_requests(int vid, uint32_t qid,
 
        vq->last_used_idx += i;
 
+out_unlock:
+       vhost_user_iotlb_rd_unlock(vq);
+       rte_rwlock_read_unlock(&vq->access_lock);
+
        return i;
 }
 
-- 
2.25.1

Reply via email to