uverbs_uapi.c 8.8 KB

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