uverbs_main.c 35 KB

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