From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id A79E9A04A3 for ; Tue, 5 Nov 2019 18:13:10 +0100 (CET) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 847B71BF05; Tue, 5 Nov 2019 18:13:10 +0100 (CET) Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by dpdk.org (Postfix) with ESMTP id A1F8F1BEF4; Tue, 5 Nov 2019 18:13:06 +0100 (CET) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga107.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 05 Nov 2019 09:12:58 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.68,271,1569308400"; d="scan'208";a="195886031" Received: from silpixa00399498.ir.intel.com (HELO silpixa00399498.ger.corp.intel.com) ([10.237.223.151]) by orsmga008.jf.intel.com with ESMTP; 05 Nov 2019 09:12:55 -0800 From: Anatoly Burakov To: dev@dpdk.org Cc: rajesh.ravi@broadcom.com, ajit.khaparde@broadcom.com, jonathan.richardson@broadcom.com, scott.branden@broadcom.com, vikram.prakash@broadcom.com, srinath.mannam@broadcom.com, thomas@monjalon.net, david.marchand@redhat.com, stable@dpdk.org Date: Tue, 5 Nov 2019 17:12:57 +0000 Message-Id: X-Mailer: git-send-email 2.17.1 Subject: [dpdk-stable] [PATCH 18.11] vfio: fix DMA mapping of externally allocated heaps X-BeenThere: stable@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches for DPDK stable branches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: stable-bounces@dpdk.org Sender: "stable" Currently, externally created heaps are supposed to be automatically mapped for VFIO DMA by EAL, however they only do so if, at the time of heap creation, VFIO is initialized and has at least one device available. If no devices are available at the time of heap creation (or if devices were available, but were since hot-unplugged, thus dropping all VFIO container mappings), then VFIO mapping code would have skipped over externally allocated heaps. The fix is to stop skipping external segments unconditionally, and instead check if each segment belongs to an internal or external heap. This way, we still skip over segments that are supposed to be mapped through the user maps, but we now map all of the heap segments. Fixes: 0f526d674f8e ("malloc: separate creating memseg list and malloc heap") Cc: stable@dpdk.org Signed-off-by: Anatoly Burakov --- lib/librte_eal/linuxapp/eal/eal_vfio.c | 61 ++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 4 deletions(-) diff --git a/lib/librte_eal/linuxapp/eal/eal_vfio.c b/lib/librte_eal/linuxapp/eal/eal_vfio.c index 830b320d70..463ed9999a 100644 --- a/lib/librte_eal/linuxapp/eal/eal_vfio.c +++ b/lib/librte_eal/linuxapp/eal/eal_vfio.c @@ -87,6 +87,35 @@ static const struct vfio_iommu_type iommu_types[] = { }, }; +static bool +is_heap_memseg_list(const struct rte_memseg_list *msl) +{ + struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config; + struct malloc_heap *heap; + int i; + + /* internal memsegs are always heap memsegs */ + if (!msl->external) + return true; + + for (i = 0; i < RTE_MAX_HEAPS; i++) { + int socket_id; + + heap = &mcfg->malloc_heaps[i]; + if (heap->total_size == 0) + continue; + + socket_id = heap->socket_id; + + /* if socket ID of the heap matches that of memseg list, we + * found the heap this segment belongs to. + */ + if (msl->socket_id == socket_id) + return true; + } + return false; +} + static int is_null_map(const struct user_mem_map *map) { @@ -1238,6 +1267,7 @@ type1_map_contig(const struct rte_memseg_list *msl, const struct rte_memseg *ms, { int *vfio_container_fd = arg; + /* skip over all externally allocated segments */ if (msl->external) return 0; @@ -1251,7 +1281,15 @@ type1_map(const struct rte_memseg_list *msl, const struct rte_memseg *ms, { int *vfio_container_fd = arg; - if (msl->external) + /* internal memsegs have already been mapped if IOVA as VA mode */ + if (rte_eal_iova_mode() == RTE_IOVA_VA && !msl->external) + return 0; + + if (!is_heap_memseg_list(msl)) + return 0; + + /* external segments may not have valid IOVA addresses */ + if (ms->iova == RTE_BAD_IOVA) return 0; return vfio_type1_dma_mem_map(*vfio_container_fd, ms->addr_64, ms->iova, @@ -1302,12 +1340,19 @@ vfio_type1_dma_mem_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova, static int vfio_type1_dma_map(int vfio_container_fd) { + int ret; + if (rte_eal_iova_mode() == RTE_IOVA_VA) { /* with IOVA as VA mode, we can get away with mapping contiguous * chunks rather than going page-by-page. */ - return rte_memseg_contig_walk(type1_map_contig, + ret = rte_memseg_contig_walk(type1_map_contig, &vfio_container_fd); + if (ret != 0) + return ret; + /* fall through to regular memseg walk because we also need to + * map external heaps. + */ } return rte_memseg_walk(type1_map, &vfio_container_fd); } @@ -1382,7 +1427,11 @@ vfio_spapr_map_walk(const struct rte_memseg_list *msl, { int *vfio_container_fd = arg; - if (msl->external) + if (!is_heap_memseg_list(msl)) + return 0; + + /* external segments may not have valid IOVA addresses */ + if (ms->iova == RTE_BAD_IOVA) return 0; return vfio_spapr_dma_do_map(*vfio_container_fd, ms->addr_64, ms->iova, @@ -1400,7 +1449,11 @@ vfio_spapr_window_size_walk(const struct rte_memseg_list *msl, struct spapr_walk_param *param = arg; uint64_t max = ms->iova + ms->len; - if (msl->external) + if (!is_heap_memseg_list(msl)) + return 0; + + /* external segments may not have valid IOVA addresses */ + if (ms->iova == RTE_BAD_IOVA) return 0; if (max > param->window_size) { -- 2.17.1