uverbs_main.c 35 KB

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