The control VQ work (mlx5_cvq_kick_handler()) runs on mvdev->wq and
takes reslock. Device reset and the error path of .set_status take
reslock and then call unregister_link_notifier() which flushes the same
workqueue. A deadlock occurs if a CVQ kick arrives just before the reset
for the CVQ work.

Lockdep caught this when QEMU quits (pkill qemu) and closes the
vhost-vdpa device:

 ======================================================
 WARNING: possible circular locking dependency detected
 ------------------------------------------------------
 qemu-system-x86/5785 is trying to acquire lock:
 ff110001cd292940 ((wq_completion)mlx5_vdpa_wq){+.+.}-{0:0}, at: 
touch_wq_lockdep_map+0x7a/0x180

 but task is already holding lock:
 ff11000136eb4928 (&ndev->reslock){+.+.}-{4:4}, at: 
mlx5_vdpa_compat_reset+0xed/0x760 [mlx5_vdpa]

 which lock already depends on the new lock.

 the existing dependency chain (in reverse order) is:

 -> #2 (&ndev->reslock){+.+.}-{4:4}:
        lock_acquire+0x126/0x330
        down_write+0x87/0x1d0
        mlx5_cvq_kick_handler+0xb1/0x1180 [mlx5_vdpa]
        process_one_work+0x8df/0x1660
        worker_thread+0x62c/0x1030
        kthread+0x377/0x490
        ret_from_fork+0x4d7/0x720
        ret_from_fork_asm+0x11/0x20

 -> #1 ((work_completion)(&ndev->cvq_ent.work)){+.+.}-{0:0}:
        lock_acquire+0x126/0x330
        process_one_work+0x874/0x1660
        worker_thread+0x62c/0x1030
        kthread+0x377/0x490
        ret_from_fork+0x4d7/0x720
        ret_from_fork_asm+0x11/0x20

 -> #0 ((wq_completion)mlx5_vdpa_wq){+.+.}-{0:0}:
        check_prev_add+0xe8/0xdb0
        __lock_acquire+0x1801/0x23e0
        lock_acquire+0x126/0x330
        touch_wq_lockdep_map+0x93/0x180
        __flush_workqueue+0xed/0x1090
        unregister_link_notifier+0x10c/0x170 [mlx5_vdpa]
        mlx5_vdpa_compat_reset+0xfe/0x760 [mlx5_vdpa]
        mlx5_vdpa_reset+0x15/0x30 [mlx5_vdpa]
        _compat_vdpa_reset+0x29b/0x3b0 [vhost_vdpa]
        vhost_vdpa_release+0x16f/0x380 [vhost_vdpa]
        __fput+0x36a/0xaa0
        task_work_run+0x111/0x1f0
        do_exit+0x7ba/0x2590
        do_group_exit+0xb8/0x250
        get_signal+0x1f46/0x2020
        arch_do_signal_or_restart+0x79/0x4a0
        exit_to_user_mode_loop+0xbe/0x450
        do_syscall_64+0x257/0x4f0
        entry_SYSCALL_64_after_hwframe+0x4b/0x53

 other info that might help us debug this:

 Chain exists of:
   (wq_completion)mlx5_vdpa_wq --> (work_completion)(&ndev->cvq_ent.work) --> 
&ndev->reslock

  Possible unsafe locking scenario:

        CPU0                    CPU1
        ----                    ----
   lock(&ndev->reslock);
                                lock((work_completion)(&ndev->cvq_ent.work));
                                lock(&ndev->reslock);
   lock((wq_completion)mlx5_vdpa_wq);

  *** DEADLOCK ***

 locks held by qemu-system-x86/5785: 3, last CPU#9:
  #0: ff1100021d7c2068 (&dev->mutex#3){+.+.}-{4:4}, at: 
vhost_vdpa_release+0x4b/0x380 [vhost_vdpa]
  #1: ff11000136eb44c8 (&vdev->cf_lock){++++}-{4:4}, at: 
_compat_vdpa_reset+0x16b/0x3b0 [vhost_vdpa]
  #2: ff11000136eb4928 (&ndev->reslock){+.+.}-{4:4}, at: 
mlx5_vdpa_compat_reset+0xed/0x760 [mlx5_vdpa]

vhost_vdpa_release() resets the device before stopping the kick
handlers, so CVQ kicks can still be queued while the reset runs.

The fix is to move unregister_link_notifier() calls outside of
reslock. The function doesn't need the reslock.

Fixes: c384c2401eed ("vdpa/mlx5: Avoid losing link state updates")
Cc: [email protected]
Signed-off-by: Dragos Tatulea <[email protected]>
---
 drivers/vdpa/mlx5/net/mlx5_vnet.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/drivers/vdpa/mlx5/net/mlx5_vnet.c 
b/drivers/vdpa/mlx5/net/mlx5_vnet.c
index f1c5f72032a3..a175f734517d 100644
--- a/drivers/vdpa/mlx5/net/mlx5_vnet.c
+++ b/drivers/vdpa/mlx5/net/mlx5_vnet.c
@@ -2908,6 +2908,8 @@ static void unregister_link_notifier(struct mlx5_vdpa_net 
*ndev)
 
        ndev->nb_registered = false;
        mlx5_notifier_unregister(ndev->mvdev.mdev, &ndev->nb);
+       /* mlx5_cvq_kick_handler() work needs the reslock. */
+       lockdep_assert_not_held(&ndev->reslock);
        if (ndev->mvdev.wq)
                flush_workqueue(ndev->mvdev.wq);
 }
@@ -3215,6 +3217,7 @@ static void mlx5_vdpa_set_status(struct vdpa_device 
*vdev, u8 status)
 {
        struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
        struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
+       bool unregister_nb = false;
        int err;
 
        print_status(mvdev, status, true);
@@ -3257,12 +3260,14 @@ static void mlx5_vdpa_set_status(struct vdpa_device 
*vdev, u8 status)
        return;
 
 err_driver:
-       unregister_link_notifier(ndev);
+       unregister_nb = true;
 err_setup:
        mlx5_vdpa_clean_mrs(&ndev->mvdev);
        ndev->mvdev.status |= VIRTIO_CONFIG_S_FAILED;
 err_clear:
        up_write(&ndev->reslock);
+       if (unregister_nb)
+               unregister_link_notifier(ndev);
 }
 
 static void init_group_to_asid_map(struct mlx5_vdpa_dev *mvdev)
@@ -3301,9 +3306,9 @@ static int mlx5_vdpa_compat_reset(struct vdpa_device 
*vdev, u32 flags)
 
        print_status(mvdev, 0, true);
        mlx5_vdpa_info(mvdev, "performing device reset\n");
+       unregister_link_notifier(ndev);
 
        down_write(&ndev->reslock);
-       unregister_link_notifier(ndev);
        vq_reset = needs_vqs_reset(mvdev);
        if (vq_reset) {
                teardown_vq_resources(ndev);
-- 
2.54.0


Reply via email to