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 static DEFINE_SPINLOCK(map_lock);
71 static DECLARE_BITMAP(dev_map, IB_UVERBS_MAX_DEVICES);
73 static ssize_t (*uverbs_cmd_table[])(struct ib_uverbs_file *file,
74 struct ib_device *ib_dev,
75 const char __user *buf, int in_len,
77 [IB_USER_VERBS_CMD_GET_CONTEXT] = ib_uverbs_get_context,
78 [IB_USER_VERBS_CMD_QUERY_DEVICE] = ib_uverbs_query_device,
79 [IB_USER_VERBS_CMD_QUERY_PORT] = ib_uverbs_query_port,
80 [IB_USER_VERBS_CMD_ALLOC_PD] = ib_uverbs_alloc_pd,
81 [IB_USER_VERBS_CMD_DEALLOC_PD] = ib_uverbs_dealloc_pd,
82 [IB_USER_VERBS_CMD_REG_MR] = ib_uverbs_reg_mr,
83 [IB_USER_VERBS_CMD_REREG_MR] = ib_uverbs_rereg_mr,
84 [IB_USER_VERBS_CMD_DEREG_MR] = ib_uverbs_dereg_mr,
85 [IB_USER_VERBS_CMD_ALLOC_MW] = ib_uverbs_alloc_mw,
86 [IB_USER_VERBS_CMD_DEALLOC_MW] = ib_uverbs_dealloc_mw,
87 [IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL] = ib_uverbs_create_comp_channel,
88 [IB_USER_VERBS_CMD_CREATE_CQ] = ib_uverbs_create_cq,
89 [IB_USER_VERBS_CMD_RESIZE_CQ] = ib_uverbs_resize_cq,
90 [IB_USER_VERBS_CMD_POLL_CQ] = ib_uverbs_poll_cq,
91 [IB_USER_VERBS_CMD_REQ_NOTIFY_CQ] = ib_uverbs_req_notify_cq,
92 [IB_USER_VERBS_CMD_DESTROY_CQ] = ib_uverbs_destroy_cq,
93 [IB_USER_VERBS_CMD_CREATE_QP] = ib_uverbs_create_qp,
94 [IB_USER_VERBS_CMD_QUERY_QP] = ib_uverbs_query_qp,
95 [IB_USER_VERBS_CMD_MODIFY_QP] = ib_uverbs_modify_qp,
96 [IB_USER_VERBS_CMD_DESTROY_QP] = ib_uverbs_destroy_qp,
97 [IB_USER_VERBS_CMD_POST_SEND] = ib_uverbs_post_send,
98 [IB_USER_VERBS_CMD_POST_RECV] = ib_uverbs_post_recv,
99 [IB_USER_VERBS_CMD_POST_SRQ_RECV] = ib_uverbs_post_srq_recv,
100 [IB_USER_VERBS_CMD_CREATE_AH] = ib_uverbs_create_ah,
101 [IB_USER_VERBS_CMD_DESTROY_AH] = ib_uverbs_destroy_ah,
102 [IB_USER_VERBS_CMD_ATTACH_MCAST] = ib_uverbs_attach_mcast,
103 [IB_USER_VERBS_CMD_DETACH_MCAST] = ib_uverbs_detach_mcast,
104 [IB_USER_VERBS_CMD_CREATE_SRQ] = ib_uverbs_create_srq,
105 [IB_USER_VERBS_CMD_MODIFY_SRQ] = ib_uverbs_modify_srq,
106 [IB_USER_VERBS_CMD_QUERY_SRQ] = ib_uverbs_query_srq,
107 [IB_USER_VERBS_CMD_DESTROY_SRQ] = ib_uverbs_destroy_srq,
108 [IB_USER_VERBS_CMD_OPEN_XRCD] = ib_uverbs_open_xrcd,
109 [IB_USER_VERBS_CMD_CLOSE_XRCD] = ib_uverbs_close_xrcd,
110 [IB_USER_VERBS_CMD_CREATE_XSRQ] = ib_uverbs_create_xsrq,
111 [IB_USER_VERBS_CMD_OPEN_QP] = ib_uverbs_open_qp,
114 static int (*uverbs_ex_cmd_table[])(struct ib_uverbs_file *file,
115 struct ib_device *ib_dev,
116 struct ib_udata *ucore,
117 struct ib_udata *uhw) = {
118 [IB_USER_VERBS_EX_CMD_CREATE_FLOW] = ib_uverbs_ex_create_flow,
119 [IB_USER_VERBS_EX_CMD_DESTROY_FLOW] = ib_uverbs_ex_destroy_flow,
120 [IB_USER_VERBS_EX_CMD_QUERY_DEVICE] = ib_uverbs_ex_query_device,
121 [IB_USER_VERBS_EX_CMD_CREATE_CQ] = ib_uverbs_ex_create_cq,
122 [IB_USER_VERBS_EX_CMD_CREATE_QP] = ib_uverbs_ex_create_qp,
123 [IB_USER_VERBS_EX_CMD_CREATE_WQ] = ib_uverbs_ex_create_wq,
124 [IB_USER_VERBS_EX_CMD_MODIFY_WQ] = ib_uverbs_ex_modify_wq,
125 [IB_USER_VERBS_EX_CMD_DESTROY_WQ] = ib_uverbs_ex_destroy_wq,
126 [IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL] = ib_uverbs_ex_create_rwq_ind_table,
127 [IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL] = ib_uverbs_ex_destroy_rwq_ind_table,
128 [IB_USER_VERBS_EX_CMD_MODIFY_QP] = ib_uverbs_ex_modify_qp,
131 static void ib_uverbs_add_one(struct ib_device *device);
132 static void ib_uverbs_remove_one(struct ib_device *device, void *client_data);
134 int uverbs_dealloc_mw(struct ib_mw *mw)
136 struct ib_pd *pd = mw->pd;
139 ret = mw->device->dealloc_mw(mw);
141 atomic_dec(&pd->usecnt);
145 static void ib_uverbs_release_dev(struct kobject *kobj)
147 struct ib_uverbs_device *dev =
148 container_of(kobj, struct ib_uverbs_device, kobj);
150 cleanup_srcu_struct(&dev->disassociate_srcu);
154 static struct kobj_type ib_uverbs_dev_ktype = {
155 .release = ib_uverbs_release_dev,
158 static void ib_uverbs_release_event_file(struct kref *ref)
160 struct ib_uverbs_event_file *file =
161 container_of(ref, struct ib_uverbs_event_file, ref);
166 void ib_uverbs_release_ucq(struct ib_uverbs_file *file,
167 struct ib_uverbs_event_file *ev_file,
168 struct ib_ucq_object *uobj)
170 struct ib_uverbs_event *evt, *tmp;
173 spin_lock_irq(&ev_file->lock);
174 list_for_each_entry_safe(evt, tmp, &uobj->comp_list, obj_list) {
175 list_del(&evt->list);
178 spin_unlock_irq(&ev_file->lock);
180 kref_put(&ev_file->ref, ib_uverbs_release_event_file);
183 spin_lock_irq(&file->async_file->lock);
184 list_for_each_entry_safe(evt, tmp, &uobj->async_list, obj_list) {
185 list_del(&evt->list);
188 spin_unlock_irq(&file->async_file->lock);
191 void ib_uverbs_release_uevent(struct ib_uverbs_file *file,
192 struct ib_uevent_object *uobj)
194 struct ib_uverbs_event *evt, *tmp;
196 spin_lock_irq(&file->async_file->lock);
197 list_for_each_entry_safe(evt, tmp, &uobj->event_list, obj_list) {
198 list_del(&evt->list);
201 spin_unlock_irq(&file->async_file->lock);
204 static void ib_uverbs_detach_umcast(struct ib_qp *qp,
205 struct ib_uqp_object *uobj)
207 struct ib_uverbs_mcast_entry *mcast, *tmp;
209 list_for_each_entry_safe(mcast, tmp, &uobj->mcast_list, list) {
210 ib_detach_mcast(qp, &mcast->gid, mcast->lid);
211 list_del(&mcast->list);
216 static int ib_uverbs_cleanup_ucontext(struct ib_uverbs_file *file,
217 struct ib_ucontext *context)
219 struct ib_uobject *uobj, *tmp;
221 context->closing = 1;
223 list_for_each_entry_safe(uobj, tmp, &context->ah_list, list) {
224 struct ib_ah *ah = uobj->object;
226 idr_remove_uobj(uobj);
228 ib_rdmacg_uncharge(&uobj->cg_obj, context->device,
229 RDMACG_RESOURCE_HCA_OBJECT);
233 /* Remove MWs before QPs, in order to support type 2A MWs. */
234 list_for_each_entry_safe(uobj, tmp, &context->mw_list, list) {
235 struct ib_mw *mw = uobj->object;
237 idr_remove_uobj(uobj);
238 uverbs_dealloc_mw(mw);
239 ib_rdmacg_uncharge(&uobj->cg_obj, context->device,
240 RDMACG_RESOURCE_HCA_OBJECT);
244 list_for_each_entry_safe(uobj, tmp, &context->rule_list, list) {
245 struct ib_flow *flow_id = uobj->object;
247 idr_remove_uobj(uobj);
248 ib_destroy_flow(flow_id);
249 ib_rdmacg_uncharge(&uobj->cg_obj, context->device,
250 RDMACG_RESOURCE_HCA_OBJECT);
254 list_for_each_entry_safe(uobj, tmp, &context->qp_list, list) {
255 struct ib_qp *qp = uobj->object;
256 struct ib_uqp_object *uqp =
257 container_of(uobj, struct ib_uqp_object, uevent.uobject);
259 idr_remove_uobj(uobj);
260 if (qp == qp->real_qp)
261 ib_uverbs_detach_umcast(qp, uqp);
263 ib_rdmacg_uncharge(&uobj->cg_obj, context->device,
264 RDMACG_RESOURCE_HCA_OBJECT);
265 ib_uverbs_release_uevent(file, &uqp->uevent);
269 list_for_each_entry_safe(uobj, tmp, &context->rwq_ind_tbl_list, list) {
270 struct ib_rwq_ind_table *rwq_ind_tbl = uobj->object;
271 struct ib_wq **ind_tbl = rwq_ind_tbl->ind_tbl;
273 idr_remove_uobj(uobj);
274 ib_destroy_rwq_ind_table(rwq_ind_tbl);
279 list_for_each_entry_safe(uobj, tmp, &context->wq_list, list) {
280 struct ib_wq *wq = uobj->object;
281 struct ib_uwq_object *uwq =
282 container_of(uobj, struct ib_uwq_object, uevent.uobject);
284 idr_remove_uobj(uobj);
286 ib_uverbs_release_uevent(file, &uwq->uevent);
290 list_for_each_entry_safe(uobj, tmp, &context->srq_list, list) {
291 struct ib_srq *srq = uobj->object;
292 struct ib_uevent_object *uevent =
293 container_of(uobj, struct ib_uevent_object, uobject);
295 idr_remove_uobj(uobj);
297 ib_rdmacg_uncharge(&uobj->cg_obj, context->device,
298 RDMACG_RESOURCE_HCA_OBJECT);
299 ib_uverbs_release_uevent(file, uevent);
303 list_for_each_entry_safe(uobj, tmp, &context->cq_list, list) {
304 struct ib_cq *cq = uobj->object;
305 struct ib_uverbs_event_file *ev_file = cq->cq_context;
306 struct ib_ucq_object *ucq =
307 container_of(uobj, struct ib_ucq_object, uobject);
309 idr_remove_uobj(uobj);
311 ib_rdmacg_uncharge(&uobj->cg_obj, context->device,
312 RDMACG_RESOURCE_HCA_OBJECT);
313 ib_uverbs_release_ucq(file, ev_file, ucq);
317 list_for_each_entry_safe(uobj, tmp, &context->mr_list, list) {
318 struct ib_mr *mr = uobj->object;
320 idr_remove_uobj(uobj);
322 ib_rdmacg_uncharge(&uobj->cg_obj, context->device,
323 RDMACG_RESOURCE_HCA_OBJECT);
327 mutex_lock(&file->device->xrcd_tree_mutex);
328 list_for_each_entry_safe(uobj, tmp, &context->xrcd_list, list) {
329 struct ib_xrcd *xrcd = uobj->object;
330 struct ib_uxrcd_object *uxrcd =
331 container_of(uobj, struct ib_uxrcd_object, uobject);
333 idr_remove_uobj(uobj);
334 ib_uverbs_dealloc_xrcd(file->device, xrcd);
337 mutex_unlock(&file->device->xrcd_tree_mutex);
339 list_for_each_entry_safe(uobj, tmp, &context->pd_list, list) {
340 struct ib_pd *pd = uobj->object;
342 idr_remove_uobj(uobj);
344 ib_rdmacg_uncharge(&uobj->cg_obj, context->device,
345 RDMACG_RESOURCE_HCA_OBJECT);
349 put_pid(context->tgid);
351 ib_rdmacg_uncharge(&context->cg_obj, context->device,
352 RDMACG_RESOURCE_HCA_HANDLE);
354 return context->device->dealloc_ucontext(context);
357 static void ib_uverbs_comp_dev(struct ib_uverbs_device *dev)
359 complete(&dev->comp);
362 static void ib_uverbs_release_file(struct kref *ref)
364 struct ib_uverbs_file *file =
365 container_of(ref, struct ib_uverbs_file, ref);
366 struct ib_device *ib_dev;
369 srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
370 ib_dev = srcu_dereference(file->device->ib_dev,
371 &file->device->disassociate_srcu);
372 if (ib_dev && !ib_dev->disassociate_ucontext)
373 module_put(ib_dev->owner);
374 srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
376 if (atomic_dec_and_test(&file->device->refcount))
377 ib_uverbs_comp_dev(file->device);
382 static ssize_t ib_uverbs_event_read(struct file *filp, char __user *buf,
383 size_t count, loff_t *pos)
385 struct ib_uverbs_event_file *file = filp->private_data;
386 struct ib_uverbs_event *event;
390 spin_lock_irq(&file->lock);
392 while (list_empty(&file->event_list)) {
393 spin_unlock_irq(&file->lock);
395 if (filp->f_flags & O_NONBLOCK)
398 if (wait_event_interruptible(file->poll_wait,
399 (!list_empty(&file->event_list) ||
400 /* The barriers built into wait_event_interruptible()
401 * and wake_up() guarentee this will see the null set
404 !file->uverbs_file->device->ib_dev)))
407 /* If device was disassociated and no event exists set an error */
408 if (list_empty(&file->event_list) &&
409 !file->uverbs_file->device->ib_dev)
412 spin_lock_irq(&file->lock);
415 event = list_entry(file->event_list.next, struct ib_uverbs_event, list);
418 eventsz = sizeof (struct ib_uverbs_async_event_desc);
420 eventsz = sizeof (struct ib_uverbs_comp_event_desc);
422 if (eventsz > count) {
426 list_del(file->event_list.next);
427 if (event->counter) {
429 list_del(&event->obj_list);
433 spin_unlock_irq(&file->lock);
436 if (copy_to_user(buf, event, eventsz))
447 static unsigned int ib_uverbs_event_poll(struct file *filp,
448 struct poll_table_struct *wait)
450 unsigned int pollflags = 0;
451 struct ib_uverbs_event_file *file = filp->private_data;
453 poll_wait(filp, &file->poll_wait, wait);
455 spin_lock_irq(&file->lock);
456 if (!list_empty(&file->event_list))
457 pollflags = POLLIN | POLLRDNORM;
458 spin_unlock_irq(&file->lock);
463 static int ib_uverbs_event_fasync(int fd, struct file *filp, int on)
465 struct ib_uverbs_event_file *file = filp->private_data;
467 return fasync_helper(fd, filp, on, &file->async_queue);
470 static int ib_uverbs_event_close(struct inode *inode, struct file *filp)
472 struct ib_uverbs_event_file *file = filp->private_data;
473 struct ib_uverbs_event *entry, *tmp;
474 int closed_already = 0;
476 mutex_lock(&file->uverbs_file->device->lists_mutex);
477 spin_lock_irq(&file->lock);
478 closed_already = file->is_closed;
480 list_for_each_entry_safe(entry, tmp, &file->event_list, list) {
482 list_del(&entry->obj_list);
485 spin_unlock_irq(&file->lock);
486 if (!closed_already) {
487 list_del(&file->list);
489 ib_unregister_event_handler(&file->uverbs_file->
492 mutex_unlock(&file->uverbs_file->device->lists_mutex);
494 kref_put(&file->uverbs_file->ref, ib_uverbs_release_file);
495 kref_put(&file->ref, ib_uverbs_release_event_file);
500 static const struct file_operations uverbs_event_fops = {
501 .owner = THIS_MODULE,
502 .read = ib_uverbs_event_read,
503 .poll = ib_uverbs_event_poll,
504 .release = ib_uverbs_event_close,
505 .fasync = ib_uverbs_event_fasync,
509 void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context)
511 struct ib_uverbs_event_file *file = cq_context;
512 struct ib_ucq_object *uobj;
513 struct ib_uverbs_event *entry;
519 spin_lock_irqsave(&file->lock, flags);
520 if (file->is_closed) {
521 spin_unlock_irqrestore(&file->lock, flags);
525 entry = kmalloc(sizeof *entry, GFP_ATOMIC);
527 spin_unlock_irqrestore(&file->lock, flags);
531 uobj = container_of(cq->uobject, struct ib_ucq_object, uobject);
533 entry->desc.comp.cq_handle = cq->uobject->user_handle;
534 entry->counter = &uobj->comp_events_reported;
536 list_add_tail(&entry->list, &file->event_list);
537 list_add_tail(&entry->obj_list, &uobj->comp_list);
538 spin_unlock_irqrestore(&file->lock, flags);
540 wake_up_interruptible(&file->poll_wait);
541 kill_fasync(&file->async_queue, SIGIO, POLL_IN);
544 static void ib_uverbs_async_handler(struct ib_uverbs_file *file,
545 __u64 element, __u64 event,
546 struct list_head *obj_list,
549 struct ib_uverbs_event *entry;
552 spin_lock_irqsave(&file->async_file->lock, flags);
553 if (file->async_file->is_closed) {
554 spin_unlock_irqrestore(&file->async_file->lock, flags);
558 entry = kmalloc(sizeof *entry, GFP_ATOMIC);
560 spin_unlock_irqrestore(&file->async_file->lock, flags);
564 entry->desc.async.element = element;
565 entry->desc.async.event_type = event;
566 entry->desc.async.reserved = 0;
567 entry->counter = counter;
569 list_add_tail(&entry->list, &file->async_file->event_list);
571 list_add_tail(&entry->obj_list, obj_list);
572 spin_unlock_irqrestore(&file->async_file->lock, flags);
574 wake_up_interruptible(&file->async_file->poll_wait);
575 kill_fasync(&file->async_file->async_queue, SIGIO, POLL_IN);
578 void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr)
580 struct ib_ucq_object *uobj = container_of(event->element.cq->uobject,
581 struct ib_ucq_object, uobject);
583 ib_uverbs_async_handler(uobj->uverbs_file, uobj->uobject.user_handle,
584 event->event, &uobj->async_list,
585 &uobj->async_events_reported);
588 void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr)
590 struct ib_uevent_object *uobj;
592 /* for XRC target qp's, check that qp is live */
593 if (!event->element.qp->uobject || !event->element.qp->uobject->live)
596 uobj = container_of(event->element.qp->uobject,
597 struct ib_uevent_object, uobject);
599 ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
600 event->event, &uobj->event_list,
601 &uobj->events_reported);
604 void ib_uverbs_wq_event_handler(struct ib_event *event, void *context_ptr)
606 struct ib_uevent_object *uobj = container_of(event->element.wq->uobject,
607 struct ib_uevent_object, uobject);
609 ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
610 event->event, &uobj->event_list,
611 &uobj->events_reported);
614 void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr)
616 struct ib_uevent_object *uobj;
618 uobj = container_of(event->element.srq->uobject,
619 struct ib_uevent_object, uobject);
621 ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
622 event->event, &uobj->event_list,
623 &uobj->events_reported);
626 void ib_uverbs_event_handler(struct ib_event_handler *handler,
627 struct ib_event *event)
629 struct ib_uverbs_file *file =
630 container_of(handler, struct ib_uverbs_file, event_handler);
632 ib_uverbs_async_handler(file, event->element.port_num, event->event,
636 void ib_uverbs_free_async_event_file(struct ib_uverbs_file *file)
638 kref_put(&file->async_file->ref, ib_uverbs_release_event_file);
639 file->async_file = NULL;
642 struct file *ib_uverbs_alloc_event_file(struct ib_uverbs_file *uverbs_file,
643 struct ib_device *ib_dev,
646 struct ib_uverbs_event_file *ev_file;
650 ev_file = kzalloc(sizeof(*ev_file), GFP_KERNEL);
652 return ERR_PTR(-ENOMEM);
654 kref_init(&ev_file->ref);
655 spin_lock_init(&ev_file->lock);
656 INIT_LIST_HEAD(&ev_file->event_list);
657 init_waitqueue_head(&ev_file->poll_wait);
658 ev_file->uverbs_file = uverbs_file;
659 kref_get(&ev_file->uverbs_file->ref);
660 ev_file->async_queue = NULL;
661 ev_file->is_closed = 0;
663 filp = anon_inode_getfile("[infinibandevent]", &uverbs_event_fops,
668 mutex_lock(&uverbs_file->device->lists_mutex);
669 list_add_tail(&ev_file->list,
670 &uverbs_file->device->uverbs_events_file_list);
671 mutex_unlock(&uverbs_file->device->lists_mutex);
674 WARN_ON(uverbs_file->async_file);
675 uverbs_file->async_file = ev_file;
676 kref_get(&uverbs_file->async_file->ref);
677 INIT_IB_EVENT_HANDLER(&uverbs_file->event_handler,
679 ib_uverbs_event_handler);
680 ret = ib_register_event_handler(&uverbs_file->event_handler);
684 /* At that point async file stuff was fully set */
685 ev_file->is_async = 1;
692 kref_put(&uverbs_file->async_file->ref, ib_uverbs_release_event_file);
693 uverbs_file->async_file = NULL;
697 kref_put(&ev_file->uverbs_file->ref, ib_uverbs_release_file);
698 kref_put(&ev_file->ref, ib_uverbs_release_event_file);
703 * Look up a completion event file by FD. If lookup is successful,
704 * takes a ref to the event file struct that it returns; if
705 * unsuccessful, returns NULL.
707 struct ib_uverbs_event_file *ib_uverbs_lookup_comp_file(int fd)
709 struct ib_uverbs_event_file *ev_file = NULL;
710 struct fd f = fdget(fd);
715 if (f.file->f_op != &uverbs_event_fops)
718 ev_file = f.file->private_data;
719 if (ev_file->is_async) {
724 kref_get(&ev_file->ref);
731 static int verify_command_mask(struct ib_device *ib_dev, __u32 command)
735 if (command <= IB_USER_VERBS_CMD_OPEN_QP)
736 mask = ib_dev->uverbs_cmd_mask;
738 mask = ib_dev->uverbs_ex_cmd_mask;
740 if (mask & ((u64)1 << command))
746 static ssize_t ib_uverbs_write(struct file *filp, const char __user *buf,
747 size_t count, loff_t *pos)
749 struct ib_uverbs_file *file = filp->private_data;
750 struct ib_device *ib_dev;
751 struct ib_uverbs_cmd_hdr hdr;
757 if (!ib_safe_file_access(filp)) {
758 pr_err_once("uverbs_write: process %d (%s) changed security contexts after opening file descriptor, this is not allowed.\n",
759 task_tgid_vnr(current), current->comm);
763 if (count < sizeof hdr)
766 if (copy_from_user(&hdr, buf, sizeof hdr))
769 srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
770 ib_dev = srcu_dereference(file->device->ib_dev,
771 &file->device->disassociate_srcu);
777 if (hdr.command & ~(__u32)(IB_USER_VERBS_CMD_FLAGS_MASK |
778 IB_USER_VERBS_CMD_COMMAND_MASK)) {
783 command = hdr.command & IB_USER_VERBS_CMD_COMMAND_MASK;
784 if (verify_command_mask(ib_dev, command)) {
789 if (!file->ucontext &&
790 command != IB_USER_VERBS_CMD_GET_CONTEXT) {
795 flags = (hdr.command &
796 IB_USER_VERBS_CMD_FLAGS_MASK) >> IB_USER_VERBS_CMD_FLAGS_SHIFT;
799 if (command >= ARRAY_SIZE(uverbs_cmd_table) ||
800 !uverbs_cmd_table[command]) {
805 if (hdr.in_words * 4 != count) {
810 ret = uverbs_cmd_table[command](file, ib_dev,
815 } else if (flags == IB_USER_VERBS_CMD_FLAG_EXTENDED) {
816 struct ib_uverbs_ex_cmd_hdr ex_hdr;
817 struct ib_udata ucore;
819 size_t written_count = count;
821 if (command >= ARRAY_SIZE(uverbs_ex_cmd_table) ||
822 !uverbs_ex_cmd_table[command]) {
827 if (!file->ucontext) {
832 if (count < (sizeof(hdr) + sizeof(ex_hdr))) {
837 if (copy_from_user(&ex_hdr, buf + sizeof(hdr), sizeof(ex_hdr))) {
842 count -= sizeof(hdr) + sizeof(ex_hdr);
843 buf += sizeof(hdr) + sizeof(ex_hdr);
845 if ((hdr.in_words + ex_hdr.provider_in_words) * 8 != count) {
850 if (ex_hdr.cmd_hdr_reserved) {
855 if (ex_hdr.response) {
856 if (!hdr.out_words && !ex_hdr.provider_out_words) {
861 if (!access_ok(VERIFY_WRITE,
862 (void __user *) (unsigned long) ex_hdr.response,
863 (hdr.out_words + ex_hdr.provider_out_words) * 8)) {
868 if (hdr.out_words || ex_hdr.provider_out_words) {
874 INIT_UDATA_BUF_OR_NULL(&ucore, buf, (unsigned long) ex_hdr.response,
875 hdr.in_words * 8, hdr.out_words * 8);
877 INIT_UDATA_BUF_OR_NULL(&uhw,
879 (unsigned long) ex_hdr.response + ucore.outlen,
880 ex_hdr.provider_in_words * 8,
881 ex_hdr.provider_out_words * 8);
883 ret = uverbs_ex_cmd_table[command](file,
894 srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
898 static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma)
900 struct ib_uverbs_file *file = filp->private_data;
901 struct ib_device *ib_dev;
905 srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
906 ib_dev = srcu_dereference(file->device->ib_dev,
907 &file->device->disassociate_srcu);
916 ret = ib_dev->mmap(file->ucontext, vma);
918 srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
923 * ib_uverbs_open() does not need the BKL:
925 * - the ib_uverbs_device structures are properly reference counted and
926 * everything else is purely local to the file being created, so
927 * races against other open calls are not a problem;
928 * - there is no ioctl method to race against;
929 * - the open method will either immediately run -ENXIO, or all
930 * required initialization will be done.
932 static int ib_uverbs_open(struct inode *inode, struct file *filp)
934 struct ib_uverbs_device *dev;
935 struct ib_uverbs_file *file;
936 struct ib_device *ib_dev;
938 int module_dependent;
941 dev = container_of(inode->i_cdev, struct ib_uverbs_device, cdev);
942 if (!atomic_inc_not_zero(&dev->refcount))
945 srcu_key = srcu_read_lock(&dev->disassociate_srcu);
946 mutex_lock(&dev->lists_mutex);
947 ib_dev = srcu_dereference(dev->ib_dev,
948 &dev->disassociate_srcu);
954 /* In case IB device supports disassociate ucontext, there is no hard
955 * dependency between uverbs device and its low level device.
957 module_dependent = !(ib_dev->disassociate_ucontext);
959 if (module_dependent) {
960 if (!try_module_get(ib_dev->owner)) {
966 file = kzalloc(sizeof(*file), GFP_KERNEL);
969 if (module_dependent)
976 spin_lock_init(&file->idr_lock);
977 idr_init(&file->idr);
978 file->ucontext = NULL;
979 file->async_file = NULL;
980 kref_init(&file->ref);
981 mutex_init(&file->mutex);
982 mutex_init(&file->cleanup_mutex);
984 filp->private_data = file;
985 kobject_get(&dev->kobj);
986 list_add_tail(&file->list, &dev->uverbs_file_list);
987 mutex_unlock(&dev->lists_mutex);
988 srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
990 return nonseekable_open(inode, filp);
993 module_put(ib_dev->owner);
996 mutex_unlock(&dev->lists_mutex);
997 srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
998 if (atomic_dec_and_test(&dev->refcount))
999 ib_uverbs_comp_dev(dev);
1004 static int ib_uverbs_close(struct inode *inode, struct file *filp)
1006 struct ib_uverbs_file *file = filp->private_data;
1007 struct ib_uverbs_device *dev = file->device;
1009 mutex_lock(&file->cleanup_mutex);
1010 if (file->ucontext) {
1011 ib_uverbs_cleanup_ucontext(file, file->ucontext);
1012 file->ucontext = NULL;
1014 mutex_unlock(&file->cleanup_mutex);
1015 idr_destroy(&file->idr);
1017 mutex_lock(&file->device->lists_mutex);
1018 if (!file->is_closed) {
1019 list_del(&file->list);
1020 file->is_closed = 1;
1022 mutex_unlock(&file->device->lists_mutex);
1024 if (file->async_file)
1025 kref_put(&file->async_file->ref, ib_uverbs_release_event_file);
1027 kref_put(&file->ref, ib_uverbs_release_file);
1028 kobject_put(&dev->kobj);
1033 static const struct file_operations uverbs_fops = {
1034 .owner = THIS_MODULE,
1035 .write = ib_uverbs_write,
1036 .open = ib_uverbs_open,
1037 .release = ib_uverbs_close,
1038 .llseek = no_llseek,
1041 static const struct file_operations uverbs_mmap_fops = {
1042 .owner = THIS_MODULE,
1043 .write = ib_uverbs_write,
1044 .mmap = ib_uverbs_mmap,
1045 .open = ib_uverbs_open,
1046 .release = ib_uverbs_close,
1047 .llseek = no_llseek,
1050 static struct ib_client uverbs_client = {
1052 .add = ib_uverbs_add_one,
1053 .remove = ib_uverbs_remove_one
1056 static ssize_t show_ibdev(struct device *device, struct device_attribute *attr,
1061 struct ib_uverbs_device *dev = dev_get_drvdata(device);
1062 struct ib_device *ib_dev;
1067 srcu_key = srcu_read_lock(&dev->disassociate_srcu);
1068 ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
1070 ret = sprintf(buf, "%s\n", ib_dev->name);
1071 srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
1075 static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
1077 static ssize_t show_dev_abi_version(struct device *device,
1078 struct device_attribute *attr, char *buf)
1080 struct ib_uverbs_device *dev = dev_get_drvdata(device);
1083 struct ib_device *ib_dev;
1087 srcu_key = srcu_read_lock(&dev->disassociate_srcu);
1088 ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
1090 ret = sprintf(buf, "%d\n", ib_dev->uverbs_abi_ver);
1091 srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
1095 static DEVICE_ATTR(abi_version, S_IRUGO, show_dev_abi_version, NULL);
1097 static CLASS_ATTR_STRING(abi_version, S_IRUGO,
1098 __stringify(IB_USER_VERBS_ABI_VERSION));
1100 static dev_t overflow_maj;
1101 static DECLARE_BITMAP(overflow_map, IB_UVERBS_MAX_DEVICES);
1104 * If we have more than IB_UVERBS_MAX_DEVICES, dynamically overflow by
1105 * requesting a new major number and doubling the number of max devices we
1106 * support. It's stupid, but simple.
1108 static int find_overflow_devnum(void)
1112 if (!overflow_maj) {
1113 ret = alloc_chrdev_region(&overflow_maj, 0, IB_UVERBS_MAX_DEVICES,
1114 "infiniband_verbs");
1116 pr_err("user_verbs: couldn't register dynamic device number\n");
1121 ret = find_first_zero_bit(overflow_map, IB_UVERBS_MAX_DEVICES);
1122 if (ret >= IB_UVERBS_MAX_DEVICES)
1128 static void ib_uverbs_add_one(struct ib_device *device)
1132 struct ib_uverbs_device *uverbs_dev;
1135 if (!device->alloc_ucontext)
1138 uverbs_dev = kzalloc(sizeof *uverbs_dev, GFP_KERNEL);
1142 ret = init_srcu_struct(&uverbs_dev->disassociate_srcu);
1148 atomic_set(&uverbs_dev->refcount, 1);
1149 init_completion(&uverbs_dev->comp);
1150 uverbs_dev->xrcd_tree = RB_ROOT;
1151 mutex_init(&uverbs_dev->xrcd_tree_mutex);
1152 kobject_init(&uverbs_dev->kobj, &ib_uverbs_dev_ktype);
1153 mutex_init(&uverbs_dev->lists_mutex);
1154 INIT_LIST_HEAD(&uverbs_dev->uverbs_file_list);
1155 INIT_LIST_HEAD(&uverbs_dev->uverbs_events_file_list);
1157 spin_lock(&map_lock);
1158 devnum = find_first_zero_bit(dev_map, IB_UVERBS_MAX_DEVICES);
1159 if (devnum >= IB_UVERBS_MAX_DEVICES) {
1160 spin_unlock(&map_lock);
1161 devnum = find_overflow_devnum();
1165 spin_lock(&map_lock);
1166 uverbs_dev->devnum = devnum + IB_UVERBS_MAX_DEVICES;
1167 base = devnum + overflow_maj;
1168 set_bit(devnum, overflow_map);
1170 uverbs_dev->devnum = devnum;
1171 base = devnum + IB_UVERBS_BASE_DEV;
1172 set_bit(devnum, dev_map);
1174 spin_unlock(&map_lock);
1176 rcu_assign_pointer(uverbs_dev->ib_dev, device);
1177 uverbs_dev->num_comp_vectors = device->num_comp_vectors;
1179 cdev_init(&uverbs_dev->cdev, NULL);
1180 uverbs_dev->cdev.owner = THIS_MODULE;
1181 uverbs_dev->cdev.ops = device->mmap ? &uverbs_mmap_fops : &uverbs_fops;
1182 uverbs_dev->cdev.kobj.parent = &uverbs_dev->kobj;
1183 kobject_set_name(&uverbs_dev->cdev.kobj, "uverbs%d", uverbs_dev->devnum);
1184 if (cdev_add(&uverbs_dev->cdev, base, 1))
1187 uverbs_dev->dev = device_create(uverbs_class, device->dev.parent,
1188 uverbs_dev->cdev.dev, uverbs_dev,
1189 "uverbs%d", uverbs_dev->devnum);
1190 if (IS_ERR(uverbs_dev->dev))
1193 if (device_create_file(uverbs_dev->dev, &dev_attr_ibdev))
1195 if (device_create_file(uverbs_dev->dev, &dev_attr_abi_version))
1198 ib_set_client_data(device, &uverbs_client, uverbs_dev);
1203 device_destroy(uverbs_class, uverbs_dev->cdev.dev);
1206 cdev_del(&uverbs_dev->cdev);
1207 if (uverbs_dev->devnum < IB_UVERBS_MAX_DEVICES)
1208 clear_bit(devnum, dev_map);
1210 clear_bit(devnum, overflow_map);
1213 if (atomic_dec_and_test(&uverbs_dev->refcount))
1214 ib_uverbs_comp_dev(uverbs_dev);
1215 wait_for_completion(&uverbs_dev->comp);
1216 kobject_put(&uverbs_dev->kobj);
1220 static void ib_uverbs_free_hw_resources(struct ib_uverbs_device *uverbs_dev,
1221 struct ib_device *ib_dev)
1223 struct ib_uverbs_file *file;
1224 struct ib_uverbs_event_file *event_file;
1225 struct ib_event event;
1227 /* Pending running commands to terminate */
1228 synchronize_srcu(&uverbs_dev->disassociate_srcu);
1229 event.event = IB_EVENT_DEVICE_FATAL;
1230 event.element.port_num = 0;
1231 event.device = ib_dev;
1233 mutex_lock(&uverbs_dev->lists_mutex);
1234 while (!list_empty(&uverbs_dev->uverbs_file_list)) {
1235 struct ib_ucontext *ucontext;
1236 file = list_first_entry(&uverbs_dev->uverbs_file_list,
1237 struct ib_uverbs_file, list);
1238 file->is_closed = 1;
1239 list_del(&file->list);
1240 kref_get(&file->ref);
1241 mutex_unlock(&uverbs_dev->lists_mutex);
1243 ib_uverbs_event_handler(&file->event_handler, &event);
1245 mutex_lock(&file->cleanup_mutex);
1246 ucontext = file->ucontext;
1247 file->ucontext = NULL;
1248 mutex_unlock(&file->cleanup_mutex);
1250 /* At this point ib_uverbs_close cannot be running
1251 * ib_uverbs_cleanup_ucontext
1254 /* We must release the mutex before going ahead and
1255 * calling disassociate_ucontext. disassociate_ucontext
1256 * might end up indirectly calling uverbs_close,
1257 * for example due to freeing the resources
1260 ib_dev->disassociate_ucontext(ucontext);
1261 ib_uverbs_cleanup_ucontext(file, ucontext);
1264 mutex_lock(&uverbs_dev->lists_mutex);
1265 kref_put(&file->ref, ib_uverbs_release_file);
1268 while (!list_empty(&uverbs_dev->uverbs_events_file_list)) {
1269 event_file = list_first_entry(&uverbs_dev->
1270 uverbs_events_file_list,
1271 struct ib_uverbs_event_file,
1273 spin_lock_irq(&event_file->lock);
1274 event_file->is_closed = 1;
1275 spin_unlock_irq(&event_file->lock);
1277 list_del(&event_file->list);
1278 if (event_file->is_async) {
1279 ib_unregister_event_handler(&event_file->uverbs_file->
1281 event_file->uverbs_file->event_handler.device = NULL;
1284 wake_up_interruptible(&event_file->poll_wait);
1285 kill_fasync(&event_file->async_queue, SIGIO, POLL_IN);
1287 mutex_unlock(&uverbs_dev->lists_mutex);
1290 static void ib_uverbs_remove_one(struct ib_device *device, void *client_data)
1292 struct ib_uverbs_device *uverbs_dev = client_data;
1293 int wait_clients = 1;
1298 dev_set_drvdata(uverbs_dev->dev, NULL);
1299 device_destroy(uverbs_class, uverbs_dev->cdev.dev);
1300 cdev_del(&uverbs_dev->cdev);
1302 if (uverbs_dev->devnum < IB_UVERBS_MAX_DEVICES)
1303 clear_bit(uverbs_dev->devnum, dev_map);
1305 clear_bit(uverbs_dev->devnum - IB_UVERBS_MAX_DEVICES, overflow_map);
1307 if (device->disassociate_ucontext) {
1308 /* We disassociate HW resources and immediately return.
1309 * Userspace will see a EIO errno for all future access.
1310 * Upon returning, ib_device may be freed internally and is not
1312 * uverbs_device is still available until all clients close
1313 * their files, then the uverbs device ref count will be zero
1314 * and its resources will be freed.
1315 * Note: At this point no more files can be opened since the
1316 * cdev was deleted, however active clients can still issue
1317 * commands and close their open files.
1319 rcu_assign_pointer(uverbs_dev->ib_dev, NULL);
1320 ib_uverbs_free_hw_resources(uverbs_dev, device);
1324 if (atomic_dec_and_test(&uverbs_dev->refcount))
1325 ib_uverbs_comp_dev(uverbs_dev);
1327 wait_for_completion(&uverbs_dev->comp);
1328 kobject_put(&uverbs_dev->kobj);
1331 static char *uverbs_devnode(struct device *dev, umode_t *mode)
1335 return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
1338 static int __init ib_uverbs_init(void)
1342 ret = register_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES,
1343 "infiniband_verbs");
1345 pr_err("user_verbs: couldn't register device number\n");
1349 uverbs_class = class_create(THIS_MODULE, "infiniband_verbs");
1350 if (IS_ERR(uverbs_class)) {
1351 ret = PTR_ERR(uverbs_class);
1352 pr_err("user_verbs: couldn't create class infiniband_verbs\n");
1356 uverbs_class->devnode = uverbs_devnode;
1358 ret = class_create_file(uverbs_class, &class_attr_abi_version.attr);
1360 pr_err("user_verbs: couldn't create abi_version attribute\n");
1364 ret = ib_register_client(&uverbs_client);
1366 pr_err("user_verbs: couldn't register client\n");
1373 class_destroy(uverbs_class);
1376 unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES);
1382 static void __exit ib_uverbs_cleanup(void)
1384 ib_unregister_client(&uverbs_client);
1385 class_destroy(uverbs_class);
1386 unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES);
1388 unregister_chrdev_region(overflow_maj, IB_UVERBS_MAX_DEVICES);
1391 module_init(ib_uverbs_init);
1392 module_exit(ib_uverbs_cleanup);