]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/infiniband/core/uverbs_uapi.c
RDMA/uverbs: Use a linear list to describe the compiled-in uapi
[mirror_ubuntu-hirsute-kernel.git] / drivers / infiniband / core / uverbs_uapi.c
CommitLineData
9ed3e5f4
JG
1// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2/*
3 * Copyright (c) 2017, Mellanox Technologies inc. All rights reserved.
4 */
5#include <rdma/uverbs_ioctl.h>
6#include <rdma/rdma_user_ioctl.h>
7#include <linux/bitops.h>
8#include "rdma_core.h"
9#include "uverbs.h"
10
11static void *uapi_add_elm(struct uverbs_api *uapi, u32 key, size_t alloc_size)
12{
13 void *elm;
14 int rc;
15
16 if (key == UVERBS_API_KEY_ERR)
17 return ERR_PTR(-EOVERFLOW);
18
19 elm = kzalloc(alloc_size, GFP_KERNEL);
20 rc = radix_tree_insert(&uapi->radix, key, elm);
21 if (rc) {
22 kfree(elm);
23 return ERR_PTR(rc);
24 }
25
26 return elm;
27}
28
29static int uapi_merge_method(struct uverbs_api *uapi,
30 struct uverbs_api_object *obj_elm, u32 obj_key,
31 const struct uverbs_method_def *method,
32 bool is_driver)
33{
34 u32 method_key = obj_key | uapi_key_ioctl_method(method->id);
35 struct uverbs_api_ioctl_method *method_elm;
36 unsigned int i;
37
38 if (!method->attrs)
39 return 0;
40
41 method_elm = uapi_add_elm(uapi, method_key, sizeof(*method_elm));
42 if (IS_ERR(method_elm)) {
43 if (method_elm != ERR_PTR(-EEXIST))
44 return PTR_ERR(method_elm);
45
46 /*
47 * This occurs when a driver uses ADD_UVERBS_ATTRIBUTES_SIMPLE
48 */
49 if (WARN_ON(method->handler))
50 return -EINVAL;
51 method_elm = radix_tree_lookup(&uapi->radix, method_key);
52 if (WARN_ON(!method_elm))
53 return -EINVAL;
54 } else {
55 WARN_ON(!method->handler);
56 rcu_assign_pointer(method_elm->handler, method->handler);
57 if (method->handler != uverbs_destroy_def_handler)
58 method_elm->driver_method = is_driver;
59 }
60
61 for (i = 0; i != method->num_attrs; i++) {
62 const struct uverbs_attr_def *attr = (*method->attrs)[i];
63 struct uverbs_api_attr *attr_slot;
64
65 if (!attr)
66 continue;
67
68 /*
69 * ENUM_IN contains the 'ids' pointer to the driver's .rodata,
70 * so if it is specified by a driver then it always makes this
71 * into a driver method.
72 */
73 if (attr->attr.type == UVERBS_ATTR_TYPE_ENUM_IN)
74 method_elm->driver_method |= is_driver;
75
70cd20ae
GL
76 /*
77 * Like other uobject based things we only support a single
78 * uobject being NEW'd or DESTROY'd
79 */
80 if (attr->attr.type == UVERBS_ATTR_TYPE_IDRS_ARRAY) {
81 u8 access = attr->attr.u2.objs_arr.access;
82
83 if (WARN_ON(access == UVERBS_ACCESS_NEW ||
84 access == UVERBS_ACCESS_DESTROY))
85 return -EINVAL;
86 }
87
9ed3e5f4
JG
88 attr_slot =
89 uapi_add_elm(uapi, method_key | uapi_key_attr(attr->id),
90 sizeof(*attr_slot));
91 /* Attributes are not allowed to be modified by drivers */
92 if (IS_ERR(attr_slot))
93 return PTR_ERR(attr_slot);
94
95 attr_slot->spec = attr->attr;
96 }
97
98 return 0;
99}
100
0cbf432d
JG
101static int uapi_merge_obj_tree(struct uverbs_api *uapi,
102 const struct uverbs_object_def *obj,
103 bool is_driver)
9ed3e5f4 104{
0cbf432d
JG
105 struct uverbs_api_object *obj_elm;
106 unsigned int i;
107 u32 obj_key;
9ed3e5f4
JG
108 int rc;
109
0cbf432d
JG
110 obj_key = uapi_key_obj(obj->id);
111 obj_elm = uapi_add_elm(uapi, obj_key, sizeof(*obj_elm));
112 if (IS_ERR(obj_elm)) {
113 if (obj_elm != ERR_PTR(-EEXIST))
114 return PTR_ERR(obj_elm);
115
116 /* This occurs when a driver uses ADD_UVERBS_METHODS */
117 if (WARN_ON(obj->type_attrs))
118 return -EINVAL;
119 obj_elm = radix_tree_lookup(&uapi->radix, obj_key);
120 if (WARN_ON(!obj_elm))
121 return -EINVAL;
122 } else {
123 obj_elm->type_attrs = obj->type_attrs;
124 if (obj->type_attrs) {
125 obj_elm->type_class = obj->type_attrs->type_class;
126 /*
127 * Today drivers are only permitted to use idr_class
128 * types. They cannot use FD types because we
129 * currently have no way to revoke the fops pointer
130 * after device disassociation.
131 */
132 if (WARN_ON(is_driver && obj->type_attrs->type_class !=
133 &uverbs_idr_class))
134 return -EINVAL;
135 }
136 }
137
138 if (!obj->methods)
9ed3e5f4
JG
139 return 0;
140
0cbf432d
JG
141 for (i = 0; i != obj->num_methods; i++) {
142 const struct uverbs_method_def *method = (*obj->methods)[i];
9ed3e5f4 143
0cbf432d 144 if (!method)
9ed3e5f4
JG
145 continue;
146
0cbf432d
JG
147 rc = uapi_merge_method(uapi, obj_elm, obj_key, method,
148 is_driver);
149 if (rc)
150 return rc;
151 }
9ed3e5f4 152
0cbf432d
JG
153 return 0;
154}
155
156static int uapi_merge_def(struct uverbs_api *uapi,
157 const struct uapi_definition *def_list,
158 bool is_driver)
159{
160 const struct uapi_definition *def = def_list;
161 int rc;
162
163 if (!def_list)
164 return 0;
9ed3e5f4 165
0cbf432d
JG
166 for (;; def++) {
167 switch ((enum uapi_definition_kind)def->kind) {
168 case UAPI_DEF_CHAIN:
169 rc = uapi_merge_def(uapi, def->chain, is_driver);
170 if (rc)
171 return rc;
9ed3e5f4
JG
172 continue;
173
0cbf432d
JG
174 case UAPI_DEF_CHAIN_OBJ_TREE:
175 if (WARN_ON(def->object_start.object_id !=
176 def->chain_obj_tree->id))
177 return -EINVAL;
9ed3e5f4 178
0cbf432d
JG
179 rc = uapi_merge_obj_tree(uapi, def->chain_obj_tree,
180 is_driver);
9ed3e5f4
JG
181 if (rc)
182 return rc;
0cbf432d
JG
183 continue;
184
185 case UAPI_DEF_END:
186 return 0;
9ed3e5f4 187 }
0cbf432d
JG
188 WARN_ON(true);
189 return -EINVAL;
9ed3e5f4 190 }
9ed3e5f4
JG
191}
192
193static int
194uapi_finalize_ioctl_method(struct uverbs_api *uapi,
195 struct uverbs_api_ioctl_method *method_elm,
196 u32 method_key)
197{
198 struct radix_tree_iter iter;
3a863577 199 unsigned int num_attrs = 0;
9ed3e5f4
JG
200 unsigned int max_bkey = 0;
201 bool single_uobj = false;
202 void __rcu **slot;
203
204 method_elm->destroy_bkey = UVERBS_API_ATTR_BKEY_LEN;
205 radix_tree_for_each_slot (slot, &uapi->radix, &iter,
206 uapi_key_attrs_start(method_key)) {
207 struct uverbs_api_attr *elm =
208 rcu_dereference_protected(*slot, true);
209 u32 attr_key = iter.index & UVERBS_API_ATTR_KEY_MASK;
210 u32 attr_bkey = uapi_bkey_attr(attr_key);
211 u8 type = elm->spec.type;
212
213 if (uapi_key_attr_to_method(iter.index) !=
214 uapi_key_attr_to_method(method_key))
215 break;
216
217 if (elm->spec.mandatory)
218 __set_bit(attr_bkey, method_elm->attr_mandatory);
219
220 if (type == UVERBS_ATTR_TYPE_IDR ||
221 type == UVERBS_ATTR_TYPE_FD) {
222 u8 access = elm->spec.u.obj.access;
223
224 /*
225 * Verbs specs may only have one NEW/DESTROY, we don't
226 * have the infrastructure to abort multiple NEW's or
227 * cope with multiple DESTROY failure.
228 */
229 if (access == UVERBS_ACCESS_NEW ||
230 access == UVERBS_ACCESS_DESTROY) {
231 if (WARN_ON(single_uobj))
232 return -EINVAL;
233
234 single_uobj = true;
235 if (WARN_ON(!elm->spec.mandatory))
236 return -EINVAL;
237 }
238
239 if (access == UVERBS_ACCESS_DESTROY)
240 method_elm->destroy_bkey = attr_bkey;
241 }
242
243 max_bkey = max(max_bkey, attr_bkey);
3a863577 244 num_attrs++;
9ed3e5f4
JG
245 }
246
247 method_elm->key_bitmap_len = max_bkey + 1;
248 WARN_ON(method_elm->key_bitmap_len > UVERBS_API_ATTR_BKEY_LEN);
249
3a863577 250 uapi_compute_bundle_size(method_elm, num_attrs);
9ed3e5f4
JG
251 return 0;
252}
253
254static int uapi_finalize(struct uverbs_api *uapi)
255{
256 struct radix_tree_iter iter;
257 void __rcu **slot;
258 int rc;
259
260 radix_tree_for_each_slot (slot, &uapi->radix, &iter, 0) {
261 struct uverbs_api_ioctl_method *method_elm =
262 rcu_dereference_protected(*slot, true);
263
264 if (uapi_key_is_ioctl_method(iter.index)) {
265 rc = uapi_finalize_ioctl_method(uapi, method_elm,
266 iter.index);
267 if (rc)
268 return rc;
269 }
270 }
271
272 return 0;
273}
274
275void uverbs_destroy_api(struct uverbs_api *uapi)
276{
277 struct radix_tree_iter iter;
278 void __rcu **slot;
279
280 if (!uapi)
281 return;
282
283 radix_tree_for_each_slot (slot, &uapi->radix, &iter, 0) {
284 kfree(rcu_dereference_protected(*slot, true));
285 radix_tree_iter_delete(&uapi->radix, &iter, slot);
286 }
a9360abd 287 kfree(uapi);
9ed3e5f4
JG
288}
289
0cbf432d
JG
290static const struct uapi_definition uverbs_core_api[] = {
291 UAPI_DEF_CHAIN(uverbs_def_obj_intf),
292 {},
293};
294
295struct uverbs_api *uverbs_alloc_api(const struct uapi_definition *driver_def,
296 enum rdma_driver_id driver_id)
9ed3e5f4
JG
297{
298 struct uverbs_api *uapi;
299 int rc;
300
301 uapi = kzalloc(sizeof(*uapi), GFP_KERNEL);
302 if (!uapi)
303 return ERR_PTR(-ENOMEM);
304
305 INIT_RADIX_TREE(&uapi->radix, GFP_KERNEL);
306 uapi->driver_id = driver_id;
307
0cbf432d
JG
308 rc = uapi_merge_def(uapi, uverbs_core_api, false);
309 if (rc)
310 goto err;
311 rc = uapi_merge_def(uapi, driver_def, true);
9ed3e5f4
JG
312 if (rc)
313 goto err;
9ed3e5f4
JG
314
315 rc = uapi_finalize(uapi);
316 if (rc)
317 goto err;
318
319 return uapi;
320err:
321 if (rc != -ENOMEM)
322 pr_err("Setup of uverbs_api failed, kernel parsing tree description is not valid (%d)??\n",
323 rc);
324
325 uverbs_destroy_api(uapi);
326 return ERR_PTR(rc);
327}
328
329/*
330 * The pre version is done before destroying the HW objects, it only blocks
331 * off method access. All methods that require the ib_dev or the module data
332 * must test one of these assignments prior to continuing.
333 */
334void uverbs_disassociate_api_pre(struct ib_uverbs_device *uverbs_dev)
335{
336 struct uverbs_api *uapi = uverbs_dev->uapi;
337 struct radix_tree_iter iter;
338 void __rcu **slot;
339
340 rcu_assign_pointer(uverbs_dev->ib_dev, NULL);
341
342 radix_tree_for_each_slot (slot, &uapi->radix, &iter, 0) {
343 if (uapi_key_is_ioctl_method(iter.index)) {
344 struct uverbs_api_ioctl_method *method_elm =
345 rcu_dereference_protected(*slot, true);
346
347 if (method_elm->driver_method)
348 rcu_assign_pointer(method_elm->handler, NULL);
349 }
350 }
351
352 synchronize_srcu(&uverbs_dev->disassociate_srcu);
353}
354
355/*
356 * Called when a driver disassociates from the ib_uverbs_device. The
357 * assumption is that the driver module will unload after. Replace everything
358 * related to the driver with NULL as a safety measure.
359 */
360void uverbs_disassociate_api(struct uverbs_api *uapi)
361{
362 struct radix_tree_iter iter;
363 void __rcu **slot;
364
365 radix_tree_for_each_slot (slot, &uapi->radix, &iter, 0) {
366 if (uapi_key_is_object(iter.index)) {
367 struct uverbs_api_object *object_elm =
368 rcu_dereference_protected(*slot, true);
369
370 /*
371 * Some type_attrs are in the driver module. We don't
372 * bother to keep track of which since there should be
373 * no use of this after disassociate.
374 */
375 object_elm->type_attrs = NULL;
376 } else if (uapi_key_is_attr(iter.index)) {
377 struct uverbs_api_attr *elm =
378 rcu_dereference_protected(*slot, true);
379
380 if (elm->spec.type == UVERBS_ATTR_TYPE_ENUM_IN)
381 elm->spec.u2.enum_def.ids = NULL;
382 }
383 }
384}