Skip to content

Commit 55ebf0d

Browse files
jasowangmstsirkin
authored andcommitted
vdpa: mlx5: prevent cvq work from hogging CPU
A userspace triggerable infinite loop could happen in mlx5_cvq_kick_handler() if userspace keeps sending a huge amount of cvq requests. Fixing this by introducing a quota and re-queue the work if we're out of the budget (currently the implicit budget is one) . While at it, using a per device work struct to avoid on demand memory allocation for cvq. Fixes: 5262912 ("vdpa/mlx5: Add support for control VQ and MAC setting") Signed-off-by: Jason Wang <jasowang@redhat.com> Link: https://lore.kernel.org/r/20220329042109.4029-1-jasowang@redhat.com Signed-off-by: Michael S. Tsirkin <mst@redhat.com> Acked-by: Eli Cohen <elic@nvidia.com>
1 parent c18c868 commit 55ebf0d

1 file changed

Lines changed: 9 additions & 12 deletions

File tree

drivers/vdpa/mlx5/net/mlx5_vnet.c

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ struct mlx5_vdpa_net {
163163
u32 cur_num_vqs;
164164
struct notifier_block nb;
165165
struct vdpa_callback config_cb;
166+
struct mlx5_vdpa_wq_ent cvq_ent;
166167
};
167168

168169
static void free_resources(struct mlx5_vdpa_net *ndev);
@@ -1659,10 +1660,10 @@ static void mlx5_cvq_kick_handler(struct work_struct *work)
16591660
ndev = to_mlx5_vdpa_ndev(mvdev);
16601661
cvq = &mvdev->cvq;
16611662
if (!(ndev->mvdev.actual_features & BIT_ULL(VIRTIO_NET_F_CTRL_VQ)))
1662-
goto out;
1663+
return;
16631664

16641665
if (!cvq->ready)
1665-
goto out;
1666+
return;
16661667

16671668
while (true) {
16681669
err = vringh_getdesc_iotlb(&cvq->vring, &cvq->riov, &cvq->wiov, &cvq->head,
@@ -1696,17 +1697,17 @@ static void mlx5_cvq_kick_handler(struct work_struct *work)
16961697

16971698
if (vringh_need_notify_iotlb(&cvq->vring))
16981699
vringh_notify(&cvq->vring);
1700+
1701+
queue_work(mvdev->wq, &wqent->work);
1702+
break;
16991703
}
1700-
out:
1701-
kfree(wqent);
17021704
}
17031705

17041706
static void mlx5_vdpa_kick_vq(struct vdpa_device *vdev, u16 idx)
17051707
{
17061708
struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
17071709
struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
17081710
struct mlx5_vdpa_virtqueue *mvq;
1709-
struct mlx5_vdpa_wq_ent *wqent;
17101711

17111712
if (!is_index_valid(mvdev, idx))
17121713
return;
@@ -1715,13 +1716,7 @@ static void mlx5_vdpa_kick_vq(struct vdpa_device *vdev, u16 idx)
17151716
if (!mvdev->wq || !mvdev->cvq.ready)
17161717
return;
17171718

1718-
wqent = kzalloc(sizeof(*wqent), GFP_ATOMIC);
1719-
if (!wqent)
1720-
return;
1721-
1722-
wqent->mvdev = mvdev;
1723-
INIT_WORK(&wqent->work, mlx5_cvq_kick_handler);
1724-
queue_work(mvdev->wq, &wqent->work);
1719+
queue_work(mvdev->wq, &ndev->cvq_ent.work);
17251720
return;
17261721
}
17271722

@@ -2740,6 +2735,8 @@ static int mlx5_vdpa_dev_add(struct vdpa_mgmt_dev *v_mdev, const char *name,
27402735
if (err)
27412736
goto err_mr;
27422737

2738+
ndev->cvq_ent.mvdev = mvdev;
2739+
INIT_WORK(&ndev->cvq_ent.work, mlx5_cvq_kick_handler);
27432740
mvdev->wq = create_singlethread_workqueue("mlx5_vdpa_wq");
27442741
if (!mvdev->wq) {
27452742
err = -ENOMEM;

0 commit comments

Comments
 (0)