uverbs_main.c 35 KB

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