LCOV - code coverage report
Current view: top level - spdk/lib/env_dpdk - memory.c (source / functions) Hit Total Coverage
Test: Combined Lines: 548 684 80.1 %
Date: 2024-07-13 01:17:18 Functions: 35 36 97.2 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 341 519 65.7 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2017 Intel Corporation.
       3                 :            :  *   All rights reserved.
       4                 :            :  */
       5                 :            : 
       6                 :            : #include "spdk/stdinc.h"
       7                 :            : 
       8                 :            : #include "env_internal.h"
       9                 :            : #include "pci_dpdk.h"
      10                 :            : 
      11                 :            : #include <rte_config.h>
      12                 :            : #include <rte_memory.h>
      13                 :            : #include <rte_eal_memconfig.h>
      14                 :            : #include <rte_dev.h>
      15                 :            : #include <rte_pci.h>
      16                 :            : 
      17                 :            : #include "spdk_internal/assert.h"
      18                 :            : 
      19                 :            : #include "spdk/assert.h"
      20                 :            : #include "spdk/likely.h"
      21                 :            : #include "spdk/queue.h"
      22                 :            : #include "spdk/util.h"
      23                 :            : #include "spdk/memory.h"
      24                 :            : #include "spdk/env_dpdk.h"
      25                 :            : #include "spdk/log.h"
      26                 :            : 
      27                 :            : #ifdef __linux__
      28                 :            : #include <linux/version.h>
      29                 :            : #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
      30                 :            : #include <linux/vfio.h>
      31                 :            : #include <rte_vfio.h>
      32                 :            : 
      33                 :            : struct spdk_vfio_dma_map {
      34                 :            :         struct vfio_iommu_type1_dma_map map;
      35                 :            :         TAILQ_ENTRY(spdk_vfio_dma_map) tailq;
      36                 :            : };
      37                 :            : 
      38                 :            : struct vfio_cfg {
      39                 :            :         int fd;
      40                 :            :         bool enabled;
      41                 :            :         bool noiommu_enabled;
      42                 :            :         unsigned device_ref;
      43                 :            :         TAILQ_HEAD(, spdk_vfio_dma_map) maps;
      44                 :            :         pthread_mutex_t mutex;
      45                 :            : };
      46                 :            : 
      47                 :            : static struct vfio_cfg g_vfio = {
      48                 :            :         .fd = -1,
      49                 :            :         .enabled = false,
      50                 :            :         .noiommu_enabled = false,
      51                 :            :         .device_ref = 0,
      52                 :            :         .maps = TAILQ_HEAD_INITIALIZER(g_vfio.maps),
      53                 :            :         .mutex = PTHREAD_MUTEX_INITIALIZER
      54                 :            : };
      55                 :            : #endif
      56                 :            : #endif
      57                 :            : 
      58                 :            : #if DEBUG
      59                 :            : #define DEBUG_PRINT(...) SPDK_ERRLOG(__VA_ARGS__)
      60                 :            : #else
      61                 :            : #define DEBUG_PRINT(...)
      62                 :            : #endif
      63                 :            : 
      64                 :            : #define FN_2MB_TO_4KB(fn)       (fn << (SHIFT_2MB - SHIFT_4KB))
      65                 :            : #define FN_4KB_TO_2MB(fn)       (fn >> (SHIFT_2MB - SHIFT_4KB))
      66                 :            : 
      67                 :            : #define MAP_256TB_IDX(vfn_2mb)  ((vfn_2mb) >> (SHIFT_1GB - SHIFT_2MB))
      68                 :            : #define MAP_1GB_IDX(vfn_2mb)    ((vfn_2mb) & ((1ULL << (SHIFT_1GB - SHIFT_2MB)) - 1))
      69                 :            : 
      70                 :            : /* Page is registered */
      71                 :            : #define REG_MAP_REGISTERED      (1ULL << 62)
      72                 :            : 
      73                 :            : /* A notification region barrier. The 2MB translation entry that's marked
      74                 :            :  * with this flag must be unregistered separately. This allows contiguous
      75                 :            :  * regions to be unregistered in the same chunks they were registered.
      76                 :            :  */
      77                 :            : #define REG_MAP_NOTIFY_START    (1ULL << 63)
      78                 :            : 
      79                 :            : /* Translation of a single 2MB page. */
      80                 :            : struct map_2mb {
      81                 :            :         uint64_t translation_2mb;
      82                 :            : };
      83                 :            : 
      84                 :            : /* Second-level map table indexed by bits [21..29] of the virtual address.
      85                 :            :  * Each entry contains the address translation or error for entries that haven't
      86                 :            :  * been retrieved yet.
      87                 :            :  */
      88                 :            : struct map_1gb {
      89                 :            :         struct map_2mb map[1ULL << (SHIFT_1GB - SHIFT_2MB)];
      90                 :            : };
      91                 :            : 
      92                 :            : /* Top-level map table indexed by bits [30..47] of the virtual address.
      93                 :            :  * Each entry points to a second-level map table or NULL.
      94                 :            :  */
      95                 :            : struct map_256tb {
      96                 :            :         struct map_1gb *map[1ULL << (SHIFT_256TB - SHIFT_1GB)];
      97                 :            : };
      98                 :            : 
      99                 :            : /* Page-granularity memory address translation */
     100                 :            : struct spdk_mem_map {
     101                 :            :         struct map_256tb map_256tb;
     102                 :            :         pthread_mutex_t mutex;
     103                 :            :         uint64_t default_translation;
     104                 :            :         struct spdk_mem_map_ops ops;
     105                 :            :         void *cb_ctx;
     106                 :            :         TAILQ_ENTRY(spdk_mem_map) tailq;
     107                 :            : };
     108                 :            : 
     109                 :            : /* Registrations map. The 64 bit translations are bit fields with the
     110                 :            :  * following layout (starting with the low bits):
     111                 :            :  *    0 - 61 : reserved
     112                 :            :  *   62 - 63 : flags
     113                 :            :  */
     114                 :            : static struct spdk_mem_map *g_mem_reg_map;
     115                 :            : static TAILQ_HEAD(spdk_mem_map_head, spdk_mem_map) g_spdk_mem_maps =
     116                 :            :         TAILQ_HEAD_INITIALIZER(g_spdk_mem_maps);
     117                 :            : static pthread_mutex_t g_spdk_mem_map_mutex = PTHREAD_MUTEX_INITIALIZER;
     118                 :            : 
     119                 :            : static bool g_legacy_mem;
     120                 :            : static bool g_huge_pages = true;
     121                 :            : 
     122                 :            : /*
     123                 :            :  * Walk the currently registered memory via the main memory registration map
     124                 :            :  * and call the new map's notify callback for each virtually contiguous region.
     125                 :            :  */
     126                 :            : static int
     127                 :       4394 : mem_map_notify_walk(struct spdk_mem_map *map, enum spdk_mem_map_notify_action action)
     128                 :            : {
     129                 :            :         size_t idx_256tb;
     130                 :            :         uint64_t idx_1gb;
     131                 :       4394 :         uint64_t contig_start = UINT64_MAX;
     132                 :       4394 :         uint64_t contig_end = UINT64_MAX;
     133                 :            :         struct map_1gb *map_1gb;
     134                 :            :         int rc;
     135                 :            : 
     136         [ -  + ]:       4394 :         if (!g_mem_reg_map) {
     137                 :          0 :                 return -EINVAL;
     138                 :            :         }
     139                 :            : 
     140                 :            :         /* Hold the memory registration map mutex so no new registrations can be added while we are looping. */
     141         [ -  + ]:       4394 :         pthread_mutex_lock(&g_mem_reg_map->mutex);
     142                 :            : 
     143         [ +  + ]:  135008513 :         for (idx_256tb = 0;
     144         [ +  + ]: 1011617573 :              idx_256tb < sizeof(g_mem_reg_map->map_256tb.map) / sizeof(g_mem_reg_map->map_256tb.map[0]);
     145                 : 1146617810 :              idx_256tb++) {
     146                 : 1146617829 :                 map_1gb = g_mem_reg_map->map_256tb.map[idx_256tb];
     147                 :            : 
     148         [ +  + ]: 1146617829 :                 if (!map_1gb) {
     149         [ -  + ]: 1146612820 :                         if (contig_start != UINT64_MAX) {
     150                 :            :                                 /* End of of a virtually contiguous range */
     151                 :          0 :                                 rc = map->ops.notify_cb(map->cb_ctx, map, action,
     152                 :            :                                                         (void *)contig_start,
     153                 :          0 :                                                         contig_end - contig_start + VALUE_2MB);
     154                 :            :                                 /* Don't bother handling unregister failures. It can't be any worse */
     155   [ #  #  #  # ]:          0 :                                 if (rc != 0 && action == SPDK_MEM_MAP_NOTIFY_REGISTER) {
     156                 :          0 :                                         goto err_unregister;
     157                 :            :                                 }
     158                 :            :                         }
     159                 : 1146612820 :                         contig_start = UINT64_MAX;
     160                 : 1146612820 :                         continue;
     161                 :            :                 }
     162                 :            : 
     163         [ +  + ]:    2617709 :                 for (idx_1gb = 0; idx_1gb < sizeof(map_1gb->map) / sizeof(map_1gb->map[0]); idx_1gb++) {
     164   [ +  +  +  + ]:    2612628 :                         if ((map_1gb->map[idx_1gb].translation_2mb & REG_MAP_REGISTERED) &&
     165                 :     538199 :                             (contig_start == UINT64_MAX ||
     166         [ +  + ]:     538199 :                              (map_1gb->map[idx_1gb].translation_2mb & REG_MAP_NOTIFY_START) == 0)) {
     167                 :            :                                 /* Rebuild the virtual address from the indexes */
     168                 :     542535 :                                 uint64_t vaddr = (idx_256tb << SHIFT_1GB) | (idx_1gb << SHIFT_2MB);
     169                 :            : 
     170         [ +  + ]:     542535 :                                 if (contig_start == UINT64_MAX) {
     171                 :      10976 :                                         contig_start = vaddr;
     172                 :            :                                 }
     173                 :            : 
     174                 :     542535 :                                 contig_end = vaddr;
     175                 :            :                         } else {
     176         [ +  + ]:    2070093 :                                 if (contig_start != UINT64_MAX) {
     177                 :            :                                         /* End of of a virtually contiguous range */
     178                 :      21216 :                                         rc = map->ops.notify_cb(map->cb_ctx, map, action,
     179                 :            :                                                                 (void *)contig_start,
     180                 :      10976 :                                                                 contig_end - contig_start + VALUE_2MB);
     181                 :            :                                         /* Don't bother handling unregister failures. It can't be any worse */
     182   [ +  +  +  - ]:      10976 :                                         if (rc != 0 && action == SPDK_MEM_MAP_NOTIFY_REGISTER) {
     183                 :         20 :                                                 goto err_unregister;
     184                 :            :                                         }
     185                 :            : 
     186                 :            :                                         /* This page might be a part of a neighbour region, so process
     187                 :            :                                          * it again. The idx_1gb will be incremented immediately.
     188                 :            :                                          */
     189                 :      10956 :                                         idx_1gb--;
     190                 :            :                                 }
     191                 :    2070073 :                                 contig_start = UINT64_MAX;
     192                 :            :                         }
     193                 :            :                 }
     194                 :            :         }
     195                 :            : 
     196         [ -  + ]:       4374 :         pthread_mutex_unlock(&g_mem_reg_map->mutex);
     197                 :       4374 :         return 0;
     198                 :            : 
     199                 :         20 : err_unregister:
     200                 :            :         /* Unwind to the first empty translation so we don't unregister
     201                 :            :          * a region that just failed to register.
     202                 :            :          */
     203                 :         20 :         idx_256tb = MAP_256TB_IDX((contig_start >> SHIFT_2MB) - 1);
     204                 :         20 :         idx_1gb = MAP_1GB_IDX((contig_start >> SHIFT_2MB) - 1);
     205                 :         20 :         contig_start = UINT64_MAX;
     206                 :         20 :         contig_end = UINT64_MAX;
     207                 :            : 
     208                 :            :         /* Unregister any memory we managed to register before the failure */
     209         [ +  + ]:         40 :         for (; idx_256tb < SIZE_MAX; idx_256tb--) {
     210                 :         20 :                 map_1gb = g_mem_reg_map->map_256tb.map[idx_256tb];
     211                 :            : 
     212         [ -  + ]:         20 :                 if (!map_1gb) {
     213         [ #  # ]:          0 :                         if (contig_end != UINT64_MAX) {
     214                 :            :                                 /* End of of a virtually contiguous range */
     215                 :          0 :                                 map->ops.notify_cb(map->cb_ctx, map,
     216                 :            :                                                    SPDK_MEM_MAP_NOTIFY_UNREGISTER,
     217                 :            :                                                    (void *)contig_start,
     218                 :          0 :                                                    contig_end - contig_start + VALUE_2MB);
     219                 :            :                         }
     220                 :          0 :                         contig_end = UINT64_MAX;
     221                 :          0 :                         continue;
     222                 :            :                 }
     223                 :            : 
     224         [ +  + ]:        180 :                 for (; idx_1gb < UINT64_MAX; idx_1gb--) {
     225                 :            :                         /* Rebuild the virtual address from the indexes */
     226                 :        160 :                         uint64_t vaddr = (idx_256tb << SHIFT_1GB) | (idx_1gb << SHIFT_2MB);
     227   [ +  +  -  + ]:        160 :                         if ((map_1gb->map[idx_1gb].translation_2mb & REG_MAP_REGISTERED) &&
     228         [ #  # ]:          0 :                             (contig_end == UINT64_MAX || (map_1gb->map[idx_1gb].translation_2mb & REG_MAP_NOTIFY_START) == 0)) {
     229                 :            : 
     230         [ +  - ]:         80 :                                 if (contig_end == UINT64_MAX) {
     231                 :         80 :                                         contig_end = vaddr;
     232                 :            :                                 }
     233                 :         80 :                                 contig_start = vaddr;
     234                 :            :                         } else {
     235         [ +  + ]:         80 :                                 if (contig_end != UINT64_MAX) {
     236         [ -  + ]:         60 :                                         if (map_1gb->map[idx_1gb].translation_2mb & REG_MAP_NOTIFY_START) {
     237                 :          0 :                                                 contig_start = vaddr;
     238                 :            :                                         }
     239                 :            :                                         /* End of of a virtually contiguous range */
     240                 :        108 :                                         map->ops.notify_cb(map->cb_ctx, map,
     241                 :            :                                                            SPDK_MEM_MAP_NOTIFY_UNREGISTER,
     242                 :            :                                                            (void *)contig_start,
     243                 :         60 :                                                            contig_end - contig_start + VALUE_2MB);
     244                 :            :                                 }
     245                 :         80 :                                 contig_end = UINT64_MAX;
     246                 :            :                         }
     247                 :            :                 }
     248                 :         20 :                 idx_1gb = sizeof(map_1gb->map) / sizeof(map_1gb->map[0]) - 1;
     249                 :            :         }
     250                 :            : 
     251         [ -  + ]:         20 :         pthread_mutex_unlock(&g_mem_reg_map->mutex);
     252                 :         20 :         return rc;
     253                 :            : }
     254                 :            : 
     255                 :            : struct spdk_mem_map *
     256                 :      10354 : spdk_mem_map_alloc(uint64_t default_translation, const struct spdk_mem_map_ops *ops, void *cb_ctx)
     257                 :            : {
     258                 :            :         struct spdk_mem_map *map;
     259                 :            :         int rc;
     260                 :            :         size_t i;
     261                 :            : 
     262                 :      10354 :         map = calloc(1, sizeof(*map));
     263         [ -  + ]:      10354 :         if (map == NULL) {
     264                 :          0 :                 return NULL;
     265                 :            :         }
     266                 :            : 
     267   [ -  +  -  + ]:      10354 :         if (pthread_mutex_init(&map->mutex, NULL)) {
     268                 :          0 :                 free(map);
     269                 :          0 :                 return NULL;
     270                 :            :         }
     271                 :            : 
     272                 :      10354 :         map->default_translation = default_translation;
     273                 :      10354 :         map->cb_ctx = cb_ctx;
     274         [ +  + ]:      10354 :         if (ops) {
     275                 :       7070 :                 map->ops = *ops;
     276                 :            :         }
     277                 :            : 
     278   [ +  +  +  + ]:      10354 :         if (ops && ops->notify_cb) {
     279         [ -  + ]:       3826 :                 pthread_mutex_lock(&g_spdk_mem_map_mutex);
     280                 :       3826 :                 rc = mem_map_notify_walk(map, SPDK_MEM_MAP_NOTIFY_REGISTER);
     281         [ +  + ]:       3826 :                 if (rc != 0) {
     282         [ -  + ]:         20 :                         pthread_mutex_unlock(&g_spdk_mem_map_mutex);
     283                 :         20 :                         DEBUG_PRINT("Initial mem_map notify failed\n");
     284         [ -  + ]:         20 :                         pthread_mutex_destroy(&map->mutex);
     285         [ +  + ]:    5242900 :                         for (i = 0; i < sizeof(map->map_256tb.map) / sizeof(map->map_256tb.map[0]); i++) {
     286                 :    5242880 :                                 free(map->map_256tb.map[i]);
     287                 :            :                         }
     288                 :         20 :                         free(map);
     289                 :         20 :                         return NULL;
     290                 :            :                 }
     291                 :       3806 :                 TAILQ_INSERT_TAIL(&g_spdk_mem_maps, map, tailq);
     292         [ -  + ]:       3806 :                 pthread_mutex_unlock(&g_spdk_mem_map_mutex);
     293                 :            :         }
     294                 :            : 
     295                 :      10334 :         return map;
     296                 :            : }
     297                 :            : 
     298                 :            : void
     299                 :        588 : spdk_mem_map_free(struct spdk_mem_map **pmap)
     300                 :            : {
     301                 :            :         struct spdk_mem_map *map;
     302                 :            :         size_t i;
     303                 :            : 
     304         [ -  + ]:        588 :         if (!pmap) {
     305                 :          0 :                 return;
     306                 :            :         }
     307                 :            : 
     308                 :        588 :         map = *pmap;
     309                 :            : 
     310         [ -  + ]:        588 :         if (!map) {
     311                 :          0 :                 return;
     312                 :            :         }
     313                 :            : 
     314         [ +  + ]:        588 :         if (map->ops.notify_cb) {
     315         [ -  + ]:        568 :                 pthread_mutex_lock(&g_spdk_mem_map_mutex);
     316                 :        568 :                 mem_map_notify_walk(map, SPDK_MEM_MAP_NOTIFY_UNREGISTER);
     317         [ +  + ]:        568 :                 TAILQ_REMOVE(&g_spdk_mem_maps, map, tailq);
     318         [ -  + ]:        568 :                 pthread_mutex_unlock(&g_spdk_mem_map_mutex);
     319                 :            :         }
     320                 :            : 
     321         [ +  + ]:  154141265 :         for (i = 0; i < sizeof(map->map_256tb.map) / sizeof(map->map_256tb.map[0]); i++) {
     322                 :  154140674 :                 free(map->map_256tb.map[i]);
     323                 :            :         }
     324                 :            : 
     325         [ -  + ]:        588 :         pthread_mutex_destroy(&map->mutex);
     326                 :            : 
     327                 :        588 :         free(map);
     328                 :        588 :         *pmap = NULL;
     329                 :            : }
     330                 :            : 
     331                 :            : int
     332                 :      44874 : spdk_mem_register(void *vaddr, size_t len)
     333                 :            : {
     334                 :            :         struct spdk_mem_map *map;
     335                 :            :         int rc;
     336                 :            :         void *seg_vaddr;
     337                 :            :         size_t seg_len;
     338                 :            :         uint64_t reg;
     339                 :            : 
     340         [ -  + ]:      44874 :         if ((uintptr_t)vaddr & ~MASK_256TB) {
     341                 :          0 :                 DEBUG_PRINT("invalid usermode virtual address %p\n", vaddr);
     342                 :          0 :                 return -EINVAL;
     343                 :            :         }
     344                 :            : 
     345   [ +  +  +  + ]:      44874 :         if (((uintptr_t)vaddr & MASK_2MB) || (len & MASK_2MB)) {
     346                 :         40 :                 DEBUG_PRINT("invalid %s parameters, vaddr=%p len=%ju\n",
     347                 :            :                             __func__, vaddr, len);
     348                 :         40 :                 return -EINVAL;
     349                 :            :         }
     350                 :            : 
     351         [ -  + ]:      44834 :         if (len == 0) {
     352                 :          0 :                 return 0;
     353                 :            :         }
     354                 :            : 
     355         [ -  + ]:      44834 :         pthread_mutex_lock(&g_spdk_mem_map_mutex);
     356                 :            : 
     357                 :      44834 :         seg_vaddr = vaddr;
     358                 :      44834 :         seg_len = len;
     359         [ +  + ]:    1214894 :         while (seg_len > 0) {
     360                 :    1170080 :                 reg = spdk_mem_map_translate(g_mem_reg_map, (uint64_t)seg_vaddr, NULL);
     361         [ +  + ]:    1170080 :                 if (reg & REG_MAP_REGISTERED) {
     362         [ -  + ]:         20 :                         pthread_mutex_unlock(&g_spdk_mem_map_mutex);
     363                 :         20 :                         return -EBUSY;
     364                 :            :                 }
     365                 :    1170060 :                 seg_vaddr += VALUE_2MB;
     366                 :    1170060 :                 seg_len -= VALUE_2MB;
     367                 :            :         }
     368                 :            : 
     369                 :      44814 :         seg_vaddr = vaddr;
     370                 :      44814 :         seg_len = 0;
     371         [ +  + ]:    1214854 :         while (len > 0) {
     372         [ +  + ]:    1170040 :                 spdk_mem_map_set_translation(g_mem_reg_map, (uint64_t)vaddr, VALUE_2MB,
     373                 :            :                                              seg_len == 0 ? REG_MAP_REGISTERED | REG_MAP_NOTIFY_START : REG_MAP_REGISTERED);
     374                 :    1170040 :                 seg_len += VALUE_2MB;
     375                 :    1170040 :                 vaddr += VALUE_2MB;
     376                 :    1170040 :                 len -= VALUE_2MB;
     377                 :            :         }
     378                 :            : 
     379         [ +  + ]:      87583 :         TAILQ_FOREACH(map, &g_spdk_mem_maps, tailq) {
     380                 :      42771 :                 rc = map->ops.notify_cb(map->cb_ctx, map, SPDK_MEM_MAP_NOTIFY_REGISTER, seg_vaddr, seg_len);
     381         [ +  + ]:      42771 :                 if (rc != 0) {
     382         [ #  # ]:          2 :                         pthread_mutex_unlock(&g_spdk_mem_map_mutex);
     383                 :          2 :                         return rc;
     384                 :            :                 }
     385                 :            :         }
     386                 :            : 
     387         [ -  + ]:      44812 :         pthread_mutex_unlock(&g_spdk_mem_map_mutex);
     388                 :      44812 :         return 0;
     389                 :            : }
     390                 :            : 
     391                 :            : int
     392                 :      41268 : spdk_mem_unregister(void *vaddr, size_t len)
     393                 :            : {
     394                 :            :         struct spdk_mem_map *map;
     395                 :            :         int rc;
     396                 :            :         void *seg_vaddr;
     397                 :            :         size_t seg_len;
     398                 :            :         uint64_t reg, newreg;
     399                 :            : 
     400         [ -  + ]:      41268 :         if ((uintptr_t)vaddr & ~MASK_256TB) {
     401                 :          0 :                 DEBUG_PRINT("invalid usermode virtual address %p\n", vaddr);
     402                 :          0 :                 return -EINVAL;
     403                 :            :         }
     404                 :            : 
     405   [ +  -  -  + ]:      41268 :         if (((uintptr_t)vaddr & MASK_2MB) || (len & MASK_2MB)) {
     406                 :          0 :                 DEBUG_PRINT("invalid %s parameters, vaddr=%p len=%ju\n",
     407                 :            :                             __func__, vaddr, len);
     408                 :          0 :                 return -EINVAL;
     409                 :            :         }
     410                 :            : 
     411         [ -  + ]:      41268 :         pthread_mutex_lock(&g_spdk_mem_map_mutex);
     412                 :            : 
     413                 :            :         /* The first page must be a start of a region. Also check if it's
     414                 :            :          * registered to make sure we don't return -ERANGE for non-registered
     415                 :            :          * regions.
     416                 :            :          */
     417                 :      41268 :         reg = spdk_mem_map_translate(g_mem_reg_map, (uint64_t)vaddr, NULL);
     418   [ +  +  +  + ]:      41268 :         if ((reg & REG_MAP_REGISTERED) && (reg & REG_MAP_NOTIFY_START) == 0) {
     419         [ -  + ]:         40 :                 pthread_mutex_unlock(&g_spdk_mem_map_mutex);
     420                 :         40 :                 return -ERANGE;
     421                 :            :         }
     422                 :            : 
     423                 :      41228 :         seg_vaddr = vaddr;
     424                 :      41228 :         seg_len = len;
     425         [ +  + ]:     867352 :         while (seg_len > 0) {
     426                 :     826144 :                 reg = spdk_mem_map_translate(g_mem_reg_map, (uint64_t)seg_vaddr, NULL);
     427         [ +  + ]:     826144 :                 if ((reg & REG_MAP_REGISTERED) == 0) {
     428         [ -  + ]:         20 :                         pthread_mutex_unlock(&g_spdk_mem_map_mutex);
     429                 :         20 :                         return -EINVAL;
     430                 :            :                 }
     431                 :     826124 :                 seg_vaddr += VALUE_2MB;
     432                 :     826124 :                 seg_len -= VALUE_2MB;
     433                 :            :         }
     434                 :            : 
     435                 :      41208 :         newreg = spdk_mem_map_translate(g_mem_reg_map, (uint64_t)seg_vaddr, NULL);
     436                 :            :         /* If the next page is registered, it must be a start of a region as well,
     437                 :            :          * otherwise we'd be unregistering only a part of a region.
     438                 :            :          */
     439   [ +  +  +  + ]:      41208 :         if ((newreg & REG_MAP_NOTIFY_START) == 0 && (newreg & REG_MAP_REGISTERED)) {
     440         [ -  + ]:         20 :                 pthread_mutex_unlock(&g_spdk_mem_map_mutex);
     441                 :         20 :                 return -ERANGE;
     442                 :            :         }
     443                 :      41188 :         seg_vaddr = vaddr;
     444                 :      41188 :         seg_len = 0;
     445                 :            : 
     446         [ +  + ]:     867292 :         while (len > 0) {
     447                 :     826104 :                 reg = spdk_mem_map_translate(g_mem_reg_map, (uint64_t)vaddr, NULL);
     448                 :     826104 :                 spdk_mem_map_set_translation(g_mem_reg_map, (uint64_t)vaddr, VALUE_2MB, 0);
     449                 :            : 
     450   [ +  +  +  + ]:     826104 :                 if (seg_len > 0 && (reg & REG_MAP_NOTIFY_START)) {
     451         [ +  + ]:        200 :                         TAILQ_FOREACH_REVERSE(map, &g_spdk_mem_maps, spdk_mem_map_head, tailq) {
     452                 :        100 :                                 rc = map->ops.notify_cb(map->cb_ctx, map, SPDK_MEM_MAP_NOTIFY_UNREGISTER, seg_vaddr, seg_len);
     453         [ -  + ]:        100 :                                 if (rc != 0) {
     454         [ #  # ]:          0 :                                         pthread_mutex_unlock(&g_spdk_mem_map_mutex);
     455                 :          0 :                                         return rc;
     456                 :            :                                 }
     457                 :            :                         }
     458                 :            : 
     459                 :        100 :                         seg_vaddr = vaddr;
     460                 :        100 :                         seg_len = VALUE_2MB;
     461                 :            :                 } else {
     462                 :     826004 :                         seg_len += VALUE_2MB;
     463                 :            :                 }
     464                 :            : 
     465                 :     826104 :                 vaddr += VALUE_2MB;
     466                 :     826104 :                 len -= VALUE_2MB;
     467                 :            :         }
     468                 :            : 
     469         [ +  - ]:      41188 :         if (seg_len > 0) {
     470         [ +  + ]:      83453 :                 TAILQ_FOREACH_REVERSE(map, &g_spdk_mem_maps, spdk_mem_map_head, tailq) {
     471                 :      42265 :                         rc = map->ops.notify_cb(map->cb_ctx, map, SPDK_MEM_MAP_NOTIFY_UNREGISTER, seg_vaddr, seg_len);
     472         [ -  + ]:      42265 :                         if (rc != 0) {
     473         [ #  # ]:          0 :                                 pthread_mutex_unlock(&g_spdk_mem_map_mutex);
     474                 :          0 :                                 return rc;
     475                 :            :                         }
     476                 :            :                 }
     477                 :            :         }
     478                 :            : 
     479         [ -  + ]:      41188 :         pthread_mutex_unlock(&g_spdk_mem_map_mutex);
     480                 :      41188 :         return 0;
     481                 :            : }
     482                 :            : 
     483                 :            : int
     484                 :          0 : spdk_mem_reserve(void *vaddr, size_t len)
     485                 :            : {
     486                 :            :         struct spdk_mem_map *map;
     487                 :            :         void *seg_vaddr;
     488                 :            :         size_t seg_len;
     489                 :            :         uint64_t reg;
     490                 :            : 
     491         [ #  # ]:          0 :         if ((uintptr_t)vaddr & ~MASK_256TB) {
     492                 :          0 :                 DEBUG_PRINT("invalid usermode virtual address %p\n", vaddr);
     493                 :          0 :                 return -EINVAL;
     494                 :            :         }
     495                 :            : 
     496   [ #  #  #  # ]:          0 :         if (((uintptr_t)vaddr & MASK_2MB) || (len & MASK_2MB)) {
     497                 :          0 :                 DEBUG_PRINT("invalid %s parameters, vaddr=%p len=%ju\n",
     498                 :            :                             __func__, vaddr, len);
     499                 :          0 :                 return -EINVAL;
     500                 :            :         }
     501                 :            : 
     502         [ #  # ]:          0 :         if (len == 0) {
     503                 :          0 :                 return 0;
     504                 :            :         }
     505                 :            : 
     506         [ #  # ]:          0 :         pthread_mutex_lock(&g_spdk_mem_map_mutex);
     507                 :            : 
     508                 :            :         /* Check if any part of this range is already registered */
     509                 :          0 :         seg_vaddr = vaddr;
     510                 :          0 :         seg_len = len;
     511         [ #  # ]:          0 :         while (seg_len > 0) {
     512                 :          0 :                 reg = spdk_mem_map_translate(g_mem_reg_map, (uint64_t)seg_vaddr, NULL);
     513         [ #  # ]:          0 :                 if (reg & REG_MAP_REGISTERED) {
     514         [ #  # ]:          0 :                         pthread_mutex_unlock(&g_spdk_mem_map_mutex);
     515                 :          0 :                         return -EBUSY;
     516                 :            :                 }
     517                 :          0 :                 seg_vaddr += VALUE_2MB;
     518                 :          0 :                 seg_len -= VALUE_2MB;
     519                 :            :         }
     520                 :            : 
     521                 :            :         /* Simply set the translation to the memory map's default. This allocates the space in the
     522                 :            :          * map but does not provide a valid translation. */
     523                 :          0 :         spdk_mem_map_set_translation(g_mem_reg_map, (uint64_t)vaddr, len,
     524                 :          0 :                                      g_mem_reg_map->default_translation);
     525                 :            : 
     526         [ #  # ]:          0 :         TAILQ_FOREACH(map, &g_spdk_mem_maps, tailq) {
     527                 :          0 :                 spdk_mem_map_set_translation(map, (uint64_t)vaddr, len, map->default_translation);
     528                 :            :         }
     529                 :            : 
     530         [ #  # ]:          0 :         pthread_mutex_unlock(&g_spdk_mem_map_mutex);
     531                 :          0 :         return 0;
     532                 :            : }
     533                 :            : 
     534                 :            : static struct map_1gb *
     535                 :    4237522 : mem_map_get_map_1gb(struct spdk_mem_map *map, uint64_t vfn_2mb)
     536                 :            : {
     537                 :            :         struct map_1gb *map_1gb;
     538                 :    4237522 :         uint64_t idx_256tb = MAP_256TB_IDX(vfn_2mb);
     539                 :            :         size_t i;
     540                 :            : 
     541         [ +  + ]:    4237522 :         if (spdk_unlikely(idx_256tb >= SPDK_COUNTOF(map->map_256tb.map))) {
     542                 :         20 :                 return NULL;
     543                 :            :         }
     544                 :            : 
     545                 :    4237502 :         map_1gb = map->map_256tb.map[idx_256tb];
     546                 :            : 
     547         [ +  + ]:    4237502 :         if (!map_1gb) {
     548         [ -  + ]:       8815 :                 pthread_mutex_lock(&map->mutex);
     549                 :            : 
     550                 :            :                 /* Recheck to make sure nobody else got the mutex first. */
     551                 :       8815 :                 map_1gb = map->map_256tb.map[idx_256tb];
     552         [ +  - ]:       8815 :                 if (!map_1gb) {
     553                 :       8815 :                         map_1gb = malloc(sizeof(struct map_1gb));
     554         [ +  - ]:       8815 :                         if (map_1gb) {
     555                 :            :                                 /* initialize all entries to default translation */
     556         [ +  + ]:    4522095 :                                 for (i = 0; i < SPDK_COUNTOF(map_1gb->map); i++) {
     557                 :    4513280 :                                         map_1gb->map[i].translation_2mb = map->default_translation;
     558                 :            :                                 }
     559                 :       8815 :                                 map->map_256tb.map[idx_256tb] = map_1gb;
     560                 :            :                         }
     561                 :            :                 }
     562                 :            : 
     563         [ -  + ]:       8815 :                 pthread_mutex_unlock(&map->mutex);
     564                 :            : 
     565         [ -  + ]:       8815 :                 if (!map_1gb) {
     566                 :          0 :                         DEBUG_PRINT("allocation failed\n");
     567                 :          0 :                         return NULL;
     568                 :            :                 }
     569                 :            :         }
     570                 :            : 
     571                 :    4237502 :         return map_1gb;
     572                 :            : }
     573                 :            : 
     574                 :            : int
     575                 :    4001548 : spdk_mem_map_set_translation(struct spdk_mem_map *map, uint64_t vaddr, uint64_t size,
     576                 :            :                              uint64_t translation)
     577                 :            : {
     578                 :            :         uint64_t vfn_2mb;
     579                 :            :         struct map_1gb *map_1gb;
     580                 :            :         uint64_t idx_1gb;
     581                 :            :         struct map_2mb *map_2mb;
     582                 :            : 
     583         [ +  + ]:    4001548 :         if ((uintptr_t)vaddr & ~MASK_256TB) {
     584                 :         20 :                 DEBUG_PRINT("invalid usermode virtual address %" PRIu64 "\n", vaddr);
     585                 :         20 :                 return -EINVAL;
     586                 :            :         }
     587                 :            : 
     588                 :            :         /* For now, only 2 MB-aligned registrations are supported */
     589   [ +  +  +  + ]:    4001528 :         if (((uintptr_t)vaddr & MASK_2MB) || (size & MASK_2MB)) {
     590                 :         40 :                 DEBUG_PRINT("invalid %s parameters, vaddr=%" PRIu64 " len=%" PRIu64 "\n",
     591                 :            :                             __func__, vaddr, size);
     592                 :         40 :                 return -EINVAL;
     593                 :            :         }
     594                 :            : 
     595                 :    4001488 :         vfn_2mb = vaddr >> SHIFT_2MB;
     596                 :            : 
     597         [ +  + ]:    8238990 :         while (size) {
     598                 :    4237522 :                 map_1gb = mem_map_get_map_1gb(map, vfn_2mb);
     599         [ +  + ]:    4237522 :                 if (!map_1gb) {
     600                 :         20 :                         DEBUG_PRINT("could not get %p map\n", (void *)vaddr);
     601                 :         20 :                         return -ENOMEM;
     602                 :            :                 }
     603                 :            : 
     604                 :    4237502 :                 idx_1gb = MAP_1GB_IDX(vfn_2mb);
     605                 :    4237502 :                 map_2mb = &map_1gb->map[idx_1gb];
     606                 :    4237502 :                 map_2mb->translation_2mb = translation;
     607                 :            : 
     608                 :    4237502 :                 size -= VALUE_2MB;
     609                 :    4237502 :                 vfn_2mb++;
     610                 :            :         }
     611                 :            : 
     612                 :    4001468 :         return 0;
     613                 :            : }
     614                 :            : 
     615                 :            : int
     616                 :     830574 : spdk_mem_map_clear_translation(struct spdk_mem_map *map, uint64_t vaddr, uint64_t size)
     617                 :            : {
     618                 :     830574 :         return spdk_mem_map_set_translation(map, vaddr, size, map->default_translation);
     619                 :            : }
     620                 :            : 
     621                 :            : inline uint64_t
     622                 :  139221914 : spdk_mem_map_translate(const struct spdk_mem_map *map, uint64_t vaddr, uint64_t *size)
     623                 :            : {
     624                 :            :         const struct map_1gb *map_1gb;
     625                 :            :         const struct map_2mb *map_2mb;
     626                 :            :         uint64_t idx_256tb;
     627                 :            :         uint64_t idx_1gb;
     628                 :            :         uint64_t vfn_2mb;
     629                 :            :         uint64_t cur_size;
     630                 :            :         uint64_t prev_translation;
     631                 :            :         uint64_t orig_translation;
     632                 :            : 
     633         [ -  + ]:  139221914 :         if (spdk_unlikely(vaddr & ~MASK_256TB)) {
     634                 :          0 :                 DEBUG_PRINT("invalid usermode virtual address %p\n", (void *)vaddr);
     635                 :          0 :                 return map->default_translation;
     636                 :            :         }
     637                 :            : 
     638                 :  139221914 :         vfn_2mb = vaddr >> SHIFT_2MB;
     639                 :  139221914 :         idx_256tb = MAP_256TB_IDX(vfn_2mb);
     640                 :  139221914 :         idx_1gb = MAP_1GB_IDX(vfn_2mb);
     641                 :            : 
     642                 :  139221914 :         map_1gb = map->map_256tb.map[idx_256tb];
     643         [ +  + ]:  139221914 :         if (spdk_unlikely(!map_1gb)) {
     644                 :     380725 :                 return map->default_translation;
     645                 :            :         }
     646                 :            : 
     647                 :  138841183 :         cur_size = VALUE_2MB - _2MB_OFFSET(vaddr);
     648                 :  138841183 :         map_2mb = &map_1gb->map[idx_1gb];
     649   [ +  +  +  + ]:  138841183 :         if (size == NULL || map->ops.are_contiguous == NULL ||
     650         [ +  + ]:   60527576 :             map_2mb->translation_2mb == map->default_translation) {
     651         [ +  + ]:   78313634 :                 if (size != NULL) {
     652                 :        200 :                         *size = spdk_min(*size, cur_size);
     653                 :            :                 }
     654                 :   78313634 :                 return map_2mb->translation_2mb;
     655                 :            :         }
     656                 :            : 
     657                 :   60527556 :         orig_translation = map_2mb->translation_2mb;
     658                 :   60527556 :         prev_translation = orig_translation;
     659         [ +  + ]:   60607152 :         while (cur_size < *size) {
     660                 :     149168 :                 vfn_2mb++;
     661                 :     149168 :                 idx_256tb = MAP_256TB_IDX(vfn_2mb);
     662                 :     149168 :                 idx_1gb = MAP_1GB_IDX(vfn_2mb);
     663                 :            : 
     664                 :     149168 :                 map_1gb = map->map_256tb.map[idx_256tb];
     665         [ -  + ]:     149168 :                 if (spdk_unlikely(!map_1gb)) {
     666                 :          0 :                         break;
     667                 :            :                 }
     668                 :            : 
     669                 :     149168 :                 map_2mb = &map_1gb->map[idx_1gb];
     670         [ +  + ]:     149168 :                 if (!map->ops.are_contiguous(prev_translation, map_2mb->translation_2mb)) {
     671                 :      69573 :                         break;
     672                 :            :                 }
     673                 :            : 
     674                 :      79595 :                 cur_size += VALUE_2MB;
     675                 :      79595 :                 prev_translation = map_2mb->translation_2mb;
     676                 :            :         }
     677                 :            : 
     678                 :   60527556 :         *size = spdk_min(*size, cur_size);
     679                 :   60527556 :         return orig_translation;
     680                 :            : }
     681                 :            : 
     682                 :            : static void
     683                 :      81018 : memory_hotplug_cb(enum rte_mem_event event_type,
     684                 :            :                   const void *addr, size_t len, void *arg)
     685                 :            : {
     686         [ +  + ]:      81018 :         if (event_type == RTE_MEM_EVENT_ALLOC) {
     687                 :      40652 :                 spdk_mem_register((void *)addr, len);
     688                 :            : 
     689         [ +  + ]:      40652 :                 if (!spdk_env_dpdk_external_init()) {
     690                 :      40634 :                         return;
     691                 :            :                 }
     692                 :            : 
     693                 :            :                 /* When the user initialized DPDK separately, we can't
     694                 :            :                  * be sure that --match-allocations RTE flag was specified.
     695                 :            :                  * Without this flag, DPDK can free memory in different units
     696                 :            :                  * than it was allocated. It doesn't work with things like RDMA MRs.
     697                 :            :                  *
     698                 :            :                  * For such cases, we mark segments so they aren't freed.
     699                 :            :                  */
     700         [ +  + ]:         42 :                 while (len > 0) {
     701                 :            :                         struct rte_memseg *seg;
     702                 :            : 
     703                 :         24 :                         seg = rte_mem_virt2memseg(addr, NULL);
     704         [ -  + ]:         24 :                         assert(seg != NULL);
     705                 :         24 :                         seg->flags |= RTE_MEMSEG_FLAG_DO_NOT_FREE;
     706                 :         24 :                         addr = (void *)((uintptr_t)addr + seg->hugepage_sz);
     707                 :         24 :                         len -= seg->hugepage_sz;
     708                 :            :                 }
     709         [ +  - ]:      40366 :         } else if (event_type == RTE_MEM_EVENT_FREE) {
     710                 :      40366 :                 spdk_mem_unregister((void *)addr, len);
     711                 :            :         }
     712                 :            : }
     713                 :            : 
     714                 :            : static int
     715                 :       3238 : memory_iter_cb(const struct rte_memseg_list *msl,
     716                 :            :                const struct rte_memseg *ms, size_t len, void *arg)
     717                 :            : {
     718                 :       3238 :         return spdk_mem_register(ms->addr, len);
     719                 :            : }
     720                 :            : 
     721                 :            : int
     722                 :       3264 : mem_map_init(bool legacy_mem)
     723                 :            : {
     724                 :       3264 :         g_legacy_mem = legacy_mem;
     725                 :            : 
     726                 :       3264 :         g_mem_reg_map = spdk_mem_map_alloc(0, NULL, NULL);
     727         [ -  + ]:       3264 :         if (g_mem_reg_map == NULL) {
     728                 :          0 :                 DEBUG_PRINT("memory registration map allocation failed\n");
     729                 :          0 :                 return -ENOMEM;
     730                 :            :         }
     731                 :            : 
     732                 :            :         /*
     733                 :            :          * Walk all DPDK memory segments and register them
     734                 :            :          * with the main memory map
     735                 :            :          */
     736   [ +  +  +  + ]:       3264 :         if (g_huge_pages) {
     737                 :       3258 :                 rte_mem_event_callback_register("spdk", memory_hotplug_cb, NULL);
     738                 :       3258 :                 rte_memseg_contig_walk(memory_iter_cb, NULL);
     739                 :            :         }
     740                 :       3264 :         return 0;
     741                 :            : }
     742                 :            : 
     743                 :            : bool
     744                 :      21477 : spdk_iommu_is_enabled(void)
     745                 :            : {
     746                 :            : #if VFIO_ENABLED
     747   [ -  +  +  +  :      21477 :         return g_vfio.enabled && !g_vfio.noiommu_enabled;
             -  +  +  - ]
     748                 :            : #else
     749                 :            :         return false;
     750                 :            : #endif
     751                 :            : }
     752                 :            : 
     753                 :            : struct spdk_vtophys_pci_device {
     754                 :            :         struct rte_pci_device *pci_device;
     755                 :            :         TAILQ_ENTRY(spdk_vtophys_pci_device) tailq;
     756                 :            : };
     757                 :            : 
     758                 :            : static pthread_mutex_t g_vtophys_pci_devices_mutex = PTHREAD_MUTEX_INITIALIZER;
     759                 :            : static TAILQ_HEAD(, spdk_vtophys_pci_device) g_vtophys_pci_devices =
     760                 :            :         TAILQ_HEAD_INITIALIZER(g_vtophys_pci_devices);
     761                 :            : 
     762                 :            : static struct spdk_mem_map *g_vtophys_map;
     763                 :            : static struct spdk_mem_map *g_phys_ref_map;
     764                 :            : 
     765                 :            : #if VFIO_ENABLED
     766                 :            : static int
     767                 :        712 : _vfio_iommu_map_dma(uint64_t vaddr, uint64_t iova, uint64_t size)
     768                 :            : {
     769                 :            :         struct spdk_vfio_dma_map *dma_map;
     770                 :            :         int ret;
     771                 :            : 
     772                 :        712 :         dma_map = calloc(1, sizeof(*dma_map));
     773         [ -  + ]:        712 :         if (dma_map == NULL) {
     774                 :          0 :                 return -ENOMEM;
     775                 :            :         }
     776                 :            : 
     777                 :        712 :         dma_map->map.argsz = sizeof(dma_map->map);
     778                 :        712 :         dma_map->map.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
     779                 :        712 :         dma_map->map.vaddr = vaddr;
     780                 :        712 :         dma_map->map.iova = iova;
     781                 :        712 :         dma_map->map.size = size;
     782                 :            : 
     783         [ +  + ]:        712 :         if (g_vfio.device_ref == 0) {
     784                 :            :                 /* VFIO requires at least one device (IOMMU group) to be added to
     785                 :            :                  * a VFIO container before it is possible to perform any IOMMU
     786                 :            :                  * operations on that container. This memory will be mapped once
     787                 :            :                  * the first device (IOMMU group) is hotplugged.
     788                 :            :                  *
     789                 :            :                  * Since the vfio container is managed internally by DPDK, it is
     790                 :            :                  * also possible that some device is already in that container, but
     791                 :            :                  * it's not managed by SPDK -  e.g. an NIC attached internally
     792                 :            :                  * inside DPDK. We could map the memory straight away in such
     793                 :            :                  * scenario, but there's no need to do it. DPDK devices clearly
     794                 :            :                  * don't need our mappings and hence we defer the mapping
     795                 :            :                  * unconditionally until the first SPDK-managed device is
     796                 :            :                  * hotplugged.
     797                 :            :                  */
     798                 :        600 :                 goto out_insert;
     799                 :            :         }
     800                 :            : 
     801                 :        112 :         ret = ioctl(g_vfio.fd, VFIO_IOMMU_MAP_DMA, &dma_map->map);
     802         [ +  - ]:        112 :         if (ret) {
     803                 :            :                 /* There are cases the vfio container doesn't have IOMMU group, it's safe for this case */
     804                 :          0 :                 SPDK_NOTICELOG("Cannot set up DMA mapping, error %d, ignored\n", errno);
     805                 :            :         }
     806                 :            : 
     807                 :        112 : out_insert:
     808                 :        712 :         TAILQ_INSERT_TAIL(&g_vfio.maps, dma_map, tailq);
     809                 :        712 :         return 0;
     810                 :            : }
     811                 :            : 
     812                 :            : 
     813                 :            : static int
     814                 :        203 : vtophys_iommu_map_dma(uint64_t vaddr, uint64_t iova, uint64_t size)
     815                 :            : {
     816                 :            :         uint64_t refcount;
     817                 :            :         int ret;
     818                 :            : 
     819                 :        203 :         refcount = spdk_mem_map_translate(g_phys_ref_map, iova, NULL);
     820         [ -  + ]:        203 :         assert(refcount < UINT64_MAX);
     821         [ -  + ]:        203 :         if (refcount > 0) {
     822                 :          0 :                 spdk_mem_map_set_translation(g_phys_ref_map, iova, size, refcount + 1);
     823                 :          0 :                 return 0;
     824                 :            :         }
     825                 :            : 
     826         [ -  + ]:        203 :         pthread_mutex_lock(&g_vfio.mutex);
     827                 :        203 :         ret = _vfio_iommu_map_dma(vaddr, iova, size);
     828         [ -  + ]:        203 :         pthread_mutex_unlock(&g_vfio.mutex);
     829         [ -  + ]:        203 :         if (ret) {
     830                 :          0 :                 return ret;
     831                 :            :         }
     832                 :            : 
     833                 :        203 :         spdk_mem_map_set_translation(g_phys_ref_map, iova, size, refcount + 1);
     834                 :        203 :         return 0;
     835                 :            : }
     836                 :            : 
     837                 :            : int
     838                 :        509 : vtophys_iommu_map_dma_bar(uint64_t vaddr, uint64_t iova, uint64_t size)
     839                 :            : {
     840                 :            :         int ret;
     841                 :            : 
     842         [ -  + ]:        509 :         pthread_mutex_lock(&g_vfio.mutex);
     843                 :        509 :         ret = _vfio_iommu_map_dma(vaddr, iova, size);
     844         [ -  + ]:        509 :         pthread_mutex_unlock(&g_vfio.mutex);
     845                 :            : 
     846                 :        509 :         return ret;
     847                 :            : }
     848                 :            : 
     849                 :            : static int
     850                 :        688 : _vfio_iommu_unmap_dma(struct spdk_vfio_dma_map *dma_map)
     851                 :            : {
     852                 :        688 :         struct vfio_iommu_type1_dma_unmap unmap = {};
     853                 :            :         int ret;
     854                 :            : 
     855         [ +  + ]:        688 :         if (g_vfio.device_ref == 0) {
     856                 :            :                 /* Memory is not mapped anymore, just remove it's references */
     857                 :         91 :                 goto out_remove;
     858                 :            :         }
     859                 :            : 
     860                 :        597 :         unmap.argsz = sizeof(unmap);
     861                 :        597 :         unmap.flags = 0;
     862                 :        597 :         unmap.iova = dma_map->map.iova;
     863                 :        597 :         unmap.size = dma_map->map.size;
     864                 :        597 :         ret = ioctl(g_vfio.fd, VFIO_IOMMU_UNMAP_DMA, &unmap);
     865         [ +  - ]:        597 :         if (ret) {
     866                 :          0 :                 SPDK_NOTICELOG("Cannot clear DMA mapping, error %d, ignored\n", errno);
     867                 :            :         }
     868                 :            : 
     869                 :        634 : out_remove:
     870         [ +  + ]:        688 :         TAILQ_REMOVE(&g_vfio.maps, dma_map, tailq);
     871                 :        688 :         free(dma_map);
     872                 :        688 :         return 0;
     873                 :            : }
     874                 :            : 
     875                 :            : static int
     876                 :        203 : vtophys_iommu_unmap_dma(uint64_t iova, uint64_t size)
     877                 :            : {
     878                 :            :         struct spdk_vfio_dma_map *dma_map;
     879                 :            :         uint64_t refcount;
     880                 :            :         int ret;
     881                 :            : 
     882         [ -  + ]:        203 :         pthread_mutex_lock(&g_vfio.mutex);
     883         [ +  - ]:        484 :         TAILQ_FOREACH(dma_map, &g_vfio.maps, tailq) {
     884         [ +  + ]:        484 :                 if (dma_map->map.iova == iova) {
     885                 :        203 :                         break;
     886                 :            :                 }
     887                 :            :         }
     888                 :            : 
     889         [ -  + ]:        203 :         if (dma_map == NULL) {
     890                 :          0 :                 DEBUG_PRINT("Cannot clear DMA mapping for IOVA %"PRIx64" - it's not mapped\n", iova);
     891         [ #  # ]:          0 :                 pthread_mutex_unlock(&g_vfio.mutex);
     892                 :          0 :                 return -ENXIO;
     893                 :            :         }
     894                 :            : 
     895                 :        203 :         refcount = spdk_mem_map_translate(g_phys_ref_map, iova, NULL);
     896         [ -  + ]:        203 :         assert(refcount < UINT64_MAX);
     897         [ +  - ]:        203 :         if (refcount > 0) {
     898                 :        203 :                 spdk_mem_map_set_translation(g_phys_ref_map, iova, size, refcount - 1);
     899                 :            :         }
     900                 :            : 
     901                 :            :         /* We still have outstanding references, don't clear it. */
     902         [ -  + ]:        203 :         if (refcount > 1) {
     903         [ #  # ]:          0 :                 pthread_mutex_unlock(&g_vfio.mutex);
     904                 :          0 :                 return 0;
     905                 :            :         }
     906                 :            : 
     907                 :            :         /** don't support partial or multiple-page unmap for now */
     908         [ -  + ]:        203 :         assert(dma_map->map.size == size);
     909                 :            : 
     910                 :        203 :         ret = _vfio_iommu_unmap_dma(dma_map);
     911         [ -  + ]:        203 :         pthread_mutex_unlock(&g_vfio.mutex);
     912                 :            : 
     913                 :        203 :         return ret;
     914                 :            : }
     915                 :            : 
     916                 :            : int
     917                 :        485 : vtophys_iommu_unmap_dma_bar(uint64_t vaddr)
     918                 :            : {
     919                 :            :         struct spdk_vfio_dma_map *dma_map;
     920                 :            :         int ret;
     921                 :            : 
     922         [ -  + ]:        485 :         pthread_mutex_lock(&g_vfio.mutex);
     923         [ +  - ]:        494 :         TAILQ_FOREACH(dma_map, &g_vfio.maps, tailq) {
     924         [ +  + ]:        494 :                 if (dma_map->map.vaddr == vaddr) {
     925                 :        485 :                         break;
     926                 :            :                 }
     927                 :            :         }
     928                 :            : 
     929         [ -  + ]:        485 :         if (dma_map == NULL) {
     930                 :          0 :                 DEBUG_PRINT("Cannot clear DMA mapping for address %"PRIx64" - it's not mapped\n", vaddr);
     931         [ #  # ]:          0 :                 pthread_mutex_unlock(&g_vfio.mutex);
     932                 :          0 :                 return -ENXIO;
     933                 :            :         }
     934                 :            : 
     935                 :        485 :         ret = _vfio_iommu_unmap_dma(dma_map);
     936         [ -  + ]:        485 :         pthread_mutex_unlock(&g_vfio.mutex);
     937                 :        485 :         return ret;
     938                 :            : }
     939                 :            : #endif
     940                 :            : 
     941                 :            : static uint64_t
     942                 :    1199557 : vtophys_get_paddr_memseg(uint64_t vaddr)
     943                 :            : {
     944                 :            :         uintptr_t paddr;
     945                 :            :         struct rte_memseg *seg;
     946                 :            : 
     947                 :    1199557 :         seg = rte_mem_virt2memseg((void *)(uintptr_t)vaddr, NULL);
     948         [ +  + ]:    1199557 :         if (seg != NULL) {
     949                 :    1198471 :                 paddr = seg->iova;
     950         [ -  + ]:    1198471 :                 if (paddr == RTE_BAD_IOVA) {
     951                 :          0 :                         return SPDK_VTOPHYS_ERROR;
     952                 :            :                 }
     953                 :    1198471 :                 paddr += (vaddr - (uintptr_t)seg->addr);
     954                 :    1198471 :                 return paddr;
     955                 :            :         }
     956                 :            : 
     957                 :       1086 :         return SPDK_VTOPHYS_ERROR;
     958                 :            : }
     959                 :            : 
     960                 :            : /* Try to get the paddr from /proc/self/pagemap */
     961                 :            : static uint64_t
     962                 :      16571 : vtophys_get_paddr_pagemap(uint64_t vaddr)
     963                 :            : {
     964                 :            :         uintptr_t paddr;
     965                 :            : 
     966                 :            :         /* Silence static analyzers */
     967         [ -  + ]:      16571 :         assert(vaddr != 0);
     968                 :      16571 :         paddr = rte_mem_virt2iova((void *)vaddr);
     969         [ +  + ]:      16571 :         if (paddr == RTE_BAD_IOVA) {
     970                 :            :                 /*
     971                 :            :                  * The vaddr may be valid but doesn't have a backing page
     972                 :            :                  * assigned yet.  Touch the page to ensure a backing page
     973                 :            :                  * gets assigned, then try to translate again.
     974                 :            :                  */
     975                 :       1174 :                 rte_atomic64_read((rte_atomic64_t *)vaddr);
     976                 :       1174 :                 paddr = rte_mem_virt2iova((void *)vaddr);
     977                 :            :         }
     978         [ +  + ]:      16571 :         if (paddr == RTE_BAD_IOVA) {
     979                 :            :                 /* Unable to get to the physical address. */
     980                 :          2 :                 return SPDK_VTOPHYS_ERROR;
     981                 :            :         }
     982                 :            : 
     983                 :      16569 :         return paddr;
     984                 :            : }
     985                 :            : 
     986                 :            : static uint64_t
     987                 :       3160 : pci_device_vtophys(struct rte_pci_device *dev, uint64_t vaddr, size_t len)
     988                 :            : {
     989                 :            :         struct rte_mem_resource *res;
     990                 :            :         uint64_t paddr;
     991                 :            :         unsigned r;
     992                 :            : 
     993         [ +  + ]:      18280 :         for (r = 0; r < PCI_MAX_RESOURCE; r++) {
     994                 :      16784 :                 res = dpdk_pci_device_get_mem_resource(dev, r);
     995                 :            : 
     996   [ +  +  +  + ]:      16784 :                 if (res->phys_addr == 0 || vaddr < (uint64_t)res->addr ||
     997         [ +  + ]:       6492 :                     (vaddr + len) >= (uint64_t)res->addr + res->len) {
     998                 :      15120 :                         continue;
     999                 :            :                 }
    1000                 :            : 
    1001                 :            : #if VFIO_ENABLED
    1002   [ -  +  -  - ]:       1664 :                 if (spdk_iommu_is_enabled() && rte_eal_iova_mode() == RTE_IOVA_VA) {
    1003                 :            :                         /*
    1004                 :            :                          * The IOMMU is on and we're using IOVA == VA. The BAR was
    1005                 :            :                          * automatically registered when it was mapped, so just return
    1006                 :            :                          * the virtual address here.
    1007                 :            :                          */
    1008                 :          0 :                         return vaddr;
    1009                 :            :                 }
    1010                 :            : #endif
    1011                 :       1664 :                 paddr = res->phys_addr + (vaddr - (uint64_t)res->addr);
    1012                 :       1664 :                 return paddr;
    1013                 :            :         }
    1014                 :            : 
    1015                 :       1496 :         return SPDK_VTOPHYS_ERROR;
    1016                 :            : }
    1017                 :            : 
    1018                 :            : /* Try to get the paddr from pci devices */
    1019                 :            : static uint64_t
    1020                 :       2658 : vtophys_get_paddr_pci(uint64_t vaddr, size_t len)
    1021                 :            : {
    1022                 :            :         struct spdk_vtophys_pci_device *vtophys_dev;
    1023                 :            :         uintptr_t paddr;
    1024                 :            :         struct rte_pci_device   *dev;
    1025                 :            : 
    1026         [ -  + ]:       2658 :         pthread_mutex_lock(&g_vtophys_pci_devices_mutex);
    1027         [ +  + ]:       4154 :         TAILQ_FOREACH(vtophys_dev, &g_vtophys_pci_devices, tailq) {
    1028                 :       3160 :                 dev = vtophys_dev->pci_device;
    1029                 :       3160 :                 paddr = pci_device_vtophys(dev, vaddr, len);
    1030         [ +  + ]:       3160 :                 if (paddr != SPDK_VTOPHYS_ERROR) {
    1031         [ #  # ]:       1664 :                         pthread_mutex_unlock(&g_vtophys_pci_devices_mutex);
    1032                 :       1664 :                         return paddr;
    1033                 :            :                 }
    1034                 :            :         }
    1035         [ -  + ]:        994 :         pthread_mutex_unlock(&g_vtophys_pci_devices_mutex);
    1036                 :            : 
    1037                 :        994 :         return SPDK_VTOPHYS_ERROR;
    1038                 :            : }
    1039                 :            : 
    1040                 :            : static int
    1041                 :      85342 : vtophys_notify(void *cb_ctx, struct spdk_mem_map *map,
    1042                 :            :                enum spdk_mem_map_notify_action action,
    1043                 :            :                void *vaddr, size_t len)
    1044                 :            : {
    1045                 :      85342 :         int rc = 0;
    1046                 :            :         uint64_t paddr;
    1047                 :            : 
    1048         [ -  + ]:      85342 :         if ((uintptr_t)vaddr & ~MASK_256TB) {
    1049                 :          0 :                 DEBUG_PRINT("invalid usermode virtual address %p\n", vaddr);
    1050                 :          0 :                 return -EINVAL;
    1051                 :            :         }
    1052                 :            : 
    1053   [ +  -  -  + ]:      85342 :         if (((uintptr_t)vaddr & MASK_2MB) || (len & MASK_2MB)) {
    1054                 :          0 :                 DEBUG_PRINT("invalid parameters, vaddr=%p len=%ju\n",
    1055                 :            :                             vaddr, len);
    1056                 :          0 :                 return -EINVAL;
    1057                 :            :         }
    1058                 :            : 
    1059                 :            :         /* Get the physical address from the DPDK memsegs */
    1060                 :      85342 :         paddr = vtophys_get_paddr_memseg((uint64_t)vaddr);
    1061                 :            : 
    1062      [ +  +  - ]:      85342 :         switch (action) {
    1063                 :      44434 :         case SPDK_MEM_MAP_NOTIFY_REGISTER:
    1064         [ +  + ]:      44434 :                 if (paddr == SPDK_VTOPHYS_ERROR) {
    1065                 :            :                         /* This is not an address that DPDK is managing. */
    1066                 :            : 
    1067                 :            :                         /* Check if this is a PCI BAR. They need special handling */
    1068                 :        544 :                         paddr = vtophys_get_paddr_pci((uint64_t)vaddr, len);
    1069         [ +  + ]:        544 :                         if (paddr != SPDK_VTOPHYS_ERROR) {
    1070                 :            :                                 /* Get paddr for each 2MB chunk in this address range */
    1071         [ +  + ]:        832 :                                 while (len > 0) {
    1072                 :        786 :                                         paddr = vtophys_get_paddr_pci((uint64_t)vaddr, VALUE_2MB);
    1073         [ -  + ]:        786 :                                         if (paddr == SPDK_VTOPHYS_ERROR) {
    1074                 :          0 :                                                 DEBUG_PRINT("could not get phys addr for %p\n", vaddr);
    1075                 :          0 :                                                 return -EFAULT;
    1076                 :            :                                         }
    1077                 :            : 
    1078                 :        786 :                                         rc = spdk_mem_map_set_translation(map, (uint64_t)vaddr, VALUE_2MB, paddr);
    1079         [ -  + ]:        786 :                                         if (rc != 0) {
    1080                 :          0 :                                                 return rc;
    1081                 :            :                                         }
    1082                 :            : 
    1083                 :        786 :                                         vaddr += VALUE_2MB;
    1084                 :        786 :                                         len -= VALUE_2MB;
    1085                 :            :                                 }
    1086                 :            : 
    1087                 :         46 :                                 return 0;
    1088                 :            :                         }
    1089                 :            : 
    1090                 :            : #if VFIO_ENABLED
    1091                 :            :                         enum rte_iova_mode iova_mode;
    1092                 :            : 
    1093                 :        498 :                         iova_mode = rte_eal_iova_mode();
    1094                 :            : 
    1095   [ +  +  +  - ]:        701 :                         if (spdk_iommu_is_enabled() && iova_mode == RTE_IOVA_VA) {
    1096                 :            :                                 /* We'll use the virtual address as the iova to match DPDK. */
    1097                 :        203 :                                 paddr = (uint64_t)vaddr;
    1098                 :        203 :                                 rc = vtophys_iommu_map_dma((uint64_t)vaddr, paddr, len);
    1099         [ -  + ]:        203 :                                 if (rc) {
    1100                 :          0 :                                         return -EFAULT;
    1101                 :            :                                 }
    1102         [ +  + ]:      38260 :                                 while (len > 0) {
    1103                 :      38057 :                                         rc = spdk_mem_map_set_translation(map, (uint64_t)vaddr, VALUE_2MB, paddr);
    1104         [ -  + ]:      38057 :                                         if (rc != 0) {
    1105                 :          0 :                                                 return rc;
    1106                 :            :                                         }
    1107                 :      38057 :                                         vaddr += VALUE_2MB;
    1108                 :      38057 :                                         paddr += VALUE_2MB;
    1109                 :      38057 :                                         len -= VALUE_2MB;
    1110                 :            :                                 }
    1111                 :            :                         } else
    1112                 :            : #endif
    1113                 :            :                         {
    1114                 :            :                                 /* Get the physical address from /proc/self/pagemap. */
    1115                 :        295 :                                 paddr = vtophys_get_paddr_pagemap((uint64_t)vaddr);
    1116         [ +  + ]:        295 :                                 if (paddr == SPDK_VTOPHYS_ERROR) {
    1117                 :          2 :                                         DEBUG_PRINT("could not get phys addr for %p\n", vaddr);
    1118                 :          2 :                                         return -EFAULT;
    1119                 :            :                                 }
    1120                 :            : 
    1121                 :            :                                 /* Get paddr for each 2MB chunk in this address range */
    1122         [ +  + ]:      16569 :                                 while (len > 0) {
    1123                 :            :                                         /* Get the physical address from /proc/self/pagemap. */
    1124                 :      16276 :                                         paddr = vtophys_get_paddr_pagemap((uint64_t)vaddr);
    1125                 :            : 
    1126         [ -  + ]:      16276 :                                         if (paddr == SPDK_VTOPHYS_ERROR) {
    1127                 :          0 :                                                 DEBUG_PRINT("could not get phys addr for %p\n", vaddr);
    1128                 :          0 :                                                 return -EFAULT;
    1129                 :            :                                         }
    1130                 :            : 
    1131         [ -  + ]:      16276 :                                         if (paddr & MASK_2MB) {
    1132                 :          0 :                                                 DEBUG_PRINT("invalid paddr 0x%" PRIx64 " - must be 2MB aligned\n", paddr);
    1133                 :          0 :                                                 return -EINVAL;
    1134                 :            :                                         }
    1135                 :            : #if VFIO_ENABLED
    1136                 :            :                                         /* If the IOMMU is on, but DPDK is using iova-mode=pa, we want to register this memory
    1137                 :            :                                          * with the IOMMU using the physical address to match. */
    1138         [ -  + ]:      16276 :                                         if (spdk_iommu_is_enabled()) {
    1139                 :          0 :                                                 rc = vtophys_iommu_map_dma((uint64_t)vaddr, paddr, VALUE_2MB);
    1140         [ #  # ]:          0 :                                                 if (rc) {
    1141                 :          0 :                                                         DEBUG_PRINT("Unable to assign vaddr %p to paddr 0x%" PRIx64 "\n", vaddr, paddr);
    1142                 :          0 :                                                         return -EFAULT;
    1143                 :            :                                                 }
    1144                 :            :                                         }
    1145                 :            : #endif
    1146                 :            : 
    1147                 :      16276 :                                         rc = spdk_mem_map_set_translation(map, (uint64_t)vaddr, VALUE_2MB, paddr);
    1148         [ -  + ]:      16276 :                                         if (rc != 0) {
    1149                 :          0 :                                                 return rc;
    1150                 :            :                                         }
    1151                 :            : 
    1152                 :      16276 :                                         vaddr += VALUE_2MB;
    1153                 :      16276 :                                         len -= VALUE_2MB;
    1154                 :            :                                 }
    1155                 :            :                         }
    1156                 :            :                 } else {
    1157                 :            :                         /* This is an address managed by DPDK. Just setup the translations. */
    1158         [ +  + ]:    1158105 :                         while (len > 0) {
    1159                 :    1114215 :                                 paddr = vtophys_get_paddr_memseg((uint64_t)vaddr);
    1160         [ -  + ]:    1114215 :                                 if (paddr == SPDK_VTOPHYS_ERROR) {
    1161                 :          0 :                                         DEBUG_PRINT("could not get phys addr for %p\n", vaddr);
    1162                 :          0 :                                         return -EFAULT;
    1163                 :            :                                 }
    1164                 :            : 
    1165                 :    1114215 :                                 rc = spdk_mem_map_set_translation(map, (uint64_t)vaddr, VALUE_2MB, paddr);
    1166         [ -  + ]:    1114215 :                                 if (rc != 0) {
    1167                 :          0 :                                         return rc;
    1168                 :            :                                 }
    1169                 :            : 
    1170                 :    1114215 :                                 vaddr += VALUE_2MB;
    1171                 :    1114215 :                                 len -= VALUE_2MB;
    1172                 :            :                         }
    1173                 :            :                 }
    1174                 :            : 
    1175                 :      44386 :                 break;
    1176                 :      40908 :         case SPDK_MEM_MAP_NOTIFY_UNREGISTER:
    1177                 :            : #if VFIO_ENABLED
    1178         [ +  + ]:      40908 :                 if (paddr == SPDK_VTOPHYS_ERROR) {
    1179                 :            :                         /*
    1180                 :            :                          * This is not an address that DPDK is managing.
    1181                 :            :                          */
    1182                 :            : 
    1183                 :            :                         /* Check if this is a PCI BAR. They need special handling */
    1184                 :        542 :                         paddr = vtophys_get_paddr_pci((uint64_t)vaddr, len);
    1185         [ +  + ]:        542 :                         if (paddr != SPDK_VTOPHYS_ERROR) {
    1186                 :            :                                 /* Get paddr for each 2MB chunk in this address range */
    1187         [ +  + ]:        832 :                                 while (len > 0) {
    1188                 :        786 :                                         paddr = vtophys_get_paddr_pci((uint64_t)vaddr, VALUE_2MB);
    1189         [ -  + ]:        786 :                                         if (paddr == SPDK_VTOPHYS_ERROR) {
    1190                 :          0 :                                                 DEBUG_PRINT("could not get phys addr for %p\n", vaddr);
    1191                 :          0 :                                                 return -EFAULT;
    1192                 :            :                                         }
    1193                 :            : 
    1194                 :        786 :                                         rc = spdk_mem_map_clear_translation(map, (uint64_t)vaddr, VALUE_2MB);
    1195         [ -  + ]:        786 :                                         if (rc != 0) {
    1196                 :          0 :                                                 return rc;
    1197                 :            :                                         }
    1198                 :            : 
    1199                 :        786 :                                         vaddr += VALUE_2MB;
    1200                 :        786 :                                         len -= VALUE_2MB;
    1201                 :            :                                 }
    1202                 :            : 
    1203                 :         46 :                                 return 0;
    1204                 :            :                         }
    1205                 :            : 
    1206                 :            :                         /* If vfio is enabled,
    1207                 :            :                          * we need to unmap the range from the IOMMU
    1208                 :            :                          */
    1209         [ +  + ]:        496 :                         if (spdk_iommu_is_enabled()) {
    1210                 :        203 :                                 uint64_t buffer_len = len;
    1211                 :        203 :                                 uint8_t *va = vaddr;
    1212                 :            :                                 enum rte_iova_mode iova_mode;
    1213                 :            : 
    1214                 :        203 :                                 iova_mode = rte_eal_iova_mode();
    1215                 :            :                                 /*
    1216                 :            :                                  * In virtual address mode, the region is contiguous and can be done in
    1217                 :            :                                  * one unmap.
    1218                 :            :                                  */
    1219         [ +  - ]:        203 :                                 if (iova_mode == RTE_IOVA_VA) {
    1220                 :        203 :                                         paddr = spdk_mem_map_translate(map, (uint64_t)va, &buffer_len);
    1221   [ +  -  -  + ]:        203 :                                         if (buffer_len != len || paddr != (uintptr_t)va) {
    1222                 :          0 :                                                 DEBUG_PRINT("Unmapping %p with length %lu failed because "
    1223                 :            :                                                             "translation had address 0x%" PRIx64 " and length %lu\n",
    1224                 :            :                                                             va, len, paddr, buffer_len);
    1225                 :          0 :                                                 return -EINVAL;
    1226                 :            :                                         }
    1227                 :        203 :                                         rc = vtophys_iommu_unmap_dma(paddr, len);
    1228         [ -  + ]:        203 :                                         if (rc) {
    1229                 :          0 :                                                 DEBUG_PRINT("Failed to iommu unmap paddr 0x%" PRIx64 "\n", paddr);
    1230                 :          0 :                                                 return -EFAULT;
    1231                 :            :                                         }
    1232         [ #  # ]:          0 :                                 } else if (iova_mode == RTE_IOVA_PA) {
    1233                 :            :                                         /* Get paddr for each 2MB chunk in this address range */
    1234         [ #  # ]:          0 :                                         while (buffer_len > 0) {
    1235                 :          0 :                                                 paddr = spdk_mem_map_translate(map, (uint64_t)va, NULL);
    1236                 :            : 
    1237   [ #  #  #  # ]:          0 :                                                 if (paddr == SPDK_VTOPHYS_ERROR || buffer_len < VALUE_2MB) {
    1238                 :          0 :                                                         DEBUG_PRINT("could not get phys addr for %p\n", va);
    1239                 :          0 :                                                         return -EFAULT;
    1240                 :            :                                                 }
    1241                 :            : 
    1242                 :          0 :                                                 rc = vtophys_iommu_unmap_dma(paddr, VALUE_2MB);
    1243         [ #  # ]:          0 :                                                 if (rc) {
    1244                 :          0 :                                                         DEBUG_PRINT("Failed to iommu unmap paddr 0x%" PRIx64 "\n", paddr);
    1245                 :          0 :                                                         return -EFAULT;
    1246                 :            :                                                 }
    1247                 :            : 
    1248                 :          0 :                                                 va += VALUE_2MB;
    1249                 :          0 :                                                 buffer_len -= VALUE_2MB;
    1250                 :            :                                         }
    1251                 :            :                                 }
    1252                 :            :                         }
    1253                 :            :                 }
    1254                 :            : #endif
    1255         [ +  + ]:     865600 :                 while (len > 0) {
    1256                 :     824738 :                         rc = spdk_mem_map_clear_translation(map, (uint64_t)vaddr, VALUE_2MB);
    1257         [ -  + ]:     824738 :                         if (rc != 0) {
    1258                 :          0 :                                 return rc;
    1259                 :            :                         }
    1260                 :            : 
    1261                 :     824738 :                         vaddr += VALUE_2MB;
    1262                 :     824738 :                         len -= VALUE_2MB;
    1263                 :            :                 }
    1264                 :            : 
    1265                 :      40862 :                 break;
    1266                 :          0 :         default:
    1267                 :          0 :                 SPDK_UNREACHABLE();
    1268                 :            :         }
    1269                 :            : 
    1270                 :      85248 :         return rc;
    1271                 :            : }
    1272                 :            : 
    1273                 :            : static int
    1274                 :     148659 : vtophys_check_contiguous_entries(uint64_t paddr1, uint64_t paddr2)
    1275                 :            : {
    1276                 :            :         /* This function is always called with paddrs for two subsequent
    1277                 :            :          * 2MB chunks in virtual address space, so those chunks will be only
    1278                 :            :          * physically contiguous if the physical addresses are 2MB apart
    1279                 :            :          * from each other as well.
    1280                 :            :          */
    1281                 :     148659 :         return (paddr2 - paddr1 == VALUE_2MB);
    1282                 :            : }
    1283                 :            : 
    1284                 :            : #if VFIO_ENABLED
    1285                 :            : 
    1286                 :            : static bool
    1287                 :       3244 : vfio_enabled(void)
    1288                 :            : {
    1289                 :       3244 :         return rte_vfio_is_enabled("vfio_pci");
    1290                 :            : }
    1291                 :            : 
    1292                 :            : /* Check if IOMMU is enabled on the system */
    1293                 :            : static bool
    1294                 :       1515 : has_iommu_groups(void)
    1295                 :            : {
    1296                 :       1515 :         int count = 0;
    1297         [ -  + ]:       1515 :         DIR *dir = opendir("/sys/kernel/iommu_groups");
    1298                 :            : 
    1299         [ -  + ]:       1515 :         if (dir == NULL) {
    1300                 :          0 :                 return false;
    1301                 :            :         }
    1302                 :            : 
    1303   [ +  +  -  +  :       5476 :         while (count < 3 && readdir(dir) != NULL) {
                   +  + ]
    1304                 :       3961 :                 count++;
    1305                 :            :         }
    1306                 :            : 
    1307         [ -  + ]:       1515 :         closedir(dir);
    1308                 :            :         /* there will always be ./ and ../ entries */
    1309                 :       1515 :         return count > 2;
    1310                 :            : }
    1311                 :            : 
    1312                 :            : static bool
    1313                 :       1515 : vfio_noiommu_enabled(void)
    1314                 :            : {
    1315                 :       1515 :         return rte_vfio_noiommu_is_enabled();
    1316                 :            : }
    1317                 :            : 
    1318                 :            : static void
    1319                 :       3244 : vtophys_iommu_init(void)
    1320                 :            : {
    1321                 :       1471 :         char proc_fd_path[PATH_MAX + 1];
    1322                 :       1471 :         char link_path[PATH_MAX + 1];
    1323                 :       3244 :         const char vfio_path[] = "/dev/vfio/vfio";
    1324                 :            :         DIR *dir;
    1325                 :            :         struct dirent *d;
    1326                 :            : 
    1327         [ +  + ]:       3244 :         if (!vfio_enabled()) {
    1328                 :       1729 :                 return;
    1329                 :            :         }
    1330                 :            : 
    1331         [ -  + ]:       1515 :         if (vfio_noiommu_enabled()) {
    1332                 :          0 :                 g_vfio.noiommu_enabled = true;
    1333         [ +  + ]:       1515 :         } else if (!has_iommu_groups()) {
    1334                 :        584 :                 return;
    1335                 :            :         }
    1336                 :            : 
    1337                 :        931 :         dir = opendir("/proc/self/fd");
    1338         [ -  + ]:        931 :         if (!dir) {
    1339                 :          0 :                 DEBUG_PRINT("Failed to open /proc/self/fd (%d)\n", errno);
    1340                 :          0 :                 return;
    1341                 :            :         }
    1342                 :            : 
    1343   [ -  +  +  - ]:      10660 :         while ((d = readdir(dir)) != NULL) {
    1344         [ +  + ]:      10660 :                 if (d->d_type != DT_LNK) {
    1345                 :       1862 :                         continue;
    1346                 :            :                 }
    1347                 :            : 
    1348                 :       8798 :                 snprintf(proc_fd_path, sizeof(proc_fd_path), "/proc/self/fd/%s", d->d_name);
    1349         [ +  + ]:       8798 :                 if (readlink(proc_fd_path, link_path, sizeof(link_path)) != (sizeof(vfio_path) - 1)) {
    1350                 :       7867 :                         continue;
    1351                 :            :                 }
    1352                 :            : 
    1353         [ +  - ]:        931 :                 if (memcmp(link_path, vfio_path, sizeof(vfio_path) - 1) == 0) {
    1354                 :        931 :                         sscanf(d->d_name, "%d", &g_vfio.fd);
    1355                 :        931 :                         break;
    1356                 :            :                 }
    1357                 :            :         }
    1358                 :            : 
    1359         [ -  + ]:        931 :         closedir(dir);
    1360                 :            : 
    1361         [ -  + ]:        931 :         if (g_vfio.fd < 0) {
    1362                 :          0 :                 DEBUG_PRINT("Failed to discover DPDK VFIO container fd.\n");
    1363                 :          0 :                 return;
    1364                 :            :         }
    1365                 :            : 
    1366                 :        931 :         g_vfio.enabled = true;
    1367                 :            : 
    1368                 :        931 :         return;
    1369                 :            : }
    1370                 :            : 
    1371                 :            : #endif
    1372                 :            : 
    1373                 :            : void
    1374                 :       1814 : vtophys_pci_device_added(struct rte_pci_device *pci_device)
    1375                 :            : {
    1376                 :            :         struct spdk_vtophys_pci_device *vtophys_dev;
    1377                 :            : 
    1378                 :       1814 :         pthread_mutex_lock(&g_vtophys_pci_devices_mutex);
    1379                 :            : 
    1380                 :       1814 :         vtophys_dev = calloc(1, sizeof(*vtophys_dev));
    1381         [ +  - ]:       1814 :         if (vtophys_dev) {
    1382                 :       1814 :                 vtophys_dev->pci_device = pci_device;
    1383                 :       1814 :                 TAILQ_INSERT_TAIL(&g_vtophys_pci_devices, vtophys_dev, tailq);
    1384                 :            :         } else {
    1385                 :          0 :                 DEBUG_PRINT("Memory allocation error\n");
    1386                 :            :         }
    1387                 :       1814 :         pthread_mutex_unlock(&g_vtophys_pci_devices_mutex);
    1388                 :            : 
    1389                 :            : #if VFIO_ENABLED
    1390                 :            :         struct spdk_vfio_dma_map *dma_map;
    1391                 :            :         int ret;
    1392                 :            : 
    1393   [ +  +  +  + ]:       1814 :         if (!g_vfio.enabled) {
    1394                 :        814 :                 return;
    1395                 :            :         }
    1396                 :            : 
    1397                 :       1000 :         pthread_mutex_lock(&g_vfio.mutex);
    1398                 :       1000 :         g_vfio.device_ref++;
    1399         [ +  + ]:       1000 :         if (g_vfio.device_ref > 1) {
    1400                 :        856 :                 pthread_mutex_unlock(&g_vfio.mutex);
    1401                 :        856 :                 return;
    1402                 :            :         }
    1403                 :            : 
    1404                 :            :         /* This is the first SPDK device using DPDK vfio. This means that the first
    1405                 :            :          * IOMMU group might have been just been added to the DPDK vfio container.
    1406                 :            :          * From this point it is certain that the memory can be mapped now.
    1407                 :            :          */
    1408         [ +  + ]:        644 :         TAILQ_FOREACH(dma_map, &g_vfio.maps, tailq) {
    1409                 :        509 :                 ret = ioctl(g_vfio.fd, VFIO_IOMMU_MAP_DMA, &dma_map->map);
    1410         [ +  + ]:        509 :                 if (ret) {
    1411                 :          9 :                         DEBUG_PRINT("Cannot update DMA mapping, error %d\n", errno);
    1412                 :          9 :                         break;
    1413                 :            :                 }
    1414                 :            :         }
    1415                 :        144 :         pthread_mutex_unlock(&g_vfio.mutex);
    1416                 :            : #endif
    1417                 :            : }
    1418                 :            : 
    1419                 :            : void
    1420                 :       1078 : vtophys_pci_device_removed(struct rte_pci_device *pci_device)
    1421                 :            : {
    1422                 :            :         struct spdk_vtophys_pci_device *vtophys_dev;
    1423                 :            : 
    1424                 :       1078 :         pthread_mutex_lock(&g_vtophys_pci_devices_mutex);
    1425         [ +  - ]:       1192 :         TAILQ_FOREACH(vtophys_dev, &g_vtophys_pci_devices, tailq) {
    1426         [ +  + ]:       1192 :                 if (vtophys_dev->pci_device == pci_device) {
    1427         [ +  + ]:       1078 :                         TAILQ_REMOVE(&g_vtophys_pci_devices, vtophys_dev, tailq);
    1428                 :       1078 :                         free(vtophys_dev);
    1429                 :       1078 :                         break;
    1430                 :            :                 }
    1431                 :            :         }
    1432                 :       1078 :         pthread_mutex_unlock(&g_vtophys_pci_devices_mutex);
    1433                 :            : 
    1434                 :            : #if VFIO_ENABLED
    1435                 :            :         struct spdk_vfio_dma_map *dma_map;
    1436                 :            :         int ret;
    1437                 :            : 
    1438   [ +  +  +  + ]:       1078 :         if (!g_vfio.enabled) {
    1439                 :        601 :                 return;
    1440                 :            :         }
    1441                 :            : 
    1442                 :        477 :         pthread_mutex_lock(&g_vfio.mutex);
    1443         [ -  + ]:        477 :         assert(g_vfio.device_ref > 0);
    1444                 :        477 :         g_vfio.device_ref--;
    1445         [ +  + ]:        477 :         if (g_vfio.device_ref > 0) {
    1446                 :        370 :                 pthread_mutex_unlock(&g_vfio.mutex);
    1447                 :        370 :                 return;
    1448                 :            :         }
    1449                 :            : 
    1450                 :            :         /* This is the last SPDK device using DPDK vfio. If DPDK doesn't have
    1451                 :            :          * any additional devices using it's vfio container, all the mappings
    1452                 :            :          * will be automatically removed by the Linux vfio driver. We unmap
    1453                 :            :          * the memory manually to be able to easily re-map it later regardless
    1454                 :            :          * of other, external factors.
    1455                 :            :          */
    1456         [ +  + ]:        107 :         TAILQ_FOREACH(dma_map, &g_vfio.maps, tailq) {
    1457                 :         11 :                 struct vfio_iommu_type1_dma_unmap unmap = {};
    1458                 :         11 :                 unmap.argsz = sizeof(unmap);
    1459                 :         11 :                 unmap.flags = 0;
    1460                 :         11 :                 unmap.iova = dma_map->map.iova;
    1461                 :         11 :                 unmap.size = dma_map->map.size;
    1462                 :         11 :                 ret = ioctl(g_vfio.fd, VFIO_IOMMU_UNMAP_DMA, &unmap);
    1463         [ +  - ]:         11 :                 if (ret) {
    1464                 :         11 :                         DEBUG_PRINT("Cannot unmap DMA memory, error %d\n", errno);
    1465                 :         11 :                         break;
    1466                 :            :                 }
    1467                 :            :         }
    1468                 :        107 :         pthread_mutex_unlock(&g_vfio.mutex);
    1469                 :            : #endif
    1470                 :            : }
    1471                 :            : 
    1472                 :            : int
    1473                 :       3244 : vtophys_init(void)
    1474                 :            : {
    1475                 :       3244 :         const struct spdk_mem_map_ops vtophys_map_ops = {
    1476                 :            :                 .notify_cb = vtophys_notify,
    1477                 :            :                 .are_contiguous = vtophys_check_contiguous_entries,
    1478                 :            :         };
    1479                 :            : 
    1480                 :       3244 :         const struct spdk_mem_map_ops phys_ref_map_ops = {
    1481                 :            :                 .notify_cb = NULL,
    1482                 :            :                 .are_contiguous = NULL,
    1483                 :            :         };
    1484                 :            : 
    1485                 :            : #if VFIO_ENABLED
    1486                 :       3244 :         vtophys_iommu_init();
    1487                 :            : #endif
    1488                 :            : 
    1489                 :       3244 :         g_phys_ref_map = spdk_mem_map_alloc(0, &phys_ref_map_ops, NULL);
    1490         [ -  + ]:       3244 :         if (g_phys_ref_map == NULL) {
    1491                 :          0 :                 DEBUG_PRINT("phys_ref map allocation failed.\n");
    1492                 :          0 :                 return -ENOMEM;
    1493                 :            :         }
    1494                 :            : 
    1495   [ +  +  +  + ]:       3244 :         if (g_huge_pages) {
    1496                 :       3238 :                 g_vtophys_map = spdk_mem_map_alloc(SPDK_VTOPHYS_ERROR, &vtophys_map_ops, NULL);
    1497         [ -  + ]:       3238 :                 if (g_vtophys_map == NULL) {
    1498                 :          0 :                         DEBUG_PRINT("vtophys map allocation failed\n");
    1499                 :          0 :                         spdk_mem_map_free(&g_phys_ref_map);
    1500                 :          0 :                         return -ENOMEM;
    1501                 :            :                 }
    1502                 :            :         }
    1503                 :       3244 :         return 0;
    1504                 :            : }
    1505                 :            : 
    1506                 :            : uint64_t
    1507                 :  117665105 : spdk_vtophys(const void *buf, uint64_t *size)
    1508                 :            : {
    1509                 :            :         uint64_t vaddr, paddr_2mb;
    1510                 :            : 
    1511   [ -  +  -  + ]:  117665105 :         if (!g_huge_pages) {
    1512                 :          0 :                 return SPDK_VTOPHYS_ERROR;
    1513                 :            :         }
    1514                 :            : 
    1515                 :  117665105 :         vaddr = (uint64_t)buf;
    1516                 :  117665105 :         paddr_2mb = spdk_mem_map_translate(g_vtophys_map, vaddr, size);
    1517                 :            : 
    1518                 :            :         /*
    1519                 :            :          * SPDK_VTOPHYS_ERROR has all bits set, so if the lookup returned SPDK_VTOPHYS_ERROR,
    1520                 :            :          * we will still bitwise-or it with the buf offset below, but the result will still be
    1521                 :            :          * SPDK_VTOPHYS_ERROR. However now that we do + rather than | (due to PCI vtophys being
    1522                 :            :          * unaligned) we must now check the return value before addition.
    1523                 :            :          */
    1524                 :            :         SPDK_STATIC_ASSERT(SPDK_VTOPHYS_ERROR == UINT64_C(-1), "SPDK_VTOPHYS_ERROR should be all 1s");
    1525         [ +  + ]:  117665105 :         if (paddr_2mb == SPDK_VTOPHYS_ERROR) {
    1526                 :        640 :                 return SPDK_VTOPHYS_ERROR;
    1527                 :            :         } else {
    1528                 :  117664457 :                 return paddr_2mb + (vaddr & MASK_2MB);
    1529                 :            :         }
    1530                 :            : }
    1531                 :            : 
    1532                 :            : int
    1533                 :         56 : spdk_mem_get_fd_and_offset(void *vaddr, uint64_t *offset)
    1534                 :            : {
    1535                 :            :         struct rte_memseg *seg;
    1536                 :            :         int ret, fd;
    1537                 :            : 
    1538                 :         56 :         seg = rte_mem_virt2memseg(vaddr, NULL);
    1539         [ -  + ]:         56 :         if (!seg) {
    1540                 :          0 :                 SPDK_ERRLOG("memory %p doesn't exist\n", vaddr);
    1541                 :          0 :                 return -ENOENT;
    1542                 :            :         }
    1543                 :            : 
    1544                 :         56 :         fd = rte_memseg_get_fd_thread_unsafe(seg);
    1545         [ -  + ]:         56 :         if (fd < 0) {
    1546                 :          0 :                 return fd;
    1547                 :            :         }
    1548                 :            : 
    1549                 :         56 :         ret = rte_memseg_get_fd_offset_thread_unsafe(seg, offset);
    1550         [ -  + ]:         56 :         if (ret < 0) {
    1551                 :          0 :                 return ret;
    1552                 :            :         }
    1553                 :            : 
    1554                 :         56 :         return fd;
    1555                 :            : }
    1556                 :            : 
    1557                 :            : void
    1558                 :          6 : mem_disable_huge_pages(void)
    1559                 :            : {
    1560                 :          6 :         g_huge_pages = false;
    1561                 :          6 : }

Generated by: LCOV version 1.14