LCOV - code coverage report
Current view: top level - spdk/lib/env_dpdk - memory.c (source / functions) Hit Total Coverage
Test: Combined Lines: 655 837 78.3 %
Date: 2024-12-09 12:21:52 Functions: 41 44 93.2 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 923 1740 53.0 %

           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                 :            : struct map_page_cfg {
      65                 :            :         uint64_t shift;
      66                 :            :         uint64_t size;
      67                 :            :         uint64_t mask;
      68                 :            :         uint64_t num_pages_per_gb;
      69                 :            : };
      70                 :            : 
      71                 :            : /**
      72                 :            :  * g_map_page_cfg can not be static because it is used in the inline function
      73                 :            :  * spdk_mem_map_translate.
      74                 :            :  */
      75                 :            : struct map_page_cfg g_map_page_cfg = {
      76                 :            :         .shift = SHIFT_2MB,
      77                 :            :         .size = VALUE_2MB,
      78                 :            :         .mask = MASK_2MB,
      79                 :            :         .num_pages_per_gb = 1UL << (SHIFT_1GB - SHIFT_2MB),
      80                 :            : };
      81                 :            : 
      82                 :            : #define MAP_PAGE_SHIFT                  (g_map_page_cfg.shift)
      83                 :            : #define MAP_PAGE_SIZE                   (g_map_page_cfg.size)
      84                 :            : #define MAP_PAGE_MASK                   (g_map_page_cfg.mask)
      85                 :            : #define MAP_NUM_PAGES_PER_GB    (g_map_page_cfg.num_pages_per_gb)
      86                 :            : 
      87                 :            : #define MAP_256TB_IDX(vfn_page) ((vfn_page) >> (SHIFT_1GB - MAP_PAGE_SHIFT))
      88                 :            : #define MAP_1GB_IDX(vfn_page)   ((vfn_page) & ((1ULL << (SHIFT_1GB - MAP_PAGE_SHIFT)) - 1))
      89                 :            : #define MAP_PAGE_OFFSET(ptr)    (((uintptr_t)(ptr)) & MAP_PAGE_MASK)
      90                 :            : 
      91                 :            : /* Page is registered */
      92                 :            : #define REG_MAP_REGISTERED      (1ULL << 62)
      93                 :            : 
      94                 :            : /* A notification region barrier. The page translation entry that's marked
      95                 :            :  * with this flag must be unregistered separately. This allows contiguous
      96                 :            :  * regions to be unregistered in the same chunks they were registered.
      97                 :            :  */
      98                 :            : #define REG_MAP_NOTIFY_START    (1ULL << 63)
      99                 :            : 
     100                 :            : /* Translation of a single page. */
     101                 :            : struct map_page {
     102                 :            :         uint64_t translation;
     103                 :            : };
     104                 :            : 
     105                 :            : /* Second-level map table indexed by bits [page_shift..29] of the virtual address.
     106                 :            :  * Each entry contains the address translation or error for entries that haven't
     107                 :            :  * been retrieved yet.
     108                 :            :  */
     109                 :            : struct map_1gb {
     110                 :            :         struct map_page map[0];
     111                 :            :         /**
     112                 :            :          * Page table space.
     113                 :            :          * Do not put any fields after this!
     114                 :            :          */
     115                 :            : };
     116                 :            : 
     117                 :            : #define MAP_SIZE_OF_MAP_1GB (sizeof(struct map_1gb) + MAP_NUM_PAGES_PER_GB * sizeof(struct map_page))
     118                 :            : 
     119                 :            : /* Top-level map table indexed by bits [30..47] of the virtual address.
     120                 :            :  * Each entry points to a second-level map table or NULL.
     121                 :            :  */
     122                 :            : struct map_256tb {
     123                 :            :         struct map_1gb *map[1ULL << (SHIFT_256TB - SHIFT_1GB)];
     124                 :            : };
     125                 :            : 
     126                 :            : /* Page-granularity memory address translation */
     127                 :            : struct spdk_mem_map {
     128                 :            :         struct map_256tb map_256tb;
     129                 :            :         pthread_mutex_t mutex;
     130                 :            :         uint64_t default_translation;
     131                 :            :         struct spdk_mem_map_ops ops;
     132                 :            :         void *cb_ctx;
     133                 :            :         TAILQ_ENTRY(spdk_mem_map) tailq;
     134                 :            : };
     135                 :            : 
     136                 :            : /* Registrations map. The 64 bit translations are bit fields with the
     137                 :            :  * following layout (starting with the low bits):
     138                 :            :  *    0 - 61 : reserved
     139                 :            :  *   62 - 63 : flags
     140                 :            :  */
     141                 :            : static struct spdk_mem_map *g_mem_reg_map;
     142                 :            : static TAILQ_HEAD(spdk_mem_map_head, spdk_mem_map) g_spdk_mem_maps =
     143                 :            :         TAILQ_HEAD_INITIALIZER(g_spdk_mem_maps);
     144                 :            : static pthread_mutex_t g_spdk_mem_map_mutex = PTHREAD_MUTEX_INITIALIZER;
     145                 :            : 
     146                 :            : static bool g_legacy_mem;
     147                 :            : static bool g_huge_pages = true;
     148                 :            : 
     149                 :            : /*
     150                 :            :  * Walk the currently registered memory via the main memory registration map
     151                 :            :  * and call the new map's notify callback for each virtually contiguous region.
     152                 :            :  */
     153                 :            : static int
     154                 :      11826 : mem_map_notify_walk(struct spdk_mem_map *map, enum spdk_mem_map_notify_action action)
     155                 :            : {
     156                 :            :         size_t idx_256tb;
     157                 :            :         uint64_t idx_1gb;
     158                 :      11826 :         uint64_t contig_start = UINT64_MAX;
     159                 :      11826 :         uint64_t contig_end = UINT64_MAX;
     160                 :            :         struct map_1gb *map_1gb;
     161                 :            :         int rc;
     162                 :            : 
     163         [ +  + ]:      11826 :         if (!g_mem_reg_map) {
     164                 :          0 :                 return -EINVAL;
     165                 :            :         }
     166                 :            : 
     167                 :            :         /* Hold the memory registration map mutex so no new registrations can be added while we are looping. */
     168   [ +  +  +  - ]:      11826 :         pthread_mutex_lock(&g_mem_reg_map->mutex);
     169                 :            : 
     170         [ +  + ]:  177745458 :         for (idx_256tb = 0;
     171         [ +  + ]: 3089116722 :              idx_256tb < sizeof(g_mem_reg_map->map_256tb.map) / sizeof(g_mem_reg_map->map_256tb.map[0]);
     172                 : 3089104896 :              idx_256tb++) {
     173   [ +  -  +  -  : 3089104938 :                 map_1gb = g_mem_reg_map->map_256tb.map[idx_256tb];
             +  -  +  - ]
     174                 :            : 
     175         [ +  + ]: 3089104938 :                 if (!map_1gb) {
     176         [ +  + ]: 3089089655 :                         if (contig_start != UINT64_MAX) {
     177                 :            :                                 /* End of of a virtually contiguous range */
     178   [ #  #  #  #  :          0 :                                 rc = map->ops.notify_cb(map->cb_ctx, map, action,
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     179                 :          0 :                                                         (void *)contig_start,
     180         [ #  # ]:          0 :                                                         contig_end - contig_start + MAP_PAGE_SIZE);
     181                 :            :                                 /* Don't bother handling unregister failures. It can't be any worse */
     182   [ #  #  #  # ]:          0 :                                 if (rc != 0 && action == SPDK_MEM_MAP_NOTIFY_REGISTER) {
     183                 :          0 :                                         goto err_unregister;
     184                 :            :                                 }
     185                 :          0 :                         }
     186                 : 3089089655 :                         contig_start = UINT64_MAX;
     187                 : 3089089655 :                         continue;
     188                 :            :                 }
     189                 :            : 
     190   [ +  +  +  + ]:   75341767 :                 for (idx_1gb = 0; idx_1gb < MAP_NUM_PAGES_PER_GB; idx_1gb++) {
     191   [ +  +  +  +  :   75634346 :                         if ((map_1gb->map[idx_1gb].translation & REG_MAP_REGISTERED) &&
          +  -  +  -  +  
             -  +  +  +  
                      + ]
     192         [ +  + ]:    7960531 :                             (contig_start == UINT64_MAX ||
     193   [ +  +  +  -  :    7958497 :                              (map_1gb->map[idx_1gb].translation & REG_MAP_NOTIFY_START) == 0)) {
          +  -  +  -  +  
                      - ]
     194                 :            :                                 /* Rebuild the virtual address from the indexes */
     195   [ +  +  +  - ]:    7971430 :                                 uint64_t vaddr = (idx_256tb << SHIFT_1GB) | (idx_1gb << MAP_PAGE_SHIFT);
     196                 :            : 
     197         [ +  + ]:    7971430 :                                 if (contig_start == UINT64_MAX) {
     198                 :      21700 :                                         contig_start = vaddr;
     199                 :       2034 :                                 }
     200                 :            : 
     201                 :    7971430 :                                 contig_end = vaddr;
     202                 :     308494 :                         } else {
     203         [ +  + ]:   67355096 :                                 if (contig_start != UINT64_MAX) {
     204                 :            :                                         /* End of of a virtually contiguous range */
     205   [ +  -  +  -  :      40154 :                                         rc = map->ops.notify_cb(map->cb_ctx, map, action,
          +  -  -  +  +  
             -  +  -  +  
                      - ]
     206                 :       2034 :                                                                 (void *)contig_start,
     207         [ +  - ]:      21700 :                                                                 contig_end - contig_start + MAP_PAGE_SIZE);
     208                 :            :                                         /* Don't bother handling unregister failures. It can't be any worse */
     209   [ +  +  +  + ]:      21700 :                                         if (rc != 0 && action == SPDK_MEM_MAP_NOTIFY_REGISTER) {
     210                 :         42 :                                                 goto err_unregister;
     211                 :            :                                         }
     212                 :            : 
     213                 :            :                                         /* This page might be a part of a neighbour region, so process
     214                 :            :                                          * it again. The idx_1gb will be incremented immediately.
     215                 :            :                                          */
     216                 :      21658 :                                         idx_1gb--;
     217                 :       2030 :                                 }
     218                 :   67355054 :                                 contig_start = UINT64_MAX;
     219                 :            :                         }
     220                 :    5830674 :                 }
     221                 :       1164 :         }
     222                 :            : 
     223   [ -  +  -  + ]:      11784 :         pthread_mutex_unlock(&g_mem_reg_map->mutex);
     224                 :      11784 :         return 0;
     225                 :            : 
     226                 :         38 : err_unregister:
     227                 :            :         /* Unwind to the first empty translation so we don't unregister
     228                 :            :          * a region that just failed to register.
     229                 :            :          */
     230   [ +  +  +  + ]:         42 :         idx_256tb = MAP_256TB_IDX((contig_start >> MAP_PAGE_SHIFT) - 1);
     231   [ +  +  +  + ]:         42 :         idx_1gb = MAP_1GB_IDX((contig_start >> MAP_PAGE_SHIFT) - 1);
     232                 :         42 :         contig_start = UINT64_MAX;
     233                 :         42 :         contig_end = UINT64_MAX;
     234                 :            : 
     235                 :            :         /* Unregister any memory we managed to register before the failure */
     236         [ +  + ]:         84 :         for (; idx_256tb < SIZE_MAX; idx_256tb--) {
     237   [ +  -  +  -  :         42 :                 map_1gb = g_mem_reg_map->map_256tb.map[idx_256tb];
             +  -  +  - ]
     238                 :            : 
     239         [ +  + ]:         42 :                 if (!map_1gb) {
     240         [ #  # ]:          0 :                         if (contig_end != UINT64_MAX) {
     241                 :            :                                 /* End of of a virtually contiguous range */
     242   [ #  #  #  #  :          0 :                                 map->ops.notify_cb(map->cb_ctx, map,
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     243                 :            :                                                    SPDK_MEM_MAP_NOTIFY_UNREGISTER,
     244                 :          0 :                                                    (void *)contig_start,
     245         [ #  # ]:          0 :                                                    contig_end - contig_start + MAP_PAGE_SIZE);
     246                 :          0 :                         }
     247                 :          0 :                         contig_end = UINT64_MAX;
     248                 :          0 :                         continue;
     249                 :            :                 }
     250                 :            : 
     251         [ +  + ]:        378 :                 for (; idx_1gb < UINT64_MAX; idx_1gb--) {
     252                 :            :                         /* Rebuild the virtual address from the indexes */
     253   [ +  +  +  - ]:        336 :                         uint64_t vaddr = (idx_256tb << SHIFT_1GB) | (idx_1gb << MAP_PAGE_SHIFT);
     254   [ +  +  +  +  :        336 :                         if ((map_1gb->map[idx_1gb].translation & REG_MAP_REGISTERED) &&
          +  -  +  -  +  
             -  +  +  #  
                      # ]
     255   [ -  +  #  #  :         16 :                             (contig_end == UINT64_MAX || (map_1gb->map[idx_1gb].translation & REG_MAP_NOTIFY_START) == 0)) {
          #  #  #  #  #  
                #  #  # ]
     256                 :            : 
     257         [ +  + ]:        168 :                                 if (contig_end == UINT64_MAX) {
     258                 :        168 :                                         contig_end = vaddr;
     259                 :         16 :                                 }
     260                 :        168 :                                 contig_start = vaddr;
     261                 :         16 :                         } else {
     262         [ +  + ]:        168 :                                 if (contig_end != UINT64_MAX) {
     263   [ +  +  +  -  :        126 :                                         if (map_1gb->map[idx_1gb].translation & REG_MAP_NOTIFY_START) {
          +  -  +  -  +  
                -  +  - ]
     264                 :          0 :                                                 contig_start = vaddr;
     265                 :          0 :                                         }
     266                 :            :                                         /* End of of a virtually contiguous range */
     267   [ +  -  +  -  :        228 :                                         map->ops.notify_cb(map->cb_ctx, map,
          +  -  -  +  +  
             -  +  -  +  
                      - ]
     268                 :            :                                                            SPDK_MEM_MAP_NOTIFY_UNREGISTER,
     269                 :         12 :                                                            (void *)contig_start,
     270         [ +  - ]:        126 :                                                            contig_end - contig_start + MAP_PAGE_SIZE);
     271                 :         12 :                                 }
     272                 :        168 :                                 contig_end = UINT64_MAX;
     273                 :            :                         }
     274                 :         32 :                 }
     275         [ +  - ]:         42 :                 idx_1gb = MAP_NUM_PAGES_PER_GB - 1;
     276                 :          4 :         }
     277                 :            : 
     278   [ +  +  +  - ]:         42 :         pthread_mutex_unlock(&g_mem_reg_map->mutex);
     279                 :         42 :         return rc;
     280                 :        682 : }
     281                 :            : 
     282                 :            : struct spdk_mem_map *
     283                 :      11432 : spdk_mem_map_alloc(uint64_t default_translation, const struct spdk_mem_map_ops *ops, void *cb_ctx)
     284                 :            : {
     285                 :            :         struct spdk_mem_map *map;
     286                 :            :         int rc;
     287                 :            :         size_t i;
     288                 :            : 
     289                 :      11432 :         map = calloc(1, sizeof(*map));
     290         [ +  + ]:      11432 :         if (map == NULL) {
     291                 :          0 :                 return NULL;
     292                 :            :         }
     293                 :            : 
     294   [ +  +  +  +  :      11432 :         if (pthread_mutex_init(&map->mutex, NULL)) {
                   -  + ]
     295                 :          0 :                 free(map);
     296                 :          0 :                 return NULL;
     297                 :            :         }
     298                 :            : 
     299   [ +  -  +  - ]:      11432 :         map->default_translation = default_translation;
     300   [ +  -  +  - ]:      11432 :         map->cb_ctx = cb_ctx;
     301         [ +  + ]:      11432 :         if (ops) {
     302         [ +  - ]:       8716 :                 map->ops = *ops;
     303                 :        487 :         }
     304                 :            : 
     305   [ +  +  +  +  :      11432 :         if (ops && ops->notify_cb) {
             +  -  +  + ]
     306         [ +  + ]:       6084 :                 pthread_mutex_lock(&g_spdk_mem_map_mutex);
     307                 :       6084 :                 rc = mem_map_notify_walk(map, SPDK_MEM_MAP_NOTIFY_REGISTER);
     308         [ +  + ]:       6084 :                 if (rc != 0) {
     309         [ +  + ]:         42 :                         pthread_mutex_unlock(&g_spdk_mem_map_mutex);
     310                 :         42 :                         DEBUG_PRINT("Initial mem_map notify failed\n");
     311   [ +  +  +  - ]:         42 :                         pthread_mutex_destroy(&map->mutex);
     312         [ +  + ]:   11010090 :                         for (i = 0; i < sizeof(map->map_256tb.map) / sizeof(map->map_256tb.map[0]); i++) {
     313   [ +  -  +  -  :   11010048 :                                 free(map->map_256tb.map[i]);
             +  -  +  - ]
     314                 :    1048576 :                         }
     315                 :         42 :                         free(map);
     316                 :         42 :                         return NULL;
     317                 :            :                 }
     318   [ +  -  +  -  :       6042 :                 TAILQ_INSERT_TAIL(&g_spdk_mem_maps, map, tailq);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     319         [ +  + ]:       6042 :                 pthread_mutex_unlock(&g_spdk_mem_map_mutex);
     320                 :        350 :         }
     321                 :            : 
     322                 :      11390 :         return map;
     323                 :        628 : }
     324                 :            : 
     325                 :            : void
     326                 :      11122 : spdk_mem_map_free(struct spdk_mem_map **pmap)
     327                 :            : {
     328                 :            :         struct spdk_mem_map *map;
     329                 :            :         size_t i;
     330                 :            : 
     331         [ +  + ]:      11122 :         if (!pmap) {
     332                 :          0 :                 return;
     333                 :            :         }
     334                 :            : 
     335         [ +  - ]:      11122 :         map = *pmap;
     336                 :            : 
     337         [ +  + ]:      11122 :         if (!map) {
     338                 :        332 :                 return;
     339                 :            :         }
     340                 :            : 
     341   [ +  +  +  -  :      10790 :         if (map->ops.notify_cb) {
             +  -  +  + ]
     342         [ +  + ]:       5742 :                 pthread_mutex_lock(&g_spdk_mem_map_mutex);
     343                 :       5742 :                 mem_map_notify_walk(map, SPDK_MEM_MAP_NOTIFY_UNREGISTER);
     344   [ +  +  +  -  :       5742 :                 TAILQ_REMOVE(&g_spdk_mem_maps, map, tailq);
          +  -  -  +  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  -  +  -  
          +  -  +  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
     345         [ +  + ]:       5742 :                 pthread_mutex_unlock(&g_spdk_mem_map_mutex);
     346                 :        328 :         }
     347                 :            : 
     348         [ +  + ]: 2828544550 :         for (i = 0; i < sizeof(map->map_256tb.map) / sizeof(map->map_256tb.map[0]); i++) {
     349   [ +  -  +  -  : 2828533760 :                 free(map->map_256tb.map[i]);
             +  -  +  - ]
     350                 :  152043520 :         }
     351                 :            : 
     352   [ +  +  +  - ]:      10790 :         pthread_mutex_destroy(&map->mutex);
     353                 :            : 
     354                 :      10790 :         free(map);
     355         [ +  - ]:      10790 :         *pmap = NULL;
     356                 :        600 : }
     357                 :            : 
     358                 :            : uint64_t
     359                 :          0 : spdk_mem_map_get_page_size(void)
     360                 :            : {
     361         [ #  # ]:          0 :         return g_map_page_cfg.size;
     362                 :            : }
     363                 :            : 
     364                 :            : int
     365                 :      33833 : spdk_mem_register(void *_vaddr, size_t len)
     366                 :            : {
     367                 :            :         struct spdk_mem_map *map;
     368                 :            :         int rc;
     369                 :      33833 :         uint64_t vaddr = (uintptr_t)_vaddr;
     370                 :            :         uint64_t seg_vaddr;
     371                 :            :         size_t seg_len;
     372                 :            :         uint64_t reg;
     373                 :            : 
     374   [ +  +  -  + ]:      33833 :         if ((uintptr_t)vaddr & ~MASK_256TB) {
     375                 :          0 :                 DEBUG_PRINT("invalid usermode virtual address %jx\n", vaddr);
     376                 :          0 :                 return -EINVAL;
     377                 :            :         }
     378                 :            : 
     379   [ +  +  +  +  :      33833 :         if (((uintptr_t)vaddr & MAP_PAGE_MASK) || (len & MAP_PAGE_MASK)) {
             +  -  +  + ]
     380                 :         84 :                 DEBUG_PRINT("invalid %s parameters, vaddr=%jx len=%ju\n",
     381                 :            :                             __func__, vaddr, len);
     382                 :         84 :                 return -EINVAL;
     383                 :            :         }
     384                 :            : 
     385         [ +  + ]:      33749 :         if (len == 0) {
     386                 :          0 :                 return 0;
     387                 :            :         }
     388                 :            : 
     389         [ +  + ]:      33749 :         pthread_mutex_lock(&g_spdk_mem_map_mutex);
     390                 :            : 
     391                 :      33749 :         seg_vaddr = vaddr;
     392                 :      33749 :         seg_len = len;
     393         [ +  + ]:    3174450 :         while (seg_len > 0) {
     394                 :    3140743 :                 reg = spdk_mem_map_translate(g_mem_reg_map, (uint64_t)seg_vaddr, NULL);
     395   [ +  +  +  + ]:    3140743 :                 if (reg & REG_MAP_REGISTERED) {
     396         [ +  + ]:         42 :                         pthread_mutex_unlock(&g_spdk_mem_map_mutex);
     397                 :         42 :                         return -EBUSY;
     398                 :            :                 }
     399         [ +  - ]:    3140701 :                 seg_vaddr += MAP_PAGE_SIZE;
     400         [ +  - ]:    3140701 :                 seg_len -= MAP_PAGE_SIZE;
     401                 :            :         }
     402                 :            : 
     403                 :      33707 :         seg_vaddr = vaddr;
     404                 :      33707 :         seg_len = 0;
     405         [ +  + ]:    3174366 :         while (len > 0) {
     406         [ +  + ]:    3226422 :                 spdk_mem_map_set_translation(g_mem_reg_map, (uint64_t)vaddr, MAP_PAGE_SIZE,
     407   [ +  -  +  -  :      85763 :                                              seg_len == 0 ? REG_MAP_REGISTERED | REG_MAP_NOTIFY_START : REG_MAP_REGISTERED);
                   +  - ]
     408         [ +  - ]:    3140659 :                 seg_len += MAP_PAGE_SIZE;
     409         [ +  - ]:    3140659 :                 vaddr += MAP_PAGE_SIZE;
     410         [ +  - ]:    3140659 :                 len -= MAP_PAGE_SIZE;
     411                 :            :         }
     412                 :            : 
     413   [ +  +  +  -  :      96272 :         TAILQ_FOREACH(map, &g_spdk_mem_maps, tailq) {
             +  -  +  - ]
     414   [ +  -  +  -  :      62595 :                 rc = map->ops.notify_cb(map->cb_ctx, map, SPDK_MEM_MAP_NOTIFY_REGISTER,
          +  -  -  +  +  
             -  +  -  +  
                      - ]
     415                 :        966 :                                         (void *)seg_vaddr, seg_len);
     416         [ +  + ]:      62567 :                 if (rc != 0) {
     417         [ #  # ]:          2 :                         pthread_mutex_unlock(&g_spdk_mem_map_mutex);
     418                 :          2 :                         return rc;
     419                 :            :                 }
     420                 :        966 :         }
     421                 :            : 
     422         [ +  + ]:      33705 :         pthread_mutex_unlock(&g_spdk_mem_map_mutex);
     423                 :      33705 :         return 0;
     424                 :        676 : }
     425                 :            : 
     426                 :            : int
     427                 :      30340 : spdk_mem_unregister(void *_vaddr, size_t len)
     428                 :            : {
     429                 :            :         struct spdk_mem_map *map;
     430                 :            :         int rc;
     431                 :      30340 :         uint64_t vaddr = (uintptr_t)_vaddr;
     432                 :            :         uint64_t seg_vaddr;
     433                 :            :         size_t seg_len;
     434                 :            :         uint64_t reg, newreg;
     435                 :            : 
     436   [ +  +  -  + ]:      30340 :         if ((uintptr_t)vaddr & ~MASK_256TB) {
     437                 :          0 :                 DEBUG_PRINT("invalid usermode virtual address %jx\n", vaddr);
     438                 :          0 :                 return -EINVAL;
     439                 :            :         }
     440                 :            : 
     441   [ +  -  +  +  :      30340 :         if (((uintptr_t)vaddr & MAP_PAGE_MASK) || (len & MAP_PAGE_MASK)) {
             +  -  -  + ]
     442                 :          0 :                 DEBUG_PRINT("invalid %s parameters, vaddr=%jx len=%ju\n",
     443                 :            :                             __func__, vaddr, len);
     444                 :          0 :                 return -EINVAL;
     445                 :            :         }
     446                 :            : 
     447         [ +  + ]:      30340 :         pthread_mutex_lock(&g_spdk_mem_map_mutex);
     448                 :            : 
     449                 :            :         /* The first page must be a start of a region. Also check if it's
     450                 :            :          * registered to make sure we don't return -ERANGE for non-registered
     451                 :            :          * regions.
     452                 :            :          */
     453                 :      30340 :         reg = spdk_mem_map_translate(g_mem_reg_map, (uint64_t)vaddr, NULL);
     454   [ +  +  +  +  :      30340 :         if ((reg & REG_MAP_REGISTERED) && (reg & REG_MAP_NOTIFY_START) == 0) {
             +  -  +  + ]
     455         [ +  + ]:         84 :                 pthread_mutex_unlock(&g_spdk_mem_map_mutex);
     456                 :         84 :                 return -ERANGE;
     457                 :            :         }
     458                 :            : 
     459                 :      30256 :         seg_vaddr = vaddr;
     460                 :      30256 :         seg_len = len;
     461         [ +  + ]:     640246 :         while (seg_len > 0) {
     462                 :     610032 :                 reg = spdk_mem_map_translate(g_mem_reg_map, (uint64_t)seg_vaddr, NULL);
     463   [ +  +  +  + ]:     610032 :                 if ((reg & REG_MAP_REGISTERED) == 0) {
     464         [ +  + ]:         42 :                         pthread_mutex_unlock(&g_spdk_mem_map_mutex);
     465                 :         42 :                         return -EINVAL;
     466                 :            :                 }
     467         [ +  - ]:     609990 :                 seg_vaddr += MAP_PAGE_SIZE;
     468         [ +  - ]:     609990 :                 seg_len -= MAP_PAGE_SIZE;
     469                 :            :         }
     470                 :            : 
     471                 :      30214 :         newreg = spdk_mem_map_translate(g_mem_reg_map, (uint64_t)seg_vaddr, NULL);
     472                 :            :         /* If the next page is registered, it must be a start of a region as well,
     473                 :            :          * otherwise we'd be unregistering only a part of a region.
     474                 :            :          */
     475   [ +  +  +  +  :      30214 :         if ((newreg & REG_MAP_NOTIFY_START) == 0 && (newreg & REG_MAP_REGISTERED)) {
             +  -  +  + ]
     476         [ +  + ]:         42 :                 pthread_mutex_unlock(&g_spdk_mem_map_mutex);
     477                 :         42 :                 return -ERANGE;
     478                 :            :         }
     479                 :      30172 :         seg_vaddr = vaddr;
     480                 :      30172 :         seg_len = 0;
     481                 :            : 
     482         [ +  + ]:     640120 :         while (len > 0) {
     483                 :     609948 :                 reg = spdk_mem_map_translate(g_mem_reg_map, (uint64_t)vaddr, NULL);
     484         [ +  - ]:     609948 :                 spdk_mem_map_set_translation(g_mem_reg_map, (uint64_t)vaddr, MAP_PAGE_SIZE, 0);
     485                 :            : 
     486   [ +  +  +  +  :     609948 :                 if (seg_len > 0 && (reg & REG_MAP_NOTIFY_START)) {
                   +  + ]
     487   [ +  +  +  -  :        420 :                         TAILQ_FOREACH_REVERSE(map, &g_spdk_mem_maps, spdk_mem_map_head, tailq) {
          +  -  +  -  +  
          +  +  -  +  -  
          +  -  +  -  +  
                -  +  - ]
     488   [ +  -  +  -  :        220 :                                 rc = map->ops.notify_cb(map->cb_ctx, map, SPDK_MEM_MAP_NOTIFY_UNREGISTER,
          +  -  -  +  +  
             -  +  -  +  
                      - ]
     489                 :         20 :                                                         (void *)seg_vaddr, seg_len);
     490         [ +  + ]:        210 :                                 if (rc != 0) {
     491         [ #  # ]:          0 :                                         pthread_mutex_unlock(&g_spdk_mem_map_mutex);
     492                 :          0 :                                         return rc;
     493                 :            :                                 }
     494                 :         20 :                         }
     495                 :            : 
     496                 :        210 :                         seg_vaddr = vaddr;
     497         [ +  - ]:        210 :                         seg_len = MAP_PAGE_SIZE;
     498                 :         20 :                 } else {
     499         [ +  - ]:     609738 :                         seg_len += MAP_PAGE_SIZE;
     500                 :            :                 }
     501                 :            : 
     502         [ +  - ]:     609948 :                 vaddr += MAP_PAGE_SIZE;
     503         [ +  - ]:     609948 :                 len -= MAP_PAGE_SIZE;
     504                 :            :         }
     505                 :            : 
     506         [ +  + ]:      30172 :         if (seg_len > 0) {
     507   [ +  +  +  -  :      91031 :                 TAILQ_FOREACH_REVERSE(map, &g_spdk_mem_maps, spdk_mem_map_head, tailq) {
          +  -  +  -  +  
          +  +  -  +  -  
          +  -  +  -  +  
                -  +  - ]
     508   [ +  -  +  -  :      60877 :                         rc = map->ops.notify_cb(map->cb_ctx, map, SPDK_MEM_MAP_NOTIFY_UNREGISTER,
          +  -  -  +  +  
             -  +  -  +  
                      - ]
     509                 :        928 :                                                 (void *)seg_vaddr, seg_len);
     510         [ +  + ]:      60859 :                         if (rc != 0) {
     511         [ #  # ]:          0 :                                 pthread_mutex_unlock(&g_spdk_mem_map_mutex);
     512                 :          0 :                                 return rc;
     513                 :            :                         }
     514                 :        928 :                 }
     515                 :        502 :         }
     516                 :            : 
     517         [ +  + ]:      30172 :         pthread_mutex_unlock(&g_spdk_mem_map_mutex);
     518                 :      30172 :         return 0;
     519                 :        518 : }
     520                 :            : 
     521                 :            : int
     522                 :          0 : spdk_mem_reserve(void *vaddr, size_t len)
     523                 :            : {
     524                 :            :         struct spdk_mem_map *map;
     525                 :            :         void *seg_vaddr;
     526                 :            :         size_t seg_len;
     527                 :            :         uint64_t reg;
     528                 :            : 
     529   [ #  #  #  # ]:          0 :         if ((uintptr_t)vaddr & ~MASK_256TB) {
     530                 :          0 :                 DEBUG_PRINT("invalid usermode virtual address %p\n", vaddr);
     531                 :          0 :                 return -EINVAL;
     532                 :            :         }
     533                 :            : 
     534   [ #  #  #  #  :          0 :         if (((uintptr_t)vaddr & MAP_PAGE_MASK) || (len & MAP_PAGE_MASK)) {
             #  #  #  # ]
     535                 :          0 :                 DEBUG_PRINT("invalid %s parameters, vaddr=%p len=%ju\n",
     536                 :            :                             __func__, vaddr, len);
     537                 :          0 :                 return -EINVAL;
     538                 :            :         }
     539                 :            : 
     540         [ #  # ]:          0 :         if (len == 0) {
     541                 :          0 :                 return 0;
     542                 :            :         }
     543                 :            : 
     544         [ #  # ]:          0 :         pthread_mutex_lock(&g_spdk_mem_map_mutex);
     545                 :            : 
     546                 :            :         /* Check if any part of this range is already registered */
     547                 :          0 :         seg_vaddr = vaddr;
     548                 :          0 :         seg_len = len;
     549         [ #  # ]:          0 :         while (seg_len > 0) {
     550                 :          0 :                 reg = spdk_mem_map_translate(g_mem_reg_map, (uint64_t)seg_vaddr, NULL);
     551   [ #  #  #  # ]:          0 :                 if (reg & REG_MAP_REGISTERED) {
     552         [ #  # ]:          0 :                         pthread_mutex_unlock(&g_spdk_mem_map_mutex);
     553                 :          0 :                         return -EBUSY;
     554                 :            :                 }
     555         [ #  # ]:          0 :                 seg_vaddr += MAP_PAGE_SIZE;
     556         [ #  # ]:          0 :                 seg_len -= MAP_PAGE_SIZE;
     557                 :            :         }
     558                 :            : 
     559                 :            :         /* Simply set the translation to the memory map's default. This allocates the space in the
     560                 :            :          * map but does not provide a valid translation. */
     561                 :          0 :         spdk_mem_map_set_translation(g_mem_reg_map, (uint64_t)vaddr, len,
     562   [ #  #  #  # ]:          0 :                                      g_mem_reg_map->default_translation);
     563                 :            : 
     564   [ #  #  #  #  :          0 :         TAILQ_FOREACH(map, &g_spdk_mem_maps, tailq) {
             #  #  #  # ]
     565   [ #  #  #  # ]:          0 :                 spdk_mem_map_set_translation(map, (uint64_t)vaddr, len, map->default_translation);
     566                 :          0 :         }
     567                 :            : 
     568         [ #  # ]:          0 :         pthread_mutex_unlock(&g_spdk_mem_map_mutex);
     569                 :          0 :         return 0;
     570                 :          0 : }
     571                 :            : 
     572                 :            : static struct map_1gb *
     573                 :   14091713 : mem_map_get_map_1gb(struct spdk_mem_map *map, uint64_t vfn_page)
     574                 :            : {
     575                 :            :         struct map_1gb *map_1gb;
     576         [ +  + ]:   14091713 :         uint64_t idx_256tb = MAP_256TB_IDX(vfn_page);
     577                 :            :         size_t i;
     578                 :            : 
     579         [ +  + ]:   14091713 :         if (spdk_unlikely(idx_256tb >= SPDK_COUNTOF(map->map_256tb.map))) {
     580                 :         42 :                 return NULL;
     581                 :            :         }
     582                 :            : 
     583   [ +  -  +  -  :   14091671 :         map_1gb = map->map_256tb.map[idx_256tb];
             +  -  +  - ]
     584                 :            : 
     585         [ +  + ]:   14091671 :         if (!map_1gb) {
     586   [ +  +  +  - ]:      12119 :                 pthread_mutex_lock(&map->mutex);
     587                 :            : 
     588                 :            :                 /* Recheck to make sure nobody else got the mutex first. */
     589   [ +  -  +  -  :      12119 :                 map_1gb = map->map_256tb.map[idx_256tb];
             +  -  +  - ]
     590         [ +  + ]:      12119 :                 if (!map_1gb) {
     591         [ +  - ]:      12119 :                         map_1gb = malloc(MAP_SIZE_OF_MAP_1GB);
     592         [ +  - ]:      12119 :                         if (map_1gb) {
     593                 :            :                                 /* initialize all entries to default translation */
     594   [ +  +  +  + ]:   49647959 :                                 for (i = 0; i < MAP_NUM_PAGES_PER_GB; i++) {
     595   [ +  -  +  -  :   49635840 :                                         map_1gb->map[i].translation = map->default_translation;
          +  -  +  -  +  
                -  +  - ]
     596                 :    3563520 :                                 }
     597   [ +  -  +  -  :      12119 :                                 map->map_256tb.map[idx_256tb] = map_1gb;
             +  -  +  - ]
     598                 :        828 :                         }
     599                 :        828 :                 }
     600                 :            : 
     601   [ +  +  +  - ]:      12119 :                 pthread_mutex_unlock(&map->mutex);
     602                 :            : 
     603         [ +  + ]:      12119 :                 if (!map_1gb) {
     604                 :          0 :                         DEBUG_PRINT("allocation failed\n");
     605                 :          0 :                         return NULL;
     606                 :            :                 }
     607                 :        828 :         }
     608                 :            : 
     609                 :   14091671 :         return map_1gb;
     610                 :     421385 : }
     611                 :            : 
     612                 :            : int
     613                 :    5779054 : spdk_mem_map_set_translation(struct spdk_mem_map *map, uint64_t vaddr, uint64_t size,
     614                 :            :                              uint64_t translation)
     615                 :            : {
     616                 :            :         uint64_t vfn_page;
     617                 :            :         struct map_1gb *map_1gb;
     618                 :            :         uint64_t idx_1gb;
     619                 :            :         struct map_page *map_page;
     620                 :            : 
     621   [ +  +  +  + ]:    5779054 :         if ((uintptr_t)vaddr & ~MASK_256TB) {
     622                 :         42 :                 DEBUG_PRINT("invalid usermode virtual address %" PRIu64 "\n", vaddr);
     623                 :         42 :                 return -EINVAL;
     624                 :            :         }
     625                 :            : 
     626                 :            :         /* Only page-aligned registrations are supported */
     627   [ +  +  +  +  :    5779012 :         if (((uintptr_t)vaddr & MAP_PAGE_MASK) || (size & MAP_PAGE_MASK)) {
             +  -  +  + ]
     628                 :         84 :                 DEBUG_PRINT("invalid %s parameters, vaddr=%" PRIu64 " len=%" PRIu64 "\n",
     629                 :            :                             __func__, vaddr, size);
     630                 :         84 :                 return -EINVAL;
     631                 :            :         }
     632                 :            : 
     633         [ +  + ]:    5778928 :         vfn_page = vaddr >> MAP_PAGE_SHIFT;
     634                 :            : 
     635         [ +  + ]:   19870599 :         while (size) {
     636                 :   14091713 :                 map_1gb = mem_map_get_map_1gb(map, vfn_page);
     637         [ +  + ]:   14091713 :                 if (!map_1gb) {
     638                 :         42 :                         DEBUG_PRINT("could not get %p map\n", (void *)vaddr);
     639                 :         42 :                         return -ENOMEM;
     640                 :            :                 }
     641                 :            : 
     642         [ +  + ]:   14091671 :                 idx_1gb = MAP_1GB_IDX(vfn_page);
     643   [ +  -  +  - ]:   14091671 :                 map_page = &map_1gb->map[idx_1gb];
     644   [ +  -  +  - ]:   14091671 :                 map_page->translation = translation;
     645                 :            : 
     646         [ +  - ]:   14091671 :                 size -= MAP_PAGE_SIZE;
     647                 :   14091671 :                 vfn_page++;
     648                 :            :         }
     649                 :            : 
     650                 :    5778886 :         return 0;
     651                 :     260459 : }
     652                 :            : 
     653                 :            : int
     654                 :     947723 : spdk_mem_map_clear_translation(struct spdk_mem_map *map, uint64_t vaddr, uint64_t size)
     655                 :            : {
     656   [ +  -  +  - ]:     947723 :         return spdk_mem_map_set_translation(map, vaddr, size, map->default_translation);
     657                 :            : }
     658                 :            : 
     659                 :            : inline uint64_t
     660                 :  128689761 : spdk_mem_map_translate(const struct spdk_mem_map *map, uint64_t vaddr, uint64_t *size)
     661                 :            : {
     662                 :            :         const struct map_1gb *map_1gb;
     663                 :            :         const struct map_page *map_page;
     664                 :            :         uint64_t idx_256tb;
     665                 :            :         uint64_t idx_1gb;
     666                 :            :         uint64_t vfn_page;
     667                 :            :         uint64_t cur_size;
     668                 :            :         uint64_t prev_translation;
     669                 :            :         uint64_t orig_translation;
     670                 :            : 
     671   [ +  +  -  + ]:  128689761 :         if (spdk_unlikely(vaddr & ~MASK_256TB)) {
     672                 :          0 :                 DEBUG_PRINT("invalid usermode virtual address %p\n", (void *)vaddr);
     673   [ #  #  #  # ]:          0 :                 return map->default_translation;
     674                 :            :         }
     675                 :            : 
     676         [ +  + ]:  128689761 :         vfn_page = vaddr >> MAP_PAGE_SHIFT;
     677         [ +  + ]:  128689761 :         idx_256tb = MAP_256TB_IDX(vfn_page);
     678         [ +  + ]:  128689761 :         idx_1gb = MAP_1GB_IDX(vfn_page);
     679                 :            : 
     680   [ +  -  +  -  :  128689761 :         map_1gb = map->map_256tb.map[idx_256tb];
             +  -  +  - ]
     681         [ +  + ]:  128689761 :         if (spdk_unlikely(!map_1gb)) {
     682   [ +  -  +  - ]:    2548852 :                 return map->default_translation;
     683                 :            :         }
     684                 :            : 
     685   [ +  -  +  - ]:  126140909 :         cur_size = MAP_PAGE_SIZE - MAP_PAGE_OFFSET(vaddr);
     686   [ +  -  +  - ]:  126140909 :         map_page = &map_1gb->map[idx_1gb];
     687   [ +  +  +  +  :  126141109 :         if (size == NULL || map->ops.are_contiguous == NULL ||
          +  +  +  -  +  
                +  +  + ]
     688   [ +  +  +  -  :   40795485 :             map_page->translation == map->default_translation) {
             +  -  +  - ]
     689         [ +  + ]:   85345508 :                 if (size != NULL) {
     690   [ +  +  +  +  :        420 :                         *size = spdk_min(*size, cur_size);
             +  -  +  - ]
     691                 :         40 :                 }
     692   [ +  -  +  - ]:   85345508 :                 return map_page->translation;
     693                 :            :         }
     694                 :            : 
     695   [ +  -  +  - ]:   40795425 :         orig_translation = map_page->translation;
     696                 :   40795425 :         prev_translation = orig_translation;
     697   [ +  +  +  + ]:   40895331 :         while (cur_size < *size) {
     698                 :     162551 :                 vfn_page++;
     699         [ +  + ]:     162551 :                 idx_256tb = MAP_256TB_IDX(vfn_page);
     700         [ +  + ]:     162551 :                 idx_1gb = MAP_1GB_IDX(vfn_page);
     701                 :            : 
     702   [ +  -  +  -  :     162551 :                 map_1gb = map->map_256tb.map[idx_256tb];
             +  -  +  - ]
     703         [ +  + ]:     162551 :                 if (spdk_unlikely(!map_1gb)) {
     704                 :          0 :                         break;
     705                 :            :                 }
     706                 :            : 
     707   [ +  -  +  - ]:     162551 :                 map_page = &map_1gb->map[idx_1gb];
     708   [ +  +  +  -  :     162551 :                 if (!map->ops.are_contiguous(prev_translation, map_page->translation)) {
          +  -  -  +  +  
          -  +  -  +  -  
                   +  + ]
     709                 :      62645 :                         break;
     710                 :            :                 }
     711                 :            : 
     712         [ +  - ]:      99906 :                 cur_size += MAP_PAGE_SIZE;
     713   [ +  -  +  - ]:      99906 :                 prev_translation = map_page->translation;
     714                 :            :         }
     715                 :            : 
     716   [ +  +  +  +  :   40795425 :         *size = spdk_min(*size, cur_size);
             -  +  +  - ]
     717                 :   40795425 :         return orig_translation;
     718                 :     960417 : }
     719                 :            : 
     720                 :            : static void
     721                 :      58726 : memory_hotplug_cb(enum rte_mem_event event_type,
     722                 :            :                   const void *addr, size_t len, void *arg)
     723                 :            : {
     724         [ +  + ]:      58726 :         if (event_type == RTE_MEM_EVENT_ALLOC) {
     725                 :      29696 :                 spdk_mem_register((void *)addr, len);
     726                 :            : 
     727         [ +  + ]:      29696 :                 if (!spdk_env_dpdk_external_init()) {
     728                 :      29670 :                         return;
     729                 :            :                 }
     730                 :            : 
     731                 :            :                 /* When the user initialized DPDK separately, we can't
     732                 :            :                  * be sure that --match-allocations RTE flag was specified.
     733                 :            :                  * Without this flag, DPDK can free memory in different units
     734                 :            :                  * than it was allocated. It doesn't work with things like RDMA MRs.
     735                 :            :                  *
     736                 :            :                  * For such cases, we mark segments so they aren't freed.
     737                 :            :                  */
     738         [ +  + ]:         62 :                 while (len > 0) {
     739                 :            :                         struct rte_memseg *seg;
     740                 :            : 
     741                 :         36 :                         seg = rte_mem_virt2memseg(addr, NULL);
     742   [ +  +  #  # ]:         36 :                         assert(seg != NULL);
     743   [ +  -  +  -  :         36 :                         seg->flags |= RTE_MEMSEG_FLAG_DO_NOT_FREE;
                   +  - ]
     744   [ +  -  +  - ]:         36 :                         addr = (void *)((uintptr_t)addr + seg->hugepage_sz);
     745   [ +  -  +  - ]:         36 :                         len -= seg->hugepage_sz;
     746                 :            :                 }
     747         [ +  + ]:      29032 :         } else if (event_type == RTE_MEM_EVENT_FREE) {
     748                 :      29030 :                 spdk_mem_unregister((void *)addr, len);
     749                 :        446 :         }
     750                 :        901 : }
     751                 :            : 
     752                 :            : static int
     753                 :       2657 : memory_iter_cb(const struct rte_memseg_list *msl,
     754                 :            :                const struct rte_memseg *ms, size_t len, void *arg)
     755                 :            : {
     756   [ +  -  +  -  :       2657 :         return spdk_mem_register(ms->addr, len);
                   +  - ]
     757                 :            : }
     758                 :            : 
     759                 :            : static bool g_mem_event_cb_registered = false;
     760                 :            : 
     761                 :            : static int
     762                 :       2599 : mem_map_mem_event_callback_register(void)
     763                 :            : {
     764                 :            :         int rc;
     765                 :            : 
     766                 :       2599 :         rc = rte_mem_event_callback_register("spdk", memory_hotplug_cb, NULL);
     767         [ -  + ]:       2599 :         if (rc != 0) {
     768                 :          0 :                 return rc;
     769                 :            :         }
     770                 :            : 
     771                 :       2599 :         g_mem_event_cb_registered = true;
     772                 :       2599 :         return 0;
     773                 :         70 : }
     774                 :            : 
     775                 :            : static void
     776                 :       2605 : mem_map_mem_event_callback_unregister(void)
     777                 :            : {
     778   [ +  +  +  + ]:       2605 :         if (g_mem_event_cb_registered) {
     779                 :       2458 :                 g_mem_event_cb_registered = false;
     780                 :       2458 :                 rte_mem_event_callback_unregister("spdk", NULL);
     781                 :         68 :         }
     782                 :       2605 : }
     783                 :            : 
     784                 :            : int
     785                 :       2674 : mem_map_init(bool legacy_mem)
     786                 :            : {
     787                 :            :         int rc;
     788                 :            : 
     789         [ +  - ]:       2674 :         g_legacy_mem = legacy_mem;
     790                 :            : 
     791                 :       2674 :         g_mem_reg_map = spdk_mem_map_alloc(0, NULL, NULL);
     792         [ +  + ]:       2674 :         if (g_mem_reg_map == NULL) {
     793                 :          0 :                 DEBUG_PRINT("memory registration map allocation failed\n");
     794                 :          0 :                 return -ENOMEM;
     795                 :            :         }
     796                 :            : 
     797   [ +  +  +  + ]:       2674 :         if (!g_legacy_mem) {
     798                 :            :                 /**
     799                 :            :                  * To prevent DPDK complaining, only register the callback when
     800                 :            :                  * we are not in legacy mem mode.
     801                 :            :                  */
     802                 :       2599 :                 rc = mem_map_mem_event_callback_register();
     803         [ +  + ]:       2599 :                 if (rc != 0) {
     804                 :          0 :                         DEBUG_PRINT("memory event callback registration failed, rc = %d\n", rc);
     805                 :          0 :                         goto err_free_reg_map;
     806                 :            :                 }
     807                 :         70 :         }
     808                 :            : 
     809                 :            :         /*
     810                 :            :          * Walk all DPDK memory segments and register them
     811                 :            :          * with the main memory map
     812                 :            :          */
     813                 :       2674 :         rc = rte_memseg_contig_walk(memory_iter_cb, NULL);
     814         [ +  + ]:       2674 :         if (rc != 0) {
     815                 :          0 :                 DEBUG_PRINT("memory segments walking failed, rc = %d\n", rc);
     816                 :          0 :                 goto err_unregister_mem_cb;
     817                 :            :         }
     818                 :            : 
     819                 :       2674 :         return 0;
     820                 :            : 
     821                 :          0 : err_unregister_mem_cb:
     822                 :          0 :         mem_map_mem_event_callback_unregister();
     823                 :          0 : err_free_reg_map:
     824                 :          0 :         spdk_mem_map_free(&g_mem_reg_map);
     825                 :          0 :         return rc;
     826                 :        137 : }
     827                 :            : 
     828                 :            : void
     829                 :       2605 : mem_map_fini(void)
     830                 :            : {
     831                 :       2605 :         mem_map_mem_event_callback_unregister();
     832                 :       2605 :         spdk_mem_map_free(&g_mem_reg_map);
     833                 :       2605 : }
     834                 :            : 
     835                 :            : bool
     836                 :      21521 : spdk_iommu_is_enabled(void)
     837                 :            : {
     838                 :            : #if VFIO_ENABLED
     839   [ +  +  +  +  :      21521 :         return g_vfio.enabled && !g_vfio.noiommu_enabled;
             +  +  +  - ]
     840                 :            : #else
     841                 :          0 :         return false;
     842                 :            : #endif
     843                 :            : }
     844                 :            : 
     845                 :            : struct spdk_vtophys_pci_device {
     846                 :            :         struct rte_pci_device *pci_device;
     847                 :            :         TAILQ_ENTRY(spdk_vtophys_pci_device) tailq;
     848                 :            : };
     849                 :            : 
     850                 :            : static pthread_mutex_t g_vtophys_pci_devices_mutex = PTHREAD_MUTEX_INITIALIZER;
     851                 :            : static TAILQ_HEAD(, spdk_vtophys_pci_device) g_vtophys_pci_devices =
     852                 :            :         TAILQ_HEAD_INITIALIZER(g_vtophys_pci_devices);
     853                 :            : 
     854                 :            : static struct spdk_mem_map *g_vtophys_map;
     855                 :            : static struct spdk_mem_map *g_phys_ref_map;
     856                 :            : static struct spdk_mem_map *g_numa_map;
     857                 :            : 
     858                 :            : #if VFIO_ENABLED
     859                 :            : static int
     860                 :        764 : _vfio_iommu_map_dma(uint64_t vaddr, uint64_t iova, uint64_t size)
     861                 :            : {
     862                 :            :         struct spdk_vfio_dma_map *dma_map;
     863                 :            :         int ret;
     864                 :            : 
     865                 :        764 :         dma_map = calloc(1, sizeof(*dma_map));
     866         [ +  + ]:        764 :         if (dma_map == NULL) {
     867                 :          0 :                 return -ENOMEM;
     868                 :            :         }
     869                 :            : 
     870   [ +  -  +  -  :        764 :         dma_map->map.argsz = sizeof(dma_map->map);
                   +  - ]
     871   [ -  +  +  -  :        764 :         dma_map->map.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
          -  +  +  -  +  
             -  +  -  +  
                      - ]
     872   [ +  -  +  -  :        764 :         dma_map->map.vaddr = vaddr;
                   +  - ]
     873   [ +  -  +  -  :        764 :         dma_map->map.iova = iova;
                   +  - ]
     874   [ +  -  +  -  :        764 :         dma_map->map.size = size;
                   +  - ]
     875                 :            : 
     876   [ +  +  -  + ]:        764 :         if (g_vfio.device_ref == 0) {
     877                 :            :                 /* VFIO requires at least one device (IOMMU group) to be added to
     878                 :            :                  * a VFIO container before it is possible to perform any IOMMU
     879                 :            :                  * operations on that container. This memory will be mapped once
     880                 :            :                  * the first device (IOMMU group) is hotplugged.
     881                 :            :                  *
     882                 :            :                  * Since the vfio container is managed internally by DPDK, it is
     883                 :            :                  * also possible that some device is already in that container, but
     884                 :            :                  * it's not managed by SPDK -  e.g. an NIC attached internally
     885                 :            :                  * inside DPDK. We could map the memory straight away in such
     886                 :            :                  * scenario, but there's no need to do it. DPDK devices clearly
     887                 :            :                  * don't need our mappings and hence we defer the mapping
     888                 :            :                  * unconditionally until the first SPDK-managed device is
     889                 :            :                  * hotplugged.
     890                 :            :                  */
     891                 :        640 :                 goto out_insert;
     892                 :            :         }
     893                 :            : 
     894   [ #  #  #  #  :        124 :         ret = ioctl(g_vfio.fd, VFIO_IOMMU_MAP_DMA, &dma_map->map);
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     895         [ +  - ]:        124 :         if (ret) {
     896                 :            :                 /* There are cases the vfio container doesn't have IOMMU group, it's safe for this case */
     897         [ #  # ]:          0 :                 SPDK_NOTICELOG("Cannot set up DMA mapping, error %d, ignored\n", errno);
     898                 :          0 :         }
     899                 :            : 
     900                 :        124 : out_insert:
     901   [ +  -  +  -  :        764 :         TAILQ_INSERT_TAIL(&g_vfio.maps, dma_map, tailq);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     902                 :        764 :         return 0;
     903                 :          2 : }
     904                 :            : 
     905                 :            : 
     906                 :            : static int
     907                 :        215 : vtophys_iommu_map_dma(uint64_t vaddr, uint64_t iova, uint64_t size)
     908                 :            : {
     909                 :            :         uint64_t refcount;
     910                 :            :         int ret;
     911                 :            : 
     912                 :        215 :         refcount = spdk_mem_map_translate(g_phys_ref_map, iova, NULL);
     913   [ -  +  #  # ]:        215 :         assert(refcount < UINT64_MAX);
     914         [ -  + ]:        215 :         if (refcount > 0) {
     915                 :          0 :                 spdk_mem_map_set_translation(g_phys_ref_map, iova, size, refcount + 1);
     916                 :          0 :                 return 0;
     917                 :            :         }
     918                 :            : 
     919         [ -  + ]:        215 :         pthread_mutex_lock(&g_vfio.mutex);
     920                 :        215 :         ret = _vfio_iommu_map_dma(vaddr, iova, size);
     921         [ -  + ]:        215 :         pthread_mutex_unlock(&g_vfio.mutex);
     922         [ -  + ]:        215 :         if (ret) {
     923                 :          0 :                 return ret;
     924                 :            :         }
     925                 :            : 
     926                 :        215 :         spdk_mem_map_set_translation(g_phys_ref_map, iova, size, refcount + 1);
     927                 :        215 :         return 0;
     928                 :          0 : }
     929                 :            : 
     930                 :            : int
     931                 :        549 : vtophys_iommu_map_dma_bar(uint64_t vaddr, uint64_t iova, uint64_t size)
     932                 :            : {
     933                 :            :         int ret;
     934                 :            : 
     935         [ +  + ]:        549 :         pthread_mutex_lock(&g_vfio.mutex);
     936                 :        549 :         ret = _vfio_iommu_map_dma(vaddr, iova, size);
     937         [ +  + ]:        549 :         pthread_mutex_unlock(&g_vfio.mutex);
     938                 :            : 
     939                 :        549 :         return ret;
     940                 :            : }
     941                 :            : 
     942                 :            : static int
     943                 :        740 : _vfio_iommu_unmap_dma(struct spdk_vfio_dma_map *dma_map)
     944                 :            : {
     945                 :        740 :         struct vfio_iommu_type1_dma_unmap unmap = {};
     946                 :            :         int ret;
     947                 :            : 
     948   [ +  +  -  + ]:        740 :         if (g_vfio.device_ref == 0) {
     949                 :            :                 /* Memory is not mapped anymore, just remove it's references */
     950                 :        103 :                 goto out_remove;
     951                 :            :         }
     952                 :            : 
     953                 :        637 :         unmap.argsz = sizeof(unmap);
     954         [ +  - ]:        637 :         unmap.flags = 0;
     955   [ +  -  +  -  :        637 :         unmap.iova = dma_map->map.iova;
             +  -  +  - ]
     956   [ +  -  +  -  :        637 :         unmap.size = dma_map->map.size;
             +  -  +  - ]
     957   [ +  -  -  +  :        637 :         ret = ioctl(g_vfio.fd, VFIO_IOMMU_UNMAP_DMA, &unmap);
          +  -  -  +  +  
             -  -  +  +  
                      - ]
     958         [ +  - ]:        637 :         if (ret) {
     959         [ #  # ]:          0 :                 SPDK_NOTICELOG("Cannot clear DMA mapping, error %d, ignored\n", errno);
     960                 :          0 :         }
     961                 :            : 
     962                 :        684 : out_remove:
     963   [ +  +  +  -  :        740 :         TAILQ_REMOVE(&g_vfio.maps, dma_map, tailq);
          +  -  +  -  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     964                 :        740 :         free(dma_map);
     965                 :        740 :         return 0;
     966                 :            : }
     967                 :            : 
     968                 :            : static int
     969                 :        215 : vtophys_iommu_unmap_dma(uint64_t iova, uint64_t size)
     970                 :            : {
     971                 :            :         struct spdk_vfio_dma_map *dma_map;
     972                 :            :         uint64_t refcount;
     973                 :            :         int ret;
     974                 :            : 
     975         [ -  + ]:        215 :         pthread_mutex_lock(&g_vfio.mutex);
     976   [ +  -  #  #  :        506 :         TAILQ_FOREACH(dma_map, &g_vfio.maps, tailq) {
          #  #  #  #  #  
                #  #  # ]
     977   [ +  +  #  #  :        506 :                 if (dma_map->map.iova == iova) {
             #  #  #  # ]
     978                 :        215 :                         break;
     979                 :            :                 }
     980                 :          0 :         }
     981                 :            : 
     982         [ -  + ]:        215 :         if (dma_map == NULL) {
     983                 :          0 :                 DEBUG_PRINT("Cannot clear DMA mapping for IOVA %"PRIx64" - it's not mapped\n", iova);
     984         [ #  # ]:          0 :                 pthread_mutex_unlock(&g_vfio.mutex);
     985                 :          0 :                 return -ENXIO;
     986                 :            :         }
     987                 :            : 
     988                 :        215 :         refcount = spdk_mem_map_translate(g_phys_ref_map, iova, NULL);
     989   [ -  +  #  # ]:        215 :         assert(refcount < UINT64_MAX);
     990         [ +  - ]:        215 :         if (refcount > 0) {
     991                 :        215 :                 spdk_mem_map_set_translation(g_phys_ref_map, iova, size, refcount - 1);
     992                 :          0 :         }
     993                 :            : 
     994                 :            :         /* We still have outstanding references, don't clear it. */
     995         [ -  + ]:        215 :         if (refcount > 1) {
     996         [ #  # ]:          0 :                 pthread_mutex_unlock(&g_vfio.mutex);
     997                 :          0 :                 return 0;
     998                 :            :         }
     999                 :            : 
    1000                 :            :         /** don't support partial or multiple-page unmap for now */
    1001   [ -  +  #  #  :        215 :         assert(dma_map->map.size == size);
          #  #  #  #  #  
                      # ]
    1002                 :            : 
    1003                 :        215 :         ret = _vfio_iommu_unmap_dma(dma_map);
    1004         [ -  + ]:        215 :         pthread_mutex_unlock(&g_vfio.mutex);
    1005                 :            : 
    1006                 :        215 :         return ret;
    1007                 :          0 : }
    1008                 :            : 
    1009                 :            : int
    1010                 :        525 : vtophys_iommu_unmap_dma_bar(uint64_t vaddr)
    1011                 :            : {
    1012                 :            :         struct spdk_vfio_dma_map *dma_map;
    1013                 :            :         int ret;
    1014                 :            : 
    1015         [ +  + ]:        525 :         pthread_mutex_lock(&g_vfio.mutex);
    1016   [ +  -  +  -  :        547 :         TAILQ_FOREACH(dma_map, &g_vfio.maps, tailq) {
          -  +  #  #  #  
                #  #  # ]
    1017   [ +  +  +  -  :        547 :                 if (dma_map->map.vaddr == vaddr) {
             +  -  -  + ]
    1018                 :        525 :                         break;
    1019                 :            :                 }
    1020                 :          0 :         }
    1021                 :            : 
    1022         [ +  + ]:        525 :         if (dma_map == NULL) {
    1023                 :          0 :                 DEBUG_PRINT("Cannot clear DMA mapping for address %"PRIx64" - it's not mapped\n", vaddr);
    1024         [ #  # ]:          0 :                 pthread_mutex_unlock(&g_vfio.mutex);
    1025                 :          0 :                 return -ENXIO;
    1026                 :            :         }
    1027                 :            : 
    1028                 :        525 :         ret = _vfio_iommu_unmap_dma(dma_map);
    1029         [ +  + ]:        525 :         pthread_mutex_unlock(&g_vfio.mutex);
    1030                 :        525 :         return ret;
    1031                 :          2 : }
    1032                 :            : #endif
    1033                 :            : 
    1034                 :            : static uint64_t
    1035                 :    1049626 : vtophys_get_paddr_memseg(uint64_t vaddr)
    1036                 :            : {
    1037                 :            :         uintptr_t paddr;
    1038                 :            :         struct rte_memseg *seg;
    1039                 :            : 
    1040                 :    1049626 :         seg = rte_mem_virt2memseg((void *)(uintptr_t)vaddr, NULL);
    1041         [ +  + ]:    1049626 :         if (seg != NULL) {
    1042   [ +  -  +  - ]:    1048514 :                 paddr = seg->iova;
    1043         [ +  + ]:    1048514 :                 if (paddr == RTE_BAD_IOVA) {
    1044                 :          0 :                         return SPDK_VTOPHYS_ERROR;
    1045                 :            :                 }
    1046   [ +  -  +  -  :    1048514 :                 paddr += (vaddr - (uintptr_t)seg->addr);
                   +  - ]
    1047                 :    1048514 :                 return paddr;
    1048                 :            :         }
    1049                 :            : 
    1050                 :       1112 :         return SPDK_VTOPHYS_ERROR;
    1051                 :      86811 : }
    1052                 :            : 
    1053                 :            : /* Try to get the paddr from /proc/self/pagemap */
    1054                 :            : static uint64_t
    1055                 :      16571 : vtophys_get_paddr_pagemap(uint64_t vaddr)
    1056                 :            : {
    1057                 :            :         uintptr_t paddr;
    1058                 :            : 
    1059                 :            :         /* Silence static analyzers */
    1060   [ -  +  #  # ]:      16571 :         assert(vaddr != 0);
    1061                 :      16571 :         paddr = rte_mem_virt2iova((void *)vaddr);
    1062         [ +  + ]:      16571 :         if (paddr == RTE_BAD_IOVA) {
    1063                 :            :                 /*
    1064                 :            :                  * The vaddr may be valid but doesn't have a backing page
    1065                 :            :                  * assigned yet.  Touch the page to ensure a backing page
    1066                 :            :                  * gets assigned, then try to translate again.
    1067                 :            :                  */
    1068                 :       1174 :                 rte_atomic64_read((rte_atomic64_t *)vaddr);
    1069                 :       1174 :                 paddr = rte_mem_virt2iova((void *)vaddr);
    1070                 :          0 :         }
    1071         [ +  + ]:      16571 :         if (paddr == RTE_BAD_IOVA) {
    1072                 :            :                 /* Unable to get to the physical address. */
    1073                 :          2 :                 return SPDK_VTOPHYS_ERROR;
    1074                 :            :         }
    1075                 :            : 
    1076                 :      16569 :         return paddr;
    1077                 :          0 : }
    1078                 :            : 
    1079                 :            : static uint64_t
    1080                 :       3164 : pci_device_vtophys(struct rte_pci_device *dev, uint64_t vaddr, size_t len)
    1081                 :            : {
    1082                 :            :         struct rte_mem_resource *res;
    1083                 :            :         uint64_t paddr;
    1084                 :            :         unsigned r;
    1085                 :            : 
    1086         [ +  + ]:      18308 :         for (r = 0; r < PCI_MAX_RESOURCE; r++) {
    1087                 :      16808 :                 res = dpdk_pci_device_get_mem_resource(dev, r);
    1088                 :            : 
    1089   [ +  +  +  +  :      16808 :                 if (res->phys_addr == 0 || vaddr < (uint64_t)res->addr ||
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    1090   [ +  +  #  #  :       6500 :                     (vaddr + len) >= (uint64_t)res->addr + res->len) {
             #  #  #  # ]
    1091                 :      15144 :                         continue;
    1092                 :            :                 }
    1093                 :            : 
    1094                 :            : #if VFIO_ENABLED
    1095   [ -  +  -  - ]:       1664 :                 if (spdk_iommu_is_enabled() && rte_eal_iova_mode() == RTE_IOVA_VA) {
    1096                 :            :                         /*
    1097                 :            :                          * The IOMMU is on and we're using IOVA == VA. The BAR was
    1098                 :            :                          * automatically registered when it was mapped, so just return
    1099                 :            :                          * the virtual address here.
    1100                 :            :                          */
    1101                 :          0 :                         return vaddr;
    1102                 :            :                 }
    1103                 :            : #endif
    1104   [ #  #  #  #  :       1664 :                 paddr = res->phys_addr + (vaddr - (uint64_t)res->addr);
             #  #  #  # ]
    1105                 :       1664 :                 return paddr;
    1106                 :            :         }
    1107                 :            : 
    1108                 :       1500 :         return SPDK_VTOPHYS_ERROR;
    1109                 :          0 : }
    1110                 :            : 
    1111                 :            : /* Try to get the paddr from pci devices */
    1112                 :            : static uint64_t
    1113                 :       2684 : vtophys_get_paddr_pci(uint64_t vaddr, size_t len)
    1114                 :            : {
    1115                 :            :         struct spdk_vtophys_pci_device *vtophys_dev;
    1116                 :            :         uintptr_t paddr;
    1117                 :            :         struct rte_pci_device   *dev;
    1118                 :            : 
    1119         [ -  + ]:       2684 :         pthread_mutex_lock(&g_vtophys_pci_devices_mutex);
    1120   [ +  +  #  #  :       4184 :         TAILQ_FOREACH(vtophys_dev, &g_vtophys_pci_devices, tailq) {
             #  #  #  # ]
    1121   [ #  #  #  # ]:       3164 :                 dev = vtophys_dev->pci_device;
    1122                 :       3164 :                 paddr = pci_device_vtophys(dev, vaddr, len);
    1123         [ +  + ]:       3164 :                 if (paddr != SPDK_VTOPHYS_ERROR) {
    1124         [ #  # ]:       1664 :                         pthread_mutex_unlock(&g_vtophys_pci_devices_mutex);
    1125                 :       1664 :                         return paddr;
    1126                 :            :                 }
    1127                 :          0 :         }
    1128         [ -  + ]:       1020 :         pthread_mutex_unlock(&g_vtophys_pci_devices_mutex);
    1129                 :            : 
    1130                 :       1020 :         return SPDK_VTOPHYS_ERROR;
    1131                 :          0 : }
    1132                 :            : 
    1133                 :            : static int
    1134                 :      65636 : vtophys_notify(void *cb_ctx, struct spdk_mem_map *map,
    1135                 :            :                enum spdk_mem_map_notify_action action,
    1136                 :            :                void *vaddr, size_t len)
    1137                 :            : {
    1138                 :      65636 :         int rc = 0;
    1139                 :            :         uint64_t paddr;
    1140                 :            : 
    1141   [ +  +  -  + ]:      65636 :         if ((uintptr_t)vaddr & ~MASK_256TB) {
    1142                 :          0 :                 DEBUG_PRINT("invalid usermode virtual address %p\n", vaddr);
    1143                 :          0 :                 return -EINVAL;
    1144                 :            :         }
    1145                 :            : 
    1146   [ +  -  +  +  :      65636 :         if (((uintptr_t)vaddr & MASK_2MB) || (len & MASK_2MB)) {
             +  -  -  + ]
    1147                 :          0 :                 DEBUG_PRINT("invalid parameters, vaddr=%p len=%ju\n",
    1148                 :            :                             vaddr, len);
    1149                 :          0 :                 return -EINVAL;
    1150                 :            :         }
    1151                 :            : 
    1152                 :            :         /* Get the physical address from the DPDK memsegs */
    1153                 :      65636 :         paddr = vtophys_get_paddr_memseg((uint64_t)vaddr);
    1154                 :            : 
    1155      [ +  +  + ]:      65636 :         switch (action) {
    1156                 :      32313 :         case SPDK_MEM_MAP_NOTIFY_REGISTER:
    1157         [ +  + ]:      32901 :                 if (paddr == SPDK_VTOPHYS_ERROR) {
    1158                 :            :                         /* This is not an address that DPDK is managing. */
    1159                 :            : 
    1160                 :            :                         /* Check if this is a PCI BAR. They need special handling */
    1161                 :        556 :                         paddr = vtophys_get_paddr_pci((uint64_t)vaddr, len);
    1162         [ +  + ]:        556 :                         if (paddr != SPDK_VTOPHYS_ERROR) {
    1163                 :            :                                 /* Get paddr for each 2MB chunk in this address range */
    1164         [ +  + ]:        832 :                                 while (len > 0) {
    1165         [ #  # ]:        786 :                                         paddr = vtophys_get_paddr_pci((uint64_t)vaddr, VALUE_2MB);
    1166         [ -  + ]:        786 :                                         if (paddr == SPDK_VTOPHYS_ERROR) {
    1167                 :          0 :                                                 DEBUG_PRINT("could not get phys addr for %p\n", vaddr);
    1168                 :          0 :                                                 return -EFAULT;
    1169                 :            :                                         }
    1170                 :            : 
    1171         [ #  # ]:        786 :                                         rc = spdk_mem_map_set_translation(map, (uint64_t)vaddr, VALUE_2MB, paddr);
    1172         [ -  + ]:        786 :                                         if (rc != 0) {
    1173                 :          0 :                                                 return rc;
    1174                 :            :                                         }
    1175                 :            : 
    1176         [ #  # ]:        786 :                                         vaddr += VALUE_2MB;
    1177         [ #  # ]:        786 :                                         len -= VALUE_2MB;
    1178                 :            :                                 }
    1179                 :            : 
    1180                 :         46 :                                 return 0;
    1181                 :            :                         }
    1182                 :            : 
    1183                 :            : #if VFIO_ENABLED
    1184                 :            :                         enum rte_iova_mode iova_mode;
    1185                 :            : 
    1186                 :        510 :                         iova_mode = rte_eal_iova_mode();
    1187                 :            : 
    1188   [ +  +  +  - ]:        725 :                         if (spdk_iommu_is_enabled() && iova_mode == RTE_IOVA_VA) {
    1189                 :            :                                 /* We'll use the virtual address as the iova to match DPDK. */
    1190                 :        215 :                                 paddr = (uint64_t)vaddr;
    1191                 :        215 :                                 rc = vtophys_iommu_map_dma((uint64_t)vaddr, paddr, len);
    1192         [ -  + ]:        215 :                                 if (rc) {
    1193                 :          0 :                                         return -EFAULT;
    1194                 :            :                                 }
    1195         [ +  + ]:      41326 :                                 while (len > 0) {
    1196         [ #  # ]:      41111 :                                         rc = spdk_mem_map_set_translation(map, (uint64_t)vaddr, VALUE_2MB, paddr);
    1197         [ -  + ]:      41111 :                                         if (rc != 0) {
    1198                 :          0 :                                                 return rc;
    1199                 :            :                                         }
    1200         [ #  # ]:      41111 :                                         vaddr += VALUE_2MB;
    1201         [ #  # ]:      41111 :                                         paddr += VALUE_2MB;
    1202         [ #  # ]:      41111 :                                         len -= VALUE_2MB;
    1203                 :            :                                 }
    1204                 :          0 :                         } else
    1205                 :            : #endif
    1206                 :            :                         {
    1207                 :            :                                 /* Get the physical address from /proc/self/pagemap. */
    1208                 :        295 :                                 paddr = vtophys_get_paddr_pagemap((uint64_t)vaddr);
    1209         [ +  + ]:        295 :                                 if (paddr == SPDK_VTOPHYS_ERROR) {
    1210                 :          2 :                                         DEBUG_PRINT("could not get phys addr for %p\n", vaddr);
    1211                 :          2 :                                         return -EFAULT;
    1212                 :            :                                 }
    1213                 :            : 
    1214                 :            :                                 /* Get paddr for each 2MB chunk in this address range */
    1215         [ +  + ]:      16569 :                                 while (len > 0) {
    1216                 :            :                                         /* Get the physical address from /proc/self/pagemap. */
    1217                 :      16276 :                                         paddr = vtophys_get_paddr_pagemap((uint64_t)vaddr);
    1218                 :            : 
    1219         [ -  + ]:      16276 :                                         if (paddr == SPDK_VTOPHYS_ERROR) {
    1220                 :          0 :                                                 DEBUG_PRINT("could not get phys addr for %p\n", vaddr);
    1221                 :          0 :                                                 return -EFAULT;
    1222                 :            :                                         }
    1223                 :            : 
    1224   [ -  +  #  # ]:      16276 :                                         if (paddr & MASK_2MB) {
    1225                 :          0 :                                                 DEBUG_PRINT("invalid paddr 0x%" PRIx64 " - must be 2MB aligned\n", paddr);
    1226                 :          0 :                                                 return -EINVAL;
    1227                 :            :                                         }
    1228                 :            : #if VFIO_ENABLED
    1229                 :            :                                         /* If the IOMMU is on, but DPDK is using iova-mode=pa, we want to register this memory
    1230                 :            :                                          * with the IOMMU using the physical address to match. */
    1231         [ -  + ]:      16276 :                                         if (spdk_iommu_is_enabled()) {
    1232         [ #  # ]:          0 :                                                 rc = vtophys_iommu_map_dma((uint64_t)vaddr, paddr, VALUE_2MB);
    1233         [ #  # ]:          0 :                                                 if (rc) {
    1234                 :          0 :                                                         DEBUG_PRINT("Unable to assign vaddr %p to paddr 0x%" PRIx64 "\n", vaddr, paddr);
    1235                 :          0 :                                                         return -EFAULT;
    1236                 :            :                                                 }
    1237                 :          0 :                                         }
    1238                 :            : #endif
    1239                 :            : 
    1240         [ #  # ]:      16276 :                                         rc = spdk_mem_map_set_translation(map, (uint64_t)vaddr, VALUE_2MB, paddr);
    1241         [ -  + ]:      16276 :                                         if (rc != 0) {
    1242                 :          0 :                                                 return rc;
    1243                 :            :                                         }
    1244                 :            : 
    1245         [ #  # ]:      16276 :                                         vaddr += VALUE_2MB;
    1246         [ #  # ]:      16276 :                                         len -= VALUE_2MB;
    1247                 :            :                                 }
    1248                 :            :                         }
    1249                 :          0 :                 } else {
    1250                 :            :                         /* This is an address managed by DPDK. Just setup the translations. */
    1251         [ +  + ]:    1016335 :                         while (len > 0) {
    1252                 :     983990 :                                 paddr = vtophys_get_paddr_memseg((uint64_t)vaddr);
    1253         [ +  + ]:     983990 :                                 if (paddr == SPDK_VTOPHYS_ERROR) {
    1254                 :          0 :                                         DEBUG_PRINT("could not get phys addr for %p\n", vaddr);
    1255                 :          0 :                                         return -EFAULT;
    1256                 :            :                                 }
    1257                 :            : 
    1258         [ +  - ]:     983990 :                                 rc = spdk_mem_map_set_translation(map, (uint64_t)vaddr, VALUE_2MB, paddr);
    1259         [ +  + ]:     983990 :                                 if (rc != 0) {
    1260                 :          0 :                                         return rc;
    1261                 :            :                                 }
    1262                 :            : 
    1263         [ +  - ]:     983990 :                                 vaddr += VALUE_2MB;
    1264         [ +  - ]:     983990 :                                 len -= VALUE_2MB;
    1265                 :            :                         }
    1266                 :            :                 }
    1267                 :            : 
    1268                 :      32853 :                 break;
    1269                 :      32159 :         case SPDK_MEM_MAP_NOTIFY_UNREGISTER:
    1270                 :            : #if VFIO_ENABLED
    1271         [ +  + ]:      32677 :                 if (paddr == SPDK_VTOPHYS_ERROR) {
    1272                 :            :                         /*
    1273                 :            :                          * This is not an address that DPDK is managing.
    1274                 :            :                          */
    1275                 :            : 
    1276                 :            :                         /* Check if this is a PCI BAR. They need special handling */
    1277                 :        556 :                         paddr = vtophys_get_paddr_pci((uint64_t)vaddr, len);
    1278         [ +  + ]:        556 :                         if (paddr != SPDK_VTOPHYS_ERROR) {
    1279                 :            :                                 /* Get paddr for each 2MB chunk in this address range */
    1280         [ +  + ]:        832 :                                 while (len > 0) {
    1281         [ #  # ]:        786 :                                         paddr = vtophys_get_paddr_pci((uint64_t)vaddr, VALUE_2MB);
    1282         [ -  + ]:        786 :                                         if (paddr == SPDK_VTOPHYS_ERROR) {
    1283                 :          0 :                                                 DEBUG_PRINT("could not get phys addr for %p\n", vaddr);
    1284                 :          0 :                                                 return -EFAULT;
    1285                 :            :                                         }
    1286                 :            : 
    1287         [ #  # ]:        786 :                                         rc = spdk_mem_map_clear_translation(map, (uint64_t)vaddr, VALUE_2MB);
    1288         [ -  + ]:        786 :                                         if (rc != 0) {
    1289                 :          0 :                                                 return rc;
    1290                 :            :                                         }
    1291                 :            : 
    1292         [ #  # ]:        786 :                                         vaddr += VALUE_2MB;
    1293         [ #  # ]:        786 :                                         len -= VALUE_2MB;
    1294                 :            :                                 }
    1295                 :            : 
    1296                 :         46 :                                 return 0;
    1297                 :            :                         }
    1298                 :            : 
    1299                 :            :                         /* If vfio is enabled,
    1300                 :            :                          * we need to unmap the range from the IOMMU
    1301                 :            :                          */
    1302         [ +  + ]:        510 :                         if (spdk_iommu_is_enabled()) {
    1303                 :        215 :                                 uint64_t buffer_len = len;
    1304                 :        215 :                                 uint8_t *va = vaddr;
    1305                 :            :                                 enum rte_iova_mode iova_mode;
    1306                 :            : 
    1307                 :        215 :                                 iova_mode = rte_eal_iova_mode();
    1308                 :            :                                 /*
    1309                 :            :                                  * In virtual address mode, the region is contiguous and can be done in
    1310                 :            :                                  * one unmap.
    1311                 :            :                                  */
    1312         [ +  - ]:        215 :                                 if (iova_mode == RTE_IOVA_VA) {
    1313                 :        215 :                                         paddr = spdk_mem_map_translate(map, (uint64_t)va, &buffer_len);
    1314   [ +  -  -  + ]:        215 :                                         if (buffer_len != len || paddr != (uintptr_t)va) {
    1315                 :          0 :                                                 DEBUG_PRINT("Unmapping %p with length %lu failed because "
    1316                 :            :                                                             "translation had address 0x%" PRIx64 " and length %lu\n",
    1317                 :            :                                                             va, len, paddr, buffer_len);
    1318                 :          0 :                                                 return -EINVAL;
    1319                 :            :                                         }
    1320                 :        215 :                                         rc = vtophys_iommu_unmap_dma(paddr, len);
    1321         [ -  + ]:        215 :                                         if (rc) {
    1322                 :          0 :                                                 DEBUG_PRINT("Failed to iommu unmap paddr 0x%" PRIx64 "\n", paddr);
    1323                 :          0 :                                                 return -EFAULT;
    1324                 :            :                                         }
    1325         [ #  # ]:          0 :                                 } else if (iova_mode == RTE_IOVA_PA) {
    1326                 :            :                                         /* Get paddr for each 2MB chunk in this address range */
    1327         [ #  # ]:          0 :                                         while (buffer_len > 0) {
    1328                 :          0 :                                                 paddr = spdk_mem_map_translate(map, (uint64_t)va, NULL);
    1329                 :            : 
    1330   [ #  #  #  #  :          0 :                                                 if (paddr == SPDK_VTOPHYS_ERROR || buffer_len < VALUE_2MB) {
                   #  # ]
    1331                 :          0 :                                                         DEBUG_PRINT("could not get phys addr for %p\n", va);
    1332                 :          0 :                                                         return -EFAULT;
    1333                 :            :                                                 }
    1334                 :            : 
    1335         [ #  # ]:          0 :                                                 rc = vtophys_iommu_unmap_dma(paddr, VALUE_2MB);
    1336         [ #  # ]:          0 :                                                 if (rc) {
    1337                 :          0 :                                                         DEBUG_PRINT("Failed to iommu unmap paddr 0x%" PRIx64 "\n", paddr);
    1338                 :          0 :                                                         return -EFAULT;
    1339                 :            :                                                 }
    1340                 :            : 
    1341   [ #  #  #  # ]:          0 :                                                 va += VALUE_2MB;
    1342         [ #  # ]:          0 :                                                 buffer_len -= VALUE_2MB;
    1343                 :            :                                         }
    1344                 :          0 :                                 }
    1345                 :          0 :                         }
    1346                 :          0 :                 }
    1347                 :            : #endif
    1348         [ +  + ]:     941745 :                 while (len > 0) {
    1349         [ +  - ]:     909056 :                         rc = spdk_mem_map_clear_translation(map, (uint64_t)vaddr, VALUE_2MB);
    1350         [ +  + ]:     909056 :                         if (rc != 0) {
    1351                 :          0 :                                 return rc;
    1352                 :            :                         }
    1353                 :            : 
    1354         [ +  - ]:     909056 :                         vaddr += VALUE_2MB;
    1355         [ +  - ]:     909056 :                         len -= VALUE_2MB;
    1356                 :            :                 }
    1357                 :            : 
    1358                 :      32689 :                 break;
    1359                 :          0 :         default:
    1360         [ #  # ]:          0 :                 SPDK_UNREACHABLE();
    1361                 :            :         }
    1362                 :            : 
    1363                 :      65542 :         return rc;
    1364                 :       1164 : }
    1365                 :            : 
    1366                 :            : static int
    1367                 :      65652 : numa_notify(void *cb_ctx, struct spdk_mem_map *map,
    1368                 :            :             enum spdk_mem_map_notify_action action,
    1369                 :            :             void *vaddr, size_t len)
    1370                 :            : {
    1371                 :            :         struct rte_memseg *seg;
    1372                 :            : 
    1373                 :            :         /* We always return 0 from here, even if we aren't able to get a
    1374                 :            :          * memseg for the address. This can happen in non-DPDK memory
    1375                 :            :          * registration paths, for example vhost or vfio-user. That is OK,
    1376                 :            :          * spdk_mem_get_numa_id() just returns SPDK_ENV_NUMA_ID_ANY for
    1377                 :            :          * that kind of memory. If we return an error here, the
    1378                 :            :          * spdk_mem_register() from vhost or vfio-user would fail which is
    1379                 :            :          * not what we want.
    1380                 :            :          */
    1381                 :      65652 :         seg = rte_mem_virt2memseg(vaddr, NULL);
    1382         [ +  + ]:      65652 :         if (seg == NULL) {
    1383                 :       1112 :                 return 0;
    1384                 :            :         }
    1385                 :            : 
    1386      [ +  +  + ]:      64540 :         switch (action) {
    1387                 :      31765 :         case SPDK_MEM_MAP_NOTIFY_REGISTER:
    1388   [ -  +  -  + ]:      32353 :                 spdk_mem_map_set_translation(map, (uint64_t)vaddr, len, seg->socket_id);
    1389                 :      32353 :                 break;
    1390                 :      31611 :         case SPDK_MEM_MAP_NOTIFY_UNREGISTER:
    1391                 :      32187 :                 spdk_mem_map_clear_translation(map, (uint64_t)vaddr, len);
    1392                 :      32187 :                 break;
    1393                 :          0 :         default:
    1394                 :          0 :                 break;
    1395                 :            :         }
    1396                 :            : 
    1397                 :      64540 :         return 0;
    1398                 :       1164 : }
    1399                 :            : 
    1400                 :            : static int
    1401                 :     158600 : vtophys_check_contiguous_entries(uint64_t paddr1, uint64_t paddr2)
    1402                 :            : {
    1403                 :            :         /* This function is always called with paddrs for two subsequent
    1404                 :            :          * 2MB chunks in virtual address space, so those chunks will be only
    1405                 :            :          * physically contiguous if the physical addresses are 2MB apart
    1406                 :            :          * from each other as well.
    1407                 :            :          */
    1408         [ +  - ]:     158600 :         return (paddr2 - paddr1 == VALUE_2MB);
    1409                 :            : }
    1410                 :            : 
    1411                 :            : #if VFIO_ENABLED
    1412                 :            : 
    1413                 :            : static bool
    1414                 :       2565 : vfio_enabled(void)
    1415                 :            : {
    1416                 :       2565 :         return rte_vfio_is_enabled("vfio_pci");
    1417                 :            : }
    1418                 :            : 
    1419                 :            : /* Check if IOMMU is enabled on the system */
    1420                 :            : static bool
    1421                 :        922 : has_iommu_groups(void)
    1422                 :            : {
    1423                 :        922 :         int count = 0;
    1424         [ +  + ]:        922 :         DIR *dir = opendir("/sys/kernel/iommu_groups");
    1425                 :            : 
    1426         [ +  + ]:        922 :         if (dir == NULL) {
    1427                 :          0 :                 return false;
    1428                 :            :         }
    1429                 :            : 
    1430   [ +  +  +  +  :       3500 :         while (count < 3 && readdir(dir) != NULL) {
                   +  + ]
    1431         [ +  - ]:       2578 :                 count++;
    1432                 :            :         }
    1433                 :            : 
    1434         [ +  + ]:        922 :         closedir(dir);
    1435                 :            :         /* there will always be ./ and ../ entries */
    1436                 :        922 :         return count > 2;
    1437                 :         66 : }
    1438                 :            : 
    1439                 :            : static bool
    1440                 :        922 : vfio_noiommu_enabled(void)
    1441                 :            : {
    1442                 :        922 :         return rte_vfio_noiommu_is_enabled();
    1443                 :            : }
    1444                 :            : 
    1445                 :            : static void
    1446                 :       2565 : vtophys_iommu_init(void)
    1447                 :            : {
    1448                 :       1034 :         char proc_fd_path[PATH_MAX + 1];
    1449                 :       1034 :         char link_path[PATH_MAX + 1];
    1450                 :       2565 :         const char vfio_path[] = "/dev/vfio/vfio";
    1451                 :            :         DIR *dir;
    1452                 :            :         struct dirent *d;
    1453                 :            : 
    1454         [ +  + ]:       2565 :         if (!vfio_enabled()) {
    1455                 :       1643 :                 return;
    1456                 :            :         }
    1457                 :            : 
    1458         [ -  + ]:        922 :         if (vfio_noiommu_enabled()) {
    1459                 :          0 :                 g_vfio.noiommu_enabled = true;
    1460         [ +  + ]:        922 :         } else if (!has_iommu_groups()) {
    1461                 :        188 :                 return;
    1462                 :            :         }
    1463                 :            : 
    1464         [ +  - ]:        734 :         dir = opendir("/proc/self/fd");
    1465         [ +  + ]:        734 :         if (!dir) {
    1466         [ #  # ]:          0 :                 DEBUG_PRINT("Failed to open /proc/self/fd (%d)\n", errno);
    1467                 :          0 :                 return;
    1468                 :            :         }
    1469                 :            : 
    1470   [ +  +  +  + ]:       8456 :         while ((d = readdir(dir)) != NULL) {
    1471   [ +  +  +  -  :       8456 :                 if (d->d_type != DT_LNK) {
                   +  + ]
    1472                 :       1468 :                         continue;
    1473                 :            :                 }
    1474                 :            : 
    1475         [ +  - ]:       6988 :                 snprintf(proc_fd_path, sizeof(proc_fd_path), "/proc/self/fd/%s", d->d_name);
    1476   [ +  +  +  -  :       6988 :                 if (readlink(proc_fd_path, link_path, sizeof(link_path)) != (sizeof(vfio_path) - 1)) {
                   +  + ]
    1477                 :       6073 :                         continue;
    1478                 :            :                 }
    1479                 :            : 
    1480   [ +  +  +  -  :        915 :                 if (memcmp(link_path, vfio_path, sizeof(vfio_path) - 1) == 0) {
                   -  + ]
    1481         [ -  + ]:        734 :                         sscanf(d->d_name, "%d", &g_vfio.fd);
    1482                 :        734 :                         break;
    1483                 :            :                 }
    1484                 :            :         }
    1485                 :            : 
    1486         [ +  + ]:        734 :         closedir(dir);
    1487                 :            : 
    1488         [ +  + ]:        734 :         if (g_vfio.fd < 0) {
    1489                 :          0 :                 DEBUG_PRINT("Failed to discover DPDK VFIO container fd.\n");
    1490                 :          0 :                 return;
    1491                 :            :         }
    1492                 :            : 
    1493         [ +  - ]:        734 :         g_vfio.enabled = true;
    1494                 :            : 
    1495                 :        734 :         return;
    1496                 :         66 : }
    1497                 :            : 
    1498                 :            : #endif
    1499                 :            : 
    1500                 :            : void
    1501                 :       1835 : vtophys_pci_device_added(struct rte_pci_device *pci_device)
    1502                 :            : {
    1503                 :            :         struct spdk_vtophys_pci_device *vtophys_dev;
    1504                 :            : 
    1505         [ +  - ]:       1835 :         pthread_mutex_lock(&g_vtophys_pci_devices_mutex);
    1506                 :            : 
    1507                 :       1835 :         vtophys_dev = calloc(1, sizeof(*vtophys_dev));
    1508         [ +  + ]:       1835 :         if (vtophys_dev) {
    1509   [ +  -  +  - ]:       1835 :                 vtophys_dev->pci_device = pci_device;
    1510   [ +  -  +  -  :       1835 :                 TAILQ_INSERT_TAIL(&g_vtophys_pci_devices, vtophys_dev, tailq);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
             -  +  -  +  
                      - ]
    1511                 :         30 :         } else {
    1512                 :          0 :                 DEBUG_PRINT("Memory allocation error\n");
    1513                 :            :         }
    1514         [ +  - ]:       1835 :         pthread_mutex_unlock(&g_vtophys_pci_devices_mutex);
    1515                 :            : 
    1516                 :            : #if VFIO_ENABLED
    1517                 :            :         struct spdk_vfio_dma_map *dma_map;
    1518                 :            :         int ret;
    1519                 :            : 
    1520   [ +  +  +  +  :       1807 :         if (!g_vfio.enabled) {
                   +  - ]
    1521                 :        783 :                 return;
    1522                 :            :         }
    1523                 :            : 
    1524         [ +  - ]:       1024 :         pthread_mutex_lock(&g_vfio.mutex);
    1525                 :       1024 :         g_vfio.device_ref++;
    1526   [ +  +  -  + ]:       1024 :         if (g_vfio.device_ref > 1) {
    1527         [ #  # ]:        885 :                 pthread_mutex_unlock(&g_vfio.mutex);
    1528                 :        885 :                 return;
    1529                 :            :         }
    1530                 :            : 
    1531                 :            :         /* This is the first SPDK device using DPDK vfio. This means that the first
    1532                 :            :          * IOMMU group might have been just been added to the DPDK vfio container.
    1533                 :            :          * From this point it is certain that the memory can be mapped now.
    1534                 :            :          */
    1535   [ +  +  +  -  :        667 :         TAILQ_FOREACH(dma_map, &g_vfio.maps, tailq) {
          +  +  +  -  +  
                -  +  - ]
    1536   [ +  -  -  +  :        537 :                 ret = ioctl(g_vfio.fd, VFIO_IOMMU_MAP_DMA, &dma_map->map);
          +  -  -  +  +  
          -  -  +  +  -  
                   +  - ]
    1537         [ +  + ]:        537 :                 if (ret) {
    1538         [ #  # ]:          9 :                         DEBUG_PRINT("Cannot update DMA mapping, error %d\n", errno);
    1539                 :          9 :                         break;
    1540                 :            :                 }
    1541                 :          2 :         }
    1542         [ +  - ]:        139 :         pthread_mutex_unlock(&g_vfio.mutex);
    1543                 :            : #endif
    1544                 :         30 : }
    1545                 :            : 
    1546                 :            : void
    1547                 :       1114 : vtophys_pci_device_removed(struct rte_pci_device *pci_device)
    1548                 :            : {
    1549                 :            :         struct spdk_vtophys_pci_device *vtophys_dev;
    1550                 :            : 
    1551         [ +  - ]:       1114 :         pthread_mutex_lock(&g_vtophys_pci_devices_mutex);
    1552   [ +  +  #  #  :       1241 :         TAILQ_FOREACH(vtophys_dev, &g_vtophys_pci_devices, tailq) {
             #  #  #  # ]
    1553   [ +  +  +  -  :       1241 :                 if (vtophys_dev->pci_device == pci_device) {
                   -  + ]
    1554   [ +  +  +  -  :       1114 :                         TAILQ_REMOVE(&g_vtophys_pci_devices, vtophys_dev, tailq);
          +  -  -  +  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
    1555                 :       1114 :                         free(vtophys_dev);
    1556                 :       1114 :                         break;
    1557                 :            :                 }
    1558                 :          0 :         }
    1559         [ +  - ]:       1114 :         pthread_mutex_unlock(&g_vtophys_pci_devices_mutex);
    1560                 :            : 
    1561                 :            : #if VFIO_ENABLED
    1562                 :            :         struct spdk_vfio_dma_map *dma_map;
    1563                 :            :         int ret;
    1564                 :            : 
    1565   [ +  +  +  +  :       1087 :         if (!g_vfio.enabled) {
                   +  - ]
    1566                 :        570 :                 return;
    1567                 :            :         }
    1568                 :            : 
    1569         [ +  - ]:        517 :         pthread_mutex_lock(&g_vfio.mutex);
    1570   [ +  +  +  -  :        517 :         assert(g_vfio.device_ref > 0);
                   #  # ]
    1571                 :        517 :         g_vfio.device_ref--;
    1572   [ +  +  -  + ]:        517 :         if (g_vfio.device_ref > 0) {
    1573         [ #  # ]:        414 :                 pthread_mutex_unlock(&g_vfio.mutex);
    1574                 :        414 :                 return;
    1575                 :            :         }
    1576                 :            : 
    1577                 :            :         /* This is the last SPDK device using DPDK vfio. If DPDK doesn't have
    1578                 :            :          * any additional devices using it's vfio container, all the mappings
    1579                 :            :          * will be automatically removed by the Linux vfio driver. We unmap
    1580                 :            :          * the memory manually to be able to easily re-map it later regardless
    1581                 :            :          * of other, external factors.
    1582                 :            :          */
    1583   [ +  +  +  -  :        103 :         TAILQ_FOREACH(dma_map, &g_vfio.maps, tailq) {
          +  -  #  #  #  
                #  #  # ]
    1584                 :         11 :                 struct vfio_iommu_type1_dma_unmap unmap = {};
    1585                 :         11 :                 unmap.argsz = sizeof(unmap);
    1586         [ #  # ]:         11 :                 unmap.flags = 0;
    1587   [ #  #  #  #  :         11 :                 unmap.iova = dma_map->map.iova;
             #  #  #  # ]
    1588   [ #  #  #  #  :         11 :                 unmap.size = dma_map->map.size;
             #  #  #  # ]
    1589   [ #  #  #  #  :         11 :                 ret = ioctl(g_vfio.fd, VFIO_IOMMU_UNMAP_DMA, &unmap);
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    1590         [ +  - ]:         11 :                 if (ret) {
    1591         [ #  # ]:         11 :                         DEBUG_PRINT("Cannot unmap DMA memory, error %d\n", errno);
    1592                 :         11 :                         break;
    1593                 :            :                 }
    1594                 :          0 :         }
    1595         [ +  - ]:        103 :         pthread_mutex_unlock(&g_vfio.mutex);
    1596                 :            : #endif
    1597                 :         29 : }
    1598                 :            : 
    1599                 :            : int
    1600                 :       2632 : vtophys_init(void)
    1601                 :            : {
    1602                 :       2632 :         const struct spdk_mem_map_ops vtophys_map_ops = {
    1603                 :            :                 .notify_cb = vtophys_notify,
    1604                 :            :                 .are_contiguous = vtophys_check_contiguous_entries,
    1605                 :            :         };
    1606                 :            : 
    1607                 :       2632 :         const struct spdk_mem_map_ops phys_ref_map_ops = {
    1608                 :            :                 .notify_cb = NULL,
    1609                 :            :                 .are_contiguous = NULL,
    1610                 :            :         };
    1611                 :            : 
    1612                 :       2632 :         const struct spdk_mem_map_ops numa_map_ops = {
    1613                 :            :                 .notify_cb = numa_notify,
    1614                 :            :                 .are_contiguous = NULL,
    1615                 :            :         };
    1616                 :            : 
    1617                 :            : #if VFIO_ENABLED
    1618                 :       2565 :         vtophys_iommu_init();
    1619                 :            : #endif
    1620                 :            : 
    1621                 :       2632 :         g_phys_ref_map = spdk_mem_map_alloc(0, &phys_ref_map_ops, NULL);
    1622         [ +  + ]:       2632 :         if (g_phys_ref_map == NULL) {
    1623                 :          0 :                 DEBUG_PRINT("phys_ref map allocation failed.\n");
    1624                 :          0 :                 return -ENOMEM;
    1625                 :            :         }
    1626                 :            : 
    1627                 :       2632 :         g_numa_map = spdk_mem_map_alloc(SPDK_ENV_NUMA_ID_ANY, &numa_map_ops, NULL);
    1628         [ +  + ]:       2632 :         if (g_numa_map == NULL) {
    1629                 :          0 :                 DEBUG_PRINT("numa map allocation failed.\n");
    1630                 :          0 :                 spdk_mem_map_free(&g_phys_ref_map);
    1631                 :          0 :                 return -ENOMEM;
    1632                 :            :         }
    1633                 :            : 
    1634   [ +  +  +  + ]:       2632 :         if (g_huge_pages) {
    1635                 :       2624 :                 g_vtophys_map = spdk_mem_map_alloc(SPDK_VTOPHYS_ERROR, &vtophys_map_ops, NULL);
    1636         [ +  + ]:       2624 :                 if (g_vtophys_map == NULL) {
    1637                 :          0 :                         DEBUG_PRINT("vtophys map allocation failed\n");
    1638                 :          0 :                         spdk_mem_map_free(&g_numa_map);
    1639                 :          0 :                         spdk_mem_map_free(&g_phys_ref_map);
    1640                 :          0 :                         return -ENOMEM;
    1641                 :            :                 }
    1642                 :        133 :         }
    1643                 :       2632 :         return 0;
    1644                 :        133 : }
    1645                 :            : 
    1646                 :            : void
    1647                 :       2563 : vtophys_fini(void)
    1648                 :            : {
    1649                 :       2563 :         spdk_mem_map_free(&g_vtophys_map);
    1650                 :       2563 :         spdk_mem_map_free(&g_numa_map);
    1651                 :       2563 :         spdk_mem_map_free(&g_phys_ref_map);
    1652                 :       2563 : }
    1653                 :            : 
    1654                 :            : uint64_t
    1655                 :  103072821 : spdk_vtophys(const void *buf, uint64_t *size)
    1656                 :            : {
    1657                 :            :         uint64_t vaddr, paddr_2mb;
    1658                 :            : 
    1659   [ +  +  +  + ]:  103072821 :         if (!g_huge_pages) {
    1660                 :          0 :                 return SPDK_VTOPHYS_ERROR;
    1661                 :            :         }
    1662                 :            : 
    1663                 :  103072821 :         vaddr = (uint64_t)buf;
    1664                 :  103072821 :         paddr_2mb = spdk_mem_map_translate(g_vtophys_map, vaddr, size);
    1665                 :            : 
    1666                 :            :         /*
    1667                 :            :          * SPDK_VTOPHYS_ERROR has all bits set, so if the lookup returned SPDK_VTOPHYS_ERROR,
    1668                 :            :          * we will still bitwise-or it with the buf offset below, but the result will still be
    1669                 :            :          * SPDK_VTOPHYS_ERROR. However now that we do + rather than | (due to PCI vtophys being
    1670                 :            :          * unaligned) we must now check the return value before addition.
    1671                 :            :          */
    1672                 :            :         SPDK_STATIC_ASSERT(SPDK_VTOPHYS_ERROR == UINT64_C(-1), "SPDK_VTOPHYS_ERROR should be all 1s");
    1673         [ +  + ]:  103072821 :         if (paddr_2mb == SPDK_VTOPHYS_ERROR) {
    1674                 :        672 :                 return SPDK_VTOPHYS_ERROR;
    1675                 :            :         } else {
    1676         [ -  + ]:  103072149 :                 return paddr_2mb + (vaddr & MASK_2MB);
    1677                 :            :         }
    1678                 :     850752 : }
    1679                 :            : 
    1680                 :            : int32_t
    1681                 :          0 : spdk_mem_get_numa_id(const void *buf, uint64_t *size)
    1682                 :            : {
    1683                 :          0 :         return spdk_mem_map_translate(g_numa_map, (uint64_t)buf, size);
    1684                 :            : }
    1685                 :            : 
    1686                 :            : int
    1687                 :        776 : spdk_mem_get_fd_and_offset(void *vaddr, uint64_t *offset)
    1688                 :            : {
    1689                 :            :         struct rte_memseg *seg;
    1690                 :            :         int ret, fd;
    1691                 :            : 
    1692                 :        776 :         seg = rte_mem_virt2memseg(vaddr, NULL);
    1693         [ +  + ]:        776 :         if (!seg) {
    1694                 :          0 :                 SPDK_ERRLOG("memory %p doesn't exist\n", vaddr);
    1695                 :          0 :                 return -ENOENT;
    1696                 :            :         }
    1697                 :            : 
    1698                 :        776 :         fd = rte_memseg_get_fd_thread_unsafe(seg);
    1699         [ +  + ]:        776 :         if (fd < 0) {
    1700                 :          0 :                 return fd;
    1701                 :            :         }
    1702                 :            : 
    1703                 :        776 :         ret = rte_memseg_get_fd_offset_thread_unsafe(seg, offset);
    1704         [ -  + ]:        776 :         if (ret < 0) {
    1705                 :          0 :                 return ret;
    1706                 :            :         }
    1707                 :            : 
    1708                 :        776 :         return fd;
    1709                 :        720 : }
    1710                 :            : 
    1711                 :            : void
    1712                 :          8 : mem_disable_huge_pages(void)
    1713                 :            : {
    1714                 :          8 :         g_huge_pages = false;
    1715                 :          8 :         mem_map_use_page_shift(SHIFT_4KB);
    1716                 :          8 : }
    1717                 :            : 
    1718                 :            : void
    1719                 :         50 : mem_map_use_page_shift(uint32_t page_shift)
    1720                 :            : {
    1721                 :         50 :         g_map_page_cfg.shift = page_shift;
    1722   [ +  +  +  - ]:         50 :         g_map_page_cfg.size = 1UL << page_shift;
    1723   [ +  -  +  - ]:         50 :         g_map_page_cfg.mask = g_map_page_cfg.size - 1;
    1724   [ +  +  +  - ]:         50 :         g_map_page_cfg.num_pages_per_gb = 1UL << (SHIFT_1GB - page_shift);
    1725                 :         50 : }

Generated by: LCOV version 1.15