uverbs_main.c 35 KB

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