uverbs_main.c 36 KB

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