1
0

rdma_core.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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 <linux/sched/mm.h>
  35. #include <rdma/ib_verbs.h>
  36. #include <rdma/uverbs_types.h>
  37. #include <linux/rcupdate.h>
  38. #include <rdma/uverbs_ioctl.h>
  39. #include <rdma/rdma_user_ioctl.h>
  40. #include "uverbs.h"
  41. #include "core_priv.h"
  42. #include "rdma_core.h"
  43. void uverbs_uobject_get(struct ib_uobject *uobject)
  44. {
  45. kref_get(&uobject->ref);
  46. }
  47. static void uverbs_uobject_free(struct kref *ref)
  48. {
  49. struct ib_uobject *uobj =
  50. container_of(ref, struct ib_uobject, ref);
  51. if (uobj->uapi_object->type_class->needs_kfree_rcu)
  52. kfree_rcu(uobj, rcu);
  53. else
  54. kfree(uobj);
  55. }
  56. void uverbs_uobject_put(struct ib_uobject *uobject)
  57. {
  58. kref_put(&uobject->ref, uverbs_uobject_free);
  59. }
  60. static int uverbs_try_lock_object(struct ib_uobject *uobj,
  61. enum rdma_lookup_mode mode)
  62. {
  63. /*
  64. * When a shared access is required, we use a positive counter. Each
  65. * shared access request checks that the value != -1 and increment it.
  66. * Exclusive access is required for operations like write or destroy.
  67. * In exclusive access mode, we check that the counter is zero (nobody
  68. * claimed this object) and we set it to -1. Releasing a shared access
  69. * lock is done simply by decreasing the counter. As for exclusive
  70. * access locks, since only a single one of them is is allowed
  71. * concurrently, setting the counter to zero is enough for releasing
  72. * this lock.
  73. */
  74. switch (mode) {
  75. case UVERBS_LOOKUP_READ:
  76. return atomic_fetch_add_unless(&uobj->usecnt, 1, -1) == -1 ?
  77. -EBUSY : 0;
  78. case UVERBS_LOOKUP_WRITE:
  79. /* lock is exclusive */
  80. return atomic_cmpxchg(&uobj->usecnt, 0, -1) == 0 ? 0 : -EBUSY;
  81. case UVERBS_LOOKUP_DESTROY:
  82. return 0;
  83. }
  84. return 0;
  85. }
  86. static void assert_uverbs_usecnt(struct ib_uobject *uobj,
  87. enum rdma_lookup_mode mode)
  88. {
  89. #ifdef CONFIG_LOCKDEP
  90. switch (mode) {
  91. case UVERBS_LOOKUP_READ:
  92. WARN_ON(atomic_read(&uobj->usecnt) <= 0);
  93. break;
  94. case UVERBS_LOOKUP_WRITE:
  95. WARN_ON(atomic_read(&uobj->usecnt) != -1);
  96. break;
  97. case UVERBS_LOOKUP_DESTROY:
  98. break;
  99. }
  100. #endif
  101. }
  102. /*
  103. * This must be called with the hw_destroy_rwsem locked for read or write,
  104. * also the uobject itself must be locked for write.
  105. *
  106. * Upon return the HW object is guaranteed to be destroyed.
  107. *
  108. * For RDMA_REMOVE_ABORT, the hw_destroy_rwsem is not required to be held,
  109. * however the type's allocat_commit function cannot have been called and the
  110. * uobject cannot be on the uobjects_lists
  111. *
  112. * For RDMA_REMOVE_DESTROY the caller shold be holding a kref (eg via
  113. * rdma_lookup_get_uobject) and the object is left in a state where the caller
  114. * needs to call rdma_lookup_put_uobject.
  115. *
  116. * For all other destroy modes this function internally unlocks the uobject
  117. * and consumes the kref on the uobj.
  118. */
  119. static int uverbs_destroy_uobject(struct ib_uobject *uobj,
  120. enum rdma_remove_reason reason)
  121. {
  122. struct ib_uverbs_file *ufile = uobj->ufile;
  123. unsigned long flags;
  124. int ret;
  125. lockdep_assert_held(&ufile->hw_destroy_rwsem);
  126. assert_uverbs_usecnt(uobj, UVERBS_LOOKUP_WRITE);
  127. if (uobj->object) {
  128. ret = uobj->uapi_object->type_class->destroy_hw(uobj, reason);
  129. if (ret) {
  130. if (ib_is_destroy_retryable(ret, reason, uobj))
  131. return ret;
  132. /* Nothing to be done, dangle the memory and move on */
  133. WARN(true,
  134. "ib_uverbs: failed to remove uobject id %d, driver err=%d",
  135. uobj->id, ret);
  136. }
  137. uobj->object = NULL;
  138. }
  139. if (reason == RDMA_REMOVE_ABORT) {
  140. WARN_ON(!list_empty(&uobj->list));
  141. WARN_ON(!uobj->context);
  142. uobj->uapi_object->type_class->alloc_abort(uobj);
  143. }
  144. uobj->context = NULL;
  145. /*
  146. * For DESTROY the usecnt is not changed, the caller is expected to
  147. * manage it via uobj_put_destroy(). Only DESTROY can remove the IDR
  148. * handle.
  149. */
  150. if (reason != RDMA_REMOVE_DESTROY)
  151. atomic_set(&uobj->usecnt, 0);
  152. else
  153. uobj->uapi_object->type_class->remove_handle(uobj);
  154. if (!list_empty(&uobj->list)) {
  155. spin_lock_irqsave(&ufile->uobjects_lock, flags);
  156. list_del_init(&uobj->list);
  157. spin_unlock_irqrestore(&ufile->uobjects_lock, flags);
  158. /*
  159. * Pairs with the get in rdma_alloc_commit_uobject(), could
  160. * destroy uobj.
  161. */
  162. uverbs_uobject_put(uobj);
  163. }
  164. /*
  165. * When aborting the stack kref remains owned by the core code, and is
  166. * not transferred into the type. Pairs with the get in alloc_uobj
  167. */
  168. if (reason == RDMA_REMOVE_ABORT)
  169. uverbs_uobject_put(uobj);
  170. return 0;
  171. }
  172. /*
  173. * This calls uverbs_destroy_uobject() using the RDMA_REMOVE_DESTROY
  174. * sequence. It should only be used from command callbacks. On success the
  175. * caller must pair this with uobj_put_destroy(). This
  176. * version requires the caller to have already obtained an
  177. * LOOKUP_DESTROY uobject kref.
  178. */
  179. int uobj_destroy(struct ib_uobject *uobj)
  180. {
  181. struct ib_uverbs_file *ufile = uobj->ufile;
  182. int ret;
  183. down_read(&ufile->hw_destroy_rwsem);
  184. /*
  185. * Once the uobject is destroyed by RDMA_REMOVE_DESTROY then it is left
  186. * write locked as the callers put it back with UVERBS_LOOKUP_DESTROY.
  187. * This is because any other concurrent thread can still see the object
  188. * in the xarray due to RCU. Leaving it locked ensures nothing else will
  189. * touch it.
  190. */
  191. ret = uverbs_try_lock_object(uobj, UVERBS_LOOKUP_WRITE);
  192. if (ret)
  193. goto out_unlock;
  194. ret = uverbs_destroy_uobject(uobj, RDMA_REMOVE_DESTROY);
  195. if (ret) {
  196. atomic_set(&uobj->usecnt, 0);
  197. goto out_unlock;
  198. }
  199. out_unlock:
  200. up_read(&ufile->hw_destroy_rwsem);
  201. return ret;
  202. }
  203. /*
  204. * uobj_get_destroy destroys the HW object and returns a handle to the uobj
  205. * with a NULL object pointer. The caller must pair this with
  206. * uobj_put_destroy().
  207. */
  208. struct ib_uobject *__uobj_get_destroy(const struct uverbs_api_object *obj,
  209. u32 id, struct ib_uverbs_file *ufile)
  210. {
  211. struct ib_uobject *uobj;
  212. int ret;
  213. uobj = rdma_lookup_get_uobject(obj, ufile, id, UVERBS_LOOKUP_DESTROY);
  214. if (IS_ERR(uobj))
  215. return uobj;
  216. ret = uobj_destroy(uobj);
  217. if (ret) {
  218. rdma_lookup_put_uobject(uobj, UVERBS_LOOKUP_DESTROY);
  219. return ERR_PTR(ret);
  220. }
  221. return uobj;
  222. }
  223. /*
  224. * Does both uobj_get_destroy() and uobj_put_destroy(). Returns success_res
  225. * on success (negative errno on failure). For use by callers that do not need
  226. * the uobj.
  227. */
  228. int __uobj_perform_destroy(const struct uverbs_api_object *obj, u32 id,
  229. struct ib_uverbs_file *ufile, int success_res)
  230. {
  231. struct ib_uobject *uobj;
  232. uobj = __uobj_get_destroy(obj, id, ufile);
  233. if (IS_ERR(uobj))
  234. return PTR_ERR(uobj);
  235. uobj_put_destroy(uobj);
  236. return success_res;
  237. }
  238. /* alloc_uobj must be undone by uverbs_destroy_uobject() */
  239. static struct ib_uobject *alloc_uobj(struct ib_uverbs_file *ufile,
  240. const struct uverbs_api_object *obj)
  241. {
  242. struct ib_uobject *uobj;
  243. struct ib_ucontext *ucontext;
  244. ucontext = ib_uverbs_get_ucontext(ufile);
  245. if (IS_ERR(ucontext))
  246. return ERR_CAST(ucontext);
  247. uobj = kzalloc(obj->type_attrs->obj_size, GFP_KERNEL);
  248. if (!uobj)
  249. return ERR_PTR(-ENOMEM);
  250. /*
  251. * user_handle should be filled by the handler,
  252. * The object is added to the list in the commit stage.
  253. */
  254. uobj->ufile = ufile;
  255. uobj->context = ucontext;
  256. INIT_LIST_HEAD(&uobj->list);
  257. uobj->uapi_object = obj;
  258. /*
  259. * Allocated objects start out as write locked to deny any other
  260. * syscalls from accessing them until they are committed. See
  261. * rdma_alloc_commit_uobject
  262. */
  263. atomic_set(&uobj->usecnt, -1);
  264. kref_init(&uobj->ref);
  265. return uobj;
  266. }
  267. static int idr_add_uobj(struct ib_uobject *uobj)
  268. {
  269. int ret;
  270. idr_preload(GFP_KERNEL);
  271. spin_lock(&uobj->ufile->idr_lock);
  272. /*
  273. * We start with allocating an idr pointing to NULL. This represents an
  274. * object which isn't initialized yet. We'll replace it later on with
  275. * the real object once we commit.
  276. */
  277. ret = idr_alloc(&uobj->ufile->idr, NULL, 0,
  278. min_t(unsigned long, U32_MAX - 1, INT_MAX), GFP_NOWAIT);
  279. if (ret >= 0)
  280. uobj->id = ret;
  281. spin_unlock(&uobj->ufile->idr_lock);
  282. idr_preload_end();
  283. return ret < 0 ? ret : 0;
  284. }
  285. /* Returns the ib_uobject or an error. The caller should check for IS_ERR. */
  286. static struct ib_uobject *
  287. lookup_get_idr_uobject(const struct uverbs_api_object *obj,
  288. struct ib_uverbs_file *ufile, s64 id,
  289. enum rdma_lookup_mode mode)
  290. {
  291. struct ib_uobject *uobj;
  292. unsigned long idrno = id;
  293. if (id < 0 || id > ULONG_MAX)
  294. return ERR_PTR(-EINVAL);
  295. rcu_read_lock();
  296. /* object won't be released as we're protected in rcu */
  297. uobj = idr_find(&ufile->idr, idrno);
  298. if (!uobj) {
  299. uobj = ERR_PTR(-ENOENT);
  300. goto free;
  301. }
  302. /*
  303. * The idr_find is guaranteed to return a pointer to something that
  304. * isn't freed yet, or NULL, as the free after idr_remove goes through
  305. * kfree_rcu(). However the object may still have been released and
  306. * kfree() could be called at any time.
  307. */
  308. if (!kref_get_unless_zero(&uobj->ref))
  309. uobj = ERR_PTR(-ENOENT);
  310. free:
  311. rcu_read_unlock();
  312. return uobj;
  313. }
  314. static struct ib_uobject *
  315. lookup_get_fd_uobject(const struct uverbs_api_object *obj,
  316. struct ib_uverbs_file *ufile, s64 id,
  317. enum rdma_lookup_mode mode)
  318. {
  319. const struct uverbs_obj_fd_type *fd_type;
  320. struct file *f;
  321. struct ib_uobject *uobject;
  322. int fdno = id;
  323. if (fdno != id)
  324. return ERR_PTR(-EINVAL);
  325. if (mode != UVERBS_LOOKUP_READ)
  326. return ERR_PTR(-EOPNOTSUPP);
  327. if (!obj->type_attrs)
  328. return ERR_PTR(-EIO);
  329. fd_type =
  330. container_of(obj->type_attrs, struct uverbs_obj_fd_type, type);
  331. f = fget(fdno);
  332. if (!f)
  333. return ERR_PTR(-EBADF);
  334. uobject = f->private_data;
  335. /*
  336. * fget(id) ensures we are not currently running uverbs_close_fd,
  337. * and the caller is expected to ensure that uverbs_close_fd is never
  338. * done while a call top lookup is possible.
  339. */
  340. if (f->f_op != fd_type->fops || uobject->ufile != ufile) {
  341. fput(f);
  342. return ERR_PTR(-EBADF);
  343. }
  344. uverbs_uobject_get(uobject);
  345. return uobject;
  346. }
  347. struct ib_uobject *rdma_lookup_get_uobject(const struct uverbs_api_object *obj,
  348. struct ib_uverbs_file *ufile, s64 id,
  349. enum rdma_lookup_mode mode)
  350. {
  351. struct ib_uobject *uobj;
  352. int ret;
  353. if (!obj)
  354. return ERR_PTR(-EINVAL);
  355. uobj = obj->type_class->lookup_get(obj, ufile, id, mode);
  356. if (IS_ERR(uobj))
  357. return uobj;
  358. if (uobj->uapi_object != obj) {
  359. ret = -EINVAL;
  360. goto free;
  361. }
  362. /*
  363. * If we have been disassociated block every command except for
  364. * DESTROY based commands.
  365. */
  366. if (mode != UVERBS_LOOKUP_DESTROY &&
  367. !srcu_dereference(ufile->device->ib_dev,
  368. &ufile->device->disassociate_srcu)) {
  369. ret = -EIO;
  370. goto free;
  371. }
  372. ret = uverbs_try_lock_object(uobj, mode);
  373. if (ret)
  374. goto free;
  375. return uobj;
  376. free:
  377. obj->type_class->lookup_put(uobj, mode);
  378. uverbs_uobject_put(uobj);
  379. return ERR_PTR(ret);
  380. }
  381. static struct ib_uobject *
  382. alloc_begin_idr_uobject(const struct uverbs_api_object *obj,
  383. struct ib_uverbs_file *ufile)
  384. {
  385. int ret;
  386. struct ib_uobject *uobj;
  387. uobj = alloc_uobj(ufile, obj);
  388. if (IS_ERR(uobj))
  389. return uobj;
  390. ret = idr_add_uobj(uobj);
  391. if (ret)
  392. goto uobj_put;
  393. ret = ib_rdmacg_try_charge(&uobj->cg_obj, uobj->context->device,
  394. RDMACG_RESOURCE_HCA_OBJECT);
  395. if (ret)
  396. goto idr_remove;
  397. return uobj;
  398. idr_remove:
  399. spin_lock(&ufile->idr_lock);
  400. idr_remove(&ufile->idr, uobj->id);
  401. spin_unlock(&ufile->idr_lock);
  402. uobj_put:
  403. uverbs_uobject_put(uobj);
  404. return ERR_PTR(ret);
  405. }
  406. static struct ib_uobject *
  407. alloc_begin_fd_uobject(const struct uverbs_api_object *obj,
  408. struct ib_uverbs_file *ufile)
  409. {
  410. int new_fd;
  411. struct ib_uobject *uobj;
  412. new_fd = get_unused_fd_flags(O_CLOEXEC);
  413. if (new_fd < 0)
  414. return ERR_PTR(new_fd);
  415. uobj = alloc_uobj(ufile, obj);
  416. if (IS_ERR(uobj)) {
  417. put_unused_fd(new_fd);
  418. return uobj;
  419. }
  420. uobj->id = new_fd;
  421. uobj->ufile = ufile;
  422. return uobj;
  423. }
  424. struct ib_uobject *rdma_alloc_begin_uobject(const struct uverbs_api_object *obj,
  425. struct ib_uverbs_file *ufile)
  426. {
  427. struct ib_uobject *ret;
  428. if (!obj)
  429. return ERR_PTR(-EINVAL);
  430. /*
  431. * The hw_destroy_rwsem is held across the entire object creation and
  432. * released during rdma_alloc_commit_uobject or
  433. * rdma_alloc_abort_uobject
  434. */
  435. if (!down_read_trylock(&ufile->hw_destroy_rwsem))
  436. return ERR_PTR(-EIO);
  437. ret = obj->type_class->alloc_begin(obj, ufile);
  438. if (IS_ERR(ret)) {
  439. up_read(&ufile->hw_destroy_rwsem);
  440. return ret;
  441. }
  442. return ret;
  443. }
  444. static void alloc_abort_idr_uobject(struct ib_uobject *uobj)
  445. {
  446. ib_rdmacg_uncharge(&uobj->cg_obj, uobj->context->device,
  447. RDMACG_RESOURCE_HCA_OBJECT);
  448. spin_lock(&uobj->ufile->idr_lock);
  449. idr_remove(&uobj->ufile->idr, uobj->id);
  450. spin_unlock(&uobj->ufile->idr_lock);
  451. }
  452. static int __must_check destroy_hw_idr_uobject(struct ib_uobject *uobj,
  453. enum rdma_remove_reason why)
  454. {
  455. const struct uverbs_obj_idr_type *idr_type =
  456. container_of(uobj->uapi_object->type_attrs,
  457. struct uverbs_obj_idr_type, type);
  458. int ret = idr_type->destroy_object(uobj, why);
  459. /*
  460. * We can only fail gracefully if the user requested to destroy the
  461. * object or when a retry may be called upon an error.
  462. * In the rest of the cases, just remove whatever you can.
  463. */
  464. if (ib_is_destroy_retryable(ret, why, uobj))
  465. return ret;
  466. if (why == RDMA_REMOVE_ABORT)
  467. return 0;
  468. ib_rdmacg_uncharge(&uobj->cg_obj, uobj->context->device,
  469. RDMACG_RESOURCE_HCA_OBJECT);
  470. return 0;
  471. }
  472. static void remove_handle_idr_uobject(struct ib_uobject *uobj)
  473. {
  474. spin_lock(&uobj->ufile->idr_lock);
  475. idr_remove(&uobj->ufile->idr, uobj->id);
  476. spin_unlock(&uobj->ufile->idr_lock);
  477. /* Matches the kref in alloc_commit_idr_uobject */
  478. uverbs_uobject_put(uobj);
  479. }
  480. static void alloc_abort_fd_uobject(struct ib_uobject *uobj)
  481. {
  482. put_unused_fd(uobj->id);
  483. }
  484. static int __must_check destroy_hw_fd_uobject(struct ib_uobject *uobj,
  485. enum rdma_remove_reason why)
  486. {
  487. const struct uverbs_obj_fd_type *fd_type = container_of(
  488. uobj->uapi_object->type_attrs, struct uverbs_obj_fd_type, type);
  489. int ret = fd_type->context_closed(uobj, why);
  490. if (ib_is_destroy_retryable(ret, why, uobj))
  491. return ret;
  492. return 0;
  493. }
  494. static void remove_handle_fd_uobject(struct ib_uobject *uobj)
  495. {
  496. }
  497. static int alloc_commit_idr_uobject(struct ib_uobject *uobj)
  498. {
  499. struct ib_uverbs_file *ufile = uobj->ufile;
  500. spin_lock(&ufile->idr_lock);
  501. /*
  502. * We already allocated this IDR with a NULL object, so
  503. * this shouldn't fail.
  504. *
  505. * NOTE: Once we set the IDR we loose ownership of our kref on uobj.
  506. * It will be put by remove_commit_idr_uobject()
  507. */
  508. WARN_ON(idr_replace(&ufile->idr, uobj, uobj->id));
  509. spin_unlock(&ufile->idr_lock);
  510. return 0;
  511. }
  512. static int alloc_commit_fd_uobject(struct ib_uobject *uobj)
  513. {
  514. const struct uverbs_obj_fd_type *fd_type = container_of(
  515. uobj->uapi_object->type_attrs, struct uverbs_obj_fd_type, type);
  516. int fd = uobj->id;
  517. struct file *filp;
  518. /*
  519. * The kref for uobj is moved into filp->private data and put in
  520. * uverbs_close_fd(). Once alloc_commit() succeeds uverbs_close_fd()
  521. * must be guaranteed to be called from the provided fops release
  522. * callback.
  523. */
  524. filp = anon_inode_getfile(fd_type->name,
  525. fd_type->fops,
  526. uobj,
  527. fd_type->flags);
  528. if (IS_ERR(filp))
  529. return PTR_ERR(filp);
  530. uobj->object = filp;
  531. /* Matching put will be done in uverbs_close_fd() */
  532. kref_get(&uobj->ufile->ref);
  533. /* This shouldn't be used anymore. Use the file object instead */
  534. uobj->id = 0;
  535. /*
  536. * NOTE: Once we install the file we loose ownership of our kref on
  537. * uobj. It will be put by uverbs_close_fd()
  538. */
  539. fd_install(fd, filp);
  540. return 0;
  541. }
  542. /*
  543. * In all cases rdma_alloc_commit_uobject() consumes the kref to uobj and the
  544. * caller can no longer assume uobj is valid. If this function fails it
  545. * destroys the uboject, including the attached HW object.
  546. */
  547. int __must_check rdma_alloc_commit_uobject(struct ib_uobject *uobj)
  548. {
  549. struct ib_uverbs_file *ufile = uobj->ufile;
  550. int ret;
  551. /* alloc_commit consumes the uobj kref */
  552. ret = uobj->uapi_object->type_class->alloc_commit(uobj);
  553. if (ret) {
  554. uverbs_destroy_uobject(uobj, RDMA_REMOVE_ABORT);
  555. up_read(&ufile->hw_destroy_rwsem);
  556. return ret;
  557. }
  558. /* kref is held so long as the uobj is on the uobj list. */
  559. uverbs_uobject_get(uobj);
  560. spin_lock_irq(&ufile->uobjects_lock);
  561. list_add(&uobj->list, &ufile->uobjects);
  562. spin_unlock_irq(&ufile->uobjects_lock);
  563. /* matches atomic_set(-1) in alloc_uobj */
  564. atomic_set(&uobj->usecnt, 0);
  565. /* Matches the down_read in rdma_alloc_begin_uobject */
  566. up_read(&ufile->hw_destroy_rwsem);
  567. return 0;
  568. }
  569. /*
  570. * This consumes the kref for uobj. It is up to the caller to unwind the HW
  571. * object and anything else connected to uobj before calling this.
  572. */
  573. void rdma_alloc_abort_uobject(struct ib_uobject *uobj)
  574. {
  575. struct ib_uverbs_file *ufile = uobj->ufile;
  576. uobj->object = NULL;
  577. uverbs_destroy_uobject(uobj, RDMA_REMOVE_ABORT);
  578. /* Matches the down_read in rdma_alloc_begin_uobject */
  579. up_read(&ufile->hw_destroy_rwsem);
  580. }
  581. static void lookup_put_idr_uobject(struct ib_uobject *uobj,
  582. enum rdma_lookup_mode mode)
  583. {
  584. }
  585. static void lookup_put_fd_uobject(struct ib_uobject *uobj,
  586. enum rdma_lookup_mode mode)
  587. {
  588. struct file *filp = uobj->object;
  589. WARN_ON(mode != UVERBS_LOOKUP_READ);
  590. /* This indirectly calls uverbs_close_fd and free the object */
  591. fput(filp);
  592. }
  593. void rdma_lookup_put_uobject(struct ib_uobject *uobj,
  594. enum rdma_lookup_mode mode)
  595. {
  596. assert_uverbs_usecnt(uobj, mode);
  597. /*
  598. * In order to unlock an object, either decrease its usecnt for
  599. * read access or zero it in case of exclusive access. See
  600. * uverbs_try_lock_object for locking schema information.
  601. */
  602. switch (mode) {
  603. case UVERBS_LOOKUP_READ:
  604. atomic_dec(&uobj->usecnt);
  605. break;
  606. case UVERBS_LOOKUP_WRITE:
  607. atomic_set(&uobj->usecnt, 0);
  608. break;
  609. case UVERBS_LOOKUP_DESTROY:
  610. break;
  611. }
  612. uobj->uapi_object->type_class->lookup_put(uobj, mode);
  613. /* Pairs with the kref obtained by type->lookup_get */
  614. uverbs_uobject_put(uobj);
  615. }
  616. void setup_ufile_idr_uobject(struct ib_uverbs_file *ufile)
  617. {
  618. spin_lock_init(&ufile->idr_lock);
  619. idr_init(&ufile->idr);
  620. }
  621. void release_ufile_idr_uobject(struct ib_uverbs_file *ufile)
  622. {
  623. struct ib_uobject *entry;
  624. int id;
  625. /*
  626. * At this point uverbs_cleanup_ufile() is guaranteed to have run, and
  627. * there are no HW objects left, however the IDR is still populated
  628. * with anything that has not been cleaned up by userspace. Since the
  629. * kref on ufile is 0, nothing is allowed to call lookup_get.
  630. *
  631. * This is an optimized equivalent to remove_handle_idr_uobject
  632. */
  633. idr_for_each_entry(&ufile->idr, entry, id) {
  634. WARN_ON(entry->object);
  635. uverbs_uobject_put(entry);
  636. }
  637. idr_destroy(&ufile->idr);
  638. }
  639. const struct uverbs_obj_type_class uverbs_idr_class = {
  640. .alloc_begin = alloc_begin_idr_uobject,
  641. .lookup_get = lookup_get_idr_uobject,
  642. .alloc_commit = alloc_commit_idr_uobject,
  643. .alloc_abort = alloc_abort_idr_uobject,
  644. .lookup_put = lookup_put_idr_uobject,
  645. .destroy_hw = destroy_hw_idr_uobject,
  646. .remove_handle = remove_handle_idr_uobject,
  647. /*
  648. * When we destroy an object, we first just lock it for WRITE and
  649. * actually DESTROY it in the finalize stage. So, the problematic
  650. * scenario is when we just started the finalize stage of the
  651. * destruction (nothing was executed yet). Now, the other thread
  652. * fetched the object for READ access, but it didn't lock it yet.
  653. * The DESTROY thread continues and starts destroying the object.
  654. * When the other thread continue - without the RCU, it would
  655. * access freed memory. However, the rcu_read_lock delays the free
  656. * until the rcu_read_lock of the READ operation quits. Since the
  657. * exclusive lock of the object is still taken by the DESTROY flow, the
  658. * READ operation will get -EBUSY and it'll just bail out.
  659. */
  660. .needs_kfree_rcu = true,
  661. };
  662. EXPORT_SYMBOL(uverbs_idr_class);
  663. void uverbs_close_fd(struct file *f)
  664. {
  665. struct ib_uobject *uobj = f->private_data;
  666. struct ib_uverbs_file *ufile = uobj->ufile;
  667. if (down_read_trylock(&ufile->hw_destroy_rwsem)) {
  668. /*
  669. * lookup_get_fd_uobject holds the kref on the struct file any
  670. * time a FD uobj is locked, which prevents this release
  671. * method from being invoked. Meaning we can always get the
  672. * write lock here, or we have a kernel bug.
  673. */
  674. WARN_ON(uverbs_try_lock_object(uobj, UVERBS_LOOKUP_WRITE));
  675. uverbs_destroy_uobject(uobj, RDMA_REMOVE_CLOSE);
  676. up_read(&ufile->hw_destroy_rwsem);
  677. }
  678. /* Matches the get in alloc_begin_fd_uobject */
  679. kref_put(&ufile->ref, ib_uverbs_release_file);
  680. /* Pairs with filp->private_data in alloc_begin_fd_uobject */
  681. uverbs_uobject_put(uobj);
  682. }
  683. static void ufile_disassociate_ucontext(struct ib_ucontext *ibcontext)
  684. {
  685. struct ib_device *ib_dev = ibcontext->device;
  686. struct task_struct *owning_process = NULL;
  687. struct mm_struct *owning_mm = NULL;
  688. owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID);
  689. if (!owning_process)
  690. return;
  691. owning_mm = get_task_mm(owning_process);
  692. if (!owning_mm) {
  693. pr_info("no mm, disassociate ucontext is pending task termination\n");
  694. while (1) {
  695. put_task_struct(owning_process);
  696. usleep_range(1000, 2000);
  697. owning_process = get_pid_task(ibcontext->tgid,
  698. PIDTYPE_PID);
  699. if (!owning_process ||
  700. owning_process->state == TASK_DEAD) {
  701. pr_info("disassociate ucontext done, task was terminated\n");
  702. /* in case task was dead need to release the
  703. * task struct.
  704. */
  705. if (owning_process)
  706. put_task_struct(owning_process);
  707. return;
  708. }
  709. }
  710. }
  711. down_write(&owning_mm->mmap_sem);
  712. ib_dev->disassociate_ucontext(ibcontext);
  713. up_write(&owning_mm->mmap_sem);
  714. mmput(owning_mm);
  715. put_task_struct(owning_process);
  716. }
  717. /*
  718. * Drop the ucontext off the ufile and completely disconnect it from the
  719. * ib_device
  720. */
  721. static void ufile_destroy_ucontext(struct ib_uverbs_file *ufile,
  722. enum rdma_remove_reason reason)
  723. {
  724. struct ib_ucontext *ucontext = ufile->ucontext;
  725. int ret;
  726. if (reason == RDMA_REMOVE_DRIVER_REMOVE)
  727. ufile_disassociate_ucontext(ucontext);
  728. put_pid(ucontext->tgid);
  729. ib_rdmacg_uncharge(&ucontext->cg_obj, ucontext->device,
  730. RDMACG_RESOURCE_HCA_HANDLE);
  731. /*
  732. * FIXME: Drivers are not permitted to fail dealloc_ucontext, remove
  733. * the error return.
  734. */
  735. ret = ucontext->device->dealloc_ucontext(ucontext);
  736. WARN_ON(ret);
  737. ufile->ucontext = NULL;
  738. }
  739. static int __uverbs_cleanup_ufile(struct ib_uverbs_file *ufile,
  740. enum rdma_remove_reason reason)
  741. {
  742. struct ib_uobject *obj, *next_obj;
  743. int ret = -EINVAL;
  744. /*
  745. * This shouldn't run while executing other commands on this
  746. * context. Thus, the only thing we should take care of is
  747. * releasing a FD while traversing this list. The FD could be
  748. * closed and released from the _release fop of this FD.
  749. * In order to mitigate this, we add a lock.
  750. * We take and release the lock per traversal in order to let
  751. * other threads (which might still use the FDs) chance to run.
  752. */
  753. list_for_each_entry_safe(obj, next_obj, &ufile->uobjects, list) {
  754. /*
  755. * if we hit this WARN_ON, that means we are
  756. * racing with a lookup_get.
  757. */
  758. WARN_ON(uverbs_try_lock_object(obj, UVERBS_LOOKUP_WRITE));
  759. if (!uverbs_destroy_uobject(obj, reason))
  760. ret = 0;
  761. else
  762. atomic_set(&obj->usecnt, 0);
  763. }
  764. return ret;
  765. }
  766. /*
  767. * Destroy the uncontext and every uobject associated with it. If called with
  768. * reason != RDMA_REMOVE_CLOSE this will not return until the destruction has
  769. * been completed and ufile->ucontext is NULL.
  770. *
  771. * This is internally locked and can be called in parallel from multiple
  772. * contexts.
  773. */
  774. void uverbs_destroy_ufile_hw(struct ib_uverbs_file *ufile,
  775. enum rdma_remove_reason reason)
  776. {
  777. if (reason == RDMA_REMOVE_CLOSE) {
  778. /*
  779. * During destruction we might trigger something that
  780. * synchronously calls release on any file descriptor. For
  781. * this reason all paths that come from file_operations
  782. * release must use try_lock. They can progress knowing that
  783. * there is an ongoing uverbs_destroy_ufile_hw that will clean
  784. * up the driver resources.
  785. */
  786. if (!mutex_trylock(&ufile->ucontext_lock))
  787. return;
  788. } else {
  789. mutex_lock(&ufile->ucontext_lock);
  790. }
  791. down_write(&ufile->hw_destroy_rwsem);
  792. /*
  793. * If a ucontext was never created then we can't have any uobjects to
  794. * cleanup, nothing to do.
  795. */
  796. if (!ufile->ucontext)
  797. goto done;
  798. ufile->ucontext->closing = true;
  799. ufile->ucontext->cleanup_retryable = true;
  800. while (!list_empty(&ufile->uobjects))
  801. if (__uverbs_cleanup_ufile(ufile, reason)) {
  802. /*
  803. * No entry was cleaned-up successfully during this
  804. * iteration
  805. */
  806. break;
  807. }
  808. ufile->ucontext->cleanup_retryable = false;
  809. if (!list_empty(&ufile->uobjects))
  810. __uverbs_cleanup_ufile(ufile, reason);
  811. ufile_destroy_ucontext(ufile, reason);
  812. done:
  813. up_write(&ufile->hw_destroy_rwsem);
  814. mutex_unlock(&ufile->ucontext_lock);
  815. }
  816. const struct uverbs_obj_type_class uverbs_fd_class = {
  817. .alloc_begin = alloc_begin_fd_uobject,
  818. .lookup_get = lookup_get_fd_uobject,
  819. .alloc_commit = alloc_commit_fd_uobject,
  820. .alloc_abort = alloc_abort_fd_uobject,
  821. .lookup_put = lookup_put_fd_uobject,
  822. .destroy_hw = destroy_hw_fd_uobject,
  823. .remove_handle = remove_handle_fd_uobject,
  824. .needs_kfree_rcu = false,
  825. };
  826. EXPORT_SYMBOL(uverbs_fd_class);
  827. struct ib_uobject *
  828. uverbs_get_uobject_from_file(u16 object_id,
  829. struct ib_uverbs_file *ufile,
  830. enum uverbs_obj_access access, s64 id)
  831. {
  832. const struct uverbs_api_object *obj =
  833. uapi_get_object(ufile->device->uapi, object_id);
  834. switch (access) {
  835. case UVERBS_ACCESS_READ:
  836. return rdma_lookup_get_uobject(obj, ufile, id,
  837. UVERBS_LOOKUP_READ);
  838. case UVERBS_ACCESS_DESTROY:
  839. /* Actual destruction is done inside uverbs_handle_method */
  840. return rdma_lookup_get_uobject(obj, ufile, id,
  841. UVERBS_LOOKUP_DESTROY);
  842. case UVERBS_ACCESS_WRITE:
  843. return rdma_lookup_get_uobject(obj, ufile, id,
  844. UVERBS_LOOKUP_WRITE);
  845. case UVERBS_ACCESS_NEW:
  846. return rdma_alloc_begin_uobject(obj, ufile);
  847. default:
  848. WARN_ON(true);
  849. return ERR_PTR(-EOPNOTSUPP);
  850. }
  851. }
  852. int uverbs_finalize_object(struct ib_uobject *uobj,
  853. enum uverbs_obj_access access,
  854. bool commit)
  855. {
  856. int ret = 0;
  857. /*
  858. * refcounts should be handled at the object level and not at the
  859. * uobject level. Refcounts of the objects themselves are done in
  860. * handlers.
  861. */
  862. switch (access) {
  863. case UVERBS_ACCESS_READ:
  864. rdma_lookup_put_uobject(uobj, UVERBS_LOOKUP_READ);
  865. break;
  866. case UVERBS_ACCESS_WRITE:
  867. rdma_lookup_put_uobject(uobj, UVERBS_LOOKUP_WRITE);
  868. break;
  869. case UVERBS_ACCESS_DESTROY:
  870. if (uobj)
  871. rdma_lookup_put_uobject(uobj, UVERBS_LOOKUP_DESTROY);
  872. break;
  873. case UVERBS_ACCESS_NEW:
  874. if (commit)
  875. ret = rdma_alloc_commit_uobject(uobj);
  876. else
  877. rdma_alloc_abort_uobject(uobj);
  878. break;
  879. default:
  880. WARN_ON(true);
  881. ret = -EOPNOTSUPP;
  882. }
  883. return ret;
  884. }