uverbs_main.c 36 KB

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