1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016 6WIND S.A.
3 * Copyright 2020 Mellanox Technologies, Ltd
5 #include <rte_eal_memconfig.h>
7 #include <rte_mempool.h>
8 #include <rte_malloc.h>
9 #include <rte_rwlock.h>
11 #include "mlx5_glue.h"
12 #include "mlx5_common_mp.h"
13 #include "mlx5_common_mr.h"
14 #include "mlx5_common_utils.h"
16 struct mr_find_contig_memsegs_data {
20 const struct rte_memseg_list *msl;
24 * Expand B-tree table to a given size. Can't be called with holding
25 * memory_hotplug_lock or share_cache.rwlock due to rte_realloc().
28 * Pointer to B-tree structure.
30 * Number of entries for expansion.
33 * 0 on success, -1 on failure.
36 mr_btree_expand(struct mlx5_mr_btree *bt, int n)
44 * Downside of directly using rte_realloc() is that SOCKET_ID_ANY is
45 * used inside if there's no room to expand. Because this is a quite
46 * rare case and a part of very slow path, it is very acceptable.
47 * Initially cache_bh[] will be given practically enough space and once
48 * it is expanded, expansion wouldn't be needed again ever.
50 mem = rte_realloc(bt->table, n * sizeof(struct mr_cache_entry), 0);
52 /* Not an error, B-tree search will be skipped. */
53 DRV_LOG(WARNING, "failed to expand MR B-tree (%p) table",
57 DRV_LOG(DEBUG, "expanded MR B-tree table (size=%u)", n);
65 * Look up LKey from given B-tree lookup table, store the last index and return
69 * Pointer to B-tree structure.
71 * Pointer to index. Even on search failure, returns index where it stops
72 * searching so that index can be used when inserting a new entry.
77 * Searched LKey on success, UINT32_MAX on no match.
80 mr_btree_lookup(struct mlx5_mr_btree *bt, uint16_t *idx, uintptr_t addr)
82 struct mr_cache_entry *lkp_tbl;
86 MLX5_ASSERT(bt != NULL);
89 /* First entry must be NULL for comparison. */
90 MLX5_ASSERT(bt->len > 0 || (lkp_tbl[0].start == 0 &&
91 lkp_tbl[0].lkey == UINT32_MAX));
94 register uint16_t delta = n >> 1;
96 if (addr < lkp_tbl[base + delta].start) {
103 MLX5_ASSERT(addr >= lkp_tbl[base].start);
105 if (addr < lkp_tbl[base].end)
106 return lkp_tbl[base].lkey;
112 * Insert an entry to B-tree lookup table.
115 * Pointer to B-tree structure.
117 * Pointer to new entry to insert.
120 * 0 on success, -1 on failure.
123 mr_btree_insert(struct mlx5_mr_btree *bt, struct mr_cache_entry *entry)
125 struct mr_cache_entry *lkp_tbl;
129 MLX5_ASSERT(bt != NULL);
130 MLX5_ASSERT(bt->len <= bt->size);
131 MLX5_ASSERT(bt->len > 0);
132 lkp_tbl = *bt->table;
133 /* Find out the slot for insertion. */
134 if (mr_btree_lookup(bt, &idx, entry->start) != UINT32_MAX) {
136 "abort insertion to B-tree(%p): already exist at"
137 " idx=%u [0x%" PRIxPTR ", 0x%" PRIxPTR ") lkey=0x%x",
138 (void *)bt, idx, entry->start, entry->end, entry->lkey);
139 /* Already exist, return. */
142 /* If table is full, return error. */
143 if (unlikely(bt->len == bt->size)) {
149 shift = (bt->len - idx) * sizeof(struct mr_cache_entry);
151 memmove(&lkp_tbl[idx + 1], &lkp_tbl[idx], shift);
152 lkp_tbl[idx] = *entry;
155 "inserted B-tree(%p)[%u],"
156 " [0x%" PRIxPTR ", 0x%" PRIxPTR ") lkey=0x%x",
157 (void *)bt, idx, entry->start, entry->end, entry->lkey);
162 * Initialize B-tree and allocate memory for lookup table.
165 * Pointer to B-tree structure.
167 * Number of entries to allocate.
169 * NUMA socket on which memory must be allocated.
172 * 0 on success, a negative errno value otherwise and rte_errno is set.
175 mlx5_mr_btree_init(struct mlx5_mr_btree *bt, int n, int socket)
181 MLX5_ASSERT(!bt->table && !bt->size);
182 memset(bt, 0, sizeof(*bt));
183 bt->table = rte_calloc_socket("B-tree table",
184 n, sizeof(struct mr_cache_entry),
186 if (bt->table == NULL) {
188 DEBUG("failed to allocate memory for btree cache on socket %d",
193 /* First entry must be NULL for binary search. */
194 (*bt->table)[bt->len++] = (struct mr_cache_entry) {
197 DEBUG("initialized B-tree %p with table %p",
198 (void *)bt, (void *)bt->table);
203 * Free B-tree resources.
206 * Pointer to B-tree structure.
209 mlx5_mr_btree_free(struct mlx5_mr_btree *bt)
213 DEBUG("freeing B-tree %p with table %p",
214 (void *)bt, (void *)bt->table);
216 memset(bt, 0, sizeof(*bt));
220 * Dump all the entries in a B-tree
223 * Pointer to B-tree structure.
226 mlx5_mr_btree_dump(struct mlx5_mr_btree *bt __rte_unused)
228 #ifdef RTE_LIBRTE_MLX5_DEBUG
230 struct mr_cache_entry *lkp_tbl;
234 lkp_tbl = *bt->table;
235 for (idx = 0; idx < bt->len; ++idx) {
236 struct mr_cache_entry *entry = &lkp_tbl[idx];
238 DEBUG("B-tree(%p)[%u],"
239 " [0x%" PRIxPTR ", 0x%" PRIxPTR ") lkey=0x%x",
240 (void *)bt, idx, entry->start, entry->end, entry->lkey);
246 * Find virtually contiguous memory chunk in a given MR.
249 * Pointer to MR structure.
251 * Pointer to returning MR cache entry. If not found, this will not be
254 * Start index of the memseg bitmap.
257 * Next index to go on lookup.
260 mr_find_next_chunk(struct mlx5_mr *mr, struct mr_cache_entry *entry,
267 /* MR for external memory doesn't have memseg list. */
268 if (mr->msl == NULL) {
269 MLX5_ASSERT(mr->ms_bmp_n == 1);
270 MLX5_ASSERT(mr->ms_n == 1);
271 MLX5_ASSERT(base_idx == 0);
273 * Can't search it from memseg list but get it directly from
274 * pmd_mr as there's only one chunk.
276 entry->start = (uintptr_t)mr->pmd_mr.addr;
277 entry->end = (uintptr_t)mr->pmd_mr.addr + mr->pmd_mr.len;
278 entry->lkey = rte_cpu_to_be_32(mr->pmd_mr.lkey);
279 /* Returning 1 ends iteration. */
282 for (idx = base_idx; idx < mr->ms_bmp_n; ++idx) {
283 if (rte_bitmap_get(mr->ms_bmp, idx)) {
284 const struct rte_memseg_list *msl;
285 const struct rte_memseg *ms;
288 ms = rte_fbarray_get(&msl->memseg_arr,
289 mr->ms_base_idx + idx);
290 MLX5_ASSERT(msl->page_sz == ms->hugepage_sz);
293 end = ms->addr_64 + ms->hugepage_sz;
295 /* Passed the end of a fragment. */
300 /* Found one chunk. */
301 entry->start = start;
303 entry->lkey = rte_cpu_to_be_32(mr->pmd_mr.lkey);
309 * Insert a MR to the global B-tree cache. It may fail due to low-on-memory.
310 * Then, this entry will have to be searched by mr_lookup_list() in
311 * mlx5_mr_create() on miss.
314 * Pointer to a global shared MR cache.
316 * Pointer to MR to insert.
319 * 0 on success, -1 on failure.
322 mlx5_mr_insert_cache(struct mlx5_mr_share_cache *share_cache,
327 DRV_LOG(DEBUG, "Inserting MR(%p) to global cache(%p)",
328 (void *)mr, (void *)share_cache);
329 for (n = 0; n < mr->ms_bmp_n; ) {
330 struct mr_cache_entry entry;
332 memset(&entry, 0, sizeof(entry));
333 /* Find a contiguous chunk and advance the index. */
334 n = mr_find_next_chunk(mr, &entry, n);
337 if (mr_btree_insert(&share_cache->cache, &entry) < 0) {
339 * Overflowed, but the global table cannot be expanded
340 * because of deadlock.
349 * Look up address in the original global MR list.
352 * Pointer to a global shared MR cache.
354 * Pointer to returning MR cache entry. If no match, this will not be updated.
359 * Found MR on match, NULL otherwise.
362 mlx5_mr_lookup_list(struct mlx5_mr_share_cache *share_cache,
363 struct mr_cache_entry *entry, uintptr_t addr)
367 /* Iterate all the existing MRs. */
368 LIST_FOREACH(mr, &share_cache->mr_list, mr) {
373 for (n = 0; n < mr->ms_bmp_n; ) {
374 struct mr_cache_entry ret;
376 memset(&ret, 0, sizeof(ret));
377 n = mr_find_next_chunk(mr, &ret, n);
378 if (addr >= ret.start && addr < ret.end) {
389 * Look up address on global MR cache.
392 * Pointer to a global shared MR cache.
394 * Pointer to returning MR cache entry. If no match, this will not be updated.
399 * Searched LKey on success, UINT32_MAX on failure and rte_errno is set.
402 mlx5_mr_lookup_cache(struct mlx5_mr_share_cache *share_cache,
403 struct mr_cache_entry *entry, uintptr_t addr)
406 uint32_t lkey = UINT32_MAX;
410 * If the global cache has overflowed since it failed to expand the
411 * B-tree table, it can't have all the existing MRs. Then, the address
412 * has to be searched by traversing the original MR list instead, which
413 * is very slow path. Otherwise, the global cache is all inclusive.
415 if (!unlikely(share_cache->cache.overflow)) {
416 lkey = mr_btree_lookup(&share_cache->cache, &idx, addr);
417 if (lkey != UINT32_MAX)
418 *entry = (*share_cache->cache.table)[idx];
420 /* Falling back to the slowest path. */
421 mr = mlx5_mr_lookup_list(share_cache, entry, addr);
425 MLX5_ASSERT(lkey == UINT32_MAX || (addr >= entry->start &&
431 * Free MR resources. MR lock must not be held to avoid a deadlock. rte_free()
432 * can raise memory free event and the callback function will spin on the lock.
435 * Pointer to MR to free.
438 mr_free(struct mlx5_mr *mr, mlx5_dereg_mr_t dereg_mr_cb)
442 DRV_LOG(DEBUG, "freeing MR(%p):", (void *)mr);
443 dereg_mr_cb(&mr->pmd_mr);
444 if (mr->ms_bmp != NULL)
445 rte_bitmap_free(mr->ms_bmp);
450 mlx5_mr_rebuild_cache(struct mlx5_mr_share_cache *share_cache)
454 DRV_LOG(DEBUG, "Rebuild dev cache[] %p", (void *)share_cache);
455 /* Flush cache to rebuild. */
456 share_cache->cache.len = 1;
457 share_cache->cache.overflow = 0;
458 /* Iterate all the existing MRs. */
459 LIST_FOREACH(mr, &share_cache->mr_list, mr)
460 if (mlx5_mr_insert_cache(share_cache, mr) < 0)
465 * Release resources of detached MR having no online entry.
468 * Pointer to a global shared MR cache.
471 mlx5_mr_garbage_collect(struct mlx5_mr_share_cache *share_cache)
473 struct mlx5_mr *mr_next;
474 struct mlx5_mr_list free_list = LIST_HEAD_INITIALIZER(free_list);
476 /* Must be called from the primary process. */
477 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
479 * MR can't be freed with holding the lock because rte_free() could call
480 * memory free callback function. This will be a deadlock situation.
482 rte_rwlock_write_lock(&share_cache->rwlock);
483 /* Detach the whole free list and release it after unlocking. */
484 free_list = share_cache->mr_free_list;
485 LIST_INIT(&share_cache->mr_free_list);
486 rte_rwlock_write_unlock(&share_cache->rwlock);
487 /* Release resources. */
488 mr_next = LIST_FIRST(&free_list);
489 while (mr_next != NULL) {
490 struct mlx5_mr *mr = mr_next;
492 mr_next = LIST_NEXT(mr, mr);
493 mr_free(mr, share_cache->dereg_mr_cb);
497 /* Called during rte_memseg_contig_walk() by mlx5_mr_create(). */
499 mr_find_contig_memsegs_cb(const struct rte_memseg_list *msl,
500 const struct rte_memseg *ms, size_t len, void *arg)
502 struct mr_find_contig_memsegs_data *data = arg;
504 if (data->addr < ms->addr_64 || data->addr >= ms->addr_64 + len)
506 /* Found, save it and stop walking. */
507 data->start = ms->addr_64;
508 data->end = ms->addr_64 + len;
514 * Create a new global Memory Region (MR) for a missing virtual address.
515 * This API should be called on a secondary process, then a request is sent to
516 * the primary process in order to create a MR for the address. As the global MR
517 * list is on the shared memory, following LKey lookup should succeed unless the
521 * Pointer to pd of a device (net, regex, vdpa,...).
523 * Pointer to a global shared MR cache.
525 * Pointer to returning MR cache entry, found in the global cache or newly
526 * created. If failed to create one, this will not be updated.
528 * Target virtual address to register.
529 * @param mr_ext_memseg_en
530 * Configurable flag about external memory segment enable or not.
533 * Searched LKey on success, UINT32_MAX on failure and rte_errno is set.
536 mlx5_mr_create_secondary(void *pd __rte_unused,
537 struct mlx5_mp_id *mp_id,
538 struct mlx5_mr_share_cache *share_cache,
539 struct mr_cache_entry *entry, uintptr_t addr,
540 unsigned int mr_ext_memseg_en __rte_unused)
544 DEBUG("port %u requesting MR creation for address (%p)",
545 mp_id->port_id, (void *)addr);
546 ret = mlx5_mp_req_mr_create(mp_id, addr);
548 DEBUG("Fail to request MR creation for address (%p)",
552 rte_rwlock_read_lock(&share_cache->rwlock);
553 /* Fill in output data. */
554 mlx5_mr_lookup_cache(share_cache, entry, addr);
555 /* Lookup can't fail. */
556 MLX5_ASSERT(entry->lkey != UINT32_MAX);
557 rte_rwlock_read_unlock(&share_cache->rwlock);
558 DEBUG("MR CREATED by primary process for %p:\n"
559 " [0x%" PRIxPTR ", 0x%" PRIxPTR "), lkey=0x%x",
560 (void *)addr, entry->start, entry->end, entry->lkey);
565 * Create a new global Memory Region (MR) for a missing virtual address.
566 * Register entire virtually contiguous memory chunk around the address.
569 * Pointer to pd of a device (net, regex, vdpa,...).
571 * Pointer to a global shared MR cache.
573 * Pointer to returning MR cache entry, found in the global cache or newly
574 * created. If failed to create one, this will not be updated.
576 * Target virtual address to register.
577 * @param mr_ext_memseg_en
578 * Configurable flag about external memory segment enable or not.
581 * Searched LKey on success, UINT32_MAX on failure and rte_errno is set.
584 mlx5_mr_create_primary(void *pd,
585 struct mlx5_mr_share_cache *share_cache,
586 struct mr_cache_entry *entry, uintptr_t addr,
587 unsigned int mr_ext_memseg_en)
589 struct mr_find_contig_memsegs_data data = {.addr = addr, };
590 struct mr_find_contig_memsegs_data data_re;
591 const struct rte_memseg_list *msl;
592 const struct rte_memseg *ms;
593 struct mlx5_mr *mr = NULL;
594 int ms_idx_shift = -1;
601 DRV_LOG(DEBUG, "Creating a MR using address (%p)", (void *)addr);
603 * Release detached MRs if any. This can't be called with holding either
604 * memory_hotplug_lock or share_cache->rwlock. MRs on the free list have
605 * been detached by the memory free event but it couldn't be released
606 * inside the callback due to deadlock. As a result, releasing resources
607 * is quite opportunistic.
609 mlx5_mr_garbage_collect(share_cache);
611 * If enabled, find out a contiguous virtual address chunk in use, to
612 * which the given address belongs, in order to register maximum range.
613 * In the best case where mempools are not dynamically recreated and
614 * '--socket-mem' is specified as an EAL option, it is very likely to
615 * have only one MR(LKey) per a socket and per a hugepage-size even
616 * though the system memory is highly fragmented. As the whole memory
617 * chunk will be pinned by kernel, it can't be reused unless entire
618 * chunk is freed from EAL.
620 * If disabled, just register one memseg (page). Then, memory
621 * consumption will be minimized but it may drop performance if there
622 * are many MRs to lookup on the datapath.
624 if (!mr_ext_memseg_en) {
625 data.msl = rte_mem_virt2memseg_list((void *)addr);
626 data.start = RTE_ALIGN_FLOOR(addr, data.msl->page_sz);
627 data.end = data.start + data.msl->page_sz;
628 } else if (!rte_memseg_contig_walk(mr_find_contig_memsegs_cb, &data)) {
630 "Unable to find virtually contiguous"
631 " chunk for address (%p)."
632 " rte_memseg_contig_walk() failed.", (void *)addr);
637 /* Addresses must be page-aligned. */
638 MLX5_ASSERT(data.msl);
639 MLX5_ASSERT(rte_is_aligned((void *)data.start, data.msl->page_sz));
640 MLX5_ASSERT(rte_is_aligned((void *)data.end, data.msl->page_sz));
642 ms = rte_mem_virt2memseg((void *)data.start, msl);
643 len = data.end - data.start;
645 MLX5_ASSERT(msl->page_sz == ms->hugepage_sz);
646 /* Number of memsegs in the range. */
647 ms_n = len / msl->page_sz;
648 DEBUG("Extending %p to [0x%" PRIxPTR ", 0x%" PRIxPTR "),"
649 " page_sz=0x%" PRIx64 ", ms_n=%u",
650 (void *)addr, data.start, data.end, msl->page_sz, ms_n);
651 /* Size of memory for bitmap. */
652 bmp_size = rte_bitmap_get_memory_footprint(ms_n);
653 mr = rte_zmalloc_socket(NULL,
654 RTE_ALIGN_CEIL(sizeof(*mr),
655 RTE_CACHE_LINE_SIZE) +
657 RTE_CACHE_LINE_SIZE, msl->socket_id);
659 DEBUG("Unable to allocate memory for a new MR of"
660 " address (%p).", (void *)addr);
666 * Save the index of the first memseg and initialize memseg bitmap. To
667 * see if a memseg of ms_idx in the memseg-list is still valid, check:
668 * rte_bitmap_get(mr->bmp, ms_idx - mr->ms_base_idx)
670 mr->ms_base_idx = rte_fbarray_find_idx(&msl->memseg_arr, ms);
671 bmp_mem = RTE_PTR_ALIGN_CEIL(mr + 1, RTE_CACHE_LINE_SIZE);
672 mr->ms_bmp = rte_bitmap_init(ms_n, bmp_mem, bmp_size);
673 if (mr->ms_bmp == NULL) {
674 DEBUG("Unable to initialize bitmap for a new MR of"
675 " address (%p).", (void *)addr);
680 * Should recheck whether the extended contiguous chunk is still valid.
681 * Because memory_hotplug_lock can't be held if there's any memory
682 * related calls in a critical path, resource allocation above can't be
683 * locked. If the memory has been changed at this point, try again with
684 * just single page. If not, go on with the big chunk atomically from
687 rte_mcfg_mem_read_lock();
689 if (len > msl->page_sz &&
690 !rte_memseg_contig_walk(mr_find_contig_memsegs_cb, &data_re)) {
691 DEBUG("Unable to find virtually contiguous"
692 " chunk for address (%p)."
693 " rte_memseg_contig_walk() failed.", (void *)addr);
697 if (data.start != data_re.start || data.end != data_re.end) {
699 * The extended contiguous chunk has been changed. Try again
700 * with single memseg instead.
702 data.start = RTE_ALIGN_FLOOR(addr, msl->page_sz);
703 data.end = data.start + msl->page_sz;
704 rte_mcfg_mem_read_unlock();
705 mr_free(mr, share_cache->dereg_mr_cb);
706 goto alloc_resources;
708 MLX5_ASSERT(data.msl == data_re.msl);
709 rte_rwlock_write_lock(&share_cache->rwlock);
711 * Check the address is really missing. If other thread already created
712 * one or it is not found due to overflow, abort and return.
714 if (mlx5_mr_lookup_cache(share_cache, entry, addr) != UINT32_MAX) {
716 * Insert to the global cache table. It may fail due to
717 * low-on-memory. Then, this entry will have to be searched
720 mr_btree_insert(&share_cache->cache, entry);
721 DEBUG("Found MR for %p on final lookup, abort", (void *)addr);
722 rte_rwlock_write_unlock(&share_cache->rwlock);
723 rte_mcfg_mem_read_unlock();
725 * Must be unlocked before calling rte_free() because
726 * mlx5_mr_mem_event_free_cb() can be called inside.
728 mr_free(mr, share_cache->dereg_mr_cb);
732 * Trim start and end addresses for verbs MR. Set bits for registering
733 * memsegs but exclude already registered ones. Bitmap can be
736 for (n = 0; n < ms_n; ++n) {
738 struct mr_cache_entry ret;
740 memset(&ret, 0, sizeof(ret));
741 start = data_re.start + n * msl->page_sz;
742 /* Exclude memsegs already registered by other MRs. */
743 if (mlx5_mr_lookup_cache(share_cache, &ret, start) ==
746 * Start from the first unregistered memseg in the
749 if (ms_idx_shift == -1) {
750 mr->ms_base_idx += n;
754 data.end = start + msl->page_sz;
755 rte_bitmap_set(mr->ms_bmp, n - ms_idx_shift);
759 len = data.end - data.start;
760 mr->ms_bmp_n = len / msl->page_sz;
761 MLX5_ASSERT(ms_idx_shift + mr->ms_bmp_n <= ms_n);
763 * Finally create an MR for the memory chunk. Verbs: ibv_reg_mr() can
764 * be called with holding the memory lock because it doesn't use
765 * mlx5_alloc_buf_extern() which eventually calls rte_malloc_socket()
766 * through mlx5_alloc_verbs_buf().
768 share_cache->reg_mr_cb(pd, (void *)data.start, len, &mr->pmd_mr);
769 if (mr->pmd_mr.obj == NULL) {
770 DEBUG("Fail to create an MR for address (%p)",
775 MLX5_ASSERT((uintptr_t)mr->pmd_mr.addr == data.start);
776 MLX5_ASSERT(mr->pmd_mr.len);
777 LIST_INSERT_HEAD(&share_cache->mr_list, mr, mr);
778 DEBUG("MR CREATED (%p) for %p:\n"
779 " [0x%" PRIxPTR ", 0x%" PRIxPTR "),"
780 " lkey=0x%x base_idx=%u ms_n=%u, ms_bmp_n=%u",
781 (void *)mr, (void *)addr, data.start, data.end,
782 rte_cpu_to_be_32(mr->pmd_mr.lkey),
783 mr->ms_base_idx, mr->ms_n, mr->ms_bmp_n);
784 /* Insert to the global cache table. */
785 mlx5_mr_insert_cache(share_cache, mr);
786 /* Fill in output data. */
787 mlx5_mr_lookup_cache(share_cache, entry, addr);
788 /* Lookup can't fail. */
789 MLX5_ASSERT(entry->lkey != UINT32_MAX);
790 rte_rwlock_write_unlock(&share_cache->rwlock);
791 rte_mcfg_mem_read_unlock();
794 rte_rwlock_write_unlock(&share_cache->rwlock);
796 rte_mcfg_mem_read_unlock();
799 * In case of error, as this can be called in a datapath, a warning
800 * message per an error is preferable instead. Must be unlocked before
801 * calling rte_free() because mlx5_mr_mem_event_free_cb() can be called
804 mr_free(mr, share_cache->dereg_mr_cb);
809 * Create a new global Memory Region (MR) for a missing virtual address.
810 * This can be called from primary and secondary process.
813 * Pointer to pd handle of a device (net, regex, vdpa,...).
815 * Pointer to a global shared MR cache.
817 * Pointer to returning MR cache entry, found in the global cache or newly
818 * created. If failed to create one, this will not be updated.
820 * Target virtual address to register.
823 * Searched LKey on success, UINT32_MAX on failure and rte_errno is set.
826 mlx5_mr_create(void *pd, struct mlx5_mp_id *mp_id,
827 struct mlx5_mr_share_cache *share_cache,
828 struct mr_cache_entry *entry, uintptr_t addr,
829 unsigned int mr_ext_memseg_en)
833 switch (rte_eal_process_type()) {
834 case RTE_PROC_PRIMARY:
835 ret = mlx5_mr_create_primary(pd, share_cache, entry,
836 addr, mr_ext_memseg_en);
838 case RTE_PROC_SECONDARY:
839 ret = mlx5_mr_create_secondary(pd, mp_id, share_cache, entry,
840 addr, mr_ext_memseg_en);
849 * Look up address in the global MR cache table. If not found, create a new MR.
850 * Insert the found/created entry to local bottom-half cache table.
853 * Pointer to pd of a device (net, regex, vdpa,...).
855 * Pointer to a global shared MR cache.
857 * Pointer to per-queue MR control structure.
859 * Pointer to returning MR cache entry, found in the global cache or newly
860 * created. If failed to create one, this is not written.
865 * Searched LKey on success, UINT32_MAX on no match.
868 mr_lookup_caches(void *pd, struct mlx5_mp_id *mp_id,
869 struct mlx5_mr_share_cache *share_cache,
870 struct mlx5_mr_ctrl *mr_ctrl,
871 struct mr_cache_entry *entry, uintptr_t addr,
872 unsigned int mr_ext_memseg_en)
874 struct mlx5_mr_btree *bt = &mr_ctrl->cache_bh;
878 /* If local cache table is full, try to double it. */
879 if (unlikely(bt->len == bt->size))
880 mr_btree_expand(bt, bt->size << 1);
881 /* Look up in the global cache. */
882 rte_rwlock_read_lock(&share_cache->rwlock);
883 lkey = mr_btree_lookup(&share_cache->cache, &idx, addr);
884 if (lkey != UINT32_MAX) {
886 *entry = (*share_cache->cache.table)[idx];
887 rte_rwlock_read_unlock(&share_cache->rwlock);
889 * Update local cache. Even if it fails, return the found entry
890 * to update top-half cache. Next time, this entry will be found
891 * in the global cache.
893 mr_btree_insert(bt, entry);
896 rte_rwlock_read_unlock(&share_cache->rwlock);
897 /* First time to see the address? Create a new MR. */
898 lkey = mlx5_mr_create(pd, mp_id, share_cache, entry, addr,
901 * Update the local cache if successfully created a new global MR. Even
902 * if failed to create one, there's no action to take in this datapath
903 * code. As returning LKey is invalid, this will eventually make HW
906 if (lkey != UINT32_MAX)
907 mr_btree_insert(bt, entry);
912 * Bottom-half of LKey search on datapath. First search in cache_bh[] and if
913 * misses, search in the global MR cache table and update the new entry to
914 * per-queue local caches.
917 * Pointer to pd of a device (net, regex, vdpa,...).
919 * Pointer to a global shared MR cache.
921 * Pointer to per-queue MR control structure.
926 * Searched LKey on success, UINT32_MAX on no match.
928 uint32_t mlx5_mr_addr2mr_bh(void *pd, struct mlx5_mp_id *mp_id,
929 struct mlx5_mr_share_cache *share_cache,
930 struct mlx5_mr_ctrl *mr_ctrl,
931 uintptr_t addr, unsigned int mr_ext_memseg_en)
935 /* Victim in top-half cache to replace with new entry. */
936 struct mr_cache_entry *repl = &mr_ctrl->cache[mr_ctrl->head];
938 /* Binary-search MR translation table. */
939 lkey = mr_btree_lookup(&mr_ctrl->cache_bh, &bh_idx, addr);
940 /* Update top-half cache. */
941 if (likely(lkey != UINT32_MAX)) {
942 *repl = (*mr_ctrl->cache_bh.table)[bh_idx];
945 * If missed in local lookup table, search in the global cache
946 * and local cache_bh[] will be updated inside if possible.
947 * Top-half cache entry will also be updated.
949 lkey = mr_lookup_caches(pd, mp_id, share_cache, mr_ctrl,
950 repl, addr, mr_ext_memseg_en);
951 if (unlikely(lkey == UINT32_MAX))
954 /* Update the most recently used entry. */
955 mr_ctrl->mru = mr_ctrl->head;
956 /* Point to the next victim, the oldest. */
957 mr_ctrl->head = (mr_ctrl->head + 1) % MLX5_MR_CACHE_N;
962 * Release all the created MRs and resources on global MR cache of a device.
966 * Pointer to a global shared MR cache.
969 mlx5_mr_release_cache(struct mlx5_mr_share_cache *share_cache)
971 struct mlx5_mr *mr_next;
973 rte_rwlock_write_lock(&share_cache->rwlock);
974 /* Detach from MR list and move to free list. */
975 mr_next = LIST_FIRST(&share_cache->mr_list);
976 while (mr_next != NULL) {
977 struct mlx5_mr *mr = mr_next;
979 mr_next = LIST_NEXT(mr, mr);
981 LIST_INSERT_HEAD(&share_cache->mr_free_list, mr, mr);
983 LIST_INIT(&share_cache->mr_list);
984 /* Free global cache. */
985 mlx5_mr_btree_free(&share_cache->cache);
986 rte_rwlock_write_unlock(&share_cache->rwlock);
987 /* Free all remaining MRs. */
988 mlx5_mr_garbage_collect(share_cache);
992 * Flush all of the local cache entries.
995 * Pointer to per-queue MR local cache.
998 mlx5_mr_flush_local_cache(struct mlx5_mr_ctrl *mr_ctrl)
1000 /* Reset the most-recently-used index. */
1002 /* Reset the linear search array. */
1004 memset(mr_ctrl->cache, 0, sizeof(mr_ctrl->cache));
1005 /* Reset the B-tree table. */
1006 mr_ctrl->cache_bh.len = 1;
1007 mr_ctrl->cache_bh.overflow = 0;
1008 /* Update the generation number. */
1009 mr_ctrl->cur_gen = *mr_ctrl->dev_gen_ptr;
1010 DRV_LOG(DEBUG, "mr_ctrl(%p): flushed, cur_gen=%d",
1011 (void *)mr_ctrl, mr_ctrl->cur_gen);
1015 * Creates a memory region for external memory, that is memory which is not
1016 * part of the DPDK memory segments.
1019 * Pointer to pd of a device (net, regex, vdpa,...).
1021 * Starting virtual address of memory.
1023 * Length of memory segment being mapped.
1025 * Socket to allocate heap memory for the control structures.
1028 * Pointer to MR structure on success, NULL otherwise.
1031 mlx5_create_mr_ext(void *pd, uintptr_t addr, size_t len, int socket_id,
1032 mlx5_reg_mr_t reg_mr_cb)
1034 struct mlx5_mr *mr = NULL;
1036 mr = rte_zmalloc_socket(NULL,
1037 RTE_ALIGN_CEIL(sizeof(*mr),
1038 RTE_CACHE_LINE_SIZE),
1039 RTE_CACHE_LINE_SIZE, socket_id);
1042 reg_mr_cb(pd, (void *)addr, len, &mr->pmd_mr);
1043 if (mr->pmd_mr.obj == NULL) {
1045 "Fail to create MR for address (%p)",
1050 mr->msl = NULL; /* Mark it is external memory. */
1055 "MR CREATED (%p) for external memory %p:\n"
1056 " [0x%" PRIxPTR ", 0x%" PRIxPTR "),"
1057 " lkey=0x%x base_idx=%u ms_n=%u, ms_bmp_n=%u",
1058 (void *)mr, (void *)addr,
1059 addr, addr + len, rte_cpu_to_be_32(mr->pmd_mr.lkey),
1060 mr->ms_base_idx, mr->ms_n, mr->ms_bmp_n);
1065 * Dump all the created MRs and the global cache entries.
1068 * Pointer to Ethernet device shared context.
1071 mlx5_mr_dump_cache(struct mlx5_mr_share_cache *share_cache __rte_unused)
1073 #ifdef RTE_LIBRTE_MLX5_DEBUG
1078 rte_rwlock_read_lock(&share_cache->rwlock);
1079 /* Iterate all the existing MRs. */
1080 LIST_FOREACH(mr, &share_cache->mr_list, mr) {
1083 DEBUG("MR[%u], LKey = 0x%x, ms_n = %u, ms_bmp_n = %u",
1084 mr_n++, rte_cpu_to_be_32(mr->pmd_mr.lkey),
1085 mr->ms_n, mr->ms_bmp_n);
1088 for (n = 0; n < mr->ms_bmp_n; ) {
1089 struct mr_cache_entry ret = { 0, };
1091 n = mr_find_next_chunk(mr, &ret, n);
1094 DEBUG(" chunk[%u], [0x%" PRIxPTR ", 0x%" PRIxPTR ")",
1095 chunk_n++, ret.start, ret.end);
1098 DEBUG("Dumping global cache %p", (void *)share_cache);
1099 mlx5_mr_btree_dump(&share_cache->cache);
1100 rte_rwlock_read_unlock(&share_cache->rwlock);