uverbs_main.c 37 KB

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