Skip to content

Commit 6211165

Browse files
dtatuleamstsirkin
authored andcommitted
vdpa/mlx5: Postpone MR deletion
Currently, when a new MR is set up, the old MR is deleted. MR deletion is about 30-40% the time of MR creation. As deleting the old MR is not important for the process of setting up the new MR, this operation can be postponed. This series adds a workqueue that does MR garbage collection at a later point. If the MR lock is taken, the handler will back off and reschedule. The exception during shutdown: then the handler must not postpone the work. Note that this is only a speculative optimization: if there is some mapping operation that is triggered while the garbage collector handler has the lock taken, this operation it will have to wait for the handler to finish. Signed-off-by: Dragos Tatulea <[email protected]> Reviewed-by: Cosmin Ratiu <[email protected]> Message-Id: <[email protected]> Signed-off-by: Michael S. Tsirkin <[email protected]>
1 parent f30a123 commit 6211165

File tree

3 files changed

+64
-5
lines changed

3 files changed

+64
-5
lines changed

drivers/vdpa/mlx5/core/mlx5_vdpa.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,18 @@ enum {
8686
struct mlx5_vdpa_mr_resources {
8787
struct mlx5_vdpa_mr *mr[MLX5_VDPA_NUM_AS];
8888
unsigned int group2asid[MLX5_VDPA_NUMVQ_GROUPS];
89+
90+
/* Pre-deletion mr list */
8991
struct list_head mr_list_head;
92+
93+
/* Deferred mr list */
94+
struct list_head mr_gc_list_head;
95+
struct workqueue_struct *wq_gc;
96+
struct delayed_work gc_dwork_ent;
97+
9098
struct mutex lock;
99+
100+
atomic_t shutdown;
91101
};
92102

93103
struct mlx5_vdpa_dev {

drivers/vdpa/mlx5/core/mr.c

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -653,14 +653,50 @@ static void _mlx5_vdpa_destroy_mr(struct mlx5_vdpa_dev *mvdev, struct mlx5_vdpa_
653653
kfree(mr);
654654
}
655655

656+
/* There can be multiple .set_map() operations in quick succession.
657+
* This large delay is a simple way to prevent the MR cleanup from blocking
658+
* .set_map() MR creation in this scenario.
659+
*/
660+
#define MLX5_VDPA_MR_GC_TRIGGER_MS 2000
661+
662+
static void mlx5_vdpa_mr_gc_handler(struct work_struct *work)
663+
{
664+
struct mlx5_vdpa_mr_resources *mres;
665+
struct mlx5_vdpa_mr *mr, *tmp;
666+
struct mlx5_vdpa_dev *mvdev;
667+
668+
mres = container_of(work, struct mlx5_vdpa_mr_resources, gc_dwork_ent.work);
669+
670+
if (atomic_read(&mres->shutdown)) {
671+
mutex_lock(&mres->lock);
672+
} else if (!mutex_trylock(&mres->lock)) {
673+
queue_delayed_work(mres->wq_gc, &mres->gc_dwork_ent,
674+
msecs_to_jiffies(MLX5_VDPA_MR_GC_TRIGGER_MS));
675+
return;
676+
}
677+
678+
mvdev = container_of(mres, struct mlx5_vdpa_dev, mres);
679+
680+
list_for_each_entry_safe(mr, tmp, &mres->mr_gc_list_head, mr_list) {
681+
_mlx5_vdpa_destroy_mr(mvdev, mr);
682+
}
683+
684+
mutex_unlock(&mres->lock);
685+
}
686+
656687
static void _mlx5_vdpa_put_mr(struct mlx5_vdpa_dev *mvdev,
657688
struct mlx5_vdpa_mr *mr)
658689
{
690+
struct mlx5_vdpa_mr_resources *mres = &mvdev->mres;
691+
659692
if (!mr)
660693
return;
661694

662-
if (refcount_dec_and_test(&mr->refcount))
663-
_mlx5_vdpa_destroy_mr(mvdev, mr);
695+
if (refcount_dec_and_test(&mr->refcount)) {
696+
list_move_tail(&mr->mr_list, &mres->mr_gc_list_head);
697+
queue_delayed_work(mres->wq_gc, &mres->gc_dwork_ent,
698+
msecs_to_jiffies(MLX5_VDPA_MR_GC_TRIGGER_MS));
699+
}
664700
}
665701

666702
void mlx5_vdpa_put_mr(struct mlx5_vdpa_dev *mvdev,
@@ -851,15 +887,28 @@ int mlx5_vdpa_init_mr_resources(struct mlx5_vdpa_dev *mvdev)
851887
{
852888
struct mlx5_vdpa_mr_resources *mres = &mvdev->mres;
853889

854-
INIT_LIST_HEAD(&mres->mr_list_head);
890+
mres->wq_gc = create_singlethread_workqueue("mlx5_vdpa_mr_gc");
891+
if (!mres->wq_gc)
892+
return -ENOMEM;
893+
894+
INIT_DELAYED_WORK(&mres->gc_dwork_ent, mlx5_vdpa_mr_gc_handler);
895+
855896
mutex_init(&mres->lock);
856897

898+
INIT_LIST_HEAD(&mres->mr_list_head);
899+
INIT_LIST_HEAD(&mres->mr_gc_list_head);
900+
857901
return 0;
858902
}
859903

860904
void mlx5_vdpa_destroy_mr_resources(struct mlx5_vdpa_dev *mvdev)
861905
{
862906
struct mlx5_vdpa_mr_resources *mres = &mvdev->mres;
863907

908+
atomic_set(&mres->shutdown, 1);
909+
910+
flush_delayed_work(&mres->gc_dwork_ent);
911+
destroy_workqueue(mres->wq_gc);
912+
mres->wq_gc = NULL;
864913
mutex_destroy(&mres->lock);
865914
}

drivers/vdpa/mlx5/net/mlx5_vnet.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3435,6 +3435,8 @@ static void mlx5_vdpa_free(struct vdpa_device *vdev)
34353435
free_fixed_resources(ndev);
34363436
mlx5_vdpa_clean_mrs(mvdev);
34373437
mlx5_vdpa_destroy_mr_resources(&ndev->mvdev);
3438+
mlx5_cmd_cleanup_async_ctx(&mvdev->async_ctx);
3439+
34383440
if (!is_zero_ether_addr(ndev->config.mac)) {
34393441
pfmdev = pci_get_drvdata(pci_physfn(mvdev->mdev->pdev));
34403442
mlx5_mpfs_del_mac(pfmdev, ndev->config.mac);
@@ -4042,8 +4044,6 @@ static void mlx5_vdpa_dev_del(struct vdpa_mgmt_dev *v_mdev, struct vdpa_device *
40424044
mvdev->wq = NULL;
40434045
destroy_workqueue(wq);
40444046
mgtdev->ndev = NULL;
4045-
4046-
mlx5_cmd_cleanup_async_ctx(&mvdev->async_ctx);
40474047
}
40484048

40494049
static int mlx5_vdpa_set_attr(struct vdpa_mgmt_dev *v_mdev, struct vdpa_device *dev,

0 commit comments

Comments
 (0)