uverbs_main.c 35 KB

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