From: Dragos Tatulea <dtatulea@nvidia.com>
To: Eugenio Perez Martin <eperezma@redhat.com>
Cc: "Michael S . Tsirkin" <mst@redhat.com>,
virtualization@lists.linux.dev, kvm@vger.kernel.org,
linux-kernel@vger.kernel.org, Gal Pressman <gal@nvidia.com>,
stable@vger.kernel.org
Subject: Re: [PATCH 1/3] vdpa/mlx5: Fix error cleanup when VQ setup fails in .dev_add
Date: Tue, 24 Nov 2026 20:15:55 +0100 [thread overview]
Message-ID: <abc046dd-b8d4-4620-bc5a-08f12b7efcb6@nvidia.com> (raw)
In-Reply-To: <CAJaqyWedvj6htnM4rJyrbynW3u1k=6uLHrfuK05YvM5z8v1Hjg@mail.gmail.com>
On 23.11.26 17:08, Eugenio Perez Martin wrote:
> On Mon, Nov 23, 2026 at 2:59 PM Dragos Tatulea <dtatulea@nvidia.com> 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@vger.kernel.org
>> Signed-off-by: Dragos Tatulea <dtatulea@nvidia.com>
>> ---
>> 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@redhat.com>
>
Thanks for the Ack.
Thanks,
Dragos
next prev parent reply other threads:[~2026-11-24 19:16 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-23 13:56 [PATCH 0/3] vdpa/mlx5: Misc fixes Dragos Tatulea
2026-11-23 13:56 ` [PATCH 1/3] vdpa/mlx5: Fix error cleanup when VQ setup fails in .dev_add Dragos Tatulea
2026-11-23 16:08 ` Eugenio Perez Martin
2026-11-24 19:15 ` Dragos Tatulea [this message]
2026-11-23 13:56 ` [PATCH 2/3] vdpa/mlx5: Increase the maximum virtqueue size Dragos Tatulea
2026-11-23 15:38 ` Eugenio Perez Martin
2026-11-24 19:08 ` Dragos Tatulea
2026-11-23 13:56 ` [PATCH 3/3] vdpa/mlx5: Don't flush the workqueue under reslock Dragos Tatulea
2026-11-23 15:47 ` Eugenio Perez Martin
2026-11-23 14:06 ` [PATCH 0/3] vdpa/mlx5: Misc fixes sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=abc046dd-b8d4-4620-bc5a-08f12b7efcb6@nvidia.com \
--to=dtatulea@nvidia.com \
--cc=eperezma@redhat.com \
--cc=gal@nvidia.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mst@redhat.com \
--cc=stable@vger.kernel.org \
--cc=virtualization@lists.linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.