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