net/mlx5: add memory region callbacks in per-device cache
[dpdk.git] / drivers / common / mlx5 / mlx5_common_mr.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016 6WIND S.A.
3  * Copyright 2020 Mellanox Technologies, Ltd
4  */
5 #include <rte_eal_memconfig.h>
6 #include <rte_errno.h>
7 #include <rte_mempool.h>
8 #include <rte_malloc.h>
9 #include <rte_rwlock.h>
10
11 #include "mlx5_glue.h"
12 #include "mlx5_common_mp.h"
13 #include "mlx5_common_mr.h"
14 #include "mlx5_common_utils.h"
15
16 struct mr_find_contig_memsegs_data {
17         uintptr_t addr;
18         uintptr_t start;
19         uintptr_t end;
20         const struct rte_memseg_list *msl;
21 };
22
23 /**
24  * Expand B-tree table to a given size. Can't be called with holding
25  * memory_hotplug_lock or share_cache.rwlock due to rte_realloc().
26  *
27  * @param bt
28  *   Pointer to B-tree structure.
29  * @param n
30  *   Number of entries for expansion.
31  *
32  * @return
33  *   0 on success, -1 on failure.
34  */
35 static int
36 mr_btree_expand(struct mlx5_mr_btree *bt, int n)
37 {
38         void *mem;
39         int ret = 0;
40
41         if (n <= bt->size)
42                 return ret;
43         /*
44          * Downside of directly using rte_realloc() is that SOCKET_ID_ANY is
45          * used inside if there's no room to expand. Because this is a quite
46          * rare case and a part of very slow path, it is very acceptable.
47          * Initially cache_bh[] will be given practically enough space and once
48          * it is expanded, expansion wouldn't be needed again ever.
49          */
50         mem = rte_realloc(bt->table, n * sizeof(struct mr_cache_entry), 0);
51         if (mem == NULL) {
52                 /* Not an error, B-tree search will be skipped. */
53                 DRV_LOG(WARNING, "failed to expand MR B-tree (%p) table",
54                         (void *)bt);
55                 ret = -1;
56         } else {
57                 DRV_LOG(DEBUG, "expanded MR B-tree table (size=%u)", n);
58                 bt->table = mem;
59                 bt->size = n;
60         }
61         return ret;
62 }
63
64 /**
65  * Look up LKey from given B-tree lookup table, store the last index and return
66  * searched LKey.
67  *
68  * @param bt
69  *   Pointer to B-tree structure.
70  * @param[out] idx
71  *   Pointer to index. Even on search failure, returns index where it stops
72  *   searching so that index can be used when inserting a new entry.
73  * @param addr
74  *   Search key.
75  *
76  * @return
77  *   Searched LKey on success, UINT32_MAX on no match.
78  */
79 static uint32_t
80 mr_btree_lookup(struct mlx5_mr_btree *bt, uint16_t *idx, uintptr_t addr)
81 {
82         struct mr_cache_entry *lkp_tbl;
83         uint16_t n;
84         uint16_t base = 0;
85
86         MLX5_ASSERT(bt != NULL);
87         lkp_tbl = *bt->table;
88         n = bt->len;
89         /* First entry must be NULL for comparison. */
90         MLX5_ASSERT(bt->len > 0 || (lkp_tbl[0].start == 0 &&
91                                     lkp_tbl[0].lkey == UINT32_MAX));
92         /* Binary search. */
93         do {
94                 register uint16_t delta = n >> 1;
95
96                 if (addr < lkp_tbl[base + delta].start) {
97                         n = delta;
98                 } else {
99                         base += delta;
100                         n -= delta;
101                 }
102         } while (n > 1);
103         MLX5_ASSERT(addr >= lkp_tbl[base].start);
104         *idx = base;
105         if (addr < lkp_tbl[base].end)
106                 return lkp_tbl[base].lkey;
107         /* Not found. */
108         return UINT32_MAX;
109 }
110
111 /**
112  * Insert an entry to B-tree lookup table.
113  *
114  * @param bt
115  *   Pointer to B-tree structure.
116  * @param entry
117  *   Pointer to new entry to insert.
118  *
119  * @return
120  *   0 on success, -1 on failure.
121  */
122 static int
123 mr_btree_insert(struct mlx5_mr_btree *bt, struct mr_cache_entry *entry)
124 {
125         struct mr_cache_entry *lkp_tbl;
126         uint16_t idx = 0;
127         size_t shift;
128
129         MLX5_ASSERT(bt != NULL);
130         MLX5_ASSERT(bt->len <= bt->size);
131         MLX5_ASSERT(bt->len > 0);
132         lkp_tbl = *bt->table;
133         /* Find out the slot for insertion. */
134         if (mr_btree_lookup(bt, &idx, entry->start) != UINT32_MAX) {
135                 DRV_LOG(DEBUG,
136                         "abort insertion to B-tree(%p): already exist at"
137                         " idx=%u [0x%" PRIxPTR ", 0x%" PRIxPTR ") lkey=0x%x",
138                         (void *)bt, idx, entry->start, entry->end, entry->lkey);
139                 /* Already exist, return. */
140                 return 0;
141         }
142         /* If table is full, return error. */
143         if (unlikely(bt->len == bt->size)) {
144                 bt->overflow = 1;
145                 return -1;
146         }
147         /* Insert entry. */
148         ++idx;
149         shift = (bt->len - idx) * sizeof(struct mr_cache_entry);
150         if (shift)
151                 memmove(&lkp_tbl[idx + 1], &lkp_tbl[idx], shift);
152         lkp_tbl[idx] = *entry;
153         bt->len++;
154         DRV_LOG(DEBUG,
155                 "inserted B-tree(%p)[%u],"
156                 " [0x%" PRIxPTR ", 0x%" PRIxPTR ") lkey=0x%x",
157                 (void *)bt, idx, entry->start, entry->end, entry->lkey);
158         return 0;
159 }
160
161 /**
162  * Initialize B-tree and allocate memory for lookup table.
163  *
164  * @param bt
165  *   Pointer to B-tree structure.
166  * @param n
167  *   Number of entries to allocate.
168  * @param socket
169  *   NUMA socket on which memory must be allocated.
170  *
171  * @return
172  *   0 on success, a negative errno value otherwise and rte_errno is set.
173  */
174 int
175 mlx5_mr_btree_init(struct mlx5_mr_btree *bt, int n, int socket)
176 {
177         if (bt == NULL) {
178                 rte_errno = EINVAL;
179                 return -rte_errno;
180         }
181         MLX5_ASSERT(!bt->table && !bt->size);
182         memset(bt, 0, sizeof(*bt));
183         bt->table = rte_calloc_socket("B-tree table",
184                                       n, sizeof(struct mr_cache_entry),
185                                       0, socket);
186         if (bt->table == NULL) {
187                 rte_errno = ENOMEM;
188                 DEBUG("failed to allocate memory for btree cache on socket %d",
189                       socket);
190                 return -rte_errno;
191         }
192         bt->size = n;
193         /* First entry must be NULL for binary search. */
194         (*bt->table)[bt->len++] = (struct mr_cache_entry) {
195                 .lkey = UINT32_MAX,
196         };
197         DEBUG("initialized B-tree %p with table %p",
198               (void *)bt, (void *)bt->table);
199         return 0;
200 }
201
202 /**
203  * Free B-tree resources.
204  *
205  * @param bt
206  *   Pointer to B-tree structure.
207  */
208 void
209 mlx5_mr_btree_free(struct mlx5_mr_btree *bt)
210 {
211         if (bt == NULL)
212                 return;
213         DEBUG("freeing B-tree %p with table %p",
214               (void *)bt, (void *)bt->table);
215         rte_free(bt->table);
216         memset(bt, 0, sizeof(*bt));
217 }
218
219 /**
220  * Dump all the entries in a B-tree
221  *
222  * @param bt
223  *   Pointer to B-tree structure.
224  */
225 void
226 mlx5_mr_btree_dump(struct mlx5_mr_btree *bt __rte_unused)
227 {
228 #ifdef RTE_LIBRTE_MLX5_DEBUG
229         int idx;
230         struct mr_cache_entry *lkp_tbl;
231
232         if (bt == NULL)
233                 return;
234         lkp_tbl = *bt->table;
235         for (idx = 0; idx < bt->len; ++idx) {
236                 struct mr_cache_entry *entry = &lkp_tbl[idx];
237
238                 DEBUG("B-tree(%p)[%u],"
239                       " [0x%" PRIxPTR ", 0x%" PRIxPTR ") lkey=0x%x",
240                       (void *)bt, idx, entry->start, entry->end, entry->lkey);
241         }
242 #endif
243 }
244
245 /**
246  * Find virtually contiguous memory chunk in a given MR.
247  *
248  * @param dev
249  *   Pointer to MR structure.
250  * @param[out] entry
251  *   Pointer to returning MR cache entry. If not found, this will not be
252  *   updated.
253  * @param start_idx
254  *   Start index of the memseg bitmap.
255  *
256  * @return
257  *   Next index to go on lookup.
258  */
259 static int
260 mr_find_next_chunk(struct mlx5_mr *mr, struct mr_cache_entry *entry,
261                    int base_idx)
262 {
263         uintptr_t start = 0;
264         uintptr_t end = 0;
265         uint32_t idx = 0;
266
267         /* MR for external memory doesn't have memseg list. */
268         if (mr->msl == NULL) {
269                 MLX5_ASSERT(mr->ms_bmp_n == 1);
270                 MLX5_ASSERT(mr->ms_n == 1);
271                 MLX5_ASSERT(base_idx == 0);
272                 /*
273                  * Can't search it from memseg list but get it directly from
274                  * pmd_mr as there's only one chunk.
275                  */
276                 entry->start = (uintptr_t)mr->pmd_mr.addr;
277                 entry->end = (uintptr_t)mr->pmd_mr.addr + mr->pmd_mr.len;
278                 entry->lkey = rte_cpu_to_be_32(mr->pmd_mr.lkey);
279                 /* Returning 1 ends iteration. */
280                 return 1;
281         }
282         for (idx = base_idx; idx < mr->ms_bmp_n; ++idx) {
283                 if (rte_bitmap_get(mr->ms_bmp, idx)) {
284                         const struct rte_memseg_list *msl;
285                         const struct rte_memseg *ms;
286
287                         msl = mr->msl;
288                         ms = rte_fbarray_get(&msl->memseg_arr,
289                                              mr->ms_base_idx + idx);
290                         MLX5_ASSERT(msl->page_sz == ms->hugepage_sz);
291                         if (!start)
292                                 start = ms->addr_64;
293                         end = ms->addr_64 + ms->hugepage_sz;
294                 } else if (start) {
295                         /* Passed the end of a fragment. */
296                         break;
297                 }
298         }
299         if (start) {
300                 /* Found one chunk. */
301                 entry->start = start;
302                 entry->end = end;
303                 entry->lkey = rte_cpu_to_be_32(mr->pmd_mr.lkey);
304         }
305         return idx;
306 }
307
308 /**
309  * Insert a MR to the global B-tree cache. It may fail due to low-on-memory.
310  * Then, this entry will have to be searched by mr_lookup_list() in
311  * mlx5_mr_create() on miss.
312  *
313  * @param share_cache
314  *   Pointer to a global shared MR cache.
315  * @param mr
316  *   Pointer to MR to insert.
317  *
318  * @return
319  *   0 on success, -1 on failure.
320  */
321 int
322 mlx5_mr_insert_cache(struct mlx5_mr_share_cache *share_cache,
323                      struct mlx5_mr *mr)
324 {
325         unsigned int n;
326
327         DRV_LOG(DEBUG, "Inserting MR(%p) to global cache(%p)",
328                 (void *)mr, (void *)share_cache);
329         for (n = 0; n < mr->ms_bmp_n; ) {
330                 struct mr_cache_entry entry;
331
332                 memset(&entry, 0, sizeof(entry));
333                 /* Find a contiguous chunk and advance the index. */
334                 n = mr_find_next_chunk(mr, &entry, n);
335                 if (!entry.end)
336                         break;
337                 if (mr_btree_insert(&share_cache->cache, &entry) < 0) {
338                         /*
339                          * Overflowed, but the global table cannot be expanded
340                          * because of deadlock.
341                          */
342                         return -1;
343                 }
344         }
345         return 0;
346 }
347
348 /**
349  * Look up address in the original global MR list.
350  *
351  * @param share_cache
352  *   Pointer to a global shared MR cache.
353  * @param[out] entry
354  *   Pointer to returning MR cache entry. If no match, this will not be updated.
355  * @param addr
356  *   Search key.
357  *
358  * @return
359  *   Found MR on match, NULL otherwise.
360  */
361 struct mlx5_mr *
362 mlx5_mr_lookup_list(struct mlx5_mr_share_cache *share_cache,
363                     struct mr_cache_entry *entry, uintptr_t addr)
364 {
365         struct mlx5_mr *mr;
366
367         /* Iterate all the existing MRs. */
368         LIST_FOREACH(mr, &share_cache->mr_list, mr) {
369                 unsigned int n;
370
371                 if (mr->ms_n == 0)
372                         continue;
373                 for (n = 0; n < mr->ms_bmp_n; ) {
374                         struct mr_cache_entry ret;
375
376                         memset(&ret, 0, sizeof(ret));
377                         n = mr_find_next_chunk(mr, &ret, n);
378                         if (addr >= ret.start && addr < ret.end) {
379                                 /* Found. */
380                                 *entry = ret;
381                                 return mr;
382                         }
383                 }
384         }
385         return NULL;
386 }
387
388 /**
389  * Look up address on global MR cache.
390  *
391  * @param share_cache
392  *   Pointer to a global shared MR cache.
393  * @param[out] entry
394  *   Pointer to returning MR cache entry. If no match, this will not be updated.
395  * @param addr
396  *   Search key.
397  *
398  * @return
399  *   Searched LKey on success, UINT32_MAX on failure and rte_errno is set.
400  */
401 uint32_t
402 mlx5_mr_lookup_cache(struct mlx5_mr_share_cache *share_cache,
403                      struct mr_cache_entry *entry, uintptr_t addr)
404 {
405         uint16_t idx;
406         uint32_t lkey = UINT32_MAX;
407         struct mlx5_mr *mr;
408
409         /*
410          * If the global cache has overflowed since it failed to expand the
411          * B-tree table, it can't have all the existing MRs. Then, the address
412          * has to be searched by traversing the original MR list instead, which
413          * is very slow path. Otherwise, the global cache is all inclusive.
414          */
415         if (!unlikely(share_cache->cache.overflow)) {
416                 lkey = mr_btree_lookup(&share_cache->cache, &idx, addr);
417                 if (lkey != UINT32_MAX)
418                         *entry = (*share_cache->cache.table)[idx];
419         } else {
420                 /* Falling back to the slowest path. */
421                 mr = mlx5_mr_lookup_list(share_cache, entry, addr);
422                 if (mr != NULL)
423                         lkey = entry->lkey;
424         }
425         MLX5_ASSERT(lkey == UINT32_MAX || (addr >= entry->start &&
426                                            addr < entry->end));
427         return lkey;
428 }
429
430 /**
431  * Free MR resources. MR lock must not be held to avoid a deadlock. rte_free()
432  * can raise memory free event and the callback function will spin on the lock.
433  *
434  * @param mr
435  *   Pointer to MR to free.
436  */
437 static void
438 mr_free(struct mlx5_mr *mr, mlx5_dereg_mr_t dereg_mr_cb)
439 {
440         if (mr == NULL)
441                 return;
442         DRV_LOG(DEBUG, "freeing MR(%p):", (void *)mr);
443         dereg_mr_cb(&mr->pmd_mr);
444         if (mr->ms_bmp != NULL)
445                 rte_bitmap_free(mr->ms_bmp);
446         rte_free(mr);
447 }
448
449 void
450 mlx5_mr_rebuild_cache(struct mlx5_mr_share_cache *share_cache)
451 {
452         struct mlx5_mr *mr;
453
454         DRV_LOG(DEBUG, "Rebuild dev cache[] %p", (void *)share_cache);
455         /* Flush cache to rebuild. */
456         share_cache->cache.len = 1;
457         share_cache->cache.overflow = 0;
458         /* Iterate all the existing MRs. */
459         LIST_FOREACH(mr, &share_cache->mr_list, mr)
460                 if (mlx5_mr_insert_cache(share_cache, mr) < 0)
461                         return;
462 }
463
464 /**
465  * Release resources of detached MR having no online entry.
466  *
467  * @param share_cache
468  *   Pointer to a global shared MR cache.
469  */
470 static void
471 mlx5_mr_garbage_collect(struct mlx5_mr_share_cache *share_cache)
472 {
473         struct mlx5_mr *mr_next;
474         struct mlx5_mr_list free_list = LIST_HEAD_INITIALIZER(free_list);
475
476         /* Must be called from the primary process. */
477         MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
478         /*
479          * MR can't be freed with holding the lock because rte_free() could call
480          * memory free callback function. This will be a deadlock situation.
481          */
482         rte_rwlock_write_lock(&share_cache->rwlock);
483         /* Detach the whole free list and release it after unlocking. */
484         free_list = share_cache->mr_free_list;
485         LIST_INIT(&share_cache->mr_free_list);
486         rte_rwlock_write_unlock(&share_cache->rwlock);
487         /* Release resources. */
488         mr_next = LIST_FIRST(&free_list);
489         while (mr_next != NULL) {
490                 struct mlx5_mr *mr = mr_next;
491
492                 mr_next = LIST_NEXT(mr, mr);
493                 mr_free(mr, share_cache->dereg_mr_cb);
494         }
495 }
496
497 /* Called during rte_memseg_contig_walk() by mlx5_mr_create(). */
498 static int
499 mr_find_contig_memsegs_cb(const struct rte_memseg_list *msl,
500                           const struct rte_memseg *ms, size_t len, void *arg)
501 {
502         struct mr_find_contig_memsegs_data *data = arg;
503
504         if (data->addr < ms->addr_64 || data->addr >= ms->addr_64 + len)
505                 return 0;
506         /* Found, save it and stop walking. */
507         data->start = ms->addr_64;
508         data->end = ms->addr_64 + len;
509         data->msl = msl;
510         return 1;
511 }
512
513 /**
514  * Create a new global Memory Region (MR) for a missing virtual address.
515  * This API should be called on a secondary process, then a request is sent to
516  * the primary process in order to create a MR for the address. As the global MR
517  * list is on the shared memory, following LKey lookup should succeed unless the
518  * request fails.
519  *
520  * @param pd
521  *   Pointer to pd of a device (net, regex, vdpa,...).
522  * @param share_cache
523  *   Pointer to a global shared MR cache.
524  * @param[out] entry
525  *   Pointer to returning MR cache entry, found in the global cache or newly
526  *   created. If failed to create one, this will not be updated.
527  * @param addr
528  *   Target virtual address to register.
529  * @param mr_ext_memseg_en
530  *   Configurable flag about external memory segment enable or not.
531  *
532  * @return
533  *   Searched LKey on success, UINT32_MAX on failure and rte_errno is set.
534  */
535 static uint32_t
536 mlx5_mr_create_secondary(void *pd __rte_unused,
537                          struct mlx5_mp_id *mp_id,
538                          struct mlx5_mr_share_cache *share_cache,
539                          struct mr_cache_entry *entry, uintptr_t addr,
540                          unsigned int mr_ext_memseg_en __rte_unused)
541 {
542         int ret;
543
544         DEBUG("port %u requesting MR creation for address (%p)",
545               mp_id->port_id, (void *)addr);
546         ret = mlx5_mp_req_mr_create(mp_id, addr);
547         if (ret) {
548                 DEBUG("Fail to request MR creation for address (%p)",
549                       (void *)addr);
550                 return UINT32_MAX;
551         }
552         rte_rwlock_read_lock(&share_cache->rwlock);
553         /* Fill in output data. */
554         mlx5_mr_lookup_cache(share_cache, entry, addr);
555         /* Lookup can't fail. */
556         MLX5_ASSERT(entry->lkey != UINT32_MAX);
557         rte_rwlock_read_unlock(&share_cache->rwlock);
558         DEBUG("MR CREATED by primary process for %p:\n"
559               "  [0x%" PRIxPTR ", 0x%" PRIxPTR "), lkey=0x%x",
560               (void *)addr, entry->start, entry->end, entry->lkey);
561         return entry->lkey;
562 }
563
564 /**
565  * Create a new global Memory Region (MR) for a missing virtual address.
566  * Register entire virtually contiguous memory chunk around the address.
567  *
568  * @param pd
569  *   Pointer to pd of a device (net, regex, vdpa,...).
570  * @param share_cache
571  *   Pointer to a global shared MR cache.
572  * @param[out] entry
573  *   Pointer to returning MR cache entry, found in the global cache or newly
574  *   created. If failed to create one, this will not be updated.
575  * @param addr
576  *   Target virtual address to register.
577  * @param mr_ext_memseg_en
578  *   Configurable flag about external memory segment enable or not.
579  *
580  * @return
581  *   Searched LKey on success, UINT32_MAX on failure and rte_errno is set.
582  */
583 uint32_t
584 mlx5_mr_create_primary(void *pd,
585                        struct mlx5_mr_share_cache *share_cache,
586                        struct mr_cache_entry *entry, uintptr_t addr,
587                        unsigned int mr_ext_memseg_en)
588 {
589         struct mr_find_contig_memsegs_data data = {.addr = addr, };
590         struct mr_find_contig_memsegs_data data_re;
591         const struct rte_memseg_list *msl;
592         const struct rte_memseg *ms;
593         struct mlx5_mr *mr = NULL;
594         int ms_idx_shift = -1;
595         uint32_t bmp_size;
596         void *bmp_mem;
597         uint32_t ms_n;
598         uint32_t n;
599         size_t len;
600
601         DRV_LOG(DEBUG, "Creating a MR using address (%p)", (void *)addr);
602         /*
603          * Release detached MRs if any. This can't be called with holding either
604          * memory_hotplug_lock or share_cache->rwlock. MRs on the free list have
605          * been detached by the memory free event but it couldn't be released
606          * inside the callback due to deadlock. As a result, releasing resources
607          * is quite opportunistic.
608          */
609         mlx5_mr_garbage_collect(share_cache);
610         /*
611          * If enabled, find out a contiguous virtual address chunk in use, to
612          * which the given address belongs, in order to register maximum range.
613          * In the best case where mempools are not dynamically recreated and
614          * '--socket-mem' is specified as an EAL option, it is very likely to
615          * have only one MR(LKey) per a socket and per a hugepage-size even
616          * though the system memory is highly fragmented. As the whole memory
617          * chunk will be pinned by kernel, it can't be reused unless entire
618          * chunk is freed from EAL.
619          *
620          * If disabled, just register one memseg (page). Then, memory
621          * consumption will be minimized but it may drop performance if there
622          * are many MRs to lookup on the datapath.
623          */
624         if (!mr_ext_memseg_en) {
625                 data.msl = rte_mem_virt2memseg_list((void *)addr);
626                 data.start = RTE_ALIGN_FLOOR(addr, data.msl->page_sz);
627                 data.end = data.start + data.msl->page_sz;
628         } else if (!rte_memseg_contig_walk(mr_find_contig_memsegs_cb, &data)) {
629                 DRV_LOG(WARNING,
630                         "Unable to find virtually contiguous"
631                         " chunk for address (%p)."
632                         " rte_memseg_contig_walk() failed.", (void *)addr);
633                 rte_errno = ENXIO;
634                 goto err_nolock;
635         }
636 alloc_resources:
637         /* Addresses must be page-aligned. */
638         MLX5_ASSERT(data.msl);
639         MLX5_ASSERT(rte_is_aligned((void *)data.start, data.msl->page_sz));
640         MLX5_ASSERT(rte_is_aligned((void *)data.end, data.msl->page_sz));
641         msl = data.msl;
642         ms = rte_mem_virt2memseg((void *)data.start, msl);
643         len = data.end - data.start;
644         MLX5_ASSERT(ms);
645         MLX5_ASSERT(msl->page_sz == ms->hugepage_sz);
646         /* Number of memsegs in the range. */
647         ms_n = len / msl->page_sz;
648         DEBUG("Extending %p to [0x%" PRIxPTR ", 0x%" PRIxPTR "),"
649               " page_sz=0x%" PRIx64 ", ms_n=%u",
650               (void *)addr, data.start, data.end, msl->page_sz, ms_n);
651         /* Size of memory for bitmap. */
652         bmp_size = rte_bitmap_get_memory_footprint(ms_n);
653         mr = rte_zmalloc_socket(NULL,
654                                 RTE_ALIGN_CEIL(sizeof(*mr),
655                                                RTE_CACHE_LINE_SIZE) +
656                                 bmp_size,
657                                 RTE_CACHE_LINE_SIZE, msl->socket_id);
658         if (mr == NULL) {
659                 DEBUG("Unable to allocate memory for a new MR of"
660                       " address (%p).", (void *)addr);
661                 rte_errno = ENOMEM;
662                 goto err_nolock;
663         }
664         mr->msl = msl;
665         /*
666          * Save the index of the first memseg and initialize memseg bitmap. To
667          * see if a memseg of ms_idx in the memseg-list is still valid, check:
668          *      rte_bitmap_get(mr->bmp, ms_idx - mr->ms_base_idx)
669          */
670         mr->ms_base_idx = rte_fbarray_find_idx(&msl->memseg_arr, ms);
671         bmp_mem = RTE_PTR_ALIGN_CEIL(mr + 1, RTE_CACHE_LINE_SIZE);
672         mr->ms_bmp = rte_bitmap_init(ms_n, bmp_mem, bmp_size);
673         if (mr->ms_bmp == NULL) {
674                 DEBUG("Unable to initialize bitmap for a new MR of"
675                       " address (%p).", (void *)addr);
676                 rte_errno = EINVAL;
677                 goto err_nolock;
678         }
679         /*
680          * Should recheck whether the extended contiguous chunk is still valid.
681          * Because memory_hotplug_lock can't be held if there's any memory
682          * related calls in a critical path, resource allocation above can't be
683          * locked. If the memory has been changed at this point, try again with
684          * just single page. If not, go on with the big chunk atomically from
685          * here.
686          */
687         rte_mcfg_mem_read_lock();
688         data_re = data;
689         if (len > msl->page_sz &&
690             !rte_memseg_contig_walk(mr_find_contig_memsegs_cb, &data_re)) {
691                 DEBUG("Unable to find virtually contiguous"
692                       " chunk for address (%p)."
693                       " rte_memseg_contig_walk() failed.", (void *)addr);
694                 rte_errno = ENXIO;
695                 goto err_memlock;
696         }
697         if (data.start != data_re.start || data.end != data_re.end) {
698                 /*
699                  * The extended contiguous chunk has been changed. Try again
700                  * with single memseg instead.
701                  */
702                 data.start = RTE_ALIGN_FLOOR(addr, msl->page_sz);
703                 data.end = data.start + msl->page_sz;
704                 rte_mcfg_mem_read_unlock();
705                 mr_free(mr, share_cache->dereg_mr_cb);
706                 goto alloc_resources;
707         }
708         MLX5_ASSERT(data.msl == data_re.msl);
709         rte_rwlock_write_lock(&share_cache->rwlock);
710         /*
711          * Check the address is really missing. If other thread already created
712          * one or it is not found due to overflow, abort and return.
713          */
714         if (mlx5_mr_lookup_cache(share_cache, entry, addr) != UINT32_MAX) {
715                 /*
716                  * Insert to the global cache table. It may fail due to
717                  * low-on-memory. Then, this entry will have to be searched
718                  * here again.
719                  */
720                 mr_btree_insert(&share_cache->cache, entry);
721                 DEBUG("Found MR for %p on final lookup, abort", (void *)addr);
722                 rte_rwlock_write_unlock(&share_cache->rwlock);
723                 rte_mcfg_mem_read_unlock();
724                 /*
725                  * Must be unlocked before calling rte_free() because
726                  * mlx5_mr_mem_event_free_cb() can be called inside.
727                  */
728                 mr_free(mr, share_cache->dereg_mr_cb);
729                 return entry->lkey;
730         }
731         /*
732          * Trim start and end addresses for verbs MR. Set bits for registering
733          * memsegs but exclude already registered ones. Bitmap can be
734          * fragmented.
735          */
736         for (n = 0; n < ms_n; ++n) {
737                 uintptr_t start;
738                 struct mr_cache_entry ret;
739
740                 memset(&ret, 0, sizeof(ret));
741                 start = data_re.start + n * msl->page_sz;
742                 /* Exclude memsegs already registered by other MRs. */
743                 if (mlx5_mr_lookup_cache(share_cache, &ret, start) ==
744                     UINT32_MAX) {
745                         /*
746                          * Start from the first unregistered memseg in the
747                          * extended range.
748                          */
749                         if (ms_idx_shift == -1) {
750                                 mr->ms_base_idx += n;
751                                 data.start = start;
752                                 ms_idx_shift = n;
753                         }
754                         data.end = start + msl->page_sz;
755                         rte_bitmap_set(mr->ms_bmp, n - ms_idx_shift);
756                         ++mr->ms_n;
757                 }
758         }
759         len = data.end - data.start;
760         mr->ms_bmp_n = len / msl->page_sz;
761         MLX5_ASSERT(ms_idx_shift + mr->ms_bmp_n <= ms_n);
762         /*
763          * Finally create an MR for the memory chunk. Verbs: ibv_reg_mr() can
764          * be called with holding the memory lock because it doesn't use
765          * mlx5_alloc_buf_extern() which eventually calls rte_malloc_socket()
766          * through mlx5_alloc_verbs_buf().
767          */
768         share_cache->reg_mr_cb(pd, (void *)data.start, len, &mr->pmd_mr);
769         if (mr->pmd_mr.obj == NULL) {
770                 DEBUG("Fail to create an MR for address (%p)",
771                       (void *)addr);
772                 rte_errno = EINVAL;
773                 goto err_mrlock;
774         }
775         MLX5_ASSERT((uintptr_t)mr->pmd_mr.addr == data.start);
776         MLX5_ASSERT(mr->pmd_mr.len);
777         LIST_INSERT_HEAD(&share_cache->mr_list, mr, mr);
778         DEBUG("MR CREATED (%p) for %p:\n"
779               "  [0x%" PRIxPTR ", 0x%" PRIxPTR "),"
780               " lkey=0x%x base_idx=%u ms_n=%u, ms_bmp_n=%u",
781               (void *)mr, (void *)addr, data.start, data.end,
782               rte_cpu_to_be_32(mr->pmd_mr.lkey),
783               mr->ms_base_idx, mr->ms_n, mr->ms_bmp_n);
784         /* Insert to the global cache table. */
785         mlx5_mr_insert_cache(share_cache, mr);
786         /* Fill in output data. */
787         mlx5_mr_lookup_cache(share_cache, entry, addr);
788         /* Lookup can't fail. */
789         MLX5_ASSERT(entry->lkey != UINT32_MAX);
790         rte_rwlock_write_unlock(&share_cache->rwlock);
791         rte_mcfg_mem_read_unlock();
792         return entry->lkey;
793 err_mrlock:
794         rte_rwlock_write_unlock(&share_cache->rwlock);
795 err_memlock:
796         rte_mcfg_mem_read_unlock();
797 err_nolock:
798         /*
799          * In case of error, as this can be called in a datapath, a warning
800          * message per an error is preferable instead. Must be unlocked before
801          * calling rte_free() because mlx5_mr_mem_event_free_cb() can be called
802          * inside.
803          */
804         mr_free(mr, share_cache->dereg_mr_cb);
805         return UINT32_MAX;
806 }
807
808 /**
809  * Create a new global Memory Region (MR) for a missing virtual address.
810  * This can be called from primary and secondary process.
811  *
812  * @param pd
813  *   Pointer to pd handle of a device (net, regex, vdpa,...).
814  * @param share_cache
815  *   Pointer to a global shared MR cache.
816  * @param[out] entry
817  *   Pointer to returning MR cache entry, found in the global cache or newly
818  *   created. If failed to create one, this will not be updated.
819  * @param addr
820  *   Target virtual address to register.
821  *
822  * @return
823  *   Searched LKey on success, UINT32_MAX on failure and rte_errno is set.
824  */
825 static uint32_t
826 mlx5_mr_create(void *pd, struct mlx5_mp_id *mp_id,
827                struct mlx5_mr_share_cache *share_cache,
828                struct mr_cache_entry *entry, uintptr_t addr,
829                unsigned int mr_ext_memseg_en)
830 {
831         uint32_t ret = 0;
832
833         switch (rte_eal_process_type()) {
834         case RTE_PROC_PRIMARY:
835                 ret = mlx5_mr_create_primary(pd, share_cache, entry,
836                                              addr, mr_ext_memseg_en);
837                 break;
838         case RTE_PROC_SECONDARY:
839                 ret = mlx5_mr_create_secondary(pd, mp_id, share_cache, entry,
840                                                addr, mr_ext_memseg_en);
841                 break;
842         default:
843                 break;
844         }
845         return ret;
846 }
847
848 /**
849  * Look up address in the global MR cache table. If not found, create a new MR.
850  * Insert the found/created entry to local bottom-half cache table.
851  *
852  * @param pd
853  *   Pointer to pd of a device (net, regex, vdpa,...).
854  * @param share_cache
855  *   Pointer to a global shared MR cache.
856  * @param mr_ctrl
857  *   Pointer to per-queue MR control structure.
858  * @param[out] entry
859  *   Pointer to returning MR cache entry, found in the global cache or newly
860  *   created. If failed to create one, this is not written.
861  * @param addr
862  *   Search key.
863  *
864  * @return
865  *   Searched LKey on success, UINT32_MAX on no match.
866  */
867 static uint32_t
868 mr_lookup_caches(void *pd, struct mlx5_mp_id *mp_id,
869                  struct mlx5_mr_share_cache *share_cache,
870                  struct mlx5_mr_ctrl *mr_ctrl,
871                  struct mr_cache_entry *entry, uintptr_t addr,
872                  unsigned int mr_ext_memseg_en)
873 {
874         struct mlx5_mr_btree *bt = &mr_ctrl->cache_bh;
875         uint32_t lkey;
876         uint16_t idx;
877
878         /* If local cache table is full, try to double it. */
879         if (unlikely(bt->len == bt->size))
880                 mr_btree_expand(bt, bt->size << 1);
881         /* Look up in the global cache. */
882         rte_rwlock_read_lock(&share_cache->rwlock);
883         lkey = mr_btree_lookup(&share_cache->cache, &idx, addr);
884         if (lkey != UINT32_MAX) {
885                 /* Found. */
886                 *entry = (*share_cache->cache.table)[idx];
887                 rte_rwlock_read_unlock(&share_cache->rwlock);
888                 /*
889                  * Update local cache. Even if it fails, return the found entry
890                  * to update top-half cache. Next time, this entry will be found
891                  * in the global cache.
892                  */
893                 mr_btree_insert(bt, entry);
894                 return lkey;
895         }
896         rte_rwlock_read_unlock(&share_cache->rwlock);
897         /* First time to see the address? Create a new MR. */
898         lkey = mlx5_mr_create(pd, mp_id, share_cache, entry, addr,
899                               mr_ext_memseg_en);
900         /*
901          * Update the local cache if successfully created a new global MR. Even
902          * if failed to create one, there's no action to take in this datapath
903          * code. As returning LKey is invalid, this will eventually make HW
904          * fail.
905          */
906         if (lkey != UINT32_MAX)
907                 mr_btree_insert(bt, entry);
908         return lkey;
909 }
910
911 /**
912  * Bottom-half of LKey search on datapath. First search in cache_bh[] and if
913  * misses, search in the global MR cache table and update the new entry to
914  * per-queue local caches.
915  *
916  * @param pd
917  *   Pointer to pd of a device (net, regex, vdpa,...).
918  * @param share_cache
919  *   Pointer to a global shared MR cache.
920  * @param mr_ctrl
921  *   Pointer to per-queue MR control structure.
922  * @param addr
923  *   Search key.
924  *
925  * @return
926  *   Searched LKey on success, UINT32_MAX on no match.
927  */
928 uint32_t mlx5_mr_addr2mr_bh(void *pd, struct mlx5_mp_id *mp_id,
929                             struct mlx5_mr_share_cache *share_cache,
930                             struct mlx5_mr_ctrl *mr_ctrl,
931                             uintptr_t addr, unsigned int mr_ext_memseg_en)
932 {
933         uint32_t lkey;
934         uint16_t bh_idx = 0;
935         /* Victim in top-half cache to replace with new entry. */
936         struct mr_cache_entry *repl = &mr_ctrl->cache[mr_ctrl->head];
937
938         /* Binary-search MR translation table. */
939         lkey = mr_btree_lookup(&mr_ctrl->cache_bh, &bh_idx, addr);
940         /* Update top-half cache. */
941         if (likely(lkey != UINT32_MAX)) {
942                 *repl = (*mr_ctrl->cache_bh.table)[bh_idx];
943         } else {
944                 /*
945                  * If missed in local lookup table, search in the global cache
946                  * and local cache_bh[] will be updated inside if possible.
947                  * Top-half cache entry will also be updated.
948                  */
949                 lkey = mr_lookup_caches(pd, mp_id, share_cache, mr_ctrl,
950                                         repl, addr, mr_ext_memseg_en);
951                 if (unlikely(lkey == UINT32_MAX))
952                         return UINT32_MAX;
953         }
954         /* Update the most recently used entry. */
955         mr_ctrl->mru = mr_ctrl->head;
956         /* Point to the next victim, the oldest. */
957         mr_ctrl->head = (mr_ctrl->head + 1) % MLX5_MR_CACHE_N;
958         return lkey;
959 }
960
961 /**
962  * Release all the created MRs and resources on global MR cache of a device.
963  * list.
964  *
965  * @param share_cache
966  *   Pointer to a global shared MR cache.
967  */
968 void
969 mlx5_mr_release_cache(struct mlx5_mr_share_cache *share_cache)
970 {
971         struct mlx5_mr *mr_next;
972
973         rte_rwlock_write_lock(&share_cache->rwlock);
974         /* Detach from MR list and move to free list. */
975         mr_next = LIST_FIRST(&share_cache->mr_list);
976         while (mr_next != NULL) {
977                 struct mlx5_mr *mr = mr_next;
978
979                 mr_next = LIST_NEXT(mr, mr);
980                 LIST_REMOVE(mr, mr);
981                 LIST_INSERT_HEAD(&share_cache->mr_free_list, mr, mr);
982         }
983         LIST_INIT(&share_cache->mr_list);
984         /* Free global cache. */
985         mlx5_mr_btree_free(&share_cache->cache);
986         rte_rwlock_write_unlock(&share_cache->rwlock);
987         /* Free all remaining MRs. */
988         mlx5_mr_garbage_collect(share_cache);
989 }
990
991 /**
992  * Flush all of the local cache entries.
993  *
994  * @param mr_ctrl
995  *   Pointer to per-queue MR local cache.
996  */
997 void
998 mlx5_mr_flush_local_cache(struct mlx5_mr_ctrl *mr_ctrl)
999 {
1000         /* Reset the most-recently-used index. */
1001         mr_ctrl->mru = 0;
1002         /* Reset the linear search array. */
1003         mr_ctrl->head = 0;
1004         memset(mr_ctrl->cache, 0, sizeof(mr_ctrl->cache));
1005         /* Reset the B-tree table. */
1006         mr_ctrl->cache_bh.len = 1;
1007         mr_ctrl->cache_bh.overflow = 0;
1008         /* Update the generation number. */
1009         mr_ctrl->cur_gen = *mr_ctrl->dev_gen_ptr;
1010         DRV_LOG(DEBUG, "mr_ctrl(%p): flushed, cur_gen=%d",
1011                 (void *)mr_ctrl, mr_ctrl->cur_gen);
1012 }
1013
1014 /**
1015  * Creates a memory region for external memory, that is memory which is not
1016  * part of the DPDK memory segments.
1017  *
1018  * @param pd
1019  *   Pointer to pd of a device (net, regex, vdpa,...).
1020  * @param addr
1021  *   Starting virtual address of memory.
1022  * @param len
1023  *   Length of memory segment being mapped.
1024  * @param socked_id
1025  *   Socket to allocate heap memory for the control structures.
1026  *
1027  * @return
1028  *   Pointer to MR structure on success, NULL otherwise.
1029  */
1030 struct mlx5_mr *
1031 mlx5_create_mr_ext(void *pd, uintptr_t addr, size_t len, int socket_id,
1032                    mlx5_reg_mr_t reg_mr_cb)
1033 {
1034         struct mlx5_mr *mr = NULL;
1035
1036         mr = rte_zmalloc_socket(NULL,
1037                                 RTE_ALIGN_CEIL(sizeof(*mr),
1038                                                RTE_CACHE_LINE_SIZE),
1039                                 RTE_CACHE_LINE_SIZE, socket_id);
1040         if (mr == NULL)
1041                 return NULL;
1042         reg_mr_cb(pd, (void *)addr, len, &mr->pmd_mr);
1043         if (mr->pmd_mr.obj == NULL) {
1044                 DRV_LOG(WARNING,
1045                         "Fail to create MR for address (%p)",
1046                         (void *)addr);
1047                 rte_free(mr);
1048                 return NULL;
1049         }
1050         mr->msl = NULL; /* Mark it is external memory. */
1051         mr->ms_bmp = NULL;
1052         mr->ms_n = 1;
1053         mr->ms_bmp_n = 1;
1054         DRV_LOG(DEBUG,
1055                 "MR CREATED (%p) for external memory %p:\n"
1056                 "  [0x%" PRIxPTR ", 0x%" PRIxPTR "),"
1057                 " lkey=0x%x base_idx=%u ms_n=%u, ms_bmp_n=%u",
1058                 (void *)mr, (void *)addr,
1059                 addr, addr + len, rte_cpu_to_be_32(mr->pmd_mr.lkey),
1060                 mr->ms_base_idx, mr->ms_n, mr->ms_bmp_n);
1061         return mr;
1062 }
1063
1064 /**
1065  * Dump all the created MRs and the global cache entries.
1066  *
1067  * @param sh
1068  *   Pointer to Ethernet device shared context.
1069  */
1070 void
1071 mlx5_mr_dump_cache(struct mlx5_mr_share_cache *share_cache __rte_unused)
1072 {
1073 #ifdef RTE_LIBRTE_MLX5_DEBUG
1074         struct mlx5_mr *mr;
1075         int mr_n = 0;
1076         int chunk_n = 0;
1077
1078         rte_rwlock_read_lock(&share_cache->rwlock);
1079         /* Iterate all the existing MRs. */
1080         LIST_FOREACH(mr, &share_cache->mr_list, mr) {
1081                 unsigned int n;
1082
1083                 DEBUG("MR[%u], LKey = 0x%x, ms_n = %u, ms_bmp_n = %u",
1084                       mr_n++, rte_cpu_to_be_32(mr->pmd_mr.lkey),
1085                       mr->ms_n, mr->ms_bmp_n);
1086                 if (mr->ms_n == 0)
1087                         continue;
1088                 for (n = 0; n < mr->ms_bmp_n; ) {
1089                         struct mr_cache_entry ret = { 0, };
1090
1091                         n = mr_find_next_chunk(mr, &ret, n);
1092                         if (!ret.end)
1093                                 break;
1094                         DEBUG("  chunk[%u], [0x%" PRIxPTR ", 0x%" PRIxPTR ")",
1095                               chunk_n++, ret.start, ret.end);
1096                 }
1097         }
1098         DEBUG("Dumping global cache %p", (void *)share_cache);
1099         mlx5_mr_btree_dump(&share_cache->cache);
1100         rte_rwlock_read_unlock(&share_cache->rwlock);
1101 #endif
1102 }