Skip to content

Commit 6be60ae

Browse files
matanb10dledford
authored andcommitted
IB/core: Add idr based standard types
This patch adds the standard idr based types. These types are used in downstream patches in order to initialize, destroy and lookup IB standard objects which are based on idr objects. An idr object requires filling out several parameters. Its op pointer should point to uverbs_idr_ops and its size should be at least the size of ib_uobject. We add a macro to make the type declaration easier. Signed-off-by: Matan Barak <matanb@mellanox.com> Reviewed-by: Yishai Hadas <yishaih@mellanox.com> Reviewed-by: Sean Hefty <sean.hefty@intel.com> Signed-off-by: Doug Ledford <dledford@redhat.com>
1 parent 3832125 commit 6be60ae

7 files changed

Lines changed: 329 additions & 10 deletions

File tree

‎drivers/infiniband/core/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ ib_umad-y := user_mad.o
3030
ib_ucm-y := ucm.o
3131

3232
ib_uverbs-y := uverbs_main.o uverbs_cmd.o uverbs_marshall.o \
33-
rdma_core.o
33+
rdma_core.o uverbs_std_types.o

‎drivers/infiniband/core/uverbs.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,12 @@ void ib_uverbs_wq_event_handler(struct ib_event *event, void *context_ptr);
201201
void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr);
202202
void ib_uverbs_event_handler(struct ib_event_handler *handler,
203203
struct ib_event *event);
204-
void ib_uverbs_dealloc_xrcd(struct ib_uverbs_device *dev, struct ib_xrcd *xrcd);
204+
int ib_uverbs_dealloc_xrcd(struct ib_uverbs_device *dev, struct ib_xrcd *xrcd,
205+
enum rdma_remove_reason why);
205206

206207
int uverbs_dealloc_mw(struct ib_mw *mw);
208+
void ib_uverbs_detach_umcast(struct ib_qp *qp,
209+
struct ib_uqp_object *uobj);
207210

