Re: [PATCH 1/3] vdpa/mlx5: Fix error cleanup when VQ setup fails in .dev_add
From: Dragos Tatulea
Date: Tue Nov 24 2026 - 14:17:37 EST
On 23.11.26 17:08, Eugenio Perez Martin wrote:
> 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.
>
It does, that's why we can call it:
- teardown_vq_resources() checks ndev->setup.
- mdev->wq is already initialized, so no need to check
My initial version was checking for NULLs and getting references in
mlx5_vdpa_dev_del() but then I realized that it is not needed and
it made the code more ugly.
> But I'm ok if you prefer this way,
>
> Acked-by: Eugenio Pérez <eperezma@xxxxxxxxxx>
>
Thanks for the Ack.
Thanks,
Dragos