Skip to content

Commit d18bb3e

Browse files
Leon Romanovskyjgunthorpe
authored andcommitted
RDMA: Clean MW allocation and free flows
Move allocation and destruction of memory windows under ib_core responsibility and clean drivers to ensure that no updates to MW ib_core structures are done in driver layer. Link: https://lore.kernel.org/r/20200902081623.746359-2-leon@kernel.org Signed-off-by: Leon Romanovsky <leonro@mellanox.com> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
1 parent b5de0c6 commit d18bb3e

16 files changed

Lines changed: 78 additions & 106 deletions

File tree

‎drivers/infiniband/core/device.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2695,6 +2695,7 @@ void ib_set_device_ops(struct ib_device *dev, const struct ib_device_ops *ops)
26952695
SET_OBJ_SIZE(dev_ops, ib_ah);
26962696
SET_OBJ_SIZE(dev_ops, ib_counters);
26972697
SET_OBJ_SIZE(dev_ops, ib_cq);
2698+
SET_OBJ_SIZE(dev_ops, ib_mw);
26982699
SET_OBJ_SIZE(dev_ops, ib_pd);
26992700
SET_OBJ_SIZE(dev_ops, ib_srq);
27002701
SET_OBJ_SIZE(dev_ops, ib_ucontext);

‎drivers/infiniband/core/uverbs_cmd.c‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ static int ib_uverbs_dereg_mr(struct uverbs_attr_bundle *attrs)
858858
static int ib_uverbs_alloc_mw(struct uverbs_attr_bundle *attrs)
859859
{
860860
struct ib_uverbs_alloc_mw cmd;
861-
struct ib_uverbs_alloc_mw_resp resp;
861+
struct ib_uverbs_alloc_mw_resp resp = {};
862862
struct ib_uobject *uobj;
863863
struct ib_pd *pd;
864864
struct ib_mw *mw;
@@ -884,15 +884,21 @@ static int ib_uverbs_alloc_mw(struct uverbs_attr_bundle *attrs)
884884
goto err_put;
885885
}
886886

887-
mw = pd->device->ops.alloc_mw(pd, cmd.mw_type, &attrs->driver_udata);
888-
if (IS_ERR(mw)) {
889-
ret = PTR_ERR(mw);
887+
mw = rdma_zalloc_drv_obj(ib_dev, ib_mw);
888+
if (!mw) {
889+
ret = -ENOMEM;
890890
goto err_put;
891891
}
892892

893-
mw->device = pd->device;
894-
mw->pd = pd;
893+
mw->device = ib_dev;
894+
mw->pd = pd;
895895
mw->uobject = uobj;
896+
mw->type = cmd.mw_type;
897+
898+
ret = pd->device->ops.alloc_mw(mw, &attrs->driver_udata);
899+
if (ret)
900+
goto err_alloc;
901+
896902
atomic_inc(&pd->usecnt);
897903

898904
uobj->object = mw;
@@ -903,6 +909,8 @@ static int ib_uverbs_alloc_mw(struct uverbs_attr_bundle *attrs)
903909
resp.mw_handle = uobj->id;
904910
return uverbs_response(attrs, &resp, sizeof(resp));
905911

912+
err_alloc:
913+
kfree(mw);
906914
err_put:
907915
uobj_put_obj_read(pd);
908916
err_free:

‎drivers/infiniband/core/uverbs_main.c‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,11 @@ int uverbs_dealloc_mw(struct ib_mw *mw)
108108
int ret;
109109

110110
ret = mw->device->ops.dealloc_mw(mw);
111-
if (!ret)
112-
atomic_dec(&pd->usecnt);
111+
if (ret)
112+
return ret;
113+
114+
atomic_dec(&pd->usecnt);
115+
kfree(mw);
113116
return ret;
114117
}
115118

‎drivers/infiniband/hw/cxgb4/iw_cxgb4.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -985,8 +985,7 @@ int c4iw_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg, int sg_nents,
985985
unsigned int *sg_offset);
986986
int c4iw_dealloc_mw(struct ib_mw *mw);
987987
void c4iw_dealloc(struct uld_ctx *ctx);
988-
struct ib_mw *c4iw_alloc_mw(struct ib_pd *pd, enum ib_mw_type type,
989-
struct ib_udata *udata);
988+
int c4iw_alloc_mw(struct ib_mw *mw, struct ib_udata *udata);
990989
struct ib_mr *c4iw_reg_user_mr(struct ib_pd *pd, u64 start,
991990
u64 length, u64 virt, int acc,
992991
struct ib_udata *udata);

‎drivers/infiniband/hw/cxgb4/mem.c‎

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -611,30 +611,23 @@ struct ib_mr *c4iw_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
611611
return ERR_PTR(err);
612612
}
613613

