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 : 12428 : 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 : 12428 : uint64_t contig_start = UINT64_MAX;
159 : 12428 : uint64_t contig_end = UINT64_MAX;
160 : : struct map_1gb *map_1gb;
161 : : int rc;
162 : :
163 [ + + ]: 12428 : 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 [ + + + - ]: 12428 : pthread_mutex_lock(&g_mem_reg_map->mutex);
169 : :
170 [ + + ]: 180367500 : for (idx_256tb = 0;
171 [ + + ]: 3246403724 : idx_256tb < sizeof(g_mem_reg_map->map_256tb.map) / sizeof(g_mem_reg_map->map_256tb.map[0]);
172 : 3246391296 : idx_256tb++) {
173 [ + - + - : 3246391340 : map_1gb = g_mem_reg_map->map_256tb.map[idx_256tb];
+ - + - ]
174 : :
175 [ + + ]: 3246391340 : if (!map_1gb) {
176 [ + + ]: 3246375499 : 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 : 3246375499 : contig_start = UINT64_MAX;
187 : 3246375499 : continue;
188 : : }
189 : :
190 [ + + + + ]: 78246049 : for (idx_1gb = 0; idx_1gb < MAP_NUM_PAGES_PER_GB; idx_1gb++) {
191 [ + + + + : 78540562 : if ((map_1gb->map[idx_1gb].translation & REG_MAP_REGISTERED) &&
+ - + - +
- + + +
+ ]
192 [ + + ]: 7975745 : (contig_start == UINT64_MAX ||
193 [ + + + - : 7973591 : (map_1gb->map[idx_1gb].translation & REG_MAP_NOTIFY_START) == 0)) {
+ - + - +
- ]
194 : : /* Rebuild the virtual address from the indexes */
195 [ + + + - ]: 7988992 : uint64_t vaddr = (idx_256tb << SHIFT_1GB) | (idx_1gb << MAP_PAGE_SHIFT);
196 : :
197 [ + + ]: 7988992 : if (contig_start == UINT64_MAX) {
198 : 24416 : contig_start = vaddr;
199 : 2154 : }
200 : :
201 : 7988992 : contig_end = vaddr;
202 : 310994 : } else {
203 [ + + ]: 70241260 : if (contig_start != UINT64_MAX) {
204 : : /* End of of a virtually contiguous range */
205 [ + - + - : 45466 : rc = map->ops.notify_cb(map->cb_ctx, map, action,
+ - - + +
- + - +
- ]
206 : 2154 : (void *)contig_start,
207 [ + - ]: 24416 : contig_end - contig_start + MAP_PAGE_SIZE);
208 : : /* Don't bother handling unregister failures. It can't be any worse */
209 [ + + + + ]: 24416 : if (rc != 0 && action == SPDK_MEM_MAP_NOTIFY_REGISTER) {
210 : 44 : 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 : 24372 : idx_1gb--;
217 : 2150 : }
218 : 70241216 : contig_start = UINT64_MAX;
219 : : }
220 : 5834890 : }
221 : 1172 : }
222 : :
223 [ - + - + ]: 12384 : pthread_mutex_unlock(&g_mem_reg_map->mutex);
224 : 12384 : return 0;
225 : :
226 : 40 : err_unregister:
227 : : /* Unwind to the first empty translation so we don't unregister
228 : : * a region that just failed to register.
229 : : */
230 [ + + + + ]: 44 : idx_256tb = MAP_256TB_IDX((contig_start >> MAP_PAGE_SHIFT) - 1);
231 [ + + + + ]: 44 : idx_1gb = MAP_1GB_IDX((contig_start >> MAP_PAGE_SHIFT) - 1);
232 : 44 : contig_start = UINT64_MAX;
233 : 44 : contig_end = UINT64_MAX;
234 : :
235 : : /* Unregister any memory we managed to register before the failure */
236 [ + + ]: 88 : for (; idx_256tb < SIZE_MAX; idx_256tb--) {
237 [ + - + - : 44 : map_1gb = g_mem_reg_map->map_256tb.map[idx_256tb];
+ - + - ]
238 : :
239 [ + + ]: 44 : 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 [ + + ]: 396 : for (; idx_1gb < UINT64_MAX; idx_1gb--) {
252 : : /* Rebuild the virtual address from the indexes */
253 [ + + + - ]: 352 : uint64_t vaddr = (idx_256tb << SHIFT_1GB) | (idx_1gb << MAP_PAGE_SHIFT);
254 [ + + + + : 352 : 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 [ + + ]: 176 : if (contig_end == UINT64_MAX) {
258 : 176 : contig_end = vaddr;
259 : 16 : }
260 : 176 : contig_start = vaddr;
261 : 16 : } else {
262 [ + + ]: 176 : if (contig_end != UINT64_MAX) {
263 [ + + + - : 132 : 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 [ + - + - : 240 : map->ops.notify_cb(map->cb_ctx, map,
+ - - + +
- + - +
- ]
268 : : SPDK_MEM_MAP_NOTIFY_UNREGISTER,
269 : 12 : (void *)contig_start,
270 [ + - ]: 132 : contig_end - contig_start + MAP_PAGE_SIZE);
271 : 12 : }
272 : 176 : contig_end = UINT64_MAX;
273 : : }
274 : 32 : }
275 [ + - ]: 44 : idx_1gb = MAP_NUM_PAGES_PER_GB - 1;
276 : 4 : }
277 : :
278 [ + + + - ]: 44 : pthread_mutex_unlock(&g_mem_reg_map->mutex);
279 : 44 : return rc;
280 : 692 : }
281 : :
282 : : struct spdk_mem_map *
283 : 12026 : 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 : 12026 : map = calloc(1, sizeof(*map));
290 [ + + ]: 12026 : if (map == NULL) {
291 : 0 : return NULL;
292 : : }
293 : :
294 [ + + + + : 12026 : if (pthread_mutex_init(&map->mutex, NULL)) {
- + ]
295 : 0 : free(map);
296 : 0 : return NULL;
297 : : }
298 : :
299 [ + - + - ]: 12026 : map->default_translation = default_translation;
300 [ + - + - ]: 12026 : map->cb_ctx = cb_ctx;
301 [ + + ]: 12026 : if (ops) {
302 [ + - ]: 9163 : map->ops = *ops;
303 : 492 : }
304 : :
305 [ + + + + : 12026 : if (ops && ops->notify_cb) {
+ - + + ]
306 [ + + ]: 6388 : pthread_mutex_lock(&g_spdk_mem_map_mutex);
307 : 6388 : rc = mem_map_notify_walk(map, SPDK_MEM_MAP_NOTIFY_REGISTER);
308 [ + + ]: 6388 : if (rc != 0) {
309 [ + + ]: 44 : pthread_mutex_unlock(&g_spdk_mem_map_mutex);
310 : 44 : DEBUG_PRINT("Initial mem_map notify failed\n");
311 [ + + + - ]: 44 : pthread_mutex_destroy(&map->mutex);
312 [ + + ]: 11534380 : for (i = 0; i < sizeof(map->map_256tb.map) / sizeof(map->map_256tb.map[0]); i++) {
313 [ + - + - : 11534336 : free(map->map_256tb.map[i]);
+ - + - ]
314 : 1048576 : }
315 : 44 : free(map);
316 : 44 : return NULL;
317 : : }
318 [ + - + - : 6344 : TAILQ_INSERT_TAIL(&g_spdk_mem_maps, map, tailq);
+ - + - +
- + - + -
+ - + - +
- + - +
- ]
319 [ + + ]: 6344 : pthread_mutex_unlock(&g_spdk_mem_map_mutex);
320 : 355 : }
321 : :
322 : 11982 : return map;
323 : 633 : }
324 : :
325 : : void
326 : 11722 : spdk_mem_map_free(struct spdk_mem_map **pmap)
327 : : {
328 : : struct spdk_mem_map *map;
329 : : size_t i;
330 : :
331 [ + + ]: 11722 : if (!pmap) {
332 : 0 : return;
333 : : }
334 : :
335 [ + - ]: 11722 : map = *pmap;
336 : :
337 [ + + ]: 11722 : if (!map) {
338 : 348 : return;
339 : : }
340 : :
341 [ + + + - : 11374 : if (map->ops.notify_cb) {
+ - + + ]
342 [ + + ]: 6040 : pthread_mutex_lock(&g_spdk_mem_map_mutex);
343 : 6040 : mem_map_notify_walk(map, SPDK_MEM_MAP_NOTIFY_UNREGISTER);
344 [ + + + - : 6040 : TAILQ_REMOVE(&g_spdk_mem_maps, map, tailq);
+ - - + #
# # # # #
# # # # #
# # # # #
# # - + -
+ - + - +
+ - + - +
- + - + -
+ - + - ]
345 [ + + ]: 6040 : pthread_mutex_unlock(&g_spdk_mem_map_mutex);
346 : 333 : }
347 : :
348 [ + + ]: 2981637230 : for (i = 0; i < sizeof(map->map_256tb.map) / sizeof(map->map_256tb.map[0]); i++) {
349 [ + - + - : 2981625856 : free(map->map_256tb.map[i]);
+ - + - ]
350 : 153354240 : }
351 : :
352 [ + + + - ]: 11374 : pthread_mutex_destroy(&map->mutex);
353 : :
354 : 11374 : free(map);
355 [ + - ]: 11374 : *pmap = NULL;
356 : 605 : }
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 : 39090 : spdk_mem_register(void *_vaddr, size_t len)
366 : : {
367 : : struct spdk_mem_map *map;
368 : : int rc;
369 : 39090 : uint64_t vaddr = (uintptr_t)_vaddr;
370 : : uint64_t seg_vaddr;
371 : : size_t seg_len;
372 : : uint64_t reg;
373 : :
374 [ + + - + ]: 39090 : if ((uintptr_t)vaddr & ~MASK_256TB) {
375 : 0 : DEBUG_PRINT("invalid usermode virtual address %jx\n", vaddr);
376 : 0 : return -EINVAL;
377 : : }
378 : :
379 [ + + + + : 39090 : if (((uintptr_t)vaddr & MAP_PAGE_MASK) || (len & MAP_PAGE_MASK)) {
+ - + + ]
380 : 88 : DEBUG_PRINT("invalid %s parameters, vaddr=%jx len=%ju\n",
381 : : __func__, vaddr, len);
382 : 88 : return -EINVAL;
383 : : }
384 : :
385 [ + + ]: 39002 : if (len == 0) {
386 : 0 : return 0;
387 : : }
388 : :
389 [ + + ]: 39002 : pthread_mutex_lock(&g_spdk_mem_map_mutex);
390 : :
391 : 39002 : seg_vaddr = vaddr;
392 : 39002 : seg_len = len;
393 [ + + ]: 3216829 : while (seg_len > 0) {
394 : 3177871 : reg = spdk_mem_map_translate(g_mem_reg_map, (uint64_t)seg_vaddr, NULL);
395 [ + + + + ]: 3177871 : if (reg & REG_MAP_REGISTERED) {
396 [ + + ]: 44 : pthread_mutex_unlock(&g_spdk_mem_map_mutex);
397 : 44 : return -EBUSY;
398 : : }
399 [ + - ]: 3177827 : seg_vaddr += MAP_PAGE_SIZE;
400 [ + - ]: 3177827 : seg_len -= MAP_PAGE_SIZE;
401 : : }
402 : :
403 : 38958 : seg_vaddr = vaddr;
404 : 38958 : seg_len = 0;
405 [ + + ]: 3216741 : while (len > 0) {
406 [ + + ]: 3263544 : spdk_mem_map_set_translation(g_mem_reg_map, (uint64_t)vaddr, MAP_PAGE_SIZE,
407 [ + - + - : 85761 : seg_len == 0 ? REG_MAP_REGISTERED | REG_MAP_NOTIFY_START : REG_MAP_REGISTERED);
+ - ]
408 [ + - ]: 3177783 : seg_len += MAP_PAGE_SIZE;
409 [ + - ]: 3177783 : vaddr += MAP_PAGE_SIZE;
410 [ + - ]: 3177783 : len -= MAP_PAGE_SIZE;
411 : : }
412 : :
413 [ + + + - : 111691 : TAILQ_FOREACH(map, &g_spdk_mem_maps, tailq) {
+ - + - ]
414 [ + - + - : 72763 : rc = map->ops.notify_cb(map->cb_ctx, map, SPDK_MEM_MAP_NOTIFY_REGISTER,
+ - - + +
- + - +
- ]
415 : 962 : (void *)seg_vaddr, seg_len);
416 [ + + ]: 72735 : if (rc != 0) {
417 [ # # ]: 2 : pthread_mutex_unlock(&g_spdk_mem_map_mutex);
418 : 2 : return rc;
419 : : }
420 : 962 : }
421 : :
422 [ + + ]: 38956 : pthread_mutex_unlock(&g_spdk_mem_map_mutex);
423 : 38956 : return 0;
424 : 674 : }
425 : :
426 : : int
427 : 34452 : spdk_mem_unregister(void *_vaddr, size_t len)
428 : : {
429 : : struct spdk_mem_map *map;
430 : : int rc;
431 : 34452 : uint64_t vaddr = (uintptr_t)_vaddr;
432 : : uint64_t seg_vaddr;
433 : : size_t seg_len;
434 : : uint64_t reg, newreg;
435 : :
436 [ + + - + ]: 34452 : if ((uintptr_t)vaddr & ~MASK_256TB) {
437 : 0 : DEBUG_PRINT("invalid usermode virtual address %jx\n", vaddr);
438 : 0 : return -EINVAL;
439 : : }
440 : :
441 [ + - + + : 34452 : 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 [ + + ]: 34452 : 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 : 34452 : reg = spdk_mem_map_translate(g_mem_reg_map, (uint64_t)vaddr, NULL);
454 [ + + + + : 34452 : if ((reg & REG_MAP_REGISTERED) && (reg & REG_MAP_NOTIFY_START) == 0) {
+ - + + ]
455 [ + + ]: 88 : pthread_mutex_unlock(&g_spdk_mem_map_mutex);
456 : 88 : return -ERANGE;
457 : : }
458 : :
459 : 34364 : seg_vaddr = vaddr;
460 : 34364 : seg_len = len;
461 [ + + ]: 675657 : while (seg_len > 0) {
462 : 641337 : reg = spdk_mem_map_translate(g_mem_reg_map, (uint64_t)seg_vaddr, NULL);
463 [ + + + + ]: 641337 : if ((reg & REG_MAP_REGISTERED) == 0) {
464 [ + + ]: 44 : pthread_mutex_unlock(&g_spdk_mem_map_mutex);
465 : 44 : return -EINVAL;
466 : : }
467 [ + - ]: 641293 : seg_vaddr += MAP_PAGE_SIZE;
468 [ + - ]: 641293 : seg_len -= MAP_PAGE_SIZE;
469 : : }
470 : :
471 : 34320 : 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 [ + + + + : 34320 : if ((newreg & REG_MAP_NOTIFY_START) == 0 && (newreg & REG_MAP_REGISTERED)) {
+ - + + ]
476 [ + + ]: 44 : pthread_mutex_unlock(&g_spdk_mem_map_mutex);
477 : 44 : return -ERANGE;
478 : : }
479 : 34276 : seg_vaddr = vaddr;
480 : 34276 : seg_len = 0;
481 : :
482 [ + + ]: 675525 : while (len > 0) {
483 : 641249 : reg = spdk_mem_map_translate(g_mem_reg_map, (uint64_t)vaddr, NULL);
484 [ + - ]: 641249 : spdk_mem_map_set_translation(g_mem_reg_map, (uint64_t)vaddr, MAP_PAGE_SIZE, 0);
485 : :
486 [ + + + + : 641249 : if (seg_len > 0 && (reg & REG_MAP_NOTIFY_START)) {
+ + ]
487 [ + + + - : 440 : TAILQ_FOREACH_REVERSE(map, &g_spdk_mem_maps, spdk_mem_map_head, tailq) {
+ - + - +
+ + - + -
+ - + - +
- + - ]
488 [ + - + - : 230 : rc = map->ops.notify_cb(map->cb_ctx, map, SPDK_MEM_MAP_NOTIFY_UNREGISTER,
+ - - + +
- + - +
- ]
489 : 20 : (void *)seg_vaddr, seg_len);
490 [ + + ]: 220 : if (rc != 0) {
491 [ # # ]: 0 : pthread_mutex_unlock(&g_spdk_mem_map_mutex);
492 : 0 : return rc;
493 : : }
494 : 20 : }
495 : :
496 : 220 : seg_vaddr = vaddr;
497 [ + - ]: 220 : seg_len = MAP_PAGE_SIZE;
498 : 20 : } else {
499 [ + - ]: 641029 : seg_len += MAP_PAGE_SIZE;
500 : : }
501 : :
502 [ + - ]: 641249 : vaddr += MAP_PAGE_SIZE;
503 [ + - ]: 641249 : len -= MAP_PAGE_SIZE;
504 : : }
505 : :
506 [ + + ]: 34276 : if (seg_len > 0) {
507 [ + + + - : 103305 : TAILQ_FOREACH_REVERSE(map, &g_spdk_mem_maps, spdk_mem_map_head, tailq) {
+ - + - +
+ + - + -
+ - + - +
- + - ]
508 [ + - + - : 69047 : rc = map->ops.notify_cb(map->cb_ctx, map, SPDK_MEM_MAP_NOTIFY_UNREGISTER,
+ - - + +
- + - +
- ]
509 : 926 : (void *)seg_vaddr, seg_len);
510 [ + + ]: 69029 : if (rc != 0) {
511 [ # # ]: 0 : pthread_mutex_unlock(&g_spdk_mem_map_mutex);
512 : 0 : return rc;
513 : : }
514 : 926 : }
515 : 501 : }
516 : :
517 [ + + ]: 34276 : pthread_mutex_unlock(&g_spdk_mem_map_mutex);
518 : 34276 : return 0;
519 : 517 : }
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 : 14308468 : mem_map_get_map_1gb(struct spdk_mem_map *map, uint64_t vfn_page)
574 : : {
575 : : struct map_1gb *map_1gb;
576 [ + + ]: 14308468 : uint64_t idx_256tb = MAP_256TB_IDX(vfn_page);
577 : : size_t i;
578 : :
579 [ + + ]: 14308468 : if (spdk_unlikely(idx_256tb >= SPDK_COUNTOF(map->map_256tb.map))) {
580 : 44 : return NULL;
581 : : }
582 : :
583 [ + - + - : 14308424 : map_1gb = map->map_256tb.map[idx_256tb];
+ - + - ]
584 : :
585 [ + + ]: 14308424 : if (!map_1gb) {
586 [ + + + - ]: 12493 : pthread_mutex_lock(&map->mutex);
587 : :
588 : : /* Recheck to make sure nobody else got the mutex first. */
589 [ + - + - : 12493 : map_1gb = map->map_256tb.map[idx_256tb];
+ - + - ]
590 [ + + ]: 12493 : if (!map_1gb) {
591 [ + - ]: 12493 : map_1gb = malloc(MAP_SIZE_OF_MAP_1GB);
592 [ + - ]: 12493 : if (map_1gb) {
593 : : /* initialize all entries to default translation */
594 [ + + + + ]: 51409613 : for (i = 0; i < MAP_NUM_PAGES_PER_GB; i++) {
595 [ + - + - : 51397120 : map_1gb->map[i].translation = map->default_translation;
+ - + - +
- + - ]
596 : 3560448 : }
597 [ + - + - : 12493 : map->map_256tb.map[idx_256tb] = map_1gb;
+ - + - ]
598 : 822 : }
599 : 822 : }
600 : :
601 [ + + + - ]: 12493 : pthread_mutex_unlock(&map->mutex);
602 : :
603 [ + + ]: 12493 : if (!map_1gb) {
604 : 0 : DEBUG_PRINT("allocation failed\n");
605 : 0 : return NULL;
606 : : }
607 : 822 : }
608 : :
609 : 14308424 : return map_1gb;
610 : 421376 : }
611 : :
612 : : int
613 : 5932089 : 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 [ + + + + ]: 5932089 : if ((uintptr_t)vaddr & ~MASK_256TB) {
622 : 44 : DEBUG_PRINT("invalid usermode virtual address %" PRIu64 "\n", vaddr);
623 : 44 : return -EINVAL;
624 : : }
625 : :
626 : : /* Only page-aligned registrations are supported */
627 [ + + + + : 5932045 : if (((uintptr_t)vaddr & MAP_PAGE_MASK) || (size & MAP_PAGE_MASK)) {
+ - + + ]
628 : 88 : DEBUG_PRINT("invalid %s parameters, vaddr=%" PRIu64 " len=%" PRIu64 "\n",
629 : : __func__, vaddr, size);
630 : 88 : return -EINVAL;
631 : : }
632 : :
633 [ + + ]: 5931957 : vfn_page = vaddr >> MAP_PAGE_SHIFT;
634 : :
635 [ + + ]: 20240381 : while (size) {
636 : 14308468 : map_1gb = mem_map_get_map_1gb(map, vfn_page);
637 [ + + ]: 14308468 : if (!map_1gb) {
638 : 44 : DEBUG_PRINT("could not get %p map\n", (void *)vaddr);
639 : 44 : return -ENOMEM;
640 : : }
641 : :
642 [ + + ]: 14308424 : idx_1gb = MAP_1GB_IDX(vfn_page);
643 [ + - + - ]: 14308424 : map_page = &map_1gb->map[idx_1gb];
644 [ + - + - ]: 14308424 : map_page->translation = translation;
645 : :
646 [ + - ]: 14308424 : size -= MAP_PAGE_SIZE;
647 : 14308424 : vfn_page++;
648 : : }
649 : :
650 : 5931913 : return 0;
651 : 260450 : }
652 : :
653 : : int
654 : 990031 : spdk_mem_map_clear_translation(struct spdk_mem_map *map, uint64_t vaddr, uint64_t size)
655 : : {
656 [ + - + - ]: 990031 : return spdk_mem_map_set_translation(map, vaddr, size, map->default_translation);
657 : : }
658 : :
659 : : inline uint64_t
660 : 142735560 : 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 [ + + - + ]: 142735560 : 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 [ + + ]: 142735560 : vfn_page = vaddr >> MAP_PAGE_SHIFT;
677 [ + + ]: 142735560 : idx_256tb = MAP_256TB_IDX(vfn_page);
678 [ + + ]: 142735560 : idx_1gb = MAP_1GB_IDX(vfn_page);
679 : :
680 [ + - + - : 142735560 : map_1gb = map->map_256tb.map[idx_256tb];
+ - + - ]
681 [ + + ]: 142735560 : if (spdk_unlikely(!map_1gb)) {
682 [ + - + - ]: 2549597 : return map->default_translation;
683 : : }
684 : :
685 [ + - + - ]: 140185963 : cur_size = MAP_PAGE_SIZE - MAP_PAGE_OFFSET(vaddr);
686 [ + - + - ]: 140185963 : map_page = &map_1gb->map[idx_1gb];
687 [ + + + + : 140186163 : if (size == NULL || map->ops.are_contiguous == NULL ||
+ + + - +
+ + + ]
688 [ + + + - : 67374087 : map_page->translation == map->default_translation) {
+ - + - ]
689 [ + + ]: 72811968 : if (size != NULL) {
690 [ + + + + : 440 : *size = spdk_min(*size, cur_size);
+ - + - ]
691 : 40 : }
692 [ + - + - ]: 72811968 : return map_page->translation;
693 : : }
694 : :
695 [ + - + - ]: 67373995 : orig_translation = map_page->translation;
696 : 67373995 : prev_translation = orig_translation;
697 [ + + + + ]: 67480875 : while (cur_size < *size) {
698 : 172727 : vfn_page++;
699 [ + + ]: 172727 : idx_256tb = MAP_256TB_IDX(vfn_page);
700 [ + + ]: 172727 : idx_1gb = MAP_1GB_IDX(vfn_page);
701 : :
702 [ + - + - : 172727 : map_1gb = map->map_256tb.map[idx_256tb];
+ - + - ]
703 [ + + ]: 172727 : if (spdk_unlikely(!map_1gb)) {
704 : 0 : break;
705 : : }
706 : :
707 [ + - + - ]: 172727 : map_page = &map_1gb->map[idx_1gb];
708 [ + + + - : 172727 : if (!map->ops.are_contiguous(prev_translation, map_page->translation)) {
+ - - + +
- + - + -
+ + ]
709 : 65847 : break;
710 : : }
711 : :
712 [ + - ]: 106880 : cur_size += MAP_PAGE_SIZE;
713 [ + - + - ]: 106880 : prev_translation = map_page->translation;
714 : : }
715 : :
716 [ + + + + : 67373995 : *size = spdk_min(*size, cur_size);
- + + - ]
717 : 67373995 : return orig_translation;
718 : 819176 : }
719 : :
720 : : static void
721 : 67872 : memory_hotplug_cb(enum rte_mem_event event_type,
722 : : const void *addr, size_t len, void *arg)
723 : : {
724 [ + + ]: 67872 : if (event_type == RTE_MEM_EVENT_ALLOC) {
725 : 34766 : spdk_mem_register((void *)addr, len);
726 : :
727 [ + + ]: 34766 : if (!spdk_env_dpdk_external_init()) {
728 : 34744 : 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 [ + + ]: 48 : while (len > 0) {
739 : : struct rte_memseg *seg;
740 : :
741 : 26 : seg = rte_mem_virt2memseg(addr, NULL);
742 [ + + # # ]: 26 : assert(seg != NULL);
743 [ + - + - : 26 : seg->flags |= RTE_MEMSEG_FLAG_DO_NOT_FREE;
+ - ]
744 [ + - + - ]: 26 : addr = (void *)((uintptr_t)addr + seg->hugepage_sz);
745 [ + - + - ]: 26 : len -= seg->hugepage_sz;
746 : : }
747 [ + + ]: 33107 : } else if (event_type == RTE_MEM_EVENT_FREE) {
748 : 33106 : spdk_mem_unregister((void *)addr, len);
749 : 445 : }
750 : 898 : }
751 : :
752 : : static int
753 : 2800 : memory_iter_cb(const struct rte_memseg_list *msl,
754 : : const struct rte_memseg *ms, size_t len, void *arg)
755 : : {
756 [ + - + - : 2800 : return spdk_mem_register(ms->addr, len);
+ - ]
757 : : }
758 : :
759 : : static bool g_mem_event_cb_registered = false;
760 : :
761 : : static int
762 : 2744 : mem_map_mem_event_callback_register(void)
763 : : {
764 : : int rc;
765 : :
766 : 2744 : rc = rte_mem_event_callback_register("spdk", memory_hotplug_cb, NULL);
767 [ - + ]: 2744 : if (rc != 0) {
768 : 0 : DEBUG_PRINT("memory event callback registration failed, rc = %d\n", rc);
769 [ # # # # ]: 0 : return -errno;
770 : : }
771 : :
772 : 2744 : g_mem_event_cb_registered = true;
773 : 2744 : return 0;
774 : 70 : }
775 : :
776 : : static void
777 : 2752 : mem_map_mem_event_callback_unregister(void)
778 : : {
779 [ + + + + ]: 2752 : if (g_mem_event_cb_registered) {
780 : 2601 : g_mem_event_cb_registered = false;
781 : 2601 : rte_mem_event_callback_unregister("spdk", NULL);
782 : 68 : }
783 : 2752 : }
784 : :
785 : : int
786 : 2819 : mem_map_init(bool legacy_mem)
787 : : {
788 : : int rc;
789 : :
790 [ + - ]: 2819 : g_legacy_mem = legacy_mem;
791 : :
792 : 2819 : g_mem_reg_map = spdk_mem_map_alloc(0, NULL, NULL);
793 [ + + ]: 2819 : if (g_mem_reg_map == NULL) {
794 : 0 : DEBUG_PRINT("memory registration map allocation failed\n");
795 : 0 : return -ENOMEM;
796 : : }
797 : :
798 [ + + + + ]: 2819 : if (!g_legacy_mem) {
799 : : /**
800 : : * To prevent DPDK complaining, only register the callback when
801 : : * we are not in legacy mem mode.
802 : : */
803 : 2744 : rc = mem_map_mem_event_callback_register();
804 [ + + ]: 2744 : if (rc != 0) {
805 : 0 : DEBUG_PRINT("memory event callback registration failed, rc = %d\n", rc);
806 : 0 : goto err_free_reg_map;
807 : : }
808 : 70 : }
809 : :
810 : : /*
811 : : * Walk all DPDK memory segments and register them
812 : : * with the main memory map
813 : : */
814 : 2819 : rc = rte_memseg_contig_walk(memory_iter_cb, NULL);
815 [ + + ]: 2819 : if (rc != 0) {
816 : 0 : DEBUG_PRINT("memory segments walking failed, rc = %d\n", rc);
817 : 0 : goto err_unregister_mem_cb;
818 : : }
819 : :
820 : 2819 : return 0;
821 : :
822 : 0 : err_unregister_mem_cb:
823 : 0 : mem_map_mem_event_callback_unregister();
824 : 0 : err_free_reg_map:
825 : 0 : spdk_mem_map_free(&g_mem_reg_map);
826 : 0 : return rc;
827 : 137 : }
828 : :
829 : : void
830 : 2752 : mem_map_fini(void)
831 : : {
832 : 2752 : mem_map_mem_event_callback_unregister();
833 : 2752 : spdk_mem_map_free(&g_mem_reg_map);
834 : 2752 : }
835 : :
836 : : bool
837 : 21549 : spdk_iommu_is_enabled(void)
838 : : {
839 : : #if VFIO_ENABLED
840 [ + + + + : 21549 : return g_vfio.enabled && !g_vfio.noiommu_enabled;
+ + + - ]
841 : : #else
842 : 0 : return false;
843 : : #endif
844 : : }
845 : :
846 : : struct spdk_vtophys_pci_device {
847 : : struct rte_pci_device *pci_device;
848 : : TAILQ_ENTRY(spdk_vtophys_pci_device) tailq;
849 : : };
850 : :
851 : : static pthread_mutex_t g_vtophys_pci_devices_mutex = PTHREAD_MUTEX_INITIALIZER;
852 : : static TAILQ_HEAD(, spdk_vtophys_pci_device) g_vtophys_pci_devices =
853 : : TAILQ_HEAD_INITIALIZER(g_vtophys_pci_devices);
854 : :
855 : : static struct spdk_mem_map *g_vtophys_map;
856 : : static struct spdk_mem_map *g_phys_ref_map;
857 : : static struct spdk_mem_map *g_numa_map;
858 : :
859 : : #if VFIO_ENABLED
860 : : static int
861 : 777 : _vfio_iommu_map_dma(uint64_t vaddr, uint64_t iova, uint64_t size)
862 : : {
863 : : struct spdk_vfio_dma_map *dma_map;
864 : : int ret;
865 : :
866 : 777 : dma_map = calloc(1, sizeof(*dma_map));
867 [ + + ]: 777 : if (dma_map == NULL) {
868 : 0 : return -ENOMEM;
869 : : }
870 : :
871 [ + - + - : 777 : dma_map->map.argsz = sizeof(dma_map->map);
+ - ]
872 [ - + + - : 777 : dma_map->map.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
- + + - +
- + - +
- ]
873 [ + - + - : 777 : dma_map->map.vaddr = vaddr;
+ - ]
874 [ + - + - : 777 : dma_map->map.iova = iova;
+ - ]
875 [ + - + - : 777 : dma_map->map.size = size;
+ - ]
876 : :
877 [ + + - + ]: 777 : if (g_vfio.device_ref == 0) {
878 : : /* VFIO requires at least one device (IOMMU group) to be added to
879 : : * a VFIO container before it is possible to perform any IOMMU
880 : : * operations on that container. This memory will be mapped once
881 : : * the first device (IOMMU group) is hotplugged.
882 : : *
883 : : * Since the vfio container is managed internally by DPDK, it is
884 : : * also possible that some device is already in that container, but
885 : : * it's not managed by SPDK - e.g. an NIC attached internally
886 : : * inside DPDK. We could map the memory straight away in such
887 : : * scenario, but there's no need to do it. DPDK devices clearly
888 : : * don't need our mappings and hence we defer the mapping
889 : : * unconditionally until the first SPDK-managed device is
890 : : * hotplugged.
891 : : */
892 : 658 : goto out_insert;
893 : : }
894 : :
895 [ # # # # : 119 : ret = ioctl(g_vfio.fd, VFIO_IOMMU_MAP_DMA, &dma_map->map);
# # # # #
# # # # #
# # ]
896 [ + - ]: 119 : if (ret) {
897 : : /* There are cases the vfio container doesn't have IOMMU group, it's safe for this case */
898 [ # # ]: 0 : SPDK_NOTICELOG("Cannot set up DMA mapping, error %d, ignored\n", errno);
899 : 0 : }
900 : :
901 : 119 : out_insert:
902 [ + - + - : 777 : TAILQ_INSERT_TAIL(&g_vfio.maps, dma_map, tailq);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
903 : 777 : return 0;
904 : 2 : }
905 : :
906 : :
907 : : static int
908 : 215 : vtophys_iommu_map_dma(uint64_t vaddr, uint64_t iova, uint64_t size)
909 : : {
910 : : uint64_t refcount;
911 : : int ret;
912 : :
913 : 215 : refcount = spdk_mem_map_translate(g_phys_ref_map, iova, NULL);
914 [ - + # # ]: 215 : assert(refcount < UINT64_MAX);
915 [ - + ]: 215 : if (refcount > 0) {
916 : 0 : spdk_mem_map_set_translation(g_phys_ref_map, iova, size, refcount + 1);
917 : 0 : return 0;
918 : : }
919 : :
920 [ - + ]: 215 : pthread_mutex_lock(&g_vfio.mutex);
921 : 215 : ret = _vfio_iommu_map_dma(vaddr, iova, size);
922 [ - + ]: 215 : pthread_mutex_unlock(&g_vfio.mutex);
923 [ - + ]: 215 : if (ret) {
924 : 0 : return ret;
925 : : }
926 : :
927 : 215 : spdk_mem_map_set_translation(g_phys_ref_map, iova, size, refcount + 1);
928 : 215 : return 0;
929 : 0 : }
930 : :
931 : : int
932 : 562 : vtophys_iommu_map_dma_bar(uint64_t vaddr, uint64_t iova, uint64_t size)
933 : : {
934 : : int ret;
935 : :
936 [ + + ]: 562 : pthread_mutex_lock(&g_vfio.mutex);
937 : 562 : ret = _vfio_iommu_map_dma(vaddr, iova, size);
938 [ + + ]: 562 : pthread_mutex_unlock(&g_vfio.mutex);
939 : :
940 : 562 : return ret;
941 : : }
942 : :
943 : : static int
944 : 753 : _vfio_iommu_unmap_dma(struct spdk_vfio_dma_map *dma_map)
945 : : {
946 : 753 : struct vfio_iommu_type1_dma_unmap unmap = {};
947 : : int ret;
948 : :
949 [ + + - + ]: 753 : if (g_vfio.device_ref == 0) {
950 : : /* Memory is not mapped anymore, just remove it's references */
951 : 103 : goto out_remove;
952 : : }
953 : :
954 : 650 : unmap.argsz = sizeof(unmap);
955 [ + - ]: 650 : unmap.flags = 0;
956 [ + - + - : 650 : unmap.iova = dma_map->map.iova;
+ - + - ]
957 [ + - + - : 650 : unmap.size = dma_map->map.size;
+ - + - ]
958 [ + - - + : 650 : ret = ioctl(g_vfio.fd, VFIO_IOMMU_UNMAP_DMA, &unmap);
+ - - + +
- - + +
- ]
959 [ + - ]: 650 : if (ret) {
960 [ # # ]: 0 : SPDK_NOTICELOG("Cannot clear DMA mapping, error %d, ignored\n", errno);
961 : 0 : }
962 : :
963 : 697 : out_remove:
964 [ + + + - : 753 : TAILQ_REMOVE(&g_vfio.maps, dma_map, tailq);
+ - + - #
# # # # #
# # # # #
# # # # #
# # + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
965 : 753 : free(dma_map);
966 : 753 : return 0;
967 : : }
968 : :
969 : : static int
970 : 215 : vtophys_iommu_unmap_dma(uint64_t iova, uint64_t size)
971 : : {
972 : : struct spdk_vfio_dma_map *dma_map;
973 : : uint64_t refcount;
974 : : int ret;
975 : :
976 [ - + ]: 215 : pthread_mutex_lock(&g_vfio.mutex);
977 [ + - # # : 510 : TAILQ_FOREACH(dma_map, &g_vfio.maps, tailq) {
# # # # #
# # # ]
978 [ + + # # : 510 : if (dma_map->map.iova == iova) {
# # # # ]
979 : 215 : break;
980 : : }
981 : 0 : }
982 : :
983 [ - + ]: 215 : if (dma_map == NULL) {
984 : 0 : DEBUG_PRINT("Cannot clear DMA mapping for IOVA %"PRIx64" - it's not mapped\n", iova);
985 [ # # ]: 0 : pthread_mutex_unlock(&g_vfio.mutex);
986 : 0 : return -ENXIO;
987 : : }
988 : :
989 : 215 : refcount = spdk_mem_map_translate(g_phys_ref_map, iova, NULL);
990 [ - + # # ]: 215 : assert(refcount < UINT64_MAX);
991 [ + - ]: 215 : if (refcount > 0) {
992 : 215 : spdk_mem_map_set_translation(g_phys_ref_map, iova, size, refcount - 1);
993 : 0 : }
994 : :
995 : : /* We still have outstanding references, don't clear it. */
996 [ - + ]: 215 : if (refcount > 1) {
997 [ # # ]: 0 : pthread_mutex_unlock(&g_vfio.mutex);
998 : 0 : return 0;
999 : : }
1000 : :
1001 : : /** don't support partial or multiple-page unmap for now */
1002 [ - + # # : 215 : assert(dma_map->map.size == size);
# # # # #
# ]
1003 : :
1004 : 215 : ret = _vfio_iommu_unmap_dma(dma_map);
1005 [ - + ]: 215 : pthread_mutex_unlock(&g_vfio.mutex);
1006 : :
1007 : 215 : return ret;
1008 : 0 : }
1009 : :
1010 : : int
1011 : 538 : vtophys_iommu_unmap_dma_bar(uint64_t vaddr)
1012 : : {
1013 : : struct spdk_vfio_dma_map *dma_map;
1014 : : int ret;
1015 : :
1016 [ + + ]: 538 : pthread_mutex_lock(&g_vfio.mutex);
1017 [ + - + - : 554 : TAILQ_FOREACH(dma_map, &g_vfio.maps, tailq) {
- + # # #
# # # ]
1018 [ + + + - : 554 : if (dma_map->map.vaddr == vaddr) {
+ - - + ]
1019 : 538 : break;
1020 : : }
1021 : 0 : }
1022 : :
1023 [ + + ]: 538 : if (dma_map == NULL) {
1024 : 0 : DEBUG_PRINT("Cannot clear DMA mapping for address %"PRIx64" - it's not mapped\n", vaddr);
1025 [ # # ]: 0 : pthread_mutex_unlock(&g_vfio.mutex);
1026 : 0 : return -ENXIO;
1027 : : }
1028 : :
1029 : 538 : ret = _vfio_iommu_unmap_dma(dma_map);
1030 [ + + ]: 538 : pthread_mutex_unlock(&g_vfio.mutex);
1031 : 538 : return ret;
1032 : 2 : }
1033 : : #endif
1034 : :
1035 : : static uint64_t
1036 : 1097120 : vtophys_get_paddr_memseg(uint64_t vaddr)
1037 : : {
1038 : : uintptr_t paddr;
1039 : : struct rte_memseg *seg;
1040 : :
1041 : 1097120 : seg = rte_mem_virt2memseg((void *)(uintptr_t)vaddr, NULL);
1042 [ + + ]: 1097120 : if (seg != NULL) {
1043 [ + - + - ]: 1096008 : paddr = seg->iova;
1044 [ + + ]: 1096008 : if (paddr == RTE_BAD_IOVA) {
1045 : 0 : return SPDK_VTOPHYS_ERROR;
1046 : : }
1047 [ + - + - : 1096008 : paddr += (vaddr - (uintptr_t)seg->addr);
+ - ]
1048 : 1096008 : return paddr;
1049 : : }
1050 : :
1051 : 1112 : return SPDK_VTOPHYS_ERROR;
1052 : 86806 : }
1053 : :
1054 : : /* Try to get the paddr from /proc/self/pagemap */
1055 : : static uint64_t
1056 : 16571 : vtophys_get_paddr_pagemap(uint64_t vaddr)
1057 : : {
1058 : : uintptr_t paddr;
1059 : :
1060 : : /* Silence static analyzers */
1061 [ - + # # ]: 16571 : assert(vaddr != 0);
1062 : 16571 : paddr = rte_mem_virt2iova((void *)vaddr);
1063 [ + + ]: 16571 : if (paddr == RTE_BAD_IOVA) {
1064 : : /*
1065 : : * The vaddr may be valid but doesn't have a backing page
1066 : : * assigned yet. Touch the page to ensure a backing page
1067 : : * gets assigned, then try to translate again.
1068 : : */
1069 : 1174 : rte_atomic64_read((rte_atomic64_t *)vaddr);
1070 : 1174 : paddr = rte_mem_virt2iova((void *)vaddr);
1071 : 0 : }
1072 [ + + ]: 16571 : if (paddr == RTE_BAD_IOVA) {
1073 : : /* Unable to get to the physical address. */
1074 : 2 : return SPDK_VTOPHYS_ERROR;
1075 : : }
1076 : :
1077 : 16569 : return paddr;
1078 : 0 : }
1079 : :
1080 : : static uint64_t
1081 : 3172 : pci_device_vtophys(struct rte_pci_device *dev, uint64_t vaddr, size_t len)
1082 : : {
1083 : : struct rte_mem_resource *res;
1084 : : uint64_t paddr;
1085 : : unsigned r;
1086 : :
1087 [ + + ]: 18364 : for (r = 0; r < PCI_MAX_RESOURCE; r++) {
1088 : 16856 : res = dpdk_pci_device_get_mem_resource(dev, r);
1089 : :
1090 [ + + + + : 16856 : if (res->phys_addr == 0 || vaddr < (uint64_t)res->addr ||
# # # # #
# # # #
# ]
1091 [ + + # # : 6508 : (vaddr + len) >= (uint64_t)res->addr + res->len) {
# # # # ]
1092 : 15192 : continue;
1093 : : }
1094 : :
1095 : : #if VFIO_ENABLED
1096 [ - + - - ]: 1664 : if (spdk_iommu_is_enabled() && rte_eal_iova_mode() == RTE_IOVA_VA) {
1097 : : /*
1098 : : * The IOMMU is on and we're using IOVA == VA. The BAR was
1099 : : * automatically registered when it was mapped, so just return
1100 : : * the virtual address here.
1101 : : */
1102 : 0 : return vaddr;
1103 : : }
1104 : : #endif
1105 [ # # # # : 1664 : paddr = res->phys_addr + (vaddr - (uint64_t)res->addr);
# # # # ]
1106 : 1664 : return paddr;
1107 : : }
1108 : :
1109 : 1508 : return SPDK_VTOPHYS_ERROR;
1110 : 0 : }
1111 : :
1112 : : /* Try to get the paddr from pci devices */
1113 : : static uint64_t
1114 : 2684 : vtophys_get_paddr_pci(uint64_t vaddr, size_t len)
1115 : : {
1116 : : struct spdk_vtophys_pci_device *vtophys_dev;
1117 : : uintptr_t paddr;
1118 : : struct rte_pci_device *dev;
1119 : :
1120 [ - + ]: 2684 : pthread_mutex_lock(&g_vtophys_pci_devices_mutex);
1121 [ + + # # : 4192 : TAILQ_FOREACH(vtophys_dev, &g_vtophys_pci_devices, tailq) {
# # # # ]
1122 [ # # # # ]: 3172 : dev = vtophys_dev->pci_device;
1123 : 3172 : paddr = pci_device_vtophys(dev, vaddr, len);
1124 [ + + ]: 3172 : if (paddr != SPDK_VTOPHYS_ERROR) {
1125 [ # # ]: 1664 : pthread_mutex_unlock(&g_vtophys_pci_devices_mutex);
1126 : 1664 : return paddr;
1127 : : }
1128 : 0 : }
1129 [ - + ]: 1020 : pthread_mutex_unlock(&g_vtophys_pci_devices_mutex);
1130 : :
1131 : 1020 : return SPDK_VTOPHYS_ERROR;
1132 : 0 : }
1133 : :
1134 : : static int
1135 : 76064 : vtophys_notify(void *cb_ctx, struct spdk_mem_map *map,
1136 : : enum spdk_mem_map_notify_action action,
1137 : : void *vaddr, size_t len)
1138 : : {
1139 : 76064 : int rc = 0;
1140 : : uint64_t paddr;
1141 : :
1142 [ + + - + ]: 76064 : if ((uintptr_t)vaddr & ~MASK_256TB) {
1143 : 0 : DEBUG_PRINT("invalid usermode virtual address %p\n", vaddr);
1144 : 0 : return -EINVAL;
1145 : : }
1146 : :
1147 [ + - + + : 76064 : if (((uintptr_t)vaddr & MASK_2MB) || (len & MASK_2MB)) {
+ - - + ]
1148 : 0 : DEBUG_PRINT("invalid parameters, vaddr=%p len=%ju\n",
1149 : : vaddr, len);
1150 : 0 : return -EINVAL;
1151 : : }
1152 : :
1153 : : /* Get the physical address from the DPDK memsegs */
1154 : 76064 : paddr = vtophys_get_paddr_memseg((uint64_t)vaddr);
1155 : :
1156 [ + + + ]: 76064 : switch (action) {
1157 : 37528 : case SPDK_MEM_MAP_NOTIFY_REGISTER:
1158 [ + + ]: 38114 : if (paddr == SPDK_VTOPHYS_ERROR) {
1159 : : /* This is not an address that DPDK is managing. */
1160 : :
1161 : : /* Check if this is a PCI BAR. They need special handling */
1162 : 556 : paddr = vtophys_get_paddr_pci((uint64_t)vaddr, len);
1163 [ + + ]: 556 : if (paddr != SPDK_VTOPHYS_ERROR) {
1164 : : /* Get paddr for each 2MB chunk in this address range */
1165 [ + + ]: 832 : while (len > 0) {
1166 [ # # ]: 786 : paddr = vtophys_get_paddr_pci((uint64_t)vaddr, VALUE_2MB);
1167 [ - + ]: 786 : if (paddr == SPDK_VTOPHYS_ERROR) {
1168 : 0 : DEBUG_PRINT("could not get phys addr for %p\n", vaddr);
1169 : 0 : return -EFAULT;
1170 : : }
1171 : :
1172 [ # # ]: 786 : rc = spdk_mem_map_set_translation(map, (uint64_t)vaddr, VALUE_2MB, paddr);
1173 [ - + ]: 786 : if (rc != 0) {
1174 : 0 : return rc;
1175 : : }
1176 : :
1177 [ # # ]: 786 : vaddr += VALUE_2MB;
1178 [ # # ]: 786 : len -= VALUE_2MB;
1179 : : }
1180 : :
1181 : 46 : return 0;
1182 : : }
1183 : :
1184 : : #if VFIO_ENABLED
1185 : : enum rte_iova_mode iova_mode;
1186 : :
1187 : 510 : iova_mode = rte_eal_iova_mode();
1188 : :
1189 [ + + + - ]: 725 : if (spdk_iommu_is_enabled() && iova_mode == RTE_IOVA_VA) {
1190 : : /* We'll use the virtual address as the iova to match DPDK. */
1191 : 215 : paddr = (uint64_t)vaddr;
1192 : 215 : rc = vtophys_iommu_map_dma((uint64_t)vaddr, paddr, len);
1193 [ - + ]: 215 : if (rc) {
1194 : 0 : return -EFAULT;
1195 : : }
1196 [ + + ]: 41326 : while (len > 0) {
1197 [ # # ]: 41111 : rc = spdk_mem_map_set_translation(map, (uint64_t)vaddr, VALUE_2MB, paddr);
1198 [ - + ]: 41111 : if (rc != 0) {
1199 : 0 : return rc;
1200 : : }
1201 [ # # ]: 41111 : vaddr += VALUE_2MB;
1202 [ # # ]: 41111 : paddr += VALUE_2MB;
1203 [ # # ]: 41111 : len -= VALUE_2MB;
1204 : : }
1205 : 0 : } else
1206 : : #endif
1207 : : {
1208 : : /* Get the physical address from /proc/self/pagemap. */
1209 : 295 : paddr = vtophys_get_paddr_pagemap((uint64_t)vaddr);
1210 [ + + ]: 295 : if (paddr == SPDK_VTOPHYS_ERROR) {
1211 : 2 : DEBUG_PRINT("could not get phys addr for %p\n", vaddr);
1212 : 2 : return -EFAULT;
1213 : : }
1214 : :
1215 : : /* Get paddr for each 2MB chunk in this address range */
1216 [ + + ]: 16569 : while (len > 0) {
1217 : : /* Get the physical address from /proc/self/pagemap. */
1218 : 16276 : paddr = vtophys_get_paddr_pagemap((uint64_t)vaddr);
1219 : :
1220 [ - + ]: 16276 : if (paddr == SPDK_VTOPHYS_ERROR) {
1221 : 0 : DEBUG_PRINT("could not get phys addr for %p\n", vaddr);
1222 : 0 : return -EFAULT;
1223 : : }
1224 : :
1225 [ - + # # ]: 16276 : if (paddr & MASK_2MB) {
1226 : 0 : DEBUG_PRINT("invalid paddr 0x%" PRIx64 " - must be 2MB aligned\n", paddr);
1227 : 0 : return -EINVAL;
1228 : : }
1229 : : #if VFIO_ENABLED
1230 : : /* If the IOMMU is on, but DPDK is using iova-mode=pa, we want to register this memory
1231 : : * with the IOMMU using the physical address to match. */
1232 [ - + ]: 16276 : if (spdk_iommu_is_enabled()) {
1233 [ # # ]: 0 : rc = vtophys_iommu_map_dma((uint64_t)vaddr, paddr, VALUE_2MB);
1234 [ # # ]: 0 : if (rc) {
1235 : 0 : DEBUG_PRINT("Unable to assign vaddr %p to paddr 0x%" PRIx64 "\n", vaddr, paddr);
1236 : 0 : return -EFAULT;
1237 : : }
1238 : 0 : }
1239 : : #endif
1240 : :
1241 [ # # ]: 16276 : rc = spdk_mem_map_set_translation(map, (uint64_t)vaddr, VALUE_2MB, paddr);
1242 [ - + ]: 16276 : if (rc != 0) {
1243 : 0 : return rc;
1244 : : }
1245 : :
1246 [ # # ]: 16276 : vaddr += VALUE_2MB;
1247 [ # # ]: 16276 : len -= VALUE_2MB;
1248 : : }
1249 : : }
1250 : 0 : } else {
1251 : : /* This is an address managed by DPDK. Just setup the translations. */
1252 [ + + ]: 1058614 : while (len > 0) {
1253 : 1021056 : paddr = vtophys_get_paddr_memseg((uint64_t)vaddr);
1254 [ + + ]: 1021056 : if (paddr == SPDK_VTOPHYS_ERROR) {
1255 : 0 : DEBUG_PRINT("could not get phys addr for %p\n", vaddr);
1256 : 0 : return -EFAULT;
1257 : : }
1258 : :
1259 [ + - ]: 1021056 : rc = spdk_mem_map_set_translation(map, (uint64_t)vaddr, VALUE_2MB, paddr);
1260 [ + + ]: 1021056 : if (rc != 0) {
1261 : 0 : return rc;
1262 : : }
1263 : :
1264 [ + - ]: 1021056 : vaddr += VALUE_2MB;
1265 [ + - ]: 1021056 : len -= VALUE_2MB;
1266 : : }
1267 : : }
1268 : :
1269 : 38066 : break;
1270 : 37375 : case SPDK_MEM_MAP_NOTIFY_UNREGISTER:
1271 : : #if VFIO_ENABLED
1272 [ + + ]: 37892 : if (paddr == SPDK_VTOPHYS_ERROR) {
1273 : : /*
1274 : : * This is not an address that DPDK is managing.
1275 : : */
1276 : :
1277 : : /* Check if this is a PCI BAR. They need special handling */
1278 : 556 : paddr = vtophys_get_paddr_pci((uint64_t)vaddr, len);
1279 [ + + ]: 556 : if (paddr != SPDK_VTOPHYS_ERROR) {
1280 : : /* Get paddr for each 2MB chunk in this address range */
1281 [ + + ]: 832 : while (len > 0) {
1282 [ # # ]: 786 : paddr = vtophys_get_paddr_pci((uint64_t)vaddr, VALUE_2MB);
1283 [ - + ]: 786 : if (paddr == SPDK_VTOPHYS_ERROR) {
1284 : 0 : DEBUG_PRINT("could not get phys addr for %p\n", vaddr);
1285 : 0 : return -EFAULT;
1286 : : }
1287 : :
1288 [ # # ]: 786 : rc = spdk_mem_map_clear_translation(map, (uint64_t)vaddr, VALUE_2MB);
1289 [ - + ]: 786 : if (rc != 0) {
1290 : 0 : return rc;
1291 : : }
1292 : :
1293 [ # # ]: 786 : vaddr += VALUE_2MB;
1294 [ # # ]: 786 : len -= VALUE_2MB;
1295 : : }
1296 : :
1297 : 46 : return 0;
1298 : : }
1299 : :
1300 : : /* If vfio is enabled,
1301 : : * we need to unmap the range from the IOMMU
1302 : : */
1303 [ + + ]: 510 : if (spdk_iommu_is_enabled()) {
1304 : 215 : uint64_t buffer_len = len;
1305 : 215 : uint8_t *va = vaddr;
1306 : : enum rte_iova_mode iova_mode;
1307 : :
1308 : 215 : iova_mode = rte_eal_iova_mode();
1309 : : /*
1310 : : * In virtual address mode, the region is contiguous and can be done in
1311 : : * one unmap.
1312 : : */
1313 [ + - ]: 215 : if (iova_mode == RTE_IOVA_VA) {
1314 : 215 : paddr = spdk_mem_map_translate(map, (uint64_t)va, &buffer_len);
1315 [ + - - + ]: 215 : if (buffer_len != len || paddr != (uintptr_t)va) {
1316 : 0 : DEBUG_PRINT("Unmapping %p with length %lu failed because "
1317 : : "translation had address 0x%" PRIx64 " and length %lu\n",
1318 : : va, len, paddr, buffer_len);
1319 : 0 : return -EINVAL;
1320 : : }
1321 : 215 : rc = vtophys_iommu_unmap_dma(paddr, len);
1322 [ - + ]: 215 : if (rc) {
1323 : 0 : DEBUG_PRINT("Failed to iommu unmap paddr 0x%" PRIx64 "\n", paddr);
1324 : 0 : return -EFAULT;
1325 : : }
1326 [ # # ]: 0 : } else if (iova_mode == RTE_IOVA_PA) {
1327 : : /* Get paddr for each 2MB chunk in this address range */
1328 [ # # ]: 0 : while (buffer_len > 0) {
1329 : 0 : paddr = spdk_mem_map_translate(map, (uint64_t)va, NULL);
1330 : :
1331 [ # # # # : 0 : if (paddr == SPDK_VTOPHYS_ERROR || buffer_len < VALUE_2MB) {
# # ]
1332 : 0 : DEBUG_PRINT("could not get phys addr for %p\n", va);
1333 : 0 : return -EFAULT;
1334 : : }
1335 : :
1336 [ # # ]: 0 : rc = vtophys_iommu_unmap_dma(paddr, VALUE_2MB);
1337 [ # # ]: 0 : if (rc) {
1338 : 0 : DEBUG_PRINT("Failed to iommu unmap paddr 0x%" PRIx64 "\n", paddr);
1339 : 0 : return -EFAULT;
1340 : : }
1341 : :
1342 [ # # # # ]: 0 : va += VALUE_2MB;
1343 [ # # ]: 0 : buffer_len -= VALUE_2MB;
1344 : : }
1345 : 0 : }
1346 : 0 : }
1347 : 0 : }
1348 : : #endif
1349 [ + + ]: 984034 : while (len > 0) {
1350 [ + - ]: 946130 : rc = spdk_mem_map_clear_translation(map, (uint64_t)vaddr, VALUE_2MB);
1351 [ + + ]: 946130 : if (rc != 0) {
1352 : 0 : return rc;
1353 : : }
1354 : :
1355 [ + - ]: 946130 : vaddr += VALUE_2MB;
1356 [ + - ]: 946130 : len -= VALUE_2MB;
1357 : : }
1358 : :
1359 : 37904 : break;
1360 : 0 : default:
1361 [ # # ]: 0 : SPDK_UNREACHABLE();
1362 : : }
1363 : :
1364 : 75970 : return rc;
1365 : 1161 : }
1366 : :
1367 : : static int
1368 : 76080 : numa_notify(void *cb_ctx, struct spdk_mem_map *map,
1369 : : enum spdk_mem_map_notify_action action,
1370 : : void *vaddr, size_t len)
1371 : : {
1372 : : struct rte_memseg *seg;
1373 : :
1374 : : /* We always return 0 from here, even if we aren't able to get a
1375 : : * memseg for the address. This can happen in non-DPDK memory
1376 : : * registration paths, for example vhost or vfio-user. That is OK,
1377 : : * spdk_mem_get_numa_id() just returns SPDK_ENV_NUMA_ID_ANY for
1378 : : * that kind of memory. If we return an error here, the
1379 : : * spdk_mem_register() from vhost or vfio-user would fail which is
1380 : : * not what we want.
1381 : : */
1382 : 76080 : seg = rte_mem_virt2memseg(vaddr, NULL);
1383 [ + + ]: 76080 : if (seg == NULL) {
1384 : 1112 : return 0;
1385 : : }
1386 : :
1387 [ + + + ]: 74968 : switch (action) {
1388 : 36980 : case SPDK_MEM_MAP_NOTIFY_REGISTER:
1389 [ - + - + ]: 37566 : spdk_mem_map_set_translation(map, (uint64_t)vaddr, len, seg->socket_id);
1390 : 37566 : break;
1391 : 36827 : case SPDK_MEM_MAP_NOTIFY_UNREGISTER:
1392 : 37402 : spdk_mem_map_clear_translation(map, (uint64_t)vaddr, len);
1393 : 37402 : break;
1394 : 0 : default:
1395 : 0 : break;
1396 : : }
1397 : :
1398 : 74968 : return 0;
1399 : 1161 : }
1400 : :
1401 : : static int
1402 : 168787 : vtophys_check_contiguous_entries(uint64_t paddr1, uint64_t paddr2)
1403 : : {
1404 : : /* This function is always called with paddrs for two subsequent
1405 : : * 2MB chunks in virtual address space, so those chunks will be only
1406 : : * physically contiguous if the physical addresses are 2MB apart
1407 : : * from each other as well.
1408 : : */
1409 [ + - ]: 168787 : return (paddr2 - paddr1 == VALUE_2MB);
1410 : : }
1411 : :
1412 : : #if VFIO_ENABLED
1413 : :
1414 : : static bool
1415 : 2708 : vfio_enabled(void)
1416 : : {
1417 : 2708 : return rte_vfio_is_enabled("vfio_pci");
1418 : : }
1419 : :
1420 : : /* Check if IOMMU is enabled on the system */
1421 : : static bool
1422 : 1065 : has_iommu_groups(void)
1423 : : {
1424 : 1065 : int count = 0;
1425 [ + + ]: 1065 : DIR *dir = opendir("/sys/kernel/iommu_groups");
1426 : :
1427 [ + + ]: 1065 : if (dir == NULL) {
1428 : 0 : return false;
1429 : : }
1430 : :
1431 [ + + + + : 4072 : while (count < 3 && readdir(dir) != NULL) {
+ + ]
1432 [ + - ]: 3007 : count++;
1433 : : }
1434 : :
1435 [ + + ]: 1065 : closedir(dir);
1436 : : /* there will always be ./ and ../ entries */
1437 : 1065 : return count > 2;
1438 : 66 : }
1439 : :
1440 : : static bool
1441 : 1065 : vfio_noiommu_enabled(void)
1442 : : {
1443 : 1065 : return rte_vfio_noiommu_is_enabled();
1444 : : }
1445 : :
1446 : : static void
1447 : 2708 : vtophys_iommu_init(void)
1448 : : {
1449 : 1032 : char proc_fd_path[PATH_MAX + 1];
1450 : 1032 : char link_path[PATH_MAX + 1];
1451 : 2708 : const char vfio_path[] = "/dev/vfio/vfio";
1452 : : DIR *dir;
1453 : : struct dirent *d;
1454 : :
1455 [ + + ]: 2708 : if (!vfio_enabled()) {
1456 : 1643 : return;
1457 : : }
1458 : :
1459 [ - + ]: 1065 : if (vfio_noiommu_enabled()) {
1460 : 0 : g_vfio.noiommu_enabled = true;
1461 [ + + ]: 1065 : } else if (!has_iommu_groups()) {
1462 : 188 : return;
1463 : : }
1464 : :
1465 [ + - ]: 877 : dir = opendir("/proc/self/fd");
1466 [ + + ]: 877 : if (!dir) {
1467 [ # # ]: 0 : DEBUG_PRINT("Failed to open /proc/self/fd (%d)\n", errno);
1468 : 0 : return;
1469 : : }
1470 : :
1471 [ + + + + ]: 10087 : while ((d = readdir(dir)) != NULL) {
1472 [ + + + - : 10087 : if (d->d_type != DT_LNK) {
+ + ]
1473 : 1754 : continue;
1474 : : }
1475 : :
1476 [ + - ]: 8333 : snprintf(proc_fd_path, sizeof(proc_fd_path), "/proc/self/fd/%s", d->d_name);
1477 [ + + + - : 8333 : if (readlink(proc_fd_path, link_path, sizeof(link_path)) != (sizeof(vfio_path) - 1)) {
+ + ]
1478 : 7456 : continue;
1479 : : }
1480 : :
1481 [ + - + - : 877 : if (memcmp(link_path, vfio_path, sizeof(vfio_path) - 1) == 0) {
- + ]
1482 [ - + ]: 877 : sscanf(d->d_name, "%d", &g_vfio.fd);
1483 : 877 : break;
1484 : : }
1485 : : }
1486 : :
1487 [ + + ]: 877 : closedir(dir);
1488 : :
1489 [ + + ]: 877 : if (g_vfio.fd < 0) {
1490 : 0 : DEBUG_PRINT("Failed to discover DPDK VFIO container fd.\n");
1491 : 0 : return;
1492 : : }
1493 : :
1494 [ + - ]: 877 : g_vfio.enabled = true;
1495 : :
1496 : 877 : return;
1497 : 66 : }
1498 : :
1499 : : #endif
1500 : :
1501 : : void
1502 : 1856 : vtophys_pci_device_added(struct rte_pci_device *pci_device)
1503 : : {
1504 : : struct spdk_vtophys_pci_device *vtophys_dev;
1505 : :
1506 [ + - ]: 1856 : pthread_mutex_lock(&g_vtophys_pci_devices_mutex);
1507 : :
1508 : 1856 : vtophys_dev = calloc(1, sizeof(*vtophys_dev));
1509 [ + + ]: 1856 : if (vtophys_dev) {
1510 [ + - + - ]: 1856 : vtophys_dev->pci_device = pci_device;
1511 [ + - + - : 1856 : TAILQ_INSERT_TAIL(&g_vtophys_pci_devices, vtophys_dev, tailq);
+ - + - +
- + - + -
+ - + - +
- + - +
- ]
1512 : 30 : } else {
1513 : 0 : DEBUG_PRINT("Memory allocation error\n");
1514 : : }
1515 [ + - ]: 1856 : pthread_mutex_unlock(&g_vtophys_pci_devices_mutex);
1516 : :
1517 : : #if VFIO_ENABLED
1518 : : struct spdk_vfio_dma_map *dma_map;
1519 : : int ret;
1520 : :
1521 [ + + + + : 1828 : if (!g_vfio.enabled) {
+ - ]
1522 : 783 : return;
1523 : : }
1524 : :
1525 [ + - ]: 1045 : pthread_mutex_lock(&g_vfio.mutex);
1526 : 1045 : g_vfio.device_ref++;
1527 [ + + - + ]: 1045 : if (g_vfio.device_ref > 1) {
1528 [ # # ]: 886 : pthread_mutex_unlock(&g_vfio.mutex);
1529 : 886 : return;
1530 : : }
1531 : :
1532 : : /* This is the first SPDK device using DPDK vfio. This means that the first
1533 : : * IOMMU group might have been just been added to the DPDK vfio container.
1534 : : * From this point it is certain that the memory can be mapped now.
1535 : : */
1536 [ + + + - : 705 : TAILQ_FOREACH(dma_map, &g_vfio.maps, tailq) {
+ + + - +
- + - ]
1537 [ + - - + : 555 : ret = ioctl(g_vfio.fd, VFIO_IOMMU_MAP_DMA, &dma_map->map);
+ - - + +
- - + + -
+ - ]
1538 [ + + ]: 555 : if (ret) {
1539 [ # # ]: 9 : DEBUG_PRINT("Cannot update DMA mapping, error %d\n", errno);
1540 : 9 : break;
1541 : : }
1542 : 2 : }
1543 [ + - ]: 159 : pthread_mutex_unlock(&g_vfio.mutex);
1544 : : #endif
1545 : 30 : }
1546 : :
1547 : : void
1548 : 1127 : vtophys_pci_device_removed(struct rte_pci_device *pci_device)
1549 : : {
1550 : : struct spdk_vtophys_pci_device *vtophys_dev;
1551 : :
1552 [ + - ]: 1127 : pthread_mutex_lock(&g_vtophys_pci_devices_mutex);
1553 [ + + # # : 1249 : TAILQ_FOREACH(vtophys_dev, &g_vtophys_pci_devices, tailq) {
# # # # ]
1554 [ + + + - : 1249 : if (vtophys_dev->pci_device == pci_device) {
- + ]
1555 [ + + + - : 1127 : TAILQ_REMOVE(&g_vtophys_pci_devices, vtophys_dev, tailq);
+ - - + #
# # # # #
# # # # #
# # # # #
# # + - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
1556 : 1127 : free(vtophys_dev);
1557 : 1127 : break;
1558 : : }
1559 : 0 : }
1560 [ + - ]: 1127 : pthread_mutex_unlock(&g_vtophys_pci_devices_mutex);
1561 : :
1562 : : #if VFIO_ENABLED
1563 : : struct spdk_vfio_dma_map *dma_map;
1564 : : int ret;
1565 : :
1566 [ + + + + : 1100 : if (!g_vfio.enabled) {
+ - ]
1567 : 570 : return;
1568 : : }
1569 : :
1570 [ + - ]: 530 : pthread_mutex_lock(&g_vfio.mutex);
1571 [ + + + - : 530 : assert(g_vfio.device_ref > 0);
# # ]
1572 : 530 : g_vfio.device_ref--;
1573 [ + + - + ]: 530 : if (g_vfio.device_ref > 0) {
1574 [ # # ]: 408 : pthread_mutex_unlock(&g_vfio.mutex);
1575 : 408 : return;
1576 : : }
1577 : :
1578 : : /* This is the last SPDK device using DPDK vfio. If DPDK doesn't have
1579 : : * any additional devices using it's vfio container, all the mappings
1580 : : * will be automatically removed by the Linux vfio driver. We unmap
1581 : : * the memory manually to be able to easily re-map it later regardless
1582 : : * of other, external factors.
1583 : : */
1584 [ + + + - : 122 : TAILQ_FOREACH(dma_map, &g_vfio.maps, tailq) {
+ - # # #
# # # ]
1585 : 11 : struct vfio_iommu_type1_dma_unmap unmap = {};
1586 : 11 : unmap.argsz = sizeof(unmap);
1587 [ # # ]: 11 : unmap.flags = 0;
1588 [ # # # # : 11 : unmap.iova = dma_map->map.iova;
# # # # ]
1589 [ # # # # : 11 : unmap.size = dma_map->map.size;
# # # # ]
1590 [ # # # # : 11 : ret = ioctl(g_vfio.fd, VFIO_IOMMU_UNMAP_DMA, &unmap);
# # # # #
# # # #
# ]
1591 [ + - ]: 11 : if (ret) {
1592 [ # # ]: 11 : DEBUG_PRINT("Cannot unmap DMA memory, error %d\n", errno);
1593 : 11 : break;
1594 : : }
1595 : 0 : }
1596 [ + - ]: 122 : pthread_mutex_unlock(&g_vfio.mutex);
1597 : : #endif
1598 : 29 : }
1599 : :
1600 : : int
1601 : 2775 : vtophys_init(void)
1602 : : {
1603 : 2775 : const struct spdk_mem_map_ops vtophys_map_ops = {
1604 : : .notify_cb = vtophys_notify,
1605 : : .are_contiguous = vtophys_check_contiguous_entries,
1606 : : };
1607 : :
1608 : 2775 : const struct spdk_mem_map_ops phys_ref_map_ops = {
1609 : : .notify_cb = NULL,
1610 : : .are_contiguous = NULL,
1611 : : };
1612 : :
1613 : 2775 : const struct spdk_mem_map_ops numa_map_ops = {
1614 : : .notify_cb = numa_notify,
1615 : : .are_contiguous = NULL,
1616 : : };
1617 : :
1618 : : #if VFIO_ENABLED
1619 : 2708 : vtophys_iommu_init();
1620 : : #endif
1621 : :
1622 : 2775 : g_phys_ref_map = spdk_mem_map_alloc(0, &phys_ref_map_ops, NULL);
1623 [ + + ]: 2775 : if (g_phys_ref_map == NULL) {
1624 : 0 : DEBUG_PRINT("phys_ref map allocation failed.\n");
1625 : 0 : return -ENOMEM;
1626 : : }
1627 : :
1628 : 2775 : g_numa_map = spdk_mem_map_alloc(SPDK_ENV_NUMA_ID_ANY, &numa_map_ops, NULL);
1629 [ + + ]: 2775 : if (g_numa_map == NULL) {
1630 : 0 : DEBUG_PRINT("numa map allocation failed.\n");
1631 : 0 : spdk_mem_map_free(&g_phys_ref_map);
1632 : 0 : return -ENOMEM;
1633 : : }
1634 : :
1635 [ + + + + ]: 2775 : if (g_huge_pages) {
1636 : 2767 : g_vtophys_map = spdk_mem_map_alloc(SPDK_VTOPHYS_ERROR, &vtophys_map_ops, NULL);
1637 [ + + ]: 2767 : if (g_vtophys_map == NULL) {
1638 : 0 : DEBUG_PRINT("vtophys map allocation failed\n");
1639 : 0 : spdk_mem_map_free(&g_numa_map);
1640 : 0 : spdk_mem_map_free(&g_phys_ref_map);
1641 : 0 : return -ENOMEM;
1642 : : }
1643 : 133 : }
1644 : 2775 : return 0;
1645 : 133 : }
1646 : :
1647 : : void
1648 : 2708 : vtophys_fini(void)
1649 : : {
1650 : 2708 : spdk_mem_map_free(&g_vtophys_map);
1651 : 2708 : spdk_mem_map_free(&g_numa_map);
1652 : 2708 : spdk_mem_map_free(&g_phys_ref_map);
1653 : 2708 : }
1654 : :
1655 : : uint64_t
1656 : 121556325 : spdk_vtophys(const void *buf, uint64_t *size)
1657 : : {
1658 : : uint64_t vaddr, paddr_2mb;
1659 : :
1660 [ + + + + ]: 121556325 : if (!g_huge_pages) {
1661 : 0 : return SPDK_VTOPHYS_ERROR;
1662 : : }
1663 : :
1664 : 121556325 : vaddr = (uint64_t)buf;
1665 : 121556325 : paddr_2mb = spdk_mem_map_translate(g_vtophys_map, vaddr, size);
1666 : :
1667 : : /*
1668 : : * SPDK_VTOPHYS_ERROR has all bits set, so if the lookup returned SPDK_VTOPHYS_ERROR,
1669 : : * we will still bitwise-or it with the buf offset below, but the result will still be
1670 : : * SPDK_VTOPHYS_ERROR. However now that we do + rather than | (due to PCI vtophys being
1671 : : * unaligned) we must now check the return value before addition.
1672 : : */
1673 : : SPDK_STATIC_ASSERT(SPDK_VTOPHYS_ERROR == UINT64_C(-1), "SPDK_VTOPHYS_ERROR should be all 1s");
1674 [ + + ]: 121556325 : if (paddr_2mb == SPDK_VTOPHYS_ERROR) {
1675 : 704 : return SPDK_VTOPHYS_ERROR;
1676 : : } else {
1677 [ - + ]: 121555621 : return paddr_2mb + (vaddr & MASK_2MB);
1678 : : }
1679 : 709492 : }
1680 : :
1681 : : int32_t
1682 : 0 : spdk_mem_get_numa_id(const void *buf, uint64_t *size)
1683 : : {
1684 : 0 : return spdk_mem_map_translate(g_numa_map, (uint64_t)buf, size);
1685 : : }
1686 : :
1687 : : int
1688 : 836 : spdk_mem_get_fd_and_offset(void *vaddr, uint64_t *offset)
1689 : : {
1690 : : struct rte_memseg *seg;
1691 : : int ret, fd;
1692 : :
1693 : 836 : seg = rte_mem_virt2memseg(vaddr, NULL);
1694 [ + + ]: 836 : if (!seg) {
1695 : 0 : SPDK_ERRLOG("memory %p doesn't exist\n", vaddr);
1696 : 0 : return -ENOENT;
1697 : : }
1698 : :
1699 : 836 : fd = rte_memseg_get_fd_thread_unsafe(seg);
1700 [ + + ]: 836 : if (fd < 0) {
1701 : 0 : return fd;
1702 : : }
1703 : :
1704 : 836 : ret = rte_memseg_get_fd_offset_thread_unsafe(seg, offset);
1705 [ - + ]: 836 : if (ret < 0) {
1706 : 0 : return ret;
1707 : : }
1708 : :
1709 : 836 : return fd;
1710 : 780 : }
1711 : :
1712 : : void
1713 : 8 : mem_disable_huge_pages(void)
1714 : : {
1715 : 8 : g_huge_pages = false;
1716 : 8 : mem_map_use_page_shift(SHIFT_4KB);
1717 : 8 : }
1718 : :
1719 : : void
1720 : 52 : mem_map_use_page_shift(uint32_t page_shift)
1721 : : {
1722 : 52 : g_map_page_cfg.shift = page_shift;
1723 [ + + + - ]: 52 : g_map_page_cfg.size = 1UL << page_shift;
1724 [ + - + - ]: 52 : g_map_page_cfg.mask = g_map_page_cfg.size - 1;
1725 [ + + + - ]: 52 : g_map_page_cfg.num_pages_per_gb = 1UL << (SHIFT_1GB - page_shift);
1726 : 52 : }
|