uverbs_main.c 38 KB

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