1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016 6WIND S.A.
3 * Copyright 2016 Mellanox Technologies, Ltd
7 #pragma GCC diagnostic ignored "-Wpedantic"
9 #include <infiniband/verbs.h>
11 #pragma GCC diagnostic error "-Wpedantic"
14 #include <rte_mempool.h>
15 #include <rte_malloc.h>
16 #include <rte_rwlock.h>
20 #include "mlx5_rxtx.h"
21 #include "mlx5_glue.h"
23 struct mr_find_contig_memsegs_data {
27 const struct rte_memseg_list *msl;
30 struct mr_update_mp_data {
31 struct rte_eth_dev *dev;
32 struct mlx5_mr_ctrl *mr_ctrl;
37 * Expand B-tree table to a given size. Can't be called with holding
38 * memory_hotplug_lock or priv->mr.rwlock due to rte_realloc().
41 * Pointer to B-tree structure.
43 * Number of entries for expansion.
46 * 0 on success, -1 on failure.
49 mr_btree_expand(struct mlx5_mr_btree *bt, int n)
57 * Downside of directly using rte_realloc() is that SOCKET_ID_ANY is
58 * used inside if there's no room to expand. Because this is a quite
59 * rare case and a part of very slow path, it is very acceptable.
60 * Initially cache_bh[] will be given practically enough space and once
61 * it is expanded, expansion wouldn't be needed again ever.
63 mem = rte_realloc(bt->table, n * sizeof(struct mlx5_mr_cache), 0);
65 /* Not an error, B-tree search will be skipped. */
66 DRV_LOG(WARNING, "failed to expand MR B-tree (%p) table",
70 DRV_LOG(DEBUG, "expanded MR B-tree table (size=%u)", n);
78 * Look up LKey from given B-tree lookup table, store the last index and return
82 * Pointer to B-tree structure.
84 * Pointer to index. Even on search failure, returns index where it stops
85 * searching so that index can be used when inserting a new entry.
90 * Searched LKey on success, UINT32_MAX on no match.
93 mr_btree_lookup(struct mlx5_mr_btree *bt, uint16_t *idx, uintptr_t addr)
95 struct mlx5_mr_cache *lkp_tbl;
100 lkp_tbl = *bt->table;
102 /* First entry must be NULL for comparison. */
103 assert(bt->len > 0 || (lkp_tbl[0].start == 0 &&
104 lkp_tbl[0].lkey == UINT32_MAX));
107 register uint16_t delta = n >> 1;
109 if (addr < lkp_tbl[base + delta].start) {
116 assert(addr >= lkp_tbl[base].start);
118 if (addr < lkp_tbl[base].end)
119 return lkp_tbl[base].lkey;
125 * Insert an entry to B-tree lookup table.
128 * Pointer to B-tree structure.
130 * Pointer to new entry to insert.
133 * 0 on success, -1 on failure.
136 mr_btree_insert(struct mlx5_mr_btree *bt, struct mlx5_mr_cache *entry)
138 struct mlx5_mr_cache *lkp_tbl;
143 assert(bt->len <= bt->size);
145 lkp_tbl = *bt->table;
146 /* Find out the slot for insertion. */
147 if (mr_btree_lookup(bt, &idx, entry->start) != UINT32_MAX) {
149 "abort insertion to B-tree(%p): already exist at"
150 " idx=%u [0x%" PRIxPTR ", 0x%" PRIxPTR ") lkey=0x%x",
151 (void *)bt, idx, entry->start, entry->end, entry->lkey);
152 /* Already exist, return. */
155 /* If table is full, return error. */
156 if (unlikely(bt->len == bt->size)) {
162 shift = (bt->len - idx) * sizeof(struct mlx5_mr_cache);
164 memmove(&lkp_tbl[idx + 1], &lkp_tbl[idx], shift);
165 lkp_tbl[idx] = *entry;
168 "inserted B-tree(%p)[%u],"
169 " [0x%" PRIxPTR ", 0x%" PRIxPTR ") lkey=0x%x",
170 (void *)bt, idx, entry->start, entry->end, entry->lkey);
175 * Initialize B-tree and allocate memory for lookup table.
178 * Pointer to B-tree structure.
180 * Number of entries to allocate.
182 * NUMA socket on which memory must be allocated.
185 * 0 on success, a negative errno value otherwise and rte_errno is set.
188 mlx5_mr_btree_init(struct mlx5_mr_btree *bt, int n, int socket)
194 assert(!bt->table && !bt->size);
195 memset(bt, 0, sizeof(*bt));
196 bt->table = rte_calloc_socket("B-tree table",
197 n, sizeof(struct mlx5_mr_cache),
199 if (bt->table == NULL) {
201 DEBUG("failed to allocate memory for btree cache on socket %d",
206 /* First entry must be NULL for binary search. */
207 (*bt->table)[bt->len++] = (struct mlx5_mr_cache) {
210 DEBUG("initialized B-tree %p with table %p",
211 (void *)bt, (void *)bt->table);
216 * Free B-tree resources.
219 * Pointer to B-tree structure.
222 mlx5_mr_btree_free(struct mlx5_mr_btree *bt)
226 DEBUG("freeing B-tree %p with table %p",
227 (void *)bt, (void *)bt->table);
229 memset(bt, 0, sizeof(*bt));
233 * Dump all the entries in a B-tree
236 * Pointer to B-tree structure.
239 mlx5_mr_btree_dump(struct mlx5_mr_btree *bt __rte_unused)
243 struct mlx5_mr_cache *lkp_tbl;
247 lkp_tbl = *bt->table;
248 for (idx = 0; idx < bt->len; ++idx) {
249 struct mlx5_mr_cache *entry = &lkp_tbl[idx];
251 DEBUG("B-tree(%p)[%u],"
252 " [0x%" PRIxPTR ", 0x%" PRIxPTR ") lkey=0x%x",
253 (void *)bt, idx, entry->start, entry->end, entry->lkey);
259 * Find virtually contiguous memory chunk in a given MR.
262 * Pointer to MR structure.
264 * Pointer to returning MR cache entry. If not found, this will not be
267 * Start index of the memseg bitmap.
270 * Next index to go on lookup.
273 mr_find_next_chunk(struct mlx5_mr *mr, struct mlx5_mr_cache *entry,
280 for (idx = base_idx; idx < mr->ms_bmp_n; ++idx) {
281 if (rte_bitmap_get(mr->ms_bmp, idx)) {
282 const struct rte_memseg_list *msl;
283 const struct rte_memseg *ms;
286 ms = rte_fbarray_get(&msl->memseg_arr,
287 mr->ms_base_idx + idx);
288 assert(msl->page_sz == ms->hugepage_sz);
291 end = ms->addr_64 + ms->hugepage_sz;
293 /* Passed the end of a fragment. */
298 /* Found one chunk. */
299 entry->start = start;
301 entry->lkey = rte_cpu_to_be_32(mr->ibv_mr->lkey);
307 * Insert a MR to the global B-tree cache. It may fail due to low-on-memory.
308 * Then, this entry will have to be searched by mr_lookup_dev_list() in
309 * mlx5_mr_create() on miss.
312 * Pointer to Ethernet device.
314 * Pointer to MR to insert.
317 * 0 on success, -1 on failure.
320 mr_insert_dev_cache(struct rte_eth_dev *dev, struct mlx5_mr *mr)
322 struct priv *priv = dev->data->dev_private;
325 DRV_LOG(DEBUG, "port %u inserting MR(%p) to global cache",
326 dev->data->port_id, (void *)mr);
327 for (n = 0; n < mr->ms_bmp_n; ) {
328 struct mlx5_mr_cache entry = { 0, };
330 /* Find a contiguous chunk and advance the index. */
331 n = mr_find_next_chunk(mr, &entry, n);
334 if (mr_btree_insert(&priv->mr.cache, &entry) < 0) {
336 * Overflowed, but the global table cannot be expanded
337 * because of deadlock.
346 * Look up address in the original global MR list.
349 * Pointer to Ethernet device.
351 * Pointer to returning MR cache entry. If no match, this will not be updated.
356 * Found MR on match, NULL otherwise.
358 static struct mlx5_mr *
359 mr_lookup_dev_list(struct rte_eth_dev *dev, struct mlx5_mr_cache *entry,
362 struct priv *priv = dev->data->dev_private;
365 /* Iterate all the existing MRs. */
366 LIST_FOREACH(mr, &priv->mr.mr_list, mr) {
371 for (n = 0; n < mr->ms_bmp_n; ) {
372 struct mlx5_mr_cache ret = { 0, };
374 n = mr_find_next_chunk(mr, &ret, n);
375 if (addr >= ret.start && addr < ret.end) {
386 * Look up address on device.
389 * Pointer to Ethernet device.
391 * Pointer to returning MR cache entry. If no match, this will not be updated.
396 * Searched LKey on success, UINT32_MAX on failure and rte_errno is set.
399 mr_lookup_dev(struct rte_eth_dev *dev, struct mlx5_mr_cache *entry,
402 struct priv *priv = dev->data->dev_private;
404 uint32_t lkey = UINT32_MAX;
408 * If the global cache has overflowed since it failed to expand the
409 * B-tree table, it can't have all the existing MRs. Then, the address
410 * has to be searched by traversing the original MR list instead, which
411 * is very slow path. Otherwise, the global cache is all inclusive.
413 if (!unlikely(priv->mr.cache.overflow)) {
414 lkey = mr_btree_lookup(&priv->mr.cache, &idx, addr);
415 if (lkey != UINT32_MAX)
416 *entry = (*priv->mr.cache.table)[idx];
418 /* Falling back to the slowest path. */
419 mr = mr_lookup_dev_list(dev, entry, addr);
423 assert(lkey == UINT32_MAX || (addr >= entry->start &&
429 * Free MR resources. MR lock must not be held to avoid a deadlock. rte_free()
430 * can raise memory free event and the callback function will spin on the lock.
433 * Pointer to MR to free.
436 mr_free(struct mlx5_mr *mr)
440 DRV_LOG(DEBUG, "freeing MR(%p):", (void *)mr);
441 if (mr->ibv_mr != NULL)
442 claim_zero(mlx5_glue->dereg_mr(mr->ibv_mr));
443 if (mr->ms_bmp != NULL)
444 rte_bitmap_free(mr->ms_bmp);
449 * Releass resources of detached MR having no online entry.
452 * Pointer to Ethernet device.
455 mlx5_mr_garbage_collect(struct rte_eth_dev *dev)
457 struct priv *priv = dev->data->dev_private;
458 struct mlx5_mr *mr_next;
459 struct mlx5_mr_list free_list = LIST_HEAD_INITIALIZER(free_list);
461 /* Must be called from the primary process. */
462 assert(rte_eal_process_type() == RTE_PROC_PRIMARY);
464 * MR can't be freed with holding the lock because rte_free() could call
465 * memory free callback function. This will be a deadlock situation.
467 rte_rwlock_write_lock(&priv->mr.rwlock);
468 /* Detach the whole free list and release it after unlocking. */
469 free_list = priv->mr.mr_free_list;
470 LIST_INIT(&priv->mr.mr_free_list);
471 rte_rwlock_write_unlock(&priv->mr.rwlock);
472 /* Release resources. */
473 mr_next = LIST_FIRST(&free_list);
474 while (mr_next != NULL) {
475 struct mlx5_mr *mr = mr_next;
477 mr_next = LIST_NEXT(mr, mr);
482 /* Called during rte_memseg_contig_walk() by mlx5_mr_create(). */
484 mr_find_contig_memsegs_cb(const struct rte_memseg_list *msl,
485 const struct rte_memseg *ms, size_t len, void *arg)
487 struct mr_find_contig_memsegs_data *data = arg;
489 if (data->addr < ms->addr_64 || data->addr >= ms->addr_64 + len)
491 /* Found, save it and stop walking. */
492 data->start = ms->addr_64;
493 data->end = ms->addr_64 + len;
499 * Create a new global Memroy Region (MR) for a missing virtual address.
500 * Register entire virtually contiguous memory chunk around the address.
503 * Pointer to Ethernet device.
505 * Pointer to returning MR cache entry, found in the global cache or newly
506 * created. If failed to create one, this will not be updated.
508 * Target virtual address to register.
511 * Searched LKey on success, UINT32_MAX on failure and rte_errno is set.
514 mlx5_mr_create(struct rte_eth_dev *dev, struct mlx5_mr_cache *entry,
517 struct priv *priv = dev->data->dev_private;
518 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
519 const struct rte_memseg_list *msl;
520 const struct rte_memseg *ms;
521 struct mlx5_mr *mr = NULL;
526 int ms_idx_shift = -1;
528 struct mr_find_contig_memsegs_data data = {
531 struct mr_find_contig_memsegs_data data_re;
533 DRV_LOG(DEBUG, "port %u creating a MR using address (%p)",
534 dev->data->port_id, (void *)addr);
535 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
537 "port %u using address (%p) of unregistered mempool"
538 " in secondary process, please create mempool"
539 " before rte_eth_dev_start()",
540 dev->data->port_id, (void *)addr);
545 * Release detached MRs if any. This can't be called with holding either
546 * memory_hotplug_lock or priv->mr.rwlock. MRs on the free list have
547 * been detached by the memory free event but it couldn't be released
548 * inside the callback due to deadlock. As a result, releasing resources
549 * is quite opportunistic.
551 mlx5_mr_garbage_collect(dev);
553 * Find out a contiguous virtual address chunk in use, to which the
554 * given address belongs, in order to register maximum range. In the
555 * best case where mempools are not dynamically recreated and
556 * '--socket-mem' is speicified as an EAL option, it is very likely to
557 * have only one MR(LKey) per a socket and per a hugepage-size even
558 * though the system memory is highly fragmented.
560 if (!rte_memseg_contig_walk(mr_find_contig_memsegs_cb, &data)) {
562 "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 DEBUG("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 DEBUG("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 DEBUG("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 * mlx5_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 mlx5_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 * mlx5_alloc_buf_extern() which eventually calls rte_malloc_socket()
700 * through mlx5_alloc_verbs_buf().
702 mr->ibv_mr = mlx5_glue->reg_mr(priv->pd, (void *)data.start, len,
703 IBV_ACCESS_LOCAL_WRITE);
704 if (mr->ibv_mr == NULL) {
705 DEBUG("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 mlx5_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 DRV_LOG(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 mlx5_mr_garbage_collect(). Even if this callback is called from a
770 * secondary process, the garbage collector will be called in primary process
771 * as the secondary process can't call mlx5_mr_create().
773 * The global cache must be rebuilt if there's any change and this event has to
774 * be propagated to dataplane threads to flush the local caches.
777 * Pointer to Ethernet device.
779 * Address of freed memory.
781 * Size of freed memory.
784 mlx5_mr_mem_event_free_cb(struct rte_eth_dev *dev, const void *addr, size_t len)
786 struct priv *priv = dev->data->dev_private;
787 const struct rte_memseg_list *msl;
793 DEBUG("port %u free callback: addr=%p, len=%zu",
794 dev->data->port_id, addr, len);
795 msl = rte_mem_virt2memseg_list(addr);
796 /* addr and len must be page-aligned. */
797 assert((uintptr_t)addr == RTE_ALIGN((uintptr_t)addr, msl->page_sz));
798 assert(len == RTE_ALIGN(len, msl->page_sz));
799 ms_n = len / msl->page_sz;
800 rte_rwlock_write_lock(&priv->mr.rwlock);
801 /* Clear bits of freed memsegs from MR. */
802 for (i = 0; i < ms_n; ++i) {
803 const struct rte_memseg *ms;
804 struct mlx5_mr_cache entry;
809 /* Find MR having this memseg. */
810 start = (uintptr_t)addr + i * msl->page_sz;
811 mr = mr_lookup_dev_list(dev, &entry, start);
814 ms = rte_mem_virt2memseg((void *)start, msl);
816 assert(msl->page_sz == ms->hugepage_sz);
817 ms_idx = rte_fbarray_find_idx(&msl->memseg_arr, ms);
818 pos = ms_idx - mr->ms_base_idx;
819 assert(rte_bitmap_get(mr->ms_bmp, pos));
820 assert(pos < mr->ms_bmp_n);
821 DEBUG("port %u MR(%p): clear bitmap[%u] for addr %p",
822 dev->data->port_id, (void *)mr, pos, (void *)start);
823 rte_bitmap_clear(mr->ms_bmp, pos);
824 if (--mr->ms_n == 0) {
826 LIST_INSERT_HEAD(&priv->mr.mr_free_list, mr, mr);
827 DEBUG("port %u remove MR(%p) from list",
828 dev->data->port_id, (void *)mr);
831 * MR is fragmented or will be freed. the global cache must be
837 mr_rebuild_dev_cache(dev);
839 * Flush local caches by propagating invalidation across cores.
840 * rte_smp_wmb() is enough to synchronize this event. If one of
841 * freed memsegs is seen by other core, that means the memseg
842 * has been allocated by allocator, which will come after this
843 * free call. Therefore, this store instruction (incrementing
844 * generation below) will be guaranteed to be seen by other core
845 * before the core sees the newly allocated memory.
848 DEBUG("broadcasting local cache flush, gen=%d",
852 rte_rwlock_write_unlock(&priv->mr.rwlock);
856 * Callback for memory event. This can be called from both primary and secondary
867 mlx5_mr_mem_event_cb(enum rte_mem_event event_type, const void *addr,
868 size_t len, void *arg __rte_unused)
871 struct mlx5_dev_list *dev_list = &mlx5_shared_data->mem_event_cb_list;
873 switch (event_type) {
874 case RTE_MEM_EVENT_FREE:
875 rte_rwlock_write_lock(&mlx5_shared_data->mem_event_rwlock);
876 /* Iterate all the existing mlx5 devices. */
877 LIST_FOREACH(priv, dev_list, mem_event_cb)
878 mlx5_mr_mem_event_free_cb(ETH_DEV(priv), addr, len);
879 rte_rwlock_write_unlock(&mlx5_shared_data->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 mlx5_mr_lookup_dev(struct rte_eth_dev *dev, struct mlx5_mr_ctrl *mr_ctrl,
906 struct mlx5_mr_cache *entry, uintptr_t addr)
908 struct priv *priv = dev->data->dev_private;
909 struct mlx5_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 = mlx5_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 mlx5_mr_addr2mr_bh(struct rte_eth_dev *dev, struct mlx5_mr_ctrl *mr_ctrl,
966 /* Victim in top-half cache to replace with new entry. */
967 struct mlx5_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 = mlx5_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) % MLX5_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 mlx5_rx_addr2mr_bh(struct mlx5_rxq_data *rxq, uintptr_t addr)
1005 struct mlx5_rxq_ctrl *rxq_ctrl =
1006 container_of(rxq, struct mlx5_rxq_ctrl, rxq);
1007 struct mlx5_mr_ctrl *mr_ctrl = &rxq->mr_ctrl;
1008 struct priv *priv = rxq_ctrl->priv;
1011 "Rx queue %u: miss on top-half, mru=%u, head=%u, addr=%p",
1012 rxq_ctrl->idx, mr_ctrl->mru, mr_ctrl->head, (void *)addr);
1013 return mlx5_mr_addr2mr_bh(ETH_DEV(priv), mr_ctrl, addr);
1017 * Bottom-half of LKey search on Tx.
1020 * Pointer to Tx queue structure.
1025 * Searched LKey on success, UINT32_MAX on no match.
1028 mlx5_tx_addr2mr_bh(struct mlx5_txq_data *txq, uintptr_t addr)
1030 struct mlx5_txq_ctrl *txq_ctrl =
1031 container_of(txq, struct mlx5_txq_ctrl, txq);
1032 struct mlx5_mr_ctrl *mr_ctrl = &txq->mr_ctrl;
1033 struct priv *priv = txq_ctrl->priv;
1036 "Tx queue %u: miss on top-half, mru=%u, head=%u, addr=%p",
1037 txq_ctrl->idx, mr_ctrl->mru, mr_ctrl->head, (void *)addr);
1038 return mlx5_mr_addr2mr_bh(ETH_DEV(priv), mr_ctrl, addr);
1042 * Flush all of the local cache entries.
1045 * Pointer to per-queue MR control structure.
1048 mlx5_mr_flush_local_cache(struct mlx5_mr_ctrl *mr_ctrl)
1050 /* Reset the most-recently-used index. */
1052 /* Reset the linear search array. */
1054 memset(mr_ctrl->cache, 0, sizeof(mr_ctrl->cache));
1055 /* Reset the B-tree table. */
1056 mr_ctrl->cache_bh.len = 1;
1057 mr_ctrl->cache_bh.overflow = 0;
1058 /* Update the generation number. */
1059 mr_ctrl->cur_gen = *mr_ctrl->dev_gen_ptr;
1060 DRV_LOG(DEBUG, "mr_ctrl(%p): flushed, cur_gen=%d",
1061 (void *)mr_ctrl, mr_ctrl->cur_gen);
1064 /* Called during rte_mempool_mem_iter() by mlx5_mr_update_mp(). */
1066 mlx5_mr_update_mp_cb(struct rte_mempool *mp __rte_unused, void *opaque,
1067 struct rte_mempool_memhdr *memhdr,
1068 unsigned mem_idx __rte_unused)
1070 struct mr_update_mp_data *data = opaque;
1073 /* Stop iteration if failed in the previous walk. */
1076 /* Register address of the chunk and update local caches. */
1077 lkey = mlx5_mr_addr2mr_bh(data->dev, data->mr_ctrl,
1078 (uintptr_t)memhdr->addr);
1079 if (lkey == UINT32_MAX)
1084 * Register entire memory chunks in a Mempool.
1087 * Pointer to Ethernet device.
1089 * Pointer to per-queue MR control structure.
1091 * Pointer to registering Mempool.
1094 * 0 on success, -1 on failure.
1097 mlx5_mr_update_mp(struct rte_eth_dev *dev, struct mlx5_mr_ctrl *mr_ctrl,
1098 struct rte_mempool *mp)
1100 struct mr_update_mp_data data = {
1106 rte_mempool_mem_iter(mp, mlx5_mr_update_mp_cb, &data);
1111 * Dump all the created MRs and the global cache entries.
1114 * Pointer to Ethernet device.
1117 mlx5_mr_dump_dev(struct rte_eth_dev *dev __rte_unused)
1120 struct priv *priv = dev->data->dev_private;
1125 rte_rwlock_read_lock(&priv->mr.rwlock);
1126 /* Iterate all the existing MRs. */
1127 LIST_FOREACH(mr, &priv->mr.mr_list, mr) {
1130 DEBUG("port %u MR[%u], LKey = 0x%x, ms_n = %u, ms_bmp_n = %u",
1131 dev->data->port_id, mr_n++,
1132 rte_cpu_to_be_32(mr->ibv_mr->lkey),
1133 mr->ms_n, mr->ms_bmp_n);
1136 for (n = 0; n < mr->ms_bmp_n; ) {
1137 struct mlx5_mr_cache ret = { 0, };
1139 n = mr_find_next_chunk(mr, &ret, n);
1142 DEBUG(" chunk[%u], [0x%" PRIxPTR ", 0x%" PRIxPTR ")",
1143 chunk_n++, ret.start, ret.end);
1146 DEBUG("port %u dumping global cache", dev->data->port_id);
1147 mlx5_mr_btree_dump(&priv->mr.cache);
1148 rte_rwlock_read_unlock(&priv->mr.rwlock);
1153 * Release all the created MRs and resources. Remove device from memory callback
1157 * Pointer to Ethernet device.
1160 mlx5_mr_release(struct rte_eth_dev *dev)
1162 struct priv *priv = dev->data->dev_private;
1163 struct mlx5_mr *mr_next = LIST_FIRST(&priv->mr.mr_list);
1165 /* Remove from memory callback device list. */
1166 rte_rwlock_write_lock(&mlx5_shared_data->mem_event_rwlock);
1167 LIST_REMOVE(priv, mem_event_cb);
1168 rte_rwlock_write_unlock(&mlx5_shared_data->mem_event_rwlock);
1169 if (rte_log_get_level(mlx5_logtype) == RTE_LOG_DEBUG)
1170 mlx5_mr_dump_dev(dev);
1171 rte_rwlock_write_lock(&priv->mr.rwlock);
1172 /* Detach from MR list and move to free list. */
1173 while (mr_next != NULL) {
1174 struct mlx5_mr *mr = mr_next;
1176 mr_next = LIST_NEXT(mr, mr);
1177 LIST_REMOVE(mr, mr);
1178 LIST_INSERT_HEAD(&priv->mr.mr_free_list, mr, mr);
1180 LIST_INIT(&priv->mr.mr_list);
1181 /* Free global cache. */
1182 mlx5_mr_btree_free(&priv->mr.cache);
1183 rte_rwlock_write_unlock(&priv->mr.rwlock);
1184 /* Free all remaining MRs. */
1185 mlx5_mr_garbage_collect(dev);