From patchwork Mon Oct 25 20:33:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxime Coquelin X-Patchwork-Id: 102795 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 85E8CA0C47; Mon, 25 Oct 2021 22:34:10 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 4FB1C41159; Mon, 25 Oct 2021 22:34:10 +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 D766C4003E for ; Mon, 25 Oct 2021 22:34:08 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1635194048; 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; bh=eCnHEsSGBY/DDpWeo7FvtneBd7u0MM2VRheKYoWjMME=; b=fzGj3Vy4ddByWuGL7m/dogFjoXTJ7iHsGZtYy3PY1QUojNokWVbsm47QBZNBY7m8f8zu/I RcdyRl5rtCqbdcRyqSOqFuseCRE1Wf8Zjyzke8OdsQibZisMml13TWyYKNxL1iBmstSiMz yrA0KVXftpPBc4+iOcrnTSnwyv+78So= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-588-u42wCVIqNzySpFqQ8_4S_g-1; Mon, 25 Oct 2021 16:34:05 -0400 X-MC-Unique: u42wCVIqNzySpFqQ8_4S_g-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E5B0A80A5C0; Mon, 25 Oct 2021 20:34:03 +0000 (UTC) Received: from max-t490s.redhat.com (unknown [10.39.208.37]) by smtp.corp.redhat.com (Postfix) with ESMTP id 223B017DBA; Mon, 25 Oct 2021 20:33:55 +0000 (UTC) From: Maxime Coquelin To: dev@dpdk.org, david.marchand@redhat.com, chenbo.xia@intel.com, xuan.ding@intel.com Cc: Maxime Coquelin Date: Mon, 25 Oct 2021 22:33:53 +0200 Message-Id: <20211025203353.147346-1-maxime.coquelin@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=maxime.coquelin@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [dpdk-dev] [PATCH] vhost: fix async DMA map 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 Sender: "dev" This patch fixes possible NULL-pointer dereferencing reported by Coverity and also fixes NUMA reallocation of the async DMA map. Fixes: 7c61fa08b716 ("vhost: enable IOMMU for async vhost") Coverity issue: 373655 Signed-off-by: Maxime Coquelin --- lib/vhost/vhost_user.c | 45 +++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/lib/vhost/vhost_user.c b/lib/vhost/vhost_user.c index 720d1c1c9d..b8fbb8b415 100644 --- a/lib/vhost/vhost_user.c +++ b/lib/vhost/vhost_user.c @@ -149,6 +149,9 @@ async_dma_map(struct rte_vhost_mem_region *region, bool *dma_map_success, bool d uint64_t host_iova; int ret = 0; + if (!rte_vfio_is_enabled("vfio")) + return 0; + host_iova = rte_mem_virt2iova((void *)(uintptr_t)region->host_user_addr); if (do_map) { /* Add mapped region into the default container of DPDK. */ @@ -176,9 +179,7 @@ async_dma_map(struct rte_vhost_mem_region *region, bool *dma_map_success, bool d VHOST_LOG_CONFIG(ERR, "DMA engine map failed\n"); return ret; - } - } else { /* No need to do vfio unmap if the map failed. */ if (!*dma_map_success) @@ -228,11 +229,8 @@ vhost_backend_cleanup(struct virtio_net *dev) free_mem_region(dev); rte_free(dev->mem); dev->mem = NULL; - - if (dev->async_map_status) { - rte_free(dev->async_map_status); - dev->async_map_status = NULL; - } + rte_free(dev->async_map_status); + dev->async_map_status = NULL; } rte_free(dev->guest_pages); @@ -689,16 +687,17 @@ numa_realloc(struct virtio_net *dev, int index) dev->mem = mem; if (dev->async_copy && rte_vfio_is_enabled("vfio")) { - if (dev->async_map_status == NULL) { - dev->async_map_status = rte_zmalloc_socket("async-dma-map-status", - sizeof(bool) * dev->mem->nregions, 0, node); - if (!dev->async_map_status) { - VHOST_LOG_CONFIG(ERR, - "(%d) failed to realloc dma mapping status on node\n", - dev->vid); - return dev; - } + bool *ams; + + ams = rte_realloc_socket(dev->async_map_status, sizeof(*ams) * dev->mem->nregions, + 0, node); + if (!ams) { + VHOST_LOG_CONFIG(ERR, "Failed to realloc dma mapping status on node %d\n", + node); + return dev; } + + dev->async_map_status = ams; } gp = rte_realloc_socket(dev->guest_pages, dev->max_guest_pages * sizeof(*gp), @@ -1294,16 +1293,14 @@ vhost_user_mmap_region(struct virtio_net *dev, if (dev->async_copy) { if (add_guest_pages(dev, region, alignment) < 0) { - VHOST_LOG_CONFIG(ERR, - "adding guest pages to region failed.\n"); + VHOST_LOG_CONFIG(ERR, "adding guest pages to region failed.\n"); return -1; } - if (rte_vfio_is_enabled("vfio")) { + if (dev->async_map_status) { ret = async_dma_map(region, &dev->async_map_status[region_index], true); if (ret) { - VHOST_LOG_CONFIG(ERR, "Configure IOMMU for DMA " - "engine failed\n"); + VHOST_LOG_CONFIG(ERR, "Configure IOMMU for DMA engine failed\n"); return -1; } } @@ -1501,10 +1498,8 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg, free_mem_region(dev); rte_free(dev->mem); dev->mem = NULL; - if (dev->async_map_status) { - rte_free(dev->async_map_status); - dev->async_map_status = NULL; - } + rte_free(dev->async_map_status); + dev->async_map_status = NULL; free_guest_pages: rte_free(dev->guest_pages); dev->guest_pages = NULL;