uverbs_main.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  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. file->ev_queue.is_closed = 1;
  359. spin_unlock_irq(&file->ev_queue.lock);
  360. uverbs_close_fd(filp);
  361. return 0;
  362. }
  363. const struct file_operations uverbs_event_fops = {
  364. .owner = THIS_MODULE,
  365. .read = ib_uverbs_comp_event_read,
  366. .poll = ib_uverbs_comp_event_poll,
  367. .release = ib_uverbs_comp_event_close,
  368. .fasync = ib_uverbs_comp_event_fasync,
  369. .llseek = no_llseek,
  370. };
  371. static const struct file_operations uverbs_async_event_fops = {
  372. .owner = THIS_MODULE,
  373. .read = ib_uverbs_async_event_read,
  374. .poll = ib_uverbs_async_event_poll,
  375. .release = ib_uverbs_async_event_close,
  376. .fasync = ib_uverbs_async_event_fasync,
  377. .llseek = no_llseek,
  378. };
  379. void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context)
  380. {
  381. struct ib_uverbs_event_queue *ev_queue = cq_context;
  382. struct ib_ucq_object *uobj;
  383. struct ib_uverbs_event *entry;
  384. unsigned long flags;
  385. if (!ev_queue)
  386. return;
  387. spin_lock_irqsave(&ev_queue->lock, flags);
  388. if (ev_queue->is_closed) {
  389. spin_unlock_irqrestore(&ev_queue->lock, flags);
  390. return;
  391. }
  392. entry = kmalloc(sizeof *entry, GFP_ATOMIC);
  393. if (!entry) {
  394. spin_unlock_irqrestore(&ev_queue->lock, flags);
  395. return;
  396. }
  397. uobj = container_of(cq->uobject, struct ib_ucq_object, uobject);
  398. entry->desc.comp.cq_handle = cq->uobject->user_handle;
  399. entry->counter = &uobj->comp_events_reported;
  400. list_add_tail(&entry->list, &ev_queue->event_list);
  401. list_add_tail(&entry->obj_list, &uobj->comp_list);
  402. spin_unlock_irqrestore(&ev_queue->lock, flags);
  403. wake_up_interruptible(&ev_queue->poll_wait);
  404. kill_fasync(&ev_queue->async_queue, SIGIO, POLL_IN);
  405. }
  406. static void ib_uverbs_async_handler(struct ib_uverbs_file *file,
  407. __u64 element, __u64 event,
  408. struct list_head *obj_list,
  409. u32 *counter)
  410. {
  411. struct ib_uverbs_event *entry;
  412. unsigned long flags;
  413. spin_lock_irqsave(&file->async_file->ev_queue.lock, flags);
  414. if (file->async_file->ev_queue.is_closed) {
  415. spin_unlock_irqrestore(&file->async_file->ev_queue.lock, flags);
  416. return;
  417. }
  418. entry = kmalloc(sizeof *entry, GFP_ATOMIC);
  419. if (!entry) {
  420. spin_unlock_irqrestore(&file->async_file->ev_queue.lock, flags);
  421. return;
  422. }
  423. entry->desc.async.element = element;
  424. entry->desc.async.event_type = event;
  425. entry->desc.async.reserved = 0;
  426. entry->counter = counter;
  427. list_add_tail(&entry->list, &file->async_file->ev_queue.event_list);
  428. if (obj_list)
  429. list_add_tail(&entry->obj_list, obj_list);
  430. spin_unlock_irqrestore(&file->async_file->ev_queue.lock, flags);
  431. wake_up_interruptible(&file->async_file->ev_queue.poll_wait);
  432. kill_fasync(&file->async_file->ev_queue.async_queue, SIGIO, POLL_IN);
  433. }
  434. void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr)
  435. {
  436. struct ib_ucq_object *uobj = container_of(event->element.cq->uobject,
  437. struct ib_ucq_object, uobject);
  438. ib_uverbs_async_handler(uobj->uverbs_file, uobj->uobject.user_handle,
  439. event->event, &uobj->async_list,
  440. &uobj->async_events_reported);
  441. }
  442. void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr)
  443. {
  444. struct ib_uevent_object *uobj;
  445. /* for XRC target qp's, check that qp is live */
  446. if (!event->element.qp->uobject)
  447. return;
  448. uobj = container_of(event->element.qp->uobject,
  449. struct ib_uevent_object, uobject);
  450. ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
  451. event->event, &uobj->event_list,
  452. &uobj->events_reported);
  453. }
  454. void ib_uverbs_wq_event_handler(struct ib_event *event, void *context_ptr)
  455. {
  456. struct ib_uevent_object *uobj = container_of(event->element.wq->uobject,
  457. struct ib_uevent_object, uobject);
  458. ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
  459. event->event, &uobj->event_list,
  460. &uobj->events_reported);
  461. }
  462. void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr)
  463. {
  464. struct ib_uevent_object *uobj;
  465. uobj = container_of(event->element.srq->uobject,
  466. struct ib_uevent_object, uobject);
  467. ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
  468. event->event, &uobj->event_list,
  469. &uobj->events_reported);
  470. }
  471. void ib_uverbs_event_handler(struct ib_event_handler *handler,
  472. struct ib_event *event)
  473. {
  474. struct ib_uverbs_file *file =
  475. container_of(handler, struct ib_uverbs_file, event_handler);
  476. ib_uverbs_async_handler(file, event->element.port_num, event->event,
  477. NULL, NULL);
  478. }
  479. void ib_uverbs_free_async_event_file(struct ib_uverbs_file *file)
  480. {
  481. kref_put(&file->async_file->ref, ib_uverbs_release_async_event_file);
  482. file->async_file = NULL;
  483. }
  484. void ib_uverbs_init_event_queue(struct ib_uverbs_event_queue *ev_queue)
  485. {
  486. spin_lock_init(&ev_queue->lock);
  487. INIT_LIST_HEAD(&ev_queue->event_list);
  488. init_waitqueue_head(&ev_queue->poll_wait);
  489. ev_queue->is_closed = 0;
  490. ev_queue->async_queue = NULL;
  491. }
  492. struct file *ib_uverbs_alloc_async_event_file(struct ib_uverbs_file *uverbs_file,
  493. struct ib_device *ib_dev)
  494. {
  495. struct ib_uverbs_async_event_file *ev_file;
  496. struct file *filp;
  497. ev_file = kzalloc(sizeof(*ev_file), GFP_KERNEL);
  498. if (!ev_file)
  499. return ERR_PTR(-ENOMEM);
  500. ib_uverbs_init_event_queue(&ev_file->ev_queue);
  501. ev_file->uverbs_file = uverbs_file;
  502. kref_get(&ev_file->uverbs_file->ref);
  503. kref_init(&ev_file->ref);
  504. filp = anon_inode_getfile("[infinibandevent]", &uverbs_async_event_fops,
  505. ev_file, O_RDONLY);
  506. if (IS_ERR(filp))
  507. goto err_put_refs;
  508. mutex_lock(&uverbs_file->device->lists_mutex);
  509. list_add_tail(&ev_file->list,
  510. &uverbs_file->device->uverbs_events_file_list);
  511. mutex_unlock(&uverbs_file->device->lists_mutex);
  512. WARN_ON(uverbs_file->async_file);
  513. uverbs_file->async_file = ev_file;
  514. kref_get(&uverbs_file->async_file->ref);
  515. INIT_IB_EVENT_HANDLER(&uverbs_file->event_handler,
  516. ib_dev,
  517. ib_uverbs_event_handler);
  518. ib_register_event_handler(&uverbs_file->event_handler);
  519. /* At that point async file stuff was fully set */
  520. return filp;
  521. err_put_refs:
  522. kref_put(&ev_file->uverbs_file->ref, ib_uverbs_release_file);
  523. kref_put(&ev_file->ref, ib_uverbs_release_async_event_file);
  524. return filp;
  525. }
  526. static int verify_command_mask(struct ib_device *ib_dev, __u32 command)
  527. {
  528. u64 mask;
  529. if (command <= IB_USER_VERBS_CMD_OPEN_QP)
  530. mask = ib_dev->uverbs_cmd_mask;
  531. else
  532. mask = ib_dev->uverbs_ex_cmd_mask;
  533. if (mask & ((u64)1 << command))
  534. return 0;
  535. return -1;
  536. }
  537. static bool verify_command_idx(u32 command, bool extended)
  538. {
  539. if (extended)
  540. return command < ARRAY_SIZE(uverbs_ex_cmd_table);
  541. return command < ARRAY_SIZE(uverbs_cmd_table);
  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. bool extended_command;
  550. __u32 command;
  551. __u32 flags;
  552. int srcu_key;
  553. ssize_t ret;
  554. if (!ib_safe_file_access(filp)) {
  555. pr_err_once("uverbs_write: process %d (%s) changed security contexts after opening file descriptor, this is not allowed.\n",
  556. task_tgid_vnr(current), current->comm);
  557. return -EACCES;
  558. }
  559. if (count < sizeof hdr)
  560. return -EINVAL;
  561. if (copy_from_user(&hdr, buf, sizeof hdr))
  562. return -EFAULT;
  563. srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
  564. ib_dev = srcu_dereference(file->device->ib_dev,
  565. &file->device->disassociate_srcu);
  566. if (!ib_dev) {
  567. ret = -EIO;
  568. goto out;
  569. }
  570. if (hdr.command & ~(__u32)(IB_USER_VERBS_CMD_FLAGS_MASK |
  571. IB_USER_VERBS_CMD_COMMAND_MASK)) {
  572. ret = -EINVAL;
  573. goto out;
  574. }
  575. command = hdr.command & IB_USER_VERBS_CMD_COMMAND_MASK;
  576. flags = (hdr.command &
  577. IB_USER_VERBS_CMD_FLAGS_MASK) >> IB_USER_VERBS_CMD_FLAGS_SHIFT;
  578. extended_command = flags & IB_USER_VERBS_CMD_FLAG_EXTENDED;
  579. if (!verify_command_idx(command, extended_command)) {
  580. ret = -EINVAL;
  581. goto out;
  582. }
  583. if (verify_command_mask(ib_dev, command)) {
  584. ret = -EOPNOTSUPP;
  585. goto out;
  586. }
  587. if (!file->ucontext &&
  588. command != IB_USER_VERBS_CMD_GET_CONTEXT) {
  589. ret = -EINVAL;
  590. goto out;
  591. }
  592. if (!flags) {
  593. if (!uverbs_cmd_table[command]) {
  594. ret = -EINVAL;
  595. goto out;
  596. }
  597. if (hdr.in_words * 4 != count) {
  598. ret = -EINVAL;
  599. goto out;
  600. }
  601. ret = uverbs_cmd_table[command](file, ib_dev,
  602. buf + sizeof(hdr),
  603. hdr.in_words * 4,
  604. hdr.out_words * 4);
  605. } else if (flags == IB_USER_VERBS_CMD_FLAG_EXTENDED) {
  606. struct ib_uverbs_ex_cmd_hdr ex_hdr;
  607. struct ib_udata ucore;
  608. struct ib_udata uhw;
  609. size_t written_count = count;
  610. if (!uverbs_ex_cmd_table[command]) {
  611. ret = -ENOSYS;
  612. goto out;
  613. }
  614. if (!file->ucontext) {
  615. ret = -EINVAL;
  616. goto out;
  617. }
  618. if (count < (sizeof(hdr) + sizeof(ex_hdr))) {
  619. ret = -EINVAL;
  620. goto out;
  621. }
  622. if (copy_from_user(&ex_hdr, buf + sizeof(hdr), sizeof(ex_hdr))) {
  623. ret = -EFAULT;
  624. goto out;
  625. }
  626. count -= sizeof(hdr) + sizeof(ex_hdr);
  627. buf += sizeof(hdr) + sizeof(ex_hdr);
  628. if ((hdr.in_words + ex_hdr.provider_in_words) * 8 != count) {
  629. ret = -EINVAL;
  630. goto out;
  631. }
  632. if (ex_hdr.cmd_hdr_reserved) {
  633. ret = -EINVAL;
  634. goto out;
  635. }
  636. if (ex_hdr.response) {
  637. if (!hdr.out_words && !ex_hdr.provider_out_words) {
  638. ret = -EINVAL;
  639. goto out;
  640. }
  641. if (!access_ok(VERIFY_WRITE,
  642. (void __user *) (unsigned long) ex_hdr.response,
  643. (hdr.out_words + ex_hdr.provider_out_words) * 8)) {
  644. ret = -EFAULT;
  645. goto out;
  646. }
  647. } else {
  648. if (hdr.out_words || ex_hdr.provider_out_words) {
  649. ret = -EINVAL;
  650. goto out;
  651. }
  652. }
  653. INIT_UDATA_BUF_OR_NULL(&ucore, buf, (unsigned long) ex_hdr.response,
  654. hdr.in_words * 8, hdr.out_words * 8);
  655. INIT_UDATA_BUF_OR_NULL(&uhw,
  656. buf + ucore.inlen,
  657. (unsigned long) ex_hdr.response + ucore.outlen,
  658. ex_hdr.provider_in_words * 8,
  659. ex_hdr.provider_out_words * 8);
  660. ret = uverbs_ex_cmd_table[command](file,
  661. ib_dev,
  662. &ucore,
  663. &uhw);
  664. if (!ret)
  665. ret = written_count;
  666. } else {
  667. ret = -ENOSYS;
  668. }
  669. out:
  670. srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
  671. return ret;
  672. }
  673. static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma)
  674. {
  675. struct ib_uverbs_file *file = filp->private_data;
  676. struct ib_device *ib_dev;
  677. int ret = 0;
  678. int srcu_key;
  679. srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
  680. ib_dev = srcu_dereference(file->device->ib_dev,
  681. &file->device->disassociate_srcu);
  682. if (!ib_dev) {
  683. ret = -EIO;
  684. goto out;
  685. }
  686. if (!file->ucontext)
  687. ret = -ENODEV;
  688. else
  689. ret = ib_dev->mmap(file->ucontext, vma);
  690. out:
  691. srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
  692. return ret;
  693. }
  694. /*
  695. * ib_uverbs_open() does not need the BKL:
  696. *
  697. * - the ib_uverbs_device structures are properly reference counted and
  698. * everything else is purely local to the file being created, so
  699. * races against other open calls are not a problem;
  700. * - there is no ioctl method to race against;
  701. * - the open method will either immediately run -ENXIO, or all
  702. * required initialization will be done.
  703. */
  704. static int ib_uverbs_open(struct inode *inode, struct file *filp)
  705. {
  706. struct ib_uverbs_device *dev;
  707. struct ib_uverbs_file *file;
  708. struct ib_device *ib_dev;
  709. int ret;
  710. int module_dependent;
  711. int srcu_key;
  712. dev = container_of(inode->i_cdev, struct ib_uverbs_device, cdev);
  713. if (!atomic_inc_not_zero(&dev->refcount))
  714. return -ENXIO;
  715. srcu_key = srcu_read_lock(&dev->disassociate_srcu);
  716. mutex_lock(&dev->lists_mutex);
  717. ib_dev = srcu_dereference(dev->ib_dev,
  718. &dev->disassociate_srcu);
  719. if (!ib_dev) {
  720. ret = -EIO;
  721. goto err;
  722. }
  723. /* In case IB device supports disassociate ucontext, there is no hard
  724. * dependency between uverbs device and its low level device.
  725. */
  726. module_dependent = !(ib_dev->disassociate_ucontext);
  727. if (module_dependent) {
  728. if (!try_module_get(ib_dev->owner)) {
  729. ret = -ENODEV;
  730. goto err;
  731. }
  732. }
  733. file = kzalloc(sizeof(*file), GFP_KERNEL);
  734. if (!file) {
  735. ret = -ENOMEM;
  736. if (module_dependent)
  737. goto err_module;
  738. goto err;
  739. }
  740. file->device = dev;
  741. spin_lock_init(&file->idr_lock);
  742. idr_init(&file->idr);
  743. file->ucontext = NULL;
  744. file->async_file = NULL;
  745. kref_init(&file->ref);
  746. mutex_init(&file->mutex);
  747. mutex_init(&file->cleanup_mutex);
  748. filp->private_data = file;
  749. kobject_get(&dev->kobj);
  750. list_add_tail(&file->list, &dev->uverbs_file_list);
  751. mutex_unlock(&dev->lists_mutex);
  752. srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
  753. return nonseekable_open(inode, filp);
  754. err_module:
  755. module_put(ib_dev->owner);
  756. err:
  757. mutex_unlock(&dev->lists_mutex);
  758. srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
  759. if (atomic_dec_and_test(&dev->refcount))
  760. ib_uverbs_comp_dev(dev);
  761. return ret;
  762. }
  763. static int ib_uverbs_close(struct inode *inode, struct file *filp)
  764. {
  765. struct ib_uverbs_file *file = filp->private_data;
  766. mutex_lock(&file->cleanup_mutex);
  767. if (file->ucontext) {
  768. ib_uverbs_cleanup_ucontext(file, file->ucontext, false);
  769. file->ucontext = NULL;
  770. }
  771. mutex_unlock(&file->cleanup_mutex);
  772. idr_destroy(&file->idr);
  773. mutex_lock(&file->device->lists_mutex);
  774. if (!file->is_closed) {
  775. list_del(&file->list);
  776. file->is_closed = 1;
  777. }
  778. mutex_unlock(&file->device->lists_mutex);
  779. if (file->async_file)
  780. kref_put(&file->async_file->ref,
  781. ib_uverbs_release_async_event_file);
  782. kref_put(&file->ref, ib_uverbs_release_file);
  783. return 0;
  784. }
  785. static const struct file_operations uverbs_fops = {
  786. .owner = THIS_MODULE,
  787. .write = ib_uverbs_write,
  788. .open = ib_uverbs_open,
  789. .release = ib_uverbs_close,
  790. .llseek = no_llseek,
  791. #if IS_ENABLED(CONFIG_INFINIBAND_EXP_USER_ACCESS)
  792. .unlocked_ioctl = ib_uverbs_ioctl,
  793. #endif
  794. };
  795. static const struct file_operations uverbs_mmap_fops = {
  796. .owner = THIS_MODULE,
  797. .write = ib_uverbs_write,
  798. .mmap = ib_uverbs_mmap,
  799. .open = ib_uverbs_open,
  800. .release = ib_uverbs_close,
  801. .llseek = no_llseek,
  802. #if IS_ENABLED(CONFIG_INFINIBAND_EXP_USER_ACCESS)
  803. .unlocked_ioctl = ib_uverbs_ioctl,
  804. #endif
  805. };
  806. static struct ib_client uverbs_client = {
  807. .name = "uverbs",
  808. .add = ib_uverbs_add_one,
  809. .remove = ib_uverbs_remove_one
  810. };
  811. static ssize_t show_ibdev(struct device *device, struct device_attribute *attr,
  812. char *buf)
  813. {
  814. int ret = -ENODEV;
  815. int srcu_key;
  816. struct ib_uverbs_device *dev = dev_get_drvdata(device);
  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, "%s\n", ib_dev->name);
  824. srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
  825. return ret;
  826. }
  827. static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
  828. static ssize_t show_dev_abi_version(struct device *device,
  829. struct device_attribute *attr, char *buf)
  830. {
  831. struct ib_uverbs_device *dev = dev_get_drvdata(device);
  832. int ret = -ENODEV;
  833. int srcu_key;
  834. struct ib_device *ib_dev;
  835. if (!dev)
  836. return -ENODEV;
  837. srcu_key = srcu_read_lock(&dev->disassociate_srcu);
  838. ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
  839. if (ib_dev)
  840. ret = sprintf(buf, "%d\n", ib_dev->uverbs_abi_ver);
  841. srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
  842. return ret;
  843. }
  844. static DEVICE_ATTR(abi_version, S_IRUGO, show_dev_abi_version, NULL);
  845. static CLASS_ATTR_STRING(abi_version, S_IRUGO,
  846. __stringify(IB_USER_VERBS_ABI_VERSION));
  847. static dev_t overflow_maj;
  848. static DECLARE_BITMAP(overflow_map, IB_UVERBS_MAX_DEVICES);
  849. /*
  850. * If we have more than IB_UVERBS_MAX_DEVICES, dynamically overflow by
  851. * requesting a new major number and doubling the number of max devices we
  852. * support. It's stupid, but simple.
  853. */
  854. static int find_overflow_devnum(void)
  855. {
  856. int ret;
  857. if (!overflow_maj) {
  858. ret = alloc_chrdev_region(&overflow_maj, 0, IB_UVERBS_MAX_DEVICES,
  859. "infiniband_verbs");
  860. if (ret) {
  861. pr_err("user_verbs: couldn't register dynamic device number\n");
  862. return ret;
  863. }
  864. }
  865. ret = find_first_zero_bit(overflow_map, IB_UVERBS_MAX_DEVICES);
  866. if (ret >= IB_UVERBS_MAX_DEVICES)
  867. return -1;
  868. return ret;
  869. }
  870. static void ib_uverbs_add_one(struct ib_device *device)
  871. {
  872. int devnum;
  873. dev_t base;
  874. struct ib_uverbs_device *uverbs_dev;
  875. int ret;
  876. if (!device->alloc_ucontext)
  877. return;
  878. uverbs_dev = kzalloc(sizeof *uverbs_dev, GFP_KERNEL);
  879. if (!uverbs_dev)
  880. return;
  881. ret = init_srcu_struct(&uverbs_dev->disassociate_srcu);
  882. if (ret) {
  883. kfree(uverbs_dev);
  884. return;
  885. }
  886. atomic_set(&uverbs_dev->refcount, 1);
  887. init_completion(&uverbs_dev->comp);
  888. uverbs_dev->xrcd_tree = RB_ROOT;
  889. mutex_init(&uverbs_dev->xrcd_tree_mutex);
  890. kobject_init(&uverbs_dev->kobj, &ib_uverbs_dev_ktype);
  891. mutex_init(&uverbs_dev->lists_mutex);
  892. INIT_LIST_HEAD(&uverbs_dev->uverbs_file_list);
  893. INIT_LIST_HEAD(&uverbs_dev->uverbs_events_file_list);
  894. spin_lock(&map_lock);
  895. devnum = find_first_zero_bit(dev_map, IB_UVERBS_MAX_DEVICES);
  896. if (devnum >= IB_UVERBS_MAX_DEVICES) {
  897. spin_unlock(&map_lock);
  898. devnum = find_overflow_devnum();
  899. if (devnum < 0)
  900. goto err;
  901. spin_lock(&map_lock);
  902. uverbs_dev->devnum = devnum + IB_UVERBS_MAX_DEVICES;
  903. base = devnum + overflow_maj;
  904. set_bit(devnum, overflow_map);
  905. } else {
  906. uverbs_dev->devnum = devnum;
  907. base = devnum + IB_UVERBS_BASE_DEV;
  908. set_bit(devnum, dev_map);
  909. }
  910. spin_unlock(&map_lock);
  911. rcu_assign_pointer(uverbs_dev->ib_dev, device);
  912. uverbs_dev->num_comp_vectors = device->num_comp_vectors;
  913. cdev_init(&uverbs_dev->cdev, NULL);
  914. uverbs_dev->cdev.owner = THIS_MODULE;
  915. uverbs_dev->cdev.ops = device->mmap ? &uverbs_mmap_fops : &uverbs_fops;
  916. cdev_set_parent(&uverbs_dev->cdev, &uverbs_dev->kobj);
  917. kobject_set_name(&uverbs_dev->cdev.kobj, "uverbs%d", uverbs_dev->devnum);
  918. if (cdev_add(&uverbs_dev->cdev, base, 1))
  919. goto err_cdev;
  920. uverbs_dev->dev = device_create(uverbs_class, device->dev.parent,
  921. uverbs_dev->cdev.dev, uverbs_dev,
  922. "uverbs%d", uverbs_dev->devnum);
  923. if (IS_ERR(uverbs_dev->dev))
  924. goto err_cdev;
  925. if (device_create_file(uverbs_dev->dev, &dev_attr_ibdev))
  926. goto err_class;
  927. if (device_create_file(uverbs_dev->dev, &dev_attr_abi_version))
  928. goto err_class;
  929. if (!device->specs_root) {
  930. const struct uverbs_object_tree_def *default_root[] = {
  931. uverbs_default_get_objects()};
  932. uverbs_dev->specs_root = uverbs_alloc_spec_tree(1,
  933. default_root);
  934. if (IS_ERR(uverbs_dev->specs_root))
  935. goto err_class;
  936. device->specs_root = uverbs_dev->specs_root;
  937. }
  938. ib_set_client_data(device, &uverbs_client, uverbs_dev);
  939. return;
  940. err_class:
  941. device_destroy(uverbs_class, uverbs_dev->cdev.dev);
  942. err_cdev:
  943. cdev_del(&uverbs_dev->cdev);
  944. if (uverbs_dev->devnum < IB_UVERBS_MAX_DEVICES)
  945. clear_bit(devnum, dev_map);
  946. else
  947. clear_bit(devnum, overflow_map);
  948. err:
  949. if (atomic_dec_and_test(&uverbs_dev->refcount))
  950. ib_uverbs_comp_dev(uverbs_dev);
  951. wait_for_completion(&uverbs_dev->comp);
  952. kobject_put(&uverbs_dev->kobj);
  953. return;
  954. }
  955. static void ib_uverbs_free_hw_resources(struct ib_uverbs_device *uverbs_dev,
  956. struct ib_device *ib_dev)
  957. {
  958. struct ib_uverbs_file *file;
  959. struct ib_uverbs_async_event_file *event_file;
  960. struct ib_event event;
  961. /* Pending running commands to terminate */
  962. synchronize_srcu(&uverbs_dev->disassociate_srcu);
  963. event.event = IB_EVENT_DEVICE_FATAL;
  964. event.element.port_num = 0;
  965. event.device = ib_dev;
  966. mutex_lock(&uverbs_dev->lists_mutex);
  967. while (!list_empty(&uverbs_dev->uverbs_file_list)) {
  968. struct ib_ucontext *ucontext;
  969. file = list_first_entry(&uverbs_dev->uverbs_file_list,
  970. struct ib_uverbs_file, list);
  971. file->is_closed = 1;
  972. list_del(&file->list);
  973. kref_get(&file->ref);
  974. mutex_unlock(&uverbs_dev->lists_mutex);
  975. mutex_lock(&file->cleanup_mutex);
  976. ucontext = file->ucontext;
  977. file->ucontext = NULL;
  978. mutex_unlock(&file->cleanup_mutex);
  979. /* At this point ib_uverbs_close cannot be running
  980. * ib_uverbs_cleanup_ucontext
  981. */
  982. if (ucontext) {
  983. /* We must release the mutex before going ahead and
  984. * calling disassociate_ucontext. disassociate_ucontext
  985. * might end up indirectly calling uverbs_close,
  986. * for example due to freeing the resources
  987. * (e.g mmput).
  988. */
  989. ib_uverbs_event_handler(&file->event_handler, &event);
  990. ib_dev->disassociate_ucontext(ucontext);
  991. mutex_lock(&file->cleanup_mutex);
  992. ib_uverbs_cleanup_ucontext(file, ucontext, true);
  993. mutex_unlock(&file->cleanup_mutex);
  994. }
  995. mutex_lock(&uverbs_dev->lists_mutex);
  996. kref_put(&file->ref, ib_uverbs_release_file);
  997. }
  998. while (!list_empty(&uverbs_dev->uverbs_events_file_list)) {
  999. event_file = list_first_entry(&uverbs_dev->
  1000. uverbs_events_file_list,
  1001. struct ib_uverbs_async_event_file,
  1002. list);
  1003. spin_lock_irq(&event_file->ev_queue.lock);
  1004. event_file->ev_queue.is_closed = 1;
  1005. spin_unlock_irq(&event_file->ev_queue.lock);
  1006. list_del(&event_file->list);
  1007. ib_unregister_event_handler(
  1008. &event_file->uverbs_file->event_handler);
  1009. event_file->uverbs_file->event_handler.device =
  1010. NULL;
  1011. wake_up_interruptible(&event_file->ev_queue.poll_wait);
  1012. kill_fasync(&event_file->ev_queue.async_queue, SIGIO, POLL_IN);
  1013. }
  1014. mutex_unlock(&uverbs_dev->lists_mutex);
  1015. }
  1016. static void ib_uverbs_remove_one(struct ib_device *device, void *client_data)
  1017. {
  1018. struct ib_uverbs_device *uverbs_dev = client_data;
  1019. int wait_clients = 1;
  1020. if (!uverbs_dev)
  1021. return;
  1022. dev_set_drvdata(uverbs_dev->dev, NULL);
  1023. device_destroy(uverbs_class, uverbs_dev->cdev.dev);
  1024. cdev_del(&uverbs_dev->cdev);
  1025. if (uverbs_dev->devnum < IB_UVERBS_MAX_DEVICES)
  1026. clear_bit(uverbs_dev->devnum, dev_map);
  1027. else
  1028. clear_bit(uverbs_dev->devnum - IB_UVERBS_MAX_DEVICES, overflow_map);
  1029. if (device->disassociate_ucontext) {
  1030. /* We disassociate HW resources and immediately return.
  1031. * Userspace will see a EIO errno for all future access.
  1032. * Upon returning, ib_device may be freed internally and is not
  1033. * valid any more.
  1034. * uverbs_device is still available until all clients close
  1035. * their files, then the uverbs device ref count will be zero
  1036. * and its resources will be freed.
  1037. * Note: At this point no more files can be opened since the
  1038. * cdev was deleted, however active clients can still issue
  1039. * commands and close their open files.
  1040. */
  1041. rcu_assign_pointer(uverbs_dev->ib_dev, NULL);
  1042. ib_uverbs_free_hw_resources(uverbs_dev, device);
  1043. wait_clients = 0;
  1044. }
  1045. if (atomic_dec_and_test(&uverbs_dev->refcount))
  1046. ib_uverbs_comp_dev(uverbs_dev);
  1047. if (wait_clients)
  1048. wait_for_completion(&uverbs_dev->comp);
  1049. if (uverbs_dev->specs_root) {
  1050. uverbs_free_spec_tree(uverbs_dev->specs_root);
  1051. device->specs_root = NULL;
  1052. }
  1053. kobject_put(&uverbs_dev->kobj);
  1054. }
  1055. static char *uverbs_devnode(struct device *dev, umode_t *mode)
  1056. {
  1057. if (mode)
  1058. *mode = 0666;
  1059. return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
  1060. }
  1061. static int __init ib_uverbs_init(void)
  1062. {
  1063. int ret;
  1064. ret = register_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES,
  1065. "infiniband_verbs");
  1066. if (ret) {
  1067. pr_err("user_verbs: couldn't register device number\n");
  1068. goto out;
  1069. }
  1070. uverbs_class = class_create(THIS_MODULE, "infiniband_verbs");
  1071. if (IS_ERR(uverbs_class)) {
  1072. ret = PTR_ERR(uverbs_class);
  1073. pr_err("user_verbs: couldn't create class infiniband_verbs\n");
  1074. goto out_chrdev;
  1075. }
  1076. uverbs_class->devnode = uverbs_devnode;
  1077. ret = class_create_file(uverbs_class, &class_attr_abi_version.attr);
  1078. if (ret) {
  1079. pr_err("user_verbs: couldn't create abi_version attribute\n");
  1080. goto out_class;
  1081. }
  1082. ret = ib_register_client(&uverbs_client);
  1083. if (ret) {
  1084. pr_err("user_verbs: couldn't register client\n");
  1085. goto out_class;
  1086. }
  1087. return 0;
  1088. out_class:
  1089. class_destroy(uverbs_class);
  1090. out_chrdev:
  1091. unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES);
  1092. out:
  1093. return ret;
  1094. }
  1095. static void __exit ib_uverbs_cleanup(void)
  1096. {
  1097. ib_unregister_client(&uverbs_client);
  1098. class_destroy(uverbs_class);
  1099. unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES);
  1100. if (overflow_maj)
  1101. unregister_chrdev_region(overflow_maj, IB_UVERBS_MAX_DEVICES);
  1102. }
  1103. module_init(ib_uverbs_init);
  1104. module_exit(ib_uverbs_cleanup);