uverbs_uapi.c 8.5 KB

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