uverbs_uapi.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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 int ib_uverbs_notsupp(struct uverbs_attr_bundle *attrs)
  11. {
  12. return -EOPNOTSUPP;
  13. }
  14. static void *uapi_add_elm(struct uverbs_api *uapi, u32 key, size_t alloc_size)
  15. {
  16. void *elm;
  17. int rc;
  18. if (key == UVERBS_API_KEY_ERR)
  19. return ERR_PTR(-EOVERFLOW);
  20. elm = kzalloc(alloc_size, GFP_KERNEL);
  21. if (!elm)
  22. return ERR_PTR(-ENOMEM);
  23. rc = radix_tree_insert(&uapi->radix, key, elm);
  24. if (rc) {
  25. kfree(elm);
  26. return ERR_PTR(rc);
  27. }
  28. return elm;
  29. }
  30. static void *uapi_add_get_elm(struct uverbs_api *uapi, u32 key,
  31. size_t alloc_size, bool *exists)
  32. {
  33. void *elm;
  34. elm = uapi_add_elm(uapi, key, alloc_size);
  35. if (!IS_ERR(elm)) {
  36. *exists = false;
  37. return elm;
  38. }
  39. if (elm != ERR_PTR(-EEXIST))
  40. return elm;
  41. elm = radix_tree_lookup(&uapi->radix, key);
  42. if (WARN_ON(!elm))
  43. return ERR_PTR(-EINVAL);
  44. *exists = true;
  45. return elm;
  46. }
  47. static int uapi_create_write(struct uverbs_api *uapi,
  48. struct ib_device *ibdev,
  49. const struct uapi_definition *def,
  50. u32 obj_key,
  51. u32 *cur_method_key)
  52. {
  53. struct uverbs_api_write_method *method_elm;
  54. u32 method_key = obj_key;
  55. bool exists;
  56. if (def->write.is_ex)
  57. method_key |= uapi_key_write_ex_method(def->write.command_num);
  58. else
  59. method_key |= uapi_key_write_method(def->write.command_num);
  60. method_elm = uapi_add_get_elm(uapi, method_key, sizeof(*method_elm),
  61. &exists);
  62. if (IS_ERR(method_elm))
  63. return PTR_ERR(method_elm);
  64. if (WARN_ON(exists && (def->write.is_ex != method_elm->is_ex)))
  65. return -EINVAL;
  66. method_elm->is_ex = def->write.is_ex;
  67. method_elm->handler = def->func_write;
  68. if (!def->write.is_ex)
  69. method_elm->disabled = !(ibdev->uverbs_cmd_mask &
  70. BIT_ULL(def->write.command_num));
  71. if (!def->write.is_ex && def->func_write) {
  72. method_elm->has_udata = def->write.has_udata;
  73. method_elm->has_resp = def->write.has_resp;
  74. method_elm->req_size = def->write.req_size;
  75. method_elm->resp_size = def->write.resp_size;
  76. }
  77. *cur_method_key = method_key;
  78. return 0;
  79. }
  80. static int uapi_merge_method(struct uverbs_api *uapi,
  81. struct uverbs_api_object *obj_elm, u32 obj_key,
  82. const struct uverbs_method_def *method,
  83. bool is_driver)
  84. {
  85. u32 method_key = obj_key | uapi_key_ioctl_method(method->id);
  86. struct uverbs_api_ioctl_method *method_elm;
  87. unsigned int i;
  88. bool exists;
  89. if (!method->attrs)
  90. return 0;
  91. method_elm = uapi_add_get_elm(uapi, method_key, sizeof(*method_elm),
  92. &exists);
  93. if (IS_ERR(method_elm))
  94. return PTR_ERR(method_elm);
  95. if (exists) {
  96. /*
  97. * This occurs when a driver uses ADD_UVERBS_ATTRIBUTES_SIMPLE
  98. */
  99. if (WARN_ON(method->handler))
  100. return -EINVAL;
  101. } else {
  102. WARN_ON(!method->handler);
  103. rcu_assign_pointer(method_elm->handler, method->handler);
  104. if (method->handler != uverbs_destroy_def_handler)
  105. method_elm->driver_method = is_driver;
  106. }
  107. for (i = 0; i != method->num_attrs; i++) {
  108. const struct uverbs_attr_def *attr = (*method->attrs)[i];
  109. struct uverbs_api_attr *attr_slot;
  110. if (!attr)
  111. continue;
  112. /*
  113. * ENUM_IN contains the 'ids' pointer to the driver's .rodata,
  114. * so if it is specified by a driver then it always makes this
  115. * into a driver method.
  116. */
  117. if (attr->attr.type == UVERBS_ATTR_TYPE_ENUM_IN)
  118. method_elm->driver_method |= is_driver;
  119. /*
  120. * Like other uobject based things we only support a single
  121. * uobject being NEW'd or DESTROY'd
  122. */
  123. if (attr->attr.type == UVERBS_ATTR_TYPE_IDRS_ARRAY) {
  124. u8 access = attr->attr.u2.objs_arr.access;
  125. if (WARN_ON(access == UVERBS_ACCESS_NEW ||
  126. access == UVERBS_ACCESS_DESTROY))
  127. return -EINVAL;
  128. }
  129. attr_slot =
  130. uapi_add_elm(uapi, method_key | uapi_key_attr(attr->id),
  131. sizeof(*attr_slot));
  132. /* Attributes are not allowed to be modified by drivers */
  133. if (IS_ERR(attr_slot))
  134. return PTR_ERR(attr_slot);
  135. attr_slot->spec = attr->attr;
  136. }
  137. return 0;
  138. }
  139. static int uapi_merge_obj_tree(struct uverbs_api *uapi,
  140. const struct uverbs_object_def *obj,
  141. bool is_driver)
  142. {
  143. struct uverbs_api_object *obj_elm;
  144. unsigned int i;
  145. u32 obj_key;
  146. bool exists;
  147. int rc;
  148. obj_key = uapi_key_obj(obj->id);
  149. obj_elm = uapi_add_get_elm(uapi, obj_key, sizeof(*obj_elm), &exists);
  150. if (IS_ERR(obj_elm))
  151. return PTR_ERR(obj_elm);
  152. if (obj->type_attrs) {
  153. if (WARN_ON(obj_elm->type_attrs))
  154. return -EINVAL;
  155. obj_elm->id = obj->id;
  156. obj_elm->type_attrs = obj->type_attrs;
  157. obj_elm->type_class = obj->type_attrs->type_class;
  158. /*
  159. * Today drivers are only permitted to use idr_class and
  160. * fd_class types. We can revoke the IDR types during
  161. * disassociation, and the FD types require the driver to use
  162. * struct file_operations.owner to prevent the driver module
  163. * code from unloading while the file is open. This provides
  164. * enough safety that uverbs_uobject_fd_release() will
  165. * continue to work. Drivers using FD are responsible to
  166. * handle disassociation of the device on their own.
  167. */
  168. if (WARN_ON(is_driver &&
  169. obj->type_attrs->type_class != &uverbs_idr_class &&
  170. obj->type_attrs->type_class != &uverbs_fd_class))
  171. return -EINVAL;
  172. }
  173. if (!obj->methods)
  174. return 0;
  175. for (i = 0; i != obj->num_methods; i++) {
  176. const struct uverbs_method_def *method = (*obj->methods)[i];
  177. if (!method)
  178. continue;
  179. rc = uapi_merge_method(uapi, obj_elm, obj_key, method,
  180. is_driver);
  181. if (rc)
  182. return rc;
  183. }
  184. return 0;
  185. }
  186. static int uapi_disable_elm(struct uverbs_api *uapi,
  187. const struct uapi_definition *def,
  188. u32 obj_key,
  189. u32 method_key)
  190. {
  191. bool exists;
  192. if (def->scope == UAPI_SCOPE_OBJECT) {
  193. struct uverbs_api_object *obj_elm;
  194. obj_elm = uapi_add_get_elm(
  195. uapi, obj_key, sizeof(*obj_elm), &exists);
  196. if (IS_ERR(obj_elm))
  197. return PTR_ERR(obj_elm);
  198. obj_elm->disabled = 1;
  199. return 0;
  200. }
  201. if (def->scope == UAPI_SCOPE_METHOD &&
  202. uapi_key_is_ioctl_method(method_key)) {
  203. struct uverbs_api_ioctl_method *method_elm;
  204. method_elm = uapi_add_get_elm(uapi, method_key,
  205. sizeof(*method_elm), &exists);
  206. if (IS_ERR(method_elm))
  207. return PTR_ERR(method_elm);
  208. method_elm->disabled = 1;
  209. return 0;
  210. }
  211. if (def->scope == UAPI_SCOPE_METHOD &&
  212. (uapi_key_is_write_method(method_key) ||
  213. uapi_key_is_write_ex_method(method_key))) {
  214. struct uverbs_api_write_method *write_elm;
  215. write_elm = uapi_add_get_elm(uapi, method_key,
  216. sizeof(*write_elm), &exists);
  217. if (IS_ERR(write_elm))
  218. return PTR_ERR(write_elm);
  219. write_elm->disabled = 1;
  220. return 0;
  221. }
  222. WARN_ON(true);
  223. return -EINVAL;
  224. }
  225. static int uapi_merge_def(struct uverbs_api *uapi, struct ib_device *ibdev,
  226. const struct uapi_definition *def_list,
  227. bool is_driver)
  228. {
  229. const struct uapi_definition *def = def_list;
  230. u32 cur_obj_key = UVERBS_API_KEY_ERR;
  231. u32 cur_method_key = UVERBS_API_KEY_ERR;
  232. bool exists;
  233. int rc;
  234. if (!def_list)
  235. return 0;
  236. for (;; def++) {
  237. switch ((enum uapi_definition_kind)def->kind) {
  238. case UAPI_DEF_CHAIN:
  239. rc = uapi_merge_def(uapi, ibdev, def->chain, is_driver);
  240. if (rc)
  241. return rc;
  242. continue;
  243. case UAPI_DEF_CHAIN_OBJ_TREE:
  244. if (WARN_ON(def->object_start.object_id !=
  245. def->chain_obj_tree->id))
  246. return -EINVAL;
  247. cur_obj_key = uapi_key_obj(def->object_start.object_id);
  248. rc = uapi_merge_obj_tree(uapi, def->chain_obj_tree,
  249. is_driver);
  250. if (rc)
  251. return rc;
  252. continue;
  253. case UAPI_DEF_END:
  254. return 0;
  255. case UAPI_DEF_IS_SUPPORTED_DEV_FN: {
  256. void **ibdev_fn =
  257. (void *)(&ibdev->ops) + def->needs_fn_offset;
  258. if (*ibdev_fn)
  259. continue;
  260. rc = uapi_disable_elm(
  261. uapi, def, cur_obj_key, cur_method_key);
  262. if (rc)
  263. return rc;
  264. continue;
  265. }
  266. case UAPI_DEF_IS_SUPPORTED_FUNC:
  267. if (def->func_is_supported(ibdev))
  268. continue;
  269. rc = uapi_disable_elm(
  270. uapi, def, cur_obj_key, cur_method_key);
  271. if (rc)
  272. return rc;
  273. continue;
  274. case UAPI_DEF_OBJECT_START: {
  275. struct uverbs_api_object *obj_elm;
  276. cur_obj_key = uapi_key_obj(def->object_start.object_id);
  277. obj_elm = uapi_add_get_elm(uapi, cur_obj_key,
  278. sizeof(*obj_elm), &exists);
  279. if (IS_ERR(obj_elm))
  280. return PTR_ERR(obj_elm);
  281. continue;
  282. }
  283. case UAPI_DEF_WRITE:
  284. rc = uapi_create_write(
  285. uapi, ibdev, def, cur_obj_key, &cur_method_key);
  286. if (rc)
  287. return rc;
  288. continue;
  289. }
  290. WARN_ON(true);
  291. return -EINVAL;
  292. }
  293. }
  294. static int
  295. uapi_finalize_ioctl_method(struct uverbs_api *uapi,
  296. struct uverbs_api_ioctl_method *method_elm,
  297. u32 method_key)
  298. {
  299. struct radix_tree_iter iter;
  300. unsigned int num_attrs = 0;
  301. unsigned int max_bkey = 0;
  302. bool single_uobj = false;
  303. void __rcu **slot;
  304. method_elm->destroy_bkey = UVERBS_API_ATTR_BKEY_LEN;
  305. radix_tree_for_each_slot (slot, &uapi->radix, &iter,
  306. uapi_key_attrs_start(method_key)) {
  307. struct uverbs_api_attr *elm =
  308. rcu_dereference_protected(*slot, true);
  309. u32 attr_key = iter.index & UVERBS_API_ATTR_KEY_MASK;
  310. u32 attr_bkey = uapi_bkey_attr(attr_key);
  311. u8 type = elm->spec.type;
  312. if (uapi_key_attr_to_ioctl_method(iter.index) !=
  313. uapi_key_attr_to_ioctl_method(method_key))
  314. break;
  315. if (elm->spec.mandatory)
  316. __set_bit(attr_bkey, method_elm->attr_mandatory);
  317. if (elm->spec.is_udata)
  318. method_elm->has_udata = true;
  319. if (type == UVERBS_ATTR_TYPE_IDR ||
  320. type == UVERBS_ATTR_TYPE_FD) {
  321. u8 access = elm->spec.u.obj.access;
  322. /*
  323. * Verbs specs may only have one NEW/DESTROY, we don't
  324. * have the infrastructure to abort multiple NEW's or
  325. * cope with multiple DESTROY failure.
  326. */
  327. if (access == UVERBS_ACCESS_NEW ||
  328. access == UVERBS_ACCESS_DESTROY) {
  329. if (WARN_ON(single_uobj))
  330. return -EINVAL;
  331. single_uobj = true;
  332. if (WARN_ON(!elm->spec.mandatory))
  333. return -EINVAL;
  334. }
  335. if (access == UVERBS_ACCESS_DESTROY)
  336. method_elm->destroy_bkey = attr_bkey;
  337. }
  338. max_bkey = max(max_bkey, attr_bkey);
  339. num_attrs++;
  340. }
  341. method_elm->key_bitmap_len = max_bkey + 1;
  342. WARN_ON(method_elm->key_bitmap_len > UVERBS_API_ATTR_BKEY_LEN);
  343. uapi_compute_bundle_size(method_elm, num_attrs);
  344. return 0;
  345. }
  346. static int uapi_finalize(struct uverbs_api *uapi)
  347. {
  348. const struct uverbs_api_write_method **data;
  349. unsigned long max_write_ex = 0;
  350. unsigned long max_write = 0;
  351. struct radix_tree_iter iter;
  352. void __rcu **slot;
  353. int rc;
  354. int i;
  355. radix_tree_for_each_slot (slot, &uapi->radix, &iter, 0) {
  356. struct uverbs_api_ioctl_method *method_elm =
  357. rcu_dereference_protected(*slot, true);
  358. if (uapi_key_is_ioctl_method(iter.index)) {
  359. rc = uapi_finalize_ioctl_method(uapi, method_elm,
  360. iter.index);
  361. if (rc)
  362. return rc;
  363. }
  364. if (uapi_key_is_write_method(iter.index))
  365. max_write = max(max_write,
  366. iter.index & UVERBS_API_ATTR_KEY_MASK);
  367. if (uapi_key_is_write_ex_method(iter.index))
  368. max_write_ex =
  369. max(max_write_ex,
  370. iter.index & UVERBS_API_ATTR_KEY_MASK);
  371. }
  372. uapi->notsupp_method.handler = ib_uverbs_notsupp;
  373. uapi->num_write = max_write + 1;
  374. uapi->num_write_ex = max_write_ex + 1;
  375. data = kmalloc_array(uapi->num_write + uapi->num_write_ex,
  376. sizeof(*uapi->write_methods), GFP_KERNEL);
  377. if (!data)
  378. return -ENOMEM;
  379. for (i = 0; i != uapi->num_write + uapi->num_write_ex; i++)
  380. data[i] = &uapi->notsupp_method;
  381. uapi->write_methods = data;
  382. uapi->write_ex_methods = data + uapi->num_write;
  383. radix_tree_for_each_slot (slot, &uapi->radix, &iter, 0) {
  384. if (uapi_key_is_write_method(iter.index))
  385. uapi->write_methods[iter.index &
  386. UVERBS_API_ATTR_KEY_MASK] =
  387. rcu_dereference_protected(*slot, true);
  388. if (uapi_key_is_write_ex_method(iter.index))
  389. uapi->write_ex_methods[iter.index &
  390. UVERBS_API_ATTR_KEY_MASK] =
  391. rcu_dereference_protected(*slot, true);
  392. }
  393. return 0;
  394. }
  395. static void uapi_remove_range(struct uverbs_api *uapi, u32 start, u32 last)
  396. {
  397. struct radix_tree_iter iter;
  398. void __rcu **slot;
  399. radix_tree_for_each_slot (slot, &uapi->radix, &iter, start) {
  400. if (iter.index > last)
  401. return;
  402. kfree(rcu_dereference_protected(*slot, true));
  403. radix_tree_iter_delete(&uapi->radix, &iter, slot);
  404. }
  405. }
  406. static void uapi_remove_object(struct uverbs_api *uapi, u32 obj_key)
  407. {
  408. uapi_remove_range(uapi, obj_key,
  409. obj_key | UVERBS_API_METHOD_KEY_MASK |
  410. UVERBS_API_ATTR_KEY_MASK);
  411. }
  412. static void uapi_remove_method(struct uverbs_api *uapi, u32 method_key)
  413. {
  414. uapi_remove_range(uapi, method_key,
  415. method_key | UVERBS_API_ATTR_KEY_MASK);
  416. }
  417. static u32 uapi_get_obj_id(struct uverbs_attr_spec *spec)
  418. {
  419. if (spec->type == UVERBS_ATTR_TYPE_IDR ||
  420. spec->type == UVERBS_ATTR_TYPE_FD)
  421. return spec->u.obj.obj_type;
  422. if (spec->type == UVERBS_ATTR_TYPE_IDRS_ARRAY)
  423. return spec->u2.objs_arr.obj_type;
  424. return UVERBS_API_KEY_ERR;
  425. }
  426. static void uapi_key_okay(u32 key)
  427. {
  428. unsigned int count = 0;
  429. if (uapi_key_is_object(key))
  430. count++;
  431. if (uapi_key_is_ioctl_method(key))
  432. count++;
  433. if (uapi_key_is_write_method(key))
  434. count++;
  435. if (uapi_key_is_write_ex_method(key))
  436. count++;
  437. if (uapi_key_is_attr(key))
  438. count++;
  439. WARN(count != 1, "Bad count %u key=%x", count, key);
  440. }
  441. static void uapi_finalize_disable(struct uverbs_api *uapi)
  442. {
  443. struct radix_tree_iter iter;
  444. u32 starting_key = 0;
  445. bool scan_again = false;
  446. void __rcu **slot;
  447. again:
  448. radix_tree_for_each_slot (slot, &uapi->radix, &iter, starting_key) {
  449. uapi_key_okay(iter.index);
  450. if (uapi_key_is_object(iter.index)) {
  451. struct uverbs_api_object *obj_elm =
  452. rcu_dereference_protected(*slot, true);
  453. if (obj_elm->disabled) {
  454. /* Have to check all the attrs again */
  455. scan_again = true;
  456. starting_key = iter.index;
  457. uapi_remove_object(uapi, iter.index);
  458. goto again;
  459. }
  460. continue;
  461. }
  462. if (uapi_key_is_ioctl_method(iter.index)) {
  463. struct uverbs_api_ioctl_method *method_elm =
  464. rcu_dereference_protected(*slot, true);
  465. if (method_elm->disabled) {
  466. starting_key = iter.index;
  467. uapi_remove_method(uapi, iter.index);
  468. goto again;
  469. }
  470. continue;
  471. }
  472. if (uapi_key_is_write_method(iter.index) ||
  473. uapi_key_is_write_ex_method(iter.index)) {
  474. struct uverbs_api_write_method *method_elm =
  475. rcu_dereference_protected(*slot, true);
  476. if (method_elm->disabled) {
  477. kfree(method_elm);
  478. radix_tree_iter_delete(&uapi->radix, &iter, slot);
  479. }
  480. continue;
  481. }
  482. if (uapi_key_is_attr(iter.index)) {
  483. struct uverbs_api_attr *attr_elm =
  484. rcu_dereference_protected(*slot, true);
  485. const struct uverbs_api_object *tmp_obj;
  486. u32 obj_key;
  487. /*
  488. * If the method has a mandatory object handle
  489. * attribute which relies on an object which is not
  490. * present then the entire method is uncallable.
  491. */
  492. if (!attr_elm->spec.mandatory)
  493. continue;
  494. obj_key = uapi_get_obj_id(&attr_elm->spec);
  495. if (obj_key == UVERBS_API_KEY_ERR)
  496. continue;
  497. tmp_obj = uapi_get_object(uapi, obj_key);
  498. if (IS_ERR(tmp_obj)) {
  499. if (PTR_ERR(tmp_obj) == -ENOMSG)
  500. continue;
  501. } else {
  502. if (!tmp_obj->disabled)
  503. continue;
  504. }
  505. starting_key = iter.index;
  506. uapi_remove_method(
  507. uapi,
  508. iter.index & (UVERBS_API_OBJ_KEY_MASK |
  509. UVERBS_API_METHOD_KEY_MASK));
  510. goto again;
  511. }
  512. WARN_ON(false);
  513. }
  514. if (!scan_again)
  515. return;
  516. scan_again = false;
  517. starting_key = 0;
  518. goto again;
  519. }
  520. void uverbs_destroy_api(struct uverbs_api *uapi)
  521. {
  522. if (!uapi)
  523. return;
  524. uapi_remove_range(uapi, 0, U32_MAX);
  525. kfree(uapi->write_methods);
  526. kfree(uapi);
  527. }
  528. static const struct uapi_definition uverbs_core_api[] = {
  529. UAPI_DEF_CHAIN(uverbs_def_obj_async_fd),
  530. UAPI_DEF_CHAIN(uverbs_def_obj_counters),
  531. UAPI_DEF_CHAIN(uverbs_def_obj_cq),
  532. UAPI_DEF_CHAIN(uverbs_def_obj_device),
  533. UAPI_DEF_CHAIN(uverbs_def_obj_dm),
  534. UAPI_DEF_CHAIN(uverbs_def_obj_flow_action),
  535. UAPI_DEF_CHAIN(uverbs_def_obj_intf),
  536. UAPI_DEF_CHAIN(uverbs_def_obj_mr),
  537. UAPI_DEF_CHAIN(uverbs_def_obj_qp),
  538. UAPI_DEF_CHAIN(uverbs_def_obj_srq),
  539. UAPI_DEF_CHAIN(uverbs_def_obj_wq),
  540. UAPI_DEF_CHAIN(uverbs_def_write_intf),
  541. {},
  542. };
  543. struct uverbs_api *uverbs_alloc_api(struct ib_device *ibdev)
  544. {
  545. struct uverbs_api *uapi;
  546. int rc;
  547. uapi = kzalloc(sizeof(*uapi), GFP_KERNEL);
  548. if (!uapi)
  549. return ERR_PTR(-ENOMEM);
  550. INIT_RADIX_TREE(&uapi->radix, GFP_KERNEL);
  551. uapi->driver_id = ibdev->ops.driver_id;
  552. rc = uapi_merge_def(uapi, ibdev, uverbs_core_api, false);
  553. if (rc)
  554. goto err;
  555. rc = uapi_merge_def(uapi, ibdev, ibdev->driver_def, true);
  556. if (rc)
  557. goto err;
  558. uapi_finalize_disable(uapi);
  559. rc = uapi_finalize(uapi);
  560. if (rc)
  561. goto err;
  562. return uapi;
  563. err:
  564. if (rc != -ENOMEM)
  565. dev_err(&ibdev->dev,
  566. "Setup of uverbs_api failed, kernel parsing tree description is not valid (%d)??\n",
  567. rc);
  568. uverbs_destroy_api(uapi);
  569. return ERR_PTR(rc);
  570. }
  571. /*
  572. * The pre version is done before destroying the HW objects, it only blocks
  573. * off method access. All methods that require the ib_dev or the module data
  574. * must test one of these assignments prior to continuing.
  575. */
  576. void uverbs_disassociate_api_pre(struct ib_uverbs_device *uverbs_dev)
  577. {
  578. struct uverbs_api *uapi = uverbs_dev->uapi;
  579. struct radix_tree_iter iter;
  580. void __rcu **slot;
  581. rcu_assign_pointer(uverbs_dev->ib_dev, NULL);
  582. radix_tree_for_each_slot (slot, &uapi->radix, &iter, 0) {
  583. if (uapi_key_is_ioctl_method(iter.index)) {
  584. struct uverbs_api_ioctl_method *method_elm =
  585. rcu_dereference_protected(*slot, true);
  586. if (method_elm->driver_method)
  587. rcu_assign_pointer(method_elm->handler, NULL);
  588. }
  589. }
  590. synchronize_srcu(&uverbs_dev->disassociate_srcu);
  591. }
  592. /*
  593. * Called when a driver disassociates from the ib_uverbs_device. The
  594. * assumption is that the driver module will unload after. Replace everything
  595. * related to the driver with NULL as a safety measure.
  596. */
  597. void uverbs_disassociate_api(struct uverbs_api *uapi)
  598. {
  599. struct radix_tree_iter iter;
  600. void __rcu **slot;
  601. radix_tree_for_each_slot (slot, &uapi->radix, &iter, 0) {
  602. if (uapi_key_is_object(iter.index)) {
  603. struct uverbs_api_object *object_elm =
  604. rcu_dereference_protected(*slot, true);
  605. /*
  606. * Some type_attrs are in the driver module. We don't
  607. * bother to keep track of which since there should be
  608. * no use of this after disassociate.
  609. */
  610. object_elm->type_attrs = NULL;
  611. } else if (uapi_key_is_attr(iter.index)) {
  612. struct uverbs_api_attr *elm =
  613. rcu_dereference_protected(*slot, true);
  614. if (elm->spec.type == UVERBS_ATTR_TYPE_ENUM_IN)
  615. elm->spec.u2.enum_def.ids = NULL;
  616. }
  617. }
  618. }