uverbs_main.c 36 KB

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