From patchwork Mon Oct 23 09:55:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Marchand X-Patchwork-Id: 133172 X-Patchwork-Delegate: maxime.coquelin@redhat.com Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id C7D75431E0; Mon, 23 Oct 2023 11:55:43 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 0A27C427D9; Mon, 23 Oct 2023 11:55:39 +0200 (CEST) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mails.dpdk.org (Postfix) with ESMTP id B94D340270 for ; Mon, 23 Oct 2023 11:55:36 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1698054936; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3X2rRgPeg4s/QNKmtmSB9TVw9KboTpbY8YAgeYZR1l8=; b=brZh3+j8Zw0IZmdVfVQHRcS1fvLrZeAu3WAPSjo+mpjUM0/1E5tNpPrZG8hCebNpmyuFhl i+lgdSnjpXCXWRQeEwhc68KPozs86mAOyLsSh9Uzz2k2kJABfHVjEaMVOgWsUID74/QJ99 1FtVYX0/X9/BfKrDZfonEheCIM/8Ngs= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-32-KHJJXG8jPdm96Un6CtCmTA-1; Mon, 23 Oct 2023 05:55:29 -0400 X-MC-Unique: KHJJXG8jPdm96Un6CtCmTA-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B7B831C07588; Mon, 23 Oct 2023 09:55:28 +0000 (UTC) Received: from dmarchan.redhat.com (unknown [10.45.225.98]) by smtp.corp.redhat.com (Postfix) with ESMTP id A7BA925C0; Mon, 23 Oct 2023 09:55:27 +0000 (UTC) From: David Marchand To: dev@dpdk.org Cc: stable@dpdk.org, Maxime Coquelin , Chenbo Xia , Eelco Chaudron Subject: [PATCH 2/3] vhost: fix virtqueue access lock in datapath Date: Mon, 23 Oct 2023 11:55:19 +0200 Message-ID: <20231023095520.2864868-2-david.marchand@redhat.com> In-Reply-To: <20231023095520.2864868-1-david.marchand@redhat.com> References: <20231023095520.2864868-1-david.marchand@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Now that a r/w lock is used, the access_ok field should only be updated under a write lock. Since the datapath code only takes a read lock on the virtqueue to check access_ok, this lock must be released and a write lock taken before calling vring_translate(). Fixes: 03f77d66d966 ("vhost: change virtqueue access lock to a read/write one") Cc: stable@dpdk.org Signed-off-by: David Marchand Acked-by: Eelco Chaudron Acked-by: Eelco Chaudron Reviewed-by: Maxime Coquelin --- lib/vhost/virtio_net.c | 60 +++++++++++++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 16 deletions(-) diff --git a/lib/vhost/virtio_net.c b/lib/vhost/virtio_net.c index 759a78e3e3..4116f79d4f 100644 --- a/lib/vhost/virtio_net.c +++ b/lib/vhost/virtio_net.c @@ -1694,6 +1694,17 @@ virtio_dev_rx_packed(struct virtio_net *dev, return pkt_idx; } +static void +virtio_dev_vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq) +{ + rte_rwlock_write_lock(&vq->access_lock); + vhost_user_iotlb_rd_lock(vq); + if (!vq->access_ok) + vring_translate(dev, vq); + vhost_user_iotlb_rd_unlock(vq); + rte_rwlock_write_unlock(&vq->access_lock); +} + static __rte_always_inline uint32_t virtio_dev_rx(struct virtio_net *dev, struct vhost_virtqueue *vq, struct rte_mbuf **pkts, uint32_t count) @@ -1708,9 +1719,13 @@ virtio_dev_rx(struct virtio_net *dev, struct vhost_virtqueue *vq, vhost_user_iotlb_rd_lock(vq); - if (unlikely(!vq->access_ok)) - if (unlikely(vring_translate(dev, vq) < 0)) - goto out; + if (unlikely(!vq->access_ok)) { + vhost_user_iotlb_rd_unlock(vq); + rte_rwlock_read_unlock(&vq->access_lock); + + virtio_dev_vring_translate(dev, vq); + goto out_no_unlock; + } count = RTE_MIN((uint32_t)MAX_PKT_BURST, count); if (count == 0) @@ -1729,6 +1744,7 @@ virtio_dev_rx(struct virtio_net *dev, struct vhost_virtqueue *vq, out_access_unlock: rte_rwlock_read_unlock(&vq->access_lock); +out_no_unlock: return nb_tx; } @@ -2523,9 +2539,13 @@ virtio_dev_rx_async_submit(struct virtio_net *dev, struct vhost_virtqueue *vq, vhost_user_iotlb_rd_lock(vq); - if (unlikely(!vq->access_ok)) - if (unlikely(vring_translate(dev, vq) < 0)) - goto out; + if (unlikely(!vq->access_ok)) { + vhost_user_iotlb_rd_unlock(vq); + rte_rwlock_read_unlock(&vq->access_lock); + + virtio_dev_vring_translate(dev, vq); + goto out_no_unlock; + } count = RTE_MIN((uint32_t)MAX_PKT_BURST, count); if (count == 0) @@ -2546,6 +2566,7 @@ virtio_dev_rx_async_submit(struct virtio_net *dev, struct vhost_virtqueue *vq, out_access_unlock: rte_rwlock_write_unlock(&vq->access_lock); +out_no_unlock: return nb_tx; } @@ -3576,11 +3597,13 @@ rte_vhost_dequeue_burst(int vid, uint16_t queue_id, vhost_user_iotlb_rd_lock(vq); - if (unlikely(!vq->access_ok)) - if (unlikely(vring_translate(dev, vq) < 0)) { - count = 0; - goto out; - } + if (unlikely(!vq->access_ok)) { + vhost_user_iotlb_rd_unlock(vq); + rte_rwlock_read_unlock(&vq->access_lock); + + virtio_dev_vring_translate(dev, vq); + goto out_no_unlock; + } /* * Construct a RARP broadcast packet, and inject it to the "pkts" @@ -3641,6 +3664,7 @@ rte_vhost_dequeue_burst(int vid, uint16_t queue_id, if (unlikely(rarp_mbuf != NULL)) count += 1; +out_no_unlock: return count; } @@ -4190,11 +4214,14 @@ rte_vhost_async_try_dequeue_burst(int vid, uint16_t queue_id, vhost_user_iotlb_rd_lock(vq); - if (unlikely(vq->access_ok == 0)) - if (unlikely(vring_translate(dev, vq) < 0)) { - count = 0; - goto out; - } + if (unlikely(vq->access_ok == 0)) { + vhost_user_iotlb_rd_unlock(vq); + rte_rwlock_read_unlock(&vq->access_lock); + + virtio_dev_vring_translate(dev, vq); + count = 0; + goto out_no_unlock; + } /* * Construct a RARP broadcast packet, and inject it to the "pkts" @@ -4260,5 +4287,6 @@ rte_vhost_async_try_dequeue_burst(int vid, uint16_t queue_id, if (unlikely(rarp_mbuf != NULL)) count += 1; +out_no_unlock: return count; }