1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2017 6WIND S.A.
3 * Copyright 2017 Mellanox Technologies, Ltd
8 * Memory management functions for mlx4 driver.
18 /* Verbs headers do not support -pedantic. */
20 #pragma GCC diagnostic ignored "-Wpedantic"
22 #include <infiniband/verbs.h>
24 #pragma GCC diagnostic error "-Wpedantic"
27 #include <rte_branch_prediction.h>
28 #include <rte_common.h>
29 #include <rte_errno.h>
30 #include <rte_malloc.h>
31 #include <rte_memory.h>
32 #include <rte_mempool.h>
33 #include <rte_rwlock.h>
35 #include "mlx4_glue.h"
37 #include "mlx4_rxtx.h"
38 #include "mlx4_utils.h"
40 struct mr_find_contig_memsegs_data {
44 const struct rte_memseg_list *msl;
47 struct mr_update_mp_data {
48 struct rte_eth_dev *dev;
49 struct mlx4_mr_ctrl *mr_ctrl;
54 * Expand B-tree table to a given size. Can't be called with holding
55 * memory_hotplug_lock or priv->mr.rwlock due to rte_realloc().
58 * Pointer to B-tree structure.
60 * Number of entries for expansion.
63 * 0 on success, -1 on failure.
66 mr_btree_expand(struct mlx4_mr_btree *bt, int n)
74 * Downside of directly using rte_realloc() is that SOCKET_ID_ANY is
75 * used inside if there's no room to expand. Because this is a quite
76 * rare case and a part of very slow path, it is very acceptable.
77 * Initially cache_bh[] will be given practically enough space and once
78 * it is expanded, expansion wouldn't be needed again ever.
80 mem = rte_realloc(bt->table, n * sizeof(struct mlx4_mr_cache), 0);
82 /* Not an error, B-tree search will be skipped. */
83 WARN("failed to expand MR B-tree (%p) table", (void *)bt);
86 DEBUG("expanded MR B-tree table (size=%u)", n);
94 * Look up LKey from given B-tree lookup table, store the last index and return
98 * Pointer to B-tree structure.
100 * Pointer to index. Even on search failure, returns index where it stops
101 * searching so that index can be used when inserting a new entry.
106 * Searched LKey on success, UINT32_MAX on no match.
109 mr_btree_lookup(struct mlx4_mr_btree *bt, uint16_t *idx, uintptr_t addr)
111 struct mlx4_mr_cache *lkp_tbl;
116 lkp_tbl = *bt->table;
118 /* First entry must be NULL for comparison. */
119 assert(bt->len > 0 || (lkp_tbl[0].start == 0 &&
120 lkp_tbl[0].lkey == UINT32_MAX));
123 register uint16_t delta = n >> 1;
125 if (addr < lkp_tbl[base + delta].start) {
132 assert(addr >= lkp_tbl[base].start);
134 if (addr < lkp_tbl[base].end)
135 return lkp_tbl[base].lkey;
141 * Insert an entry to B-tree lookup table.
144 * Pointer to B-tree structure.
146 * Pointer to new entry to insert.
149 * 0 on success, -1 on failure.
152 mr_btree_insert(struct mlx4_mr_btree *bt, struct mlx4_mr_cache *entry)
154 struct mlx4_mr_cache *lkp_tbl;
159 assert(bt->len <= bt->size);
161 lkp_tbl = *bt->table;
162 /* Find out the slot for insertion. */
163 if (mr_btree_lookup(bt, &idx, entry->start) != UINT32_MAX) {
164 DEBUG("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 mlx4_mr_cache);
179 memmove(&lkp_tbl[idx + 1], &lkp_tbl[idx], shift);
180 lkp_tbl[idx] = *entry;
182 DEBUG("inserted B-tree(%p)[%u],"
183 " [0x%" PRIxPTR ", 0x%" PRIxPTR ") lkey=0x%x",
184 (void *)bt, idx, entry->start, entry->end, entry->lkey);
189 * Initialize B-tree and allocate memory for lookup table.
192 * Pointer to B-tree structure.
194 * Number of entries to allocate.
196 * NUMA socket on which memory must be allocated.
199 * 0 on success, a negative errno value otherwise and rte_errno is set.
202 mlx4_mr_btree_init(struct mlx4_mr_btree *bt, int n, int socket)
208 memset(bt, 0, sizeof(*bt));
209 bt->table = rte_calloc_socket("B-tree table",
210 n, sizeof(struct mlx4_mr_cache),
212 if (bt->table == NULL) {
214 ERROR("failed to allocate memory for btree cache on socket %d",
219 /* First entry must be NULL for binary search. */
220 (*bt->table)[bt->len++] = (struct mlx4_mr_cache) {
223 DEBUG("initialized B-tree %p with table %p",
224 (void *)bt, (void *)bt->table);
229 * Free B-tree resources.
232 * Pointer to B-tree structure.
235 mlx4_mr_btree_free(struct mlx4_mr_btree *bt)
239 DEBUG("freeing B-tree %p with table %p", (void *)bt, (void *)bt->table);
241 memset(bt, 0, sizeof(*bt));
246 * Dump all the entries in a B-tree
249 * Pointer to B-tree structure.
252 mlx4_mr_btree_dump(struct mlx4_mr_btree *bt)
255 struct mlx4_mr_cache *lkp_tbl;
259 lkp_tbl = *bt->table;
260 for (idx = 0; idx < bt->len; ++idx) {
261 struct mlx4_mr_cache *entry = &lkp_tbl[idx];
263 DEBUG("B-tree(%p)[%u],"
264 " [0x%" PRIxPTR ", 0x%" PRIxPTR ") lkey=0x%x",
265 (void *)bt, idx, entry->start, entry->end, entry->lkey);
271 * Find virtually contiguous memory chunk in a given MR.
274 * Pointer to MR structure.
276 * Pointer to returning MR cache entry. If not found, this will not be
279 * Start index of the memseg bitmap.
282 * Next index to go on lookup.
285 mr_find_next_chunk(struct mlx4_mr *mr, struct mlx4_mr_cache *entry,
292 for (idx = base_idx; idx < mr->ms_bmp_n; ++idx) {
293 if (rte_bitmap_get(mr->ms_bmp, idx)) {
294 const struct rte_memseg_list *msl;
295 const struct rte_memseg *ms;
298 ms = rte_fbarray_get(&msl->memseg_arr,
299 mr->ms_base_idx + idx);
300 assert(msl->page_sz == ms->hugepage_sz);
303 end = ms->addr_64 + ms->hugepage_sz;
305 /* Passed the end of a fragment. */
310 /* Found one chunk. */
311 entry->start = start;
313 entry->lkey = rte_cpu_to_be_32(mr->ibv_mr->lkey);
319 * Insert a MR to the global B-tree cache. It may fail due to low-on-memory.
320 * Then, this entry will have to be searched by mr_lookup_dev_list() in
321 * mlx4_mr_create() on miss.
324 * Pointer to Ethernet device.
326 * Pointer to MR to insert.
329 * 0 on success, -1 on failure.
332 mr_insert_dev_cache(struct rte_eth_dev *dev, struct mlx4_mr *mr)
334 struct priv *priv = dev->data->dev_private;
337 DEBUG("port %u inserting MR(%p) to global cache",
338 dev->data->port_id, (void *)mr);
339 for (n = 0; n < mr->ms_bmp_n; ) {
340 struct mlx4_mr_cache entry = { 0, };
342 /* Find a contiguous chunk and advance the index. */
343 n = mr_find_next_chunk(mr, &entry, n);
346 if (mr_btree_insert(&priv->mr.cache, &entry) < 0) {
348 * Overflowed, but the global table cannot be expanded
349 * because of deadlock.
358 * Look up address in the original global MR list.
361 * Pointer to Ethernet device.
363 * Pointer to returning MR cache entry. If no match, this will not be updated.
368 * Found MR on match, NULL otherwise.
370 static struct mlx4_mr *
371 mr_lookup_dev_list(struct rte_eth_dev *dev, struct mlx4_mr_cache *entry,
374 struct priv *priv = dev->data->dev_private;
377 /* Iterate all the existing MRs. */
378 LIST_FOREACH(mr, &priv->mr.mr_list, mr) {
383 for (n = 0; n < mr->ms_bmp_n; ) {
384 struct mlx4_mr_cache ret = { 0, };
386 n = mr_find_next_chunk(mr, &ret, n);
387 if (addr >= ret.start && addr < ret.end) {
398 * Look up address on device.
401 * Pointer to Ethernet device.
403 * Pointer to returning MR cache entry. If no match, this will not be updated.
408 * Searched LKey on success, UINT32_MAX on failure and rte_errno is set.
411 mr_lookup_dev(struct rte_eth_dev *dev, struct mlx4_mr_cache *entry,
414 struct priv *priv = dev->data->dev_private;
416 uint32_t lkey = UINT32_MAX;
420 * If the global cache has overflowed since it failed to expand the
421 * B-tree table, it can't have all the existing MRs. Then, the address
422 * has to be searched by traversing the original MR list instead, which
423 * is very slow path. Otherwise, the global cache is all inclusive.
425 if (!unlikely(priv->mr.cache.overflow)) {
426 lkey = mr_btree_lookup(&priv->mr.cache, &idx, addr);
427 if (lkey != UINT32_MAX)
428 *entry = (*priv->mr.cache.table)[idx];
430 /* Falling back to the slowest path. */
431 mr = mr_lookup_dev_list(dev, entry, addr);
435 assert(lkey == UINT32_MAX || (addr >= entry->start &&
441 * Free MR resources. MR lock must not be held to avoid a deadlock. rte_free()
442 * can raise memory free event and the callback function will spin on the lock.
445 * Pointer to MR to free.
448 mr_free(struct mlx4_mr *mr)
452 DEBUG("freeing MR(%p):", (void *)mr);
453 if (mr->ibv_mr != NULL)
454 claim_zero(mlx4_glue->dereg_mr(mr->ibv_mr));
455 if (mr->ms_bmp != NULL)
456 rte_bitmap_free(mr->ms_bmp);
461 * Releass resources of detached MR having no online entry.
464 * Pointer to Ethernet device.
467 mlx4_mr_garbage_collect(struct rte_eth_dev *dev)
469 struct priv *priv = dev->data->dev_private;
470 struct mlx4_mr *mr_next;
471 struct mlx4_mr_list free_list = LIST_HEAD_INITIALIZER(free_list);
474 * MR can't be freed with holding the lock because rte_free() could call
475 * memory free callback function. This will be a deadlock situation.
477 rte_rwlock_write_lock(&priv->mr.rwlock);
478 /* Detach the whole free list and release it after unlocking. */
479 free_list = priv->mr.mr_free_list;
480 LIST_INIT(&priv->mr.mr_free_list);
481 rte_rwlock_write_unlock(&priv->mr.rwlock);
482 /* Release resources. */
483 mr_next = LIST_FIRST(&free_list);
484 while (mr_next != NULL) {
485 struct mlx4_mr *mr = mr_next;
487 mr_next = LIST_NEXT(mr, mr);
492 /* Called during rte_memseg_contig_walk() by mlx4_mr_create(). */
494 mr_find_contig_memsegs_cb(const struct rte_memseg_list *msl,
495 const struct rte_memseg *ms, size_t len, void *arg)
497 struct mr_find_contig_memsegs_data *data = arg;
499 if (data->addr < ms->addr_64 || data->addr >= ms->addr_64 + len)
501 /* Found, save it and stop walking. */
502 data->start = ms->addr_64;
503 data->end = ms->addr_64 + len;
509 * Create a new global Memroy Region (MR) for a missing virtual address.
510 * Register entire virtually contiguous memory chunk around the address.
513 * Pointer to Ethernet device.
515 * Pointer to returning MR cache entry, found in the global cache or newly
516 * created. If failed to create one, this will not be updated.
518 * Target virtual address to register.
521 * Searched LKey on success, UINT32_MAX on failure and rte_errno is set.
524 mlx4_mr_create(struct rte_eth_dev *dev, struct mlx4_mr_cache *entry,
527 struct priv *priv = dev->data->dev_private;
528 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
529 const struct rte_memseg_list *msl;
530 const struct rte_memseg *ms;
531 struct mlx4_mr *mr = NULL;
536 int ms_idx_shift = -1;
538 struct mr_find_contig_memsegs_data data = {
541 struct mr_find_contig_memsegs_data data_re;
543 DEBUG("port %u creating a MR using address (%p)",
544 dev->data->port_id, (void *)addr);
546 * Release detached MRs if any. This can't be called with holding either
547 * memory_hotplug_lock or priv->mr.rwlock. MRs on the free list have
548 * been detached by the memory free event but it couldn't be released
549 * inside the callback due to deadlock. As a result, releasing resources
550 * is quite opportunistic.
552 mlx4_mr_garbage_collect(dev);
554 * Find out a contiguous virtual address chunk in use, to which the
555 * given address belongs, in order to register maximum range. In the
556 * best case where mempools are not dynamically recreated and
557 * '--socket-mem' is speicified as an EAL option, it is very likely to
558 * have only one MR(LKey) per a socket and per a hugepage-size even
559 * though the system memory is highly fragmented.
561 if (!rte_memseg_contig_walk(mr_find_contig_memsegs_cb, &data)) {
562 WARN("port %u unable to find virtually contiguous"
563 " chunk for address (%p)."
564 " rte_memseg_contig_walk() failed.",
565 dev->data->port_id, (void *)addr);
570 /* Addresses must be page-aligned. */
571 assert(rte_is_aligned((void *)data.start, data.msl->page_sz));
572 assert(rte_is_aligned((void *)data.end, data.msl->page_sz));
574 ms = rte_mem_virt2memseg((void *)data.start, msl);
575 len = data.end - data.start;
576 assert(msl->page_sz == ms->hugepage_sz);
577 /* Number of memsegs in the range. */
578 ms_n = len / msl->page_sz;
579 DEBUG("port %u extending %p to [0x%" PRIxPTR ", 0x%" PRIxPTR "),"
580 " page_sz=0x%" PRIx64 ", ms_n=%u",
581 dev->data->port_id, (void *)addr,
582 data.start, data.end, msl->page_sz, ms_n);
583 /* Size of memory for bitmap. */
584 bmp_size = rte_bitmap_get_memory_footprint(ms_n);
585 mr = rte_zmalloc_socket(NULL,
586 RTE_ALIGN_CEIL(sizeof(*mr),
587 RTE_CACHE_LINE_SIZE) +
589 RTE_CACHE_LINE_SIZE, msl->socket_id);
591 WARN("port %u unable to allocate memory for a new MR of"
593 dev->data->port_id, (void *)addr);
599 * Save the index of the first memseg and initialize memseg bitmap. To
600 * see if a memseg of ms_idx in the memseg-list is still valid, check:
601 * rte_bitmap_get(mr->bmp, ms_idx - mr->ms_base_idx)
603 mr->ms_base_idx = rte_fbarray_find_idx(&msl->memseg_arr, ms);
604 bmp_mem = RTE_PTR_ALIGN_CEIL(mr + 1, RTE_CACHE_LINE_SIZE);
605 mr->ms_bmp = rte_bitmap_init(ms_n, bmp_mem, bmp_size);
606 if (mr->ms_bmp == NULL) {
607 WARN("port %u unable to initialize bitamp for a new MR of"
609 dev->data->port_id, (void *)addr);
614 * Should recheck whether the extended contiguous chunk is still valid.
615 * Because memory_hotplug_lock can't be held if there's any memory
616 * related calls in a critical path, resource allocation above can't be
617 * locked. If the memory has been changed at this point, try again with
618 * just single page. If not, go on with the big chunk atomically from
621 rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
623 if (len > msl->page_sz &&
624 !rte_memseg_contig_walk(mr_find_contig_memsegs_cb, &data_re)) {
625 WARN("port %u unable to find virtually contiguous"
626 " chunk for address (%p)."
627 " rte_memseg_contig_walk() failed.",
628 dev->data->port_id, (void *)addr);
632 if (data.start != data_re.start || data.end != data_re.end) {
634 * The extended contiguous chunk has been changed. Try again
635 * with single memseg instead.
637 data.start = RTE_ALIGN_FLOOR(addr, msl->page_sz);
638 data.end = data.start + msl->page_sz;
639 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
641 goto alloc_resources;
643 assert(data.msl == data_re.msl);
644 rte_rwlock_write_lock(&priv->mr.rwlock);
646 * Check the address is really missing. If other thread already created
647 * one or it is not found due to overflow, abort and return.
649 if (mr_lookup_dev(dev, entry, addr) != UINT32_MAX) {
651 * Insert to the global cache table. It may fail due to
652 * low-on-memory. Then, this entry will have to be searched
655 mr_btree_insert(&priv->mr.cache, entry);
656 DEBUG("port %u found MR for %p on final lookup, abort",
657 dev->data->port_id, (void *)addr);
658 rte_rwlock_write_unlock(&priv->mr.rwlock);
659 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
661 * Must be unlocked before calling rte_free() because
662 * mlx4_mr_mem_event_free_cb() can be called inside.
668 * Trim start and end addresses for verbs MR. Set bits for registering
669 * memsegs but exclude already registered ones. Bitmap can be
672 for (n = 0; n < ms_n; ++n) {
674 struct mlx4_mr_cache ret = { 0, };
676 start = data_re.start + n * msl->page_sz;
677 /* Exclude memsegs already registered by other MRs. */
678 if (mr_lookup_dev(dev, &ret, start) == UINT32_MAX) {
680 * Start from the first unregistered memseg in the
683 if (ms_idx_shift == -1) {
684 mr->ms_base_idx += n;
688 data.end = start + msl->page_sz;
689 rte_bitmap_set(mr->ms_bmp, n - ms_idx_shift);
693 len = data.end - data.start;
694 mr->ms_bmp_n = len / msl->page_sz;
695 assert(ms_idx_shift + mr->ms_bmp_n <= ms_n);
697 * Finally create a verbs MR for the memory chunk. ibv_reg_mr() can be
698 * called with holding the memory lock because it doesn't use
699 * mlx4_alloc_buf_extern() which eventually calls rte_malloc_socket()
700 * through mlx4_alloc_verbs_buf().
702 mr->ibv_mr = mlx4_glue->reg_mr(priv->pd, (void *)data.start, len,
703 IBV_ACCESS_LOCAL_WRITE);
704 if (mr->ibv_mr == NULL) {
705 WARN("port %u fail to create a verbs MR for address (%p)",
706 dev->data->port_id, (void *)addr);
710 assert((uintptr_t)mr->ibv_mr->addr == data.start);
711 assert(mr->ibv_mr->length == len);
712 LIST_INSERT_HEAD(&priv->mr.mr_list, mr, mr);
713 DEBUG("port %u MR CREATED (%p) for %p:\n"
714 " [0x%" PRIxPTR ", 0x%" PRIxPTR "),"
715 " lkey=0x%x base_idx=%u ms_n=%u, ms_bmp_n=%u",
716 dev->data->port_id, (void *)mr, (void *)addr,
717 data.start, data.end, rte_cpu_to_be_32(mr->ibv_mr->lkey),
718 mr->ms_base_idx, mr->ms_n, mr->ms_bmp_n);
719 /* Insert to the global cache table. */
720 mr_insert_dev_cache(dev, mr);
721 /* Fill in output data. */
722 mr_lookup_dev(dev, entry, addr);
723 /* Lookup can't fail. */
724 assert(entry->lkey != UINT32_MAX);
725 rte_rwlock_write_unlock(&priv->mr.rwlock);
726 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
729 rte_rwlock_write_unlock(&priv->mr.rwlock);
731 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
734 * In case of error, as this can be called in a datapath, a warning
735 * message per an error is preferable instead. Must be unlocked before
736 * calling rte_free() because mlx4_mr_mem_event_free_cb() can be called
744 * Rebuild the global B-tree cache of device from the original MR list.
747 * Pointer to Ethernet device.
750 mr_rebuild_dev_cache(struct rte_eth_dev *dev)
752 struct priv *priv = dev->data->dev_private;
755 DEBUG("port %u rebuild dev cache[]", dev->data->port_id);
756 /* Flush cache to rebuild. */
757 priv->mr.cache.len = 1;
758 priv->mr.cache.overflow = 0;
759 /* Iterate all the existing MRs. */
760 LIST_FOREACH(mr, &priv->mr.mr_list, mr)
761 if (mr_insert_dev_cache(dev, mr) < 0)
766 * Callback for memory free event. Iterate freed memsegs and check whether it
767 * belongs to an existing MR. If found, clear the bit from bitmap of MR. As a
768 * result, the MR would be fragmented. If it becomes empty, the MR will be freed
769 * later by mlx4_mr_garbage_collect().
771 * The global cache must be rebuilt if there's any change and this event has to
772 * be propagated to dataplane threads to flush the local caches.
775 * Pointer to Ethernet device.
777 * Address of freed memory.
779 * Size of freed memory.
782 mlx4_mr_mem_event_free_cb(struct rte_eth_dev *dev, const void *addr, size_t len)
784 struct priv *priv = dev->data->dev_private;
785 const struct rte_memseg_list *msl;
791 DEBUG("port %u free callback: addr=%p, len=%zu",
792 dev->data->port_id, addr, len);
793 msl = rte_mem_virt2memseg_list(addr);
794 /* addr and len must be page-aligned. */
795 assert((uintptr_t)addr == RTE_ALIGN((uintptr_t)addr, msl->page_sz));
796 assert(len == RTE_ALIGN(len, msl->page_sz));
797 ms_n = len / msl->page_sz;
798 rte_rwlock_write_lock(&priv->mr.rwlock);
799 /* Clear bits of freed memsegs from MR. */
800 for (i = 0; i < ms_n; ++i) {
801 const struct rte_memseg *ms;
802 struct mlx4_mr_cache entry;
807 /* Find MR having this memseg. */
808 start = (uintptr_t)addr + i * msl->page_sz;
809 mr = mr_lookup_dev_list(dev, &entry, start);
812 ms = rte_mem_virt2memseg((void *)start, msl);
814 assert(msl->page_sz == ms->hugepage_sz);
815 ms_idx = rte_fbarray_find_idx(&msl->memseg_arr, ms);
816 pos = ms_idx - mr->ms_base_idx;
817 assert(rte_bitmap_get(mr->ms_bmp, pos));
818 assert(pos < mr->ms_bmp_n);
819 DEBUG("port %u MR(%p): clear bitmap[%u] for addr %p",
820 dev->data->port_id, (void *)mr, pos, (void *)start);
821 rte_bitmap_clear(mr->ms_bmp, pos);
822 if (--mr->ms_n == 0) {
824 LIST_INSERT_HEAD(&priv->mr.mr_free_list, mr, mr);
825 DEBUG("port %u remove MR(%p) from list",
826 dev->data->port_id, (void *)mr);
829 * MR is fragmented or will be freed. the global cache must be
835 mr_rebuild_dev_cache(dev);
837 * Flush local caches by propagating invalidation across cores.
838 * rte_smp_wmb() is enough to synchronize this event. If one of
839 * freed memsegs is seen by other core, that means the memseg
840 * has been allocated by allocator, which will come after this
841 * free call. Therefore, this store instruction (incrementing
842 * generation below) will be guaranteed to be seen by other core
843 * before the core sees the newly allocated memory.
846 DEBUG("broadcasting local cache flush, gen=%d",
850 rte_rwlock_write_unlock(&priv->mr.rwlock);
853 mlx4_mr_dump_dev(dev);
858 * Callback for memory event.
868 mlx4_mr_mem_event_cb(enum rte_mem_event event_type, const void *addr,
869 size_t len, void *arg __rte_unused)
873 switch (event_type) {
874 case RTE_MEM_EVENT_FREE:
875 rte_rwlock_read_lock(&mlx4_mem_event_rwlock);
876 /* Iterate all the existing mlx4 devices. */
877 LIST_FOREACH(priv, &mlx4_mem_event_cb_list, mem_event_cb)
878 mlx4_mr_mem_event_free_cb(priv->dev, addr, len);
879 rte_rwlock_read_unlock(&mlx4_mem_event_rwlock);
881 case RTE_MEM_EVENT_ALLOC:
888 * Look up address in the global MR cache table. If not found, create a new MR.
889 * Insert the found/created entry to local bottom-half cache table.
892 * Pointer to Ethernet device.
894 * Pointer to per-queue MR control structure.
896 * Pointer to returning MR cache entry, found in the global cache or newly
897 * created. If failed to create one, this is not written.
902 * Searched LKey on success, UINT32_MAX on no match.
905 mlx4_mr_lookup_dev(struct rte_eth_dev *dev, struct mlx4_mr_ctrl *mr_ctrl,
906 struct mlx4_mr_cache *entry, uintptr_t addr)
908 struct priv *priv = dev->data->dev_private;
909 struct mlx4_mr_btree *bt = &mr_ctrl->cache_bh;
913 /* If local cache table is full, try to double it. */
914 if (unlikely(bt->len == bt->size))
915 mr_btree_expand(bt, bt->size << 1);
916 /* Look up in the global cache. */
917 rte_rwlock_read_lock(&priv->mr.rwlock);
918 lkey = mr_btree_lookup(&priv->mr.cache, &idx, addr);
919 if (lkey != UINT32_MAX) {
921 *entry = (*priv->mr.cache.table)[idx];
922 rte_rwlock_read_unlock(&priv->mr.rwlock);
924 * Update local cache. Even if it fails, return the found entry
925 * to update top-half cache. Next time, this entry will be found
926 * in the global cache.
928 mr_btree_insert(bt, entry);
931 rte_rwlock_read_unlock(&priv->mr.rwlock);
932 /* First time to see the address? Create a new MR. */
933 lkey = mlx4_mr_create(dev, entry, addr);
935 * Update the local cache if successfully created a new global MR. Even
936 * if failed to create one, there's no action to take in this datapath
937 * code. As returning LKey is invalid, this will eventually make HW
940 if (lkey != UINT32_MAX)
941 mr_btree_insert(bt, entry);
946 * Bottom-half of LKey search on datapath. Firstly search in cache_bh[] and if
947 * misses, search in the global MR cache table and update the new entry to
948 * per-queue local caches.
951 * Pointer to Ethernet device.
953 * Pointer to per-queue MR control structure.
958 * Searched LKey on success, UINT32_MAX on no match.
961 mlx4_mr_addr2mr_bh(struct rte_eth_dev *dev, struct mlx4_mr_ctrl *mr_ctrl,
966 /* Victim in top-half cache to replace with new entry. */
967 struct mlx4_mr_cache *repl = &mr_ctrl->cache[mr_ctrl->head];
969 /* Binary-search MR translation table. */
970 lkey = mr_btree_lookup(&mr_ctrl->cache_bh, &bh_idx, addr);
971 /* Update top-half cache. */
972 if (likely(lkey != UINT32_MAX)) {
973 *repl = (*mr_ctrl->cache_bh.table)[bh_idx];
976 * If missed in local lookup table, search in the global cache
977 * and local cache_bh[] will be updated inside if possible.
978 * Top-half cache entry will also be updated.
980 lkey = mlx4_mr_lookup_dev(dev, mr_ctrl, repl, addr);
981 if (unlikely(lkey == UINT32_MAX))
984 /* Update the most recently used entry. */
985 mr_ctrl->mru = mr_ctrl->head;
986 /* Point to the next victim, the oldest. */
987 mr_ctrl->head = (mr_ctrl->head + 1) % MLX4_MR_CACHE_N;
992 * Bottom-half of LKey search on Rx.
995 * Pointer to Rx queue structure.
1000 * Searched LKey on success, UINT32_MAX on no match.
1003 mlx4_rx_addr2mr_bh(struct rxq *rxq, uintptr_t addr)
1005 struct mlx4_mr_ctrl *mr_ctrl = &rxq->mr_ctrl;
1006 struct priv *priv = rxq->priv;
1008 DEBUG("Rx queue %u: miss on top-half, mru=%u, head=%u, addr=%p",
1009 rxq->stats.idx, mr_ctrl->mru, mr_ctrl->head, (void *)addr);
1010 return mlx4_mr_addr2mr_bh(priv->dev, mr_ctrl, addr);
1014 * Bottom-half of LKey search on Tx.
1017 * Pointer to Tx queue structure.
1022 * Searched LKey on success, UINT32_MAX on no match.
1025 mlx4_tx_addr2mr_bh(struct txq *txq, uintptr_t addr)
1027 struct mlx4_mr_ctrl *mr_ctrl = &txq->mr_ctrl;
1028 struct priv *priv = txq->priv;
1030 DEBUG("Tx queue %u: miss on top-half, mru=%u, head=%u, addr=%p",
1031 txq->stats.idx, mr_ctrl->mru, mr_ctrl->head, (void *)addr);
1032 return mlx4_mr_addr2mr_bh(priv->dev, mr_ctrl, addr);
1036 * Flush all of the local cache entries.
1039 * Pointer to per-queue MR control structure.
1042 mlx4_mr_flush_local_cache(struct mlx4_mr_ctrl *mr_ctrl)
1044 /* Reset the most-recently-used index. */
1046 /* Reset the linear search array. */
1048 memset(mr_ctrl->cache, 0, sizeof(mr_ctrl->cache));
1049 /* Reset the B-tree table. */
1050 mr_ctrl->cache_bh.len = 1;
1051 mr_ctrl->cache_bh.overflow = 0;
1052 /* Update the generation number. */
1053 mr_ctrl->cur_gen = *mr_ctrl->dev_gen_ptr;
1054 DEBUG("mr_ctrl(%p): flushed, cur_gen=%d",
1055 (void *)mr_ctrl, mr_ctrl->cur_gen);
1058 /* Called during rte_mempool_mem_iter() by mlx4_mr_update_mp(). */
1060 mlx4_mr_update_mp_cb(struct rte_mempool *mp __rte_unused, void *opaque,
1061 struct rte_mempool_memhdr *memhdr,
1062 unsigned mem_idx __rte_unused)
1064 struct mr_update_mp_data *data = opaque;
1067 /* Stop iteration if failed in the previous walk. */
1070 /* Register address of the chunk and update local caches. */
1071 lkey = mlx4_mr_addr2mr_bh(data->dev, data->mr_ctrl,
1072 (uintptr_t)memhdr->addr);
1073 if (lkey == UINT32_MAX)
1078 * Register entire memory chunks in a Mempool.
1081 * Pointer to Ethernet device.
1083 * Pointer to per-queue MR control structure.
1085 * Pointer to registering Mempool.
1088 * 0 on success, -1 on failure.
1091 mlx4_mr_update_mp(struct rte_eth_dev *dev, struct mlx4_mr_ctrl *mr_ctrl,
1092 struct rte_mempool *mp)
1094 struct mr_update_mp_data data = {
1100 rte_mempool_mem_iter(mp, mlx4_mr_update_mp_cb, &data);
1106 * Dump all the created MRs and the global cache entries.
1109 * Pointer to Ethernet device.
1112 mlx4_mr_dump_dev(struct rte_eth_dev *dev)
1114 struct priv *priv = dev->data->dev_private;
1119 rte_rwlock_read_lock(&priv->mr.rwlock);
1120 /* Iterate all the existing MRs. */
1121 LIST_FOREACH(mr, &priv->mr.mr_list, mr) {
1124 DEBUG("port %u MR[%u], LKey = 0x%x, ms_n = %u, ms_bmp_n = %u",
1125 dev->data->port_id, mr_n++,
1126 rte_cpu_to_be_32(mr->ibv_mr->lkey),
1127 mr->ms_n, mr->ms_bmp_n);
1130 for (n = 0; n < mr->ms_bmp_n; ) {
1131 struct mlx4_mr_cache ret = { 0, };
1133 n = mr_find_next_chunk(mr, &ret, n);
1136 DEBUG(" chunk[%u], [0x%" PRIxPTR ", 0x%" PRIxPTR ")",
1137 chunk_n++, ret.start, ret.end);
1140 DEBUG("port %u dumping global cache", dev->data->port_id);
1141 mlx4_mr_btree_dump(&priv->mr.cache);
1142 rte_rwlock_read_unlock(&priv->mr.rwlock);
1147 * Release all the created MRs and resources. Remove device from memory callback
1151 * Pointer to Ethernet device.
1154 mlx4_mr_release(struct rte_eth_dev *dev)
1156 struct priv *priv = dev->data->dev_private;
1157 struct mlx4_mr *mr_next = LIST_FIRST(&priv->mr.mr_list);
1159 /* Remove from memory callback device list. */
1160 rte_rwlock_write_lock(&mlx4_mem_event_rwlock);
1161 LIST_REMOVE(priv, mem_event_cb);
1162 rte_rwlock_write_unlock(&mlx4_mem_event_rwlock);
1164 mlx4_mr_dump_dev(dev);
1166 rte_rwlock_write_lock(&priv->mr.rwlock);
1167 /* Detach from MR list and move to free list. */
1168 while (mr_next != NULL) {
1169 struct mlx4_mr *mr = mr_next;
1171 mr_next = LIST_NEXT(mr, mr);
1172 LIST_REMOVE(mr, mr);
1173 LIST_INSERT_HEAD(&priv->mr.mr_free_list, mr, mr);
1175 LIST_INIT(&priv->mr.mr_list);
1176 /* Free global cache. */
1177 mlx4_mr_btree_free(&priv->mr.cache);
1178 rte_rwlock_write_unlock(&priv->mr.rwlock);
1179 /* Free all remaining MRs. */
1180 mlx4_mr_garbage_collect(dev);