rdma_core.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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. /*
  124. * Allocated objects start out as write locked to deny any other
  125. * syscalls from accessing them until they are committed. See
  126. * rdma_alloc_commit_uobject
  127. */
  128. atomic_set(&uobj->usecnt, -1);
  129. kref_init(&uobj->ref);
  130. return uobj;
  131. }
  132. static int idr_add_uobj(struct ib_uobject *uobj)
  133. {
  134. int ret;
  135. idr_preload(GFP_KERNEL);
  136. spin_lock(&uobj->context->ufile->idr_lock);
  137. /*
  138. * We start with allocating an idr pointing to NULL. This represents an
  139. * object which isn't initialized yet. We'll replace it later on with
  140. * the real object once we commit.
  141. */
  142. ret = idr_alloc(&uobj->context->ufile->idr, NULL, 0,
  143. min_t(unsigned long, U32_MAX - 1, INT_MAX), GFP_NOWAIT);
  144. if (ret >= 0)
  145. uobj->id = ret;
  146. spin_unlock(&uobj->context->ufile->idr_lock);
  147. idr_preload_end();
  148. return ret < 0 ? ret : 0;
  149. }
  150. /*
  151. * It only removes it from the uobjects list, uverbs_uobject_put() is still
  152. * required.
  153. */
  154. static void uverbs_idr_remove_uobj(struct ib_uobject *uobj)
  155. {
  156. spin_lock(&uobj->context->ufile->idr_lock);
  157. idr_remove(&uobj->context->ufile->idr, uobj->id);
  158. spin_unlock(&uobj->context->ufile->idr_lock);
  159. }
  160. /* Returns the ib_uobject or an error. The caller should check for IS_ERR. */
  161. static struct ib_uobject *lookup_get_idr_uobject(const struct uverbs_obj_type *type,
  162. struct ib_ucontext *ucontext,
  163. int id, bool exclusive)
  164. {
  165. struct ib_uobject *uobj;
  166. rcu_read_lock();
  167. /* object won't be released as we're protected in rcu */
  168. uobj = idr_find(&ucontext->ufile->idr, id);
  169. if (!uobj) {
  170. uobj = ERR_PTR(-ENOENT);
  171. goto free;
  172. }
  173. /*
  174. * The idr_find is guaranteed to return a pointer to something that
  175. * isn't freed yet, or NULL, as the free after idr_remove goes through
  176. * kfree_rcu(). However the object may still have been released and
  177. * kfree() could be called at any time.
  178. */
  179. if (!kref_get_unless_zero(&uobj->ref))
  180. uobj = ERR_PTR(-ENOENT);
  181. free:
  182. rcu_read_unlock();
  183. return uobj;
  184. }
  185. static struct ib_uobject *lookup_get_fd_uobject(const struct uverbs_obj_type *type,
  186. struct ib_ucontext *ucontext,
  187. int id, bool exclusive)
  188. {
  189. struct file *f;
  190. struct ib_uobject *uobject;
  191. const struct uverbs_obj_fd_type *fd_type =
  192. container_of(type, struct uverbs_obj_fd_type, type);
  193. if (exclusive)
  194. return ERR_PTR(-EOPNOTSUPP);
  195. f = fget(id);
  196. if (!f)
  197. return ERR_PTR(-EBADF);
  198. uobject = f->private_data;
  199. /*
  200. * fget(id) ensures we are not currently running uverbs_close_fd,
  201. * and the caller is expected to ensure that uverbs_close_fd is never
  202. * done while a call top lookup is possible.
  203. */
  204. if (f->f_op != fd_type->fops) {
  205. fput(f);
  206. return ERR_PTR(-EBADF);
  207. }
  208. uverbs_uobject_get(uobject);
  209. return uobject;
  210. }
  211. struct ib_uobject *rdma_lookup_get_uobject(const struct uverbs_obj_type *type,
  212. struct ib_ucontext *ucontext,
  213. int id, bool exclusive)
  214. {
  215. struct ib_uobject *uobj;
  216. int ret;
  217. uobj = type->type_class->lookup_get(type, ucontext, id, exclusive);
  218. if (IS_ERR(uobj))
  219. return uobj;
  220. if (uobj->type != type) {
  221. ret = -EINVAL;
  222. goto free;
  223. }
  224. ret = uverbs_try_lock_object(uobj, exclusive);
  225. if (ret) {
  226. WARN(ucontext->cleanup_reason,
  227. "ib_uverbs: Trying to lookup_get while cleanup context\n");
  228. goto free;
  229. }
  230. return uobj;
  231. free:
  232. uobj->type->type_class->lookup_put(uobj, exclusive);
  233. uverbs_uobject_put(uobj);
  234. return ERR_PTR(ret);
  235. }
  236. static struct ib_uobject *alloc_begin_idr_uobject(const struct uverbs_obj_type *type,
  237. struct ib_ucontext *ucontext)
  238. {
  239. int ret;
  240. struct ib_uobject *uobj;
  241. uobj = alloc_uobj(ucontext, type);
  242. if (IS_ERR(uobj))
  243. return uobj;
  244. ret = idr_add_uobj(uobj);
  245. if (ret)
  246. goto uobj_put;
  247. ret = ib_rdmacg_try_charge(&uobj->cg_obj, ucontext->device,
  248. RDMACG_RESOURCE_HCA_OBJECT);
  249. if (ret)
  250. goto idr_remove;
  251. return uobj;
  252. idr_remove:
  253. uverbs_idr_remove_uobj(uobj);
  254. uobj_put:
  255. uverbs_uobject_put(uobj);
  256. return ERR_PTR(ret);
  257. }
  258. static struct ib_uobject *alloc_begin_fd_uobject(const struct uverbs_obj_type *type,
  259. struct ib_ucontext *ucontext)
  260. {
  261. const struct uverbs_obj_fd_type *fd_type =
  262. container_of(type, struct uverbs_obj_fd_type, type);
  263. int new_fd;
  264. struct ib_uobject *uobj;
  265. struct ib_uobject_file *uobj_file;
  266. struct file *filp;
  267. new_fd = get_unused_fd_flags(O_CLOEXEC);
  268. if (new_fd < 0)
  269. return ERR_PTR(new_fd);
  270. uobj = alloc_uobj(ucontext, type);
  271. if (IS_ERR(uobj)) {
  272. put_unused_fd(new_fd);
  273. return uobj;
  274. }
  275. uobj_file = container_of(uobj, struct ib_uobject_file, uobj);
  276. filp = anon_inode_getfile(fd_type->name,
  277. fd_type->fops,
  278. uobj_file,
  279. fd_type->flags);
  280. if (IS_ERR(filp)) {
  281. put_unused_fd(new_fd);
  282. uverbs_uobject_put(uobj);
  283. return (void *)filp;
  284. }
  285. uobj_file->uobj.id = new_fd;
  286. uobj_file->uobj.object = filp;
  287. uobj_file->ufile = ucontext->ufile;
  288. INIT_LIST_HEAD(&uobj->list);
  289. kref_get(&uobj_file->ufile->ref);
  290. return uobj;
  291. }
  292. struct ib_uobject *rdma_alloc_begin_uobject(const struct uverbs_obj_type *type,
  293. struct ib_ucontext *ucontext)
  294. {
  295. return type->type_class->alloc_begin(type, ucontext);
  296. }
  297. static int __must_check remove_commit_idr_uobject(struct ib_uobject *uobj,
  298. enum rdma_remove_reason why)
  299. {
  300. const struct uverbs_obj_idr_type *idr_type =
  301. container_of(uobj->type, struct uverbs_obj_idr_type,
  302. type);
  303. int ret = idr_type->destroy_object(uobj, why);
  304. /*
  305. * We can only fail gracefully if the user requested to destroy the
  306. * object. In the rest of the cases, just remove whatever you can.
  307. */
  308. if (why == RDMA_REMOVE_DESTROY && ret)
  309. return ret;
  310. ib_rdmacg_uncharge(&uobj->cg_obj, uobj->context->device,
  311. RDMACG_RESOURCE_HCA_OBJECT);
  312. uverbs_idr_remove_uobj(uobj);
  313. return ret;
  314. }
  315. static void alloc_abort_fd_uobject(struct ib_uobject *uobj)
  316. {
  317. struct ib_uobject_file *uobj_file =
  318. container_of(uobj, struct ib_uobject_file, uobj);
  319. struct file *filp = uobj->object;
  320. int id = uobj_file->uobj.id;
  321. /* Unsuccessful NEW */
  322. fput(filp);
  323. put_unused_fd(id);
  324. }
  325. static int __must_check remove_commit_fd_uobject(struct ib_uobject *uobj,
  326. enum rdma_remove_reason why)
  327. {
  328. const struct uverbs_obj_fd_type *fd_type =
  329. container_of(uobj->type, struct uverbs_obj_fd_type, type);
  330. struct ib_uobject_file *uobj_file =
  331. container_of(uobj, struct ib_uobject_file, uobj);
  332. int ret = fd_type->context_closed(uobj_file, why);
  333. if (why == RDMA_REMOVE_DESTROY && ret)
  334. return ret;
  335. if (why == RDMA_REMOVE_DURING_CLEANUP) {
  336. alloc_abort_fd_uobject(uobj);
  337. return ret;
  338. }
  339. uobj_file->uobj.context = NULL;
  340. return ret;
  341. }
  342. static void assert_uverbs_usecnt(struct ib_uobject *uobj, bool exclusive)
  343. {
  344. #ifdef CONFIG_LOCKDEP
  345. if (exclusive)
  346. WARN_ON(atomic_read(&uobj->usecnt) != -1);
  347. else
  348. WARN_ON(atomic_read(&uobj->usecnt) <= 0);
  349. #endif
  350. }
  351. static int __must_check _rdma_remove_commit_uobject(struct ib_uobject *uobj,
  352. enum rdma_remove_reason why)
  353. {
  354. int ret;
  355. struct ib_ucontext *ucontext = uobj->context;
  356. ret = uobj->type->type_class->remove_commit(uobj, why);
  357. if (ret && why == RDMA_REMOVE_DESTROY) {
  358. /* We couldn't remove the object, so just unlock the uobject */
  359. atomic_set(&uobj->usecnt, 0);
  360. uobj->type->type_class->lookup_put(uobj, true);
  361. } else {
  362. mutex_lock(&ucontext->uobjects_lock);
  363. list_del(&uobj->list);
  364. mutex_unlock(&ucontext->uobjects_lock);
  365. /* put the ref we took when we created the object */
  366. uverbs_uobject_put(uobj);
  367. }
  368. return ret;
  369. }
  370. /* This is called only for user requested DESTROY reasons */
  371. int __must_check rdma_remove_commit_uobject(struct ib_uobject *uobj)
  372. {
  373. int ret;
  374. struct ib_ucontext *ucontext = uobj->context;
  375. /* put the ref count we took at lookup_get */
  376. uverbs_uobject_put(uobj);
  377. /* Cleanup is running. Calling this should have been impossible */
  378. if (!down_read_trylock(&ucontext->cleanup_rwsem)) {
  379. WARN(true, "ib_uverbs: Cleanup is running while removing an uobject\n");
  380. return 0;
  381. }
  382. assert_uverbs_usecnt(uobj, true);
  383. ret = _rdma_remove_commit_uobject(uobj, RDMA_REMOVE_DESTROY);
  384. up_read(&ucontext->cleanup_rwsem);
  385. return ret;
  386. }
  387. static int null_obj_type_class_remove_commit(struct ib_uobject *uobj,
  388. enum rdma_remove_reason why)
  389. {
  390. return 0;
  391. }
  392. static const struct uverbs_obj_type null_obj_type = {
  393. .type_class = &((const struct uverbs_obj_type_class){
  394. .remove_commit = null_obj_type_class_remove_commit,
  395. /* be cautious */
  396. .needs_kfree_rcu = true}),
  397. };
  398. int rdma_explicit_destroy(struct ib_uobject *uobject)
  399. {
  400. int ret;
  401. struct ib_ucontext *ucontext = uobject->context;
  402. /* Cleanup is running. Calling this should have been impossible */
  403. if (!down_read_trylock(&ucontext->cleanup_rwsem)) {
  404. WARN(true, "ib_uverbs: Cleanup is running while removing an uobject\n");
  405. return 0;
  406. }
  407. assert_uverbs_usecnt(uobject, true);
  408. ret = uobject->type->type_class->remove_commit(uobject,
  409. RDMA_REMOVE_DESTROY);
  410. if (ret)
  411. goto out;
  412. uobject->type = &null_obj_type;
  413. out:
  414. up_read(&ucontext->cleanup_rwsem);
  415. return ret;
  416. }
  417. static void alloc_commit_idr_uobject(struct ib_uobject *uobj)
  418. {
  419. spin_lock(&uobj->context->ufile->idr_lock);
  420. /*
  421. * We already allocated this IDR with a NULL object, so
  422. * this shouldn't fail.
  423. */
  424. WARN_ON(idr_replace(&uobj->context->ufile->idr,
  425. uobj, uobj->id));
  426. spin_unlock(&uobj->context->ufile->idr_lock);
  427. }
  428. static void alloc_commit_fd_uobject(struct ib_uobject *uobj)
  429. {
  430. struct ib_uobject_file *uobj_file =
  431. container_of(uobj, struct ib_uobject_file, uobj);
  432. fd_install(uobj_file->uobj.id, uobj->object);
  433. /* This shouldn't be used anymore. Use the file object instead */
  434. uobj_file->uobj.id = 0;
  435. /* Get another reference as we export this to the fops */
  436. uverbs_uobject_get(&uobj_file->uobj);
  437. }
  438. int rdma_alloc_commit_uobject(struct ib_uobject *uobj)
  439. {
  440. /* Cleanup is running. Calling this should have been impossible */
  441. if (!down_read_trylock(&uobj->context->cleanup_rwsem)) {
  442. int ret;
  443. WARN(true, "ib_uverbs: Cleanup is running while allocating an uobject\n");
  444. ret = uobj->type->type_class->remove_commit(uobj,
  445. RDMA_REMOVE_DURING_CLEANUP);
  446. if (ret)
  447. pr_warn("ib_uverbs: cleanup of idr object %d failed\n",
  448. uobj->id);
  449. return ret;
  450. }
  451. /* matches atomic_set(-1) in alloc_uobj */
  452. assert_uverbs_usecnt(uobj, true);
  453. atomic_set(&uobj->usecnt, 0);
  454. mutex_lock(&uobj->context->uobjects_lock);
  455. list_add(&uobj->list, &uobj->context->uobjects);
  456. mutex_unlock(&uobj->context->uobjects_lock);
  457. uobj->type->type_class->alloc_commit(uobj);
  458. up_read(&uobj->context->cleanup_rwsem);
  459. return 0;
  460. }
  461. static void alloc_abort_idr_uobject(struct ib_uobject *uobj)
  462. {
  463. uverbs_idr_remove_uobj(uobj);
  464. ib_rdmacg_uncharge(&uobj->cg_obj, uobj->context->device,
  465. RDMACG_RESOURCE_HCA_OBJECT);
  466. uverbs_uobject_put(uobj);
  467. }
  468. void rdma_alloc_abort_uobject(struct ib_uobject *uobj)
  469. {
  470. uobj->type->type_class->alloc_abort(uobj);
  471. }
  472. static void lookup_put_idr_uobject(struct ib_uobject *uobj, bool exclusive)
  473. {
  474. }
  475. static void lookup_put_fd_uobject(struct ib_uobject *uobj, bool exclusive)
  476. {
  477. struct file *filp = uobj->object;
  478. WARN_ON(exclusive);
  479. /* This indirectly calls uverbs_close_fd and free the object */
  480. fput(filp);
  481. }
  482. void rdma_lookup_put_uobject(struct ib_uobject *uobj, bool exclusive)
  483. {
  484. assert_uverbs_usecnt(uobj, exclusive);
  485. uobj->type->type_class->lookup_put(uobj, exclusive);
  486. /*
  487. * In order to unlock an object, either decrease its usecnt for
  488. * read access or zero it in case of exclusive access. See
  489. * uverbs_try_lock_object for locking schema information.
  490. */
  491. if (!exclusive)
  492. atomic_dec(&uobj->usecnt);
  493. else
  494. atomic_set(&uobj->usecnt, 0);
  495. uverbs_uobject_put(uobj);
  496. }
  497. const struct uverbs_obj_type_class uverbs_idr_class = {
  498. .alloc_begin = alloc_begin_idr_uobject,
  499. .lookup_get = lookup_get_idr_uobject,
  500. .alloc_commit = alloc_commit_idr_uobject,
  501. .alloc_abort = alloc_abort_idr_uobject,
  502. .lookup_put = lookup_put_idr_uobject,
  503. .remove_commit = remove_commit_idr_uobject,
  504. /*
  505. * When we destroy an object, we first just lock it for WRITE and
  506. * actually DESTROY it in the finalize stage. So, the problematic
  507. * scenario is when we just started the finalize stage of the
  508. * destruction (nothing was executed yet). Now, the other thread
  509. * fetched the object for READ access, but it didn't lock it yet.
  510. * The DESTROY thread continues and starts destroying the object.
  511. * When the other thread continue - without the RCU, it would
  512. * access freed memory. However, the rcu_read_lock delays the free
  513. * until the rcu_read_lock of the READ operation quits. Since the
  514. * exclusive lock of the object is still taken by the DESTROY flow, the
  515. * READ operation will get -EBUSY and it'll just bail out.
  516. */
  517. .needs_kfree_rcu = true,
  518. };
  519. static void _uverbs_close_fd(struct ib_uobject_file *uobj_file)
  520. {
  521. struct ib_ucontext *ucontext;
  522. struct ib_uverbs_file *ufile = uobj_file->ufile;
  523. int ret;
  524. mutex_lock(&uobj_file->ufile->cleanup_mutex);
  525. /* uobject was either already cleaned up or is cleaned up right now anyway */
  526. if (!uobj_file->uobj.context ||
  527. !down_read_trylock(&uobj_file->uobj.context->cleanup_rwsem))
  528. goto unlock;
  529. ucontext = uobj_file->uobj.context;
  530. ret = _rdma_remove_commit_uobject(&uobj_file->uobj, RDMA_REMOVE_CLOSE);
  531. up_read(&ucontext->cleanup_rwsem);
  532. if (ret)
  533. pr_warn("uverbs: unable to clean up uobject file in uverbs_close_fd.\n");
  534. unlock:
  535. mutex_unlock(&ufile->cleanup_mutex);
  536. }
  537. void uverbs_close_fd(struct file *f)
  538. {
  539. struct ib_uobject_file *uobj_file = f->private_data;
  540. struct kref *uverbs_file_ref = &uobj_file->ufile->ref;
  541. _uverbs_close_fd(uobj_file);
  542. uverbs_uobject_put(&uobj_file->uobj);
  543. kref_put(uverbs_file_ref, ib_uverbs_release_file);
  544. }
  545. void uverbs_cleanup_ucontext(struct ib_ucontext *ucontext, bool device_removed)
  546. {
  547. enum rdma_remove_reason reason = device_removed ?
  548. RDMA_REMOVE_DRIVER_REMOVE : RDMA_REMOVE_CLOSE;
  549. unsigned int cur_order = 0;
  550. ucontext->cleanup_reason = reason;
  551. /*
  552. * Waits for all remove_commit and alloc_commit to finish. Logically, We
  553. * want to hold this forever as the context is going to be destroyed,
  554. * but we'll release it since it causes a "held lock freed" BUG message.
  555. */
  556. down_write(&ucontext->cleanup_rwsem);
  557. while (!list_empty(&ucontext->uobjects)) {
  558. struct ib_uobject *obj, *next_obj;
  559. unsigned int next_order = UINT_MAX;
  560. /*
  561. * This shouldn't run while executing other commands on this
  562. * context. Thus, the only thing we should take care of is
  563. * releasing a FD while traversing this list. The FD could be
  564. * closed and released from the _release fop of this FD.
  565. * In order to mitigate this, we add a lock.
  566. * We take and release the lock per order traversal in order
  567. * to let other threads (which might still use the FDs) chance
  568. * to run.
  569. */
  570. mutex_lock(&ucontext->uobjects_lock);
  571. list_for_each_entry_safe(obj, next_obj, &ucontext->uobjects,
  572. list) {
  573. if (obj->type->destroy_order == cur_order) {
  574. int ret;
  575. /*
  576. * if we hit this WARN_ON, that means we are
  577. * racing with a lookup_get.
  578. */
  579. WARN_ON(uverbs_try_lock_object(obj, true));
  580. ret = obj->type->type_class->remove_commit(obj,
  581. reason);
  582. list_del(&obj->list);
  583. if (ret)
  584. pr_warn("ib_uverbs: failed to remove uobject id %d order %u\n",
  585. obj->id, cur_order);
  586. /* put the ref we took when we created the object */
  587. uverbs_uobject_put(obj);
  588. } else {
  589. next_order = min(next_order,
  590. obj->type->destroy_order);
  591. }
  592. }
  593. mutex_unlock(&ucontext->uobjects_lock);
  594. cur_order = next_order;
  595. }
  596. up_write(&ucontext->cleanup_rwsem);
  597. }
  598. void uverbs_initialize_ucontext(struct ib_ucontext *ucontext)
  599. {
  600. ucontext->cleanup_reason = 0;
  601. mutex_init(&ucontext->uobjects_lock);
  602. INIT_LIST_HEAD(&ucontext->uobjects);
  603. init_rwsem(&ucontext->cleanup_rwsem);
  604. }
  605. const struct uverbs_obj_type_class uverbs_fd_class = {
  606. .alloc_begin = alloc_begin_fd_uobject,
  607. .lookup_get = lookup_get_fd_uobject,
  608. .alloc_commit = alloc_commit_fd_uobject,
  609. .alloc_abort = alloc_abort_fd_uobject,
  610. .lookup_put = lookup_put_fd_uobject,
  611. .remove_commit = remove_commit_fd_uobject,
  612. .needs_kfree_rcu = false,
  613. };
  614. struct ib_uobject *uverbs_get_uobject_from_context(const struct uverbs_obj_type *type_attrs,
  615. struct ib_ucontext *ucontext,
  616. enum uverbs_obj_access access,
  617. int id)
  618. {
  619. switch (access) {
  620. case UVERBS_ACCESS_READ:
  621. return rdma_lookup_get_uobject(type_attrs, ucontext, id, false);
  622. case UVERBS_ACCESS_DESTROY:
  623. case UVERBS_ACCESS_WRITE:
  624. return rdma_lookup_get_uobject(type_attrs, ucontext, id, true);
  625. case UVERBS_ACCESS_NEW:
  626. return rdma_alloc_begin_uobject(type_attrs, ucontext);
  627. default:
  628. WARN_ON(true);
  629. return ERR_PTR(-EOPNOTSUPP);
  630. }
  631. }
  632. int uverbs_finalize_object(struct ib_uobject *uobj,
  633. enum uverbs_obj_access access,
  634. bool commit)
  635. {
  636. int ret = 0;
  637. /*
  638. * refcounts should be handled at the object level and not at the
  639. * uobject level. Refcounts of the objects themselves are done in
  640. * handlers.
  641. */
  642. switch (access) {
  643. case UVERBS_ACCESS_READ:
  644. rdma_lookup_put_uobject(uobj, false);
  645. break;
  646. case UVERBS_ACCESS_WRITE:
  647. rdma_lookup_put_uobject(uobj, true);
  648. break;
  649. case UVERBS_ACCESS_DESTROY:
  650. if (commit)
  651. ret = rdma_remove_commit_uobject(uobj);
  652. else
  653. rdma_lookup_put_uobject(uobj, true);
  654. break;
  655. case UVERBS_ACCESS_NEW:
  656. if (commit)
  657. ret = rdma_alloc_commit_uobject(uobj);
  658. else
  659. rdma_alloc_abort_uobject(uobj);
  660. break;
  661. default:
  662. WARN_ON(true);
  663. ret = -EOPNOTSUPP;
  664. }
  665. return ret;
  666. }
  667. int uverbs_finalize_objects(struct uverbs_attr_bundle *attrs_bundle,
  668. struct uverbs_attr_spec_hash * const *spec_hash,
  669. size_t num,
  670. bool commit)
  671. {
  672. unsigned int i;
  673. int ret = 0;
  674. for (i = 0; i < num; i++) {
  675. struct uverbs_attr_bundle_hash *curr_bundle =
  676. &attrs_bundle->hash[i];
  677. const struct uverbs_attr_spec_hash *curr_spec_bucket =
  678. spec_hash[i];
  679. unsigned int j;
  680. for (j = 0; j < curr_bundle->num_attrs; j++) {
  681. struct uverbs_attr *attr;
  682. const struct uverbs_attr_spec *spec;
  683. if (!uverbs_attr_is_valid_in_hash(curr_bundle, j))
  684. continue;
  685. attr = &curr_bundle->attrs[j];
  686. spec = &curr_spec_bucket->attrs[j];
  687. if (spec->type == UVERBS_ATTR_TYPE_IDR ||
  688. spec->type == UVERBS_ATTR_TYPE_FD) {
  689. int current_ret;
  690. current_ret = uverbs_finalize_object(attr->obj_attr.uobject,
  691. spec->obj.access,
  692. commit);
  693. if (!ret)
  694. ret = current_ret;
  695. }
  696. }
  697. }
  698. return ret;
  699. }