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 int ib_uverbs_notsupp(struct uverbs_attr_bundle *attrs)
16 static void *uapi_add_elm(struct uverbs_api *uapi, u32 key, size_t alloc_size)
21 if (key == UVERBS_API_KEY_ERR)
22 return ERR_PTR(-EOVERFLOW);
24 elm = kzalloc(alloc_size, GFP_KERNEL);
25 rc = radix_tree_insert(&uapi->radix, key, elm);
34 static void *uapi_add_get_elm(struct uverbs_api *uapi, u32 key,
35 size_t alloc_size, bool *exists)
39 elm = uapi_add_elm(uapi, key, alloc_size);
45 if (elm != ERR_PTR(-EEXIST))
48 elm = radix_tree_lookup(&uapi->radix, key);
50 return ERR_PTR(-EINVAL);
55 static int uapi_create_write(struct uverbs_api *uapi,
56 struct ib_device *ibdev,
57 const struct uapi_definition *def,
61 struct uverbs_api_write_method *method_elm;
62 u32 method_key = obj_key;
66 method_key |= uapi_key_write_ex_method(def->write.command_num);
68 method_key |= uapi_key_write_method(def->write.command_num);
70 method_elm = uapi_add_get_elm(uapi, method_key, sizeof(*method_elm),
72 if (IS_ERR(method_elm))
73 return PTR_ERR(method_elm);
75 if (WARN_ON(exists && (def->write.is_ex != method_elm->is_ex)))
78 method_elm->is_ex = def->write.is_ex;
79 method_elm->handler = def->func_write;
81 method_elm->disabled = !(ibdev->uverbs_ex_cmd_mask &
82 BIT_ULL(def->write.command_num));
84 method_elm->disabled = !(ibdev->uverbs_cmd_mask &
85 BIT_ULL(def->write.command_num));
87 if (!def->write.is_ex && def->func_write) {
88 method_elm->has_udata = def->write.has_udata;
89 method_elm->has_resp = def->write.has_resp;
90 method_elm->req_size = def->write.req_size;
91 method_elm->resp_size = def->write.resp_size;
94 *cur_method_key = method_key;
98 static int uapi_merge_method(struct uverbs_api *uapi,
99 struct uverbs_api_object *obj_elm, u32 obj_key,
100 const struct uverbs_method_def *method,
103 u32 method_key = obj_key | uapi_key_ioctl_method(method->id);
104 struct uverbs_api_ioctl_method *method_elm;
111 method_elm = uapi_add_get_elm(uapi, method_key, sizeof(*method_elm),
113 if (IS_ERR(method_elm))
114 return PTR_ERR(method_elm);
117 * This occurs when a driver uses ADD_UVERBS_ATTRIBUTES_SIMPLE
119 if (WARN_ON(method->handler))
122 WARN_ON(!method->handler);
123 rcu_assign_pointer(method_elm->handler, method->handler);
124 if (method->handler != uverbs_destroy_def_handler)
125 method_elm->driver_method = is_driver;
128 for (i = 0; i != method->num_attrs; i++) {
129 const struct uverbs_attr_def *attr = (*method->attrs)[i];
130 struct uverbs_api_attr *attr_slot;
136 * ENUM_IN contains the 'ids' pointer to the driver's .rodata,
137 * so if it is specified by a driver then it always makes this
138 * into a driver method.
140 if (attr->attr.type == UVERBS_ATTR_TYPE_ENUM_IN)
141 method_elm->driver_method |= is_driver;
144 * Like other uobject based things we only support a single
145 * uobject being NEW'd or DESTROY'd
147 if (attr->attr.type == UVERBS_ATTR_TYPE_IDRS_ARRAY) {
148 u8 access = attr->attr.u2.objs_arr.access;
150 if (WARN_ON(access == UVERBS_ACCESS_NEW ||
151 access == UVERBS_ACCESS_DESTROY))
156 uapi_add_elm(uapi, method_key | uapi_key_attr(attr->id),
158 /* Attributes are not allowed to be modified by drivers */
159 if (IS_ERR(attr_slot))
160 return PTR_ERR(attr_slot);
162 attr_slot->spec = attr->attr;
168 static int uapi_merge_obj_tree(struct uverbs_api *uapi,
169 const struct uverbs_object_def *obj,
172 struct uverbs_api_object *obj_elm;
178 obj_key = uapi_key_obj(obj->id);
179 obj_elm = uapi_add_get_elm(uapi, obj_key, sizeof(*obj_elm), &exists);
181 return PTR_ERR(obj_elm);
183 if (obj->type_attrs) {
184 if (WARN_ON(obj_elm->type_attrs))
187 obj_elm->id = obj->id;
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 and
192 * fd_class types. We can revoke the IDR types during
193 * disassociation, and the FD types require the driver to use
194 * struct file_operations.owner to prevent the driver module
195 * code from unloading while the file is open. This provides
196 * enough safety that uverbs_close_fd() will continue to work.
197 * Drivers using FD are responsible to handle disassociation of
198 * the device on their own.
200 if (WARN_ON(is_driver &&
201 obj->type_attrs->type_class != &uverbs_idr_class &&
202 obj->type_attrs->type_class != &uverbs_fd_class))
209 for (i = 0; i != obj->num_methods; i++) {
210 const struct uverbs_method_def *method = (*obj->methods)[i];
215 rc = uapi_merge_method(uapi, obj_elm, obj_key, method,
224 static int uapi_disable_elm(struct uverbs_api *uapi,
225 const struct uapi_definition *def,
231 if (def->scope == UAPI_SCOPE_OBJECT) {
232 struct uverbs_api_object *obj_elm;
234 obj_elm = uapi_add_get_elm(
235 uapi, obj_key, sizeof(*obj_elm), &exists);
237 return PTR_ERR(obj_elm);
238 obj_elm->disabled = 1;
242 if (def->scope == UAPI_SCOPE_METHOD &&
243 uapi_key_is_ioctl_method(method_key)) {
244 struct uverbs_api_ioctl_method *method_elm;
246 method_elm = uapi_add_get_elm(uapi, method_key,
247 sizeof(*method_elm), &exists);
248 if (IS_ERR(method_elm))
249 return PTR_ERR(method_elm);
250 method_elm->disabled = 1;
254 if (def->scope == UAPI_SCOPE_METHOD &&
255 (uapi_key_is_write_method(method_key) ||
256 uapi_key_is_write_ex_method(method_key))) {
257 struct uverbs_api_write_method *write_elm;
259 write_elm = uapi_add_get_elm(uapi, method_key,
260 sizeof(*write_elm), &exists);
261 if (IS_ERR(write_elm))
262 return PTR_ERR(write_elm);
263 write_elm->disabled = 1;
271 static int uapi_merge_def(struct uverbs_api *uapi, struct ib_device *ibdev,
272 const struct uapi_definition *def_list,
275 const struct uapi_definition *def = def_list;
276 u32 cur_obj_key = UVERBS_API_KEY_ERR;
277 u32 cur_method_key = UVERBS_API_KEY_ERR;
285 switch ((enum uapi_definition_kind)def->kind) {
287 rc = uapi_merge_def(uapi, ibdev, def->chain, is_driver);
292 case UAPI_DEF_CHAIN_OBJ_TREE:
293 if (WARN_ON(def->object_start.object_id !=
294 def->chain_obj_tree->id))
297 cur_obj_key = uapi_key_obj(def->object_start.object_id);
298 rc = uapi_merge_obj_tree(uapi, def->chain_obj_tree,
307 case UAPI_DEF_IS_SUPPORTED_DEV_FN: {
309 (void *)(&ibdev->ops) + def->needs_fn_offset;
313 rc = uapi_disable_elm(
314 uapi, def, cur_obj_key, cur_method_key);
320 case UAPI_DEF_IS_SUPPORTED_FUNC:
321 if (def->func_is_supported(ibdev))
323 rc = uapi_disable_elm(
324 uapi, def, cur_obj_key, cur_method_key);
329 case UAPI_DEF_OBJECT_START: {
330 struct uverbs_api_object *obj_elm;
332 cur_obj_key = uapi_key_obj(def->object_start.object_id);
333 obj_elm = uapi_add_get_elm(uapi, cur_obj_key,
334 sizeof(*obj_elm), &exists);
336 return PTR_ERR(obj_elm);
341 rc = uapi_create_write(
342 uapi, ibdev, def, cur_obj_key, &cur_method_key);
353 uapi_finalize_ioctl_method(struct uverbs_api *uapi,
354 struct uverbs_api_ioctl_method *method_elm,
357 struct radix_tree_iter iter;
358 unsigned int num_attrs = 0;
359 unsigned int max_bkey = 0;
360 bool single_uobj = false;
363 method_elm->destroy_bkey = UVERBS_API_ATTR_BKEY_LEN;
364 radix_tree_for_each_slot (slot, &uapi->radix, &iter,
365 uapi_key_attrs_start(method_key)) {
366 struct uverbs_api_attr *elm =
367 rcu_dereference_protected(*slot, true);
368 u32 attr_key = iter.index & UVERBS_API_ATTR_KEY_MASK;
369 u32 attr_bkey = uapi_bkey_attr(attr_key);
370 u8 type = elm->spec.type;
372 if (uapi_key_attr_to_ioctl_method(iter.index) !=
373 uapi_key_attr_to_ioctl_method(method_key))
376 if (elm->spec.mandatory)
377 __set_bit(attr_bkey, method_elm->attr_mandatory);
379 if (elm->spec.is_udata)
380 method_elm->has_udata = true;
382 if (type == UVERBS_ATTR_TYPE_IDR ||
383 type == UVERBS_ATTR_TYPE_FD) {
384 u8 access = elm->spec.u.obj.access;
387 * Verbs specs may only have one NEW/DESTROY, we don't
388 * have the infrastructure to abort multiple NEW's or
389 * cope with multiple DESTROY failure.
391 if (access == UVERBS_ACCESS_NEW ||
392 access == UVERBS_ACCESS_DESTROY) {
393 if (WARN_ON(single_uobj))
397 if (WARN_ON(!elm->spec.mandatory))
401 if (access == UVERBS_ACCESS_DESTROY)
402 method_elm->destroy_bkey = attr_bkey;
405 max_bkey = max(max_bkey, attr_bkey);
409 method_elm->key_bitmap_len = max_bkey + 1;
410 WARN_ON(method_elm->key_bitmap_len > UVERBS_API_ATTR_BKEY_LEN);
412 uapi_compute_bundle_size(method_elm, num_attrs);
416 static int uapi_finalize(struct uverbs_api *uapi)
418 const struct uverbs_api_write_method **data;
419 unsigned long max_write_ex = 0;
420 unsigned long max_write = 0;
421 struct radix_tree_iter iter;
426 radix_tree_for_each_slot (slot, &uapi->radix, &iter, 0) {
427 struct uverbs_api_ioctl_method *method_elm =
428 rcu_dereference_protected(*slot, true);
430 if (uapi_key_is_ioctl_method(iter.index)) {
431 rc = uapi_finalize_ioctl_method(uapi, method_elm,
437 if (uapi_key_is_write_method(iter.index))
438 max_write = max(max_write,
439 iter.index & UVERBS_API_ATTR_KEY_MASK);
440 if (uapi_key_is_write_ex_method(iter.index))
443 iter.index & UVERBS_API_ATTR_KEY_MASK);
446 uapi->notsupp_method.handler = ib_uverbs_notsupp;
447 uapi->num_write = max_write + 1;
448 uapi->num_write_ex = max_write_ex + 1;
449 data = kmalloc_array(uapi->num_write + uapi->num_write_ex,
450 sizeof(*uapi->write_methods), GFP_KERNEL);
451 for (i = 0; i != uapi->num_write + uapi->num_write_ex; i++)
452 data[i] = &uapi->notsupp_method;
453 uapi->write_methods = data;
454 uapi->write_ex_methods = data + uapi->num_write;
456 radix_tree_for_each_slot (slot, &uapi->radix, &iter, 0) {
457 if (uapi_key_is_write_method(iter.index))
458 uapi->write_methods[iter.index &
459 UVERBS_API_ATTR_KEY_MASK] =
460 rcu_dereference_protected(*slot, true);
461 if (uapi_key_is_write_ex_method(iter.index))
462 uapi->write_ex_methods[iter.index &
463 UVERBS_API_ATTR_KEY_MASK] =
464 rcu_dereference_protected(*slot, true);
470 static void uapi_remove_range(struct uverbs_api *uapi, u32 start, u32 last)
472 struct radix_tree_iter iter;
475 radix_tree_for_each_slot (slot, &uapi->radix, &iter, start) {
476 if (iter.index > last)
478 kfree(rcu_dereference_protected(*slot, true));
479 radix_tree_iter_delete(&uapi->radix, &iter, slot);
483 static void uapi_remove_object(struct uverbs_api *uapi, u32 obj_key)
485 uapi_remove_range(uapi, obj_key,
486 obj_key | UVERBS_API_METHOD_KEY_MASK |
487 UVERBS_API_ATTR_KEY_MASK);
490 static void uapi_remove_method(struct uverbs_api *uapi, u32 method_key)
492 uapi_remove_range(uapi, method_key,
493 method_key | UVERBS_API_ATTR_KEY_MASK);
497 static u32 uapi_get_obj_id(struct uverbs_attr_spec *spec)
499 if (spec->type == UVERBS_ATTR_TYPE_IDR ||
500 spec->type == UVERBS_ATTR_TYPE_FD)
501 return spec->u.obj.obj_type;
502 if (spec->type == UVERBS_ATTR_TYPE_IDRS_ARRAY)
503 return spec->u2.objs_arr.obj_type;
504 return UVERBS_API_KEY_ERR;
507 static void uapi_key_okay(u32 key)
509 unsigned int count = 0;
511 if (uapi_key_is_object(key))
513 if (uapi_key_is_ioctl_method(key))
515 if (uapi_key_is_write_method(key))
517 if (uapi_key_is_write_ex_method(key))
519 if (uapi_key_is_attr(key))
521 WARN(count != 1, "Bad count %d key=%x", count, key);
524 static void uapi_finalize_disable(struct uverbs_api *uapi)
526 struct radix_tree_iter iter;
527 u32 starting_key = 0;
528 bool scan_again = false;
532 radix_tree_for_each_slot (slot, &uapi->radix, &iter, starting_key) {
533 uapi_key_okay(iter.index);
535 if (uapi_key_is_object(iter.index)) {
536 struct uverbs_api_object *obj_elm =
537 rcu_dereference_protected(*slot, true);
539 if (obj_elm->disabled) {
540 /* Have to check all the attrs again */
542 starting_key = iter.index;
543 uapi_remove_object(uapi, iter.index);
549 if (uapi_key_is_ioctl_method(iter.index)) {
550 struct uverbs_api_ioctl_method *method_elm =
551 rcu_dereference_protected(*slot, true);
553 if (method_elm->disabled) {
554 starting_key = iter.index;
555 uapi_remove_method(uapi, iter.index);
561 if (uapi_key_is_write_method(iter.index) ||
562 uapi_key_is_write_ex_method(iter.index)) {
563 struct uverbs_api_write_method *method_elm =
564 rcu_dereference_protected(*slot, true);
566 if (method_elm->disabled) {
568 radix_tree_iter_delete(&uapi->radix, &iter, slot);
573 if (uapi_key_is_attr(iter.index)) {
574 struct uverbs_api_attr *attr_elm =
575 rcu_dereference_protected(*slot, true);
576 const struct uverbs_api_object *tmp_obj;
580 * If the method has a mandatory object handle
581 * attribute which relies on an object which is not
582 * present then the entire method is uncallable.
584 if (!attr_elm->spec.mandatory)
586 obj_key = uapi_get_obj_id(&attr_elm->spec);
587 if (obj_key == UVERBS_API_KEY_ERR)
589 tmp_obj = uapi_get_object(uapi, obj_key);
590 if (IS_ERR(tmp_obj)) {
591 if (PTR_ERR(tmp_obj) == -ENOMSG)
594 if (!tmp_obj->disabled)
598 starting_key = iter.index;
601 iter.index & (UVERBS_API_OBJ_KEY_MASK |
602 UVERBS_API_METHOD_KEY_MASK));
616 void uverbs_destroy_api(struct uverbs_api *uapi)
621 uapi_remove_range(uapi, 0, U32_MAX);
622 kfree(uapi->write_methods);
626 static const struct uapi_definition uverbs_core_api[] = {
627 UAPI_DEF_CHAIN(uverbs_def_obj_counters),
628 UAPI_DEF_CHAIN(uverbs_def_obj_cq),
629 UAPI_DEF_CHAIN(uverbs_def_obj_device),
630 UAPI_DEF_CHAIN(uverbs_def_obj_dm),
631 UAPI_DEF_CHAIN(uverbs_def_obj_flow_action),
632 UAPI_DEF_CHAIN(uverbs_def_obj_intf),
633 UAPI_DEF_CHAIN(uverbs_def_obj_mr),
634 UAPI_DEF_CHAIN(uverbs_def_write_intf),
638 struct uverbs_api *uverbs_alloc_api(struct ib_device *ibdev)
640 struct uverbs_api *uapi;
643 uapi = kzalloc(sizeof(*uapi), GFP_KERNEL);
645 return ERR_PTR(-ENOMEM);
647 INIT_RADIX_TREE(&uapi->radix, GFP_KERNEL);
648 uapi->driver_id = ibdev->driver_id;
650 rc = uapi_merge_def(uapi, ibdev, uverbs_core_api, false);
653 rc = uapi_merge_def(uapi, ibdev, ibdev->driver_def, true);
657 uapi_finalize_disable(uapi);
658 rc = uapi_finalize(uapi);
666 "Setup of uverbs_api failed, kernel parsing tree description is not valid (%d)??\n",
669 uverbs_destroy_api(uapi);
674 * The pre version is done before destroying the HW objects, it only blocks
675 * off method access. All methods that require the ib_dev or the module data
676 * must test one of these assignments prior to continuing.
678 void uverbs_disassociate_api_pre(struct ib_uverbs_device *uverbs_dev)
680 struct uverbs_api *uapi = uverbs_dev->uapi;
681 struct radix_tree_iter iter;
684 rcu_assign_pointer(uverbs_dev->ib_dev, NULL);
686 radix_tree_for_each_slot (slot, &uapi->radix, &iter, 0) {
687 if (uapi_key_is_ioctl_method(iter.index)) {
688 struct uverbs_api_ioctl_method *method_elm =
689 rcu_dereference_protected(*slot, true);
691 if (method_elm->driver_method)
692 rcu_assign_pointer(method_elm->handler, NULL);
696 synchronize_srcu(&uverbs_dev->disassociate_srcu);
700 * Called when a driver disassociates from the ib_uverbs_device. The
701 * assumption is that the driver module will unload after. Replace everything
702 * related to the driver with NULL as a safety measure.
704 void uverbs_disassociate_api(struct uverbs_api *uapi)
706 struct radix_tree_iter iter;
709 radix_tree_for_each_slot (slot, &uapi->radix, &iter, 0) {
710 if (uapi_key_is_object(iter.index)) {
711 struct uverbs_api_object *object_elm =
712 rcu_dereference_protected(*slot, true);
715 * Some type_attrs are in the driver module. We don't
716 * bother to keep track of which since there should be
717 * no use of this after disassociate.
719 object_elm->type_attrs = NULL;
720 } else if (uapi_key_is_attr(iter.index)) {
721 struct uverbs_api_attr *elm =
722 rcu_dereference_protected(*slot, true);
724 if (elm->spec.type == UVERBS_ATTR_TYPE_ENUM_IN)
725 elm->spec.u2.enum_def.ids = NULL;