1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright 2016 6WIND S.A. 3 * Copyright 2016 Mellanox Technologies, Ltd 4 */ 5 6 #ifdef PEDANTIC 7 #pragma GCC diagnostic ignored "-Wpedantic" 8 #endif 9 #include <infiniband/verbs.h> 10 #ifdef PEDANTIC 11 #pragma GCC diagnostic error "-Wpedantic" 12 #endif 13 14 #include <rte_mempool.h> 15 #include <rte_malloc.h> 16 #include <rte_rwlock.h> 17 18 #include "mlx5.h" 19 #include "mlx5_mr.h" 20 #include "mlx5_rxtx.h" 21 #include "mlx5_glue.h" 22 23 struct mr_find_contig_memsegs_data { 24 uintptr_t addr; 25 uintptr_t start; 26 uintptr_t end; 27 const struct rte_memseg_list *msl; 28 }; 29 30 struct mr_update_mp_data { 31 struct rte_eth_dev *dev; 32 struct mlx5_mr_ctrl *mr_ctrl; 33 int ret; 34 }; 35 36 /** 37 * Expand B-tree table to a given size. Can't be called with holding 38 * memory_hotplug_lock or priv->mr.rwlock due to rte_realloc(). 39 * 40 * @param bt 41 * Pointer to B-tree structure. 42 * @param n 43 * Number of entries for expansion. 44 * 45 * @return 46 * 0 on success, -1 on failure. 47 */ 48 static int 49 mr_btree_expand(struct mlx5_mr_btree *bt, int n) 50 { 51 void *mem; 52 int ret = 0; 53 54 if (n <= bt->size) 55 return ret; 56 /* 57 * Downside of directly using rte_realloc() is that SOCKET_ID_ANY is 58 * used inside if there's no room to expand. Because this is a quite 59 * rare case and a part of very slow path, it is very acceptable. 60 * Initially cache_bh[] will be given practically enough space and once 61 * it is expanded, expansion wouldn't be needed again ever. 62 */ 63 mem = rte_realloc(bt->table, n * sizeof(struct mlx5_mr_cache), 0); 64 if (mem == NULL) { 65 /* Not an error, B-tree search will be skipped. */ 66 DRV_LOG(WARNING, "failed to expand MR B-tree (%p) table", 67 (void *)bt); 68 ret = -1; 69 } else { 70 DRV_LOG(DEBUG, "expanded MR B-tree table (size=%u)", n); 71 bt->table = mem; 72 bt->size = n; 73 } 74 return ret; 75 } 76 77 /** 78 * Look up LKey from given B-tree lookup table, store the last index and return 79 * searched LKey. 80 * 81 * @param bt 82 * Pointer to B-tree structure. 83 * @param[out] idx 84 * Pointer to index. Even on search failure, returns index where it stops 85 * searching so that index can be used when inserting a new entry. 86 * @param addr 87 * Search key. 88 * 89 * @return 90 * Searched LKey on success, UINT32_MAX on no match. 91 */ 92 static uint32_t 93 mr_btree_lookup(struct mlx5_mr_btree *bt, uint16_t *idx, uintptr_t addr) 94 { 95 struct mlx5_mr_cache *lkp_tbl; 96 uint16_t n; 97 uint16_t base = 0; 98 99 assert(bt != NULL); 100 lkp_tbl = *bt->table; 101 n = bt->len; 102 /* First entry must be NULL for comparison. */ 103 assert(bt->len > 0 || (lkp_tbl[0].start == 0 && 104 lkp_tbl[0].lkey == UINT32_MAX)); 105 /* Binary search. */ 106 do { 107 register uint16_t delta = n >> 1; 108 109 if (addr < lkp_tbl[base + delta].start) { 110 n = delta; 111 } else { 112 base += delta; 113 n -= delta; 114 } 115 } while (n > 1); 116 assert(addr >= lkp_tbl[base].start); 117 *idx = base; 118 if (addr < lkp_tbl[base].end) 119 return lkp_tbl[base].lkey; 120 /* Not found. */ 121 return UINT32_MAX; 122 } 123 124 /** 125 * Insert an entry to B-tree lookup table. 126 * 127 * @param bt 128 * Pointer to B-tree structure. 129 * @param entry 130 * Pointer to new entry to insert. 131 * 132 * @return 133 * 0 on success, -1 on failure. 134 */ 135 static int 136 mr_btree_insert(struct mlx5_mr_btree *bt, struct mlx5_mr_cache *entry) 137 { 138 struct mlx5_mr_cache *lkp_tbl; 139 uint16_t idx = 0; 140 size_t shift; 141 142 assert(bt != NULL); 143 assert(bt->len <= bt->size); 144 assert(bt->len > 0); 145 lkp_tbl = *bt->table; 146 /* Find out the slot for insertion. */ 147 if (mr_btree_lookup(bt, &idx, entry->start) != UINT32_MAX) { 148 DRV_LOG(DEBUG, 149 "abort insertion to B-tree(%p): already exist at" 150 " idx=%u [0x%" PRIxPTR ", 0x%" PRIxPTR ") lkey=0x%x", 151 (void *)bt, idx, entry->start, entry->end, entry->lkey); 152 /* Already exist, return. */ 153 return 0; 154 } 155 /* If table is full, return error. */ 156 if (unlikely(bt->len == bt->size)) { 157 bt->overflow = 1; 158 return -1; 159 } 160 /* Insert entry. */ 161 ++idx; 162 shift = (bt->len - idx) * sizeof(struct mlx5_mr_cache); 163 if (shift) 164 memmove(&lkp_tbl[idx + 1], &lkp_tbl[idx], shift); 165 lkp_tbl[idx] = *entry; 166 bt->len++; 167 DRV_LOG(DEBUG, 168 "inserted B-tree(%p)[%u]," 169 " [0x%" PRIxPTR ", 0x%" PRIxPTR ") lkey=0x%x", 170 (void *)bt, idx, entry->start, entry->end, entry->lkey); 171 return 0; 172 } 173 174 /** 175 * Initialize B-tree and allocate memory for lookup table. 176 * 177 * @param bt 178 * Pointer to B-tree structure. 179 * @param n 180 * Number of entries to allocate. 181 * @param socket 182 * NUMA socket on which memory must be allocated. 183 * 184 * @return 185 * 0 on success, a negative errno value otherwise and rte_errno is set. 186 */ 187 int 188 mlx5_mr_btree_init(struct mlx5_mr_btree *bt, int n, int socket) 189 { 190 if (bt == NULL) { 191 rte_errno = EINVAL; 192 return -rte_errno; 193 } 194 assert(!bt->table && !bt->size); 195 memset(bt, 0, sizeof(*bt)); 196 bt->table = rte_calloc_socket("B-tree table", 197 n, sizeof(struct mlx5_mr_cache), 198 0, socket); 199 if (bt->table == NULL) { 200 rte_errno = ENOMEM; 201 DEBUG("failed to allocate memory for btree cache on socket %d", 202 socket); 203 return -rte_errno; 204 } 205 bt->size = n; 206 /* First entry must be NULL for binary search. */ 207 (*bt->table)[bt->len++] = (struct mlx5_mr_cache) { 208 .lkey = UINT32_MAX, 209 }; 210 DEBUG("initialized B-tree %p with table %p", 211 (void *)bt, (void *)bt->table); 212 return 0; 213 } 214 215 /** 216 * Free B-tree resources. 217 * 218 * @param bt 219 * Pointer to B-tree structure. 220 */ 221 void 222 mlx5_mr_btree_free(struct mlx5_mr_btree *bt) 223 { 224 if (bt == NULL) 225 return; 226 DEBUG("freeing B-tree %p with table %p", 227 (void *)bt, (void *)bt->table); 228 rte_free(bt->table); 229 memset(bt, 0, sizeof(*bt)); 230 } 231 232 /** 233 * Dump all the entries in a B-tree 234 * 235 * @param bt 236 * Pointer to B-tree structure. 237 */ 238 void 239 mlx5_mr_btree_dump(struct mlx5_mr_btree *bt __rte_unused) 240 { 241 #ifndef NDEBUG 242 int idx; 243 struct mlx5_mr_cache *lkp_tbl; 244 245 if (bt == NULL) 246 return; 247 lkp_tbl = *bt->table; 248 for (idx = 0; idx < bt->len; ++idx) { 249 struct mlx5_mr_cache *entry = &lkp_tbl[idx]; 250 251 DEBUG("B-tree(%p)[%u]," 252 " [0x%" PRIxPTR ", 0x%" PRIxPTR ") lkey=0x%x", 253 (void *)bt, idx, entry->start, entry->end, entry->lkey); 254 } 255 #endif 256 } 257 258 /** 259 * Find virtually contiguous memory chunk in a given MR. 260 * 261 * @param dev 262 * Pointer to MR structure. 263 * @param[out] entry 264 * Pointer to returning MR cache entry. If not found, this will not be 265 * updated. 266 * @param start_idx 267 * Start index of the memseg bitmap. 268 * 269 * @return 270 * Next index to go on lookup. 271 */ 272 static int 273 mr_find_next_chunk(struct mlx5_mr *mr, struct mlx5_mr_cache *entry, 274 int base_idx) 275 { 276 uintptr_t start = 0; 277 uintptr_t end = 0; 278 uint32_t idx = 0; 279 280 /* MR for external memory doesn't have memseg list. */ 281 if (mr->msl == NULL) { 282 struct ibv_mr *ibv_mr = mr->ibv_mr; 283 284 assert(mr->ms_bmp_n == 1); 285 assert(mr->ms_n == 1); 286 assert(base_idx == 0); 287 /* 288 * Can't search it from memseg list but get it directly from 289 * verbs MR as there's only one chunk. 290 */ 291 entry->start = (uintptr_t)ibv_mr->addr; 292 entry->end = (uintptr_t)ibv_mr->addr + mr->ibv_mr->length; 293 entry->lkey = rte_cpu_to_be_32(mr->ibv_mr->lkey); 294 /* Returning 1 ends iteration. */ 295 return 1; 296 } 297 for (idx = base_idx; idx < mr->ms_bmp_n; ++idx) { 298 if (rte_bitmap_get(mr->ms_bmp, idx)) { 299 const struct rte_memseg_list *msl; 300 const struct rte_memseg *ms; 301 302 msl = mr->msl; 303 ms = rte_fbarray_get(&msl->memseg_arr, 304 mr->ms_base_idx + idx); 305 assert(msl->page_sz == ms->hugepage_sz); 306 if (!start) 307 start = ms->addr_64; 308 end = ms->addr_64 + ms->hugepage_sz; 309 } else if (start) { 310 /* Passed the end of a fragment. */ 311 break; 312 } 313 } 314 if (start) { 315 /* Found one chunk. */ 316 entry->start = start; 317 entry->end = end; 318 entry->lkey = rte_cpu_to_be_32(mr->ibv_mr->lkey); 319 } 320 return idx; 321 } 322 323 /** 324 * Insert a MR to the global B-tree cache. It may fail due to low-on-memory. 325 * Then, this entry will have to be searched by mr_lookup_dev_list() in 326 * mlx5_mr_create() on miss. 327 * 328 * @param dev 329 * Pointer to Ethernet device. 330 * @param mr 331 * Pointer to MR to insert. 332 * 333 * @return 334 * 0 on success, -1 on failure. 335 */ 336 static int 337 mr_insert_dev_cache(struct rte_eth_dev *dev, struct mlx5_mr *mr) 338 { 339 struct priv *priv = dev->data->dev_private; 340 unsigned int n; 341 342 DRV_LOG(DEBUG, "port %u inserting MR(%p) to global cache", 343 dev->data->port_id, (void *)mr); 344 for (n = 0; n < mr->ms_bmp_n; ) { 345 struct mlx5_mr_cache entry; 346 347 memset(&entry, 0, sizeof(entry)); 348 /* Find a contiguous chunk and advance the index. */ 349 n = mr_find_next_chunk(mr, &entry, n); 350 if (!entry.end) 351 break; 352 if (mr_btree_insert(&priv->mr.cache, &entry) < 0) { 353 /* 354 * Overflowed, but the global table cannot be expanded 355 * because of deadlock. 356 */ 357 return -1; 358 } 359 } 360 return 0; 361 } 362 363 /** 364 * Look up address in the original global MR list. 365 * 366 * @param dev 367 * Pointer to Ethernet device. 368 * @param[out] entry 369 * Pointer to returning MR cache entry. If no match, this will not be updated. 370 * @param addr 371 * Search key. 372 * 373 * @return 374 * Found MR on match, NULL otherwise. 375 */ 376 static struct mlx5_mr * 377 mr_lookup_dev_list(struct rte_eth_dev *dev, struct mlx5_mr_cache *entry, 378 uintptr_t addr) 379 { 380 struct priv *priv = dev->data->dev_private; 381 struct mlx5_mr *mr; 382 383 /* Iterate all the existing MRs. */ 384 LIST_FOREACH(mr, &priv->mr.mr_list, mr) { 385 unsigned int n; 386 387 if (mr->ms_n == 0) 388 continue; 389 for (n = 0; n < mr->ms_bmp_n; ) { 390 struct mlx5_mr_cache ret; 391 392 memset(&ret, 0, sizeof(ret)); 393 n = mr_find_next_chunk(mr, &ret, n); 394 if (addr >= ret.start && addr < ret.end) { 395 /* Found. */ 396 *entry = ret; 397 return mr; 398 } 399 } 400 } 401 return NULL; 402 } 403 404 /** 405 * Look up address on device. 406 * 407 * @param dev 408 * Pointer to Ethernet device. 409 * @param[out] entry 410 * Pointer to returning MR cache entry. If no match, this will not be updated. 411 * @param addr 412 * Search key. 413 * 414 * @return 415 * Searched LKey on success, UINT32_MAX on failure and rte_errno is set. 416 */ 417 static uint32_t 418 mr_lookup_dev(struct rte_eth_dev *dev, struct mlx5_mr_cache *entry, 419 uintptr_t addr) 420 { 421 struct priv *priv = dev->data->dev_private; 422 uint16_t idx; 423 uint32_t lkey = UINT32_MAX; 424 struct mlx5_mr *mr; 425 426 /* 427 * If the global cache has overflowed since it failed to expand the 428 * B-tree table, it can't have all the existing MRs. Then, the address 429 * has to be searched by traversing the original MR list instead, which 430 * is very slow path. Otherwise, the global cache is all inclusive. 431 */ 432 if (!unlikely(priv->mr.cache.overflow)) { 433 lkey = mr_btree_lookup(&priv->mr.cache, &idx, addr); 434 if (lkey != UINT32_MAX) 435 *entry = (*priv->mr.cache.table)[idx]; 436 } else { 437 /* Falling back to the slowest path. */ 438 mr = mr_lookup_dev_list(dev, entry, addr); 439 if (mr != NULL) 440 lkey = entry->lkey; 441 } 442 assert(lkey == UINT32_MAX || (addr >= entry->start && 443 addr < entry->end)); 444 return lkey; 445 } 446 447 /** 448 * Free MR resources. MR lock must not be held to avoid a deadlock. rte_free() 449 * can raise memory free event and the callback function will spin on the lock. 450 * 451 * @param mr 452 * Pointer to MR to free. 453 */ 454 static void 455 mr_free(struct mlx5_mr *mr) 456 { 457 if (mr == NULL) 458 return; 459 DRV_LOG(DEBUG, "freeing MR(%p):", (void *)mr); 460 if (mr->ibv_mr != NULL) 461 claim_zero(mlx5_glue->dereg_mr(mr->ibv_mr)); 462 if (mr->ms_bmp != NULL) 463 rte_bitmap_free(mr->ms_bmp); 464 rte_free(mr); 465 } 466 467 /** 468 * Releass resources of detached MR having no online entry. 469 * 470 * @param dev 471 * Pointer to Ethernet device. 472 */ 473 static void 474 mlx5_mr_garbage_collect(struct rte_eth_dev *dev) 475 { 476 struct priv *priv = dev->data->dev_private; 477 struct mlx5_mr *mr_next; 478 struct mlx5_mr_list free_list = LIST_HEAD_INITIALIZER(free_list); 479 480 /* Must be called from the primary process. */ 481 assert(rte_eal_process_type() == RTE_PROC_PRIMARY); 482 /* 483 * MR can't be freed with holding the lock because rte_free() could call 484 * memory free callback function. This will be a deadlock situation. 485 */ 486 rte_rwlock_write_lock(&priv->mr.rwlock); 487 /* Detach the whole free list and release it after unlocking. */ 488 free_list = priv->mr.mr_free_list; 489 LIST_INIT(&priv->mr.mr_free_list); 490 rte_rwlock_write_unlock(&priv->mr.rwlock); 491 /* Release resources. */ 492 mr_next = LIST_FIRST(&free_list); 493 while (mr_next != NULL) { 494 struct mlx5_mr *mr = mr_next; 495 496 mr_next = LIST_NEXT(mr, mr); 497 mr_free(mr); 498 } 499 } 500 501 /* Called during rte_memseg_contig_walk() by mlx5_mr_create(). */ 502 static int 503 mr_find_contig_memsegs_cb(const struct rte_memseg_list *msl, 504 const struct rte_memseg *ms, size_t len, void *arg) 505 { 506 struct mr_find_contig_memsegs_data *data = arg; 507 508 if (data->addr < ms->addr_64 || data->addr >= ms->addr_64 + len) 509 return 0; 510 /* Found, save it and stop walking. */ 511 data->start = ms->addr_64; 512 data->end = ms->addr_64 + len; 513 data->msl = msl; 514 return 1; 515 } 516 517 /** 518 * Create a new global Memroy Region (MR) for a missing virtual address. 519 * Register entire virtually contiguous memory chunk around the address. 520 * 521 * @param dev 522 * Pointer to Ethernet device. 523 * @param[out] entry 524 * Pointer to returning MR cache entry, found in the global cache or newly 525 * created. If failed to create one, this will not be updated. 526 * @param addr 527 * Target virtual address to register. 528 * 529 * @return 530 * Searched LKey on success, UINT32_MAX on failure and rte_errno is set. 531 */ 532 static uint32_t 533 mlx5_mr_create(struct rte_eth_dev *dev, struct mlx5_mr_cache *entry, 534 uintptr_t addr) 535 { 536 struct priv *priv = dev->data->dev_private; 537 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config; 538 const struct rte_memseg_list *msl; 539 const struct rte_memseg *ms; 540 struct mlx5_mr *mr = NULL; 541 size_t len; 542 uint32_t ms_n; 543 uint32_t bmp_size; 544 void *bmp_mem; 545 int ms_idx_shift = -1; 546 unsigned int n; 547 struct mr_find_contig_memsegs_data data = { 548 .addr = addr, 549 }; 550 struct mr_find_contig_memsegs_data data_re; 551 552 DRV_LOG(DEBUG, "port %u creating a MR using address (%p)", 553 dev->data->port_id, (void *)addr); 554 if (rte_eal_process_type() != RTE_PROC_PRIMARY) { 555 DRV_LOG(WARNING, 556 "port %u using address (%p) of unregistered mempool" 557 " in secondary process, please create mempool" 558 " before rte_eth_dev_start()", 559 dev->data->port_id, (void *)addr); 560 rte_errno = EPERM; 561 goto err_nolock; 562 } 563 /* 564 * Release detached MRs if any. This can't be called with holding either 565 * memory_hotplug_lock or priv->mr.rwlock. MRs on the free list have 566 * been detached by the memory free event but it couldn't be released 567 * inside the callback due to deadlock. As a result, releasing resources 568 * is quite opportunistic. 569 */ 570 mlx5_mr_garbage_collect(dev); 571 /* 572 * Find out a contiguous virtual address chunk in use, to which the 573 * given address belongs, in order to register maximum range. In the 574 * best case where mempools are not dynamically recreated and 575 * '--socket-mem' is specified as an EAL option, it is very likely to 576 * have only one MR(LKey) per a socket and per a hugepage-size even 577 * though the system memory is highly fragmented. 578 */ 579 if (!rte_memseg_contig_walk(mr_find_contig_memsegs_cb, &data)) { 580 DRV_LOG(WARNING, 581 "port %u unable to find virtually contiguous" 582 " chunk for address (%p)." 583 " rte_memseg_contig_walk() failed.", 584 dev->data->port_id, (void *)addr); 585 rte_errno = ENXIO; 586 goto err_nolock; 587 } 588 alloc_resources: 589 /* Addresses must be page-aligned. */ 590 assert(rte_is_aligned((void *)data.start, data.msl->page_sz)); 591 assert(rte_is_aligned((void *)data.end, data.msl->page_sz)); 592 msl = data.msl; 593 ms = rte_mem_virt2memseg((void *)data.start, msl); 594 len = data.end - data.start; 595 assert(msl->page_sz == ms->hugepage_sz); 596 /* Number of memsegs in the range. */ 597 ms_n = len / msl->page_sz; 598 DEBUG("port %u extending %p to [0x%" PRIxPTR ", 0x%" PRIxPTR ")," 599 " page_sz=0x%" PRIx64 ", ms_n=%u", 600 dev->data->port_id, (void *)addr, 601 data.start, data.end, msl->page_sz, ms_n); 602 /* Size of memory for bitmap. */ 603 bmp_size = rte_bitmap_get_memory_footprint(ms_n); 604 mr = rte_zmalloc_socket(NULL, 605 RTE_ALIGN_CEIL(sizeof(*mr), 606 RTE_CACHE_LINE_SIZE) + 607 bmp_size, 608 RTE_CACHE_LINE_SIZE, msl->socket_id); 609 if (mr == NULL) { 610 DEBUG("port %u unable to allocate memory for a new MR of" 611 " address (%p).", 612 dev->data->port_id, (void *)addr); 613 rte_errno = ENOMEM; 614 goto err_nolock; 615 } 616 mr->msl = msl; 617 /* 618 * Save the index of the first memseg and initialize memseg bitmap. To 619 * see if a memseg of ms_idx in the memseg-list is still valid, check: 620 * rte_bitmap_get(mr->bmp, ms_idx - mr->ms_base_idx) 621 */ 622 mr->ms_base_idx = rte_fbarray_find_idx(&msl->memseg_arr, ms); 623 bmp_mem = RTE_PTR_ALIGN_CEIL(mr + 1, RTE_CACHE_LINE_SIZE); 624 mr->ms_bmp = rte_bitmap_init(ms_n, bmp_mem, bmp_size); 625 if (mr->ms_bmp == NULL) { 626 DEBUG("port %u unable to initialize bitamp for a new MR of" 627 " address (%p).", 628 dev->data->port_id, (void *)addr); 629 rte_errno = EINVAL; 630 goto err_nolock; 631 } 632 /* 633 * Should recheck whether the extended contiguous chunk is still valid. 634 * Because memory_hotplug_lock can't be held if there's any memory 635 * related calls in a critical path, resource allocation above can't be 636 * locked. If the memory has been changed at this point, try again with 637 * just single page. If not, go on with the big chunk atomically from 638 * here. 639 */ 640 rte_rwlock_read_lock(&mcfg->memory_hotplug_lock); 641 data_re = data; 642 if (len > msl->page_sz && 643 !rte_memseg_contig_walk(mr_find_contig_memsegs_cb, &data_re)) { 644 DEBUG("port %u unable to find virtually contiguous" 645 " chunk for address (%p)." 646 " rte_memseg_contig_walk() failed.", 647 dev->data->port_id, (void *)addr); 648 rte_errno = ENXIO; 649 goto err_memlock; 650 } 651 if (data.start != data_re.start || data.end != data_re.end) { 652 /* 653 * The extended contiguous chunk has been changed. Try again 654 * with single memseg instead. 655 */ 656 data.start = RTE_ALIGN_FLOOR(addr, msl->page_sz); 657 data.end = data.start + msl->page_sz; 658 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock); 659 mr_free(mr); 660 goto alloc_resources; 661 } 662 assert(data.msl == data_re.msl); 663 rte_rwlock_write_lock(&priv->mr.rwlock); 664 /* 665 * Check the address is really missing. If other thread already created 666 * one or it is not found due to overflow, abort and return. 667 */ 668 if (mr_lookup_dev(dev, entry, addr) != UINT32_MAX) { 669 /* 670 * Insert to the global cache table. It may fail due to 671 * low-on-memory. Then, this entry will have to be searched 672 * here again. 673 */ 674 mr_btree_insert(&priv->mr.cache, entry); 675 DEBUG("port %u found MR for %p on final lookup, abort", 676 dev->data->port_id, (void *)addr); 677 rte_rwlock_write_unlock(&priv->mr.rwlock); 678 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock); 679 /* 680 * Must be unlocked before calling rte_free() because 681 * mlx5_mr_mem_event_free_cb() can be called inside. 682 */ 683 mr_free(mr); 684 return entry->lkey; 685 } 686 /* 687 * Trim start and end addresses for verbs MR. Set bits for registering 688 * memsegs but exclude already registered ones. Bitmap can be 689 * fragmented. 690 */ 691 for (n = 0; n < ms_n; ++n) { 692 uintptr_t start; 693 struct mlx5_mr_cache ret; 694 695 memset(&ret, 0, sizeof(ret)); 696 start = data_re.start + n * msl->page_sz; 697 /* Exclude memsegs already registered by other MRs. */ 698 if (mr_lookup_dev(dev, &ret, start) == UINT32_MAX) { 699 /* 700 * Start from the first unregistered memseg in the 701 * extended range. 702 */ 703 if (ms_idx_shift == -1) { 704 mr->ms_base_idx += n; 705 data.start = start; 706 ms_idx_shift = n; 707 } 708 data.end = start + msl->page_sz; 709 rte_bitmap_set(mr->ms_bmp, n - ms_idx_shift); 710 ++mr->ms_n; 711 } 712 } 713 len = data.end - data.start; 714 mr->ms_bmp_n = len / msl->page_sz; 715 assert(ms_idx_shift + mr->ms_bmp_n <= ms_n); 716 /* 717 * Finally create a verbs MR for the memory chunk. ibv_reg_mr() can be 718 * called with holding the memory lock because it doesn't use 719 * mlx5_alloc_buf_extern() which eventually calls rte_malloc_socket() 720 * through mlx5_alloc_verbs_buf(). 721 */ 722 mr->ibv_mr = mlx5_glue->reg_mr(priv->pd, (void *)data.start, len, 723 IBV_ACCESS_LOCAL_WRITE); 724 if (mr->ibv_mr == NULL) { 725 DEBUG("port %u fail to create a verbs MR for address (%p)", 726 dev->data->port_id, (void *)addr); 727 rte_errno = EINVAL; 728 goto err_mrlock; 729 } 730 assert((uintptr_t)mr->ibv_mr->addr == data.start); 731 assert(mr->ibv_mr->length == len); 732 LIST_INSERT_HEAD(&priv->mr.mr_list, mr, mr); 733 DEBUG("port %u MR CREATED (%p) for %p:\n" 734 " [0x%" PRIxPTR ", 0x%" PRIxPTR ")," 735 " lkey=0x%x base_idx=%u ms_n=%u, ms_bmp_n=%u", 736 dev->data->port_id, (void *)mr, (void *)addr, 737 data.start, data.end, rte_cpu_to_be_32(mr->ibv_mr->lkey), 738 mr->ms_base_idx, mr->ms_n, mr->ms_bmp_n); 739 /* Insert to the global cache table. */ 740 mr_insert_dev_cache(dev, mr); 741 /* Fill in output data. */ 742 mr_lookup_dev(dev, entry, addr); 743 /* Lookup can't fail. */ 744 assert(entry->lkey != UINT32_MAX); 745 rte_rwlock_write_unlock(&priv->mr.rwlock); 746 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock); 747 return entry->lkey; 748 err_mrlock: 749 rte_rwlock_write_unlock(&priv->mr.rwlock); 750 err_memlock: 751 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock); 752 err_nolock: 753 /* 754 * In case of error, as this can be called in a datapath, a warning 755 * message per an error is preferable instead. Must be unlocked before 756 * calling rte_free() because mlx5_mr_mem_event_free_cb() can be called 757 * inside. 758 */ 759 mr_free(mr); 760 return UINT32_MAX; 761 } 762 763 /** 764 * Rebuild the global B-tree cache of device from the original MR list. 765 * 766 * @param dev 767 * Pointer to Ethernet device. 768 */ 769 static void 770 mr_rebuild_dev_cache(struct rte_eth_dev *dev) 771 { 772 struct priv *priv = dev->data->dev_private; 773 struct mlx5_mr *mr; 774 775 DRV_LOG(DEBUG, "port %u rebuild dev cache[]", dev->data->port_id); 776 /* Flush cache to rebuild. */ 777 priv->mr.cache.len = 1; 778 priv->mr.cache.overflow = 0; 779 /* Iterate all the existing MRs. */ 780 LIST_FOREACH(mr, &priv->mr.mr_list, mr) 781 if (mr_insert_dev_cache(dev, mr) < 0) 782 return; 783 } 784 785 /** 786 * Callback for memory free event. Iterate freed memsegs and check whether it 787 * belongs to an existing MR. If found, clear the bit from bitmap of MR. As a 788 * result, the MR would be fragmented. If it becomes empty, the MR will be freed 789 * later by mlx5_mr_garbage_collect(). Even if this callback is called from a 790 * secondary process, the garbage collector will be called in primary process 791 * as the secondary process can't call mlx5_mr_create(). 792 * 793 * The global cache must be rebuilt if there's any change and this event has to 794 * be propagated to dataplane threads to flush the local caches. 795 * 796 * @param dev 797 * Pointer to Ethernet device. 798 * @param addr 799 * Address of freed memory. 800 * @param len 801 * Size of freed memory. 802 */ 803 static void 804 mlx5_mr_mem_event_free_cb(struct rte_eth_dev *dev, const void *addr, size_t len) 805 { 806 struct priv *priv = dev->data->dev_private; 807 const struct rte_memseg_list *msl; 808 struct mlx5_mr *mr; 809 int ms_n; 810 int i; 811 int rebuild = 0; 812 813 DEBUG("port %u free callback: addr=%p, len=%zu", 814 dev->data->port_id, addr, len); 815 msl = rte_mem_virt2memseg_list(addr); 816 /* addr and len must be page-aligned. */ 817 assert((uintptr_t)addr == RTE_ALIGN((uintptr_t)addr, msl->page_sz)); 818 assert(len == RTE_ALIGN(len, msl->page_sz)); 819 ms_n = len / msl->page_sz; 820 rte_rwlock_write_lock(&priv->mr.rwlock); 821 /* Clear bits of freed memsegs from MR. */ 822 for (i = 0; i < ms_n; ++i) { 823 const struct rte_memseg *ms; 824 struct mlx5_mr_cache entry; 825 uintptr_t start; 826 int ms_idx; 827 uint32_t pos; 828 829 /* Find MR having this memseg. */ 830 start = (uintptr_t)addr + i * msl->page_sz; 831 mr = mr_lookup_dev_list(dev, &entry, start); 832 if (mr == NULL) 833 continue; 834 assert(mr->msl); /* Can't be external memory. */ 835 ms = rte_mem_virt2memseg((void *)start, msl); 836 assert(ms != NULL); 837 assert(msl->page_sz == ms->hugepage_sz); 838 ms_idx = rte_fbarray_find_idx(&msl->memseg_arr, ms); 839 pos = ms_idx - mr->ms_base_idx; 840 assert(rte_bitmap_get(mr->ms_bmp, pos)); 841 assert(pos < mr->ms_bmp_n); 842 DEBUG("port %u MR(%p): clear bitmap[%u] for addr %p", 843 dev->data->port_id, (void *)mr, pos, (void *)start); 844 rte_bitmap_clear(mr->ms_bmp, pos); 845 if (--mr->ms_n == 0) { 846 LIST_REMOVE(mr, mr); 847 LIST_INSERT_HEAD(&priv->mr.mr_free_list, mr, mr); 848 DEBUG("port %u remove MR(%p) from list", 849 dev->data->port_id, (void *)mr); 850 } 851 /* 852 * MR is fragmented or will be freed. the global cache must be 853 * rebuilt. 854 */ 855 rebuild = 1; 856 } 857 if (rebuild) { 858 mr_rebuild_dev_cache(dev); 859 /* 860 * Flush local caches by propagating invalidation across cores. 861 * rte_smp_wmb() is enough to synchronize this event. If one of 862 * freed memsegs is seen by other core, that means the memseg 863 * has been allocated by allocator, which will come after this 864 * free call. Therefore, this store instruction (incrementing 865 * generation below) will be guaranteed to be seen by other core 866 * before the core sees the newly allocated memory. 867 */ 868 ++priv->mr.dev_gen; 869 DEBUG("broadcasting local cache flush, gen=%d", 870 priv->mr.dev_gen); 871 rte_smp_wmb(); 872 } 873 rte_rwlock_write_unlock(&priv->mr.rwlock); 874 } 875 876 /** 877 * Callback for memory event. This can be called from both primary and secondary 878 * process. 879 * 880 * @param event_type 881 * Memory event type. 882 * @param addr 883 * Address of memory. 884 * @param len 885 * Size of memory. 886 */ 887 void 888 mlx5_mr_mem_event_cb(enum rte_mem_event event_type, const void *addr, 889 size_t len, void *arg __rte_unused) 890 { 891 struct priv *priv; 892 struct mlx5_dev_list *dev_list = &mlx5_shared_data->mem_event_cb_list; 893 894 switch (event_type) { 895 case RTE_MEM_EVENT_FREE: 896 rte_rwlock_write_lock(&mlx5_shared_data->mem_event_rwlock); 897 /* Iterate all the existing mlx5 devices. */ 898 LIST_FOREACH(priv, dev_list, mem_event_cb) 899 mlx5_mr_mem_event_free_cb(ETH_DEV(priv), addr, len); 900 rte_rwlock_write_unlock(&mlx5_shared_data->mem_event_rwlock); 901 break; 902 case RTE_MEM_EVENT_ALLOC: 903 default: 904 break; 905 } 906 } 907 908 /** 909 * Look up address in the global MR cache table. If not found, create a new MR. 910 * Insert the found/created entry to local bottom-half cache table. 911 * 912 * @param dev 913 * Pointer to Ethernet device. 914 * @param mr_ctrl 915 * Pointer to per-queue MR control structure. 916 * @param[out] entry 917 * Pointer to returning MR cache entry, found in the global cache or newly 918 * created. If failed to create one, this is not written. 919 * @param addr 920 * Search key. 921 * 922 * @return 923 * Searched LKey on success, UINT32_MAX on no match. 924 */ 925 static uint32_t 926 mlx5_mr_lookup_dev(struct rte_eth_dev *dev, struct mlx5_mr_ctrl *mr_ctrl, 927 struct mlx5_mr_cache *entry, uintptr_t addr) 928 { 929 struct priv *priv = dev->data->dev_private; 930 struct mlx5_mr_btree *bt = &mr_ctrl->cache_bh; 931 uint16_t idx; 932 uint32_t lkey; 933 934 /* If local cache table is full, try to double it. */ 935 if (unlikely(bt->len == bt->size)) 936 mr_btree_expand(bt, bt->size << 1); 937 /* Look up in the global cache. */ 938 rte_rwlock_read_lock(&priv->mr.rwlock); 939 lkey = mr_btree_lookup(&priv->mr.cache, &idx, addr); 940 if (lkey != UINT32_MAX) { 941 /* Found. */ 942 *entry = (*priv->mr.cache.table)[idx]; 943 rte_rwlock_read_unlock(&priv->mr.rwlock); 944 /* 945 * Update local cache. Even if it fails, return the found entry 946 * to update top-half cache. Next time, this entry will be found 947 * in the global cache. 948 */ 949 mr_btree_insert(bt, entry); 950 return lkey; 951 } 952 rte_rwlock_read_unlock(&priv->mr.rwlock); 953 /* First time to see the address? Create a new MR. */ 954 lkey = mlx5_mr_create(dev, entry, addr); 955 /* 956 * Update the local cache if successfully created a new global MR. Even 957 * if failed to create one, there's no action to take in this datapath 958 * code. As returning LKey is invalid, this will eventually make HW 959 * fail. 960 */ 961 if (lkey != UINT32_MAX) 962 mr_btree_insert(bt, entry); 963 return lkey; 964 } 965 966 /** 967 * Bottom-half of LKey search on datapath. Firstly search in cache_bh[] and if 968 * misses, search in the global MR cache table and update the new entry to 969 * per-queue local caches. 970 * 971 * @param dev 972 * Pointer to Ethernet device. 973 * @param mr_ctrl 974 * Pointer to per-queue MR control structure. 975 * @param addr 976 * Search key. 977 * 978 * @return 979 * Searched LKey on success, UINT32_MAX on no match. 980 */ 981 static uint32_t 982 mlx5_mr_addr2mr_bh(struct rte_eth_dev *dev, struct mlx5_mr_ctrl *mr_ctrl, 983 uintptr_t addr) 984 { 985 uint32_t lkey; 986 uint16_t bh_idx = 0; 987 /* Victim in top-half cache to replace with new entry. */ 988 struct mlx5_mr_cache *repl = &mr_ctrl->cache[mr_ctrl->head]; 989 990 /* Binary-search MR translation table. */ 991 lkey = mr_btree_lookup(&mr_ctrl->cache_bh, &bh_idx, addr); 992 /* Update top-half cache. */ 993 if (likely(lkey != UINT32_MAX)) { 994 *repl = (*mr_ctrl->cache_bh.table)[bh_idx]; 995 } else { 996 /* 997 * If missed in local lookup table, search in the global cache 998 * and local cache_bh[] will be updated inside if possible. 999 * Top-half cache entry will also be updated. 1000 */ 1001 lkey = mlx5_mr_lookup_dev(dev, mr_ctrl, repl, addr); 1002 if (unlikely(lkey == UINT32_MAX)) 1003 return UINT32_MAX; 1004 } 1005 /* Update the most recently used entry. */ 1006 mr_ctrl->mru = mr_ctrl->head; 1007 /* Point to the next victim, the oldest. */ 1008 mr_ctrl->head = (mr_ctrl->head + 1) % MLX5_MR_CACHE_N; 1009 return lkey; 1010 } 1011 1012 /** 1013 * Bottom-half of LKey search on Rx. 1014 * 1015 * @param rxq 1016 * Pointer to Rx queue structure. 1017 * @param addr 1018 * Search key. 1019 * 1020 * @return 1021 * Searched LKey on success, UINT32_MAX on no match. 1022 */ 1023 uint32_t 1024 mlx5_rx_addr2mr_bh(struct mlx5_rxq_data *rxq, uintptr_t addr) 1025 { 1026 struct mlx5_rxq_ctrl *rxq_ctrl = 1027 container_of(rxq, struct mlx5_rxq_ctrl, rxq); 1028 struct mlx5_mr_ctrl *mr_ctrl = &rxq->mr_ctrl; 1029 struct priv *priv = rxq_ctrl->priv; 1030 1031 DRV_LOG(DEBUG, 1032 "Rx queue %u: miss on top-half, mru=%u, head=%u, addr=%p", 1033 rxq_ctrl->idx, mr_ctrl->mru, mr_ctrl->head, (void *)addr); 1034 return mlx5_mr_addr2mr_bh(ETH_DEV(priv), mr_ctrl, addr); 1035 } 1036 1037 /** 1038 * Bottom-half of LKey search on Tx. 1039 * 1040 * @param txq 1041 * Pointer to Tx queue structure. 1042 * @param addr 1043 * Search key. 1044 * 1045 * @return 1046 * Searched LKey on success, UINT32_MAX on no match. 1047 */ 1048 static uint32_t 1049 mlx5_tx_addr2mr_bh(struct mlx5_txq_data *txq, uintptr_t addr) 1050 { 1051 struct mlx5_txq_ctrl *txq_ctrl = 1052 container_of(txq, struct mlx5_txq_ctrl, txq); 1053 struct mlx5_mr_ctrl *mr_ctrl = &txq->mr_ctrl; 1054 struct priv *priv = txq_ctrl->priv; 1055 1056 DRV_LOG(DEBUG, 1057 "Tx queue %u: miss on top-half, mru=%u, head=%u, addr=%p", 1058 txq_ctrl->idx, mr_ctrl->mru, mr_ctrl->head, (void *)addr); 1059 return mlx5_mr_addr2mr_bh(ETH_DEV(priv), mr_ctrl, addr); 1060 } 1061 1062 /** 1063 * Bottom-half of LKey search on Tx. If it can't be searched in the memseg 1064 * list, register the mempool of the mbuf as externally allocated memory. 1065 * 1066 * @param txq 1067 * Pointer to Tx queue structure. 1068 * @param mb 1069 * Pointer to mbuf. 1070 * 1071 * @return 1072 * Searched LKey on success, UINT32_MAX on no match. 1073 */ 1074 uint32_t 1075 mlx5_tx_mb2mr_bh(struct mlx5_txq_data *txq, struct rte_mbuf *mb) 1076 { 1077 uintptr_t addr = (uintptr_t)mb->buf_addr; 1078 uint32_t lkey; 1079 1080 lkey = mlx5_tx_addr2mr_bh(txq, addr); 1081 if (lkey == UINT32_MAX && rte_errno == ENXIO) { 1082 /* Mempool may have externally allocated memory. */ 1083 return mlx5_tx_update_ext_mp(txq, addr, mlx5_mb2mp(mb)); 1084 } 1085 return lkey; 1086 } 1087 1088 /** 1089 * Flush all of the local cache entries. 1090 * 1091 * @param mr_ctrl 1092 * Pointer to per-queue MR control structure. 1093 */ 1094 void 1095 mlx5_mr_flush_local_cache(struct mlx5_mr_ctrl *mr_ctrl) 1096 { 1097 /* Reset the most-recently-used index. */ 1098 mr_ctrl->mru = 0; 1099 /* Reset the linear search array. */ 1100 mr_ctrl->head = 0; 1101 memset(mr_ctrl->cache, 0, sizeof(mr_ctrl->cache)); 1102 /* Reset the B-tree table. */ 1103 mr_ctrl->cache_bh.len = 1; 1104 mr_ctrl->cache_bh.overflow = 0; 1105 /* Update the generation number. */ 1106 mr_ctrl->cur_gen = *mr_ctrl->dev_gen_ptr; 1107 DRV_LOG(DEBUG, "mr_ctrl(%p): flushed, cur_gen=%d", 1108 (void *)mr_ctrl, mr_ctrl->cur_gen); 1109 } 1110 1111 /** 1112 * Called during rte_mempool_mem_iter() by mlx5_mr_update_ext_mp(). 1113 * 1114 * Externally allocated chunk is registered and a MR is created for the chunk. 1115 * The MR object is added to the global list. If memseg list of a MR object 1116 * (mr->msl) is null, the MR object can be regarded as externally allocated 1117 * memory. 1118 * 1119 * Once external memory is registered, it should be static. If the memory is 1120 * freed and the virtual address range has different physical memory mapped 1121 * again, it may cause crash on device due to the wrong translation entry. PMD 1122 * can't track the free event of the external memory for now. 1123 */ 1124 static void 1125 mlx5_mr_update_ext_mp_cb(struct rte_mempool *mp, void *opaque, 1126 struct rte_mempool_memhdr *memhdr, 1127 unsigned mem_idx __rte_unused) 1128 { 1129 struct mr_update_mp_data *data = opaque; 1130 struct rte_eth_dev *dev = data->dev; 1131 struct priv *priv = dev->data->dev_private; 1132 struct mlx5_mr_ctrl *mr_ctrl = data->mr_ctrl; 1133 struct mlx5_mr *mr = NULL; 1134 uintptr_t addr = (uintptr_t)memhdr->addr; 1135 size_t len = memhdr->len; 1136 struct mlx5_mr_cache entry; 1137 uint32_t lkey; 1138 1139 /* If already registered, it should return. */ 1140 rte_rwlock_read_lock(&priv->mr.rwlock); 1141 lkey = mr_lookup_dev(dev, &entry, addr); 1142 rte_rwlock_read_unlock(&priv->mr.rwlock); 1143 if (lkey != UINT32_MAX) 1144 return; 1145 mr = rte_zmalloc_socket(NULL, 1146 RTE_ALIGN_CEIL(sizeof(*mr), 1147 RTE_CACHE_LINE_SIZE), 1148 RTE_CACHE_LINE_SIZE, mp->socket_id); 1149 if (mr == NULL) { 1150 DRV_LOG(WARNING, 1151 "port %u unable to allocate memory for a new MR of" 1152 " mempool (%s).", 1153 dev->data->port_id, mp->name); 1154 data->ret = -1; 1155 return; 1156 } 1157 DRV_LOG(DEBUG, "port %u register MR for chunk #%d of mempool (%s)", 1158 dev->data->port_id, mem_idx, mp->name); 1159 mr->ibv_mr = mlx5_glue->reg_mr(priv->pd, (void *)addr, len, 1160 IBV_ACCESS_LOCAL_WRITE); 1161 if (mr->ibv_mr == NULL) { 1162 DRV_LOG(WARNING, 1163 "port %u fail to create a verbs MR for address (%p)", 1164 dev->data->port_id, (void *)addr); 1165 rte_free(mr); 1166 data->ret = -1; 1167 return; 1168 } 1169 mr->msl = NULL; /* Mark it is external memory. */ 1170 mr->ms_bmp = NULL; 1171 mr->ms_n = 1; 1172 mr->ms_bmp_n = 1; 1173 rte_rwlock_write_lock(&priv->mr.rwlock); 1174 LIST_INSERT_HEAD(&priv->mr.mr_list, mr, mr); 1175 DRV_LOG(DEBUG, 1176 "port %u MR CREATED (%p) for external memory %p:\n" 1177 " [0x%" PRIxPTR ", 0x%" PRIxPTR ")," 1178 " lkey=0x%x base_idx=%u ms_n=%u, ms_bmp_n=%u", 1179 dev->data->port_id, (void *)mr, (void *)addr, 1180 addr, addr + len, rte_cpu_to_be_32(mr->ibv_mr->lkey), 1181 mr->ms_base_idx, mr->ms_n, mr->ms_bmp_n); 1182 /* Insert to the global cache table. */ 1183 mr_insert_dev_cache(dev, mr); 1184 rte_rwlock_write_unlock(&priv->mr.rwlock); 1185 /* Insert to the local cache table */ 1186 mlx5_mr_addr2mr_bh(dev, mr_ctrl, addr); 1187 } 1188 1189 /** 1190 * Register MR for entire memory chunks in a Mempool having externally allocated 1191 * memory and fill in local cache. 1192 * 1193 * @param dev 1194 * Pointer to Ethernet device. 1195 * @param mr_ctrl 1196 * Pointer to per-queue MR control structure. 1197 * @param mp 1198 * Pointer to registering Mempool. 1199 * 1200 * @return 1201 * 0 on success, -1 on failure. 1202 */ 1203 static uint32_t 1204 mlx5_mr_update_ext_mp(struct rte_eth_dev *dev, struct mlx5_mr_ctrl *mr_ctrl, 1205 struct rte_mempool *mp) 1206 { 1207 struct mr_update_mp_data data = { 1208 .dev = dev, 1209 .mr_ctrl = mr_ctrl, 1210 .ret = 0, 1211 }; 1212 1213 rte_mempool_mem_iter(mp, mlx5_mr_update_ext_mp_cb, &data); 1214 return data.ret; 1215 } 1216 1217 /** 1218 * Register MR entire memory chunks in a Mempool having externally allocated 1219 * memory and search LKey of the address to return. 1220 * 1221 * @param dev 1222 * Pointer to Ethernet device. 1223 * @param addr 1224 * Search key. 1225 * @param mp 1226 * Pointer to registering Mempool where addr belongs. 1227 * 1228 * @return 1229 * LKey for address on success, UINT32_MAX on failure. 1230 */ 1231 uint32_t 1232 mlx5_tx_update_ext_mp(struct mlx5_txq_data *txq, uintptr_t addr, 1233 struct rte_mempool *mp) 1234 { 1235 struct mlx5_txq_ctrl *txq_ctrl = 1236 container_of(txq, struct mlx5_txq_ctrl, txq); 1237 struct mlx5_mr_ctrl *mr_ctrl = &txq->mr_ctrl; 1238 struct priv *priv = txq_ctrl->priv; 1239 1240 mlx5_mr_update_ext_mp(ETH_DEV(priv), mr_ctrl, mp); 1241 return mlx5_tx_addr2mr_bh(txq, addr); 1242 } 1243 1244 /* Called during rte_mempool_mem_iter() by mlx5_mr_update_mp(). */ 1245 static void 1246 mlx5_mr_update_mp_cb(struct rte_mempool *mp __rte_unused, void *opaque, 1247 struct rte_mempool_memhdr *memhdr, 1248 unsigned mem_idx __rte_unused) 1249 { 1250 struct mr_update_mp_data *data = opaque; 1251 uint32_t lkey; 1252 1253 /* Stop iteration if failed in the previous walk. */ 1254 if (data->ret < 0) 1255 return; 1256 /* Register address of the chunk and update local caches. */ 1257 lkey = mlx5_mr_addr2mr_bh(data->dev, data->mr_ctrl, 1258 (uintptr_t)memhdr->addr); 1259 if (lkey == UINT32_MAX) 1260 data->ret = -1; 1261 } 1262 1263 /** 1264 * Register entire memory chunks in a Mempool. 1265 * 1266 * @param dev 1267 * Pointer to Ethernet device. 1268 * @param mr_ctrl 1269 * Pointer to per-queue MR control structure. 1270 * @param mp 1271 * Pointer to registering Mempool. 1272 * 1273 * @return 1274 * 0 on success, -1 on failure. 1275 */ 1276 int 1277 mlx5_mr_update_mp(struct rte_eth_dev *dev, struct mlx5_mr_ctrl *mr_ctrl, 1278 struct rte_mempool *mp) 1279 { 1280 struct mr_update_mp_data data = { 1281 .dev = dev, 1282 .mr_ctrl = mr_ctrl, 1283 .ret = 0, 1284 }; 1285 1286 rte_mempool_mem_iter(mp, mlx5_mr_update_mp_cb, &data); 1287 if (data.ret < 0 && rte_errno == ENXIO) { 1288 /* Mempool may have externally allocated memory. */ 1289 return mlx5_mr_update_ext_mp(dev, mr_ctrl, mp); 1290 } 1291 return data.ret; 1292 } 1293 1294 /** 1295 * Dump all the created MRs and the global cache entries. 1296 * 1297 * @param dev 1298 * Pointer to Ethernet device. 1299 */ 1300 void 1301 mlx5_mr_dump_dev(struct rte_eth_dev *dev __rte_unused) 1302 { 1303 #ifndef NDEBUG 1304 struct priv *priv = dev->data->dev_private; 1305 struct mlx5_mr *mr; 1306 int mr_n = 0; 1307 int chunk_n = 0; 1308 1309 rte_rwlock_read_lock(&priv->mr.rwlock); 1310 /* Iterate all the existing MRs. */ 1311 LIST_FOREACH(mr, &priv->mr.mr_list, mr) { 1312 unsigned int n; 1313 1314 DEBUG("port %u MR[%u], LKey = 0x%x, ms_n = %u, ms_bmp_n = %u", 1315 dev->data->port_id, mr_n++, 1316 rte_cpu_to_be_32(mr->ibv_mr->lkey), 1317 mr->ms_n, mr->ms_bmp_n); 1318 if (mr->ms_n == 0) 1319 continue; 1320 for (n = 0; n < mr->ms_bmp_n; ) { 1321 struct mlx5_mr_cache ret = { 0, }; 1322 1323 n = mr_find_next_chunk(mr, &ret, n); 1324 if (!ret.end) 1325 break; 1326 DEBUG(" chunk[%u], [0x%" PRIxPTR ", 0x%" PRIxPTR ")", 1327 chunk_n++, ret.start, ret.end); 1328 } 1329 } 1330 DEBUG("port %u dumping global cache", dev->data->port_id); 1331 mlx5_mr_btree_dump(&priv->mr.cache); 1332 rte_rwlock_read_unlock(&priv->mr.rwlock); 1333 #endif 1334 } 1335 1336 /** 1337 * Release all the created MRs and resources. Remove device from memory callback 1338 * list. 1339 * 1340 * @param dev 1341 * Pointer to Ethernet device. 1342 */ 1343 void 1344 mlx5_mr_release(struct rte_eth_dev *dev) 1345 { 1346 struct priv *priv = dev->data->dev_private; 1347 struct mlx5_mr *mr_next = LIST_FIRST(&priv->mr.mr_list); 1348 1349 /* Remove from memory callback device list. */ 1350 rte_rwlock_write_lock(&mlx5_shared_data->mem_event_rwlock); 1351 LIST_REMOVE(priv, mem_event_cb); 1352 rte_rwlock_write_unlock(&mlx5_shared_data->mem_event_rwlock); 1353 if (rte_log_get_level(mlx5_logtype) == RTE_LOG_DEBUG) 1354 mlx5_mr_dump_dev(dev); 1355 rte_rwlock_write_lock(&priv->mr.rwlock); 1356 /* Detach from MR list and move to free list. */ 1357 while (mr_next != NULL) { 1358 struct mlx5_mr *mr = mr_next; 1359 1360 mr_next = LIST_NEXT(mr, mr); 1361 LIST_REMOVE(mr, mr); 1362 LIST_INSERT_HEAD(&priv->mr.mr_free_list, mr, mr); 1363 } 1364 LIST_INIT(&priv->mr.mr_list); 1365 /* Free global cache. */ 1366 mlx5_mr_btree_free(&priv->mr.cache); 1367 rte_rwlock_write_unlock(&priv->mr.rwlock); 1368 /* Free all remaining MRs. */ 1369 mlx5_mr_garbage_collect(dev); 1370 } 1371