Re: [PATCH 1/3] vdpa/mlx5: Fix error cleanup when VQ setup fails in .dev_add
From: Eugenio Perez Martin
Date: Mon Nov 23 2026 - 11:08:57 EST
On Mon, Nov 23, 2026 at 2:59 PM Dragos Tatulea <dtatulea@xxxxxxxxxx> wrote:
>
> 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.
>
> Fixes: ffb1aae43ed5 ("vdpa/mlx5: Pre-create hardware VQs at vdpa .dev_add time")
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Dragos Tatulea <dtatulea@xxxxxxxxxx>
> ---
> drivers/vdpa/mlx5/net/mlx5_vnet.c | 7 ++++++-
> 1 file changed, 6 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/vdpa/mlx5/net/mlx5_vnet.c b/drivers/vdpa/mlx5/net/mlx5_vnet.c
> index 6830a80e8543..7a39b0bbbee3 100644
> --- a/drivers/vdpa/mlx5/net/mlx5_vnet.c
> +++ b/drivers/vdpa/mlx5/net/mlx5_vnet.c
> @@ -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;
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,
Acked-by: Eugenio Pérez <eperezma@xxxxxxxxxx>
Thanks!
> err_reg:
> destroy_workqueue(mvdev->wq);
> err_alloc:
> --
> 2.54.0
>
>