From patchwork Tue Oct 27 02:06:08 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Fu X-Patchwork-Id: 82257 X-Patchwork-Delegate: maxime.coquelin@redhat.com Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id CCBE0A04B5; Tue, 27 Oct 2020 03:18:47 +0100 (CET) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 71B871E2B; Tue, 27 Oct 2020 03:18:45 +0100 (CET) Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by dpdk.org (Postfix) with ESMTP id 711941D9E for ; Tue, 27 Oct 2020 03:18:44 +0100 (CET) IronPort-SDR: q1lIFXuZW1Phhl7Np2fbqGQuIQj01UyOoVNUOu12vCywjuEAEDD64LxqmcFQBO3kSKTyHsF1D4 OA/8gRpc6Gtg== X-IronPort-AV: E=McAfee;i="6000,8403,9786"; a="154984115" X-IronPort-AV: E=Sophos;i="5.77,422,1596524400"; d="scan'208";a="154984115" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Oct 2020 19:18:41 -0700 IronPort-SDR: NPOiFdaiZ3pCNPGNTozSh41zmCBVaGUKiryMx63FVCLDwc/a4N+onZnjTuoZ8l/DKG2z7Fk1wR hpKxrJ40kMVQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.77,422,1596524400"; d="scan'208";a="303736904" Received: from npg-dpdk-patrickfu-casc2.sh.intel.com ([10.67.119.92]) by fmsmga007.fm.intel.com with ESMTP; 26 Oct 2020 19:18:40 -0700 From: Patrick Fu To: dev@dpdk.org, maxime.coquelin@redhat.com, chenbo.xia@intel.com Cc: patrick.fu@intel.com Date: Tue, 27 Oct 2020 10:06:08 +0800 Message-Id: <20201027020608.4103145-1-patrick.fu@intel.com> X-Mailer: git-send-email 2.18.4 In-Reply-To: <20201026101042.4102442-1-patrick.fu@intel.com> References: <20201026101042.4102442-1-patrick.fu@intel.com> Subject: [dpdk-dev] [PATCH v1] vhost: fix guest/host physical address conversion X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 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" gpa_to_hpa() function almost always fails due to the wrong setup of the binary tree search key. Since there has already been a similar function gpa_to_first_hpa() available in the vhost, instead of fixing the issue in its original logic, gpa_to_hpa() function is rewritten to be a wrapper of the gpa_to_first_hpa() to avoid code redundancy. Fixes: e246896178e6 ("vhost: get guest/host physical address mappings") Fixes: faa9867c4da2 ("vhost: use binary search in address conversion") Signed-off-by: Patrick Fu Reviewed-by: Maxime Coquelin --- v2: - minor rewordings on commit message & title lib/librte_vhost/vhost.h | 43 ++++++++++------------------------------ 1 file changed, 11 insertions(+), 32 deletions(-) diff --git a/lib/librte_vhost/vhost.h b/lib/librte_vhost/vhost.h index 75d79f80a..361c9f79b 100644 --- a/lib/librte_vhost/vhost.h +++ b/lib/librte_vhost/vhost.h @@ -563,38 +563,6 @@ static __rte_always_inline int guest_page_addrcmp(const void *p1, return 0; } -/* Convert guest physical address to host physical address */ -static __rte_always_inline rte_iova_t -gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size) -{ - uint32_t i; - struct guest_page *page; - struct guest_page key; - - if (dev->nr_guest_pages >= VHOST_BINARY_SEARCH_THRESH) { - key.guest_phys_addr = gpa; - page = bsearch(&key, dev->guest_pages, dev->nr_guest_pages, - sizeof(struct guest_page), guest_page_addrcmp); - if (page) { - if (gpa + size < page->guest_phys_addr + page->size) - return gpa - page->guest_phys_addr + - page->host_phys_addr; - } - } else { - for (i = 0; i < dev->nr_guest_pages; i++) { - page = &dev->guest_pages[i]; - - if (gpa >= page->guest_phys_addr && - gpa + size < page->guest_phys_addr + - page->size) - return gpa - page->guest_phys_addr + - page->host_phys_addr; - } - } - - return 0; -} - static __rte_always_inline rte_iova_t gpa_to_first_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t gpa_size, uint64_t *hpa_size) @@ -645,6 +613,17 @@ gpa_to_first_hpa(struct virtio_net *dev, uint64_t gpa, return 0; } +/* Convert guest physical address to host physical address */ +static __rte_always_inline rte_iova_t +gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size) +{ + rte_iova_t hpa; + uint64_t hpa_size; + + hpa = gpa_to_first_hpa(dev, gpa, size, &hpa_size); + return hpa_size == size ? hpa : 0; +} + static __rte_always_inline uint64_t hva_to_gpa(struct virtio_net *dev, uint64_t vva, uint64_t len) {