Skip to content

Commit a862192

Browse files
committed
RDMA/mlx5: Prevent prefetch from racing with implicit destruction
Prefetch work in mlx5_ib_prefetch_mr_work can be queued and able to run concurrently with destruction of the implicit MR. The num_deferred_work was intended to serialize this, but there is a race: CPU0 CPU1 mlx5_ib_free_implicit_mr() xa_erase(odp_mkeys) synchronize_srcu() __xa_erase(implicit_children) mlx5_ib_prefetch_mr_work() pagefault_mr() pagefault_implicit_mr() implicit_get_child_mr() xa_cmpxchg() atomic_dec_and_test(num_deferred_mr) wait_event(imr->q_deferred_work) ib_umem_odp_release(odp_imr) kfree(odp_imr) At this point in mlx5_ib_free_implicit_mr() the implicit_children list is supposed to be empty forever so that destroy_unused_implicit_child_mr() and related are not and will not be running. Since it is not empty the destroy_unused_implicit_child_mr() flow ends up touching deallocated memory as mlx5_ib_free_implicit_mr() already tore down the imr parent. The solution is to flush out the prefetch wq by driving num_deferred_work to zero after creation of new prefetch work is blocked. Fixes: 5256edc ("RDMA/mlx5: Rework implicit ODP destroy") Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Leon Romanovsky <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]>
1 parent 87c4c77 commit a862192

File tree

1 file changed

+19
-3
lines changed
  • drivers/infiniband/hw/mlx5

1 file changed

+19
-3
lines changed

drivers/infiniband/hw/mlx5/odp.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,23 @@ void mlx5_ib_free_implicit_mr(struct mlx5_ib_mr *imr)
601601
*/
602602
synchronize_srcu(&dev->odp_srcu);
603603

604+
/*
605+
* All work on the prefetch list must be completed, xa_erase() prevented
606+
* new work from being created.
607+
*/
608+
wait_event(imr->q_deferred_work, !atomic_read(&imr->num_deferred_work));
609+
610+
/*
611+
* At this point it is forbidden for any other thread to enter
612+
* pagefault_mr() on this imr. It is already forbidden to call
613+
* pagefault_mr() on an implicit child. Due to this additions to
614+
* implicit_children are prevented.
615+
*/
616+
617+
/*
618+
* Block destroy_unused_implicit_child_mr() from incrementing
619+
* num_deferred_work.
620+
*/
604621
xa_lock(&imr->implicit_children);
605622
xa_for_each (&imr->implicit_children, idx, mtt) {
606623
__xa_erase(&imr->implicit_children, idx);
@@ -609,9 +626,8 @@ void mlx5_ib_free_implicit_mr(struct mlx5_ib_mr *imr)
609626
xa_unlock(&imr->implicit_children);
610627

611628
/*
612-
* num_deferred_work can only be incremented inside the odp_srcu, or
613-
* under xa_lock while the child is in the xarray. Thus at this point
614-
* it is only decreasing, and all work holding it is now on the wq.
629+
* Wait for any concurrent destroy_unused_implicit_child_mr() to
630+
* complete.
615631
*/
616632
wait_event(imr->q_deferred_work, !atomic_read(&imr->num_deferred_work));
617633

0 commit comments

Comments
 (0)