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>
52 #include <rdma/uverbs_std_types.h>
55 #include "core_priv.h"
56 #include "rdma_core.h"
58 MODULE_AUTHOR("Roland Dreier");
59 MODULE_DESCRIPTION("InfiniBand userspace verbs access");
60 MODULE_LICENSE("Dual BSD/GPL");
63 IB_UVERBS_MAJOR = 231,
64 IB_UVERBS_BASE_MINOR = 192,
65 IB_UVERBS_MAX_DEVICES = RDMA_MAX_PORTS,
66 IB_UVERBS_NUM_FIXED_MINOR = 32,
67 IB_UVERBS_NUM_DYNAMIC_MINOR = IB_UVERBS_MAX_DEVICES - IB_UVERBS_NUM_FIXED_MINOR,
70 #define IB_UVERBS_BASE_DEV MKDEV(IB_UVERBS_MAJOR, IB_UVERBS_BASE_MINOR)
72 static dev_t dynamic_uverbs_dev;
73 static struct class *uverbs_class;
75 static DECLARE_BITMAP(dev_map, IB_UVERBS_MAX_DEVICES);
77 static ssize_t (*uverbs_cmd_table[])(struct ib_uverbs_file *file,
78 const char __user *buf, int in_len,
80 [IB_USER_VERBS_CMD_GET_CONTEXT] = ib_uverbs_get_context,
81 [IB_USER_VERBS_CMD_QUERY_DEVICE] = ib_uverbs_query_device,
82 [IB_USER_VERBS_CMD_QUERY_PORT] = ib_uverbs_query_port,
83 [IB_USER_VERBS_CMD_ALLOC_PD] = ib_uverbs_alloc_pd,
84 [IB_USER_VERBS_CMD_DEALLOC_PD] = ib_uverbs_dealloc_pd,
85 [IB_USER_VERBS_CMD_REG_MR] = ib_uverbs_reg_mr,
86 [IB_USER_VERBS_CMD_REREG_MR] = ib_uverbs_rereg_mr,
87 [IB_USER_VERBS_CMD_DEREG_MR] = ib_uverbs_dereg_mr,
88 [IB_USER_VERBS_CMD_ALLOC_MW] = ib_uverbs_alloc_mw,
89 [IB_USER_VERBS_CMD_DEALLOC_MW] = ib_uverbs_dealloc_mw,
90 [IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL] = ib_uverbs_create_comp_channel,
91 [IB_USER_VERBS_CMD_CREATE_CQ] = ib_uverbs_create_cq,
92 [IB_USER_VERBS_CMD_RESIZE_CQ] = ib_uverbs_resize_cq,
93 [IB_USER_VERBS_CMD_POLL_CQ] = ib_uverbs_poll_cq,
94 [IB_USER_VERBS_CMD_REQ_NOTIFY_CQ] = ib_uverbs_req_notify_cq,
95 [IB_USER_VERBS_CMD_DESTROY_CQ] = ib_uverbs_destroy_cq,
96 [IB_USER_VERBS_CMD_CREATE_QP] = ib_uverbs_create_qp,
97 [IB_USER_VERBS_CMD_QUERY_QP] = ib_uverbs_query_qp,
98 [IB_USER_VERBS_CMD_MODIFY_QP] = ib_uverbs_modify_qp,
99 [IB_USER_VERBS_CMD_DESTROY_QP] = ib_uverbs_destroy_qp,
100 [IB_USER_VERBS_CMD_POST_SEND] = ib_uverbs_post_send,
101 [IB_USER_VERBS_CMD_POST_RECV] = ib_uverbs_post_recv,
102 [IB_USER_VERBS_CMD_POST_SRQ_RECV] = ib_uverbs_post_srq_recv,
103 [IB_USER_VERBS_CMD_CREATE_AH] = ib_uverbs_create_ah,
104 [IB_USER_VERBS_CMD_DESTROY_AH] = ib_uverbs_destroy_ah,
105 [IB_USER_VERBS_CMD_ATTACH_MCAST] = ib_uverbs_attach_mcast,
106 [IB_USER_VERBS_CMD_DETACH_MCAST] = ib_uverbs_detach_mcast,
107 [IB_USER_VERBS_CMD_CREATE_SRQ] = ib_uverbs_create_srq,
108 [IB_USER_VERBS_CMD_MODIFY_SRQ] = ib_uverbs_modify_srq,
109 [IB_USER_VERBS_CMD_QUERY_SRQ] = ib_uverbs_query_srq,
110 [IB_USER_VERBS_CMD_DESTROY_SRQ] = ib_uverbs_destroy_srq,
111 [IB_USER_VERBS_CMD_OPEN_XRCD] = ib_uverbs_open_xrcd,
112 [IB_USER_VERBS_CMD_CLOSE_XRCD] = ib_uverbs_close_xrcd,
113 [IB_USER_VERBS_CMD_CREATE_XSRQ] = ib_uverbs_create_xsrq,
114 [IB_USER_VERBS_CMD_OPEN_QP] = ib_uverbs_open_qp,
117 static int (*uverbs_ex_cmd_table[])(struct ib_uverbs_file *file,
118 struct ib_udata *ucore,
119 struct ib_udata *uhw) = {
120 [IB_USER_VERBS_EX_CMD_CREATE_FLOW] = ib_uverbs_ex_create_flow,
121 [IB_USER_VERBS_EX_CMD_DESTROY_FLOW] = ib_uverbs_ex_destroy_flow,
122 [IB_USER_VERBS_EX_CMD_QUERY_DEVICE] = ib_uverbs_ex_query_device,
123 [IB_USER_VERBS_EX_CMD_CREATE_CQ] = ib_uverbs_ex_create_cq,
124 [IB_USER_VERBS_EX_CMD_CREATE_QP] = ib_uverbs_ex_create_qp,
125 [IB_USER_VERBS_EX_CMD_CREATE_WQ] = ib_uverbs_ex_create_wq,
126 [IB_USER_VERBS_EX_CMD_MODIFY_WQ] = ib_uverbs_ex_modify_wq,
127 [IB_USER_VERBS_EX_CMD_DESTROY_WQ] = ib_uverbs_ex_destroy_wq,
128 [IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL] = ib_uverbs_ex_create_rwq_ind_table,
129 [IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL] = ib_uverbs_ex_destroy_rwq_ind_table,
130 [IB_USER_VERBS_EX_CMD_MODIFY_QP] = ib_uverbs_ex_modify_qp,
131 [IB_USER_VERBS_EX_CMD_MODIFY_CQ] = ib_uverbs_ex_modify_cq,
134 static void ib_uverbs_add_one(struct ib_device *device);
135 static void ib_uverbs_remove_one(struct ib_device *device, void *client_data);
138 * Must be called with the ufile->device->disassociate_srcu held, and the lock
139 * must be held until use of the ucontext is finished.
141 struct ib_ucontext *ib_uverbs_get_ucontext(struct ib_uverbs_file *ufile)
144 * We do not hold the hw_destroy_rwsem lock for this flow, instead
145 * srcu is used. It does not matter if someone races this with
146 * get_context, we get NULL or valid ucontext.
148 struct ib_ucontext *ucontext = smp_load_acquire(&ufile->ucontext);
150 if (!srcu_dereference(ufile->device->ib_dev,
151 &ufile->device->disassociate_srcu))
152 return ERR_PTR(-EIO);
155 return ERR_PTR(-EINVAL);
159 EXPORT_SYMBOL(ib_uverbs_get_ucontext);
161 int uverbs_dealloc_mw(struct ib_mw *mw)
163 struct ib_pd *pd = mw->pd;
166 ret = mw->device->dealloc_mw(mw);
168 atomic_dec(&pd->usecnt);
172 static void ib_uverbs_release_dev(struct kobject *kobj)
174 struct ib_uverbs_device *dev =
175 container_of(kobj, struct ib_uverbs_device, kobj);
177 uverbs_destroy_api(dev->uapi);
178 cleanup_srcu_struct(&dev->disassociate_srcu);
182 static struct kobj_type ib_uverbs_dev_ktype = {
183 .release = ib_uverbs_release_dev,
186 static void ib_uverbs_release_async_event_file(struct kref *ref)
188 struct ib_uverbs_async_event_file *file =
189 container_of(ref, struct ib_uverbs_async_event_file, ref);
194 void ib_uverbs_release_ucq(struct ib_uverbs_file *file,
195 struct ib_uverbs_completion_event_file *ev_file,
196 struct ib_ucq_object *uobj)
198 struct ib_uverbs_event *evt, *tmp;
201 spin_lock_irq(&ev_file->ev_queue.lock);
202 list_for_each_entry_safe(evt, tmp, &uobj->comp_list, obj_list) {
203 list_del(&evt->list);
206 spin_unlock_irq(&ev_file->ev_queue.lock);
208 uverbs_uobject_put(&ev_file->uobj);
211 spin_lock_irq(&file->async_file->ev_queue.lock);
212 list_for_each_entry_safe(evt, tmp, &uobj->async_list, obj_list) {
213 list_del(&evt->list);
216 spin_unlock_irq(&file->async_file->ev_queue.lock);
219 void ib_uverbs_release_uevent(struct ib_uverbs_file *file,
220 struct ib_uevent_object *uobj)
222 struct ib_uverbs_event *evt, *tmp;
224 spin_lock_irq(&file->async_file->ev_queue.lock);
225 list_for_each_entry_safe(evt, tmp, &uobj->event_list, obj_list) {
226 list_del(&evt->list);
229 spin_unlock_irq(&file->async_file->ev_queue.lock);
232 void ib_uverbs_detach_umcast(struct ib_qp *qp,
233 struct ib_uqp_object *uobj)
235 struct ib_uverbs_mcast_entry *mcast, *tmp;
237 list_for_each_entry_safe(mcast, tmp, &uobj->mcast_list, list) {
238 ib_detach_mcast(qp, &mcast->gid, mcast->lid);
239 list_del(&mcast->list);
244 static void ib_uverbs_comp_dev(struct ib_uverbs_device *dev)
246 complete(&dev->comp);
249 void ib_uverbs_release_file(struct kref *ref)
251 struct ib_uverbs_file *file =
252 container_of(ref, struct ib_uverbs_file, ref);
253 struct ib_device *ib_dev;
256 release_ufile_idr_uobject(file);
258 srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
259 ib_dev = srcu_dereference(file->device->ib_dev,
260 &file->device->disassociate_srcu);
261 if (ib_dev && !ib_dev->disassociate_ucontext)
262 module_put(ib_dev->owner);
263 srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
265 if (atomic_dec_and_test(&file->device->refcount))
266 ib_uverbs_comp_dev(file->device);
268 if (file->async_file)
269 kref_put(&file->async_file->ref,
270 ib_uverbs_release_async_event_file);
271 kobject_put(&file->device->kobj);
275 static ssize_t ib_uverbs_event_read(struct ib_uverbs_event_queue *ev_queue,
276 struct file *filp, char __user *buf,
277 size_t count, loff_t *pos,
280 struct ib_uverbs_event *event;
283 spin_lock_irq(&ev_queue->lock);
285 while (list_empty(&ev_queue->event_list)) {
286 spin_unlock_irq(&ev_queue->lock);
288 if (filp->f_flags & O_NONBLOCK)
291 if (wait_event_interruptible(ev_queue->poll_wait,
292 (!list_empty(&ev_queue->event_list) ||
293 ev_queue->is_closed)))
296 spin_lock_irq(&ev_queue->lock);
298 /* If device was disassociated and no event exists set an error */
299 if (list_empty(&ev_queue->event_list) && ev_queue->is_closed) {
300 spin_unlock_irq(&ev_queue->lock);
305 event = list_entry(ev_queue->event_list.next, struct ib_uverbs_event, list);
307 if (eventsz > count) {
311 list_del(ev_queue->event_list.next);
312 if (event->counter) {
314 list_del(&event->obj_list);
318 spin_unlock_irq(&ev_queue->lock);
321 if (copy_to_user(buf, event, eventsz))
332 static ssize_t ib_uverbs_async_event_read(struct file *filp, char __user *buf,
333 size_t count, loff_t *pos)
335 struct ib_uverbs_async_event_file *file = filp->private_data;
337 return ib_uverbs_event_read(&file->ev_queue, filp, buf, count, pos,
338 sizeof(struct ib_uverbs_async_event_desc));
341 static ssize_t ib_uverbs_comp_event_read(struct file *filp, char __user *buf,
342 size_t count, loff_t *pos)
344 struct ib_uverbs_completion_event_file *comp_ev_file =
347 return ib_uverbs_event_read(&comp_ev_file->ev_queue, filp, buf, count,
349 sizeof(struct ib_uverbs_comp_event_desc));
352 static __poll_t ib_uverbs_event_poll(struct ib_uverbs_event_queue *ev_queue,
354 struct poll_table_struct *wait)
356 __poll_t pollflags = 0;
358 poll_wait(filp, &ev_queue->poll_wait, wait);
360 spin_lock_irq(&ev_queue->lock);
361 if (!list_empty(&ev_queue->event_list))
362 pollflags = EPOLLIN | EPOLLRDNORM;
363 else if (ev_queue->is_closed)
364 pollflags = EPOLLERR;
365 spin_unlock_irq(&ev_queue->lock);
370 static __poll_t ib_uverbs_async_event_poll(struct file *filp,
371 struct poll_table_struct *wait)
373 struct ib_uverbs_async_event_file *file = filp->private_data;
375 return ib_uverbs_event_poll(&file->ev_queue, filp, wait);
378 static __poll_t ib_uverbs_comp_event_poll(struct file *filp,
379 struct poll_table_struct *wait)
381 struct ib_uverbs_completion_event_file *comp_ev_file =
384 return ib_uverbs_event_poll(&comp_ev_file->ev_queue, filp, wait);
387 static int ib_uverbs_async_event_fasync(int fd, struct file *filp, int on)
389 struct ib_uverbs_async_event_file *file = filp->private_data;
391 return fasync_helper(fd, filp, on, &file->ev_queue.async_queue);
394 static int ib_uverbs_comp_event_fasync(int fd, struct file *filp, int on)
396 struct ib_uverbs_completion_event_file *comp_ev_file =
399 return fasync_helper(fd, filp, on, &comp_ev_file->ev_queue.async_queue);
402 static int ib_uverbs_async_event_close(struct inode *inode, struct file *filp)
404 struct ib_uverbs_async_event_file *file = filp->private_data;
405 struct ib_uverbs_file *uverbs_file = file->uverbs_file;
406 struct ib_uverbs_event *entry, *tmp;
407 int closed_already = 0;
409 mutex_lock(&uverbs_file->device->lists_mutex);
410 spin_lock_irq(&file->ev_queue.lock);
411 closed_already = file->ev_queue.is_closed;
412 file->ev_queue.is_closed = 1;
413 list_for_each_entry_safe(entry, tmp, &file->ev_queue.event_list, list) {
415 list_del(&entry->obj_list);
418 spin_unlock_irq(&file->ev_queue.lock);
419 if (!closed_already) {
420 list_del(&file->list);
421 ib_unregister_event_handler(&uverbs_file->event_handler);
423 mutex_unlock(&uverbs_file->device->lists_mutex);
425 kref_put(&uverbs_file->ref, ib_uverbs_release_file);
426 kref_put(&file->ref, ib_uverbs_release_async_event_file);
431 static int ib_uverbs_comp_event_close(struct inode *inode, struct file *filp)
433 struct ib_uobject *uobj = filp->private_data;
434 struct ib_uverbs_completion_event_file *file = container_of(
435 uobj, struct ib_uverbs_completion_event_file, uobj);
436 struct ib_uverbs_event *entry, *tmp;
438 spin_lock_irq(&file->ev_queue.lock);
439 list_for_each_entry_safe(entry, tmp, &file->ev_queue.event_list, list) {
441 list_del(&entry->obj_list);
444 file->ev_queue.is_closed = 1;
445 spin_unlock_irq(&file->ev_queue.lock);
447 uverbs_close_fd(filp);
452 const struct file_operations uverbs_event_fops = {
453 .owner = THIS_MODULE,
454 .read = ib_uverbs_comp_event_read,
455 .poll = ib_uverbs_comp_event_poll,
456 .release = ib_uverbs_comp_event_close,
457 .fasync = ib_uverbs_comp_event_fasync,
461 static const struct file_operations uverbs_async_event_fops = {
462 .owner = THIS_MODULE,
463 .read = ib_uverbs_async_event_read,
464 .poll = ib_uverbs_async_event_poll,
465 .release = ib_uverbs_async_event_close,
466 .fasync = ib_uverbs_async_event_fasync,
470 void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context)
472 struct ib_uverbs_event_queue *ev_queue = cq_context;
473 struct ib_ucq_object *uobj;
474 struct ib_uverbs_event *entry;
480 spin_lock_irqsave(&ev_queue->lock, flags);
481 if (ev_queue->is_closed) {
482 spin_unlock_irqrestore(&ev_queue->lock, flags);
486 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
488 spin_unlock_irqrestore(&ev_queue->lock, flags);
492 uobj = container_of(cq->uobject, struct ib_ucq_object, uobject);
494 entry->desc.comp.cq_handle = cq->uobject->user_handle;
495 entry->counter = &uobj->comp_events_reported;
497 list_add_tail(&entry->list, &ev_queue->event_list);
498 list_add_tail(&entry->obj_list, &uobj->comp_list);
499 spin_unlock_irqrestore(&ev_queue->lock, flags);
501 wake_up_interruptible(&ev_queue->poll_wait);
502 kill_fasync(&ev_queue->async_queue, SIGIO, POLL_IN);
505 static void ib_uverbs_async_handler(struct ib_uverbs_file *file,
506 __u64 element, __u64 event,
507 struct list_head *obj_list,
510 struct ib_uverbs_event *entry;
513 spin_lock_irqsave(&file->async_file->ev_queue.lock, flags);
514 if (file->async_file->ev_queue.is_closed) {
515 spin_unlock_irqrestore(&file->async_file->ev_queue.lock, flags);
519 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
521 spin_unlock_irqrestore(&file->async_file->ev_queue.lock, flags);
525 entry->desc.async.element = element;
526 entry->desc.async.event_type = event;
527 entry->desc.async.reserved = 0;
528 entry->counter = counter;
530 list_add_tail(&entry->list, &file->async_file->ev_queue.event_list);
532 list_add_tail(&entry->obj_list, obj_list);
533 spin_unlock_irqrestore(&file->async_file->ev_queue.lock, flags);
535 wake_up_interruptible(&file->async_file->ev_queue.poll_wait);
536 kill_fasync(&file->async_file->ev_queue.async_queue, SIGIO, POLL_IN);
539 void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr)
541 struct ib_ucq_object *uobj = container_of(event->element.cq->uobject,
542 struct ib_ucq_object, uobject);
544 ib_uverbs_async_handler(uobj->uobject.ufile, uobj->uobject.user_handle,
545 event->event, &uobj->async_list,
546 &uobj->async_events_reported);
549 void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr)
551 struct ib_uevent_object *uobj;
553 /* for XRC target qp's, check that qp is live */
554 if (!event->element.qp->uobject)
557 uobj = container_of(event->element.qp->uobject,
558 struct ib_uevent_object, uobject);
560 ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
561 event->event, &uobj->event_list,
562 &uobj->events_reported);
565 void ib_uverbs_wq_event_handler(struct ib_event *event, void *context_ptr)
567 struct ib_uevent_object *uobj = container_of(event->element.wq->uobject,
568 struct ib_uevent_object, uobject);
570 ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
571 event->event, &uobj->event_list,
572 &uobj->events_reported);
575 void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr)
577 struct ib_uevent_object *uobj;
579 uobj = container_of(event->element.srq->uobject,
580 struct ib_uevent_object, uobject);
582 ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
583 event->event, &uobj->event_list,
584 &uobj->events_reported);
587 void ib_uverbs_event_handler(struct ib_event_handler *handler,
588 struct ib_event *event)
590 struct ib_uverbs_file *file =
591 container_of(handler, struct ib_uverbs_file, event_handler);
593 ib_uverbs_async_handler(file, event->element.port_num, event->event,
597 void ib_uverbs_free_async_event_file(struct ib_uverbs_file *file)
599 kref_put(&file->async_file->ref, ib_uverbs_release_async_event_file);
600 file->async_file = NULL;
603 void ib_uverbs_init_event_queue(struct ib_uverbs_event_queue *ev_queue)
605 spin_lock_init(&ev_queue->lock);
606 INIT_LIST_HEAD(&ev_queue->event_list);
607 init_waitqueue_head(&ev_queue->poll_wait);
608 ev_queue->is_closed = 0;
609 ev_queue->async_queue = NULL;
612 struct file *ib_uverbs_alloc_async_event_file(struct ib_uverbs_file *uverbs_file,
613 struct ib_device *ib_dev)
615 struct ib_uverbs_async_event_file *ev_file;
618 ev_file = kzalloc(sizeof(*ev_file), GFP_KERNEL);
620 return ERR_PTR(-ENOMEM);
622 ib_uverbs_init_event_queue(&ev_file->ev_queue);
623 ev_file->uverbs_file = uverbs_file;
624 kref_get(&ev_file->uverbs_file->ref);
625 kref_init(&ev_file->ref);
626 filp = anon_inode_getfile("[infinibandevent]", &uverbs_async_event_fops,
631 mutex_lock(&uverbs_file->device->lists_mutex);
632 list_add_tail(&ev_file->list,
633 &uverbs_file->device->uverbs_events_file_list);
634 mutex_unlock(&uverbs_file->device->lists_mutex);
636 WARN_ON(uverbs_file->async_file);
637 uverbs_file->async_file = ev_file;
638 kref_get(&uverbs_file->async_file->ref);
639 INIT_IB_EVENT_HANDLER(&uverbs_file->event_handler,
641 ib_uverbs_event_handler);
642 ib_register_event_handler(&uverbs_file->event_handler);
643 /* At that point async file stuff was fully set */
648 kref_put(&ev_file->uverbs_file->ref, ib_uverbs_release_file);
649 kref_put(&ev_file->ref, ib_uverbs_release_async_event_file);
653 static bool verify_command_mask(struct ib_uverbs_file *ufile, u32 command,
657 return ufile->uverbs_cmd_mask & BIT_ULL(command);
659 return ufile->uverbs_ex_cmd_mask & BIT_ULL(command);
662 static bool verify_command_idx(u32 command, bool extended)
665 return command < ARRAY_SIZE(uverbs_ex_cmd_table) &&
666 uverbs_ex_cmd_table[command];
668 return command < ARRAY_SIZE(uverbs_cmd_table) &&
669 uverbs_cmd_table[command];
672 static ssize_t process_hdr(struct ib_uverbs_cmd_hdr *hdr,
673 u32 *command, bool *extended)
675 if (hdr->command & ~(u32)(IB_USER_VERBS_CMD_FLAG_EXTENDED |
676 IB_USER_VERBS_CMD_COMMAND_MASK))
679 *command = hdr->command & IB_USER_VERBS_CMD_COMMAND_MASK;
680 *extended = hdr->command & IB_USER_VERBS_CMD_FLAG_EXTENDED;
682 if (!verify_command_idx(*command, *extended))
688 static ssize_t verify_hdr(struct ib_uverbs_cmd_hdr *hdr,
689 struct ib_uverbs_ex_cmd_hdr *ex_hdr,
690 size_t count, bool extended)
693 count -= sizeof(*hdr) + sizeof(*ex_hdr);
695 if ((hdr->in_words + ex_hdr->provider_in_words) * 8 != count)
698 if (ex_hdr->cmd_hdr_reserved)
701 if (ex_hdr->response) {
702 if (!hdr->out_words && !ex_hdr->provider_out_words)
705 if (!access_ok(VERIFY_WRITE,
706 u64_to_user_ptr(ex_hdr->response),
707 (hdr->out_words + ex_hdr->provider_out_words) * 8))
710 if (hdr->out_words || ex_hdr->provider_out_words)
717 /* not extended command */
718 if (hdr->in_words * 4 != count)
724 static ssize_t ib_uverbs_write(struct file *filp, const char __user *buf,
725 size_t count, loff_t *pos)
727 struct ib_uverbs_file *file = filp->private_data;
728 struct ib_uverbs_ex_cmd_hdr ex_hdr;
729 struct ib_uverbs_cmd_hdr hdr;
735 if (!ib_safe_file_access(filp)) {
736 pr_err_once("uverbs_write: process %d (%s) changed security contexts after opening file descriptor, this is not allowed.\n",
737 task_tgid_vnr(current), current->comm);
741 if (count < sizeof(hdr))
744 if (copy_from_user(&hdr, buf, sizeof(hdr)))
747 ret = process_hdr(&hdr, &command, &extended);
752 if (count < (sizeof(hdr) + sizeof(ex_hdr)))
754 if (copy_from_user(&ex_hdr, buf + sizeof(hdr), sizeof(ex_hdr)))
758 ret = verify_hdr(&hdr, &ex_hdr, count, extended);
762 srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
764 if (!verify_command_mask(file, command, extended)) {
772 ret = uverbs_cmd_table[command](file, buf,
776 struct ib_udata ucore;
779 buf += sizeof(ex_hdr);
781 ib_uverbs_init_udata_buf_or_null(&ucore, buf,
782 u64_to_user_ptr(ex_hdr.response),
783 hdr.in_words * 8, hdr.out_words * 8);
785 ib_uverbs_init_udata_buf_or_null(&uhw,
787 u64_to_user_ptr(ex_hdr.response) + ucore.outlen,
788 ex_hdr.provider_in_words * 8,
789 ex_hdr.provider_out_words * 8);
791 ret = uverbs_ex_cmd_table[command](file, &ucore, &uhw);
792 ret = (ret) ? : count;
796 srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
800 static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma)
802 struct ib_uverbs_file *file = filp->private_data;
803 struct ib_ucontext *ucontext;
807 srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
808 ucontext = ib_uverbs_get_ucontext(file);
809 if (IS_ERR(ucontext)) {
810 ret = PTR_ERR(ucontext);
814 ret = ucontext->device->mmap(ucontext, vma);
816 srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
821 * ib_uverbs_open() does not need the BKL:
823 * - the ib_uverbs_device structures are properly reference counted and
824 * everything else is purely local to the file being created, so
825 * races against other open calls are not a problem;
826 * - there is no ioctl method to race against;
827 * - the open method will either immediately run -ENXIO, or all
828 * required initialization will be done.
830 static int ib_uverbs_open(struct inode *inode, struct file *filp)
832 struct ib_uverbs_device *dev;
833 struct ib_uverbs_file *file;
834 struct ib_device *ib_dev;
836 int module_dependent;
839 dev = container_of(inode->i_cdev, struct ib_uverbs_device, cdev);
840 if (!atomic_inc_not_zero(&dev->refcount))
843 srcu_key = srcu_read_lock(&dev->disassociate_srcu);
844 mutex_lock(&dev->lists_mutex);
845 ib_dev = srcu_dereference(dev->ib_dev,
846 &dev->disassociate_srcu);
852 /* In case IB device supports disassociate ucontext, there is no hard
853 * dependency between uverbs device and its low level device.
855 module_dependent = !(ib_dev->disassociate_ucontext);
857 if (module_dependent) {
858 if (!try_module_get(ib_dev->owner)) {
864 file = kzalloc(sizeof(*file), GFP_KERNEL);
867 if (module_dependent)
874 kref_init(&file->ref);
875 mutex_init(&file->ucontext_lock);
877 spin_lock_init(&file->uobjects_lock);
878 INIT_LIST_HEAD(&file->uobjects);
879 init_rwsem(&file->hw_destroy_rwsem);
881 filp->private_data = file;
882 kobject_get(&dev->kobj);
883 list_add_tail(&file->list, &dev->uverbs_file_list);
884 mutex_unlock(&dev->lists_mutex);
885 srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
887 file->uverbs_cmd_mask = ib_dev->uverbs_cmd_mask;
888 file->uverbs_ex_cmd_mask = ib_dev->uverbs_ex_cmd_mask;
890 setup_ufile_idr_uobject(file);
892 return nonseekable_open(inode, filp);
895 module_put(ib_dev->owner);
898 mutex_unlock(&dev->lists_mutex);
899 srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
900 if (atomic_dec_and_test(&dev->refcount))
901 ib_uverbs_comp_dev(dev);
906 static int ib_uverbs_close(struct inode *inode, struct file *filp)
908 struct ib_uverbs_file *file = filp->private_data;
910 uverbs_destroy_ufile_hw(file, RDMA_REMOVE_CLOSE);
912 mutex_lock(&file->device->lists_mutex);
913 if (!file->is_closed) {
914 list_del(&file->list);
917 mutex_unlock(&file->device->lists_mutex);
919 kref_put(&file->ref, ib_uverbs_release_file);
924 static const struct file_operations uverbs_fops = {
925 .owner = THIS_MODULE,
926 .write = ib_uverbs_write,
927 .open = ib_uverbs_open,
928 .release = ib_uverbs_close,
930 .unlocked_ioctl = ib_uverbs_ioctl,
931 .compat_ioctl = ib_uverbs_ioctl,
934 static const struct file_operations uverbs_mmap_fops = {
935 .owner = THIS_MODULE,
936 .write = ib_uverbs_write,
937 .mmap = ib_uverbs_mmap,
938 .open = ib_uverbs_open,
939 .release = ib_uverbs_close,
941 .unlocked_ioctl = ib_uverbs_ioctl,
942 .compat_ioctl = ib_uverbs_ioctl,
945 static struct ib_client uverbs_client = {
947 .add = ib_uverbs_add_one,
948 .remove = ib_uverbs_remove_one
951 static ssize_t show_ibdev(struct device *device, struct device_attribute *attr,
956 struct ib_uverbs_device *dev = dev_get_drvdata(device);
957 struct ib_device *ib_dev;
962 srcu_key = srcu_read_lock(&dev->disassociate_srcu);
963 ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
965 ret = sprintf(buf, "%s\n", ib_dev->name);
966 srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
970 static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
972 static ssize_t show_dev_abi_version(struct device *device,
973 struct device_attribute *attr, char *buf)
975 struct ib_uverbs_device *dev = dev_get_drvdata(device);
978 struct ib_device *ib_dev;
982 srcu_key = srcu_read_lock(&dev->disassociate_srcu);
983 ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
985 ret = sprintf(buf, "%d\n", ib_dev->uverbs_abi_ver);
986 srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
990 static DEVICE_ATTR(abi_version, S_IRUGO, show_dev_abi_version, NULL);
992 static CLASS_ATTR_STRING(abi_version, S_IRUGO,
993 __stringify(IB_USER_VERBS_ABI_VERSION));
995 static int ib_uverbs_create_uapi(struct ib_device *device,
996 struct ib_uverbs_device *uverbs_dev)
998 struct uverbs_api *uapi;
1000 uapi = uverbs_alloc_api(device->driver_specs, device->driver_id);
1002 return PTR_ERR(uapi);
1004 uverbs_dev->uapi = uapi;
1008 static void ib_uverbs_add_one(struct ib_device *device)
1012 struct ib_uverbs_device *uverbs_dev;
1015 if (!device->alloc_ucontext)
1018 uverbs_dev = kzalloc(sizeof(*uverbs_dev), GFP_KERNEL);
1022 ret = init_srcu_struct(&uverbs_dev->disassociate_srcu);
1028 atomic_set(&uverbs_dev->refcount, 1);
1029 init_completion(&uverbs_dev->comp);
1030 uverbs_dev->xrcd_tree = RB_ROOT;
1031 mutex_init(&uverbs_dev->xrcd_tree_mutex);
1032 kobject_init(&uverbs_dev->kobj, &ib_uverbs_dev_ktype);
1033 mutex_init(&uverbs_dev->lists_mutex);
1034 INIT_LIST_HEAD(&uverbs_dev->uverbs_file_list);
1035 INIT_LIST_HEAD(&uverbs_dev->uverbs_events_file_list);
1037 devnum = find_first_zero_bit(dev_map, IB_UVERBS_MAX_DEVICES);
1038 if (devnum >= IB_UVERBS_MAX_DEVICES)
1040 uverbs_dev->devnum = devnum;
1041 set_bit(devnum, dev_map);
1042 if (devnum >= IB_UVERBS_NUM_FIXED_MINOR)
1043 base = dynamic_uverbs_dev + devnum - IB_UVERBS_NUM_FIXED_MINOR;
1045 base = IB_UVERBS_BASE_DEV + devnum;
1047 rcu_assign_pointer(uverbs_dev->ib_dev, device);
1048 uverbs_dev->num_comp_vectors = device->num_comp_vectors;
1050 if (ib_uverbs_create_uapi(device, uverbs_dev))
1053 cdev_init(&uverbs_dev->cdev, NULL);
1054 uverbs_dev->cdev.owner = THIS_MODULE;
1055 uverbs_dev->cdev.ops = device->mmap ? &uverbs_mmap_fops : &uverbs_fops;
1056 cdev_set_parent(&uverbs_dev->cdev, &uverbs_dev->kobj);
1057 kobject_set_name(&uverbs_dev->cdev.kobj, "uverbs%d", uverbs_dev->devnum);
1058 if (cdev_add(&uverbs_dev->cdev, base, 1))
1061 uverbs_dev->dev = device_create(uverbs_class, device->dev.parent,
1062 uverbs_dev->cdev.dev, uverbs_dev,
1063 "uverbs%d", uverbs_dev->devnum);
1064 if (IS_ERR(uverbs_dev->dev))
1067 if (device_create_file(uverbs_dev->dev, &dev_attr_ibdev))
1069 if (device_create_file(uverbs_dev->dev, &dev_attr_abi_version))
1072 ib_set_client_data(device, &uverbs_client, uverbs_dev);
1077 device_destroy(uverbs_class, uverbs_dev->cdev.dev);
1079 cdev_del(&uverbs_dev->cdev);
1081 clear_bit(devnum, dev_map);
1083 if (atomic_dec_and_test(&uverbs_dev->refcount))
1084 ib_uverbs_comp_dev(uverbs_dev);
1085 wait_for_completion(&uverbs_dev->comp);
1086 kobject_put(&uverbs_dev->kobj);
1090 static void ib_uverbs_free_hw_resources(struct ib_uverbs_device *uverbs_dev,
1091 struct ib_device *ib_dev)
1093 struct ib_uverbs_file *file;
1094 struct ib_uverbs_async_event_file *event_file;
1095 struct ib_event event;
1097 /* Pending running commands to terminate */
1098 uverbs_disassociate_api_pre(uverbs_dev);
1099 event.event = IB_EVENT_DEVICE_FATAL;
1100 event.element.port_num = 0;
1101 event.device = ib_dev;
1103 mutex_lock(&uverbs_dev->lists_mutex);
1104 while (!list_empty(&uverbs_dev->uverbs_file_list)) {
1105 file = list_first_entry(&uverbs_dev->uverbs_file_list,
1106 struct ib_uverbs_file, list);
1107 file->is_closed = 1;
1108 list_del(&file->list);
1109 kref_get(&file->ref);
1111 /* We must release the mutex before going ahead and calling
1112 * uverbs_cleanup_ufile, as it might end up indirectly calling
1113 * uverbs_close, for example due to freeing the resources (e.g
1116 mutex_unlock(&uverbs_dev->lists_mutex);
1118 ib_uverbs_event_handler(&file->event_handler, &event);
1119 uverbs_destroy_ufile_hw(file, RDMA_REMOVE_DRIVER_REMOVE);
1120 kref_put(&file->ref, ib_uverbs_release_file);
1122 mutex_lock(&uverbs_dev->lists_mutex);
1125 while (!list_empty(&uverbs_dev->uverbs_events_file_list)) {
1126 event_file = list_first_entry(&uverbs_dev->
1127 uverbs_events_file_list,
1128 struct ib_uverbs_async_event_file,
1130 spin_lock_irq(&event_file->ev_queue.lock);
1131 event_file->ev_queue.is_closed = 1;
1132 spin_unlock_irq(&event_file->ev_queue.lock);
1134 list_del(&event_file->list);
1135 ib_unregister_event_handler(
1136 &event_file->uverbs_file->event_handler);
1137 event_file->uverbs_file->event_handler.device =
1140 wake_up_interruptible(&event_file->ev_queue.poll_wait);
1141 kill_fasync(&event_file->ev_queue.async_queue, SIGIO, POLL_IN);
1143 mutex_unlock(&uverbs_dev->lists_mutex);
1145 uverbs_disassociate_api(uverbs_dev->uapi);
1148 static void ib_uverbs_remove_one(struct ib_device *device, void *client_data)
1150 struct ib_uverbs_device *uverbs_dev = client_data;
1151 int wait_clients = 1;
1156 dev_set_drvdata(uverbs_dev->dev, NULL);
1157 device_destroy(uverbs_class, uverbs_dev->cdev.dev);
1158 cdev_del(&uverbs_dev->cdev);
1159 clear_bit(uverbs_dev->devnum, dev_map);
1161 if (device->disassociate_ucontext) {
1162 /* We disassociate HW resources and immediately return.
1163 * Userspace will see a EIO errno for all future access.
1164 * Upon returning, ib_device may be freed internally and is not
1166 * uverbs_device is still available until all clients close
1167 * their files, then the uverbs device ref count will be zero
1168 * and its resources will be freed.
1169 * Note: At this point no more files can be opened since the
1170 * cdev was deleted, however active clients can still issue
1171 * commands and close their open files.
1173 ib_uverbs_free_hw_resources(uverbs_dev, device);
1177 if (atomic_dec_and_test(&uverbs_dev->refcount))
1178 ib_uverbs_comp_dev(uverbs_dev);
1180 wait_for_completion(&uverbs_dev->comp);
1182 kobject_put(&uverbs_dev->kobj);
1185 static char *uverbs_devnode(struct device *dev, umode_t *mode)
1189 return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
1192 static int __init ib_uverbs_init(void)
1196 ret = register_chrdev_region(IB_UVERBS_BASE_DEV,
1197 IB_UVERBS_NUM_FIXED_MINOR,
1198 "infiniband_verbs");
1200 pr_err("user_verbs: couldn't register device number\n");
1204 ret = alloc_chrdev_region(&dynamic_uverbs_dev, 0,
1205 IB_UVERBS_NUM_DYNAMIC_MINOR,
1206 "infiniband_verbs");
1208 pr_err("couldn't register dynamic device number\n");
1212 uverbs_class = class_create(THIS_MODULE, "infiniband_verbs");
1213 if (IS_ERR(uverbs_class)) {
1214 ret = PTR_ERR(uverbs_class);
1215 pr_err("user_verbs: couldn't create class infiniband_verbs\n");
1219 uverbs_class->devnode = uverbs_devnode;
1221 ret = class_create_file(uverbs_class, &class_attr_abi_version.attr);
1223 pr_err("user_verbs: couldn't create abi_version attribute\n");
1227 ret = ib_register_client(&uverbs_client);
1229 pr_err("user_verbs: couldn't register client\n");
1236 class_destroy(uverbs_class);
1239 unregister_chrdev_region(dynamic_uverbs_dev,
1240 IB_UVERBS_NUM_DYNAMIC_MINOR);
1243 unregister_chrdev_region(IB_UVERBS_BASE_DEV,
1244 IB_UVERBS_NUM_FIXED_MINOR);
1250 static void __exit ib_uverbs_cleanup(void)
1252 ib_unregister_client(&uverbs_client);
1253 class_destroy(uverbs_class);
1254 unregister_chrdev_region(IB_UVERBS_BASE_DEV,
1255 IB_UVERBS_NUM_FIXED_MINOR);
1256 unregister_chrdev_region(dynamic_uverbs_dev,
1257 IB_UVERBS_NUM_DYNAMIC_MINOR);
1260 module_init(ib_uverbs_init);
1261 module_exit(ib_uverbs_cleanup);