uverbs_main.c 36 KB

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