uverbs_main.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  1. /*
  2. * Copyright (c) 2005 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005, 2006 Cisco Systems. All rights reserved.
  4. * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
  5. * Copyright (c) 2005 Voltaire, Inc. All rights reserved.
  6. * Copyright (c) 2005 PathScale, Inc. All rights reserved.
  7. *
  8. * This software is available to you under a choice of one of two
  9. * licenses. You may choose to be licensed under the terms of the GNU
  10. * General Public License (GPL) Version 2, available from the file
  11. * COPYING in the main directory of this source tree, or the
  12. * OpenIB.org BSD license below:
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer.
  21. *
  22. * - Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials
  25. * provided with the distribution.
  26. *
  27. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  28. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  29. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  30. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  31. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  32. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  33. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  34. * SOFTWARE.
  35. */
  36. #include <linux/module.h>
  37. #include <linux/init.h>
  38. #include <linux/device.h>
  39. #include <linux/err.h>
  40. #include <linux/fs.h>
  41. #include <linux/poll.h>
  42. #include <linux/sched.h>
  43. #include <linux/file.h>
  44. #include <linux/cdev.h>
  45. #include <linux/anon_inodes.h>
  46. #include <linux/slab.h>
  47. #include <asm/uaccess.h>
  48. #include <rdma/ib.h>
  49. #include "uverbs.h"
  50. MODULE_AUTHOR("Roland Dreier");
  51. MODULE_DESCRIPTION("InfiniBand userspace verbs access");
  52. MODULE_LICENSE("Dual BSD/GPL");
  53. enum {
  54. IB_UVERBS_MAJOR = 231,
  55. IB_UVERBS_BASE_MINOR = 192,
  56. IB_UVERBS_MAX_DEVICES = 32
  57. };
  58. #define IB_UVERBS_BASE_DEV MKDEV(IB_UVERBS_MAJOR, IB_UVERBS_BASE_MINOR)
  59. static struct class *uverbs_class;
  60. DEFINE_SPINLOCK(ib_uverbs_idr_lock);
  61. DEFINE_IDR(ib_uverbs_pd_idr);
  62. DEFINE_IDR(ib_uverbs_mr_idr);
  63. DEFINE_IDR(ib_uverbs_mw_idr);
  64. DEFINE_IDR(ib_uverbs_ah_idr);
  65. DEFINE_IDR(ib_uverbs_cq_idr);
  66. DEFINE_IDR(ib_uverbs_qp_idr);
  67. DEFINE_IDR(ib_uverbs_srq_idr);
  68. DEFINE_IDR(ib_uverbs_xrcd_idr);
  69. DEFINE_IDR(ib_uverbs_rule_idr);
  70. static DEFINE_SPINLOCK(map_lock);
  71. static DECLARE_BITMAP(dev_map, IB_UVERBS_MAX_DEVICES);
  72. static ssize_t (*uverbs_cmd_table[])(struct ib_uverbs_file *file,
  73. struct ib_device *ib_dev,
  74. const char __user *buf, int in_len,
  75. int out_len) = {
  76. [IB_USER_VERBS_CMD_GET_CONTEXT] = ib_uverbs_get_context,
  77. [IB_USER_VERBS_CMD_QUERY_DEVICE] = ib_uverbs_query_device,
  78. [IB_USER_VERBS_CMD_QUERY_PORT] = ib_uverbs_query_port,
  79. [IB_USER_VERBS_CMD_ALLOC_PD] = ib_uverbs_alloc_pd,
  80. [IB_USER_VERBS_CMD_DEALLOC_PD] = ib_uverbs_dealloc_pd,
  81. [IB_USER_VERBS_CMD_REG_MR] = ib_uverbs_reg_mr,
  82. [IB_USER_VERBS_CMD_REREG_MR] = ib_uverbs_rereg_mr,
  83. [IB_USER_VERBS_CMD_DEREG_MR] = ib_uverbs_dereg_mr,
  84. [IB_USER_VERBS_CMD_ALLOC_MW] = ib_uverbs_alloc_mw,
  85. [IB_USER_VERBS_CMD_DEALLOC_MW] = ib_uverbs_dealloc_mw,
  86. [IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL] = ib_uverbs_create_comp_channel,
  87. [IB_USER_VERBS_CMD_CREATE_CQ] = ib_uverbs_create_cq,
  88. [IB_USER_VERBS_CMD_RESIZE_CQ] = ib_uverbs_resize_cq,
  89. [IB_USER_VERBS_CMD_POLL_CQ] = ib_uverbs_poll_cq,
  90. [IB_USER_VERBS_CMD_REQ_NOTIFY_CQ] = ib_uverbs_req_notify_cq,
  91. [IB_USER_VERBS_CMD_DESTROY_CQ] = ib_uverbs_destroy_cq,
  92. [IB_USER_VERBS_CMD_CREATE_QP] = ib_uverbs_create_qp,
  93. [IB_USER_VERBS_CMD_QUERY_QP] = ib_uverbs_query_qp,
  94. [IB_USER_VERBS_CMD_MODIFY_QP] = ib_uverbs_modify_qp,
  95. [IB_USER_VERBS_CMD_DESTROY_QP] = ib_uverbs_destroy_qp,
  96. [IB_USER_VERBS_CMD_POST_SEND] = ib_uverbs_post_send,
  97. [IB_USER_VERBS_CMD_POST_RECV] = ib_uverbs_post_recv,
  98. [IB_USER_VERBS_CMD_POST_SRQ_RECV] = ib_uverbs_post_srq_recv,
  99. [IB_USER_VERBS_CMD_CREATE_AH] = ib_uverbs_create_ah,
  100. [IB_USER_VERBS_CMD_DESTROY_AH] = ib_uverbs_destroy_ah,
  101. [IB_USER_VERBS_CMD_ATTACH_MCAST] = ib_uverbs_attach_mcast,
  102. [IB_USER_VERBS_CMD_DETACH_MCAST] = ib_uverbs_detach_mcast,
  103. [IB_USER_VERBS_CMD_CREATE_SRQ] = ib_uverbs_create_srq,
  104. [IB_USER_VERBS_CMD_MODIFY_SRQ] = ib_uverbs_modify_srq,
  105. [IB_USER_VERBS_CMD_QUERY_SRQ] = ib_uverbs_query_srq,
  106. [IB_USER_VERBS_CMD_DESTROY_SRQ] = ib_uverbs_destroy_srq,
  107. [IB_USER_VERBS_CMD_OPEN_XRCD] = ib_uverbs_open_xrcd,
  108. [IB_USER_VERBS_CMD_CLOSE_XRCD] = ib_uverbs_close_xrcd,
  109. [IB_USER_VERBS_CMD_CREATE_XSRQ] = ib_uverbs_create_xsrq,
  110. [IB_USER_VERBS_CMD_OPEN_QP] = ib_uverbs_open_qp,
  111. };
  112. static int (*uverbs_ex_cmd_table[])(struct ib_uverbs_file *file,
  113. struct ib_device *ib_dev,
  114. struct ib_udata *ucore,
  115. struct ib_udata *uhw) = {
  116. [IB_USER_VERBS_EX_CMD_CREATE_FLOW] = ib_uverbs_ex_create_flow,
  117. [IB_USER_VERBS_EX_CMD_DESTROY_FLOW] = ib_uverbs_ex_destroy_flow,
  118. [IB_USER_VERBS_EX_CMD_QUERY_DEVICE] = ib_uverbs_ex_query_device,
  119. [IB_USER_VERBS_EX_CMD_CREATE_CQ] = ib_uverbs_ex_create_cq,
  120. [IB_USER_VERBS_EX_CMD_CREATE_QP] = ib_uverbs_ex_create_qp,
  121. };
  122. static void ib_uverbs_add_one(struct ib_device *device);
  123. static void ib_uverbs_remove_one(struct ib_device *device, void *client_data);
  124. int uverbs_dealloc_mw(struct ib_mw *mw)
  125. {
  126. struct ib_pd *pd = mw->pd;
  127. int ret;
  128. ret = mw->device->dealloc_mw(mw);
  129. if (!ret)
  130. atomic_dec(&pd->usecnt);
  131. return ret;
  132. }
  133. static void ib_uverbs_release_dev(struct kobject *kobj)
  134. {
  135. struct ib_uverbs_device *dev =
  136. container_of(kobj, struct ib_uverbs_device, kobj);
  137. cleanup_srcu_struct(&dev->disassociate_srcu);
  138. kfree(dev);
  139. }
  140. static struct kobj_type ib_uverbs_dev_ktype = {
  141. .release = ib_uverbs_release_dev,
  142. };
  143. static void ib_uverbs_release_event_file(struct kref *ref)
  144. {
  145. struct ib_uverbs_event_file *file =
  146. container_of(ref, struct ib_uverbs_event_file, ref);
  147. kfree(file);
  148. }
  149. void ib_uverbs_release_ucq(struct ib_uverbs_file *file,
  150. struct ib_uverbs_event_file *ev_file,
  151. struct ib_ucq_object *uobj)
  152. {
  153. struct ib_uverbs_event *evt, *tmp;
  154. if (ev_file) {
  155. spin_lock_irq(&ev_file->lock);
  156. list_for_each_entry_safe(evt, tmp, &uobj->comp_list, obj_list) {
  157. list_del(&evt->list);
  158. kfree(evt);
  159. }
  160. spin_unlock_irq(&ev_file->lock);
  161. kref_put(&ev_file->ref, ib_uverbs_release_event_file);
  162. }
  163. spin_lock_irq(&file->async_file->lock);
  164. list_for_each_entry_safe(evt, tmp, &uobj->async_list, obj_list) {
  165. list_del(&evt->list);
  166. kfree(evt);
  167. }
  168. spin_unlock_irq(&file->async_file->lock);
  169. }
  170. void ib_uverbs_release_uevent(struct ib_uverbs_file *file,
  171. struct ib_uevent_object *uobj)
  172. {
  173. struct ib_uverbs_event *evt, *tmp;
  174. spin_lock_irq(&file->async_file->lock);
  175. list_for_each_entry_safe(evt, tmp, &uobj->event_list, obj_list) {
  176. list_del(&evt->list);
  177. kfree(evt);
  178. }
  179. spin_unlock_irq(&file->async_file->lock);
  180. }
  181. static void ib_uverbs_detach_umcast(struct ib_qp *qp,
  182. struct ib_uqp_object *uobj)
  183. {
  184. struct ib_uverbs_mcast_entry *mcast, *tmp;
  185. list_for_each_entry_safe(mcast, tmp, &uobj->mcast_list, list) {
  186. ib_detach_mcast(qp, &mcast->gid, mcast->lid);
  187. list_del(&mcast->list);
  188. kfree(mcast);
  189. }
  190. }
  191. static int ib_uverbs_cleanup_ucontext(struct ib_uverbs_file *file,
  192. struct ib_ucontext *context)
  193. {
  194. struct ib_uobject *uobj, *tmp;
  195. context->closing = 1;
  196. list_for_each_entry_safe(uobj, tmp, &context->ah_list, list) {
  197. struct ib_ah *ah = uobj->object;
  198. idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
  199. ib_destroy_ah(ah);
  200. kfree(uobj);
  201. }
  202. /* Remove MWs before QPs, in order to support type 2A MWs. */
  203. list_for_each_entry_safe(uobj, tmp, &context->mw_list, list) {
  204. struct ib_mw *mw = uobj->object;
  205. idr_remove_uobj(&ib_uverbs_mw_idr, uobj);
  206. uverbs_dealloc_mw(mw);
  207. kfree(uobj);
  208. }
  209. list_for_each_entry_safe(uobj, tmp, &context->rule_list, list) {
  210. struct ib_flow *flow_id = uobj->object;
  211. idr_remove_uobj(&ib_uverbs_rule_idr, uobj);
  212. ib_destroy_flow(flow_id);
  213. kfree(uobj);
  214. }
  215. list_for_each_entry_safe(uobj, tmp, &context->qp_list, list) {
  216. struct ib_qp *qp = uobj->object;
  217. struct ib_uqp_object *uqp =
  218. container_of(uobj, struct ib_uqp_object, uevent.uobject);
  219. idr_remove_uobj(&ib_uverbs_qp_idr, uobj);
  220. if (qp != qp->real_qp) {
  221. ib_close_qp(qp);
  222. } else {
  223. ib_uverbs_detach_umcast(qp, uqp);
  224. ib_destroy_qp(qp);
  225. }
  226. ib_uverbs_release_uevent(file, &uqp->uevent);
  227. kfree(uqp);
  228. }
  229. list_for_each_entry_safe(uobj, tmp, &context->srq_list, list) {
  230. struct ib_srq *srq = uobj->object;
  231. struct ib_uevent_object *uevent =
  232. container_of(uobj, struct ib_uevent_object, uobject);
  233. idr_remove_uobj(&ib_uverbs_srq_idr, uobj);
  234. ib_destroy_srq(srq);
  235. ib_uverbs_release_uevent(file, uevent);
  236. kfree(uevent);
  237. }
  238. list_for_each_entry_safe(uobj, tmp, &context->cq_list, list) {
  239. struct ib_cq *cq = uobj->object;
  240. struct ib_uverbs_event_file *ev_file = cq->cq_context;
  241. struct ib_ucq_object *ucq =
  242. container_of(uobj, struct ib_ucq_object, uobject);
  243. idr_remove_uobj(&ib_uverbs_cq_idr, uobj);
  244. ib_destroy_cq(cq);
  245. ib_uverbs_release_ucq(file, ev_file, ucq);
  246. kfree(ucq);
  247. }
  248. list_for_each_entry_safe(uobj, tmp, &context->mr_list, list) {
  249. struct ib_mr *mr = uobj->object;
  250. idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
  251. ib_dereg_mr(mr);
  252. kfree(uobj);
  253. }
  254. mutex_lock(&file->device->xrcd_tree_mutex);
  255. list_for_each_entry_safe(uobj, tmp, &context->xrcd_list, list) {
  256. struct ib_xrcd *xrcd = uobj->object;
  257. struct ib_uxrcd_object *uxrcd =
  258. container_of(uobj, struct ib_uxrcd_object, uobject);
  259. idr_remove_uobj(&ib_uverbs_xrcd_idr, uobj);
  260. ib_uverbs_dealloc_xrcd(file->device, xrcd);
  261. kfree(uxrcd);
  262. }
  263. mutex_unlock(&file->device->xrcd_tree_mutex);
  264. list_for_each_entry_safe(uobj, tmp, &context->pd_list, list) {
  265. struct ib_pd *pd = uobj->object;
  266. idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
  267. ib_dealloc_pd(pd);
  268. kfree(uobj);
  269. }
  270. put_pid(context->tgid);
  271. return context->device->dealloc_ucontext(context);
  272. }
  273. static void ib_uverbs_comp_dev(struct ib_uverbs_device *dev)
  274. {
  275. complete(&dev->comp);
  276. }
  277. static void ib_uverbs_release_file(struct kref *ref)
  278. {
  279. struct ib_uverbs_file *file =
  280. container_of(ref, struct ib_uverbs_file, ref);
  281. struct ib_device *ib_dev;
  282. int srcu_key;
  283. srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
  284. ib_dev = srcu_dereference(file->device->ib_dev,
  285. &file->device->disassociate_srcu);
  286. if (ib_dev && !ib_dev->disassociate_ucontext)
  287. module_put(ib_dev->owner);
  288. srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
  289. if (atomic_dec_and_test(&file->device->refcount))
  290. ib_uverbs_comp_dev(file->device);
  291. kfree(file);
  292. }
  293. static ssize_t ib_uverbs_event_read(struct file *filp, char __user *buf,
  294. size_t count, loff_t *pos)
  295. {
  296. struct ib_uverbs_event_file *file = filp->private_data;
  297. struct ib_uverbs_event *event;
  298. int eventsz;
  299. int ret = 0;
  300. spin_lock_irq(&file->lock);
  301. while (list_empty(&file->event_list)) {
  302. spin_unlock_irq(&file->lock);
  303. if (filp->f_flags & O_NONBLOCK)
  304. return -EAGAIN;
  305. if (wait_event_interruptible(file->poll_wait,
  306. (!list_empty(&file->event_list) ||
  307. /* The barriers built into wait_event_interruptible()
  308. * and wake_up() guarentee this will see the null set
  309. * without using RCU
  310. */
  311. !file->uverbs_file->device->ib_dev)))
  312. return -ERESTARTSYS;
  313. /* If device was disassociated and no event exists set an error */
  314. if (list_empty(&file->event_list) &&
  315. !file->uverbs_file->device->ib_dev)
  316. return -EIO;
  317. spin_lock_irq(&file->lock);
  318. }
  319. event = list_entry(file->event_list.next, struct ib_uverbs_event, list);
  320. if (file->is_async)
  321. eventsz = sizeof (struct ib_uverbs_async_event_desc);
  322. else
  323. eventsz = sizeof (struct ib_uverbs_comp_event_desc);
  324. if (eventsz > count) {
  325. ret = -EINVAL;
  326. event = NULL;
  327. } else {
  328. list_del(file->event_list.next);
  329. if (event->counter) {
  330. ++(*event->counter);
  331. list_del(&event->obj_list);
  332. }
  333. }
  334. spin_unlock_irq(&file->lock);
  335. if (event) {
  336. if (copy_to_user(buf, event, eventsz))
  337. ret = -EFAULT;
  338. else
  339. ret = eventsz;
  340. }
  341. kfree(event);
  342. return ret;
  343. }
  344. static unsigned int ib_uverbs_event_poll(struct file *filp,
  345. struct poll_table_struct *wait)
  346. {
  347. unsigned int pollflags = 0;
  348. struct ib_uverbs_event_file *file = filp->private_data;
  349. poll_wait(filp, &file->poll_wait, wait);
  350. spin_lock_irq(&file->lock);
  351. if (!list_empty(&file->event_list))
  352. pollflags = POLLIN | POLLRDNORM;
  353. spin_unlock_irq(&file->lock);
  354. return pollflags;
  355. }
  356. static int ib_uverbs_event_fasync(int fd, struct file *filp, int on)
  357. {
  358. struct ib_uverbs_event_file *file = filp->private_data;
  359. return fasync_helper(fd, filp, on, &file->async_queue);
  360. }
  361. static int ib_uverbs_event_close(struct inode *inode, struct file *filp)
  362. {
  363. struct ib_uverbs_event_file *file = filp->private_data;
  364. struct ib_uverbs_event *entry, *tmp;
  365. int closed_already = 0;
  366. mutex_lock(&file->uverbs_file->device->lists_mutex);
  367. spin_lock_irq(&file->lock);
  368. closed_already = file->is_closed;
  369. file->is_closed = 1;
  370. list_for_each_entry_safe(entry, tmp, &file->event_list, list) {
  371. if (entry->counter)
  372. list_del(&entry->obj_list);
  373. kfree(entry);
  374. }
  375. spin_unlock_irq(&file->lock);
  376. if (!closed_already) {
  377. list_del(&file->list);
  378. if (file->is_async)
  379. ib_unregister_event_handler(&file->uverbs_file->
  380. event_handler);
  381. }
  382. mutex_unlock(&file->uverbs_file->device->lists_mutex);
  383. kref_put(&file->uverbs_file->ref, ib_uverbs_release_file);
  384. kref_put(&file->ref, ib_uverbs_release_event_file);
  385. return 0;
  386. }
  387. static const struct file_operations uverbs_event_fops = {
  388. .owner = THIS_MODULE,
  389. .read = ib_uverbs_event_read,
  390. .poll = ib_uverbs_event_poll,
  391. .release = ib_uverbs_event_close,
  392. .fasync = ib_uverbs_event_fasync,
  393. .llseek = no_llseek,
  394. };
  395. void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context)
  396. {
  397. struct ib_uverbs_event_file *file = cq_context;
  398. struct ib_ucq_object *uobj;
  399. struct ib_uverbs_event *entry;
  400. unsigned long flags;
  401. if (!file)
  402. return;
  403. spin_lock_irqsave(&file->lock, flags);
  404. if (file->is_closed) {
  405. spin_unlock_irqrestore(&file->lock, flags);
  406. return;
  407. }
  408. entry = kmalloc(sizeof *entry, GFP_ATOMIC);
  409. if (!entry) {
  410. spin_unlock_irqrestore(&file->lock, flags);
  411. return;
  412. }
  413. uobj = container_of(cq->uobject, struct ib_ucq_object, uobject);
  414. entry->desc.comp.cq_handle = cq->uobject->user_handle;
  415. entry->counter = &uobj->comp_events_reported;
  416. list_add_tail(&entry->list, &file->event_list);
  417. list_add_tail(&entry->obj_list, &uobj->comp_list);
  418. spin_unlock_irqrestore(&file->lock, flags);
  419. wake_up_interruptible(&file->poll_wait);
  420. kill_fasync(&file->async_queue, SIGIO, POLL_IN);
  421. }
  422. static void ib_uverbs_async_handler(struct ib_uverbs_file *file,
  423. __u64 element, __u64 event,
  424. struct list_head *obj_list,
  425. u32 *counter)
  426. {
  427. struct ib_uverbs_event *entry;
  428. unsigned long flags;
  429. spin_lock_irqsave(&file->async_file->lock, flags);
  430. if (file->async_file->is_closed) {
  431. spin_unlock_irqrestore(&file->async_file->lock, flags);
  432. return;
  433. }
  434. entry = kmalloc(sizeof *entry, GFP_ATOMIC);
  435. if (!entry) {
  436. spin_unlock_irqrestore(&file->async_file->lock, flags);
  437. return;
  438. }
  439. entry->desc.async.element = element;
  440. entry->desc.async.event_type = event;
  441. entry->desc.async.reserved = 0;
  442. entry->counter = counter;
  443. list_add_tail(&entry->list, &file->async_file->event_list);
  444. if (obj_list)
  445. list_add_tail(&entry->obj_list, obj_list);
  446. spin_unlock_irqrestore(&file->async_file->lock, flags);
  447. wake_up_interruptible(&file->async_file->poll_wait);
  448. kill_fasync(&file->async_file->async_queue, SIGIO, POLL_IN);
  449. }
  450. void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr)
  451. {
  452. struct ib_ucq_object *uobj = container_of(event->element.cq->uobject,
  453. struct ib_ucq_object, uobject);
  454. ib_uverbs_async_handler(uobj->uverbs_file, uobj->uobject.user_handle,
  455. event->event, &uobj->async_list,
  456. &uobj->async_events_reported);
  457. }
  458. void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr)
  459. {
  460. struct ib_uevent_object *uobj;
  461. /* for XRC target qp's, check that qp is live */
  462. if (!event->element.qp->uobject || !event->element.qp->uobject->live)
  463. return;
  464. uobj = container_of(event->element.qp->uobject,
  465. struct ib_uevent_object, uobject);
  466. ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
  467. event->event, &uobj->event_list,
  468. &uobj->events_reported);
  469. }
  470. void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr)
  471. {
  472. struct ib_uevent_object *uobj;
  473. uobj = container_of(event->element.srq->uobject,
  474. struct ib_uevent_object, uobject);
  475. ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
  476. event->event, &uobj->event_list,
  477. &uobj->events_reported);
  478. }
  479. void ib_uverbs_event_handler(struct ib_event_handler *handler,
  480. struct ib_event *event)
  481. {
  482. struct ib_uverbs_file *file =
  483. container_of(handler, struct ib_uverbs_file, event_handler);
  484. ib_uverbs_async_handler(file, event->element.port_num, event->event,
  485. NULL, NULL);
  486. }
  487. void ib_uverbs_free_async_event_file(struct ib_uverbs_file *file)
  488. {
  489. kref_put(&file->async_file->ref, ib_uverbs_release_event_file);
  490. file->async_file = NULL;
  491. }
  492. struct file *ib_uverbs_alloc_event_file(struct ib_uverbs_file *uverbs_file,
  493. struct ib_device *ib_dev,
  494. int is_async)
  495. {
  496. struct ib_uverbs_event_file *ev_file;
  497. struct file *filp;
  498. int ret;
  499. ev_file = kzalloc(sizeof(*ev_file), GFP_KERNEL);
  500. if (!ev_file)
  501. return ERR_PTR(-ENOMEM);
  502. kref_init(&ev_file->ref);
  503. spin_lock_init(&ev_file->lock);
  504. INIT_LIST_HEAD(&ev_file->event_list);
  505. init_waitqueue_head(&ev_file->poll_wait);
  506. ev_file->uverbs_file = uverbs_file;
  507. kref_get(&ev_file->uverbs_file->ref);
  508. ev_file->async_queue = NULL;
  509. ev_file->is_closed = 0;
  510. filp = anon_inode_getfile("[infinibandevent]", &uverbs_event_fops,
  511. ev_file, O_RDONLY);
  512. if (IS_ERR(filp))
  513. goto err_put_refs;
  514. mutex_lock(&uverbs_file->device->lists_mutex);
  515. list_add_tail(&ev_file->list,
  516. &uverbs_file->device->uverbs_events_file_list);
  517. mutex_unlock(&uverbs_file->device->lists_mutex);
  518. if (is_async) {
  519. WARN_ON(uverbs_file->async_file);
  520. uverbs_file->async_file = ev_file;
  521. kref_get(&uverbs_file->async_file->ref);
  522. INIT_IB_EVENT_HANDLER(&uverbs_file->event_handler,
  523. ib_dev,
  524. ib_uverbs_event_handler);
  525. ret = ib_register_event_handler(&uverbs_file->event_handler);
  526. if (ret)
  527. goto err_put_file;
  528. /* At that point async file stuff was fully set */
  529. ev_file->is_async = 1;
  530. }
  531. return filp;
  532. err_put_file:
  533. fput(filp);
  534. kref_put(&uverbs_file->async_file->ref, ib_uverbs_release_event_file);
  535. uverbs_file->async_file = NULL;
  536. return ERR_PTR(ret);
  537. err_put_refs:
  538. kref_put(&ev_file->uverbs_file->ref, ib_uverbs_release_file);
  539. kref_put(&ev_file->ref, ib_uverbs_release_event_file);
  540. return filp;
  541. }
  542. /*
  543. * Look up a completion event file by FD. If lookup is successful,
  544. * takes a ref to the event file struct that it returns; if
  545. * unsuccessful, returns NULL.
  546. */
  547. struct ib_uverbs_event_file *ib_uverbs_lookup_comp_file(int fd)
  548. {
  549. struct ib_uverbs_event_file *ev_file = NULL;
  550. struct fd f = fdget(fd);
  551. if (!f.file)
  552. return NULL;
  553. if (f.file->f_op != &uverbs_event_fops)
  554. goto out;
  555. ev_file = f.file->private_data;
  556. if (ev_file->is_async) {
  557. ev_file = NULL;
  558. goto out;
  559. }
  560. kref_get(&ev_file->ref);
  561. out:
  562. fdput(f);
  563. return ev_file;
  564. }
  565. static int verify_command_mask(struct ib_device *ib_dev, __u32 command)
  566. {
  567. u64 mask;
  568. if (command <= IB_USER_VERBS_CMD_OPEN_QP)
  569. mask = ib_dev->uverbs_cmd_mask;
  570. else
  571. mask = ib_dev->uverbs_ex_cmd_mask;
  572. if (mask & ((u64)1 << command))
  573. return 0;
  574. return -1;
  575. }
  576. static ssize_t ib_uverbs_write(struct file *filp, const char __user *buf,
  577. size_t count, loff_t *pos)
  578. {
  579. struct ib_uverbs_file *file = filp->private_data;
  580. struct ib_device *ib_dev;
  581. struct ib_uverbs_cmd_hdr hdr;
  582. __u32 command;
  583. __u32 flags;
  584. int srcu_key;
  585. ssize_t ret;
  586. if (WARN_ON_ONCE(!ib_safe_file_access(filp)))
  587. return -EACCES;
  588. if (count < sizeof hdr)
  589. return -EINVAL;
  590. if (copy_from_user(&hdr, buf, sizeof hdr))
  591. return -EFAULT;
  592. srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
  593. ib_dev = srcu_dereference(file->device->ib_dev,
  594. &file->device->disassociate_srcu);
  595. if (!ib_dev) {
  596. ret = -EIO;
  597. goto out;
  598. }
  599. if (hdr.command & ~(__u32)(IB_USER_VERBS_CMD_FLAGS_MASK |
  600. IB_USER_VERBS_CMD_COMMAND_MASK)) {
  601. ret = -EINVAL;
  602. goto out;
  603. }
  604. command = hdr.command & IB_USER_VERBS_CMD_COMMAND_MASK;
  605. if (verify_command_mask(ib_dev, command)) {
  606. ret = -EOPNOTSUPP;
  607. goto out;
  608. }
  609. if (!file->ucontext &&
  610. command != IB_USER_VERBS_CMD_GET_CONTEXT) {
  611. ret = -EINVAL;
  612. goto out;
  613. }
  614. flags = (hdr.command &
  615. IB_USER_VERBS_CMD_FLAGS_MASK) >> IB_USER_VERBS_CMD_FLAGS_SHIFT;
  616. if (!flags) {
  617. if (command >= ARRAY_SIZE(uverbs_cmd_table) ||
  618. !uverbs_cmd_table[command]) {
  619. ret = -EINVAL;
  620. goto out;
  621. }
  622. if (hdr.in_words * 4 != count) {
  623. ret = -EINVAL;
  624. goto out;
  625. }
  626. ret = uverbs_cmd_table[command](file, ib_dev,
  627. buf + sizeof(hdr),
  628. hdr.in_words * 4,
  629. hdr.out_words * 4);
  630. } else if (flags == IB_USER_VERBS_CMD_FLAG_EXTENDED) {
  631. struct ib_uverbs_ex_cmd_hdr ex_hdr;
  632. struct ib_udata ucore;
  633. struct ib_udata uhw;
  634. size_t written_count = count;
  635. if (command >= ARRAY_SIZE(uverbs_ex_cmd_table) ||
  636. !uverbs_ex_cmd_table[command]) {
  637. ret = -ENOSYS;
  638. goto out;
  639. }
  640. if (!file->ucontext) {
  641. ret = -EINVAL;
  642. goto out;
  643. }
  644. if (count < (sizeof(hdr) + sizeof(ex_hdr))) {
  645. ret = -EINVAL;
  646. goto out;
  647. }
  648. if (copy_from_user(&ex_hdr, buf + sizeof(hdr), sizeof(ex_hdr))) {
  649. ret = -EFAULT;
  650. goto out;
  651. }
  652. count -= sizeof(hdr) + sizeof(ex_hdr);
  653. buf += sizeof(hdr) + sizeof(ex_hdr);
  654. if ((hdr.in_words + ex_hdr.provider_in_words) * 8 != count) {
  655. ret = -EINVAL;
  656. goto out;
  657. }
  658. if (ex_hdr.cmd_hdr_reserved) {
  659. ret = -EINVAL;
  660. goto out;
  661. }
  662. if (ex_hdr.response) {
  663. if (!hdr.out_words && !ex_hdr.provider_out_words) {
  664. ret = -EINVAL;
  665. goto out;
  666. }
  667. if (!access_ok(VERIFY_WRITE,
  668. (void __user *) (unsigned long) ex_hdr.response,
  669. (hdr.out_words + ex_hdr.provider_out_words) * 8)) {
  670. ret = -EFAULT;
  671. goto out;
  672. }
  673. } else {
  674. if (hdr.out_words || ex_hdr.provider_out_words) {
  675. ret = -EINVAL;
  676. goto out;
  677. }
  678. }
  679. INIT_UDATA_BUF_OR_NULL(&ucore, buf, (unsigned long) ex_hdr.response,
  680. hdr.in_words * 8, hdr.out_words * 8);
  681. INIT_UDATA_BUF_OR_NULL(&uhw,
  682. buf + ucore.inlen,
  683. (unsigned long) ex_hdr.response + ucore.outlen,
  684. ex_hdr.provider_in_words * 8,
  685. ex_hdr.provider_out_words * 8);
  686. ret = uverbs_ex_cmd_table[command](file,
  687. ib_dev,
  688. &ucore,
  689. &uhw);
  690. if (!ret)
  691. ret = written_count;
  692. } else {
  693. ret = -ENOSYS;
  694. }
  695. out:
  696. srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
  697. return ret;
  698. }
  699. static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma)
  700. {
  701. struct ib_uverbs_file *file = filp->private_data;
  702. struct ib_device *ib_dev;
  703. int ret = 0;
  704. int srcu_key;
  705. srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
  706. ib_dev = srcu_dereference(file->device->ib_dev,
  707. &file->device->disassociate_srcu);
  708. if (!ib_dev) {
  709. ret = -EIO;
  710. goto out;
  711. }
  712. if (!file->ucontext)
  713. ret = -ENODEV;
  714. else
  715. ret = ib_dev->mmap(file->ucontext, vma);
  716. out:
  717. srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
  718. return ret;
  719. }
  720. /*
  721. * ib_uverbs_open() does not need the BKL:
  722. *
  723. * - the ib_uverbs_device structures are properly reference counted and
  724. * everything else is purely local to the file being created, so
  725. * races against other open calls are not a problem;
  726. * - there is no ioctl method to race against;
  727. * - the open method will either immediately run -ENXIO, or all
  728. * required initialization will be done.
  729. */
  730. static int ib_uverbs_open(struct inode *inode, struct file *filp)
  731. {
  732. struct ib_uverbs_device *dev;
  733. struct ib_uverbs_file *file;
  734. struct ib_device *ib_dev;
  735. int ret;
  736. int module_dependent;
  737. int srcu_key;
  738. dev = container_of(inode->i_cdev, struct ib_uverbs_device, cdev);
  739. if (!atomic_inc_not_zero(&dev->refcount))
  740. return -ENXIO;
  741. srcu_key = srcu_read_lock(&dev->disassociate_srcu);
  742. mutex_lock(&dev->lists_mutex);
  743. ib_dev = srcu_dereference(dev->ib_dev,
  744. &dev->disassociate_srcu);
  745. if (!ib_dev) {
  746. ret = -EIO;
  747. goto err;
  748. }
  749. /* In case IB device supports disassociate ucontext, there is no hard
  750. * dependency between uverbs device and its low level device.
  751. */
  752. module_dependent = !(ib_dev->disassociate_ucontext);
  753. if (module_dependent) {
  754. if (!try_module_get(ib_dev->owner)) {
  755. ret = -ENODEV;
  756. goto err;
  757. }
  758. }
  759. file = kzalloc(sizeof(*file), GFP_KERNEL);
  760. if (!file) {
  761. ret = -ENOMEM;
  762. if (module_dependent)
  763. goto err_module;
  764. goto err;
  765. }
  766. file->device = dev;
  767. file->ucontext = NULL;
  768. file->async_file = NULL;
  769. kref_init(&file->ref);
  770. mutex_init(&file->mutex);
  771. filp->private_data = file;
  772. kobject_get(&dev->kobj);
  773. list_add_tail(&file->list, &dev->uverbs_file_list);
  774. mutex_unlock(&dev->lists_mutex);
  775. srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
  776. return nonseekable_open(inode, filp);
  777. err_module:
  778. module_put(ib_dev->owner);
  779. err:
  780. mutex_unlock(&dev->lists_mutex);
  781. srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
  782. if (atomic_dec_and_test(&dev->refcount))
  783. ib_uverbs_comp_dev(dev);
  784. return ret;
  785. }
  786. static int ib_uverbs_close(struct inode *inode, struct file *filp)
  787. {
  788. struct ib_uverbs_file *file = filp->private_data;
  789. struct ib_uverbs_device *dev = file->device;
  790. struct ib_ucontext *ucontext = NULL;
  791. mutex_lock(&file->device->lists_mutex);
  792. ucontext = file->ucontext;
  793. file->ucontext = NULL;
  794. if (!file->is_closed) {
  795. list_del(&file->list);
  796. file->is_closed = 1;
  797. }
  798. mutex_unlock(&file->device->lists_mutex);
  799. if (ucontext)
  800. ib_uverbs_cleanup_ucontext(file, ucontext);
  801. if (file->async_file)
  802. kref_put(&file->async_file->ref, ib_uverbs_release_event_file);
  803. kref_put(&file->ref, ib_uverbs_release_file);
  804. kobject_put(&dev->kobj);
  805. return 0;
  806. }
  807. static const struct file_operations uverbs_fops = {
  808. .owner = THIS_MODULE,
  809. .write = ib_uverbs_write,
  810. .open = ib_uverbs_open,
  811. .release = ib_uverbs_close,
  812. .llseek = no_llseek,
  813. };
  814. static const struct file_operations uverbs_mmap_fops = {
  815. .owner = THIS_MODULE,
  816. .write = ib_uverbs_write,
  817. .mmap = ib_uverbs_mmap,
  818. .open = ib_uverbs_open,
  819. .release = ib_uverbs_close,
  820. .llseek = no_llseek,
  821. };
  822. static struct ib_client uverbs_client = {
  823. .name = "uverbs",
  824. .add = ib_uverbs_add_one,
  825. .remove = ib_uverbs_remove_one
  826. };
  827. static ssize_t show_ibdev(struct device *device, struct device_attribute *attr,
  828. char *buf)
  829. {
  830. int ret = -ENODEV;
  831. int srcu_key;
  832. struct ib_uverbs_device *dev = dev_get_drvdata(device);
  833. struct ib_device *ib_dev;
  834. if (!dev)
  835. return -ENODEV;
  836. srcu_key = srcu_read_lock(&dev->disassociate_srcu);
  837. ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
  838. if (ib_dev)
  839. ret = sprintf(buf, "%s\n", ib_dev->name);
  840. srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
  841. return ret;
  842. }
  843. static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
  844. static ssize_t show_dev_abi_version(struct device *device,
  845. struct device_attribute *attr, char *buf)
  846. {
  847. struct ib_uverbs_device *dev = dev_get_drvdata(device);
  848. int ret = -ENODEV;
  849. int srcu_key;
  850. struct ib_device *ib_dev;
  851. if (!dev)
  852. return -ENODEV;
  853. srcu_key = srcu_read_lock(&dev->disassociate_srcu);
  854. ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
  855. if (ib_dev)
  856. ret = sprintf(buf, "%d\n", ib_dev->uverbs_abi_ver);
  857. srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
  858. return ret;
  859. }
  860. static DEVICE_ATTR(abi_version, S_IRUGO, show_dev_abi_version, NULL);
  861. static CLASS_ATTR_STRING(abi_version, S_IRUGO,
  862. __stringify(IB_USER_VERBS_ABI_VERSION));
  863. static dev_t overflow_maj;
  864. static DECLARE_BITMAP(overflow_map, IB_UVERBS_MAX_DEVICES);
  865. /*
  866. * If we have more than IB_UVERBS_MAX_DEVICES, dynamically overflow by
  867. * requesting a new major number and doubling the number of max devices we
  868. * support. It's stupid, but simple.
  869. */
  870. static int find_overflow_devnum(void)
  871. {
  872. int ret;
  873. if (!overflow_maj) {
  874. ret = alloc_chrdev_region(&overflow_maj, 0, IB_UVERBS_MAX_DEVICES,
  875. "infiniband_verbs");
  876. if (ret) {
  877. pr_err("user_verbs: couldn't register dynamic device number\n");
  878. return ret;
  879. }
  880. }
  881. ret = find_first_zero_bit(overflow_map, IB_UVERBS_MAX_DEVICES);
  882. if (ret >= IB_UVERBS_MAX_DEVICES)
  883. return -1;
  884. return ret;
  885. }
  886. static void ib_uverbs_add_one(struct ib_device *device)
  887. {
  888. int devnum;
  889. dev_t base;
  890. struct ib_uverbs_device *uverbs_dev;
  891. int ret;
  892. if (!device->alloc_ucontext)
  893. return;
  894. uverbs_dev = kzalloc(sizeof *uverbs_dev, GFP_KERNEL);
  895. if (!uverbs_dev)
  896. return;
  897. ret = init_srcu_struct(&uverbs_dev->disassociate_srcu);
  898. if (ret) {
  899. kfree(uverbs_dev);
  900. return;
  901. }
  902. atomic_set(&uverbs_dev->refcount, 1);
  903. init_completion(&uverbs_dev->comp);
  904. uverbs_dev->xrcd_tree = RB_ROOT;
  905. mutex_init(&uverbs_dev->xrcd_tree_mutex);
  906. kobject_init(&uverbs_dev->kobj, &ib_uverbs_dev_ktype);
  907. mutex_init(&uverbs_dev->lists_mutex);
  908. INIT_LIST_HEAD(&uverbs_dev->uverbs_file_list);
  909. INIT_LIST_HEAD(&uverbs_dev->uverbs_events_file_list);
  910. spin_lock(&map_lock);
  911. devnum = find_first_zero_bit(dev_map, IB_UVERBS_MAX_DEVICES);
  912. if (devnum >= IB_UVERBS_MAX_DEVICES) {
  913. spin_unlock(&map_lock);
  914. devnum = find_overflow_devnum();
  915. if (devnum < 0)
  916. goto err;
  917. spin_lock(&map_lock);
  918. uverbs_dev->devnum = devnum + IB_UVERBS_MAX_DEVICES;
  919. base = devnum + overflow_maj;
  920. set_bit(devnum, overflow_map);
  921. } else {
  922. uverbs_dev->devnum = devnum;
  923. base = devnum + IB_UVERBS_BASE_DEV;
  924. set_bit(devnum, dev_map);
  925. }
  926. spin_unlock(&map_lock);
  927. rcu_assign_pointer(uverbs_dev->ib_dev, device);
  928. uverbs_dev->num_comp_vectors = device->num_comp_vectors;
  929. cdev_init(&uverbs_dev->cdev, NULL);
  930. uverbs_dev->cdev.owner = THIS_MODULE;
  931. uverbs_dev->cdev.ops = device->mmap ? &uverbs_mmap_fops : &uverbs_fops;
  932. uverbs_dev->cdev.kobj.parent = &uverbs_dev->kobj;
  933. kobject_set_name(&uverbs_dev->cdev.kobj, "uverbs%d", uverbs_dev->devnum);
  934. if (cdev_add(&uverbs_dev->cdev, base, 1))
  935. goto err_cdev;
  936. uverbs_dev->dev = device_create(uverbs_class, device->dma_device,
  937. uverbs_dev->cdev.dev, uverbs_dev,
  938. "uverbs%d", uverbs_dev->devnum);
  939. if (IS_ERR(uverbs_dev->dev))
  940. goto err_cdev;
  941. if (device_create_file(uverbs_dev->dev, &dev_attr_ibdev))
  942. goto err_class;
  943. if (device_create_file(uverbs_dev->dev, &dev_attr_abi_version))
  944. goto err_class;
  945. ib_set_client_data(device, &uverbs_client, uverbs_dev);
  946. return;
  947. err_class:
  948. device_destroy(uverbs_class, uverbs_dev->cdev.dev);
  949. err_cdev:
  950. cdev_del(&uverbs_dev->cdev);
  951. if (uverbs_dev->devnum < IB_UVERBS_MAX_DEVICES)
  952. clear_bit(devnum, dev_map);
  953. else
  954. clear_bit(devnum, overflow_map);
  955. err:
  956. if (atomic_dec_and_test(&uverbs_dev->refcount))
  957. ib_uverbs_comp_dev(uverbs_dev);
  958. wait_for_completion(&uverbs_dev->comp);
  959. kobject_put(&uverbs_dev->kobj);
  960. return;
  961. }
  962. static void ib_uverbs_free_hw_resources(struct ib_uverbs_device *uverbs_dev,
  963. struct ib_device *ib_dev)
  964. {
  965. struct ib_uverbs_file *file;
  966. struct ib_uverbs_event_file *event_file;
  967. struct ib_event event;
  968. /* Pending running commands to terminate */
  969. synchronize_srcu(&uverbs_dev->disassociate_srcu);
  970. event.event = IB_EVENT_DEVICE_FATAL;
  971. event.element.port_num = 0;
  972. event.device = ib_dev;
  973. mutex_lock(&uverbs_dev->lists_mutex);
  974. while (!list_empty(&uverbs_dev->uverbs_file_list)) {
  975. struct ib_ucontext *ucontext;
  976. file = list_first_entry(&uverbs_dev->uverbs_file_list,
  977. struct ib_uverbs_file, list);
  978. file->is_closed = 1;
  979. ucontext = file->ucontext;
  980. list_del(&file->list);
  981. file->ucontext = NULL;
  982. kref_get(&file->ref);
  983. mutex_unlock(&uverbs_dev->lists_mutex);
  984. /* We must release the mutex before going ahead and calling
  985. * disassociate_ucontext. disassociate_ucontext might end up
  986. * indirectly calling uverbs_close, for example due to freeing
  987. * the resources (e.g mmput).
  988. */
  989. ib_uverbs_event_handler(&file->event_handler, &event);
  990. if (ucontext) {
  991. ib_dev->disassociate_ucontext(ucontext);
  992. ib_uverbs_cleanup_ucontext(file, ucontext);
  993. }
  994. mutex_lock(&uverbs_dev->lists_mutex);
  995. kref_put(&file->ref, ib_uverbs_release_file);
  996. }
  997. while (!list_empty(&uverbs_dev->uverbs_events_file_list)) {
  998. event_file = list_first_entry(&uverbs_dev->
  999. uverbs_events_file_list,
  1000. struct ib_uverbs_event_file,
  1001. list);
  1002. spin_lock_irq(&event_file->lock);
  1003. event_file->is_closed = 1;
  1004. spin_unlock_irq(&event_file->lock);
  1005. list_del(&event_file->list);
  1006. if (event_file->is_async) {
  1007. ib_unregister_event_handler(&event_file->uverbs_file->
  1008. event_handler);
  1009. event_file->uverbs_file->event_handler.device = NULL;
  1010. }
  1011. wake_up_interruptible(&event_file->poll_wait);
  1012. kill_fasync(&event_file->async_queue, SIGIO, POLL_IN);
  1013. }
  1014. mutex_unlock(&uverbs_dev->lists_mutex);
  1015. }
  1016. static void ib_uverbs_remove_one(struct ib_device *device, void *client_data)
  1017. {
  1018. struct ib_uverbs_device *uverbs_dev = client_data;
  1019. int wait_clients = 1;
  1020. if (!uverbs_dev)
  1021. return;
  1022. dev_set_drvdata(uverbs_dev->dev, NULL);
  1023. device_destroy(uverbs_class, uverbs_dev->cdev.dev);
  1024. cdev_del(&uverbs_dev->cdev);
  1025. if (uverbs_dev->devnum < IB_UVERBS_MAX_DEVICES)
  1026. clear_bit(uverbs_dev->devnum, dev_map);
  1027. else
  1028. clear_bit(uverbs_dev->devnum - IB_UVERBS_MAX_DEVICES, overflow_map);
  1029. if (device->disassociate_ucontext) {
  1030. /* We disassociate HW resources and immediately return.
  1031. * Userspace will see a EIO errno for all future access.
  1032. * Upon returning, ib_device may be freed internally and is not
  1033. * valid any more.
  1034. * uverbs_device is still available until all clients close
  1035. * their files, then the uverbs device ref count will be zero
  1036. * and its resources will be freed.
  1037. * Note: At this point no more files can be opened since the
  1038. * cdev was deleted, however active clients can still issue
  1039. * commands and close their open files.
  1040. */
  1041. rcu_assign_pointer(uverbs_dev->ib_dev, NULL);
  1042. ib_uverbs_free_hw_resources(uverbs_dev, device);
  1043. wait_clients = 0;
  1044. }
  1045. if (atomic_dec_and_test(&uverbs_dev->refcount))
  1046. ib_uverbs_comp_dev(uverbs_dev);
  1047. if (wait_clients)
  1048. wait_for_completion(&uverbs_dev->comp);
  1049. kobject_put(&uverbs_dev->kobj);
  1050. }
  1051. static char *uverbs_devnode(struct device *dev, umode_t *mode)
  1052. {
  1053. if (mode)
  1054. *mode = 0666;
  1055. return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
  1056. }
  1057. static int __init ib_uverbs_init(void)
  1058. {
  1059. int ret;
  1060. ret = register_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES,
  1061. "infiniband_verbs");
  1062. if (ret) {
  1063. pr_err("user_verbs: couldn't register device number\n");
  1064. goto out;
  1065. }
  1066. uverbs_class = class_create(THIS_MODULE, "infiniband_verbs");
  1067. if (IS_ERR(uverbs_class)) {
  1068. ret = PTR_ERR(uverbs_class);
  1069. pr_err("user_verbs: couldn't create class infiniband_verbs\n");
  1070. goto out_chrdev;
  1071. }
  1072. uverbs_class->devnode = uverbs_devnode;
  1073. ret = class_create_file(uverbs_class, &class_attr_abi_version.attr);
  1074. if (ret) {
  1075. pr_err("user_verbs: couldn't create abi_version attribute\n");
  1076. goto out_class;
  1077. }
  1078. ret = ib_register_client(&uverbs_client);
  1079. if (ret) {
  1080. pr_err("user_verbs: couldn't register client\n");
  1081. goto out_class;
  1082. }
  1083. return 0;
  1084. out_class:
  1085. class_destroy(uverbs_class);
  1086. out_chrdev:
  1087. unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES);
  1088. out:
  1089. return ret;
  1090. }
  1091. static void __exit ib_uverbs_cleanup(void)
  1092. {
  1093. ib_unregister_client(&uverbs_client);
  1094. class_destroy(uverbs_class);
  1095. unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES);
  1096. if (overflow_maj)
  1097. unregister_chrdev_region(overflow_maj, IB_UVERBS_MAX_DEVICES);
  1098. idr_destroy(&ib_uverbs_pd_idr);
  1099. idr_destroy(&ib_uverbs_mr_idr);
  1100. idr_destroy(&ib_uverbs_mw_idr);
  1101. idr_destroy(&ib_uverbs_ah_idr);
  1102. idr_destroy(&ib_uverbs_cq_idr);
  1103. idr_destroy(&ib_uverbs_qp_idr);
  1104. idr_destroy(&ib_uverbs_srq_idr);
  1105. }
  1106. module_init(ib_uverbs_init);
  1107. module_exit(ib_uverbs_cleanup);