2 * Copyright (c) 2017, Mellanox Technologies inc. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
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/restrack.h>
39 #include "rdma_core.h"
42 static int uverbs_free_ah(struct ib_uobject *uobject,
43 enum rdma_remove_reason why,
44 struct uverbs_attr_bundle *attrs)
46 return rdma_destroy_ah_user((struct ib_ah *)uobject->object,
47 RDMA_DESTROY_AH_SLEEPABLE,
48 &attrs->driver_udata);
51 static int uverbs_free_flow(struct ib_uobject *uobject,
52 enum rdma_remove_reason why,
53 struct uverbs_attr_bundle *attrs)
55 struct ib_flow *flow = (struct ib_flow *)uobject->object;
56 struct ib_uflow_object *uflow =
57 container_of(uobject, struct ib_uflow_object, uobject);
58 struct ib_qp *qp = flow->qp;
61 ret = flow->device->ops.destroy_flow(flow);
64 atomic_dec(&qp->usecnt);
65 ib_uverbs_flow_resources_free(uflow->resources);
71 static int uverbs_free_mw(struct ib_uobject *uobject,
72 enum rdma_remove_reason why,
73 struct uverbs_attr_bundle *attrs)
75 return uverbs_dealloc_mw((struct ib_mw *)uobject->object);
78 static int uverbs_free_rwq_ind_tbl(struct ib_uobject *uobject,
79 enum rdma_remove_reason why,
80 struct uverbs_attr_bundle *attrs)
82 struct ib_rwq_ind_table *rwq_ind_tbl = uobject->object;
83 struct ib_wq **ind_tbl = rwq_ind_tbl->ind_tbl;
84 u32 table_size = (1 << rwq_ind_tbl->log_ind_tbl_size);
87 if (atomic_read(&rwq_ind_tbl->usecnt))
90 ret = rwq_ind_tbl->device->ops.destroy_rwq_ind_table(rwq_ind_tbl);
91 if (ib_is_destroy_retryable(ret, why, uobject))
94 for (i = 0; i < table_size; i++)
95 atomic_dec(&ind_tbl[i]->usecnt);
102 static int uverbs_free_xrcd(struct ib_uobject *uobject,
103 enum rdma_remove_reason why,
104 struct uverbs_attr_bundle *attrs)
106 struct ib_xrcd *xrcd = uobject->object;
107 struct ib_uxrcd_object *uxrcd =
108 container_of(uobject, struct ib_uxrcd_object, uobject);
111 ret = ib_destroy_usecnt(&uxrcd->refcnt, why, uobject);
115 mutex_lock(&attrs->ufile->device->xrcd_tree_mutex);
116 ret = ib_uverbs_dealloc_xrcd(uobject, xrcd, why, attrs);
117 mutex_unlock(&attrs->ufile->device->xrcd_tree_mutex);
122 static int uverbs_free_pd(struct ib_uobject *uobject,
123 enum rdma_remove_reason why,
124 struct uverbs_attr_bundle *attrs)
126 struct ib_pd *pd = uobject->object;
129 ret = ib_destroy_usecnt(&pd->usecnt, why, uobject);
133 return ib_dealloc_pd_user(pd, &attrs->driver_udata);
136 void ib_uverbs_free_event_queue(struct ib_uverbs_event_queue *event_queue)
138 struct ib_uverbs_event *entry, *tmp;
140 spin_lock_irq(&event_queue->lock);
142 * The user must ensure that no new items are added to the event_list
143 * once is_closed is set.
145 event_queue->is_closed = 1;
146 spin_unlock_irq(&event_queue->lock);
147 wake_up_interruptible(&event_queue->poll_wait);
148 kill_fasync(&event_queue->async_queue, SIGIO, POLL_IN);
150 spin_lock_irq(&event_queue->lock);
151 list_for_each_entry_safe(entry, tmp, &event_queue->event_list, list) {
153 list_del(&entry->obj_list);
154 list_del(&entry->list);
157 spin_unlock_irq(&event_queue->lock);
161 uverbs_completion_event_file_destroy_uobj(struct ib_uobject *uobj,
162 enum rdma_remove_reason why)
164 struct ib_uverbs_completion_event_file *file =
165 container_of(uobj, struct ib_uverbs_completion_event_file,
168 ib_uverbs_free_event_queue(&file->ev_queue);
172 int uverbs_destroy_def_handler(struct uverbs_attr_bundle *attrs)
176 EXPORT_SYMBOL(uverbs_destroy_def_handler);
178 DECLARE_UVERBS_NAMED_OBJECT(
179 UVERBS_OBJECT_COMP_CHANNEL,
180 UVERBS_TYPE_ALLOC_FD(sizeof(struct ib_uverbs_completion_event_file),
181 uverbs_completion_event_file_destroy_uobj,
186 DECLARE_UVERBS_NAMED_METHOD_DESTROY(
187 UVERBS_METHOD_MW_DESTROY,
188 UVERBS_ATTR_IDR(UVERBS_ATTR_DESTROY_MW_HANDLE,
190 UVERBS_ACCESS_DESTROY,
193 DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_MW,
194 UVERBS_TYPE_ALLOC_IDR(uverbs_free_mw),
195 &UVERBS_METHOD(UVERBS_METHOD_MW_DESTROY));
197 DECLARE_UVERBS_NAMED_METHOD_DESTROY(
198 UVERBS_METHOD_AH_DESTROY,
199 UVERBS_ATTR_IDR(UVERBS_ATTR_DESTROY_AH_HANDLE,
201 UVERBS_ACCESS_DESTROY,
204 DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_AH,
205 UVERBS_TYPE_ALLOC_IDR(uverbs_free_ah),
206 &UVERBS_METHOD(UVERBS_METHOD_AH_DESTROY));
208 DECLARE_UVERBS_NAMED_METHOD_DESTROY(
209 UVERBS_METHOD_FLOW_DESTROY,
210 UVERBS_ATTR_IDR(UVERBS_ATTR_DESTROY_FLOW_HANDLE,
212 UVERBS_ACCESS_DESTROY,
215 DECLARE_UVERBS_NAMED_OBJECT(
217 UVERBS_TYPE_ALLOC_IDR_SZ(sizeof(struct ib_uflow_object),
219 &UVERBS_METHOD(UVERBS_METHOD_FLOW_DESTROY));
221 DECLARE_UVERBS_NAMED_METHOD_DESTROY(
222 UVERBS_METHOD_RWQ_IND_TBL_DESTROY,
223 UVERBS_ATTR_IDR(UVERBS_ATTR_DESTROY_RWQ_IND_TBL_HANDLE,
224 UVERBS_OBJECT_RWQ_IND_TBL,
225 UVERBS_ACCESS_DESTROY,
228 DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_RWQ_IND_TBL,
229 UVERBS_TYPE_ALLOC_IDR(uverbs_free_rwq_ind_tbl),
230 &UVERBS_METHOD(UVERBS_METHOD_RWQ_IND_TBL_DESTROY));
232 DECLARE_UVERBS_NAMED_METHOD_DESTROY(
233 UVERBS_METHOD_XRCD_DESTROY,
234 UVERBS_ATTR_IDR(UVERBS_ATTR_DESTROY_XRCD_HANDLE,
236 UVERBS_ACCESS_DESTROY,
239 DECLARE_UVERBS_NAMED_OBJECT(
241 UVERBS_TYPE_ALLOC_IDR_SZ(sizeof(struct ib_uxrcd_object),
243 &UVERBS_METHOD(UVERBS_METHOD_XRCD_DESTROY));
245 DECLARE_UVERBS_NAMED_METHOD_DESTROY(
246 UVERBS_METHOD_PD_DESTROY,
247 UVERBS_ATTR_IDR(UVERBS_ATTR_DESTROY_PD_HANDLE,
249 UVERBS_ACCESS_DESTROY,
252 DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_PD,
253 UVERBS_TYPE_ALLOC_IDR(uverbs_free_pd),
254 &UVERBS_METHOD(UVERBS_METHOD_PD_DESTROY));
256 const struct uapi_definition uverbs_def_obj_intf[] = {
257 UAPI_DEF_CHAIN_OBJ_TREE_NAMED(UVERBS_OBJECT_PD,
258 UAPI_DEF_OBJ_NEEDS_FN(dealloc_pd)),
259 UAPI_DEF_CHAIN_OBJ_TREE_NAMED(UVERBS_OBJECT_COMP_CHANNEL,
260 UAPI_DEF_OBJ_NEEDS_FN(dealloc_pd)),
261 UAPI_DEF_CHAIN_OBJ_TREE_NAMED(UVERBS_OBJECT_AH,
262 UAPI_DEF_OBJ_NEEDS_FN(destroy_ah)),
263 UAPI_DEF_CHAIN_OBJ_TREE_NAMED(UVERBS_OBJECT_MW,
264 UAPI_DEF_OBJ_NEEDS_FN(dealloc_mw)),
265 UAPI_DEF_CHAIN_OBJ_TREE_NAMED(UVERBS_OBJECT_FLOW,
266 UAPI_DEF_OBJ_NEEDS_FN(destroy_flow)),
267 UAPI_DEF_CHAIN_OBJ_TREE_NAMED(
268 UVERBS_OBJECT_RWQ_IND_TBL,
269 UAPI_DEF_OBJ_NEEDS_FN(destroy_rwq_ind_table)),
270 UAPI_DEF_CHAIN_OBJ_TREE_NAMED(UVERBS_OBJECT_XRCD,
271 UAPI_DEF_OBJ_NEEDS_FN(dealloc_xrcd)),