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