Skip to content

Commit c33e73a

Browse files
committed
IB/uverbs: Add a uobj_perform_destroy helper
This consolidates a bunch of repeated code patterns into a helper. Signed-off-by: Jason Gunthorpe <jgg@mellanox.com> Signed-off-by: Leon Romanovsky <leonro@mellanox.com>
1 parent 422e3d3 commit c33e73a

3 files changed

Lines changed: 41 additions & 62 deletions

File tree

‎drivers/infiniband/core/rdma_core.c‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,28 @@ static int uverbs_try_lock_object(struct ib_uobject *uobj, bool exclusive)
128128
return atomic_cmpxchg(&uobj->usecnt, 0, -1) == 0 ? 0 : -EBUSY;
129129
}
130130

131+
/*
132+
* Does both rdma_lookup_get_uobject() and rdma_remove_commit_uobject(), then
133+
* returns success_res on success (negative errno on failure). For use by
134+
* callers that do not need the uobj.
135+
*/
136+
int __uobj_perform_destroy(const struct uverbs_obj_type *type, int id,
137+
struct ib_uverbs_file *ufile, int success_res)
138+
{
139+
struct ib_uobject *uobj;
140+
int ret;
141+
142+
uobj = rdma_lookup_get_uobject(type, ufile->ucontext, id, true);
143+
if (IS_ERR(uobj))
144+
return PTR_ERR(uobj);
145+
146+
ret = rdma_remove_commit_uobject(uobj);
147+
if (ret)
148+
return ret;
149+
150+
return success_res;
151+
}
152+
131153
static struct ib_uobject *alloc_uobj(struct ib_ucontext *context,
132154
const struct uverbs_obj_type *type)
133155
{

‎drivers/infiniband/core/uverbs_cmd.c‎

Lines changed: 14 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -367,20 +367,12 @@ ssize_t ib_uverbs_dealloc_pd(struct ib_uverbs_file *file,
367367
int in_len, int out_len)
368368
{
369369
struct ib_uverbs_dealloc_pd cmd;
370-
struct ib_uobject *uobj;
371-
int ret;
372370

373371
if (copy_from_user(&cmd, buf, sizeof cmd))
374372
return -EFAULT;
375373

376-
uobj = uobj_get_write(UVERBS_OBJECT_PD, cmd.pd_handle,
377-
file->ucontext);
378-
if (IS_ERR(uobj))
379-
return PTR_ERR(uobj);
380-
381-
ret = uobj_remove_commit(uobj);
382-
383-
return ret ?: in_len;
374+
return uobj_perform_destroy(UVERBS_OBJECT_PD, cmd.pd_handle, file,
375+
in_len);
384376
}
385377

386378
struct xrcd_table_entry {
@@ -597,19 +589,12 @@ ssize_t ib_uverbs_close_xrcd(struct ib_uverbs_file *file,
597589
int out_len)
598590
{
599591
struct ib_uverbs_close_xrcd cmd;
600-
struct ib_uobject *uobj;
601-
int ret = 0;
602592

603593
if (copy_from_user(&cmd, buf, sizeof cmd))
604594
return -EFAULT;
605595

606-
uobj = uobj_get_write(UVERBS_OBJECT_XRCD, cmd.xrcd_handle,
607-
file->ucontext);
608-
if (IS_ERR(uobj))
609-
return PTR_ERR(uobj);
610-
611-
ret = uobj_remove_commit(uobj);
612-
return ret ?: in_len;
596+
return uobj_perform_destroy(UVERBS_OBJECT_XRCD, cmd.xrcd_handle, file,
597+
in_len);
613598
}
614599

615600
int ib_uverbs_dealloc_xrcd(struct ib_uobject *uobject,
@@ -829,20 +814,12 @@ ssize_t ib_uverbs_dereg_mr(struct ib_uverbs_file *file,
829814
int out_len)
830815
{
831816
struct ib_uverbs_dereg_mr cmd;
832-
struct ib_uobject *uobj;
833-
int ret = -EINVAL;
834817

835818
if (copy_from_user(&cmd, buf, sizeof cmd))
836819
return -EFAULT;
837820

838-
uobj = uobj_get_write(UVERBS_OBJECT_MR, cmd.mr_handle,
839-
file->ucontext);
840-
if (IS_ERR(uobj))
841-
return PTR_ERR(uobj);
842-
843-
ret = uobj_remove_commit(uobj);
844-
845-
return ret ?: in_len;
821+
return uobj_perform_destroy(UVERBS_OBJECT_MR, cmd.mr_handle, file,
822+
in_len);
846823
}
847824

848825
ssize_t ib_uverbs_alloc_mw(struct ib_uverbs_file *file,
@@ -921,19 +898,12 @@ ssize_t ib_uverbs_dealloc_mw(struct ib_uverbs_file *file,
921898
int out_len)
922899
{
923900
struct ib_uverbs_dealloc_mw cmd;
924-
struct ib_uobject *uobj;
925-
int ret = -EINVAL;
926901

927902
if (copy_from_user(&cmd, buf, sizeof(cmd)))
928903
return -EFAULT;
929904

930-
uobj = uobj_get_write(UVERBS_OBJECT_MW, cmd.mw_handle,
931-
file->ucontext);
932-
if (IS_ERR(uobj))
933-
return PTR_ERR(uobj);
934-
935-
ret = uobj_remove_commit(uobj);
936-
return ret ?: in_len;
905+
return uobj_perform_destroy(UVERBS_OBJECT_MW, cmd.mw_handle, file,
906+
in_len);
937907
}
938908

939909
ssize_t ib_uverbs_create_comp_channel(struct ib_uverbs_file *file,
@@ -2641,19 +2611,12 @@ ssize_t ib_uverbs_destroy_ah(struct ib_uverbs_file *file,
26412611
const char __user *buf, int in_len, int out_len)
26422612
{
26432613
struct ib_uverbs_destroy_ah cmd;
2644-
struct ib_uobject *uobj;
2645-
int ret;
26462614

26472615
if (copy_from_user(&cmd, buf, sizeof cmd))
26482616
return -EFAULT;
26492617

2650-
uobj = uobj_get_write(UVERBS_OBJECT_AH, cmd.ah_handle,
2651-
file->ucontext);
2652-
if (IS_ERR(uobj))
2653-
return PTR_ERR(uobj);
2654-
2655-
ret = uobj_remove_commit(uobj);
2656-
return ret ?: in_len;
2618+
return uobj_perform_destroy(UVERBS_OBJECT_AH, cmd.ah_handle, file,
2619+
in_len);
26572620
}
26582621

26592622
ssize_t ib_uverbs_attach_mcast(struct ib_uverbs_file *file,
@@ -3445,7 +3408,6 @@ int ib_uverbs_ex_destroy_rwq_ind_table(struct ib_uverbs_file *file,
34453408
struct ib_udata *uhw)
34463409
{
34473410
struct ib_uverbs_ex_destroy_rwq_ind_table cmd = {};
3448-
struct ib_uobject *uobj;
34493411
int ret;
34503412
size_t required_cmd_sz;
34513413

@@ -3466,12 +3428,8 @@ int ib_uverbs_ex_destroy_rwq_ind_table(struct ib_uverbs_file *file,
34663428
if (cmd.comp_mask)
34673429
return -EOPNOTSUPP;
34683430

3469-
uobj = uobj_get_write(UVERBS_OBJECT_RWQ_IND_TBL, cmd.ind_tbl_handle,
3470-
file->ucontext);
3471-
if (IS_ERR(uobj))
3472-
return PTR_ERR(uobj);
3473-
3474-
return uobj_remove_commit(uobj);
3431+
return uobj_perform_destroy(UVERBS_OBJECT_RWQ_IND_TBL,
3432+
cmd.ind_tbl_handle, file, 0);
34753433
}
34763434

34773435
int ib_uverbs_ex_create_flow(struct ib_uverbs_file *file,
@@ -3658,7 +3616,6 @@ int ib_uverbs_ex_destroy_flow(struct ib_uverbs_file *file,
36583616
struct ib_udata *uhw)
36593617
{
36603618
struct ib_uverbs_destroy_flow cmd;
3661-
struct ib_uobject *uobj;
36623619
int ret;
36633620

36643621
if (ucore->inlen < sizeof(cmd))
@@ -3671,13 +3628,8 @@ int ib_uverbs_ex_destroy_flow(struct ib_uverbs_file *file,
36713628
if (cmd.comp_mask)
36723629
return -EINVAL;
36733630

3674-
uobj = uobj_get_write(UVERBS_OBJECT_FLOW, cmd.flow_handle,
3675-
file->ucontext);
3676-
if (IS_ERR(uobj))
3677-
return PTR_ERR(uobj);
3678-
3679-
ret = uobj_remove_commit(uobj);
3680-
return ret;
3631+
return uobj_perform_destroy(UVERBS_OBJECT_FLOW, cmd.flow_handle, file,
3632+
0);
36813633
}
36823634

36833635
static int __uverbs_create_xsrq(struct ib_uverbs_file *file,

‎include/rdma/uverbs_std_types.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ static inline struct ib_uobject *__uobj_get(const struct uverbs_obj_type *type,
7171
#define uobj_get_write(_type, _id, _ucontext) \
7272
__uobj_get(uobj_get_type(_type), true, _ucontext, _id)
7373

74+
int __uobj_perform_destroy(const struct uverbs_obj_type *type, int id,
75+
struct ib_uverbs_file *ufile, int success_res);
76+
#define uobj_perform_destroy(_type, _id, _ufile, _success_res) \
77+
__uobj_perform_destroy(uobj_get_type(_type), _id, _ufile, _success_res)
78+
7479
static inline void uobj_put_read(struct ib_uobject *uobj)
7580
{
7681
rdma_lookup_put_uobject(uobj, false);

0 commit comments

Comments
 (0)