614-
struct ib_mw *c4iw_alloc_mw(struct ib_pd *pd, enum ib_mw_type type,
615-
struct ib_udata *udata)
614+
int c4iw_alloc_mw(struct ib_mw *ibmw, struct ib_udata *udata)
616615
{
616+
struct c4iw_mw *mhp = to_c4iw_mw(ibmw);
617617
struct c4iw_dev *rhp;
618618
struct c4iw_pd *php;
619-
struct c4iw_mw *mhp;
620619
u32 mmid;
621620
u32 stag = 0;
622621
int ret;
623622

624-
if (type != IB_MW_TYPE_1)
625-
return ERR_PTR(-EINVAL);
623+
if (ibmw->type != IB_MW_TYPE_1)
624+
return -EINVAL;
626625

627-
php = to_c4iw_pd(pd);
626+
php = to_c4iw_pd(ibmw->pd);
628627
rhp = php->rhp;
629-
mhp = kzalloc(sizeof(*mhp), GFP_KERNEL);
630-
if (!mhp)
631-
return ERR_PTR(-ENOMEM);
632-
633628
mhp->wr_waitp = c4iw_alloc_wr_wait(GFP_KERNEL);
634-
if (!mhp->wr_waitp) {
635-
ret = -ENOMEM;
636-
goto free_mhp;
637-
}
629+
if (!mhp->wr_waitp)
630+
return -ENOMEM;
638631

639632
mhp->dereg_skb = alloc_skb(SGE_MAX_WR_LEN, GFP_KERNEL);
640633
if (!mhp->dereg_skb) {
@@ -645,18 +638,19 @@ struct ib_mw *c4iw_alloc_mw(struct ib_pd *pd, enum ib_mw_type type,
645638
ret = allocate_window(&rhp->rdev, &stag, php->pdid, mhp->wr_waitp);
646639
if (ret)
647640
goto free_skb;
641+
648642
mhp->rhp = rhp;
649643
mhp->attr.pdid = php->pdid;
650644
mhp->attr.type = FW_RI_STAG_MW;
651645
mhp->attr.stag = stag;
652646
mmid = (stag) >> 8;
653-
mhp->ibmw.rkey = stag;
647+
ibmw->rkey = stag;
654648
if (xa_insert_irq(&rhp->mrs, mmid, mhp, GFP_KERNEL)) {
655649
ret = -ENOMEM;
656650
goto dealloc_win;
657651
}
658652
pr_debug("mmid 0x%x mhp %p stag 0x%x\n", mmid, mhp, stag);
659-
return &(mhp->ibmw);
653+
return 0;
660654

661655
dealloc_win:
662656
deallocate_window(&rhp->rdev, mhp->attr.stag, mhp->dereg_skb,
@@ -665,9 +659,7 @@ struct ib_mw *c4iw_alloc_mw(struct ib_pd *pd, enum ib_mw_type type,
665659
kfree_skb(mhp->dereg_skb);
666660
free_wr_wait:
667661
c4iw_put_wr_wait(mhp->wr_waitp);
668-
free_mhp:
669-
kfree(mhp);
670-
return ERR_PTR(ret);
662+
return ret;
671663
}
672664

673665
int c4iw_dealloc_mw(struct ib_mw *mw)
@@ -684,8 +676,6 @@ int c4iw_dealloc_mw(struct ib_mw *mw)
684676
mhp->wr_waitp);
685677
kfree_skb(mhp->dereg_skb);
686678
c4iw_put_wr_wait(mhp->wr_waitp);
687-
pr_debug("ib_mw %p mmid 0x%x ptr %p\n", mw, mmid, mhp);
688-
kfree(mhp);
689679
return 0;
690680
}
691681

‎drivers/infiniband/hw/cxgb4/provider.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,10 @@ static const struct ib_device_ops c4iw_dev_ops = {
498498
.query_qp = c4iw_ib_query_qp,
499499
.reg_user_mr = c4iw_reg_user_mr,
500500
.req_notify_cq = c4iw_arm_cq,
501-
INIT_RDMA_OBJ_SIZE(ib_pd, c4iw_pd, ibpd),
501+
502502
INIT_RDMA_OBJ_SIZE(ib_cq, c4iw_cq, ibcq),
503+
INIT_RDMA_OBJ_SIZE(ib_mw, c4iw_mw, ibmw),
504+
INIT_RDMA_OBJ_SIZE(ib_pd, c4iw_pd, ibpd),
503505
INIT_RDMA_OBJ_SIZE(ib_srq, c4iw_srq, ibsrq),
504506
INIT_RDMA_OBJ_SIZE(ib_ucontext, c4iw_ucontext, ibucontext),
505507
};

‎drivers/infiniband/hw/hns/hns_roce_device.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,8 +1204,7 @@ int hns_roce_hw_destroy_mpt(struct hns_roce_dev *hr_dev,
12041204
unsigned long mpt_index);
12051205
unsigned long key_to_hw_index(u32 key);
12061206

1207-
struct ib_mw *hns_roce_alloc_mw(struct ib_pd *pd, enum ib_mw_type,
1208-
struct ib_udata *udata);
1207+
int hns_roce_alloc_mw(struct ib_mw *mw, struct ib_udata *udata);
12091208
int hns_roce_dealloc_mw(struct ib_mw *ibmw);
12101209

12111210
void hns_roce_buf_free(struct hns_roce_dev *hr_dev, struct hns_roce_buf *buf);

‎drivers/infiniband/hw/hns/hns_roce_main.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,8 @@ static const struct ib_device_ops hns_roce_dev_mr_ops = {
454454
static const struct ib_device_ops hns_roce_dev_mw_ops = {
455455
.alloc_mw = hns_roce_alloc_mw,
456456
.dealloc_mw = hns_roce_dealloc_mw,
457+
458+
INIT_RDMA_OBJ_SIZE(ib_mw, hns_roce_mw, ibmw),
457459
};
458460

459461
static const struct ib_device_ops hns_roce_dev_frmr_ops = {

‎drivers/infiniband/hw/hns/hns_roce_mr.c‎

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -589,28 +589,22 @@ static int hns_roce_mw_enable(struct hns_roce_dev *hr_dev,
589589
return ret;
590590
}
591591

592-
struct ib_mw *hns_roce_alloc_mw(struct ib_pd *ib_pd, enum ib_mw_type type,
593-
struct ib_udata *udata)
592+
int hns_roce_alloc_mw(struct ib_mw *ibmw, struct ib_udata *udata)
594593
{
595-
struct hns_roce_dev *hr_dev = to_hr_dev(ib_pd->device);
596-
struct hns_roce_mw *mw;
594+
struct hns_roce_dev *hr_dev = to_hr_dev(ibmw->device);
595+
struct hns_roce_mw *mw = to_hr_mw(ibmw);
597596
unsigned long index = 0;
598597
int ret;
599598

600-
mw = kmalloc(sizeof(*mw), GFP_KERNEL);
601-
if (!mw)
602-
return ERR_PTR(-ENOMEM);
603-
604599
/* Allocate a key for mw from bitmap */
605600
ret = hns_roce_bitmap_alloc(&hr_dev->mr_table.mtpt_bitmap, &index);
606601
if (ret)
607-
goto err_bitmap;
602+
return ret;
608603

609604
mw->rkey = hw_index_to_key(index);
610605

611-
mw->ibmw.rkey = mw->rkey;
612-
mw->ibmw.type = type;
613-
mw->pdn = to_hr_pd(ib_pd)->pdn;
606+
ibmw->rkey = mw->rkey;
607+
mw->pdn = to_hr_pd(ibmw->pd)->pdn;
614608
mw->pbl_hop_num = hr_dev->caps.pbl_hop_num;
615609
mw->pbl_ba_pg_sz = hr_dev->caps.pbl_ba_pg_sz;
616610
mw->pbl_buf_pg_sz = hr_dev->caps.pbl_buf_pg_sz;
@@ -619,15 +613,11 @@ struct ib_mw *hns_roce_alloc_mw(struct ib_pd *ib_pd, enum ib_mw_type type,
619613
if (ret)
620614
goto err_mw;
621615

622-
return &mw->ibmw;
616+
return 0;
623617

624618
err_mw:
625619
hns_roce_mw_free(hr_dev, mw);
626-
627-
err_bitmap:
628-
kfree(mw);
629-
630-
return ERR_PTR(ret);
620+
return ret;
631621
}
632622

633623
int hns_roce_dealloc_mw(struct ib_mw *ibmw)
@@ -636,8 +626,6 @@ int hns_roce_dealloc_mw(struct ib_mw *ibmw)
636626
struct hns_roce_mw *mw = to_hr_mw(ibmw);
637627

638628
hns_roce_mw_free(hr_dev, mw);
639-
kfree(mw);
640-
641629
return 0;
642630
}
643631

‎drivers/infiniband/hw/mlx4/main.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2582,6 +2582,8 @@ static const struct ib_device_ops mlx4_ib_dev_wq_ops = {
25822582
static const struct ib_device_ops mlx4_ib_dev_mw_ops = {
25832583
.alloc_mw = mlx4_ib_alloc_mw,
25842584
.dealloc_mw = mlx4_ib_dealloc_mw,
2585+
2586+
INIT_RDMA_OBJ_SIZE(ib_mw, mlx4_ib_mw, ibmw),
25852587
};
25862588

25872589
static const struct ib_device_ops mlx4_ib_dev_xrc_ops = {

0 commit comments

Comments
 (0)