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>
54 #include <rdma/rdma_netlink.h>
57 #include "core_priv.h"
58 #include "rdma_core.h"
60 MODULE_AUTHOR("Roland Dreier");
61 MODULE_DESCRIPTION("InfiniBand userspace verbs access");
62 MODULE_LICENSE("Dual BSD/GPL");
65 IB_UVERBS_MAJOR = 231,
66 IB_UVERBS_BASE_MINOR = 192,
67 IB_UVERBS_MAX_DEVICES = RDMA_MAX_PORTS,
68 IB_UVERBS_NUM_FIXED_MINOR = 32,
69 IB_UVERBS_NUM_DYNAMIC_MINOR = IB_UVERBS_MAX_DEVICES - IB_UVERBS_NUM_FIXED_MINOR,
72 #define IB_UVERBS_BASE_DEV MKDEV(IB_UVERBS_MAJOR, IB_UVERBS_BASE_MINOR)
74 static dev_t dynamic_uverbs_dev;
75 static struct class *uverbs_class;
77 static DEFINE_IDA(uverbs_ida);
78 static void ib_uverbs_add_one(struct ib_device *device);
79 static void ib_uverbs_remove_one(struct ib_device *device, void *client_data);
82 * Must be called with the ufile->device->disassociate_srcu held, and the lock
83 * must be held until use of the ucontext is finished.
85 struct ib_ucontext *ib_uverbs_get_ucontext_file(struct ib_uverbs_file *ufile)
88 * We do not hold the hw_destroy_rwsem lock for this flow, instead
89 * srcu is used. It does not matter if someone races this with
90 * get_context, we get NULL or valid ucontext.
92 struct ib_ucontext *ucontext = smp_load_acquire(&ufile->ucontext);
94 if (!srcu_dereference(ufile->device->ib_dev,
95 &ufile->device->disassociate_srcu))
99 return ERR_PTR(-EINVAL);
103 EXPORT_SYMBOL(ib_uverbs_get_ucontext_file);
105 int uverbs_dealloc_mw(struct ib_mw *mw)
107 struct ib_pd *pd = mw->pd;
110 ret = mw->device->ops.dealloc_mw(mw);
112 atomic_dec(&pd->usecnt);
116 static void ib_uverbs_release_dev(struct device *device)
118 struct ib_uverbs_device *dev =
119 container_of(device, struct ib_uverbs_device, dev);
121 uverbs_destroy_api(dev->uapi);
122 cleanup_srcu_struct(&dev->disassociate_srcu);
123 mutex_destroy(&dev->lists_mutex);
124 mutex_destroy(&dev->xrcd_tree_mutex);
128 static void ib_uverbs_release_async_event_file(struct kref *ref)
130 struct ib_uverbs_async_event_file *file =
131 container_of(ref, struct ib_uverbs_async_event_file, ref);
136 void ib_uverbs_release_ucq(struct ib_uverbs_file *file,
137 struct ib_uverbs_completion_event_file *ev_file,
138 struct ib_ucq_object *uobj)
140 struct ib_uverbs_event *evt, *tmp;
143 spin_lock_irq(&ev_file->ev_queue.lock);
144 list_for_each_entry_safe(evt, tmp, &uobj->comp_list, obj_list) {
145 list_del(&evt->list);
148 spin_unlock_irq(&ev_file->ev_queue.lock);
150 uverbs_uobject_put(&ev_file->uobj);
153 spin_lock_irq(&file->async_file->ev_queue.lock);
154 list_for_each_entry_safe(evt, tmp, &uobj->async_list, obj_list) {
155 list_del(&evt->list);
158 spin_unlock_irq(&file->async_file->ev_queue.lock);
161 void ib_uverbs_release_uevent(struct ib_uverbs_file *file,
162 struct ib_uevent_object *uobj)
164 struct ib_uverbs_event *evt, *tmp;
166 spin_lock_irq(&file->async_file->ev_queue.lock);
167 list_for_each_entry_safe(evt, tmp, &uobj->event_list, obj_list) {
168 list_del(&evt->list);
171 spin_unlock_irq(&file->async_file->ev_queue.lock);
174 void ib_uverbs_detach_umcast(struct ib_qp *qp,
175 struct ib_uqp_object *uobj)
177 struct ib_uverbs_mcast_entry *mcast, *tmp;
179 list_for_each_entry_safe(mcast, tmp, &uobj->mcast_list, list) {
180 ib_detach_mcast(qp, &mcast->gid, mcast->lid);
181 list_del(&mcast->list);
186 static void ib_uverbs_comp_dev(struct ib_uverbs_device *dev)
188 complete(&dev->comp);
191 void ib_uverbs_release_file(struct kref *ref)
193 struct ib_uverbs_file *file =
194 container_of(ref, struct ib_uverbs_file, ref);
195 struct ib_device *ib_dev;
198 release_ufile_idr_uobject(file);
200 srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
201 ib_dev = srcu_dereference(file->device->ib_dev,
202 &file->device->disassociate_srcu);
203 if (ib_dev && !ib_dev->ops.disassociate_ucontext)
204 module_put(ib_dev->ops.owner);
205 srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
207 if (atomic_dec_and_test(&file->device->refcount))
208 ib_uverbs_comp_dev(file->device);
210 if (file->async_file)
211 kref_put(&file->async_file->ref,
212 ib_uverbs_release_async_event_file);
213 put_device(&file->device->dev);
215 if (file->disassociate_page)
216 __free_pages(file->disassociate_page, 0);
217 mutex_destroy(&file->umap_lock);
218 mutex_destroy(&file->ucontext_lock);
222 static ssize_t ib_uverbs_event_read(struct ib_uverbs_event_queue *ev_queue,
223 struct file *filp, char __user *buf,
224 size_t count, loff_t *pos,
227 struct ib_uverbs_event *event;
230 spin_lock_irq(&ev_queue->lock);
232 while (list_empty(&ev_queue->event_list)) {
233 spin_unlock_irq(&ev_queue->lock);
235 if (filp->f_flags & O_NONBLOCK)
238 if (wait_event_interruptible(ev_queue->poll_wait,
239 (!list_empty(&ev_queue->event_list) ||
240 ev_queue->is_closed)))
243 spin_lock_irq(&ev_queue->lock);
245 /* If device was disassociated and no event exists set an error */
246 if (list_empty(&ev_queue->event_list) && ev_queue->is_closed) {
247 spin_unlock_irq(&ev_queue->lock);
252 event = list_entry(ev_queue->event_list.next, struct ib_uverbs_event, list);
254 if (eventsz > count) {
258 list_del(ev_queue->event_list.next);
259 if (event->counter) {
261 list_del(&event->obj_list);
265 spin_unlock_irq(&ev_queue->lock);
268 if (copy_to_user(buf, event, eventsz))
279 static ssize_t ib_uverbs_async_event_read(struct file *filp, char __user *buf,
280 size_t count, loff_t *pos)
282 struct ib_uverbs_async_event_file *file = filp->private_data;
284 return ib_uverbs_event_read(&file->ev_queue, filp, buf, count, pos,
285 sizeof(struct ib_uverbs_async_event_desc));
288 static ssize_t ib_uverbs_comp_event_read(struct file *filp, char __user *buf,
289 size_t count, loff_t *pos)
291 struct ib_uverbs_completion_event_file *comp_ev_file =
294 return ib_uverbs_event_read(&comp_ev_file->ev_queue, filp, buf, count,
296 sizeof(struct ib_uverbs_comp_event_desc));
299 static __poll_t ib_uverbs_event_poll(struct ib_uverbs_event_queue *ev_queue,
301 struct poll_table_struct *wait)
303 __poll_t pollflags = 0;
305 poll_wait(filp, &ev_queue->poll_wait, wait);
307 spin_lock_irq(&ev_queue->lock);
308 if (!list_empty(&ev_queue->event_list))
309 pollflags = EPOLLIN | EPOLLRDNORM;
310 spin_unlock_irq(&ev_queue->lock);
315 static __poll_t ib_uverbs_async_event_poll(struct file *filp,
316 struct poll_table_struct *wait)
318 struct ib_uverbs_async_event_file *file = filp->private_data;
320 return ib_uverbs_event_poll(&file->ev_queue, filp, wait);
323 static __poll_t ib_uverbs_comp_event_poll(struct file *filp,
324 struct poll_table_struct *wait)
326 struct ib_uverbs_completion_event_file *comp_ev_file =
329 return ib_uverbs_event_poll(&comp_ev_file->ev_queue, filp, wait);
332 static int ib_uverbs_async_event_fasync(int fd, struct file *filp, int on)
334 struct ib_uverbs_async_event_file *file = filp->private_data;
336 return fasync_helper(fd, filp, on, &file->ev_queue.async_queue);
339 static int ib_uverbs_comp_event_fasync(int fd, struct file *filp, int on)
341 struct ib_uverbs_completion_event_file *comp_ev_file =
344 return fasync_helper(fd, filp, on, &comp_ev_file->ev_queue.async_queue);
347 static int ib_uverbs_async_event_close(struct inode *inode, struct file *filp)
349 struct ib_uverbs_async_event_file *file = filp->private_data;
350 struct ib_uverbs_file *uverbs_file = file->uverbs_file;
351 struct ib_uverbs_event *entry, *tmp;
352 int closed_already = 0;
354 mutex_lock(&uverbs_file->device->lists_mutex);
355 spin_lock_irq(&file->ev_queue.lock);
356 closed_already = file->ev_queue.is_closed;
357 file->ev_queue.is_closed = 1;
358 list_for_each_entry_safe(entry, tmp, &file->ev_queue.event_list, list) {
360 list_del(&entry->obj_list);
363 spin_unlock_irq(&file->ev_queue.lock);
364 if (!closed_already) {
365 list_del(&file->list);
366 ib_unregister_event_handler(&uverbs_file->event_handler);
368 mutex_unlock(&uverbs_file->device->lists_mutex);
370 kref_put(&uverbs_file->ref, ib_uverbs_release_file);
371 kref_put(&file->ref, ib_uverbs_release_async_event_file);
376 const struct file_operations uverbs_event_fops = {
377 .owner = THIS_MODULE,
378 .read = ib_uverbs_comp_event_read,
379 .poll = ib_uverbs_comp_event_poll,
380 .release = uverbs_uobject_fd_release,
381 .fasync = ib_uverbs_comp_event_fasync,
385 static const struct file_operations uverbs_async_event_fops = {
386 .owner = THIS_MODULE,
387 .read = ib_uverbs_async_event_read,
388 .poll = ib_uverbs_async_event_poll,
389 .release = ib_uverbs_async_event_close,
390 .fasync = ib_uverbs_async_event_fasync,
394 void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context)
396 struct ib_uverbs_event_queue *ev_queue = cq_context;
397 struct ib_ucq_object *uobj;
398 struct ib_uverbs_event *entry;
404 spin_lock_irqsave(&ev_queue->lock, flags);
405 if (ev_queue->is_closed) {
406 spin_unlock_irqrestore(&ev_queue->lock, flags);
410 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
412 spin_unlock_irqrestore(&ev_queue->lock, flags);
416 uobj = container_of(cq->uobject, struct ib_ucq_object, uobject);
418 entry->desc.comp.cq_handle = cq->uobject->user_handle;
419 entry->counter = &uobj->comp_events_reported;
421 list_add_tail(&entry->list, &ev_queue->event_list);
422 list_add_tail(&entry->obj_list, &uobj->comp_list);
423 spin_unlock_irqrestore(&ev_queue->lock, flags);
425 wake_up_interruptible(&ev_queue->poll_wait);
426 kill_fasync(&ev_queue->async_queue, SIGIO, POLL_IN);
429 static void ib_uverbs_async_handler(struct ib_uverbs_file *file,
430 __u64 element, __u64 event,
431 struct list_head *obj_list,
434 struct ib_uverbs_event *entry;
437 spin_lock_irqsave(&file->async_file->ev_queue.lock, flags);
438 if (file->async_file->ev_queue.is_closed) {
439 spin_unlock_irqrestore(&file->async_file->ev_queue.lock, flags);
443 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
445 spin_unlock_irqrestore(&file->async_file->ev_queue.lock, flags);
449 entry->desc.async.element = element;
450 entry->desc.async.event_type = event;
451 entry->desc.async.reserved = 0;
452 entry->counter = counter;
454 list_add_tail(&entry->list, &file->async_file->ev_queue.event_list);
456 list_add_tail(&entry->obj_list, obj_list);
457 spin_unlock_irqrestore(&file->async_file->ev_queue.lock, flags);
459 wake_up_interruptible(&file->async_file->ev_queue.poll_wait);
460 kill_fasync(&file->async_file->ev_queue.async_queue, SIGIO, POLL_IN);
463 void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr)
465 struct ib_ucq_object *uobj = container_of(event->element.cq->uobject,
466 struct ib_ucq_object, uobject);
468 ib_uverbs_async_handler(uobj->uobject.ufile, uobj->uobject.user_handle,
469 event->event, &uobj->async_list,
470 &uobj->async_events_reported);
473 void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr)
475 struct ib_uevent_object *uobj;
477 /* for XRC target qp's, check that qp is live */
478 if (!event->element.qp->uobject)
481 uobj = container_of(event->element.qp->uobject,
482 struct ib_uevent_object, uobject);
484 ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
485 event->event, &uobj->event_list,
486 &uobj->events_reported);
489 void ib_uverbs_wq_event_handler(struct ib_event *event, void *context_ptr)
491 struct ib_uevent_object *uobj = container_of(event->element.wq->uobject,
492 struct ib_uevent_object, uobject);
494 ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
495 event->event, &uobj->event_list,
496 &uobj->events_reported);
499 void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr)
501 struct ib_uevent_object *uobj;
503 uobj = container_of(event->element.srq->uobject,
504 struct ib_uevent_object, uobject);
506 ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
507 event->event, &uobj->event_list,
508 &uobj->events_reported);
511 void ib_uverbs_event_handler(struct ib_event_handler *handler,
512 struct ib_event *event)
514 struct ib_uverbs_file *file =
515 container_of(handler, struct ib_uverbs_file, event_handler);
517 ib_uverbs_async_handler(file, event->element.port_num, event->event,
521 void ib_uverbs_free_async_event_file(struct ib_uverbs_file *file)
523 kref_put(&file->async_file->ref, ib_uverbs_release_async_event_file);
524 file->async_file = NULL;
527 void ib_uverbs_init_event_queue(struct ib_uverbs_event_queue *ev_queue)
529 spin_lock_init(&ev_queue->lock);
530 INIT_LIST_HEAD(&ev_queue->event_list);
531 init_waitqueue_head(&ev_queue->poll_wait);
532 ev_queue->is_closed = 0;
533 ev_queue->async_queue = NULL;
536 struct file *ib_uverbs_alloc_async_event_file(struct ib_uverbs_file *uverbs_file,
537 struct ib_device *ib_dev)
539 struct ib_uverbs_async_event_file *ev_file;
542 ev_file = kzalloc(sizeof(*ev_file), GFP_KERNEL);
544 return ERR_PTR(-ENOMEM);
546 ib_uverbs_init_event_queue(&ev_file->ev_queue);
547 ev_file->uverbs_file = uverbs_file;
548 kref_get(&ev_file->uverbs_file->ref);
549 kref_init(&ev_file->ref);
550 filp = anon_inode_getfile("[infinibandevent]", &uverbs_async_event_fops,
555 mutex_lock(&uverbs_file->device->lists_mutex);
556 list_add_tail(&ev_file->list,
557 &uverbs_file->device->uverbs_events_file_list);
558 mutex_unlock(&uverbs_file->device->lists_mutex);
560 WARN_ON(uverbs_file->async_file);
561 uverbs_file->async_file = ev_file;
562 kref_get(&uverbs_file->async_file->ref);
563 INIT_IB_EVENT_HANDLER(&uverbs_file->event_handler,
565 ib_uverbs_event_handler);
566 ib_register_event_handler(&uverbs_file->event_handler);
567 /* At that point async file stuff was fully set */
572 kref_put(&ev_file->uverbs_file->ref, ib_uverbs_release_file);
573 kref_put(&ev_file->ref, ib_uverbs_release_async_event_file);
577 static ssize_t verify_hdr(struct ib_uverbs_cmd_hdr *hdr,
578 struct ib_uverbs_ex_cmd_hdr *ex_hdr, size_t count,
579 const struct uverbs_api_write_method *method_elm)
581 if (method_elm->is_ex) {
582 count -= sizeof(*hdr) + sizeof(*ex_hdr);
584 if ((hdr->in_words + ex_hdr->provider_in_words) * 8 != count)
587 if (hdr->in_words * 8 < method_elm->req_size)
590 if (ex_hdr->cmd_hdr_reserved)
593 if (ex_hdr->response) {
594 if (!hdr->out_words && !ex_hdr->provider_out_words)
597 if (hdr->out_words * 8 < method_elm->resp_size)
600 if (!access_ok(u64_to_user_ptr(ex_hdr->response),
601 (hdr->out_words + ex_hdr->provider_out_words) * 8))
604 if (hdr->out_words || ex_hdr->provider_out_words)
611 /* not extended command */
612 if (hdr->in_words * 4 != count)
615 if (count < method_elm->req_size + sizeof(hdr)) {
617 * rdma-core v18 and v19 have a bug where they send DESTROY_CQ
618 * with a 16 byte write instead of 24. Old kernels didn't
619 * check the size so they allowed this. Now that the size is
620 * checked provide a compatibility work around to not break
623 if (hdr->command == IB_USER_VERBS_CMD_DESTROY_CQ &&
630 if (hdr->out_words * 4 < method_elm->resp_size)
636 static ssize_t ib_uverbs_write(struct file *filp, const char __user *buf,
637 size_t count, loff_t *pos)
639 struct ib_uverbs_file *file = filp->private_data;
640 const struct uverbs_api_write_method *method_elm;
641 struct uverbs_api *uapi = file->device->uapi;
642 struct ib_uverbs_ex_cmd_hdr ex_hdr;
643 struct ib_uverbs_cmd_hdr hdr;
644 struct uverbs_attr_bundle bundle;
648 if (!ib_safe_file_access(filp)) {
649 pr_err_once("uverbs_write: process %d (%s) changed security contexts after opening file descriptor, this is not allowed.\n",
650 task_tgid_vnr(current), current->comm);
654 if (count < sizeof(hdr))
657 if (copy_from_user(&hdr, buf, sizeof(hdr)))
660 method_elm = uapi_get_method(uapi, hdr.command);
661 if (IS_ERR(method_elm))
662 return PTR_ERR(method_elm);
664 if (method_elm->is_ex) {
665 if (count < (sizeof(hdr) + sizeof(ex_hdr)))
667 if (copy_from_user(&ex_hdr, buf + sizeof(hdr), sizeof(ex_hdr)))
671 ret = verify_hdr(&hdr, &ex_hdr, count, method_elm);
675 srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
679 memset(bundle.attr_present, 0, sizeof(bundle.attr_present));
681 bundle.context = NULL; /* only valid if bundle has uobject */
682 if (!method_elm->is_ex) {
683 size_t in_len = hdr.in_words * 4 - sizeof(hdr);
684 size_t out_len = hdr.out_words * 4;
687 if (method_elm->has_udata) {
688 bundle.driver_udata.inlen =
689 in_len - method_elm->req_size;
690 in_len = method_elm->req_size;
691 if (bundle.driver_udata.inlen)
692 bundle.driver_udata.inbuf = buf + in_len;
694 bundle.driver_udata.inbuf = NULL;
696 memset(&bundle.driver_udata, 0,
697 sizeof(bundle.driver_udata));
700 if (method_elm->has_resp) {
702 * The macros check that if has_resp is set
703 * then the command request structure starts
704 * with a '__aligned u64 response' member.
706 ret = get_user(response, (const u64 __user *)buf);
710 if (method_elm->has_udata) {
711 bundle.driver_udata.outlen =
712 out_len - method_elm->resp_size;
713 out_len = method_elm->resp_size;
714 if (bundle.driver_udata.outlen)
715 bundle.driver_udata.outbuf =
716 u64_to_user_ptr(response +
719 bundle.driver_udata.outbuf = NULL;
722 bundle.driver_udata.outlen = 0;
723 bundle.driver_udata.outbuf = NULL;
726 ib_uverbs_init_udata_buf_or_null(
727 &bundle.ucore, buf, u64_to_user_ptr(response),
730 buf += sizeof(ex_hdr);
732 ib_uverbs_init_udata_buf_or_null(&bundle.ucore, buf,
733 u64_to_user_ptr(ex_hdr.response),
734 hdr.in_words * 8, hdr.out_words * 8);
736 ib_uverbs_init_udata_buf_or_null(
737 &bundle.driver_udata, buf + bundle.ucore.inlen,
738 u64_to_user_ptr(ex_hdr.response) + bundle.ucore.outlen,
739 ex_hdr.provider_in_words * 8,
740 ex_hdr.provider_out_words * 8);
744 ret = method_elm->handler(&bundle);
746 srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
747 return (ret) ? : count;
750 static const struct vm_operations_struct rdma_umap_ops;
752 static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma)
754 struct ib_uverbs_file *file = filp->private_data;
755 struct ib_ucontext *ucontext;
759 srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
760 ucontext = ib_uverbs_get_ucontext_file(file);
761 if (IS_ERR(ucontext)) {
762 ret = PTR_ERR(ucontext);
765 vma->vm_ops = &rdma_umap_ops;
766 ret = ucontext->device->ops.mmap(ucontext, vma);
768 srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
773 * The VMA has been dup'd, initialize the vm_private_data with a new tracking
776 static void rdma_umap_open(struct vm_area_struct *vma)
778 struct ib_uverbs_file *ufile = vma->vm_file->private_data;
779 struct rdma_umap_priv *opriv = vma->vm_private_data;
780 struct rdma_umap_priv *priv;
785 /* We are racing with disassociation */
786 if (!down_read_trylock(&ufile->hw_destroy_rwsem))
789 * Disassociation already completed, the VMA should already be zapped.
791 if (!ufile->ucontext)
794 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
797 rdma_umap_priv_init(priv, vma, opriv->entry);
799 up_read(&ufile->hw_destroy_rwsem);
803 up_read(&ufile->hw_destroy_rwsem);
806 * We can't allow the VMA to be created with the actual IO pages, that
807 * would break our API contract, and it can't be stopped at this
810 vma->vm_private_data = NULL;
811 zap_vma_ptes(vma, vma->vm_start, vma->vm_end - vma->vm_start);
814 static void rdma_umap_close(struct vm_area_struct *vma)
816 struct ib_uverbs_file *ufile = vma->vm_file->private_data;
817 struct rdma_umap_priv *priv = vma->vm_private_data;
823 * The vma holds a reference on the struct file that created it, which
824 * in turn means that the ib_uverbs_file is guaranteed to exist at
827 mutex_lock(&ufile->umap_lock);
829 rdma_user_mmap_entry_put(priv->entry);
831 list_del(&priv->list);
832 mutex_unlock(&ufile->umap_lock);
837 * Once the zap_vma_ptes has been called touches to the VMA will come here and
838 * we return a dummy writable zero page for all the pfns.
840 static vm_fault_t rdma_umap_fault(struct vm_fault *vmf)
842 struct ib_uverbs_file *ufile = vmf->vma->vm_file->private_data;
843 struct rdma_umap_priv *priv = vmf->vma->vm_private_data;
847 return VM_FAULT_SIGBUS;
849 /* Read only pages can just use the system zero page. */
850 if (!(vmf->vma->vm_flags & (VM_WRITE | VM_MAYWRITE))) {
851 vmf->page = ZERO_PAGE(vmf->address);
856 mutex_lock(&ufile->umap_lock);
857 if (!ufile->disassociate_page)
858 ufile->disassociate_page =
859 alloc_pages(vmf->gfp_mask | __GFP_ZERO, 0);
861 if (ufile->disassociate_page) {
863 * This VMA is forced to always be shared so this doesn't have
864 * to worry about COW.
866 vmf->page = ufile->disassociate_page;
869 ret = VM_FAULT_SIGBUS;
871 mutex_unlock(&ufile->umap_lock);
876 static const struct vm_operations_struct rdma_umap_ops = {
877 .open = rdma_umap_open,
878 .close = rdma_umap_close,
879 .fault = rdma_umap_fault,
882 void uverbs_user_mmap_disassociate(struct ib_uverbs_file *ufile)
884 struct rdma_umap_priv *priv, *next_priv;
886 lockdep_assert_held(&ufile->hw_destroy_rwsem);
889 struct mm_struct *mm = NULL;
891 /* Get an arbitrary mm pointer that hasn't been cleaned yet */
892 mutex_lock(&ufile->umap_lock);
893 while (!list_empty(&ufile->umaps)) {
896 priv = list_first_entry(&ufile->umaps,
897 struct rdma_umap_priv, list);
898 mm = priv->vma->vm_mm;
899 ret = mmget_not_zero(mm);
901 list_del_init(&priv->list);
907 mutex_unlock(&ufile->umap_lock);
912 * The umap_lock is nested under mmap_sem since it used within
913 * the vma_ops callbacks, so we have to clean the list one mm
914 * at a time to get the lock ordering right. Typically there
915 * will only be one mm, so no big deal.
917 down_read(&mm->mmap_sem);
918 if (!mmget_still_valid(mm))
920 mutex_lock(&ufile->umap_lock);
921 list_for_each_entry_safe (priv, next_priv, &ufile->umaps,
923 struct vm_area_struct *vma = priv->vma;
925 if (vma->vm_mm != mm)
927 list_del_init(&priv->list);
929 zap_vma_ptes(vma, vma->vm_start,
930 vma->vm_end - vma->vm_start);
933 rdma_user_mmap_entry_put(priv->entry);
937 mutex_unlock(&ufile->umap_lock);
939 up_read(&mm->mmap_sem);
945 * ib_uverbs_open() does not need the BKL:
947 * - the ib_uverbs_device structures are properly reference counted and
948 * everything else is purely local to the file being created, so
949 * races against other open calls are not a problem;
950 * - there is no ioctl method to race against;
951 * - the open method will either immediately run -ENXIO, or all
952 * required initialization will be done.
954 static int ib_uverbs_open(struct inode *inode, struct file *filp)
956 struct ib_uverbs_device *dev;
957 struct ib_uverbs_file *file;
958 struct ib_device *ib_dev;
960 int module_dependent;
963 dev = container_of(inode->i_cdev, struct ib_uverbs_device, cdev);
964 if (!atomic_inc_not_zero(&dev->refcount))
967 get_device(&dev->dev);
968 srcu_key = srcu_read_lock(&dev->disassociate_srcu);
969 mutex_lock(&dev->lists_mutex);
970 ib_dev = srcu_dereference(dev->ib_dev,
971 &dev->disassociate_srcu);
977 if (!rdma_dev_access_netns(ib_dev, current->nsproxy->net_ns)) {
982 /* In case IB device supports disassociate ucontext, there is no hard
983 * dependency between uverbs device and its low level device.
985 module_dependent = !(ib_dev->ops.disassociate_ucontext);
987 if (module_dependent) {
988 if (!try_module_get(ib_dev->ops.owner)) {
994 file = kzalloc(sizeof(*file), GFP_KERNEL);
997 if (module_dependent)
1004 kref_init(&file->ref);
1005 mutex_init(&file->ucontext_lock);
1007 spin_lock_init(&file->uobjects_lock);
1008 INIT_LIST_HEAD(&file->uobjects);
1009 init_rwsem(&file->hw_destroy_rwsem);
1010 mutex_init(&file->umap_lock);
1011 INIT_LIST_HEAD(&file->umaps);
1013 filp->private_data = file;
1014 list_add_tail(&file->list, &dev->uverbs_file_list);
1015 mutex_unlock(&dev->lists_mutex);
1016 srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
1018 setup_ufile_idr_uobject(file);
1020 return stream_open(inode, filp);
1023 module_put(ib_dev->ops.owner);
1026 mutex_unlock(&dev->lists_mutex);
1027 srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
1028 if (atomic_dec_and_test(&dev->refcount))
1029 ib_uverbs_comp_dev(dev);
1031 put_device(&dev->dev);
1035 static int ib_uverbs_close(struct inode *inode, struct file *filp)
1037 struct ib_uverbs_file *file = filp->private_data;
1039 uverbs_destroy_ufile_hw(file, RDMA_REMOVE_CLOSE);
1041 mutex_lock(&file->device->lists_mutex);
1042 list_del_init(&file->list);
1043 mutex_unlock(&file->device->lists_mutex);
1045 kref_put(&file->ref, ib_uverbs_release_file);
1050 static const struct file_operations uverbs_fops = {
1051 .owner = THIS_MODULE,
1052 .write = ib_uverbs_write,
1053 .open = ib_uverbs_open,
1054 .release = ib_uverbs_close,
1055 .llseek = no_llseek,
1056 .unlocked_ioctl = ib_uverbs_ioctl,
1057 .compat_ioctl = compat_ptr_ioctl,
1060 static const struct file_operations uverbs_mmap_fops = {
1061 .owner = THIS_MODULE,
1062 .write = ib_uverbs_write,
1063 .mmap = ib_uverbs_mmap,
1064 .open = ib_uverbs_open,
1065 .release = ib_uverbs_close,
1066 .llseek = no_llseek,
1067 .unlocked_ioctl = ib_uverbs_ioctl,
1068 .compat_ioctl = compat_ptr_ioctl,
1071 static int ib_uverbs_get_nl_info(struct ib_device *ibdev, void *client_data,
1072 struct ib_client_nl_info *res)
1074 struct ib_uverbs_device *uverbs_dev = client_data;
1077 if (res->port != -1)
1080 res->abi = ibdev->ops.uverbs_abi_ver;
1081 res->cdev = &uverbs_dev->dev;
1084 * To support DRIVER_ID binding in userspace some of the driver need
1085 * upgrading to expose their PCI dependent revision information
1086 * through get_context instead of relying on modalias matching. When
1087 * the drivers are fixed they can drop this flag.
1089 if (!ibdev->ops.uverbs_no_driver_id_binding) {
1090 ret = nla_put_u32(res->nl_msg, RDMA_NLDEV_ATTR_UVERBS_DRIVER_ID,
1091 ibdev->ops.driver_id);
1098 static struct ib_client uverbs_client = {
1100 .no_kverbs_req = true,
1101 .add = ib_uverbs_add_one,
1102 .remove = ib_uverbs_remove_one,
1103 .get_nl_info = ib_uverbs_get_nl_info,
1105 MODULE_ALIAS_RDMA_CLIENT("uverbs");
1107 static ssize_t ibdev_show(struct device *device, struct device_attribute *attr,
1110 struct ib_uverbs_device *dev =
1111 container_of(device, struct ib_uverbs_device, dev);
1114 struct ib_device *ib_dev;
1116 srcu_key = srcu_read_lock(&dev->disassociate_srcu);
1117 ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
1119 ret = sprintf(buf, "%s\n", dev_name(&ib_dev->dev));
1120 srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
1124 static DEVICE_ATTR_RO(ibdev);
1126 static ssize_t abi_version_show(struct device *device,
1127 struct device_attribute *attr, char *buf)
1129 struct ib_uverbs_device *dev =
1130 container_of(device, struct ib_uverbs_device, dev);
1133 struct ib_device *ib_dev;
1135 srcu_key = srcu_read_lock(&dev->disassociate_srcu);
1136 ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
1138 ret = sprintf(buf, "%u\n", ib_dev->ops.uverbs_abi_ver);
1139 srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
1143 static DEVICE_ATTR_RO(abi_version);
1145 static struct attribute *ib_dev_attrs[] = {
1146 &dev_attr_abi_version.attr,
1147 &dev_attr_ibdev.attr,
1151 static const struct attribute_group dev_attr_group = {
1152 .attrs = ib_dev_attrs,
1155 static CLASS_ATTR_STRING(abi_version, S_IRUGO,
1156 __stringify(IB_USER_VERBS_ABI_VERSION));
1158 static int ib_uverbs_create_uapi(struct ib_device *device,
1159 struct ib_uverbs_device *uverbs_dev)
1161 struct uverbs_api *uapi;
1163 uapi = uverbs_alloc_api(device);
1165 return PTR_ERR(uapi);
1167 uverbs_dev->uapi = uapi;
1171 static void ib_uverbs_add_one(struct ib_device *device)
1175 struct ib_uverbs_device *uverbs_dev;
1178 if (!device->ops.alloc_ucontext)
1181 uverbs_dev = kzalloc(sizeof(*uverbs_dev), GFP_KERNEL);
1185 ret = init_srcu_struct(&uverbs_dev->disassociate_srcu);
1191 device_initialize(&uverbs_dev->dev);
1192 uverbs_dev->dev.class = uverbs_class;
1193 uverbs_dev->dev.parent = device->dev.parent;
1194 uverbs_dev->dev.release = ib_uverbs_release_dev;
1195 uverbs_dev->groups[0] = &dev_attr_group;
1196 uverbs_dev->dev.groups = uverbs_dev->groups;
1197 atomic_set(&uverbs_dev->refcount, 1);
1198 init_completion(&uverbs_dev->comp);
1199 uverbs_dev->xrcd_tree = RB_ROOT;
1200 mutex_init(&uverbs_dev->xrcd_tree_mutex);
1201 mutex_init(&uverbs_dev->lists_mutex);
1202 INIT_LIST_HEAD(&uverbs_dev->uverbs_file_list);
1203 INIT_LIST_HEAD(&uverbs_dev->uverbs_events_file_list);
1204 rcu_assign_pointer(uverbs_dev->ib_dev, device);
1205 uverbs_dev->num_comp_vectors = device->num_comp_vectors;
1207 devnum = ida_alloc_max(&uverbs_ida, IB_UVERBS_MAX_DEVICES - 1,
1211 uverbs_dev->devnum = devnum;
1212 if (devnum >= IB_UVERBS_NUM_FIXED_MINOR)
1213 base = dynamic_uverbs_dev + devnum - IB_UVERBS_NUM_FIXED_MINOR;
1215 base = IB_UVERBS_BASE_DEV + devnum;
1217 if (ib_uverbs_create_uapi(device, uverbs_dev))
1220 uverbs_dev->dev.devt = base;
1221 dev_set_name(&uverbs_dev->dev, "uverbs%d", uverbs_dev->devnum);
1223 cdev_init(&uverbs_dev->cdev,
1224 device->ops.mmap ? &uverbs_mmap_fops : &uverbs_fops);
1225 uverbs_dev->cdev.owner = THIS_MODULE;
1227 ret = cdev_device_add(&uverbs_dev->cdev, &uverbs_dev->dev);
1231 ib_set_client_data(device, &uverbs_client, uverbs_dev);
1235 ida_free(&uverbs_ida, devnum);
1237 if (atomic_dec_and_test(&uverbs_dev->refcount))
1238 ib_uverbs_comp_dev(uverbs_dev);
1239 wait_for_completion(&uverbs_dev->comp);
1240 put_device(&uverbs_dev->dev);
1244 static void ib_uverbs_free_hw_resources(struct ib_uverbs_device *uverbs_dev,
1245 struct ib_device *ib_dev)
1247 struct ib_uverbs_file *file;
1248 struct ib_uverbs_async_event_file *event_file;
1249 struct ib_event event;
1251 /* Pending running commands to terminate */
1252 uverbs_disassociate_api_pre(uverbs_dev);
1253 event.event = IB_EVENT_DEVICE_FATAL;
1254 event.element.port_num = 0;
1255 event.device = ib_dev;
1257 mutex_lock(&uverbs_dev->lists_mutex);
1258 while (!list_empty(&uverbs_dev->uverbs_file_list)) {
1259 file = list_first_entry(&uverbs_dev->uverbs_file_list,
1260 struct ib_uverbs_file, list);
1261 list_del_init(&file->list);
1262 kref_get(&file->ref);
1264 /* We must release the mutex before going ahead and calling
1265 * uverbs_cleanup_ufile, as it might end up indirectly calling
1266 * uverbs_close, for example due to freeing the resources (e.g
1269 mutex_unlock(&uverbs_dev->lists_mutex);
1271 ib_uverbs_event_handler(&file->event_handler, &event);
1272 uverbs_destroy_ufile_hw(file, RDMA_REMOVE_DRIVER_REMOVE);
1273 kref_put(&file->ref, ib_uverbs_release_file);
1275 mutex_lock(&uverbs_dev->lists_mutex);
1278 while (!list_empty(&uverbs_dev->uverbs_events_file_list)) {
1279 event_file = list_first_entry(&uverbs_dev->
1280 uverbs_events_file_list,
1281 struct ib_uverbs_async_event_file,
1283 spin_lock_irq(&event_file->ev_queue.lock);
1284 event_file->ev_queue.is_closed = 1;
1285 spin_unlock_irq(&event_file->ev_queue.lock);
1287 list_del(&event_file->list);
1288 ib_unregister_event_handler(
1289 &event_file->uverbs_file->event_handler);
1290 event_file->uverbs_file->event_handler.device =
1293 wake_up_interruptible(&event_file->ev_queue.poll_wait);
1294 kill_fasync(&event_file->ev_queue.async_queue, SIGIO, POLL_IN);
1296 mutex_unlock(&uverbs_dev->lists_mutex);
1298 uverbs_disassociate_api(uverbs_dev->uapi);
1301 static void ib_uverbs_remove_one(struct ib_device *device, void *client_data)
1303 struct ib_uverbs_device *uverbs_dev = client_data;
1304 int wait_clients = 1;
1309 cdev_device_del(&uverbs_dev->cdev, &uverbs_dev->dev);
1310 ida_free(&uverbs_ida, uverbs_dev->devnum);
1312 if (device->ops.disassociate_ucontext) {
1313 /* We disassociate HW resources and immediately return.
1314 * Userspace will see a EIO errno for all future access.
1315 * Upon returning, ib_device may be freed internally and is not
1317 * uverbs_device is still available until all clients close
1318 * their files, then the uverbs device ref count will be zero
1319 * and its resources will be freed.
1320 * Note: At this point no more files can be opened since the
1321 * cdev was deleted, however active clients can still issue
1322 * commands and close their open files.
1324 ib_uverbs_free_hw_resources(uverbs_dev, device);
1328 if (atomic_dec_and_test(&uverbs_dev->refcount))
1329 ib_uverbs_comp_dev(uverbs_dev);
1331 wait_for_completion(&uverbs_dev->comp);
1333 put_device(&uverbs_dev->dev);
1336 static char *uverbs_devnode(struct device *dev, umode_t *mode)
1340 return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
1343 static int __init ib_uverbs_init(void)
1347 ret = register_chrdev_region(IB_UVERBS_BASE_DEV,
1348 IB_UVERBS_NUM_FIXED_MINOR,
1349 "infiniband_verbs");
1351 pr_err("user_verbs: couldn't register device number\n");
1355 ret = alloc_chrdev_region(&dynamic_uverbs_dev, 0,
1356 IB_UVERBS_NUM_DYNAMIC_MINOR,
1357 "infiniband_verbs");
1359 pr_err("couldn't register dynamic device number\n");
1363 uverbs_class = class_create(THIS_MODULE, "infiniband_verbs");
1364 if (IS_ERR(uverbs_class)) {
1365 ret = PTR_ERR(uverbs_class);
1366 pr_err("user_verbs: couldn't create class infiniband_verbs\n");
1370 uverbs_class->devnode = uverbs_devnode;
1372 ret = class_create_file(uverbs_class, &class_attr_abi_version.attr);
1374 pr_err("user_verbs: couldn't create abi_version attribute\n");
1378 ret = ib_register_client(&uverbs_client);
1380 pr_err("user_verbs: couldn't register client\n");
1387 class_destroy(uverbs_class);
1390 unregister_chrdev_region(dynamic_uverbs_dev,
1391 IB_UVERBS_NUM_DYNAMIC_MINOR);
1394 unregister_chrdev_region(IB_UVERBS_BASE_DEV,
1395 IB_UVERBS_NUM_FIXED_MINOR);
1401 static void __exit ib_uverbs_cleanup(void)
1403 ib_unregister_client(&uverbs_client);
1404 class_destroy(uverbs_class);
1405 unregister_chrdev_region(IB_UVERBS_BASE_DEV,
1406 IB_UVERBS_NUM_FIXED_MINOR);
1407 unregister_chrdev_region(dynamic_uverbs_dev,
1408 IB_UVERBS_NUM_DYNAMIC_MINOR);
1409 mmu_notifier_synchronize();
1412 module_init(ib_uverbs_init);
1413 module_exit(ib_uverbs_cleanup);