uverbs_main.c 37 KB

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