Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright 2016 6WIND S.A.
3 : : * Copyright 2020 Mellanox Technologies, Ltd
4 : : */
5 : : #include <stddef.h>
6 : :
7 : : #include <eal_export.h>
8 : : #include <rte_eal_memconfig.h>
9 : : #include <rte_eal_paging.h>
10 : : #include <rte_errno.h>
11 : : #include <rte_mempool.h>
12 : : #include <rte_malloc.h>
13 : : #include <rte_rwlock.h>
14 : :
15 : : #include "mlx5_glue.h"
16 : : #include "mlx5_common.h"
17 : : #include "mlx5_common_mp.h"
18 : : #include "mlx5_common_mr.h"
19 : : #include "mlx5_common_os.h"
20 : : #include "mlx5_common_log.h"
21 : : #include "mlx5_malloc.h"
22 : :
23 : : struct mr_find_contig_memsegs_data {
24 : : uintptr_t addr;
25 : : uintptr_t start;
26 : : uintptr_t end;
27 : : const struct rte_memseg_list *msl;
28 : : };
29 : :
30 : : /* Virtual memory range. */
31 : : struct mlx5_range {
32 : : uintptr_t start;
33 : : uintptr_t end;
34 : : };
35 : :
36 : : /** Memory region for a mempool. */
37 : : struct mlx5_mempool_mr {
38 : : struct mlx5_pmd_mr pmd_mr;
39 : : RTE_ATOMIC(uint32_t) refcnt; /**< Number of mempools sharing this MR. */
40 : : };
41 : :
42 : : /* Mempool registration. */
43 : : struct mlx5_mempool_reg {
44 : : LIST_ENTRY(mlx5_mempool_reg) next;
45 : : /** Registered mempool, used to designate registrations. */
46 : : struct rte_mempool *mp;
47 : : /** Memory regions for the address ranges of the mempool. */
48 : : struct mlx5_mempool_mr *mrs;
49 : : /** Number of memory regions. */
50 : : unsigned int mrs_n;
51 : : /** Whether the MR were created for external pinned memory. */
52 : : bool is_extmem;
53 : : };
54 : :
55 : : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_mprq_buf_free_cb)
56 : : void
57 : 0 : mlx5_mprq_buf_free_cb(void *addr __rte_unused, void *opaque)
58 : : {
59 : : struct mlx5_mprq_buf *buf = opaque;
60 : :
61 [ # # ]: 0 : if (rte_atomic_load_explicit(&buf->refcnt, rte_memory_order_relaxed) == 1) {
62 [ # # ]: 0 : rte_mempool_put(buf->mp, buf);
63 [ # # ]: 0 : } else if (unlikely(rte_atomic_fetch_sub_explicit(&buf->refcnt, 1,
64 : : rte_memory_order_relaxed) - 1 == 0)) {
65 : 0 : rte_atomic_store_explicit(&buf->refcnt, 1, rte_memory_order_relaxed);
66 [ # # ]: 0 : rte_mempool_put(buf->mp, buf);
67 : : }
68 : 0 : }
69 : :
70 : : /**
71 : : * Expand B-tree table to a given size. Can't be called with holding
72 : : * memory_hotplug_lock or share_cache.rwlock due to rte_realloc().
73 : : *
74 : : * @param bt
75 : : * Pointer to B-tree structure.
76 : : * @param n
77 : : * Number of entries for expansion.
78 : : *
79 : : * @return
80 : : * 0 on success, -1 on failure.
81 : : */
82 : : static int
83 : 0 : mr_btree_expand(struct mlx5_mr_btree *bt, uint32_t n)
84 : : {
85 : : void *mem;
86 : : int ret = 0;
87 : :
88 [ # # ]: 0 : if (n <= bt->size)
89 : : return ret;
90 : : /*
91 : : * Downside of directly using rte_realloc() is that SOCKET_ID_ANY is
92 : : * used inside if there's no room to expand. Because this is a quite
93 : : * rare case and a part of very slow path, it is very acceptable.
94 : : * Initially cache_bh[] will be given practically enough space and once
95 : : * it is expanded, expansion wouldn't be needed again ever.
96 : : */
97 : 0 : mem = mlx5_realloc(bt->table, MLX5_MEM_RTE | MLX5_MEM_ZERO,
98 : : n * sizeof(struct mr_cache_entry), 0, SOCKET_ID_ANY);
99 [ # # ]: 0 : if (mem == NULL) {
100 : : /* Not an error, B-tree search will be skipped. */
101 : 0 : DRV_LOG(WARNING, "failed to expand MR B-tree (%p) table",
102 : : (void *)bt);
103 : : ret = -1;
104 : : } else {
105 : 0 : DRV_LOG(DEBUG, "expanded MR B-tree table (size=%u)", n);
106 : 0 : bt->table = mem;
107 : 0 : bt->size = n;
108 : : }
109 : : return ret;
110 : : }
111 : :
112 : : /**
113 : : * Look up LKey from given B-tree lookup table, store the last index and return
114 : : * searched LKey.
115 : : *
116 : : * @param bt
117 : : * Pointer to B-tree structure.
118 : : * @param[out] idx
119 : : * Pointer to index. Even on search failure, returns index where it stops
120 : : * searching so that index can be used when inserting a new entry.
121 : : * @param addr
122 : : * Search key.
123 : : *
124 : : * @return
125 : : * Searched LKey on success, UINT32_MAX on no match.
126 : : */
127 : : static uint32_t
128 : : mr_btree_lookup(struct mlx5_mr_btree *bt, uint32_t *idx, uintptr_t addr)
129 : : {
130 : : struct mr_cache_entry *lkp_tbl;
131 : : uint32_t n;
132 : : uint32_t base = 0;
133 : :
134 : : MLX5_ASSERT(bt != NULL);
135 : 0 : lkp_tbl = *bt->table;
136 : 0 : n = bt->len;
137 : : /* First entry must be NULL for comparison. */
138 : : MLX5_ASSERT(bt->len > 0 || (lkp_tbl[0].start == 0 &&
139 : : lkp_tbl[0].lkey == UINT32_MAX));
140 : : /* Binary search. */
141 : : do {
142 : 0 : register uint32_t delta = n >> 1;
143 : :
144 [ # # # # : 0 : if (addr < lkp_tbl[base + delta].start) {
# # # # #
# # # ]
145 : : n = delta;
146 : : } else {
147 : : base += delta;
148 : 0 : n -= delta;
149 : : }
150 [ # # # # : 0 : } while (n > 1);
# # # # #
# # # ]
151 : : MLX5_ASSERT(addr >= lkp_tbl[base].start);
152 : : *idx = base;
153 [ # # # # : 0 : if (addr < lkp_tbl[base].end)
# # # # #
# # # ]
154 : 0 : return lkp_tbl[base].lkey;
155 : : /* Not found. */
156 : : return UINT32_MAX;
157 : : }
158 : :
159 : : /**
160 : : * Insert an entry to B-tree lookup table.
161 : : *
162 : : * @param bt
163 : : * Pointer to B-tree structure.
164 : : * @param entry
165 : : * Pointer to new entry to insert.
166 : : *
167 : : * @return
168 : : * 0 on success, -1 on failure.
169 : : */
170 : : static int
171 : 0 : mr_btree_insert(struct mlx5_mr_btree *bt, struct mr_cache_entry *entry)
172 : : {
173 : : struct mr_cache_entry *lkp_tbl;
174 : : uint32_t idx = 0;
175 : : size_t shift;
176 : :
177 : : MLX5_ASSERT(bt != NULL);
178 : : MLX5_ASSERT(bt->len <= bt->size);
179 : : MLX5_ASSERT(bt->len > 0);
180 : 0 : lkp_tbl = *bt->table;
181 : : /* Find out the slot for insertion. */
182 [ # # ]: 0 : if (mr_btree_lookup(bt, &idx, entry->start) != UINT32_MAX) {
183 : 0 : DRV_LOG(DEBUG,
184 : : "abort insertion to B-tree(%p): already exist at"
185 : : " idx=%u [0x%" PRIxPTR ", 0x%" PRIxPTR ") lkey=0x%x",
186 : : (void *)bt, idx, entry->start, entry->end, entry->lkey);
187 : : /* Already exist, return. */
188 : 0 : return 0;
189 : : }
190 : : /* Caller must ensure that there is enough place for a new entry. */
191 : : MLX5_ASSERT(bt->len < bt->size);
192 : : /* Insert entry. */
193 : 0 : ++idx;
194 : 0 : shift = (bt->len - idx) * sizeof(struct mr_cache_entry);
195 [ # # ]: 0 : if (shift)
196 : 0 : memmove(&lkp_tbl[idx + 1], &lkp_tbl[idx], shift);
197 : 0 : lkp_tbl[idx] = *entry;
198 : 0 : bt->len++;
199 : 0 : DRV_LOG(DEBUG,
200 : : "inserted B-tree(%p)[%u],"
201 : : " [0x%" PRIxPTR ", 0x%" PRIxPTR ") lkey=0x%x",
202 : : (void *)bt, idx, entry->start, entry->end, entry->lkey);
203 : 0 : return 0;
204 : : }
205 : :
206 : : /**
207 : : * Initialize B-tree and allocate memory for lookup table.
208 : : *
209 : : * @param bt
210 : : * Pointer to B-tree structure.
211 : : * @param n
212 : : * Number of entries to allocate.
213 : : * @param socket
214 : : * NUMA socket on which memory must be allocated.
215 : : *
216 : : * @return
217 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
218 : : */
219 : : static int
220 : 0 : mlx5_mr_btree_init(struct mlx5_mr_btree *bt, int n, int socket)
221 : : {
222 [ # # ]: 0 : if (bt == NULL) {
223 : 0 : rte_errno = EINVAL;
224 : 0 : return -rte_errno;
225 : : }
226 : : MLX5_ASSERT(!bt->table && !bt->size);
227 : : memset(bt, 0, sizeof(*bt));
228 : 0 : bt->table = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO,
229 : : sizeof(struct mr_cache_entry) * n,
230 : : 0, socket);
231 [ # # ]: 0 : if (bt->table == NULL) {
232 : 0 : rte_errno = ENOMEM;
233 : 0 : DRV_LOG(DEBUG,
234 : : "failed to allocate memory for btree cache on socket "
235 : : "%d", socket);
236 : 0 : return -rte_errno;
237 : : }
238 : 0 : bt->size = n;
239 : : /* First entry must be NULL for binary search. */
240 : 0 : (*bt->table)[bt->len++] = (struct mr_cache_entry) {
241 : : .lkey = UINT32_MAX,
242 : : };
243 : 0 : DRV_LOG(DEBUG, "initialized B-tree %p with table %p",
244 : : (void *)bt, (void *)bt->table);
245 : 0 : return 0;
246 : : }
247 : :
248 : : /**
249 : : * Free B-tree resources.
250 : : *
251 : : * @param bt
252 : : * Pointer to B-tree structure.
253 : : */
254 : : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_mr_btree_free)
255 : : void
256 : 0 : mlx5_mr_btree_free(struct mlx5_mr_btree *bt)
257 : : {
258 [ # # ]: 0 : if (bt == NULL)
259 : : return;
260 : 0 : DRV_LOG(DEBUG, "freeing B-tree %p with table %p",
261 : : (void *)bt, (void *)bt->table);
262 : 0 : mlx5_free(bt->table);
263 : : memset(bt, 0, sizeof(*bt));
264 : : }
265 : :
266 : : /**
267 : : * Dump all the entries in a B-tree
268 : : *
269 : : * @param bt
270 : : * Pointer to B-tree structure.
271 : : */
272 : : void
273 : 0 : mlx5_mr_btree_dump(struct mlx5_mr_btree *bt __rte_unused)
274 : : {
275 : : #ifdef RTE_PMD_MLX5_DEBUG
276 : : uint32_t idx;
277 : : struct mr_cache_entry *lkp_tbl;
278 : :
279 : : if (bt == NULL)
280 : : return;
281 : : lkp_tbl = *bt->table;
282 : : for (idx = 0; idx < bt->len; ++idx) {
283 : : struct mr_cache_entry *entry = &lkp_tbl[idx];
284 : :
285 : : DRV_LOG(DEBUG, "B-tree(%p)[%u],"
286 : : " [0x%" PRIxPTR ", 0x%" PRIxPTR ") lkey=0x%x",
287 : : (void *)bt, idx, entry->start, entry->end, entry->lkey);
288 : : }
289 : : #endif
290 : 0 : }
291 : :
292 : : /**
293 : : * Initialize per-queue MR control descriptor.
294 : : *
295 : : * @param mr_ctrl
296 : : * Pointer to MR control structure.
297 : : * @param dev_gen_ptr
298 : : * Pointer to generation number of global cache.
299 : : * @param socket
300 : : * NUMA socket on which memory must be allocated.
301 : : *
302 : : * @return
303 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
304 : : */
305 : : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_mr_ctrl_init)
306 : : int
307 : 0 : mlx5_mr_ctrl_init(struct mlx5_mr_ctrl *mr_ctrl, uint32_t *dev_gen_ptr,
308 : : int socket)
309 : : {
310 [ # # ]: 0 : if (mr_ctrl == NULL) {
311 : 0 : rte_errno = EINVAL;
312 : 0 : return -rte_errno;
313 : : }
314 : : /* Save pointer of global generation number to check memory event. */
315 : 0 : mr_ctrl->dev_gen_ptr = dev_gen_ptr;
316 : : /* Initialize B-tree and allocate memory for bottom-half cache table. */
317 : 0 : return mlx5_mr_btree_init(&mr_ctrl->cache_bh, MLX5_MR_BTREE_CACHE_N,
318 : : socket);
319 : : }
320 : :
321 : : /**
322 : : * Find virtually contiguous memory chunk in a given MR.
323 : : *
324 : : * @param dev
325 : : * Pointer to MR structure.
326 : : * @param[out] entry
327 : : * Pointer to returning MR cache entry. If not found, this will not be
328 : : * updated.
329 : : * @param start_idx
330 : : * Start index of the memseg bitmap.
331 : : *
332 : : * @return
333 : : * Next index to go on lookup.
334 : : */
335 : : static int
336 : 0 : mr_find_next_chunk(struct mlx5_mr *mr, struct mr_cache_entry *entry,
337 : : int base_idx)
338 : : {
339 : : uintptr_t start = 0;
340 : : uintptr_t end = 0;
341 : : uint32_t idx = 0;
342 : :
343 : : /* MR for external memory doesn't have memseg list. */
344 [ # # ]: 0 : if (mr->msl == NULL) {
345 : : MLX5_ASSERT(mr->ms_bmp_n == 1);
346 : : MLX5_ASSERT(mr->ms_n == 1);
347 : : MLX5_ASSERT(base_idx == 0);
348 : : /*
349 : : * Can't search it from memseg list but get it directly from
350 : : * pmd_mr as there's only one chunk.
351 : : */
352 : 0 : entry->start = (uintptr_t)mr->pmd_mr.addr;
353 : 0 : entry->end = (uintptr_t)mr->pmd_mr.addr + mr->pmd_mr.len;
354 [ # # ]: 0 : entry->lkey = rte_cpu_to_be_32(mr->pmd_mr.lkey);
355 : : /* Returning 1 ends iteration. */
356 : 0 : return 1;
357 : : }
358 [ # # ]: 0 : for (idx = base_idx; idx < mr->ms_bmp_n; ++idx) {
359 [ # # ]: 0 : if (rte_bitmap_get(mr->ms_bmp, idx)) {
360 : : const struct rte_memseg_list *msl;
361 : : const struct rte_memseg *ms;
362 : :
363 : 0 : msl = mr->msl;
364 : 0 : ms = rte_fbarray_get(&msl->memseg_arr,
365 : 0 : mr->ms_base_idx + idx);
366 : : MLX5_ASSERT(msl->page_sz == ms->hugepage_sz);
367 [ # # ]: 0 : if (!start)
368 : 0 : start = ms->addr_64;
369 : 0 : end = ms->addr_64 + ms->hugepage_sz;
370 [ # # ]: 0 : } else if (start) {
371 : : /* Passed the end of a fragment. */
372 : : break;
373 : : }
374 : : }
375 [ # # ]: 0 : if (start) {
376 : : /* Found one chunk. */
377 : 0 : entry->start = start;
378 : 0 : entry->end = end;
379 [ # # ]: 0 : entry->lkey = rte_cpu_to_be_32(mr->pmd_mr.lkey);
380 : : }
381 : 0 : return idx;
382 : : }
383 : :
384 : : /**
385 : : * Insert a MR to the global B-tree cache. It may fail due to low-on-memory.
386 : : * Then, this entry will have to be searched by mr_lookup_list() in
387 : : * mlx5_mr_create() on miss.
388 : : *
389 : : * @param share_cache
390 : : * Pointer to a global shared MR cache.
391 : : * @param mr
392 : : * Pointer to MR to insert.
393 : : *
394 : : * @return
395 : : * 0 on success, -1 on failure.
396 : : */
397 : : int
398 : 0 : mlx5_mr_insert_cache(struct mlx5_mr_share_cache *share_cache,
399 : : struct mlx5_mr *mr)
400 : : {
401 : : unsigned int n;
402 : :
403 : 0 : DRV_LOG(DEBUG, "Inserting MR(%p) to global cache(%p)",
404 : : (void *)mr, (void *)share_cache);
405 [ # # ]: 0 : for (n = 0; n < mr->ms_bmp_n; ) {
406 : : struct mr_cache_entry entry;
407 : :
408 : : memset(&entry, 0, sizeof(entry));
409 : : /* Find a contiguous chunk and advance the index. */
410 : 0 : n = mr_find_next_chunk(mr, &entry, n);
411 [ # # ]: 0 : if (!entry.end)
412 : : break;
413 [ # # ]: 0 : if (mr_btree_insert(&share_cache->cache, &entry) < 0)
414 : 0 : return -1;
415 : : }
416 : : return 0;
417 : : }
418 : :
419 : : /**
420 : : * Look up address in the original global MR list.
421 : : *
422 : : * @param share_cache
423 : : * Pointer to a global shared MR cache.
424 : : * @param[out] entry
425 : : * Pointer to returning MR cache entry. If no match, this will not be updated.
426 : : * @param addr
427 : : * Search key.
428 : : *
429 : : * @return
430 : : * Found MR on match, NULL otherwise.
431 : : */
432 : : struct mlx5_mr *
433 : 0 : mlx5_mr_lookup_list(struct mlx5_mr_share_cache *share_cache,
434 : : struct mr_cache_entry *entry, uintptr_t addr)
435 : : {
436 : : struct mlx5_mr *mr;
437 : :
438 : : /* Iterate all the existing MRs. */
439 [ # # ]: 0 : LIST_FOREACH(mr, &share_cache->mr_list, mr) {
440 : : unsigned int n;
441 : :
442 [ # # ]: 0 : if (mr->ms_n == 0)
443 : 0 : continue;
444 [ # # ]: 0 : for (n = 0; n < mr->ms_bmp_n; ) {
445 : : struct mr_cache_entry ret;
446 : :
447 : : memset(&ret, 0, sizeof(ret));
448 : 0 : n = mr_find_next_chunk(mr, &ret, n);
449 [ # # # # ]: 0 : if (addr >= ret.start && addr < ret.end) {
450 : : /* Found. */
451 : 0 : *entry = ret;
452 : 0 : return mr;
453 : : }
454 : : }
455 : : }
456 : : return NULL;
457 : : }
458 : :
459 : : /**
460 : : * Look up address on global MR cache.
461 : : *
462 : : * @param share_cache
463 : : * Pointer to a global shared MR cache.
464 : : * @param[out] entry
465 : : * Pointer to returning MR cache entry. If no match, this will not be updated.
466 : : * @param addr
467 : : * Search key.
468 : : *
469 : : * @return
470 : : * Searched LKey on success, UINT32_MAX on failure and rte_errno is set.
471 : : */
472 : : static uint32_t
473 : 0 : mlx5_mr_lookup_cache(struct mlx5_mr_share_cache *share_cache,
474 : : struct mr_cache_entry *entry, uintptr_t addr)
475 : : {
476 : : uint32_t idx;
477 : : uint32_t lkey;
478 : :
479 : : lkey = mr_btree_lookup(&share_cache->cache, &idx, addr);
480 [ # # ]: 0 : if (lkey != UINT32_MAX)
481 : 0 : *entry = (*share_cache->cache.table)[idx];
482 : : MLX5_ASSERT(lkey == UINT32_MAX || (addr >= entry->start &&
483 : : addr < entry->end));
484 : 0 : return lkey;
485 : : }
486 : :
487 : : /**
488 : : * Free MR resources. MR lock must not be held to avoid a deadlock. rte_free()
489 : : * can raise memory free event and the callback function will spin on the lock.
490 : : *
491 : : * @param mr
492 : : * Pointer to MR to free.
493 : : */
494 : : void
495 : 0 : mlx5_mr_free(struct mlx5_mr *mr)
496 : : {
497 [ # # ]: 0 : if (mr == NULL)
498 : : return;
499 : 0 : DRV_LOG(DEBUG, "freeing MR(%p):", (void *)mr);
500 : 0 : mlx5_os_dereg_mr(&mr->pmd_mr);
501 : : rte_bitmap_free(mr->ms_bmp);
502 : 0 : mlx5_free(mr);
503 : : }
504 : :
505 : : void
506 : 0 : mlx5_mr_rebuild_cache(struct mlx5_mr_share_cache *share_cache)
507 : : {
508 : : struct mlx5_mr *mr;
509 : :
510 : 0 : DRV_LOG(DEBUG, "Rebuild dev cache[] %p", (void *)share_cache);
511 : : /* Flush cache to rebuild. */
512 : 0 : share_cache->cache.len = 1;
513 : : /* Iterate all the existing MRs. */
514 [ # # ]: 0 : LIST_FOREACH(mr, &share_cache->mr_list, mr)
515 [ # # ]: 0 : if (mlx5_mr_insert_cache(share_cache, mr) < 0)
516 : : return;
517 : : }
518 : :
519 : : /**
520 : : * Release resources of detached MR having no online entry.
521 : : *
522 : : * @param share_cache
523 : : * Pointer to a global shared MR cache.
524 : : */
525 : : static void
526 : 0 : mlx5_mr_garbage_collect(struct mlx5_mr_share_cache *share_cache)
527 : : {
528 : : struct mlx5_mr *mr_next;
529 : : struct mlx5_mr_list free_list = LIST_HEAD_INITIALIZER(free_list);
530 : :
531 : : /* Must be called from the primary process. */
532 : : MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
533 : : /*
534 : : * MR can't be freed with holding the lock because rte_free() could call
535 : : * memory free callback function. This will be a deadlock situation.
536 : : */
537 : 0 : rte_rwlock_write_lock(&share_cache->rwlock);
538 : : /* Detach the whole free list and release it after unlocking. */
539 : 0 : free_list = share_cache->mr_free_list;
540 : 0 : LIST_INIT(&share_cache->mr_free_list);
541 : : rte_rwlock_write_unlock(&share_cache->rwlock);
542 : : /* Release resources. */
543 : : mr_next = LIST_FIRST(&free_list);
544 [ # # ]: 0 : while (mr_next != NULL) {
545 : : struct mlx5_mr *mr = mr_next;
546 : :
547 : 0 : mr_next = LIST_NEXT(mr, mr);
548 : 0 : mlx5_mr_free(mr);
549 : : }
550 : 0 : }
551 : :
552 : : /* Called during rte_memseg_contig_walk() by mlx5_mr_create(). */
553 : : static int
554 : 0 : mr_find_contig_memsegs_cb(const struct rte_memseg_list *msl,
555 : : const struct rte_memseg *ms, size_t len, void *arg)
556 : : {
557 : : struct mr_find_contig_memsegs_data *data = arg;
558 : :
559 [ # # # # ]: 0 : if (data->addr < ms->addr_64 || data->addr >= ms->addr_64 + len)
560 : : return 0;
561 : : /* Found, save it and stop walking. */
562 : 0 : data->start = ms->addr_64;
563 : 0 : data->end = ms->addr_64 + len;
564 : 0 : data->msl = msl;
565 : 0 : return 1;
566 : : }
567 : :
568 : : /**
569 : : * Get the number of virtually-contiguous chunks in the MR.
570 : : * HW MR does not need to be already created to use this function.
571 : : *
572 : : * @param mr
573 : : * Pointer to the MR.
574 : : *
575 : : * @return
576 : : * Number of chunks.
577 : : */
578 : : static uint32_t
579 : 0 : mr_get_chunk_count(const struct mlx5_mr *mr)
580 : : {
581 : : uint32_t i, count = 0;
582 : : bool was_in_chunk = false;
583 : : bool is_in_chunk;
584 : :
585 : : /* There is only one chunk in case of external memory. */
586 [ # # ]: 0 : if (mr->msl == NULL)
587 : : return 1;
588 [ # # ]: 0 : for (i = 0; i < mr->ms_bmp_n; i++) {
589 [ # # ]: 0 : is_in_chunk = rte_bitmap_get(mr->ms_bmp, i);
590 [ # # ]: 0 : if (!was_in_chunk && is_in_chunk)
591 : 0 : count++;
592 : : was_in_chunk = is_in_chunk;
593 : : }
594 : : return count;
595 : : }
596 : :
597 : : /**
598 : : * Thread-safely expand the global MR cache to at least @p new_size slots.
599 : : *
600 : : * @param share_cache
601 : : * Shared MR cache for locking.
602 : : * @param new_size
603 : : * Desired cache size.
604 : : * @param socket
605 : : * NUMA node.
606 : : *
607 : : * @return
608 : : * 0 in success, negative on failure and rte_errno is set.
609 : : */
610 : : int
611 : 0 : mlx5_mr_expand_cache(struct mlx5_mr_share_cache *share_cache,
612 : : uint32_t size, int socket)
613 : : {
614 : 0 : struct mlx5_mr_btree cache = {0};
615 : : struct mlx5_mr_btree *bt;
616 : : struct mr_cache_entry *lkp_tbl;
617 : : int ret;
618 : :
619 : : size = rte_align32pow2(size);
620 : 0 : ret = mlx5_mr_btree_init(&cache, size, socket);
621 [ # # ]: 0 : if (ret < 0)
622 : : return ret;
623 : 0 : rte_rwlock_write_lock(&share_cache->rwlock);
624 : : bt = &share_cache->cache;
625 : 0 : lkp_tbl = *bt->table;
626 [ # # ]: 0 : if (cache.size > bt->size) {
627 [ # # ]: 0 : rte_memcpy(cache.table, lkp_tbl, bt->len * sizeof(lkp_tbl[0]));
628 : 0 : RTE_SWAP(*bt, cache);
629 : 0 : DRV_LOG(DEBUG, "Global MR cache expanded to %u slots", size);
630 : : }
631 : : rte_rwlock_write_unlock(&share_cache->rwlock);
632 : 0 : mlx5_mr_btree_free(&cache);
633 : 0 : return 0;
634 : : }
635 : :
636 : : /**
637 : : * Create a new global Memory Region (MR) for a missing virtual address.
638 : : * This API should be called on a secondary process, then a request is sent to
639 : : * the primary process in order to create a MR for the address. As the global MR
640 : : * list is on the shared memory, following LKey lookup should succeed unless the
641 : : * request fails.
642 : : *
643 : : * @param cdev
644 : : * Pointer to the mlx5 common device.
645 : : * @param share_cache
646 : : * Pointer to a global shared MR cache.
647 : : * @param[out] entry
648 : : * Pointer to returning MR cache entry, found in the global cache or newly
649 : : * created. If failed to create one, this will not be updated.
650 : : * @param addr
651 : : * Target virtual address to register.
652 : : *
653 : : * @return
654 : : * Searched LKey on success, UINT32_MAX on failure and rte_errno is set.
655 : : */
656 : : static uint32_t
657 : 0 : mlx5_mr_create_secondary(struct mlx5_common_device *cdev,
658 : : struct mlx5_mr_share_cache *share_cache,
659 : : struct mr_cache_entry *entry, uintptr_t addr)
660 : : {
661 : : int ret;
662 : :
663 : 0 : DRV_LOG(DEBUG, "Requesting MR creation for address (%p)", (void *)addr);
664 : 0 : ret = mlx5_mp_req_mr_create(cdev, addr);
665 [ # # ]: 0 : if (ret) {
666 : 0 : DRV_LOG(DEBUG, "Fail to request MR creation for address (%p)",
667 : : (void *)addr);
668 : 0 : return UINT32_MAX;
669 : : }
670 : 0 : rte_rwlock_read_lock(&share_cache->rwlock);
671 : : /* Fill in output data. */
672 : 0 : mlx5_mr_lookup_cache(share_cache, entry, addr);
673 : : /* Lookup can't fail. */
674 : : MLX5_ASSERT(entry->lkey != UINT32_MAX);
675 : : rte_rwlock_read_unlock(&share_cache->rwlock);
676 : 0 : DRV_LOG(DEBUG, "MR CREATED by primary process for %p:\n"
677 : : " [0x%" PRIxPTR ", 0x%" PRIxPTR "), lkey=0x%x",
678 : : (void *)addr, entry->start, entry->end, entry->lkey);
679 : 0 : return entry->lkey;
680 : : }
681 : :
682 : : /**
683 : : * Create a new global Memory Region (MR) for a missing virtual address.
684 : : * Register entire virtually contiguous memory chunk around the address.
685 : : *
686 : : * @param pd
687 : : * Pointer to pd of a device (net, regex, vdpa,...).
688 : : * @param share_cache
689 : : * Pointer to a global shared MR cache.
690 : : * @param[out] entry
691 : : * Pointer to returning MR cache entry, found in the global cache or newly
692 : : * created. If failed to create one, this will not be updated.
693 : : * @param addr
694 : : * Target virtual address to register.
695 : : * @param mr_ext_memseg_en
696 : : * Configurable flag about external memory segment enable or not.
697 : : *
698 : : * @return
699 : : * Searched LKey on success, UINT32_MAX on failure and rte_errno is set.
700 : : */
701 : : static uint32_t
702 : 0 : mlx5_mr_create_primary(void *pd,
703 : : struct mlx5_mr_share_cache *share_cache,
704 : : struct mr_cache_entry *entry, uintptr_t addr,
705 : : unsigned int mr_ext_memseg_en)
706 : : {
707 : 0 : struct mr_find_contig_memsegs_data data = {.addr = addr, };
708 : : struct mr_find_contig_memsegs_data data_re;
709 : : const struct rte_memseg_list *msl;
710 : : const struct rte_memseg *ms;
711 : : struct mlx5_mr_btree *bt;
712 : : struct mlx5_mr *mr = NULL;
713 : : int ms_idx_shift = -1;
714 : : uint32_t bmp_size;
715 : : void *bmp_mem;
716 : : uint32_t ms_n;
717 : : uint32_t n;
718 : : uint32_t chunks_n;
719 : : size_t len;
720 : :
721 : 0 : DRV_LOG(DEBUG, "Creating a MR using address (%p)", (void *)addr);
722 : : /*
723 : : * Release detached MRs if any. This can't be called with holding either
724 : : * memory_hotplug_lock or share_cache->rwlock. MRs on the free list have
725 : : * been detached by the memory free event but it couldn't be released
726 : : * inside the callback due to deadlock. As a result, releasing resources
727 : : * is quite opportunistic.
728 : : */
729 : 0 : mlx5_mr_garbage_collect(share_cache);
730 : 0 : find_range:
731 : : /*
732 : : * If enabled, find out a contiguous virtual address chunk in use, to
733 : : * which the given address belongs, in order to register maximum range.
734 : : * In the best case where mempools are not dynamically recreated and
735 : : * '--socket-mem' is specified as an EAL option, it is very likely to
736 : : * have only one MR(LKey) per a socket and per a hugepage-size even
737 : : * though the system memory is highly fragmented. As the whole memory
738 : : * chunk will be pinned by kernel, it can't be reused unless entire
739 : : * chunk is freed from EAL.
740 : : *
741 : : * If disabled, just register one memseg (page). Then, memory
742 : : * consumption will be minimized but it may drop performance if there
743 : : * are many MRs to lookup on the datapath.
744 : : */
745 [ # # ]: 0 : if (!mr_ext_memseg_en) {
746 : 0 : data.msl = rte_mem_virt2memseg_list((void *)addr);
747 : 0 : data.start = RTE_ALIGN_FLOOR(addr, data.msl->page_sz);
748 : 0 : data.end = data.start + data.msl->page_sz;
749 [ # # ]: 0 : } else if (!rte_memseg_contig_walk(mr_find_contig_memsegs_cb, &data)) {
750 : 0 : DRV_LOG(WARNING,
751 : : "Unable to find virtually contiguous"
752 : : " chunk for address (%p)."
753 : : " rte_memseg_contig_walk() failed.", (void *)addr);
754 : 0 : rte_errno = ENXIO;
755 : 0 : goto err_nolock;
756 : : }
757 : 0 : alloc_resources:
758 : : /* Addresses must be page-aligned. */
759 : : MLX5_ASSERT(data.msl);
760 : : MLX5_ASSERT(rte_is_aligned((void *)data.start, data.msl->page_sz));
761 : : MLX5_ASSERT(rte_is_aligned((void *)data.end, data.msl->page_sz));
762 : 0 : msl = data.msl;
763 : 0 : ms = rte_mem_virt2memseg((void *)data.start, msl);
764 : 0 : len = data.end - data.start;
765 : : MLX5_ASSERT(ms);
766 : : MLX5_ASSERT(msl->page_sz == ms->hugepage_sz);
767 : : /* Number of memsegs in the range. */
768 : 0 : ms_n = len / msl->page_sz;
769 : 0 : DRV_LOG(DEBUG, "Extending %p to [0x%" PRIxPTR ", 0x%" PRIxPTR "),"
770 : : " page_sz=0x%" PRIx64 ", ms_n=%u",
771 : : (void *)addr, data.start, data.end, msl->page_sz, ms_n);
772 : : /* Size of memory for bitmap. */
773 : 0 : bmp_size = rte_bitmap_get_memory_footprint(ms_n);
774 : 0 : mr = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO,
775 : : RTE_ALIGN_CEIL(sizeof(*mr), RTE_CACHE_LINE_SIZE) +
776 : 0 : bmp_size, RTE_CACHE_LINE_SIZE, msl->socket_id);
777 [ # # ]: 0 : if (mr == NULL) {
778 : 0 : DRV_LOG(DEBUG, "Unable to allocate memory for a new MR of"
779 : : " address (%p).", (void *)addr);
780 : 0 : rte_errno = ENOMEM;
781 : 0 : goto err_nolock;
782 : : }
783 : 0 : mr->msl = msl;
784 : : /*
785 : : * Save the index of the first memseg and initialize memseg bitmap. To
786 : : * see if a memseg of ms_idx in the memseg-list is still valid, check:
787 : : * rte_bitmap_get(mr->bmp, ms_idx - mr->ms_base_idx)
788 : : */
789 : 0 : mr->ms_base_idx = rte_fbarray_find_idx(&msl->memseg_arr, ms);
790 : 0 : bmp_mem = RTE_PTR_ALIGN_CEIL(mr + 1, RTE_CACHE_LINE_SIZE);
791 : 0 : mr->ms_bmp = rte_bitmap_init(ms_n, bmp_mem, bmp_size);
792 [ # # ]: 0 : if (mr->ms_bmp == NULL) {
793 : 0 : DRV_LOG(DEBUG, "Unable to initialize bitmap for a new MR of"
794 : : " address (%p).", (void *)addr);
795 : 0 : rte_errno = EINVAL;
796 : 0 : goto err_nolock;
797 : : }
798 : : /*
799 : : * Should recheck whether the extended contiguous chunk is still valid.
800 : : * Because memory_hotplug_lock can't be held if there's any memory
801 : : * related calls in a critical path, resource allocation above can't be
802 : : * locked. If the memory has been changed at this point, try again with
803 : : * just single page. If not, go on with the big chunk atomically from
804 : : * here.
805 : : */
806 : 0 : rte_mcfg_mem_read_lock();
807 : 0 : data_re = data;
808 [ # # # # ]: 0 : if (len > msl->page_sz &&
809 : 0 : !rte_memseg_contig_walk(mr_find_contig_memsegs_cb, &data_re)) {
810 : 0 : DRV_LOG(DEBUG,
811 : : "Unable to find virtually contiguous chunk for address "
812 : : "(%p). rte_memseg_contig_walk() failed.", (void *)addr);
813 : 0 : rte_errno = ENXIO;
814 : 0 : goto err_memlock;
815 : : }
816 [ # # # # ]: 0 : if (data.start != data_re.start || data.end != data_re.end) {
817 : : /*
818 : : * The extended contiguous chunk has been changed. Try again
819 : : * with single memseg instead.
820 : : */
821 : 0 : data.start = RTE_ALIGN_FLOOR(addr, msl->page_sz);
822 : 0 : data.end = data.start + msl->page_sz;
823 : 0 : rte_mcfg_mem_read_unlock();
824 : 0 : mlx5_mr_free(mr);
825 : 0 : goto alloc_resources;
826 : : }
827 : : MLX5_ASSERT(data.msl == data_re.msl);
828 : 0 : rte_rwlock_write_lock(&share_cache->rwlock);
829 : : /*
830 : : * Check the address is really missing. If other thread already created
831 : : * one or it is not found due to overflow, abort and return.
832 : : */
833 [ # # ]: 0 : if (mlx5_mr_lookup_cache(share_cache, entry, addr) != UINT32_MAX) {
834 : : /*
835 : : * Insert to the global cache table. It may fail due to
836 : : * low-on-memory. Then, this entry will have to be searched
837 : : * here again.
838 : : */
839 : 0 : mr_btree_insert(&share_cache->cache, entry);
840 : 0 : DRV_LOG(DEBUG, "Found MR for %p on final lookup, abort",
841 : : (void *)addr);
842 : : rte_rwlock_write_unlock(&share_cache->rwlock);
843 : 0 : rte_mcfg_mem_read_unlock();
844 : : /*
845 : : * Must be unlocked before calling rte_free() because
846 : : * mlx5_mr_mem_event_free_cb() can be called inside.
847 : : */
848 : 0 : mlx5_mr_free(mr);
849 : 0 : return entry->lkey;
850 : : }
851 : : /*
852 : : * Trim start and end addresses for verbs MR. Set bits for registering
853 : : * memsegs but exclude already registered ones. Bitmap can be
854 : : * fragmented.
855 : : */
856 [ # # ]: 0 : for (n = 0; n < ms_n; ++n) {
857 : : uintptr_t start;
858 : : struct mr_cache_entry ret;
859 : :
860 : : memset(&ret, 0, sizeof(ret));
861 : 0 : start = data_re.start + n * msl->page_sz;
862 : : /* Exclude memsegs already registered by other MRs. */
863 [ # # ]: 0 : if (mlx5_mr_lookup_cache(share_cache, &ret, start) ==
864 : : UINT32_MAX) {
865 : : /*
866 : : * Start from the first unregistered memseg in the
867 : : * extended range.
868 : : */
869 [ # # ]: 0 : if (ms_idx_shift == -1) {
870 : 0 : mr->ms_base_idx += n;
871 : 0 : data.start = start;
872 : 0 : ms_idx_shift = n;
873 : : }
874 : 0 : data.end = start + msl->page_sz;
875 : 0 : rte_bitmap_set(mr->ms_bmp, n - ms_idx_shift);
876 : 0 : ++mr->ms_n;
877 : : }
878 : : }
879 : 0 : len = data.end - data.start;
880 : 0 : mr->ms_bmp_n = len / msl->page_sz;
881 : : MLX5_ASSERT(ms_idx_shift + mr->ms_bmp_n <= ms_n);
882 : : /*
883 : : * It is now known how many entries will be used in the global cache.
884 : : * If there is not enough, expand the cache.
885 : : * This cannot be done while holding the memory hotplug lock.
886 : : * While it is released, memory layout may change,
887 : : * so the process must be repeated from the beginning.
888 : : */
889 : : bt = &share_cache->cache;
890 : 0 : chunks_n = mr_get_chunk_count(mr);
891 [ # # ]: 0 : if (bt->len + chunks_n > bt->size) {
892 : : struct mlx5_common_device *cdev;
893 : : uint32_t size;
894 : :
895 : 0 : size = bt->size + chunks_n;
896 : : MLX5_ASSERT(size > bt->size);
897 : 0 : cdev = container_of(share_cache, struct mlx5_common_device,
898 : : mr_scache);
899 : : rte_rwlock_write_unlock(&share_cache->rwlock);
900 : 0 : rte_mcfg_mem_read_unlock();
901 [ # # ]: 0 : if (mlx5_mr_expand_cache(share_cache, size,
902 : 0 : cdev->dev->numa_node) < 0) {
903 : 0 : DRV_LOG(ERR, "Failed to expand global MR cache to %u slots",
904 : : size);
905 : 0 : goto err_nolock;
906 : : }
907 : 0 : goto find_range;
908 : : }
909 : : /*
910 : : * Finally create an MR for the memory chunk. Verbs: ibv_reg_mr() can
911 : : * be called with holding the memory lock because it doesn't use
912 : : * mlx5_alloc_buf_extern() which eventually calls rte_malloc_socket()
913 : : * through mlx5_alloc_verbs_buf().
914 : : */
915 : 0 : mlx5_os_reg_mr(pd, (void *)data.start, len, &mr->pmd_mr);
916 [ # # ]: 0 : if (mr->pmd_mr.obj == NULL) {
917 : 0 : DRV_LOG(DEBUG, "Fail to create an MR for address (%p)",
918 : : (void *)addr);
919 : 0 : rte_errno = EINVAL;
920 : 0 : goto err_mrlock;
921 : : }
922 : : MLX5_ASSERT((uintptr_t)mr->pmd_mr.addr == data.start);
923 : : MLX5_ASSERT(mr->pmd_mr.len);
924 [ # # ]: 0 : LIST_INSERT_HEAD(&share_cache->mr_list, mr, mr);
925 [ # # ]: 0 : DRV_LOG(DEBUG, "MR CREATED (%p) for %p:\n"
926 : : " [0x%" PRIxPTR ", 0x%" PRIxPTR "),"
927 : : " lkey=0x%x base_idx=%u ms_n=%u, ms_bmp_n=%u",
928 : : (void *)mr, (void *)addr, data.start, data.end,
929 : : rte_cpu_to_be_32(mr->pmd_mr.lkey),
930 : : mr->ms_base_idx, mr->ms_n, mr->ms_bmp_n);
931 : : /* Insert to the global cache table. */
932 : 0 : mlx5_mr_insert_cache(share_cache, mr);
933 : : /* Fill in output data. */
934 : 0 : mlx5_mr_lookup_cache(share_cache, entry, addr);
935 : : /* Lookup can't fail. */
936 : : MLX5_ASSERT(entry->lkey != UINT32_MAX);
937 : : rte_rwlock_write_unlock(&share_cache->rwlock);
938 : 0 : rte_mcfg_mem_read_unlock();
939 : 0 : return entry->lkey;
940 : : err_mrlock:
941 : : rte_rwlock_write_unlock(&share_cache->rwlock);
942 : 0 : err_memlock:
943 : 0 : rte_mcfg_mem_read_unlock();
944 : 0 : err_nolock:
945 : : /*
946 : : * In case of error, as this can be called in a datapath, a warning
947 : : * message per an error is preferable instead. Must be unlocked before
948 : : * calling rte_free() because mlx5_mr_mem_event_free_cb() can be called
949 : : * inside.
950 : : */
951 : 0 : mlx5_mr_free(mr);
952 : 0 : return UINT32_MAX;
953 : : }
954 : :
955 : : /**
956 : : * Create a new global Memory Region (MR) for a missing virtual address.
957 : : * This can be called from primary and secondary process.
958 : : *
959 : : * @param cdev
960 : : * Pointer to the mlx5 common device.
961 : : * @param share_cache
962 : : * Pointer to a global shared MR cache.
963 : : * @param[out] entry
964 : : * Pointer to returning MR cache entry, found in the global cache or newly
965 : : * created. If failed to create one, this will not be updated.
966 : : * @param addr
967 : : * Target virtual address to register.
968 : : *
969 : : * @return
970 : : * Searched LKey on success, UINT32_MAX on failure and rte_errno is set.
971 : : */
972 : : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_mr_create)
973 : : uint32_t
974 : 0 : mlx5_mr_create(struct mlx5_common_device *cdev,
975 : : struct mlx5_mr_share_cache *share_cache,
976 : : struct mr_cache_entry *entry, uintptr_t addr)
977 : : {
978 : : uint32_t ret = 0;
979 : :
980 [ # # # ]: 0 : switch (rte_eal_process_type()) {
981 : 0 : case RTE_PROC_PRIMARY:
982 : 0 : ret = mlx5_mr_create_primary(cdev->pd, share_cache, entry, addr,
983 : 0 : cdev->config.mr_ext_memseg_en);
984 : 0 : break;
985 : 0 : case RTE_PROC_SECONDARY:
986 : 0 : ret = mlx5_mr_create_secondary(cdev, share_cache, entry, addr);
987 : 0 : break;
988 : : default:
989 : : break;
990 : : }
991 : 0 : return ret;
992 : : }
993 : :
994 : : /**
995 : : * Look up address in the global MR cache table. If not found, create a new MR.
996 : : * Insert the found/created entry to local bottom-half cache table.
997 : : *
998 : : * @param mr_ctrl
999 : : * Pointer to per-queue MR control structure.
1000 : : * @param[out] entry
1001 : : * Pointer to returning MR cache entry, found in the global cache or newly
1002 : : * created. If failed to create one, this is not written.
1003 : : * @param addr
1004 : : * Search key.
1005 : : *
1006 : : * @return
1007 : : * Searched LKey on success, UINT32_MAX on no match.
1008 : : */
1009 : : static uint32_t
1010 : 0 : mr_lookup_caches(struct mlx5_mr_ctrl *mr_ctrl,
1011 : : struct mr_cache_entry *entry, uintptr_t addr)
1012 : : {
1013 : : struct mlx5_mr_share_cache *share_cache =
1014 : 0 : container_of(mr_ctrl->dev_gen_ptr, struct mlx5_mr_share_cache,
1015 : : dev_gen);
1016 : : struct mlx5_common_device *cdev =
1017 : 0 : container_of(share_cache, struct mlx5_common_device, mr_scache);
1018 : 0 : struct mlx5_mr_btree *bt = &mr_ctrl->cache_bh;
1019 : : uint32_t lkey;
1020 : : uint32_t idx;
1021 : :
1022 : : /* If local cache table is full, try to double it. */
1023 [ # # ]: 0 : if (unlikely(bt->len == bt->size))
1024 : 0 : mr_btree_expand(bt, bt->size << 1);
1025 : : /* Look up in the global cache. */
1026 : 0 : rte_rwlock_read_lock(&share_cache->rwlock);
1027 : : lkey = mr_btree_lookup(&share_cache->cache, &idx, addr);
1028 [ # # ]: 0 : if (lkey != UINT32_MAX) {
1029 : : /* Found. */
1030 : 0 : *entry = (*share_cache->cache.table)[idx];
1031 : : rte_rwlock_read_unlock(&share_cache->rwlock);
1032 : : /*
1033 : : * Update local cache. Even if it fails, return the found entry
1034 : : * to update top-half cache. Next time, this entry will be found
1035 : : * in the global cache.
1036 : : */
1037 : 0 : mr_btree_insert(bt, entry);
1038 : 0 : return lkey;
1039 : : }
1040 : : rte_rwlock_read_unlock(&share_cache->rwlock);
1041 : : /* First time to see the address? Create a new MR. */
1042 : 0 : lkey = mlx5_mr_create(cdev, share_cache, entry, addr);
1043 : : /*
1044 : : * Update the local cache if successfully created a new global MR. Even
1045 : : * if failed to create one, there's no action to take in this datapath
1046 : : * code. As returning LKey is invalid, this will eventually make HW
1047 : : * fail.
1048 : : */
1049 [ # # ]: 0 : if (lkey != UINT32_MAX)
1050 : 0 : mr_btree_insert(bt, entry);
1051 : : return lkey;
1052 : : }
1053 : :
1054 : : /**
1055 : : * Bottom-half of LKey search on datapath. First search in cache_bh[] and if
1056 : : * misses, search in the global MR cache table and update the new entry to
1057 : : * per-queue local caches.
1058 : : *
1059 : : * @param mr_ctrl
1060 : : * Pointer to per-queue MR control structure.
1061 : : * @param addr
1062 : : * Search key.
1063 : : *
1064 : : * @return
1065 : : * Searched LKey on success, UINT32_MAX on no match.
1066 : : */
1067 : : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_mr_addr2mr_bh)
1068 : : uint32_t
1069 : 0 : mlx5_mr_addr2mr_bh(struct mlx5_mr_ctrl *mr_ctrl, uintptr_t addr)
1070 : : {
1071 : : uint32_t lkey;
1072 : : uint32_t bh_idx = 0;
1073 : : /* Victim in top-half cache to replace with new entry. */
1074 : 0 : struct mr_cache_entry *repl = &mr_ctrl->cache[mr_ctrl->head];
1075 : :
1076 : : /* Binary-search MR translation table. */
1077 : : lkey = mr_btree_lookup(&mr_ctrl->cache_bh, &bh_idx, addr);
1078 : : /* Update top-half cache. */
1079 [ # # ]: 0 : if (likely(lkey != UINT32_MAX)) {
1080 : 0 : *repl = (*mr_ctrl->cache_bh.table)[bh_idx];
1081 : : } else {
1082 : : /*
1083 : : * If missed in local lookup table, search in the global cache
1084 : : * and local cache_bh[] will be updated inside if possible.
1085 : : * Top-half cache entry will also be updated.
1086 : : */
1087 : 0 : lkey = mr_lookup_caches(mr_ctrl, repl, addr);
1088 [ # # ]: 0 : if (unlikely(lkey == UINT32_MAX))
1089 : : return UINT32_MAX;
1090 : : }
1091 : : /* Update the most recently used entry. */
1092 : 0 : mr_ctrl->mru = mr_ctrl->head;
1093 : : /* Point to the next victim, the oldest. */
1094 : 0 : mr_ctrl->head = (mr_ctrl->head + 1) % MLX5_MR_CACHE_N;
1095 : 0 : return lkey;
1096 : : }
1097 : :
1098 : : /**
1099 : : * Release all the created MRs and resources on global MR cache of a device
1100 : : * list.
1101 : : *
1102 : : * @param share_cache
1103 : : * Pointer to a global shared MR cache.
1104 : : */
1105 : : void
1106 : 0 : mlx5_mr_release_cache(struct mlx5_mr_share_cache *share_cache)
1107 : : {
1108 : : struct mlx5_mr *mr_next;
1109 : :
1110 : 0 : rte_rwlock_write_lock(&share_cache->rwlock);
1111 : : /* Detach from MR list and move to free list. */
1112 : 0 : mr_next = LIST_FIRST(&share_cache->mr_list);
1113 [ # # ]: 0 : while (mr_next != NULL) {
1114 : : struct mlx5_mr *mr = mr_next;
1115 : :
1116 : 0 : mr_next = LIST_NEXT(mr, mr);
1117 [ # # ]: 0 : LIST_REMOVE(mr, mr);
1118 [ # # ]: 0 : LIST_INSERT_HEAD(&share_cache->mr_free_list, mr, mr);
1119 : : }
1120 : 0 : LIST_INIT(&share_cache->mr_list);
1121 : : /* Free global cache. */
1122 : 0 : mlx5_mr_btree_free(&share_cache->cache);
1123 : : rte_rwlock_write_unlock(&share_cache->rwlock);
1124 : : /* Free all remaining MRs. */
1125 : 0 : mlx5_mr_garbage_collect(share_cache);
1126 : 0 : }
1127 : :
1128 : : /**
1129 : : * Initialize global MR cache of a device.
1130 : : *
1131 : : * @param share_cache
1132 : : * Pointer to a global shared MR cache.
1133 : : * @param socket
1134 : : * NUMA socket on which memory must be allocated.
1135 : : *
1136 : : * @return
1137 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
1138 : : */
1139 : : int
1140 : 0 : mlx5_mr_create_cache(struct mlx5_mr_share_cache *share_cache, int socket)
1141 : : {
1142 : : rte_rwlock_init(&share_cache->rwlock);
1143 : : rte_rwlock_init(&share_cache->mprwlock);
1144 : : /* Initialize B-tree and allocate memory for global MR cache table. */
1145 : 0 : return mlx5_mr_btree_init(&share_cache->cache,
1146 : : MLX5_MR_BTREE_CACHE_N * 2, socket);
1147 : : }
1148 : :
1149 : : /**
1150 : : * Flush all of the local cache entries.
1151 : : *
1152 : : * @param mr_ctrl
1153 : : * Pointer to per-queue MR local cache.
1154 : : */
1155 : : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_mr_flush_local_cache)
1156 : : void
1157 : 0 : mlx5_mr_flush_local_cache(struct mlx5_mr_ctrl *mr_ctrl)
1158 : : {
1159 : : /* Reset the most-recently-used index. */
1160 : 0 : mr_ctrl->mru = 0;
1161 : : /* Reset the linear search array. */
1162 : 0 : mr_ctrl->head = 0;
1163 : 0 : memset(mr_ctrl->cache, 0, sizeof(mr_ctrl->cache));
1164 : : /* Reset the B-tree table. */
1165 : 0 : mr_ctrl->cache_bh.len = 1;
1166 : : /* Update the generation number. */
1167 : 0 : mr_ctrl->cur_gen = *mr_ctrl->dev_gen_ptr;
1168 : 0 : DRV_LOG(DEBUG, "mr_ctrl(%p): flushed, cur_gen=%d",
1169 : : (void *)mr_ctrl, mr_ctrl->cur_gen);
1170 : 0 : }
1171 : :
1172 : : /**
1173 : : * Creates a memory region for external memory, that is memory which is not
1174 : : * part of the DPDK memory segments.
1175 : : *
1176 : : * @param pd
1177 : : * Pointer to pd of a device (net, regex, vdpa,...).
1178 : : * @param addr
1179 : : * Starting virtual address of memory.
1180 : : * @param len
1181 : : * Length of memory segment being mapped.
1182 : : * @param socked_id
1183 : : * Socket to allocate heap memory for the control structures.
1184 : : *
1185 : : * @return
1186 : : * Pointer to MR structure on success, NULL otherwise.
1187 : : */
1188 : : struct mlx5_mr *
1189 : 0 : mlx5_create_mr_ext(void *pd, uintptr_t addr, size_t len, int socket_id)
1190 : : {
1191 : : struct mlx5_mr *mr = NULL;
1192 : :
1193 : 0 : mr = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO,
1194 : : RTE_ALIGN_CEIL(sizeof(*mr), RTE_CACHE_LINE_SIZE),
1195 : : RTE_CACHE_LINE_SIZE, socket_id);
1196 [ # # ]: 0 : if (mr == NULL)
1197 : : return NULL;
1198 : 0 : mlx5_os_reg_mr(pd, (void *)addr, len, &mr->pmd_mr);
1199 [ # # ]: 0 : if (mr->pmd_mr.obj == NULL) {
1200 : 0 : DRV_LOG(WARNING,
1201 : : "Fail to create MR for address (%p)",
1202 : : (void *)addr);
1203 : 0 : mlx5_free(mr);
1204 : 0 : return NULL;
1205 : : }
1206 : 0 : mr->msl = NULL; /* Mark it is external memory. */
1207 : 0 : mr->ms_bmp = NULL;
1208 : 0 : mr->ms_n = 1;
1209 : 0 : mr->ms_bmp_n = 1;
1210 [ # # ]: 0 : DRV_LOG(DEBUG,
1211 : : "MR CREATED (%p) for external memory %p:\n"
1212 : : " [0x%" PRIxPTR ", 0x%" PRIxPTR "),"
1213 : : " lkey=0x%x base_idx=%u ms_n=%u, ms_bmp_n=%u",
1214 : : (void *)mr, (void *)addr,
1215 : : addr, addr + len, rte_cpu_to_be_32(mr->pmd_mr.lkey),
1216 : : mr->ms_base_idx, mr->ms_n, mr->ms_bmp_n);
1217 : 0 : return mr;
1218 : : }
1219 : :
1220 : : /**
1221 : : * Callback for memory free event. Iterate freed memsegs and check whether it
1222 : : * belongs to an existing MR. If found, clear the bit from bitmap of MR. As a
1223 : : * result, the MR would be fragmented. If it becomes empty, the MR will be freed
1224 : : * later by mlx5_mr_garbage_collect(). Even if this callback is called from a
1225 : : * secondary process, the garbage collector will be called in primary process
1226 : : * as the secondary process can't call mlx5_mr_create().
1227 : : *
1228 : : * The global cache must be rebuilt if there's any change and this event has to
1229 : : * be propagated to dataplane threads to flush the local caches.
1230 : : *
1231 : : * @param share_cache
1232 : : * Pointer to a global shared MR cache.
1233 : : * @param ibdev_name
1234 : : * Name of ibv device.
1235 : : * @param addr
1236 : : * Address of freed memory.
1237 : : * @param len
1238 : : * Size of freed memory.
1239 : : */
1240 : : void
1241 : 0 : mlx5_free_mr_by_addr(struct mlx5_mr_share_cache *share_cache,
1242 : : const char *ibdev_name, const void *addr, size_t len)
1243 : : {
1244 : : const struct rte_memseg_list *msl;
1245 : : struct mlx5_mr *mr;
1246 : : int ms_n;
1247 : : int i;
1248 : : int rebuild = 0;
1249 : :
1250 : 0 : DRV_LOG(DEBUG, "device %s free callback: addr=%p, len=%zu",
1251 : : ibdev_name, addr, len);
1252 : 0 : msl = rte_mem_virt2memseg_list(addr);
1253 : : /* addr and len must be page-aligned. */
1254 : : MLX5_ASSERT((uintptr_t)addr ==
1255 : : RTE_ALIGN((uintptr_t)addr, msl->page_sz));
1256 : : MLX5_ASSERT(len == RTE_ALIGN(len, msl->page_sz));
1257 : 0 : ms_n = len / msl->page_sz;
1258 : 0 : rte_rwlock_write_lock(&share_cache->rwlock);
1259 : : /* Clear bits of freed memsegs from MR. */
1260 [ # # ]: 0 : for (i = 0; i < ms_n; ++i) {
1261 : : const struct rte_memseg *ms;
1262 : : struct mr_cache_entry entry;
1263 : : uintptr_t start;
1264 : : int ms_idx;
1265 : : uint32_t pos;
1266 : :
1267 : : /* Find MR having this memseg. */
1268 : 0 : start = (uintptr_t)addr + i * msl->page_sz;
1269 : 0 : mr = mlx5_mr_lookup_list(share_cache, &entry, start);
1270 [ # # ]: 0 : if (mr == NULL)
1271 : 0 : continue;
1272 : : MLX5_ASSERT(mr->msl); /* Can't be external memory. */
1273 : 0 : ms = rte_mem_virt2memseg((void *)start, msl);
1274 : : MLX5_ASSERT(ms != NULL);
1275 : : MLX5_ASSERT(msl->page_sz == ms->hugepage_sz);
1276 : 0 : ms_idx = rte_fbarray_find_idx(&msl->memseg_arr, ms);
1277 : 0 : pos = ms_idx - mr->ms_base_idx;
1278 : : MLX5_ASSERT(rte_bitmap_get(mr->ms_bmp, pos));
1279 : : MLX5_ASSERT(pos < mr->ms_bmp_n);
1280 : 0 : DRV_LOG(DEBUG, "device %s MR(%p): clear bitmap[%u] for addr %p",
1281 : : ibdev_name, (void *)mr, pos, (void *)start);
1282 : 0 : rte_bitmap_clear(mr->ms_bmp, pos);
1283 [ # # ]: 0 : if (--mr->ms_n == 0) {
1284 [ # # ]: 0 : LIST_REMOVE(mr, mr);
1285 [ # # ]: 0 : LIST_INSERT_HEAD(&share_cache->mr_free_list, mr, mr);
1286 : 0 : DRV_LOG(DEBUG, "device %s remove MR(%p) from list",
1287 : : ibdev_name, (void *)mr);
1288 : : }
1289 : : /*
1290 : : * MR is fragmented or will be freed. the global cache must be
1291 : : * rebuilt.
1292 : : */
1293 : : rebuild = 1;
1294 : : }
1295 [ # # ]: 0 : if (rebuild) {
1296 : 0 : mlx5_mr_rebuild_cache(share_cache);
1297 : : /*
1298 : : * No explicit wmb is needed after updating dev_gen due to
1299 : : * store-release ordering in unlock that provides the
1300 : : * implicit barrier at the software visible level.
1301 : : */
1302 : 0 : ++share_cache->dev_gen;
1303 : 0 : DRV_LOG(DEBUG, "broadcasting local cache flush, gen=%d",
1304 : : share_cache->dev_gen);
1305 : : }
1306 : : rte_rwlock_write_unlock(&share_cache->rwlock);
1307 : 0 : }
1308 : :
1309 : : /**
1310 : : * Dump all the created MRs and the global cache entries.
1311 : : *
1312 : : * @param share_cache
1313 : : * Pointer to a global shared MR cache.
1314 : : */
1315 : : void
1316 : 0 : mlx5_mr_dump_cache(struct mlx5_mr_share_cache *share_cache __rte_unused)
1317 : : {
1318 : : #ifdef RTE_PMD_MLX5_DEBUG
1319 : : struct mlx5_mr *mr;
1320 : : int mr_n = 0;
1321 : : int chunk_n = 0;
1322 : :
1323 : : rte_rwlock_read_lock(&share_cache->rwlock);
1324 : : /* Iterate all the existing MRs. */
1325 : : LIST_FOREACH(mr, &share_cache->mr_list, mr) {
1326 : : unsigned int n;
1327 : :
1328 : : DRV_LOG(DEBUG, "MR[%u], LKey = 0x%x, ms_n = %u, ms_bmp_n = %u",
1329 : : mr_n++, rte_cpu_to_be_32(mr->pmd_mr.lkey),
1330 : : mr->ms_n, mr->ms_bmp_n);
1331 : : if (mr->ms_n == 0)
1332 : : continue;
1333 : : for (n = 0; n < mr->ms_bmp_n; ) {
1334 : : struct mr_cache_entry ret = { 0, };
1335 : :
1336 : : n = mr_find_next_chunk(mr, &ret, n);
1337 : : if (!ret.end)
1338 : : break;
1339 : : DRV_LOG(DEBUG,
1340 : : " chunk[%u], [0x%" PRIxPTR ", 0x%" PRIxPTR ")",
1341 : : chunk_n++, ret.start, ret.end);
1342 : : }
1343 : : }
1344 : : DRV_LOG(DEBUG, "Dumping global cache %p", (void *)share_cache);
1345 : : mlx5_mr_btree_dump(&share_cache->cache);
1346 : : rte_rwlock_read_unlock(&share_cache->rwlock);
1347 : : #endif
1348 : 0 : }
1349 : :
1350 : : static int
1351 : 0 : mlx5_range_compare_start(const void *lhs, const void *rhs)
1352 : : {
1353 : : const struct mlx5_range *r1 = lhs, *r2 = rhs;
1354 : :
1355 [ # # ]: 0 : if (r1->start > r2->start)
1356 : : return 1;
1357 [ # # ]: 0 : else if (r1->start < r2->start)
1358 : 0 : return -1;
1359 : : return 0;
1360 : : }
1361 : :
1362 : : static void
1363 : 0 : mlx5_range_from_mempool_chunk(struct rte_mempool *mp, void *opaque,
1364 : : struct rte_mempool_memhdr *memhdr,
1365 : : unsigned int idx)
1366 : : {
1367 : 0 : struct mlx5_range *ranges = opaque, *range = &ranges[idx];
1368 : 0 : uintptr_t start = (uintptr_t)memhdr->addr;
1369 : 0 : uint64_t page_size = rte_mem_page_size();
1370 : :
1371 : : RTE_SET_USED(mp);
1372 : 0 : range->start = RTE_ALIGN_FLOOR(start, page_size);
1373 : 0 : range->end = RTE_ALIGN_CEIL(start + memhdr->len, page_size);
1374 : 0 : }
1375 : :
1376 : : /**
1377 : : * Collect page-aligned memory ranges of the mempool.
1378 : : */
1379 : : static int
1380 : 0 : mlx5_mempool_get_chunks(struct rte_mempool *mp, struct mlx5_range **out,
1381 : : unsigned int *out_n)
1382 : : {
1383 : : unsigned int n;
1384 : :
1385 : 0 : DRV_LOG(DEBUG, "Collecting chunks of regular mempool %s", mp->name);
1386 : 0 : n = mp->nb_mem_chunks;
1387 : 0 : *out = calloc(n, sizeof(**out));
1388 [ # # ]: 0 : if (*out == NULL)
1389 : : return -1;
1390 : 0 : rte_mempool_mem_iter(mp, mlx5_range_from_mempool_chunk, *out);
1391 : 0 : *out_n = n;
1392 : 0 : return 0;
1393 : : }
1394 : :
1395 : : struct mlx5_mempool_get_extmem_data {
1396 : : struct mlx5_range *heap;
1397 : : unsigned int heap_size;
1398 : : int ret;
1399 : : };
1400 : :
1401 : : static void
1402 : 0 : mlx5_mempool_get_extmem_cb(struct rte_mempool *mp, void *opaque,
1403 : : void *obj, unsigned int obj_idx)
1404 : : {
1405 : : struct mlx5_mempool_get_extmem_data *data = opaque;
1406 : : struct rte_mbuf *mbuf = obj;
1407 : 0 : uintptr_t addr = (uintptr_t)mbuf->buf_addr;
1408 : : struct mlx5_range *seg, *heap;
1409 : : struct rte_memseg_list *msl;
1410 : : size_t page_size;
1411 : : uintptr_t page_start;
1412 : 0 : unsigned int pos = 0, len = data->heap_size, delta;
1413 : :
1414 : : RTE_SET_USED(mp);
1415 : : RTE_SET_USED(obj_idx);
1416 [ # # ]: 0 : if (data->ret < 0)
1417 : : return;
1418 : : /* Binary search for an already visited page. */
1419 [ # # ]: 0 : while (len > 1) {
1420 : 0 : delta = len / 2;
1421 [ # # ]: 0 : if (addr < data->heap[pos + delta].start) {
1422 : : len = delta;
1423 : : } else {
1424 : : pos += delta;
1425 : 0 : len -= delta;
1426 : : }
1427 : : }
1428 [ # # ]: 0 : if (data->heap != NULL) {
1429 : 0 : seg = &data->heap[pos];
1430 [ # # # # ]: 0 : if (seg->start <= addr && addr < seg->end)
1431 : : return;
1432 : : }
1433 : : /* Determine the page boundaries and remember them. */
1434 : 0 : heap = realloc(data->heap, sizeof(heap[0]) * (data->heap_size + 1));
1435 [ # # ]: 0 : if (heap == NULL) {
1436 : 0 : free(data->heap);
1437 : 0 : data->heap = NULL;
1438 : 0 : data->ret = -1;
1439 : 0 : return;
1440 : : }
1441 : 0 : data->heap = heap;
1442 : 0 : data->heap_size++;
1443 : 0 : seg = &heap[data->heap_size - 1];
1444 : 0 : msl = rte_mem_virt2memseg_list((void *)addr);
1445 [ # # ]: 0 : page_size = msl != NULL ? msl->page_sz : rte_mem_page_size();
1446 : 0 : page_start = RTE_ALIGN_FLOOR(addr, page_size);
1447 : 0 : seg->start = page_start;
1448 : 0 : seg->end = page_start + page_size;
1449 : : /* Maintain the heap order. */
1450 : 0 : qsort(data->heap, data->heap_size, sizeof(heap[0]),
1451 : : mlx5_range_compare_start);
1452 : : }
1453 : :
1454 : : /**
1455 : : * Recover pages of external memory as close as possible
1456 : : * for a mempool with RTE_PKTMBUF_POOL_PINNED_EXT_BUF.
1457 : : * Pages are stored in a heap for efficient search, for mbufs are many.
1458 : : */
1459 : : static int
1460 : 0 : mlx5_mempool_get_extmem(struct rte_mempool *mp, struct mlx5_range **out,
1461 : : unsigned int *out_n)
1462 : : {
1463 : : struct mlx5_mempool_get_extmem_data data;
1464 : :
1465 : 0 : DRV_LOG(DEBUG, "Recovering external pinned pages of mempool %s",
1466 : : mp->name);
1467 : : memset(&data, 0, sizeof(data));
1468 : 0 : rte_mempool_obj_iter(mp, mlx5_mempool_get_extmem_cb, &data);
1469 : 0 : *out = data.heap;
1470 : 0 : *out_n = data.heap_size;
1471 : 0 : return data.ret;
1472 : : }
1473 : :
1474 : : /**
1475 : : * Get VA-contiguous ranges of the mempool memory.
1476 : : * Each range start and end is aligned to the system page size.
1477 : : *
1478 : : * @param[in] mp
1479 : : * Analyzed mempool.
1480 : : * @param[in] is_extmem
1481 : : * Whether the pool is contains only external pinned buffers.
1482 : : * @param[out] out
1483 : : * Receives the ranges, caller must release it with free().
1484 : : * @param[out] out_n
1485 : : * Receives the number of @p out elements.
1486 : : *
1487 : : * @return
1488 : : * 0 on success, (-1) on failure.
1489 : : */
1490 : : static int
1491 : 0 : mlx5_get_mempool_ranges(struct rte_mempool *mp, bool is_extmem,
1492 : : struct mlx5_range **out, unsigned int *out_n)
1493 : : {
1494 : : struct mlx5_range *chunks;
1495 : : unsigned int chunks_n, contig_n, i;
1496 : : int ret;
1497 : :
1498 : : /* Collect the pool underlying memory. */
1499 [ # # ]: 0 : ret = is_extmem ? mlx5_mempool_get_extmem(mp, &chunks, &chunks_n) :
1500 : 0 : mlx5_mempool_get_chunks(mp, &chunks, &chunks_n);
1501 [ # # ]: 0 : if (ret < 0)
1502 : : return ret;
1503 : : /* Merge adjacent chunks and place them at the beginning. */
1504 : 0 : qsort(chunks, chunks_n, sizeof(chunks[0]), mlx5_range_compare_start);
1505 : : contig_n = 1;
1506 [ # # ]: 0 : for (i = 1; i < chunks_n; i++)
1507 [ # # ]: 0 : if (chunks[i - 1].end < chunks[i].start) {
1508 : 0 : chunks[contig_n - 1].end = chunks[i - 1].end;
1509 : 0 : chunks[contig_n] = chunks[i];
1510 : 0 : contig_n++;
1511 : : }
1512 : : /* Extend the last contiguous chunk to the end of the mempool. */
1513 : 0 : chunks[contig_n - 1].end = chunks[i - 1].end;
1514 : 0 : *out = chunks;
1515 : 0 : *out_n = contig_n;
1516 : 0 : return 0;
1517 : : }
1518 : :
1519 : : /**
1520 : : * Analyze mempool memory to select memory ranges to register.
1521 : : *
1522 : : * @param[in] mp
1523 : : * Mempool to analyze.
1524 : : * @param[in] is_extmem
1525 : : * Whether the pool is contains only external pinned buffers.
1526 : : * @param[out] out
1527 : : * Receives memory ranges to register. For non-external memory, ranges
1528 : : * are aligned to the hugepage size if all ranges are on hugepages
1529 : : * of the same size, otherwise aligned to the system page size.
1530 : : * For external memory, if the entire pool resides within a single
1531 : : * hugepage, all ranges are combined into one of the hugepage size.
1532 : : * The caller must release them with free().
1533 : : * @param[out] out_n
1534 : : * Receives the number of @p out items.
1535 : : * @param[out] share_hugepage
1536 : : * Receives True if the entire pool resides within a single hugepage.
1537 : : *
1538 : : * @return
1539 : : * 0 on success, (-1) on failure.
1540 : : */
1541 : : static int
1542 : 0 : mlx5_mempool_reg_analyze(struct rte_mempool *mp, bool is_extmem,
1543 : : struct mlx5_range **out, unsigned int *out_n,
1544 : : bool *share_hugepage)
1545 : : {
1546 : 0 : struct mlx5_range *ranges = NULL;
1547 : 0 : unsigned int i, ranges_n = 0;
1548 : : uint64_t hugepage_sz = 0;
1549 : : bool same_hugepage_sz = true;
1550 : :
1551 [ # # ]: 0 : if (mlx5_get_mempool_ranges(mp, is_extmem, &ranges, &ranges_n) < 0) {
1552 : 0 : DRV_LOG(ERR, "Cannot get address ranges for mempool %s",
1553 : : mp->name);
1554 : 0 : return -1;
1555 : : }
1556 : : /* Check if the hugepage of the pool can be shared. */
1557 : 0 : *share_hugepage = false;
1558 : : /* Check that all ranges are on pages of the same size. */
1559 [ # # ]: 0 : for (i = 0; i < ranges_n; i++) {
1560 : : struct rte_memseg_list *range_msl;
1561 : 0 : range_msl = rte_mem_virt2memseg_list((void *)ranges[i].start);
1562 [ # # ]: 0 : if (range_msl == NULL) {
1563 : : same_hugepage_sz = false;
1564 : : break;
1565 : : }
1566 [ # # ]: 0 : if (hugepage_sz == 0)
1567 : 0 : hugepage_sz = range_msl->page_sz;
1568 [ # # ]: 0 : else if (range_msl->page_sz != hugepage_sz) {
1569 : : same_hugepage_sz = false;
1570 : : break;
1571 : : }
1572 : : }
1573 : : /*
1574 : : * For non-external memory, align ranges to the hugepage boundaries
1575 : : * and merge adjacent ones. If the entire pool fits in a single
1576 : : * hugepage, the MR for this hugepage can be shared across mempools.
1577 : : */
1578 [ # # # # ]: 0 : if (!is_extmem && same_hugepage_sz &&
1579 : 0 : hugepage_sz > rte_mem_page_size()) {
1580 : : unsigned int orig_ranges_n = ranges_n;
1581 : :
1582 [ # # ]: 0 : for (i = 0; i < ranges_n; i++) {
1583 : 0 : ranges[i].start = RTE_ALIGN_FLOOR(ranges[i].start,
1584 : : hugepage_sz);
1585 : 0 : ranges[i].end = RTE_ALIGN_CEIL(ranges[i].end,
1586 : : hugepage_sz);
1587 : : }
1588 : 0 : ranges_n = 1;
1589 [ # # ]: 0 : for (i = 1; i < orig_ranges_n; i++) {
1590 [ # # ]: 0 : if (ranges[ranges_n - 1].end >= ranges[i].start)
1591 : 0 : ranges[ranges_n - 1].end =
1592 : 0 : RTE_MAX(ranges[ranges_n - 1].end,
1593 : : ranges[i].end);
1594 : : else
1595 : 0 : ranges[ranges_n++] = ranges[i];
1596 : : }
1597 [ # # ]: 0 : if (ranges_n == 1) {
1598 : 0 : uintptr_t hugepage_end = ranges[0].start + hugepage_sz;
1599 : :
1600 [ # # ]: 0 : if (ranges[0].end <= hugepage_end) {
1601 : 0 : ranges[0].end = hugepage_end;
1602 : 0 : *share_hugepage = true;
1603 : : }
1604 : : }
1605 [ # # # # ]: 0 : } else if (is_extmem && same_hugepage_sz && hugepage_sz > 0) {
1606 : : /*
1607 : : * If the entire pool is within one hugepage,
1608 : : * combine all ranges into one of the hugepage size.
1609 : : */
1610 : 0 : uintptr_t reg_start = ranges[0].start;
1611 : 0 : uintptr_t reg_end = ranges[ranges_n - 1].end;
1612 : 0 : uintptr_t hugepage_start =
1613 : 0 : RTE_ALIGN_FLOOR(reg_start, hugepage_sz);
1614 : 0 : uintptr_t hugepage_end = hugepage_start + hugepage_sz;
1615 [ # # ]: 0 : if (reg_end < hugepage_end) {
1616 : 0 : ranges[0].start = hugepage_start;
1617 : 0 : ranges[0].end = hugepage_end;
1618 : 0 : ranges_n = 1;
1619 : 0 : *share_hugepage = true;
1620 : : }
1621 : : }
1622 : 0 : *out = ranges;
1623 : 0 : *out_n = ranges_n;
1624 : 0 : return 0;
1625 : : }
1626 : :
1627 : : /** Create a registration object for the mempool. */
1628 : : static struct mlx5_mempool_reg *
1629 : 0 : mlx5_mempool_reg_create(struct rte_mempool *mp, unsigned int mrs_n,
1630 : : bool is_extmem)
1631 : : {
1632 : : struct mlx5_mempool_reg *mpr = NULL;
1633 : :
1634 : 0 : mpr = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO,
1635 : : sizeof(struct mlx5_mempool_reg),
1636 : : RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
1637 [ # # ]: 0 : if (mpr == NULL) {
1638 : 0 : DRV_LOG(ERR, "Cannot allocate mempool %s registration object",
1639 : : mp->name);
1640 : 0 : return NULL;
1641 : : }
1642 : 0 : mpr->mrs = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO,
1643 : : mrs_n * sizeof(struct mlx5_mempool_mr),
1644 : : RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
1645 [ # # ]: 0 : if (!mpr->mrs) {
1646 : 0 : DRV_LOG(ERR, "Cannot allocate mempool %s registration MRs",
1647 : : mp->name);
1648 : 0 : mlx5_free(mpr);
1649 : 0 : return NULL;
1650 : : }
1651 : 0 : mpr->mp = mp;
1652 : 0 : mpr->mrs_n = mrs_n;
1653 : 0 : mpr->is_extmem = is_extmem;
1654 : 0 : return mpr;
1655 : : }
1656 : :
1657 : : /**
1658 : : * Destroy a mempool registration object.
1659 : : *
1660 : : * @param standalone
1661 : : * Whether @p mpr owns its MRs exclusively, i.e. they are not shared.
1662 : : */
1663 : : static void
1664 : 0 : mlx5_mempool_reg_destroy(struct mlx5_mempool_reg *mpr, bool standalone)
1665 : : {
1666 [ # # ]: 0 : if (standalone) {
1667 : : unsigned int i;
1668 : :
1669 [ # # ]: 0 : for (i = 0; i < mpr->mrs_n; i++)
1670 : 0 : mlx5_os_dereg_mr(&mpr->mrs[i].pmd_mr);
1671 : 0 : mlx5_free(mpr->mrs);
1672 : : }
1673 : 0 : mlx5_free(mpr);
1674 : 0 : }
1675 : :
1676 : : /** Find registration object of a mempool. */
1677 : : static struct mlx5_mempool_reg *
1678 : : mlx5_mempool_reg_lookup(struct mlx5_mr_share_cache *share_cache,
1679 : : struct rte_mempool *mp)
1680 : : {
1681 : : struct mlx5_mempool_reg *mpr;
1682 : :
1683 [ # # # # : 0 : LIST_FOREACH(mpr, &share_cache->mempool_reg_list, next)
# # # # ]
1684 [ # # # # : 0 : if (mpr->mp == mp)
# # # # ]
1685 : : break;
1686 : : return mpr;
1687 : : }
1688 : :
1689 : : /** Increment reference counters of MRs used in the registration. */
1690 : : static void
1691 : 0 : mlx5_mempool_reg_attach(struct mlx5_mempool_reg *mpr)
1692 : : {
1693 : : unsigned int i;
1694 : :
1695 [ # # ]: 0 : for (i = 0; i < mpr->mrs_n; i++)
1696 : 0 : rte_atomic_fetch_add_explicit(&mpr->mrs[i].refcnt, 1, rte_memory_order_relaxed);
1697 : 0 : }
1698 : :
1699 : : /**
1700 : : * Decrement reference counters of MRs used in the registration.
1701 : : *
1702 : : * @return True if no more references to @p mpr MRs exist, False otherwise.
1703 : : */
1704 : : static bool
1705 : 0 : mlx5_mempool_reg_detach(struct mlx5_mempool_reg *mpr)
1706 : : {
1707 : : unsigned int i;
1708 : : bool ret = false;
1709 : :
1710 [ # # ]: 0 : for (i = 0; i < mpr->mrs_n; i++)
1711 : 0 : ret |= rte_atomic_fetch_sub_explicit(&mpr->mrs[i].refcnt, 1,
1712 : 0 : rte_memory_order_relaxed) - 1 == 0;
1713 : 0 : return ret;
1714 : : }
1715 : :
1716 : : static int
1717 : 0 : mlx5_mr_mempool_register_primary(struct mlx5_mr_share_cache *share_cache,
1718 : : void *pd, struct rte_mempool *mp,
1719 : : bool is_extmem)
1720 : : {
1721 : 0 : struct mlx5_range *ranges = NULL;
1722 : : struct mlx5_mempool_reg *mpr, *old_mpr, *new_mpr;
1723 : : unsigned int i, ranges_n;
1724 : : bool share_hugepage, standalone = false;
1725 : : int ret = -1;
1726 : :
1727 : : /* Early check to avoid unnecessary creation of MRs. */
1728 : 0 : rte_rwlock_read_lock(&share_cache->rwlock);
1729 : : old_mpr = mlx5_mempool_reg_lookup(share_cache, mp);
1730 : : rte_rwlock_read_unlock(&share_cache->rwlock);
1731 [ # # # # : 0 : if (old_mpr != NULL && (!is_extmem || old_mpr->is_extmem)) {
# # ]
1732 : 0 : DRV_LOG(DEBUG, "Mempool %s is already registered for PD %p",
1733 : : mp->name, pd);
1734 : 0 : rte_errno = EEXIST;
1735 : 0 : goto exit;
1736 : : }
1737 [ # # ]: 0 : if (mlx5_mempool_reg_analyze(mp, is_extmem, &ranges, &ranges_n,
1738 : : &share_hugepage) < 0) {
1739 : 0 : DRV_LOG(ERR, "Cannot get mempool %s memory ranges", mp->name);
1740 : 0 : rte_errno = ENOMEM;
1741 : 0 : goto exit;
1742 : : }
1743 : 0 : new_mpr = mlx5_mempool_reg_create(mp, ranges_n, is_extmem);
1744 [ # # ]: 0 : if (new_mpr == NULL) {
1745 : 0 : DRV_LOG(ERR,
1746 : : "Cannot create a registration object for mempool %s in PD %p",
1747 : : mp->name, pd);
1748 : 0 : rte_errno = ENOMEM;
1749 : 0 : goto exit;
1750 : : }
1751 : : /*
1752 : : * If the entire mempool fits in a single hugepage, the MR for this
1753 : : * hugepage can be shared across mempools that also fit in it.
1754 : : */
1755 [ # # ]: 0 : if (share_hugepage) {
1756 : : struct mlx5_mempool_mr *gc_mrs = NULL;
1757 : :
1758 : 0 : rte_rwlock_write_lock(&share_cache->rwlock);
1759 [ # # ]: 0 : LIST_FOREACH(mpr, &share_cache->mempool_reg_list, next) {
1760 [ # # ]: 0 : if (mpr->mrs[0].pmd_mr.addr == (void *)ranges[0].start)
1761 : : break;
1762 : : }
1763 [ # # ]: 0 : if (mpr != NULL) {
1764 : : /* Releasing MRs here can create a dead-lock on share_cache->rwlock */
1765 : 0 : gc_mrs = new_mpr->mrs;
1766 : 0 : new_mpr->mrs = mpr->mrs;
1767 : 0 : mlx5_mempool_reg_attach(new_mpr);
1768 [ # # ]: 0 : LIST_INSERT_HEAD(&share_cache->mempool_reg_list,
1769 : : new_mpr, next);
1770 : : }
1771 : : rte_rwlock_write_unlock(&share_cache->rwlock);
1772 [ # # ]: 0 : if (gc_mrs != NULL)
1773 : 0 : mlx5_free(gc_mrs);
1774 [ # # ]: 0 : if (mpr != NULL) {
1775 : 0 : DRV_LOG(DEBUG, "Shared MR %#x in PD %p for mempool %s with mempool %s",
1776 : : mpr->mrs[0].pmd_mr.lkey, pd, mp->name,
1777 : : mpr->mp->name);
1778 : : ret = 0;
1779 : 0 : goto exit;
1780 : : }
1781 : : }
1782 [ # # ]: 0 : for (i = 0; i < ranges_n; i++) {
1783 : 0 : struct mlx5_mempool_mr *mr = &new_mpr->mrs[i];
1784 : 0 : const struct mlx5_range *range = &ranges[i];
1785 : 0 : size_t len = range->end - range->start;
1786 : :
1787 [ # # ]: 0 : if (mlx5_os_reg_mr(pd, (void *)range->start, len,
1788 : : &mr->pmd_mr) < 0) {
1789 : 0 : DRV_LOG(ERR,
1790 : : "Failed to create an MR in PD %p for address range "
1791 : : "[0x%" PRIxPTR ", 0x%" PRIxPTR "] (%zu bytes) for mempool %s",
1792 : : pd, range->start, range->end, len, mp->name);
1793 : 0 : break;
1794 : : }
1795 : 0 : DRV_LOG(DEBUG,
1796 : : "Created a new MR %#x in PD %p for address range "
1797 : : "[0x%" PRIxPTR ", 0x%" PRIxPTR "] (%zu bytes) for mempool %s",
1798 : : mr->pmd_mr.lkey, pd, range->start, range->end, len,
1799 : : mp->name);
1800 : : }
1801 [ # # ]: 0 : if (i != ranges_n) {
1802 : 0 : mlx5_mempool_reg_destroy(new_mpr, true);
1803 : 0 : rte_errno = EINVAL;
1804 : 0 : goto exit;
1805 : : }
1806 : : /* Concurrent registration is not supposed to happen. */
1807 : 0 : rte_rwlock_write_lock(&share_cache->rwlock);
1808 : : mpr = mlx5_mempool_reg_lookup(share_cache, mp);
1809 [ # # ]: 0 : if (mpr == old_mpr && old_mpr != NULL) {
1810 [ # # ]: 0 : LIST_REMOVE(old_mpr, next);
1811 : 0 : standalone = mlx5_mempool_reg_detach(mpr);
1812 : : /* No need to flush the cache: old MRs cannot be in use. */
1813 : : mpr = NULL;
1814 : : }
1815 [ # # ]: 0 : if (mpr == NULL) {
1816 : 0 : mlx5_mempool_reg_attach(new_mpr);
1817 [ # # ]: 0 : LIST_INSERT_HEAD(&share_cache->mempool_reg_list, new_mpr, next);
1818 : : ret = 0;
1819 : : }
1820 : : rte_rwlock_write_unlock(&share_cache->rwlock);
1821 [ # # ]: 0 : if (mpr != NULL) {
1822 : 0 : DRV_LOG(DEBUG, "Mempool %s is already registered for PD %p",
1823 : : mp->name, pd);
1824 : 0 : mlx5_mempool_reg_destroy(new_mpr, true);
1825 : 0 : rte_errno = EEXIST;
1826 : 0 : goto exit;
1827 [ # # ]: 0 : } else if (old_mpr != NULL) {
1828 : 0 : DRV_LOG(DEBUG, "Mempool %s registration for PD %p updated for external memory",
1829 : : mp->name, pd);
1830 : 0 : mlx5_mempool_reg_destroy(old_mpr, standalone);
1831 : : }
1832 : 0 : exit:
1833 : 0 : free(ranges);
1834 : 0 : return ret;
1835 : : }
1836 : :
1837 : : static int
1838 : : mlx5_mr_mempool_register_secondary(struct mlx5_common_device *cdev,
1839 : : struct rte_mempool *mp, bool is_extmem)
1840 : : {
1841 : 0 : return mlx5_mp_req_mempool_reg(cdev, mp, true, is_extmem);
1842 : : }
1843 : :
1844 : : /**
1845 : : * Register the memory of a mempool in the protection domain.
1846 : : *
1847 : : * @param cdev
1848 : : * Pointer to the mlx5 common device.
1849 : : * @param mp
1850 : : * Mempool to register.
1851 : : *
1852 : : * @return
1853 : : * 0 on success, (-1) on failure and rte_errno is set.
1854 : : */
1855 : : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_mr_mempool_register)
1856 : : int
1857 : 0 : mlx5_mr_mempool_register(struct mlx5_common_device *cdev,
1858 : : struct rte_mempool *mp, bool is_extmem)
1859 : : {
1860 [ # # ]: 0 : if (mp->flags & RTE_MEMPOOL_F_NON_IO)
1861 : : return 0;
1862 [ # # # ]: 0 : switch (rte_eal_process_type()) {
1863 : 0 : case RTE_PROC_PRIMARY:
1864 : 0 : return mlx5_mr_mempool_register_primary(&cdev->mr_scache,
1865 : : cdev->pd, mp,
1866 : : is_extmem);
1867 : 0 : case RTE_PROC_SECONDARY:
1868 : 0 : return mlx5_mr_mempool_register_secondary(cdev, mp, is_extmem);
1869 : : default:
1870 : : return -1;
1871 : : }
1872 : : }
1873 : :
1874 : : static int
1875 : 0 : mlx5_mr_mempool_unregister_primary(struct mlx5_mr_share_cache *share_cache,
1876 : : struct rte_mempool *mp)
1877 : : {
1878 : : struct mlx5_mempool_reg *mpr;
1879 : : bool standalone = false;
1880 : :
1881 : 0 : rte_rwlock_write_lock(&share_cache->rwlock);
1882 [ # # ]: 0 : LIST_FOREACH(mpr, &share_cache->mempool_reg_list, next)
1883 [ # # ]: 0 : if (mpr->mp == mp) {
1884 [ # # ]: 0 : LIST_REMOVE(mpr, next);
1885 : 0 : standalone = mlx5_mempool_reg_detach(mpr);
1886 [ # # ]: 0 : if (standalone)
1887 : : /*
1888 : : * The unlock operation below provides a memory
1889 : : * barrier due to its store-release semantics.
1890 : : */
1891 : 0 : ++share_cache->dev_gen;
1892 : : break;
1893 : : }
1894 : : rte_rwlock_write_unlock(&share_cache->rwlock);
1895 [ # # ]: 0 : if (mpr == NULL) {
1896 : 0 : rte_errno = ENOENT;
1897 : 0 : return -1;
1898 : : }
1899 : 0 : mlx5_mempool_reg_destroy(mpr, standalone);
1900 : 0 : return 0;
1901 : : }
1902 : :
1903 : : static int
1904 : : mlx5_mr_mempool_unregister_secondary(struct mlx5_common_device *cdev,
1905 : : struct rte_mempool *mp)
1906 : : {
1907 : 0 : return mlx5_mp_req_mempool_reg(cdev, mp, false, false /* is_extmem */);
1908 : : }
1909 : :
1910 : : /**
1911 : : * Unregister the memory of a mempool from the protection domain.
1912 : : *
1913 : : * @param cdev
1914 : : * Pointer to the mlx5 common device.
1915 : : * @param mp
1916 : : * Mempool to unregister.
1917 : : *
1918 : : * @return
1919 : : * 0 on success, (-1) on failure and rte_errno is set.
1920 : : */
1921 : : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_mr_mempool_unregister)
1922 : : int
1923 : 0 : mlx5_mr_mempool_unregister(struct mlx5_common_device *cdev,
1924 : : struct rte_mempool *mp)
1925 : : {
1926 [ # # ]: 0 : if (mp->flags & RTE_MEMPOOL_F_NON_IO)
1927 : : return 0;
1928 [ # # # ]: 0 : switch (rte_eal_process_type()) {
1929 : 0 : case RTE_PROC_PRIMARY:
1930 : 0 : return mlx5_mr_mempool_unregister_primary(&cdev->mr_scache, mp);
1931 : : case RTE_PROC_SECONDARY:
1932 : 0 : return mlx5_mr_mempool_unregister_secondary(cdev, mp);
1933 : : default:
1934 : : return -1;
1935 : : }
1936 : : }
1937 : :
1938 : : /**
1939 : : * Lookup a MR key by and address in a registered mempool.
1940 : : *
1941 : : * @param mpr
1942 : : * Mempool registration object.
1943 : : * @param addr
1944 : : * Address within the mempool.
1945 : : * @param entry
1946 : : * Bottom-half cache entry to fill.
1947 : : *
1948 : : * @return
1949 : : * MR key or UINT32_MAX on failure, which can only happen
1950 : : * if the address is not from within the mempool.
1951 : : */
1952 : : static uint32_t
1953 : 0 : mlx5_mempool_reg_addr2mr(struct mlx5_mempool_reg *mpr, uintptr_t addr,
1954 : : struct mr_cache_entry *entry)
1955 : : {
1956 : : uint32_t lkey = UINT32_MAX;
1957 : : unsigned int i;
1958 : :
1959 [ # # ]: 0 : for (i = 0; i < mpr->mrs_n; i++) {
1960 : 0 : const struct mlx5_pmd_mr *mr = &mpr->mrs[i].pmd_mr;
1961 : 0 : uintptr_t mr_start = (uintptr_t)mr->addr;
1962 : 0 : uintptr_t mr_end = mr_start + mr->len;
1963 : :
1964 [ # # ]: 0 : if (mr_start <= addr && addr < mr_end) {
1965 [ # # ]: 0 : lkey = rte_cpu_to_be_32(mr->lkey);
1966 : 0 : entry->start = mr_start;
1967 : 0 : entry->end = mr_end;
1968 : 0 : entry->lkey = lkey;
1969 : 0 : break;
1970 : : }
1971 : : }
1972 : 0 : return lkey;
1973 : : }
1974 : :
1975 : : /**
1976 : : * Update bottom-half cache from the list of mempool registrations.
1977 : : *
1978 : : * @param mr_ctrl
1979 : : * Per-queue MR control handle.
1980 : : * @param entry
1981 : : * Pointer to an entry in the bottom-half cache to update
1982 : : * with the MR lkey looked up.
1983 : : * @param mp
1984 : : * Mempool containing the address.
1985 : : * @param addr
1986 : : * Address to lookup.
1987 : : * @return
1988 : : * MR lkey on success, UINT32_MAX on failure.
1989 : : */
1990 : : static uint32_t
1991 : 0 : mlx5_lookup_mempool_regs(struct mlx5_mr_ctrl *mr_ctrl,
1992 : : struct mr_cache_entry *entry,
1993 : : struct rte_mempool *mp, uintptr_t addr)
1994 : : {
1995 : : struct mlx5_mr_share_cache *share_cache =
1996 : 0 : container_of(mr_ctrl->dev_gen_ptr, struct mlx5_mr_share_cache,
1997 : : dev_gen);
1998 : 0 : struct mlx5_mr_btree *bt = &mr_ctrl->cache_bh;
1999 : : struct mlx5_mempool_reg *mpr;
2000 : : uint32_t lkey = UINT32_MAX;
2001 : :
2002 : : /* If local cache table is full, try to double it. */
2003 [ # # ]: 0 : if (unlikely(bt->len == bt->size))
2004 : 0 : mr_btree_expand(bt, bt->size << 1);
2005 : : /* Look up in mempool registrations. */
2006 : 0 : rte_rwlock_read_lock(&share_cache->rwlock);
2007 : : mpr = mlx5_mempool_reg_lookup(share_cache, mp);
2008 [ # # ]: 0 : if (mpr != NULL)
2009 : 0 : lkey = mlx5_mempool_reg_addr2mr(mpr, addr, entry);
2010 : : rte_rwlock_read_unlock(&share_cache->rwlock);
2011 : : /*
2012 : : * Update local cache. Even if it fails, return the found entry
2013 : : * to update top-half cache. Next time, this entry will be found
2014 : : * in the global cache.
2015 : : */
2016 [ # # ]: 0 : if (lkey != UINT32_MAX)
2017 : 0 : mr_btree_insert(bt, entry);
2018 : 0 : return lkey;
2019 : : }
2020 : :
2021 : : /**
2022 : : * Populate cache with LKeys of all MRs used by the mempool.
2023 : : * It is intended to be used to register Rx mempools in advance.
2024 : : *
2025 : : * @param mr_ctrl
2026 : : * Per-queue MR control handle.
2027 : : * @param mp
2028 : : * Registered memory pool.
2029 : : *
2030 : : * @return
2031 : : * 0 on success, (-1) on failure and rte_errno is set.
2032 : : */
2033 : : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_mr_mempool_populate_cache)
2034 : : int
2035 : 0 : mlx5_mr_mempool_populate_cache(struct mlx5_mr_ctrl *mr_ctrl,
2036 : : struct rte_mempool *mp)
2037 : : {
2038 : : struct mlx5_mr_share_cache *share_cache =
2039 : 0 : container_of(mr_ctrl->dev_gen_ptr, struct mlx5_mr_share_cache,
2040 : : dev_gen);
2041 : 0 : struct mlx5_mr_btree *bt = &mr_ctrl->cache_bh;
2042 : : struct mlx5_mempool_reg *mpr;
2043 : : unsigned int i;
2044 : :
2045 : : /*
2046 : : * Registration is valid after the lock is released,
2047 : : * because the function is called after the mempool is registered.
2048 : : */
2049 : 0 : rte_rwlock_read_lock(&share_cache->rwlock);
2050 : : mpr = mlx5_mempool_reg_lookup(share_cache, mp);
2051 : : rte_rwlock_read_unlock(&share_cache->rwlock);
2052 [ # # ]: 0 : if (mpr == NULL) {
2053 : 0 : DRV_LOG(ERR, "Mempool %s is not registered", mp->name);
2054 : 0 : rte_errno = ENOENT;
2055 : 0 : return -1;
2056 : : }
2057 [ # # ]: 0 : for (i = 0; i < mpr->mrs_n; i++) {
2058 : 0 : struct mlx5_mempool_mr *mr = &mpr->mrs[i];
2059 : : struct mr_cache_entry entry;
2060 : : uint32_t lkey;
2061 : : uint32_t idx;
2062 : :
2063 : 0 : lkey = mr_btree_lookup(bt, &idx, (uintptr_t)mr->pmd_mr.addr);
2064 [ # # ]: 0 : if (lkey != UINT32_MAX)
2065 : 0 : continue;
2066 [ # # ]: 0 : if (bt->len == bt->size)
2067 : 0 : mr_btree_expand(bt, bt->size << 1);
2068 : 0 : entry.start = (uintptr_t)mr->pmd_mr.addr;
2069 : 0 : entry.end = entry.start + mr->pmd_mr.len;
2070 [ # # ]: 0 : entry.lkey = rte_cpu_to_be_32(mr->pmd_mr.lkey);
2071 [ # # ]: 0 : if (mr_btree_insert(bt, &entry) < 0) {
2072 : 0 : DRV_LOG(ERR, "Cannot insert cache entry for mempool %s MR %08x",
2073 : : mp->name, entry.lkey);
2074 : 0 : rte_errno = EINVAL;
2075 : 0 : return -1;
2076 : : }
2077 : : }
2078 : : return 0;
2079 : : }
2080 : :
2081 : : /**
2082 : : * Bottom-half lookup for the address from the mempool.
2083 : : *
2084 : : * @param mr_ctrl
2085 : : * Per-queue MR control handle.
2086 : : * @param mp
2087 : : * Mempool containing the address.
2088 : : * @param addr
2089 : : * Address to lookup.
2090 : : * @return
2091 : : * MR lkey on success, UINT32_MAX on failure.
2092 : : */
2093 : : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_mr_mempool2mr_bh)
2094 : : uint32_t
2095 : 0 : mlx5_mr_mempool2mr_bh(struct mlx5_mr_ctrl *mr_ctrl,
2096 : : struct rte_mempool *mp, uintptr_t addr)
2097 : : {
2098 : 0 : struct mr_cache_entry *repl = &mr_ctrl->cache[mr_ctrl->head];
2099 : : uint32_t lkey;
2100 : : uint32_t bh_idx = 0;
2101 : :
2102 : : /* Binary-search MR translation table. */
2103 : : lkey = mr_btree_lookup(&mr_ctrl->cache_bh, &bh_idx, addr);
2104 : : /* Update top-half cache. */
2105 [ # # ]: 0 : if (likely(lkey != UINT32_MAX)) {
2106 : 0 : *repl = (*mr_ctrl->cache_bh.table)[bh_idx];
2107 : : } else {
2108 : 0 : lkey = mlx5_lookup_mempool_regs(mr_ctrl, repl, mp, addr);
2109 : : /* Can only fail if the address is not from the mempool. */
2110 [ # # ]: 0 : if (unlikely(lkey == UINT32_MAX))
2111 : : return UINT32_MAX;
2112 : : }
2113 : : /* Update the most recently used entry. */
2114 : 0 : mr_ctrl->mru = mr_ctrl->head;
2115 : : /* Point to the next victim, the oldest. */
2116 : 0 : mr_ctrl->head = (mr_ctrl->head + 1) % MLX5_MR_CACHE_N;
2117 : 0 : return lkey;
2118 : : }
2119 : :
2120 : : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_mr_mb2mr_bh)
2121 : : uint32_t
2122 : 0 : mlx5_mr_mb2mr_bh(struct mlx5_mr_ctrl *mr_ctrl, struct rte_mbuf *mb)
2123 : : {
2124 : : struct rte_mempool *mp;
2125 : : struct mlx5_mprq_buf *buf;
2126 : : uint32_t lkey;
2127 : 0 : uintptr_t addr = (uintptr_t)mb->buf_addr;
2128 : : struct mlx5_mr_share_cache *share_cache =
2129 : 0 : container_of(mr_ctrl->dev_gen_ptr, struct mlx5_mr_share_cache,
2130 : : dev_gen);
2131 : : struct mlx5_common_device *cdev =
2132 : 0 : container_of(share_cache, struct mlx5_common_device, mr_scache);
2133 : : bool external, mprq, pinned = false;
2134 : :
2135 : : /* Recover MPRQ mempool. */
2136 : 0 : external = RTE_MBUF_HAS_EXTBUF(mb);
2137 [ # # # # ]: 0 : if (external && mb->shinfo->free_cb == mlx5_mprq_buf_free_cb) {
2138 : : mprq = true;
2139 : 0 : buf = mb->shinfo->fcb_opaque;
2140 : 0 : mp = buf->mp;
2141 : : } else {
2142 : : mprq = false;
2143 : : mp = mlx5_mb2mp(mb);
2144 : 0 : pinned = rte_pktmbuf_priv_flags(mp) &
2145 : : RTE_PKTMBUF_POOL_F_PINNED_EXT_BUF;
2146 : : }
2147 [ # # # # ]: 0 : if (!external || mprq || pinned) {
2148 : 0 : lkey = mlx5_mr_mempool2mr_bh(mr_ctrl, mp, addr);
2149 [ # # ]: 0 : if (lkey != UINT32_MAX)
2150 : : return lkey;
2151 : : /* MPRQ is always registered. */
2152 : : MLX5_ASSERT(!mprq);
2153 : : }
2154 : : /* Register pinned external memory if the mempool is not used for Rx. */
2155 [ # # # # ]: 0 : if (cdev->config.mr_mempool_reg_en && pinned) {
2156 [ # # ]: 0 : if (mlx5_mr_mempool_register(cdev, mp, true) < 0)
2157 : : return UINT32_MAX;
2158 : 0 : lkey = mlx5_mr_mempool2mr_bh(mr_ctrl, mp, addr);
2159 : : MLX5_ASSERT(lkey != UINT32_MAX);
2160 : 0 : return lkey;
2161 : : }
2162 : : /* Fallback to generic mechanism in corner cases. */
2163 : 0 : return mlx5_mr_addr2mr_bh(mr_ctrl, addr);
2164 : : }
|