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>
48 #include <linux/sched/mm.h>
50 #include <linux/uaccess.h>
53 #include <rdma/uverbs_std_types.h>
56 #include "core_priv.h"
57 #include "rdma_core.h"
59 MODULE_AUTHOR("Roland Dreier");
60 MODULE_DESCRIPTION("InfiniBand userspace verbs access");
61 MODULE_LICENSE("Dual BSD/GPL");
64 IB_UVERBS_MAJOR = 231,
65 IB_UVERBS_BASE_MINOR = 192,
66 IB_UVERBS_MAX_DEVICES = RDMA_MAX_PORTS,
67 IB_UVERBS_NUM_FIXED_MINOR = 32,
68 IB_UVERBS_NUM_DYNAMIC_MINOR = IB_UVERBS_MAX_DEVICES - IB_UVERBS_NUM_FIXED_MINOR,
71 #define IB_UVERBS_BASE_DEV MKDEV(IB_UVERBS_MAJOR, IB_UVERBS_BASE_MINOR)
73 static dev_t dynamic_uverbs_dev;
74 static struct class *uverbs_class;
76 static DEFINE_IDA(uverbs_ida);
77 static void ib_uverbs_add_one(struct ib_device *device);
78 static void ib_uverbs_remove_one(struct ib_device *device, void *client_data);
81 * Must be called with the ufile->device->disassociate_srcu held, and the lock
82 * must be held until use of the ucontext is finished.
84 struct ib_ucontext *ib_uverbs_get_ucontext(struct ib_uverbs_file *ufile)
87 * We do not hold the hw_destroy_rwsem lock for this flow, instead
88 * srcu is used. It does not matter if someone races this with
89 * get_context, we get NULL or valid ucontext.
91 struct ib_ucontext *ucontext = smp_load_acquire(&ufile->ucontext);
93 if (!srcu_dereference(ufile->device->ib_dev,
94 &ufile->device->disassociate_srcu))
98 return ERR_PTR(-EINVAL);
102 EXPORT_SYMBOL(ib_uverbs_get_ucontext);
104 int uverbs_dealloc_mw(struct ib_mw *mw)
106 struct ib_pd *pd = mw->pd;
109 ret = mw->device->dealloc_mw(mw);
111 atomic_dec(&pd->usecnt);
115 static void ib_uverbs_release_dev(struct device *device)
117 struct ib_uverbs_device *dev =
118 container_of(device, struct ib_uverbs_device, dev);
120 uverbs_destroy_api(dev->uapi);
121 cleanup_srcu_struct(&dev->disassociate_srcu);
125 static void ib_uverbs_release_async_event_file(struct kref *ref)
127 struct ib_uverbs_async_event_file *file =
128 container_of(ref, struct ib_uverbs_async_event_file, ref);
133 void ib_uverbs_release_ucq(struct ib_uverbs_file *file,
134 struct ib_uverbs_completion_event_file *ev_file,
135 struct ib_ucq_object *uobj)
137 struct ib_uverbs_event *evt, *tmp;
140 spin_lock_irq(&ev_file->ev_queue.lock);
141 list_for_each_entry_safe(evt, tmp, &uobj->comp_list, obj_list) {
142 list_del(&evt->list);
145 spin_unlock_irq(&ev_file->ev_queue.lock);
147 uverbs_uobject_put(&ev_file->uobj);
150 spin_lock_irq(&file->async_file->ev_queue.lock);
151 list_for_each_entry_safe(evt, tmp, &uobj->async_list, obj_list) {
152 list_del(&evt->list);
155 spin_unlock_irq(&file->async_file->ev_queue.lock);
158 void ib_uverbs_release_uevent(struct ib_uverbs_file *file,
159 struct ib_uevent_object *uobj)
161 struct ib_uverbs_event *evt, *tmp;
163 spin_lock_irq(&file->async_file->ev_queue.lock);
164 list_for_each_entry_safe(evt, tmp, &uobj->event_list, obj_list) {
165 list_del(&evt->list);
168 spin_unlock_irq(&file->async_file->ev_queue.lock);
171 void ib_uverbs_detach_umcast(struct ib_qp *qp,
172 struct ib_uqp_object *uobj)
174 struct ib_uverbs_mcast_entry *mcast, *tmp;
176 list_for_each_entry_safe(mcast, tmp, &uobj->mcast_list, list) {
177 ib_detach_mcast(qp, &mcast->gid, mcast->lid);
178 list_del(&mcast->list);
183 static void ib_uverbs_comp_dev(struct ib_uverbs_device *dev)
185 complete(&dev->comp);
188 void ib_uverbs_release_file(struct kref *ref)
190 struct ib_uverbs_file *file =
191 container_of(ref, struct ib_uverbs_file, ref);
192 struct ib_device *ib_dev;
195 release_ufile_idr_uobject(file);
197 srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
198 ib_dev = srcu_dereference(file->device->ib_dev,
199 &file->device->disassociate_srcu);
200 if (ib_dev && !ib_dev->disassociate_ucontext)
201 module_put(ib_dev->owner);
202 srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
204 if (atomic_dec_and_test(&file->device->refcount))
205 ib_uverbs_comp_dev(file->device);
207 put_device(&file->device->dev);
211 static ssize_t ib_uverbs_event_read(struct ib_uverbs_event_queue *ev_queue,
212 struct ib_uverbs_file *uverbs_file,
213 struct file *filp, char __user *buf,
214 size_t count, loff_t *pos,
217 struct ib_uverbs_event *event;
220 spin_lock_irq(&ev_queue->lock);
222 while (list_empty(&ev_queue->event_list)) {
223 spin_unlock_irq(&ev_queue->lock);
225 if (filp->f_flags & O_NONBLOCK)
228 if (wait_event_interruptible(ev_queue->poll_wait,
229 (!list_empty(&ev_queue->event_list) ||
230 /* The barriers built into wait_event_interruptible()
231 * and wake_up() guarentee this will see the null set
234 !uverbs_file->device->ib_dev)))
237 /* If device was disassociated and no event exists set an error */
238 if (list_empty(&ev_queue->event_list) &&
239 !uverbs_file->device->ib_dev)
242 spin_lock_irq(&ev_queue->lock);
245 event = list_entry(ev_queue->event_list.next, struct ib_uverbs_event, list);
247 if (eventsz > count) {
251 list_del(ev_queue->event_list.next);
252 if (event->counter) {
254 list_del(&event->obj_list);
258 spin_unlock_irq(&ev_queue->lock);
261 if (copy_to_user(buf, event, eventsz))
272 static ssize_t ib_uverbs_async_event_read(struct file *filp, char __user *buf,
273 size_t count, loff_t *pos)
275 struct ib_uverbs_async_event_file *file = filp->private_data;
277 return ib_uverbs_event_read(&file->ev_queue, file->uverbs_file, filp,
279 sizeof(struct ib_uverbs_async_event_desc));
282 static ssize_t ib_uverbs_comp_event_read(struct file *filp, char __user *buf,
283 size_t count, loff_t *pos)
285 struct ib_uverbs_completion_event_file *comp_ev_file =
288 return ib_uverbs_event_read(&comp_ev_file->ev_queue,
289 comp_ev_file->uobj.ufile, filp,
291 sizeof(struct ib_uverbs_comp_event_desc));
294 static __poll_t ib_uverbs_event_poll(struct ib_uverbs_event_queue *ev_queue,
296 struct poll_table_struct *wait)
298 __poll_t pollflags = 0;
300 poll_wait(filp, &ev_queue->poll_wait, wait);
302 spin_lock_irq(&ev_queue->lock);
303 if (!list_empty(&ev_queue->event_list))
304 pollflags = EPOLLIN | EPOLLRDNORM;
305 spin_unlock_irq(&ev_queue->lock);
310 static __poll_t ib_uverbs_async_event_poll(struct file *filp,
311 struct poll_table_struct *wait)
313 return ib_uverbs_event_poll(filp->private_data, filp, wait);
316 static __poll_t ib_uverbs_comp_event_poll(struct file *filp,
317 struct poll_table_struct *wait)
319 struct ib_uverbs_completion_event_file *comp_ev_file =
322 return ib_uverbs_event_poll(&comp_ev_file->ev_queue, filp, wait);
325 static int ib_uverbs_async_event_fasync(int fd, struct file *filp, int on)
327 struct ib_uverbs_event_queue *ev_queue = filp->private_data;
329 return fasync_helper(fd, filp, on, &ev_queue->async_queue);
332 static int ib_uverbs_comp_event_fasync(int fd, struct file *filp, int on)
334 struct ib_uverbs_completion_event_file *comp_ev_file =
337 return fasync_helper(fd, filp, on, &comp_ev_file->ev_queue.async_queue);
340 static int ib_uverbs_async_event_close(struct inode *inode, struct file *filp)
342 struct ib_uverbs_async_event_file *file = filp->private_data;
343 struct ib_uverbs_file *uverbs_file = file->uverbs_file;
344 struct ib_uverbs_event *entry, *tmp;
345 int closed_already = 0;
347 mutex_lock(&uverbs_file->device->lists_mutex);
348 spin_lock_irq(&file->ev_queue.lock);
349 closed_already = file->ev_queue.is_closed;
350 file->ev_queue.is_closed = 1;
351 list_for_each_entry_safe(entry, tmp, &file->ev_queue.event_list, list) {
353 list_del(&entry->obj_list);
356 spin_unlock_irq(&file->ev_queue.lock);
357 if (!closed_already) {
358 list_del(&file->list);
359 ib_unregister_event_handler(&uverbs_file->event_handler);
361 mutex_unlock(&uverbs_file->device->lists_mutex);
363 kref_put(&uverbs_file->ref, ib_uverbs_release_file);
364 kref_put(&file->ref, ib_uverbs_release_async_event_file);
369 static int ib_uverbs_comp_event_close(struct inode *inode, struct file *filp)
371 struct ib_uobject *uobj = filp->private_data;
372 struct ib_uverbs_completion_event_file *file = container_of(
373 uobj, struct ib_uverbs_completion_event_file, uobj);
374 struct ib_uverbs_event *entry, *tmp;
376 spin_lock_irq(&file->ev_queue.lock);
377 list_for_each_entry_safe(entry, tmp, &file->ev_queue.event_list, list) {
379 list_del(&entry->obj_list);
382 file->ev_queue.is_closed = 1;
383 spin_unlock_irq(&file->ev_queue.lock);
385 uverbs_close_fd(filp);
390 const struct file_operations uverbs_event_fops = {
391 .owner = THIS_MODULE,
392 .read = ib_uverbs_comp_event_read,
393 .poll = ib_uverbs_comp_event_poll,
394 .release = ib_uverbs_comp_event_close,
395 .fasync = ib_uverbs_comp_event_fasync,
399 static const struct file_operations uverbs_async_event_fops = {
400 .owner = THIS_MODULE,
401 .read = ib_uverbs_async_event_read,
402 .poll = ib_uverbs_async_event_poll,
403 .release = ib_uverbs_async_event_close,
404 .fasync = ib_uverbs_async_event_fasync,
408 void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context)
410 struct ib_uverbs_event_queue *ev_queue = cq_context;
411 struct ib_ucq_object *uobj;
412 struct ib_uverbs_event *entry;
418 spin_lock_irqsave(&ev_queue->lock, flags);
419 if (ev_queue->is_closed) {
420 spin_unlock_irqrestore(&ev_queue->lock, flags);
424 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
426 spin_unlock_irqrestore(&ev_queue->lock, flags);
430 uobj = container_of(cq->uobject, struct ib_ucq_object, uobject);
432 entry->desc.comp.cq_handle = cq->uobject->user_handle;
433 entry->counter = &uobj->comp_events_reported;
435 list_add_tail(&entry->list, &ev_queue->event_list);
436 list_add_tail(&entry->obj_list, &uobj->comp_list);
437 spin_unlock_irqrestore(&ev_queue->lock, flags);
439 wake_up_interruptible(&ev_queue->poll_wait);
440 kill_fasync(&ev_queue->async_queue, SIGIO, POLL_IN);
443 static void ib_uverbs_async_handler(struct ib_uverbs_file *file,
444 __u64 element, __u64 event,
445 struct list_head *obj_list,
448 struct ib_uverbs_event *entry;
451 spin_lock_irqsave(&file->async_file->ev_queue.lock, flags);
452 if (file->async_file->ev_queue.is_closed) {
453 spin_unlock_irqrestore(&file->async_file->ev_queue.lock, flags);
457 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
459 spin_unlock_irqrestore(&file->async_file->ev_queue.lock, flags);
463 entry->desc.async.element = element;
464 entry->desc.async.event_type = event;
465 entry->desc.async.reserved = 0;
466 entry->counter = counter;
468 list_add_tail(&entry->list, &file->async_file->ev_queue.event_list);
470 list_add_tail(&entry->obj_list, obj_list);
471 spin_unlock_irqrestore(&file->async_file->ev_queue.lock, flags);
473 wake_up_interruptible(&file->async_file->ev_queue.poll_wait);
474 kill_fasync(&file->async_file->ev_queue.async_queue, SIGIO, POLL_IN);
477 void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr)
479 struct ib_ucq_object *uobj = container_of(event->element.cq->uobject,
480 struct ib_ucq_object, uobject);
482 ib_uverbs_async_handler(uobj->uobject.ufile, uobj->uobject.user_handle,
483 event->event, &uobj->async_list,
484 &uobj->async_events_reported);
487 void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr)
489 struct ib_uevent_object *uobj;
491 /* for XRC target qp's, check that qp is live */
492 if (!event->element.qp->uobject)
495 uobj = container_of(event->element.qp->uobject,
496 struct ib_uevent_object, uobject);
498 ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
499 event->event, &uobj->event_list,
500 &uobj->events_reported);
503 void ib_uverbs_wq_event_handler(struct ib_event *event, void *context_ptr)
505 struct ib_uevent_object *uobj = container_of(event->element.wq->uobject,
506 struct ib_uevent_object, uobject);
508 ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
509 event->event, &uobj->event_list,
510 &uobj->events_reported);
513 void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr)
515 struct ib_uevent_object *uobj;
517 uobj = container_of(event->element.srq->uobject,
518 struct ib_uevent_object, uobject);
520 ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
521 event->event, &uobj->event_list,
522 &uobj->events_reported);
525 void ib_uverbs_event_handler(struct ib_event_handler *handler,
526 struct ib_event *event)
528 struct ib_uverbs_file *file =
529 container_of(handler, struct ib_uverbs_file, event_handler);
531 ib_uverbs_async_handler(file, event->element.port_num, event->event,
535 void ib_uverbs_free_async_event_file(struct ib_uverbs_file *file)
537 kref_put(&file->async_file->ref, ib_uverbs_release_async_event_file);
538 file->async_file = NULL;
541 void ib_uverbs_init_event_queue(struct ib_uverbs_event_queue *ev_queue)
543 spin_lock_init(&ev_queue->lock);
544 INIT_LIST_HEAD(&ev_queue->event_list);
545 init_waitqueue_head(&ev_queue->poll_wait);
546 ev_queue->is_closed = 0;
547 ev_queue->async_queue = NULL;
550 struct file *ib_uverbs_alloc_async_event_file(struct ib_uverbs_file *uverbs_file,
551 struct ib_device *ib_dev)
553 struct ib_uverbs_async_event_file *ev_file;
556 ev_file = kzalloc(sizeof(*ev_file), GFP_KERNEL);
558 return ERR_PTR(-ENOMEM);
560 ib_uverbs_init_event_queue(&ev_file->ev_queue);
561 ev_file->uverbs_file = uverbs_file;
562 kref_get(&ev_file->uverbs_file->ref);
563 kref_init(&ev_file->ref);
564 filp = anon_inode_getfile("[infinibandevent]", &uverbs_async_event_fops,
569 mutex_lock(&uverbs_file->device->lists_mutex);
570 list_add_tail(&ev_file->list,
571 &uverbs_file->device->uverbs_events_file_list);
572 mutex_unlock(&uverbs_file->device->lists_mutex);
574 WARN_ON(uverbs_file->async_file);
575 uverbs_file->async_file = ev_file;
576 kref_get(&uverbs_file->async_file->ref);
577 INIT_IB_EVENT_HANDLER(&uverbs_file->event_handler,
579 ib_uverbs_event_handler);
580 ib_register_event_handler(&uverbs_file->event_handler);
581 /* At that point async file stuff was fully set */
586 kref_put(&ev_file->uverbs_file->ref, ib_uverbs_release_file);
587 kref_put(&ev_file->ref, ib_uverbs_release_async_event_file);
591 static ssize_t verify_hdr(struct ib_uverbs_cmd_hdr *hdr,
592 struct ib_uverbs_ex_cmd_hdr *ex_hdr,
593 size_t count, bool extended)
596 count -= sizeof(*hdr) + sizeof(*ex_hdr);
598 if ((hdr->in_words + ex_hdr->provider_in_words) * 8 != count)
601 if (ex_hdr->cmd_hdr_reserved)
604 if (ex_hdr->response) {
605 if (!hdr->out_words && !ex_hdr->provider_out_words)
608 if (!access_ok(VERIFY_WRITE,
609 u64_to_user_ptr(ex_hdr->response),
610 (hdr->out_words + ex_hdr->provider_out_words) * 8))
613 if (hdr->out_words || ex_hdr->provider_out_words)
620 /* not extended command */
621 if (hdr->in_words * 4 != count)
627 static ssize_t ib_uverbs_write(struct file *filp, const char __user *buf,
628 size_t count, loff_t *pos)
630 struct ib_uverbs_file *file = filp->private_data;
631 const struct uverbs_api_write_method *method_elm;
632 struct uverbs_api *uapi = file->device->uapi;
633 struct ib_uverbs_ex_cmd_hdr ex_hdr;
634 struct ib_uverbs_cmd_hdr hdr;
638 if (!ib_safe_file_access(filp)) {
639 pr_err_once("uverbs_write: process %d (%s) changed security contexts after opening file descriptor, this is not allowed.\n",
640 task_tgid_vnr(current), current->comm);
644 if (count < sizeof(hdr))
647 if (copy_from_user(&hdr, buf, sizeof(hdr)))
650 method_elm = uapi_get_method(uapi, hdr.command);
651 if (IS_ERR(method_elm))
652 return PTR_ERR(method_elm);
654 if (method_elm->is_ex) {
655 if (count < (sizeof(hdr) + sizeof(ex_hdr)))
657 if (copy_from_user(&ex_hdr, buf + sizeof(hdr), sizeof(ex_hdr)))
661 ret = verify_hdr(&hdr, &ex_hdr, count, method_elm->is_ex);
665 srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
669 if (!method_elm->is_ex) {
670 ret = method_elm->handler(file, buf, hdr.in_words * 4,
673 struct ib_udata ucore;
676 buf += sizeof(ex_hdr);
678 ib_uverbs_init_udata_buf_or_null(&ucore, buf,
679 u64_to_user_ptr(ex_hdr.response),
680 hdr.in_words * 8, hdr.out_words * 8);
682 ib_uverbs_init_udata_buf_or_null(&uhw,
684 u64_to_user_ptr(ex_hdr.response) + ucore.outlen,
685 ex_hdr.provider_in_words * 8,
686 ex_hdr.provider_out_words * 8);
688 ret = method_elm->handler_ex(file, &ucore, &uhw);
689 ret = (ret) ? : count;
692 srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
696 static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma)
698 struct ib_uverbs_file *file = filp->private_data;
699 struct ib_ucontext *ucontext;
703 srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
704 ucontext = ib_uverbs_get_ucontext(file);
705 if (IS_ERR(ucontext)) {
706 ret = PTR_ERR(ucontext);
710 ret = ucontext->device->mmap(ucontext, vma);
712 srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
717 * Each time we map IO memory into user space this keeps track of the mapping.
718 * When the device is hot-unplugged we 'zap' the mmaps in user space to point
719 * to the zero page and allow the hot unplug to proceed.
721 * This is necessary for cases like PCI physical hot unplug as the actual BAR
722 * memory may vanish after this and access to it from userspace could MCE.
724 * RDMA drivers supporting disassociation must have their user space designed
725 * to cope in some way with their IO pages going to the zero page.
727 struct rdma_umap_priv {
728 struct vm_area_struct *vma;
729 struct list_head list;
732 static const struct vm_operations_struct rdma_umap_ops;
734 static void rdma_umap_priv_init(struct rdma_umap_priv *priv,
735 struct vm_area_struct *vma)
737 struct ib_uverbs_file *ufile = vma->vm_file->private_data;
740 vma->vm_private_data = priv;
741 vma->vm_ops = &rdma_umap_ops;
743 mutex_lock(&ufile->umap_lock);
744 list_add(&priv->list, &ufile->umaps);
745 mutex_unlock(&ufile->umap_lock);
749 * The VMA has been dup'd, initialize the vm_private_data with a new tracking
752 static void rdma_umap_open(struct vm_area_struct *vma)
754 struct ib_uverbs_file *ufile = vma->vm_file->private_data;
755 struct rdma_umap_priv *opriv = vma->vm_private_data;
756 struct rdma_umap_priv *priv;
761 /* We are racing with disassociation */
762 if (!down_read_trylock(&ufile->hw_destroy_rwsem))
765 * Disassociation already completed, the VMA should already be zapped.
767 if (!ufile->ucontext)
770 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
773 rdma_umap_priv_init(priv, vma);
775 up_read(&ufile->hw_destroy_rwsem);
779 up_read(&ufile->hw_destroy_rwsem);
782 * We can't allow the VMA to be created with the actual IO pages, that
783 * would break our API contract, and it can't be stopped at this
786 vma->vm_private_data = NULL;
787 zap_vma_ptes(vma, vma->vm_start, vma->vm_end - vma->vm_start);
790 static void rdma_umap_close(struct vm_area_struct *vma)
792 struct ib_uverbs_file *ufile = vma->vm_file->private_data;
793 struct rdma_umap_priv *priv = vma->vm_private_data;
799 * The vma holds a reference on the struct file that created it, which
800 * in turn means that the ib_uverbs_file is guaranteed to exist at
803 mutex_lock(&ufile->umap_lock);
804 list_del(&priv->list);
805 mutex_unlock(&ufile->umap_lock);
809 static const struct vm_operations_struct rdma_umap_ops = {
810 .open = rdma_umap_open,
811 .close = rdma_umap_close,
814 static struct rdma_umap_priv *rdma_user_mmap_pre(struct ib_ucontext *ucontext,
815 struct vm_area_struct *vma,
818 struct ib_uverbs_file *ufile = ucontext->ufile;
819 struct rdma_umap_priv *priv;
821 if (vma->vm_end - vma->vm_start != size)
822 return ERR_PTR(-EINVAL);
824 /* Driver is using this wrong, must be called by ib_uverbs_mmap */
825 if (WARN_ON(!vma->vm_file ||
826 vma->vm_file->private_data != ufile))
827 return ERR_PTR(-EINVAL);
828 lockdep_assert_held(&ufile->device->disassociate_srcu);
830 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
832 return ERR_PTR(-ENOMEM);
837 * Map IO memory into a process. This is to be called by drivers as part of
838 * their mmap() functions if they wish to send something like PCI-E BAR memory
841 int rdma_user_mmap_io(struct ib_ucontext *ucontext, struct vm_area_struct *vma,
842 unsigned long pfn, unsigned long size, pgprot_t prot)
844 struct rdma_umap_priv *priv = rdma_user_mmap_pre(ucontext, vma, size);
847 return PTR_ERR(priv);
849 vma->vm_page_prot = prot;
850 if (io_remap_pfn_range(vma, vma->vm_start, pfn, size, prot)) {
855 rdma_umap_priv_init(priv, vma);
858 EXPORT_SYMBOL(rdma_user_mmap_io);
861 * The page case is here for a slightly different reason, the driver expects
862 * to be able to free the page it is sharing to user space when it destroys
863 * its ucontext, which means we need to zap the user space references.
865 * We could handle this differently by providing an API to allocate a shared
866 * page and then only freeing the shared page when the last ufile is
869 int rdma_user_mmap_page(struct ib_ucontext *ucontext,
870 struct vm_area_struct *vma, struct page *page,
873 struct rdma_umap_priv *priv = rdma_user_mmap_pre(ucontext, vma, size);
876 return PTR_ERR(priv);
878 if (remap_pfn_range(vma, vma->vm_start, page_to_pfn(page), size,
879 vma->vm_page_prot)) {
884 rdma_umap_priv_init(priv, vma);
887 EXPORT_SYMBOL(rdma_user_mmap_page);
889 void uverbs_user_mmap_disassociate(struct ib_uverbs_file *ufile)
891 struct rdma_umap_priv *priv, *next_priv;
893 lockdep_assert_held(&ufile->hw_destroy_rwsem);
896 struct mm_struct *mm = NULL;
898 /* Get an arbitrary mm pointer that hasn't been cleaned yet */
899 mutex_lock(&ufile->umap_lock);
900 if (!list_empty(&ufile->umaps)) {
901 mm = list_first_entry(&ufile->umaps,
902 struct rdma_umap_priv, list)
906 mutex_unlock(&ufile->umap_lock);
911 * The umap_lock is nested under mmap_sem since it used within
912 * the vma_ops callbacks, so we have to clean the list one mm
913 * at a time to get the lock ordering right. Typically there
914 * will only be one mm, so no big deal.
916 down_write(&mm->mmap_sem);
917 mutex_lock(&ufile->umap_lock);
918 list_for_each_entry_safe (priv, next_priv, &ufile->umaps,
920 struct vm_area_struct *vma = priv->vma;
922 if (vma->vm_mm != mm)
924 list_del_init(&priv->list);
926 zap_vma_ptes(vma, vma->vm_start,
927 vma->vm_end - vma->vm_start);
928 vma->vm_flags &= ~(VM_SHARED | VM_MAYSHARE);
930 mutex_unlock(&ufile->umap_lock);
931 up_write(&mm->mmap_sem);
937 * ib_uverbs_open() does not need the BKL:
939 * - the ib_uverbs_device structures are properly reference counted and
940 * everything else is purely local to the file being created, so
941 * races against other open calls are not a problem;
942 * - there is no ioctl method to race against;
943 * - the open method will either immediately run -ENXIO, or all
944 * required initialization will be done.
946 static int ib_uverbs_open(struct inode *inode, struct file *filp)
948 struct ib_uverbs_device *dev;
949 struct ib_uverbs_file *file;
950 struct ib_device *ib_dev;
952 int module_dependent;
955 dev = container_of(inode->i_cdev, struct ib_uverbs_device, cdev);
956 if (!atomic_inc_not_zero(&dev->refcount))
959 get_device(&dev->dev);
960 srcu_key = srcu_read_lock(&dev->disassociate_srcu);
961 mutex_lock(&dev->lists_mutex);
962 ib_dev = srcu_dereference(dev->ib_dev,
963 &dev->disassociate_srcu);
969 /* In case IB device supports disassociate ucontext, there is no hard
970 * dependency between uverbs device and its low level device.
972 module_dependent = !(ib_dev->disassociate_ucontext);
974 if (module_dependent) {
975 if (!try_module_get(ib_dev->owner)) {
981 file = kzalloc(sizeof(*file), GFP_KERNEL);
984 if (module_dependent)
991 kref_init(&file->ref);
992 mutex_init(&file->ucontext_lock);
994 spin_lock_init(&file->uobjects_lock);
995 INIT_LIST_HEAD(&file->uobjects);
996 init_rwsem(&file->hw_destroy_rwsem);
997 mutex_init(&file->umap_lock);
998 INIT_LIST_HEAD(&file->umaps);
1000 filp->private_data = file;
1001 list_add_tail(&file->list, &dev->uverbs_file_list);
1002 mutex_unlock(&dev->lists_mutex);
1003 srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
1005 setup_ufile_idr_uobject(file);
1007 return nonseekable_open(inode, filp);
1010 module_put(ib_dev->owner);
1013 mutex_unlock(&dev->lists_mutex);
1014 srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
1015 if (atomic_dec_and_test(&dev->refcount))
1016 ib_uverbs_comp_dev(dev);
1018 put_device(&dev->dev);
1022 static int ib_uverbs_close(struct inode *inode, struct file *filp)
1024 struct ib_uverbs_file *file = filp->private_data;
1026 uverbs_destroy_ufile_hw(file, RDMA_REMOVE_CLOSE);
1028 mutex_lock(&file->device->lists_mutex);
1029 list_del_init(&file->list);
1030 mutex_unlock(&file->device->lists_mutex);
1032 if (file->async_file)
1033 kref_put(&file->async_file->ref,
1034 ib_uverbs_release_async_event_file);
1036 kref_put(&file->ref, ib_uverbs_release_file);
1041 static const struct file_operations uverbs_fops = {
1042 .owner = THIS_MODULE,
1043 .write = ib_uverbs_write,
1044 .open = ib_uverbs_open,
1045 .release = ib_uverbs_close,
1046 .llseek = no_llseek,
1047 .unlocked_ioctl = ib_uverbs_ioctl,
1048 .compat_ioctl = ib_uverbs_ioctl,
1051 static const struct file_operations uverbs_mmap_fops = {
1052 .owner = THIS_MODULE,
1053 .write = ib_uverbs_write,
1054 .mmap = ib_uverbs_mmap,
1055 .open = ib_uverbs_open,
1056 .release = ib_uverbs_close,
1057 .llseek = no_llseek,
1058 .unlocked_ioctl = ib_uverbs_ioctl,
1059 .compat_ioctl = ib_uverbs_ioctl,
1062 static struct ib_client uverbs_client = {
1064 .add = ib_uverbs_add_one,
1065 .remove = ib_uverbs_remove_one
1068 static ssize_t ibdev_show(struct device *device, struct device_attribute *attr,
1071 struct ib_uverbs_device *dev =
1072 container_of(device, struct ib_uverbs_device, dev);
1075 struct ib_device *ib_dev;
1077 srcu_key = srcu_read_lock(&dev->disassociate_srcu);
1078 ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
1080 ret = sprintf(buf, "%s\n", dev_name(&ib_dev->dev));
1081 srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
1085 static DEVICE_ATTR_RO(ibdev);
1087 static ssize_t abi_version_show(struct device *device,
1088 struct device_attribute *attr, char *buf)
1090 struct ib_uverbs_device *dev =
1091 container_of(device, struct ib_uverbs_device, dev);
1094 struct ib_device *ib_dev;
1096 srcu_key = srcu_read_lock(&dev->disassociate_srcu);
1097 ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
1099 ret = sprintf(buf, "%d\n", ib_dev->uverbs_abi_ver);
1100 srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
1104 static DEVICE_ATTR_RO(abi_version);
1106 static struct attribute *ib_dev_attrs[] = {
1107 &dev_attr_abi_version.attr,
1108 &dev_attr_ibdev.attr,
1112 static const struct attribute_group dev_attr_group = {
1113 .attrs = ib_dev_attrs,
1116 static CLASS_ATTR_STRING(abi_version, S_IRUGO,
1117 __stringify(IB_USER_VERBS_ABI_VERSION));
1119 static int ib_uverbs_create_uapi(struct ib_device *device,
1120 struct ib_uverbs_device *uverbs_dev)
1122 struct uverbs_api *uapi;
1124 uapi = uverbs_alloc_api(device);
1126 return PTR_ERR(uapi);
1128 uverbs_dev->uapi = uapi;
1132 static void ib_uverbs_add_one(struct ib_device *device)
1136 struct ib_uverbs_device *uverbs_dev;
1139 if (!device->alloc_ucontext)
1142 uverbs_dev = kzalloc(sizeof(*uverbs_dev), GFP_KERNEL);
1146 ret = init_srcu_struct(&uverbs_dev->disassociate_srcu);
1152 device_initialize(&uverbs_dev->dev);
1153 uverbs_dev->dev.class = uverbs_class;
1154 uverbs_dev->dev.parent = device->dev.parent;
1155 uverbs_dev->dev.release = ib_uverbs_release_dev;
1156 uverbs_dev->groups[0] = &dev_attr_group;
1157 uverbs_dev->dev.groups = uverbs_dev->groups;
1158 atomic_set(&uverbs_dev->refcount, 1);
1159 init_completion(&uverbs_dev->comp);
1160 uverbs_dev->xrcd_tree = RB_ROOT;
1161 mutex_init(&uverbs_dev->xrcd_tree_mutex);
1162 mutex_init(&uverbs_dev->lists_mutex);
1163 INIT_LIST_HEAD(&uverbs_dev->uverbs_file_list);
1164 INIT_LIST_HEAD(&uverbs_dev->uverbs_events_file_list);
1165 rcu_assign_pointer(uverbs_dev->ib_dev, device);
1166 uverbs_dev->num_comp_vectors = device->num_comp_vectors;
1168 devnum = ida_alloc_max(&uverbs_ida, IB_UVERBS_MAX_DEVICES - 1,
1172 uverbs_dev->devnum = devnum;
1173 if (devnum >= IB_UVERBS_NUM_FIXED_MINOR)
1174 base = dynamic_uverbs_dev + devnum - IB_UVERBS_NUM_FIXED_MINOR;
1176 base = IB_UVERBS_BASE_DEV + devnum;
1178 if (ib_uverbs_create_uapi(device, uverbs_dev))
1181 uverbs_dev->dev.devt = base;
1182 dev_set_name(&uverbs_dev->dev, "uverbs%d", uverbs_dev->devnum);
1184 cdev_init(&uverbs_dev->cdev,
1185 device->mmap ? &uverbs_mmap_fops : &uverbs_fops);
1186 uverbs_dev->cdev.owner = THIS_MODULE;
1188 ret = cdev_device_add(&uverbs_dev->cdev, &uverbs_dev->dev);
1192 ib_set_client_data(device, &uverbs_client, uverbs_dev);
1196 ida_free(&uverbs_ida, devnum);
1198 if (atomic_dec_and_test(&uverbs_dev->refcount))
1199 ib_uverbs_comp_dev(uverbs_dev);
1200 wait_for_completion(&uverbs_dev->comp);
1201 put_device(&uverbs_dev->dev);
1205 static void ib_uverbs_free_hw_resources(struct ib_uverbs_device *uverbs_dev,
1206 struct ib_device *ib_dev)
1208 struct ib_uverbs_file *file;
1209 struct ib_uverbs_async_event_file *event_file;
1210 struct ib_event event;
1212 /* Pending running commands to terminate */
1213 uverbs_disassociate_api_pre(uverbs_dev);
1214 event.event = IB_EVENT_DEVICE_FATAL;
1215 event.element.port_num = 0;
1216 event.device = ib_dev;
1218 mutex_lock(&uverbs_dev->lists_mutex);
1219 while (!list_empty(&uverbs_dev->uverbs_file_list)) {
1220 file = list_first_entry(&uverbs_dev->uverbs_file_list,
1221 struct ib_uverbs_file, list);
1222 list_del_init(&file->list);
1223 kref_get(&file->ref);
1225 /* We must release the mutex before going ahead and calling
1226 * uverbs_cleanup_ufile, as it might end up indirectly calling
1227 * uverbs_close, for example due to freeing the resources (e.g
1230 mutex_unlock(&uverbs_dev->lists_mutex);
1232 ib_uverbs_event_handler(&file->event_handler, &event);
1233 uverbs_destroy_ufile_hw(file, RDMA_REMOVE_DRIVER_REMOVE);
1234 kref_put(&file->ref, ib_uverbs_release_file);
1236 mutex_lock(&uverbs_dev->lists_mutex);
1239 while (!list_empty(&uverbs_dev->uverbs_events_file_list)) {
1240 event_file = list_first_entry(&uverbs_dev->
1241 uverbs_events_file_list,
1242 struct ib_uverbs_async_event_file,
1244 spin_lock_irq(&event_file->ev_queue.lock);
1245 event_file->ev_queue.is_closed = 1;
1246 spin_unlock_irq(&event_file->ev_queue.lock);
1248 list_del(&event_file->list);
1249 ib_unregister_event_handler(
1250 &event_file->uverbs_file->event_handler);
1251 event_file->uverbs_file->event_handler.device =
1254 wake_up_interruptible(&event_file->ev_queue.poll_wait);
1255 kill_fasync(&event_file->ev_queue.async_queue, SIGIO, POLL_IN);
1257 mutex_unlock(&uverbs_dev->lists_mutex);
1259 uverbs_disassociate_api(uverbs_dev->uapi);
1262 static void ib_uverbs_remove_one(struct ib_device *device, void *client_data)
1264 struct ib_uverbs_device *uverbs_dev = client_data;
1265 int wait_clients = 1;
1270 cdev_device_del(&uverbs_dev->cdev, &uverbs_dev->dev);
1271 ida_free(&uverbs_ida, uverbs_dev->devnum);
1273 if (device->disassociate_ucontext) {
1274 /* We disassociate HW resources and immediately return.
1275 * Userspace will see a EIO errno for all future access.
1276 * Upon returning, ib_device may be freed internally and is not
1278 * uverbs_device is still available until all clients close
1279 * their files, then the uverbs device ref count will be zero
1280 * and its resources will be freed.
1281 * Note: At this point no more files can be opened since the
1282 * cdev was deleted, however active clients can still issue
1283 * commands and close their open files.
1285 ib_uverbs_free_hw_resources(uverbs_dev, device);
1289 if (atomic_dec_and_test(&uverbs_dev->refcount))
1290 ib_uverbs_comp_dev(uverbs_dev);
1292 wait_for_completion(&uverbs_dev->comp);
1294 put_device(&uverbs_dev->dev);
1297 static char *uverbs_devnode(struct device *dev, umode_t *mode)
1301 return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
1304 static int __init ib_uverbs_init(void)
1308 ret = register_chrdev_region(IB_UVERBS_BASE_DEV,
1309 IB_UVERBS_NUM_FIXED_MINOR,
1310 "infiniband_verbs");
1312 pr_err("user_verbs: couldn't register device number\n");
1316 ret = alloc_chrdev_region(&dynamic_uverbs_dev, 0,
1317 IB_UVERBS_NUM_DYNAMIC_MINOR,
1318 "infiniband_verbs");
1320 pr_err("couldn't register dynamic device number\n");
1324 uverbs_class = class_create(THIS_MODULE, "infiniband_verbs");
1325 if (IS_ERR(uverbs_class)) {
1326 ret = PTR_ERR(uverbs_class);
1327 pr_err("user_verbs: couldn't create class infiniband_verbs\n");
1331 uverbs_class->devnode = uverbs_devnode;
1333 ret = class_create_file(uverbs_class, &class_attr_abi_version.attr);
1335 pr_err("user_verbs: couldn't create abi_version attribute\n");
1339 ret = ib_register_client(&uverbs_client);
1341 pr_err("user_verbs: couldn't register client\n");
1348 class_destroy(uverbs_class);
1351 unregister_chrdev_region(dynamic_uverbs_dev,
1352 IB_UVERBS_NUM_DYNAMIC_MINOR);
1355 unregister_chrdev_region(IB_UVERBS_BASE_DEV,
1356 IB_UVERBS_NUM_FIXED_MINOR);
1362 static void __exit ib_uverbs_cleanup(void)
1364 ib_unregister_client(&uverbs_client);
1365 class_destroy(uverbs_class);
1366 unregister_chrdev_region(IB_UVERBS_BASE_DEV,
1367 IB_UVERBS_NUM_FIXED_MINOR);
1368 unregister_chrdev_region(dynamic_uverbs_dev,
1369 IB_UVERBS_NUM_DYNAMIC_MINOR);
1372 module_init(ib_uverbs_init);
1373 module_exit(ib_uverbs_cleanup);