rdma_core.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. /*
  2. * Copyright (c) 2016, Mellanox Technologies inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/file.h>
  33. #include <linux/anon_inodes.h>
  34. #include <rdma/ib_verbs.h>
  35. #include <rdma/uverbs_types.h>
  36. #include <linux/rcupdate.h>
  37. #include <rdma/uverbs_ioctl.h>
  38. #include <rdma/rdma_user_ioctl.h>
  39. #include "uverbs.h"
  40. #include "core_priv.h"
  41. #include "rdma_core.h"
  42. int uverbs_ns_idx(u16 *id, unsigned int ns_count)
  43. {
  44. int ret = (*id & UVERBS_ID_NS_MASK) >> UVERBS_ID_NS_SHIFT;
  45. if (ret >= ns_count)
  46. return -EINVAL;
  47. *id &= ~UVERBS_ID_NS_MASK;
  48. return ret;
  49. }
  50. const struct uverbs_object_spec *uverbs_get_object(const struct ib_device *ibdev,
  51. uint16_t object)
  52. {
  53. const struct uverbs_root_spec *object_hash = ibdev->specs_root;
  54. const struct uverbs_object_spec_hash *objects;
  55. int ret = uverbs_ns_idx(&object, object_hash->num_buckets);
  56. if (ret < 0)
  57. return NULL;
  58. objects = object_hash->object_buckets[ret];
  59. if (object >= objects->num_objects)
  60. return NULL;
  61. return objects->objects[object];
  62. }
  63. const struct uverbs_method_spec *uverbs_get_method(const struct uverbs_object_spec *object,
  64. uint16_t method)
  65. {
  66. const struct uverbs_method_spec_hash *methods;
  67. int ret = uverbs_ns_idx(&method, object->num_buckets);
  68. if (ret < 0)
  69. return NULL;
  70. methods = object->method_buckets[ret];
  71. if (method >= methods->num_methods)
  72. return NULL;
  73. return methods->methods[method];
  74. }
  75. void uverbs_uobject_get(struct ib_uobject *uobject)
  76. {
  77. kref_get(&uobject->ref);
  78. }
  79. static void uverbs_uobject_free(struct kref *ref)
  80. {
  81. struct ib_uobject *uobj =
  82. container_of(ref, struct ib_uobject, ref);
  83. if (uobj->type->type_class->needs_kfree_rcu)
  84. kfree_rcu(uobj, rcu);
  85. else
  86. kfree(uobj);
  87. }
  88. void uverbs_uobject_put(struct ib_uobject *uobject)
  89. {
  90. kref_put(&uobject->ref, uverbs_uobject_free);
  91. }
  92. static int uverbs_try_lock_object(struct ib_uobject *uobj, bool exclusive)
  93. {
  94. /*
  95. * When a shared access is required, we use a positive counter. Each
  96. * shared access request checks that the value != -1 and increment it.
  97. * Exclusive access is required for operations like write or destroy.
  98. * In exclusive access mode, we check that the counter is zero (nobody
  99. * claimed this object) and we set it to -1. Releasing a shared access
  100. * lock is done simply by decreasing the counter. As for exclusive
  101. * access locks, since only a single one of them is is allowed
  102. * concurrently, setting the counter to zero is enough for releasing
  103. * this lock.
  104. */
  105. if (!exclusive)
  106. return __atomic_add_unless(&uobj->usecnt, 1, -1) == -1 ?
  107. -EBUSY : 0;
  108. /* lock is either WRITE or DESTROY - should be exclusive */
  109. return atomic_cmpxchg(&uobj->usecnt, 0, -1) == 0 ? 0 : -EBUSY;
  110. }
  111. static struct ib_uobject *alloc_uobj(struct ib_ucontext *context,
  112. const struct uverbs_obj_type *type)
  113. {
  114. struct ib_uobject *uobj = kzalloc(type->obj_size, GFP_KERNEL);
  115. if (!uobj)
  116. return ERR_PTR(-ENOMEM);
  117. /*
  118. * user_handle should be filled by the handler,
  119. * The object is added to the list in the commit stage.
  120. */
  121. uobj->context = context;
  122. uobj->type = type;
  123. atomic_set(&uobj->usecnt, 0);
  124. kref_init(&uobj->ref);
  125. return uobj;
  126. }
  127. static int idr_add_uobj(struct ib_uobject *uobj)
  128. {
  129. int ret;
  130. idr_preload(GFP_KERNEL);
  131. spin_lock(&uobj->context->ufile->idr_lock);
  132. /*
  133. * We start with allocating an idr pointing to NULL. This represents an
  134. * object which isn't initialized yet. We'll replace it later on with
  135. * the real object once we commit.
  136. */
  137. ret = idr_alloc(&uobj->context->ufile->idr, NULL, 0,
  138. min_t(unsigned long, U32_MAX - 1, INT_MAX), GFP_NOWAIT);
  139. if (ret >= 0)
  140. uobj->id = ret;
  141. spin_unlock(&uobj->context->ufile->idr_lock);
  142. idr_preload_end();
  143. return ret < 0 ? ret : 0;
  144. }
  145. /*
  146. * It only removes it from the uobjects list, uverbs_uobject_put() is still
  147. * required.
  148. */
  149. static void uverbs_idr_remove_uobj(struct ib_uobject *uobj)
  150. {
  151. spin_lock(&uobj->context->ufile->idr_lock);
  152. idr_remove(&uobj->context->ufile->idr, uobj->id);
  153. spin_unlock(&uobj->context->ufile->idr_lock);
  154. }
  155. /* Returns the ib_uobject or an error. The caller should check for IS_ERR. */
  156. static struct ib_uobject *lookup_get_idr_uobject(const struct uverbs_obj_type *type,
  157. struct ib_ucontext *ucontext,
  158. int id, bool exclusive)
  159. {
  160. struct ib_uobject *uobj;
  161. rcu_read_lock();
  162. /* object won't be released as we're protected in rcu */
  163. uobj = idr_find(&ucontext->ufile->idr, id);
  164. if (!uobj) {
  165. uobj = ERR_PTR(-ENOENT);
  166. goto free;
  167. }
  168. uverbs_uobject_get(uobj);
  169. free:
  170. rcu_read_unlock();
  171. return uobj;
  172. }
  173. static struct ib_uobject *lookup_get_fd_uobject(const struct uverbs_obj_type *type,
  174. struct ib_ucontext *ucontext,
  175. int id, bool exclusive)
  176. {
  177. struct file *f;
  178. struct ib_uobject *uobject;
  179. const struct uverbs_obj_fd_type *fd_type =
  180. container_of(type, struct uverbs_obj_fd_type, type);
  181. if (exclusive)
  182. return ERR_PTR(-EOPNOTSUPP);
  183. f = fget(id);
  184. if (!f)
  185. return ERR_PTR(-EBADF);
  186. uobject = f->private_data;
  187. /*
  188. * fget(id) ensures we are not currently running uverbs_close_fd,
  189. * and the caller is expected to ensure that uverbs_close_fd is never
  190. * done while a call top lookup is possible.
  191. */
  192. if (f->f_op != fd_type->fops) {
  193. fput(f);
  194. return ERR_PTR(-EBADF);
  195. }
  196. uverbs_uobject_get(uobject);
  197. return uobject;
  198. }
  199. struct ib_uobject *rdma_lookup_get_uobject(const struct uverbs_obj_type *type,
  200. struct ib_ucontext *ucontext,
  201. int id, bool exclusive)
  202. {
  203. struct ib_uobject *uobj;
  204. int ret;
  205. uobj = type->type_class->lookup_get(type, ucontext, id, exclusive);
  206. if (IS_ERR(uobj))
  207. return uobj;
  208. if (uobj->type != type) {
  209. ret = -EINVAL;
  210. goto free;
  211. }
  212. ret = uverbs_try_lock_object(uobj, exclusive);
  213. if (ret) {
  214. WARN(ucontext->cleanup_reason,
  215. "ib_uverbs: Trying to lookup_get while cleanup context\n");
  216. goto free;
  217. }
  218. return uobj;
  219. free:
  220. uobj->type->type_class->lookup_put(uobj, exclusive);
  221. uverbs_uobject_put(uobj);
  222. return ERR_PTR(ret);
  223. }
  224. static struct ib_uobject *alloc_begin_idr_uobject(const struct uverbs_obj_type *type,
  225. struct ib_ucontext *ucontext)
  226. {
  227. int ret;
  228. struct ib_uobject *uobj;
  229. uobj = alloc_uobj(ucontext, type);
  230. if (IS_ERR(uobj))
  231. return uobj;
  232. ret = idr_add_uobj(uobj);
  233. if (ret)
  234. goto uobj_put;
  235. ret = ib_rdmacg_try_charge(&uobj->cg_obj, ucontext->device,
  236. RDMACG_RESOURCE_HCA_OBJECT);
  237. if (ret)
  238. goto idr_remove;
  239. return uobj;
  240. idr_remove:
  241. uverbs_idr_remove_uobj(uobj);
  242. uobj_put:
  243. uverbs_uobject_put(uobj);
  244. return ERR_PTR(ret);
  245. }
  246. static struct ib_uobject *alloc_begin_fd_uobject(const struct uverbs_obj_type *type,
  247. struct ib_ucontext *ucontext)
  248. {
  249. const struct uverbs_obj_fd_type *fd_type =
  250. container_of(type, struct uverbs_obj_fd_type, type);
  251. int new_fd;
  252. struct ib_uobject *uobj;
  253. struct ib_uobject_file *uobj_file;
  254. struct file *filp;
  255. new_fd = get_unused_fd_flags(O_CLOEXEC);
  256. if (new_fd < 0)
  257. return ERR_PTR(new_fd);
  258. uobj = alloc_uobj(ucontext, type);
  259. if (IS_ERR(uobj)) {
  260. put_unused_fd(new_fd);
  261. return uobj;
  262. }
  263. uobj_file = container_of(uobj, struct ib_uobject_file, uobj);
  264. filp = anon_inode_getfile(fd_type->name,
  265. fd_type->fops,
  266. uobj_file,
  267. fd_type->flags);
  268. if (IS_ERR(filp)) {
  269. put_unused_fd(new_fd);
  270. uverbs_uobject_put(uobj);
  271. return (void *)filp;
  272. }
  273. uobj_file->uobj.id = new_fd;
  274. uobj_file->uobj.object = filp;
  275. uobj_file->ufile = ucontext->ufile;
  276. INIT_LIST_HEAD(&uobj->list);
  277. kref_get(&uobj_file->ufile->ref);
  278. return uobj;
  279. }
  280. struct ib_uobject *rdma_alloc_begin_uobject(const struct uverbs_obj_type *type,
  281. struct ib_ucontext *ucontext)
  282. {
  283. return type->type_class->alloc_begin(type, ucontext);
  284. }
  285. static void uverbs_uobject_add(struct ib_uobject *uobject)
  286. {
  287. mutex_lock(&uobject->context->uobjects_lock);
  288. list_add(&uobject->list, &uobject->context->uobjects);
  289. mutex_unlock(&uobject->context->uobjects_lock);
  290. }
  291. static int __must_check remove_commit_idr_uobject(struct ib_uobject *uobj,
  292. enum rdma_remove_reason why)
  293. {
  294. const struct uverbs_obj_idr_type *idr_type =
  295. container_of(uobj->type, struct uverbs_obj_idr_type,
  296. type);
  297. int ret = idr_type->destroy_object(uobj, why);
  298. /*
  299. * We can only fail gracefully if the user requested to destroy the
  300. * object. In the rest of the cases, just remove whatever you can.
  301. */
  302. if (why == RDMA_REMOVE_DESTROY && ret)
  303. return ret;
  304. ib_rdmacg_uncharge(&uobj->cg_obj, uobj->context->device,
  305. RDMACG_RESOURCE_HCA_OBJECT);
  306. uverbs_idr_remove_uobj(uobj);
  307. return ret;
  308. }
  309. static void alloc_abort_fd_uobject(struct ib_uobject *uobj)
  310. {
  311. struct ib_uobject_file *uobj_file =
  312. container_of(uobj, struct ib_uobject_file, uobj);
  313. struct file *filp = uobj->object;
  314. int id = uobj_file->uobj.id;
  315. /* Unsuccessful NEW */
  316. fput(filp);
  317. put_unused_fd(id);
  318. }
  319. static int __must_check remove_commit_fd_uobject(struct ib_uobject *uobj,
  320. enum rdma_remove_reason why)
  321. {
  322. const struct uverbs_obj_fd_type *fd_type =
  323. container_of(uobj->type, struct uverbs_obj_fd_type, type);
  324. struct ib_uobject_file *uobj_file =
  325. container_of(uobj, struct ib_uobject_file, uobj);
  326. int ret = fd_type->context_closed(uobj_file, why);
  327. if (why == RDMA_REMOVE_DESTROY && ret)
  328. return ret;
  329. if (why == RDMA_REMOVE_DURING_CLEANUP) {
  330. alloc_abort_fd_uobject(uobj);
  331. return ret;
  332. }
  333. uobj_file->uobj.context = NULL;
  334. return ret;
  335. }
  336. static void lockdep_check(struct ib_uobject *uobj, bool exclusive)
  337. {
  338. #ifdef CONFIG_LOCKDEP
  339. if (exclusive)
  340. WARN_ON(atomic_read(&uobj->usecnt) > 0);
  341. else
  342. WARN_ON(atomic_read(&uobj->usecnt) == -1);
  343. #endif
  344. }
  345. static int __must_check _rdma_remove_commit_uobject(struct ib_uobject *uobj,
  346. enum rdma_remove_reason why)
  347. {
  348. int ret;
  349. struct ib_ucontext *ucontext = uobj->context;
  350. ret = uobj->type->type_class->remove_commit(uobj, why);
  351. if (ret && why == RDMA_REMOVE_DESTROY) {
  352. /* We couldn't remove the object, so just unlock the uobject */
  353. atomic_set(&uobj->usecnt, 0);
  354. uobj->type->type_class->lookup_put(uobj, true);
  355. } else {
  356. mutex_lock(&ucontext->uobjects_lock);
  357. list_del(&uobj->list);
  358. mutex_unlock(&ucontext->uobjects_lock);
  359. /* put the ref we took when we created the object */
  360. uverbs_uobject_put(uobj);
  361. }
  362. return ret;
  363. }
  364. /* This is called only for user requested DESTROY reasons */
  365. int __must_check rdma_remove_commit_uobject(struct ib_uobject *uobj)
  366. {
  367. int ret;
  368. struct ib_ucontext *ucontext = uobj->context;
  369. /* put the ref count we took at lookup_get */
  370. uverbs_uobject_put(uobj);
  371. /* Cleanup is running. Calling this should have been impossible */
  372. if (!down_read_trylock(&ucontext->cleanup_rwsem)) {
  373. WARN(true, "ib_uverbs: Cleanup is running while removing an uobject\n");
  374. return 0;
  375. }
  376. lockdep_check(uobj, true);
  377. ret = _rdma_remove_commit_uobject(uobj, RDMA_REMOVE_DESTROY);
  378. up_read(&ucontext->cleanup_rwsem);
  379. return ret;
  380. }
  381. static int null_obj_type_class_remove_commit(struct ib_uobject *uobj,
  382. enum rdma_remove_reason why)
  383. {
  384. return 0;
  385. }
  386. static const struct uverbs_obj_type null_obj_type = {
  387. .type_class = &((const struct uverbs_obj_type_class){
  388. .remove_commit = null_obj_type_class_remove_commit,
  389. /* be cautious */
  390. .needs_kfree_rcu = true}),
  391. };
  392. int rdma_explicit_destroy(struct ib_uobject *uobject)
  393. {
  394. int ret;
  395. struct ib_ucontext *ucontext = uobject->context;
  396. /* Cleanup is running. Calling this should have been impossible */
  397. if (!down_read_trylock(&ucontext->cleanup_rwsem)) {
  398. WARN(true, "ib_uverbs: Cleanup is running while removing an uobject\n");
  399. return 0;
  400. }
  401. lockdep_check(uobject, true);
  402. ret = uobject->type->type_class->remove_commit(uobject,
  403. RDMA_REMOVE_DESTROY);
  404. if (ret)
  405. return ret;
  406. uobject->type = &null_obj_type;
  407. up_read(&ucontext->cleanup_rwsem);
  408. return 0;
  409. }
  410. static void alloc_commit_idr_uobject(struct ib_uobject *uobj)
  411. {
  412. uverbs_uobject_add(uobj);
  413. spin_lock(&uobj->context->ufile->idr_lock);
  414. /*
  415. * We already allocated this IDR with a NULL object, so
  416. * this shouldn't fail.
  417. */
  418. WARN_ON(idr_replace(&uobj->context->ufile->idr,
  419. uobj, uobj->id));
  420. spin_unlock(&uobj->context->ufile->idr_lock);
  421. }
  422. static void alloc_commit_fd_uobject(struct ib_uobject *uobj)
  423. {
  424. struct ib_uobject_file *uobj_file =
  425. container_of(uobj, struct ib_uobject_file, uobj);
  426. uverbs_uobject_add(&uobj_file->uobj);
  427. fd_install(uobj_file->uobj.id, uobj->object);
  428. /* This shouldn't be used anymore. Use the file object instead */
  429. uobj_file->uobj.id = 0;
  430. /* Get another reference as we export this to the fops */
  431. uverbs_uobject_get(&uobj_file->uobj);
  432. }
  433. int rdma_alloc_commit_uobject(struct ib_uobject *uobj)
  434. {
  435. /* Cleanup is running. Calling this should have been impossible */
  436. if (!down_read_trylock(&uobj->context->cleanup_rwsem)) {
  437. int ret;
  438. WARN(true, "ib_uverbs: Cleanup is running while allocating an uobject\n");
  439. ret = uobj->type->type_class->remove_commit(uobj,
  440. RDMA_REMOVE_DURING_CLEANUP);
  441. if (ret)
  442. pr_warn("ib_uverbs: cleanup of idr object %d failed\n",
  443. uobj->id);
  444. return ret;
  445. }
  446. uobj->type->type_class->alloc_commit(uobj);
  447. up_read(&uobj->context->cleanup_rwsem);
  448. return 0;
  449. }
  450. static void alloc_abort_idr_uobject(struct ib_uobject *uobj)
  451. {
  452. uverbs_idr_remove_uobj(uobj);
  453. ib_rdmacg_uncharge(&uobj->cg_obj, uobj->context->device,
  454. RDMACG_RESOURCE_HCA_OBJECT);
  455. uverbs_uobject_put(uobj);
  456. }
  457. void rdma_alloc_abort_uobject(struct ib_uobject *uobj)
  458. {
  459. uobj->type->type_class->alloc_abort(uobj);
  460. }
  461. static void lookup_put_idr_uobject(struct ib_uobject *uobj, bool exclusive)
  462. {
  463. }
  464. static void lookup_put_fd_uobject(struct ib_uobject *uobj, bool exclusive)
  465. {
  466. struct file *filp = uobj->object;
  467. WARN_ON(exclusive);
  468. /* This indirectly calls uverbs_close_fd and free the object */
  469. fput(filp);
  470. }
  471. void rdma_lookup_put_uobject(struct ib_uobject *uobj, bool exclusive)
  472. {
  473. lockdep_check(uobj, exclusive);
  474. uobj->type->type_class->lookup_put(uobj, exclusive);
  475. /*
  476. * In order to unlock an object, either decrease its usecnt for
  477. * read access or zero it in case of exclusive access. See
  478. * uverbs_try_lock_object for locking schema information.
  479. */
  480. if (!exclusive)
  481. atomic_dec(&uobj->usecnt);
  482. else
  483. atomic_set(&uobj->usecnt, 0);
  484. uverbs_uobject_put(uobj);
  485. }
  486. const struct uverbs_obj_type_class uverbs_idr_class = {
  487. .alloc_begin = alloc_begin_idr_uobject,
  488. .lookup_get = lookup_get_idr_uobject,
  489. .alloc_commit = alloc_commit_idr_uobject,
  490. .alloc_abort = alloc_abort_idr_uobject,
  491. .lookup_put = lookup_put_idr_uobject,
  492. .remove_commit = remove_commit_idr_uobject,
  493. /*
  494. * When we destroy an object, we first just lock it for WRITE and
  495. * actually DESTROY it in the finalize stage. So, the problematic
  496. * scenario is when we just started the finalize stage of the
  497. * destruction (nothing was executed yet). Now, the other thread
  498. * fetched the object for READ access, but it didn't lock it yet.
  499. * The DESTROY thread continues and starts destroying the object.
  500. * When the other thread continue - without the RCU, it would
  501. * access freed memory. However, the rcu_read_lock delays the free
  502. * until the rcu_read_lock of the READ operation quits. Since the
  503. * exclusive lock of the object is still taken by the DESTROY flow, the
  504. * READ operation will get -EBUSY and it'll just bail out.
  505. */
  506. .needs_kfree_rcu = true,
  507. };
  508. static void _uverbs_close_fd(struct ib_uobject_file *uobj_file)
  509. {
  510. struct ib_ucontext *ucontext;
  511. struct ib_uverbs_file *ufile = uobj_file->ufile;
  512. int ret;
  513. mutex_lock(&uobj_file->ufile->cleanup_mutex);
  514. /* uobject was either already cleaned up or is cleaned up right now anyway */
  515. if (!uobj_file->uobj.context ||
  516. !down_read_trylock(&uobj_file->uobj.context->cleanup_rwsem))
  517. goto unlock;
  518. ucontext = uobj_file->uobj.context;
  519. ret = _rdma_remove_commit_uobject(&uobj_file->uobj, RDMA_REMOVE_CLOSE);
  520. up_read(&ucontext->cleanup_rwsem);
  521. if (ret)
  522. pr_warn("uverbs: unable to clean up uobject file in uverbs_close_fd.\n");
  523. unlock:
  524. mutex_unlock(&ufile->cleanup_mutex);
  525. }
  526. void uverbs_close_fd(struct file *f)
  527. {
  528. struct ib_uobject_file *uobj_file = f->private_data;
  529. struct kref *uverbs_file_ref = &uobj_file->ufile->ref;
  530. _uverbs_close_fd(uobj_file);
  531. uverbs_uobject_put(&uobj_file->uobj);
  532. kref_put(uverbs_file_ref, ib_uverbs_release_file);
  533. }
  534. void uverbs_cleanup_ucontext(struct ib_ucontext *ucontext, bool device_removed)
  535. {
  536. enum rdma_remove_reason reason = device_removed ?
  537. RDMA_REMOVE_DRIVER_REMOVE : RDMA_REMOVE_CLOSE;
  538. unsigned int cur_order = 0;
  539. ucontext->cleanup_reason = reason;
  540. /*
  541. * Waits for all remove_commit and alloc_commit to finish. Logically, We
  542. * want to hold this forever as the context is going to be destroyed,
  543. * but we'll release it since it causes a "held lock freed" BUG message.
  544. */
  545. down_write(&ucontext->cleanup_rwsem);
  546. while (!list_empty(&ucontext->uobjects)) {
  547. struct ib_uobject *obj, *next_obj;
  548. unsigned int next_order = UINT_MAX;
  549. /*
  550. * This shouldn't run while executing other commands on this
  551. * context. Thus, the only thing we should take care of is
  552. * releasing a FD while traversing this list. The FD could be
  553. * closed and released from the _release fop of this FD.
  554. * In order to mitigate this, we add a lock.
  555. * We take and release the lock per order traversal in order
  556. * to let other threads (which might still use the FDs) chance
  557. * to run.
  558. */
  559. mutex_lock(&ucontext->uobjects_lock);
  560. list_for_each_entry_safe(obj, next_obj, &ucontext->uobjects,
  561. list) {
  562. if (obj->type->destroy_order == cur_order) {
  563. int ret;
  564. /*
  565. * if we hit this WARN_ON, that means we are
  566. * racing with a lookup_get.
  567. */
  568. WARN_ON(uverbs_try_lock_object(obj, true));
  569. ret = obj->type->type_class->remove_commit(obj,
  570. reason);
  571. list_del(&obj->list);
  572. if (ret)
  573. pr_warn("ib_uverbs: failed to remove uobject id %d order %u\n",
  574. obj->id, cur_order);
  575. /* put the ref we took when we created the object */
  576. uverbs_uobject_put(obj);
  577. } else {
  578. next_order = min(next_order,
  579. obj->type->destroy_order);
  580. }
  581. }
  582. mutex_unlock(&ucontext->uobjects_lock);
  583. cur_order = next_order;
  584. }
  585. up_write(&ucontext->cleanup_rwsem);
  586. }
  587. void uverbs_initialize_ucontext(struct ib_ucontext *ucontext)
  588. {
  589. ucontext->cleanup_reason = 0;
  590. mutex_init(&ucontext->uobjects_lock);
  591. INIT_LIST_HEAD(&ucontext->uobjects);
  592. init_rwsem(&ucontext->cleanup_rwsem);
  593. }
  594. const struct uverbs_obj_type_class uverbs_fd_class = {
  595. .alloc_begin = alloc_begin_fd_uobject,
  596. .lookup_get = lookup_get_fd_uobject,
  597. .alloc_commit = alloc_commit_fd_uobject,
  598. .alloc_abort = alloc_abort_fd_uobject,
  599. .lookup_put = lookup_put_fd_uobject,
  600. .remove_commit = remove_commit_fd_uobject,
  601. .needs_kfree_rcu = false,
  602. };
  603. struct ib_uobject *uverbs_get_uobject_from_context(const struct uverbs_obj_type *type_attrs,
  604. struct ib_ucontext *ucontext,
  605. enum uverbs_obj_access access,
  606. int id)
  607. {
  608. switch (access) {
  609. case UVERBS_ACCESS_READ:
  610. return rdma_lookup_get_uobject(type_attrs, ucontext, id, false);
  611. case UVERBS_ACCESS_DESTROY:
  612. case UVERBS_ACCESS_WRITE:
  613. return rdma_lookup_get_uobject(type_attrs, ucontext, id, true);
  614. case UVERBS_ACCESS_NEW:
  615. return rdma_alloc_begin_uobject(type_attrs, ucontext);
  616. default:
  617. WARN_ON(true);
  618. return ERR_PTR(-EOPNOTSUPP);
  619. }
  620. }
  621. int uverbs_finalize_object(struct ib_uobject *uobj,
  622. enum uverbs_obj_access access,
  623. bool commit)
  624. {
  625. int ret = 0;
  626. /*
  627. * refcounts should be handled at the object level and not at the
  628. * uobject level. Refcounts of the objects themselves are done in
  629. * handlers.
  630. */
  631. switch (access) {
  632. case UVERBS_ACCESS_READ:
  633. rdma_lookup_put_uobject(uobj, false);
  634. break;
  635. case UVERBS_ACCESS_WRITE:
  636. rdma_lookup_put_uobject(uobj, true);
  637. break;
  638. case UVERBS_ACCESS_DESTROY:
  639. if (commit)
  640. ret = rdma_remove_commit_uobject(uobj);
  641. else
  642. rdma_lookup_put_uobject(uobj, true);
  643. break;
  644. case UVERBS_ACCESS_NEW:
  645. if (commit)
  646. ret = rdma_alloc_commit_uobject(uobj);
  647. else
  648. rdma_alloc_abort_uobject(uobj);
  649. break;
  650. default:
  651. WARN_ON(true);
  652. ret = -EOPNOTSUPP;
  653. }
  654. return ret;
  655. }
  656. int uverbs_finalize_objects(struct uverbs_attr_bundle *attrs_bundle,
  657. struct uverbs_attr_spec_hash * const *spec_hash,
  658. size_t num,
  659. bool commit)
  660. {
  661. unsigned int i;
  662. int ret = 0;
  663. for (i = 0; i < num; i++) {
  664. struct uverbs_attr_bundle_hash *curr_bundle =
  665. &attrs_bundle->hash[i];
  666. const struct uverbs_attr_spec_hash *curr_spec_bucket =
  667. spec_hash[i];
  668. unsigned int j;
  669. for (j = 0; j < curr_bundle->num_attrs; j++) {
  670. struct uverbs_attr *attr;
  671. const struct uverbs_attr_spec *spec;
  672. if (!uverbs_attr_is_valid_in_hash(curr_bundle, j))
  673. continue;
  674. attr = &curr_bundle->attrs[j];
  675. spec = &curr_spec_bucket->attrs[j];
  676. if (spec->type == UVERBS_ATTR_TYPE_IDR ||
  677. spec->type == UVERBS_ATTR_TYPE_FD) {
  678. int current_ret;
  679. current_ret = uverbs_finalize_object(attr->obj_attr.uobject,
  680. spec->obj.access,
  681. commit);
  682. if (!ret)
  683. ret = current_ret;
  684. }
  685. }
  686. }
  687. return ret;
  688. }