2 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
33 #include <rdma/ib_umem.h>
34 #include <rdma/ib_umem_odp.h>
35 #include <linux/kernel.h>
36 #include <linux/dma-buf.h>
37 #include <linux/dma-resv.h>
43 #include <linux/mlx5/eq.h>
45 /* Contains the details of a pagefault. */
46 struct mlx5_pagefault {
52 /* Initiator or send message responder pagefault details. */
54 /* Received packet size, only valid for responders. */
57 * Number of resource holding WQE, depends on type.
61 * WQE index. Refers to either the send queue or
62 * receive queue, according to event_subtype.
66 /* RDMA responder pagefault details */
70 * Received packet size, minimal size page fault
71 * resolution required for forward progress.
79 struct mlx5_ib_pf_eq *eq;
80 struct work_struct work;
83 #define MAX_PREFETCH_LEN (4*1024*1024U)
85 /* Timeout in ms to wait for an active mmu notifier to complete when handling
87 #define MMU_NOTIFIER_TIMEOUT 1000
89 #define MLX5_IMR_MTT_BITS (30 - PAGE_SHIFT)
90 #define MLX5_IMR_MTT_SHIFT (MLX5_IMR_MTT_BITS + PAGE_SHIFT)
91 #define MLX5_IMR_MTT_ENTRIES BIT_ULL(MLX5_IMR_MTT_BITS)
92 #define MLX5_IMR_MTT_SIZE BIT_ULL(MLX5_IMR_MTT_SHIFT)
93 #define MLX5_IMR_MTT_MASK (~(MLX5_IMR_MTT_SIZE - 1))
95 #define MLX5_KSM_PAGE_SHIFT MLX5_IMR_MTT_SHIFT
97 static u64 mlx5_imr_ksm_entries;
99 static void populate_klm(struct mlx5_klm *pklm, size_t idx, size_t nentries,
100 struct mlx5_ib_mr *imr, int flags)
102 struct mlx5_klm *end = pklm + nentries;
104 if (flags & MLX5_IB_UPD_XLT_ZAP) {
105 for (; pklm != end; pklm++, idx++) {
106 pklm->bcount = cpu_to_be32(MLX5_IMR_MTT_SIZE);
107 pklm->key = cpu_to_be32(mr_to_mdev(imr)->null_mkey);
114 * The locking here is pretty subtle. Ideally the implicit_children
115 * xarray would be protected by the umem_mutex, however that is not
116 * possible. Instead this uses a weaker update-then-lock pattern:
119 * mutex_lock(umem_mutex)
120 * mlx5_ib_update_xlt()
121 * mutex_unlock(umem_mutex)
124 * ie any change the xarray must be followed by the locked update_xlt
127 * The umem_mutex provides the acquire/release semantic needed to make
128 * the xa_store() visible to a racing thread.
130 lockdep_assert_held(&to_ib_umem_odp(imr->umem)->umem_mutex);
132 for (; pklm != end; pklm++, idx++) {
133 struct mlx5_ib_mr *mtt = xa_load(&imr->implicit_children, idx);
135 pklm->bcount = cpu_to_be32(MLX5_IMR_MTT_SIZE);
137 pklm->key = cpu_to_be32(mtt->ibmr.lkey);
138 pklm->va = cpu_to_be64(idx * MLX5_IMR_MTT_SIZE);
140 pklm->key = cpu_to_be32(mr_to_mdev(imr)->null_mkey);
146 static u64 umem_dma_to_mtt(dma_addr_t umem_dma)
148 u64 mtt_entry = umem_dma & ODP_DMA_ADDR_MASK;
150 if (umem_dma & ODP_READ_ALLOWED_BIT)
151 mtt_entry |= MLX5_IB_MTT_READ;
152 if (umem_dma & ODP_WRITE_ALLOWED_BIT)
153 mtt_entry |= MLX5_IB_MTT_WRITE;
158 static void populate_mtt(__be64 *pas, size_t idx, size_t nentries,
159 struct mlx5_ib_mr *mr, int flags)
161 struct ib_umem_odp *odp = to_ib_umem_odp(mr->umem);
165 if (flags & MLX5_IB_UPD_XLT_ZAP)
168 for (i = 0; i < nentries; i++) {
169 pa = odp->dma_list[idx + i];
170 pas[i] = cpu_to_be64(umem_dma_to_mtt(pa));
174 void mlx5_odp_populate_xlt(void *xlt, size_t idx, size_t nentries,
175 struct mlx5_ib_mr *mr, int flags)
177 if (flags & MLX5_IB_UPD_XLT_INDIRECT) {
178 populate_klm(xlt, idx, nentries, mr, flags);
180 populate_mtt(xlt, idx, nentries, mr, flags);
184 static void dma_fence_odp_mr(struct mlx5_ib_mr *mr)
186 struct ib_umem_odp *odp = to_ib_umem_odp(mr->umem);
188 /* Ensure mlx5_ib_invalidate_range() will not touch the MR any more */
189 mutex_lock(&odp->umem_mutex);
191 mlx5_mr_cache_invalidate(mr);
192 ib_umem_odp_unmap_dma_pages(odp, ib_umem_start(odp),
194 WARN_ON(odp->npages);
197 mutex_unlock(&odp->umem_mutex);
199 if (!mr->cache_ent) {
200 mlx5_core_destroy_mkey(mr_to_mdev(mr)->mdev, &mr->mmkey);
206 * This must be called after the mr has been removed from implicit_children.
207 * NOTE: The MR does not necessarily have to be
208 * empty here, parallel page faults could have raced with the free process and
211 static void free_implicit_child_mr(struct mlx5_ib_mr *mr, bool need_imr_xlt)
213 struct mlx5_ib_mr *imr = mr->parent;
214 struct ib_umem_odp *odp_imr = to_ib_umem_odp(imr->umem);
215 struct ib_umem_odp *odp = to_ib_umem_odp(mr->umem);
216 unsigned long idx = ib_umem_start(odp) >> MLX5_IMR_MTT_SHIFT;
218 mlx5r_deref_wait_odp_mkey(&mr->mmkey);
221 mutex_lock(&odp_imr->umem_mutex);
222 mlx5_ib_update_xlt(mr->parent, idx, 1, 0,
223 MLX5_IB_UPD_XLT_INDIRECT |
224 MLX5_IB_UPD_XLT_ATOMIC);
225 mutex_unlock(&odp_imr->umem_mutex);
228 dma_fence_odp_mr(mr);
231 mlx5_mr_cache_free(mr_to_mdev(mr), mr);
232 ib_umem_odp_release(odp);
235 static void free_implicit_child_mr_work(struct work_struct *work)
237 struct mlx5_ib_mr *mr =
238 container_of(work, struct mlx5_ib_mr, odp_destroy.work);
239 struct mlx5_ib_mr *imr = mr->parent;
241 free_implicit_child_mr(mr, true);
242 mlx5r_deref_odp_mkey(&imr->mmkey);
245 static void destroy_unused_implicit_child_mr(struct mlx5_ib_mr *mr)
247 struct ib_umem_odp *odp = to_ib_umem_odp(mr->umem);
248 unsigned long idx = ib_umem_start(odp) >> MLX5_IMR_MTT_SHIFT;
249 struct mlx5_ib_mr *imr = mr->parent;
251 if (!refcount_inc_not_zero(&imr->mmkey.usecount))
254 xa_erase(&imr->implicit_children, idx);
256 /* Freeing a MR is a sleeping operation, so bounce to a work queue */
257 INIT_WORK(&mr->odp_destroy.work, free_implicit_child_mr_work);
258 queue_work(system_unbound_wq, &mr->odp_destroy.work);
261 static bool mlx5_ib_invalidate_range(struct mmu_interval_notifier *mni,
262 const struct mmu_notifier_range *range,
263 unsigned long cur_seq)
265 struct ib_umem_odp *umem_odp =
266 container_of(mni, struct ib_umem_odp, notifier);
267 struct mlx5_ib_mr *mr;
268 const u64 umr_block_mask = (MLX5_UMR_MTT_ALIGNMENT /
269 sizeof(struct mlx5_mtt)) - 1;
270 u64 idx = 0, blk_start_idx = 0;
271 u64 invalidations = 0;
277 if (!mmu_notifier_range_blockable(range))
280 mutex_lock(&umem_odp->umem_mutex);
281 mmu_interval_set_seq(mni, cur_seq);
283 * If npages is zero then umem_odp->private may not be setup yet. This
284 * does not complete until after the first page is mapped for DMA.
286 if (!umem_odp->npages)
288 mr = umem_odp->private;
290 start = max_t(u64, ib_umem_start(umem_odp), range->start);
291 end = min_t(u64, ib_umem_end(umem_odp), range->end);
294 * Iteration one - zap the HW's MTTs. The notifiers_count ensures that
295 * while we are doing the invalidation, no page fault will attempt to
296 * overwrite the same MTTs. Concurent invalidations might race us,
297 * but they will write 0s as well, so no difference in the end result.
299 for (addr = start; addr < end; addr += BIT(umem_odp->page_shift)) {
300 idx = (addr - ib_umem_start(umem_odp)) >> umem_odp->page_shift;
302 * Strive to write the MTTs in chunks, but avoid overwriting
303 * non-existing MTTs. The huristic here can be improved to
304 * estimate the cost of another UMR vs. the cost of bigger
307 if (umem_odp->dma_list[idx] &
308 (ODP_READ_ALLOWED_BIT | ODP_WRITE_ALLOWED_BIT)) {
314 /* Count page invalidations */
315 invalidations += idx - blk_start_idx + 1;
317 u64 umr_offset = idx & umr_block_mask;
319 if (in_block && umr_offset == 0) {
320 mlx5_ib_update_xlt(mr, blk_start_idx,
321 idx - blk_start_idx, 0,
322 MLX5_IB_UPD_XLT_ZAP |
323 MLX5_IB_UPD_XLT_ATOMIC);
329 mlx5_ib_update_xlt(mr, blk_start_idx,
330 idx - blk_start_idx + 1, 0,
331 MLX5_IB_UPD_XLT_ZAP |
332 MLX5_IB_UPD_XLT_ATOMIC);
334 mlx5_update_odp_stats(mr, invalidations, invalidations);
337 * We are now sure that the device will not access the
338 * memory. We can safely unmap it, and mark it as dirty if
342 ib_umem_odp_unmap_dma_pages(umem_odp, start, end);
344 if (unlikely(!umem_odp->npages && mr->parent))
345 destroy_unused_implicit_child_mr(mr);
347 mutex_unlock(&umem_odp->umem_mutex);
351 const struct mmu_interval_notifier_ops mlx5_mn_ops = {
352 .invalidate = mlx5_ib_invalidate_range,
355 void mlx5_ib_internal_fill_odp_caps(struct mlx5_ib_dev *dev)
357 struct ib_odp_caps *caps = &dev->odp_caps;
359 memset(caps, 0, sizeof(*caps));
361 if (!MLX5_CAP_GEN(dev->mdev, pg) ||
362 !mlx5_ib_can_load_pas_with_umr(dev, 0))
365 caps->general_caps = IB_ODP_SUPPORT;
367 if (MLX5_CAP_GEN(dev->mdev, umr_extended_translation_offset))
368 dev->odp_max_size = U64_MAX;
370 dev->odp_max_size = BIT_ULL(MLX5_MAX_UMR_SHIFT + PAGE_SHIFT);
372 if (MLX5_CAP_ODP(dev->mdev, ud_odp_caps.send))
373 caps->per_transport_caps.ud_odp_caps |= IB_ODP_SUPPORT_SEND;
375 if (MLX5_CAP_ODP(dev->mdev, ud_odp_caps.srq_receive))
376 caps->per_transport_caps.ud_odp_caps |= IB_ODP_SUPPORT_SRQ_RECV;
378 if (MLX5_CAP_ODP(dev->mdev, rc_odp_caps.send))
379 caps->per_transport_caps.rc_odp_caps |= IB_ODP_SUPPORT_SEND;
381 if (MLX5_CAP_ODP(dev->mdev, rc_odp_caps.receive))
382 caps->per_transport_caps.rc_odp_caps |= IB_ODP_SUPPORT_RECV;
384 if (MLX5_CAP_ODP(dev->mdev, rc_odp_caps.write))
385 caps->per_transport_caps.rc_odp_caps |= IB_ODP_SUPPORT_WRITE;
387 if (MLX5_CAP_ODP(dev->mdev, rc_odp_caps.read))
388 caps->per_transport_caps.rc_odp_caps |= IB_ODP_SUPPORT_READ;
390 if (MLX5_CAP_ODP(dev->mdev, rc_odp_caps.atomic))
391 caps->per_transport_caps.rc_odp_caps |= IB_ODP_SUPPORT_ATOMIC;
393 if (MLX5_CAP_ODP(dev->mdev, rc_odp_caps.srq_receive))
394 caps->per_transport_caps.rc_odp_caps |= IB_ODP_SUPPORT_SRQ_RECV;
396 if (MLX5_CAP_ODP(dev->mdev, xrc_odp_caps.send))
397 caps->per_transport_caps.xrc_odp_caps |= IB_ODP_SUPPORT_SEND;
399 if (MLX5_CAP_ODP(dev->mdev, xrc_odp_caps.receive))
400 caps->per_transport_caps.xrc_odp_caps |= IB_ODP_SUPPORT_RECV;
402 if (MLX5_CAP_ODP(dev->mdev, xrc_odp_caps.write))
403 caps->per_transport_caps.xrc_odp_caps |= IB_ODP_SUPPORT_WRITE;
405 if (MLX5_CAP_ODP(dev->mdev, xrc_odp_caps.read))
406 caps->per_transport_caps.xrc_odp_caps |= IB_ODP_SUPPORT_READ;
408 if (MLX5_CAP_ODP(dev->mdev, xrc_odp_caps.atomic))
409 caps->per_transport_caps.xrc_odp_caps |= IB_ODP_SUPPORT_ATOMIC;
411 if (MLX5_CAP_ODP(dev->mdev, xrc_odp_caps.srq_receive))
412 caps->per_transport_caps.xrc_odp_caps |= IB_ODP_SUPPORT_SRQ_RECV;
414 if (MLX5_CAP_GEN(dev->mdev, fixed_buffer_size) &&
415 MLX5_CAP_GEN(dev->mdev, null_mkey) &&
416 MLX5_CAP_GEN(dev->mdev, umr_extended_translation_offset) &&
417 !MLX5_CAP_GEN(dev->mdev, umr_indirect_mkey_disabled))
418 caps->general_caps |= IB_ODP_SUPPORT_IMPLICIT;
421 static void mlx5_ib_page_fault_resume(struct mlx5_ib_dev *dev,
422 struct mlx5_pagefault *pfault,
425 int wq_num = pfault->event_subtype == MLX5_PFAULT_SUBTYPE_WQE ?
426 pfault->wqe.wq_num : pfault->token;
427 u32 in[MLX5_ST_SZ_DW(page_fault_resume_in)] = {};
430 MLX5_SET(page_fault_resume_in, in, opcode, MLX5_CMD_OP_PAGE_FAULT_RESUME);
431 MLX5_SET(page_fault_resume_in, in, page_fault_type, pfault->type);
432 MLX5_SET(page_fault_resume_in, in, token, pfault->token);
433 MLX5_SET(page_fault_resume_in, in, wq_number, wq_num);
434 MLX5_SET(page_fault_resume_in, in, error, !!error);
436 err = mlx5_cmd_exec_in(dev->mdev, page_fault_resume, in);
438 mlx5_ib_err(dev, "Failed to resolve the page fault on WQ 0x%x err %d\n",
442 static struct mlx5_ib_mr *implicit_get_child_mr(struct mlx5_ib_mr *imr,
445 struct ib_umem_odp *odp;
446 struct mlx5_ib_mr *mr;
447 struct mlx5_ib_mr *ret;
450 odp = ib_umem_odp_alloc_child(to_ib_umem_odp(imr->umem),
451 idx * MLX5_IMR_MTT_SIZE,
452 MLX5_IMR_MTT_SIZE, &mlx5_mn_ops);
454 return ERR_CAST(odp);
456 ret = mr = mlx5_mr_cache_alloc(
457 mr_to_mdev(imr), MLX5_IMR_MTT_CACHE_ENTRY, imr->access_flags);
461 mr->ibmr.pd = imr->ibmr.pd;
462 mr->ibmr.device = &mr_to_mdev(imr)->ib_dev;
463 mr->umem = &odp->umem;
464 mr->ibmr.lkey = mr->mmkey.key;
465 mr->ibmr.rkey = mr->mmkey.key;
466 mr->mmkey.iova = idx * MLX5_IMR_MTT_SIZE;
471 * First refcount is owned by the xarray and second refconut
472 * is returned to the caller.
474 refcount_set(&mr->mmkey.usecount, 2);
476 err = mlx5_ib_update_xlt(mr, 0,
477 MLX5_IMR_MTT_ENTRIES,
479 MLX5_IB_UPD_XLT_ZAP |
480 MLX5_IB_UPD_XLT_ENABLE);
486 xa_lock(&imr->implicit_children);
487 ret = __xa_cmpxchg(&imr->implicit_children, idx, NULL, mr,
490 if (xa_is_err(ret)) {
491 ret = ERR_PTR(xa_err(ret));
495 * Another thread beat us to creating the child mr, use
498 refcount_inc(&ret->mmkey.usecount);
501 xa_unlock(&imr->implicit_children);
503 mlx5_ib_dbg(mr_to_mdev(imr), "key %x mr %p\n", mr->mmkey.key, mr);
507 xa_unlock(&imr->implicit_children);
509 mlx5_mr_cache_free(mr_to_mdev(imr), mr);
511 ib_umem_odp_release(odp);
515 struct mlx5_ib_mr *mlx5_ib_alloc_implicit_mr(struct mlx5_ib_pd *pd,
516 struct ib_udata *udata,
519 struct mlx5_ib_dev *dev = to_mdev(pd->ibpd.device);
520 struct ib_umem_odp *umem_odp;
521 struct mlx5_ib_mr *imr;
524 if (!mlx5_ib_can_load_pas_with_umr(dev,
525 MLX5_IMR_MTT_ENTRIES * PAGE_SIZE))
526 return ERR_PTR(-EOPNOTSUPP);
528 umem_odp = ib_umem_odp_alloc_implicit(&dev->ib_dev, access_flags);
529 if (IS_ERR(umem_odp))
530 return ERR_CAST(umem_odp);
532 imr = mlx5_mr_cache_alloc(dev, MLX5_IMR_KSM_CACHE_ENTRY, access_flags);
538 imr->ibmr.pd = &pd->ibpd;
540 imr->umem = &umem_odp->umem;
541 imr->ibmr.lkey = imr->mmkey.key;
542 imr->ibmr.rkey = imr->mmkey.key;
543 imr->ibmr.device = &dev->ib_dev;
544 imr->umem = &umem_odp->umem;
545 imr->is_odp_implicit = true;
546 xa_init(&imr->implicit_children);
548 err = mlx5_ib_update_xlt(imr, 0,
549 mlx5_imr_ksm_entries,
551 MLX5_IB_UPD_XLT_INDIRECT |
552 MLX5_IB_UPD_XLT_ZAP |
553 MLX5_IB_UPD_XLT_ENABLE);
557 err = mlx5r_store_odp_mkey(dev, &imr->mmkey);
561 mlx5_ib_dbg(dev, "key %x mr %p\n", imr->mmkey.key, imr);
564 mlx5_ib_err(dev, "Failed to register MKEY %d\n", err);
565 mlx5_mr_cache_free(dev, imr);
567 ib_umem_odp_release(umem_odp);
571 void mlx5_ib_free_implicit_mr(struct mlx5_ib_mr *imr)
573 struct ib_umem_odp *odp_imr = to_ib_umem_odp(imr->umem);
574 struct mlx5_ib_dev *dev = mr_to_mdev(imr);
575 struct mlx5_ib_mr *mtt;
578 xa_erase(&dev->odp_mkeys, mlx5_base_mkey(imr->mmkey.key));
580 * All work on the prefetch list must be completed, xa_erase() prevented
581 * new work from being created.
583 mlx5r_deref_wait_odp_mkey(&imr->mmkey);
585 * At this point it is forbidden for any other thread to enter
586 * pagefault_mr() on this imr. It is already forbidden to call
587 * pagefault_mr() on an implicit child. Due to this additions to
588 * implicit_children are prevented.
589 * In addition, any new call to destroy_unused_implicit_child_mr()
590 * may return immediately.
594 * Fence the imr before we destroy the children. This allows us to
595 * skip updating the XLT of the imr during destroy of the child mkey
598 mlx5_mr_cache_invalidate(imr);
600 xa_for_each(&imr->implicit_children, idx, mtt) {
601 xa_erase(&imr->implicit_children, idx);
602 free_implicit_child_mr(mtt, false);
605 mlx5_mr_cache_free(dev, imr);
606 ib_umem_odp_release(odp_imr);
610 * mlx5_ib_fence_odp_mr - Stop all access to the ODP MR
613 * On return no parallel threads will be touching this MR and no DMA will be
616 void mlx5_ib_fence_odp_mr(struct mlx5_ib_mr *mr)
618 /* Prevent new page faults and prefetch requests from succeeding */
619 xa_erase(&mr_to_mdev(mr)->odp_mkeys, mlx5_base_mkey(mr->mmkey.key));
621 /* Wait for all running page-fault handlers to finish. */
622 mlx5r_deref_wait_odp_mkey(&mr->mmkey);
624 dma_fence_odp_mr(mr);
628 * mlx5_ib_fence_dmabuf_mr - Stop all access to the dmabuf MR
631 * On return no parallel threads will be touching this MR and no DMA will be
634 void mlx5_ib_fence_dmabuf_mr(struct mlx5_ib_mr *mr)
636 struct ib_umem_dmabuf *umem_dmabuf = to_ib_umem_dmabuf(mr->umem);
638 /* Prevent new page faults and prefetch requests from succeeding */
639 xa_erase(&mr_to_mdev(mr)->odp_mkeys, mlx5_base_mkey(mr->mmkey.key));
641 mlx5r_deref_wait_odp_mkey(&mr->mmkey);
643 dma_resv_lock(umem_dmabuf->attach->dmabuf->resv, NULL);
644 mlx5_mr_cache_invalidate(mr);
645 umem_dmabuf->private = NULL;
646 ib_umem_dmabuf_unmap_pages(umem_dmabuf);
647 dma_resv_unlock(umem_dmabuf->attach->dmabuf->resv);
649 if (!mr->cache_ent) {
650 mlx5_core_destroy_mkey(mr_to_mdev(mr)->mdev, &mr->mmkey);
655 #define MLX5_PF_FLAGS_DOWNGRADE BIT(1)
656 #define MLX5_PF_FLAGS_SNAPSHOT BIT(2)
657 #define MLX5_PF_FLAGS_ENABLE BIT(3)
658 static int pagefault_real_mr(struct mlx5_ib_mr *mr, struct ib_umem_odp *odp,
659 u64 user_va, size_t bcnt, u32 *bytes_mapped,
662 int page_shift, ret, np;
663 bool downgrade = flags & MLX5_PF_FLAGS_DOWNGRADE;
666 bool fault = !(flags & MLX5_PF_FLAGS_SNAPSHOT);
667 u32 xlt_flags = MLX5_IB_UPD_XLT_ATOMIC;
669 if (flags & MLX5_PF_FLAGS_ENABLE)
670 xlt_flags |= MLX5_IB_UPD_XLT_ENABLE;
672 page_shift = odp->page_shift;
673 start_idx = (user_va - ib_umem_start(odp)) >> page_shift;
674 access_mask = ODP_READ_ALLOWED_BIT;
676 if (odp->umem.writable && !downgrade)
677 access_mask |= ODP_WRITE_ALLOWED_BIT;
679 np = ib_umem_odp_map_dma_and_lock(odp, user_va, bcnt, access_mask, fault);
684 * No need to check whether the MTTs really belong to this MR, since
685 * ib_umem_odp_map_dma_and_lock already checks this.
687 ret = mlx5_ib_update_xlt(mr, start_idx, np, page_shift, xlt_flags);
688 mutex_unlock(&odp->umem_mutex);
692 mlx5_ib_err(mr_to_mdev(mr),
693 "Failed to update mkey page tables\n");
698 u32 new_mappings = (np << page_shift) -
699 (user_va - round_down(user_va, 1 << page_shift));
701 *bytes_mapped += min_t(u32, new_mappings, bcnt);
704 return np << (page_shift - PAGE_SHIFT);
710 static int pagefault_implicit_mr(struct mlx5_ib_mr *imr,
711 struct ib_umem_odp *odp_imr, u64 user_va,
712 size_t bcnt, u32 *bytes_mapped, u32 flags)
714 unsigned long end_idx = (user_va + bcnt - 1) >> MLX5_IMR_MTT_SHIFT;
715 unsigned long upd_start_idx = end_idx + 1;
716 unsigned long upd_len = 0;
717 unsigned long npages = 0;
721 if (unlikely(user_va >= mlx5_imr_ksm_entries * MLX5_IMR_MTT_SIZE ||
722 mlx5_imr_ksm_entries * MLX5_IMR_MTT_SIZE - user_va < bcnt))
725 /* Fault each child mr that intersects with our interval. */
727 unsigned long idx = user_va >> MLX5_IMR_MTT_SHIFT;
728 struct ib_umem_odp *umem_odp;
729 struct mlx5_ib_mr *mtt;
732 xa_lock(&imr->implicit_children);
733 mtt = xa_load(&imr->implicit_children, idx);
734 if (unlikely(!mtt)) {
735 xa_unlock(&imr->implicit_children);
736 mtt = implicit_get_child_mr(imr, idx);
741 upd_start_idx = min(upd_start_idx, idx);
742 upd_len = idx - upd_start_idx + 1;
744 refcount_inc(&mtt->mmkey.usecount);
745 xa_unlock(&imr->implicit_children);
748 umem_odp = to_ib_umem_odp(mtt->umem);
749 len = min_t(u64, user_va + bcnt, ib_umem_end(umem_odp)) -
752 ret = pagefault_real_mr(mtt, umem_odp, user_va, len,
753 bytes_mapped, flags);
755 mlx5r_deref_odp_mkey(&mtt->mmkey);
767 * Any time the implicit_children are changed we must perform an
768 * update of the xlt before exiting to ensure the HW and the
769 * implicit_children remains synchronized.
772 if (likely(!upd_len))
776 * Notice this is not strictly ordered right, the KSM is updated after
777 * the implicit_children is updated, so a parallel page fault could
778 * see a MR that is not yet visible in the KSM. This is similar to a
779 * parallel page fault seeing a MR that is being concurrently removed
780 * from the KSM. Both of these improbable situations are resolved
781 * safely by resuming the HW and then taking another page fault. The
782 * next pagefault handler will see the new information.
784 mutex_lock(&odp_imr->umem_mutex);
785 err = mlx5_ib_update_xlt(imr, upd_start_idx, upd_len, 0,
786 MLX5_IB_UPD_XLT_INDIRECT |
787 MLX5_IB_UPD_XLT_ATOMIC);
788 mutex_unlock(&odp_imr->umem_mutex);
790 mlx5_ib_err(mr_to_mdev(imr), "Failed to update PAS\n");
796 static int pagefault_dmabuf_mr(struct mlx5_ib_mr *mr, size_t bcnt,
797 u32 *bytes_mapped, u32 flags)
799 struct ib_umem_dmabuf *umem_dmabuf = to_ib_umem_dmabuf(mr->umem);
802 unsigned int page_size;
804 if (flags & MLX5_PF_FLAGS_ENABLE)
805 xlt_flags |= MLX5_IB_UPD_XLT_ENABLE;
807 dma_resv_lock(umem_dmabuf->attach->dmabuf->resv, NULL);
808 err = ib_umem_dmabuf_map_pages(umem_dmabuf);
810 dma_resv_unlock(umem_dmabuf->attach->dmabuf->resv);
814 page_size = mlx5_umem_find_best_pgsz(&umem_dmabuf->umem, mkc,
816 umem_dmabuf->umem.iova);
817 if (unlikely(page_size < PAGE_SIZE)) {
818 ib_umem_dmabuf_unmap_pages(umem_dmabuf);
821 err = mlx5_ib_update_mr_pas(mr, xlt_flags);
823 dma_resv_unlock(umem_dmabuf->attach->dmabuf->resv);
829 *bytes_mapped += bcnt;
831 return ib_umem_num_pages(mr->umem);
836 * -EFAULT: The io_virt->bcnt is not within the MR, it covers pages that are
837 * not accessible, or the MR is no longer valid.
838 * -EAGAIN/-ENOMEM: The operation should be retried
840 * -EINVAL/others: General internal malfunction
841 * >0: Number of pages mapped
843 static int pagefault_mr(struct mlx5_ib_mr *mr, u64 io_virt, size_t bcnt,
844 u32 *bytes_mapped, u32 flags)
846 struct ib_umem_odp *odp = to_ib_umem_odp(mr->umem);
848 if (unlikely(io_virt < mr->mmkey.iova))
851 if (mr->umem->is_dmabuf)
852 return pagefault_dmabuf_mr(mr, bcnt, bytes_mapped, flags);
854 if (!odp->is_implicit_odp) {
857 if (check_add_overflow(io_virt - mr->mmkey.iova,
858 (u64)odp->umem.address, &user_va))
860 if (unlikely(user_va >= ib_umem_end(odp) ||
861 ib_umem_end(odp) - user_va < bcnt))
863 return pagefault_real_mr(mr, odp, user_va, bcnt, bytes_mapped,
866 return pagefault_implicit_mr(mr, odp, io_virt, bcnt, bytes_mapped,
870 int mlx5_ib_init_odp_mr(struct mlx5_ib_mr *mr)
874 ret = pagefault_real_mr(mr, to_ib_umem_odp(mr->umem), mr->umem->address,
875 mr->umem->length, NULL,
876 MLX5_PF_FLAGS_SNAPSHOT | MLX5_PF_FLAGS_ENABLE);
877 return ret >= 0 ? 0 : ret;
880 int mlx5_ib_init_dmabuf_mr(struct mlx5_ib_mr *mr)
884 ret = pagefault_dmabuf_mr(mr, mr->umem->length, NULL,
885 MLX5_PF_FLAGS_ENABLE);
887 return ret >= 0 ? 0 : ret;
891 struct pf_frame *next;
898 static bool mkey_is_eq(struct mlx5_core_mkey *mmkey, u32 key)
902 if (mmkey->type == MLX5_MKEY_MW)
903 return mlx5_base_mkey(mmkey->key) == mlx5_base_mkey(key);
904 return mmkey->key == key;
907 static int get_indirect_num_descs(struct mlx5_core_mkey *mmkey)
909 struct mlx5_ib_mw *mw;
910 struct mlx5_ib_devx_mr *devx_mr;
912 if (mmkey->type == MLX5_MKEY_MW) {
913 mw = container_of(mmkey, struct mlx5_ib_mw, mmkey);
917 devx_mr = container_of(mmkey, struct mlx5_ib_devx_mr,
919 return devx_mr->ndescs;
923 * Handle a single data segment in a page-fault WQE or RDMA region.
925 * Returns number of OS pages retrieved on success. The caller may continue to
926 * the next data segment.
927 * Can return the following error codes:
928 * -EAGAIN to designate a temporary error. The caller will abort handling the
929 * page fault and resolve it.
930 * -EFAULT when there's an error mapping the requested pages. The caller will
931 * abort the page fault handling.
933 static int pagefault_single_data_segment(struct mlx5_ib_dev *dev,
934 struct ib_pd *pd, u32 key,
935 u64 io_virt, size_t bcnt,
936 u32 *bytes_committed,
939 int npages = 0, ret, i, outlen, cur_outlen = 0, depth = 0;
940 struct pf_frame *head = NULL, *frame;
941 struct mlx5_core_mkey *mmkey;
942 struct mlx5_ib_mr *mr;
943 struct mlx5_klm *pklm;
948 io_virt += *bytes_committed;
949 bcnt -= *bytes_committed;
952 xa_lock(&dev->odp_mkeys);
953 mmkey = xa_load(&dev->odp_mkeys, mlx5_base_mkey(key));
955 xa_unlock(&dev->odp_mkeys);
958 "skipping non ODP MR (lkey=0x%06x) in page fault handler.\n",
961 *bytes_mapped += bcnt;
963 * The user could specify a SGL with multiple lkeys and only
964 * some of them are ODP. Treat the non-ODP ones as fully
970 refcount_inc(&mmkey->usecount);
971 xa_unlock(&dev->odp_mkeys);
973 if (!mkey_is_eq(mmkey, key)) {
974 mlx5_ib_dbg(dev, "failed to find mkey %x\n", key);
979 switch (mmkey->type) {
981 mr = container_of(mmkey, struct mlx5_ib_mr, mmkey);
983 ret = pagefault_mr(mr, io_virt, bcnt, bytes_mapped, 0);
987 mlx5_update_odp_stats(mr, faults, ret);
994 case MLX5_MKEY_INDIRECT_DEVX:
995 ndescs = get_indirect_num_descs(mmkey);
997 if (depth >= MLX5_CAP_GEN(dev->mdev, max_indirection)) {
998 mlx5_ib_dbg(dev, "indirection level exceeded\n");
1003 outlen = MLX5_ST_SZ_BYTES(query_mkey_out) +
1004 sizeof(*pklm) * (ndescs - 2);
1006 if (outlen > cur_outlen) {
1008 out = kzalloc(outlen, GFP_KERNEL);
1013 cur_outlen = outlen;
1016 pklm = (struct mlx5_klm *)MLX5_ADDR_OF(query_mkey_out, out,
1017 bsf0_klm0_pas_mtt0_1);
1019 ret = mlx5_core_query_mkey(dev->mdev, mmkey, out, outlen);
1023 offset = io_virt - MLX5_GET64(query_mkey_out, out,
1024 memory_key_mkey_entry.start_addr);
1026 for (i = 0; bcnt && i < ndescs; i++, pklm++) {
1027 if (offset >= be32_to_cpu(pklm->bcount)) {
1028 offset -= be32_to_cpu(pklm->bcount);
1032 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1038 frame->key = be32_to_cpu(pklm->key);
1039 frame->io_virt = be64_to_cpu(pklm->va) + offset;
1040 frame->bcnt = min_t(size_t, bcnt,
1041 be32_to_cpu(pklm->bcount) - offset);
1042 frame->depth = depth + 1;
1046 bcnt -= frame->bcnt;
1052 mlx5_ib_dbg(dev, "wrong mkey type %d\n", mmkey->type);
1062 io_virt = frame->io_virt;
1064 depth = frame->depth;
1067 mlx5r_deref_odp_mkey(mmkey);
1073 mlx5r_deref_odp_mkey(mmkey);
1081 *bytes_committed = 0;
1082 return ret ? ret : npages;
1086 * Parse a series of data segments for page fault handling.
1088 * @dev: Pointer to mlx5 IB device
1089 * @pfault: contains page fault information.
1090 * @wqe: points at the first data segment in the WQE.
1091 * @wqe_end: points after the end of the WQE.
1092 * @bytes_mapped: receives the number of bytes that the function was able to
1093 * map. This allows the caller to decide intelligently whether
1094 * enough memory was mapped to resolve the page fault
1095 * successfully (e.g. enough for the next MTU, or the entire
1097 * @total_wqe_bytes: receives the total data size of this WQE in bytes (minus
1098 * the committed bytes).
1099 * @receive_queue: receive WQE end of sg list
1101 * Returns the number of pages loaded if positive, zero for an empty WQE, or a
1102 * negative error code.
1104 static int pagefault_data_segments(struct mlx5_ib_dev *dev,
1105 struct mlx5_pagefault *pfault,
1107 void *wqe_end, u32 *bytes_mapped,
1108 u32 *total_wqe_bytes, bool receive_queue)
1110 int ret = 0, npages = 0;
1119 if (total_wqe_bytes)
1120 *total_wqe_bytes = 0;
1122 while (wqe < wqe_end) {
1123 struct mlx5_wqe_data_seg *dseg = wqe;
1125 io_virt = be64_to_cpu(dseg->addr);
1126 key = be32_to_cpu(dseg->lkey);
1127 byte_count = be32_to_cpu(dseg->byte_count);
1128 inline_segment = !!(byte_count & MLX5_INLINE_SEG);
1129 bcnt = byte_count & ~MLX5_INLINE_SEG;
1131 if (inline_segment) {
1132 bcnt = bcnt & MLX5_WQE_INLINE_SEG_BYTE_COUNT_MASK;
1133 wqe += ALIGN(sizeof(struct mlx5_wqe_inline_seg) + bcnt,
1136 wqe += sizeof(*dseg);
1139 /* receive WQE end of sg list. */
1140 if (receive_queue && bcnt == 0 && key == MLX5_INVALID_LKEY &&
1144 if (!inline_segment && total_wqe_bytes) {
1145 *total_wqe_bytes += bcnt - min_t(size_t, bcnt,
1146 pfault->bytes_committed);
1149 /* A zero length data segment designates a length of 2GB. */
1153 if (inline_segment || bcnt <= pfault->bytes_committed) {
1154 pfault->bytes_committed -=
1156 pfault->bytes_committed);
1160 ret = pagefault_single_data_segment(dev, NULL, key,
1162 &pfault->bytes_committed,
1169 return ret < 0 ? ret : npages;
1173 * Parse initiator WQE. Advances the wqe pointer to point at the
1174 * scatter-gather list, and set wqe_end to the end of the WQE.
1176 static int mlx5_ib_mr_initiator_pfault_handler(
1177 struct mlx5_ib_dev *dev, struct mlx5_pagefault *pfault,
1178 struct mlx5_ib_qp *qp, void **wqe, void **wqe_end, int wqe_length)
1180 struct mlx5_wqe_ctrl_seg *ctrl = *wqe;
1181 u16 wqe_index = pfault->wqe.wqe_index;
1182 struct mlx5_base_av *av;
1183 unsigned ds, opcode;
1184 u32 qpn = qp->trans_qp.base.mqp.qpn;
1186 ds = be32_to_cpu(ctrl->qpn_ds) & MLX5_WQE_CTRL_DS_MASK;
1187 if (ds * MLX5_WQE_DS_UNITS > wqe_length) {
1188 mlx5_ib_err(dev, "Unable to read the complete WQE. ds = 0x%x, ret = 0x%x\n",
1194 mlx5_ib_err(dev, "Got WQE with zero DS. wqe_index=%x, qpn=%x\n",
1199 *wqe_end = *wqe + ds * MLX5_WQE_DS_UNITS;
1200 *wqe += sizeof(*ctrl);
1202 opcode = be32_to_cpu(ctrl->opmod_idx_opcode) &
1203 MLX5_WQE_CTRL_OPCODE_MASK;
1205 if (qp->ibqp.qp_type == IB_QPT_XRC_INI)
1206 *wqe += sizeof(struct mlx5_wqe_xrc_seg);
1208 if (qp->type == IB_QPT_UD || qp->type == MLX5_IB_QPT_DCI) {
1210 if (av->dqp_dct & cpu_to_be32(MLX5_EXTENDED_UD_AV))
1211 *wqe += sizeof(struct mlx5_av);
1213 *wqe += sizeof(struct mlx5_base_av);
1217 case MLX5_OPCODE_RDMA_WRITE:
1218 case MLX5_OPCODE_RDMA_WRITE_IMM:
1219 case MLX5_OPCODE_RDMA_READ:
1220 *wqe += sizeof(struct mlx5_wqe_raddr_seg);
1222 case MLX5_OPCODE_ATOMIC_CS:
1223 case MLX5_OPCODE_ATOMIC_FA:
1224 *wqe += sizeof(struct mlx5_wqe_raddr_seg);
1225 *wqe += sizeof(struct mlx5_wqe_atomic_seg);
1233 * Parse responder WQE and set wqe_end to the end of the WQE.
1235 static int mlx5_ib_mr_responder_pfault_handler_srq(struct mlx5_ib_dev *dev,
1236 struct mlx5_ib_srq *srq,
1237 void **wqe, void **wqe_end,
1240 int wqe_size = 1 << srq->msrq.wqe_shift;
1242 if (wqe_size > wqe_length) {
1243 mlx5_ib_err(dev, "Couldn't read all of the receive WQE's content\n");
1247 *wqe_end = *wqe + wqe_size;
1248 *wqe += sizeof(struct mlx5_wqe_srq_next_seg);
1253 static int mlx5_ib_mr_responder_pfault_handler_rq(struct mlx5_ib_dev *dev,
1254 struct mlx5_ib_qp *qp,
1255 void *wqe, void **wqe_end,
1258 struct mlx5_ib_wq *wq = &qp->rq;
1259 int wqe_size = 1 << wq->wqe_shift;
1261 if (qp->flags_en & MLX5_QP_FLAG_SIGNATURE) {
1262 mlx5_ib_err(dev, "ODP fault with WQE signatures is not supported\n");
1266 if (wqe_size > wqe_length) {
1267 mlx5_ib_err(dev, "Couldn't read all of the receive WQE's content\n");
1271 *wqe_end = wqe + wqe_size;
1276 static inline struct mlx5_core_rsc_common *odp_get_rsc(struct mlx5_ib_dev *dev,
1277 u32 wq_num, int pf_type)
1279 struct mlx5_core_rsc_common *common = NULL;
1280 struct mlx5_core_srq *srq;
1283 case MLX5_WQE_PF_TYPE_RMP:
1284 srq = mlx5_cmd_get_srq(dev, wq_num);
1286 common = &srq->common;
1288 case MLX5_WQE_PF_TYPE_REQ_SEND_OR_WRITE:
1289 case MLX5_WQE_PF_TYPE_RESP:
1290 case MLX5_WQE_PF_TYPE_REQ_READ_OR_ATOMIC:
1291 common = mlx5_core_res_hold(dev, wq_num, MLX5_RES_QP);
1300 static inline struct mlx5_ib_qp *res_to_qp(struct mlx5_core_rsc_common *res)
1302 struct mlx5_core_qp *mqp = (struct mlx5_core_qp *)res;
1304 return to_mibqp(mqp);
1307 static inline struct mlx5_ib_srq *res_to_srq(struct mlx5_core_rsc_common *res)
1309 struct mlx5_core_srq *msrq =
1310 container_of(res, struct mlx5_core_srq, common);
1312 return to_mibsrq(msrq);
1315 static void mlx5_ib_mr_wqe_pfault_handler(struct mlx5_ib_dev *dev,
1316 struct mlx5_pagefault *pfault)
1318 bool sq = pfault->type & MLX5_PFAULT_REQUESTOR;
1319 u16 wqe_index = pfault->wqe.wqe_index;
1320 void *wqe, *wqe_start = NULL, *wqe_end = NULL;
1321 u32 bytes_mapped, total_wqe_bytes;
1322 struct mlx5_core_rsc_common *res;
1323 int resume_with_error = 1;
1324 struct mlx5_ib_qp *qp;
1325 size_t bytes_copied;
1328 res = odp_get_rsc(dev, pfault->wqe.wq_num, pfault->type);
1330 mlx5_ib_dbg(dev, "wqe page fault for missing resource %d\n", pfault->wqe.wq_num);
1334 if (res->res != MLX5_RES_QP && res->res != MLX5_RES_SRQ &&
1335 res->res != MLX5_RES_XSRQ) {
1336 mlx5_ib_err(dev, "wqe page fault for unsupported type %d\n",
1338 goto resolve_page_fault;
1341 wqe_start = (void *)__get_free_page(GFP_KERNEL);
1343 mlx5_ib_err(dev, "Error allocating memory for IO page fault handling.\n");
1344 goto resolve_page_fault;
1348 qp = (res->res == MLX5_RES_QP) ? res_to_qp(res) : NULL;
1350 ret = mlx5_ib_read_wqe_sq(qp, wqe_index, wqe, PAGE_SIZE,
1354 ret = mlx5_ib_mr_initiator_pfault_handler(
1355 dev, pfault, qp, &wqe, &wqe_end, bytes_copied);
1356 } else if (qp && !sq) {
1357 ret = mlx5_ib_read_wqe_rq(qp, wqe_index, wqe, PAGE_SIZE,
1361 ret = mlx5_ib_mr_responder_pfault_handler_rq(
1362 dev, qp, wqe, &wqe_end, bytes_copied);
1364 struct mlx5_ib_srq *srq = res_to_srq(res);
1366 ret = mlx5_ib_read_wqe_srq(srq, wqe_index, wqe, PAGE_SIZE,
1370 ret = mlx5_ib_mr_responder_pfault_handler_srq(
1371 dev, srq, &wqe, &wqe_end, bytes_copied);
1374 if (ret < 0 || wqe >= wqe_end)
1375 goto resolve_page_fault;
1377 ret = pagefault_data_segments(dev, pfault, wqe, wqe_end, &bytes_mapped,
1378 &total_wqe_bytes, !sq);
1382 if (ret < 0 || total_wqe_bytes > bytes_mapped)
1383 goto resolve_page_fault;
1387 resume_with_error = 0;
1393 "Failed reading a WQE following page fault, error %d, wqe_index %x, qpn %x\n",
1394 ret, wqe_index, pfault->token);
1397 mlx5_ib_page_fault_resume(dev, pfault, resume_with_error);
1398 mlx5_ib_dbg(dev, "PAGE FAULT completed. QP 0x%x resume_with_error=%d, type: 0x%x\n",
1399 pfault->wqe.wq_num, resume_with_error,
1401 mlx5_core_res_put(res);
1402 free_page((unsigned long)wqe_start);
1405 static int pages_in_range(u64 address, u32 length)
1407 return (ALIGN(address + length, PAGE_SIZE) -
1408 (address & PAGE_MASK)) >> PAGE_SHIFT;
1411 static void mlx5_ib_mr_rdma_pfault_handler(struct mlx5_ib_dev *dev,
1412 struct mlx5_pagefault *pfault)
1416 u32 prefetch_len = pfault->bytes_committed;
1417 int prefetch_activated = 0;
1418 u32 rkey = pfault->rdma.r_key;
1421 /* The RDMA responder handler handles the page fault in two parts.
1422 * First it brings the necessary pages for the current packet
1423 * (and uses the pfault context), and then (after resuming the QP)
1424 * prefetches more pages. The second operation cannot use the pfault
1425 * context and therefore uses the dummy_pfault context allocated on
1427 pfault->rdma.rdma_va += pfault->bytes_committed;
1428 pfault->rdma.rdma_op_len -= min(pfault->bytes_committed,
1429 pfault->rdma.rdma_op_len);
1430 pfault->bytes_committed = 0;
1432 address = pfault->rdma.rdma_va;
1433 length = pfault->rdma.rdma_op_len;
1435 /* For some operations, the hardware cannot tell the exact message
1436 * length, and in those cases it reports zero. Use prefetch
1439 prefetch_activated = 1;
1440 length = pfault->rdma.packet_size;
1441 prefetch_len = min(MAX_PREFETCH_LEN, prefetch_len);
1444 ret = pagefault_single_data_segment(dev, NULL, rkey, address, length,
1445 &pfault->bytes_committed, NULL);
1446 if (ret == -EAGAIN) {
1447 /* We're racing with an invalidation, don't prefetch */
1448 prefetch_activated = 0;
1449 } else if (ret < 0 || pages_in_range(address, length) > ret) {
1450 mlx5_ib_page_fault_resume(dev, pfault, 1);
1452 mlx5_ib_dbg(dev, "PAGE FAULT error %d. QP 0x%x, type: 0x%x\n",
1453 ret, pfault->token, pfault->type);
1457 mlx5_ib_page_fault_resume(dev, pfault, 0);
1458 mlx5_ib_dbg(dev, "PAGE FAULT completed. QP 0x%x, type: 0x%x, prefetch_activated: %d\n",
1459 pfault->token, pfault->type,
1460 prefetch_activated);
1462 /* At this point, there might be a new pagefault already arriving in
1463 * the eq, switch to the dummy pagefault for the rest of the
1464 * processing. We're still OK with the objects being alive as the
1465 * work-queue is being fenced. */
1467 if (prefetch_activated) {
1468 u32 bytes_committed = 0;
1470 ret = pagefault_single_data_segment(dev, NULL, rkey, address,
1472 &bytes_committed, NULL);
1473 if (ret < 0 && ret != -EAGAIN) {
1474 mlx5_ib_dbg(dev, "Prefetch failed. ret: %d, QP 0x%x, address: 0x%.16llx, length = 0x%.16x\n",
1475 ret, pfault->token, address, prefetch_len);
1480 static void mlx5_ib_pfault(struct mlx5_ib_dev *dev, struct mlx5_pagefault *pfault)
1482 u8 event_subtype = pfault->event_subtype;
1484 switch (event_subtype) {
1485 case MLX5_PFAULT_SUBTYPE_WQE:
1486 mlx5_ib_mr_wqe_pfault_handler(dev, pfault);
1488 case MLX5_PFAULT_SUBTYPE_RDMA:
1489 mlx5_ib_mr_rdma_pfault_handler(dev, pfault);
1492 mlx5_ib_err(dev, "Invalid page fault event subtype: 0x%x\n",
1494 mlx5_ib_page_fault_resume(dev, pfault, 1);
1498 static void mlx5_ib_eqe_pf_action(struct work_struct *work)
1500 struct mlx5_pagefault *pfault = container_of(work,
1501 struct mlx5_pagefault,
1503 struct mlx5_ib_pf_eq *eq = pfault->eq;
1505 mlx5_ib_pfault(eq->dev, pfault);
1506 mempool_free(pfault, eq->pool);
1509 static void mlx5_ib_eq_pf_process(struct mlx5_ib_pf_eq *eq)
1511 struct mlx5_eqe_page_fault *pf_eqe;
1512 struct mlx5_pagefault *pfault;
1513 struct mlx5_eqe *eqe;
1516 while ((eqe = mlx5_eq_get_eqe(eq->core, cc))) {
1517 pfault = mempool_alloc(eq->pool, GFP_ATOMIC);
1519 schedule_work(&eq->work);
1523 pf_eqe = &eqe->data.page_fault;
1524 pfault->event_subtype = eqe->sub_type;
1525 pfault->bytes_committed = be32_to_cpu(pf_eqe->bytes_committed);
1527 mlx5_ib_dbg(eq->dev,
1528 "PAGE_FAULT: subtype: 0x%02x, bytes_committed: 0x%06x\n",
1529 eqe->sub_type, pfault->bytes_committed);
1531 switch (eqe->sub_type) {
1532 case MLX5_PFAULT_SUBTYPE_RDMA:
1533 /* RDMA based event */
1535 be32_to_cpu(pf_eqe->rdma.pftype_token) >> 24;
1537 be32_to_cpu(pf_eqe->rdma.pftype_token) &
1539 pfault->rdma.r_key =
1540 be32_to_cpu(pf_eqe->rdma.r_key);
1541 pfault->rdma.packet_size =
1542 be16_to_cpu(pf_eqe->rdma.packet_length);
1543 pfault->rdma.rdma_op_len =
1544 be32_to_cpu(pf_eqe->rdma.rdma_op_len);
1545 pfault->rdma.rdma_va =
1546 be64_to_cpu(pf_eqe->rdma.rdma_va);
1547 mlx5_ib_dbg(eq->dev,
1548 "PAGE_FAULT: type:0x%x, token: 0x%06x, r_key: 0x%08x\n",
1549 pfault->type, pfault->token,
1550 pfault->rdma.r_key);
1551 mlx5_ib_dbg(eq->dev,
1552 "PAGE_FAULT: rdma_op_len: 0x%08x, rdma_va: 0x%016llx\n",
1553 pfault->rdma.rdma_op_len,
1554 pfault->rdma.rdma_va);
1557 case MLX5_PFAULT_SUBTYPE_WQE:
1558 /* WQE based event */
1560 (be32_to_cpu(pf_eqe->wqe.pftype_wq) >> 24) & 0x7;
1562 be32_to_cpu(pf_eqe->wqe.token);
1563 pfault->wqe.wq_num =
1564 be32_to_cpu(pf_eqe->wqe.pftype_wq) &
1566 pfault->wqe.wqe_index =
1567 be16_to_cpu(pf_eqe->wqe.wqe_index);
1568 pfault->wqe.packet_size =
1569 be16_to_cpu(pf_eqe->wqe.packet_length);
1570 mlx5_ib_dbg(eq->dev,
1571 "PAGE_FAULT: type:0x%x, token: 0x%06x, wq_num: 0x%06x, wqe_index: 0x%04x\n",
1572 pfault->type, pfault->token,
1574 pfault->wqe.wqe_index);
1578 mlx5_ib_warn(eq->dev,
1579 "Unsupported page fault event sub-type: 0x%02hhx\n",
1581 /* Unsupported page faults should still be
1582 * resolved by the page fault handler
1587 INIT_WORK(&pfault->work, mlx5_ib_eqe_pf_action);
1588 queue_work(eq->wq, &pfault->work);
1590 cc = mlx5_eq_update_cc(eq->core, ++cc);
1593 mlx5_eq_update_ci(eq->core, cc, 1);
1596 static int mlx5_ib_eq_pf_int(struct notifier_block *nb, unsigned long type,
1599 struct mlx5_ib_pf_eq *eq =
1600 container_of(nb, struct mlx5_ib_pf_eq, irq_nb);
1601 unsigned long flags;
1603 if (spin_trylock_irqsave(&eq->lock, flags)) {
1604 mlx5_ib_eq_pf_process(eq);
1605 spin_unlock_irqrestore(&eq->lock, flags);
1607 schedule_work(&eq->work);
1613 /* mempool_refill() was proposed but unfortunately wasn't accepted
1614 * http://lkml.iu.edu/hypermail/linux/kernel/1512.1/05073.html
1617 static void mempool_refill(mempool_t *pool)
1619 while (pool->curr_nr < pool->min_nr)
1620 mempool_free(mempool_alloc(pool, GFP_KERNEL), pool);
1623 static void mlx5_ib_eq_pf_action(struct work_struct *work)
1625 struct mlx5_ib_pf_eq *eq =
1626 container_of(work, struct mlx5_ib_pf_eq, work);
1628 mempool_refill(eq->pool);
1630 spin_lock_irq(&eq->lock);
1631 mlx5_ib_eq_pf_process(eq);
1632 spin_unlock_irq(&eq->lock);
1636 MLX5_IB_NUM_PF_EQE = 0x1000,
1637 MLX5_IB_NUM_PF_DRAIN = 64,
1641 mlx5_ib_create_pf_eq(struct mlx5_ib_dev *dev, struct mlx5_ib_pf_eq *eq)
1643 struct mlx5_eq_param param = {};
1646 INIT_WORK(&eq->work, mlx5_ib_eq_pf_action);
1647 spin_lock_init(&eq->lock);
1650 eq->pool = mempool_create_kmalloc_pool(MLX5_IB_NUM_PF_DRAIN,
1651 sizeof(struct mlx5_pagefault));
1655 eq->wq = alloc_workqueue("mlx5_ib_page_fault",
1656 WQ_HIGHPRI | WQ_UNBOUND | WQ_MEM_RECLAIM,
1663 eq->irq_nb.notifier_call = mlx5_ib_eq_pf_int;
1664 param = (struct mlx5_eq_param) {
1666 .nent = MLX5_IB_NUM_PF_EQE,
1668 param.mask[0] = 1ull << MLX5_EVENT_TYPE_PAGE_FAULT;
1669 eq->core = mlx5_eq_create_generic(dev->mdev, ¶m);
1670 if (IS_ERR(eq->core)) {
1671 err = PTR_ERR(eq->core);
1674 err = mlx5_eq_enable(dev->mdev, eq->core, &eq->irq_nb);
1676 mlx5_ib_err(dev, "failed to enable odp EQ %d\n", err);
1682 mlx5_eq_destroy_generic(dev->mdev, eq->core);
1684 destroy_workqueue(eq->wq);
1686 mempool_destroy(eq->pool);
1691 mlx5_ib_destroy_pf_eq(struct mlx5_ib_dev *dev, struct mlx5_ib_pf_eq *eq)
1695 mlx5_eq_disable(dev->mdev, eq->core, &eq->irq_nb);
1696 err = mlx5_eq_destroy_generic(dev->mdev, eq->core);
1697 cancel_work_sync(&eq->work);
1698 destroy_workqueue(eq->wq);
1699 mempool_destroy(eq->pool);
1704 void mlx5_odp_init_mr_cache_entry(struct mlx5_cache_ent *ent)
1706 if (!(ent->dev->odp_caps.general_caps & IB_ODP_SUPPORT_IMPLICIT))
1709 switch (ent->order - 2) {
1710 case MLX5_IMR_MTT_CACHE_ENTRY:
1711 ent->page = PAGE_SHIFT;
1712 ent->xlt = MLX5_IMR_MTT_ENTRIES *
1713 sizeof(struct mlx5_mtt) /
1714 MLX5_IB_UMR_OCTOWORD;
1715 ent->access_mode = MLX5_MKC_ACCESS_MODE_MTT;
1719 case MLX5_IMR_KSM_CACHE_ENTRY:
1720 ent->page = MLX5_KSM_PAGE_SHIFT;
1721 ent->xlt = mlx5_imr_ksm_entries *
1722 sizeof(struct mlx5_klm) /
1723 MLX5_IB_UMR_OCTOWORD;
1724 ent->access_mode = MLX5_MKC_ACCESS_MODE_KSM;
1730 static const struct ib_device_ops mlx5_ib_dev_odp_ops = {
1731 .advise_mr = mlx5_ib_advise_mr,
1734 int mlx5_ib_odp_init_one(struct mlx5_ib_dev *dev)
1738 if (!(dev->odp_caps.general_caps & IB_ODP_SUPPORT))
1741 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_odp_ops);
1743 if (dev->odp_caps.general_caps & IB_ODP_SUPPORT_IMPLICIT) {
1744 ret = mlx5_cmd_null_mkey(dev->mdev, &dev->null_mkey);
1746 mlx5_ib_err(dev, "Error getting null_mkey %d\n", ret);
1751 ret = mlx5_ib_create_pf_eq(dev, &dev->odp_pf_eq);
1756 void mlx5_ib_odp_cleanup_one(struct mlx5_ib_dev *dev)
1758 if (!(dev->odp_caps.general_caps & IB_ODP_SUPPORT))
1761 mlx5_ib_destroy_pf_eq(dev, &dev->odp_pf_eq);
1764 int mlx5_ib_odp_init(void)
1766 mlx5_imr_ksm_entries = BIT_ULL(get_order(TASK_SIZE) -
1772 struct prefetch_mr_work {
1773 struct work_struct work;
1778 struct mlx5_ib_mr *mr;
1783 static void destroy_prefetch_work(struct prefetch_mr_work *work)
1787 for (i = 0; i < work->num_sge; ++i)
1788 mlx5r_deref_odp_mkey(&work->frags[i].mr->mmkey);
1793 static struct mlx5_ib_mr *
1794 get_prefetchable_mr(struct ib_pd *pd, enum ib_uverbs_advise_mr_advice advice,
1797 struct mlx5_ib_dev *dev = to_mdev(pd->device);
1798 struct mlx5_core_mkey *mmkey;
1799 struct mlx5_ib_mr *mr = NULL;
1801 xa_lock(&dev->odp_mkeys);
1802 mmkey = xa_load(&dev->odp_mkeys, mlx5_base_mkey(lkey));
1803 if (!mmkey || mmkey->key != lkey || mmkey->type != MLX5_MKEY_MR)
1806 mr = container_of(mmkey, struct mlx5_ib_mr, mmkey);
1808 if (mr->ibmr.pd != pd) {
1813 /* prefetch with write-access must be supported by the MR */
1814 if (advice == IB_UVERBS_ADVISE_MR_ADVICE_PREFETCH_WRITE &&
1815 !mr->umem->writable) {
1820 refcount_inc(&mmkey->usecount);
1822 xa_unlock(&dev->odp_mkeys);
1826 static void mlx5_ib_prefetch_mr_work(struct work_struct *w)
1828 struct prefetch_mr_work *work =
1829 container_of(w, struct prefetch_mr_work, work);
1830 u32 bytes_mapped = 0;
1834 /* We rely on IB/core that work is executed if we have num_sge != 0 only. */
1835 WARN_ON(!work->num_sge);
1836 for (i = 0; i < work->num_sge; ++i) {
1837 ret = pagefault_mr(work->frags[i].mr, work->frags[i].io_virt,
1838 work->frags[i].length, &bytes_mapped,
1842 mlx5_update_odp_stats(work->frags[i].mr, prefetch, ret);
1845 destroy_prefetch_work(work);
1848 static bool init_prefetch_work(struct ib_pd *pd,
1849 enum ib_uverbs_advise_mr_advice advice,
1850 u32 pf_flags, struct prefetch_mr_work *work,
1851 struct ib_sge *sg_list, u32 num_sge)
1855 INIT_WORK(&work->work, mlx5_ib_prefetch_mr_work);
1856 work->pf_flags = pf_flags;
1858 for (i = 0; i < num_sge; ++i) {
1859 work->frags[i].io_virt = sg_list[i].addr;
1860 work->frags[i].length = sg_list[i].length;
1862 get_prefetchable_mr(pd, advice, sg_list[i].lkey);
1863 if (!work->frags[i].mr) {
1868 work->num_sge = num_sge;
1872 static int mlx5_ib_prefetch_sg_list(struct ib_pd *pd,
1873 enum ib_uverbs_advise_mr_advice advice,
1874 u32 pf_flags, struct ib_sge *sg_list,
1877 u32 bytes_mapped = 0;
1881 for (i = 0; i < num_sge; ++i) {
1882 struct mlx5_ib_mr *mr;
1884 mr = get_prefetchable_mr(pd, advice, sg_list[i].lkey);
1887 ret = pagefault_mr(mr, sg_list[i].addr, sg_list[i].length,
1888 &bytes_mapped, pf_flags);
1890 mlx5r_deref_odp_mkey(&mr->mmkey);
1893 mlx5_update_odp_stats(mr, prefetch, ret);
1894 mlx5r_deref_odp_mkey(&mr->mmkey);
1900 int mlx5_ib_advise_mr_prefetch(struct ib_pd *pd,
1901 enum ib_uverbs_advise_mr_advice advice,
1902 u32 flags, struct ib_sge *sg_list, u32 num_sge)
1905 struct prefetch_mr_work *work;
1907 if (advice == IB_UVERBS_ADVISE_MR_ADVICE_PREFETCH)
1908 pf_flags |= MLX5_PF_FLAGS_DOWNGRADE;
1910 if (advice == IB_UVERBS_ADVISE_MR_ADVICE_PREFETCH_NO_FAULT)
1911 pf_flags |= MLX5_PF_FLAGS_SNAPSHOT;
1913 if (flags & IB_UVERBS_ADVISE_MR_FLAG_FLUSH)
1914 return mlx5_ib_prefetch_sg_list(pd, advice, pf_flags, sg_list,
1917 work = kvzalloc(struct_size(work, frags, num_sge), GFP_KERNEL);
1921 if (!init_prefetch_work(pd, advice, pf_flags, work, sg_list, num_sge)) {
1922 destroy_prefetch_work(work);
1925 queue_work(system_unbound_wq, &work->work);