1 /* 2 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved. 3 * 4 * This software is available to you under a choice of one of two 5 * licenses. You may choose to be licensed under the terms of the GNU 6 * General Public License (GPL) Version 2, available from the file 7 * COPYING in the main directory of this source tree, or the 8 * OpenIB.org BSD license below: 9 * 10 * Redistribution and use in source and binary forms, with or 11 * without modification, are permitted provided that the following 12 * conditions are met: 13 * 14 * - Redistributions of source code must retain the above 15 * copyright notice, this list of conditions and the following 16 * disclaimer. 17 * 18 * - Redistributions in binary form must reproduce the above 19 * copyright notice, this list of conditions and the following 20 * disclaimer in the documentation and/or other materials 21 * provided with the distribution. 22 * 23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 30 * SOFTWARE. 31 */ 32 33 #include <rdma/ib_umem.h> 34 #include <rdma/ib_umem_odp.h> 35 #include <linux/kernel.h> 36 37 #include "mlx5_ib.h" 38 #include "cmd.h" 39 40 #include <linux/mlx5/eq.h> 41 42 /* Contains the details of a pagefault. */ 43 struct mlx5_pagefault { 44 u32 bytes_committed; 45 u32 token; 46 u8 event_subtype; 47 u8 type; 48 union { 49 /* Initiator or send message responder pagefault details. */ 50 struct { 51 /* Received packet size, only valid for responders. */ 52 u32 packet_size; 53 /* 54 * Number of resource holding WQE, depends on type. 55 */ 56 u32 wq_num; 57 /* 58 * WQE index. Refers to either the send queue or 59 * receive queue, according to event_subtype. 60 */ 61 u16 wqe_index; 62 } wqe; 63 /* RDMA responder pagefault details */ 64 struct { 65 u32 r_key; 66 /* 67 * Received packet size, minimal size page fault 68 * resolution required for forward progress. 69 */ 70 u32 packet_size; 71 u32 rdma_op_len; 72 u64 rdma_va; 73 } rdma; 74 }; 75 76 struct mlx5_ib_pf_eq *eq; 77 struct work_struct work; 78 }; 79 80 #define MAX_PREFETCH_LEN (4*1024*1024U) 81 82 /* Timeout in ms to wait for an active mmu notifier to complete when handling 83 * a pagefault. */ 84 #define MMU_NOTIFIER_TIMEOUT 1000 85 86 #define MLX5_IMR_MTT_BITS (30 - PAGE_SHIFT) 87 #define MLX5_IMR_MTT_SHIFT (MLX5_IMR_MTT_BITS + PAGE_SHIFT) 88 #define MLX5_IMR_MTT_ENTRIES BIT_ULL(MLX5_IMR_MTT_BITS) 89 #define MLX5_IMR_MTT_SIZE BIT_ULL(MLX5_IMR_MTT_SHIFT) 90 #define MLX5_IMR_MTT_MASK (~(MLX5_IMR_MTT_SIZE - 1)) 91 92 #define MLX5_KSM_PAGE_SHIFT MLX5_IMR_MTT_SHIFT 93 94 static u64 mlx5_imr_ksm_entries; 95 96 static int check_parent(struct ib_umem_odp *odp, 97 struct mlx5_ib_mr *parent) 98 { 99 struct mlx5_ib_mr *mr = odp->private; 100 101 return mr && mr->parent == parent && !odp->dying; 102 } 103 104 static struct ib_ucontext_per_mm *mr_to_per_mm(struct mlx5_ib_mr *mr) 105 { 106 if (WARN_ON(!mr || !is_odp_mr(mr))) 107 return NULL; 108 109 return to_ib_umem_odp(mr->umem)->per_mm; 110 } 111 112 static struct ib_umem_odp *odp_next(struct ib_umem_odp *odp) 113 { 114 struct mlx5_ib_mr *mr = odp->private, *parent = mr->parent; 115 struct ib_ucontext_per_mm *per_mm = odp->per_mm; 116 struct rb_node *rb; 117 118 down_read(&per_mm->umem_rwsem); 119 while (1) { 120 rb = rb_next(&odp->interval_tree.rb); 121 if (!rb) 122 goto not_found; 123 odp = rb_entry(rb, struct ib_umem_odp, interval_tree.rb); 124 if (check_parent(odp, parent)) 125 goto end; 126 } 127 not_found: 128 odp = NULL; 129 end: 130 up_read(&per_mm->umem_rwsem); 131 return odp; 132 } 133 134 static struct ib_umem_odp *odp_lookup(u64 start, u64 length, 135 struct mlx5_ib_mr *parent) 136 { 137 struct ib_ucontext_per_mm *per_mm = mr_to_per_mm(parent); 138 struct ib_umem_odp *odp; 139 struct rb_node *rb; 140 141 down_read(&per_mm->umem_rwsem); 142 odp = rbt_ib_umem_lookup(&per_mm->umem_tree, start, length); 143 if (!odp) 144 goto end; 145 146 while (1) { 147 if (check_parent(odp, parent)) 148 goto end; 149 rb = rb_next(&odp->interval_tree.rb); 150 if (!rb) 151 goto not_found; 152 odp = rb_entry(rb, struct ib_umem_odp, interval_tree.rb); 153 if (ib_umem_start(odp) > start + length) 154 goto not_found; 155 } 156 not_found: 157 odp = NULL; 158 end: 159 up_read(&per_mm->umem_rwsem); 160 return odp; 161 } 162 163 void mlx5_odp_populate_klm(struct mlx5_klm *pklm, size_t offset, 164 size_t nentries, struct mlx5_ib_mr *mr, int flags) 165 { 166 struct ib_pd *pd = mr->ibmr.pd; 167 struct mlx5_ib_dev *dev = to_mdev(pd->device); 168 struct ib_umem_odp *odp; 169 unsigned long va; 170 int i; 171 172 if (flags & MLX5_IB_UPD_XLT_ZAP) { 173 for (i = 0; i < nentries; i++, pklm++) { 174 pklm->bcount = cpu_to_be32(MLX5_IMR_MTT_SIZE); 175 pklm->key = cpu_to_be32(dev->null_mkey); 176 pklm->va = 0; 177 } 178 return; 179 } 180 181 odp = odp_lookup(offset * MLX5_IMR_MTT_SIZE, 182 nentries * MLX5_IMR_MTT_SIZE, mr); 183 184 for (i = 0; i < nentries; i++, pklm++) { 185 pklm->bcount = cpu_to_be32(MLX5_IMR_MTT_SIZE); 186 va = (offset + i) * MLX5_IMR_MTT_SIZE; 187 if (odp && odp->umem.address == va) { 188 struct mlx5_ib_mr *mtt = odp->private; 189 190 pklm->key = cpu_to_be32(mtt->ibmr.lkey); 191 odp = odp_next(odp); 192 } else { 193 pklm->key = cpu_to_be32(dev->null_mkey); 194 } 195 mlx5_ib_dbg(dev, "[%d] va %lx key %x\n", 196 i, va, be32_to_cpu(pklm->key)); 197 } 198 } 199 200 static void mr_leaf_free_action(struct work_struct *work) 201 { 202 struct ib_umem_odp *odp = container_of(work, struct ib_umem_odp, work); 203 int idx = ib_umem_start(odp) >> MLX5_IMR_MTT_SHIFT; 204 struct mlx5_ib_mr *mr = odp->private, *imr = mr->parent; 205 206 mr->parent = NULL; 207 synchronize_srcu(&mr->dev->mr_srcu); 208 209 ib_umem_release(&odp->umem); 210 if (imr->live) 211 mlx5_ib_update_xlt(imr, idx, 1, 0, 212 MLX5_IB_UPD_XLT_INDIRECT | 213 MLX5_IB_UPD_XLT_ATOMIC); 214 mlx5_mr_cache_free(mr->dev, mr); 215 216 if (atomic_dec_and_test(&imr->num_leaf_free)) 217 wake_up(&imr->q_leaf_free); 218 } 219 220 void mlx5_ib_invalidate_range(struct ib_umem_odp *umem_odp, unsigned long start, 221 unsigned long end) 222 { 223 struct mlx5_ib_mr *mr; 224 const u64 umr_block_mask = (MLX5_UMR_MTT_ALIGNMENT / 225 sizeof(struct mlx5_mtt)) - 1; 226 u64 idx = 0, blk_start_idx = 0; 227 int in_block = 0; 228 u64 addr; 229 230 if (!umem_odp) { 231 pr_err("invalidation called on NULL umem or non-ODP umem\n"); 232 return; 233 } 234 235 mr = umem_odp->private; 236 237 if (!mr || !mr->ibmr.pd) 238 return; 239 240 start = max_t(u64, ib_umem_start(umem_odp), start); 241 end = min_t(u64, ib_umem_end(umem_odp), end); 242 243 /* 244 * Iteration one - zap the HW's MTTs. The notifiers_count ensures that 245 * while we are doing the invalidation, no page fault will attempt to 246 * overwrite the same MTTs. Concurent invalidations might race us, 247 * but they will write 0s as well, so no difference in the end result. 248 */ 249 mutex_lock(&umem_odp->umem_mutex); 250 for (addr = start; addr < end; addr += BIT(umem_odp->page_shift)) { 251 idx = (addr - ib_umem_start(umem_odp)) >> umem_odp->page_shift; 252 /* 253 * Strive to write the MTTs in chunks, but avoid overwriting 254 * non-existing MTTs. The huristic here can be improved to 255 * estimate the cost of another UMR vs. the cost of bigger 256 * UMR. 257 */ 258 if (umem_odp->dma_list[idx] & 259 (ODP_READ_ALLOWED_BIT | ODP_WRITE_ALLOWED_BIT)) { 260 if (!in_block) { 261 blk_start_idx = idx; 262 in_block = 1; 263 } 264 } else { 265 u64 umr_offset = idx & umr_block_mask; 266 267 if (in_block && umr_offset == 0) { 268 mlx5_ib_update_xlt(mr, blk_start_idx, 269 idx - blk_start_idx, 0, 270 MLX5_IB_UPD_XLT_ZAP | 271 MLX5_IB_UPD_XLT_ATOMIC); 272 in_block = 0; 273 } 274 } 275 } 276 if (in_block) 277 mlx5_ib_update_xlt(mr, blk_start_idx, 278 idx - blk_start_idx + 1, 0, 279 MLX5_IB_UPD_XLT_ZAP | 280 MLX5_IB_UPD_XLT_ATOMIC); 281 mutex_unlock(&umem_odp->umem_mutex); 282 /* 283 * We are now sure that the device will not access the 284 * memory. We can safely unmap it, and mark it as dirty if 285 * needed. 286 */ 287 288 ib_umem_odp_unmap_dma_pages(umem_odp, start, end); 289 290 if (unlikely(!umem_odp->npages && mr->parent && 291 !umem_odp->dying)) { 292 WRITE_ONCE(umem_odp->dying, 1); 293 atomic_inc(&mr->parent->num_leaf_free); 294 schedule_work(&umem_odp->work); 295 } 296 } 297 298 void mlx5_ib_internal_fill_odp_caps(struct mlx5_ib_dev *dev) 299 { 300 struct ib_odp_caps *caps = &dev->odp_caps; 301 302 memset(caps, 0, sizeof(*caps)); 303 304 if (!MLX5_CAP_GEN(dev->mdev, pg)) 305 return; 306 307 caps->general_caps = IB_ODP_SUPPORT; 308 309 if (MLX5_CAP_GEN(dev->mdev, umr_extended_translation_offset)) 310 dev->odp_max_size = U64_MAX; 311 else 312 dev->odp_max_size = BIT_ULL(MLX5_MAX_UMR_SHIFT + PAGE_SHIFT); 313 314 if (MLX5_CAP_ODP(dev->mdev, ud_odp_caps.send)) 315 caps->per_transport_caps.ud_odp_caps |= IB_ODP_SUPPORT_SEND; 316 317 if (MLX5_CAP_ODP(dev->mdev, ud_odp_caps.srq_receive)) 318 caps->per_transport_caps.ud_odp_caps |= IB_ODP_SUPPORT_SRQ_RECV; 319 320 if (MLX5_CAP_ODP(dev->mdev, rc_odp_caps.send)) 321 caps->per_transport_caps.rc_odp_caps |= IB_ODP_SUPPORT_SEND; 322 323 if (MLX5_CAP_ODP(dev->mdev, rc_odp_caps.receive)) 324 caps->per_transport_caps.rc_odp_caps |= IB_ODP_SUPPORT_RECV; 325 326 if (MLX5_CAP_ODP(dev->mdev, rc_odp_caps.write)) 327 caps->per_transport_caps.rc_odp_caps |= IB_ODP_SUPPORT_WRITE; 328 329 if (MLX5_CAP_ODP(dev->mdev, rc_odp_caps.read)) 330 caps->per_transport_caps.rc_odp_caps |= IB_ODP_SUPPORT_READ; 331 332 if (MLX5_CAP_ODP(dev->mdev, rc_odp_caps.atomic)) 333 caps->per_transport_caps.rc_odp_caps |= IB_ODP_SUPPORT_ATOMIC; 334 335 if (MLX5_CAP_ODP(dev->mdev, rc_odp_caps.srq_receive)) 336 caps->per_transport_caps.rc_odp_caps |= IB_ODP_SUPPORT_SRQ_RECV; 337 338 if (MLX5_CAP_ODP(dev->mdev, xrc_odp_caps.send)) 339 caps->per_transport_caps.xrc_odp_caps |= IB_ODP_SUPPORT_SEND; 340 341 if (MLX5_CAP_ODP(dev->mdev, xrc_odp_caps.receive)) 342 caps->per_transport_caps.xrc_odp_caps |= IB_ODP_SUPPORT_RECV; 343 344 if (MLX5_CAP_ODP(dev->mdev, xrc_odp_caps.write)) 345 caps->per_transport_caps.xrc_odp_caps |= IB_ODP_SUPPORT_WRITE; 346 347 if (MLX5_CAP_ODP(dev->mdev, xrc_odp_caps.read)) 348 caps->per_transport_caps.xrc_odp_caps |= IB_ODP_SUPPORT_READ; 349 350 if (MLX5_CAP_ODP(dev->mdev, xrc_odp_caps.atomic)) 351 caps->per_transport_caps.xrc_odp_caps |= IB_ODP_SUPPORT_ATOMIC; 352 353 if (MLX5_CAP_ODP(dev->mdev, xrc_odp_caps.srq_receive)) 354 caps->per_transport_caps.xrc_odp_caps |= IB_ODP_SUPPORT_SRQ_RECV; 355 356 if (MLX5_CAP_GEN(dev->mdev, fixed_buffer_size) && 357 MLX5_CAP_GEN(dev->mdev, null_mkey) && 358 MLX5_CAP_GEN(dev->mdev, umr_extended_translation_offset)) 359 caps->general_caps |= IB_ODP_SUPPORT_IMPLICIT; 360 361 return; 362 } 363 364 static void mlx5_ib_page_fault_resume(struct mlx5_ib_dev *dev, 365 struct mlx5_pagefault *pfault, 366 int error) 367 { 368 int wq_num = pfault->event_subtype == MLX5_PFAULT_SUBTYPE_WQE ? 369 pfault->wqe.wq_num : pfault->token; 370 u32 out[MLX5_ST_SZ_DW(page_fault_resume_out)] = { }; 371 u32 in[MLX5_ST_SZ_DW(page_fault_resume_in)] = { }; 372 int err; 373 374 MLX5_SET(page_fault_resume_in, in, opcode, MLX5_CMD_OP_PAGE_FAULT_RESUME); 375 MLX5_SET(page_fault_resume_in, in, page_fault_type, pfault->type); 376 MLX5_SET(page_fault_resume_in, in, token, pfault->token); 377 MLX5_SET(page_fault_resume_in, in, wq_number, wq_num); 378 MLX5_SET(page_fault_resume_in, in, error, !!error); 379 380 err = mlx5_cmd_exec(dev->mdev, in, sizeof(in), out, sizeof(out)); 381 if (err) 382 mlx5_ib_err(dev, "Failed to resolve the page fault on WQ 0x%x err %d\n", 383 wq_num, err); 384 } 385 386 static struct mlx5_ib_mr *implicit_mr_alloc(struct ib_pd *pd, 387 struct ib_umem *umem, 388 bool ksm, int access_flags) 389 { 390 struct mlx5_ib_dev *dev = to_mdev(pd->device); 391 struct mlx5_ib_mr *mr; 392 int err; 393 394 mr = mlx5_mr_cache_alloc(dev, ksm ? MLX5_IMR_KSM_CACHE_ENTRY : 395 MLX5_IMR_MTT_CACHE_ENTRY); 396 397 if (IS_ERR(mr)) 398 return mr; 399 400 mr->ibmr.pd = pd; 401 402 mr->dev = dev; 403 mr->access_flags = access_flags; 404 mr->mmkey.iova = 0; 405 mr->umem = umem; 406 407 if (ksm) { 408 err = mlx5_ib_update_xlt(mr, 0, 409 mlx5_imr_ksm_entries, 410 MLX5_KSM_PAGE_SHIFT, 411 MLX5_IB_UPD_XLT_INDIRECT | 412 MLX5_IB_UPD_XLT_ZAP | 413 MLX5_IB_UPD_XLT_ENABLE); 414 415 } else { 416 err = mlx5_ib_update_xlt(mr, 0, 417 MLX5_IMR_MTT_ENTRIES, 418 PAGE_SHIFT, 419 MLX5_IB_UPD_XLT_ZAP | 420 MLX5_IB_UPD_XLT_ENABLE | 421 MLX5_IB_UPD_XLT_ATOMIC); 422 } 423 424 if (err) 425 goto fail; 426 427 mr->ibmr.lkey = mr->mmkey.key; 428 mr->ibmr.rkey = mr->mmkey.key; 429 430 mr->live = 1; 431 432 mlx5_ib_dbg(dev, "key %x dev %p mr %p\n", 433 mr->mmkey.key, dev->mdev, mr); 434 435 return mr; 436 437 fail: 438 mlx5_ib_err(dev, "Failed to register MKEY %d\n", err); 439 mlx5_mr_cache_free(dev, mr); 440 441 return ERR_PTR(err); 442 } 443 444 static struct ib_umem_odp *implicit_mr_get_data(struct mlx5_ib_mr *mr, 445 u64 io_virt, size_t bcnt) 446 { 447 struct mlx5_ib_dev *dev = to_mdev(mr->ibmr.pd->device); 448 struct ib_umem_odp *odp, *result = NULL; 449 struct ib_umem_odp *odp_mr = to_ib_umem_odp(mr->umem); 450 u64 addr = io_virt & MLX5_IMR_MTT_MASK; 451 int nentries = 0, start_idx = 0, ret; 452 struct mlx5_ib_mr *mtt; 453 454 mutex_lock(&odp_mr->umem_mutex); 455 odp = odp_lookup(addr, 1, mr); 456 457 mlx5_ib_dbg(dev, "io_virt:%llx bcnt:%zx addr:%llx odp:%p\n", 458 io_virt, bcnt, addr, odp); 459 460 next_mr: 461 if (likely(odp)) { 462 if (nentries) 463 nentries++; 464 } else { 465 odp = ib_alloc_odp_umem(odp_mr, addr, 466 MLX5_IMR_MTT_SIZE); 467 if (IS_ERR(odp)) { 468 mutex_unlock(&odp_mr->umem_mutex); 469 return ERR_CAST(odp); 470 } 471 472 mtt = implicit_mr_alloc(mr->ibmr.pd, &odp->umem, 0, 473 mr->access_flags); 474 if (IS_ERR(mtt)) { 475 mutex_unlock(&odp_mr->umem_mutex); 476 ib_umem_release(&odp->umem); 477 return ERR_CAST(mtt); 478 } 479 480 odp->private = mtt; 481 mtt->umem = &odp->umem; 482 mtt->mmkey.iova = addr; 483 mtt->parent = mr; 484 INIT_WORK(&odp->work, mr_leaf_free_action); 485 486 if (!nentries) 487 start_idx = addr >> MLX5_IMR_MTT_SHIFT; 488 nentries++; 489 } 490 491 /* Return first odp if region not covered by single one */ 492 if (likely(!result)) 493 result = odp; 494 495 addr += MLX5_IMR_MTT_SIZE; 496 if (unlikely(addr < io_virt + bcnt)) { 497 odp = odp_next(odp); 498 if (odp && odp->umem.address != addr) 499 odp = NULL; 500 goto next_mr; 501 } 502 503 if (unlikely(nentries)) { 504 ret = mlx5_ib_update_xlt(mr, start_idx, nentries, 0, 505 MLX5_IB_UPD_XLT_INDIRECT | 506 MLX5_IB_UPD_XLT_ATOMIC); 507 if (ret) { 508 mlx5_ib_err(dev, "Failed to update PAS\n"); 509 result = ERR_PTR(ret); 510 } 511 } 512 513 mutex_unlock(&odp_mr->umem_mutex); 514 return result; 515 } 516 517 struct mlx5_ib_mr *mlx5_ib_alloc_implicit_mr(struct mlx5_ib_pd *pd, 518 struct ib_udata *udata, 519 int access_flags) 520 { 521 struct mlx5_ib_mr *imr; 522 struct ib_umem *umem; 523 524 umem = ib_umem_get(udata, 0, 0, access_flags, 0); 525 if (IS_ERR(umem)) 526 return ERR_CAST(umem); 527 528 imr = implicit_mr_alloc(&pd->ibpd, umem, 1, access_flags); 529 if (IS_ERR(imr)) { 530 ib_umem_release(umem); 531 return ERR_CAST(imr); 532 } 533 534 imr->umem = umem; 535 init_waitqueue_head(&imr->q_leaf_free); 536 atomic_set(&imr->num_leaf_free, 0); 537 atomic_set(&imr->num_pending_prefetch, 0); 538 539 return imr; 540 } 541 542 static int mr_leaf_free(struct ib_umem_odp *umem_odp, u64 start, u64 end, 543 void *cookie) 544 { 545 struct mlx5_ib_mr *mr = umem_odp->private, *imr = cookie; 546 547 if (mr->parent != imr) 548 return 0; 549 550 ib_umem_odp_unmap_dma_pages(umem_odp, ib_umem_start(umem_odp), 551 ib_umem_end(umem_odp)); 552 553 if (umem_odp->dying) 554 return 0; 555 556 WRITE_ONCE(umem_odp->dying, 1); 557 atomic_inc(&imr->num_leaf_free); 558 schedule_work(&umem_odp->work); 559 560 return 0; 561 } 562 563 void mlx5_ib_free_implicit_mr(struct mlx5_ib_mr *imr) 564 { 565 struct ib_ucontext_per_mm *per_mm = mr_to_per_mm(imr); 566 567 down_read(&per_mm->umem_rwsem); 568 rbt_ib_umem_for_each_in_range(&per_mm->umem_tree, 0, ULLONG_MAX, 569 mr_leaf_free, true, imr); 570 up_read(&per_mm->umem_rwsem); 571 572 wait_event(imr->q_leaf_free, !atomic_read(&imr->num_leaf_free)); 573 } 574 575 #define MLX5_PF_FLAGS_PREFETCH BIT(0) 576 #define MLX5_PF_FLAGS_DOWNGRADE BIT(1) 577 static int pagefault_mr(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr, 578 u64 io_virt, size_t bcnt, u32 *bytes_mapped, 579 u32 flags) 580 { 581 int npages = 0, current_seq, page_shift, ret, np; 582 bool implicit = false; 583 struct ib_umem_odp *odp_mr = to_ib_umem_odp(mr->umem); 584 bool downgrade = flags & MLX5_PF_FLAGS_DOWNGRADE; 585 bool prefetch = flags & MLX5_PF_FLAGS_PREFETCH; 586 u64 access_mask; 587 u64 start_idx, page_mask; 588 struct ib_umem_odp *odp; 589 size_t size; 590 591 if (!odp_mr->page_list) { 592 odp = implicit_mr_get_data(mr, io_virt, bcnt); 593 594 if (IS_ERR(odp)) 595 return PTR_ERR(odp); 596 mr = odp->private; 597 implicit = true; 598 } else { 599 odp = odp_mr; 600 } 601 602 next_mr: 603 size = min_t(size_t, bcnt, ib_umem_end(odp) - io_virt); 604 605 page_shift = odp->page_shift; 606 page_mask = ~(BIT(page_shift) - 1); 607 start_idx = (io_virt - (mr->mmkey.iova & page_mask)) >> page_shift; 608 access_mask = ODP_READ_ALLOWED_BIT; 609 610 if (prefetch && !downgrade && !mr->umem->writable) { 611 /* prefetch with write-access must 612 * be supported by the MR 613 */ 614 ret = -EINVAL; 615 goto out; 616 } 617 618 if (mr->umem->writable && !downgrade) 619 access_mask |= ODP_WRITE_ALLOWED_BIT; 620 621 current_seq = READ_ONCE(odp->notifiers_seq); 622 /* 623 * Ensure the sequence number is valid for some time before we call 624 * gup. 625 */ 626 smp_rmb(); 627 628 ret = ib_umem_odp_map_dma_pages(to_ib_umem_odp(mr->umem), io_virt, size, 629 access_mask, current_seq); 630 631 if (ret < 0) 632 goto out; 633 634 np = ret; 635 636 mutex_lock(&odp->umem_mutex); 637 if (!ib_umem_mmu_notifier_retry(to_ib_umem_odp(mr->umem), 638 current_seq)) { 639 /* 640 * No need to check whether the MTTs really belong to 641 * this MR, since ib_umem_odp_map_dma_pages already 642 * checks this. 643 */ 644 ret = mlx5_ib_update_xlt(mr, start_idx, np, 645 page_shift, MLX5_IB_UPD_XLT_ATOMIC); 646 } else { 647 ret = -EAGAIN; 648 } 649 mutex_unlock(&odp->umem_mutex); 650 651 if (ret < 0) { 652 if (ret != -EAGAIN) 653 mlx5_ib_err(dev, "Failed to update mkey page tables\n"); 654 goto out; 655 } 656 657 if (bytes_mapped) { 658 u32 new_mappings = (np << page_shift) - 659 (io_virt - round_down(io_virt, 1 << page_shift)); 660 *bytes_mapped += min_t(u32, new_mappings, size); 661 } 662 663 npages += np << (page_shift - PAGE_SHIFT); 664 bcnt -= size; 665 666 if (unlikely(bcnt)) { 667 struct ib_umem_odp *next; 668 669 io_virt += size; 670 next = odp_next(odp); 671 if (unlikely(!next || next->umem.address != io_virt)) { 672 mlx5_ib_dbg(dev, "next implicit leaf removed at 0x%llx. got %p\n", 673 io_virt, next); 674 return -EAGAIN; 675 } 676 odp = next; 677 mr = odp->private; 678 goto next_mr; 679 } 680 681 return npages; 682 683 out: 684 if (ret == -EAGAIN) { 685 if (implicit || !odp->dying) { 686 unsigned long timeout = 687 msecs_to_jiffies(MMU_NOTIFIER_TIMEOUT); 688 689 if (!wait_for_completion_timeout( 690 &odp->notifier_completion, 691 timeout)) { 692 mlx5_ib_warn(dev, "timeout waiting for mmu notifier. seq %d against %d. notifiers_count=%d\n", 693 current_seq, odp->notifiers_seq, odp->notifiers_count); 694 } 695 } else { 696 /* The MR is being killed, kill the QP as well. */ 697 ret = -EFAULT; 698 } 699 } 700 701 return ret; 702 } 703 704 struct pf_frame { 705 struct pf_frame *next; 706 u32 key; 707 u64 io_virt; 708 size_t bcnt; 709 int depth; 710 }; 711 712 static bool mkey_is_eq(struct mlx5_core_mkey *mmkey, u32 key) 713 { 714 if (!mmkey) 715 return false; 716 if (mmkey->type == MLX5_MKEY_MW) 717 return mlx5_base_mkey(mmkey->key) == mlx5_base_mkey(key); 718 return mmkey->key == key; 719 } 720 721 static int get_indirect_num_descs(struct mlx5_core_mkey *mmkey) 722 { 723 struct mlx5_ib_mw *mw; 724 struct mlx5_ib_devx_mr *devx_mr; 725 726 if (mmkey->type == MLX5_MKEY_MW) { 727 mw = container_of(mmkey, struct mlx5_ib_mw, mmkey); 728 return mw->ndescs; 729 } 730 731 devx_mr = container_of(mmkey, struct mlx5_ib_devx_mr, 732 mmkey); 733 return devx_mr->ndescs; 734 } 735 736 /* 737 * Handle a single data segment in a page-fault WQE or RDMA region. 738 * 739 * Returns number of OS pages retrieved on success. The caller may continue to 740 * the next data segment. 741 * Can return the following error codes: 742 * -EAGAIN to designate a temporary error. The caller will abort handling the 743 * page fault and resolve it. 744 * -EFAULT when there's an error mapping the requested pages. The caller will 745 * abort the page fault handling. 746 */ 747 static int pagefault_single_data_segment(struct mlx5_ib_dev *dev, 748 struct ib_pd *pd, u32 key, 749 u64 io_virt, size_t bcnt, 750 u32 *bytes_committed, 751 u32 *bytes_mapped, u32 flags) 752 { 753 int npages = 0, srcu_key, ret, i, outlen, cur_outlen = 0, depth = 0; 754 bool prefetch = flags & MLX5_PF_FLAGS_PREFETCH; 755 struct pf_frame *head = NULL, *frame; 756 struct mlx5_core_mkey *mmkey; 757 struct mlx5_ib_mr *mr; 758 struct mlx5_klm *pklm; 759 u32 *out = NULL; 760 size_t offset; 761 int ndescs; 762 763 srcu_key = srcu_read_lock(&dev->mr_srcu); 764 765 io_virt += *bytes_committed; 766 bcnt -= *bytes_committed; 767 768 next_mr: 769 mmkey = xa_load(&dev->mdev->priv.mkey_table, mlx5_base_mkey(key)); 770 if (!mkey_is_eq(mmkey, key)) { 771 mlx5_ib_dbg(dev, "failed to find mkey %x\n", key); 772 ret = -EFAULT; 773 goto srcu_unlock; 774 } 775 776 if (prefetch && mmkey->type != MLX5_MKEY_MR) { 777 mlx5_ib_dbg(dev, "prefetch is allowed only for MR\n"); 778 ret = -EINVAL; 779 goto srcu_unlock; 780 } 781 782 switch (mmkey->type) { 783 case MLX5_MKEY_MR: 784 mr = container_of(mmkey, struct mlx5_ib_mr, mmkey); 785 if (!mr->live || !mr->ibmr.pd) { 786 mlx5_ib_dbg(dev, "got dead MR\n"); 787 ret = -EFAULT; 788 goto srcu_unlock; 789 } 790 791 if (prefetch) { 792 if (!is_odp_mr(mr) || 793 mr->ibmr.pd != pd) { 794 mlx5_ib_dbg(dev, "Invalid prefetch request: %s\n", 795 is_odp_mr(mr) ? "MR is not ODP" : 796 "PD is not of the MR"); 797 ret = -EINVAL; 798 goto srcu_unlock; 799 } 800 } 801 802 if (!is_odp_mr(mr)) { 803 mlx5_ib_dbg(dev, "skipping non ODP MR (lkey=0x%06x) in page fault handler.\n", 804 key); 805 if (bytes_mapped) 806 *bytes_mapped += bcnt; 807 ret = 0; 808 goto srcu_unlock; 809 } 810 811 ret = pagefault_mr(dev, mr, io_virt, bcnt, bytes_mapped, flags); 812 if (ret < 0) 813 goto srcu_unlock; 814 815 npages += ret; 816 ret = 0; 817 break; 818 819 case MLX5_MKEY_MW: 820 case MLX5_MKEY_INDIRECT_DEVX: 821 ndescs = get_indirect_num_descs(mmkey); 822 823 if (depth >= MLX5_CAP_GEN(dev->mdev, max_indirection)) { 824 mlx5_ib_dbg(dev, "indirection level exceeded\n"); 825 ret = -EFAULT; 826 goto srcu_unlock; 827 } 828 829 outlen = MLX5_ST_SZ_BYTES(query_mkey_out) + 830 sizeof(*pklm) * (ndescs - 2); 831 832 if (outlen > cur_outlen) { 833 kfree(out); 834 out = kzalloc(outlen, GFP_KERNEL); 835 if (!out) { 836 ret = -ENOMEM; 837 goto srcu_unlock; 838 } 839 cur_outlen = outlen; 840 } 841 842 pklm = (struct mlx5_klm *)MLX5_ADDR_OF(query_mkey_out, out, 843 bsf0_klm0_pas_mtt0_1); 844 845 ret = mlx5_core_query_mkey(dev->mdev, mmkey, out, outlen); 846 if (ret) 847 goto srcu_unlock; 848 849 offset = io_virt - MLX5_GET64(query_mkey_out, out, 850 memory_key_mkey_entry.start_addr); 851 852 for (i = 0; bcnt && i < ndescs; i++, pklm++) { 853 if (offset >= be32_to_cpu(pklm->bcount)) { 854 offset -= be32_to_cpu(pklm->bcount); 855 continue; 856 } 857 858 frame = kzalloc(sizeof(*frame), GFP_KERNEL); 859 if (!frame) { 860 ret = -ENOMEM; 861 goto srcu_unlock; 862 } 863 864 frame->key = be32_to_cpu(pklm->key); 865 frame->io_virt = be64_to_cpu(pklm->va) + offset; 866 frame->bcnt = min_t(size_t, bcnt, 867 be32_to_cpu(pklm->bcount) - offset); 868 frame->depth = depth + 1; 869 frame->next = head; 870 head = frame; 871 872 bcnt -= frame->bcnt; 873 offset = 0; 874 } 875 break; 876 877 default: 878 mlx5_ib_dbg(dev, "wrong mkey type %d\n", mmkey->type); 879 ret = -EFAULT; 880 goto srcu_unlock; 881 } 882 883 if (head) { 884 frame = head; 885 head = frame->next; 886 887 key = frame->key; 888 io_virt = frame->io_virt; 889 bcnt = frame->bcnt; 890 depth = frame->depth; 891 kfree(frame); 892 893 goto next_mr; 894 } 895 896 srcu_unlock: 897 while (head) { 898 frame = head; 899 head = frame->next; 900 kfree(frame); 901 } 902 kfree(out); 903 904 srcu_read_unlock(&dev->mr_srcu, srcu_key); 905 *bytes_committed = 0; 906 return ret ? ret : npages; 907 } 908 909 /** 910 * Parse a series of data segments for page fault handling. 911 * 912 * @pfault contains page fault information. 913 * @wqe points at the first data segment in the WQE. 914 * @wqe_end points after the end of the WQE. 915 * @bytes_mapped receives the number of bytes that the function was able to 916 * map. This allows the caller to decide intelligently whether 917 * enough memory was mapped to resolve the page fault 918 * successfully (e.g. enough for the next MTU, or the entire 919 * WQE). 920 * @total_wqe_bytes receives the total data size of this WQE in bytes (minus 921 * the committed bytes). 922 * 923 * Returns the number of pages loaded if positive, zero for an empty WQE, or a 924 * negative error code. 925 */ 926 static int pagefault_data_segments(struct mlx5_ib_dev *dev, 927 struct mlx5_pagefault *pfault, 928 void *wqe, 929 void *wqe_end, u32 *bytes_mapped, 930 u32 *total_wqe_bytes, bool receive_queue) 931 { 932 int ret = 0, npages = 0; 933 u64 io_virt; 934 u32 key; 935 u32 byte_count; 936 size_t bcnt; 937 int inline_segment; 938 939 if (bytes_mapped) 940 *bytes_mapped = 0; 941 if (total_wqe_bytes) 942 *total_wqe_bytes = 0; 943 944 while (wqe < wqe_end) { 945 struct mlx5_wqe_data_seg *dseg = wqe; 946 947 io_virt = be64_to_cpu(dseg->addr); 948 key = be32_to_cpu(dseg->lkey); 949 byte_count = be32_to_cpu(dseg->byte_count); 950 inline_segment = !!(byte_count & MLX5_INLINE_SEG); 951 bcnt = byte_count & ~MLX5_INLINE_SEG; 952 953 if (inline_segment) { 954 bcnt = bcnt & MLX5_WQE_INLINE_SEG_BYTE_COUNT_MASK; 955 wqe += ALIGN(sizeof(struct mlx5_wqe_inline_seg) + bcnt, 956 16); 957 } else { 958 wqe += sizeof(*dseg); 959 } 960 961 /* receive WQE end of sg list. */ 962 if (receive_queue && bcnt == 0 && key == MLX5_INVALID_LKEY && 963 io_virt == 0) 964 break; 965 966 if (!inline_segment && total_wqe_bytes) { 967 *total_wqe_bytes += bcnt - min_t(size_t, bcnt, 968 pfault->bytes_committed); 969 } 970 971 /* A zero length data segment designates a length of 2GB. */ 972 if (bcnt == 0) 973 bcnt = 1U << 31; 974 975 if (inline_segment || bcnt <= pfault->bytes_committed) { 976 pfault->bytes_committed -= 977 min_t(size_t, bcnt, 978 pfault->bytes_committed); 979 continue; 980 } 981 982 ret = pagefault_single_data_segment(dev, NULL, key, 983 io_virt, bcnt, 984 &pfault->bytes_committed, 985 bytes_mapped, 0); 986 if (ret < 0) 987 break; 988 npages += ret; 989 } 990 991 return ret < 0 ? ret : npages; 992 } 993 994 static const u32 mlx5_ib_odp_opcode_cap[] = { 995 [MLX5_OPCODE_SEND] = IB_ODP_SUPPORT_SEND, 996 [MLX5_OPCODE_SEND_IMM] = IB_ODP_SUPPORT_SEND, 997 [MLX5_OPCODE_SEND_INVAL] = IB_ODP_SUPPORT_SEND, 998 [MLX5_OPCODE_RDMA_WRITE] = IB_ODP_SUPPORT_WRITE, 999 [MLX5_OPCODE_RDMA_WRITE_IMM] = IB_ODP_SUPPORT_WRITE, 1000 [MLX5_OPCODE_RDMA_READ] = IB_ODP_SUPPORT_READ, 1001 [MLX5_OPCODE_ATOMIC_CS] = IB_ODP_SUPPORT_ATOMIC, 1002 [MLX5_OPCODE_ATOMIC_FA] = IB_ODP_SUPPORT_ATOMIC, 1003 }; 1004 1005 /* 1006 * Parse initiator WQE. Advances the wqe pointer to point at the 1007 * scatter-gather list, and set wqe_end to the end of the WQE. 1008 */ 1009 static int mlx5_ib_mr_initiator_pfault_handler( 1010 struct mlx5_ib_dev *dev, struct mlx5_pagefault *pfault, 1011 struct mlx5_ib_qp *qp, void **wqe, void **wqe_end, int wqe_length) 1012 { 1013 struct mlx5_wqe_ctrl_seg *ctrl = *wqe; 1014 u16 wqe_index = pfault->wqe.wqe_index; 1015 u32 transport_caps; 1016 struct mlx5_base_av *av; 1017 unsigned ds, opcode; 1018 #if defined(DEBUG) 1019 u32 ctrl_wqe_index, ctrl_qpn; 1020 #endif 1021 u32 qpn = qp->trans_qp.base.mqp.qpn; 1022 1023 ds = be32_to_cpu(ctrl->qpn_ds) & MLX5_WQE_CTRL_DS_MASK; 1024 if (ds * MLX5_WQE_DS_UNITS > wqe_length) { 1025 mlx5_ib_err(dev, "Unable to read the complete WQE. ds = 0x%x, ret = 0x%x\n", 1026 ds, wqe_length); 1027 return -EFAULT; 1028 } 1029 1030 if (ds == 0) { 1031 mlx5_ib_err(dev, "Got WQE with zero DS. wqe_index=%x, qpn=%x\n", 1032 wqe_index, qpn); 1033 return -EFAULT; 1034 } 1035 1036 #if defined(DEBUG) 1037 ctrl_wqe_index = (be32_to_cpu(ctrl->opmod_idx_opcode) & 1038 MLX5_WQE_CTRL_WQE_INDEX_MASK) >> 1039 MLX5_WQE_CTRL_WQE_INDEX_SHIFT; 1040 if (wqe_index != ctrl_wqe_index) { 1041 mlx5_ib_err(dev, "Got WQE with invalid wqe_index. wqe_index=0x%x, qpn=0x%x ctrl->wqe_index=0x%x\n", 1042 wqe_index, qpn, 1043 ctrl_wqe_index); 1044 return -EFAULT; 1045 } 1046 1047 ctrl_qpn = (be32_to_cpu(ctrl->qpn_ds) & MLX5_WQE_CTRL_QPN_MASK) >> 1048 MLX5_WQE_CTRL_QPN_SHIFT; 1049 if (qpn != ctrl_qpn) { 1050 mlx5_ib_err(dev, "Got WQE with incorrect QP number. wqe_index=0x%x, qpn=0x%x ctrl->qpn=0x%x\n", 1051 wqe_index, qpn, 1052 ctrl_qpn); 1053 return -EFAULT; 1054 } 1055 #endif /* DEBUG */ 1056 1057 *wqe_end = *wqe + ds * MLX5_WQE_DS_UNITS; 1058 *wqe += sizeof(*ctrl); 1059 1060 opcode = be32_to_cpu(ctrl->opmod_idx_opcode) & 1061 MLX5_WQE_CTRL_OPCODE_MASK; 1062 1063 switch (qp->ibqp.qp_type) { 1064 case IB_QPT_XRC_INI: 1065 *wqe += sizeof(struct mlx5_wqe_xrc_seg); 1066 transport_caps = dev->odp_caps.per_transport_caps.xrc_odp_caps; 1067 break; 1068 case IB_QPT_RC: 1069 transport_caps = dev->odp_caps.per_transport_caps.rc_odp_caps; 1070 break; 1071 case IB_QPT_UD: 1072 transport_caps = dev->odp_caps.per_transport_caps.ud_odp_caps; 1073 break; 1074 default: 1075 mlx5_ib_err(dev, "ODP fault on QP of an unsupported transport 0x%x\n", 1076 qp->ibqp.qp_type); 1077 return -EFAULT; 1078 } 1079 1080 if (unlikely(opcode >= ARRAY_SIZE(mlx5_ib_odp_opcode_cap) || 1081 !(transport_caps & mlx5_ib_odp_opcode_cap[opcode]))) { 1082 mlx5_ib_err(dev, "ODP fault on QP of an unsupported opcode 0x%x\n", 1083 opcode); 1084 return -EFAULT; 1085 } 1086 1087 if (qp->ibqp.qp_type == IB_QPT_UD) { 1088 av = *wqe; 1089 if (av->dqp_dct & cpu_to_be32(MLX5_EXTENDED_UD_AV)) 1090 *wqe += sizeof(struct mlx5_av); 1091 else 1092 *wqe += sizeof(struct mlx5_base_av); 1093 } 1094 1095 switch (opcode) { 1096 case MLX5_OPCODE_RDMA_WRITE: 1097 case MLX5_OPCODE_RDMA_WRITE_IMM: 1098 case MLX5_OPCODE_RDMA_READ: 1099 *wqe += sizeof(struct mlx5_wqe_raddr_seg); 1100 break; 1101 case MLX5_OPCODE_ATOMIC_CS: 1102 case MLX5_OPCODE_ATOMIC_FA: 1103 *wqe += sizeof(struct mlx5_wqe_raddr_seg); 1104 *wqe += sizeof(struct mlx5_wqe_atomic_seg); 1105 break; 1106 } 1107 1108 return 0; 1109 } 1110 1111 /* 1112 * Parse responder WQE and set wqe_end to the end of the WQE. 1113 */ 1114 static int mlx5_ib_mr_responder_pfault_handler_srq(struct mlx5_ib_dev *dev, 1115 struct mlx5_ib_srq *srq, 1116 void **wqe, void **wqe_end, 1117 int wqe_length) 1118 { 1119 int wqe_size = 1 << srq->msrq.wqe_shift; 1120 1121 if (wqe_size > wqe_length) { 1122 mlx5_ib_err(dev, "Couldn't read all of the receive WQE's content\n"); 1123 return -EFAULT; 1124 } 1125 1126 *wqe_end = *wqe + wqe_size; 1127 *wqe += sizeof(struct mlx5_wqe_srq_next_seg); 1128 1129 return 0; 1130 } 1131 1132 static int mlx5_ib_mr_responder_pfault_handler_rq(struct mlx5_ib_dev *dev, 1133 struct mlx5_ib_qp *qp, 1134 void *wqe, void **wqe_end, 1135 int wqe_length) 1136 { 1137 struct mlx5_ib_wq *wq = &qp->rq; 1138 int wqe_size = 1 << wq->wqe_shift; 1139 1140 if (qp->wq_sig) { 1141 mlx5_ib_err(dev, "ODP fault with WQE signatures is not supported\n"); 1142 return -EFAULT; 1143 } 1144 1145 if (wqe_size > wqe_length) { 1146 mlx5_ib_err(dev, "Couldn't read all of the receive WQE's content\n"); 1147 return -EFAULT; 1148 } 1149 1150 switch (qp->ibqp.qp_type) { 1151 case IB_QPT_RC: 1152 if (!(dev->odp_caps.per_transport_caps.rc_odp_caps & 1153 IB_ODP_SUPPORT_RECV)) 1154 goto invalid_transport_or_opcode; 1155 break; 1156 default: 1157 invalid_transport_or_opcode: 1158 mlx5_ib_err(dev, "ODP fault on QP of an unsupported transport. transport: 0x%x\n", 1159 qp->ibqp.qp_type); 1160 return -EFAULT; 1161 } 1162 1163 *wqe_end = wqe + wqe_size; 1164 1165 return 0; 1166 } 1167 1168 static inline struct mlx5_core_rsc_common *odp_get_rsc(struct mlx5_ib_dev *dev, 1169 u32 wq_num, int pf_type) 1170 { 1171 struct mlx5_core_rsc_common *common = NULL; 1172 struct mlx5_core_srq *srq; 1173 1174 switch (pf_type) { 1175 case MLX5_WQE_PF_TYPE_RMP: 1176 srq = mlx5_cmd_get_srq(dev, wq_num); 1177 if (srq) 1178 common = &srq->common; 1179 break; 1180 case MLX5_WQE_PF_TYPE_REQ_SEND_OR_WRITE: 1181 case MLX5_WQE_PF_TYPE_RESP: 1182 case MLX5_WQE_PF_TYPE_REQ_READ_OR_ATOMIC: 1183 common = mlx5_core_res_hold(dev->mdev, wq_num, MLX5_RES_QP); 1184 break; 1185 default: 1186 break; 1187 } 1188 1189 return common; 1190 } 1191 1192 static inline struct mlx5_ib_qp *res_to_qp(struct mlx5_core_rsc_common *res) 1193 { 1194 struct mlx5_core_qp *mqp = (struct mlx5_core_qp *)res; 1195 1196 return to_mibqp(mqp); 1197 } 1198 1199 static inline struct mlx5_ib_srq *res_to_srq(struct mlx5_core_rsc_common *res) 1200 { 1201 struct mlx5_core_srq *msrq = 1202 container_of(res, struct mlx5_core_srq, common); 1203 1204 return to_mibsrq(msrq); 1205 } 1206 1207 static void mlx5_ib_mr_wqe_pfault_handler(struct mlx5_ib_dev *dev, 1208 struct mlx5_pagefault *pfault) 1209 { 1210 bool sq = pfault->type & MLX5_PFAULT_REQUESTOR; 1211 u16 wqe_index = pfault->wqe.wqe_index; 1212 void *wqe = NULL, *wqe_end = NULL; 1213 u32 bytes_mapped, total_wqe_bytes; 1214 struct mlx5_core_rsc_common *res; 1215 int resume_with_error = 1; 1216 struct mlx5_ib_qp *qp; 1217 size_t bytes_copied; 1218 int ret = 0; 1219 1220 res = odp_get_rsc(dev, pfault->wqe.wq_num, pfault->type); 1221 if (!res) { 1222 mlx5_ib_dbg(dev, "wqe page fault for missing resource %d\n", pfault->wqe.wq_num); 1223 return; 1224 } 1225 1226 if (res->res != MLX5_RES_QP && res->res != MLX5_RES_SRQ && 1227 res->res != MLX5_RES_XSRQ) { 1228 mlx5_ib_err(dev, "wqe page fault for unsupported type %d\n", 1229 pfault->type); 1230 goto resolve_page_fault; 1231 } 1232 1233 wqe = (void *)__get_free_page(GFP_KERNEL); 1234 if (!wqe) { 1235 mlx5_ib_err(dev, "Error allocating memory for IO page fault handling.\n"); 1236 goto resolve_page_fault; 1237 } 1238 1239 qp = (res->res == MLX5_RES_QP) ? res_to_qp(res) : NULL; 1240 if (qp && sq) { 1241 ret = mlx5_ib_read_user_wqe_sq(qp, wqe_index, wqe, PAGE_SIZE, 1242 &bytes_copied); 1243 if (ret) 1244 goto read_user; 1245 ret = mlx5_ib_mr_initiator_pfault_handler( 1246 dev, pfault, qp, &wqe, &wqe_end, bytes_copied); 1247 } else if (qp && !sq) { 1248 ret = mlx5_ib_read_user_wqe_rq(qp, wqe_index, wqe, PAGE_SIZE, 1249 &bytes_copied); 1250 if (ret) 1251 goto read_user; 1252 ret = mlx5_ib_mr_responder_pfault_handler_rq( 1253 dev, qp, wqe, &wqe_end, bytes_copied); 1254 } else if (!qp) { 1255 struct mlx5_ib_srq *srq = res_to_srq(res); 1256 1257 ret = mlx5_ib_read_user_wqe_srq(srq, wqe_index, wqe, PAGE_SIZE, 1258 &bytes_copied); 1259 if (ret) 1260 goto read_user; 1261 ret = mlx5_ib_mr_responder_pfault_handler_srq( 1262 dev, srq, &wqe, &wqe_end, bytes_copied); 1263 } 1264 1265 if (ret < 0 || wqe >= wqe_end) 1266 goto resolve_page_fault; 1267 1268 ret = pagefault_data_segments(dev, pfault, wqe, wqe_end, &bytes_mapped, 1269 &total_wqe_bytes, !sq); 1270 if (ret == -EAGAIN) 1271 goto out; 1272 1273 if (ret < 0 || total_wqe_bytes > bytes_mapped) 1274 goto resolve_page_fault; 1275 1276 out: 1277 ret = 0; 1278 resume_with_error = 0; 1279 1280 read_user: 1281 if (ret) 1282 mlx5_ib_err( 1283 dev, 1284 "Failed reading a WQE following page fault, error %d, wqe_index %x, qpn %x\n", 1285 ret, wqe_index, pfault->token); 1286 1287 resolve_page_fault: 1288 mlx5_ib_page_fault_resume(dev, pfault, resume_with_error); 1289 mlx5_ib_dbg(dev, "PAGE FAULT completed. QP 0x%x resume_with_error=%d, type: 0x%x\n", 1290 pfault->wqe.wq_num, resume_with_error, 1291 pfault->type); 1292 mlx5_core_res_put(res); 1293 free_page((unsigned long)wqe); 1294 } 1295 1296 static int pages_in_range(u64 address, u32 length) 1297 { 1298 return (ALIGN(address + length, PAGE_SIZE) - 1299 (address & PAGE_MASK)) >> PAGE_SHIFT; 1300 } 1301 1302 static void mlx5_ib_mr_rdma_pfault_handler(struct mlx5_ib_dev *dev, 1303 struct mlx5_pagefault *pfault) 1304 { 1305 u64 address; 1306 u32 length; 1307 u32 prefetch_len = pfault->bytes_committed; 1308 int prefetch_activated = 0; 1309 u32 rkey = pfault->rdma.r_key; 1310 int ret; 1311 1312 /* The RDMA responder handler handles the page fault in two parts. 1313 * First it brings the necessary pages for the current packet 1314 * (and uses the pfault context), and then (after resuming the QP) 1315 * prefetches more pages. The second operation cannot use the pfault 1316 * context and therefore uses the dummy_pfault context allocated on 1317 * the stack */ 1318 pfault->rdma.rdma_va += pfault->bytes_committed; 1319 pfault->rdma.rdma_op_len -= min(pfault->bytes_committed, 1320 pfault->rdma.rdma_op_len); 1321 pfault->bytes_committed = 0; 1322 1323 address = pfault->rdma.rdma_va; 1324 length = pfault->rdma.rdma_op_len; 1325 1326 /* For some operations, the hardware cannot tell the exact message 1327 * length, and in those cases it reports zero. Use prefetch 1328 * logic. */ 1329 if (length == 0) { 1330 prefetch_activated = 1; 1331 length = pfault->rdma.packet_size; 1332 prefetch_len = min(MAX_PREFETCH_LEN, prefetch_len); 1333 } 1334 1335 ret = pagefault_single_data_segment(dev, NULL, rkey, address, length, 1336 &pfault->bytes_committed, NULL, 1337 0); 1338 if (ret == -EAGAIN) { 1339 /* We're racing with an invalidation, don't prefetch */ 1340 prefetch_activated = 0; 1341 } else if (ret < 0 || pages_in_range(address, length) > ret) { 1342 mlx5_ib_page_fault_resume(dev, pfault, 1); 1343 if (ret != -ENOENT) 1344 mlx5_ib_dbg(dev, "PAGE FAULT error %d. QP 0x%x, type: 0x%x\n", 1345 ret, pfault->token, pfault->type); 1346 return; 1347 } 1348 1349 mlx5_ib_page_fault_resume(dev, pfault, 0); 1350 mlx5_ib_dbg(dev, "PAGE FAULT completed. QP 0x%x, type: 0x%x, prefetch_activated: %d\n", 1351 pfault->token, pfault->type, 1352 prefetch_activated); 1353 1354 /* At this point, there might be a new pagefault already arriving in 1355 * the eq, switch to the dummy pagefault for the rest of the 1356 * processing. We're still OK with the objects being alive as the 1357 * work-queue is being fenced. */ 1358 1359 if (prefetch_activated) { 1360 u32 bytes_committed = 0; 1361 1362 ret = pagefault_single_data_segment(dev, NULL, rkey, address, 1363 prefetch_len, 1364 &bytes_committed, NULL, 1365 0); 1366 if (ret < 0 && ret != -EAGAIN) { 1367 mlx5_ib_dbg(dev, "Prefetch failed. ret: %d, QP 0x%x, address: 0x%.16llx, length = 0x%.16x\n", 1368 ret, pfault->token, address, prefetch_len); 1369 } 1370 } 1371 } 1372 1373 static void mlx5_ib_pfault(struct mlx5_ib_dev *dev, struct mlx5_pagefault *pfault) 1374 { 1375 u8 event_subtype = pfault->event_subtype; 1376 1377 switch (event_subtype) { 1378 case MLX5_PFAULT_SUBTYPE_WQE: 1379 mlx5_ib_mr_wqe_pfault_handler(dev, pfault); 1380 break; 1381 case MLX5_PFAULT_SUBTYPE_RDMA: 1382 mlx5_ib_mr_rdma_pfault_handler(dev, pfault); 1383 break; 1384 default: 1385 mlx5_ib_err(dev, "Invalid page fault event subtype: 0x%x\n", 1386 event_subtype); 1387 mlx5_ib_page_fault_resume(dev, pfault, 1); 1388 } 1389 } 1390 1391 static void mlx5_ib_eqe_pf_action(struct work_struct *work) 1392 { 1393 struct mlx5_pagefault *pfault = container_of(work, 1394 struct mlx5_pagefault, 1395 work); 1396 struct mlx5_ib_pf_eq *eq = pfault->eq; 1397 1398 mlx5_ib_pfault(eq->dev, pfault); 1399 mempool_free(pfault, eq->pool); 1400 } 1401 1402 static void mlx5_ib_eq_pf_process(struct mlx5_ib_pf_eq *eq) 1403 { 1404 struct mlx5_eqe_page_fault *pf_eqe; 1405 struct mlx5_pagefault *pfault; 1406 struct mlx5_eqe *eqe; 1407 int cc = 0; 1408 1409 while ((eqe = mlx5_eq_get_eqe(eq->core, cc))) { 1410 pfault = mempool_alloc(eq->pool, GFP_ATOMIC); 1411 if (!pfault) { 1412 schedule_work(&eq->work); 1413 break; 1414 } 1415 1416 pf_eqe = &eqe->data.page_fault; 1417 pfault->event_subtype = eqe->sub_type; 1418 pfault->bytes_committed = be32_to_cpu(pf_eqe->bytes_committed); 1419 1420 mlx5_ib_dbg(eq->dev, 1421 "PAGE_FAULT: subtype: 0x%02x, bytes_committed: 0x%06x\n", 1422 eqe->sub_type, pfault->bytes_committed); 1423 1424 switch (eqe->sub_type) { 1425 case MLX5_PFAULT_SUBTYPE_RDMA: 1426 /* RDMA based event */ 1427 pfault->type = 1428 be32_to_cpu(pf_eqe->rdma.pftype_token) >> 24; 1429 pfault->token = 1430 be32_to_cpu(pf_eqe->rdma.pftype_token) & 1431 MLX5_24BIT_MASK; 1432 pfault->rdma.r_key = 1433 be32_to_cpu(pf_eqe->rdma.r_key); 1434 pfault->rdma.packet_size = 1435 be16_to_cpu(pf_eqe->rdma.packet_length); 1436 pfault->rdma.rdma_op_len = 1437 be32_to_cpu(pf_eqe->rdma.rdma_op_len); 1438 pfault->rdma.rdma_va = 1439 be64_to_cpu(pf_eqe->rdma.rdma_va); 1440 mlx5_ib_dbg(eq->dev, 1441 "PAGE_FAULT: type:0x%x, token: 0x%06x, r_key: 0x%08x\n", 1442 pfault->type, pfault->token, 1443 pfault->rdma.r_key); 1444 mlx5_ib_dbg(eq->dev, 1445 "PAGE_FAULT: rdma_op_len: 0x%08x, rdma_va: 0x%016llx\n", 1446 pfault->rdma.rdma_op_len, 1447 pfault->rdma.rdma_va); 1448 break; 1449 1450 case MLX5_PFAULT_SUBTYPE_WQE: 1451 /* WQE based event */ 1452 pfault->type = 1453 (be32_to_cpu(pf_eqe->wqe.pftype_wq) >> 24) & 0x7; 1454 pfault->token = 1455 be32_to_cpu(pf_eqe->wqe.token); 1456 pfault->wqe.wq_num = 1457 be32_to_cpu(pf_eqe->wqe.pftype_wq) & 1458 MLX5_24BIT_MASK; 1459 pfault->wqe.wqe_index = 1460 be16_to_cpu(pf_eqe->wqe.wqe_index); 1461 pfault->wqe.packet_size = 1462 be16_to_cpu(pf_eqe->wqe.packet_length); 1463 mlx5_ib_dbg(eq->dev, 1464 "PAGE_FAULT: type:0x%x, token: 0x%06x, wq_num: 0x%06x, wqe_index: 0x%04x\n", 1465 pfault->type, pfault->token, 1466 pfault->wqe.wq_num, 1467 pfault->wqe.wqe_index); 1468 break; 1469 1470 default: 1471 mlx5_ib_warn(eq->dev, 1472 "Unsupported page fault event sub-type: 0x%02hhx\n", 1473 eqe->sub_type); 1474 /* Unsupported page faults should still be 1475 * resolved by the page fault handler 1476 */ 1477 } 1478 1479 pfault->eq = eq; 1480 INIT_WORK(&pfault->work, mlx5_ib_eqe_pf_action); 1481 queue_work(eq->wq, &pfault->work); 1482 1483 cc = mlx5_eq_update_cc(eq->core, ++cc); 1484 } 1485 1486 mlx5_eq_update_ci(eq->core, cc, 1); 1487 } 1488 1489 static int mlx5_ib_eq_pf_int(struct notifier_block *nb, unsigned long type, 1490 void *data) 1491 { 1492 struct mlx5_ib_pf_eq *eq = 1493 container_of(nb, struct mlx5_ib_pf_eq, irq_nb); 1494 unsigned long flags; 1495 1496 if (spin_trylock_irqsave(&eq->lock, flags)) { 1497 mlx5_ib_eq_pf_process(eq); 1498 spin_unlock_irqrestore(&eq->lock, flags); 1499 } else { 1500 schedule_work(&eq->work); 1501 } 1502 1503 return IRQ_HANDLED; 1504 } 1505 1506 /* mempool_refill() was proposed but unfortunately wasn't accepted 1507 * http://lkml.iu.edu/hypermail/linux/kernel/1512.1/05073.html 1508 * Cheap workaround. 1509 */ 1510 static void mempool_refill(mempool_t *pool) 1511 { 1512 while (pool->curr_nr < pool->min_nr) 1513 mempool_free(mempool_alloc(pool, GFP_KERNEL), pool); 1514 } 1515 1516 static void mlx5_ib_eq_pf_action(struct work_struct *work) 1517 { 1518 struct mlx5_ib_pf_eq *eq = 1519 container_of(work, struct mlx5_ib_pf_eq, work); 1520 1521 mempool_refill(eq->pool); 1522 1523 spin_lock_irq(&eq->lock); 1524 mlx5_ib_eq_pf_process(eq); 1525 spin_unlock_irq(&eq->lock); 1526 } 1527 1528 enum { 1529 MLX5_IB_NUM_PF_EQE = 0x1000, 1530 MLX5_IB_NUM_PF_DRAIN = 64, 1531 }; 1532 1533 static int 1534 mlx5_ib_create_pf_eq(struct mlx5_ib_dev *dev, struct mlx5_ib_pf_eq *eq) 1535 { 1536 struct mlx5_eq_param param = {}; 1537 int err; 1538 1539 INIT_WORK(&eq->work, mlx5_ib_eq_pf_action); 1540 spin_lock_init(&eq->lock); 1541 eq->dev = dev; 1542 1543 eq->pool = mempool_create_kmalloc_pool(MLX5_IB_NUM_PF_DRAIN, 1544 sizeof(struct mlx5_pagefault)); 1545 if (!eq->pool) 1546 return -ENOMEM; 1547 1548 eq->wq = alloc_workqueue("mlx5_ib_page_fault", 1549 WQ_HIGHPRI | WQ_UNBOUND | WQ_MEM_RECLAIM, 1550 MLX5_NUM_CMD_EQE); 1551 if (!eq->wq) { 1552 err = -ENOMEM; 1553 goto err_mempool; 1554 } 1555 1556 eq->irq_nb.notifier_call = mlx5_ib_eq_pf_int; 1557 param = (struct mlx5_eq_param) { 1558 .irq_index = 0, 1559 .nent = MLX5_IB_NUM_PF_EQE, 1560 }; 1561 param.mask[0] = 1ull << MLX5_EVENT_TYPE_PAGE_FAULT; 1562 eq->core = mlx5_eq_create_generic(dev->mdev, ¶m); 1563 if (IS_ERR(eq->core)) { 1564 err = PTR_ERR(eq->core); 1565 goto err_wq; 1566 } 1567 err = mlx5_eq_enable(dev->mdev, eq->core, &eq->irq_nb); 1568 if (err) { 1569 mlx5_ib_err(dev, "failed to enable odp EQ %d\n", err); 1570 goto err_eq; 1571 } 1572 1573 return 0; 1574 err_eq: 1575 mlx5_eq_destroy_generic(dev->mdev, eq->core); 1576 err_wq: 1577 destroy_workqueue(eq->wq); 1578 err_mempool: 1579 mempool_destroy(eq->pool); 1580 return err; 1581 } 1582 1583 static int 1584 mlx5_ib_destroy_pf_eq(struct mlx5_ib_dev *dev, struct mlx5_ib_pf_eq *eq) 1585 { 1586 int err; 1587 1588 mlx5_eq_disable(dev->mdev, eq->core, &eq->irq_nb); 1589 err = mlx5_eq_destroy_generic(dev->mdev, eq->core); 1590 cancel_work_sync(&eq->work); 1591 destroy_workqueue(eq->wq); 1592 mempool_destroy(eq->pool); 1593 1594 return err; 1595 } 1596 1597 void mlx5_odp_init_mr_cache_entry(struct mlx5_cache_ent *ent) 1598 { 1599 if (!(ent->dev->odp_caps.general_caps & IB_ODP_SUPPORT_IMPLICIT)) 1600 return; 1601 1602 switch (ent->order - 2) { 1603 case MLX5_IMR_MTT_CACHE_ENTRY: 1604 ent->page = PAGE_SHIFT; 1605 ent->xlt = MLX5_IMR_MTT_ENTRIES * 1606 sizeof(struct mlx5_mtt) / 1607 MLX5_IB_UMR_OCTOWORD; 1608 ent->access_mode = MLX5_MKC_ACCESS_MODE_MTT; 1609 ent->limit = 0; 1610 break; 1611 1612 case MLX5_IMR_KSM_CACHE_ENTRY: 1613 ent->page = MLX5_KSM_PAGE_SHIFT; 1614 ent->xlt = mlx5_imr_ksm_entries * 1615 sizeof(struct mlx5_klm) / 1616 MLX5_IB_UMR_OCTOWORD; 1617 ent->access_mode = MLX5_MKC_ACCESS_MODE_KSM; 1618 ent->limit = 0; 1619 break; 1620 } 1621 } 1622 1623 static const struct ib_device_ops mlx5_ib_dev_odp_ops = { 1624 .advise_mr = mlx5_ib_advise_mr, 1625 }; 1626 1627 int mlx5_ib_odp_init_one(struct mlx5_ib_dev *dev) 1628 { 1629 int ret = 0; 1630 1631 if (dev->odp_caps.general_caps & IB_ODP_SUPPORT) 1632 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_odp_ops); 1633 1634 if (dev->odp_caps.general_caps & IB_ODP_SUPPORT_IMPLICIT) { 1635 ret = mlx5_cmd_null_mkey(dev->mdev, &dev->null_mkey); 1636 if (ret) { 1637 mlx5_ib_err(dev, "Error getting null_mkey %d\n", ret); 1638 return ret; 1639 } 1640 } 1641 1642 if (!MLX5_CAP_GEN(dev->mdev, pg)) 1643 return ret; 1644 1645 ret = mlx5_ib_create_pf_eq(dev, &dev->odp_pf_eq); 1646 1647 return ret; 1648 } 1649 1650 void mlx5_ib_odp_cleanup_one(struct mlx5_ib_dev *dev) 1651 { 1652 if (!MLX5_CAP_GEN(dev->mdev, pg)) 1653 return; 1654 1655 mlx5_ib_destroy_pf_eq(dev, &dev->odp_pf_eq); 1656 } 1657 1658 int mlx5_ib_odp_init(void) 1659 { 1660 mlx5_imr_ksm_entries = BIT_ULL(get_order(TASK_SIZE) - 1661 MLX5_IMR_MTT_BITS); 1662 1663 return 0; 1664 } 1665 1666 struct prefetch_mr_work { 1667 struct work_struct work; 1668 struct ib_pd *pd; 1669 u32 pf_flags; 1670 u32 num_sge; 1671 struct ib_sge sg_list[0]; 1672 }; 1673 1674 static void num_pending_prefetch_dec(struct mlx5_ib_dev *dev, 1675 struct ib_sge *sg_list, u32 num_sge, 1676 u32 from) 1677 { 1678 u32 i; 1679 int srcu_key; 1680 1681 srcu_key = srcu_read_lock(&dev->mr_srcu); 1682 1683 for (i = from; i < num_sge; ++i) { 1684 struct mlx5_core_mkey *mmkey; 1685 struct mlx5_ib_mr *mr; 1686 1687 mmkey = xa_load(&dev->mdev->priv.mkey_table, 1688 mlx5_base_mkey(sg_list[i].lkey)); 1689 mr = container_of(mmkey, struct mlx5_ib_mr, mmkey); 1690 atomic_dec(&mr->num_pending_prefetch); 1691 } 1692 1693 srcu_read_unlock(&dev->mr_srcu, srcu_key); 1694 } 1695 1696 static bool num_pending_prefetch_inc(struct ib_pd *pd, 1697 struct ib_sge *sg_list, u32 num_sge) 1698 { 1699 struct mlx5_ib_dev *dev = to_mdev(pd->device); 1700 bool ret = true; 1701 u32 i; 1702 1703 for (i = 0; i < num_sge; ++i) { 1704 struct mlx5_core_mkey *mmkey; 1705 struct mlx5_ib_mr *mr; 1706 1707 mmkey = xa_load(&dev->mdev->priv.mkey_table, 1708 mlx5_base_mkey(sg_list[i].lkey)); 1709 if (!mmkey || mmkey->key != sg_list[i].lkey) { 1710 ret = false; 1711 break; 1712 } 1713 1714 if (mmkey->type != MLX5_MKEY_MR) { 1715 ret = false; 1716 break; 1717 } 1718 1719 mr = container_of(mmkey, struct mlx5_ib_mr, mmkey); 1720 1721 if (mr->ibmr.pd != pd) { 1722 ret = false; 1723 break; 1724 } 1725 1726 if (!mr->live) { 1727 ret = false; 1728 break; 1729 } 1730 1731 atomic_inc(&mr->num_pending_prefetch); 1732 } 1733 1734 if (!ret) 1735 num_pending_prefetch_dec(dev, sg_list, i, 0); 1736 1737 return ret; 1738 } 1739 1740 static int mlx5_ib_prefetch_sg_list(struct ib_pd *pd, u32 pf_flags, 1741 struct ib_sge *sg_list, u32 num_sge) 1742 { 1743 u32 i; 1744 int ret = 0; 1745 struct mlx5_ib_dev *dev = to_mdev(pd->device); 1746 1747 for (i = 0; i < num_sge; ++i) { 1748 struct ib_sge *sg = &sg_list[i]; 1749 int bytes_committed = 0; 1750 1751 ret = pagefault_single_data_segment(dev, pd, sg->lkey, sg->addr, 1752 sg->length, 1753 &bytes_committed, NULL, 1754 pf_flags); 1755 if (ret < 0) 1756 break; 1757 } 1758 1759 return ret < 0 ? ret : 0; 1760 } 1761 1762 static void mlx5_ib_prefetch_mr_work(struct work_struct *work) 1763 { 1764 struct prefetch_mr_work *w = 1765 container_of(work, struct prefetch_mr_work, work); 1766 1767 if (ib_device_try_get(w->pd->device)) { 1768 mlx5_ib_prefetch_sg_list(w->pd, w->pf_flags, w->sg_list, 1769 w->num_sge); 1770 ib_device_put(w->pd->device); 1771 } 1772 1773 num_pending_prefetch_dec(to_mdev(w->pd->device), w->sg_list, 1774 w->num_sge, 0); 1775 kvfree(w); 1776 } 1777 1778 int mlx5_ib_advise_mr_prefetch(struct ib_pd *pd, 1779 enum ib_uverbs_advise_mr_advice advice, 1780 u32 flags, struct ib_sge *sg_list, u32 num_sge) 1781 { 1782 struct mlx5_ib_dev *dev = to_mdev(pd->device); 1783 u32 pf_flags = MLX5_PF_FLAGS_PREFETCH; 1784 struct prefetch_mr_work *work; 1785 bool valid_req; 1786 int srcu_key; 1787 1788 if (advice == IB_UVERBS_ADVISE_MR_ADVICE_PREFETCH) 1789 pf_flags |= MLX5_PF_FLAGS_DOWNGRADE; 1790 1791 if (flags & IB_UVERBS_ADVISE_MR_FLAG_FLUSH) 1792 return mlx5_ib_prefetch_sg_list(pd, pf_flags, sg_list, 1793 num_sge); 1794 1795 work = kvzalloc(struct_size(work, sg_list, num_sge), GFP_KERNEL); 1796 if (!work) 1797 return -ENOMEM; 1798 1799 memcpy(work->sg_list, sg_list, num_sge * sizeof(struct ib_sge)); 1800 1801 /* It is guaranteed that the pd when work is executed is the pd when 1802 * work was queued since pd can't be destroyed while it holds MRs and 1803 * destroying a MR leads to flushing the workquque 1804 */ 1805 work->pd = pd; 1806 work->pf_flags = pf_flags; 1807 work->num_sge = num_sge; 1808 1809 INIT_WORK(&work->work, mlx5_ib_prefetch_mr_work); 1810 1811 srcu_key = srcu_read_lock(&dev->mr_srcu); 1812 1813 valid_req = num_pending_prefetch_inc(pd, sg_list, num_sge); 1814 if (valid_req) 1815 queue_work(system_unbound_wq, &work->work); 1816 else 1817 kvfree(work); 1818 1819 srcu_read_unlock(&dev->mr_srcu, srcu_key); 1820 1821 return valid_req ? 0 : -EINVAL; 1822 } 1823