uverbs_uapi.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. static void *uapi_add_elm(struct uverbs_api *uapi, u32 key, size_t alloc_size)
  11. {
  12. void *elm;
  13. int rc;
  14. if (key == UVERBS_API_KEY_ERR)
  15. return ERR_PTR(-EOVERFLOW);
  16. elm = kzalloc(alloc_size, GFP_KERNEL);
  17. rc = radix_tree_insert(&uapi->radix, key, elm);
  18. if (rc) {
  19. kfree(elm);
  20. return ERR_PTR(rc);
  21. }
  22. return elm;
  23. }
  24. static int uapi_merge_method(struct uverbs_api *uapi,
  25. struct uverbs_api_object *obj_elm, u32 obj_key,
  26. const struct uverbs_method_def *method,
  27. bool is_driver)
  28. {
  29. u32 method_key = obj_key | uapi_key_ioctl_method(method->id);
  30. struct uverbs_api_ioctl_method *method_elm;
  31. unsigned int i;
  32. if (!method->attrs)
  33. return 0;
  34. method_elm = uapi_add_elm(uapi, method_key, sizeof(*method_elm));
  35. if (IS_ERR(method_elm)) {
  36. if (method_elm != ERR_PTR(-EEXIST))
  37. return PTR_ERR(method_elm);
  38. /*
  39. * This occurs when a driver uses ADD_UVERBS_ATTRIBUTES_SIMPLE
  40. */
  41. if (WARN_ON(method->handler))
  42. return -EINVAL;
  43. method_elm = radix_tree_lookup(&uapi->radix, method_key);
  44. if (WARN_ON(!method_elm))
  45. return -EINVAL;
  46. } else {
  47. WARN_ON(!method->handler);
  48. rcu_assign_pointer(method_elm->handler, method->handler);
  49. if (method->handler != uverbs_destroy_def_handler)
  50. method_elm->driver_method = is_driver;
  51. }
  52. for (i = 0; i != method->num_attrs; i++) {
  53. const struct uverbs_attr_def *attr = (*method->attrs)[i];
  54. struct uverbs_api_attr *attr_slot;
  55. if (!attr)
  56. continue;
  57. /*
  58. * ENUM_IN contains the 'ids' pointer to the driver's .rodata,
  59. * so if it is specified by a driver then it always makes this
  60. * into a driver method.
  61. */
  62. if (attr->attr.type == UVERBS_ATTR_TYPE_ENUM_IN)
  63. method_elm->driver_method |= is_driver;
  64. attr_slot =
  65. uapi_add_elm(uapi, method_key | uapi_key_attr(attr->id),
  66. sizeof(*attr_slot));
  67. /* Attributes are not allowed to be modified by drivers */
  68. if (IS_ERR(attr_slot))
  69. return PTR_ERR(attr_slot);
  70. attr_slot->spec = attr->attr;
  71. }
  72. return 0;
  73. }
  74. static int uapi_merge_tree(struct uverbs_api *uapi,
  75. const struct uverbs_object_tree_def *tree,
  76. bool is_driver)
  77. {
  78. unsigned int i, j;
  79. int rc;
  80. if (!tree->objects)
  81. return 0;
  82. for (i = 0; i != tree->num_objects; i++) {
  83. const struct uverbs_object_def *obj = (*tree->objects)[i];
  84. struct uverbs_api_object *obj_elm;
  85. u32 obj_key;
  86. if (!obj)
  87. continue;
  88. obj_key = uapi_key_obj(obj->id);
  89. obj_elm = uapi_add_elm(uapi, obj_key, sizeof(*obj_elm));
  90. if (IS_ERR(obj_elm)) {
  91. if (obj_elm != ERR_PTR(-EEXIST))
  92. return PTR_ERR(obj_elm);
  93. /* This occurs when a driver uses ADD_UVERBS_METHODS */
  94. if (WARN_ON(obj->type_attrs))
  95. return -EINVAL;
  96. obj_elm = radix_tree_lookup(&uapi->radix, obj_key);
  97. if (WARN_ON(!obj_elm))
  98. return -EINVAL;
  99. } else {
  100. obj_elm->type_attrs = obj->type_attrs;
  101. if (obj->type_attrs) {
  102. obj_elm->type_class =
  103. obj->type_attrs->type_class;
  104. /*
  105. * Today drivers are only permitted to use
  106. * idr_class types. They cannot use FD types
  107. * because we currently have no way to revoke
  108. * the fops pointer after device
  109. * disassociation.
  110. */
  111. if (WARN_ON(is_driver &&
  112. obj->type_attrs->type_class !=
  113. &uverbs_idr_class))
  114. return -EINVAL;
  115. }
  116. }
  117. if (!obj->methods)
  118. continue;
  119. for (j = 0; j != obj->num_methods; j++) {
  120. const struct uverbs_method_def *method =
  121. (*obj->methods)[j];
  122. if (!method)
  123. continue;
  124. rc = uapi_merge_method(uapi, obj_elm, obj_key, method,
  125. is_driver);
  126. if (rc)
  127. return rc;
  128. }
  129. }
  130. return 0;
  131. }
  132. static int
  133. uapi_finalize_ioctl_method(struct uverbs_api *uapi,
  134. struct uverbs_api_ioctl_method *method_elm,
  135. u32 method_key)
  136. {
  137. struct radix_tree_iter iter;
  138. unsigned int max_bkey = 0;
  139. bool single_uobj = false;
  140. void __rcu **slot;
  141. method_elm->destroy_bkey = UVERBS_API_ATTR_BKEY_LEN;
  142. radix_tree_for_each_slot (slot, &uapi->radix, &iter,
  143. uapi_key_attrs_start(method_key)) {
  144. struct uverbs_api_attr *elm =
  145. rcu_dereference_protected(*slot, true);
  146. u32 attr_key = iter.index & UVERBS_API_ATTR_KEY_MASK;
  147. u32 attr_bkey = uapi_bkey_attr(attr_key);
  148. u8 type = elm->spec.type;
  149. if (uapi_key_attr_to_method(iter.index) !=
  150. uapi_key_attr_to_method(method_key))
  151. break;
  152. if (elm->spec.mandatory)
  153. __set_bit(attr_bkey, method_elm->attr_mandatory);
  154. if (type == UVERBS_ATTR_TYPE_IDR ||
  155. type == UVERBS_ATTR_TYPE_FD) {
  156. u8 access = elm->spec.u.obj.access;
  157. /*
  158. * Verbs specs may only have one NEW/DESTROY, we don't
  159. * have the infrastructure to abort multiple NEW's or
  160. * cope with multiple DESTROY failure.
  161. */
  162. if (access == UVERBS_ACCESS_NEW ||
  163. access == UVERBS_ACCESS_DESTROY) {
  164. if (WARN_ON(single_uobj))
  165. return -EINVAL;
  166. single_uobj = true;
  167. if (WARN_ON(!elm->spec.mandatory))
  168. return -EINVAL;
  169. }
  170. if (access == UVERBS_ACCESS_DESTROY)
  171. method_elm->destroy_bkey = attr_bkey;
  172. }
  173. max_bkey = max(max_bkey, attr_bkey);
  174. }
  175. method_elm->key_bitmap_len = max_bkey + 1;
  176. WARN_ON(method_elm->key_bitmap_len > UVERBS_API_ATTR_BKEY_LEN);
  177. return 0;
  178. }
  179. static int uapi_finalize(struct uverbs_api *uapi)
  180. {
  181. struct radix_tree_iter iter;
  182. void __rcu **slot;
  183. int rc;
  184. radix_tree_for_each_slot (slot, &uapi->radix, &iter, 0) {
  185. struct uverbs_api_ioctl_method *method_elm =
  186. rcu_dereference_protected(*slot, true);
  187. if (uapi_key_is_ioctl_method(iter.index)) {
  188. rc = uapi_finalize_ioctl_method(uapi, method_elm,
  189. iter.index);
  190. if (rc)
  191. return rc;
  192. }
  193. }
  194. return 0;
  195. }
  196. void uverbs_destroy_api(struct uverbs_api *uapi)
  197. {
  198. struct radix_tree_iter iter;
  199. void __rcu **slot;
  200. if (!uapi)
  201. return;
  202. radix_tree_for_each_slot (slot, &uapi->radix, &iter, 0) {
  203. kfree(rcu_dereference_protected(*slot, true));
  204. radix_tree_iter_delete(&uapi->radix, &iter, slot);
  205. }
  206. }
  207. struct uverbs_api *uverbs_alloc_api(
  208. const struct uverbs_object_tree_def *const *driver_specs,
  209. enum rdma_driver_id driver_id)
  210. {
  211. struct uverbs_api *uapi;
  212. int rc;
  213. uapi = kzalloc(sizeof(*uapi), GFP_KERNEL);
  214. if (!uapi)
  215. return ERR_PTR(-ENOMEM);
  216. INIT_RADIX_TREE(&uapi->radix, GFP_KERNEL);
  217. uapi->driver_id = driver_id;
  218. rc = uapi_merge_tree(uapi, uverbs_default_get_objects(), false);
  219. if (rc)
  220. goto err;
  221. for (; driver_specs && *driver_specs; driver_specs++) {
  222. rc = uapi_merge_tree(uapi, *driver_specs, true);
  223. if (rc)
  224. goto err;
  225. }
  226. rc = uapi_finalize(uapi);
  227. if (rc)
  228. goto err;
  229. return uapi;
  230. err:
  231. if (rc != -ENOMEM)
  232. pr_err("Setup of uverbs_api failed, kernel parsing tree description is not valid (%d)??\n",
  233. rc);
  234. uverbs_destroy_api(uapi);
  235. return ERR_PTR(rc);
  236. }
  237. /*
  238. * The pre version is done before destroying the HW objects, it only blocks
  239. * off method access. All methods that require the ib_dev or the module data
  240. * must test one of these assignments prior to continuing.
  241. */
  242. void uverbs_disassociate_api_pre(struct ib_uverbs_device *uverbs_dev)
  243. {
  244. struct uverbs_api *uapi = uverbs_dev->uapi;
  245. struct radix_tree_iter iter;
  246. void __rcu **slot;
  247. rcu_assign_pointer(uverbs_dev->ib_dev, NULL);
  248. radix_tree_for_each_slot (slot, &uapi->radix, &iter, 0) {
  249. if (uapi_key_is_ioctl_method(iter.index)) {
  250. struct uverbs_api_ioctl_method *method_elm =
  251. rcu_dereference_protected(*slot, true);
  252. if (method_elm->driver_method)
  253. rcu_assign_pointer(method_elm->handler, NULL);
  254. }
  255. }
  256. synchronize_srcu(&uverbs_dev->disassociate_srcu);
  257. }
  258. /*
  259. * Called when a driver disassociates from the ib_uverbs_device. The
  260. * assumption is that the driver module will unload after. Replace everything
  261. * related to the driver with NULL as a safety measure.
  262. */
  263. void uverbs_disassociate_api(struct uverbs_api *uapi)
  264. {
  265. struct radix_tree_iter iter;
  266. void __rcu **slot;
  267. radix_tree_for_each_slot (slot, &uapi->radix, &iter, 0) {
  268. if (uapi_key_is_object(iter.index)) {
  269. struct uverbs_api_object *object_elm =
  270. rcu_dereference_protected(*slot, true);
  271. /*
  272. * Some type_attrs are in the driver module. We don't
  273. * bother to keep track of which since there should be
  274. * no use of this after disassociate.
  275. */
  276. object_elm->type_attrs = NULL;
  277. } else if (uapi_key_is_attr(iter.index)) {
  278. struct uverbs_api_attr *elm =
  279. rcu_dereference_protected(*slot, true);
  280. if (elm->spec.type == UVERBS_ATTR_TYPE_ENUM_IN)
  281. elm->spec.u2.enum_def.ids = NULL;
  282. }
  283. }
  284. }