uverbs_main.c 37 KB

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