uverbs_main.c 35 KB

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