208211
struct ib_uverbs_flow_spec {
209212
union {

‎drivers/infiniband/core/uverbs_cmd.c‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -958,19 +958,25 @@ ssize_t ib_uverbs_close_xrcd(struct ib_uverbs_file *file,
958958
return ret;
959959
}
960960

961-
void ib_uverbs_dealloc_xrcd(struct ib_uverbs_device *dev,
962-
struct ib_xrcd *xrcd)
961+
int ib_uverbs_dealloc_xrcd(struct ib_uverbs_device *dev,
962+
struct ib_xrcd *xrcd,
963+
enum rdma_remove_reason why)
963964
{
964965
struct inode *inode;
966+
int ret;
965967

966968
inode = xrcd->inode;
967969
if (inode && !atomic_dec_and_test(&xrcd->usecnt))
968-
return;
970+
return 0;
969971

970-
ib_dealloc_xrcd(xrcd);
972+
ret = ib_dealloc_xrcd(xrcd);
971973

972-
if (inode)
974+
if (why == RDMA_REMOVE_DESTROY && ret)
975+
atomic_inc(&xrcd->usecnt);
976+
else if (inode)
973977
xrcd_table_delete(dev, inode);
978+
979+
return ret;
974980
}
975981

976982
ssize_t ib_uverbs_reg_mr(struct ib_uverbs_file *file,

‎drivers/infiniband/core/uverbs_main.c‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ void ib_uverbs_release_uevent(struct ib_uverbs_file *file,
201201
spin_unlock_irq(&file->async_file->lock);
202202
}
203203

204-
static void ib_uverbs_detach_umcast(struct ib_qp *qp,
205-
struct ib_uqp_object *uobj)
204+
void ib_uverbs_detach_umcast(struct ib_qp *qp,
205+
struct ib_uqp_object *uobj)
206206
{
207207
struct ib_uverbs_mcast_entry *mcast, *tmp;
208208

@@ -331,7 +331,9 @@ static int ib_uverbs_cleanup_ucontext(struct ib_uverbs_file *file,
331331
container_of(uobj, struct ib_uxrcd_object, uobject);
332332

333333
idr_remove_uobj(uobj);
334-
ib_uverbs_dealloc_xrcd(file->device, xrcd);
334+
ib_uverbs_dealloc_xrcd(file->device, xrcd,
335+
file->ucontext ? RDMA_REMOVE_CLOSE :
336+
RDMA_REMOVE_DRIVER_REMOVE);
335337
kfree(uxrcd);
336338
}
337339
mutex_unlock(&file->device->xrcd_tree_mutex);
Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
/*
2+
* Copyright (c) 2017, Mellanox Technologies inc. All rights reserved.
3+
*
4+
* This software is available to you under a choice of one of two
5+
* licenses. You may choose to be licensed under the terms of the GNU
6+
* General Public License (GPL) Version 2, available from the file
7+
* COPYING in the main directory of this source tree, or the
8+
* OpenIB.org BSD license below:
9+
*
10+
* Redistribution and use in source and binary forms, with or
11+
* without modification, are permitted provided that the following
12+
* conditions are met:
13+
*
14+
* - Redistributions of source code must retain the above
15+
* copyright notice, this list of conditions and the following
16+
* disclaimer.
17+
*
18+
* - Redistributions in binary form must reproduce the above
19+
* copyright notice, this list of conditions and the following
20+
* disclaimer in the documentation and/or other materials
21+
* provided with the distribution.
22+
*
23+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30+
* SOFTWARE.
31+
*/
32+
33+
#include <rdma/uverbs_std_types.h>
34+
#include <rdma/ib_user_verbs.h>
35+
#include <rdma/ib_verbs.h>
36+
#include <linux/bug.h>
37+
#include <linux/file.h>
38+
#include "rdma_core.h"
39+
#include "uverbs.h"
40+
41+
int uverbs_free_ah(struct ib_uobject *uobject,
42+
enum rdma_remove_reason why)
43+
{
44+
return ib_destroy_ah((struct ib_ah *)uobject->object);
45+
}
46+
47+
int uverbs_free_flow(struct ib_uobject *uobject,
48+
enum rdma_remove_reason why)
49+
{
50+
return ib_destroy_flow((struct ib_flow *)uobject->object);
51+
}
52+
53+
int uverbs_free_mw(struct ib_uobject *uobject,
54+
enum rdma_remove_reason why)
55+
{
56+
return uverbs_dealloc_mw((struct ib_mw *)uobject->object);
57+
}
58+
59+
int uverbs_free_qp(struct ib_uobject *uobject,
60+
enum rdma_remove_reason why)
61+
{
62+
struct ib_qp *qp = uobject->object;
63+
struct ib_uqp_object *uqp =
64+
container_of(uobject, struct ib_uqp_object, uevent.uobject);
65+
int ret;
66+
67+
if (why == RDMA_REMOVE_DESTROY) {
68+
if (!list_empty(&uqp->mcast_list))
69+
return -EBUSY;
70+
} else if (qp == qp->real_qp) {
71+
ib_uverbs_detach_umcast(qp, uqp);
72+
}
73+
74+
ret = ib_destroy_qp(qp);
75+
if (ret && why == RDMA_REMOVE_DESTROY)
76+
return ret;
77+
78+
if (uqp->uxrcd)
79+
atomic_dec(&uqp->uxrcd->refcnt);
80+
81+
ib_uverbs_release_uevent(uobject->context->ufile, &uqp->uevent);
82+
return ret;
83+
}
84+
85+
int uverbs_free_rwq_ind_tbl(struct ib_uobject *uobject,
86+
enum rdma_remove_reason why)
87+
{
88+
struct ib_rwq_ind_table *rwq_ind_tbl = uobject->object;
89+
struct ib_wq **ind_tbl = rwq_ind_tbl->ind_tbl;
90+
int ret;
91+
92+
ret = ib_destroy_rwq_ind_table(rwq_ind_tbl);
93+
if (!ret || why != RDMA_REMOVE_DESTROY)
94+
kfree(ind_tbl);
95+
return ret;
96+
}
97+
98+
int uverbs_free_wq(struct ib_uobject *uobject,
99+
enum rdma_remove_reason why)
100+
{
101+
struct ib_wq *wq = uobject->object;
102+
struct ib_uwq_object *uwq =
103+
container_of(uobject, struct ib_uwq_object, uevent.uobject);
104+
int ret;
105+
106+
ret = ib_destroy_wq(wq);
107+
if (!ret || why != RDMA_REMOVE_DESTROY)
108+
ib_uverbs_release_uevent(uobject->context->ufile, &uwq->uevent);
109+
return ret;
110+
}
111+
112+
int uverbs_free_srq(struct ib_uobject *uobject,
113+
enum rdma_remove_reason why)
114+
{
115+
struct ib_srq *srq = uobject->object;
116+
struct ib_uevent_object *uevent =
117+
container_of(uobject, struct ib_uevent_object, uobject);
118+
enum ib_srq_type srq_type = srq->srq_type;
119+
int ret;
120+
121+
ret = ib_destroy_srq(srq);
122+
123+
if (ret && why == RDMA_REMOVE_DESTROY)
124+
return ret;
125+
126+
if (srq_type == IB_SRQT_XRC) {
127+
struct ib_usrq_object *us =
128+
container_of(uevent, struct ib_usrq_object, uevent);
129+
130+
atomic_dec(&us->uxrcd->refcnt);
131+
}
132+
133+
ib_uverbs_release_uevent(uobject->context->ufile, uevent);
134+
return ret;
135+
}
136+
137+
int uverbs_free_cq(struct ib_uobject *uobject,
138+
enum rdma_remove_reason why)
139+
{
140+
struct ib_cq *cq = uobject->object;
141+
struct ib_uverbs_event_file *ev_file = cq->cq_context;
142+
struct ib_ucq_object *ucq =
143+
container_of(uobject, struct ib_ucq_object, uobject);
144+
int ret;
145+
146+
ret = ib_destroy_cq(cq);
147+
if (!ret || why != RDMA_REMOVE_DESTROY)
148+
ib_uverbs_release_ucq(uobject->context->ufile, ev_file, ucq);
149+
return ret;
150+
}
151+
152+
int uverbs_free_mr(struct ib_uobject *uobject,
153+
enum rdma_remove_reason why)
154+
{
155+
return ib_dereg_mr((struct ib_mr *)uobject->object);
156+
}
157+
158+
int uverbs_free_xrcd(struct ib_uobject *uobject,
159+
enum rdma_remove_reason why)
160+
{
161+
struct ib_xrcd *xrcd = uobject->object;
162+
struct ib_uxrcd_object *uxrcd =
163+
container_of(uobject, struct ib_uxrcd_object, uobject);
164+
int ret;
165+
166+
mutex_lock(&uobject->context->ufile->device->xrcd_tree_mutex);
167+
if (why == RDMA_REMOVE_DESTROY && atomic_read(&uxrcd->refcnt))
168+
ret = -EBUSY;
169+
else
170+
ret = ib_uverbs_dealloc_xrcd(uobject->context->ufile->device,
171+
xrcd, why);
172+
mutex_unlock(&uobject->context->ufile->device->xrcd_tree_mutex);
173+
174+
return ret;
175+
}
176+
177+
int uverbs_free_pd(struct ib_uobject *uobject,
178+
enum rdma_remove_reason why)
179+
{
180+
struct ib_pd *pd = uobject->object;
181+
182+
if (why == RDMA_REMOVE_DESTROY && atomic_read(&pd->usecnt))
183+
return -EBUSY;
184+
185+
ib_dealloc_pd((struct ib_pd *)uobject->object);
186+
return 0;
187+
}
188+
189+
const struct uverbs_obj_idr_type uverbs_type_attrs_cq = {
190+
.type = UVERBS_TYPE_ALLOC_IDR_SZ(sizeof(struct ib_ucq_object), 0),
191+
.destroy_object = uverbs_free_cq,
192+
};
193+
194+
const struct uverbs_obj_idr_type uverbs_type_attrs_qp = {
195+
.type = UVERBS_TYPE_ALLOC_IDR_SZ(sizeof(struct ib_uqp_object), 0),
196+
.destroy_object = uverbs_free_qp,
197+
};
198+
199+
const struct uverbs_obj_idr_type uverbs_type_attrs_mw = {
200+
.type = UVERBS_TYPE_ALLOC_IDR(0),
201+
.destroy_object = uverbs_free_mw,
202+
};
203+
204+
const struct uverbs_obj_idr_type uverbs_type_attrs_mr = {
205+
/* 1 is used in order to free the MR after all the MWs */
206+
.type = UVERBS_TYPE_ALLOC_IDR(1),
207+
.destroy_object = uverbs_free_mr,
208+
};
209+
210+
const struct uverbs_obj_idr_type uverbs_type_attrs_srq = {
211+
.type = UVERBS_TYPE_ALLOC_IDR_SZ(sizeof(struct ib_usrq_object), 0),
212+
.destroy_object = uverbs_free_srq,
213+
};
214+
215+
const struct uverbs_obj_idr_type uverbs_type_attrs_ah = {
216+
.type = UVERBS_TYPE_ALLOC_IDR(0),
217+
.destroy_object = uverbs_free_ah,
218+
};
219+
220+
const struct uverbs_obj_idr_type uverbs_type_attrs_flow = {
221+
.type = UVERBS_TYPE_ALLOC_IDR(0),
222+
.destroy_object = uverbs_free_flow,
223+
};
224+
225+
const struct uverbs_obj_idr_type uverbs_type_attrs_wq = {
226+
.type = UVERBS_TYPE_ALLOC_IDR_SZ(sizeof(struct ib_uwq_object), 0),
227+
.destroy_object = uverbs_free_wq,
228+
};
229+
230+
const struct uverbs_obj_idr_type uverbs_type_attrs_rwq_ind_table = {
231+
.type = UVERBS_TYPE_ALLOC_IDR(0),
232+
.destroy_object = uverbs_free_rwq_ind_tbl,
233+
};
234+
235+
const struct uverbs_obj_idr_type uverbs_type_attrs_xrcd = {
236+
.type = UVERBS_TYPE_ALLOC_IDR_SZ(sizeof(struct ib_uxrcd_object), 0),
237+
.destroy_object = uverbs_free_xrcd,
238+
};
239+
240+
const struct uverbs_obj_idr_type uverbs_type_attrs_pd = {
241+
/* 2 is used in order to free the PD after MRs */
242+
.type = UVERBS_TYPE_ALLOC_IDR(2),
243+
.destroy_object = uverbs_free_pd,
244+
};

‎include/rdma/uverbs_std_types.h‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2017, Mellanox Technologies inc. All rights reserved.
3+
*
4+
* This software is available to you under a choice of one of two
5+
* licenses. You may choose to be licensed under the terms of the GNU
6+
* General Public License (GPL) Version 2, available from the file
7+
* COPYING in the main directory of this source tree, or the
8+
* OpenIB.org BSD license below:
9+
*
10+
* Redistribution and use in source and binary forms, with or
11+
* without modification, are permitted provided that the following
12+
* conditions are met:
13+
*
14+
* - Redistributions of source code must retain the above
15+
* copyright notice, this list of conditions and the following
16+
* disclaimer.
17+
*
18+
* - Redistributions in binary form must reproduce the above
19+
* copyright notice, this list of conditions and the following
20+
* disclaimer in the documentation and/or other materials
21+
* provided with the distribution.
22+
*
23+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30+
* SOFTWARE.
31+
*/
32+
33+
#ifndef _UVERBS_STD_TYPES__
34+
#define _UVERBS_STD_TYPES__
35+
36+
#include <rdma/uverbs_types.h>
37+
38+
extern const struct uverbs_obj_idr_type uverbs_type_attrs_cq;
39+
extern const struct uverbs_obj_idr_type uverbs_type_attrs_qp;
40+
extern const struct uverbs_obj_idr_type uverbs_type_attrs_rwq_ind_table;
41+
extern const struct uverbs_obj_idr_type uverbs_type_attrs_wq;
42+
extern const struct uverbs_obj_idr_type uverbs_type_attrs_srq;
43+
extern const struct uverbs_obj_idr_type uverbs_type_attrs_ah;
44+
extern const struct uverbs_obj_idr_type uverbs_type_attrs_flow;
45+
extern const struct uverbs_obj_idr_type uverbs_type_attrs_mr;
46+
extern const struct uverbs_obj_idr_type uverbs_type_attrs_mw;
47+
extern const struct uverbs_obj_idr_type uverbs_type_attrs_pd;
48+
extern const struct uverbs_obj_idr_type uverbs_type_attrs_xrcd;
49+
#endif
50+

‎include/rdma/uverbs_types.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,18 @@ void rdma_alloc_abort_uobject(struct ib_uobject *uobj);
129129
int __must_check rdma_remove_commit_uobject(struct ib_uobject *uobj);
130130
int rdma_alloc_commit_uobject(struct ib_uobject *uobj);
131131

132+
extern const struct uverbs_obj_type_class uverbs_idr_class;
133+
134+
#define UVERBS_BUILD_BUG_ON(cond) (sizeof(char[1 - 2 * !!(cond)]) - \
135+
sizeof(char))
136+
#define UVERBS_TYPE_ALLOC_IDR_SZ(_size, _order) \
137+
{ \
138+
.destroy_order = _order, \
139+
.type_class = &uverbs_idr_class, \
140+
.obj_size = (_size) + \
141+
UVERBS_BUILD_BUG_ON((_size) < \
142+
sizeof(struct ib_uobject)), \
143+
}
144+
#define UVERBS_TYPE_ALLOC_IDR(_order) \
145+
UVERBS_TYPE_ALLOC_IDR_SZ(sizeof(struct ib_uobject), _order)
132146
#endif

0 commit comments

Comments
 (0)