From patchwork Fri Oct 20 08:48:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxime Coquelin X-Patchwork-Id: 133063 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 EB6C3431BA; Fri, 20 Oct 2023 10:48:53 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 17F8140E6E; Fri, 20 Oct 2023 10:48:26 +0200 (CEST) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mails.dpdk.org (Postfix) with ESMTP id 1DF64410FD for ; Fri, 20 Oct 2023 10:48:24 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1697791703; 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=8JSUrGSqP7nlt3ZcvKG9CyGqWhawsrSkVV8RRlCQ52Q=; b=heR0rWQiGee8NnnrPvNdkw8UaEhaHzgRB0aZCH6qD3u+ZDhoVXZ832l/aAppaGN7ALwNO8 P6Z3G8FIlHdbxluXcqB/PLZ23QfVqJDe7Q7ZNK7DHYVRDu3RqtoBgKi9j9nWTsr7qFG/ig pHwWiW0q4UvWLRAxn2NDhHm14wjB3Rc= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-317-efWv7JpiN3O-6iRQlRVr1w-1; Fri, 20 Oct 2023 04:48:20 -0400 X-MC-Unique: efWv7JpiN3O-6iRQlRVr1w-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 416EA886C67; Fri, 20 Oct 2023 08:48:20 +0000 (UTC) Received: from max-p1.redhat.com (unknown [10.39.208.31]) by smtp.corp.redhat.com (Postfix) with ESMTP id E4CBC40C6F7B; Fri, 20 Oct 2023 08:48:18 +0000 (UTC) From: Maxime Coquelin To: dev@dpdk.org, david.marchand@redhat.com, chenbo.xia@outlook.com, fengli@smartx.com Cc: Maxime Coquelin , stable@dpdk.org Subject: [PATCH v2 7/7] vhost: fix checking virtqueue access in stats API Date: Fri, 20 Oct 2023 10:48:04 +0200 Message-ID: <20231020084804.3625099-8-maxime.coquelin@redhat.com> In-Reply-To: <20231020084804.3625099-1-maxime.coquelin@redhat.com> References: <20231020084804.3625099-1-maxime.coquelin@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 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 Acquiring the access lock is not enough to ensure virtqueue's metadata such as vring pointers are valid. The access status must also be checked. Fixes: be75dc99ea1f ("vhost: support per-virtqueue statistics") Cc: stable@dpdk.org Signed-off-by: Maxime Coquelin --- lib/vhost/vhost.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/vhost/vhost.c b/lib/vhost/vhost.c index d8d74623d4..b03e3b391d 100644 --- a/lib/vhost/vhost.c +++ b/lib/vhost/vhost.c @@ -2199,6 +2199,7 @@ rte_vhost_vring_stats_get(int vid, uint16_t queue_id, struct virtio_net *dev = get_device(vid); struct vhost_virtqueue *vq; unsigned int i; + int ret = VHOST_NB_VQ_STATS; if (dev == NULL) return -1; @@ -2215,6 +2216,12 @@ rte_vhost_vring_stats_get(int vid, uint16_t queue_id, vq = dev->virtqueue[queue_id]; rte_rwlock_write_lock(&vq->access_lock); + + if (unlikely(!vq->access_ok)) { + ret = -1; + goto out_unlock; + } + for (i = 0; i < VHOST_NB_VQ_STATS; i++) { /* * No need to the read atomic counters as such, due to the @@ -2224,15 +2231,18 @@ rte_vhost_vring_stats_get(int vid, uint16_t queue_id, *(uint64_t *)(((char *)vq) + vhost_vq_stat_strings[i].offset); stats[i].id = i; } + +out_unlock: rte_rwlock_write_unlock(&vq->access_lock); - return VHOST_NB_VQ_STATS; + return ret; } int rte_vhost_vring_stats_reset(int vid, uint16_t queue_id) { struct virtio_net *dev = get_device(vid); struct vhost_virtqueue *vq; + int ret = 0; if (dev == NULL) return -1; @@ -2246,14 +2256,21 @@ int rte_vhost_vring_stats_reset(int vid, uint16_t queue_id) vq = dev->virtqueue[queue_id]; rte_rwlock_write_lock(&vq->access_lock); + + if (unlikely(!vq->access_ok)) { + ret = -1; + goto out_unlock; + } /* * No need to the reset atomic counters as such, due to the * above write access_lock preventing them to be updated. */ memset(&vq->stats, 0, sizeof(vq->stats)); + +out_unlock: rte_rwlock_write_unlock(&vq->access_lock); - return 0; + return ret; } int