uverbs_main.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  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/sched/mm.h>
  48. #include <linux/uaccess.h>
  49. #include <rdma/ib.h>
  50. #include <rdma/uverbs_std_types.h>
  51. #include <rdma/rdma_netlink.h>
  52. #include <rdma/ib_ucaps.h>
  53. #include "uverbs.h"
  54. #include "core_priv.h"
  55. #include "rdma_core.h"
  56. MODULE_AUTHOR("Roland Dreier");
  57. MODULE_DESCRIPTION("InfiniBand userspace verbs access");
  58. MODULE_LICENSE("Dual BSD/GPL");
  59. enum {
  60. IB_UVERBS_MAJOR = 231,
  61. IB_UVERBS_BASE_MINOR = 192,
  62. IB_UVERBS_MAX_DEVICES = RDMA_MAX_PORTS,
  63. IB_UVERBS_NUM_FIXED_MINOR = 32,
  64. IB_UVERBS_NUM_DYNAMIC_MINOR = IB_UVERBS_MAX_DEVICES - IB_UVERBS_NUM_FIXED_MINOR,
  65. };
  66. #define IB_UVERBS_BASE_DEV MKDEV(IB_UVERBS_MAJOR, IB_UVERBS_BASE_MINOR)
  67. static dev_t dynamic_uverbs_dev;
  68. static DEFINE_IDA(uverbs_ida);
  69. static int ib_uverbs_add_one(struct ib_device *device);
  70. static void ib_uverbs_remove_one(struct ib_device *device, void *client_data);
  71. static struct ib_client uverbs_client;
  72. static char *uverbs_devnode(const struct device *dev, umode_t *mode)
  73. {
  74. if (mode)
  75. *mode = 0666;
  76. return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
  77. }
  78. static const struct class uverbs_class = {
  79. .name = "infiniband_verbs",
  80. .devnode = uverbs_devnode,
  81. };
  82. /*
  83. * Must be called with the ufile->device->disassociate_srcu held, and the lock
  84. * must be held until use of the ucontext is finished.
  85. */
  86. struct ib_ucontext *ib_uverbs_get_ucontext_file(struct ib_uverbs_file *ufile)
  87. {
  88. /*
  89. * We do not hold the hw_destroy_rwsem lock for this flow, instead
  90. * srcu is used. It does not matter if someone races this with
  91. * get_context, we get NULL or valid ucontext.
  92. */
  93. struct ib_ucontext *ucontext = smp_load_acquire(&ufile->ucontext);
  94. if (!srcu_dereference(ufile->device->ib_dev,
  95. &ufile->device->disassociate_srcu))
  96. return ERR_PTR(-EIO);
  97. if (!ucontext)
  98. return ERR_PTR(-EINVAL);
  99. return ucontext;
  100. }
  101. EXPORT_SYMBOL(ib_uverbs_get_ucontext_file);
  102. int uverbs_dealloc_mw(struct ib_mw *mw)
  103. {
  104. struct ib_pd *pd = mw->pd;
  105. int ret;
  106. ret = mw->device->ops.dealloc_mw(mw);
  107. if (ret)
  108. return ret;
  109. atomic_dec(&pd->usecnt);
  110. kfree(mw);
  111. return ret;
  112. }
  113. static void ib_uverbs_release_dev(struct device *device)
  114. {
  115. struct ib_uverbs_device *dev =
  116. container_of(device, struct ib_uverbs_device, dev);
  117. uverbs_destroy_api(dev->uapi);
  118. cleanup_srcu_struct(&dev->disassociate_srcu);
  119. mutex_destroy(&dev->lists_mutex);
  120. mutex_destroy(&dev->xrcd_tree_mutex);
  121. kfree(dev);
  122. }
  123. void ib_uverbs_release_ucq(struct ib_uverbs_completion_event_file *ev_file,
  124. struct ib_ucq_object *uobj)
  125. {
  126. struct ib_uverbs_event *evt, *tmp;
  127. if (ev_file) {
  128. spin_lock_irq(&ev_file->ev_queue.lock);
  129. list_for_each_entry_safe(evt, tmp, &uobj->comp_list, obj_list) {
  130. list_del(&evt->list);
  131. kfree(evt);
  132. }
  133. spin_unlock_irq(&ev_file->ev_queue.lock);
  134. uverbs_uobject_put(&ev_file->uobj);
  135. }
  136. ib_uverbs_release_uevent(&uobj->uevent);
  137. }
  138. void ib_uverbs_release_uevent(struct ib_uevent_object *uobj)
  139. {
  140. struct ib_uverbs_async_event_file *async_file = uobj->event_file;
  141. struct ib_uverbs_event *evt, *tmp;
  142. if (!async_file)
  143. return;
  144. spin_lock_irq(&async_file->ev_queue.lock);
  145. list_for_each_entry_safe(evt, tmp, &uobj->event_list, obj_list) {
  146. list_del(&evt->list);
  147. kfree(evt);
  148. }
  149. spin_unlock_irq(&async_file->ev_queue.lock);
  150. uverbs_uobject_put(&async_file->uobj);
  151. }
  152. void ib_uverbs_detach_umcast(struct ib_qp *qp,
  153. struct ib_uqp_object *uobj)
  154. {
  155. struct ib_uverbs_mcast_entry *mcast, *tmp;
  156. list_for_each_entry_safe(mcast, tmp, &uobj->mcast_list, list) {
  157. ib_detach_mcast(qp, &mcast->gid, mcast->lid);
  158. list_del(&mcast->list);
  159. kfree(mcast);
  160. }
  161. }
  162. static void ib_uverbs_comp_dev(struct ib_uverbs_device *dev)
  163. {
  164. complete(&dev->comp);
  165. }
  166. void ib_uverbs_release_file(struct kref *ref)
  167. {
  168. struct ib_uverbs_file *file =
  169. container_of(ref, struct ib_uverbs_file, ref);
  170. struct ib_device *ib_dev;
  171. int srcu_key;
  172. release_ufile_idr_uobject(file);
  173. srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
  174. ib_dev = srcu_dereference(file->device->ib_dev,
  175. &file->device->disassociate_srcu);
  176. if (ib_dev && !ib_dev->ops.disassociate_ucontext)
  177. module_put(ib_dev->ops.owner);
  178. srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
  179. if (refcount_dec_and_test(&file->device->refcount))
  180. ib_uverbs_comp_dev(file->device);
  181. if (file->default_async_file)
  182. uverbs_uobject_put(&file->default_async_file->uobj);
  183. put_device(&file->device->dev);
  184. if (file->disassociate_page)
  185. __free_pages(file->disassociate_page, 0);
  186. mutex_destroy(&file->disassociation_lock);
  187. mutex_destroy(&file->umap_lock);
  188. mutex_destroy(&file->ucontext_lock);
  189. kfree(file);
  190. }
  191. static ssize_t ib_uverbs_event_read(struct ib_uverbs_event_queue *ev_queue,
  192. struct file *filp, char __user *buf,
  193. size_t count, loff_t *pos,
  194. size_t eventsz)
  195. {
  196. struct ib_uverbs_event *event;
  197. int ret = 0;
  198. spin_lock_irq(&ev_queue->lock);
  199. while (list_empty(&ev_queue->event_list)) {
  200. if (ev_queue->is_closed) {
  201. spin_unlock_irq(&ev_queue->lock);
  202. return -EIO;
  203. }
  204. spin_unlock_irq(&ev_queue->lock);
  205. if (filp->f_flags & O_NONBLOCK)
  206. return -EAGAIN;
  207. if (wait_event_interruptible(ev_queue->poll_wait,
  208. (!list_empty(&ev_queue->event_list) ||
  209. ev_queue->is_closed)))
  210. return -ERESTARTSYS;
  211. spin_lock_irq(&ev_queue->lock);
  212. }
  213. event = list_entry(ev_queue->event_list.next, struct ib_uverbs_event, list);
  214. if (eventsz > count) {
  215. ret = -EINVAL;
  216. event = NULL;
  217. } else {
  218. list_del(ev_queue->event_list.next);
  219. if (event->counter) {
  220. ++(*event->counter);
  221. list_del(&event->obj_list);
  222. }
  223. }
  224. spin_unlock_irq(&ev_queue->lock);
  225. if (event) {
  226. if (copy_to_user(buf, event, eventsz))
  227. ret = -EFAULT;
  228. else
  229. ret = eventsz;
  230. }
  231. kfree(event);
  232. return ret;
  233. }
  234. static ssize_t ib_uverbs_async_event_read(struct file *filp, char __user *buf,
  235. size_t count, loff_t *pos)
  236. {
  237. struct ib_uverbs_async_event_file *file = filp->private_data;
  238. return ib_uverbs_event_read(&file->ev_queue, filp, buf, count, pos,
  239. sizeof(struct ib_uverbs_async_event_desc));
  240. }
  241. static ssize_t ib_uverbs_comp_event_read(struct file *filp, char __user *buf,
  242. size_t count, loff_t *pos)
  243. {
  244. struct ib_uverbs_completion_event_file *comp_ev_file =
  245. filp->private_data;
  246. return ib_uverbs_event_read(&comp_ev_file->ev_queue, filp, buf, count,
  247. pos,
  248. sizeof(struct ib_uverbs_comp_event_desc));
  249. }
  250. static __poll_t ib_uverbs_event_poll(struct ib_uverbs_event_queue *ev_queue,
  251. struct file *filp,
  252. struct poll_table_struct *wait)
  253. {
  254. __poll_t pollflags = 0;
  255. poll_wait(filp, &ev_queue->poll_wait, wait);
  256. spin_lock_irq(&ev_queue->lock);
  257. if (!list_empty(&ev_queue->event_list))
  258. pollflags = EPOLLIN | EPOLLRDNORM;
  259. else if (ev_queue->is_closed)
  260. pollflags = EPOLLERR;
  261. spin_unlock_irq(&ev_queue->lock);
  262. return pollflags;
  263. }
  264. static __poll_t ib_uverbs_async_event_poll(struct file *filp,
  265. struct poll_table_struct *wait)
  266. {
  267. struct ib_uverbs_async_event_file *file = filp->private_data;
  268. return ib_uverbs_event_poll(&file->ev_queue, filp, wait);
  269. }
  270. static __poll_t ib_uverbs_comp_event_poll(struct file *filp,
  271. struct poll_table_struct *wait)
  272. {
  273. struct ib_uverbs_completion_event_file *comp_ev_file =
  274. filp->private_data;
  275. return ib_uverbs_event_poll(&comp_ev_file->ev_queue, filp, wait);
  276. }
  277. static int ib_uverbs_async_event_fasync(int fd, struct file *filp, int on)
  278. {
  279. struct ib_uverbs_async_event_file *file = filp->private_data;
  280. return fasync_helper(fd, filp, on, &file->ev_queue.async_queue);
  281. }
  282. static int ib_uverbs_comp_event_fasync(int fd, struct file *filp, int on)
  283. {
  284. struct ib_uverbs_completion_event_file *comp_ev_file =
  285. filp->private_data;
  286. return fasync_helper(fd, filp, on, &comp_ev_file->ev_queue.async_queue);
  287. }
  288. const struct file_operations uverbs_event_fops = {
  289. .owner = THIS_MODULE,
  290. .read = ib_uverbs_comp_event_read,
  291. .poll = ib_uverbs_comp_event_poll,
  292. .release = uverbs_uobject_fd_release,
  293. .fasync = ib_uverbs_comp_event_fasync,
  294. };
  295. const struct file_operations uverbs_async_event_fops = {
  296. .owner = THIS_MODULE,
  297. .read = ib_uverbs_async_event_read,
  298. .poll = ib_uverbs_async_event_poll,
  299. .release = uverbs_async_event_release,
  300. .fasync = ib_uverbs_async_event_fasync,
  301. };
  302. void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context)
  303. {
  304. struct ib_uverbs_event_queue *ev_queue = cq_context;
  305. struct ib_ucq_object *uobj;
  306. struct ib_uverbs_event *entry;
  307. unsigned long flags;
  308. if (!ev_queue)
  309. return;
  310. spin_lock_irqsave(&ev_queue->lock, flags);
  311. if (ev_queue->is_closed) {
  312. spin_unlock_irqrestore(&ev_queue->lock, flags);
  313. return;
  314. }
  315. entry = kmalloc_obj(*entry, GFP_ATOMIC);
  316. if (!entry) {
  317. spin_unlock_irqrestore(&ev_queue->lock, flags);
  318. return;
  319. }
  320. uobj = cq->uobject;
  321. entry->desc.comp.cq_handle = cq->uobject->uevent.uobject.user_handle;
  322. entry->counter = &uobj->comp_events_reported;
  323. list_add_tail(&entry->list, &ev_queue->event_list);
  324. list_add_tail(&entry->obj_list, &uobj->comp_list);
  325. spin_unlock_irqrestore(&ev_queue->lock, flags);
  326. wake_up_interruptible(&ev_queue->poll_wait);
  327. kill_fasync(&ev_queue->async_queue, SIGIO, POLL_IN);
  328. }
  329. void ib_uverbs_async_handler(struct ib_uverbs_async_event_file *async_file,
  330. __u64 element, __u64 event,
  331. struct list_head *obj_list, u32 *counter)
  332. {
  333. struct ib_uverbs_event *entry;
  334. unsigned long flags;
  335. if (!async_file)
  336. return;
  337. spin_lock_irqsave(&async_file->ev_queue.lock, flags);
  338. if (async_file->ev_queue.is_closed) {
  339. spin_unlock_irqrestore(&async_file->ev_queue.lock, flags);
  340. return;
  341. }
  342. entry = kmalloc_obj(*entry, GFP_ATOMIC);
  343. if (!entry) {
  344. spin_unlock_irqrestore(&async_file->ev_queue.lock, flags);
  345. return;
  346. }
  347. entry->desc.async.element = element;
  348. entry->desc.async.event_type = event;
  349. entry->desc.async.reserved = 0;
  350. entry->counter = counter;
  351. list_add_tail(&entry->list, &async_file->ev_queue.event_list);
  352. if (obj_list)
  353. list_add_tail(&entry->obj_list, obj_list);
  354. spin_unlock_irqrestore(&async_file->ev_queue.lock, flags);
  355. wake_up_interruptible(&async_file->ev_queue.poll_wait);
  356. kill_fasync(&async_file->ev_queue.async_queue, SIGIO, POLL_IN);
  357. }
  358. static void uverbs_uobj_event(struct ib_uevent_object *eobj,
  359. struct ib_event *event)
  360. {
  361. ib_uverbs_async_handler(eobj->event_file,
  362. eobj->uobject.user_handle, event->event,
  363. &eobj->event_list, &eobj->events_reported);
  364. }
  365. void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr)
  366. {
  367. uverbs_uobj_event(&event->element.cq->uobject->uevent, event);
  368. }
  369. void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr)
  370. {
  371. /* for XRC target qp's, check that qp is live */
  372. if (!event->element.qp->uobject)
  373. return;
  374. uverbs_uobj_event(&event->element.qp->uobject->uevent, event);
  375. }
  376. void ib_uverbs_wq_event_handler(struct ib_event *event, void *context_ptr)
  377. {
  378. uverbs_uobj_event(&event->element.wq->uobject->uevent, event);
  379. }
  380. void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr)
  381. {
  382. uverbs_uobj_event(&event->element.srq->uobject->uevent, event);
  383. }
  384. static void ib_uverbs_event_handler(struct ib_event_handler *handler,
  385. struct ib_event *event)
  386. {
  387. ib_uverbs_async_handler(
  388. container_of(handler, struct ib_uverbs_async_event_file,
  389. event_handler),
  390. event->element.port_num, event->event, NULL, NULL);
  391. }
  392. void ib_uverbs_init_event_queue(struct ib_uverbs_event_queue *ev_queue)
  393. {
  394. spin_lock_init(&ev_queue->lock);
  395. INIT_LIST_HEAD(&ev_queue->event_list);
  396. init_waitqueue_head(&ev_queue->poll_wait);
  397. ev_queue->is_closed = 0;
  398. ev_queue->async_queue = NULL;
  399. }
  400. void ib_uverbs_init_async_event_file(
  401. struct ib_uverbs_async_event_file *async_file)
  402. {
  403. struct ib_uverbs_file *uverbs_file = async_file->uobj.ufile;
  404. struct ib_device *ib_dev = async_file->uobj.context->device;
  405. ib_uverbs_init_event_queue(&async_file->ev_queue);
  406. /* The first async_event_file becomes the default one for the file. */
  407. mutex_lock(&uverbs_file->ucontext_lock);
  408. if (!uverbs_file->default_async_file) {
  409. /* Pairs with the put in ib_uverbs_release_file */
  410. uverbs_uobject_get(&async_file->uobj);
  411. smp_store_release(&uverbs_file->default_async_file, async_file);
  412. }
  413. mutex_unlock(&uverbs_file->ucontext_lock);
  414. INIT_IB_EVENT_HANDLER(&async_file->event_handler, ib_dev,
  415. ib_uverbs_event_handler);
  416. ib_register_event_handler(&async_file->event_handler);
  417. }
  418. static ssize_t verify_hdr(struct ib_uverbs_cmd_hdr *hdr,
  419. struct ib_uverbs_ex_cmd_hdr *ex_hdr, size_t count,
  420. const struct uverbs_api_write_method *method_elm)
  421. {
  422. if (method_elm->is_ex) {
  423. count -= sizeof(*hdr) + sizeof(*ex_hdr);
  424. if ((hdr->in_words + ex_hdr->provider_in_words) * 8 != count)
  425. return -EINVAL;
  426. if (hdr->in_words * 8 < method_elm->req_size)
  427. return -ENOSPC;
  428. if (ex_hdr->cmd_hdr_reserved)
  429. return -EINVAL;
  430. if (ex_hdr->response) {
  431. if (!hdr->out_words && !ex_hdr->provider_out_words)
  432. return -EINVAL;
  433. if (hdr->out_words * 8 < method_elm->resp_size)
  434. return -ENOSPC;
  435. if (!access_ok(u64_to_user_ptr(ex_hdr->response),
  436. (hdr->out_words + ex_hdr->provider_out_words) * 8))
  437. return -EFAULT;
  438. } else {
  439. if (hdr->out_words || ex_hdr->provider_out_words)
  440. return -EINVAL;
  441. }
  442. return 0;
  443. }
  444. /* not extended command */
  445. if (hdr->in_words * 4 != count)
  446. return -EINVAL;
  447. if (count < method_elm->req_size + sizeof(*hdr)) {
  448. /*
  449. * rdma-core v18 and v19 have a bug where they send DESTROY_CQ
  450. * with a 16 byte write instead of 24. Old kernels didn't
  451. * check the size so they allowed this. Now that the size is
  452. * checked provide a compatibility work around to not break
  453. * those userspaces.
  454. */
  455. if (hdr->command == IB_USER_VERBS_CMD_DESTROY_CQ &&
  456. count == 16) {
  457. hdr->in_words = 6;
  458. return 0;
  459. }
  460. return -ENOSPC;
  461. }
  462. if (hdr->out_words * 4 < method_elm->resp_size)
  463. return -ENOSPC;
  464. return 0;
  465. }
  466. static ssize_t ib_uverbs_write(struct file *filp, const char __user *buf,
  467. size_t count, loff_t *pos)
  468. {
  469. struct ib_uverbs_file *file = filp->private_data;
  470. const struct uverbs_api_write_method *method_elm;
  471. struct uverbs_api *uapi = file->device->uapi;
  472. struct ib_uverbs_ex_cmd_hdr ex_hdr;
  473. struct ib_uverbs_cmd_hdr hdr;
  474. struct uverbs_attr_bundle bundle;
  475. int srcu_key;
  476. ssize_t ret;
  477. if (!ib_safe_file_access(filp)) {
  478. pr_err_once("uverbs_write: process %d (%s) changed security contexts after opening file descriptor, this is not allowed.\n",
  479. task_tgid_vnr(current), current->comm);
  480. return -EACCES;
  481. }
  482. if (count < sizeof(hdr))
  483. return -EINVAL;
  484. if (copy_from_user(&hdr, buf, sizeof(hdr)))
  485. return -EFAULT;
  486. method_elm = uapi_get_method(uapi, hdr.command);
  487. if (IS_ERR(method_elm))
  488. return PTR_ERR(method_elm);
  489. if (method_elm->is_ex) {
  490. if (count < (sizeof(hdr) + sizeof(ex_hdr)))
  491. return -EINVAL;
  492. if (copy_from_user(&ex_hdr, buf + sizeof(hdr), sizeof(ex_hdr)))
  493. return -EFAULT;
  494. }
  495. ret = verify_hdr(&hdr, &ex_hdr, count, method_elm);
  496. if (ret)
  497. return ret;
  498. srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
  499. buf += sizeof(hdr);
  500. memset(bundle.attr_present, 0, sizeof(bundle.attr_present));
  501. bundle.ufile = file;
  502. bundle.context = NULL; /* only valid if bundle has uobject */
  503. bundle.uobject = NULL;
  504. if (!method_elm->is_ex) {
  505. size_t in_len = hdr.in_words * 4 - sizeof(hdr);
  506. size_t out_len = hdr.out_words * 4;
  507. u64 response = 0;
  508. if (method_elm->has_udata) {
  509. bundle.driver_udata.inlen =
  510. in_len - method_elm->req_size;
  511. in_len = method_elm->req_size;
  512. if (bundle.driver_udata.inlen)
  513. bundle.driver_udata.inbuf = buf + in_len;
  514. else
  515. bundle.driver_udata.inbuf = NULL;
  516. } else {
  517. memset(&bundle.driver_udata, 0,
  518. sizeof(bundle.driver_udata));
  519. }
  520. if (method_elm->has_resp) {
  521. /*
  522. * The macros check that if has_resp is set
  523. * then the command request structure starts
  524. * with a '__aligned u64 response' member.
  525. */
  526. ret = get_user(response, (const u64 __user *)buf);
  527. if (ret)
  528. goto out_unlock;
  529. if (method_elm->has_udata) {
  530. bundle.driver_udata.outlen =
  531. out_len - method_elm->resp_size;
  532. out_len = method_elm->resp_size;
  533. if (bundle.driver_udata.outlen)
  534. bundle.driver_udata.outbuf =
  535. u64_to_user_ptr(response +
  536. out_len);
  537. else
  538. bundle.driver_udata.outbuf = NULL;
  539. }
  540. } else {
  541. bundle.driver_udata.outlen = 0;
  542. bundle.driver_udata.outbuf = NULL;
  543. }
  544. ib_uverbs_init_udata_buf_or_null(
  545. &bundle.ucore, buf, u64_to_user_ptr(response),
  546. in_len, out_len);
  547. } else {
  548. buf += sizeof(ex_hdr);
  549. ib_uverbs_init_udata_buf_or_null(&bundle.ucore, buf,
  550. u64_to_user_ptr(ex_hdr.response),
  551. hdr.in_words * 8, hdr.out_words * 8);
  552. ib_uverbs_init_udata_buf_or_null(
  553. &bundle.driver_udata, buf + bundle.ucore.inlen,
  554. u64_to_user_ptr(ex_hdr.response) + bundle.ucore.outlen,
  555. ex_hdr.provider_in_words * 8,
  556. ex_hdr.provider_out_words * 8);
  557. }
  558. ret = method_elm->handler(&bundle);
  559. if (bundle.uobject)
  560. uverbs_finalize_object(bundle.uobject, UVERBS_ACCESS_NEW, true,
  561. !ret, &bundle);
  562. out_unlock:
  563. srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
  564. return (ret) ? : count;
  565. }
  566. static const struct vm_operations_struct rdma_umap_ops;
  567. static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma)
  568. {
  569. struct ib_uverbs_file *file = filp->private_data;
  570. struct ib_ucontext *ucontext;
  571. int ret = 0;
  572. int srcu_key;
  573. srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
  574. ucontext = ib_uverbs_get_ucontext_file(file);
  575. if (IS_ERR(ucontext)) {
  576. ret = PTR_ERR(ucontext);
  577. goto out;
  578. }
  579. mutex_lock(&file->disassociation_lock);
  580. vma->vm_ops = &rdma_umap_ops;
  581. ret = ucontext->device->ops.mmap(ucontext, vma);
  582. mutex_unlock(&file->disassociation_lock);
  583. out:
  584. srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
  585. return ret;
  586. }
  587. /*
  588. * The VMA has been dup'd, initialize the vm_private_data with a new tracking
  589. * struct
  590. */
  591. static void rdma_umap_open(struct vm_area_struct *vma)
  592. {
  593. struct ib_uverbs_file *ufile = vma->vm_file->private_data;
  594. struct rdma_umap_priv *opriv = vma->vm_private_data;
  595. struct rdma_umap_priv *priv;
  596. if (!opriv)
  597. return;
  598. /* We are racing with disassociation */
  599. if (!down_read_trylock(&ufile->hw_destroy_rwsem))
  600. goto out_zap;
  601. mutex_lock(&ufile->disassociation_lock);
  602. /*
  603. * Disassociation already completed, the VMA should already be zapped.
  604. */
  605. if (!ufile->ucontext)
  606. goto out_unlock;
  607. priv = kzalloc_obj(*priv);
  608. if (!priv)
  609. goto out_unlock;
  610. rdma_umap_priv_init(priv, vma, opriv->entry);
  611. mutex_unlock(&ufile->disassociation_lock);
  612. up_read(&ufile->hw_destroy_rwsem);
  613. return;
  614. out_unlock:
  615. mutex_unlock(&ufile->disassociation_lock);
  616. up_read(&ufile->hw_destroy_rwsem);
  617. out_zap:
  618. /*
  619. * We can't allow the VMA to be created with the actual IO pages, that
  620. * would break our API contract, and it can't be stopped at this
  621. * point, so zap it.
  622. */
  623. vma->vm_private_data = NULL;
  624. zap_vma_ptes(vma, vma->vm_start, vma->vm_end - vma->vm_start);
  625. }
  626. static void rdma_umap_close(struct vm_area_struct *vma)
  627. {
  628. struct ib_uverbs_file *ufile = vma->vm_file->private_data;
  629. struct rdma_umap_priv *priv = vma->vm_private_data;
  630. if (!priv)
  631. return;
  632. /*
  633. * The vma holds a reference on the struct file that created it, which
  634. * in turn means that the ib_uverbs_file is guaranteed to exist at
  635. * this point.
  636. */
  637. mutex_lock(&ufile->umap_lock);
  638. if (priv->entry)
  639. rdma_user_mmap_entry_put(priv->entry);
  640. list_del(&priv->list);
  641. mutex_unlock(&ufile->umap_lock);
  642. kfree(priv);
  643. }
  644. /*
  645. * Once the zap_vma_ptes has been called touches to the VMA will come here and
  646. * we return a dummy writable zero page for all the pfns.
  647. */
  648. static vm_fault_t rdma_umap_fault(struct vm_fault *vmf)
  649. {
  650. struct ib_uverbs_file *ufile = vmf->vma->vm_file->private_data;
  651. struct rdma_umap_priv *priv = vmf->vma->vm_private_data;
  652. vm_fault_t ret = 0;
  653. if (!priv)
  654. return VM_FAULT_SIGBUS;
  655. /* Read only pages can just use the system zero page. */
  656. if (!(vmf->vma->vm_flags & (VM_WRITE | VM_MAYWRITE))) {
  657. vmf->page = ZERO_PAGE(vmf->address);
  658. get_page(vmf->page);
  659. return 0;
  660. }
  661. mutex_lock(&ufile->umap_lock);
  662. if (!ufile->disassociate_page)
  663. ufile->disassociate_page =
  664. alloc_pages(vmf->gfp_mask | __GFP_ZERO, 0);
  665. if (ufile->disassociate_page) {
  666. /*
  667. * This VMA is forced to always be shared so this doesn't have
  668. * to worry about COW.
  669. */
  670. vmf->page = ufile->disassociate_page;
  671. get_page(vmf->page);
  672. } else {
  673. ret = VM_FAULT_SIGBUS;
  674. }
  675. mutex_unlock(&ufile->umap_lock);
  676. return ret;
  677. }
  678. static const struct vm_operations_struct rdma_umap_ops = {
  679. .open = rdma_umap_open,
  680. .close = rdma_umap_close,
  681. .fault = rdma_umap_fault,
  682. };
  683. void uverbs_user_mmap_disassociate(struct ib_uverbs_file *ufile)
  684. {
  685. struct rdma_umap_priv *priv, *next_priv;
  686. mutex_lock(&ufile->disassociation_lock);
  687. while (1) {
  688. struct mm_struct *mm = NULL;
  689. /* Get an arbitrary mm pointer that hasn't been cleaned yet */
  690. mutex_lock(&ufile->umap_lock);
  691. while (!list_empty(&ufile->umaps)) {
  692. int ret;
  693. priv = list_first_entry(&ufile->umaps,
  694. struct rdma_umap_priv, list);
  695. mm = priv->vma->vm_mm;
  696. ret = mmget_not_zero(mm);
  697. if (!ret) {
  698. list_del_init(&priv->list);
  699. if (priv->entry) {
  700. rdma_user_mmap_entry_put(priv->entry);
  701. priv->entry = NULL;
  702. }
  703. mm = NULL;
  704. continue;
  705. }
  706. break;
  707. }
  708. mutex_unlock(&ufile->umap_lock);
  709. if (!mm) {
  710. mutex_unlock(&ufile->disassociation_lock);
  711. return;
  712. }
  713. /*
  714. * The umap_lock is nested under mmap_lock since it used within
  715. * the vma_ops callbacks, so we have to clean the list one mm
  716. * at a time to get the lock ordering right. Typically there
  717. * will only be one mm, so no big deal.
  718. */
  719. mmap_read_lock(mm);
  720. mutex_lock(&ufile->umap_lock);
  721. list_for_each_entry_safe (priv, next_priv, &ufile->umaps,
  722. list) {
  723. struct vm_area_struct *vma = priv->vma;
  724. if (vma->vm_mm != mm)
  725. continue;
  726. list_del_init(&priv->list);
  727. zap_vma_ptes(vma, vma->vm_start,
  728. vma->vm_end - vma->vm_start);
  729. if (priv->entry) {
  730. rdma_user_mmap_entry_put(priv->entry);
  731. priv->entry = NULL;
  732. }
  733. }
  734. mutex_unlock(&ufile->umap_lock);
  735. mmap_read_unlock(mm);
  736. mmput(mm);
  737. }
  738. mutex_unlock(&ufile->disassociation_lock);
  739. }
  740. /**
  741. * rdma_user_mmap_disassociate() - Revoke mmaps for a device
  742. * @device: device to revoke
  743. *
  744. * This function should be called by drivers that need to disable mmaps for the
  745. * device, for instance because it is going to be reset.
  746. */
  747. void rdma_user_mmap_disassociate(struct ib_device *device)
  748. {
  749. struct ib_uverbs_device *uverbs_dev =
  750. ib_get_client_data(device, &uverbs_client);
  751. struct ib_uverbs_file *ufile;
  752. mutex_lock(&uverbs_dev->lists_mutex);
  753. list_for_each_entry(ufile, &uverbs_dev->uverbs_file_list, list) {
  754. if (ufile->ucontext)
  755. uverbs_user_mmap_disassociate(ufile);
  756. }
  757. mutex_unlock(&uverbs_dev->lists_mutex);
  758. }
  759. EXPORT_SYMBOL(rdma_user_mmap_disassociate);
  760. /*
  761. * ib_uverbs_open() does not need the BKL:
  762. *
  763. * - the ib_uverbs_device structures are properly reference counted and
  764. * everything else is purely local to the file being created, so
  765. * races against other open calls are not a problem;
  766. * - there is no ioctl method to race against;
  767. * - the open method will either immediately run -ENXIO, or all
  768. * required initialization will be done.
  769. */
  770. static int ib_uverbs_open(struct inode *inode, struct file *filp)
  771. {
  772. struct ib_uverbs_device *dev;
  773. struct ib_uverbs_file *file;
  774. struct ib_device *ib_dev;
  775. int ret;
  776. int module_dependent;
  777. int srcu_key;
  778. dev = container_of(inode->i_cdev, struct ib_uverbs_device, cdev);
  779. if (!refcount_inc_not_zero(&dev->refcount))
  780. return -ENXIO;
  781. get_device(&dev->dev);
  782. srcu_key = srcu_read_lock(&dev->disassociate_srcu);
  783. mutex_lock(&dev->lists_mutex);
  784. ib_dev = srcu_dereference(dev->ib_dev,
  785. &dev->disassociate_srcu);
  786. if (!ib_dev) {
  787. ret = -EIO;
  788. goto err;
  789. }
  790. if (!rdma_dev_access_netns(ib_dev, current->nsproxy->net_ns)) {
  791. ret = -EPERM;
  792. goto err;
  793. }
  794. /* In case IB device supports disassociate ucontext, there is no hard
  795. * dependency between uverbs device and its low level device.
  796. */
  797. module_dependent = !(ib_dev->ops.disassociate_ucontext);
  798. if (module_dependent) {
  799. if (!try_module_get(ib_dev->ops.owner)) {
  800. ret = -ENODEV;
  801. goto err;
  802. }
  803. }
  804. file = kzalloc_obj(*file);
  805. if (!file) {
  806. ret = -ENOMEM;
  807. if (module_dependent)
  808. goto err_module;
  809. goto err;
  810. }
  811. file->device = dev;
  812. kref_init(&file->ref);
  813. mutex_init(&file->ucontext_lock);
  814. spin_lock_init(&file->uobjects_lock);
  815. INIT_LIST_HEAD(&file->uobjects);
  816. init_rwsem(&file->hw_destroy_rwsem);
  817. mutex_init(&file->umap_lock);
  818. INIT_LIST_HEAD(&file->umaps);
  819. mutex_init(&file->disassociation_lock);
  820. filp->private_data = file;
  821. list_add_tail(&file->list, &dev->uverbs_file_list);
  822. mutex_unlock(&dev->lists_mutex);
  823. srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
  824. setup_ufile_idr_uobject(file);
  825. return stream_open(inode, filp);
  826. err_module:
  827. module_put(ib_dev->ops.owner);
  828. err:
  829. mutex_unlock(&dev->lists_mutex);
  830. srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
  831. if (refcount_dec_and_test(&dev->refcount))
  832. ib_uverbs_comp_dev(dev);
  833. put_device(&dev->dev);
  834. return ret;
  835. }
  836. static int ib_uverbs_close(struct inode *inode, struct file *filp)
  837. {
  838. struct ib_uverbs_file *file = filp->private_data;
  839. uverbs_destroy_ufile_hw(file, RDMA_REMOVE_CLOSE);
  840. mutex_lock(&file->device->lists_mutex);
  841. list_del_init(&file->list);
  842. mutex_unlock(&file->device->lists_mutex);
  843. kref_put(&file->ref, ib_uverbs_release_file);
  844. return 0;
  845. }
  846. static const struct file_operations uverbs_fops = {
  847. .owner = THIS_MODULE,
  848. .write = ib_uverbs_write,
  849. .open = ib_uverbs_open,
  850. .release = ib_uverbs_close,
  851. .unlocked_ioctl = ib_uverbs_ioctl,
  852. .compat_ioctl = compat_ptr_ioctl,
  853. };
  854. static const struct file_operations uverbs_mmap_fops = {
  855. .owner = THIS_MODULE,
  856. .write = ib_uverbs_write,
  857. .mmap = ib_uverbs_mmap,
  858. .open = ib_uverbs_open,
  859. .release = ib_uverbs_close,
  860. .unlocked_ioctl = ib_uverbs_ioctl,
  861. .compat_ioctl = compat_ptr_ioctl,
  862. };
  863. static int ib_uverbs_get_nl_info(struct ib_device *ibdev, void *client_data,
  864. struct ib_client_nl_info *res)
  865. {
  866. struct ib_uverbs_device *uverbs_dev = client_data;
  867. int ret;
  868. if (res->port != -1)
  869. return -EINVAL;
  870. res->abi = ibdev->ops.uverbs_abi_ver;
  871. res->cdev = &uverbs_dev->dev;
  872. /*
  873. * To support DRIVER_ID binding in userspace some of the driver need
  874. * upgrading to expose their PCI dependent revision information
  875. * through get_context instead of relying on modalias matching. When
  876. * the drivers are fixed they can drop this flag.
  877. */
  878. if (!ibdev->ops.uverbs_no_driver_id_binding) {
  879. ret = nla_put_u32(res->nl_msg, RDMA_NLDEV_ATTR_UVERBS_DRIVER_ID,
  880. ibdev->ops.driver_id);
  881. if (ret)
  882. return ret;
  883. }
  884. return 0;
  885. }
  886. static struct ib_client uverbs_client = {
  887. .name = "uverbs",
  888. .no_kverbs_req = true,
  889. .add = ib_uverbs_add_one,
  890. .remove = ib_uverbs_remove_one,
  891. .get_nl_info = ib_uverbs_get_nl_info,
  892. };
  893. MODULE_ALIAS_RDMA_CLIENT("uverbs");
  894. static ssize_t ibdev_show(struct device *device, struct device_attribute *attr,
  895. char *buf)
  896. {
  897. struct ib_uverbs_device *dev =
  898. container_of(device, struct ib_uverbs_device, dev);
  899. int ret = -ENODEV;
  900. int srcu_key;
  901. struct ib_device *ib_dev;
  902. srcu_key = srcu_read_lock(&dev->disassociate_srcu);
  903. ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
  904. if (ib_dev)
  905. ret = sysfs_emit(buf, "%s\n", dev_name(&ib_dev->dev));
  906. srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
  907. return ret;
  908. }
  909. static DEVICE_ATTR_RO(ibdev);
  910. static ssize_t abi_version_show(struct device *device,
  911. struct device_attribute *attr, char *buf)
  912. {
  913. struct ib_uverbs_device *dev =
  914. container_of(device, struct ib_uverbs_device, dev);
  915. int ret = -ENODEV;
  916. int srcu_key;
  917. struct ib_device *ib_dev;
  918. srcu_key = srcu_read_lock(&dev->disassociate_srcu);
  919. ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
  920. if (ib_dev)
  921. ret = sysfs_emit(buf, "%u\n", ib_dev->ops.uverbs_abi_ver);
  922. srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
  923. return ret;
  924. }
  925. static DEVICE_ATTR_RO(abi_version);
  926. static struct attribute *ib_dev_attrs[] = {
  927. &dev_attr_abi_version.attr,
  928. &dev_attr_ibdev.attr,
  929. NULL,
  930. };
  931. static const struct attribute_group dev_attr_group = {
  932. .attrs = ib_dev_attrs,
  933. };
  934. static CLASS_ATTR_STRING(abi_version, S_IRUGO,
  935. __stringify(IB_USER_VERBS_ABI_VERSION));
  936. static int ib_uverbs_create_uapi(struct ib_device *device,
  937. struct ib_uverbs_device *uverbs_dev)
  938. {
  939. struct uverbs_api *uapi;
  940. uapi = uverbs_alloc_api(device);
  941. if (IS_ERR(uapi))
  942. return PTR_ERR(uapi);
  943. uverbs_dev->uapi = uapi;
  944. return 0;
  945. }
  946. static int ib_uverbs_add_one(struct ib_device *device)
  947. {
  948. int devnum;
  949. dev_t base;
  950. struct ib_uverbs_device *uverbs_dev;
  951. int ret;
  952. if (!device->ops.alloc_ucontext ||
  953. device->type == RDMA_DEVICE_TYPE_SMI)
  954. return -EOPNOTSUPP;
  955. uverbs_dev = kzalloc_obj(*uverbs_dev);
  956. if (!uverbs_dev)
  957. return -ENOMEM;
  958. ret = init_srcu_struct(&uverbs_dev->disassociate_srcu);
  959. if (ret) {
  960. kfree(uverbs_dev);
  961. return -ENOMEM;
  962. }
  963. device_initialize(&uverbs_dev->dev);
  964. uverbs_dev->dev.class = &uverbs_class;
  965. uverbs_dev->dev.parent = device->dev.parent;
  966. uverbs_dev->dev.release = ib_uverbs_release_dev;
  967. uverbs_dev->groups[0] = &dev_attr_group;
  968. uverbs_dev->dev.groups = uverbs_dev->groups;
  969. refcount_set(&uverbs_dev->refcount, 1);
  970. init_completion(&uverbs_dev->comp);
  971. uverbs_dev->xrcd_tree = RB_ROOT;
  972. mutex_init(&uverbs_dev->xrcd_tree_mutex);
  973. mutex_init(&uverbs_dev->lists_mutex);
  974. INIT_LIST_HEAD(&uverbs_dev->uverbs_file_list);
  975. rcu_assign_pointer(uverbs_dev->ib_dev, device);
  976. uverbs_dev->num_comp_vectors = device->num_comp_vectors;
  977. devnum = ida_alloc_max(&uverbs_ida, IB_UVERBS_MAX_DEVICES - 1,
  978. GFP_KERNEL);
  979. if (devnum < 0) {
  980. ret = -ENOMEM;
  981. goto err;
  982. }
  983. uverbs_dev->devnum = devnum;
  984. if (devnum >= IB_UVERBS_NUM_FIXED_MINOR)
  985. base = dynamic_uverbs_dev + devnum - IB_UVERBS_NUM_FIXED_MINOR;
  986. else
  987. base = IB_UVERBS_BASE_DEV + devnum;
  988. ret = ib_uverbs_create_uapi(device, uverbs_dev);
  989. if (ret)
  990. goto err_uapi;
  991. uverbs_dev->dev.devt = base;
  992. dev_set_name(&uverbs_dev->dev, "uverbs%d", uverbs_dev->devnum);
  993. cdev_init(&uverbs_dev->cdev,
  994. device->ops.mmap ? &uverbs_mmap_fops : &uverbs_fops);
  995. uverbs_dev->cdev.owner = THIS_MODULE;
  996. ret = cdev_device_add(&uverbs_dev->cdev, &uverbs_dev->dev);
  997. if (ret)
  998. goto err_uapi;
  999. ib_set_client_data(device, &uverbs_client, uverbs_dev);
  1000. return 0;
  1001. err_uapi:
  1002. ida_free(&uverbs_ida, devnum);
  1003. err:
  1004. if (refcount_dec_and_test(&uverbs_dev->refcount))
  1005. ib_uverbs_comp_dev(uverbs_dev);
  1006. wait_for_completion(&uverbs_dev->comp);
  1007. put_device(&uverbs_dev->dev);
  1008. return ret;
  1009. }
  1010. static void ib_uverbs_free_hw_resources(struct ib_uverbs_device *uverbs_dev,
  1011. struct ib_device *ib_dev)
  1012. {
  1013. struct ib_uverbs_file *file;
  1014. /* Pending running commands to terminate */
  1015. uverbs_disassociate_api_pre(uverbs_dev);
  1016. mutex_lock(&uverbs_dev->lists_mutex);
  1017. while (!list_empty(&uverbs_dev->uverbs_file_list)) {
  1018. file = list_first_entry(&uverbs_dev->uverbs_file_list,
  1019. struct ib_uverbs_file, list);
  1020. list_del_init(&file->list);
  1021. kref_get(&file->ref);
  1022. /* We must release the mutex before going ahead and calling
  1023. * uverbs_cleanup_ufile, as it might end up indirectly calling
  1024. * uverbs_close, for example due to freeing the resources (e.g
  1025. * mmput).
  1026. */
  1027. mutex_unlock(&uverbs_dev->lists_mutex);
  1028. uverbs_destroy_ufile_hw(file, RDMA_REMOVE_DRIVER_REMOVE);
  1029. kref_put(&file->ref, ib_uverbs_release_file);
  1030. mutex_lock(&uverbs_dev->lists_mutex);
  1031. }
  1032. mutex_unlock(&uverbs_dev->lists_mutex);
  1033. uverbs_disassociate_api(uverbs_dev->uapi);
  1034. }
  1035. static void ib_uverbs_remove_one(struct ib_device *device, void *client_data)
  1036. {
  1037. struct ib_uverbs_device *uverbs_dev = client_data;
  1038. int wait_clients = 1;
  1039. cdev_device_del(&uverbs_dev->cdev, &uverbs_dev->dev);
  1040. ida_free(&uverbs_ida, uverbs_dev->devnum);
  1041. if (device->ops.disassociate_ucontext) {
  1042. /* We disassociate HW resources and immediately return.
  1043. * Userspace will see a EIO errno for all future access.
  1044. * Upon returning, ib_device may be freed internally and is not
  1045. * valid any more.
  1046. * uverbs_device is still available until all clients close
  1047. * their files, then the uverbs device ref count will be zero
  1048. * and its resources will be freed.
  1049. * Note: At this point no more files can be opened since the
  1050. * cdev was deleted, however active clients can still issue
  1051. * commands and close their open files.
  1052. */
  1053. ib_uverbs_free_hw_resources(uverbs_dev, device);
  1054. wait_clients = 0;
  1055. }
  1056. if (refcount_dec_and_test(&uverbs_dev->refcount))
  1057. ib_uverbs_comp_dev(uverbs_dev);
  1058. if (wait_clients)
  1059. wait_for_completion(&uverbs_dev->comp);
  1060. put_device(&uverbs_dev->dev);
  1061. }
  1062. static int __init ib_uverbs_init(void)
  1063. {
  1064. int ret;
  1065. ret = register_chrdev_region(IB_UVERBS_BASE_DEV,
  1066. IB_UVERBS_NUM_FIXED_MINOR,
  1067. "infiniband_verbs");
  1068. if (ret) {
  1069. pr_err("user_verbs: couldn't register device number\n");
  1070. goto out;
  1071. }
  1072. ret = alloc_chrdev_region(&dynamic_uverbs_dev, 0,
  1073. IB_UVERBS_NUM_DYNAMIC_MINOR,
  1074. "infiniband_verbs");
  1075. if (ret) {
  1076. pr_err("couldn't register dynamic device number\n");
  1077. goto out_alloc;
  1078. }
  1079. ret = class_register(&uverbs_class);
  1080. if (ret) {
  1081. pr_err("user_verbs: couldn't create class infiniband_verbs\n");
  1082. goto out_chrdev;
  1083. }
  1084. ret = class_create_file(&uverbs_class, &class_attr_abi_version.attr);
  1085. if (ret) {
  1086. pr_err("user_verbs: couldn't create abi_version attribute\n");
  1087. goto out_class;
  1088. }
  1089. ret = ib_register_client(&uverbs_client);
  1090. if (ret) {
  1091. pr_err("user_verbs: couldn't register client\n");
  1092. goto out_class;
  1093. }
  1094. return 0;
  1095. out_class:
  1096. class_unregister(&uverbs_class);
  1097. out_chrdev:
  1098. unregister_chrdev_region(dynamic_uverbs_dev,
  1099. IB_UVERBS_NUM_DYNAMIC_MINOR);
  1100. out_alloc:
  1101. unregister_chrdev_region(IB_UVERBS_BASE_DEV,
  1102. IB_UVERBS_NUM_FIXED_MINOR);
  1103. out:
  1104. return ret;
  1105. }
  1106. static void __exit ib_uverbs_cleanup(void)
  1107. {
  1108. ib_unregister_client(&uverbs_client);
  1109. class_unregister(&uverbs_class);
  1110. unregister_chrdev_region(IB_UVERBS_BASE_DEV,
  1111. IB_UVERBS_NUM_FIXED_MINOR);
  1112. unregister_chrdev_region(dynamic_uverbs_dev,
  1113. IB_UVERBS_NUM_DYNAMIC_MINOR);
  1114. ib_cleanup_ucaps();
  1115. mmu_notifier_synchronize();
  1116. }
  1117. module_init(ib_uverbs_init);
  1118. module_exit(ib_uverbs_cleanup);