1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016 6WIND S.A.
3 * Copyright 2020 Mellanox Technologies, Ltd
7 #include <rte_eal_memconfig.h>
8 #include <rte_eal_paging.h>
10 #include <rte_mempool.h>
11 #include <rte_malloc.h>
12 #include <rte_rwlock.h>
14 #include "mlx5_glue.h"
15 #include "mlx5_common_mp.h"
16 #include "mlx5_common_mr.h"
17 #include "mlx5_common_log.h"
18 #include "mlx5_malloc.h"
20 struct mr_find_contig_memsegs_data {
24 const struct rte_memseg_list *msl;
27 /* Virtual memory range. */
33 /** Memory region for a mempool. */
34 struct mlx5_mempool_mr {
35 struct mlx5_pmd_mr pmd_mr;
36 uint32_t refcnt; /**< Number of mempools sharing this MR. */
39 /* Mempool registration. */
40 struct mlx5_mempool_reg {
41 LIST_ENTRY(mlx5_mempool_reg) next;
42 /** Registered mempool, used to designate registrations. */
43 struct rte_mempool *mp;
44 /** Memory regions for the address ranges of the mempool. */
45 struct mlx5_mempool_mr *mrs;
46 /** Number of memory regions. */
51 * Expand B-tree table to a given size. Can't be called with holding
52 * memory_hotplug_lock or share_cache.rwlock due to rte_realloc().
55 * Pointer to B-tree structure.
57 * Number of entries for expansion.
60 * 0 on success, -1 on failure.
63 mr_btree_expand(struct mlx5_mr_btree *bt, int n)
71 * Downside of directly using rte_realloc() is that SOCKET_ID_ANY is
72 * used inside if there's no room to expand. Because this is a quite
73 * rare case and a part of very slow path, it is very acceptable.
74 * Initially cache_bh[] will be given practically enough space and once
75 * it is expanded, expansion wouldn't be needed again ever.
77 mem = mlx5_realloc(bt->table, MLX5_MEM_RTE | MLX5_MEM_ZERO,
78 n * sizeof(struct mr_cache_entry), 0, SOCKET_ID_ANY);
80 /* Not an error, B-tree search will be skipped. */
81 DRV_LOG(WARNING, "failed to expand MR B-tree (%p) table",
85 DRV_LOG(DEBUG, "expanded MR B-tree table (size=%u)", n);
93 * Look up LKey from given B-tree lookup table, store the last index and return
97 * Pointer to B-tree structure.
99 * Pointer to index. Even on search failure, returns index where it stops
100 * searching so that index can be used when inserting a new entry.
105 * Searched LKey on success, UINT32_MAX on no match.
108 mr_btree_lookup(struct mlx5_mr_btree *bt, uint16_t *idx, uintptr_t addr)
110 struct mr_cache_entry *lkp_tbl;
114 MLX5_ASSERT(bt != NULL);
115 lkp_tbl = *bt->table;
117 /* First entry must be NULL for comparison. */
118 MLX5_ASSERT(bt->len > 0 || (lkp_tbl[0].start == 0 &&
119 lkp_tbl[0].lkey == UINT32_MAX));
122 register uint16_t delta = n >> 1;
124 if (addr < lkp_tbl[base + delta].start) {
131 MLX5_ASSERT(addr >= lkp_tbl[base].start);
133 if (addr < lkp_tbl[base].end)
134 return lkp_tbl[base].lkey;
140 * Insert an entry to B-tree lookup table.
143 * Pointer to B-tree structure.
145 * Pointer to new entry to insert.
148 * 0 on success, -1 on failure.
151 mr_btree_insert(struct mlx5_mr_btree *bt, struct mr_cache_entry *entry)
153 struct mr_cache_entry *lkp_tbl;
157 MLX5_ASSERT(bt != NULL);
158 MLX5_ASSERT(bt->len <= bt->size);
159 MLX5_ASSERT(bt->len > 0);
160 lkp_tbl = *bt->table;
161 /* Find out the slot for insertion. */
162 if (mr_btree_lookup(bt, &idx, entry->start) != UINT32_MAX) {
164 "abort insertion to B-tree(%p): already exist at"
165 " idx=%u [0x%" PRIxPTR ", 0x%" PRIxPTR ") lkey=0x%x",
166 (void *)bt, idx, entry->start, entry->end, entry->lkey);
167 /* Already exist, return. */
170 /* If table is full, return error. */
171 if (unlikely(bt->len == bt->size)) {
177 shift = (bt->len - idx) * sizeof(struct mr_cache_entry);
179 memmove(&lkp_tbl[idx + 1], &lkp_tbl[idx], shift);
180 lkp_tbl[idx] = *entry;
183 "inserted B-tree(%p)[%u],"
184 " [0x%" PRIxPTR ", 0x%" PRIxPTR ") lkey=0x%x",
185 (void *)bt, idx, entry->start, entry->end, entry->lkey);
190 * Initialize B-tree and allocate memory for lookup table.
193 * Pointer to B-tree structure.
195 * Number of entries to allocate.
197 * NUMA socket on which memory must be allocated.
200 * 0 on success, a negative errno value otherwise and rte_errno is set.
203 mlx5_mr_btree_init(struct mlx5_mr_btree *bt, int n, int socket)
209 MLX5_ASSERT(!bt->table && !bt->size);
210 memset(bt, 0, sizeof(*bt));
211 bt->table = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO,
212 sizeof(struct mr_cache_entry) * n,
214 if (bt->table == NULL) {
217 "failed to allocate memory for btree cache on socket "
222 /* First entry must be NULL for binary search. */
223 (*bt->table)[bt->len++] = (struct mr_cache_entry) {
226 DRV_LOG(DEBUG, "initialized B-tree %p with table %p",
227 (void *)bt, (void *)bt->table);
232 * Free B-tree resources.
235 * Pointer to B-tree structure.
238 mlx5_mr_btree_free(struct mlx5_mr_btree *bt)
242 DRV_LOG(DEBUG, "freeing B-tree %p with table %p",
243 (void *)bt, (void *)bt->table);
244 mlx5_free(bt->table);
245 memset(bt, 0, sizeof(*bt));
249 * Dump all the entries in a B-tree
252 * Pointer to B-tree structure.
255 mlx5_mr_btree_dump(struct mlx5_mr_btree *bt __rte_unused)
257 #ifdef RTE_LIBRTE_MLX5_DEBUG
259 struct mr_cache_entry *lkp_tbl;
263 lkp_tbl = *bt->table;
264 for (idx = 0; idx < bt->len; ++idx) {
265 struct mr_cache_entry *entry = &lkp_tbl[idx];
267 DRV_LOG(DEBUG, "B-tree(%p)[%u],"
268 " [0x%" PRIxPTR ", 0x%" PRIxPTR ") lkey=0x%x",
269 (void *)bt, idx, entry->start, entry->end, entry->lkey);
275 * Initialize per-queue MR control descriptor.
278 * Pointer to MR control structure.
280 * Pointer to generation number of global cache.
282 * NUMA socket on which memory must be allocated.
285 * 0 on success, a negative errno value otherwise and rte_errno is set.
288 mlx5_mr_ctrl_init(struct mlx5_mr_ctrl *mr_ctrl, uint32_t *dev_gen_ptr,
291 if (mr_ctrl == NULL) {
295 /* Save pointer of global generation number to check memory event. */
296 mr_ctrl->dev_gen_ptr = dev_gen_ptr;
297 /* Initialize B-tree and allocate memory for bottom-half cache table. */
298 return mlx5_mr_btree_init(&mr_ctrl->cache_bh, MLX5_MR_BTREE_CACHE_N,
303 * Find virtually contiguous memory chunk in a given MR.
306 * Pointer to MR structure.
308 * Pointer to returning MR cache entry. If not found, this will not be
311 * Start index of the memseg bitmap.
314 * Next index to go on lookup.
317 mr_find_next_chunk(struct mlx5_mr *mr, struct mr_cache_entry *entry,
324 /* MR for external memory doesn't have memseg list. */
325 if (mr->msl == NULL) {
326 MLX5_ASSERT(mr->ms_bmp_n == 1);
327 MLX5_ASSERT(mr->ms_n == 1);
328 MLX5_ASSERT(base_idx == 0);
330 * Can't search it from memseg list but get it directly from
331 * pmd_mr as there's only one chunk.
333 entry->start = (uintptr_t)mr->pmd_mr.addr;
334 entry->end = (uintptr_t)mr->pmd_mr.addr + mr->pmd_mr.len;
335 entry->lkey = rte_cpu_to_be_32(mr->pmd_mr.lkey);
336 /* Returning 1 ends iteration. */
339 for (idx = base_idx; idx < mr->ms_bmp_n; ++idx) {
340 if (rte_bitmap_get(mr->ms_bmp, idx)) {
341 const struct rte_memseg_list *msl;
342 const struct rte_memseg *ms;
345 ms = rte_fbarray_get(&msl->memseg_arr,
346 mr->ms_base_idx + idx);
347 MLX5_ASSERT(msl->page_sz == ms->hugepage_sz);
350 end = ms->addr_64 + ms->hugepage_sz;
352 /* Passed the end of a fragment. */
357 /* Found one chunk. */
358 entry->start = start;
360 entry->lkey = rte_cpu_to_be_32(mr->pmd_mr.lkey);
366 * Insert a MR to the global B-tree cache. It may fail due to low-on-memory.
367 * Then, this entry will have to be searched by mr_lookup_list() in
368 * mlx5_mr_create() on miss.
371 * Pointer to a global shared MR cache.
373 * Pointer to MR to insert.
376 * 0 on success, -1 on failure.
379 mlx5_mr_insert_cache(struct mlx5_mr_share_cache *share_cache,
384 DRV_LOG(DEBUG, "Inserting MR(%p) to global cache(%p)",
385 (void *)mr, (void *)share_cache);
386 for (n = 0; n < mr->ms_bmp_n; ) {
387 struct mr_cache_entry entry;
389 memset(&entry, 0, sizeof(entry));
390 /* Find a contiguous chunk and advance the index. */
391 n = mr_find_next_chunk(mr, &entry, n);
394 if (mr_btree_insert(&share_cache->cache, &entry) < 0) {
396 * Overflowed, but the global table cannot be expanded
397 * because of deadlock.
406 * Look up address in the original global MR list.
409 * Pointer to a global shared MR cache.
411 * Pointer to returning MR cache entry. If no match, this will not be updated.
416 * Found MR on match, NULL otherwise.
419 mlx5_mr_lookup_list(struct mlx5_mr_share_cache *share_cache,
420 struct mr_cache_entry *entry, uintptr_t addr)
424 /* Iterate all the existing MRs. */
425 LIST_FOREACH(mr, &share_cache->mr_list, mr) {
430 for (n = 0; n < mr->ms_bmp_n; ) {
431 struct mr_cache_entry ret;
433 memset(&ret, 0, sizeof(ret));
434 n = mr_find_next_chunk(mr, &ret, n);
435 if (addr >= ret.start && addr < ret.end) {
446 * Look up address on global MR cache.
449 * Pointer to a global shared MR cache.
451 * Pointer to returning MR cache entry. If no match, this will not be updated.
456 * Searched LKey on success, UINT32_MAX on failure and rte_errno is set.
459 mlx5_mr_lookup_cache(struct mlx5_mr_share_cache *share_cache,
460 struct mr_cache_entry *entry, uintptr_t addr)
463 uint32_t lkey = UINT32_MAX;
467 * If the global cache has overflowed since it failed to expand the
468 * B-tree table, it can't have all the existing MRs. Then, the address
469 * has to be searched by traversing the original MR list instead, which
470 * is very slow path. Otherwise, the global cache is all inclusive.
472 if (!unlikely(share_cache->cache.overflow)) {
473 lkey = mr_btree_lookup(&share_cache->cache, &idx, addr);
474 if (lkey != UINT32_MAX)
475 *entry = (*share_cache->cache.table)[idx];
477 /* Falling back to the slowest path. */
478 mr = mlx5_mr_lookup_list(share_cache, entry, addr);
482 MLX5_ASSERT(lkey == UINT32_MAX || (addr >= entry->start &&
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.
492 * Pointer to MR to free.
495 mlx5_mr_free(struct mlx5_mr *mr, mlx5_dereg_mr_t dereg_mr_cb)
499 DRV_LOG(DEBUG, "freeing MR(%p):", (void *)mr);
500 dereg_mr_cb(&mr->pmd_mr);
501 if (mr->ms_bmp != NULL)
502 rte_bitmap_free(mr->ms_bmp);
507 mlx5_mr_rebuild_cache(struct mlx5_mr_share_cache *share_cache)
511 DRV_LOG(DEBUG, "Rebuild dev cache[] %p", (void *)share_cache);
512 /* Flush cache to rebuild. */
513 share_cache->cache.len = 1;
514 share_cache->cache.overflow = 0;
515 /* Iterate all the existing MRs. */
516 LIST_FOREACH(mr, &share_cache->mr_list, mr)
517 if (mlx5_mr_insert_cache(share_cache, mr) < 0)
522 * Release resources of detached MR having no online entry.
525 * Pointer to a global shared MR cache.
528 mlx5_mr_garbage_collect(struct mlx5_mr_share_cache *share_cache)
530 struct mlx5_mr *mr_next;
531 struct mlx5_mr_list free_list = LIST_HEAD_INITIALIZER(free_list);
533 /* Must be called from the primary process. */
534 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
536 * MR can't be freed with holding the lock because rte_free() could call
537 * memory free callback function. This will be a deadlock situation.
539 rte_rwlock_write_lock(&share_cache->rwlock);
540 /* Detach the whole free list and release it after unlocking. */
541 free_list = share_cache->mr_free_list;
542 LIST_INIT(&share_cache->mr_free_list);
543 rte_rwlock_write_unlock(&share_cache->rwlock);
544 /* Release resources. */
545 mr_next = LIST_FIRST(&free_list);
546 while (mr_next != NULL) {
547 struct mlx5_mr *mr = mr_next;
549 mr_next = LIST_NEXT(mr, mr);
550 mlx5_mr_free(mr, share_cache->dereg_mr_cb);
554 /* Called during rte_memseg_contig_walk() by mlx5_mr_create(). */
556 mr_find_contig_memsegs_cb(const struct rte_memseg_list *msl,
557 const struct rte_memseg *ms, size_t len, void *arg)
559 struct mr_find_contig_memsegs_data *data = arg;
561 if (data->addr < ms->addr_64 || data->addr >= ms->addr_64 + len)
563 /* Found, save it and stop walking. */
564 data->start = ms->addr_64;
565 data->end = ms->addr_64 + len;
571 * Create a new global Memory Region (MR) for a missing virtual address.
572 * This API should be called on a secondary process, then a request is sent to
573 * the primary process in order to create a MR for the address. As the global MR
574 * list is on the shared memory, following LKey lookup should succeed unless the
578 * Pointer to pd of a device (net, regex, vdpa,...).
580 * Pointer to a global shared MR cache.
582 * Pointer to returning MR cache entry, found in the global cache or newly
583 * created. If failed to create one, this will not be updated.
585 * Target virtual address to register.
586 * @param mr_ext_memseg_en
587 * Configurable flag about external memory segment enable or not.
590 * Searched LKey on success, UINT32_MAX on failure and rte_errno is set.
593 mlx5_mr_create_secondary(void *pd __rte_unused,
594 struct mlx5_mp_id *mp_id,
595 struct mlx5_mr_share_cache *share_cache,
596 struct mr_cache_entry *entry, uintptr_t addr,
597 unsigned int mr_ext_memseg_en __rte_unused)
601 DRV_LOG(DEBUG, "port %u requesting MR creation for address (%p)",
602 mp_id->port_id, (void *)addr);
603 ret = mlx5_mp_req_mr_create(mp_id, addr);
605 DRV_LOG(DEBUG, "Fail to request MR creation for address (%p)",
609 rte_rwlock_read_lock(&share_cache->rwlock);
610 /* Fill in output data. */
611 mlx5_mr_lookup_cache(share_cache, entry, addr);
612 /* Lookup can't fail. */
613 MLX5_ASSERT(entry->lkey != UINT32_MAX);
614 rte_rwlock_read_unlock(&share_cache->rwlock);
615 DRV_LOG(DEBUG, "MR CREATED by primary process for %p:\n"
616 " [0x%" PRIxPTR ", 0x%" PRIxPTR "), lkey=0x%x",
617 (void *)addr, entry->start, entry->end, entry->lkey);
622 * Create a new global Memory Region (MR) for a missing virtual address.
623 * Register entire virtually contiguous memory chunk around the address.
626 * Pointer to pd of a device (net, regex, vdpa,...).
628 * Pointer to a global shared MR cache.
630 * Pointer to returning MR cache entry, found in the global cache or newly
631 * created. If failed to create one, this will not be updated.
633 * Target virtual address to register.
634 * @param mr_ext_memseg_en
635 * Configurable flag about external memory segment enable or not.
638 * Searched LKey on success, UINT32_MAX on failure and rte_errno is set.
641 mlx5_mr_create_primary(void *pd,
642 struct mlx5_mr_share_cache *share_cache,
643 struct mr_cache_entry *entry, uintptr_t addr,
644 unsigned int mr_ext_memseg_en)
646 struct mr_find_contig_memsegs_data data = {.addr = addr, };
647 struct mr_find_contig_memsegs_data data_re;
648 const struct rte_memseg_list *msl;
649 const struct rte_memseg *ms;
650 struct mlx5_mr *mr = NULL;
651 int ms_idx_shift = -1;
658 DRV_LOG(DEBUG, "Creating a MR using address (%p)", (void *)addr);
660 * Release detached MRs if any. This can't be called with holding either
661 * memory_hotplug_lock or share_cache->rwlock. MRs on the free list have
662 * been detached by the memory free event but it couldn't be released
663 * inside the callback due to deadlock. As a result, releasing resources
664 * is quite opportunistic.
666 mlx5_mr_garbage_collect(share_cache);
668 * If enabled, find out a contiguous virtual address chunk in use, to
669 * which the given address belongs, in order to register maximum range.
670 * In the best case where mempools are not dynamically recreated and
671 * '--socket-mem' is specified as an EAL option, it is very likely to
672 * have only one MR(LKey) per a socket and per a hugepage-size even
673 * though the system memory is highly fragmented. As the whole memory
674 * chunk will be pinned by kernel, it can't be reused unless entire
675 * chunk is freed from EAL.
677 * If disabled, just register one memseg (page). Then, memory
678 * consumption will be minimized but it may drop performance if there
679 * are many MRs to lookup on the datapath.
681 if (!mr_ext_memseg_en) {
682 data.msl = rte_mem_virt2memseg_list((void *)addr);
683 data.start = RTE_ALIGN_FLOOR(addr, data.msl->page_sz);
684 data.end = data.start + data.msl->page_sz;
685 } else if (!rte_memseg_contig_walk(mr_find_contig_memsegs_cb, &data)) {
687 "Unable to find virtually contiguous"
688 " chunk for address (%p)."
689 " rte_memseg_contig_walk() failed.", (void *)addr);
694 /* Addresses must be page-aligned. */
695 MLX5_ASSERT(data.msl);
696 MLX5_ASSERT(rte_is_aligned((void *)data.start, data.msl->page_sz));
697 MLX5_ASSERT(rte_is_aligned((void *)data.end, data.msl->page_sz));
699 ms = rte_mem_virt2memseg((void *)data.start, msl);
700 len = data.end - data.start;
702 MLX5_ASSERT(msl->page_sz == ms->hugepage_sz);
703 /* Number of memsegs in the range. */
704 ms_n = len / msl->page_sz;
705 DRV_LOG(DEBUG, "Extending %p to [0x%" PRIxPTR ", 0x%" PRIxPTR "),"
706 " page_sz=0x%" PRIx64 ", ms_n=%u",
707 (void *)addr, data.start, data.end, msl->page_sz, ms_n);
708 /* Size of memory for bitmap. */
709 bmp_size = rte_bitmap_get_memory_footprint(ms_n);
710 mr = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO,
711 RTE_ALIGN_CEIL(sizeof(*mr), RTE_CACHE_LINE_SIZE) +
712 bmp_size, RTE_CACHE_LINE_SIZE, msl->socket_id);
714 DRV_LOG(DEBUG, "Unable to allocate memory for a new MR of"
715 " address (%p).", (void *)addr);
721 * Save the index of the first memseg and initialize memseg bitmap. To
722 * see if a memseg of ms_idx in the memseg-list is still valid, check:
723 * rte_bitmap_get(mr->bmp, ms_idx - mr->ms_base_idx)
725 mr->ms_base_idx = rte_fbarray_find_idx(&msl->memseg_arr, ms);
726 bmp_mem = RTE_PTR_ALIGN_CEIL(mr + 1, RTE_CACHE_LINE_SIZE);
727 mr->ms_bmp = rte_bitmap_init(ms_n, bmp_mem, bmp_size);
728 if (mr->ms_bmp == NULL) {
729 DRV_LOG(DEBUG, "Unable to initialize bitmap for a new MR of"
730 " address (%p).", (void *)addr);
735 * Should recheck whether the extended contiguous chunk is still valid.
736 * Because memory_hotplug_lock can't be held if there's any memory
737 * related calls in a critical path, resource allocation above can't be
738 * locked. If the memory has been changed at this point, try again with
739 * just single page. If not, go on with the big chunk atomically from
742 rte_mcfg_mem_read_lock();
744 if (len > msl->page_sz &&
745 !rte_memseg_contig_walk(mr_find_contig_memsegs_cb, &data_re)) {
747 "Unable to find virtually contiguous chunk for address "
748 "(%p). rte_memseg_contig_walk() failed.", (void *)addr);
752 if (data.start != data_re.start || data.end != data_re.end) {
754 * The extended contiguous chunk has been changed. Try again
755 * with single memseg instead.
757 data.start = RTE_ALIGN_FLOOR(addr, msl->page_sz);
758 data.end = data.start + msl->page_sz;
759 rte_mcfg_mem_read_unlock();
760 mlx5_mr_free(mr, share_cache->dereg_mr_cb);
761 goto alloc_resources;
763 MLX5_ASSERT(data.msl == data_re.msl);
764 rte_rwlock_write_lock(&share_cache->rwlock);
766 * Check the address is really missing. If other thread already created
767 * one or it is not found due to overflow, abort and return.
769 if (mlx5_mr_lookup_cache(share_cache, entry, addr) != UINT32_MAX) {
771 * Insert to the global cache table. It may fail due to
772 * low-on-memory. Then, this entry will have to be searched
775 mr_btree_insert(&share_cache->cache, entry);
776 DRV_LOG(DEBUG, "Found MR for %p on final lookup, abort",
778 rte_rwlock_write_unlock(&share_cache->rwlock);
779 rte_mcfg_mem_read_unlock();
781 * Must be unlocked before calling rte_free() because
782 * mlx5_mr_mem_event_free_cb() can be called inside.
784 mlx5_mr_free(mr, share_cache->dereg_mr_cb);
788 * Trim start and end addresses for verbs MR. Set bits for registering
789 * memsegs but exclude already registered ones. Bitmap can be
792 for (n = 0; n < ms_n; ++n) {
794 struct mr_cache_entry ret;
796 memset(&ret, 0, sizeof(ret));
797 start = data_re.start + n * msl->page_sz;
798 /* Exclude memsegs already registered by other MRs. */
799 if (mlx5_mr_lookup_cache(share_cache, &ret, start) ==
802 * Start from the first unregistered memseg in the
805 if (ms_idx_shift == -1) {
806 mr->ms_base_idx += n;
810 data.end = start + msl->page_sz;
811 rte_bitmap_set(mr->ms_bmp, n - ms_idx_shift);
815 len = data.end - data.start;
816 mr->ms_bmp_n = len / msl->page_sz;
817 MLX5_ASSERT(ms_idx_shift + mr->ms_bmp_n <= ms_n);
819 * Finally create an MR for the memory chunk. Verbs: ibv_reg_mr() can
820 * be called with holding the memory lock because it doesn't use
821 * mlx5_alloc_buf_extern() which eventually calls rte_malloc_socket()
822 * through mlx5_alloc_verbs_buf().
824 share_cache->reg_mr_cb(pd, (void *)data.start, len, &mr->pmd_mr);
825 if (mr->pmd_mr.obj == NULL) {
826 DRV_LOG(DEBUG, "Fail to create an MR for address (%p)",
831 MLX5_ASSERT((uintptr_t)mr->pmd_mr.addr == data.start);
832 MLX5_ASSERT(mr->pmd_mr.len);
833 LIST_INSERT_HEAD(&share_cache->mr_list, mr, mr);
834 DRV_LOG(DEBUG, "MR CREATED (%p) for %p:\n"
835 " [0x%" PRIxPTR ", 0x%" PRIxPTR "),"
836 " lkey=0x%x base_idx=%u ms_n=%u, ms_bmp_n=%u",
837 (void *)mr, (void *)addr, data.start, data.end,
838 rte_cpu_to_be_32(mr->pmd_mr.lkey),
839 mr->ms_base_idx, mr->ms_n, mr->ms_bmp_n);
840 /* Insert to the global cache table. */
841 mlx5_mr_insert_cache(share_cache, mr);
842 /* Fill in output data. */
843 mlx5_mr_lookup_cache(share_cache, entry, addr);
844 /* Lookup can't fail. */
845 MLX5_ASSERT(entry->lkey != UINT32_MAX);
846 rte_rwlock_write_unlock(&share_cache->rwlock);
847 rte_mcfg_mem_read_unlock();
850 rte_rwlock_write_unlock(&share_cache->rwlock);
852 rte_mcfg_mem_read_unlock();
855 * In case of error, as this can be called in a datapath, a warning
856 * message per an error is preferable instead. Must be unlocked before
857 * calling rte_free() because mlx5_mr_mem_event_free_cb() can be called
860 mlx5_mr_free(mr, share_cache->dereg_mr_cb);
865 * Create a new global Memory Region (MR) for a missing virtual address.
866 * This can be called from primary and secondary process.
869 * Pointer to pd handle of a device (net, regex, vdpa,...).
871 * Pointer to a global shared MR cache.
873 * Pointer to returning MR cache entry, found in the global cache or newly
874 * created. If failed to create one, this will not be updated.
876 * Target virtual address to register.
879 * Searched LKey on success, UINT32_MAX on failure and rte_errno is set.
882 mlx5_mr_create(void *pd, struct mlx5_mp_id *mp_id,
883 struct mlx5_mr_share_cache *share_cache,
884 struct mr_cache_entry *entry, uintptr_t addr,
885 unsigned int mr_ext_memseg_en)
889 switch (rte_eal_process_type()) {
890 case RTE_PROC_PRIMARY:
891 ret = mlx5_mr_create_primary(pd, share_cache, entry,
892 addr, mr_ext_memseg_en);
894 case RTE_PROC_SECONDARY:
895 ret = mlx5_mr_create_secondary(pd, mp_id, share_cache, entry,
896 addr, mr_ext_memseg_en);
905 * Look up address in the global MR cache table. If not found, create a new MR.
906 * Insert the found/created entry to local bottom-half cache table.
909 * Pointer to pd of a device (net, regex, vdpa,...).
911 * Pointer to a global shared MR cache.
913 * Pointer to per-queue MR control structure.
915 * Pointer to returning MR cache entry, found in the global cache or newly
916 * created. If failed to create one, this is not written.
921 * Searched LKey on success, UINT32_MAX on no match.
924 mr_lookup_caches(void *pd, struct mlx5_mp_id *mp_id,
925 struct mlx5_mr_share_cache *share_cache,
926 struct mlx5_mr_ctrl *mr_ctrl,
927 struct mr_cache_entry *entry, uintptr_t addr,
928 unsigned int mr_ext_memseg_en)
930 struct mlx5_mr_btree *bt = &mr_ctrl->cache_bh;
934 /* If local cache table is full, try to double it. */
935 if (unlikely(bt->len == bt->size))
936 mr_btree_expand(bt, bt->size << 1);
937 /* Look up in the global cache. */
938 rte_rwlock_read_lock(&share_cache->rwlock);
939 lkey = mr_btree_lookup(&share_cache->cache, &idx, addr);
940 if (lkey != UINT32_MAX) {
942 *entry = (*share_cache->cache.table)[idx];
943 rte_rwlock_read_unlock(&share_cache->rwlock);
945 * Update local cache. Even if it fails, return the found entry
946 * to update top-half cache. Next time, this entry will be found
947 * in the global cache.
949 mr_btree_insert(bt, entry);
952 rte_rwlock_read_unlock(&share_cache->rwlock);
953 /* First time to see the address? Create a new MR. */
954 lkey = mlx5_mr_create(pd, mp_id, share_cache, entry, addr,
957 * Update the local cache if successfully created a new global MR. Even
958 * if failed to create one, there's no action to take in this datapath
959 * code. As returning LKey is invalid, this will eventually make HW
962 if (lkey != UINT32_MAX)
963 mr_btree_insert(bt, entry);
968 * Bottom-half of LKey search on datapath. First search in cache_bh[] and if
969 * misses, search in the global MR cache table and update the new entry to
970 * per-queue local caches.
973 * Pointer to pd of a device (net, regex, vdpa,...).
975 * Pointer to a global shared MR cache.
977 * Pointer to per-queue MR control structure.
982 * Searched LKey on success, UINT32_MAX on no match.
984 uint32_t mlx5_mr_addr2mr_bh(void *pd, struct mlx5_mp_id *mp_id,
985 struct mlx5_mr_share_cache *share_cache,
986 struct mlx5_mr_ctrl *mr_ctrl,
987 uintptr_t addr, unsigned int mr_ext_memseg_en)
991 /* Victim in top-half cache to replace with new entry. */
992 struct mr_cache_entry *repl = &mr_ctrl->cache[mr_ctrl->head];
994 /* Binary-search MR translation table. */
995 lkey = mr_btree_lookup(&mr_ctrl->cache_bh, &bh_idx, addr);
996 /* Update top-half cache. */
997 if (likely(lkey != UINT32_MAX)) {
998 *repl = (*mr_ctrl->cache_bh.table)[bh_idx];
1001 * If missed in local lookup table, search in the global cache
1002 * and local cache_bh[] will be updated inside if possible.
1003 * Top-half cache entry will also be updated.
1005 lkey = mr_lookup_caches(pd, mp_id, share_cache, mr_ctrl,
1006 repl, addr, mr_ext_memseg_en);
1007 if (unlikely(lkey == UINT32_MAX))
1010 /* Update the most recently used entry. */
1011 mr_ctrl->mru = mr_ctrl->head;
1012 /* Point to the next victim, the oldest. */
1013 mr_ctrl->head = (mr_ctrl->head + 1) % MLX5_MR_CACHE_N;
1018 * Release all the created MRs and resources on global MR cache of a device.
1021 * @param share_cache
1022 * Pointer to a global shared MR cache.
1025 mlx5_mr_release_cache(struct mlx5_mr_share_cache *share_cache)
1027 struct mlx5_mr *mr_next;
1029 rte_rwlock_write_lock(&share_cache->rwlock);
1030 /* Detach from MR list and move to free list. */
1031 mr_next = LIST_FIRST(&share_cache->mr_list);
1032 while (mr_next != NULL) {
1033 struct mlx5_mr *mr = mr_next;
1035 mr_next = LIST_NEXT(mr, mr);
1036 LIST_REMOVE(mr, mr);
1037 LIST_INSERT_HEAD(&share_cache->mr_free_list, mr, mr);
1039 LIST_INIT(&share_cache->mr_list);
1040 /* Free global cache. */
1041 mlx5_mr_btree_free(&share_cache->cache);
1042 rte_rwlock_write_unlock(&share_cache->rwlock);
1043 /* Free all remaining MRs. */
1044 mlx5_mr_garbage_collect(share_cache);
1048 * Flush all of the local cache entries.
1051 * Pointer to per-queue MR local cache.
1054 mlx5_mr_flush_local_cache(struct mlx5_mr_ctrl *mr_ctrl)
1056 /* Reset the most-recently-used index. */
1058 /* Reset the linear search array. */
1060 memset(mr_ctrl->cache, 0, sizeof(mr_ctrl->cache));
1061 /* Reset the B-tree table. */
1062 mr_ctrl->cache_bh.len = 1;
1063 mr_ctrl->cache_bh.overflow = 0;
1064 /* Update the generation number. */
1065 mr_ctrl->cur_gen = *mr_ctrl->dev_gen_ptr;
1066 DRV_LOG(DEBUG, "mr_ctrl(%p): flushed, cur_gen=%d",
1067 (void *)mr_ctrl, mr_ctrl->cur_gen);
1071 * Creates a memory region for external memory, that is memory which is not
1072 * part of the DPDK memory segments.
1075 * Pointer to pd of a device (net, regex, vdpa,...).
1077 * Starting virtual address of memory.
1079 * Length of memory segment being mapped.
1081 * Socket to allocate heap memory for the control structures.
1084 * Pointer to MR structure on success, NULL otherwise.
1087 mlx5_create_mr_ext(void *pd, uintptr_t addr, size_t len, int socket_id,
1088 mlx5_reg_mr_t reg_mr_cb)
1090 struct mlx5_mr *mr = NULL;
1092 mr = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO,
1093 RTE_ALIGN_CEIL(sizeof(*mr), RTE_CACHE_LINE_SIZE),
1094 RTE_CACHE_LINE_SIZE, socket_id);
1097 reg_mr_cb(pd, (void *)addr, len, &mr->pmd_mr);
1098 if (mr->pmd_mr.obj == NULL) {
1100 "Fail to create MR for address (%p)",
1105 mr->msl = NULL; /* Mark it is external memory. */
1110 "MR CREATED (%p) for external memory %p:\n"
1111 " [0x%" PRIxPTR ", 0x%" PRIxPTR "),"
1112 " lkey=0x%x base_idx=%u ms_n=%u, ms_bmp_n=%u",
1113 (void *)mr, (void *)addr,
1114 addr, addr + len, rte_cpu_to_be_32(mr->pmd_mr.lkey),
1115 mr->ms_base_idx, mr->ms_n, mr->ms_bmp_n);
1120 * Callback for memory free event. Iterate freed memsegs and check whether it
1121 * belongs to an existing MR. If found, clear the bit from bitmap of MR. As a
1122 * result, the MR would be fragmented. If it becomes empty, the MR will be freed
1123 * later by mlx5_mr_garbage_collect(). Even if this callback is called from a
1124 * secondary process, the garbage collector will be called in primary process
1125 * as the secondary process can't call mlx5_mr_create().
1127 * The global cache must be rebuilt if there's any change and this event has to
1128 * be propagated to dataplane threads to flush the local caches.
1130 * @param share_cache
1131 * Pointer to a global shared MR cache.
1133 * Name of ibv device.
1135 * Address of freed memory.
1137 * Size of freed memory.
1140 mlx5_free_mr_by_addr(struct mlx5_mr_share_cache *share_cache,
1141 const char *ibdev_name, const void *addr, size_t len)
1143 const struct rte_memseg_list *msl;
1149 DRV_LOG(DEBUG, "device %s free callback: addr=%p, len=%zu",
1150 ibdev_name, addr, len);
1151 msl = rte_mem_virt2memseg_list(addr);
1152 /* addr and len must be page-aligned. */
1153 MLX5_ASSERT((uintptr_t)addr ==
1154 RTE_ALIGN((uintptr_t)addr, msl->page_sz));
1155 MLX5_ASSERT(len == RTE_ALIGN(len, msl->page_sz));
1156 ms_n = len / msl->page_sz;
1157 rte_rwlock_write_lock(&share_cache->rwlock);
1158 /* Clear bits of freed memsegs from MR. */
1159 for (i = 0; i < ms_n; ++i) {
1160 const struct rte_memseg *ms;
1161 struct mr_cache_entry entry;
1166 /* Find MR having this memseg. */
1167 start = (uintptr_t)addr + i * msl->page_sz;
1168 mr = mlx5_mr_lookup_list(share_cache, &entry, start);
1171 MLX5_ASSERT(mr->msl); /* Can't be external memory. */
1172 ms = rte_mem_virt2memseg((void *)start, msl);
1173 MLX5_ASSERT(ms != NULL);
1174 MLX5_ASSERT(msl->page_sz == ms->hugepage_sz);
1175 ms_idx = rte_fbarray_find_idx(&msl->memseg_arr, ms);
1176 pos = ms_idx - mr->ms_base_idx;
1177 MLX5_ASSERT(rte_bitmap_get(mr->ms_bmp, pos));
1178 MLX5_ASSERT(pos < mr->ms_bmp_n);
1179 DRV_LOG(DEBUG, "device %s MR(%p): clear bitmap[%u] for addr %p",
1180 ibdev_name, (void *)mr, pos, (void *)start);
1181 rte_bitmap_clear(mr->ms_bmp, pos);
1182 if (--mr->ms_n == 0) {
1183 LIST_REMOVE(mr, mr);
1184 LIST_INSERT_HEAD(&share_cache->mr_free_list, mr, mr);
1185 DRV_LOG(DEBUG, "device %s remove MR(%p) from list",
1186 ibdev_name, (void *)mr);
1189 * MR is fragmented or will be freed. the global cache must be
1195 mlx5_mr_rebuild_cache(share_cache);
1197 * No explicit wmb is needed after updating dev_gen due to
1198 * store-release ordering in unlock that provides the
1199 * implicit barrier at the software visible level.
1201 ++share_cache->dev_gen;
1202 DRV_LOG(DEBUG, "broadcasting local cache flush, gen=%d",
1203 share_cache->dev_gen);
1205 rte_rwlock_write_unlock(&share_cache->rwlock);
1209 * Dump all the created MRs and the global cache entries.
1212 * Pointer to Ethernet device shared context.
1215 mlx5_mr_dump_cache(struct mlx5_mr_share_cache *share_cache __rte_unused)
1217 #ifdef RTE_LIBRTE_MLX5_DEBUG
1222 rte_rwlock_read_lock(&share_cache->rwlock);
1223 /* Iterate all the existing MRs. */
1224 LIST_FOREACH(mr, &share_cache->mr_list, mr) {
1227 DRV_LOG(DEBUG, "MR[%u], LKey = 0x%x, ms_n = %u, ms_bmp_n = %u",
1228 mr_n++, rte_cpu_to_be_32(mr->pmd_mr.lkey),
1229 mr->ms_n, mr->ms_bmp_n);
1232 for (n = 0; n < mr->ms_bmp_n; ) {
1233 struct mr_cache_entry ret = { 0, };
1235 n = mr_find_next_chunk(mr, &ret, n);
1239 " chunk[%u], [0x%" PRIxPTR ", 0x%" PRIxPTR ")",
1240 chunk_n++, ret.start, ret.end);
1243 DRV_LOG(DEBUG, "Dumping global cache %p", (void *)share_cache);
1244 mlx5_mr_btree_dump(&share_cache->cache);
1245 rte_rwlock_read_unlock(&share_cache->rwlock);
1250 mlx5_range_compare_start(const void *lhs, const void *rhs)
1252 const struct mlx5_range *r1 = lhs, *r2 = rhs;
1254 if (r1->start > r2->start)
1256 else if (r1->start < r2->start)
1262 mlx5_range_from_mempool_chunk(struct rte_mempool *mp, void *opaque,
1263 struct rte_mempool_memhdr *memhdr,
1266 struct mlx5_range *ranges = opaque, *range = &ranges[idx];
1267 uint64_t page_size = rte_mem_page_size();
1270 range->start = RTE_ALIGN_FLOOR((uintptr_t)memhdr->addr, page_size);
1271 range->end = RTE_ALIGN_CEIL(range->start + memhdr->len, page_size);
1275 * Get VA-contiguous ranges of the mempool memory.
1276 * Each range start and end is aligned to the system page size.
1281 * Receives the ranges, caller must release it with free().
1282 * @param[out] ount_n
1283 * Receives the number of @p out elements.
1286 * 0 on success, (-1) on failure.
1289 mlx5_get_mempool_ranges(struct rte_mempool *mp, struct mlx5_range **out,
1290 unsigned int *out_n)
1292 struct mlx5_range *chunks;
1293 unsigned int chunks_n = mp->nb_mem_chunks, contig_n, i;
1295 /* Collect page-aligned memory ranges of the mempool. */
1296 chunks = calloc(sizeof(chunks[0]), chunks_n);
1299 rte_mempool_mem_iter(mp, mlx5_range_from_mempool_chunk, chunks);
1300 /* Merge adjacent chunks and place them at the beginning. */
1301 qsort(chunks, chunks_n, sizeof(chunks[0]), mlx5_range_compare_start);
1303 for (i = 1; i < chunks_n; i++)
1304 if (chunks[i - 1].end != chunks[i].start) {
1305 chunks[contig_n - 1].end = chunks[i - 1].end;
1306 chunks[contig_n] = chunks[i];
1309 /* Extend the last contiguous chunk to the end of the mempool. */
1310 chunks[contig_n - 1].end = chunks[i - 1].end;
1317 * Analyze mempool memory to select memory ranges to register.
1320 * Mempool to analyze.
1322 * Receives memory ranges to register, aligned to the system page size.
1323 * The caller must release them with free().
1325 * Receives the number of @p out items.
1326 * @param[out] share_hugepage
1327 * Receives True if the entire pool resides within a single hugepage.
1330 * 0 on success, (-1) on failure.
1333 mlx5_mempool_reg_analyze(struct rte_mempool *mp, struct mlx5_range **out,
1334 unsigned int *out_n, bool *share_hugepage)
1336 struct mlx5_range *ranges = NULL;
1337 unsigned int i, ranges_n = 0;
1338 struct rte_memseg_list *msl;
1340 if (mlx5_get_mempool_ranges(mp, &ranges, &ranges_n) < 0) {
1341 DRV_LOG(ERR, "Cannot get address ranges for mempool %s",
1345 /* Check if the hugepage of the pool can be shared. */
1346 *share_hugepage = false;
1347 msl = rte_mem_virt2memseg_list((void *)ranges[0].start);
1349 uint64_t hugepage_sz = 0;
1351 /* Check that all ranges are on pages of the same size. */
1352 for (i = 0; i < ranges_n; i++) {
1353 if (hugepage_sz != 0 && hugepage_sz != msl->page_sz)
1355 hugepage_sz = msl->page_sz;
1357 if (i == ranges_n) {
1359 * If the entire pool is within one hugepage,
1360 * combine all ranges into one of the hugepage size.
1362 uintptr_t reg_start = ranges[0].start;
1363 uintptr_t reg_end = ranges[ranges_n - 1].end;
1364 uintptr_t hugepage_start =
1365 RTE_ALIGN_FLOOR(reg_start, hugepage_sz);
1366 uintptr_t hugepage_end = hugepage_start + hugepage_sz;
1367 if (reg_end < hugepage_end) {
1368 ranges[0].start = hugepage_start;
1369 ranges[0].end = hugepage_end;
1371 *share_hugepage = true;
1380 /** Create a registration object for the mempool. */
1381 static struct mlx5_mempool_reg *
1382 mlx5_mempool_reg_create(struct rte_mempool *mp, unsigned int mrs_n)
1384 struct mlx5_mempool_reg *mpr = NULL;
1386 mpr = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO,
1387 sizeof(*mpr) + mrs_n * sizeof(mpr->mrs[0]),
1388 RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
1390 DRV_LOG(ERR, "Cannot allocate mempool %s registration object",
1395 mpr->mrs = (struct mlx5_mempool_mr *)(mpr + 1);
1401 * Destroy a mempool registration object.
1404 * Whether @p mpr owns its MRs excludively, i.e. they are not shared.
1407 mlx5_mempool_reg_destroy(struct mlx5_mr_share_cache *share_cache,
1408 struct mlx5_mempool_reg *mpr, bool standalone)
1413 for (i = 0; i < mpr->mrs_n; i++)
1414 share_cache->dereg_mr_cb(&mpr->mrs[i].pmd_mr);
1419 /** Find registration object of a mempool. */
1420 static struct mlx5_mempool_reg *
1421 mlx5_mempool_reg_lookup(struct mlx5_mr_share_cache *share_cache,
1422 struct rte_mempool *mp)
1424 struct mlx5_mempool_reg *mpr;
1426 LIST_FOREACH(mpr, &share_cache->mempool_reg_list, next)
1432 /** Increment reference counters of MRs used in the registration. */
1434 mlx5_mempool_reg_attach(struct mlx5_mempool_reg *mpr)
1438 for (i = 0; i < mpr->mrs_n; i++)
1439 __atomic_add_fetch(&mpr->mrs[i].refcnt, 1, __ATOMIC_RELAXED);
1443 * Decrement reference counters of MRs used in the registration.
1445 * @return True if no more references to @p mpr MRs exist, False otherwise.
1448 mlx5_mempool_reg_detach(struct mlx5_mempool_reg *mpr)
1453 for (i = 0; i < mpr->mrs_n; i++)
1454 ret |= __atomic_sub_fetch(&mpr->mrs[i].refcnt, 1,
1455 __ATOMIC_RELAXED) == 0;
1460 mlx5_mr_mempool_register_primary(struct mlx5_mr_share_cache *share_cache,
1461 void *pd, struct rte_mempool *mp)
1463 struct mlx5_range *ranges = NULL;
1464 struct mlx5_mempool_reg *mpr, *new_mpr;
1465 unsigned int i, ranges_n;
1466 bool share_hugepage;
1469 /* Early check to avoid unnecessary creation of MRs. */
1470 rte_rwlock_read_lock(&share_cache->rwlock);
1471 mpr = mlx5_mempool_reg_lookup(share_cache, mp);
1472 rte_rwlock_read_unlock(&share_cache->rwlock);
1474 DRV_LOG(DEBUG, "Mempool %s is already registered for PD %p",
1479 if (mlx5_mempool_reg_analyze(mp, &ranges, &ranges_n,
1480 &share_hugepage) < 0) {
1481 DRV_LOG(ERR, "Cannot get mempool %s memory ranges", mp->name);
1485 new_mpr = mlx5_mempool_reg_create(mp, ranges_n);
1486 if (new_mpr == NULL) {
1488 "Cannot create a registration object for mempool %s in PD %p",
1494 * If the entire mempool fits in a single hugepage, the MR for this
1495 * hugepage can be shared across mempools that also fit in it.
1497 if (share_hugepage) {
1498 rte_rwlock_write_lock(&share_cache->rwlock);
1499 LIST_FOREACH(mpr, &share_cache->mempool_reg_list, next) {
1500 if (mpr->mrs[0].pmd_mr.addr == (void *)ranges[0].start)
1504 new_mpr->mrs = mpr->mrs;
1505 mlx5_mempool_reg_attach(new_mpr);
1506 LIST_INSERT_HEAD(&share_cache->mempool_reg_list,
1509 rte_rwlock_write_unlock(&share_cache->rwlock);
1511 DRV_LOG(DEBUG, "Shared MR %#x in PD %p for mempool %s with mempool %s",
1512 mpr->mrs[0].pmd_mr.lkey, pd, mp->name,
1518 for (i = 0; i < ranges_n; i++) {
1519 struct mlx5_mempool_mr *mr = &new_mpr->mrs[i];
1520 const struct mlx5_range *range = &ranges[i];
1521 size_t len = range->end - range->start;
1523 if (share_cache->reg_mr_cb(pd, (void *)range->start, len,
1526 "Failed to create an MR in PD %p for address range "
1527 "[0x%" PRIxPTR ", 0x%" PRIxPTR "] (%zu bytes) for mempool %s",
1528 pd, range->start, range->end, len, mp->name);
1532 "Created a new MR %#x in PD %p for address range "
1533 "[0x%" PRIxPTR ", 0x%" PRIxPTR "] (%zu bytes) for mempool %s",
1534 mr->pmd_mr.lkey, pd, range->start, range->end, len,
1537 if (i != ranges_n) {
1538 mlx5_mempool_reg_destroy(share_cache, new_mpr, true);
1542 /* Concurrent registration is not supposed to happen. */
1543 rte_rwlock_write_lock(&share_cache->rwlock);
1544 mpr = mlx5_mempool_reg_lookup(share_cache, mp);
1546 mlx5_mempool_reg_attach(new_mpr);
1547 LIST_INSERT_HEAD(&share_cache->mempool_reg_list,
1551 rte_rwlock_write_unlock(&share_cache->rwlock);
1553 DRV_LOG(DEBUG, "Mempool %s is already registered for PD %p",
1555 mlx5_mempool_reg_destroy(share_cache, new_mpr, true);
1565 mlx5_mr_mempool_register_secondary(struct mlx5_mr_share_cache *share_cache,
1566 void *pd, struct rte_mempool *mp,
1567 struct mlx5_mp_id *mp_id)
1569 if (mp_id == NULL) {
1573 return mlx5_mp_req_mempool_reg(mp_id, share_cache, pd, mp, true);
1577 * Register the memory of a mempool in the protection domain.
1579 * @param share_cache
1580 * Shared MR cache of the protection domain.
1582 * Protection domain object.
1584 * Mempool to register.
1586 * Multi-process identifier, may be NULL for the primary process.
1589 * 0 on success, (-1) on failure and rte_errno is set.
1592 mlx5_mr_mempool_register(struct mlx5_mr_share_cache *share_cache, void *pd,
1593 struct rte_mempool *mp, struct mlx5_mp_id *mp_id)
1595 if (mp->flags & RTE_MEMPOOL_F_NON_IO)
1597 switch (rte_eal_process_type()) {
1598 case RTE_PROC_PRIMARY:
1599 return mlx5_mr_mempool_register_primary(share_cache, pd, mp);
1600 case RTE_PROC_SECONDARY:
1601 return mlx5_mr_mempool_register_secondary(share_cache, pd, mp,
1609 mlx5_mr_mempool_unregister_primary(struct mlx5_mr_share_cache *share_cache,
1610 struct rte_mempool *mp)
1612 struct mlx5_mempool_reg *mpr;
1613 bool standalone = false;
1615 rte_rwlock_write_lock(&share_cache->rwlock);
1616 LIST_FOREACH(mpr, &share_cache->mempool_reg_list, next)
1617 if (mpr->mp == mp) {
1618 LIST_REMOVE(mpr, next);
1619 standalone = mlx5_mempool_reg_detach(mpr);
1622 * The unlock operation below provides a memory
1623 * barrier due to its store-release semantics.
1625 ++share_cache->dev_gen;
1628 rte_rwlock_write_unlock(&share_cache->rwlock);
1633 mlx5_mempool_reg_destroy(share_cache, mpr, standalone);
1638 mlx5_mr_mempool_unregister_secondary(struct mlx5_mr_share_cache *share_cache,
1639 struct rte_mempool *mp,
1640 struct mlx5_mp_id *mp_id)
1642 if (mp_id == NULL) {
1646 return mlx5_mp_req_mempool_reg(mp_id, share_cache, NULL, mp, false);
1650 * Unregister the memory of a mempool from the protection domain.
1652 * @param share_cache
1653 * Shared MR cache of the protection domain.
1655 * Mempool to unregister.
1657 * Multi-process identifier, may be NULL for the primary process.
1660 * 0 on success, (-1) on failure and rte_errno is set.
1663 mlx5_mr_mempool_unregister(struct mlx5_mr_share_cache *share_cache,
1664 struct rte_mempool *mp, struct mlx5_mp_id *mp_id)
1666 if (mp->flags & RTE_MEMPOOL_F_NON_IO)
1668 switch (rte_eal_process_type()) {
1669 case RTE_PROC_PRIMARY:
1670 return mlx5_mr_mempool_unregister_primary(share_cache, mp);
1671 case RTE_PROC_SECONDARY:
1672 return mlx5_mr_mempool_unregister_secondary(share_cache, mp,
1680 * Lookup a MR key by and address in a registered mempool.
1683 * Mempool registration object.
1685 * Address within the mempool.
1687 * Bottom-half cache entry to fill.
1690 * MR key or UINT32_MAX on failure, which can only happen
1691 * if the address is not from within the mempool.
1694 mlx5_mempool_reg_addr2mr(struct mlx5_mempool_reg *mpr, uintptr_t addr,
1695 struct mr_cache_entry *entry)
1697 uint32_t lkey = UINT32_MAX;
1700 for (i = 0; i < mpr->mrs_n; i++) {
1701 const struct mlx5_pmd_mr *mr = &mpr->mrs[i].pmd_mr;
1702 uintptr_t mr_addr = (uintptr_t)mr->addr;
1704 if (mr_addr <= addr) {
1705 lkey = rte_cpu_to_be_32(mr->lkey);
1706 entry->start = mr_addr;
1707 entry->end = mr_addr + mr->len;
1716 * Update bottom-half cache from the list of mempool registrations.
1718 * @param share_cache
1719 * Pointer to a global shared MR cache.
1721 * Per-queue MR control handle.
1723 * Pointer to an entry in the bottom-half cache to update
1724 * with the MR lkey looked up.
1726 * Mempool containing the address.
1728 * Address to lookup.
1730 * MR lkey on success, UINT32_MAX on failure.
1733 mlx5_lookup_mempool_regs(struct mlx5_mr_share_cache *share_cache,
1734 struct mlx5_mr_ctrl *mr_ctrl,
1735 struct mr_cache_entry *entry,
1736 struct rte_mempool *mp, uintptr_t addr)
1738 struct mlx5_mr_btree *bt = &mr_ctrl->cache_bh;
1739 struct mlx5_mempool_reg *mpr;
1740 uint32_t lkey = UINT32_MAX;
1742 /* If local cache table is full, try to double it. */
1743 if (unlikely(bt->len == bt->size))
1744 mr_btree_expand(bt, bt->size << 1);
1745 /* Look up in mempool registrations. */
1746 rte_rwlock_read_lock(&share_cache->rwlock);
1747 mpr = mlx5_mempool_reg_lookup(share_cache, mp);
1749 lkey = mlx5_mempool_reg_addr2mr(mpr, addr, entry);
1750 rte_rwlock_read_unlock(&share_cache->rwlock);
1752 * Update local cache. Even if it fails, return the found entry
1753 * to update top-half cache. Next time, this entry will be found
1754 * in the global cache.
1756 if (lkey != UINT32_MAX)
1757 mr_btree_insert(bt, entry);
1762 * Bottom-half lookup for the address from the mempool.
1764 * @param share_cache
1765 * Pointer to a global shared MR cache.
1767 * Per-queue MR control handle.
1769 * Mempool containing the address.
1771 * Address to lookup.
1773 * MR lkey on success, UINT32_MAX on failure.
1776 mlx5_mr_mempool2mr_bh(struct mlx5_mr_share_cache *share_cache,
1777 struct mlx5_mr_ctrl *mr_ctrl,
1778 struct rte_mempool *mp, uintptr_t addr)
1780 struct mr_cache_entry *repl = &mr_ctrl->cache[mr_ctrl->head];
1782 uint16_t bh_idx = 0;
1784 /* Binary-search MR translation table. */
1785 lkey = mr_btree_lookup(&mr_ctrl->cache_bh, &bh_idx, addr);
1786 /* Update top-half cache. */
1787 if (likely(lkey != UINT32_MAX)) {
1788 *repl = (*mr_ctrl->cache_bh.table)[bh_idx];
1790 lkey = mlx5_lookup_mempool_regs(share_cache, mr_ctrl, repl,
1792 /* Can only fail if the address is not from the mempool. */
1793 if (unlikely(lkey == UINT32_MAX))
1796 /* Update the most recently used entry. */
1797 mr_ctrl->mru = mr_ctrl->head;
1798 /* Point to the next victim, the oldest. */
1799 mr_ctrl->head = (mr_ctrl->head + 1) % MLX5_MR_CACHE_N;