vdpa/mlx5: Misc fixes
Subsystems (4)
Dragos Tatulea
maintainercover vdpa/mlx5: Misc fixes
Here are 3 fixes that I've had queued for a few weeks but didn't get to send.
Dragos Tatulea (3):
vdpa/mlx5: Fix error cleanup when VQ setup fails in .dev_add vdpa/mlx5: Increase the maximum virtqueue size vdpa/mlx5: Don't flush the workqueue under reslock
drivers/vdpa/mlx5/net/mlx5_vnet.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)
Signature · 1 line
2.54.0
Dragos Tatulea
maintainerPATCH 1/3 vdpa/mlx5: Fix error cleanup when VQ setup fails in .dev_add
When setup_vq_resources() fails in mlx5_vdpa_dev_add(), e.g. because the firmware rejects the provisioned features (HOST_TSO4/6 without CSUM), the error path unregisters the vdpa device first and then keeps using it:
- _vdpa_unregister_device() drops the last reference to the device, so mlx5_vdpa_free() runs and ndev is freed.
- destroy_workqueue(mvdev->wq) then reads the freed ndev.
- put_device() on the freed device underflows the refcount:
refcount_t: underflow; use-after-free.
WARNING: lib/refcount.c:28 at refcount_warn_saturate+0x59/0x90 Call Trace: mlx5_vdpa_dev_add+0x3e0/0x760 [mlx5_vdpa] vdpa_nl_cmd_dev_add_set_doit+0x362/0x440 [vdpa] ...
- mgtdev->ndev is left pointing to the freed ndev, so every following "vdpa dev add" on this management device fails with -ENOSPC.
mlx5_vdpa_dev_del() already does the cleanup properly so use that. Just make sure that an extra reference is taken during the call to avoid _vdpa_unregister_device() from freeing the device under us.
--- drivers/vdpa/mlx5/net/mlx5_vnet.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/vdpa/mlx5/net/mlx5_vnet.c +6 -1
@@ -161,6 +161,8 @@ static void mvqs_set_defaults(struct mlx5_vdpa_net *ndev);static int setup_vq_resources (struct mlx5_vdpa_net *ndev,bool filled);static void teardown_vq_resources (struct mlx5_vdpa_net *ndev);static int resume_vq (struct mlx5_vdpa_net *ndev,struct mlx5_vdpa_virtqueue *mvq); +static void mlx5_vdpa_dev_del (struct vdpa_mgmt_dev *v_mdev, +struct vdpa_device *dev);static bool mlx5_vdpa_debug;
@@ -4043,7 +4045,10 @@ static int mlx5_vdpa_dev_add(struct vdpa_mgmt_dev *v_mdev, const char *name,return 0 ; err_setup_vq_res: -_vdpa_unregister_device (&mvdev->vdev); +get_device (&mvdev->vdev.dev); +mlx5_vdpa_dev_del (&mgtdev->mgtdev, &mvdev->vdev); +put_device (&mvdev->vdev.dev); +return err; err_reg:destroy_workqueue (mvdev->wq); err_alloc:
Signature · 1 line
2.54.0
Re: [PATCH 1/3] vdpa/mlx5: Fix error cleanup when VQ setup fails in .dev_add
Quoted 46 lines · Dragos Tatulea
When
setup_vq_resources()fails inmlx5_vdpa_dev_add(), e.g. because the firmware rejects the provisioned features (HOST_TSO4/6 without CSUM), the error path unregisters the vdpa device first and then keeps using it:-
_vdpa_unregister_device()drops the last reference to the device, somlx5_vdpa_free()runs and ndev is freed.
-destroy_workqueue(mvdev->wq)then reads the freed ndev.
-put_device()on the freed device underflows the refcount:
refcount_t: underflow; use-after-free.WARNING: lib/refcount.c:28 at refcount_warn_saturate+0x59/0x90 Call Trace: mlx5_vdpa_dev_add+0x3e0/0x760 [mlx5_vdpa] vdpa_nl_cmd_dev_add_set_doit+0x362/0x440 [vdpa] ...-
mgtdev->ndevis left pointing to the freed ndev, so every following "vdpa dev add" on this management device fails with-ENOSPC.
mlx5_vdpa_dev_del()already does the cleanup properly so use that. Just make sure that an extra reference is taken during the call to avoid_vdpa_unregister_device()from freeing the device under us.Fixes ffb1aae43ed5 ("vdpa/mlx5: Pre-create hardware VQs at vdpa .dev_add time")Signed-off-by Dragos Tatulea <[email protected]>--- drivers/vdpa/mlx5/net/mlx5_vnet.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)drivers/vdpa/mlx5/net/mlx5_vnet.c +2
-0@@ -161,6 +161,8 @@ static void mvqs_set_defaults(struct mlx5_vdpa_net *ndev);static int setup_vq_resources (struct mlx5_vdpa_net *ndev,bool filled);static void teardown_vq_resources (struct mlx5_vdpa_net *ndev);static int resume_vq (struct mlx5_vdpa_net *ndev,struct mlx5_vdpa_virtqueue *mvq); +static void mlx5_vdpa_dev_del (struct vdpa_mgmt_dev *v_mdev, +struct vdpa_device *dev);static bool mlx5_vdpa_debug;
drivers/vdpa/mlx5/net/mlx5_vnet.c static int mlx5_vdpa_dev_add(struct vdpa_mgmt_dev *v_mdev, const char *name, · line 4051
Quoted 8 lines
drivers/vdpa/mlx5/net/mlx5_vnet.c +4
-1@@ -4043,7 +4045,10 @@ static int mlx5_vdpa_dev_add(struct vdpa_mgmt_dev *v_mdev, const char *name,return 0 ; err_setup_vq_res: -_vdpa_unregister_device (&mvdev->vdev); +get_device (&mvdev->vdev.dev); +mlx5_vdpa_dev_del (&mgtdev->mgtdev, &mvdev->vdev); +put_device (&mvdev->vdev.dev); +return err;
As a suggestion, the cleanup could be more symmetric if mlx5_vdpa_dev_del checks for NULLs.
But I'm ok if you prefer this way,
Thanks!
Quoted 7 lines
drivers/vdpa/mlx5/net/mlx5_vnet.c +0
-1err_reg:destroy_workqueue (mvdev->wq); err_alloc: --2.54.0
Dragos Tatulea
maintainerPATCH 2/3 vdpa/mlx5: Increase the maximum virtqueue size
In reply to Dragos Tatuleamlx5_vdpa has always advertised a maximum virtqueue size of 256 through .get_vq_num_max(), while accepting any queue size in .set_vq_num(). The hardware handles larger rings just fine and users have been running with 1024-entry queues (e.g. QEMU's virtio-net rx_queue_size/tx_queue_size) since the driver was introduced.
The commit in the fixes tag vhost-vdpa enforces the advertised maximum on VHOST_SET_VRING_NUM, so configuring a 1024-entry queue fails with -EINVAL.
Advertise 1024 which is the max that qemu uses today. The device itself supports more but doesn't advertise it in any way.
The Fixes tag points at the vhost-vdpa commit rather than at the driver because that is where the mismatch became user visible. It also keeps this change out of trees without the vhost enforcement, where virtio-vdpa would otherwise start creating 1024-entry rings instead of 256.
--- drivers/vdpa/mlx5/net/mlx5_vnet.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/vdpa/mlx5/net/mlx5_vnet.c +1 -1
@@ -2965,7 +2965,7 @@ static void mlx5_vdpa_set_config_cb(struct vdpa_device *vdev, struct vdpa_callba ndev->config_cb = *cb; } -#define MLX5_VDPA_MAX_VQ_ENTRIES 256 +#define MLX5_VDPA_MAX_VQ_ENTRIES 1024static u16 mlx5_vdpa_get_vq_num_max (struct vdpa_device *vdev) {return MLX5_VDPA_MAX_VQ_ENTRIES ;
Signature · 1 line
2.54.0
Re: [PATCH 2/3] vdpa/mlx5: Increase the maximum virtqueue size
Quoted 14 lines · Dragos Tatulea
mlx5_vdpahas always advertised a maximum virtqueue size of 256 through .get_vq_num_max(), while accepting any queue size in .set_vq_num(). The hardware handles larger rings just fine and users have been running with 1024-entry queues (e.g. QEMU's virtio-net rx_queue_size/tx_queue_size) since the driver was introduced.The commit in the fixes tag vhost-vdpa enforces the advertised maximum on
VHOST_SET_VRING_NUM, so configuring a 1024-entry queue fails with-EINVAL.Advertise 1024 which is the max that qemu uses today. The device itself supports more but doesn't advertise it in any way.
Quoted 6 lines
The Fixes tag points at the vhost-vdpa commit rather than at the driver because that is where the mismatch became user visible. It also keeps this change out of trees without the vhost enforcement, where virtio-vdpa would otherwise start creating 1024-entry rings instead of 256.
Fixes ccb1dc7c527f ("vhost/vdpa: reject VRING_NUM larger than device max")
Is it worth backporting this change? Nothing should break (or be broken because of the 256 vq size), it sounds like an improvement to me.
Either way,
Thanks!
Quoted 22 lines
Signed-off-by Dragos Tatulea <[email protected]>--- drivers/vdpa/mlx5/net/mlx5_vnet.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/vdpa/mlx5/net/mlx5_vnet.c +1
-2@@ -2965,7 +2965,7 @@ static void mlx5_vdpa_set_config_cb(struct vdpa_device *vdev, struct vdpa_callba ndev->config_cb = *cb; } -#define MLX5_VDPA_MAX_VQ_ENTRIES 256 +#define MLX5_VDPA_MAX_VQ_ENTRIES 1024static u16 mlx5_vdpa_get_vq_num_max (struct vdpa_device *vdev) {return MLX5_VDPA_MAX_VQ_ENTRIES ; --2.54.0
Dragos Tatulea
maintainerPATCH 3/3 vdpa/mlx5: Don't flush the workqueue under reslock
In reply to Dragos TatuleaThe 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/0x53other 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.
--- drivers/vdpa/mlx5/net/mlx5_vnet.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/vdpa/mlx5/net/mlx5_vnet.c +7 -2
@@ -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);
Signature · 1 line
2.54.0
Re: [PATCH 3/3] vdpa/mlx5: Don't flush the workqueue under reslock
Quoted 83 lines · Dragos Tatulea
The control VQ work (
mlx5_cvq_kick_handler()) runs onmvdev->wqand takes reslock. Device reset and the error path of .set_status take reslock and then callunregister_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/0x180but 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/0x53other 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]
Quoted 8 lines
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")
Thanks!
Quoted 56 lines
Signed-off-by Dragos Tatulea <[email protected]>--- drivers/vdpa/mlx5/net/mlx5_vnet.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)drivers/vdpa/mlx5/net/mlx5_vnet.c +7
-3@@ -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
[email protected]
AI reviewSashiko has reviewed this 3-patch series and found no issues.
Sashiko has reviewed this 3-patch series and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]