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 kobject_put(&file->device->kobj);
272 static ssize_t ib_uverbs_event_read(struct ib_uverbs_event_queue *ev_queue,
273 struct ib_uverbs_file *uverbs_file,
274 struct file *filp, char __user *buf,
275 size_t count, loff_t *pos,
278 struct ib_uverbs_event *event;
281 spin_lock_irq(&ev_queue->lock);
283 while (list_empty(&ev_queue->event_list)) {
284 spin_unlock_irq(&ev_queue->lock);
286 if (filp->f_flags & O_NONBLOCK)
289 if (wait_event_interruptible(ev_queue->poll_wait,
290 (!list_empty(&ev_queue->event_list) ||
291 /* The barriers built into wait_event_interruptible()
292 * and wake_up() guarentee this will see the null set
295 !uverbs_file->device->ib_dev)))
298 /* If device was disassociated and no event exists set an error */
299 if (list_empty(&ev_queue->event_list) &&
300 !uverbs_file->device->ib_dev)
303 spin_lock_irq(&ev_queue->lock);
306 event = list_entry(ev_queue->event_list.next, struct ib_uverbs_event, list);
308 if (eventsz > count) {
312 list_del(ev_queue->event_list.next);
313 if (event->counter) {
315 list_del(&event->obj_list);
319 spin_unlock_irq(&ev_queue->lock);
322 if (copy_to_user(buf, event, eventsz))
333 static ssize_t ib_uverbs_async_event_read(struct file *filp, char __user *buf,
334 size_t count, loff_t *pos)
336 struct ib_uverbs_async_event_file *file = filp->private_data;
338 return ib_uverbs_event_read(&file->ev_queue, file->uverbs_file, filp,
340 sizeof(struct ib_uverbs_async_event_desc));
343 static ssize_t ib_uverbs_comp_event_read(struct file *filp, char __user *buf,
344 size_t count, loff_t *pos)
346 struct ib_uverbs_completion_event_file *comp_ev_file =
349 return ib_uverbs_event_read(&comp_ev_file->ev_queue,
350 comp_ev_file->uobj.ufile, filp,
352 sizeof(struct ib_uverbs_comp_event_desc));
355 static __poll_t ib_uverbs_event_poll(struct ib_uverbs_event_queue *ev_queue,
357 struct poll_table_struct *wait)
359 __poll_t pollflags = 0;
361 poll_wait(filp, &ev_queue->poll_wait, wait);
363 spin_lock_irq(&ev_queue->lock);
364 if (!list_empty(&ev_queue->event_list))
365 pollflags = EPOLLIN | EPOLLRDNORM;
366 spin_unlock_irq(&ev_queue->lock);
371 static __poll_t ib_uverbs_async_event_poll(struct file *filp,
372 struct poll_table_struct *wait)
374 return ib_uverbs_event_poll(filp->private_data, filp, wait);
377 static __poll_t ib_uverbs_comp_event_poll(struct file *filp,
378 struct poll_table_struct *wait)
380 struct ib_uverbs_completion_event_file *comp_ev_file =
383 return ib_uverbs_event_poll(&comp_ev_file->ev_queue, filp, wait);
386 static int ib_uverbs_async_event_fasync(int fd, struct file *filp, int on)
388 struct ib_uverbs_event_queue *ev_queue = filp->private_data;
390 return fasync_helper(fd, filp, on, &ev_queue->async_queue);
393 static int ib_uverbs_comp_event_fasync(int fd, struct file *filp, int on)
395 struct ib_uverbs_completion_event_file *comp_ev_file =
398 return fasync_helper(fd, filp, on, &comp_ev_file->ev_queue.async_queue);
401 static int ib_uverbs_async_event_close(struct inode *inode, struct file *filp)
403 struct ib_uverbs_async_event_file *file = filp->private_data;
404 struct ib_uverbs_file *uverbs_file = file->uverbs_file;
405 struct ib_uverbs_event *entry, *tmp;
406 int closed_already = 0;
408 mutex_lock(&uverbs_file->device->lists_mutex);
409 spin_lock_irq(&file->ev_queue.lock);
410 closed_already = file->ev_queue.is_closed;
411 file->ev_queue.is_closed = 1;
412 list_for_each_entry_safe(entry, tmp, &file->ev_queue.event_list, list) {
414 list_del(&entry->obj_list);
417 spin_unlock_irq(&file->ev_queue.lock);
418 if (!closed_already) {
419 list_del(&file->list);
420 ib_unregister_event_handler(&uverbs_file->event_handler);
422 mutex_unlock(&uverbs_file->device->lists_mutex);
424 kref_put(&uverbs_file->ref, ib_uverbs_release_file);
425 kref_put(&file->ref, ib_uverbs_release_async_event_file);
430 static int ib_uverbs_comp_event_close(struct inode *inode, struct file *filp)
432 struct ib_uobject *uobj = filp->private_data;
433 struct ib_uverbs_completion_event_file *file = container_of(
434 uobj, struct ib_uverbs_completion_event_file, uobj);
435 struct ib_uverbs_event *entry, *tmp;
437 spin_lock_irq(&file->ev_queue.lock);
438 list_for_each_entry_safe(entry, tmp, &file->ev_queue.event_list, list) {
440 list_del(&entry->obj_list);
443 spin_unlock_irq(&file->ev_queue.lock);
445 uverbs_close_fd(filp);
450 const struct file_operations uverbs_event_fops = {
451 .owner = THIS_MODULE,
452 .read = ib_uverbs_comp_event_read,
453 .poll = ib_uverbs_comp_event_poll,
454 .release = ib_uverbs_comp_event_close,
455 .fasync = ib_uverbs_comp_event_fasync,
459 static const struct file_operations uverbs_async_event_fops = {
460 .owner = THIS_MODULE,
461 .read = ib_uverbs_async_event_read,
462 .poll = ib_uverbs_async_event_poll,
463 .release = ib_uverbs_async_event_close,
464 .fasync = ib_uverbs_async_event_fasync,
468 void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context)
470 struct ib_uverbs_event_queue *ev_queue = cq_context;
471 struct ib_ucq_object *uobj;
472 struct ib_uverbs_event *entry;
478 spin_lock_irqsave(&ev_queue->lock, flags);
479 if (ev_queue->is_closed) {
480 spin_unlock_irqrestore(&ev_queue->lock, flags);
484 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
486 spin_unlock_irqrestore(&ev_queue->lock, flags);
490 uobj = container_of(cq->uobject, struct ib_ucq_object, uobject);
492 entry->desc.comp.cq_handle = cq->uobject->user_handle;
493 entry->counter = &uobj->comp_events_reported;
495 list_add_tail(&entry->list, &ev_queue->event_list);
496 list_add_tail(&entry->obj_list, &uobj->comp_list);
497 spin_unlock_irqrestore(&ev_queue->lock, flags);
499 wake_up_interruptible(&ev_queue->poll_wait);
500 kill_fasync(&ev_queue->async_queue, SIGIO, POLL_IN);
503 static void ib_uverbs_async_handler(struct ib_uverbs_file *file,
504 __u64 element, __u64 event,
505 struct list_head *obj_list,
508 struct ib_uverbs_event *entry;
511 spin_lock_irqsave(&file->async_file->ev_queue.lock, flags);
512 if (file->async_file->ev_queue.is_closed) {
513 spin_unlock_irqrestore(&file->async_file->ev_queue.lock, flags);
517 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
519 spin_unlock_irqrestore(&file->async_file->ev_queue.lock, flags);
523 entry->desc.async.element = element;
524 entry->desc.async.event_type = event;
525 entry->desc.async.reserved = 0;
526 entry->counter = counter;
528 list_add_tail(&entry->list, &file->async_file->ev_queue.event_list);
530 list_add_tail(&entry->obj_list, obj_list);
531 spin_unlock_irqrestore(&file->async_file->ev_queue.lock, flags);
533 wake_up_interruptible(&file->async_file->ev_queue.poll_wait);
534 kill_fasync(&file->async_file->ev_queue.async_queue, SIGIO, POLL_IN);
537 void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr)
539 struct ib_ucq_object *uobj = container_of(event->element.cq->uobject,
540 struct ib_ucq_object, uobject);
542 ib_uverbs_async_handler(uobj->uobject.ufile, uobj->uobject.user_handle,
543 event->event, &uobj->async_list,
544 &uobj->async_events_reported);
547 void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr)
549 struct ib_uevent_object *uobj;
551 /* for XRC target qp's, check that qp is live */
552 if (!event->element.qp->uobject)
555 uobj = container_of(event->element.qp->uobject,
556 struct ib_uevent_object, uobject);
558 ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
559 event->event, &uobj->event_list,
560 &uobj->events_reported);
563 void ib_uverbs_wq_event_handler(struct ib_event *event, void *context_ptr)
565 struct ib_uevent_object *uobj = container_of(event->element.wq->uobject,
566 struct ib_uevent_object, uobject);
568 ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
569 event->event, &uobj->event_list,
570 &uobj->events_reported);
573 void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr)
575 struct ib_uevent_object *uobj;
577 uobj = container_of(event->element.srq->uobject,
578 struct ib_uevent_object, uobject);
580 ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
581 event->event, &uobj->event_list,
582 &uobj->events_reported);
585 void ib_uverbs_event_handler(struct ib_event_handler *handler,
586 struct ib_event *event)
588 struct ib_uverbs_file *file =
589 container_of(handler, struct ib_uverbs_file, event_handler);
591 ib_uverbs_async_handler(file, event->element.port_num, event->event,
595 void ib_uverbs_free_async_event_file(struct ib_uverbs_file *file)
597 kref_put(&file->async_file->ref, ib_uverbs_release_async_event_file);
598 file->async_file = NULL;
601 void ib_uverbs_init_event_queue(struct ib_uverbs_event_queue *ev_queue)
603 spin_lock_init(&ev_queue->lock);
604 INIT_LIST_HEAD(&ev_queue->event_list);
605 init_waitqueue_head(&ev_queue->poll_wait);
606 ev_queue->is_closed = 0;
607 ev_queue->async_queue = NULL;
610 struct file *ib_uverbs_alloc_async_event_file(struct ib_uverbs_file *uverbs_file,
611 struct ib_device *ib_dev)
613 struct ib_uverbs_async_event_file *ev_file;
616 ev_file = kzalloc(sizeof(*ev_file), GFP_KERNEL);
618 return ERR_PTR(-ENOMEM);
620 ib_uverbs_init_event_queue(&ev_file->ev_queue);
621 ev_file->uverbs_file = uverbs_file;
622 kref_get(&ev_file->uverbs_file->ref);
623 kref_init(&ev_file->ref);
624 filp = anon_inode_getfile("[infinibandevent]", &uverbs_async_event_fops,
629 mutex_lock(&uverbs_file->device->lists_mutex);
630 list_add_tail(&ev_file->list,
631 &uverbs_file->device->uverbs_events_file_list);
632 mutex_unlock(&uverbs_file->device->lists_mutex);
634 WARN_ON(uverbs_file->async_file);
635 uverbs_file->async_file = ev_file;
636 kref_get(&uverbs_file->async_file->ref);
637 INIT_IB_EVENT_HANDLER(&uverbs_file->event_handler,
639 ib_uverbs_event_handler);
640 ib_register_event_handler(&uverbs_file->event_handler);
641 /* At that point async file stuff was fully set */
646 kref_put(&ev_file->uverbs_file->ref, ib_uverbs_release_file);
647 kref_put(&ev_file->ref, ib_uverbs_release_async_event_file);
651 static bool verify_command_mask(struct ib_uverbs_file *ufile, u32 command,
655 return ufile->uverbs_cmd_mask & BIT_ULL(command);
657 return ufile->uverbs_ex_cmd_mask & BIT_ULL(command);
660 static bool verify_command_idx(u32 command, bool extended)
663 return command < ARRAY_SIZE(uverbs_ex_cmd_table) &&
664 uverbs_ex_cmd_table[command];
666 return command < ARRAY_SIZE(uverbs_cmd_table) &&
667 uverbs_cmd_table[command];
670 static ssize_t process_hdr(struct ib_uverbs_cmd_hdr *hdr,
671 u32 *command, bool *extended)
673 if (hdr->command & ~(u32)(IB_USER_VERBS_CMD_FLAG_EXTENDED |
674 IB_USER_VERBS_CMD_COMMAND_MASK))
677 *command = hdr->command & IB_USER_VERBS_CMD_COMMAND_MASK;
678 *extended = hdr->command & IB_USER_VERBS_CMD_FLAG_EXTENDED;
680 if (!verify_command_idx(*command, *extended))
686 static ssize_t verify_hdr(struct ib_uverbs_cmd_hdr *hdr,
687 struct ib_uverbs_ex_cmd_hdr *ex_hdr,
688 size_t count, bool extended)
691 count -= sizeof(*hdr) + sizeof(*ex_hdr);
693 if ((hdr->in_words + ex_hdr->provider_in_words) * 8 != count)
696 if (ex_hdr->cmd_hdr_reserved)
699 if (ex_hdr->response) {
700 if (!hdr->out_words && !ex_hdr->provider_out_words)
703 if (!access_ok(VERIFY_WRITE,
704 u64_to_user_ptr(ex_hdr->response),
705 (hdr->out_words + ex_hdr->provider_out_words) * 8))
708 if (hdr->out_words || ex_hdr->provider_out_words)
715 /* not extended command */
716 if (hdr->in_words * 4 != count)
722 static ssize_t ib_uverbs_write(struct file *filp, const char __user *buf,
723 size_t count, loff_t *pos)
725 struct ib_uverbs_file *file = filp->private_data;
726 struct ib_uverbs_ex_cmd_hdr ex_hdr;
727 struct ib_uverbs_cmd_hdr hdr;
733 if (!ib_safe_file_access(filp)) {
734 pr_err_once("uverbs_write: process %d (%s) changed security contexts after opening file descriptor, this is not allowed.\n",
735 task_tgid_vnr(current), current->comm);
739 if (count < sizeof(hdr))
742 if (copy_from_user(&hdr, buf, sizeof(hdr)))
745 ret = process_hdr(&hdr, &command, &extended);
750 if (count < (sizeof(hdr) + sizeof(ex_hdr)))
752 if (copy_from_user(&ex_hdr, buf + sizeof(hdr), sizeof(ex_hdr)))
756 ret = verify_hdr(&hdr, &ex_hdr, count, extended);
760 srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
762 if (!verify_command_mask(file, command, extended)) {
770 ret = uverbs_cmd_table[command](file, buf,
774 struct ib_udata ucore;
777 buf += sizeof(ex_hdr);
779 ib_uverbs_init_udata_buf_or_null(&ucore, buf,
780 u64_to_user_ptr(ex_hdr.response),
781 hdr.in_words * 8, hdr.out_words * 8);
783 ib_uverbs_init_udata_buf_or_null(&uhw,
785 u64_to_user_ptr(ex_hdr.response) + ucore.outlen,
786 ex_hdr.provider_in_words * 8,
787 ex_hdr.provider_out_words * 8);
789 ret = uverbs_ex_cmd_table[command](file, &ucore, &uhw);
790 ret = (ret) ? : count;
794 srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
798 static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma)
800 struct ib_uverbs_file *file = filp->private_data;
801 struct ib_ucontext *ucontext;
805 srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
806 ucontext = ib_uverbs_get_ucontext(file);
807 if (IS_ERR(ucontext)) {
808 ret = PTR_ERR(ucontext);
812 ret = ucontext->device->mmap(ucontext, vma);
814 srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
819 * ib_uverbs_open() does not need the BKL:
821 * - the ib_uverbs_device structures are properly reference counted and
822 * everything else is purely local to the file being created, so
823 * races against other open calls are not a problem;
824 * - there is no ioctl method to race against;
825 * - the open method will either immediately run -ENXIO, or all
826 * required initialization will be done.
828 static int ib_uverbs_open(struct inode *inode, struct file *filp)
830 struct ib_uverbs_device *dev;
831 struct ib_uverbs_file *file;
832 struct ib_device *ib_dev;
834 int module_dependent;
837 dev = container_of(inode->i_cdev, struct ib_uverbs_device, cdev);
838 if (!atomic_inc_not_zero(&dev->refcount))
841 srcu_key = srcu_read_lock(&dev->disassociate_srcu);
842 mutex_lock(&dev->lists_mutex);
843 ib_dev = srcu_dereference(dev->ib_dev,
844 &dev->disassociate_srcu);
850 /* In case IB device supports disassociate ucontext, there is no hard
851 * dependency between uverbs device and its low level device.
853 module_dependent = !(ib_dev->disassociate_ucontext);
855 if (module_dependent) {
856 if (!try_module_get(ib_dev->owner)) {
862 file = kzalloc(sizeof(*file), GFP_KERNEL);
865 if (module_dependent)
872 kref_init(&file->ref);
873 mutex_init(&file->ucontext_lock);
875 spin_lock_init(&file->uobjects_lock);
876 INIT_LIST_HEAD(&file->uobjects);
877 init_rwsem(&file->hw_destroy_rwsem);
879 filp->private_data = file;
880 kobject_get(&dev->kobj);
881 list_add_tail(&file->list, &dev->uverbs_file_list);
882 mutex_unlock(&dev->lists_mutex);
883 srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
885 file->uverbs_cmd_mask = ib_dev->uverbs_cmd_mask;
886 file->uverbs_ex_cmd_mask = ib_dev->uverbs_ex_cmd_mask;
888 setup_ufile_idr_uobject(file);
890 return nonseekable_open(inode, filp);
893 module_put(ib_dev->owner);
896 mutex_unlock(&dev->lists_mutex);
897 srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
898 if (atomic_dec_and_test(&dev->refcount))
899 ib_uverbs_comp_dev(dev);
904 static int ib_uverbs_close(struct inode *inode, struct file *filp)
906 struct ib_uverbs_file *file = filp->private_data;
908 uverbs_destroy_ufile_hw(file, RDMA_REMOVE_CLOSE);
910 mutex_lock(&file->device->lists_mutex);
911 if (!file->is_closed) {
912 list_del(&file->list);
915 mutex_unlock(&file->device->lists_mutex);
917 if (file->async_file)
918 kref_put(&file->async_file->ref,
919 ib_uverbs_release_async_event_file);
921 kref_put(&file->ref, ib_uverbs_release_file);
926 static const struct file_operations uverbs_fops = {
927 .owner = THIS_MODULE,
928 .write = ib_uverbs_write,
929 .open = ib_uverbs_open,
930 .release = ib_uverbs_close,
932 .unlocked_ioctl = ib_uverbs_ioctl,
933 .compat_ioctl = ib_uverbs_ioctl,
936 static const struct file_operations uverbs_mmap_fops = {
937 .owner = THIS_MODULE,
938 .write = ib_uverbs_write,
939 .mmap = ib_uverbs_mmap,
940 .open = ib_uverbs_open,
941 .release = ib_uverbs_close,
943 .unlocked_ioctl = ib_uverbs_ioctl,
944 .compat_ioctl = ib_uverbs_ioctl,
947 static struct ib_client uverbs_client = {
949 .add = ib_uverbs_add_one,
950 .remove = ib_uverbs_remove_one
953 static ssize_t show_ibdev(struct device *device, struct device_attribute *attr,
958 struct ib_uverbs_device *dev = dev_get_drvdata(device);
959 struct ib_device *ib_dev;
964 srcu_key = srcu_read_lock(&dev->disassociate_srcu);
965 ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
967 ret = sprintf(buf, "%s\n", ib_dev->name);
968 srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
972 static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
974 static ssize_t show_dev_abi_version(struct device *device,
975 struct device_attribute *attr, char *buf)
977 struct ib_uverbs_device *dev = dev_get_drvdata(device);
980 struct ib_device *ib_dev;
984 srcu_key = srcu_read_lock(&dev->disassociate_srcu);
985 ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
987 ret = sprintf(buf, "%d\n", ib_dev->uverbs_abi_ver);
988 srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
992 static DEVICE_ATTR(abi_version, S_IRUGO, show_dev_abi_version, NULL);
994 static CLASS_ATTR_STRING(abi_version, S_IRUGO,
995 __stringify(IB_USER_VERBS_ABI_VERSION));
997 static int ib_uverbs_create_uapi(struct ib_device *device,
998 struct ib_uverbs_device *uverbs_dev)
1000 struct uverbs_api *uapi;
1002 uapi = uverbs_alloc_api(device->driver_specs, device->driver_id);
1004 return PTR_ERR(uapi);
1006 uverbs_dev->uapi = uapi;
1010 static void ib_uverbs_add_one(struct ib_device *device)
1014 struct ib_uverbs_device *uverbs_dev;
1017 if (!device->alloc_ucontext)
1020 uverbs_dev = kzalloc(sizeof(*uverbs_dev), GFP_KERNEL);
1024 ret = init_srcu_struct(&uverbs_dev->disassociate_srcu);
1030 atomic_set(&uverbs_dev->refcount, 1);
1031 init_completion(&uverbs_dev->comp);
1032 uverbs_dev->xrcd_tree = RB_ROOT;
1033 mutex_init(&uverbs_dev->xrcd_tree_mutex);
1034 kobject_init(&uverbs_dev->kobj, &ib_uverbs_dev_ktype);
1035 mutex_init(&uverbs_dev->lists_mutex);
1036 INIT_LIST_HEAD(&uverbs_dev->uverbs_file_list);
1037 INIT_LIST_HEAD(&uverbs_dev->uverbs_events_file_list);
1039 devnum = find_first_zero_bit(dev_map, IB_UVERBS_MAX_DEVICES);
1040 if (devnum >= IB_UVERBS_MAX_DEVICES)
1042 uverbs_dev->devnum = devnum;
1043 set_bit(devnum, dev_map);
1044 if (devnum >= IB_UVERBS_NUM_FIXED_MINOR)
1045 base = dynamic_uverbs_dev + devnum - IB_UVERBS_NUM_FIXED_MINOR;
1047 base = IB_UVERBS_BASE_DEV + devnum;
1049 rcu_assign_pointer(uverbs_dev->ib_dev, device);
1050 uverbs_dev->num_comp_vectors = device->num_comp_vectors;
1052 if (ib_uverbs_create_uapi(device, uverbs_dev))
1055 cdev_init(&uverbs_dev->cdev, NULL);
1056 uverbs_dev->cdev.owner = THIS_MODULE;
1057 uverbs_dev->cdev.ops = device->mmap ? &uverbs_mmap_fops : &uverbs_fops;
1058 cdev_set_parent(&uverbs_dev->cdev, &uverbs_dev->kobj);
1059 kobject_set_name(&uverbs_dev->cdev.kobj, "uverbs%d", uverbs_dev->devnum);
1060 if (cdev_add(&uverbs_dev->cdev, base, 1))
1063 uverbs_dev->dev = device_create(uverbs_class, device->dev.parent,
1064 uverbs_dev->cdev.dev, uverbs_dev,
1065 "uverbs%d", uverbs_dev->devnum);
1066 if (IS_ERR(uverbs_dev->dev))
1069 if (device_create_file(uverbs_dev->dev, &dev_attr_ibdev))
1071 if (device_create_file(uverbs_dev->dev, &dev_attr_abi_version))
1074 ib_set_client_data(device, &uverbs_client, uverbs_dev);
1079 device_destroy(uverbs_class, uverbs_dev->cdev.dev);
1082 cdev_del(&uverbs_dev->cdev);
1083 clear_bit(devnum, dev_map);
1086 if (atomic_dec_and_test(&uverbs_dev->refcount))
1087 ib_uverbs_comp_dev(uverbs_dev);
1088 wait_for_completion(&uverbs_dev->comp);
1089 kobject_put(&uverbs_dev->kobj);
1093 static void ib_uverbs_free_hw_resources(struct ib_uverbs_device *uverbs_dev,
1094 struct ib_device *ib_dev)
1096 struct ib_uverbs_file *file;
1097 struct ib_uverbs_async_event_file *event_file;
1098 struct ib_event event;
1100 /* Pending running commands to terminate */
1101 uverbs_disassociate_api_pre(uverbs_dev);
1102 event.event = IB_EVENT_DEVICE_FATAL;
1103 event.element.port_num = 0;
1104 event.device = ib_dev;
1106 mutex_lock(&uverbs_dev->lists_mutex);
1107 while (!list_empty(&uverbs_dev->uverbs_file_list)) {
1108 file = list_first_entry(&uverbs_dev->uverbs_file_list,
1109 struct ib_uverbs_file, list);
1110 file->is_closed = 1;
1111 list_del(&file->list);
1112 kref_get(&file->ref);
1114 /* We must release the mutex before going ahead and calling
1115 * uverbs_cleanup_ufile, as it might end up indirectly calling
1116 * uverbs_close, for example due to freeing the resources (e.g
1119 mutex_unlock(&uverbs_dev->lists_mutex);
1121 ib_uverbs_event_handler(&file->event_handler, &event);
1122 uverbs_destroy_ufile_hw(file, RDMA_REMOVE_DRIVER_REMOVE);
1123 kref_put(&file->ref, ib_uverbs_release_file);
1125 mutex_lock(&uverbs_dev->lists_mutex);
1128 while (!list_empty(&uverbs_dev->uverbs_events_file_list)) {
1129 event_file = list_first_entry(&uverbs_dev->
1130 uverbs_events_file_list,
1131 struct ib_uverbs_async_event_file,
1133 spin_lock_irq(&event_file->ev_queue.lock);
1134 event_file->ev_queue.is_closed = 1;
1135 spin_unlock_irq(&event_file->ev_queue.lock);
1137 list_del(&event_file->list);
1138 ib_unregister_event_handler(
1139 &event_file->uverbs_file->event_handler);
1140 event_file->uverbs_file->event_handler.device =
1143 wake_up_interruptible(&event_file->ev_queue.poll_wait);
1144 kill_fasync(&event_file->ev_queue.async_queue, SIGIO, POLL_IN);
1146 mutex_unlock(&uverbs_dev->lists_mutex);
1148 uverbs_disassociate_api(uverbs_dev->uapi);
1151 static void ib_uverbs_remove_one(struct ib_device *device, void *client_data)
1153 struct ib_uverbs_device *uverbs_dev = client_data;
1154 int wait_clients = 1;
1159 dev_set_drvdata(uverbs_dev->dev, NULL);
1160 device_destroy(uverbs_class, uverbs_dev->cdev.dev);
1161 cdev_del(&uverbs_dev->cdev);
1162 clear_bit(uverbs_dev->devnum, dev_map);
1164 if (device->disassociate_ucontext) {
1165 /* We disassociate HW resources and immediately return.
1166 * Userspace will see a EIO errno for all future access.
1167 * Upon returning, ib_device may be freed internally and is not
1169 * uverbs_device is still available until all clients close
1170 * their files, then the uverbs device ref count will be zero
1171 * and its resources will be freed.
1172 * Note: At this point no more files can be opened since the
1173 * cdev was deleted, however active clients can still issue
1174 * commands and close their open files.
1176 ib_uverbs_free_hw_resources(uverbs_dev, device);
1180 if (atomic_dec_and_test(&uverbs_dev->refcount))
1181 ib_uverbs_comp_dev(uverbs_dev);
1183 wait_for_completion(&uverbs_dev->comp);
1185 kobject_put(&uverbs_dev->kobj);
1188 static char *uverbs_devnode(struct device *dev, umode_t *mode)
1192 return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
1195 static int __init ib_uverbs_init(void)
1199 ret = register_chrdev_region(IB_UVERBS_BASE_DEV,
1200 IB_UVERBS_NUM_FIXED_MINOR,
1201 "infiniband_verbs");
1203 pr_err("user_verbs: couldn't register device number\n");
1207 ret = alloc_chrdev_region(&dynamic_uverbs_dev, 0,
1208 IB_UVERBS_NUM_DYNAMIC_MINOR,
1209 "infiniband_verbs");
1211 pr_err("couldn't register dynamic device number\n");
1215 uverbs_class = class_create(THIS_MODULE, "infiniband_verbs");
1216 if (IS_ERR(uverbs_class)) {
1217 ret = PTR_ERR(uverbs_class);
1218 pr_err("user_verbs: couldn't create class infiniband_verbs\n");
1222 uverbs_class->devnode = uverbs_devnode;
1224 ret = class_create_file(uverbs_class, &class_attr_abi_version.attr);
1226 pr_err("user_verbs: couldn't create abi_version attribute\n");
1230 ret = ib_register_client(&uverbs_client);
1232 pr_err("user_verbs: couldn't register client\n");
1239 class_destroy(uverbs_class);
1242 unregister_chrdev_region(dynamic_uverbs_dev,
1243 IB_UVERBS_NUM_DYNAMIC_MINOR);
1246 unregister_chrdev_region(IB_UVERBS_BASE_DEV,
1247 IB_UVERBS_NUM_FIXED_MINOR);
1253 static void __exit ib_uverbs_cleanup(void)
1255 ib_unregister_client(&uverbs_client);
1256 class_destroy(uverbs_class);
1257 unregister_chrdev_region(IB_UVERBS_BASE_DEV,
1258 IB_UVERBS_NUM_FIXED_MINOR);
1259 unregister_chrdev_region(dynamic_uverbs_dev,
1260 IB_UVERBS_NUM_DYNAMIC_MINOR);
1263 module_init(ib_uverbs_init);
1264 module_exit(ib_uverbs_cleanup);