1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
3 * Copyright (c) 2017, Mellanox Technologies inc. All rights reserved.
5 #include <rdma/uverbs_ioctl.h>
6 #include <rdma/rdma_user_ioctl.h>
7 #include <linux/bitops.h>
11 static ssize_t ib_uverbs_notsupp(struct ib_uverbs_file *file,
12 const char __user *buf, int in_len,
18 static int ib_uverbs_ex_notsupp(struct ib_uverbs_file *file,
19 struct ib_udata *ucore, struct ib_udata *uhw)
24 static void *uapi_add_elm(struct uverbs_api *uapi, u32 key, size_t alloc_size)
29 if (key == UVERBS_API_KEY_ERR)
30 return ERR_PTR(-EOVERFLOW);
32 elm = kzalloc(alloc_size, GFP_KERNEL);
33 rc = radix_tree_insert(&uapi->radix, key, elm);
42 static void *uapi_add_get_elm(struct uverbs_api *uapi, u32 key,
43 size_t alloc_size, bool *exists)
47 elm = uapi_add_elm(uapi, key, alloc_size);
53 if (elm != ERR_PTR(-EEXIST))
56 elm = radix_tree_lookup(&uapi->radix, key);
58 return ERR_PTR(-EINVAL);
63 static int uapi_create_write(struct uverbs_api *uapi, struct ib_device *ibdev,
64 const struct uapi_definition *def, u32 obj_key)
66 struct uverbs_api_write_method *method_elm;
67 u32 method_key = obj_key;
71 method_key |= uapi_key_write_ex_method(def->write.command_num);
73 method_key |= uapi_key_write_method(def->write.command_num);
75 method_elm = uapi_add_get_elm(uapi, method_key, sizeof(*method_elm),
77 if (IS_ERR(method_elm))
78 return PTR_ERR(method_elm);
80 if (WARN_ON(exists && (def->write.is_ex != method_elm->is_ex ||
81 method_elm->handler_ex || method_elm->handler)))
84 method_elm->is_ex = def->write.is_ex;
85 if (def->write.is_ex) {
86 method_elm->handler_ex = def->func_write_ex;
88 method_elm->disabled = !(ibdev->uverbs_ex_cmd_mask &
89 BIT_ULL(def->write.command_num));
91 method_elm->handler = def->func_write;
93 method_elm->disabled = !(ibdev->uverbs_cmd_mask &
94 BIT_ULL(def->write.command_num));
99 static int uapi_merge_method(struct uverbs_api *uapi,
100 struct uverbs_api_object *obj_elm, u32 obj_key,
101 const struct uverbs_method_def *method,
104 u32 method_key = obj_key | uapi_key_ioctl_method(method->id);
105 struct uverbs_api_ioctl_method *method_elm;
112 method_elm = uapi_add_get_elm(uapi, method_key, sizeof(*method_elm),
114 if (IS_ERR(method_elm))
115 return PTR_ERR(method_elm);
118 * This occurs when a driver uses ADD_UVERBS_ATTRIBUTES_SIMPLE
120 if (WARN_ON(method->handler))
123 WARN_ON(!method->handler);
124 rcu_assign_pointer(method_elm->handler, method->handler);
125 if (method->handler != uverbs_destroy_def_handler)
126 method_elm->driver_method = is_driver;
129 for (i = 0; i != method->num_attrs; i++) {
130 const struct uverbs_attr_def *attr = (*method->attrs)[i];
131 struct uverbs_api_attr *attr_slot;
137 * ENUM_IN contains the 'ids' pointer to the driver's .rodata,
138 * so if it is specified by a driver then it always makes this
139 * into a driver method.
141 if (attr->attr.type == UVERBS_ATTR_TYPE_ENUM_IN)
142 method_elm->driver_method |= is_driver;
145 * Like other uobject based things we only support a single
146 * uobject being NEW'd or DESTROY'd
148 if (attr->attr.type == UVERBS_ATTR_TYPE_IDRS_ARRAY) {
149 u8 access = attr->attr.u2.objs_arr.access;
151 if (WARN_ON(access == UVERBS_ACCESS_NEW ||
152 access == UVERBS_ACCESS_DESTROY))
157 uapi_add_elm(uapi, method_key | uapi_key_attr(attr->id),
159 /* Attributes are not allowed to be modified by drivers */
160 if (IS_ERR(attr_slot))
161 return PTR_ERR(attr_slot);
163 attr_slot->spec = attr->attr;
169 static int uapi_merge_obj_tree(struct uverbs_api *uapi,
170 const struct uverbs_object_def *obj,
173 struct uverbs_api_object *obj_elm;
179 obj_key = uapi_key_obj(obj->id);
180 obj_elm = uapi_add_get_elm(uapi, obj_key, sizeof(*obj_elm), &exists);
182 return PTR_ERR(obj_elm);
184 if (obj->type_attrs) {
185 if (WARN_ON(obj_elm->type_attrs))
188 obj_elm->type_attrs = obj->type_attrs;
189 obj_elm->type_class = obj->type_attrs->type_class;
191 * Today drivers are only permitted to use idr_class
192 * types. They cannot use FD types because we currently have
193 * no way to revoke the fops pointer after device
196 if (WARN_ON(is_driver &&
197 obj->type_attrs->type_class != &uverbs_idr_class))
204 for (i = 0; i != obj->num_methods; i++) {
205 const struct uverbs_method_def *method = (*obj->methods)[i];
210 rc = uapi_merge_method(uapi, obj_elm, obj_key, method,
219 static int uapi_disable_elm(struct uverbs_api *uapi,
220 const struct uapi_definition *def,
225 if (def->scope == UAPI_SCOPE_OBJECT) {
226 struct uverbs_api_object *obj_elm;
228 obj_elm = uapi_add_get_elm(
229 uapi, obj_key, sizeof(*obj_elm), &exists);
231 return PTR_ERR(obj_elm);
232 obj_elm->disabled = 1;
240 static int uapi_merge_def(struct uverbs_api *uapi, struct ib_device *ibdev,
241 const struct uapi_definition *def_list,
244 const struct uapi_definition *def = def_list;
245 u32 cur_obj_key = UVERBS_API_KEY_ERR;
253 switch ((enum uapi_definition_kind)def->kind) {
255 rc = uapi_merge_def(uapi, ibdev, def->chain, is_driver);
260 case UAPI_DEF_CHAIN_OBJ_TREE:
261 if (WARN_ON(def->object_start.object_id !=
262 def->chain_obj_tree->id))
265 cur_obj_key = uapi_key_obj(def->object_start.object_id);
266 rc = uapi_merge_obj_tree(uapi, def->chain_obj_tree,
275 case UAPI_DEF_IS_SUPPORTED_DEV_FN: {
276 void **ibdev_fn = (void *)ibdev + def->needs_fn_offset;
280 rc = uapi_disable_elm(uapi, def, cur_obj_key);
286 case UAPI_DEF_IS_SUPPORTED_FUNC:
287 if (def->func_is_supported(ibdev))
289 rc = uapi_disable_elm(uapi, def, cur_obj_key);
294 case UAPI_DEF_OBJECT_START: {
295 struct uverbs_api_object *obj_elm;
297 cur_obj_key = uapi_key_obj(def->object_start.object_id);
298 obj_elm = uapi_add_get_elm(uapi, cur_obj_key,
299 sizeof(*obj_elm), &exists);
301 return PTR_ERR(obj_elm);
306 rc = uapi_create_write(uapi, ibdev, def, cur_obj_key);
317 uapi_finalize_ioctl_method(struct uverbs_api *uapi,
318 struct uverbs_api_ioctl_method *method_elm,
321 struct radix_tree_iter iter;
322 unsigned int num_attrs = 0;
323 unsigned int max_bkey = 0;
324 bool single_uobj = false;
327 method_elm->destroy_bkey = UVERBS_API_ATTR_BKEY_LEN;
328 radix_tree_for_each_slot (slot, &uapi->radix, &iter,
329 uapi_key_attrs_start(method_key)) {
330 struct uverbs_api_attr *elm =
331 rcu_dereference_protected(*slot, true);
332 u32 attr_key = iter.index & UVERBS_API_ATTR_KEY_MASK;
333 u32 attr_bkey = uapi_bkey_attr(attr_key);
334 u8 type = elm->spec.type;
336 if (uapi_key_attr_to_ioctl_method(iter.index) !=
337 uapi_key_attr_to_ioctl_method(method_key))
340 if (elm->spec.mandatory)
341 __set_bit(attr_bkey, method_elm->attr_mandatory);
343 if (type == UVERBS_ATTR_TYPE_IDR ||
344 type == UVERBS_ATTR_TYPE_FD) {
345 u8 access = elm->spec.u.obj.access;
348 * Verbs specs may only have one NEW/DESTROY, we don't
349 * have the infrastructure to abort multiple NEW's or
350 * cope with multiple DESTROY failure.
352 if (access == UVERBS_ACCESS_NEW ||
353 access == UVERBS_ACCESS_DESTROY) {
354 if (WARN_ON(single_uobj))
358 if (WARN_ON(!elm->spec.mandatory))
362 if (access == UVERBS_ACCESS_DESTROY)
363 method_elm->destroy_bkey = attr_bkey;
366 max_bkey = max(max_bkey, attr_bkey);
370 method_elm->key_bitmap_len = max_bkey + 1;
371 WARN_ON(method_elm->key_bitmap_len > UVERBS_API_ATTR_BKEY_LEN);
373 uapi_compute_bundle_size(method_elm, num_attrs);
377 static int uapi_finalize(struct uverbs_api *uapi)
379 const struct uverbs_api_write_method **data;
380 unsigned long max_write_ex = 0;
381 unsigned long max_write = 0;
382 struct radix_tree_iter iter;
387 radix_tree_for_each_slot (slot, &uapi->radix, &iter, 0) {
388 struct uverbs_api_ioctl_method *method_elm =
389 rcu_dereference_protected(*slot, true);
391 if (uapi_key_is_ioctl_method(iter.index)) {
392 rc = uapi_finalize_ioctl_method(uapi, method_elm,
398 if (uapi_key_is_write_method(iter.index))
399 max_write = max(max_write,
400 iter.index & UVERBS_API_ATTR_KEY_MASK);
401 if (uapi_key_is_write_ex_method(iter.index))
404 iter.index & UVERBS_API_ATTR_KEY_MASK);
407 uapi->notsupp_method.handler = ib_uverbs_notsupp;
408 uapi->notsupp_method.handler_ex = ib_uverbs_ex_notsupp;
409 uapi->num_write = max_write + 1;
410 uapi->num_write_ex = max_write_ex + 1;
411 data = kmalloc_array(uapi->num_write + uapi->num_write_ex,
412 sizeof(*uapi->write_methods), GFP_KERNEL);
413 for (i = 0; i != uapi->num_write + uapi->num_write_ex; i++)
414 data[i] = &uapi->notsupp_method;
415 uapi->write_methods = data;
416 uapi->write_ex_methods = data + uapi->num_write;
418 radix_tree_for_each_slot (slot, &uapi->radix, &iter, 0) {
419 if (uapi_key_is_write_method(iter.index))
420 uapi->write_methods[iter.index &
421 UVERBS_API_ATTR_KEY_MASK] =
422 rcu_dereference_protected(*slot, true);
423 if (uapi_key_is_write_ex_method(iter.index))
424 uapi->write_ex_methods[iter.index &
425 UVERBS_API_ATTR_KEY_MASK] =
426 rcu_dereference_protected(*slot, true);
432 static void uapi_remove_range(struct uverbs_api *uapi, u32 start, u32 last)
434 struct radix_tree_iter iter;
437 radix_tree_for_each_slot (slot, &uapi->radix, &iter, start) {
438 if (iter.index > last)
440 kfree(rcu_dereference_protected(*slot, true));
441 radix_tree_iter_delete(&uapi->radix, &iter, slot);
445 static void uapi_remove_object(struct uverbs_api *uapi, u32 obj_key)
447 uapi_remove_range(uapi, obj_key,
448 obj_key | UVERBS_API_METHOD_KEY_MASK |
449 UVERBS_API_ATTR_KEY_MASK);
452 static void uapi_remove_method(struct uverbs_api *uapi, u32 method_key)
454 uapi_remove_range(uapi, method_key,
455 method_key | UVERBS_API_ATTR_KEY_MASK);
459 static u32 uapi_get_obj_id(struct uverbs_attr_spec *spec)
461 if (spec->type == UVERBS_ATTR_TYPE_IDR ||
462 spec->type == UVERBS_ATTR_TYPE_FD)
463 return spec->u.obj.obj_type;
464 if (spec->type == UVERBS_ATTR_TYPE_IDRS_ARRAY)
465 return spec->u2.objs_arr.obj_type;
466 return UVERBS_API_KEY_ERR;
469 static void uapi_key_okay(u32 key)
471 unsigned int count = 0;
473 if (uapi_key_is_object(key))
475 if (uapi_key_is_ioctl_method(key))
477 if (uapi_key_is_write_method(key))
479 if (uapi_key_is_write_ex_method(key))
481 if (uapi_key_is_attr(key))
483 WARN(count != 1, "Bad count %d key=%x", count, key);
486 static void uapi_finalize_disable(struct uverbs_api *uapi)
488 struct radix_tree_iter iter;
489 u32 starting_key = 0;
490 bool scan_again = false;
494 radix_tree_for_each_slot (slot, &uapi->radix, &iter, starting_key) {
495 uapi_key_okay(iter.index);
497 if (uapi_key_is_object(iter.index)) {
498 struct uverbs_api_object *obj_elm =
499 rcu_dereference_protected(*slot, true);
501 if (obj_elm->disabled) {
502 /* Have to check all the attrs again */
504 starting_key = iter.index;
505 uapi_remove_object(uapi, iter.index);
511 if (uapi_key_is_ioctl_method(iter.index)) {
512 struct uverbs_api_ioctl_method *method_elm =
513 rcu_dereference_protected(*slot, true);
515 if (method_elm->disabled) {
516 starting_key = iter.index;
517 uapi_remove_method(uapi, iter.index);
523 if (uapi_key_is_write_method(iter.index) ||
524 uapi_key_is_write_ex_method(iter.index)) {
525 struct uverbs_api_write_method *method_elm =
526 rcu_dereference_protected(*slot, true);
528 if (method_elm->disabled) {
530 radix_tree_iter_delete(&uapi->radix, &iter, slot);
535 if (uapi_key_is_attr(iter.index)) {
536 struct uverbs_api_attr *attr_elm =
537 rcu_dereference_protected(*slot, true);
538 const struct uverbs_api_object *tmp_obj;
542 * If the method has a mandatory object handle
543 * attribute which relies on an object which is not
544 * present then the entire method is uncallable.
546 if (!attr_elm->spec.mandatory)
548 obj_key = uapi_get_obj_id(&attr_elm->spec);
549 if (obj_key == UVERBS_API_KEY_ERR)
551 tmp_obj = uapi_get_object(uapi, obj_key);
552 if (tmp_obj && !tmp_obj->disabled)
555 starting_key = iter.index;
558 iter.index & (UVERBS_API_OBJ_KEY_MASK |
559 UVERBS_API_METHOD_KEY_MASK));
573 void uverbs_destroy_api(struct uverbs_api *uapi)
578 uapi_remove_range(uapi, 0, U32_MAX);
579 kfree(uapi->write_methods);
583 static const struct uapi_definition uverbs_core_api[] = {
584 UAPI_DEF_CHAIN(uverbs_def_obj_counters),
585 UAPI_DEF_CHAIN(uverbs_def_obj_cq),
586 UAPI_DEF_CHAIN(uverbs_def_obj_dm),
587 UAPI_DEF_CHAIN(uverbs_def_obj_flow_action),
588 UAPI_DEF_CHAIN(uverbs_def_obj_intf),
589 UAPI_DEF_CHAIN(uverbs_def_obj_mr),
590 UAPI_DEF_CHAIN(uverbs_def_write_intf),
594 struct uverbs_api *uverbs_alloc_api(struct ib_device *ibdev)
596 struct uverbs_api *uapi;
599 uapi = kzalloc(sizeof(*uapi), GFP_KERNEL);
601 return ERR_PTR(-ENOMEM);
603 INIT_RADIX_TREE(&uapi->radix, GFP_KERNEL);
604 uapi->driver_id = ibdev->driver_id;
606 rc = uapi_merge_def(uapi, ibdev, uverbs_core_api, false);
609 rc = uapi_merge_def(uapi, ibdev, ibdev->driver_def, true);
613 uapi_finalize_disable(uapi);
614 rc = uapi_finalize(uapi);
622 "Setup of uverbs_api failed, kernel parsing tree description is not valid (%d)??\n",
625 uverbs_destroy_api(uapi);
630 * The pre version is done before destroying the HW objects, it only blocks
631 * off method access. All methods that require the ib_dev or the module data
632 * must test one of these assignments prior to continuing.
634 void uverbs_disassociate_api_pre(struct ib_uverbs_device *uverbs_dev)
636 struct uverbs_api *uapi = uverbs_dev->uapi;
637 struct radix_tree_iter iter;
640 rcu_assign_pointer(uverbs_dev->ib_dev, NULL);
642 radix_tree_for_each_slot (slot, &uapi->radix, &iter, 0) {
643 if (uapi_key_is_ioctl_method(iter.index)) {
644 struct uverbs_api_ioctl_method *method_elm =
645 rcu_dereference_protected(*slot, true);
647 if (method_elm->driver_method)
648 rcu_assign_pointer(method_elm->handler, NULL);
652 synchronize_srcu(&uverbs_dev->disassociate_srcu);
656 * Called when a driver disassociates from the ib_uverbs_device. The
657 * assumption is that the driver module will unload after. Replace everything
658 * related to the driver with NULL as a safety measure.
660 void uverbs_disassociate_api(struct uverbs_api *uapi)
662 struct radix_tree_iter iter;
665 radix_tree_for_each_slot (slot, &uapi->radix, &iter, 0) {
666 if (uapi_key_is_object(iter.index)) {
667 struct uverbs_api_object *object_elm =
668 rcu_dereference_protected(*slot, true);
671 * Some type_attrs are in the driver module. We don't
672 * bother to keep track of which since there should be
673 * no use of this after disassociate.
675 object_elm->type_attrs = NULL;
676 } else if (uapi_key_is_attr(iter.index)) {
677 struct uverbs_api_attr *elm =
678 rcu_dereference_protected(*slot, true);
680 if (elm->spec.type == UVERBS_ATTR_TYPE_ENUM_IN)
681 elm->spec.u2.enum_def.ids = NULL;