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.
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:
14 * Redistribution and use in source and binary forms, with or
15 * without modification, are permitted provided that the following
18 * - Redistributions of source code must retain the above
19 * copyright notice, this list of conditions and the following
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.
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
37 #include <linux/module.h>
38 #include <linux/init.h>
39 #include <linux/device.h>
40 #include <linux/err.h>
42 #include <linux/poll.h>
43 #include <linux/sched.h>
44 #include <linux/file.h>
45 #include <linux/cdev.h>
46 #include <linux/anon_inodes.h>
47 #include <linux/slab.h>
49 #include <linux/uaccess.h>
54 #include "core_priv.h"
56 MODULE_AUTHOR("Roland Dreier");
57 MODULE_DESCRIPTION("InfiniBand userspace verbs access");
58 MODULE_LICENSE("Dual BSD/GPL");
61 IB_UVERBS_MAJOR = 231,
62 IB_UVERBS_BASE_MINOR = 192,
63 IB_UVERBS_MAX_DEVICES = 32
66 #define IB_UVERBS_BASE_DEV MKDEV(IB_UVERBS_MAJOR, IB_UVERBS_BASE_MINOR)
68 static struct class *uverbs_class;
70 DEFINE_SPINLOCK(ib_uverbs_idr_lock);
71 DEFINE_IDR(ib_uverbs_pd_idr);
72 DEFINE_IDR(ib_uverbs_mr_idr);
73 DEFINE_IDR(ib_uverbs_mw_idr);
74 DEFINE_IDR(ib_uverbs_ah_idr);
75 DEFINE_IDR(ib_uverbs_cq_idr);
76 DEFINE_IDR(ib_uverbs_qp_idr);
77 DEFINE_IDR(ib_uverbs_srq_idr);
78 DEFINE_IDR(ib_uverbs_xrcd_idr);
79 DEFINE_IDR(ib_uverbs_rule_idr);
80 DEFINE_IDR(ib_uverbs_wq_idr);
81 DEFINE_IDR(ib_uverbs_rwq_ind_tbl_idr);
83 static DEFINE_SPINLOCK(map_lock);
84 static DECLARE_BITMAP(dev_map, IB_UVERBS_MAX_DEVICES);
86 static ssize_t (*uverbs_cmd_table[])(struct ib_uverbs_file *file,
87 struct ib_device *ib_dev,
88 const char __user *buf, int in_len,
90 [IB_USER_VERBS_CMD_GET_CONTEXT] = ib_uverbs_get_context,
91 [IB_USER_VERBS_CMD_QUERY_DEVICE] = ib_uverbs_query_device,
92 [IB_USER_VERBS_CMD_QUERY_PORT] = ib_uverbs_query_port,
93 [IB_USER_VERBS_CMD_ALLOC_PD] = ib_uverbs_alloc_pd,
94 [IB_USER_VERBS_CMD_DEALLOC_PD] = ib_uverbs_dealloc_pd,
95 [IB_USER_VERBS_CMD_REG_MR] = ib_uverbs_reg_mr,
96 [IB_USER_VERBS_CMD_REREG_MR] = ib_uverbs_rereg_mr,
97 [IB_USER_VERBS_CMD_DEREG_MR] = ib_uverbs_dereg_mr,
98 [IB_USER_VERBS_CMD_ALLOC_MW] = ib_uverbs_alloc_mw,
99 [IB_USER_VERBS_CMD_DEALLOC_MW] = ib_uverbs_dealloc_mw,
100 [IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL] = ib_uverbs_create_comp_channel,
101 [IB_USER_VERBS_CMD_CREATE_CQ] = ib_uverbs_create_cq,
102 [IB_USER_VERBS_CMD_RESIZE_CQ] = ib_uverbs_resize_cq,
103 [IB_USER_VERBS_CMD_POLL_CQ] = ib_uverbs_poll_cq,
104 [IB_USER_VERBS_CMD_REQ_NOTIFY_CQ] = ib_uverbs_req_notify_cq,
105 [IB_USER_VERBS_CMD_DESTROY_CQ] = ib_uverbs_destroy_cq,
106 [IB_USER_VERBS_CMD_CREATE_QP] = ib_uverbs_create_qp,
107 [IB_USER_VERBS_CMD_QUERY_QP] = ib_uverbs_query_qp,
108 [IB_USER_VERBS_CMD_MODIFY_QP] = ib_uverbs_modify_qp,
109 [IB_USER_VERBS_CMD_DESTROY_QP] = ib_uverbs_destroy_qp,
110 [IB_USER_VERBS_CMD_POST_SEND] = ib_uverbs_post_send,
111 [IB_USER_VERBS_CMD_POST_RECV] = ib_uverbs_post_recv,
112 [IB_USER_VERBS_CMD_POST_SRQ_RECV] = ib_uverbs_post_srq_recv,
113 [IB_USER_VERBS_CMD_CREATE_AH] = ib_uverbs_create_ah,
114 [IB_USER_VERBS_CMD_DESTROY_AH] = ib_uverbs_destroy_ah,
115 [IB_USER_VERBS_CMD_ATTACH_MCAST] = ib_uverbs_attach_mcast,
116 [IB_USER_VERBS_CMD_DETACH_MCAST] = ib_uverbs_detach_mcast,
117 [IB_USER_VERBS_CMD_CREATE_SRQ] = ib_uverbs_create_srq,
118 [IB_USER_VERBS_CMD_MODIFY_SRQ] = ib_uverbs_modify_srq,
119 [IB_USER_VERBS_CMD_QUERY_SRQ] = ib_uverbs_query_srq,
120 [IB_USER_VERBS_CMD_DESTROY_SRQ] = ib_uverbs_destroy_srq,
121 [IB_USER_VERBS_CMD_OPEN_XRCD] = ib_uverbs_open_xrcd,
122 [IB_USER_VERBS_CMD_CLOSE_XRCD] = ib_uverbs_close_xrcd,
123 [IB_USER_VERBS_CMD_CREATE_XSRQ] = ib_uverbs_create_xsrq,
124 [IB_USER_VERBS_CMD_OPEN_QP] = ib_uverbs_open_qp,
127 static int (*uverbs_ex_cmd_table[])(struct ib_uverbs_file *file,
128 struct ib_device *ib_dev,
129 struct ib_udata *ucore,
130 struct ib_udata *uhw) = {
131 [IB_USER_VERBS_EX_CMD_CREATE_FLOW] = ib_uverbs_ex_create_flow,
132 [IB_USER_VERBS_EX_CMD_DESTROY_FLOW] = ib_uverbs_ex_destroy_flow,
133 [IB_USER_VERBS_EX_CMD_QUERY_DEVICE] = ib_uverbs_ex_query_device,
134 [IB_USER_VERBS_EX_CMD_CREATE_CQ] = ib_uverbs_ex_create_cq,
135 [IB_USER_VERBS_EX_CMD_CREATE_QP] = ib_uverbs_ex_create_qp,
136 [IB_USER_VERBS_EX_CMD_CREATE_WQ] = ib_uverbs_ex_create_wq,
137 [IB_USER_VERBS_EX_CMD_MODIFY_WQ] = ib_uverbs_ex_modify_wq,
138 [IB_USER_VERBS_EX_CMD_DESTROY_WQ] = ib_uverbs_ex_destroy_wq,
139 [IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL] = ib_uverbs_ex_create_rwq_ind_table,
140 [IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL] = ib_uverbs_ex_destroy_rwq_ind_table,
141 [IB_USER_VERBS_EX_CMD_MODIFY_QP] = ib_uverbs_ex_modify_qp,
144 static void ib_uverbs_add_one(struct ib_device *device);
145 static void ib_uverbs_remove_one(struct ib_device *device, void *client_data);
147 int uverbs_dealloc_mw(struct ib_mw *mw)
149 struct ib_pd *pd = mw->pd;
152 ret = mw->device->dealloc_mw(mw);
154 atomic_dec(&pd->usecnt);
158 static void ib_uverbs_release_dev(struct kobject *kobj)
160 struct ib_uverbs_device *dev =
161 container_of(kobj, struct ib_uverbs_device, kobj);
163 cleanup_srcu_struct(&dev->disassociate_srcu);
167 static struct kobj_type ib_uverbs_dev_ktype = {
168 .release = ib_uverbs_release_dev,
171 static void ib_uverbs_release_event_file(struct kref *ref)
173 struct ib_uverbs_event_file *file =
174 container_of(ref, struct ib_uverbs_event_file, ref);
179 void ib_uverbs_release_ucq(struct ib_uverbs_file *file,
180 struct ib_uverbs_event_file *ev_file,
181 struct ib_ucq_object *uobj)
183 struct ib_uverbs_event *evt, *tmp;
186 spin_lock_irq(&ev_file->lock);
187 list_for_each_entry_safe(evt, tmp, &uobj->comp_list, obj_list) {
188 list_del(&evt->list);
191 spin_unlock_irq(&ev_file->lock);
193 kref_put(&ev_file->ref, ib_uverbs_release_event_file);
196 spin_lock_irq(&file->async_file->lock);
197 list_for_each_entry_safe(evt, tmp, &uobj->async_list, obj_list) {
198 list_del(&evt->list);
201 spin_unlock_irq(&file->async_file->lock);
204 void ib_uverbs_release_uevent(struct ib_uverbs_file *file,
205 struct ib_uevent_object *uobj)
207 struct ib_uverbs_event *evt, *tmp;
209 spin_lock_irq(&file->async_file->lock);
210 list_for_each_entry_safe(evt, tmp, &uobj->event_list, obj_list) {
211 list_del(&evt->list);
214 spin_unlock_irq(&file->async_file->lock);
217 static void ib_uverbs_detach_umcast(struct ib_qp *qp,
218 struct ib_uqp_object *uobj)
220 struct ib_uverbs_mcast_entry *mcast, *tmp;
222 list_for_each_entry_safe(mcast, tmp, &uobj->mcast_list, list) {
223 ib_detach_mcast(qp, &mcast->gid, mcast->lid);
224 list_del(&mcast->list);
229 static int ib_uverbs_cleanup_ucontext(struct ib_uverbs_file *file,
230 struct ib_ucontext *context)
232 struct ib_uobject *uobj, *tmp;
234 context->closing = 1;
236 list_for_each_entry_safe(uobj, tmp, &context->ah_list, list) {
237 struct ib_ah *ah = uobj->object;
239 idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
241 ib_rdmacg_uncharge(&uobj->cg_obj, context->device,
242 RDMACG_RESOURCE_HCA_OBJECT);
246 /* Remove MWs before QPs, in order to support type 2A MWs. */
247 list_for_each_entry_safe(uobj, tmp, &context->mw_list, list) {
248 struct ib_mw *mw = uobj->object;
250 idr_remove_uobj(&ib_uverbs_mw_idr, uobj);
251 uverbs_dealloc_mw(mw);
252 ib_rdmacg_uncharge(&uobj->cg_obj, context->device,
253 RDMACG_RESOURCE_HCA_OBJECT);
257 list_for_each_entry_safe(uobj, tmp, &context->rule_list, list) {
258 struct ib_flow *flow_id = uobj->object;
260 idr_remove_uobj(&ib_uverbs_rule_idr, uobj);
261 ib_destroy_flow(flow_id);
262 ib_rdmacg_uncharge(&uobj->cg_obj, context->device,
263 RDMACG_RESOURCE_HCA_OBJECT);
267 list_for_each_entry_safe(uobj, tmp, &context->qp_list, list) {
268 struct ib_qp *qp = uobj->object;
269 struct ib_uqp_object *uqp =
270 container_of(uobj, struct ib_uqp_object, uevent.uobject);
272 idr_remove_uobj(&ib_uverbs_qp_idr, uobj);
273 if (qp == qp->real_qp)
274 ib_uverbs_detach_umcast(qp, uqp);
276 ib_rdmacg_uncharge(&uobj->cg_obj, context->device,
277 RDMACG_RESOURCE_HCA_OBJECT);
278 ib_uverbs_release_uevent(file, &uqp->uevent);
282 list_for_each_entry_safe(uobj, tmp, &context->rwq_ind_tbl_list, list) {
283 struct ib_rwq_ind_table *rwq_ind_tbl = uobj->object;
284 struct ib_wq **ind_tbl = rwq_ind_tbl->ind_tbl;
286 idr_remove_uobj(&ib_uverbs_rwq_ind_tbl_idr, uobj);
287 ib_destroy_rwq_ind_table(rwq_ind_tbl);
292 list_for_each_entry_safe(uobj, tmp, &context->wq_list, list) {
293 struct ib_wq *wq = uobj->object;
294 struct ib_uwq_object *uwq =
295 container_of(uobj, struct ib_uwq_object, uevent.uobject);
297 idr_remove_uobj(&ib_uverbs_wq_idr, uobj);
299 ib_uverbs_release_uevent(file, &uwq->uevent);
303 list_for_each_entry_safe(uobj, tmp, &context->srq_list, list) {
304 struct ib_srq *srq = uobj->object;
305 struct ib_uevent_object *uevent =
306 container_of(uobj, struct ib_uevent_object, uobject);
308 idr_remove_uobj(&ib_uverbs_srq_idr, uobj);
310 ib_rdmacg_uncharge(&uobj->cg_obj, context->device,
311 RDMACG_RESOURCE_HCA_OBJECT);
312 ib_uverbs_release_uevent(file, uevent);
316 list_for_each_entry_safe(uobj, tmp, &context->cq_list, list) {
317 struct ib_cq *cq = uobj->object;
318 struct ib_uverbs_event_file *ev_file = cq->cq_context;
319 struct ib_ucq_object *ucq =
320 container_of(uobj, struct ib_ucq_object, uobject);
322 idr_remove_uobj(&ib_uverbs_cq_idr, uobj);
324 ib_rdmacg_uncharge(&uobj->cg_obj, context->device,
325 RDMACG_RESOURCE_HCA_OBJECT);
326 ib_uverbs_release_ucq(file, ev_file, ucq);
330 list_for_each_entry_safe(uobj, tmp, &context->mr_list, list) {
331 struct ib_mr *mr = uobj->object;
333 idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
335 ib_rdmacg_uncharge(&uobj->cg_obj, context->device,
336 RDMACG_RESOURCE_HCA_OBJECT);
340 mutex_lock(&file->device->xrcd_tree_mutex);
341 list_for_each_entry_safe(uobj, tmp, &context->xrcd_list, list) {
342 struct ib_xrcd *xrcd = uobj->object;
343 struct ib_uxrcd_object *uxrcd =
344 container_of(uobj, struct ib_uxrcd_object, uobject);
346 idr_remove_uobj(&ib_uverbs_xrcd_idr, uobj);
347 ib_uverbs_dealloc_xrcd(file->device, xrcd);
350 mutex_unlock(&file->device->xrcd_tree_mutex);
352 list_for_each_entry_safe(uobj, tmp, &context->pd_list, list) {
353 struct ib_pd *pd = uobj->object;
355 idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
357 ib_rdmacg_uncharge(&uobj->cg_obj, context->device,
358 RDMACG_RESOURCE_HCA_OBJECT);
362 put_pid(context->tgid);
364 ib_rdmacg_uncharge(&context->cg_obj, context->device,
365 RDMACG_RESOURCE_HCA_HANDLE);
367 return context->device->dealloc_ucontext(context);
370 static void ib_uverbs_comp_dev(struct ib_uverbs_device *dev)
372 complete(&dev->comp);
375 static void ib_uverbs_release_file(struct kref *ref)
377 struct ib_uverbs_file *file =
378 container_of(ref, struct ib_uverbs_file, ref);
379 struct ib_device *ib_dev;
382 srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
383 ib_dev = srcu_dereference(file->device->ib_dev,
384 &file->device->disassociate_srcu);
385 if (ib_dev && !ib_dev->disassociate_ucontext)
386 module_put(ib_dev->owner);
387 srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
389 if (atomic_dec_and_test(&file->device->refcount))
390 ib_uverbs_comp_dev(file->device);
395 static ssize_t ib_uverbs_event_read(struct file *filp, char __user *buf,
396 size_t count, loff_t *pos)
398 struct ib_uverbs_event_file *file = filp->private_data;
399 struct ib_uverbs_event *event;
403 spin_lock_irq(&file->lock);
405 while (list_empty(&file->event_list)) {
406 spin_unlock_irq(&file->lock);
408 if (filp->f_flags & O_NONBLOCK)
411 if (wait_event_interruptible(file->poll_wait,
412 (!list_empty(&file->event_list) ||
413 /* The barriers built into wait_event_interruptible()
414 * and wake_up() guarentee this will see the null set
417 !file->uverbs_file->device->ib_dev)))
420 /* If device was disassociated and no event exists set an error */
421 if (list_empty(&file->event_list) &&
422 !file->uverbs_file->device->ib_dev)
425 spin_lock_irq(&file->lock);
428 event = list_entry(file->event_list.next, struct ib_uverbs_event, list);
431 eventsz = sizeof (struct ib_uverbs_async_event_desc);
433 eventsz = sizeof (struct ib_uverbs_comp_event_desc);
435 if (eventsz > count) {
439 list_del(file->event_list.next);
440 if (event->counter) {
442 list_del(&event->obj_list);
446 spin_unlock_irq(&file->lock);
449 if (copy_to_user(buf, event, eventsz))
460 static unsigned int ib_uverbs_event_poll(struct file *filp,
461 struct poll_table_struct *wait)
463 unsigned int pollflags = 0;
464 struct ib_uverbs_event_file *file = filp->private_data;
466 poll_wait(filp, &file->poll_wait, wait);
468 spin_lock_irq(&file->lock);
469 if (!list_empty(&file->event_list))
470 pollflags = POLLIN | POLLRDNORM;
471 spin_unlock_irq(&file->lock);
476 static int ib_uverbs_event_fasync(int fd, struct file *filp, int on)
478 struct ib_uverbs_event_file *file = filp->private_data;
480 return fasync_helper(fd, filp, on, &file->async_queue);
483 static int ib_uverbs_event_close(struct inode *inode, struct file *filp)
485 struct ib_uverbs_event_file *file = filp->private_data;
486 struct ib_uverbs_event *entry, *tmp;
487 int closed_already = 0;
489 mutex_lock(&file->uverbs_file->device->lists_mutex);
490 spin_lock_irq(&file->lock);
491 closed_already = file->is_closed;
493 list_for_each_entry_safe(entry, tmp, &file->event_list, list) {
495 list_del(&entry->obj_list);
498 spin_unlock_irq(&file->lock);
499 if (!closed_already) {
500 list_del(&file->list);
502 ib_unregister_event_handler(&file->uverbs_file->
505 mutex_unlock(&file->uverbs_file->device->lists_mutex);
507 kref_put(&file->uverbs_file->ref, ib_uverbs_release_file);
508 kref_put(&file->ref, ib_uverbs_release_event_file);
513 static const struct file_operations uverbs_event_fops = {
514 .owner = THIS_MODULE,
515 .read = ib_uverbs_event_read,
516 .poll = ib_uverbs_event_poll,
517 .release = ib_uverbs_event_close,
518 .fasync = ib_uverbs_event_fasync,
522 void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context)
524 struct ib_uverbs_event_file *file = cq_context;
525 struct ib_ucq_object *uobj;
526 struct ib_uverbs_event *entry;
532 spin_lock_irqsave(&file->lock, flags);
533 if (file->is_closed) {
534 spin_unlock_irqrestore(&file->lock, flags);
538 entry = kmalloc(sizeof *entry, GFP_ATOMIC);
540 spin_unlock_irqrestore(&file->lock, flags);
544 uobj = container_of(cq->uobject, struct ib_ucq_object, uobject);
546 entry->desc.comp.cq_handle = cq->uobject->user_handle;
547 entry->counter = &uobj->comp_events_reported;
549 list_add_tail(&entry->list, &file->event_list);
550 list_add_tail(&entry->obj_list, &uobj->comp_list);
551 spin_unlock_irqrestore(&file->lock, flags);
553 wake_up_interruptible(&file->poll_wait);
554 kill_fasync(&file->async_queue, SIGIO, POLL_IN);
557 static void ib_uverbs_async_handler(struct ib_uverbs_file *file,
558 __u64 element, __u64 event,
559 struct list_head *obj_list,
562 struct ib_uverbs_event *entry;
565 spin_lock_irqsave(&file->async_file->lock, flags);
566 if (file->async_file->is_closed) {
567 spin_unlock_irqrestore(&file->async_file->lock, flags);
571 entry = kmalloc(sizeof *entry, GFP_ATOMIC);
573 spin_unlock_irqrestore(&file->async_file->lock, flags);
577 entry->desc.async.element = element;
578 entry->desc.async.event_type = event;
579 entry->desc.async.reserved = 0;
580 entry->counter = counter;
582 list_add_tail(&entry->list, &file->async_file->event_list);
584 list_add_tail(&entry->obj_list, obj_list);
585 spin_unlock_irqrestore(&file->async_file->lock, flags);
587 wake_up_interruptible(&file->async_file->poll_wait);
588 kill_fasync(&file->async_file->async_queue, SIGIO, POLL_IN);
591 void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr)
593 struct ib_ucq_object *uobj = container_of(event->element.cq->uobject,
594 struct ib_ucq_object, uobject);
596 ib_uverbs_async_handler(uobj->uverbs_file, uobj->uobject.user_handle,
597 event->event, &uobj->async_list,
598 &uobj->async_events_reported);
601 void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr)
603 struct ib_uevent_object *uobj;
605 /* for XRC target qp's, check that qp is live */
606 if (!event->element.qp->uobject || !event->element.qp->uobject->live)
609 uobj = container_of(event->element.qp->uobject,
610 struct ib_uevent_object, uobject);
612 ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
613 event->event, &uobj->event_list,
614 &uobj->events_reported);
617 void ib_uverbs_wq_event_handler(struct ib_event *event, void *context_ptr)
619 struct ib_uevent_object *uobj = container_of(event->element.wq->uobject,
620 struct ib_uevent_object, uobject);
622 ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
623 event->event, &uobj->event_list,
624 &uobj->events_reported);
627 void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr)
629 struct ib_uevent_object *uobj;
631 uobj = container_of(event->element.srq->uobject,
632 struct ib_uevent_object, uobject);
634 ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
635 event->event, &uobj->event_list,
636 &uobj->events_reported);
639 void ib_uverbs_event_handler(struct ib_event_handler *handler,
640 struct ib_event *event)
642 struct ib_uverbs_file *file =
643 container_of(handler, struct ib_uverbs_file, event_handler);
645 ib_uverbs_async_handler(file, event->element.port_num, event->event,
649 void ib_uverbs_free_async_event_file(struct ib_uverbs_file *file)
651 kref_put(&file->async_file->ref, ib_uverbs_release_event_file);
652 file->async_file = NULL;
655 struct file *ib_uverbs_alloc_event_file(struct ib_uverbs_file *uverbs_file,
656 struct ib_device *ib_dev,
659 struct ib_uverbs_event_file *ev_file;
663 ev_file = kzalloc(sizeof(*ev_file), GFP_KERNEL);
665 return ERR_PTR(-ENOMEM);
667 kref_init(&ev_file->ref);
668 spin_lock_init(&ev_file->lock);
669 INIT_LIST_HEAD(&ev_file->event_list);
670 init_waitqueue_head(&ev_file->poll_wait);
671 ev_file->uverbs_file = uverbs_file;
672 kref_get(&ev_file->uverbs_file->ref);
673 ev_file->async_queue = NULL;
674 ev_file->is_closed = 0;
676 filp = anon_inode_getfile("[infinibandevent]", &uverbs_event_fops,
681 mutex_lock(&uverbs_file->device->lists_mutex);
682 list_add_tail(&ev_file->list,
683 &uverbs_file->device->uverbs_events_file_list);
684 mutex_unlock(&uverbs_file->device->lists_mutex);
687 WARN_ON(uverbs_file->async_file);
688 uverbs_file->async_file = ev_file;
689 kref_get(&uverbs_file->async_file->ref);
690 INIT_IB_EVENT_HANDLER(&uverbs_file->event_handler,
692 ib_uverbs_event_handler);
693 ret = ib_register_event_handler(&uverbs_file->event_handler);
697 /* At that point async file stuff was fully set */
698 ev_file->is_async = 1;
705 kref_put(&uverbs_file->async_file->ref, ib_uverbs_release_event_file);
706 uverbs_file->async_file = NULL;
710 kref_put(&ev_file->uverbs_file->ref, ib_uverbs_release_file);
711 kref_put(&ev_file->ref, ib_uverbs_release_event_file);
716 * Look up a completion event file by FD. If lookup is successful,
717 * takes a ref to the event file struct that it returns; if
718 * unsuccessful, returns NULL.
720 struct ib_uverbs_event_file *ib_uverbs_lookup_comp_file(int fd)
722 struct ib_uverbs_event_file *ev_file = NULL;
723 struct fd f = fdget(fd);
728 if (f.file->f_op != &uverbs_event_fops)
731 ev_file = f.file->private_data;
732 if (ev_file->is_async) {
737 kref_get(&ev_file->ref);
744 static int verify_command_mask(struct ib_device *ib_dev, __u32 command)
748 if (command <= IB_USER_VERBS_CMD_OPEN_QP)
749 mask = ib_dev->uverbs_cmd_mask;
751 mask = ib_dev->uverbs_ex_cmd_mask;
753 if (mask & ((u64)1 << command))
759 static ssize_t ib_uverbs_write(struct file *filp, const char __user *buf,
760 size_t count, loff_t *pos)
762 struct ib_uverbs_file *file = filp->private_data;
763 struct ib_device *ib_dev;
764 struct ib_uverbs_cmd_hdr hdr;
770 if (!ib_safe_file_access(filp)) {
771 pr_err_once("uverbs_write: process %d (%s) changed security contexts after opening file descriptor, this is not allowed.\n",
772 task_tgid_vnr(current), current->comm);
776 if (count < sizeof hdr)
779 if (copy_from_user(&hdr, buf, sizeof hdr))
782 srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
783 ib_dev = srcu_dereference(file->device->ib_dev,
784 &file->device->disassociate_srcu);
790 if (hdr.command & ~(__u32)(IB_USER_VERBS_CMD_FLAGS_MASK |
791 IB_USER_VERBS_CMD_COMMAND_MASK)) {
796 command = hdr.command & IB_USER_VERBS_CMD_COMMAND_MASK;
797 if (verify_command_mask(ib_dev, command)) {
802 if (!file->ucontext &&
803 command != IB_USER_VERBS_CMD_GET_CONTEXT) {
808 flags = (hdr.command &
809 IB_USER_VERBS_CMD_FLAGS_MASK) >> IB_USER_VERBS_CMD_FLAGS_SHIFT;
812 if (command >= ARRAY_SIZE(uverbs_cmd_table) ||
813 !uverbs_cmd_table[command]) {
818 if (hdr.in_words * 4 != count) {
823 ret = uverbs_cmd_table[command](file, ib_dev,
828 } else if (flags == IB_USER_VERBS_CMD_FLAG_EXTENDED) {
829 struct ib_uverbs_ex_cmd_hdr ex_hdr;
830 struct ib_udata ucore;
832 size_t written_count = count;
834 if (command >= ARRAY_SIZE(uverbs_ex_cmd_table) ||
835 !uverbs_ex_cmd_table[command]) {
840 if (!file->ucontext) {
845 if (count < (sizeof(hdr) + sizeof(ex_hdr))) {
850 if (copy_from_user(&ex_hdr, buf + sizeof(hdr), sizeof(ex_hdr))) {
855 count -= sizeof(hdr) + sizeof(ex_hdr);
856 buf += sizeof(hdr) + sizeof(ex_hdr);
858 if ((hdr.in_words + ex_hdr.provider_in_words) * 8 != count) {
863 if (ex_hdr.cmd_hdr_reserved) {
868 if (ex_hdr.response) {
869 if (!hdr.out_words && !ex_hdr.provider_out_words) {
874 if (!access_ok(VERIFY_WRITE,
875 (void __user *) (unsigned long) ex_hdr.response,
876 (hdr.out_words + ex_hdr.provider_out_words) * 8)) {
881 if (hdr.out_words || ex_hdr.provider_out_words) {
887 INIT_UDATA_BUF_OR_NULL(&ucore, buf, (unsigned long) ex_hdr.response,
888 hdr.in_words * 8, hdr.out_words * 8);
890 INIT_UDATA_BUF_OR_NULL(&uhw,
892 (unsigned long) ex_hdr.response + ucore.outlen,
893 ex_hdr.provider_in_words * 8,
894 ex_hdr.provider_out_words * 8);
896 ret = uverbs_ex_cmd_table[command](file,
907 srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
911 static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma)
913 struct ib_uverbs_file *file = filp->private_data;
914 struct ib_device *ib_dev;
918 srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
919 ib_dev = srcu_dereference(file->device->ib_dev,
920 &file->device->disassociate_srcu);
929 ret = ib_dev->mmap(file->ucontext, vma);
931 srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
936 * ib_uverbs_open() does not need the BKL:
938 * - the ib_uverbs_device structures are properly reference counted and
939 * everything else is purely local to the file being created, so
940 * races against other open calls are not a problem;
941 * - there is no ioctl method to race against;
942 * - the open method will either immediately run -ENXIO, or all
943 * required initialization will be done.
945 static int ib_uverbs_open(struct inode *inode, struct file *filp)
947 struct ib_uverbs_device *dev;
948 struct ib_uverbs_file *file;
949 struct ib_device *ib_dev;
951 int module_dependent;
954 dev = container_of(inode->i_cdev, struct ib_uverbs_device, cdev);
955 if (!atomic_inc_not_zero(&dev->refcount))
958 srcu_key = srcu_read_lock(&dev->disassociate_srcu);
959 mutex_lock(&dev->lists_mutex);
960 ib_dev = srcu_dereference(dev->ib_dev,
961 &dev->disassociate_srcu);
967 /* In case IB device supports disassociate ucontext, there is no hard
968 * dependency between uverbs device and its low level device.
970 module_dependent = !(ib_dev->disassociate_ucontext);
972 if (module_dependent) {
973 if (!try_module_get(ib_dev->owner)) {
979 file = kzalloc(sizeof(*file), GFP_KERNEL);
982 if (module_dependent)
989 file->ucontext = NULL;
990 file->async_file = NULL;
991 kref_init(&file->ref);
992 mutex_init(&file->mutex);
993 mutex_init(&file->cleanup_mutex);
995 filp->private_data = file;
996 kobject_get(&dev->kobj);
997 list_add_tail(&file->list, &dev->uverbs_file_list);
998 mutex_unlock(&dev->lists_mutex);
999 srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
1001 return nonseekable_open(inode, filp);
1004 module_put(ib_dev->owner);
1007 mutex_unlock(&dev->lists_mutex);
1008 srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
1009 if (atomic_dec_and_test(&dev->refcount))
1010 ib_uverbs_comp_dev(dev);
1015 static int ib_uverbs_close(struct inode *inode, struct file *filp)
1017 struct ib_uverbs_file *file = filp->private_data;
1018 struct ib_uverbs_device *dev = file->device;
1020 mutex_lock(&file->cleanup_mutex);
1021 if (file->ucontext) {
1022 ib_uverbs_cleanup_ucontext(file, file->ucontext);
1023 file->ucontext = NULL;
1025 mutex_unlock(&file->cleanup_mutex);
1027 mutex_lock(&file->device->lists_mutex);
1028 if (!file->is_closed) {
1029 list_del(&file->list);
1030 file->is_closed = 1;
1032 mutex_unlock(&file->device->lists_mutex);
1034 if (file->async_file)
1035 kref_put(&file->async_file->ref, ib_uverbs_release_event_file);
1037 kref_put(&file->ref, ib_uverbs_release_file);
1038 kobject_put(&dev->kobj);
1043 static const struct file_operations uverbs_fops = {
1044 .owner = THIS_MODULE,
1045 .write = ib_uverbs_write,
1046 .open = ib_uverbs_open,
1047 .release = ib_uverbs_close,
1048 .llseek = no_llseek,
1051 static const struct file_operations uverbs_mmap_fops = {
1052 .owner = THIS_MODULE,
1053 .write = ib_uverbs_write,
1054 .mmap = ib_uverbs_mmap,
1055 .open = ib_uverbs_open,
1056 .release = ib_uverbs_close,
1057 .llseek = no_llseek,
1060 static struct ib_client uverbs_client = {
1062 .add = ib_uverbs_add_one,
1063 .remove = ib_uverbs_remove_one
1066 static ssize_t show_ibdev(struct device *device, struct device_attribute *attr,
1071 struct ib_uverbs_device *dev = dev_get_drvdata(device);
1072 struct ib_device *ib_dev;
1077 srcu_key = srcu_read_lock(&dev->disassociate_srcu);
1078 ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
1080 ret = sprintf(buf, "%s\n", ib_dev->name);
1081 srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
1085 static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
1087 static ssize_t show_dev_abi_version(struct device *device,
1088 struct device_attribute *attr, char *buf)
1090 struct ib_uverbs_device *dev = dev_get_drvdata(device);
1093 struct ib_device *ib_dev;
1097 srcu_key = srcu_read_lock(&dev->disassociate_srcu);
1098 ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
1100 ret = sprintf(buf, "%d\n", ib_dev->uverbs_abi_ver);
1101 srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
1105 static DEVICE_ATTR(abi_version, S_IRUGO, show_dev_abi_version, NULL);
1107 static CLASS_ATTR_STRING(abi_version, S_IRUGO,
1108 __stringify(IB_USER_VERBS_ABI_VERSION));
1110 static dev_t overflow_maj;
1111 static DECLARE_BITMAP(overflow_map, IB_UVERBS_MAX_DEVICES);
1114 * If we have more than IB_UVERBS_MAX_DEVICES, dynamically overflow by
1115 * requesting a new major number and doubling the number of max devices we
1116 * support. It's stupid, but simple.
1118 static int find_overflow_devnum(void)
1122 if (!overflow_maj) {
1123 ret = alloc_chrdev_region(&overflow_maj, 0, IB_UVERBS_MAX_DEVICES,
1124 "infiniband_verbs");
1126 pr_err("user_verbs: couldn't register dynamic device number\n");
1131 ret = find_first_zero_bit(overflow_map, IB_UVERBS_MAX_DEVICES);
1132 if (ret >= IB_UVERBS_MAX_DEVICES)
1138 static void ib_uverbs_add_one(struct ib_device *device)
1142 struct ib_uverbs_device *uverbs_dev;
1145 if (!device->alloc_ucontext)
1148 uverbs_dev = kzalloc(sizeof *uverbs_dev, GFP_KERNEL);
1152 ret = init_srcu_struct(&uverbs_dev->disassociate_srcu);
1158 atomic_set(&uverbs_dev->refcount, 1);
1159 init_completion(&uverbs_dev->comp);
1160 uverbs_dev->xrcd_tree = RB_ROOT;
1161 mutex_init(&uverbs_dev->xrcd_tree_mutex);
1162 kobject_init(&uverbs_dev->kobj, &ib_uverbs_dev_ktype);
1163 mutex_init(&uverbs_dev->lists_mutex);
1164 INIT_LIST_HEAD(&uverbs_dev->uverbs_file_list);
1165 INIT_LIST_HEAD(&uverbs_dev->uverbs_events_file_list);
1167 spin_lock(&map_lock);
1168 devnum = find_first_zero_bit(dev_map, IB_UVERBS_MAX_DEVICES);
1169 if (devnum >= IB_UVERBS_MAX_DEVICES) {
1170 spin_unlock(&map_lock);
1171 devnum = find_overflow_devnum();
1175 spin_lock(&map_lock);
1176 uverbs_dev->devnum = devnum + IB_UVERBS_MAX_DEVICES;
1177 base = devnum + overflow_maj;
1178 set_bit(devnum, overflow_map);
1180 uverbs_dev->devnum = devnum;
1181 base = devnum + IB_UVERBS_BASE_DEV;
1182 set_bit(devnum, dev_map);
1184 spin_unlock(&map_lock);
1186 rcu_assign_pointer(uverbs_dev->ib_dev, device);
1187 uverbs_dev->num_comp_vectors = device->num_comp_vectors;
1189 cdev_init(&uverbs_dev->cdev, NULL);
1190 uverbs_dev->cdev.owner = THIS_MODULE;
1191 uverbs_dev->cdev.ops = device->mmap ? &uverbs_mmap_fops : &uverbs_fops;
1192 uverbs_dev->cdev.kobj.parent = &uverbs_dev->kobj;
1193 kobject_set_name(&uverbs_dev->cdev.kobj, "uverbs%d", uverbs_dev->devnum);
1194 if (cdev_add(&uverbs_dev->cdev, base, 1))
1197 uverbs_dev->dev = device_create(uverbs_class, device->dev.parent,
1198 uverbs_dev->cdev.dev, uverbs_dev,
1199 "uverbs%d", uverbs_dev->devnum);
1200 if (IS_ERR(uverbs_dev->dev))
1203 if (device_create_file(uverbs_dev->dev, &dev_attr_ibdev))
1205 if (device_create_file(uverbs_dev->dev, &dev_attr_abi_version))
1208 ib_set_client_data(device, &uverbs_client, uverbs_dev);
1213 device_destroy(uverbs_class, uverbs_dev->cdev.dev);
1216 cdev_del(&uverbs_dev->cdev);
1217 if (uverbs_dev->devnum < IB_UVERBS_MAX_DEVICES)
1218 clear_bit(devnum, dev_map);
1220 clear_bit(devnum, overflow_map);
1223 if (atomic_dec_and_test(&uverbs_dev->refcount))
1224 ib_uverbs_comp_dev(uverbs_dev);
1225 wait_for_completion(&uverbs_dev->comp);
1226 kobject_put(&uverbs_dev->kobj);
1230 static void ib_uverbs_free_hw_resources(struct ib_uverbs_device *uverbs_dev,
1231 struct ib_device *ib_dev)
1233 struct ib_uverbs_file *file;
1234 struct ib_uverbs_event_file *event_file;
1235 struct ib_event event;
1237 /* Pending running commands to terminate */
1238 synchronize_srcu(&uverbs_dev->disassociate_srcu);
1239 event.event = IB_EVENT_DEVICE_FATAL;
1240 event.element.port_num = 0;
1241 event.device = ib_dev;
1243 mutex_lock(&uverbs_dev->lists_mutex);
1244 while (!list_empty(&uverbs_dev->uverbs_file_list)) {
1245 struct ib_ucontext *ucontext;
1246 file = list_first_entry(&uverbs_dev->uverbs_file_list,
1247 struct ib_uverbs_file, list);
1248 file->is_closed = 1;
1249 list_del(&file->list);
1250 kref_get(&file->ref);
1251 mutex_unlock(&uverbs_dev->lists_mutex);
1253 ib_uverbs_event_handler(&file->event_handler, &event);
1255 mutex_lock(&file->cleanup_mutex);
1256 ucontext = file->ucontext;
1257 file->ucontext = NULL;
1258 mutex_unlock(&file->cleanup_mutex);
1260 /* At this point ib_uverbs_close cannot be running
1261 * ib_uverbs_cleanup_ucontext
1264 /* We must release the mutex before going ahead and
1265 * calling disassociate_ucontext. disassociate_ucontext
1266 * might end up indirectly calling uverbs_close,
1267 * for example due to freeing the resources
1270 ib_dev->disassociate_ucontext(ucontext);
1271 ib_uverbs_cleanup_ucontext(file, ucontext);
1274 mutex_lock(&uverbs_dev->lists_mutex);
1275 kref_put(&file->ref, ib_uverbs_release_file);
1278 while (!list_empty(&uverbs_dev->uverbs_events_file_list)) {
1279 event_file = list_first_entry(&uverbs_dev->
1280 uverbs_events_file_list,
1281 struct ib_uverbs_event_file,
1283 spin_lock_irq(&event_file->lock);
1284 event_file->is_closed = 1;
1285 spin_unlock_irq(&event_file->lock);
1287 list_del(&event_file->list);
1288 if (event_file->is_async) {
1289 ib_unregister_event_handler(&event_file->uverbs_file->
1291 event_file->uverbs_file->event_handler.device = NULL;
1294 wake_up_interruptible(&event_file->poll_wait);
1295 kill_fasync(&event_file->async_queue, SIGIO, POLL_IN);
1297 mutex_unlock(&uverbs_dev->lists_mutex);
1300 static void ib_uverbs_remove_one(struct ib_device *device, void *client_data)
1302 struct ib_uverbs_device *uverbs_dev = client_data;
1303 int wait_clients = 1;
1308 dev_set_drvdata(uverbs_dev->dev, NULL);
1309 device_destroy(uverbs_class, uverbs_dev->cdev.dev);
1310 cdev_del(&uverbs_dev->cdev);
1312 if (uverbs_dev->devnum < IB_UVERBS_MAX_DEVICES)
1313 clear_bit(uverbs_dev->devnum, dev_map);
1315 clear_bit(uverbs_dev->devnum - IB_UVERBS_MAX_DEVICES, overflow_map);
1317 if (device->disassociate_ucontext) {
1318 /* We disassociate HW resources and immediately return.
1319 * Userspace will see a EIO errno for all future access.
1320 * Upon returning, ib_device may be freed internally and is not
1322 * uverbs_device is still available until all clients close
1323 * their files, then the uverbs device ref count will be zero
1324 * and its resources will be freed.
1325 * Note: At this point no more files can be opened since the
1326 * cdev was deleted, however active clients can still issue
1327 * commands and close their open files.
1329 rcu_assign_pointer(uverbs_dev->ib_dev, NULL);
1330 ib_uverbs_free_hw_resources(uverbs_dev, device);
1334 if (atomic_dec_and_test(&uverbs_dev->refcount))
1335 ib_uverbs_comp_dev(uverbs_dev);
1337 wait_for_completion(&uverbs_dev->comp);
1338 kobject_put(&uverbs_dev->kobj);
1341 static char *uverbs_devnode(struct device *dev, umode_t *mode)
1345 return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
1348 static int __init ib_uverbs_init(void)
1352 ret = register_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES,
1353 "infiniband_verbs");
1355 pr_err("user_verbs: couldn't register device number\n");
1359 uverbs_class = class_create(THIS_MODULE, "infiniband_verbs");
1360 if (IS_ERR(uverbs_class)) {
1361 ret = PTR_ERR(uverbs_class);
1362 pr_err("user_verbs: couldn't create class infiniband_verbs\n");
1366 uverbs_class->devnode = uverbs_devnode;
1368 ret = class_create_file(uverbs_class, &class_attr_abi_version.attr);
1370 pr_err("user_verbs: couldn't create abi_version attribute\n");
1374 ret = ib_register_client(&uverbs_client);
1376 pr_err("user_verbs: couldn't register client\n");
1383 class_destroy(uverbs_class);
1386 unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES);
1392 static void __exit ib_uverbs_cleanup(void)
1394 ib_unregister_client(&uverbs_client);
1395 class_destroy(uverbs_class);
1396 unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES);
1398 unregister_chrdev_region(overflow_maj, IB_UVERBS_MAX_DEVICES);
1399 idr_destroy(&ib_uverbs_pd_idr);
1400 idr_destroy(&ib_uverbs_mr_idr);
1401 idr_destroy(&ib_uverbs_mw_idr);
1402 idr_destroy(&ib_uverbs_ah_idr);
1403 idr_destroy(&ib_uverbs_cq_idr);
1404 idr_destroy(&ib_uverbs_qp_idr);
1405 idr_destroy(&ib_uverbs_srq_idr);
1408 module_init(ib_uverbs_init);
1409 module_exit(ib_uverbs_cleanup);